Mark all plugin API functions to have "default" (public) visibility
[geany-mirror.git] / src / stash.c
bloba7628bd63eabdff85e3b196e13f10be9884e3647
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 "pluginexport.h" /* for GEANY_API_SYMBOL */
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 <stdlib.h> /* only for atoi() */
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 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 /** Creates a new group.
352 * @param name Name used for @c GKeyFile group.
353 * @return Group. */
354 GEANY_API_SYMBOL
355 StashGroup *stash_group_new(const gchar *name)
357 StashGroup *group = g_new0(StashGroup, 1);
359 group->name = name;
360 group->entries = g_ptr_array_new();
361 group->use_defaults = TRUE;
362 return group;
366 /** Frees the memory allocated for setting values in a group.
367 * Useful e.g. to avoid freeing strings individually.
368 * @note This is *not* called by stash_group_free().
369 * @param group . */
370 GEANY_API_SYMBOL
371 void stash_group_free_settings(StashGroup *group)
373 StashPref *entry;
374 guint i;
376 foreach_ptr_array(entry, i, group->entries)
378 if (entry->setting_type == G_TYPE_STRING)
379 g_free(*(gchararray *) entry->setting);
380 else if (entry->setting_type == G_TYPE_STRV)
381 g_strfreev(*(gchararray **) entry->setting);
382 else
383 continue;
385 *(gpointer**) entry->setting = NULL;
390 /** Frees a group.
391 * @param group . */
392 GEANY_API_SYMBOL
393 void stash_group_free(StashGroup *group)
395 StashPref *entry;
396 guint i;
398 foreach_ptr_array(entry, i, group->entries)
400 if (entry->widget_type == GTK_TYPE_RADIO_BUTTON)
402 g_free(entry->extra.radio_buttons);
404 g_slice_free(StashPref, entry);
406 g_ptr_array_free(group->entries, TRUE);
407 g_free(group);
411 /* Used for selecting groups passed to stash_tree_setup().
412 * @c FALSE by default. */
413 void stash_group_set_various(StashGroup *group, gboolean various)
415 group->various = various;
419 /* When @c FALSE, Stash doesn't change the setting if there is no keyfile entry, so it
420 * remains whatever it was initialized/set to by user code.
421 * @c TRUE by default. */
422 void stash_group_set_use_defaults(StashGroup *group, gboolean use_defaults)
424 group->use_defaults = use_defaults;
428 static StashPref *
429 add_pref(StashGroup *group, GType type, gpointer setting,
430 const gchar *key_name, gpointer default_value)
432 StashPref init = {type, setting, key_name, default_value, G_TYPE_NONE, NULL, {NULL}};
433 StashPref *entry = g_slice_new(StashPref);
435 *entry = init;
437 /* init any pointer settings to NULL so they can be freed later */
438 if (type == G_TYPE_STRING ||
439 type == G_TYPE_STRV)
440 if (group->use_defaults)
441 *(gpointer**)setting = NULL;
443 g_ptr_array_add(group->entries, entry);
444 return entry;
448 /** Adds boolean setting.
449 * @param group .
450 * @param setting Address of setting variable.
451 * @param key_name Name for key in a @c GKeyFile.
452 * @param default_value Value to use if the key doesn't exist when loading. */
453 GEANY_API_SYMBOL
454 void stash_group_add_boolean(StashGroup *group, gboolean *setting,
455 const gchar *key_name, gboolean default_value)
457 add_pref(group, G_TYPE_BOOLEAN, setting, key_name, GINT_TO_POINTER(default_value));
461 /** Adds integer setting.
462 * @param group .
463 * @param setting Address of setting variable.
464 * @param key_name Name for key in a @c GKeyFile.
465 * @param default_value Value to use if the key doesn't exist when loading. */
466 GEANY_API_SYMBOL
467 void stash_group_add_integer(StashGroup *group, gint *setting,
468 const gchar *key_name, gint default_value)
470 add_pref(group, G_TYPE_INT, setting, key_name, GINT_TO_POINTER(default_value));
474 /** Adds string setting.
475 * The contents of @a setting will be initialized to @c NULL.
476 * @param group .
477 * @param setting Address of setting variable.
478 * @param key_name Name for key in a @c GKeyFile.
479 * @param default_value String to copy if the key doesn't exist when loading, or @c NULL. */
480 GEANY_API_SYMBOL
481 void stash_group_add_string(StashGroup *group, gchar **setting,
482 const gchar *key_name, const gchar *default_value)
484 add_pref(group, G_TYPE_STRING, setting, key_name, (gpointer)default_value);
488 /** Adds string vector setting (array of strings).
489 * The contents of @a setting will be initialized to @c NULL.
490 * @param group .
491 * @param setting Address of setting variable.
492 * @param key_name Name for key in a @c GKeyFile.
493 * @param default_value Vector to copy if the key doesn't exist when loading. Usually @c NULL. */
494 GEANY_API_SYMBOL
495 void stash_group_add_string_vector(StashGroup *group, gchar ***setting,
496 const gchar *key_name, const gchar **default_value)
498 add_pref(group, G_TYPE_STRV, setting, key_name, (gpointer)default_value);
502 /* *** GTK-related functions *** */
504 static void handle_toggle_button(GtkWidget *widget, gboolean *setting,
505 PrefAction action)
507 switch (action)
509 case PREF_DISPLAY:
510 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), *setting);
511 break;
512 case PREF_UPDATE:
513 *setting = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
514 break;
519 static void handle_spin_button(GtkWidget *widget, StashPref *entry,
520 PrefAction action)
522 gint *setting = entry->setting;
524 g_assert(entry->setting_type == G_TYPE_INT); /* only int spin prefs */
526 switch (action)
528 case PREF_DISPLAY:
529 gtk_spin_button_set_value(GTK_SPIN_BUTTON(widget), *setting);
530 break;
531 case PREF_UPDATE:
532 /* if the widget is focussed, the value might not be updated */
533 gtk_spin_button_update(GTK_SPIN_BUTTON(widget));
534 *setting = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(widget));
535 break;
540 static void handle_combo_box(GtkWidget *widget, StashPref *entry,
541 PrefAction action)
543 gint *setting = entry->setting;
545 switch (action)
547 case PREF_DISPLAY:
548 gtk_combo_box_set_active(GTK_COMBO_BOX(widget), *setting);
549 break;
550 case PREF_UPDATE:
551 *setting = gtk_combo_box_get_active(GTK_COMBO_BOX(widget));
552 break;
557 static void handle_entry(GtkWidget *widget, StashPref *entry,
558 PrefAction action)
560 gchararray *setting = entry->setting;
562 switch (action)
564 case PREF_DISPLAY:
565 gtk_entry_set_text(GTK_ENTRY(widget), *setting);
566 break;
567 case PREF_UPDATE:
568 g_free(*setting);
569 *setting = g_strdup(gtk_entry_get_text(GTK_ENTRY(widget)));
570 break;
575 static void handle_combo_box_entry(GtkWidget *widget, StashPref *entry,
576 PrefAction action)
578 widget = gtk_bin_get_child(GTK_BIN(widget));
579 handle_entry(widget, entry, action);
583 /* taken from Glade 2.x generated support.c */
584 static GtkWidget*
585 lookup_widget(GtkWidget *widget, const gchar *widget_name)
587 GtkWidget *parent, *found_widget;
589 g_return_val_if_fail(widget != NULL, NULL);
590 g_return_val_if_fail(widget_name != NULL, NULL);
592 for (;;)
594 if (GTK_IS_MENU(widget))
595 parent = gtk_menu_get_attach_widget(GTK_MENU(widget));
596 else
597 parent = gtk_widget_get_parent(widget);
598 if (parent == NULL)
599 parent = (GtkWidget*) g_object_get_data(G_OBJECT(widget), "GladeParentKey");
600 if (parent == NULL)
601 break;
602 widget = parent;
605 found_widget = (GtkWidget*) g_object_get_data(G_OBJECT(widget), widget_name);
606 if (G_UNLIKELY(found_widget == NULL))
607 g_warning("Widget not found: %s", widget_name);
608 return found_widget;
612 static GtkWidget *
613 get_widget(GtkWidget *owner, StashWidgetID widget_id)
615 GtkWidget *widget;
617 if (owner)
618 widget = lookup_widget(owner, (const gchar *)widget_id);
619 else
620 widget = (GtkWidget *)widget_id;
622 if (!GTK_IS_WIDGET(widget))
624 g_warning("Unknown widget in %s()!", G_STRFUNC);
625 return NULL;
627 return widget;
631 static void handle_radio_button(GtkWidget *widget, gint enum_id, gboolean *setting,
632 PrefAction action)
634 switch (action)
636 case PREF_DISPLAY:
637 if (*setting == enum_id)
638 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), TRUE);
639 break;
640 case PREF_UPDATE:
641 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
642 *setting = enum_id;
643 break;
648 static void handle_radio_buttons(GtkWidget *owner, StashPref *entry,
649 PrefAction action)
651 EnumWidget *field = entry->extra.radio_buttons;
652 gsize count = 0;
653 GtkWidget *widget = NULL;
655 while (1)
657 widget = get_widget(owner, field->widget_id);
659 if (!widget)
660 continue;
662 count++;
663 handle_radio_button(widget, field->enum_id, entry->setting, action);
664 field++;
665 if (!field->widget_id)
666 break;
668 if (g_slist_length(gtk_radio_button_get_group(GTK_RADIO_BUTTON(widget))) != count)
669 g_warning("Missing/invalid radio button widget IDs found!");
673 static void handle_widget_property(GtkWidget *widget, StashPref *entry,
674 PrefAction action)
676 GObject *object = G_OBJECT(widget);
677 const gchar *name = entry->extra.property_name;
679 switch (action)
681 case PREF_DISPLAY:
682 g_object_set(object, name, entry->setting, NULL);
683 break;
684 case PREF_UPDATE:
685 if (entry->setting_type == G_TYPE_STRING)
686 g_free(entry->setting);
687 /* TODO: Which other types need freeing here? */
689 g_object_get(object, name, entry->setting, NULL);
690 break;
695 static void pref_action(PrefAction action, StashGroup *group, GtkWidget *owner)
697 StashPref *entry;
698 guint i;
700 foreach_ptr_array(entry, i, group->entries)
702 GtkWidget *widget;
704 /* ignore settings with no widgets */
705 if (entry->widget_type == G_TYPE_NONE)
706 continue;
708 /* radio buttons have several widgets */
709 if (entry->widget_type == GTK_TYPE_RADIO_BUTTON)
711 handle_radio_buttons(owner, entry, action);
712 continue;
715 widget = get_widget(owner, entry->widget_id);
716 if (!widget)
718 g_warning("Unknown widget for %s::%s in %s()!", group->name, entry->key_name,
719 G_STRFUNC);
720 continue;
723 /* note: can't use switch for GTK_TYPE macros */
724 if (entry->widget_type == GTK_TYPE_TOGGLE_BUTTON)
725 handle_toggle_button(widget, entry->setting, action);
726 else if (entry->widget_type == GTK_TYPE_SPIN_BUTTON)
727 handle_spin_button(widget, entry, action);
728 else if (entry->widget_type == GTK_TYPE_COMBO_BOX)
729 handle_combo_box(widget, entry, action);
730 else if (entry->widget_type == TYPE_COMBO_BOX_ENTRY)
731 handle_combo_box_entry(widget, entry, action);
732 else if (entry->widget_type == GTK_TYPE_ENTRY)
733 handle_entry(widget, entry, action);
734 else if (entry->widget_type == G_TYPE_PARAM)
735 handle_widget_property(widget, entry, action);
736 else
737 g_warning("Unhandled type for %s::%s in %s()!", group->name, entry->key_name,
738 G_STRFUNC);
743 /** Applies Stash settings to widgets, usually called before displaying the widgets.
744 * The @a owner argument depends on which type you use for @ref StashWidgetID.
745 * @param group .
746 * @param owner If non-NULL, used to lookup widgets by name, otherwise
747 * widget pointers are assumed.
748 * @see stash_group_update(). */
749 GEANY_API_SYMBOL
750 void stash_group_display(StashGroup *group, GtkWidget *owner)
752 pref_action(PREF_DISPLAY, group, owner);
756 /** Applies widget values to Stash settings, usually called after displaying the widgets.
757 * The @a owner argument depends on which type you use for @ref StashWidgetID.
758 * @param group .
759 * @param owner If non-NULL, used to lookup widgets by name, otherwise
760 * widget pointers are assumed.
761 * @see stash_group_display(). */
762 GEANY_API_SYMBOL
763 void stash_group_update(StashGroup *group, GtkWidget *owner)
765 pref_action(PREF_UPDATE, group, owner);
769 static StashPref *
770 add_widget_pref(StashGroup *group, GType setting_type, gpointer setting,
771 const gchar *key_name, gpointer default_value,
772 GType widget_type, StashWidgetID widget_id)
774 StashPref *entry =
775 add_pref(group, setting_type, setting, key_name, default_value);
777 entry->widget_type = widget_type;
778 entry->widget_id = widget_id;
779 return entry;
783 /** Adds a @c GtkToggleButton (or @c GtkCheckButton) widget pref.
784 * @param group .
785 * @param setting Address of setting variable.
786 * @param key_name Name for key in a @c GKeyFile.
787 * @param default_value Value to use if the key doesn't exist when loading.
788 * @param widget_id @c GtkWidget pointer or string to lookup widget later.
789 * @see stash_group_add_radio_buttons(). */
790 GEANY_API_SYMBOL
791 void stash_group_add_toggle_button(StashGroup *group, gboolean *setting,
792 const gchar *key_name, gboolean default_value, StashWidgetID widget_id)
794 add_widget_pref(group, G_TYPE_BOOLEAN, setting, key_name, GINT_TO_POINTER(default_value),
795 GTK_TYPE_TOGGLE_BUTTON, widget_id);
799 /** Adds a @c GtkRadioButton widget group pref.
800 * @param group .
801 * @param setting Address of setting variable.
802 * @param key_name Name for key in a @c GKeyFile.
803 * @param default_value Value to use if the key doesn't exist when loading.
804 * @param widget_id @c GtkWidget pointer or string to lookup widget later.
805 * @param enum_id Enum value for @a widget_id.
806 * @param ... pairs of @a widget_id, @a enum_id.
807 * Example (using widget lookup strings, but widget pointers can also work):
808 * @code
809 * enum {FOO, BAR};
810 * stash_group_add_radio_buttons(group, &which_one_setting, "which_one", BAR,
811 * "radio_foo", FOO, "radio_bar", BAR, NULL);
812 * @endcode */
813 GEANY_API_SYMBOL
814 void stash_group_add_radio_buttons(StashGroup *group, gint *setting,
815 const gchar *key_name, gint default_value,
816 StashWidgetID widget_id, gint enum_id, ...)
818 StashPref *entry =
819 add_widget_pref(group, G_TYPE_INT, setting, key_name, GINT_TO_POINTER(default_value),
820 GTK_TYPE_RADIO_BUTTON, NULL);
821 va_list args;
822 gsize count = 1;
823 EnumWidget *item, *array;
825 /* count pairs of args */
826 va_start(args, enum_id);
827 while (1)
829 if (!va_arg(args, gpointer))
830 break;
831 va_arg(args, gint);
832 count++;
834 va_end(args);
836 array = g_new0(EnumWidget, count + 1);
837 entry->extra.radio_buttons = array;
839 va_start(args, enum_id);
840 foreach_c_array(item, array, count)
842 if (item == array)
844 /* first element */
845 item->widget_id = widget_id;
846 item->enum_id = enum_id;
848 else
850 item->widget_id = va_arg(args, gpointer);
851 item->enum_id = va_arg(args, gint);
854 va_end(args);
858 /** Adds a @c GtkSpinButton widget pref.
859 * @param group .
860 * @param setting Address of setting variable.
861 * @param key_name Name for key in a @c GKeyFile.
862 * @param default_value Value to use if the key doesn't exist when loading.
863 * @param widget_id @c GtkWidget pointer or string to lookup widget later. */
864 GEANY_API_SYMBOL
865 void stash_group_add_spin_button_integer(StashGroup *group, gint *setting,
866 const gchar *key_name, gint default_value, StashWidgetID widget_id)
868 add_widget_pref(group, G_TYPE_INT, setting, key_name, GINT_TO_POINTER(default_value),
869 GTK_TYPE_SPIN_BUTTON, widget_id);
873 /** Adds a @c GtkComboBox widget 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 * @see stash_group_add_combo_box_entry(). */
880 GEANY_API_SYMBOL
881 void stash_group_add_combo_box(StashGroup *group, gint *setting,
882 const gchar *key_name, gint default_value, StashWidgetID widget_id)
884 add_widget_pref(group, G_TYPE_INT, setting, key_name, GINT_TO_POINTER(default_value),
885 GTK_TYPE_COMBO_BOX, widget_id);
889 /** Adds a @c GtkComboBoxEntry widget pref.
890 * @param group .
891 * @param setting Address of setting variable.
892 * @param key_name Name for key in a @c GKeyFile.
893 * @param default_value Value to use if the key doesn't exist when loading.
894 * @param widget_id @c GtkWidget pointer or string to lookup widget later. */
895 /* We could maybe also have something like stash_group_add_combo_box_entry_with_menu()
896 * for the history list - or should that be stored as a separate setting? */
897 GEANY_API_SYMBOL
898 void stash_group_add_combo_box_entry(StashGroup *group, gchar **setting,
899 const gchar *key_name, const gchar *default_value, StashWidgetID widget_id)
901 add_widget_pref(group, G_TYPE_STRING, setting, key_name, (gpointer)default_value,
902 TYPE_COMBO_BOX_ENTRY, widget_id);
906 /** Adds a @c GtkEntry widget pref.
907 * @param group .
908 * @param setting Address of setting variable.
909 * @param key_name Name for key in a @c GKeyFile.
910 * @param default_value Value to use if the key doesn't exist when loading.
911 * @param widget_id @c GtkWidget pointer or string to lookup widget later. */
912 GEANY_API_SYMBOL
913 void stash_group_add_entry(StashGroup *group, gchar **setting,
914 const gchar *key_name, const gchar *default_value, StashWidgetID widget_id)
916 add_widget_pref(group, G_TYPE_STRING, setting, key_name, (gpointer)default_value,
917 GTK_TYPE_ENTRY, widget_id);
921 static GType object_get_property_type(GObject *object, const gchar *property_name)
923 GObjectClass *klass = G_OBJECT_GET_CLASS(object);
924 GParamSpec *ps;
926 ps = g_object_class_find_property(klass, property_name);
927 return ps->value_type;
931 /** Adds a widget's read/write property to the stash group.
932 * The property will be set when calling
933 * stash_group_display(), and read when calling stash_group_update().
934 * @param group .
935 * @param setting Address of e.g. an integer if using an integer property.
936 * @param key_name Name for key in a @c GKeyFile.
937 * @param default_value Value to use if the key doesn't exist when loading.
938 * Should be cast into a pointer e.g. with @c GINT_TO_POINTER().
939 * @param widget_id @c GtkWidget pointer or string to lookup widget later.
940 * @param property_name .
941 * @param type can be @c 0 if passing a @c GtkWidget as the @a widget_id argument to look it up from the
942 * @c GObject data.
943 * @warning Currently only string GValue properties will be freed before setting; patch for
944 * other types - see @c handle_widget_property(). */
945 GEANY_API_SYMBOL
946 void stash_group_add_widget_property(StashGroup *group, gpointer setting,
947 const gchar *key_name, gpointer default_value, StashWidgetID widget_id,
948 const gchar *property_name, GType type)
950 if (!type)
951 type = object_get_property_type(G_OBJECT(widget_id), property_name);
953 add_widget_pref(group, type, setting, key_name, default_value,
954 G_TYPE_PARAM, widget_id)->extra.property_name = property_name;
958 enum
960 STASH_TREE_NAME,
961 STASH_TREE_VALUE,
962 STASH_TREE_COUNT
966 struct StashTreeValue
968 const gchar *group_name;
969 StashPref *pref;
970 union
972 gchararray tree_string;
973 gint tree_int;
974 } data;
977 typedef struct StashTreeValue StashTreeValue;
980 static void stash_tree_renderer_set_data(GtkCellLayout *cell_layout, GtkCellRenderer *cell,
981 GtkTreeModel *model, GtkTreeIter *iter, gpointer user_data)
983 GType cell_type = GPOINTER_TO_SIZE(user_data);
984 StashTreeValue *value;
985 StashPref *pref;
986 gboolean matches_type;
988 gtk_tree_model_get(model, iter, STASH_TREE_VALUE, &value, -1);
989 pref = value->pref;
990 matches_type = pref->setting_type == cell_type;
991 g_object_set(cell, "visible", matches_type, "sensitive", matches_type,
992 cell_type == G_TYPE_BOOLEAN ? "activatable" : "editable", matches_type, NULL);
994 if (matches_type)
996 switch (pref->setting_type)
998 case G_TYPE_BOOLEAN:
999 g_object_set(cell, "active", value->data.tree_int, NULL);
1000 break;
1001 case G_TYPE_INT:
1003 gchar *text = g_strdup_printf("%d", value->data.tree_int);
1004 g_object_set(cell, "text", text, NULL);
1005 g_free(text);
1006 break;
1008 case G_TYPE_STRING:
1009 g_object_set(cell, "text", value->data.tree_string, NULL);
1010 break;
1016 static void stash_tree_renderer_edited(gchar *path_str, gchar *new_text, GtkTreeModel *model)
1018 GtkTreePath *path;
1019 GtkTreeIter iter;
1020 StashTreeValue *value;
1021 StashPref *pref;
1023 path = gtk_tree_path_new_from_string(path_str);
1024 gtk_tree_model_get_iter(model, &iter, path);
1025 gtk_tree_model_get(model, &iter, STASH_TREE_VALUE, &value, -1);
1026 pref = value->pref;
1028 switch (pref->setting_type)
1030 case G_TYPE_BOOLEAN:
1031 value->data.tree_int = !value->data.tree_int;
1032 break;
1033 case G_TYPE_INT:
1034 value->data.tree_int = atoi(new_text);
1035 break;
1036 case G_TYPE_STRING:
1037 SETPTR(value->data.tree_string, g_strdup(new_text));
1038 break;
1041 gtk_tree_model_row_changed(model, path, &iter);
1042 gtk_tree_path_free(path);
1046 static void stash_tree_boolean_toggled(GtkCellRendererToggle *cell, gchar *path_str,
1047 GtkTreeModel *model)
1049 stash_tree_renderer_edited(path_str, NULL, model);
1053 static void stash_tree_string_edited(GtkCellRenderer *cell, gchar *path_str, gchar *new_text,
1054 GtkTreeModel *model)
1056 stash_tree_renderer_edited(path_str, new_text, model);
1060 static gboolean stash_tree_discard_value(GtkTreeModel *model, GtkTreePath *path,
1061 GtkTreeIter *iter, gpointer user_data)
1063 StashTreeValue *value;
1065 gtk_tree_model_get(model, iter, STASH_TREE_VALUE, &value, -1);
1066 if (value->pref->setting_type == G_TYPE_STRING)
1067 g_free(value->data.tree_string);
1068 g_free(value);
1070 return FALSE;
1074 static void stash_tree_destroy_cb(GtkWidget *widget, gpointer user_data)
1076 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(widget));
1077 gtk_tree_model_foreach(model, stash_tree_discard_value, NULL);
1081 static void stash_tree_append_pref(StashGroup *group, StashPref *entry, GtkListStore *store,
1082 PrefAction action)
1084 GtkTreeIter iter;
1085 StashTreeValue *value;
1087 value = g_new0(StashTreeValue, 1);
1089 value->group_name = group->name;
1090 value->pref = entry;
1092 gtk_list_store_append(store, &iter);
1093 gtk_list_store_set(store, &iter, STASH_TREE_NAME, entry->key_name,
1094 STASH_TREE_VALUE, value, -1);
1098 static void stash_tree_append_prefs(GPtrArray *group_array,
1099 GtkListStore *store, PrefAction action)
1101 StashGroup *group;
1102 guint i, j;
1103 StashPref *entry;
1105 foreach_ptr_array(group, i, group_array)
1107 if (group->various)
1109 foreach_ptr_array(entry, j, group->entries)
1110 stash_tree_append_pref(group, entry, store, action);
1116 /* Setups a simple editor for stash preferences based on the widget arguments.
1117 * group_array - Array of groups which's settings will be edited.
1118 * tree - GtkTreeView in which to edit the preferences. Must be empty. */
1119 void stash_tree_setup(GPtrArray *group_array, GtkTreeView *tree)
1121 GtkListStore *store;
1122 GtkTreeModel *model;
1123 GtkCellRenderer *cell;
1124 GtkTreeViewColumn *column;
1125 GtkAdjustment *adjustment;
1127 store = gtk_list_store_new(STASH_TREE_COUNT, G_TYPE_STRING, G_TYPE_POINTER);
1128 stash_tree_append_prefs(group_array, store, PREF_DISPLAY);
1129 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store), STASH_TREE_NAME,
1130 GTK_SORT_ASCENDING);
1132 model = GTK_TREE_MODEL(store);
1133 gtk_tree_view_set_model(tree, model);
1134 g_object_unref(G_OBJECT(store));
1135 g_signal_connect(tree, "destroy", G_CALLBACK(stash_tree_destroy_cb), NULL);
1137 cell = gtk_cell_renderer_text_new();
1138 column = gtk_tree_view_column_new_with_attributes(_("Name"), cell, "text",
1139 STASH_TREE_NAME, NULL);
1140 gtk_tree_view_column_set_sort_column_id(column, STASH_TREE_NAME);
1141 gtk_tree_view_column_set_sort_indicator(column, TRUE);
1142 gtk_tree_view_append_column(tree, column);
1144 column = gtk_tree_view_column_new();
1145 gtk_tree_view_column_set_title(column, _("Value"));
1146 gtk_tree_view_append_column(tree, column);
1147 /* boolean renderer */
1148 cell = gtk_cell_renderer_toggle_new();
1149 g_signal_connect(cell, "toggled", G_CALLBACK(stash_tree_boolean_toggled), model);
1150 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(column), cell, FALSE);
1151 gtk_cell_layout_set_cell_data_func(GTK_CELL_LAYOUT(column), cell,
1152 stash_tree_renderer_set_data, GSIZE_TO_POINTER(G_TYPE_BOOLEAN), NULL);
1153 /* string renderer */
1154 cell = gtk_cell_renderer_text_new();
1155 g_object_set(cell, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
1156 g_signal_connect(cell, "edited", G_CALLBACK(stash_tree_string_edited), model);
1157 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(column), cell, TRUE);
1158 gtk_cell_layout_set_cell_data_func(GTK_CELL_LAYOUT(column), cell,
1159 stash_tree_renderer_set_data, GSIZE_TO_POINTER(G_TYPE_STRING), NULL);
1160 /* integer renderer */
1161 cell = gtk_cell_renderer_spin_new();
1162 adjustment = GTK_ADJUSTMENT(gtk_adjustment_new(0, G_MININT, G_MAXINT, 1, 10, 0));
1163 g_object_set(cell, "adjustment", adjustment, NULL);
1164 g_signal_connect(cell, "edited", G_CALLBACK(stash_tree_string_edited), model);
1165 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(column), cell, FALSE);
1166 gtk_cell_layout_set_cell_data_func(GTK_CELL_LAYOUT(column), cell,
1167 stash_tree_renderer_set_data, GSIZE_TO_POINTER(G_TYPE_INT), NULL);
1171 static void stash_tree_display_pref(StashTreeValue *value, StashPref *entry)
1173 switch (entry->setting_type)
1175 case G_TYPE_BOOLEAN:
1176 value->data.tree_int = *(gboolean *) entry->setting;
1177 break;
1178 case G_TYPE_INT:
1179 value->data.tree_int = *(gint *) entry->setting;
1180 break;
1181 case G_TYPE_STRING:
1182 SETPTR(value->data.tree_string, g_strdup(*(gchararray *) entry->setting));
1183 break;
1184 default:
1185 g_warning("Unhandled type for %s::%s in %s()!", value->group_name,
1186 entry->key_name, G_STRFUNC);
1191 static void stash_tree_update_pref(StashTreeValue *value, StashPref *entry)
1193 switch (entry->setting_type)
1195 case G_TYPE_BOOLEAN:
1196 *(gboolean *) entry->setting = value->data.tree_int;
1197 break;
1198 case G_TYPE_INT:
1199 *(gint *) entry->setting = value->data.tree_int;
1200 break;
1201 case G_TYPE_STRING:
1203 gchararray *text = entry->setting;
1204 SETPTR(*text, g_strdup(value->data.tree_string));
1205 break;
1207 default:
1208 g_warning("Unhandled type for %s::%s in %s()!", value->group_name,
1209 entry->key_name, G_STRFUNC);
1214 static void stash_tree_action(GtkTreeModel *model, PrefAction action)
1216 GtkTreeIter iter;
1217 gboolean valid = gtk_tree_model_get_iter_first(model, &iter);
1218 StashTreeValue *value;
1220 while (valid)
1222 gtk_tree_model_get(model, &iter, STASH_TREE_VALUE, &value, -1);
1224 switch (action)
1226 case PREF_DISPLAY:
1227 stash_tree_display_pref(value, value->pref);
1228 break;
1229 case PREF_UPDATE:
1230 stash_tree_update_pref(value, value->pref);
1231 break;
1233 valid = gtk_tree_model_iter_next(model, &iter);
1238 void stash_tree_display(GtkTreeView *tree)
1240 stash_tree_action(gtk_tree_view_get_model(tree), PREF_DISPLAY);
1244 void stash_tree_update(GtkTreeView *tree)
1246 stash_tree_action(gtk_tree_view_get_model(tree), PREF_UPDATE);