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.
81 #include "support.h" /* only for _("text") */
82 #include "utils.h" /* only for foreach_*, utils_get_setting_*(). Stash should not depend on Geany. */
84 #include <stdlib.h> /* only for atoi() */
87 /* GTK3 removed ComboBoxEntry, but we need a value to differentiate combo box with and
88 * without entries, and it must not collide with other GTypes */
89 #ifdef GTK_TYPE_COMBO_BOX_ENTRY
90 # define TYPE_COMBO_BOX_ENTRY GTK_TYPE_COMBO_BOX_ENTRY
91 #else /* !GTK_TYPE_COMBO_BOX_ENTRY */
92 # define TYPE_COMBO_BOX_ENTRY get_combo_box_entry_type()
93 static GType
get_combo_box_entry_type(void)
95 static volatile gsize type
= 0;
96 if (g_once_init_enter(&type
))
98 GType g_type
= g_type_register_static_simple(GTK_TYPE_COMBO_BOX
, "dummy-combo-box-entry",
99 sizeof(GtkComboBoxClass
), NULL
, sizeof(GtkComboBox
), NULL
, G_TYPE_FLAG_ABSTRACT
);
100 g_once_init_leave(&type
, g_type
);
104 #endif /* !GTK_TYPE_COMBO_BOX_ENTRY */
109 GType setting_type
; /* e.g. G_TYPE_INT */
110 gpointer setting
; /* Address of a variable */
111 const gchar
*key_name
;
112 gpointer default_value
; /* Default value, e.g. (gpointer)1 */
113 GType widget_type
; /* e.g. GTK_TYPE_TOGGLE_BUTTON */
114 StashWidgetID widget_id
; /* (GtkWidget*) or (gchar*) */
117 struct EnumWidget
*radio_buttons
;
118 const gchar
*property_name
;
119 } extra
; /* extra fields depending on widget_type */
122 typedef struct StashPref StashPref
;
126 const gchar
*name
; /* group name to use in the keyfile */
127 GPtrArray
*entries
; /* array of (StashPref*) */
128 gboolean various
; /* mark group for display/edit in stash treeview */
129 gboolean use_defaults
; /* use default values if there's no keyfile entry */
132 typedef struct EnumWidget
134 StashWidgetID widget_id
;
140 typedef enum SettingAction
147 typedef enum PrefAction
155 static void handle_boolean_setting(StashGroup
*group
, StashPref
*se
,
156 GKeyFile
*config
, SettingAction action
)
158 gboolean
*setting
= se
->setting
;
163 *setting
= utils_get_setting_boolean(config
, group
->name
, se
->key_name
,
164 GPOINTER_TO_INT(se
->default_value
));
167 g_key_file_set_boolean(config
, group
->name
, se
->key_name
, *setting
);
173 static void handle_integer_setting(StashGroup
*group
, StashPref
*se
,
174 GKeyFile
*config
, SettingAction action
)
176 gint
*setting
= se
->setting
;
181 *setting
= utils_get_setting_integer(config
, group
->name
, se
->key_name
,
182 GPOINTER_TO_INT(se
->default_value
));
185 g_key_file_set_integer(config
, group
->name
, se
->key_name
, *setting
);
191 static void handle_string_setting(StashGroup
*group
, StashPref
*se
,
192 GKeyFile
*config
, SettingAction action
)
194 gchararray
*setting
= se
->setting
;
200 *setting
= utils_get_setting_string(config
, group
->name
, se
->key_name
,
204 g_key_file_set_string(config
, group
->name
, se
->key_name
,
205 *setting
? *setting
: "");
211 static void handle_strv_setting(StashGroup
*group
, StashPref
*se
,
212 GKeyFile
*config
, SettingAction action
)
214 gchararray
**setting
= se
->setting
;
219 g_strfreev(*setting
);
220 *setting
= g_key_file_get_string_list(config
, group
->name
, se
->key_name
,
222 if (*setting
== NULL
)
223 *setting
= g_strdupv(se
->default_value
);
228 /* don't try to save a NULL string vector */
229 const gchar
*dummy
[] = { "", NULL
};
230 const gchar
**strv
= *setting
? (const gchar
**)*setting
: dummy
;
232 g_key_file_set_string_list(config
, group
->name
, se
->key_name
,
233 strv
, g_strv_length((gchar
**)strv
));
240 static void keyfile_action(SettingAction action
, StashGroup
*group
, GKeyFile
*keyfile
)
245 foreach_ptr_array(entry
, i
, group
->entries
)
247 /* don't override settings with default values */
248 if (!group
->use_defaults
&& action
== SETTING_READ
&&
249 !g_key_file_has_key(keyfile
, group
->name
, entry
->key_name
, NULL
))
252 switch (entry
->setting_type
)
255 handle_boolean_setting(group
, entry
, keyfile
, action
); break;
257 handle_integer_setting(group
, entry
, keyfile
, action
); break;
259 handle_string_setting(group
, entry
, keyfile
, action
); break;
261 /* Note: G_TYPE_STRV is not a constant, can't use case label */
262 if (entry
->setting_type
== G_TYPE_STRV
)
263 handle_strv_setting(group
, entry
, keyfile
, action
);
265 g_warning("Unhandled type for %s::%s in %s()!", group
->name
, entry
->key_name
,
272 /** Reads key values from @a keyfile into the group settings.
273 * @note You should still call this even if the keyfile couldn't be loaded from disk
274 * so that all Stash settings are initialized to defaults.
276 * @param keyfile Usually loaded from a configuration file first. */
277 void stash_group_load_from_key_file(StashGroup
*group
, GKeyFile
*keyfile
)
279 keyfile_action(SETTING_READ
, group
, keyfile
);
283 /** Writes group settings into key values in @a keyfile.
284 * @a keyfile is usually written to a configuration file afterwards.
286 * @param keyfile . */
287 void stash_group_save_to_key_file(StashGroup
*group
, GKeyFile
*keyfile
)
289 keyfile_action(SETTING_WRITE
, group
, keyfile
);
293 /** Reads group settings from a configuration file using @c GKeyFile.
294 * @note Stash settings will be initialized to defaults if the keyfile
295 * couldn't be loaded from disk.
297 * @param filename Filename of the file to read, in locale encoding.
298 * @return @c TRUE if a key file could be loaded.
299 * @see stash_group_load_from_key_file().
301 gboolean
stash_group_load_from_file(StashGroup
*group
, const gchar
*filename
)
306 keyfile
= g_key_file_new();
307 ret
= g_key_file_load_from_file(keyfile
, filename
, 0, NULL
);
308 /* even on failure we load settings to apply defaults */
309 stash_group_load_from_key_file(group
, keyfile
);
311 g_key_file_free(keyfile
);
316 /** Writes group settings to a configuration file using @c GKeyFile.
319 * @param filename Filename of the file to write, in locale encoding.
320 * @param flags Keyfile options - @c G_KEY_FILE_NONE is the most efficient.
321 * @return 0 if the file was successfully written, otherwise the @c errno of the
322 * failed operation is returned.
323 * @see stash_group_save_to_key_file().
325 gint
stash_group_save_to_file(StashGroup
*group
, const gchar
*filename
,
332 keyfile
= g_key_file_new();
333 /* if we need to keep comments or translations, try to load first */
335 g_key_file_load_from_file(keyfile
, filename
, flags
, NULL
);
337 stash_group_save_to_key_file(group
, keyfile
);
338 data
= g_key_file_to_data(keyfile
, NULL
, NULL
);
339 ret
= utils_write_file(filename
, data
);
341 g_key_file_free(keyfile
);
346 /** Creates a new group.
347 * @param name Name used for @c GKeyFile group.
349 StashGroup
*stash_group_new(const gchar
*name
)
351 StashGroup
*group
= g_new0(StashGroup
, 1);
354 group
->entries
= g_ptr_array_new();
355 group
->use_defaults
= TRUE
;
360 /** Frees the memory allocated for setting values in a group.
361 * Useful e.g. to avoid freeing strings individually.
362 * @note This is *not* called by stash_group_free().
364 void stash_group_free_settings(StashGroup
*group
)
369 foreach_ptr_array(entry
, i
, group
->entries
)
371 if (entry
->setting_type
== G_TYPE_STRING
)
372 g_free(*(gchararray
*) entry
->setting
);
373 else if (entry
->setting_type
== G_TYPE_STRV
)
374 g_strfreev(*(gchararray
**) entry
->setting
);
378 *(gpointer
**) entry
->setting
= NULL
;
385 void stash_group_free(StashGroup
*group
)
390 foreach_ptr_array(entry
, i
, group
->entries
)
392 if (entry
->widget_type
== GTK_TYPE_RADIO_BUTTON
)
394 g_free(entry
->extra
.radio_buttons
);
396 g_slice_free(StashPref
, entry
);
398 g_ptr_array_free(group
->entries
, TRUE
);
403 /* Used for selecting groups passed to stash_tree_setup().
404 * @c FALSE by default. */
405 void stash_group_set_various(StashGroup
*group
, gboolean various
)
407 group
->various
= various
;
411 /* When @c FALSE, Stash doesn't change the setting if there is no keyfile entry, so it
412 * remains whatever it was initialized/set to by user code.
413 * @c TRUE by default. */
414 void stash_group_set_use_defaults(StashGroup
*group
, gboolean use_defaults
)
416 group
->use_defaults
= use_defaults
;
421 add_pref(StashGroup
*group
, GType type
, gpointer setting
,
422 const gchar
*key_name
, gpointer default_value
)
424 StashPref init
= {type
, setting
, key_name
, default_value
, G_TYPE_NONE
, NULL
, {NULL
}};
425 StashPref
*entry
= g_slice_new(StashPref
);
429 /* init any pointer settings to NULL so they can be freed later */
430 if (type
== G_TYPE_STRING
||
432 if (group
->use_defaults
)
433 *(gpointer
**)setting
= NULL
;
435 g_ptr_array_add(group
->entries
, entry
);
440 /** Adds boolean setting.
442 * @param setting Address of setting variable.
443 * @param key_name Name for key in a @c GKeyFile.
444 * @param default_value Value to use if the key doesn't exist when loading. */
445 void stash_group_add_boolean(StashGroup
*group
, gboolean
*setting
,
446 const gchar
*key_name
, gboolean default_value
)
448 add_pref(group
, G_TYPE_BOOLEAN
, setting
, key_name
, GINT_TO_POINTER(default_value
));
452 /** Adds integer setting.
454 * @param setting Address of setting variable.
455 * @param key_name Name for key in a @c GKeyFile.
456 * @param default_value Value to use if the key doesn't exist when loading. */
457 void stash_group_add_integer(StashGroup
*group
, gint
*setting
,
458 const gchar
*key_name
, gint default_value
)
460 add_pref(group
, G_TYPE_INT
, setting
, key_name
, GINT_TO_POINTER(default_value
));
464 /** Adds string setting.
465 * The contents of @a setting will be initialized to @c NULL.
467 * @param setting Address of setting variable.
468 * @param key_name Name for key in a @c GKeyFile.
469 * @param default_value String to copy if the key doesn't exist when loading, or @c NULL. */
470 void stash_group_add_string(StashGroup
*group
, gchar
**setting
,
471 const gchar
*key_name
, const gchar
*default_value
)
473 add_pref(group
, G_TYPE_STRING
, setting
, key_name
, (gpointer
)default_value
);
477 /** Adds string vector setting (array of strings).
478 * The contents of @a setting will be initialized to @c NULL.
480 * @param setting Address of setting variable.
481 * @param key_name Name for key in a @c GKeyFile.
482 * @param default_value Vector to copy if the key doesn't exist when loading. Usually @c NULL. */
483 void stash_group_add_string_vector(StashGroup
*group
, gchar
***setting
,
484 const gchar
*key_name
, const gchar
**default_value
)
486 add_pref(group
, G_TYPE_STRV
, setting
, key_name
, (gpointer
)default_value
);
490 /* *** GTK-related functions *** */
492 static void handle_toggle_button(GtkWidget
*widget
, gboolean
*setting
,
498 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget
), *setting
);
501 *setting
= gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget
));
507 static void handle_spin_button(GtkWidget
*widget
, StashPref
*entry
,
510 gint
*setting
= entry
->setting
;
512 g_assert(entry
->setting_type
== G_TYPE_INT
); /* only int spin prefs */
517 gtk_spin_button_set_value(GTK_SPIN_BUTTON(widget
), *setting
);
520 /* if the widget is focussed, the value might not be updated */
521 gtk_spin_button_update(GTK_SPIN_BUTTON(widget
));
522 *setting
= gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(widget
));
528 static void handle_combo_box(GtkWidget
*widget
, StashPref
*entry
,
531 gint
*setting
= entry
->setting
;
536 gtk_combo_box_set_active(GTK_COMBO_BOX(widget
), *setting
);
539 *setting
= gtk_combo_box_get_active(GTK_COMBO_BOX(widget
));
545 static void handle_entry(GtkWidget
*widget
, StashPref
*entry
,
548 gchararray
*setting
= entry
->setting
;
553 gtk_entry_set_text(GTK_ENTRY(widget
), *setting
);
557 *setting
= g_strdup(gtk_entry_get_text(GTK_ENTRY(widget
)));
563 static void handle_combo_box_entry(GtkWidget
*widget
, StashPref
*entry
,
566 widget
= gtk_bin_get_child(GTK_BIN(widget
));
567 handle_entry(widget
, entry
, action
);
571 /* taken from Glade 2.x generated support.c */
573 lookup_widget(GtkWidget
*widget
, const gchar
*widget_name
)
575 GtkWidget
*parent
, *found_widget
;
577 g_return_val_if_fail(widget
!= NULL
, NULL
);
578 g_return_val_if_fail(widget_name
!= NULL
, NULL
);
582 if (GTK_IS_MENU(widget
))
583 parent
= gtk_menu_get_attach_widget(GTK_MENU(widget
));
585 parent
= gtk_widget_get_parent(widget
);
587 parent
= (GtkWidget
*) g_object_get_data(G_OBJECT(widget
), "GladeParentKey");
593 found_widget
= (GtkWidget
*) g_object_get_data(G_OBJECT(widget
), widget_name
);
594 if (G_UNLIKELY(found_widget
== NULL
))
595 g_warning("Widget not found: %s", widget_name
);
601 get_widget(GtkWidget
*owner
, StashWidgetID widget_id
)
606 widget
= lookup_widget(owner
, (const gchar
*)widget_id
);
608 widget
= (GtkWidget
*)widget_id
;
610 if (!GTK_IS_WIDGET(widget
))
612 g_warning("Unknown widget in %s()!", G_STRFUNC
);
619 static void handle_radio_button(GtkWidget
*widget
, gint enum_id
, gboolean
*setting
,
625 if (*setting
== enum_id
)
626 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget
), TRUE
);
629 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget
)))
636 static void handle_radio_buttons(GtkWidget
*owner
, StashPref
*entry
,
639 EnumWidget
*field
= entry
->extra
.radio_buttons
;
641 GtkWidget
*widget
= NULL
;
645 widget
= get_widget(owner
, field
->widget_id
);
651 handle_radio_button(widget
, field
->enum_id
, entry
->setting
, action
);
653 if (!field
->widget_id
)
656 if (g_slist_length(gtk_radio_button_get_group(GTK_RADIO_BUTTON(widget
))) != count
)
657 g_warning("Missing/invalid radio button widget IDs found!");
661 static void handle_widget_property(GtkWidget
*widget
, StashPref
*entry
,
664 GObject
*object
= G_OBJECT(widget
);
665 const gchar
*name
= entry
->extra
.property_name
;
670 g_object_set(object
, name
, entry
->setting
, NULL
);
673 if (entry
->setting_type
== G_TYPE_STRING
)
674 g_free(entry
->setting
);
675 /* TODO: Which other types need freeing here? */
677 g_object_get(object
, name
, entry
->setting
, NULL
);
683 static void pref_action(PrefAction action
, StashGroup
*group
, GtkWidget
*owner
)
688 foreach_ptr_array(entry
, i
, group
->entries
)
692 /* ignore settings with no widgets */
693 if (entry
->widget_type
== G_TYPE_NONE
)
696 /* radio buttons have several widgets */
697 if (entry
->widget_type
== GTK_TYPE_RADIO_BUTTON
)
699 handle_radio_buttons(owner
, entry
, action
);
703 widget
= get_widget(owner
, entry
->widget_id
);
706 g_warning("Unknown widget for %s::%s in %s()!", group
->name
, entry
->key_name
,
711 /* note: can't use switch for GTK_TYPE macros */
712 if (entry
->widget_type
== GTK_TYPE_TOGGLE_BUTTON
)
713 handle_toggle_button(widget
, entry
->setting
, action
);
714 else if (entry
->widget_type
== GTK_TYPE_SPIN_BUTTON
)
715 handle_spin_button(widget
, entry
, action
);
716 else if (entry
->widget_type
== GTK_TYPE_COMBO_BOX
)
717 handle_combo_box(widget
, entry
, action
);
718 else if (entry
->widget_type
== TYPE_COMBO_BOX_ENTRY
)
719 handle_combo_box_entry(widget
, entry
, action
);
720 else if (entry
->widget_type
== GTK_TYPE_ENTRY
)
721 handle_entry(widget
, entry
, action
);
722 else if (entry
->widget_type
== G_TYPE_PARAM
)
723 handle_widget_property(widget
, entry
, action
);
725 g_warning("Unhandled type for %s::%s in %s()!", group
->name
, entry
->key_name
,
731 /** Applies Stash settings to widgets, usually called before displaying the widgets.
732 * The @a owner argument depends on which type you use for @ref StashWidgetID.
734 * @param owner If non-NULL, used to lookup widgets by name, otherwise
735 * widget pointers are assumed.
736 * @see stash_group_update(). */
737 void stash_group_display(StashGroup
*group
, GtkWidget
*owner
)
739 pref_action(PREF_DISPLAY
, group
, owner
);
743 /** Applies widget values to Stash settings, usually called after displaying the widgets.
744 * The @a owner argument depends on which type you use for @ref StashWidgetID.
746 * @param owner If non-NULL, used to lookup widgets by name, otherwise
747 * widget pointers are assumed.
748 * @see stash_group_display(). */
749 void stash_group_update(StashGroup
*group
, GtkWidget
*owner
)
751 pref_action(PREF_UPDATE
, group
, owner
);
756 add_widget_pref(StashGroup
*group
, GType setting_type
, gpointer setting
,
757 const gchar
*key_name
, gpointer default_value
,
758 GType widget_type
, StashWidgetID widget_id
)
761 add_pref(group
, setting_type
, setting
, key_name
, default_value
);
763 entry
->widget_type
= widget_type
;
764 entry
->widget_id
= widget_id
;
769 /** Adds a @c GtkToggleButton (or @c GtkCheckButton) widget pref.
771 * @param setting Address of setting variable.
772 * @param key_name Name for key in a @c GKeyFile.
773 * @param default_value Value to use if the key doesn't exist when loading.
774 * @param widget_id @c GtkWidget pointer or string to lookup widget later.
775 * @see stash_group_add_radio_buttons(). */
776 void stash_group_add_toggle_button(StashGroup
*group
, gboolean
*setting
,
777 const gchar
*key_name
, gboolean default_value
, StashWidgetID widget_id
)
779 add_widget_pref(group
, G_TYPE_BOOLEAN
, setting
, key_name
, GINT_TO_POINTER(default_value
),
780 GTK_TYPE_TOGGLE_BUTTON
, widget_id
);
784 /** Adds a @c GtkRadioButton widget group pref.
786 * @param setting Address of setting variable.
787 * @param key_name Name for key in a @c GKeyFile.
788 * @param default_value Value to use if the key doesn't exist when loading.
789 * @param widget_id @c GtkWidget pointer or string to lookup widget later.
790 * @param enum_id Enum value for @a widget_id.
791 * @param ... pairs of @a widget_id, @a enum_id.
792 * Example (using widget lookup strings, but widget pointers can also work):
795 * stash_group_add_radio_buttons(group, &which_one_setting, "which_one", BAR,
796 * "radio_foo", FOO, "radio_bar", BAR, NULL);
798 void stash_group_add_radio_buttons(StashGroup
*group
, gint
*setting
,
799 const gchar
*key_name
, gint default_value
,
800 StashWidgetID widget_id
, gint enum_id
, ...)
803 add_widget_pref(group
, G_TYPE_INT
, setting
, key_name
, GINT_TO_POINTER(default_value
),
804 GTK_TYPE_RADIO_BUTTON
, NULL
);
807 EnumWidget
*item
, *array
;
809 /* count pairs of args */
810 va_start(args
, enum_id
);
813 if (!va_arg(args
, gpointer
))
820 array
= g_new0(EnumWidget
, count
+ 1);
821 entry
->extra
.radio_buttons
= array
;
823 va_start(args
, enum_id
);
824 foreach_c_array(item
, array
, count
)
829 item
->widget_id
= widget_id
;
830 item
->enum_id
= enum_id
;
834 item
->widget_id
= va_arg(args
, gpointer
);
835 item
->enum_id
= va_arg(args
, gint
);
842 /** Adds a @c GtkSpinButton widget pref.
844 * @param setting Address of setting variable.
845 * @param key_name Name for key in a @c GKeyFile.
846 * @param default_value Value to use if the key doesn't exist when loading.
847 * @param widget_id @c GtkWidget pointer or string to lookup widget later. */
848 void stash_group_add_spin_button_integer(StashGroup
*group
, gint
*setting
,
849 const gchar
*key_name
, gint default_value
, StashWidgetID widget_id
)
851 add_widget_pref(group
, G_TYPE_INT
, setting
, key_name
, GINT_TO_POINTER(default_value
),
852 GTK_TYPE_SPIN_BUTTON
, widget_id
);
856 /** Adds a @c GtkComboBox widget pref.
858 * @param setting Address of setting variable.
859 * @param key_name Name for key in a @c GKeyFile.
860 * @param default_value Value to use if the key doesn't exist when loading.
861 * @param widget_id @c GtkWidget pointer or string to lookup widget later.
862 * @see stash_group_add_combo_box_entry(). */
863 void stash_group_add_combo_box(StashGroup
*group
, gint
*setting
,
864 const gchar
*key_name
, gint default_value
, StashWidgetID widget_id
)
866 add_widget_pref(group
, G_TYPE_INT
, setting
, key_name
, GINT_TO_POINTER(default_value
),
867 GTK_TYPE_COMBO_BOX
, widget_id
);
871 /** Adds a @c GtkComboBoxEntry widget pref.
873 * @param setting Address of setting variable.
874 * @param key_name Name for key in a @c GKeyFile.
875 * @param default_value Value to use if the key doesn't exist when loading.
876 * @param widget_id @c GtkWidget pointer or string to lookup widget later. */
877 /* We could maybe also have something like stash_group_add_combo_box_entry_with_menu()
878 * for the history list - or should that be stored as a separate setting? */
879 void stash_group_add_combo_box_entry(StashGroup
*group
, gchar
**setting
,
880 const gchar
*key_name
, const gchar
*default_value
, StashWidgetID widget_id
)
882 add_widget_pref(group
, G_TYPE_STRING
, setting
, key_name
, (gpointer
)default_value
,
883 TYPE_COMBO_BOX_ENTRY
, widget_id
);
887 /** Adds a @c GtkEntry widget pref.
889 * @param setting Address of setting variable.
890 * @param key_name Name for key in a @c GKeyFile.
891 * @param default_value Value to use if the key doesn't exist when loading.
892 * @param widget_id @c GtkWidget pointer or string to lookup widget later. */
893 void stash_group_add_entry(StashGroup
*group
, gchar
**setting
,
894 const gchar
*key_name
, const gchar
*default_value
, StashWidgetID widget_id
)
896 add_widget_pref(group
, G_TYPE_STRING
, setting
, key_name
, (gpointer
)default_value
,
897 GTK_TYPE_ENTRY
, widget_id
);
901 static GType
object_get_property_type(GObject
*object
, const gchar
*property_name
)
903 GObjectClass
*klass
= G_OBJECT_GET_CLASS(object
);
906 ps
= g_object_class_find_property(klass
, property_name
);
907 return ps
->value_type
;
911 /** Adds a widget's read/write property to the stash group.
912 * The property will be set when calling
913 * stash_group_display(), and read when calling stash_group_update().
915 * @param setting Address of e.g. an integer if using an integer property.
916 * @param key_name Name for key in a @c GKeyFile.
917 * @param default_value Value to use if the key doesn't exist when loading.
918 * Should be cast into a pointer e.g. with @c GINT_TO_POINTER().
919 * @param widget_id @c GtkWidget pointer or string to lookup widget later.
920 * @param property_name .
921 * @param type can be @c 0 if passing a @c GtkWidget as the @a widget_id argument to look it up from the
923 * @warning Currently only string GValue properties will be freed before setting; patch for
924 * other types - see @c handle_widget_property(). */
925 void stash_group_add_widget_property(StashGroup
*group
, gpointer setting
,
926 const gchar
*key_name
, gpointer default_value
, StashWidgetID widget_id
,
927 const gchar
*property_name
, GType type
)
930 type
= object_get_property_type(G_OBJECT(widget_id
), property_name
);
932 add_widget_pref(group
, type
, setting
, key_name
, default_value
,
933 G_TYPE_PARAM
, widget_id
)->extra
.property_name
= property_name
;
945 struct StashTreeValue
947 const gchar
*group_name
;
951 gchararray tree_string
;
956 typedef struct StashTreeValue StashTreeValue
;
959 static void stash_tree_renderer_set_data(GtkCellLayout
*cell_layout
, GtkCellRenderer
*cell
,
960 GtkTreeModel
*model
, GtkTreeIter
*iter
, gpointer user_data
)
962 GType cell_type
= GPOINTER_TO_SIZE(user_data
);
963 StashTreeValue
*value
;
965 gboolean matches_type
;
967 gtk_tree_model_get(model
, iter
, STASH_TREE_VALUE
, &value
, -1);
969 matches_type
= pref
->setting_type
== cell_type
;
970 g_object_set(cell
, "visible", matches_type
, "sensitive", matches_type
,
971 cell_type
== G_TYPE_BOOLEAN
? "activatable" : "editable", matches_type
, NULL
);
975 switch (pref
->setting_type
)
978 g_object_set(cell
, "active", value
->data
.tree_int
, NULL
);
982 gchar
*text
= g_strdup_printf("%d", value
->data
.tree_int
);
983 g_object_set(cell
, "text", text
, NULL
);
988 g_object_set(cell
, "text", value
->data
.tree_string
, NULL
);
995 static void stash_tree_renderer_edited(gchar
*path_str
, gchar
*new_text
, GtkTreeModel
*model
)
999 StashTreeValue
*value
;
1002 path
= gtk_tree_path_new_from_string(path_str
);
1003 gtk_tree_model_get_iter(model
, &iter
, path
);
1004 gtk_tree_model_get(model
, &iter
, STASH_TREE_VALUE
, &value
, -1);
1007 switch (pref
->setting_type
)
1009 case G_TYPE_BOOLEAN
:
1010 value
->data
.tree_int
= !value
->data
.tree_int
;
1013 value
->data
.tree_int
= atoi(new_text
);
1016 SETPTR(value
->data
.tree_string
, g_strdup(new_text
));
1020 gtk_tree_model_row_changed(model
, path
, &iter
);
1021 gtk_tree_path_free(path
);
1025 static void stash_tree_boolean_toggled(GtkCellRendererToggle
*cell
, gchar
*path_str
,
1026 GtkTreeModel
*model
)
1028 stash_tree_renderer_edited(path_str
, NULL
, model
);
1032 static void stash_tree_string_edited(GtkCellRenderer
*cell
, gchar
*path_str
, gchar
*new_text
,
1033 GtkTreeModel
*model
)
1035 stash_tree_renderer_edited(path_str
, new_text
, model
);
1039 static gboolean
stash_tree_discard_value(GtkTreeModel
*model
, GtkTreePath
*path
,
1040 GtkTreeIter
*iter
, gpointer user_data
)
1042 StashTreeValue
*value
;
1044 gtk_tree_model_get(model
, iter
, STASH_TREE_VALUE
, &value
, -1);
1045 if (value
->pref
->setting_type
== G_TYPE_STRING
)
1046 g_free(value
->data
.tree_string
);
1053 static void stash_tree_destroy_cb(GtkWidget
*widget
, gpointer user_data
)
1055 GtkTreeModel
*model
= gtk_tree_view_get_model(GTK_TREE_VIEW(widget
));
1056 gtk_tree_model_foreach(model
, stash_tree_discard_value
, NULL
);
1060 static void stash_tree_append_pref(StashGroup
*group
, StashPref
*entry
, GtkListStore
*store
,
1064 StashTreeValue
*value
;
1066 value
= g_new0(StashTreeValue
, 1);
1068 value
->group_name
= group
->name
;
1069 value
->pref
= entry
;
1071 gtk_list_store_append(store
, &iter
);
1072 gtk_list_store_set(store
, &iter
, STASH_TREE_NAME
, entry
->key_name
,
1073 STASH_TREE_VALUE
, value
, -1);
1077 static void stash_tree_append_prefs(GPtrArray
*group_array
,
1078 GtkListStore
*store
, PrefAction action
)
1084 foreach_ptr_array(group
, i
, group_array
)
1088 foreach_ptr_array(entry
, j
, group
->entries
)
1089 stash_tree_append_pref(group
, entry
, store
, action
);
1095 /* Setups a simple editor for stash preferences based on the widget arguments.
1096 * group_array - Array of groups which's settings will be edited.
1097 * tree - GtkTreeView in which to edit the preferences. Must be empty. */
1098 void stash_tree_setup(GPtrArray
*group_array
, GtkTreeView
*tree
)
1100 GtkListStore
*store
;
1101 GtkTreeModel
*model
;
1102 GtkCellRenderer
*cell
;
1103 GtkTreeViewColumn
*column
;
1104 GtkAdjustment
*adjustment
;
1106 store
= gtk_list_store_new(STASH_TREE_COUNT
, G_TYPE_STRING
, G_TYPE_POINTER
);
1107 stash_tree_append_prefs(group_array
, store
, PREF_DISPLAY
);
1108 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store
), STASH_TREE_NAME
,
1109 GTK_SORT_ASCENDING
);
1111 model
= GTK_TREE_MODEL(store
);
1112 gtk_tree_view_set_model(tree
, model
);
1113 g_object_unref(G_OBJECT(store
));
1114 g_signal_connect(tree
, "destroy", G_CALLBACK(stash_tree_destroy_cb
), NULL
);
1116 cell
= gtk_cell_renderer_text_new();
1117 column
= gtk_tree_view_column_new_with_attributes(_("Name"), cell
, "text",
1118 STASH_TREE_NAME
, NULL
);
1119 gtk_tree_view_column_set_sort_column_id(column
, STASH_TREE_NAME
);
1120 gtk_tree_view_column_set_sort_indicator(column
, TRUE
);
1121 gtk_tree_view_append_column(tree
, column
);
1123 column
= gtk_tree_view_column_new();
1124 gtk_tree_view_column_set_title(column
, _("Value"));
1125 gtk_tree_view_append_column(tree
, column
);
1126 /* boolean renderer */
1127 cell
= gtk_cell_renderer_toggle_new();
1128 g_signal_connect(cell
, "toggled", G_CALLBACK(stash_tree_boolean_toggled
), model
);
1129 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(column
), cell
, FALSE
);
1130 gtk_cell_layout_set_cell_data_func(GTK_CELL_LAYOUT(column
), cell
,
1131 stash_tree_renderer_set_data
, GSIZE_TO_POINTER(G_TYPE_BOOLEAN
), NULL
);
1132 /* string renderer */
1133 cell
= gtk_cell_renderer_text_new();
1134 g_object_set(cell
, "ellipsize", PANGO_ELLIPSIZE_END
, NULL
);
1135 g_signal_connect(cell
, "edited", G_CALLBACK(stash_tree_string_edited
), model
);
1136 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(column
), cell
, TRUE
);
1137 gtk_cell_layout_set_cell_data_func(GTK_CELL_LAYOUT(column
), cell
,
1138 stash_tree_renderer_set_data
, GSIZE_TO_POINTER(G_TYPE_STRING
), NULL
);
1139 /* integer renderer */
1140 cell
= gtk_cell_renderer_spin_new();
1141 adjustment
= GTK_ADJUSTMENT(gtk_adjustment_new(0, G_MININT
, G_MAXINT
, 1, 10, 0));
1142 g_object_set(cell
, "adjustment", adjustment
, NULL
);
1143 g_signal_connect(cell
, "edited", G_CALLBACK(stash_tree_string_edited
), model
);
1144 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(column
), cell
, FALSE
);
1145 gtk_cell_layout_set_cell_data_func(GTK_CELL_LAYOUT(column
), cell
,
1146 stash_tree_renderer_set_data
, GSIZE_TO_POINTER(G_TYPE_INT
), NULL
);
1150 static void stash_tree_display_pref(StashTreeValue
*value
, StashPref
*entry
)
1152 switch (entry
->setting_type
)
1154 case G_TYPE_BOOLEAN
:
1155 value
->data
.tree_int
= *(gboolean
*) entry
->setting
;
1158 value
->data
.tree_int
= *(gint
*) entry
->setting
;
1161 SETPTR(value
->data
.tree_string
, g_strdup(*(gchararray
*) entry
->setting
));
1164 g_warning("Unhandled type for %s::%s in %s()!", value
->group_name
,
1165 entry
->key_name
, G_STRFUNC
);
1170 static void stash_tree_update_pref(StashTreeValue
*value
, StashPref
*entry
)
1172 switch (entry
->setting_type
)
1174 case G_TYPE_BOOLEAN
:
1175 *(gboolean
*) entry
->setting
= value
->data
.tree_int
;
1178 *(gint
*) entry
->setting
= value
->data
.tree_int
;
1182 gchararray
*text
= entry
->setting
;
1183 SETPTR(*text
, g_strdup(value
->data
.tree_string
));
1187 g_warning("Unhandled type for %s::%s in %s()!", value
->group_name
,
1188 entry
->key_name
, G_STRFUNC
);
1193 static void stash_tree_action(GtkTreeModel
*model
, PrefAction action
)
1196 gboolean valid
= gtk_tree_model_get_iter_first(model
, &iter
);
1197 StashTreeValue
*value
;
1201 gtk_tree_model_get(model
, &iter
, STASH_TREE_VALUE
, &value
, -1);
1206 stash_tree_display_pref(value
, value
->pref
);
1209 stash_tree_update_pref(value
, value
->pref
);
1212 valid
= gtk_tree_model_iter_next(model
, &iter
);
1217 void stash_tree_display(GtkTreeView
*tree
)
1219 stash_tree_action(gtk_tree_view_get_model(tree
), PREF_DISPLAY
);
1223 void stash_tree_update(GtkTreeView
*tree
)
1225 stash_tree_action(gtk_tree_view_get_model(tree
), PREF_UPDATE
);