1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
|
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string.h>
#include "gm-source-style-scheme.h"
#include "gm-color-table.h"
#include "gm-app.h"
#include "gm-support.h"
#include "gm-debug.h"
#define GM_TYPE_SOURCE_STYLE_SCHEME (gm_source_style_scheme_get_type ())
#define GM_SOURCE_STYLE_SCHEME(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GM_TYPE_SOURCE_STYLE_SCHEME, GmSourceStyleScheme))
#define GM_SOURCE_STYLE_SCHEME_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GM_TYPE_SOURCE_STYLE_SCHEME, GmSourceStyleSchemeClass))
#define GM_IS_SOURCE_STYLE_SCHEME(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GM_TYPE_SOURCE_STYLE_SCHEME))
#define GM_IS_SOURCE_STYLE_SCHEME_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GM_TYPE_SOURCE_STYLE_SCHEME))
#define GM_SOURCE_STYLE_SCHEME_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GM_TYPE_SOURCE_STYLE_SCHEME, GmSourceStyleSchemeClass))
typedef struct _GmSourceStyleScheme GmSourceStyleScheme;
typedef struct _GmSourceStyleSchemeClass GmSourceStyleSchemeClass;
struct _GmSourceStyleScheme {
GObject parent_instance;
GHashTable *mapping;
GHashTable *styles;
};
struct _GmSourceStyleSchemeClass {
GObjectClass parent_class;
};
static GObjectClass *parent_class = NULL;
static GType gm_source_style_scheme_get_type (void);
static void gm_source_style_scheme_class_init(GmSourceStyleSchemeClass *klass);
static void gm_source_style_scheme_IFace_init(GtkSourceStyleSchemeClass *iface);
static void gm_source_style_scheme_init(GmSourceStyleScheme *scheme);
static void gm_source_style_scheme_finalize(GObject *object);
static GtkSourceTagStyle *gm_source_style_scheme_get_tag_style(
GtkSourceStyleScheme *scheme, const gchar *style_name);
static const gchar *gm_source_style_scheme_get_name(GtkSourceStyleScheme *scheme);
static GSList *gm_source_style_scheme_get_style_names(GtkSourceStyleScheme *scheme);
static GType
gm_source_style_scheme_get_type (void)
{
static GType type = 0;
if (!type)
{
static const GTypeInfo info =
{
sizeof (GmSourceStyleSchemeClass),
NULL, /* base_init */
NULL, /* base_finalize */
(GClassInitFunc) gm_source_style_scheme_class_init,
NULL, /* class_finalize */
NULL, /* class_data */
sizeof (GmSourceStyleScheme),
0, /* n_preallocs */
(GInstanceInitFunc) gm_source_style_scheme_init,
};
static const GInterfaceInfo iface_info =
{
(GInterfaceInitFunc) gm_source_style_scheme_IFace_init, /* interface_init */
NULL, /* interface_finalize */
NULL /* interface_data */
};
type = g_type_register_static (G_TYPE_OBJECT,
"GmSourceStyleScheme",
&info,
0);
g_type_add_interface_static (type,
GTK_TYPE_SOURCE_STYLE_SCHEME,
&iface_info);
}
return type;
}
static void
gm_source_style_scheme_class_init(GmSourceStyleSchemeClass *klass) {
GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
parent_class = g_type_class_peek_parent(klass);
gobject_class->finalize = gm_source_style_scheme_finalize;
}
static void
gm_source_style_scheme_IFace_init(GtkSourceStyleSchemeClass *iface) {
iface->get_tag_style = gm_source_style_scheme_get_tag_style;
iface->get_name = gm_source_style_scheme_get_name;
iface->get_style_names = gm_source_style_scheme_get_style_names;
}
static GtkSourceTagStyle *
new_tag_style(gchar const * foreground, gchar const * background, gboolean bold,
gboolean italic) {
GtkSourceTagStyle *ts;
ts = gtk_source_tag_style_new();
gdk_color_parse(foreground, &ts->foreground);
ts->mask |= GTK_SOURCE_TAG_STYLE_USE_FOREGROUND;
if (background != NULL) {
gdk_color_parse (background, &ts->background);
ts->mask |= GTK_SOURCE_TAG_STYLE_USE_BACKGROUND;
}
ts->italic = italic;
ts->bold = bold;
ts->is_default = TRUE;
return ts;
}
typedef struct _GmSourceStyleEntry {
gchar const *name;
gchar const *color_name;
gboolean bold;
gboolean italic;
} GmSourceStyleEntry;
static GmSourceStyleEntry entries[] = {
{"Base-N Integer", "fg_purple_h"},
{"Character", "fg_purple_h"},
{"Comment", "fg_blue_h"},
{"Data Type", "fg_cyan", TRUE},
{"Function", "fg_green"},
{"Decimal", "fg_purple_h"},
{"Floating Point", "fg_purple_h"},
{"Keyword", "fg_red", TRUE},
{"String", "fg_purple_h"},
{NULL, NULL}
};
static GmKeyValuePair mapping[] = {
{"fg_purple_h", "Object@32@Number String"},
{"fg_cyan", "Builtin@32@Variables Types S-Ref Error"},
{"fg_green", "Builtin@32@Functions"},
{"fg_blue_h", "Line@32@Comment"},
{"fg_red", "Keywords"},
{NULL, NULL}
};
static void
gm_source_style_scheme_init_mapping(GmSourceStyleScheme *scheme) {
GmKeyValuePair *entry;
scheme->mapping = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
(GDestroyNotify)g_strfreev);
for (entry = mapping; entry->key; ++entry) {
g_hash_table_insert(scheme->mapping, g_strdup(entry->key),
g_strsplit(entry->value, " ", 0));
}
}
static void
gm_source_style_scheme_init(GmSourceStyleScheme *scheme) {
GtkSourceTagStyle *ts;
GmColorTable *table = gm_app_color_table(gm_app_instance());
GmSourceStyleEntry *entry;
scheme->styles = g_hash_table_new_full((GHashFunc)g_str_hash,
(GEqualFunc)g_str_equal, (GDestroyNotify)g_free,
(GDestroyNotify)gtk_source_tag_style_free);
for (entry = entries; entry->name; ++entry) {
ts = new_tag_style(gm_color_table_get_hex(table, entry->color_name),
NULL, entry->bold, entry->italic);
g_hash_table_insert(scheme->styles, g_strdup(entry->name), ts);
}
gm_source_style_scheme_init_mapping(scheme);
}
static void
gm_source_style_scheme_finalize(GObject *object) {
GmSourceStyleScheme *scheme = GM_SOURCE_STYLE_SCHEME(object);
g_hash_table_destroy(scheme->styles);
g_hash_table_destroy(scheme->mapping);
G_OBJECT_CLASS(parent_class)->finalize (object);
}
static GtkSourceTagStyle *
gm_source_style_scheme_get_tag_style(GtkSourceStyleScheme *scheme,
const gchar *style_name) {
GmSourceStyleScheme *ds;
const gpointer *style;
g_return_val_if_fail(GM_IS_SOURCE_STYLE_SCHEME(scheme), NULL);
g_return_val_if_fail(style_name != NULL, NULL);
ds = GM_SOURCE_STYLE_SCHEME(scheme);
style = g_hash_table_lookup(ds->styles, style_name);
return (style != NULL) ? gtk_source_tag_style_copy((GtkSourceTagStyle *)style) : NULL;
}
static const gchar *
gm_source_style_scheme_get_name(GtkSourceStyleScheme *scheme) {
g_return_val_if_fail(GTK_IS_SOURCE_STYLE_SCHEME(scheme), NULL);
return _("Gnoemoe");
}
static void
add_style_name(gpointer key, gpointer value, gpointer user_data) {
GSList **l = user_data;
*l = g_slist_append(*l, g_strdup(key));
}
static GSList *
gm_source_style_scheme_get_style_names(GtkSourceStyleScheme *scheme) {
GmSourceStyleScheme *ds;
GSList *l = NULL;
g_return_val_if_fail(GTK_IS_SOURCE_STYLE_SCHEME(scheme), NULL);
ds = GM_SOURCE_STYLE_SCHEME(scheme);
g_hash_table_foreach(ds->styles, add_style_name, &l);
return l;
}
/* Default style scheme */
static GtkSourceStyleScheme *gm_style_scheme = NULL;
static void
on_gm_source_style_scheme_color_changed(GmColorTable *table,
gchar const *color) {
gchar **ids;
gchar const *hex = gm_color_table_get_hex(table, color);
GmSourceStyleEntry *entry;
GtkSourceTagStyle *style;
for (entry = entries; entry->name; ++entry) {
if (strcmp(entry->color_name, color) == 0) {
style = g_hash_table_lookup(
GM_SOURCE_STYLE_SCHEME(gm_style_scheme)->styles,
entry->name);
if (style != NULL) {
gdk_color_parse(hex, &(style->foreground));
}
}
}
ids = g_hash_table_lookup(GM_SOURCE_STYLE_SCHEME(gm_style_scheme)->mapping,
color);
if (ids) {
for (; *ids; ++ids) {
g_signal_emit_by_name(gm_style_scheme, "style_changed",
*ids);
}
}
}
void
gm_source_style_weak_destroy(gpointer data, GObject *object) {
g_signal_handlers_disconnect_by_func(gm_app_color_table(gm_app_instance()),
on_gm_source_style_scheme_color_changed, NULL);
gm_style_scheme = NULL;
}
GtkSourceStyleScheme *
gm_source_style_scheme_get_default(void) {
if (gm_style_scheme == NULL) {
gm_style_scheme = g_object_new(GM_TYPE_SOURCE_STYLE_SCHEME,
NULL);
g_object_weak_ref(G_OBJECT(gm_style_scheme),
gm_source_style_weak_destroy, NULL);
g_signal_connect(gm_app_color_table(gm_app_instance()),
"color-changed",
G_CALLBACK(on_gm_source_style_scheme_color_changed), NULL);
} else {
g_object_ref(gm_style_scheme);
}
return gm_style_scheme;
}
|