Update of po files for string freeze of Geany 1.24
[geany-mirror.git] / src / stash.c
blobcadfacfccc57eb4ca5a415065c9361533b7c47e1
1 /*
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.
22 /**
23 * @file stash.h
24 * Lightweight library for reading/writing @c GKeyFile settings and synchronizing widgets with
25 * C variables.
27 * Note: Stash should only depend on GLib and GTK, but currently has some minor
28 * dependencies on Geany's utils.c.
30 * @section Terms
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
57 * just data settings.
59 * This example uses lookup strings for widgets as they are more flexible than widget pointers.
60 * Code to load and save the settings is omitted - see the first example instead.
62 * Here we show a dialog with a toggle button for whether the cup should have a handle.
63 * @include stash-gui-example.c
64 * @note This example should also work for other widget containers besides dialogs, e.g. popup menus.
67 /* Implementation Note
68 * We dynamically allocate prefs. It would be more efficient for user code to declare
69 * a static array of StashPref structs, but we don't do this because:
71 * * It would be more ugly (lots of casts and NULLs).
72 * * Less type checking.
73 * * The API & ABI would have to break when adding/changing fields.
75 * Usually the prefs code isn't what user code will spend most of its time doing, so this
76 * should be efficient enough.
80 #include "geany.h" /* necessary for utils.h, otherwise use gtk/gtk.h */
81 #include <stdlib.h> /* only for atoi() */
82 #include "support.h" /* only for _("text") */
83 #include "utils.h" /* only for foreach_*, utils_get_setting_*(). Stash should not depend on Geany. */
85 #include "stash.h"
88 /* GTK3 removed ComboBoxEntry, but we need a value to differentiate combo box with and
89 * without entries, and it must not collide with other GTypes */
90 #ifdef GTK_TYPE_COMBO_BOX_ENTRY
91 # define TYPE_COMBO_BOX_ENTRY GTK_TYPE_COMBO_BOX_ENTRY
92 #else /* !GTK_TYPE_COMBO_BOX_ENTRY */
93 # define TYPE_COMBO_BOX_ENTRY get_combo_box_entry_type()
94 static GType get_combo_box_entry_type(void)
96 static volatile gsize type = 0;
97 if (g_once_init_enter(&type))
99 GType g_type = g_type_register_static_simple(GTK_TYPE_COMBO_BOX, "dummy-combo-box-entry",
100 sizeof(GtkComboBoxClass), NULL, sizeof(GtkComboBox), NULL, G_TYPE_FLAG_ABSTRACT);
101 g_once_init_leave(&type, g_type);
103 return type;
105 #endif /* !GTK_TYPE_COMBO_BOX_ENTRY */
108 struct StashPref
110 GType setting_type; /* e.g. G_TYPE_INT */
111 gpointer setting; /* Address of a variable */
112 const gchar *key_name;
113 gpointer default_value; /* Default value, e.g. (gpointer)1 */
114 GType widget_type; /* e.g. GTK_TYPE_TOGGLE_BUTTON */
115 StashWidgetID widget_id; /* (GtkWidget*) or (gchar*) */
116 union
118 struct EnumWidget *radio_buttons;
119 const gchar *property_name;
120 } extra; /* extra fields depending on widget_type */
123 typedef struct StashPref StashPref;
125 struct StashGroup
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;
136 gint enum_id;
138 EnumWidget;
141 typedef enum SettingAction
143 SETTING_READ,
144 SETTING_WRITE
146 SettingAction;
148 typedef enum PrefAction
150 PREF_DISPLAY,
151 PREF_UPDATE
153 PrefAction;
156 static void handle_boolean_setting(StashGroup *group, StashPref *se,
157 GKeyFile *config, SettingAction action)
159 gboolean *setting = se->setting;
161 switch (action)
163 case SETTING_READ:
164 *setting = utils_get_setting_boolean(config, group->name, se->key_name,
165 GPOINTER_TO_INT(se->default_value));
166 break;
167 case SETTING_WRITE:
168 g_key_file_set_boolean(config, group->name, se->key_name, *setting);
169 break;
174 static void handle_integer_setting(StashGroup *group, StashPref *se,
175 GKeyFile *config, SettingAction action)
177 gint *setting = se->setting;
179 switch (action)
181 case SETTING_READ:
182 *setting = utils_get_setting_integer(config, group->name, se->key_name,
183 GPOINTER_TO_INT(se->default_value));
184 break;
185 case SETTING_WRITE:
186 g_key_file_set_integer(config, group->name, se->key_name, *setting);
187 break;
192 static void handle_string_setting(StashGroup *group, StashPref *se,
193 GKeyFile *config, SettingAction action)
195 gchararray *setting = se->setting;
197 switch (action)
199 case SETTING_READ:
200 g_free(*setting);
201 *setting = utils_get_setting_string(config, group->name, se->key_name,
202 se->default_value);
203 break;
204 case SETTING_WRITE:
205 g_key_file_set_string(config, group->name, se->key_name,
206 *setting ? *setting : "");
207 break;
212 static void handle_strv_setting(StashGroup *group, StashPref *se,
213 GKeyFile *config, SettingAction action)
215 gchararray **setting = se->setting;
217 switch (action)
219 case SETTING_READ:
220 g_strfreev(*setting);
221 *setting = g_key_file_get_string_list(config, group->name, se->key_name,
222 NULL, NULL);
223 if (*setting == NULL)
224 *setting = g_strdupv(se->default_value);
225 break;
227 case SETTING_WRITE:
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));
235 break;
241 static void keyfile_action(SettingAction action, StashGroup *group, GKeyFile *keyfile)
243 StashPref *entry;
244 guint i;
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))
251 continue;
253 switch (entry->setting_type)
255 case G_TYPE_BOOLEAN:
256 handle_boolean_setting(group, entry, keyfile, action); break;
257 case G_TYPE_INT:
258 handle_integer_setting(group, entry, keyfile, action); break;
259 case G_TYPE_STRING:
260 handle_string_setting(group, entry, keyfile, action); break;
261 default:
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);
265 else
266 g_warning("Unhandled type for %s::%s in %s()!", group->name, entry->key_name,
267 G_STRFUNC);
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.
276 * @param group .
277 * @param keyfile Usually loaded from a configuration file first. */
278 void stash_group_load_from_key_file(StashGroup *group, GKeyFile *keyfile)
280 keyfile_action(SETTING_READ, group, keyfile);
284 /** Writes group settings into key values in @a keyfile.
285 * @a keyfile is usually written to a configuration file afterwards.
286 * @param group .
287 * @param keyfile . */
288 void stash_group_save_to_key_file(StashGroup *group, GKeyFile *keyfile)
290 keyfile_action(SETTING_WRITE, group, keyfile);
294 /** Reads group settings from a configuration file using @c GKeyFile.
295 * @note Stash settings will be initialized to defaults if the keyfile
296 * couldn't be loaded from disk.
297 * @param group .
298 * @param filename Filename of the file to read, in locale encoding.
299 * @return @c TRUE if a key file could be loaded.
300 * @see stash_group_load_from_key_file().
302 gboolean stash_group_load_from_file(StashGroup *group, const gchar *filename)
304 GKeyFile *keyfile;
305 gboolean ret;
307 keyfile = g_key_file_new();
308 ret = g_key_file_load_from_file(keyfile, filename, 0, NULL);
309 /* even on failure we load settings to apply defaults */
310 stash_group_load_from_key_file(group, keyfile);
312 g_key_file_free(keyfile);
313 return ret;
317 /** Writes group settings to a configuration file using @c GKeyFile.
319 * @param group .
320 * @param filename Filename of the file to write, in locale encoding.
321 * @param flags Keyfile options - @c G_KEY_FILE_NONE is the most efficient.
322 * @return 0 if the file was successfully written, otherwise the @c errno of the
323 * failed operation is returned.
324 * @see stash_group_save_to_key_file().
326 gint stash_group_save_to_file(StashGroup *group, const gchar *filename,
327 GKeyFileFlags flags)
329 GKeyFile *keyfile;
330 gchar *data;
331 gint ret;
333 keyfile = g_key_file_new();
334 /* if we need to keep comments or translations, try to load first */
335 if (flags)
336 g_key_file_load_from_file(keyfile, filename, flags, NULL);
338 stash_group_save_to_key_file(group, keyfile);
339 data = g_key_file_to_data(keyfile, NULL, NULL);
340 ret = utils_write_file(filename, data);
341 g_free(data);
342 g_key_file_free(keyfile);
343 return ret;
347 /** Creates a new group.
348 * @param name Name used for @c GKeyFile group.
349 * @return Group. */
350 StashGroup *stash_group_new(const gchar *name)
352 StashGroup *group = g_new0(StashGroup, 1);
354 group->name = name;
355 group->entries = g_ptr_array_new();
356 group->use_defaults = TRUE;
357 return group;
361 /** Frees the memory allocated for setting values in a group.
362 * Useful e.g. to avoid freeing strings individually.
363 * @note This is *not* called by stash_group_free().
364 * @param group . */
365 void stash_group_free_settings(StashGroup *group)
367 StashPref *entry;
368 guint i;
370 foreach_ptr_array(entry, i, group->entries)
372 if (entry->setting_type == G_TYPE_STRING)
373 g_free(*(gchararray *) entry->setting);
374 else if (entry->setting_type == G_TYPE_STRV)
375 g_strfreev(*(gchararray **) entry->setting);
376 else
377 continue;
379 *(gpointer**) entry->setting = NULL;
384 /** Frees a group.
385 * @param group . */
386 void stash_group_free(StashGroup *group)
388 StashPref *entry;
389 guint i;
391 foreach_ptr_array(entry, i, group->entries)
393 if (entry->widget_type == GTK_TYPE_RADIO_BUTTON)
395 g_free(entry->extra.radio_buttons);
397 g_slice_free(StashPref, entry);
399 g_ptr_array_free(group->entries, TRUE);
400 g_free(group);
404 /* Used for selecting groups passed to stash_tree_setup().
405 * @c FALSE by default. */
406 void stash_group_set_various(StashGroup *group, gboolean various)
408 group->various = various;
412 /* When @c FALSE, Stash doesn't change the setting if there is no keyfile entry, so it
413 * remains whatever it was initialized/set to by user code.
414 * @c TRUE by default. */
415 void stash_group_set_use_defaults(StashGroup *group, gboolean use_defaults)
417 group->use_defaults = use_defaults;
421 static StashPref *
422 add_pref(StashGroup *group, GType type, gpointer setting,
423 const gchar *key_name, gpointer default_value)
425 StashPref init = {type, setting, key_name, default_value, G_TYPE_NONE, NULL, {NULL}};
426 StashPref *entry = g_slice_new(StashPref);
428 *entry = init;
430 /* init any pointer settings to NULL so they can be freed later */
431 if (type == G_TYPE_STRING ||
432 type == G_TYPE_STRV)
433 if (group->use_defaults)
434 *(gpointer**)setting = NULL;
436 g_ptr_array_add(group->entries, entry);
437 return entry;
441 /** Adds boolean setting.
442 * @param group .
443 * @param setting Address of setting variable.
444 * @param key_name Name for key in a @c GKeyFile.
445 * @param default_value Value to use if the key doesn't exist when loading. */
446 void stash_group_add_boolean(StashGroup *group, gboolean *setting,
447 const gchar *key_name, gboolean default_value)
449 add_pref(group, G_TYPE_BOOLEAN, setting, key_name, GINT_TO_POINTER(default_value));
453 /** Adds integer setting.
454 * @param group .
455 * @param setting Address of setting variable.
456 * @param key_name Name for key in a @c GKeyFile.
457 * @param default_value Value to use if the key doesn't exist when loading. */
458 void stash_group_add_integer(StashGroup *group, gint *setting,
459 const gchar *key_name, gint default_value)
461 add_pref(group, G_TYPE_INT, setting, key_name, GINT_TO_POINTER(default_value));
465 /** Adds string setting.
466 * The contents of @a setting will be initialized to @c NULL.
467 * @param group .
468 * @param setting Address of setting variable.
469 * @param key_name Name for key in a @c GKeyFile.
470 * @param default_value String to copy if the key doesn't exist when loading, or @c NULL. */
471 void stash_group_add_string(StashGroup *group, gchar **setting,
472 const gchar *key_name, const gchar *default_value)
474 add_pref(group, G_TYPE_STRING, setting, key_name, (gpointer)default_value);
478 /** Adds string vector setting (array of strings).
479 * The contents of @a setting will be initialized to @c NULL.
480 * @param group .
481 * @param setting Address of setting variable.
482 * @param key_name Name for key in a @c GKeyFile.
483 * @param default_value Vector to copy if the key doesn't exist when loading. Usually @c NULL. */
484 void stash_group_add_string_vector(StashGroup *group, gchar ***setting,
485 const gchar *key_name, const gchar **default_value)
487 add_pref(group, G_TYPE_STRV, setting, key_name, (gpointer)default_value);
491 /* *** GTK-related functions *** */
493 static void handle_toggle_button(GtkWidget *widget, gboolean *setting,
494 PrefAction action)
496 switch (action)
498 case PREF_DISPLAY:
499 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), *setting);
500 break;
501 case PREF_UPDATE:
502 *setting = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
503 break;
508 static void handle_spin_button(GtkWidget *widget, StashPref *entry,
509 PrefAction action)
511 gint *setting = entry->setting;
513 g_assert(entry->setting_type == G_TYPE_INT); /* only int spin prefs */
515 switch (action)
517 case PREF_DISPLAY:
518 gtk_spin_button_set_value(GTK_SPIN_BUTTON(widget), *setting);
519 break;
520 case PREF_UPDATE:
521 /* if the widget is focussed, the value might not be updated */
522 gtk_spin_button_update(GTK_SPIN_BUTTON(widget));
523 *setting = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(widget));
524 break;
529 static void handle_combo_box(GtkWidget *widget, StashPref *entry,
530 PrefAction action)
532 gint *setting = entry->setting;
534 switch (action)
536 case PREF_DISPLAY:
537 gtk_combo_box_set_active(GTK_COMBO_BOX(widget), *setting);
538 break;
539 case PREF_UPDATE:
540 *setting = gtk_combo_box_get_active(GTK_COMBO_BOX(widget));
541 break;
546 static void handle_entry(GtkWidget *widget, StashPref *entry,
547 PrefAction action)
549 gchararray *setting = entry->setting;
551 switch (action)
553 case PREF_DISPLAY:
554 gtk_entry_set_text(GTK_ENTRY(widget), *setting);
555 break;
556 case PREF_UPDATE:
557 g_free(*setting);
558 *setting = g_strdup(gtk_entry_get_text(GTK_ENTRY(widget)));
559 break;
564 static void handle_combo_box_entry(GtkWidget *widget, StashPref *entry,
565 PrefAction action)
567 widget = gtk_bin_get_child(GTK_BIN(widget));
568 handle_entry(widget, entry, action);
572 /* taken from Glade 2.x generated support.c */
573 static GtkWidget*
574 lookup_widget(GtkWidget *widget, const gchar *widget_name)
576 GtkWidget *parent, *found_widget;
578 g_return_val_if_fail(widget != NULL, NULL);
579 g_return_val_if_fail(widget_name != NULL, NULL);
581 for (;;)
583 if (GTK_IS_MENU(widget))
584 parent = gtk_menu_get_attach_widget(GTK_MENU(widget));
585 else
586 parent = gtk_widget_get_parent(widget);
587 if (parent == NULL)
588 parent = (GtkWidget*) g_object_get_data(G_OBJECT(widget), "GladeParentKey");
589 if (parent == NULL)
590 break;
591 widget = parent;
594 found_widget = (GtkWidget*) g_object_get_data(G_OBJECT(widget), widget_name);
595 if (G_UNLIKELY(found_widget == NULL))
596 g_warning("Widget not found: %s", widget_name);
597 return found_widget;
601 static GtkWidget *
602 get_widget(GtkWidget *owner, StashWidgetID widget_id)
604 GtkWidget *widget;
606 if (owner)
607 widget = lookup_widget(owner, (const gchar *)widget_id);
608 else
609 widget = (GtkWidget *)widget_id;
611 if (!GTK_IS_WIDGET(widget))
613 g_warning("Unknown widget in %s()!", G_STRFUNC);
614 return NULL;
616 return widget;
620 static void handle_radio_button(GtkWidget *widget, gint enum_id, gboolean *setting,
621 PrefAction action)
623 switch (action)
625 case PREF_DISPLAY:
626 if (*setting == enum_id)
627 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), TRUE);
628 break;
629 case PREF_UPDATE:
630 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
631 *setting = enum_id;
632 break;
637 static void handle_radio_buttons(GtkWidget *owner, StashPref *entry,
638 PrefAction action)
640 EnumWidget *field = entry->extra.radio_buttons;
641 gsize count = 0;
642 GtkWidget *widget = NULL;
644 while (1)
646 widget = get_widget(owner, field->widget_id);
648 if (!widget)
649 continue;
651 count++;
652 handle_radio_button(widget, field->enum_id, entry->setting, action);
653 field++;
654 if (!field->widget_id)
655 break;
657 if (g_slist_length(gtk_radio_button_get_group(GTK_RADIO_BUTTON(widget))) != count)
658 g_warning("Missing/invalid radio button widget IDs found!");
662 static void handle_widget_property(GtkWidget *widget, StashPref *entry,
663 PrefAction action)
665 GObject *object = G_OBJECT(widget);
666 const gchar *name = entry->extra.property_name;
668 switch (action)
670 case PREF_DISPLAY:
671 g_object_set(object, name, entry->setting, NULL);
672 break;
673 case PREF_UPDATE:
674 if (entry->setting_type == G_TYPE_STRING)
675 g_free(entry->setting);
676 /* TODO: Which other types need freeing here? */
678 g_object_get(object, name, entry->setting, NULL);
679 break;
684 static void pref_action(PrefAction action, StashGroup *group, GtkWidget *owner)
686 StashPref *entry;
687 guint i;
689 foreach_ptr_array(entry, i, group->entries)
691 GtkWidget *widget;
693 /* ignore settings with no widgets */
694 if (entry->widget_type == G_TYPE_NONE)
695 continue;
697 /* radio buttons have several widgets */
698 if (entry->widget_type == GTK_TYPE_RADIO_BUTTON)
700 handle_radio_buttons(owner, entry, action);
701 continue;
704 widget = get_widget(owner, entry->widget_id);
705 if (!widget)
707 g_warning("Unknown widget for %s::%s in %s()!", group->name, entry->key_name,
708 G_STRFUNC);
709 continue;
712 /* note: can't use switch for GTK_TYPE macros */
713 if (entry->widget_type == GTK_TYPE_TOGGLE_BUTTON)
714 handle_toggle_button(widget, entry->setting, action);
715 else if (entry->widget_type == GTK_TYPE_SPIN_BUTTON)
716 handle_spin_button(widget, entry, action);
717 else if (entry->widget_type == GTK_TYPE_COMBO_BOX)
718 handle_combo_box(widget, entry, action);
719 else if (entry->widget_type == TYPE_COMBO_BOX_ENTRY)
720 handle_combo_box_entry(widget, entry, action);
721 else if (entry->widget_type == GTK_TYPE_ENTRY)
722 handle_entry(widget, entry, action);
723 else if (entry->widget_type == G_TYPE_PARAM)
724 handle_widget_property(widget, entry, action);
725 else
726 g_warning("Unhandled type for %s::%s in %s()!", group->name, entry->key_name,
727 G_STRFUNC);
732 /** Applies Stash settings to widgets, usually called before displaying the widgets.
733 * The @a owner argument depends on which type you use for @ref StashWidgetID.
734 * @param group .
735 * @param owner If non-NULL, used to lookup widgets by name, otherwise
736 * widget pointers are assumed.
737 * @see stash_group_update(). */
738 void stash_group_display(StashGroup *group, GtkWidget *owner)
740 pref_action(PREF_DISPLAY, group, owner);
744 /** Applies widget values to Stash settings, usually called after displaying the widgets.
745 * The @a owner argument depends on which type you use for @ref StashWidgetID.
746 * @param group .
747 * @param owner If non-NULL, used to lookup widgets by name, otherwise
748 * widget pointers are assumed.
749 * @see stash_group_display(). */
750 void stash_group_update(StashGroup *group, GtkWidget *owner)
752 pref_action(PREF_UPDATE, group, owner);
756 static StashPref *
757 add_widget_pref(StashGroup *group, GType setting_type, gpointer setting,
758 const gchar *key_name, gpointer default_value,
759 GType widget_type, StashWidgetID widget_id)
761 StashPref *entry =
762 add_pref(group, setting_type, setting, key_name, default_value);
764 entry->widget_type = widget_type;
765 entry->widget_id = widget_id;
766 return entry;
770 /** Adds a @c GtkToggleButton (or @c GtkCheckButton) widget pref.
771 * @param group .
772 * @param setting Address of setting variable.
773 * @param key_name Name for key in a @c GKeyFile.
774 * @param default_value Value to use if the key doesn't exist when loading.
775 * @param widget_id @c GtkWidget pointer or string to lookup widget later.
776 * @see stash_group_add_radio_buttons(). */
777 void stash_group_add_toggle_button(StashGroup *group, gboolean *setting,
778 const gchar *key_name, gboolean default_value, StashWidgetID widget_id)
780 add_widget_pref(group, G_TYPE_BOOLEAN, setting, key_name, GINT_TO_POINTER(default_value),
781 GTK_TYPE_TOGGLE_BUTTON, widget_id);
785 /** Adds a @c GtkRadioButton widget group pref.
786 * @param group .
787 * @param setting Address of setting variable.
788 * @param key_name Name for key in a @c GKeyFile.
789 * @param default_value Value to use if the key doesn't exist when loading.
790 * @param widget_id @c GtkWidget pointer or string to lookup widget later.
791 * @param enum_id Enum value for @a widget_id.
792 * @param ... pairs of @a widget_id, @a enum_id.
793 * Example (using widget lookup strings, but widget pointers can also work):
794 * @code
795 * enum {FOO, BAR};
796 * stash_group_add_radio_buttons(group, &which_one_setting, "which_one", BAR,
797 * "radio_foo", FOO, "radio_bar", BAR, NULL);
798 * @endcode */
799 void stash_group_add_radio_buttons(StashGroup *group, gint *setting,
800 const gchar *key_name, gint default_value,
801 StashWidgetID widget_id, gint enum_id, ...)
803 StashPref *entry =
804 add_widget_pref(group, G_TYPE_INT, setting, key_name, GINT_TO_POINTER(default_value),
805 GTK_TYPE_RADIO_BUTTON, NULL);
806 va_list args;
807 gsize count = 1;
808 EnumWidget *item, *array;
810 /* count pairs of args */
811 va_start(args, enum_id);
812 while (1)
814 if (!va_arg(args, gpointer))
815 break;
816 va_arg(args, gint);
817 count++;
819 va_end(args);
821 array = g_new0(EnumWidget, count + 1);
822 entry->extra.radio_buttons = array;
824 va_start(args, enum_id);
825 foreach_c_array(item, array, count)
827 if (item == array)
829 /* first element */
830 item->widget_id = widget_id;
831 item->enum_id = enum_id;
833 else
835 item->widget_id = va_arg(args, gpointer);
836 item->enum_id = va_arg(args, gint);
839 va_end(args);
843 /** Adds a @c GtkSpinButton widget pref.
844 * @param group .
845 * @param setting Address of setting variable.
846 * @param key_name Name for key in a @c GKeyFile.
847 * @param default_value Value to use if the key doesn't exist when loading.
848 * @param widget_id @c GtkWidget pointer or string to lookup widget later. */
849 void stash_group_add_spin_button_integer(StashGroup *group, gint *setting,
850 const gchar *key_name, gint default_value, StashWidgetID widget_id)
852 add_widget_pref(group, G_TYPE_INT, setting, key_name, GINT_TO_POINTER(default_value),
853 GTK_TYPE_SPIN_BUTTON, widget_id);
857 /** Adds a @c GtkComboBox widget pref.
858 * @param group .
859 * @param setting Address of setting variable.
860 * @param key_name Name for key in a @c GKeyFile.
861 * @param default_value Value to use if the key doesn't exist when loading.
862 * @param widget_id @c GtkWidget pointer or string to lookup widget later.
863 * @see stash_group_add_combo_box_entry(). */
864 void stash_group_add_combo_box(StashGroup *group, gint *setting,
865 const gchar *key_name, gint default_value, StashWidgetID widget_id)
867 add_widget_pref(group, G_TYPE_INT, setting, key_name, GINT_TO_POINTER(default_value),
868 GTK_TYPE_COMBO_BOX, widget_id);
872 /** Adds a @c GtkComboBoxEntry widget pref.
873 * @param group .
874 * @param setting Address of setting variable.
875 * @param key_name Name for key in a @c GKeyFile.
876 * @param default_value Value to use if the key doesn't exist when loading.
877 * @param widget_id @c GtkWidget pointer or string to lookup widget later. */
878 /* We could maybe also have something like stash_group_add_combo_box_entry_with_menu()
879 * for the history list - or should that be stored as a separate setting? */
880 void stash_group_add_combo_box_entry(StashGroup *group, gchar **setting,
881 const gchar *key_name, const gchar *default_value, StashWidgetID widget_id)
883 add_widget_pref(group, G_TYPE_STRING, setting, key_name, (gpointer)default_value,
884 TYPE_COMBO_BOX_ENTRY, widget_id);
888 /** Adds a @c GtkEntry widget pref.
889 * @param group .
890 * @param setting Address of setting variable.
891 * @param key_name Name for key in a @c GKeyFile.
892 * @param default_value Value to use if the key doesn't exist when loading.
893 * @param widget_id @c GtkWidget pointer or string to lookup widget later. */
894 void stash_group_add_entry(StashGroup *group, gchar **setting,
895 const gchar *key_name, const gchar *default_value, StashWidgetID widget_id)
897 add_widget_pref(group, G_TYPE_STRING, setting, key_name, (gpointer)default_value,
898 GTK_TYPE_ENTRY, widget_id);
902 static GType object_get_property_type(GObject *object, const gchar *property_name)
904 GObjectClass *klass = G_OBJECT_GET_CLASS(object);
905 GParamSpec *ps;
907 ps = g_object_class_find_property(klass, property_name);
908 return ps->value_type;
912 /** Adds a widget's read/write property to the stash group.
913 * The property will be set when calling
914 * stash_group_display(), and read when calling stash_group_update().
915 * @param group .
916 * @param setting Address of e.g. an integer if using an integer property.
917 * @param key_name Name for key in a @c GKeyFile.
918 * @param default_value Value to use if the key doesn't exist when loading.
919 * Should be cast into a pointer e.g. with @c GINT_TO_POINTER().
920 * @param widget_id @c GtkWidget pointer or string to lookup widget later.
921 * @param property_name .
922 * @param type can be @c 0 if passing a @c GtkWidget as the @a widget_id argument to look it up from the
923 * @c GObject data.
924 * @warning Currently only string GValue properties will be freed before setting; patch for
925 * other types - see @c handle_widget_property(). */
926 void stash_group_add_widget_property(StashGroup *group, gpointer setting,
927 const gchar *key_name, gpointer default_value, StashWidgetID widget_id,
928 const gchar *property_name, GType type)
930 if (!type)
931 type = object_get_property_type(G_OBJECT(widget_id), property_name);
933 add_widget_pref(group, type, setting, key_name, default_value,
934 G_TYPE_PARAM, widget_id)->extra.property_name = property_name;
938 enum
940 STASH_TREE_NAME,
941 STASH_TREE_VALUE,
942 STASH_TREE_COUNT
946 struct StashTreeValue
948 const gchar *group_name;
949 StashPref *pref;
950 union
952 gchararray tree_string;
953 gint tree_int;
954 } data;
957 typedef struct StashTreeValue StashTreeValue;
960 static void stash_tree_renderer_set_data(GtkCellLayout *cell_layout, GtkCellRenderer *cell,
961 GtkTreeModel *model, GtkTreeIter *iter, gpointer user_data)
963 GType cell_type = GPOINTER_TO_SIZE(user_data);
964 StashTreeValue *value;
965 StashPref *pref;
966 gboolean matches_type;
968 gtk_tree_model_get(model, iter, STASH_TREE_VALUE, &value, -1);
969 pref = value->pref;
970 matches_type = pref->setting_type == cell_type;
971 g_object_set(cell, "visible", matches_type, "sensitive", matches_type,
972 cell_type == G_TYPE_BOOLEAN ? "activatable" : "editable", matches_type, NULL);
974 if (matches_type)
976 switch (pref->setting_type)
978 case G_TYPE_BOOLEAN:
979 g_object_set(cell, "active", value->data.tree_int, NULL);
980 break;
981 case G_TYPE_INT:
983 gchar *text = g_strdup_printf("%d", value->data.tree_int);
984 g_object_set(cell, "text", text, NULL);
985 g_free(text);
986 break;
988 case G_TYPE_STRING:
989 g_object_set(cell, "text", value->data.tree_string, NULL);
990 break;
996 static void stash_tree_renderer_edited(gchar *path_str, gchar *new_text, GtkTreeModel *model)
998 GtkTreePath *path;
999 GtkTreeIter iter;
1000 StashTreeValue *value;
1001 StashPref *pref;
1003 path = gtk_tree_path_new_from_string(path_str);
1004 gtk_tree_model_get_iter(model, &iter, path);
1005 gtk_tree_model_get(model, &iter, STASH_TREE_VALUE, &value, -1);
1006 pref = value->pref;
1008 switch (pref->setting_type)
1010 case G_TYPE_BOOLEAN:
1011 value->data.tree_int = !value->data.tree_int;
1012 break;
1013 case G_TYPE_INT:
1014 value->data.tree_int = atoi(new_text);
1015 break;
1016 case G_TYPE_STRING:
1017 SETPTR(value->data.tree_string, g_strdup(new_text));
1018 break;
1021 gtk_tree_model_row_changed(model, path, &iter);
1022 gtk_tree_path_free(path);
1026 static void stash_tree_boolean_toggled(GtkCellRendererToggle *cell, gchar *path_str,
1027 GtkTreeModel *model)
1029 stash_tree_renderer_edited(path_str, NULL, model);
1033 static void stash_tree_string_edited(GtkCellRenderer *cell, gchar *path_str, gchar *new_text,
1034 GtkTreeModel *model)
1036 stash_tree_renderer_edited(path_str, new_text, model);
1040 static gboolean stash_tree_discard_value(GtkTreeModel *model, GtkTreePath *path,
1041 GtkTreeIter *iter, gpointer user_data)
1043 StashTreeValue *value;
1045 gtk_tree_model_get(model, iter, STASH_TREE_VALUE, &value, -1);
1046 if (value->pref->setting_type == G_TYPE_STRING)
1047 g_free(value->data.tree_string);
1048 g_free(value);
1050 return FALSE;
1054 static void stash_tree_destroy_cb(GtkWidget *widget, gpointer user_data)
1056 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(widget));
1057 gtk_tree_model_foreach(model, stash_tree_discard_value, NULL);
1061 static void stash_tree_append_pref(StashGroup *group, StashPref *entry, GtkListStore *store,
1062 PrefAction action)
1064 GtkTreeIter iter;
1065 StashTreeValue *value;
1067 value = g_new0(StashTreeValue, 1);
1069 value->group_name = group->name;
1070 value->pref = entry;
1072 gtk_list_store_append(store, &iter);
1073 gtk_list_store_set(store, &iter, STASH_TREE_NAME, entry->key_name,
1074 STASH_TREE_VALUE, value, -1);
1078 static void stash_tree_append_prefs(GPtrArray *group_array,
1079 GtkListStore *store, PrefAction action)
1081 StashGroup *group;
1082 guint i, j;
1083 StashPref *entry;
1085 foreach_ptr_array(group, i, group_array)
1087 if (group->various)
1089 foreach_ptr_array(entry, j, group->entries)
1090 stash_tree_append_pref(group, entry, store, action);
1096 /* Setups a simple editor for stash preferences based on the widget arguments.
1097 * group_array - Array of groups which's settings will be edited.
1098 * tree - GtkTreeView in which to edit the preferences. Must be empty. */
1099 void stash_tree_setup(GPtrArray *group_array, GtkTreeView *tree)
1101 GtkListStore *store;
1102 GtkTreeModel *model;
1103 GtkCellRenderer *cell;
1104 GtkTreeViewColumn *column;
1105 GtkAdjustment *adjustment;
1107 store = gtk_list_store_new(STASH_TREE_COUNT, G_TYPE_STRING, G_TYPE_POINTER);
1108 stash_tree_append_prefs(group_array, store, PREF_DISPLAY);
1109 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store), STASH_TREE_NAME,
1110 GTK_SORT_ASCENDING);
1112 model = GTK_TREE_MODEL(store);
1113 gtk_tree_view_set_model(tree, model);
1114 g_object_unref(G_OBJECT(store));
1115 g_signal_connect(tree, "destroy", G_CALLBACK(stash_tree_destroy_cb), NULL);
1117 cell = gtk_cell_renderer_text_new();
1118 column = gtk_tree_view_column_new_with_attributes(_("Name"), cell, "text",
1119 STASH_TREE_NAME, NULL);
1120 gtk_tree_view_column_set_sort_column_id(column, STASH_TREE_NAME);
1121 gtk_tree_view_column_set_sort_indicator(column, TRUE);
1122 gtk_tree_view_append_column(tree, column);
1124 column = gtk_tree_view_column_new();
1125 gtk_tree_view_column_set_title(column, _("Value"));
1126 gtk_tree_view_append_column(tree, column);
1127 /* boolean renderer */
1128 cell = gtk_cell_renderer_toggle_new();
1129 g_signal_connect(cell, "toggled", G_CALLBACK(stash_tree_boolean_toggled), model);
1130 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(column), cell, FALSE);
1131 gtk_cell_layout_set_cell_data_func(GTK_CELL_LAYOUT(column), cell,
1132 stash_tree_renderer_set_data, GSIZE_TO_POINTER(G_TYPE_BOOLEAN), NULL);
1133 /* string renderer */
1134 cell = gtk_cell_renderer_text_new();
1135 g_object_set(cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
1136 g_signal_connect(cell, "edited", G_CALLBACK(stash_tree_string_edited), model);
1137 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(column), cell, TRUE);
1138 gtk_cell_layout_set_cell_data_func(GTK_CELL_LAYOUT(column), cell,
1139 stash_tree_renderer_set_data, GSIZE_TO_POINTER(G_TYPE_STRING), NULL);
1140 /* integer renderer */
1141 cell = gtk_cell_renderer_spin_new();
1142 adjustment = GTK_ADJUSTMENT(gtk_adjustment_new(0, G_MININT, G_MAXINT, 1, 10, 0));
1143 g_object_set(cell, "adjustment", adjustment, NULL);
1144 g_signal_connect(cell, "edited", G_CALLBACK(stash_tree_string_edited), model);
1145 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(column), cell, FALSE);
1146 gtk_cell_layout_set_cell_data_func(GTK_CELL_LAYOUT(column), cell,
1147 stash_tree_renderer_set_data, GSIZE_TO_POINTER(G_TYPE_INT), NULL);
1151 static void stash_tree_display_pref(StashTreeValue *value, StashPref *entry)
1153 switch (entry->setting_type)
1155 case G_TYPE_BOOLEAN:
1156 value->data.tree_int = *(gboolean *) entry->setting;
1157 break;
1158 case G_TYPE_INT:
1159 value->data.tree_int = *(gint *) entry->setting;
1160 break;
1161 case G_TYPE_STRING:
1162 SETPTR(value->data.tree_string, g_strdup(*(gchararray *) entry->setting));
1163 break;
1164 default:
1165 g_warning("Unhandled type for %s::%s in %s()!", value->group_name,
1166 entry->key_name, G_STRFUNC);
1171 static void stash_tree_update_pref(StashTreeValue *value, StashPref *entry)
1173 switch (entry->setting_type)
1175 case G_TYPE_BOOLEAN:
1176 *(gboolean *) entry->setting = value->data.tree_int;
1177 break;
1178 case G_TYPE_INT:
1179 *(gint *) entry->setting = value->data.tree_int;
1180 break;
1181 case G_TYPE_STRING:
1183 gchararray *text = entry->setting;
1184 SETPTR(*text, g_strdup(value->data.tree_string));
1185 break;
1187 default:
1188 g_warning("Unhandled type for %s::%s in %s()!", value->group_name,
1189 entry->key_name, G_STRFUNC);
1194 static void stash_tree_action(GtkTreeModel *model, PrefAction action)
1196 GtkTreeIter iter;
1197 gboolean valid = gtk_tree_model_get_iter_first(model, &iter);
1198 StashTreeValue *value;
1200 while (valid)
1202 gtk_tree_model_get(model, &iter, STASH_TREE_VALUE, &value, -1);
1204 switch (action)
1206 case PREF_DISPLAY:
1207 stash_tree_display_pref(value, value->pref);
1208 break;
1209 case PREF_UPDATE:
1210 stash_tree_update_pref(value, value->pref);
1211 break;
1213 valid = gtk_tree_model_iter_next(model, &iter);
1218 void stash_tree_display(GtkTreeView *tree)
1220 stash_tree_action(gtk_tree_view_get_model(tree), PREF_DISPLAY);
1224 void stash_tree_update(GtkTreeView *tree)
1226 stash_tree_action(gtk_tree_view_get_model(tree), PREF_UPDATE);