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 if(glk_data->css_file != NULL) {
291 int f = open(glk_data->css_file, O_RDONLY);
294 g_scanner_input_file(scanner, f);
295 scanner->input_name = glk_data->css_file;
296 scanner->config->cset_identifier_first = G_CSET_a_2_z G_CSET_A_2_Z "#";
297 scanner->config->cset_identifier_nth = G_CSET_a_2_z G_CSET_A_2_Z "-_" G_CSET_DIGITS;
298 scanner->config->symbol_2_token = TRUE;
299 scanner->config->cpair_comment_single = NULL;
300 scanner->config->scan_float = FALSE;
302 /* Run the scanner over the CSS file, overriding defaults */
303 while( g_scanner_peek_next_token(scanner) != G_TOKEN_EOF) {
304 if( !style_accept_style_selector(scanner) )
308 g_scanner_destroy(scanner);
311 g_warning("Could not find CSS file");
314 /* Set the current style to a copy of the default style */
315 g_hash_table_foreach(default_text_grid_styles, style_table_copy, current_text_grid_styles);
316 g_hash_table_foreach(default_text_buffer_styles, style_table_copy, current_text_buffer_styles);
317 glk_data->current_styles->text_grid = current_text_grid_styles;
318 glk_data->current_styles->text_buffer = current_text_buffer_styles;
320 text_tag_to_attr_list(pager_tag, glk_data->pager_attr_list);
322 glk_data->style_initialized = TRUE;
325 /* Internal function: parses a token */
327 style_accept(GScanner *scanner, GTokenType token)
329 GTokenType next = g_scanner_get_next_token(scanner);
331 g_scanner_unexp_token(scanner, token, NULL, NULL, NULL, "CSS Error", 1);
338 /* Internal function: parses a style selector */
340 style_accept_style_selector(GScanner *scanner)
342 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
344 GtkTextTag *current_tag;
346 GTokenType token = g_scanner_get_next_token(scanner);
347 GTokenValue value = g_scanner_cur_value(scanner);
350 token != G_TOKEN_IDENTIFIER ||
351 ( strcmp(value.v_identifier, "buffer") && strcmp(value.v_identifier, "grid") )
353 g_scanner_error(scanner, "CSS Error: buffer/grid expected");
357 field = g_strdup(value.v_identifier);
359 if( !style_accept(scanner, '.') )
362 token = g_scanner_get_next_token(scanner);
363 value = g_scanner_cur_value(scanner);
365 if(token != G_TOKEN_IDENTIFIER) {
366 g_scanner_error(scanner, "CSS Error: style selector expected");
370 if( !strcmp(field, "buffer") )
371 current_tag = g_hash_table_lookup(glk_data->default_styles->text_buffer, value.v_identifier);
373 current_tag = g_hash_table_lookup(glk_data->default_styles->text_grid, value.v_identifier);
375 if(current_tag == NULL) {
376 g_scanner_error(scanner, "CSS Error: invalid style identifier");
380 if( !style_accept(scanner, '{') )
383 while( g_scanner_peek_next_token(scanner) != '}') {
384 if( !style_accept_style_hint(scanner, current_tag) )
388 if( !style_accept(scanner, '}') )
394 /* Internal function: parses a style hint */
396 style_accept_style_hint(GScanner *scanner, GtkTextTag *current_tag)
398 GTokenType token = g_scanner_get_next_token(scanner);
399 GTokenValue value = g_scanner_cur_value(scanner);
402 if(token != G_TOKEN_IDENTIFIER) {
403 g_scanner_error(scanner, "CSS Error: style hint expected");
407 hint = g_strdup(value.v_identifier);
409 if( !style_accept(scanner, ':') )
412 token = g_scanner_get_next_token(scanner);
413 value = g_scanner_cur_value(scanner);
415 if( !strcmp(hint, "font-family") ) {
416 if(token != G_TOKEN_STRING) {
417 g_scanner_error(scanner, "CSS Error: string expected");
420 g_object_set(current_tag, "family", value.v_string, "family-set", TRUE, NULL);
422 else if( !strcmp(hint, "font-weight") ) {
423 if(token != G_TOKEN_IDENTIFIER) {
424 g_scanner_error(scanner, "CSS Error: bold/normal expected");
428 if( !strcmp(value.v_identifier, "bold") )
429 g_object_set(current_tag, "weight", PANGO_WEIGHT_BOLD, "weight-set", TRUE, NULL);
430 else if( !strcmp(value.v_identifier, "normal") )
431 g_object_set(current_tag, "weight", PANGO_WEIGHT_NORMAL, "weight-set", TRUE, NULL);
433 g_scanner_error(scanner, "CSS Error: bold/normal expected");
437 else if( !strcmp(hint, "font-style") ) {
438 if(token != G_TOKEN_IDENTIFIER) {
439 g_scanner_error(scanner, "CSS Error: italic/normal expected");
443 if( !strcmp(value.v_identifier, "italic") )
444 g_object_set(current_tag, "style", PANGO_STYLE_ITALIC, "style-set", TRUE, NULL);
445 else if( !strcmp(value.v_identifier, "normal") )
446 g_object_set(current_tag, "style", PANGO_STYLE_NORMAL, "style-set", TRUE, NULL);
448 g_scanner_error(scanner, "CSS Error: italic/normal expected");
452 else if( !strcmp(hint, "font-size") ) {
453 if(token == G_TOKEN_INT)
454 g_object_set(current_tag, "size-points", (float)value.v_int, "size-set", TRUE, NULL);
455 else if(token == G_TOKEN_FLOAT)
456 g_object_set(current_tag, "size-points", value.v_float, "size-set", TRUE, NULL);
458 g_scanner_error(scanner, "CSS Error: integer or float expected");
462 else if( !strcmp(hint, "color") ) {
463 if(token != G_TOKEN_IDENTIFIER) {
464 g_scanner_error(scanner, "CSS Error: hex color expected");
468 g_object_set(current_tag, "foreground", value.v_identifier, "foreground-set", TRUE, NULL);
470 else if( !strcmp(hint, "background-color") ) {
471 if(token != G_TOKEN_IDENTIFIER) {
472 g_scanner_error(scanner, "CSS Error: hex color expected");
475 g_object_set(current_tag, "background", value.v_identifier, "background-set", TRUE, NULL);
477 else if( !strcmp(hint, "text-align") ) {
478 if(token != G_TOKEN_IDENTIFIER) {
479 g_scanner_error(scanner, "CSS Error: left/right/center expected");
483 if( !strcmp(value.v_identifier, "left") )
484 g_object_set(current_tag, "justification", GTK_JUSTIFY_LEFT, "justification-set", TRUE, NULL);
485 else if( !strcmp(value.v_identifier, "right") )
486 g_object_set(current_tag, "justification", GTK_JUSTIFY_RIGHT, "justification-set", TRUE, NULL);
487 else if( !strcmp(value.v_identifier, "center") )
488 g_object_set(current_tag, "justification", GTK_JUSTIFY_CENTER, "justification-set", TRUE, NULL);
490 g_scanner_error(scanner, "CSS Error: left/right/center expected");
494 else if( !strcmp(hint, "margin-left") ) {
495 if(token != G_TOKEN_INT) {
496 g_scanner_error(scanner, "CSS Error: integer expected");
499 g_object_set(current_tag, "left-margin", value.v_int, "left-margin-set", TRUE, NULL);
501 else if( !strcmp(hint, "margin-right") ) {
502 if(token != G_TOKEN_INT) {
503 g_scanner_error(scanner, "CSS Error: integer expected");
506 g_object_set(current_tag, "right-margin", value.v_int, "right-margin-set", TRUE, NULL);
508 else if( !strcmp(hint, "margin-top") ) {
509 if(token != G_TOKEN_INT) {
510 g_scanner_error(scanner, "CSS Error: integer expected");
513 g_object_set(current_tag, "pixels-above-lines", value.v_int, "pixels-above-lines-set", TRUE, NULL);
515 else if( !strcmp(hint, "margin-bottom") ) {
516 if(token != G_TOKEN_INT) {
517 g_scanner_error(scanner, "CSS Error: integer expected");
520 g_object_set(current_tag, "pixels-below-lines", value.v_int, "pixels-below-lines-set", TRUE, NULL);
524 g_scanner_error(scanner, "CSS Error: invalid style hint %s", hint);
528 if( !style_accept(scanner, ';') )
534 /* Internal function: parses a glk color to a #hex-value */
536 glkcolor_to_hex(glui32 val, gchar *buffer)
538 g_return_if_fail(buffer != NULL);
540 sprintf(buffer, "#%02X%02X%02X",
541 ((val & 0xff0000) >> 16),
542 ((val & 0x00ff00) >> 8),
547 /* Internal function: parses a glk color to a GdkColor */
549 glkcolor_to_gdkcolor(glui32 val, GdkColor *color)
551 color->red = 256 * ((val & 0xff0000) >> 16);
552 color->green = 256 * ((val & 0x00ff00) >> 8);
553 color->blue = 256 * (val & 0x0000ff);
556 /* Internal function: parses a GdkColor to a glk color */
558 gdkcolor_to_glkcolor(GdkColor *color)
560 g_return_val_if_fail(color != NULL, 0);
561 return (glui32) color->pixel;
564 /* Internal function: changes a GTK tag to correspond with the given style. */
566 apply_stylehint_to_tag(GtkTextTag *tag, glui32 hint, glsi32 val)
568 g_return_if_fail(tag != NULL);
570 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
571 GObject *tag_object = G_OBJECT(tag);
573 gint reverse_color = GPOINTER_TO_INT( g_object_get_data(tag_object, "reverse-color") );
578 case stylehint_Indentation:
579 g_object_set(tag_object, "left-margin", 5*val, "left-margin-set", TRUE, NULL);
580 g_object_set(tag_object, "right-margin", 5*val, "right-margin-set", TRUE, NULL);
583 case stylehint_ParaIndentation:
584 g_object_set(tag_object, "indent", 5*val, "indent-set", TRUE, NULL);
587 case stylehint_Justification:
589 case stylehint_just_LeftFlush: i = GTK_JUSTIFY_LEFT; break;
590 case stylehint_just_LeftRight: i = GTK_JUSTIFY_FILL; break;
591 case stylehint_just_Centered: i = GTK_JUSTIFY_CENTER; break;
592 case stylehint_just_RightFlush: i = GTK_JUSTIFY_RIGHT; break;
594 WARNING("Unknown justification");
595 i = GTK_JUSTIFY_LEFT;
597 g_object_set(tag_object, "justification", i, "justification-set", TRUE, NULL);
600 case stylehint_Weight:
602 case -1: i = PANGO_WEIGHT_LIGHT; break;
603 case 0: i = PANGO_WEIGHT_NORMAL; break;
604 case 1: i = PANGO_WEIGHT_BOLD; break;
605 default: WARNING("Unknown font weight");
607 g_object_set(tag_object, "weight", i, "weight-set", TRUE, NULL);
611 g_object_set(tag_object, "size", 14+(2*val), "size-set", TRUE, NULL);
614 case stylehint_Oblique:
615 g_object_set(tag_object, "style", val ? PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL, "style-set", TRUE, NULL);
618 case stylehint_Proportional:
619 g_object_set(tag_object, "font-desc", val ? glk_data->default_font_desc : glk_data->monospace_font_desc, NULL);
622 case stylehint_TextColor:
623 glkcolor_to_hex(val, color);
626 g_object_set(tag_object, "foreground", color, "foreground-set", TRUE, NULL);
628 g_object_set(tag_object, "background", color, "background-set", TRUE, NULL);
632 case stylehint_BackColor:
633 glkcolor_to_hex(val, color);
636 g_object_set(tag_object, "background", color, "background-set", TRUE, NULL);
638 g_object_set(tag_object, "foreground", color, "background-set", TRUE, NULL);
642 case stylehint_ReverseColor:
643 if(reverse_color != val) {
644 /* Flip the fore- and background colors */
645 GdkColor* foreground_color;
646 GdkColor* background_color;
647 gint f_set, b_set = 0;
648 g_object_get(tag_object, "foreground-set", &f_set, "background-set", &b_set, NULL);
651 g_object_get(tag_object, "foreground-gdk", &foreground_color, NULL);
653 g_object_get(tag_object, "background-gdk", &background_color, NULL);
656 g_object_set(tag_object, "foreground-gdk", background_color, NULL);
658 g_object_set(tag_object, "foreground", "#ffffff", NULL);
661 g_object_set(tag_object, "background-gdk", foreground_color, NULL);
663 g_object_set(tag_object, "background", "#000000", NULL);
665 g_object_set_data( tag_object, "reverse-color", GINT_TO_POINTER(val != 0) );
670 WARNING("Unknown style hint");
673 /*Internal function: queries a text tag for the value of a given style hint */
675 query_tag(GtkTextTag *tag, glui32 hint)
681 g_return_val_if_fail(tag != NULL, 0);
683 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
686 case stylehint_Indentation:
687 g_object_get(tag, "left_margin", &intval, NULL);
691 case stylehint_ParaIndentation:
692 g_object_get(tag, "indent", &intval, NULL);
696 case stylehint_Justification:
697 g_object_get(tag, "justification", &intval, NULL);
699 case GTK_JUSTIFY_LEFT: return stylehint_just_LeftFlush; break;
700 case GTK_JUSTIFY_FILL: return stylehint_just_LeftRight; break;
701 case GTK_JUSTIFY_CENTER: return stylehint_just_Centered; break;
702 case GTK_JUSTIFY_RIGHT: return stylehint_just_RightFlush; break;
704 WARNING("Unknown justification");
705 return stylehint_just_LeftFlush;
709 case stylehint_Weight:
710 g_object_get(tag, "weight", &intval, NULL);
712 case PANGO_WEIGHT_LIGHT: return -1; break;
713 case PANGO_WEIGHT_NORMAL: return 0; break;
714 case PANGO_WEIGHT_BOLD: return 1; break;
715 default: WARNING("Unknown font weight"); return 0;
720 g_object_get(tag, "size", &intval, NULL);
721 return (intval/2)-14;
724 case stylehint_Oblique:
725 g_object_get(tag, "style", &intval , NULL);
726 return intval == PANGO_STYLE_ITALIC ? 1 : 0;
729 case stylehint_Proportional:
730 g_object_get(tag, "font-desc", &objval, NULL);
731 return objval == (GObject *)glk_data->monospace_font_desc ? 0 : 1;
734 case stylehint_TextColor:
735 g_object_get(tag, "foreground-gdk", &colval, NULL);
736 return gdkcolor_to_glkcolor(colval);
739 case stylehint_BackColor:
740 g_object_get(tag, "background-gdk", &colval, NULL);
741 return gdkcolor_to_glkcolor(colval);
744 case stylehint_ReverseColor:
745 return GPOINTER_TO_INT( g_object_get_data(G_OBJECT(tag), "reverse_color") );
749 WARNING("Unknown style hint");
757 * @wintype: The window type to set a style hint on, or %wintype_AllTypes.
758 * @styl: The style to set a hint for.
759 * @hint: The type of style hint, one of the <code>stylehint_</code> constants.
760 * @val: The style hint. The meaning of this depends on @hint.
762 * Sets a hint about the appearance of one style for a particular type of
763 * window. You can also set wintype to %wintype_AllTypes, which sets a hint for
764 * all types of window.
766 * There is no equivalent constant to set a hint for all styles of a single
771 glk_stylehint_set(glui32 wintype, glui32 styl, glui32 hint, glsi32 val)
773 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
775 if( G_UNLIKELY(!glk_data->style_initialized) ) {
779 GtkTextTag *to_change;
780 if(wintype == wintype_TextBuffer || wintype == wintype_AllTypes) {
781 to_change = g_hash_table_lookup( glk_data->current_styles->text_buffer, get_tag_name(styl) );
782 apply_stylehint_to_tag(to_change, hint, val);
785 if(wintype == wintype_TextGrid || wintype == wintype_AllTypes) {
786 to_change = g_hash_table_lookup( glk_data->current_styles->text_grid, get_tag_name(styl) );
787 apply_stylehint_to_tag(to_change, hint, val);
792 * glk_stylehint_clear:
793 * @wintype: The window type to set a style hint on, or %wintype_AllTypes.
794 * @styl: The style to set a hint for.
795 * @hint: The type of style hint, one of the <code>stylehint_</code> constants.
797 * Resets a hint about the appearance of one style for a particular type of
798 * window to it's default value. You can also set wintype to %wintype_AllTypes, which resets a hint for
799 * all types of window.
801 * There is no equivalent constant to reset a hint for all styles of a single
806 glk_stylehint_clear(glui32 wintype, glui32 styl, glui32 hint)
808 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
812 case wintype_TextBuffer:
813 tag = g_hash_table_lookup( glk_data->default_styles->text_buffer, get_tag_name(styl) );
814 glk_stylehint_set( wintype, styl, hint, query_tag(tag, hint) );
816 case wintype_TextGrid:
817 tag = g_hash_table_lookup( glk_data->default_styles->text_grid, get_tag_name(styl) );
818 glk_stylehint_set( wintype, styl, hint, query_tag(tag, hint) );
825 * glk_style_distinguish:
826 * @win: The window in which the styles are to be distinguished.
827 * @styl1: The first style to be distinguished from the second style.
828 * @styl2: The second styel to be distinguished from the first style.
830 * Returns: TRUE if the two styles are visually distinguishable in the given window.
831 * If they are not, it returns FALSE.
834 glk_style_distinguish(winid_t win, glui32 styl1, glui32 styl2)
836 return styl1 != styl2;
841 * @win: The window from which to take the style.
842 * @styl: The style to perform the measurement on.
843 * @hint: The stylehint to measure.
844 * @result: Address to write the result to.
846 * This function can be used to query the current value of a particular style hint.
847 * Returns: TRUE upon successul retrievel, otherwise FALSE.
850 glk_style_measure(winid_t win, glui32 styl, glui32 hint, glui32 *result)
852 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
856 case wintype_TextBuffer:
857 tag = g_hash_table_lookup( glk_data->current_styles->text_buffer, get_tag_name(styl) );
858 *result = query_tag(tag, hint);
860 case wintype_TextGrid:
861 tag = g_hash_table_lookup( glk_data->current_styles->text_grid, get_tag_name(styl) );
862 *result = query_tag(tag, hint);
870 /* Internal function returning the current default font for a window type
871 * This can be used later for size calculations. Only wintype_TextGrid and wintype_TextBuffer are
872 * supported for now */
873 PangoFontDescription*
874 get_current_font(guint32 wintype)
876 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
879 if( G_UNLIKELY(!glk_data->style_initialized) ) {
884 case wintype_TextGrid:
885 normal = g_hash_table_lookup(glk_data->current_styles->text_grid, "normal");
887 case wintype_TextBuffer:
888 normal = g_hash_table_lookup(glk_data->current_styles->text_buffer, "normal");
894 PangoFontDescription *font;
895 g_object_get( G_OBJECT(normal), "font-desc", &font, NULL );
900 /* Internal function copying the attributes of a text tag to a pango attribute list */
902 text_tag_to_attr_list(GtkTextTag *tag, PangoAttrList *list)
905 GdkColor *foreground, *background;
907 PangoFontDescription *font_desc;
908 gboolean strikethrough;
909 PangoUnderline underline;
911 g_object_get(tag, "foreground-set", &set, "foreground-gdk", &foreground, NULL);
913 pango_attr_list_insert(
915 pango_attr_foreground_new(foreground->red, foreground->green, foreground->blue)
918 g_object_get(tag, "background-set", &set, "background-gdk", &background, NULL);
920 pango_attr_list_insert(
922 pango_attr_background_new(background->red, background->green, background->blue)
925 g_object_get(tag, "language-set", &set, "language", &string, NULL);
927 pango_attr_list_insert(
929 pango_attr_language_new( pango_language_from_string(string) )
933 /* Font description updates the following properties simultaniously:
934 * family, style, weight, variant, stretch, size
936 g_object_get(tag, "font-desc", &font_desc, NULL);
937 pango_attr_list_insert(
939 pango_attr_font_desc_new(font_desc)
942 g_object_get(tag, "strikethrough-set", &set, "strikethrough", &strikethrough, NULL);
944 pango_attr_list_insert(
946 pango_attr_strikethrough_new(strikethrough)
949 g_object_get(tag, "underline-set", &set, "underline", &underline, NULL);
951 pango_attr_list_insert(
953 pango_attr_underline_new(underline)