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