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