SF.net SVN: geany:[3285] trunk

ntrel at users.sourceforge.net ntrel at xxxxx
Fri Nov 28 12:52:19 UTC 2008


Revision: 3285
          http://geany.svn.sourceforge.net/geany/?rev=3285&view=rev
Author:   ntrel
Date:     2008-11-28 12:52:19 +0000 (Fri, 28 Nov 2008)

Log Message:
-----------
Add foreach_c_array() macro in utils.h.
Add stash.[hc] for reading/writing GKeyFile settings and (later)
synchronizing widgets with C variables. Currently this only
supports boolean and integer settings.
Replace keyfile.c SettingEntry code with new stash code.

Modified Paths:
--------------
    trunk/ChangeLog
    trunk/src/Makefile.am
    trunk/src/keyfile.c
    trunk/src/makefile.win32
    trunk/src/utils.h
    trunk/wscript

Added Paths:
-----------
    trunk/src/stash.c
    trunk/src/stash.h

Modified: trunk/ChangeLog
===================================================================
--- trunk/ChangeLog	2008-11-28 12:39:29 UTC (rev 3284)
+++ trunk/ChangeLog	2008-11-28 12:52:19 UTC (rev 3285)
@@ -1,3 +1,14 @@
+2008-11-28  Nick Treleaven  <nick(dot)treleaven(at)btinternet(dot)com>
+
+ * src/utils.h, src/makefile.win32, src/stash.c, src/stash.h,
+   src/keyfile.c, src/Makefile.am, wscript:
+   Add foreach_c_array() macro in utils.h.
+   Add stash.[hc] for reading/writing GKeyFile settings and (later)
+   synchronizing widgets with C variables. Currently this only
+   supports boolean and integer settings.
+   Replace keyfile.c SettingEntry code with new stash code.
+
+
 2008-11-26  Nick Treleaven  <nick(dot)treleaven(at)btinternet(dot)com>
 
  * src/utils.c, src/ui_utils.h, src/utils.h, src/geany.h,

Modified: trunk/src/Makefile.am
===================================================================
--- trunk/src/Makefile.am	2008-11-28 12:39:29 UTC (rev 3284)
+++ trunk/src/Makefile.am	2008-11-28 12:52:19 UTC (rev 3285)
@@ -35,6 +35,7 @@
 	sciwrappers.c sciwrappers.h \
 	search.c search.h \
 	socket.c socket.h \
+	stash.c stash.h \
 	support.c support.h \
 	symbols.c symbols.h \
 	templates.c templates.h \

Modified: trunk/src/keyfile.c
===================================================================
--- trunk/src/keyfile.c	2008-11-28 12:39:29 UTC (rev 3284)
+++ trunk/src/keyfile.c	2008-11-28 12:52:19 UTC (rev 3285)
@@ -59,6 +59,7 @@
 #include "printing.h"
 #include "plugins.h"
 #include "templates.h"
+#include "stash.h"
 
 
 /* some default settings which are used at the very first start of Geany to fill
@@ -91,110 +92,67 @@
 static gint vpan_position;
 
 
-typedef enum SettingCallbackAction
+/* probably this enum will be removed */
+typedef enum SettingAction
 {
 	SETTING_READ,
 	SETTING_WRITE
 }
-SettingCallbackAction;
+SettingAction;
 
-
-typedef struct SettingEntry
+static void settings_action(GKeyFile *config, SettingAction action)
 {
-	gpointer setting;
-	const gchar *group;
-	const gchar *key_name;
-	gpointer default_value;
-}
-SettingEntry;
-
-
-static void bool_settings_foreach(GKeyFile *config, SettingCallbackAction action)
-{
-	guint i;
-	SettingEntry items[] =
+	GeanyPrefEntry package_items[] =
 	{
-		{&file_prefs.cmdline_new_files, PACKAGE, "cmdline_new_files", (gpointer)TRUE},
+		{G_TYPE_BOOLEAN, &file_prefs.cmdline_new_files, "cmdline_new_files",
+			(gpointer)TRUE, NULL},
 
-		{&search_prefs.suppress_dialogs, PACKAGE, "pref_main_suppress_search_dialogs", (gpointer)FALSE},
-		{&search_prefs.use_current_word, PACKAGE, "pref_main_search_use_current_word", (gpointer)TRUE},
-		{&search_prefs.use_current_file_dir, "search", "pref_search_current_file_dir", (gpointer)TRUE},
+		{G_TYPE_BOOLEAN, &search_prefs.suppress_dialogs, "pref_main_suppress_search_dialogs",
+			(gpointer)FALSE, NULL},
+		{G_TYPE_BOOLEAN, &search_prefs.use_current_word, "pref_main_search_use_current_word",
+			(gpointer)TRUE, NULL},
 
-		{&editor_prefs.indentation->detect_type, PACKAGE, "check_detect_indent", (gpointer)FALSE},
-		{&editor_prefs.use_tab_to_indent, PACKAGE, "use_tab_to_indent", (gpointer)TRUE}
-	};
+		{G_TYPE_BOOLEAN, &editor_prefs.indentation->detect_type, "check_detect_indent",
+			(gpointer)FALSE, NULL},
+		{G_TYPE_BOOLEAN, &editor_prefs.use_tab_to_indent, "use_tab_to_indent",
+			(gpointer)TRUE, NULL},
 
-	for (i = 0; i < G_N_ELEMENTS(items); i++)
+		{G_TYPE_INT, &editor_prefs.indentation->width, "pref_editor_tab_width",
+			(gpointer)4, NULL},
+		{G_TYPE_INT, &editor_prefs.indentation->hard_tab_width, "indent_hard_tab_width",
+			(gpointer)8, NULL},
+		{G_TYPE_INT, &editor_prefs.indentation->auto_indent_mode, "indent_mode",
+			(gpointer)GEANY_AUTOINDENT_CURRENTCHARS, NULL},
+		{G_TYPE_INT, &editor_prefs.indentation->type, "indent_type",
+			(gpointer)GEANY_INDENT_TYPE_TABS, NULL},
+		{G_TYPE_INT, &editor_prefs.autocompletion_max_entries, "autocompletion_max_entries",
+			(gpointer)GEANY_MAX_AUTOCOMPLETE_WORDS, NULL}
+	};
+	GeanyPrefEntry search_items[] =
 	{
-		SettingEntry *se = &items[i];
-		gboolean *setting = se->setting;
-
-		switch (action)
-		{
-			case SETTING_READ:
-				*setting = utils_get_setting_boolean(config, se->group, se->key_name,
-					GPOINTER_TO_INT(se->default_value));
-				break;
-			case SETTING_WRITE:
-				g_key_file_set_boolean(config, se->group, se->key_name, *setting);
-				break;
-		}
-	}
-}
-
-
-static void int_settings_foreach(GKeyFile *config, SettingCallbackAction action)
-{
-	guint i;
-	SettingEntry items[] =
+		{G_TYPE_BOOLEAN, &search_prefs.use_current_file_dir, "pref_search_current_file_dir",
+			(gpointer)TRUE, NULL}
+	};
+	GeanyPrefGroup groups[] =
 	{
-		{&editor_prefs.indentation->width, PACKAGE, "pref_editor_tab_width", (gpointer)4},
-		{&editor_prefs.indentation->hard_tab_width, PACKAGE, "indent_hard_tab_width", (gpointer)8},
-		{&editor_prefs.indentation->auto_indent_mode, PACKAGE, "indent_mode",
-			(gpointer)GEANY_AUTOINDENT_CURRENTCHARS},
-		{&editor_prefs.indentation->type, PACKAGE, "indent_type", (gpointer)GEANY_INDENT_TYPE_TABS},
-		{&editor_prefs.autocompletion_max_entries, PACKAGE, "autocompletion_max_entries",
-			(gpointer)GEANY_MAX_AUTOCOMPLETE_WORDS}
+		{PACKAGE, package_items, G_N_ELEMENTS(package_items), FALSE},
+		{"search", search_items, G_N_ELEMENTS(search_items), FALSE}
 	};
+	GeanyPrefGroup *group;
 
-	for (i = 0; i < G_N_ELEMENTS(items); i++)
+	foreach_c_array(group, groups, G_N_ELEMENTS(groups))
 	{
-		SettingEntry *se = &items[i];
-		gboolean *setting = se->setting;
-
 		switch (action)
 		{
 			case SETTING_READ:
-				*setting = utils_get_setting_integer(config, se->group, se->key_name,
-					GPOINTER_TO_INT(se->default_value));
-				break;
+				stash_load_group(group, config); break;
 			case SETTING_WRITE:
-				g_key_file_set_integer(config, se->group, se->key_name, *setting);
-				break;
+				stash_save_group(group, config); break;
 		}
 	}
 }
 
 
-typedef void (*SettingItemsCallback)(GKeyFile *config, SettingCallbackAction action);
-
-/* List of functions which hold the SettingEntry arrays. These allow access to
- * runtime setting fields like EditorPrefs::indentation->width. */
-SettingItemsCallback setting_item_callbacks[] = {
-	bool_settings_foreach,
-	int_settings_foreach
-};
-
-
-static void settings_action(GKeyFile *config, SettingCallbackAction action)
-{
-	guint i;
-
-	for (i = 0; i < G_N_ELEMENTS(setting_item_callbacks); i++)
-		setting_item_callbacks[i](config, action);
-}
-
-
 static void save_recent_files(GKeyFile *config)
 {
 	gchar **recent_files = g_new0(gchar*, file_prefs.mru_length + 1);

Modified: trunk/src/makefile.win32
===================================================================
--- trunk/src/makefile.win32	2008-11-28 12:39:29 UTC (rev 3284)
+++ trunk/src/makefile.win32	2008-11-28 12:52:19 UTC (rev 3285)
@@ -63,7 +63,8 @@
 OBJS =	about.o build.o callbacks.o dialogs.o document.o editor.o encodings.o filetypes.o \
 		geanyobject.o geanywraplabel.o highlighting.o interface.o keybindings.o keyfile.o \
 		log.o main.o msgwindow.o navqueue.o notebook.o plugins.o prefs.o printing.o project.o \
-		sciwrappers.o search.o socket.o support.o symbols.o templates.o treeviews.o tools.o \
+		sciwrappers.o search.o socket.o stash.o \
+		support.o symbols.o templates.o treeviews.o tools.o \
 		ui_utils.o utils.o win32.o
 
 .c.o:

Added: trunk/src/stash.c
===================================================================
--- trunk/src/stash.c	                        (rev 0)
+++ trunk/src/stash.c	2008-11-28 12:52:19 UTC (rev 3285)
@@ -0,0 +1,114 @@
+/*
+ *      stash.c - this file is part of Geany, a fast and lightweight IDE
+ *
+ *      Copyright 2008 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
+ *      Copyright 2008 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
+ *
+ *      This program is free software; you can redistribute it and/or modify
+ *      it under the terms of the GNU General Public License as published by
+ *      the Free Software Foundation; either version 2 of the License, or
+ *      (at your option) any later version.
+ *
+ *      This program is distributed in the hope that it will be useful,
+ *      but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *      GNU General Public License for more details.
+ *
+ *      You should have received a copy of the GNU General Public License
+ *      along with this program; if not, write to the Free Software
+ *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ *      MA 02110-1301, USA.
+ *
+ * $Id$
+ */
+
+/* Mini-library for reading/writing GKeyFile settings and synchronizing widgets with
+ * C variables. */
+
+
+#include <gtk/gtk.h>
+
+#include "stash.h"
+#include "utils.h"
+
+
+typedef enum SettingAction
+{
+	SETTING_READ,
+	SETTING_WRITE
+}
+SettingAction;
+
+
+
+static void handle_bool_setting(GeanyPrefGroup *group, GeanyPrefEntry *se,
+		GKeyFile *config, SettingAction action)
+{
+	gboolean *setting = se->setting;
+
+	switch (action)
+	{
+		case SETTING_READ:
+			*setting = utils_get_setting_boolean(config, group->name, se->key_name,
+				GPOINTER_TO_INT(se->default_value));
+			break;
+		case SETTING_WRITE:
+			g_key_file_set_boolean(config, group->name, se->key_name, *setting);
+			break;
+	}
+}
+
+
+static void handle_int_setting(GeanyPrefGroup *group, GeanyPrefEntry *se,
+		GKeyFile *config, SettingAction action)
+{
+	gboolean *setting = se->setting;
+
+	switch (action)
+	{
+		case SETTING_READ:
+			*setting = utils_get_setting_integer(config, group->name, se->key_name,
+				GPOINTER_TO_INT(se->default_value));
+			break;
+		case SETTING_WRITE:
+			g_key_file_set_integer(config, group->name, se->key_name, *setting);
+			break;
+	}
+}
+
+
+static void keyfile_action(GeanyPrefGroup *group, GKeyFile *keyfile, SettingAction action)
+{
+	GeanyPrefEntry *entry;
+
+	foreach_c_array(entry, group->entries, group->entries_len)
+	{
+		if (group->write_once && action == SETTING_WRITE &&
+			g_key_file_has_key(keyfile, group->name, entry->key_name, NULL))
+			continue; /* don't overwrite write_once prefs */
+
+		switch (entry->type)
+		{
+			case G_TYPE_BOOLEAN:
+				handle_bool_setting(group, entry, keyfile, action); break;
+			case G_TYPE_INT:
+				handle_int_setting(group, entry, keyfile, action); break;
+			default:
+				g_warning("Unhandled type for %s::%s!", group->name, entry->key_name);
+		}
+	}
+}
+
+
+void stash_load_group(GeanyPrefGroup *group, GKeyFile *keyfile)
+{
+	keyfile_action(group, keyfile, SETTING_READ);
+}
+
+
+void stash_save_group(GeanyPrefGroup *group, GKeyFile *keyfile)
+{
+	keyfile_action(group, keyfile, SETTING_WRITE);
+}
+
+


Property changes on: trunk/src/stash.c
___________________________________________________________________
Added: svn:keywords
   + Author Date Id Revision
Added: svn:eol-style
   + native

Added: trunk/src/stash.h
===================================================================
--- trunk/src/stash.h	                        (rev 0)
+++ trunk/src/stash.h	2008-11-28 12:52:19 UTC (rev 3285)
@@ -0,0 +1,50 @@
+/*
+ *      stash.h - this file is part of Geany, a fast and lightweight IDE
+ *
+ *      Copyright 2008 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
+ *
+ *      This program is free software; you can redistribute it and/or modify
+ *      it under the terms of the GNU General Public License as published by
+ *      the Free Software Foundation; either version 2 of the License, or
+ *      (at your option) any later version.
+ *
+ *      This program is distributed in the hope that it will be useful,
+ *      but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *      GNU General Public License for more details.
+ *
+ *      You should have received a copy of the GNU General Public License
+ *      along with this program; if not, write to the Free Software
+ *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ *      MA 02110-1301, USA.
+ *
+ * $Id$
+ */
+
+#ifndef GEANY_STASH_H
+#define GEANY_STASH_H
+
+typedef struct GeanyPrefEntry
+{
+	GType type;					/* e.g. G_TYPE_INT */
+	gpointer setting;
+	const gchar *key_name;
+	gpointer default_value;
+	const gchar *widget_name;
+}
+GeanyPrefEntry;
+
+typedef struct GeanyPrefGroup
+{
+	const gchar *name;			/* group name to use in the keyfile */
+	GeanyPrefEntry *entries;
+	gsize entries_len;
+	gboolean write_once;		/* only write settings if they don't already exist */
+}
+GeanyPrefGroup;
+
+
+void stash_load_group(GeanyPrefGroup *group, GKeyFile *keyfile);
+void stash_save_group(GeanyPrefGroup *group, GKeyFile *keyfile);
+
+#endif


Property changes on: trunk/src/stash.h
___________________________________________________________________
Added: svn:keywords
   + Author Date Id Revision
Added: svn:eol-style
   + native

Modified: trunk/src/utils.h
===================================================================
--- trunk/src/utils.h	2008-11-28 12:39:29 UTC (rev 3284)
+++ trunk/src/utils.h	2008-11-28 12:52:19 UTC (rev 3285)
@@ -45,7 +45,10 @@
 		g_free(setptr_tmp);\
 	}
 
+#define foreach_c_array(item, array, len) \
+	for (item = array; item < &array[len]; item++)
 
+
 void utils_start_browser(const gchar *uri);
 
 gint utils_get_line_endings(const gchar* buffer, glong size);

Modified: trunk/wscript
===================================================================
--- trunk/wscript	2008-11-28 12:39:29 UTC (rev 3284)
+++ trunk/wscript	2008-11-28 12:52:19 UTC (rev 3285)
@@ -91,7 +91,8 @@
 	'src/geanywraplabel.c', 'src/highlighting.c', 'src/interface.c', 'src/keybindings.c',
 	'src/keyfile.c', 'src/log.c', 'src/main.c', 'src/msgwindow.c', 'src/navqueue.c', 'src/notebook.c',
 	'src/plugins.c', 'src/prefix.c', 'src/prefs.c', 'src/printing.c', 'src/project.c',
-	'src/sciwrappers.c', 'src/search.c', 'src/socket.c', 'src/support.c', 'src/symbols.c',
+	'src/sciwrappers.c', 'src/search.c', 'src/socket.c', 'src/stash.c',
+	'src/support.c', 'src/symbols.c',
 	'src/templates.c', 'src/tools.c', 'src/treeviews.c', 'src/ui_utils.c', 'src/utils.c' ]
 
 


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