Merge pull request #1133 from techee/readme_rst
[geany-mirror.git] / src / stash.c
blobd028e4f3e5f10a77ac8e77ad2cb426c15f1f0f7d
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.
79 #include "stash.h"
81 #include "support.h" /* only for _("text") */
82 #include "utils.h" /* only for foreach_*, utils_get_setting_*(). Stash should not depend on Geany. */
84 #include <stdlib.h> /* only for atoi() */
87 /* GTK3 removed ComboBoxEntry, but we need a value to differentiate combo box with and
88 * without entries, and it must not collide with other GTypes */
89 #ifdef GTK_TYPE_COMBO_BOX_ENTRY
90 # define TYPE_COMBO_BOX_ENTRY GTK_TYPE_COMBO_BOX_ENTRY
91 #else /* !GTK_TYPE_COMBO_BOX_ENTRY */
92 # define TYPE_COMBO_BOX_ENTRY get_combo_box_entry_type()
93 static GType get_combo_box_entry_type(void)
95 static volatile gsize type = 0;
96 if (g_once_init_enter(&type))
98 GType g_type = g_type_register_static_simple(GTK_TYPE_COMBO_BOX, "dummy-combo-box-entry",
99 sizeof(GtkComboBoxClass), NULL, sizeof(GtkComboBox), NULL, G_TYPE_FLAG_ABSTRACT);
100 g_once_init_leave(&type, g_type);
102 return type;
104 #endif /* !GTK_TYPE_COMBO_BOX_ENTRY */
107 struct StashPref
109 GType setting_type; /* e.g. G_TYPE_INT */
110 gpointer setting; /* Address of a variable */
111 const gchar *key_name;
112 gpointer default_value; /* Default value, e.g. (gpointer)1 */
113 GType widget_type; /* e.g. GTK_TYPE_TOGGLE_BUTTON */
114 StashWidgetID widget_id; /* (GtkWidget*) or (gchar*) */
115 union
117 struct EnumWidget *radio_buttons;
118 const gchar *property_name;
119 } extra; /* extra fields depending on widget_type */
122 typedef struct StashPref StashPref;
124 struct StashGroup
126 guint refcount; /* ref count for GBoxed implementation */
127 const gchar *name; /* group name to use in the keyfile */
128 GPtrArray *entries; /* array of (StashPref*) */
129 gboolean various; /* mark group for display/edit in stash treeview */
130 gboolean use_defaults; /* use default values if there's no keyfile entry */
133 typedef struct EnumWidget
135 StashWidgetID widget_id;
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 GEANY_API_SYMBOL
279 void stash_group_load_from_key_file(StashGroup *group, GKeyFile *keyfile)
281 keyfile_action(SETTING_READ, group, keyfile);
285 /** Writes group settings into key values in @a keyfile.
286 * @a keyfile is usually written to a configuration file afterwards.
287 * @param group .
288 * @param keyfile . */
289 GEANY_API_SYMBOL
290 void stash_group_save_to_key_file(StashGroup *group, GKeyFile *keyfile)
292 keyfile_action(SETTING_WRITE, group, keyfile);
296 /** Reads group settings from a configuration file using @c GKeyFile.
297 * @note Stash settings will be initialized to defaults if the keyfile
298 * couldn't be loaded from disk.
299 * @param group .
300 * @param filename Filename of the file to read, in locale encoding.
301 * @return @c TRUE if a key file could be loaded.
302 * @see stash_group_load_from_key_file().
304 GEANY_API_SYMBOL
305 gboolean stash_group_load_from_file(StashGroup *group, const gchar *filename)
307 GKeyFile *keyfile;
308 gboolean ret;
310 keyfile = g_key_file_new();
311 ret = g_key_file_load_from_file(keyfile, filename, 0, NULL);
312 /* even on failure we load settings to apply defaults */
313 stash_group_load_from_key_file(group, keyfile);
315 g_key_file_free(keyfile);
316 return ret;
320 /** Writes group settings to a configuration file using @c GKeyFile.
322 * @param group .
323 * @param filename Filename of the file to write, in locale encoding.
324 * @param flags Keyfile options - @c G_KEY_FILE_NONE is the most efficient.
325 * @return 0 if the file was successfully written, otherwise the @c errno of the
326 * failed operation is returned.
327 * @see stash_group_save_to_key_file().
329 GEANY_API_SYMBOL
330 gint stash_group_save_to_file(StashGroup *group, const gchar *filename,
331 GKeyFileFlags flags)
333 GKeyFile *keyfile;
334 gchar *data;
335 gint ret;
337 keyfile = g_key_file_new();
338 /* if we need to keep comments or translations, try to load first */
339 if (flags)
340 g_key_file_load_from_file(keyfile, filename, flags, NULL);
342 stash_group_save_to_key_file(group, keyfile);
343 data = g_key_file_to_data(keyfile, NULL, NULL);
344 ret = utils_write_file(filename, data);
345 g_free(data);
346 g_key_file_free(keyfile);
347 return ret;
351 static void free_stash_pref(StashPref *pref)
353 if (pref->widget_type == GTK_TYPE_RADIO_BUTTON)
354 g_free(pref->extra.radio_buttons);
356 g_slice_free(StashPref, pref);
360 /** Creates a new group.
361 * @param name Name used for @c GKeyFile group.
362 * @return Group. */
363 GEANY_API_SYMBOL
364 StashGroup *stash_group_new(const gchar *name)
366 StashGroup *group = g_slice_new0(StashGroup);
368 group->name = name;
369 group->entries = g_ptr_array_new_with_free_func((GDestroyNotify) free_stash_pref);
370 group->use_defaults = TRUE;
371 group->refcount = 1;
372 return group;
376 /** Frees the memory allocated for setting values in a group.
377 * Useful e.g. to avoid freeing strings individually.
378 * @note This is *not* called by stash_group_free().
379 * @param group . */
380 GEANY_API_SYMBOL
381 void stash_group_free_settings(StashGroup *group)
383 StashPref *entry;
384 guint i;
386 foreach_ptr_array(entry, i, group->entries)
388 if (entry->setting_type == G_TYPE_STRING)
389 g_free(*(gchararray *) entry->setting);
390 else if (entry->setting_type == G_TYPE_STRV)
391 g_strfreev(*(gchararray **) entry->setting);
392 else
393 continue;
395 *(gpointer**) entry->setting = NULL;
400 static StashGroup *stash_group_dup(StashGroup *src)
402 g_atomic_int_inc(&src->refcount);
404 return src;
408 /** Frees a group.
409 * @param group . */
410 GEANY_API_SYMBOL
411 void stash_group_free(StashGroup *group)
413 if (g_atomic_int_dec_and_test(&group->refcount))
415 g_ptr_array_free(group->entries, TRUE);
416 g_slice_free(StashGroup, group);
421 /** Gets the GBoxed-derived GType for StashGroup
423 * @return StashGroup type . */
424 GEANY_API_SYMBOL
425 GType stash_group_get_type(void);
427 G_DEFINE_BOXED_TYPE(StashGroup, stash_group, stash_group_dup, stash_group_free);
430 /* Used for selecting groups passed to stash_tree_setup().
431 * @c FALSE by default. */
432 void stash_group_set_various(StashGroup *group, gboolean various)
434 group->various = various;
438 /* When @c FALSE, Stash doesn't change the setting if there is no keyfile entry, so it
439 * remains whatever it was initialized/set to by user code.
440 * @c TRUE by default. */
441 void stash_group_set_use_defaults(StashGroup *group, gboolean use_defaults)
443 group->use_defaults = use_defaults;
447 static StashPref *
448 add_pref(StashGroup *group, GType type, gpointer setting,
449 const gchar *key_name, gpointer default_value)
451 StashPref init = {type, setting, key_name, default_value, G_TYPE_NONE, NULL, {NULL}};
452 StashPref *entry = g_slice_new(StashPref);
454 *entry = init;
456 /* init any pointer settings to NULL so they can be freed later */
457 if (type == G_TYPE_STRING ||
458 type == G_TYPE_STRV)
459 if (group->use_defaults)
460 *(gpointer**)setting = NULL;
462 g_ptr_array_add(group->entries, entry);
463 return entry;
467 /** Adds boolean setting.
468 * @param group .
469 * @param setting Address of setting variable.
470 * @param key_name Name for key in a @c GKeyFile.
471 * @param default_value Value to use if the key doesn't exist when loading. */
472 GEANY_API_SYMBOL
473 void stash_group_add_boolean(StashGroup *group, gboolean *setting,
474 const gchar *key_name, gboolean default_value)
476 add_pref(group, G_TYPE_BOOLEAN, setting, key_name, GINT_TO_POINTER(default_value));
480 /** Adds integer setting.
481 * @param group .
482 * @param setting Address of setting variable.
483 * @param key_name Name for key in a @c GKeyFile.
484 * @param default_value Value to use if the key doesn't exist when loading. */
485 GEANY_API_SYMBOL
486 void stash_group_add_integer(StashGroup *group, gint *setting,
487 const gchar *key_name, gint default_value)
489 add_pref(group, G_TYPE_INT, setting, key_name, GINT_TO_POINTER(default_value));
493 /** Adds string setting.
494 * The contents of @a setting will be initialized to @c NULL.
495 * @param group .
496 * @param setting Address of setting variable.
497 * @param key_name Name for key in a @c GKeyFile.
498 * @param default_value @nullable String to copy if the key doesn't exist when loading, or @c NULL. */
499 GEANY_API_SYMBOL
500 void stash_group_add_string(StashGroup *group, gchar **setting,
501 const gchar *key_name, const gchar *default_value)
503 add_pref(group, G_TYPE_STRING, setting, key_name, (gpointer)default_value);
507 /** Adds string vector setting (array of strings).
508 * The contents of @a setting will be initialized to @c NULL.
509 * @param group .
510 * @param setting Address of setting variable.
511 * @param key_name Name for key in a @c GKeyFile.
512 * @param default_value Vector to copy if the key doesn't exist when loading. Usually @c NULL. */
513 GEANY_API_SYMBOL
514 void stash_group_add_string_vector(StashGroup *group, gchar ***setting,
515 const gchar *key_name, const gchar **default_value)
517 add_pref(group, G_TYPE_STRV, setting, key_name, (gpointer)default_value);
521 /* *** GTK-related functions *** */
523 static void handle_toggle_button(GtkWidget *widget, gboolean *setting,
524 PrefAction action)
526 switch (action)
528 case PREF_DISPLAY:
529 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), *setting);
530 break;
531 case PREF_UPDATE:
532 *setting = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
533 break;
538 static void handle_spin_button(GtkWidget *widget, StashPref *entry,
539 PrefAction action)
541 gint *setting = entry->setting;
543 g_assert(entry->setting_type == G_TYPE_INT); /* only int spin prefs */
545 switch (action)
547 case PREF_DISPLAY:
548 gtk_spin_button_set_value(GTK_SPIN_BUTTON(widget), *setting);
549 break;
550 case PREF_UPDATE:
551 /* if the widget is focussed, the value might not be updated */
552 gtk_spin_button_update(GTK_SPIN_BUTTON(widget));
553 *setting = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(widget));
554 break;
559 static void handle_combo_box(GtkWidget *widget, StashPref *entry,
560 PrefAction action)
562 gint *setting = entry->setting;
564 switch (action)
566 case PREF_DISPLAY:
567 gtk_combo_box_set_active(GTK_COMBO_BOX(widget), *setting);
568 break;
569 case PREF_UPDATE:
570 *setting = gtk_combo_box_get_active(GTK_COMBO_BOX(widget));
571 break;
576 static void handle_entry(GtkWidget *widget, StashPref *entry,
577 PrefAction action)
579 gchararray *setting = entry->setting;
581 switch (action)
583 case PREF_DISPLAY:
584 gtk_entry_set_text(GTK_ENTRY(widget), *setting);
585 break;
586 case PREF_UPDATE:
587 g_free(*setting);
588 *setting = g_strdup(gtk_entry_get_text(GTK_ENTRY(widget)));
589 break;
594 static void handle_combo_box_entry(GtkWidget *widget, StashPref *entry,
595 PrefAction action)
597 widget = gtk_bin_get_child(GTK_BIN(widget));
598 handle_entry(widget, entry, action);
602 /* taken from Glade 2.x generated support.c */
603 static GtkWidget*
604 lookup_widget(GtkWidget *widget, const gchar *widget_name)
606 GtkWidget *parent, *found_widget;
608 g_return_val_if_fail(widget != NULL, NULL);
609 g_return_val_if_fail(widget_name != NULL, NULL);
611 for (;;)
613 if (GTK_IS_MENU(widget))
614 parent = gtk_menu_get_attach_widget(GTK_MENU(widget));
615 else
616 parent = gtk_widget_get_parent(widget);
617 if (parent == NULL)
618 parent = (GtkWidget*) g_object_get_data(G_OBJECT(widget), "GladeParentKey");
619 if (parent == NULL)
620 break;
621 widget = parent;
624 found_widget = (GtkWidget*) g_object_get_data(G_OBJECT(widget), widget_name);
625 if (G_UNLIKELY(found_widget == NULL))
626 g_warning("Widget not found: %s", widget_name);
627 return found_widget;
631 static GtkWidget *
632 get_widget(GtkWidget *owner, StashWidgetID widget_id)
634 GtkWidget *widget;
636 if (owner)
637 widget = lookup_widget(owner, (const gchar *)widget_id);
638 else
639 widget = (GtkWidget *)widget_id;
641 if (!GTK_IS_WIDGET(widget))
643 g_warning("Unknown widget in %s()!", G_STRFUNC);
644 return NULL;
646 return widget;
650 static void handle_radio_button(GtkWidget *widget, gint enum_id, gboolean *setting,
651 PrefAction action)
653 switch (action)
655 case PREF_DISPLAY:
656 if (*setting == enum_id)
657 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), TRUE);
658 break;
659 case PREF_UPDATE:
660 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
661 *setting = enum_id;
662 break;
667 static void handle_radio_buttons(GtkWidget *owner, StashPref *entry,
668 PrefAction action)
670 EnumWidget *field = entry->extra.radio_buttons;
671 gsize count = 0;
672 GtkWidget *widget = NULL;
674 while (1)
676 widget = get_widget(owner, field->widget_id);
678 if (!widget)
679 continue;
681 count++;
682 handle_radio_button(widget, field->enum_id, entry->setting, action);
683 field++;
684 if (!field->widget_id)
685 break;
687 if (g_slist_length(gtk_radio_button_get_group(GTK_RADIO_BUTTON(widget))) != count)
688 g_warning("Missing/invalid radio button widget IDs found!");
692 static void handle_widget_property(GtkWidget *widget, StashPref *entry,
693 PrefAction action)
695 GObject *object = G_OBJECT(widget);
696 const gchar *name = entry->extra.property_name;
698 switch (action)
700 case PREF_DISPLAY:
701 if (entry->setting_type == G_TYPE_BOOLEAN)
702 g_object_set(object, name, *(gboolean*)entry->setting, NULL);
703 else if (entry->setting_type == G_TYPE_INT)
704 g_object_set(object, name, *(gint*)entry->setting, NULL);
705 else if (entry->setting_type == G_TYPE_STRING)
706 g_object_set(object, name, *(gchararray*)entry->setting, NULL);
707 else if (entry->setting_type == G_TYPE_STRV)
708 g_object_set(object, name, *(gchararray**)entry->setting, NULL);
709 else
711 g_warning("Unhandled type %s for %s in %s()!", g_type_name(entry->setting_type),
712 entry->key_name, G_STRFUNC);
714 break;
715 case PREF_UPDATE:
716 if (entry->setting_type == G_TYPE_STRING)
717 g_free(*(gchararray*)entry->setting);
718 else if (entry->setting_type == G_TYPE_STRV)
719 g_strfreev(*(gchararray**)entry->setting);
721 g_object_get(object, name, entry->setting, NULL);
722 break;
727 static void pref_action(PrefAction action, StashGroup *group, GtkWidget *owner)
729 StashPref *entry;
730 guint i;
732 foreach_ptr_array(entry, i, group->entries)
734 GtkWidget *widget;
736 /* ignore settings with no widgets */
737 if (entry->widget_type == G_TYPE_NONE)
738 continue;
740 /* radio buttons have several widgets */
741 if (entry->widget_type == GTK_TYPE_RADIO_BUTTON)
743 handle_radio_buttons(owner, entry, action);
744 continue;
747 widget = get_widget(owner, entry->widget_id);
748 if (!widget)
750 g_warning("Unknown widget for %s::%s in %s()!", group->name, entry->key_name,
751 G_STRFUNC);
752 continue;
755 /* note: can't use switch for GTK_TYPE macros */
756 if (entry->widget_type == GTK_TYPE_TOGGLE_BUTTON)
757 handle_toggle_button(widget, entry->setting, action);
758 else if (entry->widget_type == GTK_TYPE_SPIN_BUTTON)
759 handle_spin_button(widget, entry, action);
760 else if (entry->widget_type == GTK_TYPE_COMBO_BOX)
761 handle_combo_box(widget, entry, action);
762 else if (entry->widget_type == TYPE_COMBO_BOX_ENTRY)
763 handle_combo_box_entry(widget, entry, action);
764 else if (entry->widget_type == GTK_TYPE_ENTRY)
765 handle_entry(widget, entry, action);
766 else if (entry->widget_type == G_TYPE_PARAM)
767 handle_widget_property(widget, entry, action);
768 else
769 g_warning("Unhandled type for %s::%s in %s()!", group->name, entry->key_name,
770 G_STRFUNC);
775 /** Applies Stash settings to widgets, usually called before displaying the widgets.
776 * The @a owner argument depends on which type you use for @ref StashWidgetID.
777 * @param group .
778 * @param owner If non-NULL, used to lookup widgets by name, otherwise
779 * widget pointers are assumed.
780 * @see stash_group_update(). */
781 GEANY_API_SYMBOL
782 void stash_group_display(StashGroup *group, GtkWidget *owner)
784 pref_action(PREF_DISPLAY, group, owner);
788 /** Applies widget values to Stash settings, usually called after displaying the widgets.
789 * The @a owner argument depends on which type you use for @ref StashWidgetID.
790 * @param group .
791 * @param owner If non-NULL, used to lookup widgets by name, otherwise
792 * widget pointers are assumed.
793 * @see stash_group_display(). */
794 GEANY_API_SYMBOL
795 void stash_group_update(StashGroup *group, GtkWidget *owner)
797 pref_action(PREF_UPDATE, group, owner);
801 static StashPref *
802 add_widget_pref(StashGroup *group, GType setting_type, gpointer setting,
803 const gchar *key_name, gpointer default_value,
804 GType widget_type, StashWidgetID widget_id)
806 StashPref *entry =
807 add_pref(group, setting_type, setting, key_name, default_value);
809 entry->widget_type = widget_type;
810 entry->widget_id = widget_id;
811 return entry;
815 /** Adds a @c GtkToggleButton (or @c GtkCheckButton) widget pref.
816 * @param group .
817 * @param setting Address of setting variable.
818 * @param key_name Name for key in a @c GKeyFile.
819 * @param default_value Value to use if the key doesn't exist when loading.
820 * @param widget_id @c GtkWidget pointer or string to lookup widget later.
821 * @see stash_group_add_radio_buttons(). */
822 GEANY_API_SYMBOL
823 void stash_group_add_toggle_button(StashGroup *group, gboolean *setting,
824 const gchar *key_name, gboolean default_value, StashWidgetID widget_id)
826 add_widget_pref(group, G_TYPE_BOOLEAN, setting, key_name, GINT_TO_POINTER(default_value),
827 GTK_TYPE_TOGGLE_BUTTON, widget_id);
831 /** Adds a @c GtkRadioButton widget group pref.
832 * @param group .
833 * @param setting Address of setting variable.
834 * @param key_name Name for key in a @c GKeyFile.
835 * @param default_value Value to use if the key doesn't exist when loading.
836 * @param widget_id @c GtkWidget pointer or string to lookup widget later.
837 * @param enum_id Enum value for @a widget_id.
838 * @param ... pairs of @a widget_id, @a enum_id.
839 * Example (using widget lookup strings, but widget pointers can also work):
840 * @code
841 * enum {FOO, BAR};
842 * stash_group_add_radio_buttons(group, &which_one_setting, "which_one", BAR,
843 * "radio_foo", FOO, "radio_bar", BAR, NULL);
844 * @endcode */
845 GEANY_API_SYMBOL
846 void stash_group_add_radio_buttons(StashGroup *group, gint *setting,
847 const gchar *key_name, gint default_value,
848 StashWidgetID widget_id, gint enum_id, ...)
850 StashPref *entry =
851 add_widget_pref(group, G_TYPE_INT, setting, key_name, GINT_TO_POINTER(default_value),
852 GTK_TYPE_RADIO_BUTTON, NULL);
853 va_list args;
854 gsize count = 1;
855 EnumWidget *item, *array;
857 /* count pairs of args */
858 va_start(args, enum_id);
859 while (1)
861 if (!va_arg(args, gpointer))
862 break;
863 va_arg(args, gint);
864 count++;
866 va_end(args);
868 array = g_new0(EnumWidget, count + 1);
869 entry->extra.radio_buttons = array;
871 va_start(args, enum_id);
872 foreach_c_array(item, array, count)
874 if (item == array)
876 /* first element */
877 item->widget_id = widget_id;
878 item->enum_id = enum_id;
880 else
882 item->widget_id = va_arg(args, gpointer);
883 item->enum_id = va_arg(args, gint);
886 va_end(args);
890 /** Adds a @c GtkSpinButton widget pref.
891 * @param group .
892 * @param setting Address of setting variable.
893 * @param key_name Name for key in a @c GKeyFile.
894 * @param default_value Value to use if the key doesn't exist when loading.
895 * @param widget_id @c GtkWidget pointer or string to lookup widget later. */
896 GEANY_API_SYMBOL
897 void stash_group_add_spin_button_integer(StashGroup *group, gint *setting,
898 const gchar *key_name, gint default_value, StashWidgetID widget_id)
900 add_widget_pref(group, G_TYPE_INT, setting, key_name, GINT_TO_POINTER(default_value),
901 GTK_TYPE_SPIN_BUTTON, widget_id);
905 /** Adds a @c GtkComboBox widget pref.
906 * @param group .
907 * @param setting Address of setting variable.
908 * @param key_name Name for key in a @c GKeyFile.
909 * @param default_value Value to use if the key doesn't exist when loading.
910 * @param widget_id @c GtkWidget pointer or string to lookup widget later.
911 * @see stash_group_add_combo_box_entry(). */
912 GEANY_API_SYMBOL
913 void stash_group_add_combo_box(StashGroup *group, gint *setting,
914 const gchar *key_name, gint default_value, StashWidgetID widget_id)
916 add_widget_pref(group, G_TYPE_INT, setting, key_name, GINT_TO_POINTER(default_value),
917 GTK_TYPE_COMBO_BOX, widget_id);
921 /** Adds a @c GtkComboBoxEntry widget pref.
922 * @param group .
923 * @param setting Address of setting variable.
924 * @param key_name Name for key in a @c GKeyFile.
925 * @param default_value Value to use if the key doesn't exist when loading.
926 * @param widget_id @c GtkWidget pointer or string to lookup widget later. */
927 /* We could maybe also have something like stash_group_add_combo_box_entry_with_menu()
928 * for the history list - or should that be stored as a separate setting? */
929 GEANY_API_SYMBOL
930 void stash_group_add_combo_box_entry(StashGroup *group, gchar **setting,
931 const gchar *key_name, const gchar *default_value, StashWidgetID widget_id)
933 add_widget_pref(group, G_TYPE_STRING, setting, key_name, (gpointer)default_value,
934 TYPE_COMBO_BOX_ENTRY, widget_id);
938 /** Adds a @c GtkEntry widget pref.
939 * @param group .
940 * @param setting Address of setting variable.
941 * @param key_name Name for key in a @c GKeyFile.
942 * @param default_value Value to use if the key doesn't exist when loading.
943 * @param widget_id @c GtkWidget pointer or string to lookup widget later. */
944 GEANY_API_SYMBOL
945 void stash_group_add_entry(StashGroup *group, gchar **setting,
946 const gchar *key_name, const gchar *default_value, StashWidgetID widget_id)
948 add_widget_pref(group, G_TYPE_STRING, setting, key_name, (gpointer)default_value,
949 GTK_TYPE_ENTRY, widget_id);
953 static GType object_get_property_type(GObject *object, const gchar *property_name)
955 GObjectClass *klass = G_OBJECT_GET_CLASS(object);
956 GParamSpec *ps;
958 ps = g_object_class_find_property(klass, property_name);
959 return ps->value_type;
963 /** Adds a widget's read/write property to the stash group.
964 * The property will be set when calling
965 * stash_group_display(), and read when calling stash_group_update().
966 * @param group .
967 * @param setting Address of e.g. an integer if using an integer property.
968 * @param key_name Name for key in a @c GKeyFile.
969 * @param default_value Value to use if the key doesn't exist when loading.
970 * Should be cast into a pointer e.g. with @c GINT_TO_POINTER().
971 * @param widget_id @c GtkWidget pointer or string to lookup widget later.
972 * @param property_name .
973 * @param type can be @c 0 if passing a @c GtkWidget as the @a widget_id argument to look it up from the
974 * @c GObject data.
975 * @warning Currently only string GValue properties will be freed before setting; patch for
976 * other types - see @c handle_widget_property(). */
977 GEANY_API_SYMBOL
978 void stash_group_add_widget_property(StashGroup *group, gpointer setting,
979 const gchar *key_name, gpointer default_value, StashWidgetID widget_id,
980 const gchar *property_name, GType type)
982 if (!type)
983 type = object_get_property_type(G_OBJECT(widget_id), property_name);
985 add_widget_pref(group, type, setting, key_name, default_value,
986 G_TYPE_PARAM, widget_id)->extra.property_name = property_name;
990 enum
992 STASH_TREE_NAME,
993 STASH_TREE_VALUE,
994 STASH_TREE_COUNT
998 struct StashTreeValue
1000 const gchar *group_name;
1001 StashPref *pref;
1002 struct
1004 gchararray tree_string;
1005 gint tree_int;
1006 } data;
1009 typedef struct StashTreeValue StashTreeValue;
1012 static void stash_tree_renderer_set_data(GtkCellLayout *cell_layout, GtkCellRenderer *cell,
1013 GtkTreeModel *model, GtkTreeIter *iter, gpointer user_data)
1015 GType cell_type = GPOINTER_TO_SIZE(user_data);
1016 StashTreeValue *value;
1017 StashPref *pref;
1018 gboolean matches_type;
1020 gtk_tree_model_get(model, iter, STASH_TREE_VALUE, &value, -1);
1021 pref = value->pref;
1022 matches_type = pref->setting_type == cell_type;
1023 g_object_set(cell, "visible", matches_type, "sensitive", matches_type,
1024 cell_type == G_TYPE_BOOLEAN ? "activatable" : "editable", matches_type, NULL);
1026 if (matches_type)
1028 switch (pref->setting_type)
1030 case G_TYPE_BOOLEAN:
1031 g_object_set(cell, "active", value->data.tree_int, NULL);
1032 break;
1033 case G_TYPE_INT:
1035 gchar *text = g_strdup_printf("%d", value->data.tree_int);
1036 g_object_set(cell, "text", text, NULL);
1037 g_free(text);
1038 break;
1040 case G_TYPE_STRING:
1041 g_object_set(cell, "text", value->data.tree_string, NULL);
1042 break;
1048 static void stash_tree_renderer_edited(gchar *path_str, gchar *new_text, GtkTreeModel *model)
1050 GtkTreePath *path;
1051 GtkTreeIter iter;
1052 StashTreeValue *value;
1053 StashPref *pref;
1055 path = gtk_tree_path_new_from_string(path_str);
1056 gtk_tree_model_get_iter(model, &iter, path);
1057 gtk_tree_model_get(model, &iter, STASH_TREE_VALUE, &value, -1);
1058 pref = value->pref;
1060 switch (pref->setting_type)
1062 case G_TYPE_BOOLEAN:
1063 value->data.tree_int = !value->data.tree_int;
1064 break;
1065 case G_TYPE_INT:
1066 value->data.tree_int = atoi(new_text);
1067 break;
1068 case G_TYPE_STRING:
1069 SETPTR(value->data.tree_string, g_strdup(new_text));
1070 break;
1073 gtk_tree_model_row_changed(model, path, &iter);
1074 gtk_tree_path_free(path);
1078 static void stash_tree_boolean_toggled(GtkCellRendererToggle *cell, gchar *path_str,
1079 GtkTreeModel *model)
1081 stash_tree_renderer_edited(path_str, NULL, model);
1085 static void stash_tree_string_edited(GtkCellRenderer *cell, gchar *path_str, gchar *new_text,
1086 GtkTreeModel *model)
1088 stash_tree_renderer_edited(path_str, new_text, model);
1092 static gboolean stash_tree_discard_value(GtkTreeModel *model, GtkTreePath *path,
1093 GtkTreeIter *iter, gpointer user_data)
1095 StashTreeValue *value;
1097 gtk_tree_model_get(model, iter, STASH_TREE_VALUE, &value, -1);
1098 /* don't access value->pref as it might already have been freed */
1099 g_free(value->data.tree_string);
1100 g_free(value);
1102 return FALSE;
1106 static void stash_tree_destroy_cb(GtkWidget *widget, gpointer user_data)
1108 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(widget));
1109 gtk_tree_model_foreach(model, stash_tree_discard_value, NULL);
1113 static void stash_tree_append_pref(StashGroup *group, StashPref *entry, GtkListStore *store,
1114 PrefAction action)
1116 GtkTreeIter iter;
1117 StashTreeValue *value;
1119 value = g_new0(StashTreeValue, 1);
1121 value->group_name = group->name;
1122 value->pref = entry;
1124 gtk_list_store_append(store, &iter);
1125 gtk_list_store_set(store, &iter, STASH_TREE_NAME, entry->key_name,
1126 STASH_TREE_VALUE, value, -1);
1130 static void stash_tree_append_prefs(GPtrArray *group_array,
1131 GtkListStore *store, PrefAction action)
1133 StashGroup *group;
1134 guint i, j;
1135 StashPref *entry;
1137 foreach_ptr_array(group, i, group_array)
1139 if (group->various)
1141 foreach_ptr_array(entry, j, group->entries)
1142 stash_tree_append_pref(group, entry, store, action);
1148 /* Setups a simple editor for stash preferences based on the widget arguments.
1149 * group_array - Array of groups which's settings will be edited.
1150 * tree - GtkTreeView in which to edit the preferences. Must be empty. */
1151 void stash_tree_setup(GPtrArray *group_array, GtkTreeView *tree)
1153 GtkListStore *store;
1154 GtkTreeModel *model;
1155 GtkCellRenderer *cell;
1156 GtkTreeViewColumn *column;
1157 GtkAdjustment *adjustment;
1159 store = gtk_list_store_new(STASH_TREE_COUNT, G_TYPE_STRING, G_TYPE_POINTER);
1160 stash_tree_append_prefs(group_array, store, PREF_DISPLAY);
1161 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store), STASH_TREE_NAME,
1162 GTK_SORT_ASCENDING);
1164 model = GTK_TREE_MODEL(store);
1165 gtk_tree_view_set_model(tree, model);
1166 g_object_unref(G_OBJECT(store));
1167 g_signal_connect(tree, "destroy", G_CALLBACK(stash_tree_destroy_cb), NULL);
1169 cell = gtk_cell_renderer_text_new();
1170 column = gtk_tree_view_column_new_with_attributes(_("Name"), cell, "text",
1171 STASH_TREE_NAME, NULL);
1172 gtk_tree_view_column_set_sort_column_id(column, STASH_TREE_NAME);
1173 gtk_tree_view_column_set_sort_indicator(column, TRUE);
1174 gtk_tree_view_append_column(tree, column);
1176 column = gtk_tree_view_column_new();
1177 gtk_tree_view_column_set_title(column, _("Value"));
1178 gtk_tree_view_append_column(tree, column);
1179 /* boolean renderer */
1180 cell = gtk_cell_renderer_toggle_new();
1181 g_signal_connect(cell, "toggled", G_CALLBACK(stash_tree_boolean_toggled), model);
1182 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(column), cell, FALSE);
1183 gtk_cell_layout_set_cell_data_func(GTK_CELL_LAYOUT(column), cell,
1184 stash_tree_renderer_set_data, GSIZE_TO_POINTER(G_TYPE_BOOLEAN), NULL);
1185 /* string renderer */
1186 cell = gtk_cell_renderer_text_new();
1187 g_object_set(cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
1188 g_signal_connect(cell, "edited", G_CALLBACK(stash_tree_string_edited), model);
1189 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(column), cell, TRUE);
1190 gtk_cell_layout_set_cell_data_func(GTK_CELL_LAYOUT(column), cell,
1191 stash_tree_renderer_set_data, GSIZE_TO_POINTER(G_TYPE_STRING), NULL);
1192 /* integer renderer */
1193 cell = gtk_cell_renderer_spin_new();
1194 adjustment = GTK_ADJUSTMENT(gtk_adjustment_new(0, G_MININT, G_MAXINT, 1, 10, 0));
1195 g_object_set(cell, "adjustment", adjustment, NULL);
1196 g_signal_connect(cell, "edited", G_CALLBACK(stash_tree_string_edited), model);
1197 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(column), cell, FALSE);
1198 gtk_cell_layout_set_cell_data_func(GTK_CELL_LAYOUT(column), cell,
1199 stash_tree_renderer_set_data, GSIZE_TO_POINTER(G_TYPE_INT), NULL);
1203 static void stash_tree_display_pref(StashTreeValue *value, StashPref *entry)
1205 switch (entry->setting_type)
1207 case G_TYPE_BOOLEAN:
1208 value->data.tree_int = *(gboolean *) entry->setting;
1209 break;
1210 case G_TYPE_INT:
1211 value->data.tree_int = *(gint *) entry->setting;
1212 break;
1213 case G_TYPE_STRING:
1214 SETPTR(value->data.tree_string, g_strdup(*(gchararray *) entry->setting));
1215 break;
1216 default:
1217 g_warning("Unhandled type for %s::%s in %s()!", value->group_name,
1218 entry->key_name, G_STRFUNC);
1223 static void stash_tree_update_pref(StashTreeValue *value, StashPref *entry)
1225 switch (entry->setting_type)
1227 case G_TYPE_BOOLEAN:
1228 *(gboolean *) entry->setting = value->data.tree_int;
1229 break;
1230 case G_TYPE_INT:
1231 *(gint *) entry->setting = value->data.tree_int;
1232 break;
1233 case G_TYPE_STRING:
1235 gchararray *text = entry->setting;
1236 SETPTR(*text, g_strdup(value->data.tree_string));
1237 break;
1239 default:
1240 g_warning("Unhandled type for %s::%s in %s()!", value->group_name,
1241 entry->key_name, G_STRFUNC);
1246 static void stash_tree_action(GtkTreeModel *model, PrefAction action)
1248 GtkTreeIter iter;
1249 gboolean valid = gtk_tree_model_get_iter_first(model, &iter);
1250 StashTreeValue *value;
1252 while (valid)
1254 gtk_tree_model_get(model, &iter, STASH_TREE_VALUE, &value, -1);
1256 switch (action)
1258 case PREF_DISPLAY:
1259 stash_tree_display_pref(value, value->pref);
1260 break;
1261 case PREF_UPDATE:
1262 stash_tree_update_pref(value, value->pref);
1263 break;
1265 valid = gtk_tree_model_iter_next(model, &iter);
1270 void stash_tree_display(GtkTreeView *tree)
1272 stash_tree_action(gtk_tree_view_get_model(tree), PREF_DISPLAY);
1276 void stash_tree_update(GtkTreeView *tree)
1278 stash_tree_action(gtk_tree_view_get_model(tree), PREF_UPDATE);