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