2 * stash.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2008-2010 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
5 * Copyright 2008-2010 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
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
27 * Lightweight library for reading/writing @c GKeyFile settings and synchronizing widgets with
30 * Note: Stash should only depend on GLib and GTK, but currently has some minor
31 * dependencies on Geany's utils.c.
34 * 'Setting' is used only for data stored on disk or in memory.
35 * 'Pref' can also include visual widget information.
37 * @section Memory Usage
38 * Stash will not duplicate strings if they are normally static arrays, such as
39 * keyfile group names and key names, string default values, widget_id names, property names.
41 * @section String Settings
42 * String settings and other dynamically allocated settings will be initialized to NULL when
43 * added to a StashGroup (so they can safely be reassigned later).
45 * @section Widget Support
46 * Widgets very commonly used in configuration dialogs will be supported with their own function.
47 * Widgets less commonly used such as @c GtkExpander or widget settings that aren't commonly needed
48 * to be persistent won't be directly supported, to keep the library lightweight. However, you can
49 * use stash_group_add_widget_property() to also save these settings for any read/write widget
50 * property. Macros could be added for common widget properties such as @c GtkExpander:"expanded".
52 * @section settings-example Settings Example
53 * @include stash-example.c
54 * @note You might want to handle the warning/error conditions differently from above.
56 * @section prefs-example GUI Prefs Example
57 * For prefs, it's the same as the above example but you add widget prefs instead of e.g.
60 * This example uses lookup strings for widgets as they are more flexible than widget pointers.
64 GtkWidget *my_check_button;
66 stash_group_add_toggle_button(group, &want_handle, "handle", TRUE, "check_handle");
68 gtk_container_add(GTK_CONTAINER(parent), my_check_button);
69 ui_hookup_widget(parent, my_check_button, "check_handle");
71 stash_group_display(group, parent);
73 * Now let the user manipulate widgets. To synchronize the Stash settings afterwards, call:
75 stash_group_update(group, parent);
79 /* Implementation Note
80 * We use a GArray to hold prefs. It would be more efficient for user code to declare
81 * a static array of StashPref structs, but we don't do this because:
83 * * It would be more ugly (lots of casts and NULLs).
84 * * Less type checking.
85 * * The API would have to break when adding/changing fields.
87 * Usually the prefs code isn't what user code will spend most of its time doing, so this
88 * should be efficient enough. But, if desired we could add a stash_group_set_size() function
89 * to reduce reallocation.
91 * TODO: Maybe using GSlice chunks with an extra 'next' pointer would be more (memory) efficient.
98 #include "utils.h" /* only for foreach_*, utils_get_setting_*(). Stash should not depend on Geany. */
103 GType setting_type
; /* e.g. G_TYPE_INT */
105 const gchar
*key_name
;
106 gpointer default_value
;
107 GType widget_type
; /* e.g. GTK_TYPE_TOGGLE_BUTTON */
108 StashWidgetID widget_id
; /* (GtkWidget*) or (gchar*) */
109 gpointer fields
; /* extra fields */
112 typedef struct StashPref StashPref
;
116 const gchar
*name
; /* group name to use in the keyfile */
117 GArray
*entries
; /* array of StashPref */
118 gboolean write_once
; /* only write settings if they don't already exist */
119 gboolean use_defaults
; /* use default values if there's no keyfile entry */
122 typedef struct EnumWidget
124 StashWidgetID widget_id
;
130 typedef enum SettingAction
137 typedef enum PrefAction
145 static void handle_boolean_setting(StashGroup
*group
, StashPref
*se
,
146 GKeyFile
*config
, SettingAction action
)
148 gboolean
*setting
= se
->setting
;
153 *setting
= utils_get_setting_boolean(config
, group
->name
, se
->key_name
,
154 GPOINTER_TO_INT(se
->default_value
));
157 g_key_file_set_boolean(config
, group
->name
, se
->key_name
, *setting
);
163 static void handle_integer_setting(StashGroup
*group
, StashPref
*se
,
164 GKeyFile
*config
, SettingAction action
)
166 gboolean
*setting
= se
->setting
;
171 *setting
= utils_get_setting_integer(config
, group
->name
, se
->key_name
,
172 GPOINTER_TO_INT(se
->default_value
));
175 g_key_file_set_integer(config
, group
->name
, se
->key_name
, *setting
);
181 static void handle_string_setting(StashGroup
*group
, StashPref
*se
,
182 GKeyFile
*config
, SettingAction action
)
184 gchararray
*setting
= se
->setting
;
190 *setting
= utils_get_setting_string(config
, group
->name
, se
->key_name
,
194 g_key_file_set_string(config
, group
->name
, se
->key_name
,
195 *setting
? *setting
: "");
201 static void handle_strv_setting(StashGroup
*group
, StashPref
*se
,
202 GKeyFile
*config
, SettingAction action
)
204 gchararray
**setting
= se
->setting
;
209 g_strfreev(*setting
);
210 *setting
= g_key_file_get_string_list(config
, group
->name
, se
->key_name
,
212 if (*setting
== NULL
)
213 *setting
= g_strdupv(se
->default_value
);
218 /* don't try to save a NULL string vector */
219 gchar
*dummy
[] = { "", NULL
};
220 gchar
**strv
= *setting
? *setting
: dummy
;
222 g_key_file_set_string_list(config
, group
->name
, se
->key_name
,
223 (const gchar
**)strv
, g_strv_length(strv
));
230 static void keyfile_action(SettingAction action
, StashGroup
*group
, GKeyFile
*keyfile
)
234 foreach_array(StashPref
, entry
, group
->entries
)
236 /* don't overwrite write_once prefs */
237 if (group
->write_once
&& action
== SETTING_WRITE
&&
238 g_key_file_has_key(keyfile
, group
->name
, entry
->key_name
, NULL
))
240 /* don't override settings with default values */
241 if (!group
->use_defaults
&& action
== SETTING_READ
&&
242 !g_key_file_has_key(keyfile
, group
->name
, entry
->key_name
, NULL
))
245 switch (entry
->setting_type
)
248 handle_boolean_setting(group
, entry
, keyfile
, action
); break;
250 handle_integer_setting(group
, entry
, keyfile
, action
); break;
252 handle_string_setting(group
, entry
, keyfile
, action
); break;
254 /* G_TYPE_STRV is not a constant */
255 if (entry
->setting_type
== G_TYPE_STRV
)
256 handle_strv_setting(group
, entry
, keyfile
, action
);
258 g_warning("Unhandled type for %s::%s in %s()!", group
->name
, entry
->key_name
,
265 /** Reads key values from @a keyfile into the group settings.
266 * @note You should still call this even if the keyfile couldn't be loaded from disk
267 * so that all Stash settings are initialized to defaults.
269 * @param keyfile Usually loaded from a configuration file first. */
270 void stash_group_load_from_key_file(StashGroup
*group
, GKeyFile
*keyfile
)
272 keyfile_action(SETTING_READ
, group
, keyfile
);
276 /** Writes group settings into key values in @a keyfile.
277 * @a keyfile is usually written to a configuration file afterwards.
279 * @param keyfile . */
280 void stash_group_save_to_key_file(StashGroup
*group
, GKeyFile
*keyfile
)
282 keyfile_action(SETTING_WRITE
, group
, keyfile
);
286 /** Reads group settings from a configuration file using @c GKeyFile.
287 * @note Stash settings will be initialized to defaults if the keyfile
288 * couldn't be loaded from disk.
290 * @param filename Filename of the file to read, in locale encoding.
291 * @return @c TRUE if a key file could be loaded.
292 * @see stash_group_load_from_key_file().
294 gboolean
stash_group_load_from_file(StashGroup
*group
, const gchar
*filename
)
299 keyfile
= g_key_file_new();
300 ret
= g_key_file_load_from_file(keyfile
, filename
, 0, NULL
);
301 /* even on failure we load settings to apply defaults */
302 stash_group_load_from_key_file(group
, keyfile
);
304 g_key_file_free(keyfile
);
309 /** Writes group settings to a configuration file using @c GKeyFile.
312 * @param filename Filename of the file to write, in locale encoding.
313 * @param flags Keyfile options - @c G_KEY_FILE_NONE is the most efficient.
314 * @return 0 if the file was successfully written, otherwise the @c errno of the
315 * failed operation is returned.
316 * @see stash_group_save_to_key_file().
318 gint
stash_group_save_to_file(StashGroup
*group
, const gchar
*filename
,
325 keyfile
= g_key_file_new();
326 /* if we need to keep comments or translations, try to load first */
328 g_key_file_load_from_file(keyfile
, filename
, flags
, NULL
);
330 stash_group_save_to_key_file(group
, keyfile
);
331 data
= g_key_file_to_data(keyfile
, NULL
, NULL
);
332 ret
= utils_write_file(filename
, data
);
334 g_key_file_free(keyfile
);
339 /** Creates a new group.
340 * @param name Name used for @c GKeyFile group.
342 StashGroup
*stash_group_new(const gchar
*name
)
344 StashGroup
*group
= g_new0(StashGroup
, 1);
347 group
->entries
= g_array_new(FALSE
, FALSE
, sizeof(StashPref
));
348 group
->use_defaults
= TRUE
;
355 void stash_group_free(StashGroup
*group
)
359 foreach_array(StashPref
, entry
, group
->entries
)
361 if (entry
->widget_type
== GTK_TYPE_RADIO_BUTTON
)
362 g_free(entry
->fields
);
363 else if (entry
->widget_type
== G_TYPE_PARAM
)
366 g_assert(entry
->fields
== NULL
); /* to prevent memory leaks, must handle fields above */
368 g_array_free(group
->entries
, TRUE
);
373 /* Useful so the user can edit the keyfile manually while the program is running,
374 * and the setting won't be overridden.
375 * @c FALSE by default. */
376 void stash_group_set_write_once(StashGroup
*group
, gboolean write_once
)
378 group
->write_once
= write_once
;
382 /* When @c FALSE, Stash doesn't change the setting if there is no keyfile entry, so it
383 * remains whatever it was initialized/set to by user code.
384 * @c TRUE by default. */
385 void stash_group_set_use_defaults(StashGroup
*group
, gboolean use_defaults
)
387 group
->use_defaults
= use_defaults
;
392 add_pref(StashGroup
*group
, GType type
, gpointer setting
,
393 const gchar
*key_name
, gpointer default_value
)
395 StashPref entry
= {type
, setting
, key_name
, default_value
, G_TYPE_NONE
, NULL
, NULL
};
396 GArray
*array
= group
->entries
;
398 /* init any pointer settings to NULL so they can be freed later */
399 if (type
== G_TYPE_STRING
||
401 if (group
->use_defaults
)
402 *(gpointer
**)setting
= NULL
;
404 g_array_append_val(array
, entry
);
406 return &g_array_index(array
, StashPref
, array
->len
- 1);
410 /** Adds boolean setting.
412 * @param setting Address of setting variable.
413 * @param key_name Name for key in a @c GKeyFile.
414 * @param default_value Value to use if the key doesn't exist when loading. */
415 void stash_group_add_boolean(StashGroup
*group
, gboolean
*setting
,
416 const gchar
*key_name
, gboolean default_value
)
418 add_pref(group
, G_TYPE_BOOLEAN
, setting
, key_name
, GINT_TO_POINTER(default_value
));
422 /** Adds integer setting.
424 * @param setting Address of setting variable.
425 * @param key_name Name for key in a @c GKeyFile.
426 * @param default_value Value to use if the key doesn't exist when loading. */
427 void stash_group_add_integer(StashGroup
*group
, gint
*setting
,
428 const gchar
*key_name
, gint default_value
)
430 add_pref(group
, G_TYPE_INT
, setting
, key_name
, GINT_TO_POINTER(default_value
));
434 /** Adds string setting.
435 * The contents of @a setting will be initialized to @c NULL.
437 * @param setting Address of setting variable.
438 * @param key_name Name for key in a @c GKeyFile.
439 * @param default_value String to copy if the key doesn't exist when loading, or @c NULL. */
440 void stash_group_add_string(StashGroup
*group
, gchar
**setting
,
441 const gchar
*key_name
, const gchar
*default_value
)
443 add_pref(group
, G_TYPE_STRING
, setting
, key_name
, (gpointer
)default_value
);
447 /** Adds string vector setting (array of strings).
448 * The contents of @a setting will be initialized to @c NULL.
450 * @param setting Address of setting variable.
451 * @param key_name Name for key in a @c GKeyFile.
452 * @param default_value Vector to copy if the key doesn't exist when loading. Usually @c NULL. */
453 void stash_group_add_string_vector(StashGroup
*group
, gchar
***setting
,
454 const gchar
*key_name
, const gchar
**default_value
)
456 add_pref(group
, G_TYPE_STRV
, setting
, key_name
, (gpointer
)default_value
);
460 /* *** GTK-related functions *** */
462 static void handle_toggle_button(GtkWidget
*widget
, gboolean
*setting
,
468 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget
), *setting
);
471 *setting
= gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget
));
477 static void handle_spin_button(GtkWidget
*widget
, StashPref
*entry
,
480 gint
*setting
= entry
->setting
;
482 g_assert(entry
->setting_type
== G_TYPE_INT
); /* only int spin prefs */
487 gtk_spin_button_set_value(GTK_SPIN_BUTTON(widget
), *setting
);
490 /* if the widget is focussed, the value might not be updated */
491 gtk_spin_button_update(GTK_SPIN_BUTTON(widget
));
492 *setting
= gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(widget
));
498 static void handle_combo_box(GtkWidget
*widget
, StashPref
*entry
,
501 gint
*setting
= entry
->setting
;
506 gtk_combo_box_set_active(GTK_COMBO_BOX(widget
), *setting
);
509 *setting
= gtk_combo_box_get_active(GTK_COMBO_BOX(widget
));
515 static void handle_entry(GtkWidget
*widget
, StashPref
*entry
,
518 gchararray
*setting
= entry
->setting
;
523 gtk_entry_set_text(GTK_ENTRY(widget
), *setting
);
527 *setting
= g_strdup(gtk_entry_get_text(GTK_ENTRY(widget
)));
533 static void handle_combo_box_entry(GtkWidget
*widget
, StashPref
*entry
,
536 widget
= gtk_bin_get_child(GTK_BIN(widget
));
537 handle_entry(widget
, entry
, action
);
541 /* taken from Glade 2.x generated support.c */
543 lookup_widget(GtkWidget
*widget
, const gchar
*widget_name
)
545 GtkWidget
*parent
, *found_widget
;
549 if (GTK_IS_MENU (widget
))
550 parent
= gtk_menu_get_attach_widget (GTK_MENU (widget
));
552 parent
= widget
->parent
;
554 parent
= (GtkWidget
*) g_object_get_data (G_OBJECT (widget
), "GladeParentKey");
560 found_widget
= (GtkWidget
*) g_object_get_data (G_OBJECT (widget
), widget_name
);
562 g_warning ("Widget not found: %s", widget_name
);
568 get_widget(GtkWidget
*owner
, StashWidgetID widget_id
)
570 GtkWidget
*widget
= widget_id
;
574 const gchar
*widget_name
= widget_id
;
576 widget
= lookup_widget(owner
, widget_name
);
578 if (!GTK_IS_WIDGET(widget
))
580 g_warning("Unknown widget in %s()!", G_STRFUNC
);
587 static void handle_radio_button(GtkWidget
*widget
, gint enum_id
, gboolean
*setting
,
593 if (*setting
== enum_id
)
594 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget
), TRUE
);
597 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget
)))
604 static void handle_radio_buttons(GtkWidget
*owner
, EnumWidget
*fields
,
608 EnumWidget
*field
= fields
;
610 GtkWidget
*widget
= NULL
;
614 widget
= get_widget(owner
, field
->widget_id
);
620 handle_radio_button(widget
, field
->enum_id
, setting
, action
);
622 if (!field
->widget_id
)
625 if (g_slist_length(gtk_radio_button_get_group(GTK_RADIO_BUTTON(widget
))) != count
)
626 g_warning("Missing/invalid radio button widget IDs found!");
630 static void handle_widget_property(GtkWidget
*widget
, StashPref
*entry
,
633 GObject
*object
= G_OBJECT(widget
);
634 const gchar
*name
= entry
->fields
;
639 g_object_set(object
, name
, entry
->setting
, NULL
);
642 if (entry
->setting_type
== G_TYPE_STRING
)
643 g_free(entry
->setting
);
644 /* TODO: Which other types need freeing here? */
646 g_object_get(object
, name
, entry
->setting
, NULL
);
652 static void pref_action(PrefAction action
, StashGroup
*group
, GtkWidget
*owner
)
656 foreach_array(StashPref
, entry
, group
->entries
)
660 /* ignore settings with no widgets */
661 if (entry
->widget_type
== G_TYPE_NONE
)
664 /* radio buttons have several widgets */
665 if (entry
->widget_type
== GTK_TYPE_RADIO_BUTTON
)
667 handle_radio_buttons(owner
, entry
->fields
, entry
->setting
, action
);
671 widget
= get_widget(owner
, entry
->widget_id
);
674 g_warning("Unknown widget for %s::%s in %s()!", group
->name
, entry
->key_name
,
679 /* note: can't use switch for GTK_TYPE macros */
680 if (entry
->widget_type
== GTK_TYPE_TOGGLE_BUTTON
)
681 handle_toggle_button(widget
, entry
->setting
, action
);
682 else if (entry
->widget_type
== GTK_TYPE_SPIN_BUTTON
)
683 handle_spin_button(widget
, entry
, action
);
684 else if (entry
->widget_type
== GTK_TYPE_COMBO_BOX
)
685 handle_combo_box(widget
, entry
, action
);
686 else if (entry
->widget_type
== GTK_TYPE_COMBO_BOX_ENTRY
)
687 handle_combo_box_entry(widget
, entry
, action
);
688 else if (entry
->widget_type
== GTK_TYPE_ENTRY
)
689 handle_entry(widget
, entry
, action
);
690 else if (entry
->widget_type
== G_TYPE_PARAM
)
691 handle_widget_property(widget
, entry
, action
);
693 g_warning("Unhandled type for %s::%s in %s()!", group
->name
, entry
->key_name
,
699 /** Applies Stash settings to widgets, usually called before displaying the widgets.
700 * The @a owner argument depends on which type you use for @ref StashWidgetID.
702 * @param owner If non-NULL, used to lookup widgets by name, otherwise
703 * widget pointers are assumed.
704 * @see stash_group_update(). */
705 void stash_group_display(StashGroup
*group
, GtkWidget
*owner
)
707 pref_action(PREF_DISPLAY
, group
, owner
);
711 /** Applies widget values to Stash settings, usually called after displaying the widgets.
712 * The @a owner argument depends on which type you use for @ref StashWidgetID.
714 * @param owner If non-NULL, used to lookup widgets by name, otherwise
715 * widget pointers are assumed.
716 * @see stash_group_display(). */
717 void stash_group_update(StashGroup
*group
, GtkWidget
*owner
)
719 pref_action(PREF_UPDATE
, group
, owner
);
724 add_widget_pref(StashGroup
*group
, GType setting_type
, gpointer setting
,
725 const gchar
*key_name
, gpointer default_value
,
726 GType widget_type
, StashWidgetID widget_id
)
729 add_pref(group
, setting_type
, setting
, key_name
, default_value
);
731 entry
->widget_type
= widget_type
;
732 entry
->widget_id
= widget_id
;
737 /** Adds a @c GtkToggleButton (or @c GtkCheckButton) widget pref.
739 * @param setting Address of setting variable.
740 * @param key_name Name for key in a @c GKeyFile.
741 * @param default_value Value to use if the key doesn't exist when loading.
742 * @param widget_id @c GtkWidget pointer or string to lookup widget later.
743 * @see stash_group_add_radio_buttons(). */
744 void stash_group_add_toggle_button(StashGroup
*group
, gboolean
*setting
,
745 const gchar
*key_name
, gboolean default_value
, StashWidgetID widget_id
)
747 add_widget_pref(group
, G_TYPE_BOOLEAN
, setting
, key_name
, GINT_TO_POINTER(default_value
),
748 GTK_TYPE_TOGGLE_BUTTON
, widget_id
);
752 /** Adds a @c GtkRadioButton widget group pref.
754 * @param setting Address of setting variable.
755 * @param key_name Name for key in a @c GKeyFile.
756 * @param default_value Value to use if the key doesn't exist when loading.
757 * @param widget_id @c GtkWidget pointer or string to lookup widget later.
758 * @param enum_id Enum value for @a widget_id.
759 * @param ... pairs of @a widget_id, @a enum_id.
760 * Example (using widget lookup strings, but widget pointers can also work):
763 * stash_group_add_radio_buttons(group, &which_one_setting, "which_one", BAR,
764 * "radio_foo", FOO, "radio_bar", BAR, NULL);
766 void stash_group_add_radio_buttons(StashGroup
*group
, gint
*setting
,
767 const gchar
*key_name
, gint default_value
,
768 StashWidgetID widget_id
, gint enum_id
, ...)
771 add_widget_pref(group
, G_TYPE_INT
, setting
, key_name
, GINT_TO_POINTER(default_value
),
772 GTK_TYPE_RADIO_BUTTON
, NULL
);
775 EnumWidget
*item
, *array
;
777 /* count pairs of args */
778 va_start(args
, enum_id
);
783 if (!va_arg(args
, gpointer
))
785 dummy
= va_arg(args
, gint
);
790 array
= g_new0(EnumWidget
, count
+ 1);
791 entry
->fields
= array
;
793 va_start(args
, enum_id
);
794 foreach_c_array(item
, array
, count
)
799 item
->widget_id
= widget_id
;
800 item
->enum_id
= enum_id
;
804 item
->widget_id
= va_arg(args
, gpointer
);
805 item
->enum_id
= va_arg(args
, gint
);
812 /** Adds a @c GtkSpinButton widget pref.
814 * @param setting Address of setting variable.
815 * @param key_name Name for key in a @c GKeyFile.
816 * @param default_value Value to use if the key doesn't exist when loading.
817 * @param widget_id @c GtkWidget pointer or string to lookup widget later. */
818 void stash_group_add_spin_button_integer(StashGroup
*group
, gint
*setting
,
819 const gchar
*key_name
, gint default_value
, StashWidgetID widget_id
)
821 add_widget_pref(group
, G_TYPE_INT
, setting
, key_name
, GINT_TO_POINTER(default_value
),
822 GTK_TYPE_SPIN_BUTTON
, widget_id
);
826 /** Adds a @c GtkComboBox widget pref.
828 * @param setting Address of setting variable.
829 * @param key_name Name for key in a @c GKeyFile.
830 * @param default_value Value to use if the key doesn't exist when loading.
831 * @param widget_id @c GtkWidget pointer or string to lookup widget later.
832 * @see stash_group_add_combo_box_entry(). */
833 void stash_group_add_combo_box(StashGroup
*group
, gint
*setting
,
834 const gchar
*key_name
, gint default_value
, StashWidgetID widget_id
)
836 add_widget_pref(group
, G_TYPE_INT
, setting
, key_name
, GINT_TO_POINTER(default_value
),
837 GTK_TYPE_COMBO_BOX
, widget_id
);
841 /** Adds a @c GtkComboBoxEntry widget pref.
843 * @param setting Address of setting variable.
844 * @param key_name Name for key in a @c GKeyFile.
845 * @param default_value Value to use if the key doesn't exist when loading.
846 * @param widget_id @c GtkWidget pointer or string to lookup widget later. */
847 /* We could maybe also have something like stash_group_add_combo_box_entry_with_menu()
848 * for the history list - or should that be stored as a separate setting? */
849 void stash_group_add_combo_box_entry(StashGroup
*group
, gchar
**setting
,
850 const gchar
*key_name
, const gchar
*default_value
, StashWidgetID widget_id
)
852 add_widget_pref(group
, G_TYPE_STRING
, setting
, key_name
, (gpointer
)default_value
,
853 GTK_TYPE_COMBO_BOX_ENTRY
, widget_id
);
857 /** Adds a @c GtkEntry 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 void stash_group_add_entry(StashGroup
*group
, gchar
**setting
,
864 const gchar
*key_name
, const gchar
*default_value
, StashWidgetID widget_id
)
866 add_widget_pref(group
, G_TYPE_STRING
, setting
, key_name
, (gpointer
)default_value
,
867 GTK_TYPE_ENTRY
, widget_id
);
871 static GType
object_get_property_type(GObject
*object
, const gchar
*property_name
)
873 GObjectClass
*klass
= G_OBJECT_GET_CLASS(object
);
876 ps
= g_object_class_find_property(klass
, property_name
);
877 return ps
->value_type
;
881 /** Adds a widget's read/write property to the stash group.
882 * The property will be set when calling
883 * stash_group_display(), and read when calling stash_group_update().
885 * @param setting Address of e.g. an integer if using an integer property.
886 * @param key_name Name for key in a @c GKeyFile.
887 * @param default_value Value to use if the key doesn't exist when loading.
888 * Should be cast into a pointer e.g. with @c GINT_TO_POINTER().
889 * @param widget_id @c GtkWidget pointer or string to lookup widget later.
890 * @param property_name .
891 * @param type can be @c 0 if passing a @c GtkWidget as the @a widget_id argument to look it up from the
893 * @warning Currently only string GValue properties will be freed before setting; patch for
894 * other types - see @c handle_widget_property(). */
895 void stash_group_add_widget_property(StashGroup
*group
, gpointer setting
,
896 const gchar
*key_name
, gpointer default_value
, StashWidgetID widget_id
,
897 const gchar
*property_name
, GType type
)
900 type
= object_get_property_type(G_OBJECT(widget_id
), property_name
);
902 add_widget_pref(group
, type
, setting
, key_name
, default_value
,
903 G_TYPE_PARAM
, widget_id
)->fields
= (gchar
*)property_name
;