SF.net SVN: geany:[2829] branches/custom-tab-width

ntrel at users.sourceforge.net ntrel at xxxxx
Mon Jul 28 13:42:15 UTC 2008


Revision: 2829
          http://geany.svn.sourceforge.net/geany/?rev=2829&view=rev
Author:   ntrel
Date:     2008-07-28 13:42:14 +0000 (Mon, 28 Jul 2008)

Log Message:
-----------
Move toggle_prefs to a function toggle_items_foreach(), which takes
a PREF_DISPLAY or PREF_UPDATE argument. This means the PrefEntry
array can contain runtime fields, so can read pointer contents.
Add pref_item_callbacks array of functions to call like
toggle_items_foreach().

Modified Paths:
--------------
    branches/custom-tab-width/ChangeLog
    branches/custom-tab-width/src/prefs.c

Modified: branches/custom-tab-width/ChangeLog
===================================================================
--- branches/custom-tab-width/ChangeLog	2008-07-28 13:38:12 UTC (rev 2828)
+++ branches/custom-tab-width/ChangeLog	2008-07-28 13:42:14 UTC (rev 2829)
@@ -5,6 +5,12 @@
    radio option, and a Tab Width spin entry.
    Replace GeanyIndentPrefs::use_tabs with GeanyIndentType field 'type'.
    (Still won't compile ;-)).
+ * src/prefs.c:
+   Move toggle_prefs to a function toggle_items_foreach(), which takes
+   a PREF_DISPLAY or PREF_UPDATE argument. This means the PrefEntry
+   array can contain runtime fields, so can read pointer contents.
+   Add pref_item_callbacks array of functions to call like
+   toggle_items_foreach().
 
 
 2008-07-25  Nick Treleaven  <nick(dot)treleaven(at)btinternet(dot)com>

Modified: branches/custom-tab-width/src/prefs.c
===================================================================
--- branches/custom-tab-width/src/prefs.c	2008-07-28 13:38:12 UTC (rev 2828)
+++ branches/custom-tab-width/src/prefs.c	2008-07-28 13:42:14 UTC (rev 2829)
@@ -89,7 +89,14 @@
 static void on_prefs_print_page_header_toggled(GtkToggleButton *togglebutton, gpointer user_data);
 
 
-/* used in e.g. init_toggle_button_prefs(). */
+typedef enum PrefCallbackAction
+{
+	PREF_DISPLAY,
+	PREF_UPDATE
+}
+PrefCallbackAction;
+
+
 typedef struct PrefEntry
 {
 	const gchar *widget_name;
@@ -97,14 +104,44 @@
 }
 PrefEntry;
 
-static PrefEntry toggle_prefs[] =
+
+static void toggle_items_foreach(PrefCallbackAction action)
 {
-	{"check_cmdline_new_files", &file_prefs.cmdline_new_files},
+	guint i;
+	PrefEntry items[] =
+	{
+		{"check_cmdline_new_files", &file_prefs.cmdline_new_files},
 
-	{"check_ask_suppress_search_dialogs", &search_prefs.suppress_dialogs},
-	{"check_search_use_current_word", &search_prefs.use_current_word},
-	{"check_fif_current_dir", &search_prefs.use_current_file_dir},
-	{NULL, NULL}	/* must be terminated */
+		{"check_ask_suppress_search_dialogs", &search_prefs.suppress_dialogs},
+		{"check_search_use_current_word", &search_prefs.use_current_word},
+		{"check_fif_current_dir", &search_prefs.use_current_file_dir},
+	};
+
+	for (i = 0; i < G_N_ELEMENTS(items); i++)
+	{
+		PrefEntry *pe = &items[i];
+		GtkWidget *widget = lookup_widget(ui_widgets.prefs_dialog, pe->widget_name);
+		gboolean *setting = pe->setting;
+
+		switch (action)
+		{
+			case PREF_DISPLAY:
+				gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), *setting);
+				break;
+			case PREF_UPDATE:
+				*setting = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
+				break;
+		}
+	}
+}
+
+
+typedef void (*PrefItemsCallback)(PrefCallbackAction action);
+
+/* List of functions which hold the PrefEntry arrays. This allows access to
+ * runtime setting fields like EditorPrefs::indentation->width. */
+PrefItemsCallback pref_item_callbacks[] = {
+	toggle_items_foreach
 };
 
 
@@ -180,16 +217,12 @@
 }
 
 
-static void init_toggle_button_prefs()
+static void init_prefs(void)
 {
-	PrefEntry *pe;
+	guint i;
 
-	for (pe = toggle_prefs; pe->widget_name != NULL; pe++)
-	{
-		GtkWidget *widget = lookup_widget(ui_widgets.prefs_dialog, pe->widget_name);
-
-		gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), *(gboolean*)pe->setting);
-	}
+	for (i = 0; i < G_N_ELEMENTS(pref_item_callbacks); i++)
+		pref_item_callbacks[i](PREF_DISPLAY);
 }
 
 
@@ -198,7 +231,7 @@
 	GtkWidget *widget;
 	GdkColor *color;
 
-	init_toggle_button_prefs();
+	init_prefs();
 
 	/* General settings */
 	/* startup */
@@ -614,16 +647,12 @@
 }
 
 
-static void update_toggle_button_prefs()
+static void update_prefs(void)
 {
-	PrefEntry *pe;
+	guint i;
 
-	for (pe = toggle_prefs; pe->widget_name != NULL; pe++)
-	{
-		GtkWidget *widget = lookup_widget(ui_widgets.prefs_dialog, pe->widget_name);
-
-		*(gboolean*)pe->setting = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
-	}
+	for (i = 0; i < G_N_ELEMENTS(pref_item_callbacks); i++)
+		pref_item_callbacks[i](PREF_UPDATE);
 }
 
 
@@ -638,7 +667,7 @@
 		GtkWidget *widget;
 		guint i;
 
-		update_toggle_button_prefs();
+		update_prefs();
 
 		/* General settings */
 		/* startup */


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.



More information about the Commits mailing list