2 * stash.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2008 The Geany contributors
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 * Lightweight library for reading/writing @c GKeyFile settings and synchronizing widgets with
26 * Note: Stash should only depend on GLib and GTK, but currently has some minor
27 * dependencies on Geany's utils.c.
30 * 'Setting' is used only for data stored on disk or in memory.
31 * 'Pref' can also include visual widget information.
33 * @section Memory Usage
34 * Stash will not duplicate strings if they are normally static arrays, such as
35 * keyfile group names and key names, string default values, widget_id names, property names.
37 * @section String Settings
38 * String settings and other dynamically allocated settings will be initialized to NULL when
39 * added to a StashGroup (so they can safely be reassigned later).
41 * @section Widget Support
42 * Widgets very commonly used in configuration dialogs will be supported with their own function.
43 * Widgets less commonly used such as @c GtkExpander or widget settings that aren't commonly needed
44 * to be persistent won't be directly supported, to keep the library lightweight. However, you can
45 * use stash_group_add_widget_property() to also save these settings for any read/write widget
46 * property. Macros could be added for common widget properties such as @c GtkExpander:"expanded".
48 * @section settings-example Settings Example
49 * Here we have some settings for a cup - whether it is made of porcelain, who made it,
50 * how many we have, and how much they cost. (Yes, it's a stupid example).
51 * @include stash-example.c
52 * @note You might want to handle the warning/error conditions differently from above.
54 * @section prefs-example GUI Prefs Example
55 * For prefs, it's the same as the above example but you tell Stash to add widget prefs instead of
58 * This example uses lookup strings for widgets as they are more flexible than widget pointers.
59 * Code to load and save the settings is omitted - see the first example instead.
61 * Here we show a dialog with a toggle button for whether the cup should have a handle.
62 * @include stash-gui-example.c
63 * @note This example should also work for other widget containers besides dialogs, e.g. popup menus.
66 /* Implementation Note
67 * We dynamically allocate prefs. It would be more efficient for user code to declare
68 * a static array of StashPref structs, but we don't do this because:
70 * * It would be more ugly (lots of casts and NULLs).
71 * * Less type checking.
72 * * The API & ABI would have to break when adding/changing fields.
74 * Usually the prefs code isn't what user code will spend most of its time doing, so this
75 * should be efficient enough.
80 #include "support.h" /* only for _("text") */
81 #include "utils.h" /* only for foreach_*, utils_get_setting_*(). Stash should not depend on Geany. */
83 #include <stdlib.h> /* only for atoi() */
86 /* GTK3 removed ComboBoxEntry, but we need a value to differentiate combo box with and
87 * without entries, and it must not collide with other GTypes */
88 #define TYPE_COMBO_BOX_ENTRY get_combo_box_entry_type()
89 static GType
get_combo_box_entry_type(void)
91 static gsize type
= 0;
92 if (g_once_init_enter(&type
))
94 GType g_type
= g_type_register_static_simple(GTK_TYPE_COMBO_BOX
, "dummy-combo-box-entry",
95 sizeof(GtkComboBoxClass
), NULL
, sizeof(GtkComboBox
), NULL
, G_TYPE_FLAG_ABSTRACT
);
96 g_once_init_leave(&type
, g_type
);
101 /* storage for StashPref default values */
110 GtkWidget
*widget_val
;
115 GType setting_type
; /* e.g. G_TYPE_INT */
116 gpointer setting
; /* Address of a variable */
117 const gchar
*key_name
;
118 union Value default_value
; /* Default value, as per setting_type above, e.g. .int_val */
119 GType widget_type
; /* e.g. GTK_TYPE_TOGGLE_BUTTON */
120 StashWidgetID widget_id
; /* (GtkWidget*) or (gchar*) */
123 struct EnumWidget
*radio_buttons
;
124 const gchar
*property_name
;
125 } extra
; /* extra fields depending on widget_type */
128 typedef struct StashPref StashPref
;
132 guint refcount
; /* ref count for GBoxed implementation */
133 const gchar
*name
; /* group name to use in the keyfile */
134 GPtrArray
*entries
; /* array of (StashPref*) */
135 gboolean various
; /* mark group for display/edit in stash treeview */
136 const gchar
*prefix
; /* text to display for Various UI instead of name */
137 gboolean use_defaults
; /* use default values if there's no keyfile entry */
140 typedef struct EnumWidget
142 StashWidgetID widget_id
;
148 typedef enum SettingAction
155 typedef enum PrefAction
163 static void handle_boolean_setting(StashGroup
*group
, StashPref
*se
,
164 GKeyFile
*config
, SettingAction action
)
166 gboolean
*setting
= se
->setting
;
171 *setting
= utils_get_setting_boolean(config
, group
->name
, se
->key_name
,
172 se
->default_value
.bool_val
);
175 g_key_file_set_boolean(config
, group
->name
, se
->key_name
, *setting
);
181 static void handle_double_setting(StashGroup
*group
, StashPref
*se
,
182 GKeyFile
*config
, SettingAction action
)
184 gdouble
*setting
= se
->setting
;
189 *setting
= utils_get_setting_double(config
, group
->name
, se
->key_name
,
190 se
->default_value
.double_val
);
193 g_key_file_set_double(config
, group
->name
, se
->key_name
, *setting
);
199 static void handle_integer_setting(StashGroup
*group
, StashPref
*se
,
200 GKeyFile
*config
, SettingAction action
)
202 gint
*setting
= se
->setting
;
207 *setting
= utils_get_setting_integer(config
, group
->name
, se
->key_name
,
208 se
->default_value
.int_val
);
211 g_key_file_set_integer(config
, group
->name
, se
->key_name
, *setting
);
217 static void handle_string_setting(StashGroup
*group
, StashPref
*se
,
218 GKeyFile
*config
, SettingAction action
)
220 gchararray
*setting
= se
->setting
;
226 *setting
= utils_get_setting_string(config
, group
->name
, se
->key_name
,
227 se
->default_value
.str_val
);
230 g_key_file_set_string(config
, group
->name
, se
->key_name
,
231 *setting
? *setting
: "");
237 static void handle_strv_setting(StashGroup
*group
, StashPref
*se
,
238 GKeyFile
*config
, SettingAction action
)
240 gchararray
**setting
= se
->setting
;
245 g_strfreev(*setting
);
246 *setting
= g_key_file_get_string_list(config
, group
->name
, se
->key_name
,
248 if (*setting
== NULL
)
249 *setting
= g_strdupv(se
->default_value
.strv_val
);
254 /* don't try to save a NULL string vector */
255 const gchar
*dummy
[] = { "", NULL
};
256 const gchar
**strv
= *setting
? (const gchar
**)*setting
: dummy
;
258 g_key_file_set_string_list(config
, group
->name
, se
->key_name
,
259 strv
, g_strv_length((gchar
**)strv
));
266 static void keyfile_action(SettingAction action
, StashGroup
*group
, GKeyFile
*keyfile
)
271 foreach_ptr_array(entry
, i
, group
->entries
)
273 /* don't override settings with default values */
274 if (!group
->use_defaults
&& action
== SETTING_READ
&&
275 !g_key_file_has_key(keyfile
, group
->name
, entry
->key_name
, NULL
))
278 switch (entry
->setting_type
)
281 handle_boolean_setting(group
, entry
, keyfile
, action
); break;
283 handle_integer_setting(group
, entry
, keyfile
, action
); break;
285 handle_double_setting(group
, entry
, keyfile
, action
); break;
287 handle_string_setting(group
, entry
, keyfile
, action
); break;
289 /* Note: G_TYPE_STRV is not a constant, can't use case label */
290 if (entry
->setting_type
== G_TYPE_STRV
)
291 handle_strv_setting(group
, entry
, keyfile
, action
);
293 g_warning("Unhandled type for %s::%s in %s()!", group
->name
, entry
->key_name
,
300 /** Reads key values from @a keyfile into the group settings.
301 * @note You should still call this even if the keyfile couldn't be loaded from disk
302 * so that all Stash settings are initialized to defaults.
304 * @param keyfile Usually loaded from a configuration file first. */
306 void stash_group_load_from_key_file(StashGroup
*group
, GKeyFile
*keyfile
)
308 keyfile_action(SETTING_READ
, group
, keyfile
);
312 /** Writes group settings into key values in @a keyfile.
313 * @a keyfile is usually written to a configuration file afterwards.
315 * @param keyfile . */
317 void stash_group_save_to_key_file(StashGroup
*group
, GKeyFile
*keyfile
)
319 keyfile_action(SETTING_WRITE
, group
, keyfile
);
323 /** Reads group settings from a configuration file using @c GKeyFile.
324 * @note Stash settings will be initialized to defaults if the keyfile
325 * couldn't be loaded from disk.
327 * @param filename Filename of the file to read, in locale encoding.
328 * @return @c TRUE if a key file could be loaded.
329 * @see stash_group_load_from_key_file().
332 gboolean
stash_group_load_from_file(StashGroup
*group
, const gchar
*filename
)
337 keyfile
= g_key_file_new();
338 ret
= g_key_file_load_from_file(keyfile
, filename
, 0, NULL
);
339 /* even on failure we load settings to apply defaults */
340 stash_group_load_from_key_file(group
, keyfile
);
342 g_key_file_free(keyfile
);
347 /** Writes group settings to a configuration file using @c GKeyFile.
350 * @param filename Filename of the file to write, in locale encoding.
351 * @param flags Keyfile options - @c G_KEY_FILE_NONE is the most efficient.
352 * @return 0 if the file was successfully written, otherwise the @c errno of the
353 * failed operation is returned.
354 * @see stash_group_save_to_key_file().
357 gint
stash_group_save_to_file(StashGroup
*group
, const gchar
*filename
,
364 keyfile
= g_key_file_new();
365 /* if we need to keep comments or translations, try to load first */
367 g_key_file_load_from_file(keyfile
, filename
, flags
, NULL
);
369 stash_group_save_to_key_file(group
, keyfile
);
370 data
= g_key_file_to_data(keyfile
, NULL
, NULL
);
371 ret
= utils_write_file(filename
, data
);
373 g_key_file_free(keyfile
);
378 static void free_stash_pref(StashPref
*pref
)
380 if (pref
->widget_type
== GTK_TYPE_RADIO_BUTTON
)
381 g_free(pref
->extra
.radio_buttons
);
383 g_slice_free(StashPref
, pref
);
387 /** Creates a new group.
388 * @param name Name used for @c GKeyFile group.
391 StashGroup
*stash_group_new(const gchar
*name
)
393 StashGroup
*group
= g_slice_new0(StashGroup
);
396 group
->entries
= g_ptr_array_new_with_free_func((GDestroyNotify
) free_stash_pref
);
397 group
->use_defaults
= TRUE
;
403 /** Frees the memory allocated for setting values in a group.
404 * Useful e.g. to avoid freeing strings individually.
405 * @note This is *not* called by stash_group_free().
408 void stash_group_free_settings(StashGroup
*group
)
413 foreach_ptr_array(entry
, i
, group
->entries
)
415 if (entry
->setting_type
== G_TYPE_STRING
)
416 g_free(*(gchararray
*) entry
->setting
);
417 else if (entry
->setting_type
== G_TYPE_STRV
)
418 g_strfreev(*(gchararray
**) entry
->setting
);
422 *(gpointer
**) entry
->setting
= NULL
;
427 static StashGroup
*stash_group_dup(StashGroup
*src
)
429 g_atomic_int_inc(&src
->refcount
);
438 void stash_group_free(StashGroup
*group
)
440 if (g_atomic_int_dec_and_test(&group
->refcount
))
442 g_ptr_array_free(group
->entries
, TRUE
);
443 g_slice_free(StashGroup
, group
);
448 /** Gets the GBoxed-derived GType for StashGroup
450 * @return StashGroup type . */
452 GType
stash_group_get_type(void);
454 G_DEFINE_BOXED_TYPE(StashGroup
, stash_group
, stash_group_dup
, stash_group_free
);
457 /* Used for selecting groups passed to stash_tree_setup().
458 * @param various @c FALSE by default.
459 * @param prefix @nullable Group prefix or @c NULL to use @c group->name. */
460 void stash_group_set_various(StashGroup
*group
, gboolean various
,
463 group
->various
= various
;
464 group
->prefix
= prefix
;
468 /* When @c FALSE, Stash doesn't change the setting if there is no keyfile entry, so it
469 * remains whatever it was initialized/set to by user code.
470 * @c TRUE by default. */
471 void stash_group_set_use_defaults(StashGroup
*group
, gboolean use_defaults
)
473 group
->use_defaults
= use_defaults
;
478 add_pref(StashGroup
*group
, GType type
, gpointer setting
,
479 const gchar
*key_name
, union Value default_value
)
481 StashPref
*entry
= g_slice_new(StashPref
);
483 *entry
= (StashPref
) {type
, setting
, key_name
, default_value
, G_TYPE_NONE
, NULL
, {NULL
}};
485 /* init any pointer settings to NULL so they can be freed later */
486 if (type
== G_TYPE_STRING
|| type
== G_TYPE_STRV
) {
487 if (group
->use_defaults
)
488 *(gpointer
**)setting
= NULL
;
491 g_ptr_array_add(group
->entries
, entry
);
496 /** Adds boolean setting.
498 * @param setting Address of setting variable.
499 * @param key_name Name for key in a @c GKeyFile.
500 * @param default_value Value to use if the key doesn't exist when loading. */
502 void stash_group_add_boolean(StashGroup
*group
, gboolean
*setting
,
503 const gchar
*key_name
, gboolean default_value
)
505 add_pref(group
, G_TYPE_BOOLEAN
, setting
, key_name
, (union Value
) {.bool_val
= default_value
});
509 /** Adds double setting.
511 * @param setting Address of setting variable.
512 * @param key_name Name for key in a @c GKeyFile.
513 * @param default_value Value to use if the key doesn't exist when loading. */
515 void stash_group_add_double(StashGroup
*group
, gdouble
*setting
,
516 const gchar
*key_name
, gdouble default_value
)
518 add_pref(group
, G_TYPE_DOUBLE
, setting
, key_name
, (union Value
) {.double_val
= default_value
});
522 /** Adds integer setting.
524 * @param setting Address of setting variable.
525 * @param key_name Name for key in a @c GKeyFile.
526 * @param default_value Value to use if the key doesn't exist when loading. */
528 void stash_group_add_integer(StashGroup
*group
, gint
*setting
,
529 const gchar
*key_name
, gint default_value
)
531 add_pref(group
, G_TYPE_INT
, setting
, key_name
, (union Value
) {.int_val
= default_value
});
535 /** Adds string setting.
536 * The contents of @a setting will be initialized to @c NULL.
538 * @param setting Address of setting variable.
539 * @param key_name Name for key in a @c GKeyFile.
540 * @param default_value @nullable String to copy if the key doesn't exist when loading, or @c NULL. */
542 void stash_group_add_string(StashGroup
*group
, gchar
**setting
,
543 const gchar
*key_name
, const gchar
*default_value
)
545 add_pref(group
, G_TYPE_STRING
, setting
, key_name
, (union Value
) {.str_val
= (gchar
*) default_value
});
549 /** Adds string vector setting (array of strings).
550 * The contents of @a setting will be initialized to @c NULL.
552 * @param setting Address of setting variable.
553 * @param key_name Name for key in a @c GKeyFile.
554 * @param default_value Vector to copy if the key doesn't exist when loading. Usually @c NULL. */
556 void stash_group_add_string_vector(StashGroup
*group
, gchar
***setting
,
557 const gchar
*key_name
, const gchar
**default_value
)
559 add_pref(group
, G_TYPE_STRV
, setting
, key_name
, (union Value
) {.strv_val
= (gchar
**) default_value
});
563 /* *** GTK-related functions *** */
565 static void handle_toggle_button(GtkWidget
*widget
, gboolean
*setting
,
571 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget
), *setting
);
574 *setting
= gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget
));
580 static void handle_spin_button(GtkWidget
*widget
, StashPref
*entry
,
583 gint
*setting
= entry
->setting
;
585 g_assert(entry
->setting_type
== G_TYPE_INT
); /* only int spin prefs */
590 gtk_spin_button_set_value(GTK_SPIN_BUTTON(widget
), *setting
);
593 /* if the widget is focussed, the value might not be updated */
594 gtk_spin_button_update(GTK_SPIN_BUTTON(widget
));
595 *setting
= gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(widget
));
601 static void handle_combo_box(GtkWidget
*widget
, StashPref
*entry
,
604 gint
*setting
= entry
->setting
;
609 gtk_combo_box_set_active(GTK_COMBO_BOX(widget
), *setting
);
612 *setting
= gtk_combo_box_get_active(GTK_COMBO_BOX(widget
));
618 static void handle_entry(GtkWidget
*widget
, StashPref
*entry
,
621 gchararray
*setting
= entry
->setting
;
626 gtk_entry_set_text(GTK_ENTRY(widget
), *setting
);
630 *setting
= g_strdup(gtk_entry_get_text(GTK_ENTRY(widget
)));
636 static void handle_combo_box_entry(GtkWidget
*widget
, StashPref
*entry
,
639 widget
= gtk_bin_get_child(GTK_BIN(widget
));
640 handle_entry(widget
, entry
, action
);
644 /* taken from Glade 2.x generated support.c */
646 lookup_widget(GtkWidget
*widget
, const gchar
*widget_name
)
648 GtkWidget
*parent
, *found_widget
;
650 g_return_val_if_fail(widget
!= NULL
, NULL
);
651 g_return_val_if_fail(widget_name
!= NULL
, NULL
);
655 if (GTK_IS_MENU(widget
))
656 parent
= gtk_menu_get_attach_widget(GTK_MENU(widget
));
658 parent
= gtk_widget_get_parent(widget
);
660 parent
= (GtkWidget
*) g_object_get_data(G_OBJECT(widget
), "GladeParentKey");
666 found_widget
= (GtkWidget
*) g_object_get_data(G_OBJECT(widget
), widget_name
);
667 if (G_UNLIKELY(found_widget
== NULL
))
668 g_warning("Widget not found: %s", widget_name
);
674 get_widget(GtkWidget
*owner
, StashWidgetID widget_id
)
679 widget
= lookup_widget(owner
, (const gchar
*)widget_id
);
681 widget
= (GtkWidget
*)widget_id
;
683 if (!GTK_IS_WIDGET(widget
))
685 g_warning("Unknown widget in %s()!", G_STRFUNC
);
692 static void handle_radio_button(GtkWidget
*widget
, gint enum_id
, gboolean
*setting
,
698 if (*setting
== enum_id
)
699 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget
), TRUE
);
702 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget
)))
709 static void handle_radio_buttons(GtkWidget
*owner
, StashPref
*entry
,
712 EnumWidget
*field
= entry
->extra
.radio_buttons
;
714 GtkWidget
*widget
= NULL
;
718 widget
= get_widget(owner
, field
->widget_id
);
724 handle_radio_button(widget
, field
->enum_id
, entry
->setting
, action
);
726 if (!field
->widget_id
)
729 if (g_slist_length(gtk_radio_button_get_group(GTK_RADIO_BUTTON(widget
))) != count
)
730 g_warning("Missing/invalid radio button widget IDs found!");
734 static void handle_widget_property(GtkWidget
*widget
, StashPref
*entry
,
737 GObject
*object
= G_OBJECT(widget
);
738 const gchar
*name
= entry
->extra
.property_name
;
743 if (entry
->setting_type
== G_TYPE_BOOLEAN
)
744 g_object_set(object
, name
, *(gboolean
*)entry
->setting
, NULL
);
745 else if (entry
->setting_type
== G_TYPE_INT
)
746 g_object_set(object
, name
, *(gint
*)entry
->setting
, NULL
);
747 else if (entry
->setting_type
== G_TYPE_STRING
)
748 g_object_set(object
, name
, *(gchararray
*)entry
->setting
, NULL
);
749 else if (entry
->setting_type
== G_TYPE_STRV
)
750 g_object_set(object
, name
, *(gchararray
**)entry
->setting
, NULL
);
753 g_warning("Unhandled type %s for %s in %s()!", g_type_name(entry
->setting_type
),
754 entry
->key_name
, G_STRFUNC
);
758 if (entry
->setting_type
== G_TYPE_STRING
)
759 g_free(*(gchararray
*)entry
->setting
);
760 else if (entry
->setting_type
== G_TYPE_STRV
)
761 g_strfreev(*(gchararray
**)entry
->setting
);
763 g_object_get(object
, name
, entry
->setting
, NULL
);
769 static void pref_action(PrefAction action
, StashGroup
*group
, GtkWidget
*owner
)
774 foreach_ptr_array(entry
, i
, group
->entries
)
778 /* ignore settings with no widgets */
779 if (entry
->widget_type
== G_TYPE_NONE
)
782 /* radio buttons have several widgets */
783 if (entry
->widget_type
== GTK_TYPE_RADIO_BUTTON
)
785 handle_radio_buttons(owner
, entry
, action
);
789 widget
= get_widget(owner
, entry
->widget_id
);
792 g_warning("Unknown widget for %s::%s in %s()!", group
->name
, entry
->key_name
,
797 /* note: can't use switch for GTK_TYPE macros */
798 if (entry
->widget_type
== GTK_TYPE_TOGGLE_BUTTON
)
799 handle_toggle_button(widget
, entry
->setting
, action
);
800 else if (entry
->widget_type
== GTK_TYPE_SPIN_BUTTON
)
801 handle_spin_button(widget
, entry
, action
);
802 else if (entry
->widget_type
== GTK_TYPE_COMBO_BOX
)
803 handle_combo_box(widget
, entry
, action
);
804 else if (entry
->widget_type
== TYPE_COMBO_BOX_ENTRY
)
805 handle_combo_box_entry(widget
, entry
, action
);
806 else if (entry
->widget_type
== GTK_TYPE_ENTRY
)
807 handle_entry(widget
, entry
, action
);
808 else if (entry
->widget_type
== G_TYPE_PARAM
)
809 handle_widget_property(widget
, entry
, action
);
811 g_warning("Unhandled type for %s::%s in %s()!", group
->name
, entry
->key_name
,
817 /** Applies Stash settings to widgets, usually called before displaying the widgets.
818 * The @a owner argument depends on which type you use for @ref StashWidgetID.
820 * @param owner If non-NULL, used to lookup widgets by name, otherwise
821 * widget pointers are assumed.
822 * @see stash_group_update(). */
824 void stash_group_display(StashGroup
*group
, GtkWidget
*owner
)
826 pref_action(PREF_DISPLAY
, group
, owner
);
830 /** Applies widget values to Stash settings, usually called after displaying the widgets.
831 * The @a owner argument depends on which type you use for @ref StashWidgetID.
833 * @param owner If non-NULL, used to lookup widgets by name, otherwise
834 * widget pointers are assumed.
835 * @see stash_group_display(). */
837 void stash_group_update(StashGroup
*group
, GtkWidget
*owner
)
839 pref_action(PREF_UPDATE
, group
, owner
);
844 add_widget_pref(StashGroup
*group
, GType setting_type
, gpointer setting
,
845 const gchar
*key_name
, union Value default_value
,
846 GType widget_type
, StashWidgetID widget_id
)
849 add_pref(group
, setting_type
, setting
, key_name
, default_value
);
851 entry
->widget_type
= widget_type
;
852 entry
->widget_id
= widget_id
;
857 /** Adds a @c GtkToggleButton (or @c GtkCheckButton) 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_radio_buttons(). */
865 void stash_group_add_toggle_button(StashGroup
*group
, gboolean
*setting
,
866 const gchar
*key_name
, gboolean default_value
, StashWidgetID widget_id
)
868 add_widget_pref(group
, G_TYPE_BOOLEAN
, setting
, key_name
, (union Value
) {.bool_val
= default_value
},
869 GTK_TYPE_TOGGLE_BUTTON
, widget_id
);
873 /** Adds a @c GtkRadioButton widget group pref.
875 * @param setting Address of setting variable.
876 * @param key_name Name for key in a @c GKeyFile.
877 * @param default_value Value to use if the key doesn't exist when loading.
878 * @param widget_id @c GtkWidget pointer or string to lookup widget later.
879 * @param enum_id Enum value for @a widget_id.
880 * @param ... pairs of @a widget_id, @a enum_id.
881 * Example (using widget lookup strings, but widget pointers can also work):
884 * stash_group_add_radio_buttons(group, &which_one_setting, "which_one", BAR,
885 * "radio_foo", FOO, "radio_bar", BAR, NULL);
888 void stash_group_add_radio_buttons(StashGroup
*group
, gint
*setting
,
889 const gchar
*key_name
, gint default_value
,
890 StashWidgetID widget_id
, gint enum_id
, ...)
893 add_widget_pref(group
, G_TYPE_INT
, setting
, key_name
, (union Value
) {.int_val
= default_value
},
894 GTK_TYPE_RADIO_BUTTON
, NULL
);
897 EnumWidget
*item
, *array
;
899 /* count pairs of args */
900 va_start(args
, enum_id
);
903 if (!va_arg(args
, gpointer
))
910 array
= g_new0(EnumWidget
, count
+ 1);
911 entry
->extra
.radio_buttons
= array
;
913 va_start(args
, enum_id
);
914 foreach_c_array(item
, array
, count
)
919 item
->widget_id
= widget_id
;
920 item
->enum_id
= enum_id
;
924 item
->widget_id
= va_arg(args
, gpointer
);
925 item
->enum_id
= va_arg(args
, gint
);
932 /** Adds a @c GtkSpinButton widget pref.
934 * @param setting Address of setting variable.
935 * @param key_name Name for key in a @c GKeyFile.
936 * @param default_value Value to use if the key doesn't exist when loading.
937 * @param widget_id @c GtkWidget pointer or string to lookup widget later. */
939 void stash_group_add_spin_button_integer(StashGroup
*group
, gint
*setting
,
940 const gchar
*key_name
, gint default_value
, StashWidgetID widget_id
)
942 add_widget_pref(group
, G_TYPE_INT
, setting
, key_name
,
943 (union Value
) {.int_val
= default_value
}, GTK_TYPE_SPIN_BUTTON
, widget_id
);
947 /** Adds a @c GtkComboBox widget pref.
949 * @param setting Address of setting variable.
950 * @param key_name Name for key in a @c GKeyFile.
951 * @param default_value Value to use if the key doesn't exist when loading.
952 * @param widget_id @c GtkWidget pointer or string to lookup widget later.
953 * @see stash_group_add_combo_box_entry(). */
955 void stash_group_add_combo_box(StashGroup
*group
, gint
*setting
,
956 const gchar
*key_name
, gint default_value
, StashWidgetID widget_id
)
958 add_widget_pref(group
, G_TYPE_INT
, setting
, key_name
,
959 (union Value
) {.int_val
= default_value
}, GTK_TYPE_COMBO_BOX
, widget_id
);
963 /** Adds a @c GtkComboBoxEntry widget pref.
965 * @param setting Address of setting variable.
966 * @param key_name Name for key in a @c GKeyFile.
967 * @param default_value Value to use if the key doesn't exist when loading.
968 * @param widget_id @c GtkWidget pointer or string to lookup widget later. */
969 /* We could maybe also have something like stash_group_add_combo_box_entry_with_menu()
970 * for the history list - or should that be stored as a separate setting? */
972 void stash_group_add_combo_box_entry(StashGroup
*group
, gchar
**setting
,
973 const gchar
*key_name
, const gchar
*default_value
, StashWidgetID widget_id
)
975 add_widget_pref(group
, G_TYPE_STRING
, setting
, key_name
,
976 (union Value
) {.str_val
= (gchar
*) default_value
}, TYPE_COMBO_BOX_ENTRY
, widget_id
);
980 /** Adds a @c GtkEntry widget pref.
982 * @param setting Address of setting variable.
983 * @param key_name Name for key in a @c GKeyFile.
984 * @param default_value Value to use if the key doesn't exist when loading.
985 * @param widget_id @c GtkWidget pointer or string to lookup widget later. */
987 void stash_group_add_entry(StashGroup
*group
, gchar
**setting
,
988 const gchar
*key_name
, const gchar
*default_value
, StashWidgetID widget_id
)
990 add_widget_pref(group
, G_TYPE_STRING
, setting
, key_name
,
991 (union Value
) {.str_val
= (gchar
*) default_value
}, GTK_TYPE_ENTRY
, widget_id
);
995 static GType
object_get_property_type(GObject
*object
, const gchar
*property_name
)
997 GObjectClass
*klass
= G_OBJECT_GET_CLASS(object
);
1000 ps
= g_object_class_find_property(klass
, property_name
);
1001 return ps
->value_type
;
1005 /** Adds a widget's read/write property to the stash group.
1006 * The property will be set when calling
1007 * stash_group_display(), and read when calling stash_group_update().
1009 * @param setting Address of e.g. an integer if using an integer property.
1010 * @param key_name Name for key in a @c GKeyFile.
1011 * @param default_value Value to use if the key doesn't exist when loading.
1012 * Should be cast into a pointer e.g. with @c GINT_TO_POINTER().
1013 * @param widget_id @c GtkWidget pointer or string to lookup widget later.
1014 * @param property_name .
1015 * @param type can be @c 0 if passing a @c GtkWidget as the @a widget_id argument to look it up from the
1017 * @warning Currently only string GValue properties will be freed before setting; patch for
1018 * other types - see @c handle_widget_property(). */
1020 void stash_group_add_widget_property(StashGroup
*group
, gpointer setting
,
1021 const gchar
*key_name
, gpointer default_value
, StashWidgetID widget_id
,
1022 const gchar
*property_name
, GType type
)
1027 type
= object_get_property_type(G_OBJECT(widget_id
), property_name
);
1029 entry
= add_widget_pref(group
, type
, setting
, key_name
,
1030 (union Value
) {.ptr_val
= default_value
}, G_TYPE_PARAM
, widget_id
);
1031 entry
->extra
.property_name
= property_name
;
1043 struct StashTreeValue
1045 const gchar
*group_name
;
1049 gchararray tree_string
;
1054 typedef struct StashTreeValue StashTreeValue
;
1057 static void stash_tree_renderer_set_data(GtkCellLayout
*cell_layout
, GtkCellRenderer
*cell
,
1058 GtkTreeModel
*model
, GtkTreeIter
*iter
, gpointer user_data
)
1060 GType cell_type
= GPOINTER_TO_SIZE(user_data
);
1061 StashTreeValue
*value
;
1063 gboolean matches_type
;
1065 gtk_tree_model_get(model
, iter
, STASH_TREE_VALUE
, &value
, -1);
1067 matches_type
= pref
->setting_type
== cell_type
;
1068 g_object_set(cell
, "visible", matches_type
, "sensitive", matches_type
,
1069 cell_type
== G_TYPE_BOOLEAN
? "activatable" : "editable", matches_type
, NULL
);
1073 switch (pref
->setting_type
)
1075 case G_TYPE_BOOLEAN
:
1076 g_object_set(cell
, "active", value
->data
.tree_int
, NULL
);
1080 gchar
*text
= g_strdup_printf("%d", value
->data
.tree_int
);
1081 g_object_set(cell
, "text", text
, NULL
);
1086 g_object_set(cell
, "text", value
->data
.tree_string
, NULL
);
1093 static void stash_tree_renderer_edited(gchar
*path_str
, gchar
*new_text
, GtkTreeModel
*model
)
1097 StashTreeValue
*value
;
1100 path
= gtk_tree_path_new_from_string(path_str
);
1101 gtk_tree_model_get_iter(model
, &iter
, path
);
1102 gtk_tree_model_get(model
, &iter
, STASH_TREE_VALUE
, &value
, -1);
1105 switch (pref
->setting_type
)
1107 case G_TYPE_BOOLEAN
:
1108 value
->data
.tree_int
= !value
->data
.tree_int
;
1111 value
->data
.tree_int
= atoi(new_text
);
1114 SETPTR(value
->data
.tree_string
, g_strdup(new_text
));
1118 gtk_tree_model_row_changed(model
, path
, &iter
);
1119 gtk_tree_path_free(path
);
1123 static void stash_tree_boolean_toggled(GtkCellRendererToggle
*cell
, gchar
*path_str
,
1124 GtkTreeModel
*model
)
1126 stash_tree_renderer_edited(path_str
, NULL
, model
);
1130 static void stash_tree_string_edited(GtkCellRenderer
*cell
, gchar
*path_str
, gchar
*new_text
,
1131 GtkTreeModel
*model
)
1133 stash_tree_renderer_edited(path_str
, new_text
, model
);
1137 static gboolean
stash_tree_discard_value(GtkTreeModel
*model
, GtkTreePath
*path
,
1138 GtkTreeIter
*iter
, gpointer user_data
)
1140 StashTreeValue
*value
;
1142 gtk_tree_model_get(model
, iter
, STASH_TREE_VALUE
, &value
, -1);
1143 /* don't access value->pref as it might already have been freed */
1144 g_free(value
->data
.tree_string
);
1151 static void stash_tree_destroy_cb(GtkWidget
*widget
, gpointer user_data
)
1153 GtkTreeModel
*model
= gtk_tree_view_get_model(GTK_TREE_VIEW(widget
));
1154 gtk_tree_model_foreach(model
, stash_tree_discard_value
, NULL
);
1158 static void stash_tree_append_pref(StashGroup
*group
, StashPref
*entry
, GtkListStore
*store
,
1162 StashTreeValue
*value
;
1165 value
= g_new0(StashTreeValue
, 1);
1167 value
->group_name
= group
->name
;
1168 value
->pref
= entry
;
1170 gtk_list_store_append(store
, &iter
);
1171 text
= g_strconcat(group
->prefix
? group
->prefix
: group
->name
,
1172 ".", entry
->key_name
, NULL
);
1173 gtk_list_store_set(store
, &iter
, STASH_TREE_NAME
, text
,
1174 STASH_TREE_VALUE
, value
, -1);
1179 static void stash_tree_append_prefs(GPtrArray
*group_array
,
1180 GtkListStore
*store
, PrefAction action
)
1186 foreach_ptr_array(group
, i
, group_array
)
1190 foreach_ptr_array(entry
, j
, group
->entries
)
1191 stash_tree_append_pref(group
, entry
, store
, action
);
1197 /* Setups a simple editor for stash preferences based on the widget arguments.
1198 * group_array - Array of groups which's settings will be edited.
1199 * tree - GtkTreeView in which to edit the preferences. Must be empty. */
1200 void stash_tree_setup(GPtrArray
*group_array
, GtkTreeView
*tree
)
1202 GtkListStore
*store
;
1203 GtkTreeModel
*model
;
1204 GtkCellRenderer
*cell
;
1205 GtkTreeViewColumn
*column
;
1206 GtkAdjustment
*adjustment
;
1208 store
= gtk_list_store_new(STASH_TREE_COUNT
, G_TYPE_STRING
, G_TYPE_POINTER
);
1209 stash_tree_append_prefs(group_array
, store
, PREF_DISPLAY
);
1210 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store
), STASH_TREE_NAME
,
1211 GTK_SORT_ASCENDING
);
1213 model
= GTK_TREE_MODEL(store
);
1214 gtk_tree_view_set_model(tree
, model
);
1215 g_object_unref(G_OBJECT(store
));
1216 g_signal_connect(tree
, "destroy", G_CALLBACK(stash_tree_destroy_cb
), NULL
);
1218 cell
= gtk_cell_renderer_text_new();
1219 column
= gtk_tree_view_column_new_with_attributes(_("Name"), cell
, "text",
1220 STASH_TREE_NAME
, NULL
);
1221 gtk_tree_view_column_set_sort_column_id(column
, STASH_TREE_NAME
);
1222 gtk_tree_view_column_set_sort_indicator(column
, TRUE
);
1223 gtk_tree_view_append_column(tree
, column
);
1225 column
= gtk_tree_view_column_new();
1226 gtk_tree_view_column_set_title(column
, _("Value"));
1227 gtk_tree_view_append_column(tree
, column
);
1228 /* boolean renderer */
1229 cell
= gtk_cell_renderer_toggle_new();
1230 g_signal_connect(cell
, "toggled", G_CALLBACK(stash_tree_boolean_toggled
), model
);
1231 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(column
), cell
, FALSE
);
1232 gtk_cell_layout_set_cell_data_func(GTK_CELL_LAYOUT(column
), cell
,
1233 stash_tree_renderer_set_data
, GSIZE_TO_POINTER(G_TYPE_BOOLEAN
), NULL
);
1234 /* string renderer */
1235 cell
= gtk_cell_renderer_text_new();
1236 g_object_set(cell
, "ellipsize", PANGO_ELLIPSIZE_END
, NULL
);
1237 g_signal_connect(cell
, "edited", G_CALLBACK(stash_tree_string_edited
), model
);
1238 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(column
), cell
, TRUE
);
1239 gtk_cell_layout_set_cell_data_func(GTK_CELL_LAYOUT(column
), cell
,
1240 stash_tree_renderer_set_data
, GSIZE_TO_POINTER(G_TYPE_STRING
), NULL
);
1241 /* integer renderer */
1242 cell
= gtk_cell_renderer_spin_new();
1243 adjustment
= GTK_ADJUSTMENT(gtk_adjustment_new(0, G_MININT
, G_MAXINT
, 1, 10, 0));
1244 g_object_set(cell
, "adjustment", adjustment
, NULL
);
1245 g_signal_connect(cell
, "edited", G_CALLBACK(stash_tree_string_edited
), model
);
1246 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(column
), cell
, FALSE
);
1247 gtk_cell_layout_set_cell_data_func(GTK_CELL_LAYOUT(column
), cell
,
1248 stash_tree_renderer_set_data
, GSIZE_TO_POINTER(G_TYPE_INT
), NULL
);
1252 static void stash_tree_display_pref(StashTreeValue
*value
, StashPref
*entry
)
1254 switch (entry
->setting_type
)
1256 case G_TYPE_BOOLEAN
:
1257 value
->data
.tree_int
= *(gboolean
*) entry
->setting
;
1260 value
->data
.tree_int
= *(gint
*) entry
->setting
;
1263 SETPTR(value
->data
.tree_string
, g_strdup(*(gchararray
*) entry
->setting
));
1266 g_warning("Unhandled type for %s::%s in %s()!", value
->group_name
,
1267 entry
->key_name
, G_STRFUNC
);
1272 static void stash_tree_update_pref(StashTreeValue
*value
, StashPref
*entry
)
1274 switch (entry
->setting_type
)
1276 case G_TYPE_BOOLEAN
:
1277 *(gboolean
*) entry
->setting
= value
->data
.tree_int
;
1280 *(gint
*) entry
->setting
= value
->data
.tree_int
;
1284 gchararray
*text
= entry
->setting
;
1285 SETPTR(*text
, g_strdup(value
->data
.tree_string
));
1289 g_warning("Unhandled type for %s::%s in %s()!", value
->group_name
,
1290 entry
->key_name
, G_STRFUNC
);
1295 static void stash_tree_action(GtkTreeModel
*model
, PrefAction action
)
1298 gboolean valid
= gtk_tree_model_get_iter_first(model
, &iter
);
1299 StashTreeValue
*value
;
1303 gtk_tree_model_get(model
, &iter
, STASH_TREE_VALUE
, &value
, -1);
1308 stash_tree_display_pref(value
, value
->pref
);
1311 stash_tree_update_pref(value
, value
->pref
);
1314 valid
= gtk_tree_model_iter_next(model
, &iter
);
1319 void stash_tree_display(GtkTreeView
*tree
)
1321 stash_tree_action(gtk_tree_view_get_model(tree
), PREF_DISPLAY
);
1325 void stash_tree_update(GtkTreeView
*tree
)
1327 stash_tree_action(gtk_tree_view_get_model(tree
), PREF_UPDATE
);