From 4dddfc7cdc6c122a91115b73827a8db1533480f5 Mon Sep 17 00:00:00 2001 From: Jesse van den Kieboom Date: Mon, 2 Jan 2006 17:55:08 +0000 Subject: [PATCH] Added use system font, fixed filling from options with missing options --- gnoemoe/gm-color-table.c | 212 +++++++++++++++++++++++++++++++-------- gnoemoe/gm-color-table.h | 4 + 2 files changed, 173 insertions(+), 43 deletions(-) diff --git a/gnoemoe/gm-color-table.c b/gnoemoe/gm-color-table.c index 469d624..1bdbd76 100644 --- a/gnoemoe/gm-color-table.c +++ b/gnoemoe/gm-color-table.c @@ -1,4 +1,5 @@ #include +#include #include "gm-color-table.h" #include "string.h" #include "ansi.h" @@ -6,7 +7,7 @@ #define GM_COLOR_TABLE_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE((object), GM_TYPE_COLOR_TABLE, GmColorTablePrivate)) -void gm_color_table_item_free(gpointer item); +static void gm_color_table_item_free(gpointer item); typedef struct _GmColorTableItem GmColorTableItem; @@ -103,11 +104,20 @@ static const GmColorScheme scheme_names[] = { struct _GmColorTablePrivate { GHashTable *colors; gchar *font_description; + gboolean use_system_font; GmOptions *options; GmColorTableScheme scheme; + guint gconf_connection_id; }; +static void on_gm_color_table_monospace_font_change_notify(GConfClient *client, + guint cnxn_id, GConfEntry *entry, gpointer user_data); +static void gm_color_table_disconnect_font_changed(GmColorTable *table); + +#define MONOSPACE_FONT_DIR "/desktop/gnome/interface" +#define MONOSPACE_FONT_KEY MONOSPACE_FONT_DIR "/monospace_font_name" + /* Signals */ enum { @@ -122,8 +132,9 @@ G_DEFINE_TYPE(GmColorTable, gm_color_table, G_TYPE_OBJECT) static void gm_color_table_finalize(GObject *object) { - //GmColorTable *table = GM_COLOR_TABLE(object); + GmColorTable *table = GM_COLOR_TABLE(object); + gm_color_table_disconnect_font_changed(table); G_OBJECT_CLASS(gm_color_table_parent_class)->finalize(object); } @@ -167,7 +178,7 @@ gm_color_table_init(GmColorTable *table) { } /* Private functions */ -void +static void gm_color_table_item_free(gpointer item) { GmColorTableItem *i = (GmColorTableItem *)(item); @@ -175,7 +186,7 @@ gm_color_table_item_free(gpointer item) { g_free(i); } -void +static void gm_color_table_set_value(GmColorTable *table, gchar const *name, gchar const *hex) { GmColorTableItem *item; @@ -195,59 +206,95 @@ gm_color_table_set_value(GmColorTable *table, gchar const *name, } } -void -gm_color_table_load_scheme(GmColorTable *table, - GmColorTableScheme scheme) { - guint i = 0; - GmColorTableSchemeItem const *values; - GmOptions *options = table->priv->options; - gchar const *value; +static gchar * +gm_color_table_default_font(GmColorTable *table) { + GConfClient *conf = gconf_client_get_default(); + gchar *name = gconf_client_get_string(conf, MONOSPACE_FONT_KEY, NULL); - if (scheme == SCHEME_USER) { - for (i = 0; i < sizeof(ansi_colors) / sizeof(ansinamepair); ++i) { - value = gm_options_get(options, ansi_colors[i].name); - - if (value != NULL) { - gm_color_table_set_value(table, ansi_colors[i].name, value); - } - } + g_object_unref(G_OBJECT(conf)); + + if (name) { + return name; } else { - values = scheme_names[scheme].values; - - while (values[i].name != NULL) { - gm_color_table_set_value(table, values[i].name, values[i].hex); - ++i; - } + return g_strdup("Monospace 10"); } - - table->priv->scheme = scheme; } -void -gm_color_table_initialize(GmColorTable *table) { - gm_color_table_set_font_description(table, "Monospace 10"); - gm_color_table_load_scheme(table, SCHEME_DEFAULT); -} - -void +static void gm_color_table_fill_from_options(GmColorTable *table) { gchar const *value; + gint vint; + gchar *font; GmOptions *options = table->priv->options; // New, color schemes value = gm_options_get(options, "color_scheme"); - gm_color_table_set_from_scheme_name(table, gm_options_get(options, - "color_scheme")); + gm_color_table_set_from_scheme_name(table, value); - value = gm_options_get(options, "font_family"); + value = gm_options_get(options, "use_system_font"); + vint = gm_options_get_int(options, "use_system_font"); - if (value && *value != '\0') { - gm_color_table_set_font_description(table, value); + if (!value || vint) { + gm_color_table_set_use_system_font(table, TRUE); } else { - gm_options_set(options, "font_family", "Monospace 10"); + value = gm_options_get(options, "font_family"); + + if (!value || *value == '\0') { + font = gm_color_table_default_font(table); + gm_options_set(options, "font_family", font); + g_free(font); + value = gm_options_get(options, "font_family"); + } + + gm_color_table_set_font_description(table, value); } } +static void +gm_color_table_disconnect_font_changed(GmColorTable *table) { + GConfClient *conf; + + if (table->priv->gconf_connection_id) { + conf = gconf_client_get_default(); + gconf_client_notify_remove(conf, table->priv->gconf_connection_id); + gconf_client_remove_dir(conf, MONOSPACE_FONT_DIR, NULL); + table->priv->gconf_connection_id = 0; + g_object_unref(G_OBJECT(conf)); + } +} + +static void +gm_color_table_connect_font_changed(GmColorTable *table) { + GError *err; + GConfClient *conf; + guint connection_id; + + conf = gconf_client_get_default (); + + err = NULL; + gconf_client_add_dir(conf, MONOSPACE_FONT_DIR, + GCONF_CLIENT_PRELOAD_ONELEVEL, &err); + + if (err) { + gm_debug_msg(DEBUG_DEFAULT, "There was an error loading config from " + "%s. (%s)\n", MONOSPACE_FONT_DIR, err->message); + g_error_free(err); + } + + err = NULL; + connection_id = gconf_client_notify_add(conf, MONOSPACE_FONT_DIR, + on_gm_color_table_monospace_font_change_notify, table, NULL, &err); + g_object_unref (conf); + + if (err) { + gm_debug_msg(DEBUG_DEFAULT, "There was an error subscribing to " + "notification of monospace font changes. (%s)\n", err->message); + g_error_free(err); + } else { + table->priv->gconf_connection_id = connection_id; + } +} + /* Public functions */ GmColorTable * @@ -256,6 +303,8 @@ gm_color_table_new(void) { NULL)); gm_color_table_load_scheme(table, SCHEME_DEFAULT); + gm_color_table_set_use_system_font(table, TRUE); + return table; } @@ -329,12 +378,14 @@ gm_color_table_get_hex(GmColorTable *table, const gchar *name) { void gm_color_table_set_font_description(GmColorTable *table, gchar const *font_description) { - const gchar *fd; + gchar *fd; if (font_description == NULL) { - fd = "Monospace 10"; + fd = gm_color_table_default_font(table); + } else if (table->priv->use_system_font) { + return; } else { - fd = font_description; + fd = g_strdup(font_description); } gm_options_set(table->priv->options, "font_family", @@ -348,6 +399,34 @@ gm_color_table_set_font_description(GmColorTable *table, g_signal_emit(table, color_table_signals[FONT_CHANGED], 0, table->priv->font_description); } + + g_free(fd); +} + +void +gm_color_table_set_use_system_font(GmColorTable *table, + gboolean use_system_font) { + + if (table->priv->use_system_font == use_system_font) { + return; + } + + table->priv->use_system_font = use_system_font; + + if (use_system_font) { + gm_color_table_connect_font_changed(table); + gm_color_table_set_font_description(table, NULL); + + gm_options_set(table->priv->options, "use_system_font", "1"); + } else { + gm_color_table_disconnect_font_changed(table); + gm_options_set(table->priv->options, "use_system_font", "0"); + } +} + +gboolean +gm_color_table_get_use_system_font(GmColorTable *table) { + return table->priv->use_system_font; } const gchar * @@ -355,6 +434,41 @@ gm_color_table_font_description(GmColorTable *table) { return table->priv->font_description; } +void +gm_color_table_load_scheme(GmColorTable *table, + GmColorTableScheme scheme) { + guint i = 0; + GmColorTableSchemeItem const *values; + GmOptions *options = table->priv->options; + gchar const *value; + + if (scheme == SCHEME_USER) { + values = scheme_names[SCHEME_DEFAULT].values; + + while (values[i].name != NULL) { + value = gm_options_get(options, values[i].name); + + if (value != NULL) { + gm_color_table_set_value(table, values[i].name, value); + } else { + gm_options_set(options, values[i].name, values[i].hex); + gm_color_table_set_value(table, values[i].name, values[i].hex); + } + + ++i; + } + } else { + values = scheme_names[scheme].values; + + while (values[i].name != NULL) { + gm_color_table_set_value(table, values[i].name, values[i].hex); + ++i; + } + } + + table->priv->scheme = scheme; +} + void gm_color_table_set_from_scheme_name(GmColorTable *table, gchar const *scheme) { int i = 0; @@ -377,3 +491,15 @@ gm_color_table_set_from_scheme_name(GmColorTable *table, gchar const *scheme) { scheme_names[i].name); } } + +/* Callbacks */ +static void +on_gm_color_table_monospace_font_change_notify(GConfClient *client, + guint cnxn_id, GConfEntry *entry, gpointer user_data) { + GmColorTable *table = GM_COLOR_TABLE(user_data); + + if (strcmp(entry->key, MONOSPACE_FONT_KEY) == 0) { + gm_color_table_set_font_description(table, NULL); + } +} + diff --git a/gnoemoe/gm-color-table.h b/gnoemoe/gm-color-table.h index 92f4ca9..62929ab 100644 --- a/gnoemoe/gm-color-table.h +++ b/gnoemoe/gm-color-table.h @@ -74,6 +74,10 @@ void gm_color_table_set_font_description(GmColorTable *table, gchar const *font_description); const gchar *gm_color_table_font_description(GmColorTable *table); +void gm_color_table_set_use_system_font(GmColorTable *table, + gboolean use_system_font); +gboolean gm_color_table_get_use_system_font(GmColorTable *table); + void gm_color_table_load_scheme(GmColorTable *table, GmColorTableScheme scheme); void gm_color_table_set_from_scheme_name(GmColorTable *table,