67240764ebf33e2b3c9922f6cf16a94cbb657d35
[projects/chimara/chimara.git] / libchimara / style.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <math.h>
4 #include "chimara-glk-private.h"
5 #include "glk.h"
6 #include "style.h"
7 #include "magic.h"
8 #include "stream.h"
9 #include "strio.h"
10
11 extern GPrivate glk_data_key;
12
13 static gboolean style_accept(GScanner *scanner, GTokenType token);
14 static gboolean style_accept_style_selector(GScanner *scanner, ChimaraGlk *glk);
15 static gboolean style_accept_style_hint(GScanner *scanner, GtkTextTag *current_tag);
16 static void style_copy_tag_to_textbuffer(gpointer key, gpointer tag, gpointer target_table);
17 static void text_tag_to_attr_list(GtkTextTag *tag, PangoAttrList *list);
18 GtkTextTag* gtk_text_tag_copy(GtkTextTag *tag);
19 static void style_cascade_colors(GtkTextTag *tag, GtkTextTag *glk_tag, GtkTextTag *default_tag, GdkColor **foreground, GdkColor **background);
20
21 /**
22  * glk_set_style:
23  * @styl: The style to apply
24  *
25  * Changes the style of the current output stream. @styl should be one of the
26  * <code>style_</code> constants. However, any value is actually legal; if the 
27  * interpreter does not recognize the style value, it will treat it as 
28  * %style_Normal.
29  * <note><para>
30  *  This policy allows for the future definition of styles without breaking old
31  *  Glk libraries.
32  * </para></note>
33  */
34 void
35 glk_set_style(glui32 styl)
36 {
37         ChimaraGlkPrivate *glk_data = g_private_get(&glk_data_key);
38         g_return_if_fail(glk_data->current_stream != NULL);
39         glk_set_style_stream(glk_data->current_stream, styl);
40 }
41
42 /* The first 11 tag names must correspond to the first 11 glk tag names as defined below */
43 static const gchar* TAG_NAMES[] = {
44         "normal",
45         "emphasized",
46         "preformatted",
47         "header",
48         "subheader",
49         "alert",
50         "note",
51         "block-quote",
52         "input",
53         "user1",
54         "user2",
55         "hyperlink",
56         "default"
57 };
58
59 /* The first 11 glk tag names must correspond to the first 11 tag names as defined above */
60 static const gchar* GLK_TAG_NAMES[] = {
61         "glk-normal",
62         "glk-emphasized",
63         "glk-preformatted",
64         "glk-header",
65         "glk-subheader",
66         "glk-alert",
67         "glk-note",
68         "glk-block-quote",
69         "glk-input",
70         "glk-user1",
71         "glk-user2"
72 };
73
74 const gchar**
75 style_get_tag_names()
76 {
77         return TAG_NAMES;
78 }
79
80 /* Internal function: mapping from style enum to tag name */
81 static const gchar*
82 get_tag_name(glui32 style)
83 {
84         if(style >= CHIMARA_NUM_STYLES) {
85                 WARNING("Unsupported style");
86                 return "normal";
87         } else {
88                 return (gchar*) TAG_NAMES[style];
89         }
90 }
91
92 /* Internal function: mapping from glk style enum to tag name */
93 static const gchar*
94 get_glk_tag_name(glui32 style)
95 {
96         if(style >= style_NUMSTYLES) {
97                 WARNING("Unsupported style");
98                 return "normal";
99         } else {
100                 return (gchar*) GLK_TAG_NAMES[style];
101         }
102 }
103
104 /** 
105  * glk_set_style_stream:
106  * @str: Output stream to change the style of
107  * @styl: The style to apply
108  *
109  * This changes the style of the stream @str. See glk_set_style().
110  */
111 void
112 glk_set_style_stream(strid_t str, glui32 styl) {
113 #ifdef DEBUG_STYLES
114         g_printf("glk_set_style(str->rock=%d, styl=%d)\n", str->rock, styl);
115 #endif
116
117         if(str->window == NULL)
118                 return;
119
120         flush_window_buffer(str->window);
121         str->style = (gchar*) get_tag_name(styl);
122         str->glk_style = (gchar*) get_glk_tag_name(styl);
123 }
124
125 /* Internal function: call this to initialize the default styles to a textbuffer. */
126 void
127 style_init_textbuffer(GtkTextBuffer *buffer)
128 {
129         g_return_if_fail(buffer != NULL);
130
131         ChimaraGlkPrivate *glk_data = g_private_get(&glk_data_key);
132
133         /* Place the default text tags in the textbuffer's tag table */
134         g_hash_table_foreach(glk_data->styles->text_buffer, style_copy_tag_to_textbuffer, gtk_text_buffer_get_tag_table(buffer));
135
136         /* Copy the override text tags to the textbuffers's tag table */
137         g_hash_table_foreach(glk_data->glk_styles->text_buffer, style_copy_tag_to_textbuffer, gtk_text_buffer_get_tag_table(buffer));
138
139         /* Assign the 'default' tag the lowest priority */
140         gtk_text_tag_set_priority( gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer), "default"), 0 );
141 }
142
143
144 /* Internal function: call this to initialize the default styles to a textgrid. */
145 void
146 style_init_textgrid(GtkTextBuffer *buffer)
147 {
148         g_return_if_fail(buffer != NULL);
149
150         ChimaraGlkPrivate *glk_data = g_private_get(&glk_data_key);
151
152         /* Place the default text tags in the textbuffer's tag table */
153         g_hash_table_foreach(glk_data->styles->text_grid, style_copy_tag_to_textbuffer, gtk_text_buffer_get_tag_table(buffer));
154
155         /* Copy the current text tags to the textbuffers's tag table */
156         g_hash_table_foreach(glk_data->glk_styles->text_grid, style_copy_tag_to_textbuffer, gtk_text_buffer_get_tag_table(buffer));
157
158         /* Assign the 'default' tag the lowest priority */
159         gtk_text_tag_set_priority( gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(buffer), "default"), 0 );
160 }
161
162 /* Internal function used to iterate over a style table, copying it */
163 static void
164 style_copy_tag_to_textbuffer(gpointer key, gpointer tag, gpointer target_table)
165 {
166         g_return_if_fail(key != NULL);
167         g_return_if_fail(tag != NULL);
168         g_return_if_fail(target_table != NULL);
169
170         gtk_text_tag_table_add(target_table, gtk_text_tag_copy( GTK_TEXT_TAG(tag) ));
171 }
172
173 /* Internal function that copies a text tag */
174 GtkTextTag *
175 gtk_text_tag_copy(GtkTextTag *tag)
176 {
177         GtkTextTag *copy;
178         char *tag_name;
179         GParamSpec **properties;
180         unsigned nprops, count;
181
182         g_return_val_if_fail(tag != NULL, NULL);
183
184         g_object_get(tag, "name", &tag_name, NULL);
185         copy = gtk_text_tag_new(tag_name);
186         g_free(tag_name);
187
188         /* Copy all the original tag's properties to the new tag */
189         properties = g_object_class_list_properties( G_OBJECT_GET_CLASS(tag), &nprops );
190         for(count = 0; count < nprops; count++) {
191
192                 /* Only copy properties that are readable, writable, not construct-only,
193                 and not deprecated */
194                 GParamFlags flags = properties[count]->flags;
195                 if(flags & G_PARAM_CONSTRUCT_ONLY
196                         || flags & G_PARAM_DEPRECATED
197                         || !(flags & G_PARAM_READABLE)
198                         || !(flags & G_PARAM_WRITABLE))
199                         continue;
200
201                 const char *prop_name = g_param_spec_get_name(properties[count]);
202                 GValue prop_value = G_VALUE_INIT;
203
204                 g_value_init( &prop_value, G_PARAM_SPEC_VALUE_TYPE(properties[count]) );
205                 g_object_get_property( G_OBJECT(tag), prop_name, &prop_value );
206                 /* Don't copy the PangoTabArray if it is NULL, that prints a warning */
207                 if(strcmp(prop_name, "tabs") == 0 && g_value_get_boxed(&prop_value) == NULL) {
208                         g_value_unset(&prop_value);
209                         continue;
210                 }
211                 g_object_set_property( G_OBJECT(copy), prop_name, &prop_value );
212                 g_value_unset(&prop_value);
213         }
214         g_free(properties);
215
216         /* Copy the data that was added manually */
217         gpointer reverse_color = g_object_get_data( G_OBJECT(tag), "reverse-color" );
218
219         if(reverse_color) {
220                 g_object_set_data( G_OBJECT(copy), "reverse-color", reverse_color );
221         }
222
223         return copy;
224 }
225
226 /* Internal function that constructs the default styles */
227 void
228 style_init(ChimaraGlk *glk)
229 {
230         CHIMARA_GLK_USE_PRIVATE(glk, priv);
231         
232         GHashTable *default_text_grid_styles = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_object_unref);
233         GHashTable *default_text_buffer_styles = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_object_unref);
234         GHashTable *glk_text_grid_styles = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_object_unref);
235         GHashTable *glk_text_buffer_styles = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_object_unref);
236         GtkTextTag *tag;
237
238         /* Initialise the default styles for a text grid */
239         tag = gtk_text_tag_new("default");
240         g_object_set(tag, "family", "Monospace", "family-set", TRUE, NULL);
241         g_hash_table_insert(default_text_grid_styles, "default", tag);
242
243         tag = gtk_text_tag_new("normal");
244         g_hash_table_insert(default_text_grid_styles, "normal", tag);
245
246         tag = gtk_text_tag_new("emphasized");
247         g_object_set(tag, "style", PANGO_STYLE_ITALIC, "style-set", TRUE, NULL);
248         g_hash_table_insert(default_text_grid_styles, "emphasized", tag);
249
250         tag = gtk_text_tag_new("preformatted");
251         g_hash_table_insert(default_text_grid_styles, "preformatted", tag);
252
253         tag = gtk_text_tag_new("header");
254         g_object_set(tag, "weight", PANGO_WEIGHT_BOLD, "weight-set", TRUE, NULL);
255         g_hash_table_insert(default_text_grid_styles, "header", tag);
256
257         tag = gtk_text_tag_new("subheader");
258         g_object_set(tag, "weight", PANGO_WEIGHT_BOLD, "weight-set", TRUE, NULL);
259         g_hash_table_insert(default_text_grid_styles, "subheader", tag);
260
261         tag = gtk_text_tag_new("alert");
262         g_object_set(tag, "foreground", "#aa0000", "foreground-set", TRUE, "weight", PANGO_WEIGHT_BOLD, "weight-set", TRUE, NULL);
263         g_hash_table_insert(default_text_grid_styles, "alert", tag);
264
265         tag = gtk_text_tag_new("note");
266         g_object_set(tag, "foreground", "#aaaa00", "foreground-set", TRUE, "weight", PANGO_WEIGHT_BOLD, "weight-set", TRUE, NULL);
267         g_hash_table_insert(default_text_grid_styles, "note", tag);
268
269         tag = gtk_text_tag_new("block-quote");
270         g_object_set(tag, "style", PANGO_STYLE_ITALIC, "style-set", TRUE, NULL);
271         g_hash_table_insert(default_text_grid_styles, "block-quote", tag);
272
273         tag = gtk_text_tag_new("input");
274         g_hash_table_insert(default_text_grid_styles, "input", tag);
275
276         tag = gtk_text_tag_new("user1");
277         g_hash_table_insert(default_text_grid_styles, "user1", tag);
278
279         tag = gtk_text_tag_new("user2");
280         g_hash_table_insert(default_text_grid_styles, "user2", tag);
281
282         tag = gtk_text_tag_new("hyperlink");
283         g_object_set(tag, "foreground", "#0000ff", "foreground-set", TRUE, "underline", PANGO_UNDERLINE_SINGLE, "underline-set", TRUE, NULL);
284         g_hash_table_insert(default_text_grid_styles, "hyperlink", tag);
285
286         /* Initialise the default styles for a text buffer */
287         tag = gtk_text_tag_new("default");
288         g_object_set(tag, "family", "Serif", "family-set", TRUE, NULL);
289         g_hash_table_insert(default_text_buffer_styles, "default", tag);
290
291         tag = gtk_text_tag_new("normal");
292         g_hash_table_insert(default_text_buffer_styles, "normal", tag);
293
294         tag = gtk_text_tag_new("emphasized");
295         g_object_set(tag, "style", PANGO_STYLE_ITALIC, "style-set", TRUE, NULL);
296         g_hash_table_insert(default_text_buffer_styles, "emphasized", tag);
297
298         tag = gtk_text_tag_new("preformatted");
299         g_object_set(tag, "family", "Monospace", "family-set", TRUE, NULL);
300         g_hash_table_insert(default_text_buffer_styles, "preformatted", tag);
301
302         tag = gtk_text_tag_new("header");
303         g_object_set(tag, "weight", PANGO_WEIGHT_BOLD, "weight-set", TRUE, NULL);
304         g_hash_table_insert(default_text_buffer_styles, "header", tag);
305
306         tag = gtk_text_tag_new("subheader");
307         g_object_set(tag, "weight", PANGO_WEIGHT_BOLD, "weight-set", TRUE, NULL);
308         g_hash_table_insert(default_text_buffer_styles, "subheader", tag);
309
310         tag = gtk_text_tag_new("alert");
311         g_object_set(tag, "foreground", "#aa0000", "foreground-set", TRUE, "weight", PANGO_WEIGHT_BOLD, "weight-set", TRUE, NULL);
312         g_hash_table_insert(default_text_buffer_styles, "alert", tag);
313
314         tag = gtk_text_tag_new("note");
315         g_object_set(tag, "foreground", "#aaaa00", "foreground-set", TRUE, "weight", PANGO_WEIGHT_BOLD, "weight-set", TRUE, NULL);
316         g_hash_table_insert(default_text_buffer_styles, "note", tag);
317
318         tag = gtk_text_tag_new("block-quote");
319         g_object_set(tag, "justification", GTK_JUSTIFY_CENTER, "justification-set", TRUE, "style", PANGO_STYLE_ITALIC, "style-set", TRUE, NULL);
320         g_hash_table_insert(default_text_buffer_styles, "block-quote", tag);
321
322         tag = gtk_text_tag_new("input");
323         g_hash_table_insert(default_text_buffer_styles, "input", tag);
324
325         tag = gtk_text_tag_new("user1");
326         g_hash_table_insert(default_text_buffer_styles, "user1", tag);
327
328         tag = gtk_text_tag_new("user2");
329         g_hash_table_insert(default_text_buffer_styles, "user2", tag);
330
331         tag = gtk_text_tag_new("hyperlink");
332         g_object_set(tag, "foreground", "#0000ff", "foreground-set", TRUE, "underline", PANGO_UNDERLINE_SINGLE, "underline-set", TRUE, NULL);
333         g_hash_table_insert(default_text_buffer_styles, "hyperlink", tag);
334
335         priv->styles->text_grid = default_text_grid_styles;
336         priv->styles->text_buffer = default_text_buffer_styles;
337
338         /* Initialize the GLK styles to empty tags */
339         int i;
340         for(i=0; i<style_NUMSTYLES; i++) {
341                 tag = gtk_text_tag_new(GLK_TAG_NAMES[i]);
342                 g_hash_table_insert(glk_text_grid_styles, (gchar*) GLK_TAG_NAMES[i], tag);
343                 tag = gtk_text_tag_new(GLK_TAG_NAMES[i]);
344                 g_hash_table_insert(glk_text_buffer_styles, (gchar*) GLK_TAG_NAMES[i], tag);
345         }
346
347         priv->glk_styles->text_grid = glk_text_grid_styles;
348         priv->glk_styles->text_buffer = glk_text_buffer_styles;
349
350 }
351
352 /* Reset style tables to the library's internal defaults */
353 void
354 reset_default_styles(ChimaraGlk *glk)
355 {
356         /* TODO: write this function */
357 }
358
359 /* Create the CSS file scanner */
360 GScanner *
361 create_css_file_scanner(void)
362 {
363         GScanner *scanner = g_scanner_new(NULL);
364         scanner->config->cset_identifier_first = G_CSET_a_2_z G_CSET_A_2_Z "#";
365         scanner->config->cset_identifier_nth = G_CSET_a_2_z G_CSET_A_2_Z "-_" G_CSET_DIGITS;
366         scanner->config->symbol_2_token = TRUE;
367         scanner->config->cpair_comment_single = NULL;
368         scanner->config->scan_float = FALSE;
369         return scanner;
370 }
371
372 /* Run the scanner over the CSS file, overriding the default styles */
373 void
374 scan_css_file(GScanner *scanner, ChimaraGlk *glk)
375 {
376         while( g_scanner_peek_next_token(scanner) != G_TOKEN_EOF) {
377                 if( !style_accept_style_selector(scanner, glk) )
378                         break;
379         }
380
381         g_scanner_destroy(scanner);
382 }
383
384 /* Internal function: parses a token */
385 static gboolean
386 style_accept(GScanner *scanner, GTokenType token)
387 {
388         GTokenType next = g_scanner_get_next_token(scanner);
389         if(next != token) {
390                 g_scanner_unexp_token(scanner, token, NULL, NULL, NULL, "CSS Error", 1);
391                 return FALSE;
392         }
393         return TRUE;
394 }
395
396 /* Internal function: parses a style selector */
397 static gboolean
398 style_accept_style_selector(GScanner *scanner, ChimaraGlk *glk)
399 {
400         CHIMARA_GLK_USE_PRIVATE(glk, priv);
401
402         GtkTextTag *current_tag;
403         gchar *field;
404         GTokenType token = g_scanner_get_next_token(scanner);
405         GTokenValue value = g_scanner_cur_value(scanner);
406
407         if(
408                 token != G_TOKEN_IDENTIFIER ||
409                 ( strcmp(value.v_identifier, "buffer") && strcmp(value.v_identifier, "grid") )
410         ) {
411                 g_scanner_error(scanner, "CSS Error: buffer/grid expected");
412                 return FALSE;
413         }
414
415         field = g_strdup(value.v_identifier);
416
417         /* Parse the tag name to change */
418         if( g_scanner_peek_next_token(scanner) == '{') {
419                 style_accept(scanner, '{');
420                 if( !strcmp(field, "buffer") )
421                         current_tag = g_hash_table_lookup(priv->styles->text_buffer, "default");
422                 else
423                         current_tag = g_hash_table_lookup(priv->styles->text_grid, "default");
424         } else {
425                 if( !style_accept(scanner, '.') )
426                         return FALSE;
427
428                 token = g_scanner_get_next_token(scanner);
429                 value = g_scanner_cur_value(scanner);
430
431                 if(token != G_TOKEN_IDENTIFIER) {
432                         g_scanner_error(scanner, "CSS Error: style selector expected");
433                         return FALSE;
434                 }
435
436                 if( !strcmp(field, "buffer") )
437                         current_tag = g_hash_table_lookup(priv->styles->text_buffer, value.v_identifier);
438                 else
439                         current_tag = g_hash_table_lookup(priv->styles->text_grid, value.v_identifier);
440
441                 if(current_tag == NULL) {
442                         g_scanner_error(scanner, "CSS Error: invalid style identifier");
443                         return FALSE;
444                 }
445
446                 if( !style_accept(scanner, '{') )
447                         return FALSE;
448         }
449
450         while( g_scanner_peek_next_token(scanner) != '}') {
451                 if( !style_accept_style_hint(scanner, current_tag) )
452                         return FALSE;
453         }
454                 
455         if( !style_accept(scanner, '}') )
456                 return FALSE;
457
458         return TRUE;
459 }
460
461 /* Internal function: parses a style hint */
462 static gboolean
463 style_accept_style_hint(GScanner *scanner, GtkTextTag *current_tag)
464 {
465         GTokenType token = g_scanner_get_next_token(scanner);
466         GTokenValue value = g_scanner_cur_value(scanner);
467         gchar *hint;
468
469         if(token != G_TOKEN_IDENTIFIER) {
470                 g_scanner_error(scanner, "CSS Error: style hint expected");
471                 return FALSE;
472         }
473
474         hint = g_strdup(value.v_identifier);
475
476         if( !style_accept(scanner, ':') )
477                 return FALSE;
478
479         token = g_scanner_get_next_token(scanner);
480         value = g_scanner_cur_value(scanner);
481
482         if( !strcmp(hint, "font-family") ) {
483                 if(token != G_TOKEN_STRING) {
484                         g_scanner_error(scanner, "CSS Error: string expected");
485                         return FALSE;
486                 }
487                 g_object_set(current_tag, "family", value.v_string, "family-set", TRUE, NULL);
488         }
489         else if( !strcmp(hint, "font-weight") ) {
490                 if(token != G_TOKEN_IDENTIFIER) {
491                         g_scanner_error(scanner, "CSS Error: bold/normal expected");
492                         return FALSE;
493                 }
494
495                 if( !strcmp(value.v_identifier, "bold") )
496                         g_object_set(current_tag, "weight", PANGO_WEIGHT_BOLD, "weight-set", TRUE, NULL);
497                 else if( !strcmp(value.v_identifier, "normal") )
498                         g_object_set(current_tag, "weight", PANGO_WEIGHT_NORMAL, "weight-set", TRUE, NULL);
499                 else {
500                         g_scanner_error(scanner, "CSS Error: bold/normal expected");
501                         return FALSE;
502                 }
503         }
504         else if( !strcmp(hint, "font-style") ) {
505                 if(token != G_TOKEN_IDENTIFIER) {
506                         g_scanner_error(scanner, "CSS Error: italic/normal expected");
507                         return FALSE;
508                 }
509
510                 if( !strcmp(value.v_identifier, "italic") )
511                         g_object_set(current_tag, "style", PANGO_STYLE_ITALIC, "style-set", TRUE, NULL);
512                 else if( !strcmp(value.v_identifier, "normal") )
513                         g_object_set(current_tag, "style", PANGO_STYLE_NORMAL, "style-set", TRUE, NULL);
514                 else {
515                         g_scanner_error(scanner, "CSS Error: italic/normal expected");
516                         return FALSE;
517                 }
518         }
519         else if( !strcmp(hint, "font-size") ) {
520                 if(token == G_TOKEN_INT) 
521                         g_object_set(current_tag, "size-points", (float)value.v_int, "size-set", TRUE, NULL);
522                 else if(token == G_TOKEN_FLOAT)
523                         g_object_set(current_tag, "size-points", value.v_float, "size-set", TRUE, NULL);
524                 else {
525                         g_scanner_error(scanner, "CSS Error: integer or float expected");
526                         return FALSE;
527                 }
528         }
529         else if( !strcmp(hint, "color") ) {
530                 if(token != G_TOKEN_IDENTIFIER) {
531                         g_scanner_error(scanner, "CSS Error: hex color expected");
532                         return FALSE;
533                 }
534
535                 g_object_set(current_tag, "foreground", value.v_identifier, "foreground-set", TRUE, NULL);
536         }
537         else if( !strcmp(hint, "background-color") ) {
538                 if(token != G_TOKEN_IDENTIFIER) {
539                         g_scanner_error(scanner, "CSS Error: hex color expected");
540                         return FALSE;
541                 }
542                 g_object_set(current_tag, "background", value.v_identifier, "background-set", TRUE, NULL);
543         }
544         else if( !strcmp(hint, "text-align") ) {
545                 if(token != G_TOKEN_IDENTIFIER) {
546                         g_scanner_error(scanner, "CSS Error: left/right/center expected");
547                         return FALSE;
548                 }
549                 
550                 if( !strcmp(value.v_identifier, "left") )
551                         g_object_set(current_tag, "justification", GTK_JUSTIFY_LEFT, "justification-set", TRUE, NULL);
552                 else if( !strcmp(value.v_identifier, "right") )
553                         g_object_set(current_tag, "justification", GTK_JUSTIFY_RIGHT, "justification-set", TRUE, NULL);
554                 else if( !strcmp(value.v_identifier, "center") )
555                         g_object_set(current_tag, "justification", GTK_JUSTIFY_CENTER, "justification-set", TRUE, NULL);
556                 else {
557                         g_scanner_error(scanner, "CSS Error: left/right/center expected");
558                         return FALSE;
559                 }
560         }
561         else if( !strcmp(hint, "margin-left") ) {
562                 if(token != G_TOKEN_INT) {
563                         g_scanner_error(scanner, "CSS Error: integer expected");
564                         return FALSE;
565                 }
566                 g_object_set(current_tag, "left-margin", value.v_int, "left-margin-set", TRUE, NULL);
567         }
568         else if( !strcmp(hint, "margin-right") ) {
569                 if(token != G_TOKEN_INT) {
570                         g_scanner_error(scanner, "CSS Error: integer expected");
571                         return FALSE;
572                 }
573                 g_object_set(current_tag, "right-margin", value.v_int, "right-margin-set", TRUE, NULL);
574         }
575         else if( !strcmp(hint, "margin-top") ) {
576                 if(token != G_TOKEN_INT) {
577                         g_scanner_error(scanner, "CSS Error: integer expected");
578                         return FALSE;
579                 }
580                 g_object_set(current_tag, "pixels-above-lines", value.v_int, "pixels-above-lines-set", TRUE, NULL);
581         }
582         else if( !strcmp(hint, "margin-bottom") ) {
583                 if(token != G_TOKEN_INT) {
584                         g_scanner_error(scanner, "CSS Error: integer expected");
585                         return FALSE;
586                 }
587                 g_object_set(current_tag, "pixels-below-lines", value.v_int, "pixels-below-lines-set", TRUE, NULL);
588         }
589                 
590         else {
591                 g_scanner_error(scanner, "CSS Error: invalid style hint %s", hint);
592                 return FALSE;
593         }
594
595         if( !style_accept(scanner, ';') )
596                 return FALSE;
597
598         return TRUE;
599 }
600
601 /* Internal function: parses a glk color to a GdkColor */
602 void
603 glkcolor_to_gdkcolor(glui32 val, GdkColor *color)
604 {
605         color->red = 256 * ((val & 0xff0000) >> 16);
606         color->green = 256 * ((val & 0x00ff00) >> 8);
607         color->blue = 256 * (val & 0x0000ff);
608 }
609
610 /* Internal function: parses a glk color to a hex string */
611 gchar*
612 glkcolor_to_hex(glui32 val)
613 {
614         return g_strdup_printf("%04X%04X%04X",
615                 256 * ((val & 0xff0000) >> 16),
616                 256 * ((val & 0x00ff00) >> 8),
617                 256 * (val & 0x0000ff)
618         );
619 }
620
621 /* Internal function: parses a gdk color to a hex string */
622 gchar*
623 gdkcolor_to_hex(GdkColor *color)
624 {
625         return g_strdup_printf("%04X%04X%04X",
626                 color->red, color->green, color->blue
627         );
628 }
629
630 /* Internal function: parses a GdkColor to a glk color */
631 static glui32
632 gdkcolor_to_glkcolor(GdkColor *color)
633 {
634         g_return_val_if_fail(color != NULL, 0);
635         return (glui32) color->pixel;
636 }
637
638 /* Internal function: changes a GTK tag to correspond with the given style. */
639 static void
640 apply_stylehint_to_tag(GtkTextTag *tag, glui32 wintype, glui32 styl, glui32 hint, glsi32 val)
641 {
642         g_return_if_fail(tag != NULL);
643
644         ChimaraGlkPrivate *glk_data = g_private_get(&glk_data_key);
645         GObject *tag_object = G_OBJECT(tag);
646
647         gint reverse_color = GPOINTER_TO_INT( g_object_get_data(tag_object, "reverse-color") );
648
649         int i = 0;
650         GdkColor color;
651         switch(hint) {
652         case stylehint_Indentation:
653                 g_object_set(tag_object, "left-margin", 5*val, "left-margin-set", TRUE, NULL);
654                 g_object_set(tag_object, "right-margin", 5*val, "right-margin-set", TRUE, NULL);
655                 break;
656         
657         case stylehint_ParaIndentation:
658                 g_object_set(tag_object, "indent", 5*val, "indent-set", TRUE, NULL);
659                 break;
660
661         case stylehint_Justification:
662                 switch(val) {
663                         case stylehint_just_LeftFlush:  i = GTK_JUSTIFY_LEFT; break;
664                         case stylehint_just_LeftRight:  i = GTK_JUSTIFY_FILL; break;
665                         case stylehint_just_Centered:   i = GTK_JUSTIFY_CENTER; break;
666                         case stylehint_just_RightFlush: i = GTK_JUSTIFY_RIGHT; break;
667                         default: 
668                                 WARNING("Unknown justification");
669                                 i = GTK_JUSTIFY_LEFT;
670                 }
671                 g_object_set(tag_object, "justification", i, "justification-set", TRUE, NULL);
672                 break;
673
674         case stylehint_Weight:
675                 switch(val) {
676                         case -1: i = PANGO_WEIGHT_LIGHT; break;
677                         case  0: i = PANGO_WEIGHT_NORMAL; break;
678                         case  1: i = PANGO_WEIGHT_BOLD; break;
679                         default: WARNING("Unknown font weight");
680                 }
681                 g_object_set(tag_object, "weight", i, "weight-set", TRUE, NULL);
682                 break;
683
684         case stylehint_Size:
685                 {
686                         gdouble scale = PANGO_SCALE_MEDIUM;
687                         switch(val) {
688                                 case -3: scale = PANGO_SCALE_XX_SMALL; break;
689                                 case -2: scale = PANGO_SCALE_X_SMALL; break;
690                                 case -1: scale = PANGO_SCALE_SMALL; break;
691                                 case  0: scale = PANGO_SCALE_MEDIUM; break;
692                                 case  1: scale = PANGO_SCALE_LARGE; break;
693                                 case  2: scale = PANGO_SCALE_X_LARGE; break;
694                                 case  3: scale = PANGO_SCALE_XX_LARGE; break;
695                                 default:
696                                         /* We follow Pango's convention of having each magnification
697                                         step be a scaling of 1.2 */
698                                         scale = pow(1.2, (double)val);
699                         }
700                         g_object_set(tag_object, "scale", scale, "scale-set", TRUE, NULL);
701                 }
702                 break;
703
704         case stylehint_Oblique:
705                 g_object_set(tag_object, "style", val ? PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL, "style-set", TRUE, NULL);
706                 break;
707
708         case stylehint_Proportional:
709         {
710                 gchar *font_family;
711                 gboolean family_set;
712
713                 if(wintype != wintype_TextBuffer) {
714                         if(val)
715                                 WARNING("Style hint 'propotional' only supported on text buffers.");
716
717                         break;
718                 }
719
720                 GtkTextTag *font_tag = g_hash_table_lookup(glk_data->styles->text_buffer, val? "default" : "preformatted");
721                 g_object_get(font_tag, "family", &font_family, "family-set", &family_set, NULL);
722                 g_object_set(tag_object, "family", font_family, "family-set", family_set, NULL);
723                 g_free(font_family);
724         }
725                 break;
726
727         case stylehint_TextColor:
728                 glkcolor_to_gdkcolor(val, &color);
729
730                 if(!reverse_color)
731                         g_object_set(tag_object, "foreground-gdk", &color, "foreground-set", TRUE, NULL);
732                 else
733                         g_object_set(tag_object, "background-gdk", &color, "background-set", TRUE, NULL);
734
735                 break;
736
737         case stylehint_BackColor:
738                 glkcolor_to_gdkcolor(val, &color);
739
740                 if(!reverse_color)
741                         g_object_set(tag_object, "background-gdk", &color, "background-set", TRUE, NULL);
742                 else
743                         g_object_set(tag_object, "foreground-gdk", &color, "background-set", TRUE, NULL);
744
745                 break;
746
747         case stylehint_ReverseColor:
748         {
749                 // Determine the current colors
750                 
751                 // If all fails, use black/white
752                 // FIXME: Use system theme here
753                 GdkColor foreground, background;
754                 gdk_color_parse("black", &foreground);
755                 gdk_color_parse("white", &background);
756                 GdkColor *current_foreground = &foreground;
757                 GdkColor *current_background = &background;
758
759                 if(wintype == wintype_TextBuffer) {
760                         GtkTextTag* default_tag = g_hash_table_lookup(glk_data->styles->text_buffer, "default");
761                         GtkTextTag* base_tag = g_hash_table_lookup(glk_data->styles->text_buffer, get_tag_name(styl));
762                         style_cascade_colors(base_tag, tag, default_tag, &current_foreground, &current_background);
763                 }
764                 else if(wintype == wintype_TextGrid) {
765                         GtkTextTag* default_tag = g_hash_table_lookup(glk_data->styles->text_grid, "default");
766                         GtkTextTag* base_tag = g_hash_table_lookup(glk_data->styles->text_grid, get_tag_name(styl));
767                         style_cascade_colors(base_tag, tag, default_tag, &current_foreground, &current_background);
768                 }
769
770                 if(val) {
771                         /* Flip the fore- and background colors */
772                         GdkColor *temp = current_foreground;
773                         current_foreground = current_background;
774                         current_background = temp;
775                 }
776
777                 g_object_set(tag,
778                         "foreground-gdk", current_foreground,
779                         "foreground-set", TRUE,
780                         "background-gdk", current_background,
781                         "background-set", TRUE,
782                         NULL
783                 );
784
785                 g_object_set_data( tag_object, "reverse-color", GINT_TO_POINTER(val != 0) );
786                 break;
787         }
788
789         default:
790                 WARNING("Unknown style hint");
791         }
792 }
793
794 /* Internal function: queries a text tag for the value of a given style hint */
795 static gint
796 query_tag(GtkTextTag *tag, glui32 wintype, glui32 hint)
797 {
798         gint intval;
799         gdouble doubleval;
800         GdkColor *colval;
801
802         g_return_val_if_fail(tag != NULL, 0);
803
804         ChimaraGlkPrivate *glk_data = g_private_get(&glk_data_key);
805
806         switch(hint) {
807         case stylehint_Indentation:
808                 g_object_get(tag, "left_margin", &intval, NULL);
809                 return intval/5;
810         
811         case stylehint_ParaIndentation:
812                 g_object_get(tag, "indent", &intval, NULL);
813                 return intval/5;
814
815         case stylehint_Justification:
816                 g_object_get(tag, "justification", &intval, NULL);
817                 switch(intval) {
818                         case GTK_JUSTIFY_LEFT: return stylehint_just_LeftFlush;
819                         case GTK_JUSTIFY_FILL: return stylehint_just_LeftRight;
820                         case GTK_JUSTIFY_CENTER: return stylehint_just_Centered;
821                         case GTK_JUSTIFY_RIGHT: return stylehint_just_RightFlush;
822                         default: 
823                                 WARNING("Unknown justification");
824                                 return stylehint_just_LeftFlush;
825                 }
826
827         case stylehint_Weight:
828                 g_object_get(tag, "weight", &intval, NULL);
829                 switch(intval) {
830                         case PANGO_WEIGHT_LIGHT: return -1;
831                         case PANGO_WEIGHT_NORMAL: return 0;
832                         case PANGO_WEIGHT_BOLD: return 1;
833                         default: WARNING("Unknown font weight"); return 0;
834                 }
835
836         case stylehint_Size:
837                 g_object_get(tag, "scale", &doubleval, NULL);
838                 return (gint)round(log(doubleval) / log(1.2));
839
840         case stylehint_Oblique:
841                 g_object_get(tag, "style", &intval , NULL);
842                 return intval == PANGO_STYLE_ITALIC ? 1 : 0;
843
844         case stylehint_Proportional:
845                 /* Use pango_font_family_is_monospace()? */
846         {
847                 gchar *font_family, *query_font_family;
848                 GtkTextTag *font_tag = g_hash_table_lookup(
849                     wintype == wintype_TextBuffer? glk_data->styles->text_buffer : glk_data->styles->text_grid,
850                     "preformatted");
851                 g_object_get(font_tag, "family", &font_family, NULL);
852                 g_object_get(tag, "family", &query_font_family, NULL);
853                 gint retval = strcmp(font_family, query_font_family)? 0 : 1;
854                 g_free(font_family);
855                 g_free(query_font_family);
856                 return retval;
857         }
858
859         case stylehint_TextColor:
860                 g_object_get(tag, "foreground-gdk", &colval, NULL);
861                 return gdkcolor_to_glkcolor(colval);
862
863         case stylehint_BackColor:
864                 g_object_get(tag, "background-gdk", &colval, NULL);
865                 return gdkcolor_to_glkcolor(colval);
866
867         case stylehint_ReverseColor:
868                 return GPOINTER_TO_INT( g_object_get_data(G_OBJECT(tag), "reverse_color") );
869
870         default:
871                 WARNING("Unknown style hint");
872         }
873         
874         return 0;
875 }
876
877 /**
878  * glk_stylehint_set:
879  * @wintype: The window type to set a style hint on, or %wintype_AllTypes.
880  * @styl: The style to set a hint for.
881  * @hint: The type of style hint, one of the <code>stylehint_</code> constants.
882  * @val: The style hint. The meaning of this depends on @hint.
883  *
884  * Sets a hint about the appearance of one style for a particular type of 
885  * window. You can also set @wintype to %wintype_AllTypes, which sets a hint for 
886  * all types of window.
887  * <note><para>
888  *  There is no equivalent constant to set a hint for all styles of a single 
889  *  window type.
890  * </para></note>
891  */
892 void
893 glk_stylehint_set(glui32 wintype, glui32 styl, glui32 hint, glsi32 val)
894 {
895 #ifdef DEBUG_STYLES
896         g_printf("glk_stylehint_set(wintype=%d, styl=%d, hint=%d, val=%d)\n", wintype, styl, hint, val);
897 #endif
898
899         ChimaraGlkPrivate *glk_data = g_private_get(&glk_data_key);
900
901         GtkTextTag *to_change;
902         if(wintype == wintype_TextBuffer || wintype == wintype_AllTypes) {
903                 to_change = g_hash_table_lookup( glk_data->glk_styles->text_buffer, get_glk_tag_name(styl) );
904                 apply_stylehint_to_tag(to_change, wintype_TextBuffer, styl, hint, val);
905         }
906
907         if(wintype == wintype_TextGrid || wintype == wintype_AllTypes) {
908                 to_change = g_hash_table_lookup( glk_data->glk_styles->text_grid, get_glk_tag_name(styl) );
909                 apply_stylehint_to_tag(to_change, wintype_TextGrid, styl, hint, val);
910         }
911 }
912
913 /**
914  * glk_stylehint_clear:
915  * @wintype: The window type to set a style hint on, or %wintype_AllTypes.
916  * @styl: The style to set a hint for.
917  * @hint: The type of style hint, one of the <code>stylehint_</code> constants.
918  *
919  * Clears a hint about the appearance of one style for a particular type of 
920  * window to its default value. You can also set @wintype to %wintype_AllTypes, 
921  * which clears a hint for all types of window.
922  * <note><para>
923  *  There is no equivalent constant to reset a hint for all styles of a single 
924  *  window type.
925  * </para></note>
926  */
927 void
928 glk_stylehint_clear(glui32 wintype, glui32 styl, glui32 hint)
929 {
930 #ifdef DEBUG_STYLES
931         g_printf("glk_stylehint_clear(wintype=%d, styl=%d, hint=%d)\n", wintype, styl, hint);
932 #endif
933
934         ChimaraGlkPrivate *glk_data = g_private_get(&glk_data_key);
935         GtkTextTag *tag;
936
937         switch(wintype) {
938         case wintype_TextBuffer:
939                 tag = g_hash_table_lookup( glk_data->glk_styles->text_buffer, get_glk_tag_name(styl) );
940                 if(tag) {
941                         glk_stylehint_set( wintype, styl, hint, query_tag(tag, wintype, hint) );
942                 }
943                 break;
944         case wintype_TextGrid:
945                 tag = g_hash_table_lookup( glk_data->glk_styles->text_grid, get_glk_tag_name(styl) );
946                 if(tag) {
947                         glk_stylehint_set( wintype, styl, hint, query_tag(tag, wintype, hint) );
948                 }
949         default:
950                 return;
951         }
952 }
953
954 /**
955  * glk_style_distinguish:
956  * @win: The window in which the styles are to be distinguished.
957  * @styl1: The first style to be distinguished from the second style.
958  * @styl2: The second style to be distinguished from the first style.
959  * 
960  * Decides whether two styles are visually distinguishable in the given window.
961  * The exact meaning of this is left for the library to determine.
962  * <note><title>Chimara</title><para>
963  *   Currently, all styles of one window are assumed to be mutually
964  *   distinguishable.
965  * </para></note>
966  * 
967  * Returns: %TRUE (1) if the two styles are visually distinguishable. If they 
968  * are not, it returns %FALSE (0).
969  */
970 glui32
971 glk_style_distinguish(winid_t win, glui32 styl1, glui32 styl2)
972 {
973 #ifdef DEBUG_STYLES
974         g_printf("glk_style_distinguish(win->rock=%d, styl1=%d, styl2=%d)\n", win->rock, styl1, styl2);
975 #endif
976
977         /* FIXME */
978         return styl1 != styl2;
979 }
980
981 /**
982  * glk_style_measure:
983  * @win: The window from which to take the style.
984  * @styl: The style to perform the measurement on.
985  * @hint: The stylehint to measure.
986  * @result: Address to write the result to.
987  * 
988  * Tries to test an attribute of one style in the given window @win. The library
989  * may not be able to determine the attribute; if not, this returns %FALSE (0).
990  * If it can, it returns %TRUE (1) and stores the value in the location pointed
991  * at by @result. 
992  * <note><para>
993  *   As usual, it is legal for @result to be %NULL, although fairly pointless.
994  * </para></note>
995  *
996  * The meaning of the value depends on the hint which was tested:
997  * <variablelist>
998  * <varlistentry>
999  *   <term>%stylehint_Indentation, %stylehint_ParaIndentation</term>
1000  *   <listitem><para>The indentation and paragraph indentation. These are in a
1001  *   metric which is platform-dependent.</para>
1002  *   <note><para>Most likely either characters or pixels.</para></note>
1003  *   </listitem>
1004  * </varlistentry>
1005  * <varlistentry>
1006  *   <term>%stylehint_Justification</term>
1007  *   <listitem><para>One of the constants %stylehint_just_LeftFlush,
1008  *   %stylehint_just_LeftRight, %stylehint_just_Centered, or
1009  *   %stylehint_just_RightFlush.</para></listitem>
1010  * </varlistentry>
1011  * <varlistentry>
1012  *   <term>%stylehint_Size</term>
1013  *   <listitem><para>The font size. Again, this is in a platform-dependent
1014  *   metric.</para>
1015  *   <note><para>Pixels, points, or simply 1 if the library does not support
1016  *   varying font sizes.</para></note>
1017  *   </listitem>
1018  * </varlistentry>
1019  * <varlistentry>
1020  *   <term>%stylehint_Weight</term>
1021  *   <listitem><para>1 for heavy-weight fonts (boldface), 0 for normal weight,
1022  *   and -1 for light-weight fonts.</para></listitem>
1023  * </varlistentry>
1024  * <varlistentry>
1025  *   <term>%stylehint_Oblique</term>
1026  *   <listitem><para>1 for oblique fonts (italic), or 0 for normal angle.</para>
1027  *   </listitem>
1028  * </varlistentry>
1029  * <varlistentry>
1030  *   <term>%stylehint_Proportional</term>
1031  *   <listitem><para>1 for proportional-width fonts, or 0 for fixed-width.
1032  *   </para></listitem>
1033  * </varlistentry>
1034  * <varlistentry>
1035  *   <term>%stylehint_TextColor, %stylehint_BackColor</term>
1036  *   <listitem><para>These are values from 0x00000000 to 0x00FFFFFF, encoded as
1037  *   described in <link 
1038  *   linkend="chimara-Suggesting-the-Appearance-of-Styles">Suggesting the
1039  *   Appearance of Styles</link>.</para></listitem>
1040  * </varlistentry>
1041  * <varlistentry>
1042  *   <term>%stylehint_ReverseColor</term>
1043  *   <listitem><para>0 for normal printing, 1 if the foreground and background
1044  *   colors are reversed.</para></listitem>
1045  * </varlistentry>
1046  * </variablelist>
1047  * Signed values, such as the %stylehint_Weight value, are cast to
1048  * <type>glui32</type>. They may be cast to <type>glsi32</type> to be dealt with
1049  * in a more natural context.
1050  * 
1051  * Returns: TRUE upon successul retrieval, otherwise FALSE.
1052  */
1053 glui32
1054 glk_style_measure(winid_t win, glui32 styl, glui32 hint, glui32 *result)
1055 {
1056 #ifdef DEBUG_STYLES
1057         g_printf("glk_style_measure(win->rock=%d, styl=%d, hint=%d, result=...)\n", win->rock, styl, hint);
1058 #endif
1059
1060         ChimaraGlkPrivate *glk_data = g_private_get(&glk_data_key);
1061         GtkTextTag *tag;
1062
1063         switch(win->type) {
1064         case wintype_TextBuffer:
1065                 tag = g_hash_table_lookup( glk_data->glk_styles->text_buffer, get_glk_tag_name(styl) );
1066                 if(result)
1067                         *result = query_tag(tag, win->type, hint);
1068                 break;
1069         case wintype_TextGrid:
1070                 tag = g_hash_table_lookup( glk_data->glk_styles->text_grid, get_glk_tag_name(styl) );
1071                 if(result)
1072                         *result = query_tag(tag, win->type, hint);
1073         default:
1074                 return FALSE;
1075         }
1076
1077         return TRUE;
1078 }
1079
1080 /* Internal function returning the current default font for a window type
1081  * This can be used later for size calculations. Only wintype_TextGrid and wintype_TextBuffer are
1082  * supported for now */
1083 PangoFontDescription *
1084 get_current_font(guint32 wintype)
1085 {
1086         ChimaraGlkPrivate *glk_data = g_private_get(&glk_data_key);
1087         GHashTable *styles, *glk_styles;
1088         PangoFontDescription *font;
1089
1090         switch(wintype) {
1091         case wintype_TextGrid:
1092                 styles = glk_data->styles->text_grid;
1093                 glk_styles = glk_data->glk_styles->text_grid;
1094                 font = pango_font_description_from_string("Monospace");
1095                 break;
1096         case wintype_TextBuffer:
1097                 styles = glk_data->styles->text_buffer;
1098                 glk_styles = glk_data->glk_styles->text_buffer;
1099                 font = pango_font_description_from_string("Serif");
1100                 break;
1101         default:
1102                 return NULL;
1103         }
1104
1105         PangoAttrList *list = pango_attr_list_new();
1106
1107         text_tag_to_attr_list( g_hash_table_lookup(styles, "default"), list );
1108         PangoAttrIterator *it = pango_attr_list_get_iterator(list);
1109         pango_attr_iterator_get_font(it, font, NULL, NULL);
1110         pango_attr_iterator_destroy(it);
1111
1112         text_tag_to_attr_list( g_hash_table_lookup(styles, "normal"), list );
1113         it = pango_attr_list_get_iterator(list);
1114         pango_attr_iterator_get_font(it, font, NULL, NULL);
1115         pango_attr_iterator_destroy(it);
1116
1117         text_tag_to_attr_list( g_hash_table_lookup(glk_styles, "glk-normal"), list );
1118         it = pango_attr_list_get_iterator(list);
1119         pango_attr_iterator_get_font(it, font, NULL, NULL);
1120         pango_attr_iterator_destroy(it);
1121
1122         /* Make a copy of the family, preventing it's destruction at the end of this function. */
1123         pango_font_description_set_family( font, pango_font_description_get_family(font) );
1124
1125         pango_attr_list_unref(list);
1126
1127         return font;
1128 }
1129
1130 /* Internal function copying the attributes of a text tag to a pango attribute list */
1131 static void
1132 text_tag_to_attr_list(GtkTextTag *tag, PangoAttrList *list)
1133 {
1134         gboolean set;
1135         GdkColor *foreground, *background;
1136         gchar *string;
1137         PangoFontDescription *font_desc;
1138         gboolean strikethrough;
1139         PangoUnderline underline;
1140
1141         g_object_get(tag, "foreground-set", &set, "foreground-gdk", &foreground, NULL);
1142         if(set) {
1143                 pango_attr_list_insert(
1144                         list,
1145                         pango_attr_foreground_new(foreground->red, foreground->green, foreground->blue)
1146                 );
1147         }
1148         g_object_get(tag, "background-set", &set, "background-gdk", &background, NULL);
1149         if(set) {
1150                 pango_attr_list_insert(
1151                         list,
1152                         pango_attr_background_new(background->red, background->green, background->blue)
1153                 );
1154         }
1155         g_object_get(tag, "language-set", &set, "language", &string, NULL);
1156         if(set) {
1157                 pango_attr_list_insert(
1158                         list,
1159                         pango_attr_language_new( pango_language_from_string(string) )
1160                 );
1161         }
1162
1163         /* Font description updates the following properties simultaniously:
1164          * family, style, weight, variant, stretch, size.
1165          * FIXME: Except it doesn't really.
1166          */
1167         g_object_get(tag, "font-desc", &font_desc, NULL);
1168         pango_attr_list_insert(
1169                 list,
1170                 pango_attr_font_desc_new(font_desc)
1171         );
1172
1173         g_object_get(tag, "strikethrough-set", &set, "strikethrough", &strikethrough, NULL);
1174         if(set) {
1175                 pango_attr_list_insert(
1176                         list,
1177                         pango_attr_strikethrough_new(strikethrough)
1178                 );
1179         }
1180         g_object_get(tag, "underline-set", &set, "underline", &underline, NULL);
1181         if(set) {
1182                 pango_attr_list_insert(
1183                         list,
1184                         pango_attr_underline_new(underline)
1185                 );
1186         }
1187 }
1188
1189 /* Determine the current colors used to render the text for a given stream. 
1190  * This can be set in a number of places */
1191 static void
1192 style_cascade_colors(GtkTextTag *tag, GtkTextTag *glk_tag, GtkTextTag *default_tag, GdkColor **foreground, GdkColor **background)
1193 {
1194         gboolean foreground_set = FALSE;
1195         gboolean background_set = FALSE;
1196         gint reverse_color;
1197
1198         // Default color
1199         reverse_color = GPOINTER_TO_INT( g_object_get_data(G_OBJECT(default_tag), "reverse-color") );
1200         g_object_get(default_tag, "foreground-set", &foreground_set, "background-set", &background_set, NULL);
1201         if(foreground_set)
1202                 g_object_get(default_tag, "foreground-gdk", reverse_color ? background:foreground, NULL);
1203         if(background_set)
1204                 g_object_get(default_tag, "background-gdk", reverse_color ? foreground:background, NULL);
1205
1206         // Player defined color
1207         reverse_color = GPOINTER_TO_INT( g_object_get_data(G_OBJECT(tag), "reverse-color") );
1208         g_object_get(tag, "foreground-set", &foreground_set, "background-set", &background_set, NULL);
1209         if(foreground_set)
1210                 g_object_get(tag, "foreground-gdk", reverse_color ? background:foreground, NULL);
1211         if(background_set)
1212                 g_object_get(tag, "background-gdk", reverse_color ? foreground:background, NULL);
1213
1214         // GLK defined color
1215         reverse_color = GPOINTER_TO_INT( g_object_get_data(G_OBJECT(glk_tag), "reverse-color") );
1216         g_object_get(glk_tag, "foreground-set", &foreground_set, "background-set", &background_set, NULL);
1217         if(foreground_set)
1218                 g_object_get(glk_tag, "foreground-gdk", reverse_color ? background:foreground, NULL);
1219         if(background_set)
1220                 g_object_get(glk_tag, "background-gdk", reverse_color ? foreground:background, NULL);
1221
1222 }
1223
1224 /* Determine the current colors used to render the text for a given stream. 
1225  * This can be set in a number of places */
1226 void
1227 style_stream_colors(strid_t str, GdkColor **foreground, GdkColor **background)
1228 {
1229         VALID_STREAM(str, return);
1230         g_return_if_fail(str->window != NULL);
1231         g_return_if_fail(str->window->type != wintype_TextBuffer || str->window->type != wintype_TextGrid);
1232         
1233         GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(str->window->widget) ); 
1234         GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
1235         GtkTextTag* default_tag = gtk_text_tag_table_lookup(tags, "default");
1236         GtkTextTag* tag = gtk_text_tag_table_lookup(tags, str->style);
1237         GtkTextTag* glk_tag = gtk_text_tag_table_lookup(tags, str->glk_style);
1238
1239         style_cascade_colors(tag, glk_tag, default_tag, foreground, background);
1240
1241         gboolean foreground_set, background_set;
1242
1243         // Windows can have zcolors defined
1244         if(str->window->zcolor) {
1245                 g_object_get(str->window->zcolor, "foreground-set", &foreground_set, "background-set", &background_set, NULL);
1246                 if(foreground_set)
1247                         g_object_get(str->window->zcolor, "foreground-gdk", foreground, NULL);
1248                 if(background_set)
1249                         g_object_get(str->window->zcolor, "background-gdk", background, NULL);
1250         }
1251 }
1252
1253 /* Apply styles to a segment of text in a GtkTextBuffer, combining multiple
1254  * GtkTextTags.
1255  */
1256 void
1257 style_apply(winid_t win, GtkTextIter *start, GtkTextIter *end)
1258 {
1259         GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
1260         GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
1261
1262         GtkTextTag *default_tag = gtk_text_tag_table_lookup(tags, "default");
1263         GtkTextTag *style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->style);
1264         GtkTextTag *glk_style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->glk_style);
1265
1266         // Player's style overrides
1267         gtk_text_buffer_apply_tag(buffer, style_tag, start, end);
1268
1269         // GLK Program's style overrides
1270         gtk_text_buffer_apply_tag(buffer, glk_style_tag, start, end);
1271
1272         // Default style
1273         gtk_text_buffer_apply_tag(buffer, default_tag, start, end);
1274
1275         // Link style overrides
1276         if(win->window_stream->hyperlink_mode) {
1277                 GtkTextTag *link_style_tag = gtk_text_tag_table_lookup(tags, "hyperlink");
1278                 GtkTextTag *link_tag = win->current_hyperlink->tag;
1279                 gtk_text_buffer_apply_tag(buffer, link_style_tag, start, end);
1280                 gtk_text_buffer_apply_tag(buffer, link_tag, start, end);
1281         }
1282
1283         // GLK Program's style overrides using garglk_set_zcolors()
1284         if(win->zcolor != NULL) {
1285                 gtk_text_buffer_apply_tag(buffer, win->zcolor, start, end);
1286         }
1287
1288         // GLK Program's style overrides using garglk_set_reversevideo()
1289         if(win->zcolor_reversed != NULL) {
1290                 gtk_text_buffer_apply_tag(buffer, win->zcolor_reversed, start, end);
1291         }
1292 }
1293