Pager in place
[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         int f = open(glk_data->css_file, O_RDONLY);
291         if(f != -1)
292         {
293                 g_scanner_input_file(scanner, f);
294                 scanner->input_name = glk_data->css_file;
295                 scanner->config->cset_identifier_first = G_CSET_a_2_z G_CSET_A_2_Z "#";
296                 scanner->config->cset_identifier_nth = G_CSET_a_2_z G_CSET_A_2_Z "-_" G_CSET_DIGITS;
297                 scanner->config->symbol_2_token = TRUE;
298                 scanner->config->cpair_comment_single = NULL;
299                 scanner->config->scan_float = FALSE;
300
301                 /* Run the scanner over the CSS file, overriding defaults */
302                 while( g_scanner_peek_next_token(scanner) != G_TOKEN_EOF) {
303                         if( !style_accept_style_selector(scanner) )
304                                 break;
305                 }
306
307                 g_scanner_destroy(scanner);
308         }
309         else
310                 g_warning("Could not find CSS file");
311
312         /* Set the current style to a copy of the default style */
313         g_hash_table_foreach(default_text_grid_styles, style_table_copy, current_text_grid_styles);
314         g_hash_table_foreach(default_text_buffer_styles, style_table_copy, current_text_buffer_styles);
315         glk_data->current_styles->text_grid = current_text_grid_styles;
316         glk_data->current_styles->text_buffer = current_text_buffer_styles;
317
318         text_tag_to_attr_list(pager_tag, glk_data->pager_attr_list);
319
320         glk_data->style_initialized = TRUE;
321 }
322
323 /* Internal function: parses a token */
324 static gboolean
325 style_accept(GScanner *scanner, GTokenType token)
326 {
327         GTokenType next = g_scanner_get_next_token(scanner);
328         if(next != token ) {
329                 g_scanner_unexp_token(scanner, token, NULL, NULL, NULL, "CSS Error", 1);
330                 return FALSE;
331         } else {
332                 return TRUE;
333         }
334 }
335
336 /* Internal function: parses a style selector */
337 static gboolean
338 style_accept_style_selector(GScanner *scanner)
339 {
340         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
341
342         GtkTextTag *current_tag;
343         gchar *field;
344         GTokenType token = g_scanner_get_next_token(scanner);
345         GTokenValue value = g_scanner_cur_value(scanner);
346
347         if(
348                 token != G_TOKEN_IDENTIFIER ||
349                 ( strcmp(value.v_identifier, "buffer") && strcmp(value.v_identifier, "grid") )
350         ) {
351                 g_scanner_error(scanner, "CSS Error: buffer/grid expected");
352                 return FALSE;
353         }
354
355         field = g_strdup(value.v_identifier);
356
357         if( !style_accept(scanner, '.') )
358                 return FALSE;
359
360         token = g_scanner_get_next_token(scanner);
361         value = g_scanner_cur_value(scanner);
362
363         if(token != G_TOKEN_IDENTIFIER) {
364                 g_scanner_error(scanner, "CSS Error: style selector expected");
365                 return FALSE;
366         }
367
368         if( !strcmp(field, "buffer") )
369                 current_tag = g_hash_table_lookup(glk_data->default_styles->text_buffer, value.v_identifier);
370         else
371                 current_tag = g_hash_table_lookup(glk_data->default_styles->text_grid, value.v_identifier);
372
373         if(current_tag == NULL) {
374                 g_scanner_error(scanner, "CSS Error: invalid style identifier");
375                 return FALSE;
376         }
377
378         if( !style_accept(scanner, '{') )
379                 return FALSE;
380
381         while( g_scanner_peek_next_token(scanner) != '}') {
382                 if( !style_accept_style_hint(scanner, current_tag) )
383                         return FALSE;
384         }
385                 
386         if( !style_accept(scanner, '}') )
387                 return FALSE;
388
389         return TRUE;
390 }
391
392 /* Internal function: parses a style hint */
393 static gboolean
394 style_accept_style_hint(GScanner *scanner, GtkTextTag *current_tag)
395 {
396         GTokenType token = g_scanner_get_next_token(scanner);
397         GTokenValue value = g_scanner_cur_value(scanner);
398         gchar *hint;
399
400         if(token != G_TOKEN_IDENTIFIER) {
401                 g_scanner_error(scanner, "CSS Error: style hint expected");
402                 return FALSE;
403         }
404
405         hint = g_strdup(value.v_identifier);
406
407         if( !style_accept(scanner, ':') )
408                 return FALSE;
409
410         token = g_scanner_get_next_token(scanner);
411         value = g_scanner_cur_value(scanner);
412
413         if( !strcmp(hint, "font-family") ) {
414                 if(token != G_TOKEN_STRING) {
415                         g_scanner_error(scanner, "CSS Error: string expected");
416                         return FALSE;
417                 }
418                 g_object_set(current_tag, "family", value.v_string, "family-set", TRUE, NULL);
419         }
420         else if( !strcmp(hint, "font-weight") ) {
421                 if(token != G_TOKEN_IDENTIFIER) {
422                         g_scanner_error(scanner, "CSS Error: bold/normal expected");
423                         return FALSE;
424                 }
425
426                 if( !strcmp(value.v_identifier, "bold") )
427                         g_object_set(current_tag, "weight", PANGO_WEIGHT_BOLD, "weight-set", TRUE, NULL);
428                 else if( !strcmp(value.v_identifier, "normal") )
429                         g_object_set(current_tag, "weight", PANGO_WEIGHT_NORMAL, "weight-set", TRUE, NULL);
430                 else {
431                         g_scanner_error(scanner, "CSS Error: bold/normal expected");
432                         return FALSE;
433                 }
434         }
435         else if( !strcmp(hint, "font-style") ) {
436                 if(token != G_TOKEN_IDENTIFIER) {
437                         g_scanner_error(scanner, "CSS Error: italic/normal expected");
438                         return FALSE;
439                 }
440
441                 if( !strcmp(value.v_identifier, "italic") )
442                         g_object_set(current_tag, "style", PANGO_STYLE_ITALIC, "style-set", TRUE, NULL);
443                 else if( !strcmp(value.v_identifier, "normal") )
444                         g_object_set(current_tag, "style", PANGO_STYLE_NORMAL, "style-set", TRUE, NULL);
445                 else {
446                         g_scanner_error(scanner, "CSS Error: italic/normal expected");
447                         return FALSE;
448                 }
449         }
450         else if( !strcmp(hint, "font-size") ) {
451                 if(token == G_TOKEN_INT) 
452                         g_object_set(current_tag, "size-points", (float)value.v_int, "size-set", TRUE, NULL);
453                 else if(token == G_TOKEN_FLOAT)
454                         g_object_set(current_tag, "size-points", value.v_float, "size-set", TRUE, NULL);
455                 else {
456                         g_scanner_error(scanner, "CSS Error: integer or float expected");
457                         return FALSE;
458                 }
459         }
460         else if( !strcmp(hint, "color") ) {
461                 if(token != G_TOKEN_IDENTIFIER) {
462                         g_scanner_error(scanner, "CSS Error: hex color expected");
463                         return FALSE;
464                 }
465
466                 g_object_set(current_tag, "foreground", value.v_identifier, "foreground-set", TRUE, NULL);
467         }
468         else if( !strcmp(hint, "background-color") ) {
469                 if(token != G_TOKEN_IDENTIFIER) {
470                         g_scanner_error(scanner, "CSS Error: hex color expected");
471                         return FALSE;
472                 }
473                 g_object_set(current_tag, "background", value.v_identifier, "background-set", TRUE, NULL);
474         }
475         else if( !strcmp(hint, "text-align") ) {
476                 if(token != G_TOKEN_IDENTIFIER) {
477                         g_scanner_error(scanner, "CSS Error: left/right/center expected");
478                         return FALSE;
479                 }
480                 
481                 if( !strcmp(value.v_identifier, "left") )
482                         g_object_set(current_tag, "justification", GTK_JUSTIFY_LEFT, "justification-set", TRUE, NULL);
483                 else if( !strcmp(value.v_identifier, "right") )
484                         g_object_set(current_tag, "justification", GTK_JUSTIFY_RIGHT, "justification-set", TRUE, NULL);
485                 else if( !strcmp(value.v_identifier, "center") )
486                         g_object_set(current_tag, "justification", GTK_JUSTIFY_CENTER, "justification-set", TRUE, NULL);
487                 else {
488                         g_scanner_error(scanner, "CSS Error: left/right/center expected");
489                         return FALSE;
490                 }
491         }
492         else if( !strcmp(hint, "margin-left") ) {
493                 if(token != G_TOKEN_INT) {
494                         g_scanner_error(scanner, "CSS Error: integer expected");
495                         return FALSE;
496                 }
497                 g_object_set(current_tag, "left-margin", value.v_int, "left-margin-set", TRUE, NULL);
498         }
499         else if( !strcmp(hint, "margin-right") ) {
500                 if(token != G_TOKEN_INT) {
501                         g_scanner_error(scanner, "CSS Error: integer expected");
502                         return FALSE;
503                 }
504                 g_object_set(current_tag, "right-margin", value.v_int, "right-margin-set", TRUE, NULL);
505         }
506         else if( !strcmp(hint, "margin-top") ) {
507                 if(token != G_TOKEN_INT) {
508                         g_scanner_error(scanner, "CSS Error: integer expected");
509                         return FALSE;
510                 }
511                 g_object_set(current_tag, "pixels-above-lines", value.v_int, "pixels-above-lines-set", TRUE, NULL);
512         }
513         else if( !strcmp(hint, "margin-bottom") ) {
514                 if(token != G_TOKEN_INT) {
515                         g_scanner_error(scanner, "CSS Error: integer expected");
516                         return FALSE;
517                 }
518                 g_object_set(current_tag, "pixels-below-lines", value.v_int, "pixels-below-lines-set", TRUE, NULL);
519         }
520                 
521         else {
522                 g_scanner_error(scanner, "CSS Error: invalid style hint %s", hint);
523                 return FALSE;
524         }
525
526         if( !style_accept(scanner, ';') )
527                 return FALSE;
528
529         return TRUE;
530 }
531
532 /* Internal function: parses a glk color to a #hex-value */
533 static void
534 glkcolor_to_hex(glui32 val, gchar *buffer)
535 {
536         g_return_if_fail(buffer != NULL);
537
538         sprintf(buffer, "#%02X%02X%02X",
539                 ((val & 0xff0000) >> 16),
540                 ((val & 0x00ff00) >> 8),
541                 (val & 0x0000ff)
542         );
543 }
544
545 /* Internal function: parses a glk color to a GdkColor */
546 void
547 glkcolor_to_gdkcolor(glui32 val, GdkColor *color)
548 {
549         color->red = 256 * ((val & 0xff0000) >> 16);
550         color->green = 256 * ((val & 0x00ff00) >> 8);
551         color->blue = 256 * (val & 0x0000ff);
552 }
553
554 /* Internal function: parses a GdkColor to a glk color */
555 static glui32
556 gdkcolor_to_glkcolor(GdkColor *color)
557 {
558         g_return_val_if_fail(color != NULL, 0);
559         return (glui32) color->pixel;
560 }
561
562 /* Internal function: changes a GTK tag to correspond with the given style. */
563 static void
564 apply_stylehint_to_tag(GtkTextTag *tag, glui32 hint, glsi32 val)
565 {
566         g_return_if_fail(tag != NULL);
567
568         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
569         GObject *tag_object = G_OBJECT(tag);
570
571         gint reverse_color = GPOINTER_TO_INT( g_object_get_data(tag_object, "reverse-color") );
572
573         int i = 0;
574         gchar color[20];
575         switch(hint) {
576         case stylehint_Indentation:
577                 g_object_set(tag_object, "left-margin", 5*val, "left-margin-set", TRUE, NULL);
578                 g_object_set(tag_object, "right-margin", 5*val, "right-margin-set", TRUE, NULL);
579                 break;
580         
581         case stylehint_ParaIndentation:
582                 g_object_set(tag_object, "indent", 5*val, "indent-set", TRUE, NULL);
583                 break;
584
585         case stylehint_Justification:
586                 switch(val) {
587                         case stylehint_just_LeftFlush:  i = GTK_JUSTIFY_LEFT; break;
588                         case stylehint_just_LeftRight:  i = GTK_JUSTIFY_FILL; break;
589                         case stylehint_just_Centered:   i = GTK_JUSTIFY_CENTER; break;
590                         case stylehint_just_RightFlush: i = GTK_JUSTIFY_RIGHT; break;
591                         default: 
592                                 WARNING("Unknown justification");
593                                 i = GTK_JUSTIFY_LEFT;
594                 }
595                 g_object_set(tag_object, "justification", i, "justification-set", TRUE, NULL);
596                 break;
597
598         case stylehint_Weight:
599                 switch(val) {
600                         case -1: i = PANGO_WEIGHT_LIGHT; break;
601                         case  0: i = PANGO_WEIGHT_NORMAL; break;
602                         case  1: i = PANGO_WEIGHT_BOLD; break;
603                         default: WARNING("Unknown font weight");
604                 }
605                 g_object_set(tag_object, "weight", i, "weight-set", TRUE, NULL);
606                 break;
607
608         case stylehint_Size:
609                 g_object_set(tag_object, "size", 14+(2*val), "size-set", TRUE, NULL);
610                 break;
611
612         case stylehint_Oblique:
613                 g_object_set(tag_object, "style", val ? PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL, "style-set", TRUE, NULL);
614                 break;
615
616         case stylehint_Proportional:
617                 g_object_set(tag_object, "font-desc", val ? glk_data->default_font_desc : glk_data->monospace_font_desc, NULL);
618                 break;
619
620         case stylehint_TextColor:
621                 glkcolor_to_hex(val, color);
622
623                 if(!reverse_color)
624                         g_object_set(tag_object, "foreground", color, "foreground-set", TRUE, NULL);
625                 else
626                         g_object_set(tag_object, "background", color, "background-set", TRUE, NULL);
627
628                 break;
629
630         case stylehint_BackColor:
631                 glkcolor_to_hex(val, color);
632
633                 if(!reverse_color)
634                         g_object_set(tag_object, "background", color, "background-set", TRUE, NULL);
635                 else
636                         g_object_set(tag_object, "foreground", color, "background-set", TRUE, NULL);
637
638                 break;
639
640         case stylehint_ReverseColor:
641                 if(reverse_color != val) {
642                         /* Flip the fore- and background colors */
643                         GdkColor* foreground_color;
644                         GdkColor* background_color;
645                         gint f_set, b_set = 0;
646                         g_object_get(tag_object, "foreground-set", &f_set, "background-set", &b_set, NULL);
647
648                         if(f_set)
649                                 g_object_get(tag_object, "foreground-gdk", &foreground_color, NULL);
650                         if(b_set)
651                                 g_object_get(tag_object, "background-gdk", &background_color, NULL);
652
653                         if(b_set)
654                                 g_object_set(tag_object, "foreground-gdk", background_color, NULL);
655                         else
656                                 g_object_set(tag_object, "foreground", "#ffffff", NULL);
657
658                         if(f_set)
659                                 g_object_set(tag_object, "background-gdk", foreground_color, NULL);
660                         else
661                                 g_object_set(tag_object, "background", "#000000", NULL);
662
663                         g_object_set_data( tag_object, "reverse-color", GINT_TO_POINTER(val != 0) );
664                 }
665                 break;
666
667         default:
668                 WARNING("Unknown style hint");
669         }
670 }
671 /*Internal function: queries a text tag for the value of a given style hint */
672 static gint
673 query_tag(GtkTextTag *tag, glui32 hint)
674 {
675         gint intval;
676         GObject *objval;
677         GdkColor *colval;
678
679         g_return_val_if_fail(tag != NULL, 0);
680
681         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
682
683         switch(hint) {
684         case stylehint_Indentation:
685                 g_object_get(tag, "left_margin", &intval, NULL);
686                 return intval/5;
687                 break;
688         
689         case stylehint_ParaIndentation:
690                 g_object_get(tag, "indent", &intval, NULL);
691                 return intval/5;
692                 break;
693
694         case stylehint_Justification:
695                 g_object_get(tag, "justification", &intval, NULL);
696                 switch(intval) {
697                         case GTK_JUSTIFY_LEFT: return stylehint_just_LeftFlush; break;
698                         case GTK_JUSTIFY_FILL: return stylehint_just_LeftRight; break;
699                         case GTK_JUSTIFY_CENTER: return stylehint_just_Centered; break;
700                         case GTK_JUSTIFY_RIGHT: return stylehint_just_RightFlush; break;
701                         default: 
702                                 WARNING("Unknown justification");
703                                 return stylehint_just_LeftFlush;
704                 }
705                 break;
706
707         case stylehint_Weight:
708                 g_object_get(tag, "weight", &intval, NULL);
709                 switch(intval) {
710                         case PANGO_WEIGHT_LIGHT: return -1; break;
711                         case PANGO_WEIGHT_NORMAL: return 0; break;
712                         case PANGO_WEIGHT_BOLD: return 1; break;
713                         default: WARNING("Unknown font weight"); return 0;
714                 }
715                 break;
716
717         case stylehint_Size:
718                 g_object_get(tag, "size", &intval, NULL);
719                 return (intval/2)-14;
720                 break;
721
722         case stylehint_Oblique:
723                 g_object_get(tag, "style", &intval , NULL);
724                 return intval == PANGO_STYLE_ITALIC ? 1 : 0;
725                 break;
726
727         case stylehint_Proportional:
728                 g_object_get(tag, "font-desc", &objval, NULL);
729                 return objval == (GObject *)glk_data->monospace_font_desc ? 0 : 1;
730                 break;
731
732         case stylehint_TextColor:
733                 g_object_get(tag, "foreground-gdk", &colval, NULL);
734                 return gdkcolor_to_glkcolor(colval);
735                 break;
736
737         case stylehint_BackColor:
738                 g_object_get(tag, "background-gdk", &colval, NULL);
739                 return gdkcolor_to_glkcolor(colval);
740                 break;
741
742         case stylehint_ReverseColor:
743                 return GPOINTER_TO_INT( g_object_get_data(G_OBJECT(tag), "reverse_color") );
744                 break;
745
746         default:
747                 WARNING("Unknown style hint");
748         }
749         
750         return 0;
751 }
752
753 /**
754  * glk_stylehint_set:
755  * @wintype: The window type to set a style hint on, or %wintype_AllTypes.
756  * @styl: The style to set a hint for.
757  * @hint: The type of style hint, one of the <code>stylehint_</code> constants.
758  * @val: The style hint. The meaning of this depends on @hint.
759  *
760  * Sets a hint about the appearance of one style for a particular type of 
761  * window. You can also set wintype to %wintype_AllTypes, which sets a hint for 
762  * all types of window.
763  * <note><para>
764  *  There is no equivalent constant to set a hint for all styles of a single 
765  *  window type.
766  * </para></note>
767  */
768 void
769 glk_stylehint_set(glui32 wintype, glui32 styl, glui32 hint, glsi32 val)
770 {
771         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
772
773         if( G_UNLIKELY(!glk_data->style_initialized) ) {
774                 style_init();
775         }
776
777         GtkTextTag *to_change;
778         if(wintype == wintype_TextBuffer || wintype == wintype_AllTypes) {
779                 to_change = g_hash_table_lookup( glk_data->current_styles->text_buffer, get_tag_name(styl) );
780                 apply_stylehint_to_tag(to_change, hint, val);
781         }
782
783         if(wintype == wintype_TextGrid || wintype == wintype_AllTypes) {
784                 to_change = g_hash_table_lookup( glk_data->current_styles->text_grid, get_tag_name(styl) );
785                 apply_stylehint_to_tag(to_change, hint, val);
786         }
787 }
788
789 /**
790  * glk_stylehint_clear:
791  * @wintype: The window type to set a style hint on, or %wintype_AllTypes.
792  * @styl: The style to set a hint for.
793  * @hint: The type of style hint, one of the <code>stylehint_</code> constants.
794  *
795  * Resets a hint about the appearance of one style for a particular type of 
796  * window to it's default value. You can also set wintype to %wintype_AllTypes, which resets a hint for 
797  * all types of window.
798  * <note><para>
799  *  There is no equivalent constant to reset a hint for all styles of a single 
800  *  window type.
801  * </para></note>
802  */
803 void
804 glk_stylehint_clear(glui32 wintype, glui32 styl, glui32 hint)
805 {
806         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
807         GtkTextTag *tag;
808
809         switch(wintype) {
810         case wintype_TextBuffer:
811                 tag = g_hash_table_lookup( glk_data->default_styles->text_buffer, get_tag_name(styl) );
812                 glk_stylehint_set( wintype, styl, hint, query_tag(tag, hint) );
813                 break;
814         case wintype_TextGrid:
815                 tag = g_hash_table_lookup( glk_data->default_styles->text_grid, get_tag_name(styl) );
816                 glk_stylehint_set( wintype, styl, hint, query_tag(tag, hint) );
817         default:
818                 return;
819         }
820 }
821
822 /**
823  * glk_style_distinguish:
824  * @win: The window in which the styles are to be distinguished.
825  * @styl1: The first style to be distinguished from the second style.
826  * @styl2: The second styel to be distinguished from the first style.
827  * 
828  * Returns: TRUE if the two styles are visually distinguishable in the given window.
829  * If they are not, it returns FALSE.
830  */
831 glui32
832 glk_style_distinguish(winid_t win, glui32 styl1, glui32 styl2)
833 {
834         return styl1 != styl2;
835 }
836
837 /**
838  * glk_style_measure:
839  * @win: The window from which to take the style.
840  * @styl: The style to perform the measurement on.
841  * @hint: The stylehint to measure.
842  * @result: Address to write the result to.
843  * 
844  * This function can be used to query the current value of a particular style hint.
845  * Returns: TRUE upon successul retrievel, otherwise FALSE.
846  */
847 glui32
848 glk_style_measure(winid_t win, glui32 styl, glui32 hint, glui32 *result)
849 {
850         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
851         GtkTextTag *tag;
852
853         switch(win->type) {
854         case wintype_TextBuffer:
855                 tag = g_hash_table_lookup( glk_data->current_styles->text_buffer, get_tag_name(styl) );
856                 *result = query_tag(tag, hint);
857                 break;
858         case wintype_TextGrid:
859                 tag = g_hash_table_lookup( glk_data->current_styles->text_grid, get_tag_name(styl) );
860                 *result = query_tag(tag, hint);
861         default:
862                 return FALSE;
863         }
864
865         return TRUE;
866 }
867
868 /* Internal function returning the current default font for a window type
869  * This can be used later for size calculations. Only wintype_TextGrid and wintype_TextBuffer are
870  * supported for now */
871 PangoFontDescription*
872 get_current_font(guint32 wintype)
873 {
874         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
875         GtkTextTag *normal;
876
877         if( G_UNLIKELY(!glk_data->style_initialized) ) {
878                 style_init();
879         }
880
881         switch(wintype) {
882         case wintype_TextGrid:
883                 normal = g_hash_table_lookup(glk_data->current_styles->text_grid, "normal");
884                 break;
885         case wintype_TextBuffer:
886                 normal = g_hash_table_lookup(glk_data->current_styles->text_buffer, "normal");
887                 break;
888         default:
889                 return NULL;
890         }
891
892         PangoFontDescription *font;
893         g_object_get( G_OBJECT(normal), "font-desc", &font, NULL );
894
895         return font;
896 }
897
898 /* Internal function copying the attributes of a text tag to a pango attribute list */
899 static void
900 text_tag_to_attr_list(GtkTextTag *tag, PangoAttrList *list)
901 {
902         gboolean set;
903         GdkColor *foreground, *background;
904         gchar *string;
905         PangoFontDescription *font_desc;
906         gboolean strikethrough;
907         PangoUnderline underline;
908
909         g_object_get(tag, "foreground-set", &set, "foreground-gdk", &foreground, NULL);
910         if(set) {
911                 pango_attr_list_insert(
912                         list,
913                         pango_attr_foreground_new(foreground->red, foreground->green, foreground->blue)
914                 );
915         }
916         g_object_get(tag, "background-set", &set, "background-gdk", &background, NULL);
917         if(set) {
918                 pango_attr_list_insert(
919                         list,
920                         pango_attr_background_new(background->red, background->green, background->blue)
921                 );
922         }
923         g_object_get(tag, "language-set", &set, "language", &string, NULL);
924         if(set) {
925                 pango_attr_list_insert(
926                         list,
927                         pango_attr_language_new( pango_language_from_string(string) )
928                 );
929         }
930
931         /* Font description updates the following properties simultaniously:
932          * family, style, weight, variant, stretch, size
933          */
934         g_object_get(tag, "font-desc", &font_desc, NULL);
935         pango_attr_list_insert(
936                 list,
937                 pango_attr_font_desc_new(font_desc)
938         );
939
940         g_object_get(tag, "strikethrough-set", &set, "strikethrough", &strikethrough, NULL);
941         if(set) {
942                 pango_attr_list_insert(
943                         list,
944                         pango_attr_strikethrough_new(strikethrough)
945                 );
946         }
947         g_object_get(tag, "underline-set", &set, "underline", &underline, NULL);
948         if(set) {
949                 pango_attr_list_insert(
950                         list,
951                         pango_attr_underline_new(underline)
952                 );
953         }
954 }