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