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 guint refcount
; /* ref count for GBoxed implementation */
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. */
279 void stash_group_load_from_key_file(StashGroup
*group
, GKeyFile
*keyfile
)
281 keyfile_action(SETTING_READ
, group
, keyfile
);
285 /** Writes group settings into key values in @a keyfile.
286 * @a keyfile is usually written to a configuration file afterwards.
288 * @param keyfile . */
290 void stash_group_save_to_key_file(StashGroup
*group
, GKeyFile
*keyfile
)
292 keyfile_action(SETTING_WRITE
, group
, keyfile
);
296 /** Reads group settings from a configuration file using @c GKeyFile.
297 * @note Stash settings will be initialized to defaults if the keyfile
298 * couldn't be loaded from disk.
300 * @param filename Filename of the file to read, in locale encoding.
301 * @return @c TRUE if a key file could be loaded.
302 * @see stash_group_load_from_key_file().
305 gboolean
stash_group_load_from_file(StashGroup
*group
, const gchar
*filename
)
310 keyfile
= g_key_file_new();
311 ret
= g_key_file_load_from_file(keyfile
, filename
, 0, NULL
);
312 /* even on failure we load settings to apply defaults */
313 stash_group_load_from_key_file(group
, keyfile
);
315 g_key_file_free(keyfile
);
320 /** Writes group settings to a configuration file using @c GKeyFile.
323 * @param filename Filename of the file to write, in locale encoding.
324 * @param flags Keyfile options - @c G_KEY_FILE_NONE is the most efficient.
325 * @return 0 if the file was successfully written, otherwise the @c errno of the
326 * failed operation is returned.
327 * @see stash_group_save_to_key_file().
330 gint
stash_group_save_to_file(StashGroup
*group
, const gchar
*filename
,
337 keyfile
= g_key_file_new();
338 /* if we need to keep comments or translations, try to load first */
340 g_key_file_load_from_file(keyfile
, filename
, flags
, NULL
);
342 stash_group_save_to_key_file(group
, keyfile
);
343 data
= g_key_file_to_data(keyfile
, NULL
, NULL
);
344 ret
= utils_write_file(filename
, data
);
346 g_key_file_free(keyfile
);
351 static void free_stash_pref(StashPref
*pref
)
353 if (pref
->widget_type
== GTK_TYPE_RADIO_BUTTON
)
354 g_free(pref
->extra
.radio_buttons
);
356 g_slice_free(StashPref
, pref
);
360 /** Creates a new group.
361 * @param name Name used for @c GKeyFile group.
364 StashGroup
*stash_group_new(const gchar
*name
)
366 StashGroup
*group
= g_slice_new0(StashGroup
);
369 group
->entries
= g_ptr_array_new_with_free_func((GDestroyNotify
) free_stash_pref
);
370 group
->use_defaults
= TRUE
;
376 /** Frees the memory allocated for setting values in a group.
377 * Useful e.g. to avoid freeing strings individually.
378 * @note This is *not* called by stash_group_free().
381 void stash_group_free_settings(StashGroup
*group
)
386 foreach_ptr_array(entry
, i
, group
->entries
)
388 if (entry
->setting_type
== G_TYPE_STRING
)
389 g_free(*(gchararray
*) entry
->setting
);
390 else if (entry
->setting_type
== G_TYPE_STRV
)
391 g_strfreev(*(gchararray
**) entry
->setting
);
395 *(gpointer
**) entry
->setting
= NULL
;
400 static StashGroup
*stash_group_dup(StashGroup
*src
)
402 g_atomic_int_inc(&src
->refcount
);
411 void stash_group_free(StashGroup
*group
)
413 if (g_atomic_int_dec_and_test(&group
->refcount
))
415 g_ptr_array_free(group
->entries
, TRUE
);
416 g_slice_free(StashGroup
, group
);
421 /** Gets the GBoxed-derived GType for StashGroup
423 * @return StashGroup type . */
425 GType
stash_group_get_type(void);
427 G_DEFINE_BOXED_TYPE(StashGroup
, stash_group
, stash_group_dup
, stash_group_free
);
430 /* Used for selecting groups passed to stash_tree_setup().
431 * @c FALSE by default. */
432 void stash_group_set_various(StashGroup
*group
, gboolean various
)
434 group
->various
= various
;
438 /* When @c FALSE, Stash doesn't change the setting if there is no keyfile entry, so it
439 * remains whatever it was initialized/set to by user code.
440 * @c TRUE by default. */
441 void stash_group_set_use_defaults(StashGroup
*group
, gboolean use_defaults
)
443 group
->use_defaults
= use_defaults
;
448 add_pref(StashGroup
*group
, GType type
, gpointer setting
,
449 const gchar
*key_name
, gpointer default_value
)
451 StashPref init
= {type
, setting
, key_name
, default_value
, G_TYPE_NONE
, NULL
, {NULL
}};
452 StashPref
*entry
= g_slice_new(StashPref
);
456 /* init any pointer settings to NULL so they can be freed later */
457 if (type
== G_TYPE_STRING
||
459 if (group
->use_defaults
)
460 *(gpointer
**)setting
= NULL
;
462 g_ptr_array_add(group
->entries
, entry
);
467 /** Adds boolean setting.
469 * @param setting Address of setting variable.
470 * @param key_name Name for key in a @c GKeyFile.
471 * @param default_value Value to use if the key doesn't exist when loading. */
473 void stash_group_add_boolean(StashGroup
*group
, gboolean
*setting
,
474 const gchar
*key_name
, gboolean default_value
)
476 add_pref(group
, G_TYPE_BOOLEAN
, setting
, key_name
, GINT_TO_POINTER(default_value
));
480 /** Adds integer setting.
482 * @param setting Address of setting variable.
483 * @param key_name Name for key in a @c GKeyFile.
484 * @param default_value Value to use if the key doesn't exist when loading. */
486 void stash_group_add_integer(StashGroup
*group
, gint
*setting
,
487 const gchar
*key_name
, gint default_value
)
489 add_pref(group
, G_TYPE_INT
, setting
, key_name
, GINT_TO_POINTER(default_value
));
493 /** Adds string setting.
494 * The contents of @a setting will be initialized to @c NULL.
496 * @param setting Address of setting variable.
497 * @param key_name Name for key in a @c GKeyFile.
498 * @param default_value @nullable String to copy if the key doesn't exist when loading, or @c NULL. */
500 void stash_group_add_string(StashGroup
*group
, gchar
**setting
,
501 const gchar
*key_name
, const gchar
*default_value
)
503 add_pref(group
, G_TYPE_STRING
, setting
, key_name
, (gpointer
)default_value
);
507 /** Adds string vector setting (array of strings).
508 * The contents of @a setting will be initialized to @c NULL.
510 * @param setting Address of setting variable.
511 * @param key_name Name for key in a @c GKeyFile.
512 * @param default_value Vector to copy if the key doesn't exist when loading. Usually @c NULL. */
514 void stash_group_add_string_vector(StashGroup
*group
, gchar
***setting
,
515 const gchar
*key_name
, const gchar
**default_value
)
517 add_pref(group
, G_TYPE_STRV
, setting
, key_name
, (gpointer
)default_value
);
521 /* *** GTK-related functions *** */
523 static void handle_toggle_button(GtkWidget
*widget
, gboolean
*setting
,
529 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget
), *setting
);
532 *setting
= gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget
));
538 static void handle_spin_button(GtkWidget
*widget
, StashPref
*entry
,
541 gint
*setting
= entry
->setting
;
543 g_assert(entry
->setting_type
== G_TYPE_INT
); /* only int spin prefs */
548 gtk_spin_button_set_value(GTK_SPIN_BUTTON(widget
), *setting
);
551 /* if the widget is focussed, the value might not be updated */
552 gtk_spin_button_update(GTK_SPIN_BUTTON(widget
));
553 *setting
= gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(widget
));
559 static void handle_combo_box(GtkWidget
*widget
, StashPref
*entry
,
562 gint
*setting
= entry
->setting
;
567 gtk_combo_box_set_active(GTK_COMBO_BOX(widget
), *setting
);
570 *setting
= gtk_combo_box_get_active(GTK_COMBO_BOX(widget
));
576 static void handle_entry(GtkWidget
*widget
, StashPref
*entry
,
579 gchararray
*setting
= entry
->setting
;
584 gtk_entry_set_text(GTK_ENTRY(widget
), *setting
);
588 *setting
= g_strdup(gtk_entry_get_text(GTK_ENTRY(widget
)));
594 static void handle_combo_box_entry(GtkWidget
*widget
, StashPref
*entry
,
597 widget
= gtk_bin_get_child(GTK_BIN(widget
));
598 handle_entry(widget
, entry
, action
);
602 /* taken from Glade 2.x generated support.c */
604 lookup_widget(GtkWidget
*widget
, const gchar
*widget_name
)
606 GtkWidget
*parent
, *found_widget
;
608 g_return_val_if_fail(widget
!= NULL
, NULL
);
609 g_return_val_if_fail(widget_name
!= NULL
, NULL
);
613 if (GTK_IS_MENU(widget
))
614 parent
= gtk_menu_get_attach_widget(GTK_MENU(widget
));
616 parent
= gtk_widget_get_parent(widget
);
618 parent
= (GtkWidget
*) g_object_get_data(G_OBJECT(widget
), "GladeParentKey");
624 found_widget
= (GtkWidget
*) g_object_get_data(G_OBJECT(widget
), widget_name
);
625 if (G_UNLIKELY(found_widget
== NULL
))
626 g_warning("Widget not found: %s", widget_name
);
632 get_widget(GtkWidget
*owner
, StashWidgetID widget_id
)
637 widget
= lookup_widget(owner
, (const gchar
*)widget_id
);
639 widget
= (GtkWidget
*)widget_id
;
641 if (!GTK_IS_WIDGET(widget
))
643 g_warning("Unknown widget in %s()!", G_STRFUNC
);
650 static void handle_radio_button(GtkWidget
*widget
, gint enum_id
, gboolean
*setting
,
656 if (*setting
== enum_id
)
657 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget
), TRUE
);
660 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget
)))
667 static void handle_radio_buttons(GtkWidget
*owner
, StashPref
*entry
,
670 EnumWidget
*field
= entry
->extra
.radio_buttons
;
672 GtkWidget
*widget
= NULL
;
676 widget
= get_widget(owner
, field
->widget_id
);
682 handle_radio_button(widget
, field
->enum_id
, entry
->setting
, action
);
684 if (!field
->widget_id
)
687 if (g_slist_length(gtk_radio_button_get_group(GTK_RADIO_BUTTON(widget
))) != count
)
688 g_warning("Missing/invalid radio button widget IDs found!");
692 static void handle_widget_property(GtkWidget
*widget
, StashPref
*entry
,
695 GObject
*object
= G_OBJECT(widget
);
696 const gchar
*name
= entry
->extra
.property_name
;
701 if (entry
->setting_type
== G_TYPE_BOOLEAN
)
702 g_object_set(object
, name
, *(gboolean
*)entry
->setting
, NULL
);
703 else if (entry
->setting_type
== G_TYPE_INT
)
704 g_object_set(object
, name
, *(gint
*)entry
->setting
, NULL
);
705 else if (entry
->setting_type
== G_TYPE_STRING
)
706 g_object_set(object
, name
, *(gchararray
*)entry
->setting
, NULL
);
707 else if (entry
->setting_type
== G_TYPE_STRV
)
708 g_object_set(object
, name
, *(gchararray
**)entry
->setting
, NULL
);
711 g_warning("Unhandled type %s for %s in %s()!", g_type_name(entry
->setting_type
),
712 entry
->key_name
, G_STRFUNC
);
716 if (entry
->setting_type
== G_TYPE_STRING
)
717 g_free(*(gchararray
*)entry
->setting
);
718 else if (entry
->setting_type
== G_TYPE_STRV
)
719 g_strfreev(*(gchararray
**)entry
->setting
);
721 g_object_get(object
, name
, entry
->setting
, NULL
);
727 static void pref_action(PrefAction action
, StashGroup
*group
, GtkWidget
*owner
)
732 foreach_ptr_array(entry
, i
, group
->entries
)
736 /* ignore settings with no widgets */
737 if (entry
->widget_type
== G_TYPE_NONE
)
740 /* radio buttons have several widgets */
741 if (entry
->widget_type
== GTK_TYPE_RADIO_BUTTON
)
743 handle_radio_buttons(owner
, entry
, action
);
747 widget
= get_widget(owner
, entry
->widget_id
);
750 g_warning("Unknown widget for %s::%s in %s()!", group
->name
, entry
->key_name
,
755 /* note: can't use switch for GTK_TYPE macros */
756 if (entry
->widget_type
== GTK_TYPE_TOGGLE_BUTTON
)
757 handle_toggle_button(widget
, entry
->setting
, action
);
758 else if (entry
->widget_type
== GTK_TYPE_SPIN_BUTTON
)
759 handle_spin_button(widget
, entry
, action
);
760 else if (entry
->widget_type
== GTK_TYPE_COMBO_BOX
)
761 handle_combo_box(widget
, entry
, action
);
762 else if (entry
->widget_type
== TYPE_COMBO_BOX_ENTRY
)
763 handle_combo_box_entry(widget
, entry
, action
);
764 else if (entry
->widget_type
== GTK_TYPE_ENTRY
)
765 handle_entry(widget
, entry
, action
);
766 else if (entry
->widget_type
== G_TYPE_PARAM
)
767 handle_widget_property(widget
, entry
, action
);
769 g_warning("Unhandled type for %s::%s in %s()!", group
->name
, entry
->key_name
,
775 /** Applies Stash settings to widgets, usually called before displaying the widgets.
776 * The @a owner argument depends on which type you use for @ref StashWidgetID.
778 * @param owner If non-NULL, used to lookup widgets by name, otherwise
779 * widget pointers are assumed.
780 * @see stash_group_update(). */
782 void stash_group_display(StashGroup
*group
, GtkWidget
*owner
)
784 pref_action(PREF_DISPLAY
, group
, owner
);
788 /** Applies widget values to Stash settings, usually called after displaying the widgets.
789 * The @a owner argument depends on which type you use for @ref StashWidgetID.
791 * @param owner If non-NULL, used to lookup widgets by name, otherwise
792 * widget pointers are assumed.
793 * @see stash_group_display(). */
795 void stash_group_update(StashGroup
*group
, GtkWidget
*owner
)
797 pref_action(PREF_UPDATE
, group
, owner
);
802 add_widget_pref(StashGroup
*group
, GType setting_type
, gpointer setting
,
803 const gchar
*key_name
, gpointer default_value
,
804 GType widget_type
, StashWidgetID widget_id
)
807 add_pref(group
, setting_type
, setting
, key_name
, default_value
);
809 entry
->widget_type
= widget_type
;
810 entry
->widget_id
= widget_id
;
815 /** Adds a @c GtkToggleButton (or @c GtkCheckButton) widget pref.
817 * @param setting Address of setting variable.
818 * @param key_name Name for key in a @c GKeyFile.
819 * @param default_value Value to use if the key doesn't exist when loading.
820 * @param widget_id @c GtkWidget pointer or string to lookup widget later.
821 * @see stash_group_add_radio_buttons(). */
823 void stash_group_add_toggle_button(StashGroup
*group
, gboolean
*setting
,
824 const gchar
*key_name
, gboolean default_value
, StashWidgetID widget_id
)
826 add_widget_pref(group
, G_TYPE_BOOLEAN
, setting
, key_name
, GINT_TO_POINTER(default_value
),
827 GTK_TYPE_TOGGLE_BUTTON
, widget_id
);
831 /** Adds a @c GtkRadioButton widget group pref.
833 * @param setting Address of setting variable.
834 * @param key_name Name for key in a @c GKeyFile.
835 * @param default_value Value to use if the key doesn't exist when loading.
836 * @param widget_id @c GtkWidget pointer or string to lookup widget later.
837 * @param enum_id Enum value for @a widget_id.
838 * @param ... pairs of @a widget_id, @a enum_id.
839 * Example (using widget lookup strings, but widget pointers can also work):
842 * stash_group_add_radio_buttons(group, &which_one_setting, "which_one", BAR,
843 * "radio_foo", FOO, "radio_bar", BAR, NULL);
846 void stash_group_add_radio_buttons(StashGroup
*group
, gint
*setting
,
847 const gchar
*key_name
, gint default_value
,
848 StashWidgetID widget_id
, gint enum_id
, ...)
851 add_widget_pref(group
, G_TYPE_INT
, setting
, key_name
, GINT_TO_POINTER(default_value
),
852 GTK_TYPE_RADIO_BUTTON
, NULL
);
855 EnumWidget
*item
, *array
;
857 /* count pairs of args */
858 va_start(args
, enum_id
);
861 if (!va_arg(args
, gpointer
))
868 array
= g_new0(EnumWidget
, count
+ 1);
869 entry
->extra
.radio_buttons
= array
;
871 va_start(args
, enum_id
);
872 foreach_c_array(item
, array
, count
)
877 item
->widget_id
= widget_id
;
878 item
->enum_id
= enum_id
;
882 item
->widget_id
= va_arg(args
, gpointer
);
883 item
->enum_id
= va_arg(args
, gint
);
890 /** Adds a @c GtkSpinButton widget pref.
892 * @param setting Address of setting variable.
893 * @param key_name Name for key in a @c GKeyFile.
894 * @param default_value Value to use if the key doesn't exist when loading.
895 * @param widget_id @c GtkWidget pointer or string to lookup widget later. */
897 void stash_group_add_spin_button_integer(StashGroup
*group
, gint
*setting
,
898 const gchar
*key_name
, gint default_value
, StashWidgetID widget_id
)
900 add_widget_pref(group
, G_TYPE_INT
, setting
, key_name
, GINT_TO_POINTER(default_value
),
901 GTK_TYPE_SPIN_BUTTON
, widget_id
);
905 /** Adds a @c GtkComboBox widget pref.
907 * @param setting Address of setting variable.
908 * @param key_name Name for key in a @c GKeyFile.
909 * @param default_value Value to use if the key doesn't exist when loading.
910 * @param widget_id @c GtkWidget pointer or string to lookup widget later.
911 * @see stash_group_add_combo_box_entry(). */
913 void stash_group_add_combo_box(StashGroup
*group
, gint
*setting
,
914 const gchar
*key_name
, gint default_value
, StashWidgetID widget_id
)
916 add_widget_pref(group
, G_TYPE_INT
, setting
, key_name
, GINT_TO_POINTER(default_value
),
917 GTK_TYPE_COMBO_BOX
, widget_id
);
921 /** Adds a @c GtkComboBoxEntry widget pref.
923 * @param setting Address of setting variable.
924 * @param key_name Name for key in a @c GKeyFile.
925 * @param default_value Value to use if the key doesn't exist when loading.
926 * @param widget_id @c GtkWidget pointer or string to lookup widget later. */
927 /* We could maybe also have something like stash_group_add_combo_box_entry_with_menu()
928 * for the history list - or should that be stored as a separate setting? */
930 void stash_group_add_combo_box_entry(StashGroup
*group
, gchar
**setting
,
931 const gchar
*key_name
, const gchar
*default_value
, StashWidgetID widget_id
)
933 add_widget_pref(group
, G_TYPE_STRING
, setting
, key_name
, (gpointer
)default_value
,
934 TYPE_COMBO_BOX_ENTRY
, widget_id
);
938 /** Adds a @c GtkEntry widget pref.
940 * @param setting Address of setting variable.
941 * @param key_name Name for key in a @c GKeyFile.
942 * @param default_value Value to use if the key doesn't exist when loading.
943 * @param widget_id @c GtkWidget pointer or string to lookup widget later. */
945 void stash_group_add_entry(StashGroup
*group
, gchar
**setting
,
946 const gchar
*key_name
, const gchar
*default_value
, StashWidgetID widget_id
)
948 add_widget_pref(group
, G_TYPE_STRING
, setting
, key_name
, (gpointer
)default_value
,
949 GTK_TYPE_ENTRY
, widget_id
);
953 static GType
object_get_property_type(GObject
*object
, const gchar
*property_name
)
955 GObjectClass
*klass
= G_OBJECT_GET_CLASS(object
);
958 ps
= g_object_class_find_property(klass
, property_name
);
959 return ps
->value_type
;
963 /** Adds a widget's read/write property to the stash group.
964 * The property will be set when calling
965 * stash_group_display(), and read when calling stash_group_update().
967 * @param setting Address of e.g. an integer if using an integer property.
968 * @param key_name Name for key in a @c GKeyFile.
969 * @param default_value Value to use if the key doesn't exist when loading.
970 * Should be cast into a pointer e.g. with @c GINT_TO_POINTER().
971 * @param widget_id @c GtkWidget pointer or string to lookup widget later.
972 * @param property_name .
973 * @param type can be @c 0 if passing a @c GtkWidget as the @a widget_id argument to look it up from the
975 * @warning Currently only string GValue properties will be freed before setting; patch for
976 * other types - see @c handle_widget_property(). */
978 void stash_group_add_widget_property(StashGroup
*group
, gpointer setting
,
979 const gchar
*key_name
, gpointer default_value
, StashWidgetID widget_id
,
980 const gchar
*property_name
, GType type
)
983 type
= object_get_property_type(G_OBJECT(widget_id
), property_name
);
985 add_widget_pref(group
, type
, setting
, key_name
, default_value
,
986 G_TYPE_PARAM
, widget_id
)->extra
.property_name
= property_name
;
998 struct StashTreeValue
1000 const gchar
*group_name
;
1004 gchararray tree_string
;
1009 typedef struct StashTreeValue StashTreeValue
;
1012 static void stash_tree_renderer_set_data(GtkCellLayout
*cell_layout
, GtkCellRenderer
*cell
,
1013 GtkTreeModel
*model
, GtkTreeIter
*iter
, gpointer user_data
)
1015 GType cell_type
= GPOINTER_TO_SIZE(user_data
);
1016 StashTreeValue
*value
;
1018 gboolean matches_type
;
1020 gtk_tree_model_get(model
, iter
, STASH_TREE_VALUE
, &value
, -1);
1022 matches_type
= pref
->setting_type
== cell_type
;
1023 g_object_set(cell
, "visible", matches_type
, "sensitive", matches_type
,
1024 cell_type
== G_TYPE_BOOLEAN
? "activatable" : "editable", matches_type
, NULL
);
1028 switch (pref
->setting_type
)
1030 case G_TYPE_BOOLEAN
:
1031 g_object_set(cell
, "active", value
->data
.tree_int
, NULL
);
1035 gchar
*text
= g_strdup_printf("%d", value
->data
.tree_int
);
1036 g_object_set(cell
, "text", text
, NULL
);
1041 g_object_set(cell
, "text", value
->data
.tree_string
, NULL
);
1048 static void stash_tree_renderer_edited(gchar
*path_str
, gchar
*new_text
, GtkTreeModel
*model
)
1052 StashTreeValue
*value
;
1055 path
= gtk_tree_path_new_from_string(path_str
);
1056 gtk_tree_model_get_iter(model
, &iter
, path
);
1057 gtk_tree_model_get(model
, &iter
, STASH_TREE_VALUE
, &value
, -1);
1060 switch (pref
->setting_type
)
1062 case G_TYPE_BOOLEAN
:
1063 value
->data
.tree_int
= !value
->data
.tree_int
;
1066 value
->data
.tree_int
= atoi(new_text
);
1069 SETPTR(value
->data
.tree_string
, g_strdup(new_text
));
1073 gtk_tree_model_row_changed(model
, path
, &iter
);
1074 gtk_tree_path_free(path
);
1078 static void stash_tree_boolean_toggled(GtkCellRendererToggle
*cell
, gchar
*path_str
,
1079 GtkTreeModel
*model
)
1081 stash_tree_renderer_edited(path_str
, NULL
, model
);
1085 static void stash_tree_string_edited(GtkCellRenderer
*cell
, gchar
*path_str
, gchar
*new_text
,
1086 GtkTreeModel
*model
)
1088 stash_tree_renderer_edited(path_str
, new_text
, model
);
1092 static gboolean
stash_tree_discard_value(GtkTreeModel
*model
, GtkTreePath
*path
,
1093 GtkTreeIter
*iter
, gpointer user_data
)
1095 StashTreeValue
*value
;
1097 gtk_tree_model_get(model
, iter
, STASH_TREE_VALUE
, &value
, -1);
1098 /* don't access value->pref as it might already have been freed */
1099 g_free(value
->data
.tree_string
);
1106 static void stash_tree_destroy_cb(GtkWidget
*widget
, gpointer user_data
)
1108 GtkTreeModel
*model
= gtk_tree_view_get_model(GTK_TREE_VIEW(widget
));
1109 gtk_tree_model_foreach(model
, stash_tree_discard_value
, NULL
);
1113 static void stash_tree_append_pref(StashGroup
*group
, StashPref
*entry
, GtkListStore
*store
,
1117 StashTreeValue
*value
;
1119 value
= g_new0(StashTreeValue
, 1);
1121 value
->group_name
= group
->name
;
1122 value
->pref
= entry
;
1124 gtk_list_store_append(store
, &iter
);
1125 gtk_list_store_set(store
, &iter
, STASH_TREE_NAME
, entry
->key_name
,
1126 STASH_TREE_VALUE
, value
, -1);
1130 static void stash_tree_append_prefs(GPtrArray
*group_array
,
1131 GtkListStore
*store
, PrefAction action
)
1137 foreach_ptr_array(group
, i
, group_array
)
1141 foreach_ptr_array(entry
, j
, group
->entries
)
1142 stash_tree_append_pref(group
, entry
, store
, action
);
1148 /* Setups a simple editor for stash preferences based on the widget arguments.
1149 * group_array - Array of groups which's settings will be edited.
1150 * tree - GtkTreeView in which to edit the preferences. Must be empty. */
1151 void stash_tree_setup(GPtrArray
*group_array
, GtkTreeView
*tree
)
1153 GtkListStore
*store
;
1154 GtkTreeModel
*model
;
1155 GtkCellRenderer
*cell
;
1156 GtkTreeViewColumn
*column
;
1157 GtkAdjustment
*adjustment
;
1159 store
= gtk_list_store_new(STASH_TREE_COUNT
, G_TYPE_STRING
, G_TYPE_POINTER
);
1160 stash_tree_append_prefs(group_array
, store
, PREF_DISPLAY
);
1161 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store
), STASH_TREE_NAME
,
1162 GTK_SORT_ASCENDING
);
1164 model
= GTK_TREE_MODEL(store
);
1165 gtk_tree_view_set_model(tree
, model
);
1166 g_object_unref(G_OBJECT(store
));
1167 g_signal_connect(tree
, "destroy", G_CALLBACK(stash_tree_destroy_cb
), NULL
);
1169 cell
= gtk_cell_renderer_text_new();
1170 column
= gtk_tree_view_column_new_with_attributes(_("Name"), cell
, "text",
1171 STASH_TREE_NAME
, NULL
);
1172 gtk_tree_view_column_set_sort_column_id(column
, STASH_TREE_NAME
);
1173 gtk_tree_view_column_set_sort_indicator(column
, TRUE
);
1174 gtk_tree_view_append_column(tree
, column
);
1176 column
= gtk_tree_view_column_new();
1177 gtk_tree_view_column_set_title(column
, _("Value"));
1178 gtk_tree_view_append_column(tree
, column
);
1179 /* boolean renderer */
1180 cell
= gtk_cell_renderer_toggle_new();
1181 g_signal_connect(cell
, "toggled", G_CALLBACK(stash_tree_boolean_toggled
), model
);
1182 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(column
), cell
, FALSE
);
1183 gtk_cell_layout_set_cell_data_func(GTK_CELL_LAYOUT(column
), cell
,
1184 stash_tree_renderer_set_data
, GSIZE_TO_POINTER(G_TYPE_BOOLEAN
), NULL
);
1185 /* string renderer */
1186 cell
= gtk_cell_renderer_text_new();
1187 g_object_set(cell
, "ellipsize", PANGO_ELLIPSIZE_END
, NULL
);
1188 g_signal_connect(cell
, "edited", G_CALLBACK(stash_tree_string_edited
), model
);
1189 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(column
), cell
, TRUE
);
1190 gtk_cell_layout_set_cell_data_func(GTK_CELL_LAYOUT(column
), cell
,
1191 stash_tree_renderer_set_data
, GSIZE_TO_POINTER(G_TYPE_STRING
), NULL
);
1192 /* integer renderer */
1193 cell
= gtk_cell_renderer_spin_new();
1194 adjustment
= GTK_ADJUSTMENT(gtk_adjustment_new(0, G_MININT
, G_MAXINT
, 1, 10, 0));
1195 g_object_set(cell
, "adjustment", adjustment
, NULL
);
1196 g_signal_connect(cell
, "edited", G_CALLBACK(stash_tree_string_edited
), model
);
1197 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(column
), cell
, FALSE
);
1198 gtk_cell_layout_set_cell_data_func(GTK_CELL_LAYOUT(column
), cell
,
1199 stash_tree_renderer_set_data
, GSIZE_TO_POINTER(G_TYPE_INT
), NULL
);
1203 static void stash_tree_display_pref(StashTreeValue
*value
, StashPref
*entry
)
1205 switch (entry
->setting_type
)
1207 case G_TYPE_BOOLEAN
:
1208 value
->data
.tree_int
= *(gboolean
*) entry
->setting
;
1211 value
->data
.tree_int
= *(gint
*) entry
->setting
;
1214 SETPTR(value
->data
.tree_string
, g_strdup(*(gchararray
*) entry
->setting
));
1217 g_warning("Unhandled type for %s::%s in %s()!", value
->group_name
,
1218 entry
->key_name
, G_STRFUNC
);
1223 static void stash_tree_update_pref(StashTreeValue
*value
, StashPref
*entry
)
1225 switch (entry
->setting_type
)
1227 case G_TYPE_BOOLEAN
:
1228 *(gboolean
*) entry
->setting
= value
->data
.tree_int
;
1231 *(gint
*) entry
->setting
= value
->data
.tree_int
;
1235 gchararray
*text
= entry
->setting
;
1236 SETPTR(*text
, g_strdup(value
->data
.tree_string
));
1240 g_warning("Unhandled type for %s::%s in %s()!", value
->group_name
,
1241 entry
->key_name
, G_STRFUNC
);
1246 static void stash_tree_action(GtkTreeModel
*model
, PrefAction action
)
1249 gboolean valid
= gtk_tree_model_get_iter_first(model
, &iter
);
1250 StashTreeValue
*value
;
1254 gtk_tree_model_get(model
, &iter
, STASH_TREE_VALUE
, &value
, -1);
1259 stash_tree_display_pref(value
, value
->pref
);
1262 stash_tree_update_pref(value
, value
->pref
);
1265 valid
= gtk_tree_model_iter_next(model
, &iter
);
1270 void stash_tree_display(GtkTreeView
*tree
)
1272 stash_tree_action(gtk_tree_view_get_model(tree
), PREF_DISPLAY
);
1276 void stash_tree_update(GtkTreeView
*tree
)
1278 stash_tree_action(gtk_tree_view_get_model(tree
), PREF_UPDATE
);