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