Revision: 3375 http://geany.svn.sourceforge.net/geany/?rev=3375&view=rev Author: ntrel Date: 2008-12-15 13:02:29 +0000 (Mon, 15 Dec 2008)
Log Message: ----------- Use Stash radio button prefs instead of RadioPrefEntry. Add stash_group_add_radio_buttons().
Modified Paths: -------------- trunk/ChangeLog trunk/src/keyfile.c trunk/src/prefs.c trunk/src/stash.c trunk/src/stash.h
Modified: trunk/ChangeLog =================================================================== --- trunk/ChangeLog 2008-12-13 11:36:05 UTC (rev 3374) +++ trunk/ChangeLog 2008-12-15 13:02:29 UTC (rev 3375) @@ -1,3 +1,10 @@ +2008-12-15 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com> + + * src/prefs.c, src/stash.c, src/stash.h, src/keyfile.c: + Use Stash radio button prefs instead of RadioPrefEntry. + Add stash_group_add_radio_buttons(). + + 2008-12-11 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
* src/plugins.c, src/toolbar.c:
Modified: trunk/src/keyfile.c =================================================================== --- trunk/src/keyfile.c 2008-12-13 11:36:05 UTC (rev 3374) +++ trunk/src/keyfile.c 2008-12-15 13:02:29 UTC (rev 3375) @@ -134,8 +134,12 @@ "indent_hard_tab_width", 8); stash_group_add_integer(group, (gint*)&editor_prefs.indentation->auto_indent_mode, "indent_mode", GEANY_AUTOINDENT_CURRENTCHARS); - stash_group_add_integer(group, (gint*)&editor_prefs.indentation->type, - "indent_type", GEANY_INDENT_TYPE_TABS); + stash_group_add_radio_buttons(group, (gint*)&editor_prefs.indentation->type, + "indent_type", GEANY_INDENT_TYPE_TABS, + "radio_indent_spaces", GEANY_INDENT_TYPE_SPACES, + "radio_indent_tabs", GEANY_INDENT_TYPE_TABS, + "radio_indent_both", GEANY_INDENT_TYPE_BOTH, + NULL); stash_group_add_integer(group, (gint*)&editor_prefs.autocompletion_max_entries, "autocompletion_max_entries", GEANY_MAX_AUTOCOMPLETE_WORDS);
Modified: trunk/src/prefs.c =================================================================== --- trunk/src/prefs.c 2008-12-13 11:36:05 UTC (rev 3374) +++ trunk/src/prefs.c 2008-12-15 13:02:29 UTC (rev 3375) @@ -137,45 +137,6 @@ }
-typedef struct RadioPrefEntry -{ - const gchar *widget_name; - gpointer setting; - gint value; -} -RadioPrefEntry; - -static void radio_prefs_foreach(PrefCallbackAction action) -{ - guint i; - RadioPrefEntry items[] = - { - {"radio_indent_spaces", &editor_prefs.indentation->type, GEANY_INDENT_TYPE_SPACES}, - {"radio_indent_tabs", &editor_prefs.indentation->type, GEANY_INDENT_TYPE_TABS}, - {"radio_indent_both", &editor_prefs.indentation->type, GEANY_INDENT_TYPE_BOTH} - }; - - for (i = 0; i < G_N_ELEMENTS(items); i++) - { - RadioPrefEntry *pe = &items[i]; - GtkWidget *widget = lookup_widget(ui_widgets.prefs_dialog, pe->widget_name); - gint *setting = pe->setting; - - switch (action) - { - case PREF_DISPLAY: - if (*setting == pe->value) - gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), TRUE); - break; - case PREF_UPDATE: - if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget))) - *setting = pe->value; - break; - } - } -} - - static void combo_prefs_foreach(PrefCallbackAction action) { guint i; @@ -209,7 +170,6 @@ * runtime setting fields like EditorPrefs::indentation->width. */ PrefItemsCallback pref_item_callbacks[] = { spin_prefs_foreach, - radio_prefs_foreach, combo_prefs_foreach };
Modified: trunk/src/stash.c =================================================================== --- trunk/src/stash.c 2008-12-13 11:36:05 UTC (rev 3374) +++ trunk/src/stash.c 2008-12-15 13:02:29 UTC (rev 3375) @@ -47,6 +47,7 @@ gpointer default_value; GType widget_type; /* e.g. GTK_TYPE_TOGGLE_BUTTON */ gpointer widget_id; /* can be GtkWidget or gchararray */ + gpointer fields; /* extra fields */ };
struct GeanyPrefGroup @@ -56,7 +57,14 @@ gboolean write_once; /* only write settings if they don't already exist */ };
+typedef struct EnumWidget +{ + gpointer widget_id; + gint enum_id; +} +EnumWidget;
+ typedef enum SettingAction { SETTING_READ, @@ -177,6 +185,15 @@
void stash_group_free(GeanyPrefGroup *group) { + GeanyPrefEntry *entry; + + foreach_array(GeanyPrefEntry, entry, group->entries) + { + if (entry->widget_type == GTK_TYPE_RADIO_BUTTON) + g_free(entry->fields); + else + g_assert(entry->fields == NULL); + } g_array_free(group->entries, TRUE); g_free(group); } @@ -192,7 +209,7 @@ add_pref(GeanyPrefGroup *group, GType type, gpointer setting, const gchar *key_name, gpointer default_value) { - GeanyPrefEntry entry = {type, setting, key_name, default_value, G_TYPE_NONE, NULL}; + GeanyPrefEntry entry = {type, setting, key_name, default_value, G_TYPE_NONE, NULL, NULL}; GArray *array = group->entries;
g_array_append_val(array, entry); @@ -265,23 +282,88 @@ }
+static GtkWidget * +get_widget(GtkWidget *owner, gpointer widget_id) +{ + GtkWidget *widget = widget_id; + + if (owner) + { + const gchar *widget_name = widget_id; + + widget = lookup_widget(owner, widget_name); + } + if (!GTK_IS_WIDGET(widget)) + { + g_warning("Unknown widget in %s!", G_GNUC_FUNCTION); + return NULL; + } + return widget; +} + + +static void handle_radio_button(GtkWidget *widget, gint enum_id, gboolean *setting, + PrefAction action) +{ + switch (action) + { + case PREF_DISPLAY: + if (*setting == enum_id) + gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), TRUE); + break; + case PREF_UPDATE: + if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget))) + *setting = enum_id; + break; + } +} + + +static void handle_radio_buttons(GtkWidget *owner, EnumWidget *fields, + gboolean *setting, + PrefAction action) +{ + EnumWidget *field = fields; + gsize count = 0; + GtkWidget *widget = NULL; + + while (1) + { + widget = get_widget(owner, field->widget_id); + + if (!widget) + continue; + + count++; + handle_radio_button(widget, field->enum_id, setting, action); + field++; + if (!field->widget_id) + break; + } + if (g_slist_length(gtk_radio_button_get_group(GTK_RADIO_BUTTON(widget))) != count) + g_warning("Missing/invalid radio button widget IDs found!"); +} + + static void pref_action(PrefAction action, GeanyPrefGroup *group, GtkWidget *owner) { GeanyPrefEntry *entry;
foreach_array(GeanyPrefEntry, entry, group->entries) { - GtkWidget *widget = entry->widget_id; - const gchar *widget_name = entry->widget_id; + GtkWidget *widget;
if (entry->widget_type == G_TYPE_NONE) continue;
- if (owner) + if (entry->widget_type == GTK_TYPE_RADIO_BUTTON) { - widget = lookup_widget(owner, widget_name); + handle_radio_buttons(owner, entry->fields, entry->setting, action); + continue; } - if (!GTK_IS_WIDGET(widget)) + + widget = get_widget(owner, entry->widget_id); + if (!widget) { g_warning("Unknown widget for %s::%s in %s!", group->name, entry->key_name, G_GNUC_FUNCTION); @@ -310,6 +392,8 @@ }
+/* Used for GtkCheckButton or GtkToggleButton widgets. + * @see stash_group_add_radio_buttons(). */ void stash_group_add_toggle_button(GeanyPrefGroup *group, gboolean *setting, const gchar *key_name, gboolean default_value, gpointer widget_id) { @@ -321,3 +405,57 @@ }
+/* @param ... pairs of widget_id, enum_id. + * Example (using widget lookup strings, but widget pointers can also work): + * @code + * enum {FOO, BAR}; + * stash_group_add_radio_buttons(group, &which_one_setting, "which_one", BAR, + * "radio_foo", FOO, "radio_bar", BAR, NULL); + * @endcode */ +void stash_group_add_radio_buttons(GeanyPrefGroup *group, gint *setting, + const gchar *key_name, gint default_value, + gpointer widget_id, gint enum_id, ...) +{ + GeanyPrefEntry *entry = + add_pref(group, G_TYPE_INT, setting, key_name, GINT_TO_POINTER(default_value)); + va_list args; + gsize count = 1; + EnumWidget *item, *array; + + entry->widget_type = GTK_TYPE_RADIO_BUTTON; + + /* count pairs of args */ + va_start(args, enum_id); + while (1) + { + gint dummy; + + if (!va_arg(args, gpointer)) + break; + dummy = va_arg(args, gint); + count++; + } + va_end(args); + + array = g_new0(EnumWidget, count + 1); + entry->fields = array; + + va_start(args, enum_id); + foreach_c_array(item, array, count) + { + if (item == array) + { + /* first element */ + item->widget_id = widget_id; + item->enum_id = enum_id; + } + else + { + item->widget_id = va_arg(args, gpointer); + item->enum_id = va_arg(args, gint); + } + } + va_end(args); +} + +
Modified: trunk/src/stash.h =================================================================== --- trunk/src/stash.h 2008-12-13 11:36:05 UTC (rev 3374) +++ trunk/src/stash.h 2008-12-15 13:02:29 UTC (rev 3375) @@ -46,6 +46,10 @@ void stash_group_add_toggle_button(GeanyPrefGroup *group, gboolean *setting, const gchar *key_name, gboolean default_value, gpointer widget_id);
+void stash_group_add_radio_buttons(GeanyPrefGroup *group, gint *setting, + const gchar *key_name, gint default_value, + gpointer widget_id, gint enum_id, ...) G_GNUC_NULL_TERMINATED; + void stash_group_load(GeanyPrefGroup *group, GKeyFile *keyfile);
void stash_group_save(GeanyPrefGroup *group, GKeyFile *keyfile);
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.