From 4d3959ac2fb1478143a47eb96e11485d675c0fa8 Mon Sep 17 00:00:00 2001 From: Nick Treleaven Date: Wed, 21 Sep 2011 15:50:36 +0000 Subject: [PATCH] Fix making various prefs tree sync with data values when showing the prefs dialog (patch by Dimitar Zhekov, thanks). git-svn-id: https://geany.svn.sourceforge.net/svnroot/geany/trunk@5942 ea778897-0a13-0410-b9d1-a72fbfd435f5 --- ChangeLog | 3 ++ src/prefs.c | 11 +++-- src/stash.c | 114 +++++++++++++++++++++++++++++++++------------------- src/stash.h | 2 + 4 files changed, 85 insertions(+), 45 deletions(-) diff --git a/ChangeLog b/ChangeLog index 1a96d0ae..e4a7dec9 100644 --- a/ChangeLog +++ b/ChangeLog @@ -4,6 +4,9 @@ Rename use_safe_file_saving various pref to use_atomic_file_saving as it's not 'safe'. Use old pref if new one is missing for compatibility with Geany <= 0.20. + * src/prefs.c, src/stash.h, src/stash.c: + Fix making various prefs tree sync with data values when showing + the prefs dialog (patch by Dimitar Zhekov, thanks). 2011-09-17 Frank Lanitz diff --git a/src/prefs.c b/src/prefs.c index 218ba6f0..95ac1212 100644 --- a/src/prefs.c +++ b/src/prefs.c @@ -109,6 +109,7 @@ static void prefs_action(PrefCallbackAction action) { StashGroup *group; guint i; + GtkWidget *widget = ui_lookup_widget(ui_widgets.prefs_dialog, "various_treeview"); foreach_ptr_array(group, i, pref_groups) { @@ -123,10 +124,14 @@ static void prefs_action(PrefCallbackAction action) } } - if (action == PREF_UPDATE) + switch (action) { - GtkWidget *widget = ui_lookup_widget(ui_widgets.prefs_dialog, "various_treeview"); - stash_tree_update(pref_groups, GTK_TREE_VIEW(widget)); + case PREF_DISPLAY: + stash_tree_display(pref_groups, GTK_TREE_VIEW(widget)); + break; + case PREF_UPDATE: + stash_tree_update(pref_groups, GTK_TREE_VIEW(widget)); + break; } } diff --git a/src/stash.c b/src/stash.c index 478b5c72..5f7015ca 100644 --- a/src/stash.c +++ b/src/stash.c @@ -1008,10 +1008,11 @@ static void stash_tree_destroy_cb(GtkWidget *widget, gpointer user_data) } -typedef void (*stash_foreach_pref_func)(StashGroup *group, StashPref *entry, gpointer user_data); +typedef void (*stash_foreach_pref_func)(StashGroup *group, StashPref *entry, gpointer container, + PrefAction action); static void stash_foreach_various_pref(GPtrArray *group_array, stash_foreach_pref_func pref_func, - gpointer user_data) + gpointer container, PrefAction action) { StashGroup *group; guint i; @@ -1022,39 +1023,22 @@ static void stash_foreach_various_pref(GPtrArray *group_array, stash_foreach_pre if (group->various) { foreach_array(StashPref, entry, group->entries) - pref_func(group, entry, user_data); + pref_func(group, entry, container, action); } } } -static void stash_tree_append_pref(StashGroup *group, StashPref *entry, GtkListStore *store) +static void stash_tree_append_pref(StashGroup *group, StashPref *entry, GtkListStore *store, + PrefAction action) { - gpointer setting; GtkTreeIter iter; StashTreeValue *value; - switch (entry->setting_type) - { - case G_TYPE_BOOLEAN: - setting = GINT_TO_POINTER(*(gboolean *) entry->setting); - break; - case G_TYPE_INT: - setting = GINT_TO_POINTER(*(gint *) entry->setting); - break; - case G_TYPE_STRING: - setting = g_strdup(*(gchararray *) entry->setting); - break; - default: - g_warning("Unhandled type for %s::%s in %s()!", group->name, - entry->key_name, G_STRFUNC); - return; - } - value = g_new(StashTreeValue, 1); value->setting_type = entry->setting_type; - value->setting = setting; + value->setting = NULL; value->key_name = entry->key_name; value->group_name = group->name; @@ -1077,7 +1061,7 @@ void stash_tree_setup(GPtrArray *group_array, GtkTreeView *tree) store = gtk_list_store_new(STASH_TREE_COUNT, G_TYPE_STRING, G_TYPE_POINTER); stash_foreach_various_pref(group_array, - (stash_foreach_pref_func) stash_tree_append_pref, store); + (stash_foreach_pref_func) stash_tree_append_pref, store, PREF_DISPLAY); gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store), STASH_TREE_NAME, GTK_SORT_ASCENDING); @@ -1120,14 +1104,62 @@ void stash_tree_setup(GPtrArray *group_array, GtkTreeView *tree) } +static void stash_tree_display_pref(StashTreeValue *value, StashPref *entry) +{ + switch (entry->setting_type) + { + case G_TYPE_BOOLEAN: + value->setting = GINT_TO_POINTER(*(gboolean *) entry->setting); + break; + case G_TYPE_INT: + value->setting = GINT_TO_POINTER(*(gint *) entry->setting); + break; + case G_TYPE_STRING: + { + g_free(value->setting); + value->setting = g_strdup(*(gchararray *) entry->setting); + break; + } + default: + g_warning("Unhandled type for %s::%s in %s()!", value->group_name, + entry->key_name, G_STRFUNC); + } +} + + +static void stash_tree_update_pref(StashTreeValue *value, StashPref *entry) +{ + gpointer *setting = value->setting; + + switch (entry->setting_type) + { + case G_TYPE_BOOLEAN: + *(gboolean *) entry->setting = GPOINTER_TO_INT(setting); + break; + case G_TYPE_INT: + *(gint *) entry->setting = GPOINTER_TO_INT(setting); + break; + case G_TYPE_STRING: + { + gchararray *text = entry->setting; + g_free(*text); + *text = g_strdup((gchararray) setting); + break; + } + default: + g_warning("Unhandled type for %s::%s in %s()!", value->group_name, + value->key_name, G_STRFUNC); + } +} + /* These functions can handle about 200 settings on a 1GHz x86 CPU in ~0.06 seconds. * For 250+ settings, you'd better write something more efficient. */ -static void stash_tree_update_pref(StashGroup *group, StashPref *entry, GtkTreeModel *model) +static void stash_tree_handle_pref(StashGroup *group, StashPref *entry, GtkTreeModel *model, + PrefAction action) { GtkTreeIter iter; gboolean valid = gtk_tree_model_get_iter_first(model, &iter); StashTreeValue *value; - gpointer *setting; while (valid) { @@ -1136,23 +1168,14 @@ static void stash_tree_update_pref(StashGroup *group, StashPref *entry, GtkTreeM if (strcmp(group->name, value->group_name) == 0 && strcmp(entry->key_name, value->key_name) == 0) { - setting = value->setting; - - switch (entry->setting_type) + switch (action) { - case G_TYPE_BOOLEAN: - *(gboolean *) entry->setting = GPOINTER_TO_INT(setting); + case PREF_DISPLAY: + stash_tree_display_pref(value, entry); break; - case G_TYPE_INT: - *(gint *) entry->setting = GPOINTER_TO_INT(setting); + case PREF_UPDATE: + stash_tree_update_pref(value, entry); break; - case G_TYPE_STRING: - { - gchararray *text = entry->setting; - g_free(*text); - *text = g_strdup((gchararray) setting); - break; - } } break; @@ -1163,8 +1186,15 @@ static void stash_tree_update_pref(StashGroup *group, StashPref *entry, GtkTreeM } +void stash_tree_display(GPtrArray *group_array, GtkTreeView *tree) +{ + stash_foreach_various_pref(group_array, (stash_foreach_pref_func) stash_tree_handle_pref, + gtk_tree_view_get_model(tree), PREF_DISPLAY); +} + + void stash_tree_update(GPtrArray *group_array, GtkTreeView *tree) { - stash_foreach_various_pref(group_array, (stash_foreach_pref_func) stash_tree_update_pref, - gtk_tree_view_get_model(tree)); + stash_foreach_various_pref(group_array, (stash_foreach_pref_func) stash_tree_handle_pref, + gtk_tree_view_get_model(tree), PREF_UPDATE); } diff --git a/src/stash.h b/src/stash.h index e4091569..0c96d8d6 100644 --- a/src/stash.h +++ b/src/stash.h @@ -94,6 +94,8 @@ void stash_group_update(StashGroup *group, GtkWidget *owner); void stash_tree_setup(GPtrArray *group_array, GtkTreeView *tree); +void stash_tree_display(GPtrArray *group_array, GtkTreeView *tree); + void stash_tree_update(GPtrArray *group_array, GtkTreeView *tree); #endif