Added use system font, fixed filling from options with missing options

This commit is contained in:
Jesse van den Kieboom 2006-01-02 17:55:08 +00:00
parent 906856cab0
commit 4dddfc7cdc
2 changed files with 173 additions and 43 deletions

View File

@ -1,4 +1,5 @@
#include <glib.h> #include <glib.h>
#include <gconf/gconf-client.h>
#include "gm-color-table.h" #include "gm-color-table.h"
#include "string.h" #include "string.h"
#include "ansi.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)) #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; typedef struct _GmColorTableItem GmColorTableItem;
@ -103,11 +104,20 @@ static const GmColorScheme scheme_names[] = {
struct _GmColorTablePrivate { struct _GmColorTablePrivate {
GHashTable *colors; GHashTable *colors;
gchar *font_description; gchar *font_description;
gboolean use_system_font;
GmOptions *options; GmOptions *options;
GmColorTableScheme scheme; 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 */ /* Signals */
enum { enum {
@ -122,8 +132,9 @@ G_DEFINE_TYPE(GmColorTable, gm_color_table, G_TYPE_OBJECT)
static void static void
gm_color_table_finalize(GObject *object) { 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); G_OBJECT_CLASS(gm_color_table_parent_class)->finalize(object);
} }
@ -167,7 +178,7 @@ gm_color_table_init(GmColorTable *table) {
} }
/* Private functions */ /* Private functions */
void static void
gm_color_table_item_free(gpointer item) { gm_color_table_item_free(gpointer item) {
GmColorTableItem *i = (GmColorTableItem *)(item); GmColorTableItem *i = (GmColorTableItem *)(item);
@ -175,7 +186,7 @@ gm_color_table_item_free(gpointer item) {
g_free(i); g_free(i);
} }
void static void
gm_color_table_set_value(GmColorTable *table, gchar const *name, gm_color_table_set_value(GmColorTable *table, gchar const *name,
gchar const *hex) { gchar const *hex) {
GmColorTableItem *item; GmColorTableItem *item;
@ -195,59 +206,95 @@ gm_color_table_set_value(GmColorTable *table, gchar const *name,
} }
} }
void static gchar *
gm_color_table_load_scheme(GmColorTable *table, gm_color_table_default_font(GmColorTable *table) {
GmColorTableScheme scheme) { GConfClient *conf = gconf_client_get_default();
guint i = 0; gchar *name = gconf_client_get_string(conf, MONOSPACE_FONT_KEY, NULL);
GmColorTableSchemeItem const *values;
GmOptions *options = table->priv->options;
gchar const *value;
if (scheme == SCHEME_USER) { g_object_unref(G_OBJECT(conf));
for (i = 0; i < sizeof(ansi_colors) / sizeof(ansinamepair); ++i) {
value = gm_options_get(options, ansi_colors[i].name); if (name) {
return name;
if (value != NULL) {
gm_color_table_set_value(table, ansi_colors[i].name, value);
}
}
} else { } else {
values = scheme_names[scheme].values; return g_strdup("Monospace 10");
while (values[i].name != NULL) {
gm_color_table_set_value(table, values[i].name, values[i].hex);
++i;
}
} }
table->priv->scheme = scheme;
} }
void static 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
gm_color_table_fill_from_options(GmColorTable *table) { gm_color_table_fill_from_options(GmColorTable *table) {
gchar const *value; gchar const *value;
gint vint;
gchar *font;
GmOptions *options = table->priv->options; GmOptions *options = table->priv->options;
// New, color schemes // New, color schemes
value = gm_options_get(options, "color_scheme"); value = gm_options_get(options, "color_scheme");
gm_color_table_set_from_scheme_name(table, gm_options_get(options, gm_color_table_set_from_scheme_name(table, value);
"color_scheme"));
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') { if (!value || vint) {
gm_color_table_set_font_description(table, value); gm_color_table_set_use_system_font(table, TRUE);
} else { } 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 */ /* Public functions */
GmColorTable * GmColorTable *
@ -256,6 +303,8 @@ gm_color_table_new(void) {
NULL)); NULL));
gm_color_table_load_scheme(table, SCHEME_DEFAULT); gm_color_table_load_scheme(table, SCHEME_DEFAULT);
gm_color_table_set_use_system_font(table, TRUE);
return table; return table;
} }
@ -329,12 +378,14 @@ gm_color_table_get_hex(GmColorTable *table, const gchar *name) {
void void
gm_color_table_set_font_description(GmColorTable *table, gm_color_table_set_font_description(GmColorTable *table,
gchar const *font_description) { gchar const *font_description) {
const gchar *fd; gchar *fd;
if (font_description == NULL) { if (font_description == NULL) {
fd = "Monospace 10"; fd = gm_color_table_default_font(table);
} else if (table->priv->use_system_font) {
return;
} else { } else {
fd = font_description; fd = g_strdup(font_description);
} }
gm_options_set(table->priv->options, "font_family", 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, g_signal_emit(table, color_table_signals[FONT_CHANGED], 0,
table->priv->font_description); 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 * const gchar *
@ -355,6 +434,41 @@ gm_color_table_font_description(GmColorTable *table) {
return table->priv->font_description; 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 void
gm_color_table_set_from_scheme_name(GmColorTable *table, gchar const *scheme) { gm_color_table_set_from_scheme_name(GmColorTable *table, gchar const *scheme) {
int i = 0; int i = 0;
@ -377,3 +491,15 @@ gm_color_table_set_from_scheme_name(GmColorTable *table, gchar const *scheme) {
scheme_names[i].name); 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);
}
}

View File

@ -74,6 +74,10 @@ void gm_color_table_set_font_description(GmColorTable *table,
gchar const *font_description); gchar const *font_description);
const gchar *gm_color_table_font_description(GmColorTable *table); 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, void gm_color_table_load_scheme(GmColorTable *table,
GmColorTableScheme scheme); GmColorTableScheme scheme);
void gm_color_table_set_from_scheme_name(GmColorTable *table, void gm_color_table_set_from_scheme_name(GmColorTable *table,