~ghugesss/xpad/debug_support

« back to all changes in this revision

Viewing changes to src/xpad-settings.c

  • Committer: Arthur Borsboom
  • Date: 2014-06-19 13:15:30 UTC
  • mfrom: (665.1.59 xpad-4.3)
  • Revision ID: arthurborsboom@gmail.com-20140619131530-ecsvweog8592zqie
Fix: prevented color updating conflict between preferences and pad colors
Fix: Technical - reduced code by removing double code
Fix: Technical - ensured that all the menus of each pad are updated when changing options in the View menu
Fix: renamed Xpad_periodic functions to lowercase in line with other function namings
Fix: repaired showing all the notes in the menu: pad->menu->notes
Fix: Technical - removed unnecessary initialization and unnecessary function
Fix: Resolved a disposing error of the clipboard contents after erasing a pad
Fix: Pad properties - switched foreground and background order in GUI.
Fix: Technical - got rid of the global variable: xpad_global_settings
New: added shortcuts keys for New pad (CTRL-N) and Delete (SHIFT-DEL)
Fix: Technical - changed comment style
Fix: replaced get user home dir with specific glib function
Fix: Default values are set correctly on first startup (no configuration file) by reintroducing a double default value administration.
Fix: Improved performance by reducing the amount of GTK_ casts, as suggested in the GTK best practices
Fix: Applied GTK best practices for inclusion of 1st config.h and 2nd its header file in each c-file

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 
20
20
*/
21
21
 
 
22
#include "../config.h"
 
23
#include "xpad-settings.h"
22
24
#include <glib/gi18n.h>
23
25
#include <gtk/gtk.h>
24
26
#include <string.h>
25
27
#include "fio.h"
26
 
#include "xpad-settings.h"
27
28
#include "xpad-app.h"
28
29
 
29
30
struct XpadSettingsPrivate
49
50
        guint autostart_display_pads;
50
51
};
51
52
 
52
 
G_DEFINE_TYPE_WITH_PRIVATE(XpadSettings, xpad_settings, G_TYPE_OBJECT)
 
53
G_DEFINE_TYPE_WITH_PRIVATE (XpadSettings, xpad_settings, G_TYPE_OBJECT)
53
54
 
54
55
#define DEFAULTS_FILENAME       "default-style"
55
56
 
81
82
  PROP_AUTOSTART_NEW_PAD,
82
83
  PROP_AUTOSTART_STICKY,
83
84
  PROP_AUTOSTART_DISPLAY_PADS,
84
 
  LAST_PROP
 
85
  N_PROPERTIES
85
86
};
86
87
 
 
88
static GParamSpec *obj_prop[N_PROPERTIES] = { NULL, };
 
89
static guint signals[LAST_SIGNAL] = { 0 };
 
90
 
87
91
static void load_from_file (XpadSettings *settings, const gchar *filename);
88
92
static void save_to_file (XpadSettings *settings, const gchar *filename);
89
93
static void xpad_settings_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
90
94
static void xpad_settings_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
91
 
static void xpad_settings_dispose (GObject *object);
92
95
static void xpad_settings_finalize (GObject *object);
93
96
 
94
 
static guint signals[LAST_SIGNAL] = { 0 };
95
 
 
96
97
XpadSettings *
97
98
xpad_settings_new (void)
98
99
{
104
105
{
105
106
        GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
106
107
 
107
 
        gobject_class->dispose = xpad_settings_dispose;
108
108
        gobject_class->finalize = xpad_settings_finalize;
109
109
        gobject_class->set_property = xpad_settings_set_property;
110
110
        gobject_class->get_property = xpad_settings_get_property;
111
111
        
112
 
        g_object_class_install_property (gobject_class,
113
 
                                         PROP_WIDTH,
114
 
                                         g_param_spec_uint ("width",
115
 
                                                            "Default Width of Pads",
116
 
                                                            "Window width of pads on creation",
117
 
                                                            0,
118
 
                                                            G_MAXUINT,
119
 
                                                            200,
120
 
                                                            G_PARAM_READWRITE));
121
 
        
122
 
        g_object_class_install_property (gobject_class,
123
 
                                         PROP_HEIGHT,
124
 
                                         g_param_spec_uint ("height",
125
 
                                                            "Default Height of Pads",
126
 
                                                            "Window height of pads on creation",
127
 
                                                            0,
128
 
                                                            G_MAXUINT,
129
 
                                                            200,
130
 
                                                            G_PARAM_READWRITE));
131
 
        
132
 
        g_object_class_install_property (gobject_class,
133
 
                                         PROP_HAS_DECORATIONS,
134
 
                                         g_param_spec_boolean ("has-decorations",
135
 
                                                               "Has Decorations",
136
 
                                                               "Whether pads have window decorations",
137
 
                                                               TRUE,
138
 
                                                               G_PARAM_READWRITE));
139
 
        
140
 
        g_object_class_install_property (gobject_class,
141
 
                                         PROP_CONFIRM_DESTROY,
142
 
                                         g_param_spec_boolean ("confirm-destroy",
143
 
                                                               "Confirm Destroy",
144
 
                                                               "Whether destroying a pad requires user confirmation",
145
 
                                                               TRUE,
146
 
                                                               G_PARAM_READWRITE));
147
 
        
148
 
        g_object_class_install_property (gobject_class,
149
 
                                         PROP_EDIT_LOCK,
150
 
                                         g_param_spec_boolean ("edit-lock",
151
 
                                                               "Edit Lock",
152
 
                                                               "Whether edit lock mode is enabled",
153
 
                                                               FALSE,
154
 
                                                               G_PARAM_READWRITE));
155
 
        g_object_class_install_property (gobject_class,
156
 
                                         PROP_TRAY_ENABLED,
157
 
                                         g_param_spec_boolean ("tray-enabled",
158
 
                                                               "Enable the tray icon",
159
 
                                                               "Whether to enable or disable the systray icon",
160
 
                                                               TRUE,
161
 
                                                               G_PARAM_READWRITE));
162
 
 
163
 
        g_object_class_install_property (gobject_class,
164
 
                                     PROP_TRAY_CLICK_CONFIGURATION,
165
 
                                     g_param_spec_uint ("tray-click-configuration",
166
 
                                                        "Tray click configuration",
167
 
                                                        "What configuration is selected on tray click",
168
 
                                                        0,
169
 
                                                        G_MAXUINT,
170
 
                                                        0,
171
 
                                                        G_PARAM_READWRITE));
172
 
        g_object_class_install_property (gobject_class,
173
 
                                         PROP_HAS_TOOLBAR,
174
 
                                         g_param_spec_boolean ("has-toolbar",
175
 
                                                               "Each pad has a toolbar",
176
 
                                                               "Whether pads have toolbars",
177
 
                                                               TRUE,
178
 
                                                               G_PARAM_READWRITE));
179
 
        
180
 
        g_object_class_install_property (gobject_class,
181
 
                                         PROP_AUTOHIDE_TOOLBAR,
182
 
                                         g_param_spec_boolean ("autohide-toolbar",
183
 
                                                               "Autohide Toolbar",
184
 
                                                               "Whether toolbars hide when not used",
185
 
                                                               TRUE,
186
 
                                                               G_PARAM_READWRITE));
187
 
        
188
 
        g_object_class_install_property (gobject_class,
189
 
                                         PROP_HAS_SCROLLBAR,
190
 
                                         g_param_spec_boolean ("has-scrollbar",
191
 
                                                               "Has Scrollbar",
192
 
                                                               "Whether pads have scrollbars",
193
 
                                                               TRUE,
194
 
                                                               G_PARAM_READWRITE));
195
 
        
196
 
        g_object_class_install_property (gobject_class,
197
 
                                         PROP_FONTNAME,
198
 
                                         g_param_spec_string ("fontname",
199
 
                                                              "Font Name",
200
 
                                                              "Default name of pad font",
201
 
                                                              NULL,
202
 
                                                              G_PARAM_READWRITE));
203
 
        
204
 
        g_object_class_install_property (gobject_class,
205
 
                                         PROP_TEXT_COLOR,
206
 
                                         g_param_spec_boxed ("text-color",
207
 
                                                             "Text Color",
208
 
                                                             "Default color of pad text",
209
 
                                                             GDK_TYPE_RGBA,
210
 
                                                             G_PARAM_READWRITE));
211
 
        
212
 
        g_object_class_install_property (gobject_class,
213
 
                                         PROP_BACK_COLOR,
214
 
                                         g_param_spec_boxed ("back-color",
215
 
                                                             "Back Color",
216
 
                                                             "Default color of pad background",
217
 
                                                             GDK_TYPE_RGBA,
218
 
                                                             G_PARAM_READWRITE));
219
 
 
220
 
        g_object_class_install_property (gobject_class,
221
 
                                         PROP_AUTOSTART_XPAD,
222
 
                                         g_param_spec_boolean ("autostart-xpad",
223
 
                                                               "Automatically start xpad",
224
 
                                                               "Whether to start xpad after login",
225
 
                                                               FALSE,
226
 
                                                               G_PARAM_READWRITE));
227
 
 
228
 
        g_object_class_install_property (gobject_class,
229
 
                                         PROP_AUTOSTART_WAIT_SYSTRAY,
230
 
                                         g_param_spec_boolean ("autostart-wait-systray",
231
 
                                                               "Autostart Xpad wait for systray",
232
 
                                                               "Whether to wait for the systray before starting xpad automatically after login",
233
 
                                                               TRUE,
234
 
                                                               G_PARAM_READWRITE));
235
 
 
236
 
        g_object_class_install_property (gobject_class,
237
 
                                         PROP_AUTOSTART_NEW_PAD,
238
 
                                         g_param_spec_boolean ("autostart-new-pad",
239
 
                                                               "Autostart a new pad",
240
 
                                                               "Whether to create a new pad on startup",
241
 
                                                               FALSE,
242
 
                                                               G_PARAM_READWRITE));
243
 
        
244
 
        g_object_class_install_property (gobject_class,
245
 
                                         PROP_AUTOSTART_STICKY,
246
 
                                         g_param_spec_boolean ("autostart-sticky",
247
 
                                                               "Default Stickiness",
248
 
                                                               "Whether pads are sticky on creation",
249
 
                                                               FALSE,
250
 
                                                               G_PARAM_READWRITE));
251
 
        
252
 
        g_object_class_install_property (gobject_class,
253
 
                                     PROP_AUTOSTART_DELAY,
254
 
                                     g_param_spec_uint ("autostart-delay",
255
 
                                                        "Delay autostart of Xpad",
256
 
                                                        "How many seconds will Xpad wait before continuing startup",
257
 
                                                        0,
258
 
                                                        G_MAXUINT,
259
 
                                                        0,
260
 
                                                        G_PARAM_READWRITE));
261
 
 
262
 
        g_object_class_install_property (gobject_class,
263
 
                                     PROP_AUTOSTART_DISPLAY_PADS,
264
 
                                     g_param_spec_uint ("autostart-display-pads",
265
 
                                                        "Autostart display pads",
266
 
                                                        "How to show the different pads when Xpad is started",
267
 
                                                        0,
268
 
                                                        G_MAXUINT,
269
 
                                                        2,
270
 
                                                        G_PARAM_READWRITE));
271
 
 
272
 
        signals[CHANGE_BUTTONS] = 
273
 
                g_signal_new ("change_buttons",
274
 
                              G_OBJECT_CLASS_TYPE (gobject_class),
275
 
                              G_SIGNAL_RUN_LAST,
276
 
                              G_STRUCT_OFFSET (XpadSettingsClass, change_buttons),
277
 
                              NULL, NULL,
278
 
                              g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
 
112
        obj_prop[PROP_WIDTH]                                    = g_param_spec_uint ("width", "Default width of pads", "Window width of pads on creation", 0, G_MAXUINT, 200, G_PARAM_READWRITE);
 
113
        obj_prop[PROP_HEIGHT]                                   = g_param_spec_uint ("height", "Default height of pads", "Window height of pads on creation", 0, G_MAXUINT, 200, G_PARAM_READWRITE);
 
114
        obj_prop[PROP_HAS_DECORATIONS]                  = g_param_spec_boolean ("has-decorations", "Has decorations", "Whether pads have window decorations", TRUE, G_PARAM_READWRITE);
 
115
        obj_prop[PROP_CONFIRM_DESTROY]                  = g_param_spec_boolean ("confirm-destroy", "Confirm destroy", "Ask for delete confirmation", TRUE, G_PARAM_READWRITE);
 
116
        obj_prop[PROP_EDIT_LOCK]                                = g_param_spec_boolean ("edit-lock", "Edit lock", "Toggle read-only mode", FALSE, G_PARAM_READWRITE);
 
117
        obj_prop[PROP_TRAY_ENABLED]                             = g_param_spec_boolean ("tray-enabled", "Enable tray icon", "Enable or disable the systray icon", TRUE, G_PARAM_READWRITE);
 
118
        obj_prop[PROP_TRAY_CLICK_CONFIGURATION] = g_param_spec_uint ("tray-click-configuration", "Tray click configuration", "Configure tray left click", 0, G_MAXUINT, 0, G_PARAM_READWRITE);
 
119
        obj_prop[PROP_HAS_TOOLBAR]                              = g_param_spec_boolean ("has-toolbar", "Has toolbar", "Whether pads have toolbars", TRUE, G_PARAM_READWRITE);
 
120
        obj_prop[PROP_AUTOHIDE_TOOLBAR]                 = g_param_spec_boolean ("autohide-toolbar", "Autohide toolbar", "Hide toolbars when not used", TRUE, G_PARAM_READWRITE);
 
121
        obj_prop[PROP_HAS_SCROLLBAR]                    = g_param_spec_boolean ("has-scrollbar", "Has scrollbar", "Whether pads have scrollbars", TRUE, G_PARAM_READWRITE);
 
122
        obj_prop[PROP_FONTNAME]                                 = g_param_spec_string ("fontname", "Font name", "Default name of pad font", NULL, G_PARAM_READWRITE);
 
123
        obj_prop[PROP_TEXT_COLOR]                               = g_param_spec_boxed ("text-color", "Text color", "Default color of pad text", GDK_TYPE_RGBA, G_PARAM_READWRITE);
 
124
        obj_prop[PROP_BACK_COLOR]                               = g_param_spec_boxed ("back-color", "Back color", "Default color of pad background", GDK_TYPE_RGBA, G_PARAM_READWRITE);
 
125
        obj_prop[PROP_AUTOSTART_XPAD]                   = g_param_spec_boolean ("autostart-xpad", "Automatically start xpad", "Start Xpad after login", FALSE, G_PARAM_READWRITE);
 
126
        obj_prop[PROP_AUTOSTART_WAIT_SYSTRAY]   = g_param_spec_boolean ("autostart-wait-systray", "Wait for systray", "Whether to wait for the systray after login", TRUE, G_PARAM_READWRITE);
 
127
        obj_prop[PROP_AUTOSTART_NEW_PAD]                = g_param_spec_boolean ("autostart-new-pad", "Start a new pad", "Whether to create a new pad on startup", FALSE, G_PARAM_READWRITE);
 
128
        obj_prop[PROP_AUTOSTART_STICKY]                 = g_param_spec_boolean ("autostart-sticky", "Stick to desktop", "Whether pads are sticky on creation", FALSE, G_PARAM_READWRITE);
 
129
        obj_prop[PROP_AUTOSTART_DELAY]                  = g_param_spec_uint ("autostart-delay", "Delay autostart of Xpad", "Number of seconds to wait before start of Xpad", 0, G_MAXUINT, 0, G_PARAM_READWRITE);
 
130
        obj_prop[PROP_AUTOSTART_DISPLAY_PADS]   = g_param_spec_uint ("autostart-display-pads", "Autostart display pads", "Show/hide/restore pads at start", 0, G_MAXUINT, 2, G_PARAM_READWRITE);
 
131
 
 
132
        g_object_class_install_properties (gobject_class, N_PROPERTIES, obj_prop);
 
133
        
 
134
        signals[CHANGE_BUTTONS] = g_signal_new ("change_buttons", G_OBJECT_CLASS_TYPE (gobject_class), G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (XpadSettingsClass, change_buttons), NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
279
135
}
280
136
 
281
137
static void
282
138
xpad_settings_init (XpadSettings *settings)
283
139
{
284
 
        settings->priv = xpad_settings_get_instance_private(settings);
 
140
        settings->priv = xpad_settings_get_instance_private (settings);
285
141
 
 
142
        /*
 
143
         * FIXME: Although the default values below have been set in the xpad_settings_class_init above,
 
144
         * these default values are not applied to the private variables of this instance.
 
145
         * I haven't found a way to resolve this yet. So, for now we have a double
 
146
         * administration of defaults values. It would be awesome to reduce this to 1 administration.
 
147
         *
 
148
         * Found the explanation, but not a solution:
 
149
         * http://blogs.gnome.org/desrt/2012/02/26/a-gentle-introduction-to-gobject-construction/
 
150
         */
 
151
        settings->priv->width = 200;
 
152
        settings->priv->height = 200;
 
153
        settings->priv->has_decorations = TRUE;
 
154
        settings->priv->confirm_destroy = TRUE;
 
155
        settings->priv->edit_lock = FALSE;
 
156
        settings->priv->tray_enabled = TRUE;
 
157
        settings->priv->tray_click_configuration = 1;
 
158
        settings->priv->has_toolbar = TRUE;
 
159
        settings->priv->autohide_toolbar = TRUE;
 
160
        settings->priv->has_scrollbar = TRUE;
 
161
        settings->priv->fontname = NULL;
286
162
        /* A pleasant light yellow background color, similar to commercial sticky notes, with black text. */
287
163
        settings->priv->text = gdk_rgba_copy(&(GdkRGBA) {0, 0, 0, 1});
288
164
        settings->priv->back = gdk_rgba_copy(&(GdkRGBA) {1, 0.933334350586, 0.6, 1});
 
165
        settings->priv->autostart_wait_systray = TRUE;
 
166
        settings->priv->autostart_new_pad = FALSE;
 
167
        settings->priv->autostart_sticky = FALSE;
 
168
        settings->priv->autostart_delay = 0;
 
169
        settings->priv->autostart_display_pads = 2;
289
170
 
290
171
        settings->priv->toolbar_buttons = NULL;
291
172
        settings->priv->toolbar_buttons = g_slist_append (settings->priv->toolbar_buttons, g_strdup ("New"));
302
183
}
303
184
 
304
185
static void
305
 
xpad_settings_dispose (GObject *object)
306
 
{
307
 
        G_OBJECT_CLASS (xpad_settings_parent_class)->dispose (object);
308
 
}
309
 
 
310
 
static void
311
186
xpad_settings_finalize (GObject *object)
312
187
{
313
188
        XpadSettings *settings = XPAD_SETTINGS (object);
389
264
        
390
265
        switch (prop_id)
391
266
        {
 
267
 
392
268
        case PROP_WIDTH:
393
269
                settings->priv->width = g_value_get_uint (value);
394
270
                break;
395
 
        
 
271
 
396
272
        case PROP_HEIGHT:
397
273
                settings->priv->height = g_value_get_uint (value);
398
274
                break;
536
412
        
537
413
        switch (prop_id)
538
414
        {
 
415
 
539
416
        case PROP_WIDTH:
540
417
                g_value_set_uint (value, settings->priv->width);
541
418
                break;
542
 
        
 
419
 
543
420
        case PROP_HEIGHT:
544
421
                g_value_set_uint (value, settings->priv->height);
545
422
                break;
598
475
                break;
599
476
 
600
477
        case PROP_AUTOSTART_XPAD:
601
 
                g_value_set_boolean (value, g_file_test (g_strdup_printf ("%s/.config/autostart/xpad.desktop", g_getenv ("HOME")), G_FILE_TEST_EXISTS));
 
478
                g_value_set_boolean (value, g_file_test (g_strdup_printf ("%s/.config/autostart/xpad.desktop", g_get_home_dir()), G_FILE_TEST_EXISTS));
602
479
                break;
603
480
 
604
481
        case PROP_AUTOSTART_WAIT_SYSTRAY:
630
507
static void
631
508
load_from_file (XpadSettings *settings, const gchar *filename)
632
509
{
633
 
        gchar *buttons = NULL;
634
 
        gchar *text_color_string = NULL;
635
 
        gchar *background_color_string = NULL;
636
 
        GdkRGBA text = {0, 0, 0, 0};
637
 
        GdkRGBA back = {0, 0, 0, 0};
 
510
        gchar *buttons = NULL, *text_color_string = NULL, *background_color_string = NULL;
 
511
        GdkRGBA text_color = {0, 0, 0, 0}, back_color = {0, 0, 0, 0};
638
512
        gboolean use_text, use_back;
639
513
        
640
514
        use_text = settings->priv->text ? TRUE : FALSE;
641
515
        if (settings->priv->text)
642
 
                text = *settings->priv->text;
 
516
                text_color = *settings->priv->text;
643
517
 
644
518
        use_back = settings->priv->back ? TRUE : FALSE;
645
519
        if (settings->priv->back)
646
 
                back = *settings->priv->back;
 
520
                back_color = *settings->priv->back;
647
521
 
648
522
        /* get all the values from the default-style text file in the forms of booleans, ints or strings. */
649
523
        if (fio_get_values_from_file (filename, 
680
554
                 * set the color to the default.
681
555
                 */
682
556
                if (text_color_string == NULL)
683
 
                        text = (GdkRGBA) {0, 0, 0, 1};
 
557
                        text_color = (GdkRGBA) {0, 0, 0, 1};
684
558
                else
685
559
                        /* If, for some reason, the parsing of the colors fail, set the color to the default. */
686
 
                        if (!gdk_rgba_parse (&text, text_color_string))
687
 
                                text = (GdkRGBA) {0, 0, 0, 1};
 
560
                        if (!gdk_rgba_parse (&text_color, text_color_string))
 
561
                                text_color = (GdkRGBA) {0, 0, 0, 1};
688
562
 
689
 
                settings->priv->text = gdk_rgba_copy (&text);
 
563
                settings->priv->text = gdk_rgba_copy (&text_color);
690
564
        }
691
565
 
692
566
        gdk_rgba_free (settings->priv->back);
697
571
                 * set the color to the default.
698
572
                 */
699
573
                if (background_color_string == NULL)
700
 
                        back = (GdkRGBA) {1, 0.933334350586, 0.6, 1};
 
574
                        back_color = (GdkRGBA) {1, 0.933334350586, 0.6, 1};
701
575
                else
702
576
                        /* If, for some reason, the parsing of the colors fail, set the color to the default. */
703
 
                        if (!gdk_rgba_parse (&back, background_color_string))
704
 
                                back = (GdkRGBA) {1, 0.933334350586, 0.6, 1};
 
577
                        if (!gdk_rgba_parse (&back_color, background_color_string))
 
578
                                back_color = (GdkRGBA) {1, 0.933334350586, 0.6, 1};
705
579
 
706
 
                settings->priv->back = gdk_rgba_copy (&back);
 
580
                settings->priv->back = gdk_rgba_copy (&back_color);
707
581
        }
708
582
        else
709
583
                settings->priv->back = NULL;
736
610
        }
737
611
}
738
612
 
739
 
 
740
613
static void
741
614
save_to_file (XpadSettings *settings, const gchar *filename)
742
615
{