5 extern GPrivate *glk_data_key;
7 static gboolean style_accept(GScanner *scanner, GTokenType token);
8 static gboolean style_accept_style_selector(GScanner *scanner);
9 static gboolean style_accept_style_hint(GScanner *scanner, GtkTextTag *current_tag);
10 static void style_add_tag_to_textbuffer(gpointer key, gpointer tag, gpointer tag_table);
11 static void style_table_copy(gpointer key, gpointer tag, gpointer target_table);
12 static GtkTextTag* gtk_text_tag_copy(GtkTextTag *tag);
16 * @styl: The style to apply
18 * Changes the style of the current output stream. @styl should be one of the
19 * <code>style_</code> constants listed above. However, any value is actually
20 * legal; if the interpreter does not recognize the style value, it will treat
21 * it as %style_Normal.
23 * This policy allows for the future definition of styles without breaking old
28 glk_set_style(glui32 styl)
30 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
31 g_return_if_fail(glk_data->current_stream != NULL);
32 glk_set_style_stream(glk_data->current_stream, styl);
35 static const gchar* TAG_NAMES[] = {
49 /* Internal function: mapping from style enum to tag name */
51 get_tag_name(glui32 style)
53 if(style >= style_NUMSTYLES) {
54 WARNING("Unsupported style");
57 return (gchar*) TAG_NAMES[style];
62 * glk_set_style_stream:
63 * @str: Output stream to change the style of
64 * @styl: The style to apply
66 * This changes the style of the stream @str. See glk_set_style().
69 glk_set_style_stream(strid_t str, glui32 styl) {
70 str->style = get_tag_name(styl);
73 /* Internal function: call this to initialize the default styles to a textbuffer. */
75 style_init_textbuffer(GtkTextBuffer *buffer)
77 g_return_if_fail(buffer != NULL);
79 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
80 if( G_UNLIKELY(!glk_data->style_initialized) ) {
83 g_hash_table_foreach(glk_data->current_styles->text_buffer, style_add_tag_to_textbuffer, gtk_text_buffer_get_tag_table(buffer));
86 /* Internal function: call this to initialize the default styles to a textgrid. */
88 style_init_textgrid(GtkTextBuffer *buffer)
90 g_return_if_fail(buffer != NULL);
92 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
93 if( G_UNLIKELY(!glk_data->style_initialized) ) {
96 g_hash_table_foreach(glk_data->current_styles->text_grid, style_add_tag_to_textbuffer, gtk_text_buffer_get_tag_table(buffer));
99 /* Internal function used to iterate over the default text tag table, applying them to a textbuffer */
101 style_add_tag_to_textbuffer(gpointer key, gpointer tag, gpointer tag_table)
103 gtk_text_tag_table_add( tag_table, gtk_text_tag_copy(tag) );
106 /* Internal function used to iterate over a style table, copying it */
108 style_table_copy(gpointer key, gpointer tag, gpointer target_table)
110 g_return_if_fail(key != NULL);
111 g_return_if_fail(tag != NULL);
112 g_return_if_fail(target_table != NULL);
114 g_hash_table_insert(target_table, key, gtk_text_tag_copy( GTK_TEXT_TAG(tag) ));
117 /* Internal function that copies a text tag */
119 gtk_text_tag_copy(GtkTextTag *tag)
123 g_return_val_if_fail(tag != NULL, NULL);
125 copy = gtk_text_tag_new(tag->name);
126 gtk_text_attributes_copy_values(tag->values, copy->values);
128 #define _COPY_FLAG(flag) copy->flag = tag->flag
129 _COPY_FLAG (bg_color_set);
130 _COPY_FLAG (bg_color_set);
131 _COPY_FLAG (bg_stipple_set);
132 _COPY_FLAG (fg_color_set);
133 _COPY_FLAG (fg_stipple_set);
134 _COPY_FLAG (justification_set);
135 _COPY_FLAG (left_margin_set);
136 _COPY_FLAG (indent_set);
137 _COPY_FLAG (rise_set);
138 _COPY_FLAG (strikethrough_set);
139 _COPY_FLAG (right_margin_set);
140 _COPY_FLAG (pixels_above_lines_set);
141 _COPY_FLAG (pixels_below_lines_set);
142 _COPY_FLAG (pixels_inside_wrap_set);
143 _COPY_FLAG (tabs_set);
144 _COPY_FLAG (underline_set);
145 _COPY_FLAG (wrap_mode_set);
146 _COPY_FLAG (bg_full_height_set);
147 _COPY_FLAG (invisible_set);
148 _COPY_FLAG (editable_set);
149 _COPY_FLAG (language_set);
155 /* Internal function that reads the default styles from a CSS file */
159 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
160 GHashTable *default_text_grid_styles = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_object_unref);
161 GHashTable *default_text_buffer_styles = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_object_unref);
162 GHashTable *current_text_grid_styles = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_object_unref);
163 GHashTable *current_text_buffer_styles = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_object_unref);
166 /* Create the CSS file scanner */
167 GScanner *scanner = g_scanner_new(NULL);
169 int f = open(glk_data->css_file, O_RDONLY);
170 g_return_if_fail(f != -1);
171 g_scanner_input_file(scanner, f);
172 scanner->input_name = glk_data->css_file;
173 scanner->config->cset_identifier_first = G_CSET_a_2_z G_CSET_A_2_Z "#";
174 scanner->config->cset_identifier_nth = G_CSET_a_2_z G_CSET_A_2_Z "-_" G_CSET_DIGITS;
175 scanner->config->symbol_2_token = TRUE;
176 scanner->config->cpair_comment_single = NULL;
177 scanner->config->scan_float = FALSE;
179 /* Initialise the default styles */
180 g_hash_table_insert(default_text_grid_styles, "normal", gtk_text_tag_new("normal"));
182 tag = gtk_text_tag_new("emphasized");
183 g_object_set(tag, "style", PANGO_STYLE_ITALIC, "style-set", TRUE, NULL);
184 g_hash_table_insert(default_text_grid_styles, "emphasized", tag);
186 tag = gtk_text_tag_new("preformatted");
187 g_object_set(tag, "font-desc", glk_data->monospace_font_desc, NULL);
188 g_hash_table_insert(default_text_grid_styles, "preformatted", tag);
190 tag = gtk_text_tag_new("header");
191 g_object_set(tag, "size-points", 18.0, "weight", PANGO_WEIGHT_BOLD, NULL);
192 g_hash_table_insert(default_text_grid_styles, "header", tag);
194 tag = gtk_text_tag_new("subheader");
195 g_object_set(tag, "size-points", 14.0, "weight", PANGO_WEIGHT_BOLD, NULL);
196 g_hash_table_insert(default_text_grid_styles, "subheader", tag);
198 tag = gtk_text_tag_new("alert");
199 g_object_set(tag, "foreground", "#aa0000", "weight", PANGO_WEIGHT_BOLD, NULL);
200 g_hash_table_insert(default_text_grid_styles, "alert", tag);
202 tag = gtk_text_tag_new("note");
203 g_object_set(tag, "foreground", "#aaaa00", "weight", PANGO_WEIGHT_BOLD, NULL);
204 g_hash_table_insert(default_text_grid_styles, "note", tag);
206 tag = gtk_text_tag_new("block-quote");
207 g_object_set(tag, "justification", GTK_JUSTIFY_CENTER, "style", PANGO_STYLE_ITALIC, NULL);
208 g_hash_table_insert(default_text_grid_styles, "block-quote", tag);
210 g_hash_table_insert(default_text_grid_styles, "input", gtk_text_tag_new("input"));
211 g_hash_table_insert(default_text_grid_styles, "user1", gtk_text_tag_new("user1"));
212 g_hash_table_insert(default_text_grid_styles, "user2", gtk_text_tag_new("user2"));
214 g_hash_table_foreach(default_text_grid_styles, style_table_copy, default_text_buffer_styles);
215 glk_data->default_styles->text_grid = default_text_grid_styles;
216 glk_data->default_styles->text_buffer = default_text_buffer_styles;
218 /* Run the scanner over the CSS file, overriding defaults */
219 while( g_scanner_peek_next_token(scanner) != G_TOKEN_EOF) {
220 if( !style_accept_style_selector(scanner) )
224 /* Set the current style to a copy of the default style */
225 g_hash_table_foreach(default_text_grid_styles, style_table_copy, current_text_grid_styles);
226 g_hash_table_foreach(default_text_buffer_styles, style_table_copy, current_text_buffer_styles);
227 glk_data->current_styles->text_grid = current_text_grid_styles;
228 glk_data->current_styles->text_buffer = current_text_buffer_styles;
230 g_scanner_destroy(scanner);
232 glk_data->style_initialized = TRUE;
235 /* Internal function: parses a token */
237 style_accept(GScanner *scanner, GTokenType token)
239 GTokenType next = g_scanner_get_next_token(scanner);
241 g_scanner_unexp_token(scanner, token, NULL, NULL, NULL, "CSS Error", 1);
248 /* Internal function: parses a style selector */
250 style_accept_style_selector(GScanner *scanner)
252 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
254 GtkTextTag *current_tag;
256 GTokenType token = g_scanner_get_next_token(scanner);
257 GTokenValue value = g_scanner_cur_value(scanner);
260 token != G_TOKEN_IDENTIFIER ||
261 ( strcmp(value.v_identifier, "buffer") && strcmp(value.v_identifier, "grid") )
263 g_scanner_error(scanner, "CSS Error: buffer/grid expected");
267 field = g_strdup(value.v_identifier);
269 if( !style_accept(scanner, '.') )
272 token = g_scanner_get_next_token(scanner);
273 value = g_scanner_cur_value(scanner);
275 if(token != G_TOKEN_IDENTIFIER) {
276 g_scanner_error(scanner, "CSS Error: style selector expected");
280 if( !strcmp(field, "buffer") )
281 current_tag = g_hash_table_lookup(glk_data->default_styles->text_buffer, value.v_identifier);
283 current_tag = g_hash_table_lookup(glk_data->default_styles->text_grid, value.v_identifier);
285 if(current_tag == NULL) {
286 g_scanner_error(scanner, "CSS Error: invalid style identifier");
290 if( !style_accept(scanner, '{') )
293 while( g_scanner_peek_next_token(scanner) != '}') {
294 if( !style_accept_style_hint(scanner, current_tag) )
298 if( !style_accept(scanner, '}') )
304 /* Internal function: parses a style hint */
306 style_accept_style_hint(GScanner *scanner, GtkTextTag *current_tag)
308 GTokenType token = g_scanner_get_next_token(scanner);
309 GTokenValue value = g_scanner_cur_value(scanner);
312 if(token != G_TOKEN_IDENTIFIER) {
313 g_scanner_error(scanner, "CSS Error: style hint expected");
317 hint = g_strdup(value.v_identifier);
319 if( !style_accept(scanner, ':') )
322 token = g_scanner_get_next_token(scanner);
323 value = g_scanner_cur_value(scanner);
325 if( !strcmp(hint, "font-family") ) {
326 if(token != G_TOKEN_STRING) {
327 g_scanner_error(scanner, "CSS Error: string expected");
330 g_object_set(current_tag, "family", value.v_string, "family-set", TRUE, NULL);
332 else if( !strcmp(hint, "font-weight") ) {
333 if(token != G_TOKEN_IDENTIFIER) {
334 g_scanner_error(scanner, "CSS Error: bold/normal expected");
338 if( !strcmp(value.v_identifier, "bold") )
339 g_object_set(current_tag, "weight", PANGO_WEIGHT_BOLD, "weight-set", TRUE, NULL);
340 else if( !strcmp(value.v_identifier, "normal") )
341 g_object_set(current_tag, "weight", PANGO_WEIGHT_NORMAL, "weight-set", TRUE, NULL);
343 g_scanner_error(scanner, "CSS Error: bold/normal expected");
347 else if( !strcmp(hint, "font-style") ) {
348 if(token != G_TOKEN_IDENTIFIER) {
349 g_scanner_error(scanner, "CSS Error: italic/normal expected");
353 if( !strcmp(value.v_identifier, "italic") )
354 g_object_set(current_tag, "style", PANGO_STYLE_ITALIC, "style-set", TRUE, NULL);
355 else if( !strcmp(value.v_identifier, "normal") )
356 g_object_set(current_tag, "style", PANGO_STYLE_NORMAL, "style-set", TRUE, NULL);
358 g_scanner_error(scanner, "CSS Error: italic/normal expected");
362 else if( !strcmp(hint, "font-size") ) {
363 if(token == G_TOKEN_INT)
364 g_object_set(current_tag, "size-points", (float)value.v_int, "size-set", TRUE, NULL);
365 else if(token == G_TOKEN_FLOAT)
366 g_object_set(current_tag, "size-points", value.v_float, "size-set", TRUE, NULL);
368 g_scanner_error(scanner, "CSS Error: integer or float expected");
372 else if( !strcmp(hint, "color") ) {
373 if(token != G_TOKEN_IDENTIFIER) {
374 g_scanner_error(scanner, "CSS Error: hex color expected");
378 g_object_set(current_tag, "foreground", value.v_identifier, "foreground-set", TRUE, NULL);
380 else if( !strcmp(hint, "background-color") ) {
381 if(token != G_TOKEN_IDENTIFIER) {
382 g_scanner_error(scanner, "CSS Error: hex color expected");
385 g_object_set(current_tag, "background", value.v_identifier, "background-set", TRUE, NULL);
387 else if( !strcmp(hint, "text-align") ) {
388 if(token != G_TOKEN_IDENTIFIER) {
389 g_scanner_error(scanner, "CSS Error: left/right/center expected");
393 if( !strcmp(value.v_identifier, "left") )
394 g_object_set(current_tag, "justification", GTK_JUSTIFY_LEFT, "justification-set", TRUE, NULL);
395 else if( !strcmp(value.v_identifier, "right") )
396 g_object_set(current_tag, "justification", GTK_JUSTIFY_RIGHT, "justification-set", TRUE, NULL);
397 else if( !strcmp(value.v_identifier, "center") )
398 g_object_set(current_tag, "justification", GTK_JUSTIFY_CENTER, "justification-set", TRUE, NULL);
400 g_scanner_error(scanner, "CSS Error: left/right/center expected");
404 else if( !strcmp(hint, "margin-left") ) {
405 if(token != G_TOKEN_INT) {
406 g_scanner_error(scanner, "CSS Error: integer expected");
409 g_object_set(current_tag, "left-margin", value.v_int, "left-margin-set", TRUE, NULL);
411 else if( !strcmp(hint, "margin-right") ) {
412 if(token != G_TOKEN_INT) {
413 g_scanner_error(scanner, "CSS Error: integer expected");
416 g_object_set(current_tag, "right-margin", value.v_int, "right-margin-set", TRUE, NULL);
418 else if( !strcmp(hint, "margin-top") ) {
419 if(token != G_TOKEN_INT) {
420 g_scanner_error(scanner, "CSS Error: integer expected");
423 g_object_set(current_tag, "pixels-above-lines", value.v_int, "pixels-above-lines-set", TRUE, NULL);
425 else if( !strcmp(hint, "margin-bottom") ) {
426 if(token != G_TOKEN_INT) {
427 g_scanner_error(scanner, "CSS Error: integer expected");
430 g_object_set(current_tag, "pixels-below-lines", value.v_int, "pixels-below-lines-set", TRUE, NULL);
434 g_scanner_error(scanner, "CSS Error: invalid style hint %s", hint);
438 if( !style_accept(scanner, ';') )
444 /* Internal function: parses a glk color to a #hex-value */
446 color_format(glui32 val, gchar *buffer)
448 g_return_if_fail(buffer != NULL);
450 sprintf(buffer, "#%02X%02X%02X",
451 ((val & 0xff0000) >> 16),
452 ((val & 0x00ff00) >> 8),
457 /* Internal function: parses a GdkColor to a glk color */
459 color_parse_gdk(GdkColor *color)
461 g_return_val_if_fail(color != NULL, 0);
462 return (glui32) color->pixel;
465 /* Internal function: changes a GTK tag to correspond with the given style. */
467 apply_stylehint_to_tag(GtkTextTag *tag, glui32 hint, glsi32 val)
469 g_return_if_fail(tag != NULL);
471 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
472 GObject *tag_object = G_OBJECT(tag);
473 gint reverse_color = 0;
475 /* FIXME where should we keep track of this?
476 g_object_get(tag, "reverse_color", &reverse_color, NULL);
482 case stylehint_Indentation:
483 g_object_set(tag_object, "left-margin", 5*val, "left-margin-set", TRUE, NULL);
484 g_object_set(tag_object, "right-margin", 5*val, "right-margin-set", TRUE, NULL);
487 case stylehint_ParaIndentation:
488 g_object_set(tag_object, "indent", 5*val, "indent-set", TRUE, NULL);
491 case stylehint_Justification:
493 case stylehint_just_LeftFlush: i = GTK_JUSTIFY_LEFT; break;
494 case stylehint_just_LeftRight: i = GTK_JUSTIFY_FILL; break;
495 case stylehint_just_Centered: i = GTK_JUSTIFY_CENTER; break;
496 case stylehint_just_RightFlush: i = GTK_JUSTIFY_RIGHT; break;
498 WARNING("Unknown justification");
499 i = GTK_JUSTIFY_LEFT;
501 g_object_set(tag_object, "justification", i, "justification-set", TRUE, NULL);
504 case stylehint_Weight:
506 case -1: i = PANGO_WEIGHT_LIGHT; break;
507 case 0: i = PANGO_WEIGHT_NORMAL; break;
508 case 1: i = PANGO_WEIGHT_BOLD; break;
509 default: WARNING("Unknown font weight");
511 g_object_set(tag_object, "weight", i, "weight-set", TRUE, NULL);
515 g_object_set(tag_object, "size", 14+(2*val), "size-set", TRUE, NULL);
518 case stylehint_Oblique:
519 g_object_set(tag_object, "style", val ? PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL, "style-set", TRUE, NULL);
522 case stylehint_Proportional:
523 g_object_set(tag_object, "font-desc", val ? glk_data->default_font_desc : glk_data->monospace_font_desc, NULL);
526 case stylehint_TextColor:
527 color_format(val, color);
530 g_object_set(tag_object, "foreground", color, "foreground-set", TRUE, NULL);
532 g_object_set(tag_object, "background", color, "background-set", TRUE, NULL);
536 case stylehint_BackColor:
537 color_format(val, color);
540 g_object_set(tag_object, "background", color, "background-set", TRUE, NULL);
542 g_object_set(tag_object, "foreground", color, "background-set", TRUE, NULL);
546 case stylehint_ReverseColor:
547 if(reverse_color != val) {
548 /* Flip the fore- and background colors */
549 GdkColor* foreground_color;
550 GdkColor* background_color;
551 gint f_set, b_set = 0;
552 g_object_get(tag_object, "foreground-set", &f_set, "background-set", &b_set, NULL);
555 g_object_get(tag_object, "foreground-gdk", &foreground_color, NULL);
557 g_object_get(tag_object, "background-gdk", &background_color, NULL);
560 g_object_set(tag_object, "foreground-gdk", background_color, NULL);
562 g_object_set(tag_object, "foreground", "#ffffff", NULL);
565 g_object_set(tag_object, "background-gdk", foreground_color, NULL);
567 g_object_set(tag_object, "background", "#000000", NULL);
572 WARNING("Unknown style hint");
575 /*Internal function: queries a text tag for the value of a given style hint */
577 query_tag(GtkTextTag *tag, glui32 hint)
583 g_return_val_if_fail(tag != NULL, 0);
585 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
588 case stylehint_Indentation:
589 g_object_get(tag, "left_margin", &intval, NULL);
593 case stylehint_ParaIndentation:
594 g_object_get(tag, "indent", &intval, NULL);
598 case stylehint_Justification:
599 g_object_get(tag, "justification", &intval, NULL);
601 case GTK_JUSTIFY_LEFT: return stylehint_just_LeftFlush; break;
602 case GTK_JUSTIFY_FILL: return stylehint_just_LeftRight; break;
603 case GTK_JUSTIFY_CENTER: return stylehint_just_Centered; break;
604 case GTK_JUSTIFY_RIGHT: return stylehint_just_RightFlush; break;
606 WARNING("Unknown justification");
607 return stylehint_just_LeftFlush;
611 case stylehint_Weight:
612 g_object_get(tag, "weight", &intval, NULL);
614 case PANGO_WEIGHT_LIGHT: return -1; break;
615 case PANGO_WEIGHT_NORMAL: return 0; break;
616 case PANGO_WEIGHT_BOLD: return 1; break;
617 default: WARNING("Unknown font weight"); return 0;
622 g_object_get(tag, "size", &intval, NULL);
623 return (intval/2)-14;
626 case stylehint_Oblique:
627 g_object_get(tag, "style", &intval , NULL);
628 return intval == PANGO_STYLE_ITALIC ? 1 : 0;
631 case stylehint_Proportional:
632 g_object_get(tag, "font-desc", &objval, NULL);
633 return objval == (GObject *)glk_data->monospace_font_desc ? 0 : 1;
636 case stylehint_TextColor:
637 g_object_get(tag, "foreground-gdk", &colval, NULL);
638 return color_parse_gdk(colval);
641 case stylehint_BackColor:
642 g_object_get(tag, "background-gdk", &colval, NULL);
643 return color_parse_gdk(colval);
646 case stylehint_ReverseColor:
647 /* FIXME: implement this */
652 WARNING("Unknown style hint");
660 * @wintype: The window type to set a style hint on, or %wintype_AllTypes.
661 * @styl: The style to set a hint for.
662 * @hint: The type of style hint, one of the <code>stylehint_</code> constants.
663 * @val: The style hint. The meaning of this depends on @hint.
665 * Sets a hint about the appearance of one style for a particular type of
666 * window. You can also set wintype to %wintype_AllTypes, which sets a hint for
667 * all types of window.
669 * There is no equivalent constant to set a hint for all styles of a single
674 glk_stylehint_set(glui32 wintype, glui32 styl, glui32 hint, glsi32 val)
676 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
678 if( G_UNLIKELY(!glk_data->style_initialized) ) {
682 GtkTextTag *to_change;
683 if(wintype == wintype_TextBuffer || wintype == wintype_AllTypes) {
684 to_change = g_hash_table_lookup( glk_data->current_styles->text_buffer, get_tag_name(styl) );
685 apply_stylehint_to_tag(to_change, hint, val);
688 if(wintype == wintype_TextGrid || wintype == wintype_AllTypes) {
689 to_change = g_hash_table_lookup( glk_data->current_styles->text_grid, get_tag_name(styl) );
690 apply_stylehint_to_tag(to_change, hint, val);
695 glk_stylehint_clear(glui32 wintype, glui32 styl, glui32 hint)
697 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
701 case wintype_TextBuffer:
702 tag = g_hash_table_lookup( glk_data->default_styles->text_buffer, get_tag_name(styl) );
703 glk_stylehint_set( wintype, styl, hint, query_tag(tag, hint) );
705 case wintype_TextGrid:
706 tag = g_hash_table_lookup( glk_data->default_styles->text_grid, get_tag_name(styl) );
707 glk_stylehint_set( wintype, styl, hint, query_tag(tag, hint) );
714 glk_style_distinguish(winid_t win, glui32 styl1, glui32 styl2)
716 return styl1 != styl2;
720 glk_style_measure(winid_t win, glui32 styl, glui32 hint, glui32 *result)
722 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
726 case wintype_TextBuffer:
727 tag = g_hash_table_lookup( glk_data->current_styles->text_buffer, get_tag_name(styl) );
728 *result = query_tag(tag, hint);
730 case wintype_TextGrid:
731 tag = g_hash_table_lookup( glk_data->current_styles->text_grid, get_tag_name(styl) );
732 *result = query_tag(tag, hint);