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

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.



git-svn-id: https://geany.svn.sourceforge.net/svnroot/geany/trunk@3285 ea778897-0a13-0410-b9d1-a72fbfd435f5
üst 82449f38
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> 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, * src/utils.c, src/ui_utils.h, src/utils.h, src/geany.h,
......
...@@ -35,6 +35,7 @@ SRCS = \ ...@@ -35,6 +35,7 @@ SRCS = \
sciwrappers.c sciwrappers.h \ sciwrappers.c sciwrappers.h \
search.c search.h \ search.c search.h \
socket.c socket.h \ socket.c socket.h \
stash.c stash.h \
support.c support.h \ support.c support.h \
symbols.c symbols.h \ symbols.c symbols.h \
templates.c templates.h \ templates.c templates.h \
......
...@@ -59,6 +59,7 @@ ...@@ -59,6 +59,7 @@
#include "printing.h" #include "printing.h"
#include "plugins.h" #include "plugins.h"
#include "templates.h" #include "templates.h"
#include "stash.h"
/* some default settings which are used at the very first start of Geany to fill /* some default settings which are used at the very first start of Geany to fill
...@@ -91,110 +92,67 @@ static gint hpan_position; ...@@ -91,110 +92,67 @@ static gint hpan_position;
static gint vpan_position; static gint vpan_position;
typedef enum SettingCallbackAction /* probably this enum will be removed */
typedef enum SettingAction
{ {
SETTING_READ, SETTING_READ,
SETTING_WRITE SETTING_WRITE
} }
SettingCallbackAction; SettingAction;
static void settings_action(GKeyFile *config, SettingAction action)
typedef struct SettingEntry
{
gpointer setting;
const gchar *group;
const gchar *key_name;
gpointer default_value;
}
SettingEntry;
static void bool_settings_foreach(GKeyFile *config, SettingCallbackAction action)
{ {
guint i; GeanyPrefEntry package_items[] =
SettingEntry 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}, {G_TYPE_BOOLEAN, &search_prefs.suppress_dialogs, "pref_main_suppress_search_dialogs",
{&search_prefs.use_current_file_dir, "search", "pref_search_current_file_dir", (gpointer)TRUE}, (gpointer)FALSE, NULL},
{G_TYPE_BOOLEAN, &search_prefs.use_current_word, "pref_main_search_use_current_word",
{&editor_prefs.indentation->detect_type, PACKAGE, "check_detect_indent", (gpointer)FALSE}, (gpointer)TRUE, NULL},
{&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},
{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[] =
for (i = 0; i < G_N_ELEMENTS(items); i++)
{ {
SettingEntry *se = &items[i]; {G_TYPE_BOOLEAN, &search_prefs.use_current_file_dir, "pref_search_current_file_dir",
gboolean *setting = se->setting; (gpointer)TRUE, NULL}
};
switch (action) GeanyPrefGroup groups[] =
{
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[] =
{ {
{&editor_prefs.indentation->width, PACKAGE, "pref_editor_tab_width", (gpointer)4}, {PACKAGE, package_items, G_N_ELEMENTS(package_items), FALSE},
{&editor_prefs.indentation->hard_tab_width, PACKAGE, "indent_hard_tab_width", (gpointer)8}, {"search", search_items, G_N_ELEMENTS(search_items), FALSE}
{&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}
}; };
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) switch (action)
{ {
case SETTING_READ: case SETTING_READ:
*setting = utils_get_setting_integer(config, se->group, se->key_name, stash_load_group(group, config); break;
GPOINTER_TO_INT(se->default_value));
break;
case SETTING_WRITE: case SETTING_WRITE:
g_key_file_set_integer(config, se->group, se->key_name, *setting); stash_save_group(group, config); break;
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) static void save_recent_files(GKeyFile *config)
{ {
gchar **recent_files = g_new0(gchar*, file_prefs.mru_length + 1); gchar **recent_files = g_new0(gchar*, file_prefs.mru_length + 1);
......
...@@ -63,7 +63,8 @@ endif ...@@ -63,7 +63,8 @@ endif
OBJS = about.o build.o callbacks.o dialogs.o document.o editor.o encodings.o filetypes.o \ 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 \ 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 \ 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 ui_utils.o utils.o win32.o
.c.o: .c.o:
......
/*
* 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);
}
/*
* 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
...@@ -45,6 +45,9 @@ ...@@ -45,6 +45,9 @@
g_free(setptr_tmp);\ 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); void utils_start_browser(const gchar *uri);
......
...@@ -91,7 +91,8 @@ geany_sources = [ ...@@ -91,7 +91,8 @@ geany_sources = [
'src/geanywraplabel.c', 'src/highlighting.c', 'src/interface.c', 'src/keybindings.c', '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/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/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' ] 'src/templates.c', 'src/tools.c', 'src/treeviews.c', 'src/ui_utils.c', 'src/utils.c' ]
......
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