Fixed freeing data at stop & start. Previously the root window was set to NULL when...
[rodin/chimara.git] / libchimara / style.c
1 #include <stdio.h>
2 #include <fcntl.h>
3 #include <string.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);
15 static gboolean style_accept_style_hint(GScanner *scanner, GtkTextTag *current_tag);
16 static void style_add_tag_to_textbuffer(gpointer key, gpointer tag, gpointer tag_table);
17 static void style_table_copy(gpointer key, gpointer tag, gpointer target_table);
18 GtkTextTag* gtk_text_tag_copy(GtkTextTag *tag);
19
20 /**
21  * glk_set_style:
22  * @styl: The style to apply
23  *
24  * Changes the style of the current output stream. @styl should be one of the
25  * <code>style_</code> constants listed above. However, any value is actually
26  * legal; if the interpreter does not recognize the style value, it will treat
27  * it as %style_Normal.
28  * <note><para>
29  *  This policy allows for the future definition of styles without breaking old
30  *  Glk libraries.
31  * </para></note>
32  */
33 void
34 glk_set_style(glui32 styl)
35 {
36         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
37         g_return_if_fail(glk_data->current_stream != NULL);
38         glk_set_style_stream(glk_data->current_stream, styl);
39 }
40
41 static const gchar* TAG_NAMES[] = {
42         "normal",
43         "emphasized",
44         "preformatted",
45         "header",
46         "subheader",
47         "alert",
48         "note",
49         "block-quote",
50         "input",
51         "user1",
52         "user2"
53 };
54
55 /* Internal function: mapping from style enum to tag name */
56 static gchar*
57 get_tag_name(glui32 style)
58 {
59         if(style >= style_NUMSTYLES) {
60                 WARNING("Unsupported style");
61                 return "normal";
62         } else {
63                 return (gchar*) TAG_NAMES[style];
64         }
65 }
66
67 /** 
68  * glk_set_style_stream:
69  * @str: Output stream to change the style of
70  * @styl: The style to apply
71  *
72  * This changes the style of the stream @str. See glk_set_style().
73  */
74 void
75 glk_set_style_stream(strid_t str, glui32 styl) {
76         if(str->window == NULL)
77                 return;
78
79         flush_window_buffer(str->window);
80         str->style = get_tag_name(styl);
81 }
82
83 /* Internal function: call this to initialize the default styles to a textbuffer. */
84 void
85 style_init_textbuffer(GtkTextBuffer *buffer)
86 {
87         g_return_if_fail(buffer != NULL);
88
89         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
90         if( G_UNLIKELY(!glk_data->style_initialized) ) {
91                 style_init();
92         }
93
94         /* Copy the current text tags to the textbuffer's tag table */
95         g_hash_table_foreach(glk_data->current_styles->text_buffer, style_add_tag_to_textbuffer, gtk_text_buffer_get_tag_table(buffer));
96 }
97
98 /* Internal function: call this to initialize the default styles to a textgrid. */
99 void
100 style_init_textgrid(GtkTextBuffer *buffer)
101 {
102         g_return_if_fail(buffer != NULL);
103         
104         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
105         if( G_UNLIKELY(!glk_data->style_initialized) ) {
106                 style_init();
107         }
108
109         /* Copy the current text tags to the textgrid's tag table */
110         g_hash_table_foreach(glk_data->current_styles->text_grid, style_add_tag_to_textbuffer, gtk_text_buffer_get_tag_table(buffer));
111 }
112
113 /* Internal function used to iterate over the default text tag table, applying them to a textbuffer */
114 static void
115 style_add_tag_to_textbuffer(gpointer key, gpointer tag, gpointer tag_table)
116 {
117         gtk_text_tag_table_add( tag_table, gtk_text_tag_copy(tag) );
118 }
119
120 /* Internal function used to iterate over a style table, copying it */
121 static void
122 style_table_copy(gpointer key, gpointer tag, gpointer target_table)
123 {
124         g_return_if_fail(key != NULL);
125         g_return_if_fail(tag != NULL);
126         g_return_if_fail(target_table != NULL);
127
128         g_hash_table_insert(target_table, key, gtk_text_tag_copy( GTK_TEXT_TAG(tag) ));
129 }
130
131 /* Internal function that copies a text tag */
132 GtkTextTag*
133 gtk_text_tag_copy(GtkTextTag *tag)
134 {
135         GtkTextTag *copy;
136
137         g_return_val_if_fail(tag != NULL, NULL);
138
139         copy = gtk_text_tag_new(tag->name);
140         gtk_text_attributes_copy_values(tag->values, copy->values);
141
142         #define _COPY_FLAG(flag) copy->flag = tag->flag
143                 _COPY_FLAG (bg_color_set);
144                 _COPY_FLAG (bg_color_set);
145                 _COPY_FLAG (bg_stipple_set);
146                 _COPY_FLAG (fg_color_set);
147                 _COPY_FLAG (fg_stipple_set);
148                 _COPY_FLAG (justification_set);
149                 _COPY_FLAG (left_margin_set);
150                 _COPY_FLAG (indent_set);
151                 _COPY_FLAG (rise_set);
152                 _COPY_FLAG (strikethrough_set);
153                 _COPY_FLAG (right_margin_set);
154                 _COPY_FLAG (pixels_above_lines_set);
155                 _COPY_FLAG (pixels_below_lines_set);
156                 _COPY_FLAG (pixels_inside_wrap_set);
157                 _COPY_FLAG (tabs_set);
158                 _COPY_FLAG (underline_set);
159                 _COPY_FLAG (wrap_mode_set);
160                 _COPY_FLAG (bg_full_height_set);
161                 _COPY_FLAG (invisible_set);
162                 _COPY_FLAG (editable_set);
163                 _COPY_FLAG (language_set);
164         #undef _COPY_FLAG
165
166         /* Copy the data that was added manually */
167         gpointer reverse_color = g_object_get_data( G_OBJECT(tag), "reverse_color" );
168
169         if(reverse_color)
170                 g_object_set_data( G_OBJECT(copy), "reverse_color", reverse_color );
171
172         return copy;
173 }
174
175 /* Internal function that reads the default styles from a CSS file */
176 void
177 style_init()
178 {
179         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
180         GHashTable *default_text_grid_styles = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_object_unref);
181         GHashTable *default_text_buffer_styles = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_object_unref);
182         GHashTable *current_text_grid_styles = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_object_unref);
183         GHashTable *current_text_buffer_styles = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_object_unref);
184         GtkTextTag *tag;
185
186         /* Initialise the default styles for a text grid */
187         tag = gtk_text_tag_new("normal");
188         g_object_set(tag, "font-desc", glk_data->monospace_font_desc, NULL);
189         g_hash_table_insert(default_text_grid_styles, "normal", tag);
190
191         tag = gtk_text_tag_new("emphasized");
192         g_object_set(tag, "style", PANGO_STYLE_ITALIC, "style-set", TRUE, NULL);
193         g_hash_table_insert(default_text_grid_styles, "emphasized", tag);
194
195         tag = gtk_text_tag_new("preformatted");
196         g_object_set(tag, "font-desc", glk_data->monospace_font_desc, NULL);
197         g_hash_table_insert(default_text_grid_styles, "preformatted", tag);
198
199         tag = gtk_text_tag_new("header");
200         g_object_set(tag, "weight", PANGO_WEIGHT_BOLD, NULL);
201         g_hash_table_insert(default_text_grid_styles, "header", tag);
202
203         tag = gtk_text_tag_new("subheader");
204         g_object_set(tag, "weight", PANGO_WEIGHT_BOLD, NULL);
205         g_hash_table_insert(default_text_grid_styles, "subheader", tag);
206
207         tag = gtk_text_tag_new("alert");
208         g_object_set(tag, "foreground", "#aa0000", "weight", PANGO_WEIGHT_BOLD, NULL);
209         g_hash_table_insert(default_text_grid_styles, "alert", tag);
210
211         tag = gtk_text_tag_new("note");
212         g_object_set(tag, "foreground", "#aaaa00", "weight", PANGO_WEIGHT_BOLD, NULL);
213         g_hash_table_insert(default_text_grid_styles, "note", tag);
214
215         tag = gtk_text_tag_new("block-quote");
216         g_object_set(tag, "style", PANGO_STYLE_ITALIC, "style-set", TRUE, NULL);
217         g_hash_table_insert(default_text_grid_styles, "block-quote", tag);
218
219         g_hash_table_insert(default_text_grid_styles, "input", gtk_text_tag_new("input"));
220         g_hash_table_insert(default_text_grid_styles, "user1", gtk_text_tag_new("user1"));
221         g_hash_table_insert(default_text_grid_styles, "user2", gtk_text_tag_new("user2"));
222
223         tag = gtk_text_tag_new("hyperlink");
224         g_object_set(tag, "foreground", "#0000ff", "underline", PANGO_UNDERLINE_SINGLE, "underline-set", TRUE, NULL);
225         g_hash_table_insert(default_text_grid_styles, "hyperlink", tag);
226
227         /* Tags for the textbuffer */
228         tag = gtk_text_tag_new("normal");
229         g_object_set(tag, "font-desc", glk_data->default_font_desc, NULL);
230         g_hash_table_insert(default_text_buffer_styles, "normal", tag);
231
232         tag = gtk_text_tag_new("emphasized");
233         g_object_set(tag, "style", PANGO_STYLE_ITALIC, "style-set", TRUE, NULL);
234         g_hash_table_insert(default_text_buffer_styles, "emphasized", tag);
235
236         tag = gtk_text_tag_new("preformatted");
237         g_object_set(tag, "font-desc", glk_data->monospace_font_desc, NULL);
238         g_hash_table_insert(default_text_buffer_styles, "preformatted", tag);
239
240         tag = gtk_text_tag_new("header");
241         g_object_set(tag, "size-points", 18.0, "weight", PANGO_WEIGHT_BOLD, NULL);
242         g_hash_table_insert(default_text_buffer_styles, "header", tag);
243
244         tag = gtk_text_tag_new("subheader");
245         g_object_set(tag, "size-points", 14.0, "weight", PANGO_WEIGHT_BOLD, NULL);
246         g_hash_table_insert(default_text_buffer_styles, "subheader", tag);
247
248         tag = gtk_text_tag_new("alert");
249         g_object_set(tag, "foreground", "#aa0000", "weight", PANGO_WEIGHT_BOLD, NULL);
250         g_hash_table_insert(default_text_buffer_styles, "alert", tag);
251
252         tag = gtk_text_tag_new("note");
253         g_object_set(tag, "foreground", "#aaaa00", "weight", PANGO_WEIGHT_BOLD, NULL);
254         g_hash_table_insert(default_text_buffer_styles, "note", tag);
255
256         tag = gtk_text_tag_new("block-quote");
257         g_object_set(tag, "justification", GTK_JUSTIFY_CENTER, "style", PANGO_STYLE_ITALIC, "style-set", TRUE, NULL);
258         g_hash_table_insert(default_text_buffer_styles, "block-quote", tag);
259
260         g_hash_table_insert(default_text_buffer_styles, "input", gtk_text_tag_new("input"));
261         g_hash_table_insert(default_text_buffer_styles, "user1", gtk_text_tag_new("user1"));
262         g_hash_table_insert(default_text_buffer_styles, "user2", gtk_text_tag_new("user2"));
263
264         tag = gtk_text_tag_new("hyperlink");
265         g_object_set(tag, "foreground", "#0000ff", "underline", PANGO_UNDERLINE_SINGLE, "underline-set", TRUE, NULL);
266         g_hash_table_insert(default_text_buffer_styles, "hyperlink", tag);
267
268         glk_data->default_styles->text_grid = default_text_grid_styles;
269         glk_data->default_styles->text_buffer = default_text_buffer_styles;
270
271         /* Create the CSS file scanner */
272         GScanner *scanner = g_scanner_new(NULL);
273
274         int f = open(glk_data->css_file, O_RDONLY);
275         if(f != -1)
276         {
277                 g_scanner_input_file(scanner, f);
278                 scanner->input_name = glk_data->css_file;
279                 scanner->config->cset_identifier_first = G_CSET_a_2_z G_CSET_A_2_Z "#";
280                 scanner->config->cset_identifier_nth = G_CSET_a_2_z G_CSET_A_2_Z "-_" G_CSET_DIGITS;
281                 scanner->config->symbol_2_token = TRUE;
282                 scanner->config->cpair_comment_single = NULL;
283                 scanner->config->scan_float = FALSE;
284
285                 /* Run the scanner over the CSS file, overriding defaults */
286                 while( g_scanner_peek_next_token(scanner) != G_TOKEN_EOF) {
287                         if( !style_accept_style_selector(scanner) )
288                                 break;
289                 }
290
291                 g_scanner_destroy(scanner);
292         }
293         else
294                 g_warning("Could not find CSS file");
295
296         /* Set the current style to a copy of the default style */
297         g_hash_table_foreach(default_text_grid_styles, style_table_copy, current_text_grid_styles);
298         g_hash_table_foreach(default_text_buffer_styles, style_table_copy, current_text_buffer_styles);
299         glk_data->current_styles->text_grid = current_text_grid_styles;
300         glk_data->current_styles->text_buffer = current_text_buffer_styles;
301
302         glk_data->style_initialized = TRUE;
303 }
304
305 /* Internal function: parses a token */
306 static gboolean
307 style_accept(GScanner *scanner, GTokenType token)
308 {
309         GTokenType next = g_scanner_get_next_token(scanner);
310         if(next != token ) {
311                 g_scanner_unexp_token(scanner, token, NULL, NULL, NULL, "CSS Error", 1);
312                 return FALSE;
313         } else {
314                 return TRUE;
315         }
316 }
317
318 /* Internal function: parses a style selector */
319 static gboolean
320 style_accept_style_selector(GScanner *scanner)
321 {
322         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
323
324         GtkTextTag *current_tag;
325         gchar *field;
326         GTokenType token = g_scanner_get_next_token(scanner);
327         GTokenValue value = g_scanner_cur_value(scanner);
328
329         if(
330                 token != G_TOKEN_IDENTIFIER ||
331                 ( strcmp(value.v_identifier, "buffer") && strcmp(value.v_identifier, "grid") )
332         ) {
333                 g_scanner_error(scanner, "CSS Error: buffer/grid expected");
334                 return FALSE;
335         }
336
337         field = g_strdup(value.v_identifier);
338
339         if( !style_accept(scanner, '.') )
340                 return FALSE;
341
342         token = g_scanner_get_next_token(scanner);
343         value = g_scanner_cur_value(scanner);
344
345         if(token != G_TOKEN_IDENTIFIER) {
346                 g_scanner_error(scanner, "CSS Error: style selector expected");
347                 return FALSE;
348         }
349
350         if( !strcmp(field, "buffer") )
351                 current_tag = g_hash_table_lookup(glk_data->default_styles->text_buffer, value.v_identifier);
352         else
353                 current_tag = g_hash_table_lookup(glk_data->default_styles->text_grid, value.v_identifier);
354
355         if(current_tag == NULL) {
356                 g_scanner_error(scanner, "CSS Error: invalid style identifier");
357                 return FALSE;
358         }
359
360         if( !style_accept(scanner, '{') )
361                 return FALSE;
362
363         while( g_scanner_peek_next_token(scanner) != '}') {
364                 if( !style_accept_style_hint(scanner, current_tag) )
365                         return FALSE;
366         }
367                 
368         if( !style_accept(scanner, '}') )
369                 return FALSE;
370
371         return TRUE;
372 }
373
374 /* Internal function: parses a style hint */
375 static gboolean
376 style_accept_style_hint(GScanner *scanner, GtkTextTag *current_tag)
377 {
378         GTokenType token = g_scanner_get_next_token(scanner);
379         GTokenValue value = g_scanner_cur_value(scanner);
380         gchar *hint;
381
382         if(token != G_TOKEN_IDENTIFIER) {
383                 g_scanner_error(scanner, "CSS Error: style hint expected");
384                 return FALSE;
385         }
386
387         hint = g_strdup(value.v_identifier);
388
389         if( !style_accept(scanner, ':') )
390                 return FALSE;
391
392         token = g_scanner_get_next_token(scanner);
393         value = g_scanner_cur_value(scanner);
394
395         if( !strcmp(hint, "font-family") ) {
396                 if(token != G_TOKEN_STRING) {
397                         g_scanner_error(scanner, "CSS Error: string expected");
398                         return FALSE;
399                 }
400                 g_object_set(current_tag, "family", value.v_string, "family-set", TRUE, NULL);
401         }
402         else if( !strcmp(hint, "font-weight") ) {
403                 if(token != G_TOKEN_IDENTIFIER) {
404                         g_scanner_error(scanner, "CSS Error: bold/normal expected");
405                         return FALSE;
406                 }
407
408                 if( !strcmp(value.v_identifier, "bold") )
409                         g_object_set(current_tag, "weight", PANGO_WEIGHT_BOLD, "weight-set", TRUE, NULL);
410                 else if( !strcmp(value.v_identifier, "normal") )
411                         g_object_set(current_tag, "weight", PANGO_WEIGHT_NORMAL, "weight-set", TRUE, NULL);
412                 else {
413                         g_scanner_error(scanner, "CSS Error: bold/normal expected");
414                         return FALSE;
415                 }
416         }
417         else if( !strcmp(hint, "font-style") ) {
418                 if(token != G_TOKEN_IDENTIFIER) {
419                         g_scanner_error(scanner, "CSS Error: italic/normal expected");
420                         return FALSE;
421                 }
422
423                 if( !strcmp(value.v_identifier, "italic") )
424                         g_object_set(current_tag, "style", PANGO_STYLE_ITALIC, "style-set", TRUE, NULL);
425                 else if( !strcmp(value.v_identifier, "normal") )
426                         g_object_set(current_tag, "style", PANGO_STYLE_NORMAL, "style-set", TRUE, NULL);
427                 else {
428                         g_scanner_error(scanner, "CSS Error: italic/normal expected");
429                         return FALSE;
430                 }
431         }
432         else if( !strcmp(hint, "font-size") ) {
433                 if(token == G_TOKEN_INT) 
434                         g_object_set(current_tag, "size-points", (float)value.v_int, "size-set", TRUE, NULL);
435                 else if(token == G_TOKEN_FLOAT)
436                         g_object_set(current_tag, "size-points", value.v_float, "size-set", TRUE, NULL);
437                 else {
438                         g_scanner_error(scanner, "CSS Error: integer or float expected");
439                         return FALSE;
440                 }
441         }
442         else if( !strcmp(hint, "color") ) {
443                 if(token != G_TOKEN_IDENTIFIER) {
444                         g_scanner_error(scanner, "CSS Error: hex color expected");
445                         return FALSE;
446                 }
447
448                 g_object_set(current_tag, "foreground", value.v_identifier, "foreground-set", TRUE, NULL);
449         }
450         else if( !strcmp(hint, "background-color") ) {
451                 if(token != G_TOKEN_IDENTIFIER) {
452                         g_scanner_error(scanner, "CSS Error: hex color expected");
453                         return FALSE;
454                 }
455                 g_object_set(current_tag, "background", value.v_identifier, "background-set", TRUE, NULL);
456         }
457         else if( !strcmp(hint, "text-align") ) {
458                 if(token != G_TOKEN_IDENTIFIER) {
459                         g_scanner_error(scanner, "CSS Error: left/right/center expected");
460                         return FALSE;
461                 }
462                 
463                 if( !strcmp(value.v_identifier, "left") )
464                         g_object_set(current_tag, "justification", GTK_JUSTIFY_LEFT, "justification-set", TRUE, NULL);
465                 else if( !strcmp(value.v_identifier, "right") )
466                         g_object_set(current_tag, "justification", GTK_JUSTIFY_RIGHT, "justification-set", TRUE, NULL);
467                 else if( !strcmp(value.v_identifier, "center") )
468                         g_object_set(current_tag, "justification", GTK_JUSTIFY_CENTER, "justification-set", TRUE, NULL);
469                 else {
470                         g_scanner_error(scanner, "CSS Error: left/right/center expected");
471                         return FALSE;
472                 }
473         }
474         else if( !strcmp(hint, "margin-left") ) {
475                 if(token != G_TOKEN_INT) {
476                         g_scanner_error(scanner, "CSS Error: integer expected");
477                         return FALSE;
478                 }
479                 g_object_set(current_tag, "left-margin", value.v_int, "left-margin-set", TRUE, NULL);
480         }
481         else if( !strcmp(hint, "margin-right") ) {
482                 if(token != G_TOKEN_INT) {
483                         g_scanner_error(scanner, "CSS Error: integer expected");
484                         return FALSE;
485                 }
486                 g_object_set(current_tag, "right-margin", value.v_int, "right-margin-set", TRUE, NULL);
487         }
488         else if( !strcmp(hint, "margin-top") ) {
489                 if(token != G_TOKEN_INT) {
490                         g_scanner_error(scanner, "CSS Error: integer expected");
491                         return FALSE;
492                 }
493                 g_object_set(current_tag, "pixels-above-lines", value.v_int, "pixels-above-lines-set", TRUE, NULL);
494         }
495         else if( !strcmp(hint, "margin-bottom") ) {
496                 if(token != G_TOKEN_INT) {
497                         g_scanner_error(scanner, "CSS Error: integer expected");
498                         return FALSE;
499                 }
500                 g_object_set(current_tag, "pixels-below-lines", value.v_int, "pixels-below-lines-set", TRUE, NULL);
501         }
502                 
503         else {
504                 g_scanner_error(scanner, "CSS Error: invalid style hint %s", hint);
505                 return FALSE;
506         }
507
508         if( !style_accept(scanner, ';') )
509                 return FALSE;
510
511         return TRUE;
512 }
513
514 /* Internal function: parses a glk color to a #hex-value */
515 static void
516 color_format(glui32 val, gchar *buffer)
517 {
518         g_return_if_fail(buffer != NULL);
519
520         sprintf(buffer, "#%02X%02X%02X",
521                 ((val & 0xff0000) >> 16),
522                 ((val & 0x00ff00) >> 8),
523                 (val & 0x0000ff)
524         );
525 }
526
527 /* Internal function: parses a GdkColor to a glk color */
528 static glui32
529 color_parse_gdk(GdkColor *color)
530 {
531         g_return_val_if_fail(color != NULL, 0);
532         return (glui32) color->pixel;
533 }
534
535 /* Internal function: changes a GTK tag to correspond with the given style. */
536 static void
537 apply_stylehint_to_tag(GtkTextTag *tag, glui32 hint, glsi32 val)
538 {
539         g_return_if_fail(tag != NULL);
540
541         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
542         GObject *tag_object = G_OBJECT(tag);
543
544         gint reverse_color = GPOINTER_TO_INT( g_object_get_data(tag_object, "reverse-color") );
545
546         int i = 0;
547         gchar color[20];
548         switch(hint) {
549         case stylehint_Indentation:
550                 g_object_set(tag_object, "left-margin", 5*val, "left-margin-set", TRUE, NULL);
551                 g_object_set(tag_object, "right-margin", 5*val, "right-margin-set", TRUE, NULL);
552                 break;
553         
554         case stylehint_ParaIndentation:
555                 g_object_set(tag_object, "indent", 5*val, "indent-set", TRUE, NULL);
556                 break;
557
558         case stylehint_Justification:
559                 switch(val) {
560                         case stylehint_just_LeftFlush:  i = GTK_JUSTIFY_LEFT; break;
561                         case stylehint_just_LeftRight:  i = GTK_JUSTIFY_FILL; break;
562                         case stylehint_just_Centered:   i = GTK_JUSTIFY_CENTER; break;
563                         case stylehint_just_RightFlush: i = GTK_JUSTIFY_RIGHT; break;
564                         default: 
565                                 WARNING("Unknown justification");
566                                 i = GTK_JUSTIFY_LEFT;
567                 }
568                 g_object_set(tag_object, "justification", i, "justification-set", TRUE, NULL);
569                 break;
570
571         case stylehint_Weight:
572                 switch(val) {
573                         case -1: i = PANGO_WEIGHT_LIGHT; break;
574                         case  0: i = PANGO_WEIGHT_NORMAL; break;
575                         case  1: i = PANGO_WEIGHT_BOLD; break;
576                         default: WARNING("Unknown font weight");
577                 }
578                 g_object_set(tag_object, "weight", i, "weight-set", TRUE, NULL);
579                 break;
580
581         case stylehint_Size:
582                 g_object_set(tag_object, "size", 14+(2*val), "size-set", TRUE, NULL);
583                 break;
584
585         case stylehint_Oblique:
586                 g_object_set(tag_object, "style", val ? PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL, "style-set", TRUE, NULL);
587                 break;
588
589         case stylehint_Proportional:
590                 g_object_set(tag_object, "font-desc", val ? glk_data->default_font_desc : glk_data->monospace_font_desc, NULL);
591                 break;
592
593         case stylehint_TextColor:
594                 color_format(val, color);
595
596                 if(!reverse_color)
597                         g_object_set(tag_object, "foreground", color, "foreground-set", TRUE, NULL);
598                 else
599                         g_object_set(tag_object, "background", color, "background-set", TRUE, NULL);
600
601                 break;
602
603         case stylehint_BackColor:
604                 color_format(val, color);
605
606                 if(!reverse_color)
607                         g_object_set(tag_object, "background", color, "background-set", TRUE, NULL);
608                 else
609                         g_object_set(tag_object, "foreground", color, "background-set", TRUE, NULL);
610
611                 break;
612
613         case stylehint_ReverseColor:
614                 if(reverse_color != val) {
615                         /* Flip the fore- and background colors */
616                         GdkColor* foreground_color;
617                         GdkColor* background_color;
618                         gint f_set, b_set = 0;
619                         g_object_get(tag_object, "foreground-set", &f_set, "background-set", &b_set, NULL);
620
621                         if(f_set)
622                                 g_object_get(tag_object, "foreground-gdk", &foreground_color, NULL);
623                         if(b_set)
624                                 g_object_get(tag_object, "background-gdk", &background_color, NULL);
625
626                         if(b_set)
627                                 g_object_set(tag_object, "foreground-gdk", background_color, NULL);
628                         else
629                                 g_object_set(tag_object, "foreground", "#ffffff", NULL);
630
631                         if(f_set)
632                                 g_object_set(tag_object, "background-gdk", foreground_color, NULL);
633                         else
634                                 g_object_set(tag_object, "background", "#000000", NULL);
635
636                         g_object_set_data( tag_object, "reverse-color", GINT_TO_POINTER(val != 0) );
637                 }
638                 break;
639
640         default:
641                 WARNING("Unknown style hint");
642         }
643 }
644 /*Internal function: queries a text tag for the value of a given style hint */
645 static gint
646 query_tag(GtkTextTag *tag, glui32 hint)
647 {
648         gint intval;
649         GObject *objval;
650         GdkColor *colval;
651
652         g_return_val_if_fail(tag != NULL, 0);
653
654         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
655
656         switch(hint) {
657         case stylehint_Indentation:
658                 g_object_get(tag, "left_margin", &intval, NULL);
659                 return intval/5;
660                 break;
661         
662         case stylehint_ParaIndentation:
663                 g_object_get(tag, "indent", &intval, NULL);
664                 return intval/5;
665                 break;
666
667         case stylehint_Justification:
668                 g_object_get(tag, "justification", &intval, NULL);
669                 switch(intval) {
670                         case GTK_JUSTIFY_LEFT: return stylehint_just_LeftFlush; break;
671                         case GTK_JUSTIFY_FILL: return stylehint_just_LeftRight; break;
672                         case GTK_JUSTIFY_CENTER: return stylehint_just_Centered; break;
673                         case GTK_JUSTIFY_RIGHT: return stylehint_just_RightFlush; break;
674                         default: 
675                                 WARNING("Unknown justification");
676                                 return stylehint_just_LeftFlush;
677                 }
678                 break;
679
680         case stylehint_Weight:
681                 g_object_get(tag, "weight", &intval, NULL);
682                 switch(intval) {
683                         case PANGO_WEIGHT_LIGHT: return -1; break;
684                         case PANGO_WEIGHT_NORMAL: return 0; break;
685                         case PANGO_WEIGHT_BOLD: return 1; break;
686                         default: WARNING("Unknown font weight"); return 0;
687                 }
688                 break;
689
690         case stylehint_Size:
691                 g_object_get(tag, "size", &intval, NULL);
692                 return (intval/2)-14;
693                 break;
694
695         case stylehint_Oblique:
696                 g_object_get(tag, "style", &intval , NULL);
697                 return intval == PANGO_STYLE_ITALIC ? 1 : 0;
698                 break;
699
700         case stylehint_Proportional:
701                 g_object_get(tag, "font-desc", &objval, NULL);
702                 return objval == (GObject *)glk_data->monospace_font_desc ? 0 : 1;
703                 break;
704
705         case stylehint_TextColor:
706                 g_object_get(tag, "foreground-gdk", &colval, NULL);
707                 return color_parse_gdk(colval);
708                 break;
709
710         case stylehint_BackColor:
711                 g_object_get(tag, "background-gdk", &colval, NULL);
712                 return color_parse_gdk(colval);
713                 break;
714
715         case stylehint_ReverseColor:
716                 return GPOINTER_TO_INT( g_object_get_data(G_OBJECT(tag), "reverse_color") );
717                 break;
718
719         default:
720                 WARNING("Unknown style hint");
721         }
722         
723         return 0;
724 }
725
726 /**
727  * glk_stylehint_set:
728  * @wintype: The window type to set a style hint on, or %wintype_AllTypes.
729  * @styl: The style to set a hint for.
730  * @hint: The type of style hint, one of the <code>stylehint_</code> constants.
731  * @val: The style hint. The meaning of this depends on @hint.
732  *
733  * Sets a hint about the appearance of one style for a particular type of 
734  * window. You can also set wintype to %wintype_AllTypes, which sets a hint for 
735  * all types of window.
736  * <note><para>
737  *  There is no equivalent constant to set a hint for all styles of a single 
738  *  window type.
739  * </para></note>
740  */
741 void
742 glk_stylehint_set(glui32 wintype, glui32 styl, glui32 hint, glsi32 val)
743 {
744         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
745
746         if( G_UNLIKELY(!glk_data->style_initialized) ) {
747                 style_init();
748         }
749
750         GtkTextTag *to_change;
751         if(wintype == wintype_TextBuffer || wintype == wintype_AllTypes) {
752                 to_change = g_hash_table_lookup( glk_data->current_styles->text_buffer, get_tag_name(styl) );
753                 apply_stylehint_to_tag(to_change, hint, val);
754         }
755
756         if(wintype == wintype_TextGrid || wintype == wintype_AllTypes) {
757                 to_change = g_hash_table_lookup( glk_data->current_styles->text_grid, get_tag_name(styl) );
758                 apply_stylehint_to_tag(to_change, hint, val);
759         }
760 }
761
762 /**
763  * glk_stylehint_clear:
764  * @wintype: The window type to set a style hint on, or %wintype_AllTypes.
765  * @styl: The style to set a hint for.
766  * @hint: The type of style hint, one of the <code>stylehint_</code> constants.
767  *
768  * Resets a hint about the appearance of one style for a particular type of 
769  * window to it's default value. You can also set wintype to %wintype_AllTypes, which resets a hint for 
770  * all types of window.
771  * <note><para>
772  *  There is no equivalent constant to reset a hint for all styles of a single 
773  *  window type.
774  * </para></note>
775  */
776 void
777 glk_stylehint_clear(glui32 wintype, glui32 styl, glui32 hint)
778 {
779         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
780         GtkTextTag *tag;
781
782         switch(wintype) {
783         case wintype_TextBuffer:
784                 tag = g_hash_table_lookup( glk_data->default_styles->text_buffer, get_tag_name(styl) );
785                 glk_stylehint_set( wintype, styl, hint, query_tag(tag, hint) );
786                 break;
787         case wintype_TextGrid:
788                 tag = g_hash_table_lookup( glk_data->default_styles->text_grid, get_tag_name(styl) );
789                 glk_stylehint_set( wintype, styl, hint, query_tag(tag, hint) );
790         default:
791                 return;
792         }
793 }
794
795 /**
796  * glk_style_distinguish:
797  * @win: The window in which the styles are to be distinguished.
798  * @styl1: The first style to be distinguished from the second style.
799  * @styl2: The second styel to be distinguished from the first style.
800  * 
801  * Returns: TRUE if the two styles are visually distinguishable in the given window.
802  * If they are not, it returns FALSE.
803  */
804 glui32
805 glk_style_distinguish(winid_t win, glui32 styl1, glui32 styl2)
806 {
807         return styl1 != styl2;
808 }
809
810 /**
811  * glk_style_measure:
812  * @win: The window from which to take the style.
813  * @styl: The style to perform the measurement on.
814  * @hint: The stylehint to measure.
815  * @result: Address to write the result to.
816  * 
817  * This function can be used to query the current value of a particular style hint.
818  * Returns: TRUE upon successul retrievel, otherwise FALSE.
819  */
820 glui32
821 glk_style_measure(winid_t win, glui32 styl, glui32 hint, glui32 *result)
822 {
823         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
824         GtkTextTag *tag;
825
826         switch(win->type) {
827         case wintype_TextBuffer:
828                 tag = g_hash_table_lookup( glk_data->current_styles->text_buffer, get_tag_name(styl) );
829                 *result = query_tag(tag, hint);
830                 break;
831         case wintype_TextGrid:
832                 tag = g_hash_table_lookup( glk_data->current_styles->text_grid, get_tag_name(styl) );
833                 *result = query_tag(tag, hint);
834         default:
835                 return FALSE;
836         }
837
838         return TRUE;
839 }
840
841 /* Internal function returning the current default font for a window type
842  * This can be used later for size calculations. Only wintype_TextGrid and wintype_TextBuffer are
843  * supported for now */
844 PangoFontDescription*
845 get_current_font(guint32 wintype)
846 {
847         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
848         GtkTextTag *normal;
849
850         if( G_UNLIKELY(!glk_data->style_initialized) ) {
851                 style_init();
852         }
853
854         switch(wintype) {
855         case wintype_TextGrid:
856                 normal = g_hash_table_lookup(glk_data->current_styles->text_grid, "normal");
857                 break;
858         case wintype_TextBuffer:
859                 normal = g_hash_table_lookup(glk_data->current_styles->text_buffer, "normal");
860                 break;
861         default:
862                 return NULL;
863         }
864
865         PangoFontDescription *font;
866         g_object_get( G_OBJECT(normal), "font-desc", &font, NULL );
867
868         return font;
869 }