282 lines
8.1 KiB
C
282 lines
8.1 KiB
C
|
#include <glib.h>
|
||
|
#include "gm-color-table.h"
|
||
|
#include "string.h"
|
||
|
#include "ansi.h"
|
||
|
#include "debug.h"
|
||
|
|
||
|
#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);
|
||
|
|
||
|
typedef struct _GmColorTableItem GmColorTableItem;
|
||
|
|
||
|
struct _GmColorTableItem {
|
||
|
gchar *hex;
|
||
|
GdkColor color;
|
||
|
};
|
||
|
|
||
|
struct _GmColorTablePrivate {
|
||
|
gboolean bold;
|
||
|
GHashTable *colors;
|
||
|
gchar *font_description;
|
||
|
};
|
||
|
|
||
|
/* Signals */
|
||
|
|
||
|
enum {
|
||
|
COLOR_CHANGED,
|
||
|
BOLD_TOGGLED,
|
||
|
FONT_CHANGED,
|
||
|
NUM_SIGNALS
|
||
|
};
|
||
|
|
||
|
static guint color_table_signals[NUM_SIGNALS] = {0};
|
||
|
|
||
|
G_DEFINE_TYPE(GmColorTable, gm_color_table, G_TYPE_OBJECT)
|
||
|
|
||
|
static void
|
||
|
gm_color_table_finalize(GObject *object) {
|
||
|
//GmColorTable *table = GM_COLOR_TABLE(object);
|
||
|
|
||
|
G_OBJECT_CLASS(gm_color_table_parent_class)->finalize(object);
|
||
|
}
|
||
|
|
||
|
static void
|
||
|
gm_color_table_class_init(GmColorTableClass *klass) {
|
||
|
GObjectClass *object_class = G_OBJECT_CLASS(klass);
|
||
|
|
||
|
object_class->finalize = gm_color_table_finalize;
|
||
|
|
||
|
color_table_signals[COLOR_CHANGED] =
|
||
|
g_signal_new("color_changed",
|
||
|
G_OBJECT_CLASS_TYPE(object_class),
|
||
|
G_SIGNAL_RUN_LAST,
|
||
|
G_STRUCT_OFFSET(GmColorTableClass, color_changed),
|
||
|
NULL, NULL,
|
||
|
g_cclosure_marshal_VOID__STRING,
|
||
|
G_TYPE_NONE,
|
||
|
1,
|
||
|
G_TYPE_STRING);
|
||
|
|
||
|
color_table_signals[BOLD_TOGGLED] =
|
||
|
g_signal_new("bold_toggled",
|
||
|
G_OBJECT_CLASS_TYPE(object_class),
|
||
|
G_SIGNAL_RUN_LAST,
|
||
|
G_STRUCT_OFFSET(GmColorTableClass, bold_toggled),
|
||
|
NULL, NULL,
|
||
|
g_cclosure_marshal_VOID__BOOLEAN,
|
||
|
G_TYPE_NONE,
|
||
|
1,
|
||
|
G_TYPE_BOOLEAN);
|
||
|
|
||
|
color_table_signals[FONT_CHANGED] =
|
||
|
g_signal_new("font_changed",
|
||
|
G_OBJECT_CLASS_TYPE(object_class),
|
||
|
G_SIGNAL_RUN_LAST,
|
||
|
G_STRUCT_OFFSET(GmColorTableClass, font_changed),
|
||
|
NULL, NULL,
|
||
|
g_cclosure_marshal_VOID__STRING,
|
||
|
G_TYPE_NONE,
|
||
|
1,
|
||
|
G_TYPE_STRING);
|
||
|
|
||
|
g_type_class_add_private(object_class, sizeof(GmColorTablePrivate));
|
||
|
}
|
||
|
|
||
|
static void
|
||
|
gm_color_table_init(GmColorTable *table) {
|
||
|
table->priv = GM_COLOR_TABLE_GET_PRIVATE(table);
|
||
|
table->priv->colors = g_hash_table_new_full(g_str_hash, g_str_equal,
|
||
|
g_free, gm_color_table_item_free);
|
||
|
}
|
||
|
|
||
|
/* Private functions */
|
||
|
void
|
||
|
gm_color_table_item_free(gpointer item) {
|
||
|
GmColorTableItem *i = (GmColorTableItem *)(item);
|
||
|
|
||
|
g_free(i->hex);
|
||
|
g_free(i);
|
||
|
}
|
||
|
|
||
|
void
|
||
|
gm_color_table_initialize(GmColorTable *table) {
|
||
|
gm_color_table_set_bold(table, FALSE);
|
||
|
gm_color_table_set_font_description(table, "Monospace 10");
|
||
|
|
||
|
/*gm_color_table_set(table, "fg_default", "#D6B5D6B5D6B5");
|
||
|
gm_color_table_set(table, "fg_black", "#2D6B2D6B2D6B");
|
||
|
gm_color_table_set(table, "fg_red", "#FFFF00000000");
|
||
|
gm_color_table_set(table, "fg_green", "#0000FFFF0000");
|
||
|
gm_color_table_set(table, "fg_yellow", "#FFFFD0450000");
|
||
|
gm_color_table_set(table, "fg_blue", "#3EF73EF7BFFF");
|
||
|
gm_color_table_set(table, "fg_purple", "#A0A02020F0F0");
|
||
|
gm_color_table_set(table, "fg_cyan", "#0000FFFFFFFF");
|
||
|
gm_color_table_set(table, "fg_white", "#D8C5D8C5D8C5");
|
||
|
|
||
|
gm_color_table_set(table, "fg_default_h", "#FFFFFFFFFFFF");
|
||
|
gm_color_table_set(table, "fg_black_h", "#529452945294");
|
||
|
gm_color_table_set(table, "fg_red_h", "#FFFF785F785F");
|
||
|
gm_color_table_set(table, "fg_green_h", "#66ADFFFF66AD");
|
||
|
gm_color_table_set(table, "fg_yellow_h", "#FFFFFFFF58C6");
|
||
|
gm_color_table_set(table, "fg_blue_h", "#86318631FFFF");
|
||
|
gm_color_table_set(table, "fg_purple_h", "#C6576A18FFFF");
|
||
|
gm_color_table_set(table, "fg_cyan_h", "#86EEFFFFFFFF");
|
||
|
gm_color_table_set(table, "fg_white_h", "#FFFFFFFFFFFF");
|
||
|
|
||
|
gm_color_table_set(table, "bg_default", "#000000000000");
|
||
|
gm_color_table_set(table, "bg_black", "#2B5B2B5B2B5B");
|
||
|
gm_color_table_set(table, "bg_red", "#FFFF00000000");
|
||
|
gm_color_table_set(table, "bg_green", "#000080000000");
|
||
|
gm_color_table_set(table, "bg_yellow", "#C047C0470000");
|
||
|
gm_color_table_set(table, "bg_blue", "#00000000FFFF");
|
||
|
gm_color_table_set(table, "bg_purple", "#A0A02020F0F0");
|
||
|
gm_color_table_set(table, "bg_cyan", "#0000B74CB74C");
|
||
|
gm_color_table_set(table, "bg_white", "#FFFFFFFFFFFF");*/
|
||
|
|
||
|
gm_color_table_set(table, "fg_default", "#000000");
|
||
|
gm_color_table_set(table, "fg_black", "#000000");
|
||
|
gm_color_table_set(table, "fg_red", "#663822");
|
||
|
gm_color_table_set(table, "fg_green", "#445632");
|
||
|
gm_color_table_set(table, "fg_yellow", "#D1940C");
|
||
|
gm_color_table_set(table, "fg_blue", "#314E6C");
|
||
|
gm_color_table_set(table, "fg_purple", "#494066");
|
||
|
gm_color_table_set(table, "fg_cyan", "#0000FFFFFFFF");
|
||
|
gm_color_table_set(table, "fg_white", "#BAB5AB");
|
||
|
|
||
|
gm_color_table_set(table, "fg_default_h", "#565248");
|
||
|
gm_color_table_set(table, "fg_black_h", "#565248");
|
||
|
gm_color_table_set(table, "fg_red_h", "#990000");
|
||
|
gm_color_table_set(table, "fg_green_h", "#267726");
|
||
|
gm_color_table_set(table, "fg_yellow_h", "#EED680");
|
||
|
gm_color_table_set(table, "fg_blue_h", "#9DB8D2");
|
||
|
gm_color_table_set(table, "fg_purple_h", "#ADA7C8");
|
||
|
gm_color_table_set(table, "fg_cyan_h", "#86EEFFFFFFFF");
|
||
|
gm_color_table_set(table, "fg_white_h", "#807D74");
|
||
|
|
||
|
gm_color_table_set(table, "bg_default", "#EAE8E3");
|
||
|
gm_color_table_set(table, "bg_black", "#000000");
|
||
|
gm_color_table_set(table, "bg_red", "#663822");
|
||
|
gm_color_table_set(table, "bg_green", "#445632");
|
||
|
gm_color_table_set(table, "bg_yellow", "#D1940C");
|
||
|
gm_color_table_set(table, "bg_blue", "#314E6C");
|
||
|
gm_color_table_set(table, "bg_purple", "#494066");
|
||
|
gm_color_table_set(table, "bg_cyan", "#0000FFFFFFFF");
|
||
|
gm_color_table_set(table, "bg_white", "#FFFFFFFFFFFF");
|
||
|
}
|
||
|
|
||
|
/* Public functions */
|
||
|
|
||
|
GmColorTable *
|
||
|
gm_color_table_new(void) {
|
||
|
GmColorTable *table = GM_COLOR_TABLE(g_object_new(GM_TYPE_COLOR_TABLE, NULL));
|
||
|
|
||
|
gm_color_table_restore_defaults(table);
|
||
|
return table;
|
||
|
}
|
||
|
|
||
|
GmColorTable *
|
||
|
gm_color_table_new_from_options(GmOptions *options) {
|
||
|
GmColorTable *table = GM_COLOR_TABLE(g_object_new(GM_TYPE_COLOR_TABLE, NULL));
|
||
|
|
||
|
gm_color_table_set_from_options(table, options);
|
||
|
return table;
|
||
|
}
|
||
|
|
||
|
void
|
||
|
gm_color_table_restore_defaults(GmColorTable *table) {
|
||
|
gm_color_table_initialize(table);
|
||
|
}
|
||
|
|
||
|
void
|
||
|
gm_color_table_set(GmColorTable *table, const gchar *name, const gchar *hex) {
|
||
|
GmColorTableItem *item;
|
||
|
|
||
|
item = g_hash_table_lookup(table->priv->colors, name);
|
||
|
|
||
|
if (!item) {
|
||
|
item = g_new0(GmColorTableItem, 1);
|
||
|
g_hash_table_insert(table->priv->colors, g_strdup(name), item);
|
||
|
}
|
||
|
|
||
|
if (item->hex == NULL || strcmp(hex, item->hex) != 0) {
|
||
|
g_free(item->hex);
|
||
|
item->hex = g_strdup(hex);
|
||
|
gdk_color_parse(item->hex, &(item->color));
|
||
|
|
||
|
g_signal_emit(table, color_table_signals[COLOR_CHANGED], 0, name);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
gboolean
|
||
|
gm_color_table_get(GmColorTable *table, const gchar *name, GdkColor *color) {
|
||
|
GmColorTableItem *item;
|
||
|
|
||
|
item = g_hash_table_lookup(table->priv->colors, name);
|
||
|
|
||
|
if (item != NULL) {
|
||
|
*color = item->color;
|
||
|
return TRUE;
|
||
|
} else {
|
||
|
return FALSE;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void
|
||
|
gm_color_table_set_bold(GmColorTable *table, gboolean bold) {
|
||
|
if (table->priv->bold != bold) {
|
||
|
table->priv->bold = bold;
|
||
|
|
||
|
g_signal_emit(table, color_table_signals[BOLD_TOGGLED], 0, bold);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
gboolean
|
||
|
gm_color_table_bold(GmColorTable *table) {
|
||
|
return table->priv->bold;
|
||
|
}
|
||
|
|
||
|
void
|
||
|
gm_color_table_set_font_description(GmColorTable *table,
|
||
|
const gchar *font_description) {
|
||
|
const gchar *fd;
|
||
|
|
||
|
if (font_description == NULL) {
|
||
|
fd = "Monospace 10";
|
||
|
} else {
|
||
|
fd = font_description;
|
||
|
}
|
||
|
|
||
|
if (table->priv->font_description == NULL ||
|
||
|
strcmp(table->priv->font_description, fd) != 0) {
|
||
|
g_free(table->priv->font_description);
|
||
|
table->priv->font_description = g_strdup(fd);
|
||
|
|
||
|
g_signal_emit(table, color_table_signals[FONT_CHANGED], 0,
|
||
|
table->priv->font_description);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
const gchar *
|
||
|
gm_color_table_font_description(GmColorTable *table) {
|
||
|
return table->priv->font_description;
|
||
|
}
|
||
|
|
||
|
void
|
||
|
gm_color_table_set_from_options(GmColorTable *table, GmOptions *options) {
|
||
|
unsigned int i;
|
||
|
const gchar *value;
|
||
|
|
||
|
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(table, ansi_colors[i].name, value);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
gm_color_table_set_bold(table, gm_options_get_int(options, "bold-colors"));
|
||
|
gm_color_table_set_font_description(table, gm_options_get(options,
|
||
|
"font-family"));
|
||
|
}
|