Kaydet (Commit) f4fd1ea2 authored tarafından Nick Treleaven's avatar Nick Treleaven

Use Stash radio button prefs instead of RadioPrefEntry.

Add stash_group_add_radio_buttons().



git-svn-id: https://geany.svn.sourceforge.net/svnroot/geany/trunk@3375 ea778897-0a13-0410-b9d1-a72fbfd435f5
üst aaf3e6aa
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:
......
......@@ -134,8 +134,12 @@ static void init_pref_groups(void)
"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);
......
......@@ -137,45 +137,6 @@ static void spin_prefs_foreach(PrefCallbackAction action)
}
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 @@ typedef void (*PrefItemsCallback)(PrefCallbackAction action);
* runtime setting fields like EditorPrefs::indentation->width. */
PrefItemsCallback pref_item_callbacks[] = {
spin_prefs_foreach,
radio_prefs_foreach,
combo_prefs_foreach
};
......
......@@ -47,6 +47,7 @@ struct GeanyPrefEntry
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,6 +57,13 @@ struct GeanyPrefGroup
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
{
......@@ -177,6 +185,15 @@ GeanyPrefGroup *stash_group_new(const gchar *name)
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 @@ static GeanyPrefEntry *
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 @@ lookup_widget (GtkWidget *widget,
}
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 @@ void stash_group_update(GeanyPrefGroup *group, GtkWidget *owner)
}
/* 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 @@ void stash_group_add_toggle_button(GeanyPrefGroup *group, gboolean *setting,
}
/* @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);
}
......@@ -46,6 +46,10 @@ void stash_group_add_string(GeanyPrefGroup *group, gchar **setting,
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);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment