4 #include "chimara-glk-private.h"
11 extern GPrivate *glk_data_key;
13 static gboolean style_accept(GScanner *scanner, GTokenType token);
14 static gboolean style_accept_style_selector(GScanner *scanner);
15 static gboolean style_accept_style_hint(GScanner *scanner, GtkTextTag *current_tag);
16 static void style_add_tag_to_textbuffer(gpointer key, gpointer tag, gpointer tag_table);
17 static void style_table_copy(gpointer key, gpointer tag, gpointer target_table);
18 static void text_tag_to_attr_list(GtkTextTag *tag, PangoAttrList *list);
19 GtkTextTag* gtk_text_tag_copy(GtkTextTag *tag);
23 * @styl: The style to apply
25 * Changes the style of the current output stream. @styl should be one of the
26 * <code>style_</code> constants listed above. However, any value is actually
27 * legal; if the interpreter does not recognize the style value, it will treat
28 * it as %style_Normal.
30 * This policy allows for the future definition of styles without breaking old
35 glk_set_style(glui32 styl)
37 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
38 g_return_if_fail(glk_data->current_stream != NULL);
39 glk_set_style_stream(glk_data->current_stream, styl);
42 static const gchar* TAG_NAMES[] = {
56 /* Internal function: mapping from style enum to tag name */
58 get_tag_name(glui32 style)
60 if(style >= style_NUMSTYLES) {
61 WARNING("Unsupported style");
64 return (gchar*) TAG_NAMES[style];
69 * glk_set_style_stream:
70 * @str: Output stream to change the style of
71 * @styl: The style to apply
73 * This changes the style of the stream @str. See glk_set_style().
76 glk_set_style_stream(strid_t str, glui32 styl) {
77 if(str->window == NULL)
80 flush_window_buffer(str->window);
81 str->style = get_tag_name(styl);
84 /* Internal function: call this to initialize the layout of the 'more' prompt. */
86 style_init_more_prompt(winid_t win)
88 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
90 win->pager_layout = gtk_widget_create_pango_layout(win->widget, "More");
91 pango_layout_set_attributes(win->pager_layout, glk_data->pager_attr_list);
94 /* Internal function: call this to initialize the default styles to a textbuffer. */
96 style_init_textbuffer(GtkTextBuffer *buffer)
98 g_return_if_fail(buffer != NULL);
100 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
101 if( G_UNLIKELY(!glk_data->style_initialized) ) {
105 /* Copy the current text tags to the textbuffer's tag table */
106 g_hash_table_foreach(glk_data->current_styles->text_buffer, style_add_tag_to_textbuffer, gtk_text_buffer_get_tag_table(buffer));
110 /* Internal function: call this to initialize the default styles to a textgrid. */
112 style_init_textgrid(GtkTextBuffer *buffer)
114 g_return_if_fail(buffer != NULL);
116 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
117 if( G_UNLIKELY(!glk_data->style_initialized) ) {
121 /* Copy the current text tags to the textgrid's tag table */
122 g_hash_table_foreach(glk_data->current_styles->text_grid, style_add_tag_to_textbuffer, gtk_text_buffer_get_tag_table(buffer));
125 /* Internal function used to iterate over the default text tag table, applying them to a textbuffer */
127 style_add_tag_to_textbuffer(gpointer key, gpointer tag, gpointer tag_table)
129 gtk_text_tag_table_add( tag_table, gtk_text_tag_copy(tag) );
132 /* Internal function used to iterate over a style table, copying it */
134 style_table_copy(gpointer key, gpointer tag, gpointer target_table)
136 g_return_if_fail(key != NULL);
137 g_return_if_fail(tag != NULL);
138 g_return_if_fail(target_table != NULL);
140 g_hash_table_insert(target_table, key, gtk_text_tag_copy( GTK_TEXT_TAG(tag) ));
143 /* Internal function that copies a text tag */
145 gtk_text_tag_copy(GtkTextTag *tag)
149 g_return_val_if_fail(tag != NULL, NULL);
151 copy = gtk_text_tag_new(tag->name);
152 gtk_text_attributes_copy_values(tag->values, copy->values);
154 #define _COPY_FLAG(flag) copy->flag = tag->flag
155 _COPY_FLAG (bg_color_set);
156 _COPY_FLAG (bg_color_set);
157 _COPY_FLAG (bg_stipple_set);
158 _COPY_FLAG (fg_color_set);
159 _COPY_FLAG (fg_stipple_set);
160 _COPY_FLAG (justification_set);
161 _COPY_FLAG (left_margin_set);
162 _COPY_FLAG (indent_set);
163 _COPY_FLAG (rise_set);
164 _COPY_FLAG (strikethrough_set);
165 _COPY_FLAG (right_margin_set);
166 _COPY_FLAG (pixels_above_lines_set);
167 _COPY_FLAG (pixels_below_lines_set);
168 _COPY_FLAG (pixels_inside_wrap_set);
169 _COPY_FLAG (tabs_set);
170 _COPY_FLAG (underline_set);
171 _COPY_FLAG (wrap_mode_set);
172 _COPY_FLAG (bg_full_height_set);
173 _COPY_FLAG (invisible_set);
174 _COPY_FLAG (editable_set);
175 _COPY_FLAG (language_set);
178 /* Copy the data that was added manually */
179 gpointer reverse_color = g_object_get_data( G_OBJECT(tag), "reverse_color" );
182 g_object_set_data( G_OBJECT(copy), "reverse_color", reverse_color );
187 /* Internal function that reads the default styles from a CSS file */
191 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
192 GHashTable *default_text_grid_styles = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_object_unref);
193 GHashTable *default_text_buffer_styles = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_object_unref);
194 GHashTable *current_text_grid_styles = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_object_unref);
195 GHashTable *current_text_buffer_styles = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_object_unref);
198 /* Initialise the default styles for a text grid */
199 tag = gtk_text_tag_new("normal");
200 g_object_set(tag, "font-desc", glk_data->monospace_font_desc, NULL);
201 g_hash_table_insert(default_text_grid_styles, "normal", tag);
203 tag = gtk_text_tag_new("emphasized");
204 g_object_set(tag, "style", PANGO_STYLE_ITALIC, "style-set", TRUE, NULL);
205 g_hash_table_insert(default_text_grid_styles, "emphasized", tag);
207 tag = gtk_text_tag_new("preformatted");
208 g_object_set(tag, "font-desc", glk_data->monospace_font_desc, NULL);
209 g_hash_table_insert(default_text_grid_styles, "preformatted", tag);
211 tag = gtk_text_tag_new("header");
212 g_object_set(tag, "weight", PANGO_WEIGHT_BOLD, NULL);
213 g_hash_table_insert(default_text_grid_styles, "header", tag);
215 tag = gtk_text_tag_new("subheader");
216 g_object_set(tag, "weight", PANGO_WEIGHT_BOLD, NULL);
217 g_hash_table_insert(default_text_grid_styles, "subheader", tag);
219 tag = gtk_text_tag_new("alert");
220 g_object_set(tag, "foreground", "#aa0000", "weight", PANGO_WEIGHT_BOLD, NULL);
221 g_hash_table_insert(default_text_grid_styles, "alert", tag);
223 tag = gtk_text_tag_new("note");
224 g_object_set(tag, "foreground", "#aaaa00", "weight", PANGO_WEIGHT_BOLD, NULL);
225 g_hash_table_insert(default_text_grid_styles, "note", tag);
227 tag = gtk_text_tag_new("block-quote");
228 g_object_set(tag, "style", PANGO_STYLE_ITALIC, "style-set", TRUE, NULL);
229 g_hash_table_insert(default_text_grid_styles, "block-quote", tag);
231 g_hash_table_insert(default_text_grid_styles, "input", gtk_text_tag_new("input"));
232 g_hash_table_insert(default_text_grid_styles, "user1", gtk_text_tag_new("user1"));
233 g_hash_table_insert(default_text_grid_styles, "user2", gtk_text_tag_new("user2"));
235 tag = gtk_text_tag_new("hyperlink");
236 g_object_set(tag, "foreground", "#0000ff", "underline", PANGO_UNDERLINE_SINGLE, "underline-set", TRUE, NULL);
237 g_hash_table_insert(default_text_grid_styles, "hyperlink", tag);
239 /* Tags for the textbuffer */
240 tag = gtk_text_tag_new("normal");
241 g_object_set(tag, "font-desc", glk_data->default_font_desc, NULL);
242 g_hash_table_insert(default_text_buffer_styles, "normal", tag);
244 tag = gtk_text_tag_new("emphasized");
245 g_object_set(tag, "style", PANGO_STYLE_ITALIC, "style-set", TRUE, NULL);
246 g_hash_table_insert(default_text_buffer_styles, "emphasized", tag);
248 tag = gtk_text_tag_new("preformatted");
249 g_object_set(tag, "font-desc", glk_data->monospace_font_desc, NULL);
250 g_hash_table_insert(default_text_buffer_styles, "preformatted", tag);
252 tag = gtk_text_tag_new("header");
253 g_object_set(tag, "size-points", 18.0, "weight", PANGO_WEIGHT_BOLD, NULL);
254 g_hash_table_insert(default_text_buffer_styles, "header", tag);
256 tag = gtk_text_tag_new("subheader");
257 g_object_set(tag, "size-points", 14.0, "weight", PANGO_WEIGHT_BOLD, NULL);
258 g_hash_table_insert(default_text_buffer_styles, "subheader", tag);
260 tag = gtk_text_tag_new("alert");
261 g_object_set(tag, "foreground", "#aa0000", "weight", PANGO_WEIGHT_BOLD, NULL);
262 g_hash_table_insert(default_text_buffer_styles, "alert", tag);
264 tag = gtk_text_tag_new("note");
265 g_object_set(tag, "foreground", "#aaaa00", "weight", PANGO_WEIGHT_BOLD, NULL);
266 g_hash_table_insert(default_text_buffer_styles, "note", tag);
268 tag = gtk_text_tag_new("block-quote");
269 g_object_set(tag, "justification", GTK_JUSTIFY_CENTER, "style", PANGO_STYLE_ITALIC, "style-set", TRUE, NULL);
270 g_hash_table_insert(default_text_buffer_styles, "block-quote", tag);
272 g_hash_table_insert(default_text_buffer_styles, "input", gtk_text_tag_new("input"));
273 g_hash_table_insert(default_text_buffer_styles, "user1", gtk_text_tag_new("user1"));
274 g_hash_table_insert(default_text_buffer_styles, "user2", gtk_text_tag_new("user2"));
276 tag = gtk_text_tag_new("hyperlink");
277 g_object_set(tag, "foreground", "#0000ff", "underline", PANGO_UNDERLINE_SINGLE, "underline-set", TRUE, NULL);
278 g_hash_table_insert(default_text_buffer_styles, "hyperlink", tag);
280 GtkTextTag *pager_tag = gtk_text_tag_new("pager");
281 g_object_set(pager_tag, "foreground", "#ffffff", "background", "#000000", NULL);
282 g_hash_table_insert(default_text_buffer_styles, "pager", pager_tag);
284 glk_data->default_styles->text_grid = default_text_grid_styles;
285 glk_data->default_styles->text_buffer = default_text_buffer_styles;
287 /* Create the CSS file scanner */
288 GScanner *scanner = g_scanner_new(NULL);
290 int f = open(glk_data->css_file, O_RDONLY);
293 g_scanner_input_file(scanner, f);
294 scanner->input_name = glk_data->css_file;
295 scanner->config->cset_identifier_first = G_CSET_a_2_z G_CSET_A_2_Z "#";
296 scanner->config->cset_identifier_nth = G_CSET_a_2_z G_CSET_A_2_Z "-_" G_CSET_DIGITS;
297 scanner->config->symbol_2_token = TRUE;
298 scanner->config->cpair_comment_single = NULL;
299 scanner->config->scan_float = FALSE;
301 /* Run the scanner over the CSS file, overriding defaults */
302 while( g_scanner_peek_next_token(scanner) != G_TOKEN_EOF) {
303 if( !style_accept_style_selector(scanner) )
307 g_scanner_destroy(scanner);
310 g_warning("Could not find CSS file");
312 /* Set the current style to a copy of the default style */
313 g_hash_table_foreach(default_text_grid_styles, style_table_copy, current_text_grid_styles);
314 g_hash_table_foreach(default_text_buffer_styles, style_table_copy, current_text_buffer_styles);
315 glk_data->current_styles->text_grid = current_text_grid_styles;
316 glk_data->current_styles->text_buffer = current_text_buffer_styles;
318 text_tag_to_attr_list(pager_tag, glk_data->pager_attr_list);
320 glk_data->style_initialized = TRUE;
323 /* Internal function: parses a token */
325 style_accept(GScanner *scanner, GTokenType token)
327 GTokenType next = g_scanner_get_next_token(scanner);
329 g_scanner_unexp_token(scanner, token, NULL, NULL, NULL, "CSS Error", 1);
336 /* Internal function: parses a style selector */
338 style_accept_style_selector(GScanner *scanner)
340 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
342 GtkTextTag *current_tag;
344 GTokenType token = g_scanner_get_next_token(scanner);
345 GTokenValue value = g_scanner_cur_value(scanner);
348 token != G_TOKEN_IDENTIFIER ||
349 ( strcmp(value.v_identifier, "buffer") && strcmp(value.v_identifier, "grid") )
351 g_scanner_error(scanner, "CSS Error: buffer/grid expected");
355 field = g_strdup(value.v_identifier);
357 if( !style_accept(scanner, '.') )
360 token = g_scanner_get_next_token(scanner);
361 value = g_scanner_cur_value(scanner);
363 if(token != G_TOKEN_IDENTIFIER) {
364 g_scanner_error(scanner, "CSS Error: style selector expected");
368 if( !strcmp(field, "buffer") )
369 current_tag = g_hash_table_lookup(glk_data->default_styles->text_buffer, value.v_identifier);
371 current_tag = g_hash_table_lookup(glk_data->default_styles->text_grid, value.v_identifier);
373 if(current_tag == NULL) {
374 g_scanner_error(scanner, "CSS Error: invalid style identifier");
378 if( !style_accept(scanner, '{') )
381 while( g_scanner_peek_next_token(scanner) != '}') {
382 if( !style_accept_style_hint(scanner, current_tag) )
386 if( !style_accept(scanner, '}') )
392 /* Internal function: parses a style hint */
394 style_accept_style_hint(GScanner *scanner, GtkTextTag *current_tag)
396 GTokenType token = g_scanner_get_next_token(scanner);
397 GTokenValue value = g_scanner_cur_value(scanner);
400 if(token != G_TOKEN_IDENTIFIER) {
401 g_scanner_error(scanner, "CSS Error: style hint expected");
405 hint = g_strdup(value.v_identifier);
407 if( !style_accept(scanner, ':') )
410 token = g_scanner_get_next_token(scanner);
411 value = g_scanner_cur_value(scanner);
413 if( !strcmp(hint, "font-family") ) {
414 if(token != G_TOKEN_STRING) {
415 g_scanner_error(scanner, "CSS Error: string expected");
418 g_object_set(current_tag, "family", value.v_string, "family-set", TRUE, NULL);
420 else if( !strcmp(hint, "font-weight") ) {
421 if(token != G_TOKEN_IDENTIFIER) {
422 g_scanner_error(scanner, "CSS Error: bold/normal expected");
426 if( !strcmp(value.v_identifier, "bold") )
427 g_object_set(current_tag, "weight", PANGO_WEIGHT_BOLD, "weight-set", TRUE, NULL);
428 else if( !strcmp(value.v_identifier, "normal") )
429 g_object_set(current_tag, "weight", PANGO_WEIGHT_NORMAL, "weight-set", TRUE, NULL);
431 g_scanner_error(scanner, "CSS Error: bold/normal expected");
435 else if( !strcmp(hint, "font-style") ) {
436 if(token != G_TOKEN_IDENTIFIER) {
437 g_scanner_error(scanner, "CSS Error: italic/normal expected");
441 if( !strcmp(value.v_identifier, "italic") )
442 g_object_set(current_tag, "style", PANGO_STYLE_ITALIC, "style-set", TRUE, NULL);
443 else if( !strcmp(value.v_identifier, "normal") )
444 g_object_set(current_tag, "style", PANGO_STYLE_NORMAL, "style-set", TRUE, NULL);
446 g_scanner_error(scanner, "CSS Error: italic/normal expected");
450 else if( !strcmp(hint, "font-size") ) {
451 if(token == G_TOKEN_INT)
452 g_object_set(current_tag, "size-points", (float)value.v_int, "size-set", TRUE, NULL);
453 else if(token == G_TOKEN_FLOAT)
454 g_object_set(current_tag, "size-points", value.v_float, "size-set", TRUE, NULL);
456 g_scanner_error(scanner, "CSS Error: integer or float expected");
460 else if( !strcmp(hint, "color") ) {
461 if(token != G_TOKEN_IDENTIFIER) {
462 g_scanner_error(scanner, "CSS Error: hex color expected");
466 g_object_set(current_tag, "foreground", value.v_identifier, "foreground-set", TRUE, NULL);
468 else if( !strcmp(hint, "background-color") ) {
469 if(token != G_TOKEN_IDENTIFIER) {
470 g_scanner_error(scanner, "CSS Error: hex color expected");
473 g_object_set(current_tag, "background", value.v_identifier, "background-set", TRUE, NULL);
475 else if( !strcmp(hint, "text-align") ) {
476 if(token != G_TOKEN_IDENTIFIER) {
477 g_scanner_error(scanner, "CSS Error: left/right/center expected");
481 if( !strcmp(value.v_identifier, "left") )
482 g_object_set(current_tag, "justification", GTK_JUSTIFY_LEFT, "justification-set", TRUE, NULL);
483 else if( !strcmp(value.v_identifier, "right") )
484 g_object_set(current_tag, "justification", GTK_JUSTIFY_RIGHT, "justification-set", TRUE, NULL);
485 else if( !strcmp(value.v_identifier, "center") )
486 g_object_set(current_tag, "justification", GTK_JUSTIFY_CENTER, "justification-set", TRUE, NULL);
488 g_scanner_error(scanner, "CSS Error: left/right/center expected");
492 else if( !strcmp(hint, "margin-left") ) {
493 if(token != G_TOKEN_INT) {
494 g_scanner_error(scanner, "CSS Error: integer expected");
497 g_object_set(current_tag, "left-margin", value.v_int, "left-margin-set", TRUE, NULL);
499 else if( !strcmp(hint, "margin-right") ) {
500 if(token != G_TOKEN_INT) {
501 g_scanner_error(scanner, "CSS Error: integer expected");
504 g_object_set(current_tag, "right-margin", value.v_int, "right-margin-set", TRUE, NULL);
506 else if( !strcmp(hint, "margin-top") ) {
507 if(token != G_TOKEN_INT) {
508 g_scanner_error(scanner, "CSS Error: integer expected");
511 g_object_set(current_tag, "pixels-above-lines", value.v_int, "pixels-above-lines-set", TRUE, NULL);
513 else if( !strcmp(hint, "margin-bottom") ) {
514 if(token != G_TOKEN_INT) {
515 g_scanner_error(scanner, "CSS Error: integer expected");
518 g_object_set(current_tag, "pixels-below-lines", value.v_int, "pixels-below-lines-set", TRUE, NULL);
522 g_scanner_error(scanner, "CSS Error: invalid style hint %s", hint);
526 if( !style_accept(scanner, ';') )
532 /* Internal function: parses a glk color to a #hex-value */
534 glkcolor_to_hex(glui32 val, gchar *buffer)
536 g_return_if_fail(buffer != NULL);
538 sprintf(buffer, "#%02X%02X%02X",
539 ((val & 0xff0000) >> 16),
540 ((val & 0x00ff00) >> 8),
545 /* Internal function: parses a glk color to a GdkColor */
547 glkcolor_to_gdkcolor(glui32 val, GdkColor *color)
549 color->red = 256 * ((val & 0xff0000) >> 16);
550 color->green = 256 * ((val & 0x00ff00) >> 8);
551 color->blue = 256 * (val & 0x0000ff);
554 /* Internal function: parses a GdkColor to a glk color */
556 gdkcolor_to_glkcolor(GdkColor *color)
558 g_return_val_if_fail(color != NULL, 0);
559 return (glui32) color->pixel;
562 /* Internal function: changes a GTK tag to correspond with the given style. */
564 apply_stylehint_to_tag(GtkTextTag *tag, glui32 hint, glsi32 val)
566 g_return_if_fail(tag != NULL);
568 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
569 GObject *tag_object = G_OBJECT(tag);
571 gint reverse_color = GPOINTER_TO_INT( g_object_get_data(tag_object, "reverse-color") );
576 case stylehint_Indentation:
577 g_object_set(tag_object, "left-margin", 5*val, "left-margin-set", TRUE, NULL);
578 g_object_set(tag_object, "right-margin", 5*val, "right-margin-set", TRUE, NULL);
581 case stylehint_ParaIndentation:
582 g_object_set(tag_object, "indent", 5*val, "indent-set", TRUE, NULL);
585 case stylehint_Justification:
587 case stylehint_just_LeftFlush: i = GTK_JUSTIFY_LEFT; break;
588 case stylehint_just_LeftRight: i = GTK_JUSTIFY_FILL; break;
589 case stylehint_just_Centered: i = GTK_JUSTIFY_CENTER; break;
590 case stylehint_just_RightFlush: i = GTK_JUSTIFY_RIGHT; break;
592 WARNING("Unknown justification");
593 i = GTK_JUSTIFY_LEFT;
595 g_object_set(tag_object, "justification", i, "justification-set", TRUE, NULL);
598 case stylehint_Weight:
600 case -1: i = PANGO_WEIGHT_LIGHT; break;
601 case 0: i = PANGO_WEIGHT_NORMAL; break;
602 case 1: i = PANGO_WEIGHT_BOLD; break;
603 default: WARNING("Unknown font weight");
605 g_object_set(tag_object, "weight", i, "weight-set", TRUE, NULL);
609 g_object_set(tag_object, "size", 14+(2*val), "size-set", TRUE, NULL);
612 case stylehint_Oblique:
613 g_object_set(tag_object, "style", val ? PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL, "style-set", TRUE, NULL);
616 case stylehint_Proportional:
617 g_object_set(tag_object, "font-desc", val ? glk_data->default_font_desc : glk_data->monospace_font_desc, NULL);
620 case stylehint_TextColor:
621 glkcolor_to_hex(val, color);
624 g_object_set(tag_object, "foreground", color, "foreground-set", TRUE, NULL);
626 g_object_set(tag_object, "background", color, "background-set", TRUE, NULL);
630 case stylehint_BackColor:
631 glkcolor_to_hex(val, color);
634 g_object_set(tag_object, "background", color, "background-set", TRUE, NULL);
636 g_object_set(tag_object, "foreground", color, "background-set", TRUE, NULL);
640 case stylehint_ReverseColor:
641 if(reverse_color != val) {
642 /* Flip the fore- and background colors */
643 GdkColor* foreground_color;
644 GdkColor* background_color;
645 gint f_set, b_set = 0;
646 g_object_get(tag_object, "foreground-set", &f_set, "background-set", &b_set, NULL);
649 g_object_get(tag_object, "foreground-gdk", &foreground_color, NULL);
651 g_object_get(tag_object, "background-gdk", &background_color, NULL);
654 g_object_set(tag_object, "foreground-gdk", background_color, NULL);
656 g_object_set(tag_object, "foreground", "#ffffff", NULL);
659 g_object_set(tag_object, "background-gdk", foreground_color, NULL);
661 g_object_set(tag_object, "background", "#000000", NULL);
663 g_object_set_data( tag_object, "reverse-color", GINT_TO_POINTER(val != 0) );
668 WARNING("Unknown style hint");
671 /*Internal function: queries a text tag for the value of a given style hint */
673 query_tag(GtkTextTag *tag, glui32 hint)
679 g_return_val_if_fail(tag != NULL, 0);
681 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
684 case stylehint_Indentation:
685 g_object_get(tag, "left_margin", &intval, NULL);
689 case stylehint_ParaIndentation:
690 g_object_get(tag, "indent", &intval, NULL);
694 case stylehint_Justification:
695 g_object_get(tag, "justification", &intval, NULL);
697 case GTK_JUSTIFY_LEFT: return stylehint_just_LeftFlush; break;
698 case GTK_JUSTIFY_FILL: return stylehint_just_LeftRight; break;
699 case GTK_JUSTIFY_CENTER: return stylehint_just_Centered; break;
700 case GTK_JUSTIFY_RIGHT: return stylehint_just_RightFlush; break;
702 WARNING("Unknown justification");
703 return stylehint_just_LeftFlush;
707 case stylehint_Weight:
708 g_object_get(tag, "weight", &intval, NULL);
710 case PANGO_WEIGHT_LIGHT: return -1; break;
711 case PANGO_WEIGHT_NORMAL: return 0; break;
712 case PANGO_WEIGHT_BOLD: return 1; break;
713 default: WARNING("Unknown font weight"); return 0;
718 g_object_get(tag, "size", &intval, NULL);
719 return (intval/2)-14;
722 case stylehint_Oblique:
723 g_object_get(tag, "style", &intval , NULL);
724 return intval == PANGO_STYLE_ITALIC ? 1 : 0;
727 case stylehint_Proportional:
728 g_object_get(tag, "font-desc", &objval, NULL);
729 return objval == (GObject *)glk_data->monospace_font_desc ? 0 : 1;
732 case stylehint_TextColor:
733 g_object_get(tag, "foreground-gdk", &colval, NULL);
734 return gdkcolor_to_glkcolor(colval);
737 case stylehint_BackColor:
738 g_object_get(tag, "background-gdk", &colval, NULL);
739 return gdkcolor_to_glkcolor(colval);
742 case stylehint_ReverseColor:
743 return GPOINTER_TO_INT( g_object_get_data(G_OBJECT(tag), "reverse_color") );
747 WARNING("Unknown style hint");
755 * @wintype: The window type to set a style hint on, or %wintype_AllTypes.
756 * @styl: The style to set a hint for.
757 * @hint: The type of style hint, one of the <code>stylehint_</code> constants.
758 * @val: The style hint. The meaning of this depends on @hint.
760 * Sets a hint about the appearance of one style for a particular type of
761 * window. You can also set wintype to %wintype_AllTypes, which sets a hint for
762 * all types of window.
764 * There is no equivalent constant to set a hint for all styles of a single
769 glk_stylehint_set(glui32 wintype, glui32 styl, glui32 hint, glsi32 val)
771 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
773 if( G_UNLIKELY(!glk_data->style_initialized) ) {
777 GtkTextTag *to_change;
778 if(wintype == wintype_TextBuffer || wintype == wintype_AllTypes) {
779 to_change = g_hash_table_lookup( glk_data->current_styles->text_buffer, get_tag_name(styl) );
780 apply_stylehint_to_tag(to_change, hint, val);
783 if(wintype == wintype_TextGrid || wintype == wintype_AllTypes) {
784 to_change = g_hash_table_lookup( glk_data->current_styles->text_grid, get_tag_name(styl) );
785 apply_stylehint_to_tag(to_change, hint, val);
790 * glk_stylehint_clear:
791 * @wintype: The window type to set a style hint on, or %wintype_AllTypes.
792 * @styl: The style to set a hint for.
793 * @hint: The type of style hint, one of the <code>stylehint_</code> constants.
795 * Resets a hint about the appearance of one style for a particular type of
796 * window to it's default value. You can also set wintype to %wintype_AllTypes, which resets a hint for
797 * all types of window.
799 * There is no equivalent constant to reset a hint for all styles of a single
804 glk_stylehint_clear(glui32 wintype, glui32 styl, glui32 hint)
806 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
810 case wintype_TextBuffer:
811 tag = g_hash_table_lookup( glk_data->default_styles->text_buffer, get_tag_name(styl) );
812 glk_stylehint_set( wintype, styl, hint, query_tag(tag, hint) );
814 case wintype_TextGrid:
815 tag = g_hash_table_lookup( glk_data->default_styles->text_grid, get_tag_name(styl) );
816 glk_stylehint_set( wintype, styl, hint, query_tag(tag, hint) );
823 * glk_style_distinguish:
824 * @win: The window in which the styles are to be distinguished.
825 * @styl1: The first style to be distinguished from the second style.
826 * @styl2: The second styel to be distinguished from the first style.
828 * Returns: TRUE if the two styles are visually distinguishable in the given window.
829 * If they are not, it returns FALSE.
832 glk_style_distinguish(winid_t win, glui32 styl1, glui32 styl2)
834 return styl1 != styl2;
839 * @win: The window from which to take the style.
840 * @styl: The style to perform the measurement on.
841 * @hint: The stylehint to measure.
842 * @result: Address to write the result to.
844 * This function can be used to query the current value of a particular style hint.
845 * Returns: TRUE upon successul retrievel, otherwise FALSE.
848 glk_style_measure(winid_t win, glui32 styl, glui32 hint, glui32 *result)
850 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
854 case wintype_TextBuffer:
855 tag = g_hash_table_lookup( glk_data->current_styles->text_buffer, get_tag_name(styl) );
856 *result = query_tag(tag, hint);
858 case wintype_TextGrid:
859 tag = g_hash_table_lookup( glk_data->current_styles->text_grid, get_tag_name(styl) );
860 *result = query_tag(tag, hint);
868 /* Internal function returning the current default font for a window type
869 * This can be used later for size calculations. Only wintype_TextGrid and wintype_TextBuffer are
870 * supported for now */
871 PangoFontDescription*
872 get_current_font(guint32 wintype)
874 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
877 if( G_UNLIKELY(!glk_data->style_initialized) ) {
882 case wintype_TextGrid:
883 normal = g_hash_table_lookup(glk_data->current_styles->text_grid, "normal");
885 case wintype_TextBuffer:
886 normal = g_hash_table_lookup(glk_data->current_styles->text_buffer, "normal");
892 PangoFontDescription *font;
893 g_object_get( G_OBJECT(normal), "font-desc", &font, NULL );
898 /* Internal function copying the attributes of a text tag to a pango attribute list */
900 text_tag_to_attr_list(GtkTextTag *tag, PangoAttrList *list)
903 GdkColor *foreground, *background;
905 PangoFontDescription *font_desc;
906 gboolean strikethrough;
907 PangoUnderline underline;
909 g_object_get(tag, "foreground-set", &set, "foreground-gdk", &foreground, NULL);
911 pango_attr_list_insert(
913 pango_attr_foreground_new(foreground->red, foreground->green, foreground->blue)
916 g_object_get(tag, "background-set", &set, "background-gdk", &background, NULL);
918 pango_attr_list_insert(
920 pango_attr_background_new(background->red, background->green, background->blue)
923 g_object_get(tag, "language-set", &set, "language", &string, NULL);
925 pango_attr_list_insert(
927 pango_attr_language_new( pango_language_from_string(string) )
931 /* Font description updates the following properties simultaniously:
932 * family, style, weight, variant, stretch, size
934 g_object_get(tag, "font-desc", &font_desc, NULL);
935 pango_attr_list_insert(
937 pango_attr_font_desc_new(font_desc)
940 g_object_get(tag, "strikethrough-set", &set, "strikethrough", &strikethrough, NULL);
942 pango_attr_list_insert(
944 pango_attr_strikethrough_new(strikethrough)
947 g_object_get(tag, "underline-set", &set, "underline", &underline, NULL);
949 pango_attr_list_insert(
951 pango_attr_underline_new(underline)