Showing off dynamic styles: started work on preferences dialog.
[rodin/chimara.git] / libchimara / style.c
1 #include <stdio.h>
2 #include <string.h>
3 #include "chimara-glk-private.h"
4 #include "glk.h"
5 #include "style.h"
6 #include "magic.h"
7 #include "stream.h"
8 #include "strio.h"
9
10 extern GPrivate *glk_data_key;
11
12 static gboolean style_accept(GScanner *scanner, GTokenType token);
13 static gboolean style_accept_style_selector(GScanner *scanner, ChimaraGlk *glk);
14 static gboolean style_accept_style_hint(GScanner *scanner, GtkTextTag *current_tag);
15 static void style_add_tag_to_textbuffer(gpointer key, gpointer tag, gpointer tag_table);
16 static void style_copy_tag_to_textbuffer(gpointer key, gpointer tag, gpointer target_table);
17 static void text_tag_to_attr_list(GtkTextTag *tag, PangoAttrList *list);
18 GtkTextTag* gtk_text_tag_copy(GtkTextTag *tag);
19
20 /**
21  * glk_set_style:
22  * @styl: The style to apply
23  *
24  * Changes the style of the current output stream. @styl should be one of the
25  * <code>style_</code> constants. However, any value is actually legal; if the 
26  * interpreter does not recognize the style value, it will treat it as 
27  * %style_Normal.
28  * <note><para>
29  *  This policy allows for the future definition of styles without breaking old
30  *  Glk libraries.
31  * </para></note>
32  */
33 void
34 glk_set_style(glui32 styl)
35 {
36         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
37         g_return_if_fail(glk_data->current_stream != NULL);
38         glk_set_style_stream(glk_data->current_stream, styl);
39 }
40
41 static const gchar* TAG_NAMES[] = {
42         "normal",
43         "emphasized",
44         "preformatted",
45         "header",
46         "subheader",
47         "alert",
48         "note",
49         "block-quote",
50         "input",
51         "user1",
52         "user2",
53         "hyperlink",
54         "pager"
55 };
56
57 static const gchar* GLK_TAG_NAMES[] = {
58         "glk-normal",
59         "glk-emphasized",
60         "glk-preformatted",
61         "glk-header",
62         "glk-subheader",
63         "glk-alert",
64         "glk-note",
65         "glk-block-quote",
66         "glk-input",
67         "glk-user1",
68         "glk-user2"
69 };
70
71 const gchar**
72 style_get_tag_names()
73 {
74         return TAG_NAMES;
75 }
76
77 /* Internal function: mapping from style enum to tag name */
78 static const gchar*
79 get_tag_name(glui32 style)
80 {
81         if(style >= CHIMARA_NUM_STYLES) {
82                 WARNING("Unsupported style");
83                 return "normal";
84         } else {
85                 return (gchar*) TAG_NAMES[style];
86         }
87 }
88
89 /* Internal function: mapping from glk style enum to tag name */
90 static const gchar*
91 get_glk_tag_name(glui32 style)
92 {
93         if(style >= style_NUMSTYLES) {
94                 WARNING("Unsupported style");
95                 return "normal";
96         } else {
97                 return (gchar*) GLK_TAG_NAMES[style];
98         }
99 }
100
101 /** 
102  * glk_set_style_stream:
103  * @str: Output stream to change the style of
104  * @styl: The style to apply
105  *
106  * This changes the style of the stream @str. See glk_set_style().
107  */
108 void
109 glk_set_style_stream(strid_t str, glui32 styl) {
110         if(str->window == NULL)
111                 return;
112
113         flush_window_buffer(str->window);
114         str->style = (gchar*) get_tag_name(styl);
115         str->glk_style = (gchar*) get_glk_tag_name(styl);
116 }
117
118 /* Internal function: call this to initialize the layout of the 'more' prompt. */
119 void
120 style_init_more_prompt(winid_t win)
121 {
122         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
123
124         win->pager_layout = gtk_widget_create_pango_layout(win->widget, "More");
125         pango_layout_set_attributes(win->pager_layout, glk_data->pager_attr_list);
126 }
127
128 /* Internal function: call this to initialize the default styles to a textbuffer. */
129 void
130 style_init_textbuffer(GtkTextBuffer *buffer)
131 {
132         g_return_if_fail(buffer != NULL);
133
134         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
135
136         /* Place the default text tags in the textbuffer's tag table */
137         g_hash_table_foreach(glk_data->styles->text_buffer, style_add_tag_to_textbuffer, gtk_text_buffer_get_tag_table(buffer));
138
139         /* Copy the override text tags to the textbuffers's tag table */
140         g_hash_table_foreach(glk_data->glk_styles->text_buffer, style_copy_tag_to_textbuffer, gtk_text_buffer_get_tag_table(buffer));
141 }
142
143
144 /* Internal function: call this to initialize the default styles to a textgrid. */
145 void
146 style_init_textgrid(GtkTextBuffer *buffer)
147 {
148         g_return_if_fail(buffer != NULL);
149         
150         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
151
152         /* Place the default text tags in the textbuffer's tag table */
153         g_hash_table_foreach(glk_data->styles->text_grid, style_add_tag_to_textbuffer, gtk_text_buffer_get_tag_table(buffer));
154
155         /* Copy the current text tags to the textbuffers's tag table */
156         g_hash_table_foreach(glk_data->glk_styles->text_grid, style_copy_tag_to_textbuffer, gtk_text_buffer_get_tag_table(buffer));
157 }
158
159 /* Internal function used to iterate over the default text tag table, applying them to a textbuffer */
160 static void
161 style_add_tag_to_textbuffer(gpointer key, gpointer tag, gpointer tag_table)
162 {
163         g_return_if_fail(key != NULL);
164         g_return_if_fail(tag != NULL);
165         g_return_if_fail(tag_table != NULL);
166
167         gtk_text_tag_table_add(tag_table, tag);
168 }
169
170 /* Internal function used to iterate over a style table, copying it */
171 static void
172 style_copy_tag_to_textbuffer(gpointer key, gpointer tag, gpointer target_table)
173 {
174         g_return_if_fail(key != NULL);
175         g_return_if_fail(tag != NULL);
176         g_return_if_fail(target_table != NULL);
177
178         gtk_text_tag_table_add(target_table, gtk_text_tag_copy( GTK_TEXT_TAG(tag) ));
179 }
180
181 /* Internal function that copies a text tag */
182 GtkTextTag *
183 gtk_text_tag_copy(GtkTextTag *tag)
184 {
185         GtkTextTag *copy;
186
187         g_return_val_if_fail(tag != NULL, NULL);
188
189         copy = gtk_text_tag_new(tag->name);
190         gtk_text_attributes_copy_values(tag->values, copy->values);
191         
192         #define _COPY_FLAG(flag) copy->flag = tag->flag
193                 _COPY_FLAG (bg_color_set);
194                 _COPY_FLAG (bg_color_set);
195                 _COPY_FLAG (bg_stipple_set);
196                 _COPY_FLAG (fg_color_set);
197                 _COPY_FLAG (fg_stipple_set);
198                 _COPY_FLAG (justification_set);
199                 _COPY_FLAG (left_margin_set);
200                 _COPY_FLAG (indent_set);
201                 _COPY_FLAG (rise_set);
202                 _COPY_FLAG (strikethrough_set);
203                 _COPY_FLAG (right_margin_set);
204                 _COPY_FLAG (pixels_above_lines_set);
205                 _COPY_FLAG (pixels_below_lines_set);
206                 _COPY_FLAG (pixels_inside_wrap_set);
207                 _COPY_FLAG (tabs_set);
208                 _COPY_FLAG (underline_set);
209                 _COPY_FLAG (wrap_mode_set);
210                 _COPY_FLAG (bg_full_height_set);
211                 _COPY_FLAG (invisible_set);
212                 _COPY_FLAG (editable_set);
213                 _COPY_FLAG (language_set);
214         #undef _COPY_FLAG
215
216         /* Copy the data that was added manually */
217         gpointer reverse_color = g_object_get_data( G_OBJECT(tag), "reverse_color" );
218
219         if(reverse_color)
220                 g_object_set_data( G_OBJECT(copy), "reverse_color", reverse_color );
221
222         return copy;
223 }
224
225 /* Internal function that constructs the default styles */
226 void
227 style_init(ChimaraGlk *glk)
228 {
229         CHIMARA_GLK_USE_PRIVATE(glk, priv);
230         
231         GHashTable *default_text_grid_styles = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_object_unref);
232         GHashTable *default_text_buffer_styles = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_object_unref);
233         GHashTable *glk_text_grid_styles = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_object_unref);
234         GHashTable *glk_text_buffer_styles = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_object_unref);
235         GtkTextTag *tag;
236
237         PangoFontDescription *default_font_desc = pango_font_description_from_string("Serif");
238         PangoFontDescription *monospace_font_desc = pango_font_description_from_string("Monospace");
239         
240         /* Initialise the default styles for a text grid */
241         tag = gtk_text_tag_new("normal");
242         g_object_set(tag, "font-desc", monospace_font_desc, NULL);
243         g_hash_table_insert(default_text_grid_styles, "normal", tag);
244
245         tag = gtk_text_tag_new("emphasized");
246         g_object_set(tag, "font-desc", monospace_font_desc, "style", PANGO_STYLE_ITALIC, "style-set", TRUE, NULL);
247         g_hash_table_insert(default_text_grid_styles, "emphasized", tag);
248
249         tag = gtk_text_tag_new("preformatted");
250         g_object_set(tag, "font-desc", monospace_font_desc, NULL);
251         g_hash_table_insert(default_text_grid_styles, "preformatted", tag);
252
253         tag = gtk_text_tag_new("header");
254         g_object_set(tag, "font-desc", monospace_font_desc, "weight", PANGO_WEIGHT_BOLD, NULL);
255         g_hash_table_insert(default_text_grid_styles, "header", tag);
256
257         tag = gtk_text_tag_new("subheader");
258         g_object_set(tag, "font-desc", monospace_font_desc, "weight", PANGO_WEIGHT_BOLD, NULL);
259         g_hash_table_insert(default_text_grid_styles, "subheader", tag);
260
261         tag = gtk_text_tag_new("alert");
262         g_object_set(tag, "font-desc", monospace_font_desc, "foreground", "#aa0000", "weight", PANGO_WEIGHT_BOLD, NULL);
263         g_hash_table_insert(default_text_grid_styles, "alert", tag);
264
265         tag = gtk_text_tag_new("note");
266         g_object_set(tag, "font-desc", monospace_font_desc, "foreground", "#aaaa00", "weight", PANGO_WEIGHT_BOLD, NULL);
267         g_hash_table_insert(default_text_grid_styles, "note", tag);
268
269         tag = gtk_text_tag_new("block-quote");
270         g_object_set(tag, "font-desc", monospace_font_desc, "style", PANGO_STYLE_ITALIC, "style-set", TRUE, NULL);
271         g_hash_table_insert(default_text_grid_styles, "block-quote", tag);
272
273         tag = gtk_text_tag_new("input");
274         g_object_set(tag, "font-desc", monospace_font_desc, NULL);
275         g_hash_table_insert(default_text_grid_styles, "input", tag);
276
277         tag = gtk_text_tag_new("user1");
278         g_object_set(tag, "font-desc", monospace_font_desc, NULL);
279         g_hash_table_insert(default_text_grid_styles, "user1", tag);
280
281         tag = gtk_text_tag_new("user2");
282         g_object_set(tag, "font-desc", monospace_font_desc, NULL);
283         g_hash_table_insert(default_text_grid_styles, "user2", tag);
284
285         tag = gtk_text_tag_new("hyperlink");
286         g_object_set(tag, "foreground", "#0000ff", "underline", PANGO_UNDERLINE_SINGLE, "underline-set", TRUE, NULL);
287         g_hash_table_insert(default_text_grid_styles, "hyperlink", tag);
288
289         /* Initialise the default styles for a text buffer */
290         tag = gtk_text_tag_new("normal");
291         g_object_set(tag, "font-desc", default_font_desc, NULL);
292         g_hash_table_insert(default_text_buffer_styles, "normal", tag);
293
294         tag = gtk_text_tag_new("emphasized");
295         g_object_set(tag, "font-desc", default_font_desc, "style", PANGO_STYLE_ITALIC, "style-set", TRUE, NULL);
296         g_hash_table_insert(default_text_buffer_styles, "emphasized", tag);
297
298         tag = gtk_text_tag_new("preformatted");
299         g_object_set(tag, "font-desc", monospace_font_desc, NULL);
300         g_hash_table_insert(default_text_buffer_styles, "preformatted", tag);
301
302         tag = gtk_text_tag_new("header");
303         g_object_set(tag, "font-desc", default_font_desc, "size-points", 18.0, "weight", PANGO_WEIGHT_BOLD, NULL);
304         g_hash_table_insert(default_text_buffer_styles, "header", tag);
305
306         tag = gtk_text_tag_new("subheader");
307         g_object_set(tag, "font-desc", default_font_desc, "size-points", 14.0, "weight", PANGO_WEIGHT_BOLD, NULL);
308         g_hash_table_insert(default_text_buffer_styles, "subheader", tag);
309
310         tag = gtk_text_tag_new("alert");
311         g_object_set(tag, "font-desc", default_font_desc, "foreground", "#aa0000", "weight", PANGO_WEIGHT_BOLD, NULL);
312         g_hash_table_insert(default_text_buffer_styles, "alert", tag);
313
314         tag = gtk_text_tag_new("note");
315         g_object_set(tag, "font-desc", default_font_desc, "foreground", "#aaaa00", "weight", PANGO_WEIGHT_BOLD, NULL);
316         g_hash_table_insert(default_text_buffer_styles, "note", tag);
317
318         tag = gtk_text_tag_new("block-quote");
319         g_object_set(tag, "font-desc", default_font_desc, "justification", GTK_JUSTIFY_CENTER, "style", PANGO_STYLE_ITALIC, "style-set", TRUE, NULL);
320         g_hash_table_insert(default_text_buffer_styles, "block-quote", tag);
321
322         tag = gtk_text_tag_new("input");
323         g_object_set(tag, "font-desc", default_font_desc, NULL);
324         g_hash_table_insert(default_text_buffer_styles, "input", tag);
325
326         tag = gtk_text_tag_new("user1");
327         g_object_set(tag, "font-desc", default_font_desc, NULL);
328         g_hash_table_insert(default_text_buffer_styles, "user1", tag);
329
330         tag = gtk_text_tag_new("user2");
331         g_object_set(tag, "font-desc", default_font_desc, NULL);
332         g_hash_table_insert(default_text_buffer_styles, "user2", tag);
333
334         tag = gtk_text_tag_new("hyperlink");
335         g_object_set(tag, "font-desc", default_font_desc, "foreground", "#0000ff", "underline", PANGO_UNDERLINE_SINGLE, "underline-set", TRUE, NULL);
336         g_hash_table_insert(default_text_buffer_styles, "hyperlink", tag);
337
338         GtkTextTag *pager_tag = gtk_text_tag_new("pager");
339         g_object_set(pager_tag, "font-desc", default_font_desc, "foreground", "#ffffff", "background", "#000000", NULL);
340         g_hash_table_insert(default_text_buffer_styles, "pager", pager_tag);
341         text_tag_to_attr_list(pager_tag, priv->pager_attr_list);
342
343         pango_font_description_free(default_font_desc);
344         pango_font_description_free(monospace_font_desc);
345         
346         priv->styles->text_grid = default_text_grid_styles;
347         priv->styles->text_buffer = default_text_buffer_styles;
348
349
350         /* Initialize the GLK styles to empty tags */
351         int i;
352         for(i=0; i<style_NUMSTYLES; i++) {
353                 tag = gtk_text_tag_new(GLK_TAG_NAMES[i]);
354                 g_hash_table_insert(glk_text_grid_styles, (gchar*) GLK_TAG_NAMES[i], tag);
355                 g_hash_table_insert(glk_text_buffer_styles, (gchar*) GLK_TAG_NAMES[i], tag);
356         }
357
358         priv->glk_styles->text_grid = glk_text_grid_styles;
359         priv->glk_styles->text_buffer = glk_text_buffer_styles;
360
361 }
362
363 /* Reset style tables to the library's internal defaults */
364 void
365 reset_default_styles(ChimaraGlk *glk)
366 {
367         /* TODO: write this function */
368 }
369
370 /* Copy the default styles to the current styles
371  FIXME: This function is temporary and will not be needed later on */
372 void
373 copy_default_styles_to_current_styles(ChimaraGlk *glk)
374 {
375         /*
376         CHIMARA_GLK_USE_PRIVATE(glk, priv);
377         g_hash_table_foreach(priv->styles->text_grid, style_table_copy, priv->glk_styles->text_grid);
378         g_hash_table_foreach(priv->styles->text_buffer, style_table_copy, priv->glk_styles->text_buffer);
379
380         GtkTextTag *pager_tag = GTK_TEXT_TAG( g_hash_table_lookup(priv->styles->text_buffer, "pager") );
381         text_tag_to_attr_list(pager_tag, priv->pager_attr_list);
382         */
383 }
384
385 /* Create the CSS file scanner */
386 GScanner *
387 create_css_file_scanner(void)
388 {
389         GScanner *scanner = g_scanner_new(NULL);
390         scanner->config->cset_identifier_first = G_CSET_a_2_z G_CSET_A_2_Z "#";
391         scanner->config->cset_identifier_nth = G_CSET_a_2_z G_CSET_A_2_Z "-_" G_CSET_DIGITS;
392         scanner->config->symbol_2_token = TRUE;
393         scanner->config->cpair_comment_single = NULL;
394         scanner->config->scan_float = FALSE;
395         return scanner;
396 }
397
398 /* Run the scanner over the CSS file, overriding the default styles */
399 void
400 scan_css_file(GScanner *scanner, ChimaraGlk *glk)
401 {
402         CHIMARA_GLK_USE_PRIVATE(glk, priv);
403
404         while( g_scanner_peek_next_token(scanner) != G_TOKEN_EOF) {
405                 if( !style_accept_style_selector(scanner, glk) )
406                         break;
407         }
408
409         g_scanner_destroy(scanner);
410
411         /* Update the pager prompt to the new style */
412         GtkTextTag *pager_tag = GTK_TEXT_TAG( g_hash_table_lookup(priv->styles->text_buffer, "pager") );
413         text_tag_to_attr_list(pager_tag, priv->pager_attr_list);
414 }
415
416 /* Internal function: parses a token */
417 static gboolean
418 style_accept(GScanner *scanner, GTokenType token)
419 {
420         GTokenType next = g_scanner_get_next_token(scanner);
421         if(next != token) {
422                 g_scanner_unexp_token(scanner, token, NULL, NULL, NULL, "CSS Error", 1);
423                 return FALSE;
424         }
425         return TRUE;
426 }
427
428 /* Internal function: parses a style selector */
429 static gboolean
430 style_accept_style_selector(GScanner *scanner, ChimaraGlk *glk)
431 {
432         CHIMARA_GLK_USE_PRIVATE(glk, priv);
433
434         GtkTextTag *current_tag;
435         gchar *field;
436         GTokenType token = g_scanner_get_next_token(scanner);
437         GTokenValue value = g_scanner_cur_value(scanner);
438
439         if(
440                 token != G_TOKEN_IDENTIFIER ||
441                 ( strcmp(value.v_identifier, "buffer") && strcmp(value.v_identifier, "grid") )
442         ) {
443                 g_scanner_error(scanner, "CSS Error: buffer/grid expected");
444                 return FALSE;
445         }
446
447         field = g_strdup(value.v_identifier);
448
449         if( !style_accept(scanner, '.') )
450                 return FALSE;
451
452         token = g_scanner_get_next_token(scanner);
453         value = g_scanner_cur_value(scanner);
454
455         if(token != G_TOKEN_IDENTIFIER) {
456                 g_scanner_error(scanner, "CSS Error: style selector expected");
457                 return FALSE;
458         }
459
460         if( !strcmp(field, "buffer") )
461                 current_tag = g_hash_table_lookup(priv->styles->text_buffer, value.v_identifier);
462         else
463                 current_tag = g_hash_table_lookup(priv->styles->text_grid, value.v_identifier);
464
465         if(current_tag == NULL) {
466                 g_scanner_error(scanner, "CSS Error: invalid style identifier");
467                 return FALSE;
468         }
469
470         if( !style_accept(scanner, '{') )
471                 return FALSE;
472
473         while( g_scanner_peek_next_token(scanner) != '}') {
474                 if( !style_accept_style_hint(scanner, current_tag) )
475                         return FALSE;
476         }
477                 
478         if( !style_accept(scanner, '}') )
479                 return FALSE;
480
481         return TRUE;
482 }
483
484 /* Internal function: parses a style hint */
485 static gboolean
486 style_accept_style_hint(GScanner *scanner, GtkTextTag *current_tag)
487 {
488         GTokenType token = g_scanner_get_next_token(scanner);
489         GTokenValue value = g_scanner_cur_value(scanner);
490         gchar *hint;
491
492         if(token != G_TOKEN_IDENTIFIER) {
493                 g_scanner_error(scanner, "CSS Error: style hint expected");
494                 return FALSE;
495         }
496
497         hint = g_strdup(value.v_identifier);
498
499         if( !style_accept(scanner, ':') )
500                 return FALSE;
501
502         token = g_scanner_get_next_token(scanner);
503         value = g_scanner_cur_value(scanner);
504
505         if( !strcmp(hint, "font-family") ) {
506                 if(token != G_TOKEN_STRING) {
507                         g_scanner_error(scanner, "CSS Error: string expected");
508                         return FALSE;
509                 }
510                 g_object_set(current_tag, "family", value.v_string, "family-set", TRUE, NULL);
511         }
512         else if( !strcmp(hint, "font-weight") ) {
513                 if(token != G_TOKEN_IDENTIFIER) {
514                         g_scanner_error(scanner, "CSS Error: bold/normal expected");
515                         return FALSE;
516                 }
517
518                 if( !strcmp(value.v_identifier, "bold") )
519                         g_object_set(current_tag, "weight", PANGO_WEIGHT_BOLD, "weight-set", TRUE, NULL);
520                 else if( !strcmp(value.v_identifier, "normal") )
521                         g_object_set(current_tag, "weight", PANGO_WEIGHT_NORMAL, "weight-set", TRUE, NULL);
522                 else {
523                         g_scanner_error(scanner, "CSS Error: bold/normal expected");
524                         return FALSE;
525                 }
526         }
527         else if( !strcmp(hint, "font-style") ) {
528                 if(token != G_TOKEN_IDENTIFIER) {
529                         g_scanner_error(scanner, "CSS Error: italic/normal expected");
530                         return FALSE;
531                 }
532
533                 if( !strcmp(value.v_identifier, "italic") )
534                         g_object_set(current_tag, "style", PANGO_STYLE_ITALIC, "style-set", TRUE, NULL);
535                 else if( !strcmp(value.v_identifier, "normal") )
536                         g_object_set(current_tag, "style", PANGO_STYLE_NORMAL, "style-set", TRUE, NULL);
537                 else {
538                         g_scanner_error(scanner, "CSS Error: italic/normal expected");
539                         return FALSE;
540                 }
541         }
542         else if( !strcmp(hint, "font-size") ) {
543                 if(token == G_TOKEN_INT) 
544                         g_object_set(current_tag, "size-points", (float)value.v_int, "size-set", TRUE, NULL);
545                 else if(token == G_TOKEN_FLOAT)
546                         g_object_set(current_tag, "size-points", value.v_float, "size-set", TRUE, NULL);
547                 else {
548                         g_scanner_error(scanner, "CSS Error: integer or float expected");
549                         return FALSE;
550                 }
551         }
552         else if( !strcmp(hint, "color") ) {
553                 if(token != G_TOKEN_IDENTIFIER) {
554                         g_scanner_error(scanner, "CSS Error: hex color expected");
555                         return FALSE;
556                 }
557
558                 g_object_set(current_tag, "foreground", value.v_identifier, "foreground-set", TRUE, NULL);
559         }
560         else if( !strcmp(hint, "background-color") ) {
561                 if(token != G_TOKEN_IDENTIFIER) {
562                         g_scanner_error(scanner, "CSS Error: hex color expected");
563                         return FALSE;
564                 }
565                 g_object_set(current_tag, "background", value.v_identifier, "background-set", TRUE, NULL);
566         }
567         else if( !strcmp(hint, "text-align") ) {
568                 if(token != G_TOKEN_IDENTIFIER) {
569                         g_scanner_error(scanner, "CSS Error: left/right/center expected");
570                         return FALSE;
571                 }
572                 
573                 if( !strcmp(value.v_identifier, "left") )
574                         g_object_set(current_tag, "justification", GTK_JUSTIFY_LEFT, "justification-set", TRUE, NULL);
575                 else if( !strcmp(value.v_identifier, "right") )
576                         g_object_set(current_tag, "justification", GTK_JUSTIFY_RIGHT, "justification-set", TRUE, NULL);
577                 else if( !strcmp(value.v_identifier, "center") )
578                         g_object_set(current_tag, "justification", GTK_JUSTIFY_CENTER, "justification-set", TRUE, NULL);
579                 else {
580                         g_scanner_error(scanner, "CSS Error: left/right/center expected");
581                         return FALSE;
582                 }
583         }
584         else if( !strcmp(hint, "margin-left") ) {
585                 if(token != G_TOKEN_INT) {
586                         g_scanner_error(scanner, "CSS Error: integer expected");
587                         return FALSE;
588                 }
589                 g_object_set(current_tag, "left-margin", value.v_int, "left-margin-set", TRUE, NULL);
590         }
591         else if( !strcmp(hint, "margin-right") ) {
592                 if(token != G_TOKEN_INT) {
593                         g_scanner_error(scanner, "CSS Error: integer expected");
594                         return FALSE;
595                 }
596                 g_object_set(current_tag, "right-margin", value.v_int, "right-margin-set", TRUE, NULL);
597         }
598         else if( !strcmp(hint, "margin-top") ) {
599                 if(token != G_TOKEN_INT) {
600                         g_scanner_error(scanner, "CSS Error: integer expected");
601                         return FALSE;
602                 }
603                 g_object_set(current_tag, "pixels-above-lines", value.v_int, "pixels-above-lines-set", TRUE, NULL);
604         }
605         else if( !strcmp(hint, "margin-bottom") ) {
606                 if(token != G_TOKEN_INT) {
607                         g_scanner_error(scanner, "CSS Error: integer expected");
608                         return FALSE;
609                 }
610                 g_object_set(current_tag, "pixels-below-lines", value.v_int, "pixels-below-lines-set", TRUE, NULL);
611         }
612                 
613         else {
614                 g_scanner_error(scanner, "CSS Error: invalid style hint %s", hint);
615                 return FALSE;
616         }
617
618         if( !style_accept(scanner, ';') )
619                 return FALSE;
620
621         return TRUE;
622 }
623
624 /* Internal function: parses a glk color to a #hex-value */
625 static void
626 glkcolor_to_hex(glui32 val, gchar *buffer)
627 {
628         g_return_if_fail(buffer != NULL);
629
630         sprintf(buffer, "#%02X%02X%02X",
631                 ((val & 0xff0000) >> 16),
632                 ((val & 0x00ff00) >> 8),
633                 (val & 0x0000ff)
634         );
635 }
636
637 /* Internal function: parses a glk color to a GdkColor */
638 void
639 glkcolor_to_gdkcolor(glui32 val, GdkColor *color)
640 {
641         color->red = 256 * ((val & 0xff0000) >> 16);
642         color->green = 256 * ((val & 0x00ff00) >> 8);
643         color->blue = 256 * (val & 0x0000ff);
644 }
645
646 /* Internal function: parses a GdkColor to a glk color */
647 static glui32
648 gdkcolor_to_glkcolor(GdkColor *color)
649 {
650         g_return_val_if_fail(color != NULL, 0);
651         return (glui32) color->pixel;
652 }
653
654 /* Internal function: changes a GTK tag to correspond with the given style. */
655 static void
656 apply_stylehint_to_tag(GtkTextTag *tag, GtkTextTag *default_tag, glui32 wintype, glui32 hint, glsi32 val)
657 {
658         g_return_if_fail(tag != NULL);
659
660         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
661         GObject *tag_object = G_OBJECT(tag);
662
663         gint reverse_color = GPOINTER_TO_INT( g_object_get_data(tag_object, "reverse-color") );
664
665         int i = 0;
666         gchar color[20];
667         switch(hint) {
668         case stylehint_Indentation:
669                 g_object_set(tag_object, "left-margin", 5*val, "left-margin-set", TRUE, NULL);
670                 g_object_set(tag_object, "right-margin", 5*val, "right-margin-set", TRUE, NULL);
671                 break;
672         
673         case stylehint_ParaIndentation:
674                 g_object_set(tag_object, "indent", 5*val, "indent-set", TRUE, NULL);
675                 break;
676
677         case stylehint_Justification:
678                 switch(val) {
679                         case stylehint_just_LeftFlush:  i = GTK_JUSTIFY_LEFT; break;
680                         case stylehint_just_LeftRight:  i = GTK_JUSTIFY_FILL; break;
681                         case stylehint_just_Centered:   i = GTK_JUSTIFY_CENTER; break;
682                         case stylehint_just_RightFlush: i = GTK_JUSTIFY_RIGHT; break;
683                         default: 
684                                 WARNING("Unknown justification");
685                                 i = GTK_JUSTIFY_LEFT;
686                 }
687                 g_object_set(tag_object, "justification", i, "justification-set", TRUE, NULL);
688                 break;
689
690         case stylehint_Weight:
691                 switch(val) {
692                         case -1: i = PANGO_WEIGHT_LIGHT; break;
693                         case  0: i = PANGO_WEIGHT_NORMAL; break;
694                         case  1: i = PANGO_WEIGHT_BOLD; break;
695                         default: WARNING("Unknown font weight");
696                 }
697                 g_object_set(tag_object, "weight", i, "weight-set", TRUE, NULL);
698                 break;
699
700         case stylehint_Size:
701                 g_object_set(tag_object, "size", 14+(2*val), "size-set", TRUE, NULL);
702                 break;
703
704         case stylehint_Oblique:
705                 g_object_set(tag_object, "style", val ? PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL, "style-set", TRUE, NULL);
706                 break;
707
708         case stylehint_Proportional:
709         {
710                 gchar *font_family;
711                 GtkTextTag *font_tag = g_hash_table_lookup(
712                     wintype == wintype_TextBuffer? glk_data->styles->text_buffer : glk_data->styles->text_grid,
713                     val? "normal" : "preformatted");
714                 g_object_get(font_tag, "family", &font_family, NULL);
715                 g_object_set(tag_object, "family", font_family, "family-set", TRUE, NULL);
716                 g_free(font_family);
717         }
718                 break;
719
720         case stylehint_TextColor:
721                 glkcolor_to_hex(val, color);
722
723                 if(!reverse_color)
724                         g_object_set(tag_object, "foreground", color, "foreground-set", TRUE, NULL);
725                 else
726                         g_object_set(tag_object, "background", color, "background-set", TRUE, NULL);
727
728                 break;
729
730         case stylehint_BackColor:
731                 glkcolor_to_hex(val, color);
732
733                 if(!reverse_color)
734                         g_object_set(tag_object, "background", color, "background-set", TRUE, NULL);
735                 else
736                         g_object_set(tag_object, "foreground", color, "background-set", TRUE, NULL);
737
738                 break;
739
740         case stylehint_ReverseColor:
741                 if(reverse_color != val) {
742                         /* Flip the fore- and background colors */
743                         GdkColor* foreground_color;
744                         GdkColor* background_color;
745                         gint f_set, b_set, df_set, db_set = 0;
746                         g_object_get(tag_object, "foreground-set", &f_set, "background-set", &b_set, NULL);
747                         g_object_get(default_tag, "foreground-set", &df_set, "background-set", &db_set, NULL);
748
749                         if(f_set)
750                                 g_object_get(tag_object, "foreground-gdk", &foreground_color, NULL);
751                         else if(df_set)
752                                 g_object_get(default_tag, "foreground-gdk", &foreground_color, NULL);
753                         if(b_set)
754                                 g_object_get(tag_object, "background-gdk", &background_color, NULL);
755                         else if(db_set)
756                                 g_object_get(default_tag, "background-gdk", &background_color, NULL);
757
758                         if(b_set || db_set)
759                                 g_object_set(tag_object, "foreground-gdk", background_color, NULL);
760                         else
761                                 g_object_set(tag_object, "foreground", "#ffffff", NULL);
762
763                         if(f_set || df_set)
764                                 g_object_set(tag_object, "background-gdk", foreground_color, NULL);
765                         else
766                                 g_object_set(tag_object, "background", "#000000", NULL);
767
768                         g_object_set_data( tag_object, "reverse-color", GINT_TO_POINTER(val != 0) );
769                 }
770                 break;
771
772         default:
773                 WARNING("Unknown style hint");
774         }
775 }
776
777 /* Internal function: queries a text tag for the value of a given style hint */
778 static gint
779 query_tag(GtkTextTag *tag, glui32 wintype, glui32 hint)
780 {
781         gint intval;
782         GdkColor *colval;
783
784         g_return_val_if_fail(tag != NULL, 0);
785
786         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
787
788         switch(hint) {
789         case stylehint_Indentation:
790                 g_object_get(tag, "left_margin", &intval, NULL);
791                 return intval/5;
792         
793         case stylehint_ParaIndentation:
794                 g_object_get(tag, "indent", &intval, NULL);
795                 return intval/5;
796
797         case stylehint_Justification:
798                 g_object_get(tag, "justification", &intval, NULL);
799                 switch(intval) {
800                         case GTK_JUSTIFY_LEFT: return stylehint_just_LeftFlush;
801                         case GTK_JUSTIFY_FILL: return stylehint_just_LeftRight;
802                         case GTK_JUSTIFY_CENTER: return stylehint_just_Centered;
803                         case GTK_JUSTIFY_RIGHT: return stylehint_just_RightFlush;
804                         default: 
805                                 WARNING("Unknown justification");
806                                 return stylehint_just_LeftFlush;
807                 }
808
809         case stylehint_Weight:
810                 g_object_get(tag, "weight", &intval, NULL);
811                 switch(intval) {
812                         case PANGO_WEIGHT_LIGHT: return -1;
813                         case PANGO_WEIGHT_NORMAL: return 0;
814                         case PANGO_WEIGHT_BOLD: return 1;
815                         default: WARNING("Unknown font weight"); return 0;
816                 }
817
818         case stylehint_Size:
819                 g_object_get(tag, "size", &intval, NULL);
820                 return (intval/2)-14;
821
822         case stylehint_Oblique:
823                 g_object_get(tag, "style", &intval , NULL);
824                 return intval == PANGO_STYLE_ITALIC ? 1 : 0;
825
826         case stylehint_Proportional:
827                 /* Use pango_font_family_is_monospace()? */
828         {
829                 gchar *font_family, *query_font_family;
830                 GtkTextTag *font_tag = g_hash_table_lookup(
831                     wintype == wintype_TextBuffer? glk_data->styles->text_buffer : glk_data->styles->text_grid,
832                     "preformatted");
833                 g_object_get(font_tag, "family", &font_family, NULL);
834                 g_object_get(tag, "family", &query_font_family, NULL);
835                 gint retval = strcmp(font_family, query_font_family)? 0 : 1;
836                 g_free(font_family);
837                 g_free(query_font_family);
838                 return retval;
839         }
840
841         case stylehint_TextColor:
842                 g_object_get(tag, "foreground-gdk", &colval, NULL);
843                 return gdkcolor_to_glkcolor(colval);
844
845         case stylehint_BackColor:
846                 g_object_get(tag, "background-gdk", &colval, NULL);
847                 return gdkcolor_to_glkcolor(colval);
848
849         case stylehint_ReverseColor:
850                 return GPOINTER_TO_INT( g_object_get_data(G_OBJECT(tag), "reverse_color") );
851
852         default:
853                 WARNING("Unknown style hint");
854         }
855         
856         return 0;
857 }
858
859 /**
860  * glk_stylehint_set:
861  * @wintype: The window type to set a style hint on, or %wintype_AllTypes.
862  * @styl: The style to set a hint for.
863  * @hint: The type of style hint, one of the <code>stylehint_</code> constants.
864  * @val: The style hint. The meaning of this depends on @hint.
865  *
866  * Sets a hint about the appearance of one style for a particular type of 
867  * window. You can also set @wintype to %wintype_AllTypes, which sets a hint for 
868  * all types of window.
869  * <note><para>
870  *  There is no equivalent constant to set a hint for all styles of a single 
871  *  window type.
872  * </para></note>
873  */
874 void
875 glk_stylehint_set(glui32 wintype, glui32 styl, glui32 hint, glsi32 val)
876 {
877         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
878
879         GtkTextTag *to_change, *default_tag;
880         if(wintype == wintype_TextBuffer || wintype == wintype_AllTypes) {
881                 to_change = g_hash_table_lookup( glk_data->glk_styles->text_buffer, get_glk_tag_name(styl) );
882                 default_tag = g_hash_table_lookup( glk_data->styles->text_buffer, get_tag_name(styl) );
883                 apply_stylehint_to_tag(to_change, default_tag, wintype_TextBuffer, hint, val);
884         }
885
886         if(wintype == wintype_TextGrid || wintype == wintype_AllTypes) {
887                 to_change = g_hash_table_lookup( glk_data->glk_styles->text_grid, get_glk_tag_name(styl) );
888                 default_tag = g_hash_table_lookup( glk_data->styles->text_grid, get_tag_name(styl) );
889                 apply_stylehint_to_tag(to_change, default_tag, wintype_TextGrid, hint, val);
890         }
891 }
892
893 /**
894  * glk_stylehint_clear:
895  * @wintype: The window type to set a style hint on, or %wintype_AllTypes.
896  * @styl: The style to set a hint for.
897  * @hint: The type of style hint, one of the <code>stylehint_</code> constants.
898  *
899  * Clears a hint about the appearance of one style for a particular type of 
900  * window to its default value. You can also set @wintype to %wintype_AllTypes, 
901  * which clears a hint for all types of window.
902  * <note><para>
903  *  There is no equivalent constant to reset a hint for all styles of a single 
904  *  window type.
905  * </para></note>
906  */
907 void
908 glk_stylehint_clear(glui32 wintype, glui32 styl, glui32 hint)
909 {
910         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
911         GtkTextTag *tag;
912
913         switch(wintype) {
914         case wintype_TextBuffer:
915                 tag = g_hash_table_lookup( glk_data->glk_styles->text_buffer, get_glk_tag_name(styl) );
916                 if(tag) {
917                         glk_stylehint_set( wintype, styl, hint, query_tag(tag, wintype, hint) );
918                 }
919                 break;
920         case wintype_TextGrid:
921                 tag = g_hash_table_lookup( glk_data->glk_styles->text_grid, get_glk_tag_name(styl) );
922                 if(tag) {
923                         glk_stylehint_set( wintype, styl, hint, query_tag(tag, wintype, hint) );
924                 }
925         default:
926                 return;
927         }
928 }
929
930 /**
931  * glk_style_distinguish:
932  * @win: The window in which the styles are to be distinguished.
933  * @styl1: The first style to be distinguished from the second style.
934  * @styl2: The second style to be distinguished from the first style.
935  * 
936  * Decides whether two styles are visually distinguishable in the given window.
937  * The exact meaning of this is left for the library to determine.
938  * <note><title>Chimara</title><para>
939  *   Currently, all styles of one window are assumed to be mutually
940  *   distinguishable.
941  * </para></note>
942  * 
943  * Returns: %TRUE (1) if the two styles are visually distinguishable. If they 
944  * are not, it returns %FALSE (0).
945  */
946 glui32
947 glk_style_distinguish(winid_t win, glui32 styl1, glui32 styl2)
948 {
949         /* FIXME */
950         return styl1 != styl2;
951 }
952
953 /**
954  * glk_style_measure:
955  * @win: The window from which to take the style.
956  * @styl: The style to perform the measurement on.
957  * @hint: The stylehint to measure.
958  * @result: Address to write the result to.
959  * 
960  * Tries to test an attribute of one style in the given window @win. The library
961  * may not be able to determine the attribute; if not, this returns %FALSE (0).
962  * If it can, it returns %TRUE (1) and stores the value in the location pointed
963  * at by @result. 
964  * <note><para>
965  *   As usual, it is legal for @result to be %NULL, although fairly pointless.
966  * </para></note>
967  *
968  * The meaning of the value depends on the hint which was tested:
969  * <variablelist>
970  * <varlistentry>
971  *   <term>%stylehint_Indentation, %stylehint_ParaIndentation</term>
972  *   <listitem><para>The indentation and paragraph indentation. These are in a
973  *   metric which is platform-dependent.</para>
974  *   <note><para>Most likely either characters or pixels.</para></note>
975  *   </listitem>
976  * </varlistentry>
977  * <varlistentry>
978  *   <term>%stylehint_Justification</term>
979  *   <listitem><para>One of the constants %stylehint_just_LeftFlush,
980  *   %stylehint_just_LeftRight, %stylehint_just_Centered, or
981  *   %stylehint_just_RightFlush.</para></listitem>
982  * </varlistentry>
983  * <varlistentry>
984  *   <term>%stylehint_Size</term>
985  *   <listitem><para>The font size. Again, this is in a platform-dependent
986  *   metric.</para>
987  *   <note><para>Pixels, points, or simply 1 if the library does not support
988  *   varying font sizes.</para></note>
989  *   </listitem>
990  * </varlistentry>
991  * <varlistentry>
992  *   <term>%stylehint_Weight</term>
993  *   <listitem><para>1 for heavy-weight fonts (boldface), 0 for normal weight,
994  *   and -1 for light-weight fonts.</para></listitem>
995  * </varlistentry>
996  * <varlistentry>
997  *   <term>%stylehint_Oblique</term>
998  *   <listitem><para>1 for oblique fonts (italic), or 0 for normal angle.</para>
999  *   </listitem>
1000  * </varlistentry>
1001  * <varlistentry>
1002  *   <term>%stylehint_Proportional</term>
1003  *   <listitem><para>1 for proportional-width fonts, or 0 for fixed-width.
1004  *   </para></listitem>
1005  * </varlistentry>
1006  * <varlistentry>
1007  *   <term>%stylehint_TextColor, %stylehint_BackColor</term>
1008  *   <listitem><para>These are values from 0x00000000 to 0x00FFFFFF, encoded as
1009  *   described in <link 
1010  *   linkend="chimara-Suggesting-the-Appearance-of-Styles">Suggesting the
1011  *   Appearance of Styles</link>.</para></listitem>
1012  * </varlistentry>
1013  * <varlistentry>
1014  *   <term>%stylehint_ReverseColor</term>
1015  *   <listitem><para>0 for normal printing, 1 if the foreground and background
1016  *   colors are reversed.</para></listitem>
1017  * </varlistentry>
1018  * </variablelist>
1019  * 
1020  * Returns: TRUE upon successul retrieval, otherwise FALSE.
1021  */
1022 glui32
1023 glk_style_measure(winid_t win, glui32 styl, glui32 hint, glui32 *result)
1024 {
1025         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
1026         GtkTextTag *tag;
1027
1028         switch(win->type) {
1029         case wintype_TextBuffer:
1030                 tag = g_hash_table_lookup( glk_data->glk_styles->text_buffer, get_glk_tag_name(styl) );
1031                 if(result)
1032                         *result = query_tag(tag, win->type, hint);
1033                 break;
1034         case wintype_TextGrid:
1035                 tag = g_hash_table_lookup( glk_data->glk_styles->text_grid, get_glk_tag_name(styl) );
1036                 if(result)
1037                         *result = query_tag(tag, win->type, hint);
1038         default:
1039                 return FALSE;
1040         }
1041
1042         return TRUE;
1043 }
1044
1045 /* Internal function returning the current default font for a window type
1046  * This can be used later for size calculations. Only wintype_TextGrid and wintype_TextBuffer are
1047  * supported for now */
1048 PangoFontDescription *
1049 get_current_font(guint32 wintype)
1050 {
1051         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
1052         GtkTextTag *normal;
1053
1054         switch(wintype) {
1055         case wintype_TextGrid:
1056                 normal = g_hash_table_lookup(glk_data->styles->text_grid, "normal");
1057                 break;
1058         case wintype_TextBuffer:
1059                 normal = g_hash_table_lookup(glk_data->styles->text_buffer, "normal");
1060                 break;
1061         default:
1062                 return NULL;
1063         }
1064
1065         PangoFontDescription *font;
1066         g_object_get( G_OBJECT(normal), "font-desc", &font, NULL );
1067
1068         return font;
1069 }
1070
1071 /* Internal function copying the attributes of a text tag to a pango attribute list */
1072 static void
1073 text_tag_to_attr_list(GtkTextTag *tag, PangoAttrList *list)
1074 {
1075         gboolean set;
1076         GdkColor *foreground, *background;
1077         gchar *string;
1078         PangoFontDescription *font_desc;
1079         gboolean strikethrough;
1080         PangoUnderline underline;
1081
1082         g_object_get(tag, "foreground-set", &set, "foreground-gdk", &foreground, NULL);
1083         if(set) {
1084                 pango_attr_list_insert(
1085                         list,
1086                         pango_attr_foreground_new(foreground->red, foreground->green, foreground->blue)
1087                 );
1088         }
1089         g_object_get(tag, "background-set", &set, "background-gdk", &background, NULL);
1090         if(set) {
1091                 pango_attr_list_insert(
1092                         list,
1093                         pango_attr_background_new(background->red, background->green, background->blue)
1094                 );
1095         }
1096         g_object_get(tag, "language-set", &set, "language", &string, NULL);
1097         if(set) {
1098                 pango_attr_list_insert(
1099                         list,
1100                         pango_attr_language_new( pango_language_from_string(string) )
1101                 );
1102         }
1103
1104         /* Font description updates the following properties simultaniously:
1105          * family, style, weight, variant, stretch, size
1106          */
1107         g_object_get(tag, "font-desc", &font_desc, NULL);
1108         pango_attr_list_insert(
1109                 list,
1110                 pango_attr_font_desc_new(font_desc)
1111         );
1112
1113         g_object_get(tag, "strikethrough-set", &set, "strikethrough", &strikethrough, NULL);
1114         if(set) {
1115                 pango_attr_list_insert(
1116                         list,
1117                         pango_attr_strikethrough_new(strikethrough)
1118                 );
1119         }
1120         g_object_get(tag, "underline-set", &set, "underline", &underline, NULL);
1121         if(set) {
1122                 pango_attr_list_insert(
1123                         list,
1124                         pango_attr_underline_new(underline)
1125                 );
1126         }
1127 }
1128
1129 /* Update pager and reverse video tags */
1130 void
1131 style_update(ChimaraGlk *glk)
1132 {
1133         CHIMARA_GLK_USE_PRIVATE(glk, priv);
1134
1135         GtkTextTag *pager_tag = GTK_TEXT_TAG( g_hash_table_lookup(priv->styles->text_buffer, "pager") );
1136         text_tag_to_attr_list(pager_tag, priv->pager_attr_list);
1137 }