2 * stash.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2008-2012 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
5 * Copyright 2008-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * Lightweight library for reading/writing @c GKeyFile settings and synchronizing widgets with
27 * Note: Stash should only depend on GLib and GTK, but currently has some minor
28 * dependencies on Geany's utils.c.
31 * 'Setting' is used only for data stored on disk or in memory.
32 * 'Pref' can also include visual widget information.
34 * @section Memory Usage
35 * Stash will not duplicate strings if they are normally static arrays, such as
36 * keyfile group names and key names, string default values, widget_id names, property names.
38 * @section String Settings
39 * String settings and other dynamically allocated settings will be initialized to NULL when
40 * added to a StashGroup (so they can safely be reassigned later).
42 * @section Widget Support
43 * Widgets very commonly used in configuration dialogs will be supported with their own function.
44 * Widgets less commonly used such as @c GtkExpander or widget settings that aren't commonly needed
45 * to be persistent won't be directly supported, to keep the library lightweight. However, you can
46 * use stash_group_add_widget_property() to also save these settings for any read/write widget
47 * property. Macros could be added for common widget properties such as @c GtkExpander:"expanded".
49 * @section settings-example Settings Example
50 * Here we have some settings for how to make a cup - whether it should be made of china
51 * and who's going to make it. (Yes, it's a stupid example).
52 * @include stash-example.c
53 * @note You might want to handle the warning/error conditions differently from above.
55 * @section prefs-example GUI Prefs Example
56 * For prefs, it's the same as the above example but you tell Stash to add widget prefs instead of
59 * This example uses lookup strings for widgets as they are more flexible than widget pointers.
60 * Code to load and save the settings is omitted - see the first example instead.
62 * Here we show a dialog with a toggle button for whether the cup should have a handle.
63 * @include stash-gui-example.c
64 * @note This example should also work for other widget containers besides dialogs, e.g. popup menus.
67 /* Implementation Note
68 * We dynamically allocate prefs. It would be more efficient for user code to declare
69 * a static array of StashPref structs, but we don't do this because:
71 * * It would be more ugly (lots of casts and NULLs).
72 * * Less type checking.
73 * * The API & ABI would have to break when adding/changing fields.
75 * Usually the prefs code isn't what user code will spend most of its time doing, so this
76 * should be efficient enough.
80 #include "geany.h" /* necessary for utils.h, otherwise use gtk/gtk.h */
81 #include <stdlib.h> /* only for atoi() */
82 #include "support.h" /* only for _("text") */
83 #include "utils.h" /* only for foreach_*, utils_get_setting_*(). Stash should not depend on Geany. */
88 /* GTK3 removed ComboBoxEntry, but we need a value to differentiate combo box with and
89 * without entries, and it must not collide with other GTypes */
90 #ifdef GTK_TYPE_COMBO_BOX_ENTRY
91 # define TYPE_COMBO_BOX_ENTRY GTK_TYPE_COMBO_BOX_ENTRY
92 #else /* !GTK_TYPE_COMBO_BOX_ENTRY */
93 # define TYPE_COMBO_BOX_ENTRY get_combo_box_entry_type()
94 static GType
get_combo_box_entry_type(void)
96 static volatile gsize type
= 0;
97 if (g_once_init_enter(&type
))
99 GType g_type
= g_type_register_static_simple(GTK_TYPE_COMBO_BOX
, "dummy-combo-box-entry",
100 sizeof(GtkComboBoxClass
), NULL
, sizeof(GtkComboBox
), NULL
, G_TYPE_FLAG_ABSTRACT
);
101 g_once_init_leave(&type
, g_type
);
105 #endif /* !GTK_TYPE_COMBO_BOX_ENTRY */
110 GType setting_type
; /* e.g. G_TYPE_INT */
111 gpointer setting
; /* Address of a variable */
112 const gchar
*key_name
;
113 gpointer default_value
; /* Default value, e.g. (gpointer)1 */
114 GType widget_type
; /* e.g. GTK_TYPE_TOGGLE_BUTTON */
115 StashWidgetID widget_id
; /* (GtkWidget*) or (gchar*) */
118 struct EnumWidget
*radio_buttons
;
119 const gchar
*property_name
;
120 } extra
; /* extra fields depending on widget_type */
123 typedef struct StashPref StashPref
;
127 const gchar
*name
; /* group name to use in the keyfile */
128 GPtrArray
*entries
; /* array of (StashPref*) */
129 gboolean various
; /* mark group for display/edit in stash treeview */
130 gboolean use_defaults
; /* use default values if there's no keyfile entry */
133 typedef struct EnumWidget
135 StashWidgetID widget_id
;
141 typedef enum SettingAction
148 typedef enum PrefAction
156 static void handle_boolean_setting(StashGroup
*group
, StashPref
*se
,
157 GKeyFile
*config
, SettingAction action
)
159 gboolean
*setting
= se
->setting
;
164 *setting
= utils_get_setting_boolean(config
, group
->name
, se
->key_name
,
165 GPOINTER_TO_INT(se
->default_value
));
168 g_key_file_set_boolean(config
, group
->name
, se
->key_name
, *setting
);
174 static void handle_integer_setting(StashGroup
*group
, StashPref
*se
,
175 GKeyFile
*config
, SettingAction action
)
177 gint
*setting
= se
->setting
;
182 *setting
= utils_get_setting_integer(config
, group
->name
, se
->key_name
,
183 GPOINTER_TO_INT(se
->default_value
));
186 g_key_file_set_integer(config
, group
->name
, se
->key_name
, *setting
);
192 static void handle_string_setting(StashGroup
*group
, StashPref
*se
,
193 GKeyFile
*config
, SettingAction action
)
195 gchararray
*setting
= se
->setting
;
201 *setting
= utils_get_setting_string(config
, group
->name
, se
->key_name
,
205 g_key_file_set_string(config
, group
->name
, se
->key_name
,
206 *setting
? *setting
: "");
212 static void handle_strv_setting(StashGroup
*group
, StashPref
*se
,
213 GKeyFile
*config
, SettingAction action
)
215 gchararray
**setting
= se
->setting
;
220 g_strfreev(*setting
);
221 *setting
= g_key_file_get_string_list(config
, group
->name
, se
->key_name
,
223 if (*setting
== NULL
)
224 *setting
= g_strdupv(se
->default_value
);
229 /* don't try to save a NULL string vector */
230 const gchar
*dummy
[] = { "", NULL
};
231 const gchar
**strv
= *setting
? (const gchar
**)*setting
: dummy
;
233 g_key_file_set_string_list(config
, group
->name
, se
->key_name
,
234 strv
, g_strv_length((gchar
**)strv
));
241 static void keyfile_action(SettingAction action
, StashGroup
*group
, GKeyFile
*keyfile
)
246 foreach_ptr_array(entry
, i
, group
->entries
)
248 /* don't override settings with default values */
249 if (!group
->use_defaults
&& action
== SETTING_READ
&&
250 !g_key_file_has_key(keyfile
, group
->name
, entry
->key_name
, NULL
))
253 switch (entry
->setting_type
)
256 handle_boolean_setting(group
, entry
, keyfile
, action
); break;
258 handle_integer_setting(group
, entry
, keyfile
, action
); break;
260 handle_string_setting(group
, entry
, keyfile
, action
); break;
262 /* Note: G_TYPE_STRV is not a constant, can't use case label */
263 if (entry
->setting_type
== G_TYPE_STRV
)
264 handle_strv_setting(group
, entry
, keyfile
, action
);
266 g_warning("Unhandled type for %s::%s in %s()!", group
->name
, entry
->key_name
,
273 /** Reads key values from @a keyfile into the group settings.
274 * @note You should still call this even if the keyfile couldn't be loaded from disk
275 * so that all Stash settings are initialized to defaults.
277 * @param keyfile Usually loaded from a configuration file first. */
278 void stash_group_load_from_key_file(StashGroup
*group
, GKeyFile
*keyfile
)
280 keyfile_action(SETTING_READ
, group
, keyfile
);
284 /** Writes group settings into key values in @a keyfile.
285 * @a keyfile is usually written to a configuration file afterwards.
287 * @param keyfile . */
288 void stash_group_save_to_key_file(StashGroup
*group
, GKeyFile
*keyfile
)
290 keyfile_action(SETTING_WRITE
, group
, keyfile
);
294 /** Reads group settings from a configuration file using @c GKeyFile.
295 * @note Stash settings will be initialized to defaults if the keyfile
296 * couldn't be loaded from disk.
298 * @param filename Filename of the file to read, in locale encoding.
299 * @return @c TRUE if a key file could be loaded.
300 * @see stash_group_load_from_key_file().
302 gboolean
stash_group_load_from_file(StashGroup
*group
, const gchar
*filename
)
307 keyfile
= g_key_file_new();
308 ret
= g_key_file_load_from_file(keyfile
, filename
, 0, NULL
);
309 /* even on failure we load settings to apply defaults */
310 stash_group_load_from_key_file(group
, keyfile
);
312 g_key_file_free(keyfile
);
317 /** Writes group settings to a configuration file using @c GKeyFile.
320 * @param filename Filename of the file to write, in locale encoding.
321 * @param flags Keyfile options - @c G_KEY_FILE_NONE is the most efficient.
322 * @return 0 if the file was successfully written, otherwise the @c errno of the
323 * failed operation is returned.
324 * @see stash_group_save_to_key_file().
326 gint
stash_group_save_to_file(StashGroup
*group
, const gchar
*filename
,
333 keyfile
= g_key_file_new();
334 /* if we need to keep comments or translations, try to load first */
336 g_key_file_load_from_file(keyfile
, filename
, flags
, NULL
);
338 stash_group_save_to_key_file(group
, keyfile
);
339 data
= g_key_file_to_data(keyfile
, NULL
, NULL
);
340 ret
= utils_write_file(filename
, data
);
342 g_key_file_free(keyfile
);
347 /** Creates a new group.
348 * @param name Name used for @c GKeyFile group.
350 StashGroup
*stash_group_new(const gchar
*name
)
352 StashGroup
*group
= g_new0(StashGroup
, 1);
355 group
->entries
= g_ptr_array_new();
356 group
->use_defaults
= TRUE
;
361 /** Frees the memory allocated for setting values in a group.
362 * Useful e.g. to avoid freeing strings individually.
363 * @note This is *not* called by stash_group_free().
365 void stash_group_free_settings(StashGroup
*group
)
370 foreach_ptr_array(entry
, i
, group
->entries
)
372 if (entry
->setting_type
== G_TYPE_STRING
)
373 g_free(*(gchararray
*) entry
->setting
);
374 else if (entry
->setting_type
== G_TYPE_STRV
)
375 g_strfreev(*(gchararray
**) entry
->setting
);
379 *(gpointer
**) entry
->setting
= NULL
;
386 void stash_group_free(StashGroup
*group
)
391 foreach_ptr_array(entry
, i
, group
->entries
)
393 if (entry
->widget_type
== GTK_TYPE_RADIO_BUTTON
)
395 g_free(entry
->extra
.radio_buttons
);
397 g_slice_free(StashPref
, entry
);
399 g_ptr_array_free(group
->entries
, TRUE
);
404 /* Used for selecting groups passed to stash_tree_setup().
405 * @c FALSE by default. */
406 void stash_group_set_various(StashGroup
*group
, gboolean various
)
408 group
->various
= various
;
412 /* When @c FALSE, Stash doesn't change the setting if there is no keyfile entry, so it
413 * remains whatever it was initialized/set to by user code.
414 * @c TRUE by default. */
415 void stash_group_set_use_defaults(StashGroup
*group
, gboolean use_defaults
)
417 group
->use_defaults
= use_defaults
;
422 add_pref(StashGroup
*group
, GType type
, gpointer setting
,
423 const gchar
*key_name
, gpointer default_value
)
425 StashPref init
= {type
, setting
, key_name
, default_value
, G_TYPE_NONE
, NULL
, {NULL
}};
426 StashPref
*entry
= g_slice_new(StashPref
);
430 /* init any pointer settings to NULL so they can be freed later */
431 if (type
== G_TYPE_STRING
||
433 if (group
->use_defaults
)
434 *(gpointer
**)setting
= NULL
;
436 g_ptr_array_add(group
->entries
, entry
);
441 /** Adds boolean setting.
443 * @param setting Address of setting variable.
444 * @param key_name Name for key in a @c GKeyFile.
445 * @param default_value Value to use if the key doesn't exist when loading. */
446 void stash_group_add_boolean(StashGroup
*group
, gboolean
*setting
,
447 const gchar
*key_name
, gboolean default_value
)
449 add_pref(group
, G_TYPE_BOOLEAN
, setting
, key_name
, GINT_TO_POINTER(default_value
));
453 /** Adds integer setting.
455 * @param setting Address of setting variable.
456 * @param key_name Name for key in a @c GKeyFile.
457 * @param default_value Value to use if the key doesn't exist when loading. */
458 void stash_group_add_integer(StashGroup
*group
, gint
*setting
,
459 const gchar
*key_name
, gint default_value
)
461 add_pref(group
, G_TYPE_INT
, setting
, key_name
, GINT_TO_POINTER(default_value
));
465 /** Adds string setting.
466 * The contents of @a setting will be initialized to @c NULL.
468 * @param setting Address of setting variable.
469 * @param key_name Name for key in a @c GKeyFile.
470 * @param default_value String to copy if the key doesn't exist when loading, or @c NULL. */
471 void stash_group_add_string(StashGroup
*group
, gchar
**setting
,
472 const gchar
*key_name
, const gchar
*default_value
)
474 add_pref(group
, G_TYPE_STRING
, setting
, key_name
, (gpointer
)default_value
);
478 /** Adds string vector setting (array of strings).
479 * The contents of @a setting will be initialized to @c NULL.
481 * @param setting Address of setting variable.
482 * @param key_name Name for key in a @c GKeyFile.
483 * @param default_value Vector to copy if the key doesn't exist when loading. Usually @c NULL. */
484 void stash_group_add_string_vector(StashGroup
*group
, gchar
***setting
,
485 const gchar
*key_name
, const gchar
**default_value
)
487 add_pref(group
, G_TYPE_STRV
, setting
, key_name
, (gpointer
)default_value
);
491 /* *** GTK-related functions *** */
493 static void handle_toggle_button(GtkWidget
*widget
, gboolean
*setting
,
499 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget
), *setting
);
502 *setting
= gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget
));
508 static void handle_spin_button(GtkWidget
*widget
, StashPref
*entry
,
511 gint
*setting
= entry
->setting
;
513 g_assert(entry
->setting_type
== G_TYPE_INT
); /* only int spin prefs */
518 gtk_spin_button_set_value(GTK_SPIN_BUTTON(widget
), *setting
);
521 /* if the widget is focussed, the value might not be updated */
522 gtk_spin_button_update(GTK_SPIN_BUTTON(widget
));
523 *setting
= gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(widget
));
529 static void handle_combo_box(GtkWidget
*widget
, StashPref
*entry
,
532 gint
*setting
= entry
->setting
;
537 gtk_combo_box_set_active(GTK_COMBO_BOX(widget
), *setting
);
540 *setting
= gtk_combo_box_get_active(GTK_COMBO_BOX(widget
));
546 static void handle_entry(GtkWidget
*widget
, StashPref
*entry
,
549 gchararray
*setting
= entry
->setting
;
554 gtk_entry_set_text(GTK_ENTRY(widget
), *setting
);
558 *setting
= g_strdup(gtk_entry_get_text(GTK_ENTRY(widget
)));
564 static void handle_combo_box_entry(GtkWidget
*widget
, StashPref
*entry
,
567 widget
= gtk_bin_get_child(GTK_BIN(widget
));
568 handle_entry(widget
, entry
, action
);
572 /* taken from Glade 2.x generated support.c */
574 lookup_widget(GtkWidget
*widget
, const gchar
*widget_name
)
576 GtkWidget
*parent
, *found_widget
;
578 g_return_val_if_fail(widget
!= NULL
, NULL
);
579 g_return_val_if_fail(widget_name
!= NULL
, NULL
);
583 if (GTK_IS_MENU(widget
))
584 parent
= gtk_menu_get_attach_widget(GTK_MENU(widget
));
586 parent
= gtk_widget_get_parent(widget
);
588 parent
= (GtkWidget
*) g_object_get_data(G_OBJECT(widget
), "GladeParentKey");
594 found_widget
= (GtkWidget
*) g_object_get_data(G_OBJECT(widget
), widget_name
);
595 if (G_UNLIKELY(found_widget
== NULL
))
596 g_warning("Widget not found: %s", widget_name
);
602 get_widget(GtkWidget
*owner
, StashWidgetID widget_id
)
607 widget
= lookup_widget(owner
, (const gchar
*)widget_id
);
609 widget
= (GtkWidget
*)widget_id
;
611 if (!GTK_IS_WIDGET(widget
))
613 g_warning("Unknown widget in %s()!", G_STRFUNC
);
620 static void handle_radio_button(GtkWidget
*widget
, gint enum_id
, gboolean
*setting
,
626 if (*setting
== enum_id
)
627 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget
), TRUE
);
630 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget
)))
637 static void handle_radio_buttons(GtkWidget
*owner
, StashPref
*entry
,
640 EnumWidget
*field
= entry
->extra
.radio_buttons
;
642 GtkWidget
*widget
= NULL
;
646 widget
= get_widget(owner
, field
->widget_id
);
652 handle_radio_button(widget
, field
->enum_id
, entry
->setting
, action
);
654 if (!field
->widget_id
)
657 if (g_slist_length(gtk_radio_button_get_group(GTK_RADIO_BUTTON(widget
))) != count
)
658 g_warning("Missing/invalid radio button widget IDs found!");
662 static void handle_widget_property(GtkWidget
*widget
, StashPref
*entry
,
665 GObject
*object
= G_OBJECT(widget
);
666 const gchar
*name
= entry
->extra
.property_name
;
671 g_object_set(object
, name
, entry
->setting
, NULL
);
674 if (entry
->setting_type
== G_TYPE_STRING
)
675 g_free(entry
->setting
);
676 /* TODO: Which other types need freeing here? */
678 g_object_get(object
, name
, entry
->setting
, NULL
);
684 static void pref_action(PrefAction action
, StashGroup
*group
, GtkWidget
*owner
)
689 foreach_ptr_array(entry
, i
, group
->entries
)
693 /* ignore settings with no widgets */
694 if (entry
->widget_type
== G_TYPE_NONE
)
697 /* radio buttons have several widgets */
698 if (entry
->widget_type
== GTK_TYPE_RADIO_BUTTON
)
700 handle_radio_buttons(owner
, entry
, action
);
704 widget
= get_widget(owner
, entry
->widget_id
);
707 g_warning("Unknown widget for %s::%s in %s()!", group
->name
, entry
->key_name
,
712 /* note: can't use switch for GTK_TYPE macros */
713 if (entry
->widget_type
== GTK_TYPE_TOGGLE_BUTTON
)
714 handle_toggle_button(widget
, entry
->setting
, action
);
715 else if (entry
->widget_type
== GTK_TYPE_SPIN_BUTTON
)
716 handle_spin_button(widget
, entry
, action
);
717 else if (entry
->widget_type
== GTK_TYPE_COMBO_BOX
)
718 handle_combo_box(widget
, entry
, action
);
719 else if (entry
->widget_type
== TYPE_COMBO_BOX_ENTRY
)
720 handle_combo_box_entry(widget
, entry
, action
);
721 else if (entry
->widget_type
== GTK_TYPE_ENTRY
)
722 handle_entry(widget
, entry
, action
);
723 else if (entry
->widget_type
== G_TYPE_PARAM
)
724 handle_widget_property(widget
, entry
, action
);
726 g_warning("Unhandled type for %s::%s in %s()!", group
->name
, entry
->key_name
,
732 /** Applies Stash settings to widgets, usually called before displaying the widgets.
733 * The @a owner argument depends on which type you use for @ref StashWidgetID.
735 * @param owner If non-NULL, used to lookup widgets by name, otherwise
736 * widget pointers are assumed.
737 * @see stash_group_update(). */
738 void stash_group_display(StashGroup
*group
, GtkWidget
*owner
)
740 pref_action(PREF_DISPLAY
, group
, owner
);
744 /** Applies widget values to Stash settings, usually called after displaying the widgets.
745 * The @a owner argument depends on which type you use for @ref StashWidgetID.
747 * @param owner If non-NULL, used to lookup widgets by name, otherwise
748 * widget pointers are assumed.
749 * @see stash_group_display(). */
750 void stash_group_update(StashGroup
*group
, GtkWidget
*owner
)
752 pref_action(PREF_UPDATE
, group
, owner
);
757 add_widget_pref(StashGroup
*group
, GType setting_type
, gpointer setting
,
758 const gchar
*key_name
, gpointer default_value
,
759 GType widget_type
, StashWidgetID widget_id
)
762 add_pref(group
, setting_type
, setting
, key_name
, default_value
);
764 entry
->widget_type
= widget_type
;
765 entry
->widget_id
= widget_id
;
770 /** Adds a @c GtkToggleButton (or @c GtkCheckButton) widget pref.
772 * @param setting Address of setting variable.
773 * @param key_name Name for key in a @c GKeyFile.
774 * @param default_value Value to use if the key doesn't exist when loading.
775 * @param widget_id @c GtkWidget pointer or string to lookup widget later.
776 * @see stash_group_add_radio_buttons(). */
777 void stash_group_add_toggle_button(StashGroup
*group
, gboolean
*setting
,
778 const gchar
*key_name
, gboolean default_value
, StashWidgetID widget_id
)
780 add_widget_pref(group
, G_TYPE_BOOLEAN
, setting
, key_name
, GINT_TO_POINTER(default_value
),
781 GTK_TYPE_TOGGLE_BUTTON
, widget_id
);
785 /** Adds a @c GtkRadioButton widget group pref.
787 * @param setting Address of setting variable.
788 * @param key_name Name for key in a @c GKeyFile.
789 * @param default_value Value to use if the key doesn't exist when loading.
790 * @param widget_id @c GtkWidget pointer or string to lookup widget later.
791 * @param enum_id Enum value for @a widget_id.
792 * @param ... pairs of @a widget_id, @a enum_id.
793 * Example (using widget lookup strings, but widget pointers can also work):
796 * stash_group_add_radio_buttons(group, &which_one_setting, "which_one", BAR,
797 * "radio_foo", FOO, "radio_bar", BAR, NULL);
799 void stash_group_add_radio_buttons(StashGroup
*group
, gint
*setting
,
800 const gchar
*key_name
, gint default_value
,
801 StashWidgetID widget_id
, gint enum_id
, ...)
804 add_widget_pref(group
, G_TYPE_INT
, setting
, key_name
, GINT_TO_POINTER(default_value
),
805 GTK_TYPE_RADIO_BUTTON
, NULL
);
808 EnumWidget
*item
, *array
;
810 /* count pairs of args */
811 va_start(args
, enum_id
);
814 if (!va_arg(args
, gpointer
))
821 array
= g_new0(EnumWidget
, count
+ 1);
822 entry
->extra
.radio_buttons
= array
;
824 va_start(args
, enum_id
);
825 foreach_c_array(item
, array
, count
)
830 item
->widget_id
= widget_id
;
831 item
->enum_id
= enum_id
;
835 item
->widget_id
= va_arg(args
, gpointer
);
836 item
->enum_id
= va_arg(args
, gint
);
843 /** Adds a @c GtkSpinButton widget pref.
845 * @param setting Address of setting variable.
846 * @param key_name Name for key in a @c GKeyFile.
847 * @param default_value Value to use if the key doesn't exist when loading.
848 * @param widget_id @c GtkWidget pointer or string to lookup widget later. */
849 void stash_group_add_spin_button_integer(StashGroup
*group
, gint
*setting
,
850 const gchar
*key_name
, gint default_value
, StashWidgetID widget_id
)
852 add_widget_pref(group
, G_TYPE_INT
, setting
, key_name
, GINT_TO_POINTER(default_value
),
853 GTK_TYPE_SPIN_BUTTON
, widget_id
);
857 /** Adds a @c GtkComboBox widget pref.
859 * @param setting Address of setting variable.
860 * @param key_name Name for key in a @c GKeyFile.
861 * @param default_value Value to use if the key doesn't exist when loading.
862 * @param widget_id @c GtkWidget pointer or string to lookup widget later.
863 * @see stash_group_add_combo_box_entry(). */
864 void stash_group_add_combo_box(StashGroup
*group
, gint
*setting
,
865 const gchar
*key_name
, gint default_value
, StashWidgetID widget_id
)
867 add_widget_pref(group
, G_TYPE_INT
, setting
, key_name
, GINT_TO_POINTER(default_value
),
868 GTK_TYPE_COMBO_BOX
, widget_id
);
872 /** Adds a @c GtkComboBoxEntry widget pref.
874 * @param setting Address of setting variable.
875 * @param key_name Name for key in a @c GKeyFile.
876 * @param default_value Value to use if the key doesn't exist when loading.
877 * @param widget_id @c GtkWidget pointer or string to lookup widget later. */
878 /* We could maybe also have something like stash_group_add_combo_box_entry_with_menu()
879 * for the history list - or should that be stored as a separate setting? */
880 void stash_group_add_combo_box_entry(StashGroup
*group
, gchar
**setting
,
881 const gchar
*key_name
, const gchar
*default_value
, StashWidgetID widget_id
)
883 add_widget_pref(group
, G_TYPE_STRING
, setting
, key_name
, (gpointer
)default_value
,
884 TYPE_COMBO_BOX_ENTRY
, widget_id
);
888 /** Adds a @c GtkEntry widget pref.
890 * @param setting Address of setting variable.
891 * @param key_name Name for key in a @c GKeyFile.
892 * @param default_value Value to use if the key doesn't exist when loading.
893 * @param widget_id @c GtkWidget pointer or string to lookup widget later. */
894 void stash_group_add_entry(StashGroup
*group
, gchar
**setting
,
895 const gchar
*key_name
, const gchar
*default_value
, StashWidgetID widget_id
)
897 add_widget_pref(group
, G_TYPE_STRING
, setting
, key_name
, (gpointer
)default_value
,
898 GTK_TYPE_ENTRY
, widget_id
);
902 static GType
object_get_property_type(GObject
*object
, const gchar
*property_name
)
904 GObjectClass
*klass
= G_OBJECT_GET_CLASS(object
);
907 ps
= g_object_class_find_property(klass
, property_name
);
908 return ps
->value_type
;
912 /** Adds a widget's read/write property to the stash group.
913 * The property will be set when calling
914 * stash_group_display(), and read when calling stash_group_update().
916 * @param setting Address of e.g. an integer if using an integer property.
917 * @param key_name Name for key in a @c GKeyFile.
918 * @param default_value Value to use if the key doesn't exist when loading.
919 * Should be cast into a pointer e.g. with @c GINT_TO_POINTER().
920 * @param widget_id @c GtkWidget pointer or string to lookup widget later.
921 * @param property_name .
922 * @param type can be @c 0 if passing a @c GtkWidget as the @a widget_id argument to look it up from the
924 * @warning Currently only string GValue properties will be freed before setting; patch for
925 * other types - see @c handle_widget_property(). */
926 void stash_group_add_widget_property(StashGroup
*group
, gpointer setting
,
927 const gchar
*key_name
, gpointer default_value
, StashWidgetID widget_id
,
928 const gchar
*property_name
, GType type
)
931 type
= object_get_property_type(G_OBJECT(widget_id
), property_name
);
933 add_widget_pref(group
, type
, setting
, key_name
, default_value
,
934 G_TYPE_PARAM
, widget_id
)->extra
.property_name
= property_name
;
946 struct StashTreeValue
948 const gchar
*group_name
;
952 gchararray tree_string
;
957 typedef struct StashTreeValue StashTreeValue
;
960 static void stash_tree_renderer_set_data(GtkCellLayout
*cell_layout
, GtkCellRenderer
*cell
,
961 GtkTreeModel
*model
, GtkTreeIter
*iter
, gpointer user_data
)
963 GType cell_type
= GPOINTER_TO_SIZE(user_data
);
964 StashTreeValue
*value
;
966 gboolean matches_type
;
968 gtk_tree_model_get(model
, iter
, STASH_TREE_VALUE
, &value
, -1);
970 matches_type
= pref
->setting_type
== cell_type
;
971 g_object_set(cell
, "visible", matches_type
, "sensitive", matches_type
,
972 cell_type
== G_TYPE_BOOLEAN
? "activatable" : "editable", matches_type
, NULL
);
976 switch (pref
->setting_type
)
979 g_object_set(cell
, "active", value
->data
.tree_int
, NULL
);
983 gchar
*text
= g_strdup_printf("%d", value
->data
.tree_int
);
984 g_object_set(cell
, "text", text
, NULL
);
989 g_object_set(cell
, "text", value
->data
.tree_string
, NULL
);
996 static void stash_tree_renderer_edited(gchar
*path_str
, gchar
*new_text
, GtkTreeModel
*model
)
1000 StashTreeValue
*value
;
1003 path
= gtk_tree_path_new_from_string(path_str
);
1004 gtk_tree_model_get_iter(model
, &iter
, path
);
1005 gtk_tree_model_get(model
, &iter
, STASH_TREE_VALUE
, &value
, -1);
1008 switch (pref
->setting_type
)
1010 case G_TYPE_BOOLEAN
:
1011 value
->data
.tree_int
= !value
->data
.tree_int
;
1014 value
->data
.tree_int
= atoi(new_text
);
1017 SETPTR(value
->data
.tree_string
, g_strdup(new_text
));
1021 gtk_tree_model_row_changed(model
, path
, &iter
);
1022 gtk_tree_path_free(path
);
1026 static void stash_tree_boolean_toggled(GtkCellRendererToggle
*cell
, gchar
*path_str
,
1027 GtkTreeModel
*model
)
1029 stash_tree_renderer_edited(path_str
, NULL
, model
);
1033 static void stash_tree_string_edited(GtkCellRenderer
*cell
, gchar
*path_str
, gchar
*new_text
,
1034 GtkTreeModel
*model
)
1036 stash_tree_renderer_edited(path_str
, new_text
, model
);
1040 static gboolean
stash_tree_discard_value(GtkTreeModel
*model
, GtkTreePath
*path
,
1041 GtkTreeIter
*iter
, gpointer user_data
)
1043 StashTreeValue
*value
;
1045 gtk_tree_model_get(model
, iter
, STASH_TREE_VALUE
, &value
, -1);
1046 if (value
->pref
->setting_type
== G_TYPE_STRING
)
1047 g_free(value
->data
.tree_string
);
1054 static void stash_tree_destroy_cb(GtkWidget
*widget
, gpointer user_data
)
1056 GtkTreeModel
*model
= gtk_tree_view_get_model(GTK_TREE_VIEW(widget
));
1057 gtk_tree_model_foreach(model
, stash_tree_discard_value
, NULL
);
1061 static void stash_tree_append_pref(StashGroup
*group
, StashPref
*entry
, GtkListStore
*store
,
1065 StashTreeValue
*value
;
1067 value
= g_new0(StashTreeValue
, 1);
1069 value
->group_name
= group
->name
;
1070 value
->pref
= entry
;
1072 gtk_list_store_append(store
, &iter
);
1073 gtk_list_store_set(store
, &iter
, STASH_TREE_NAME
, entry
->key_name
,
1074 STASH_TREE_VALUE
, value
, -1);
1078 static void stash_tree_append_prefs(GPtrArray
*group_array
,
1079 GtkListStore
*store
, PrefAction action
)
1085 foreach_ptr_array(group
, i
, group_array
)
1089 foreach_ptr_array(entry
, j
, group
->entries
)
1090 stash_tree_append_pref(group
, entry
, store
, action
);
1096 /* Setups a simple editor for stash preferences based on the widget arguments.
1097 * group_array - Array of groups which's settings will be edited.
1098 * tree - GtkTreeView in which to edit the preferences. Must be empty. */
1099 void stash_tree_setup(GPtrArray
*group_array
, GtkTreeView
*tree
)
1101 GtkListStore
*store
;
1102 GtkTreeModel
*model
;
1103 GtkCellRenderer
*cell
;
1104 GtkTreeViewColumn
*column
;
1105 GtkAdjustment
*adjustment
;
1107 store
= gtk_list_store_new(STASH_TREE_COUNT
, G_TYPE_STRING
, G_TYPE_POINTER
);
1108 stash_tree_append_prefs(group_array
, store
, PREF_DISPLAY
);
1109 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store
), STASH_TREE_NAME
,
1110 GTK_SORT_ASCENDING
);
1112 model
= GTK_TREE_MODEL(store
);
1113 gtk_tree_view_set_model(tree
, model
);
1114 g_object_unref(G_OBJECT(store
));
1115 g_signal_connect(tree
, "destroy", G_CALLBACK(stash_tree_destroy_cb
), NULL
);
1117 cell
= gtk_cell_renderer_text_new();
1118 column
= gtk_tree_view_column_new_with_attributes(_("Name"), cell
, "text",
1119 STASH_TREE_NAME
, NULL
);
1120 gtk_tree_view_column_set_sort_column_id(column
, STASH_TREE_NAME
);
1121 gtk_tree_view_column_set_sort_indicator(column
, TRUE
);
1122 gtk_tree_view_append_column(tree
, column
);
1124 column
= gtk_tree_view_column_new();
1125 gtk_tree_view_column_set_title(column
, _("Value"));
1126 gtk_tree_view_append_column(tree
, column
);
1127 /* boolean renderer */
1128 cell
= gtk_cell_renderer_toggle_new();
1129 g_signal_connect(cell
, "toggled", G_CALLBACK(stash_tree_boolean_toggled
), model
);
1130 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(column
), cell
, FALSE
);
1131 gtk_cell_layout_set_cell_data_func(GTK_CELL_LAYOUT(column
), cell
,
1132 stash_tree_renderer_set_data
, GSIZE_TO_POINTER(G_TYPE_BOOLEAN
), NULL
);
1133 /* string renderer */
1134 cell
= gtk_cell_renderer_text_new();
1135 g_object_set(cell
, "ellipsize", PANGO_ELLIPSIZE_END
, NULL
);
1136 g_signal_connect(cell
, "edited", G_CALLBACK(stash_tree_string_edited
), model
);
1137 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(column
), cell
, TRUE
);
1138 gtk_cell_layout_set_cell_data_func(GTK_CELL_LAYOUT(column
), cell
,
1139 stash_tree_renderer_set_data
, GSIZE_TO_POINTER(G_TYPE_STRING
), NULL
);
1140 /* integer renderer */
1141 cell
= gtk_cell_renderer_spin_new();
1142 adjustment
= GTK_ADJUSTMENT(gtk_adjustment_new(0, G_MININT
, G_MAXINT
, 1, 10, 0));
1143 g_object_set(cell
, "adjustment", adjustment
, NULL
);
1144 g_signal_connect(cell
, "edited", G_CALLBACK(stash_tree_string_edited
), model
);
1145 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(column
), cell
, FALSE
);
1146 gtk_cell_layout_set_cell_data_func(GTK_CELL_LAYOUT(column
), cell
,
1147 stash_tree_renderer_set_data
, GSIZE_TO_POINTER(G_TYPE_INT
), NULL
);
1151 static void stash_tree_display_pref(StashTreeValue
*value
, StashPref
*entry
)
1153 switch (entry
->setting_type
)
1155 case G_TYPE_BOOLEAN
:
1156 value
->data
.tree_int
= *(gboolean
*) entry
->setting
;
1159 value
->data
.tree_int
= *(gint
*) entry
->setting
;
1162 SETPTR(value
->data
.tree_string
, g_strdup(*(gchararray
*) entry
->setting
));
1165 g_warning("Unhandled type for %s::%s in %s()!", value
->group_name
,
1166 entry
->key_name
, G_STRFUNC
);
1171 static void stash_tree_update_pref(StashTreeValue
*value
, StashPref
*entry
)
1173 switch (entry
->setting_type
)
1175 case G_TYPE_BOOLEAN
:
1176 *(gboolean
*) entry
->setting
= value
->data
.tree_int
;
1179 *(gint
*) entry
->setting
= value
->data
.tree_int
;
1183 gchararray
*text
= entry
->setting
;
1184 SETPTR(*text
, g_strdup(value
->data
.tree_string
));
1188 g_warning("Unhandled type for %s::%s in %s()!", value
->group_name
,
1189 entry
->key_name
, G_STRFUNC
);
1194 static void stash_tree_action(GtkTreeModel
*model
, PrefAction action
)
1197 gboolean valid
= gtk_tree_model_get_iter_first(model
, &iter
);
1198 StashTreeValue
*value
;
1202 gtk_tree_model_get(model
, &iter
, STASH_TREE_VALUE
, &value
, -1);
1207 stash_tree_display_pref(value
, value
->pref
);
1210 stash_tree_update_pref(value
, value
->pref
);
1213 valid
= gtk_tree_model_iter_next(model
, &iter
);
1218 void stash_tree_display(GtkTreeView
*tree
)
1220 stash_tree_action(gtk_tree_view_get_model(tree
), PREF_DISPLAY
);
1224 void stash_tree_update(GtkTreeView
*tree
)
1226 stash_tree_action(gtk_tree_view_get_model(tree
), PREF_UPDATE
);