3 #include "chimara-glk-private.h"
10 extern GPrivate *glk_data_key;
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);
22 * @styl: The style to apply
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
29 * This policy allows for the future definition of styles without breaking old
34 glk_set_style(glui32 styl)
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);
41 static const gchar* TAG_NAMES[] = {
55 /* Internal function: mapping from style enum to tag name */
57 get_tag_name(glui32 style)
59 if(style >= style_NUMSTYLES) {
60 WARNING("Unsupported style");
63 return (gchar*) TAG_NAMES[style];
68 * glk_set_style_stream:
69 * @str: Output stream to change the style of
70 * @styl: The style to apply
72 * This changes the style of the stream @str. See glk_set_style().
75 glk_set_style_stream(strid_t str, glui32 styl) {
76 if(str->window == NULL)
79 flush_window_buffer(str->window);
80 str->style = get_tag_name(styl);
83 /* Internal function: call this to initialize the layout of the 'more' prompt. */
85 style_init_more_prompt(winid_t win)
87 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
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);
93 /* Internal function: call this to initialize the default styles to a textbuffer. */
95 style_init_textbuffer(GtkTextBuffer *buffer)
97 g_return_if_fail(buffer != NULL);
99 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
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));
106 /* Internal function: call this to initialize the default styles to a textgrid. */
108 style_init_textgrid(GtkTextBuffer *buffer)
110 g_return_if_fail(buffer != NULL);
112 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
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));
118 /* Internal function used to iterate over the default text tag table, applying them to a textbuffer */
120 style_add_tag_to_textbuffer(gpointer key, gpointer tag, gpointer tag_table)
122 gtk_text_tag_table_add( tag_table, gtk_text_tag_copy(tag) );
125 /* Internal function used to iterate over a style table, copying it */
127 style_table_copy(gpointer key, gpointer tag, gpointer target_table)
129 g_return_if_fail(key != NULL);
130 g_return_if_fail(tag != NULL);
131 g_return_if_fail(target_table != NULL);
133 g_hash_table_insert(target_table, key, gtk_text_tag_copy( GTK_TEXT_TAG(tag) ));
136 /* Internal function that copies a text tag */
138 gtk_text_tag_copy(GtkTextTag *tag)
142 g_return_val_if_fail(tag != NULL, NULL);
144 copy = gtk_text_tag_new(tag->name);
145 gtk_text_attributes_copy_values(tag->values, copy->values);
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);
171 /* Copy the data that was added manually */
172 gpointer reverse_color = g_object_get_data( G_OBJECT(tag), "reverse_color" );
175 g_object_set_data( G_OBJECT(copy), "reverse_color", reverse_color );
180 /* Internal function that reads the default styles from a CSS file */
182 style_init(ChimaraGlk *glk)
184 CHIMARA_GLK_USE_PRIVATE(glk, priv);
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);
192 PangoFontDescription *default_font_desc = pango_font_description_from_string("Serif");
193 PangoFontDescription *monospace_font_desc = pango_font_description_from_string("Monospace");
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);
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);
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);
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);
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);
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);
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);
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);
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"));
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);
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);
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);
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);
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);
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);
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);
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);
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);
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"));
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);
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);
281 pango_font_description_free(default_font_desc);
282 pango_font_description_free(monospace_font_desc);
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;
289 /* Set the current style to a copy of the default style */
290 copy_default_styles_to_current_styles(glk);
292 text_tag_to_attr_list(pager_tag, priv->pager_attr_list);
295 /* Reset style tables to the library's internal defaults */
297 reset_default_styles(ChimaraGlk *glk)
299 /* TODO: write this function */
302 /* Copy the default styles to the current styles
303 FIXME: This function is temporary and will not be needed later on */
305 copy_default_styles_to_current_styles(ChimaraGlk *glk)
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);
311 GtkTextTag *pager_tag = GTK_TEXT_TAG( g_hash_table_lookup(priv->default_styles->text_buffer, "pager") );
312 text_tag_to_attr_list(pager_tag, priv->pager_attr_list);
315 /* Create the CSS file scanner */
317 create_css_file_scanner(void)
319 GScanner *scanner = g_scanner_new(NULL);
320 scanner->config->cset_identifier_first = G_CSET_a_2_z G_CSET_A_2_Z "#";
321 scanner->config->cset_identifier_nth = G_CSET_a_2_z G_CSET_A_2_Z "-_" G_CSET_DIGITS;
322 scanner->config->symbol_2_token = TRUE;
323 scanner->config->cpair_comment_single = NULL;
324 scanner->config->scan_float = FALSE;
328 /* Run the scanner over the CSS file, overriding the default styles */
330 scan_css_file(GScanner *scanner, ChimaraGlk *glk)
332 while( g_scanner_peek_next_token(scanner) != G_TOKEN_EOF) {
333 if( !style_accept_style_selector(scanner, glk) )
337 g_scanner_destroy(scanner);
340 /* Internal function: parses a token */
342 style_accept(GScanner *scanner, GTokenType token)
344 GTokenType next = g_scanner_get_next_token(scanner);
346 g_scanner_unexp_token(scanner, token, NULL, NULL, NULL, "CSS Error", 1);
352 /* Internal function: parses a style selector */
354 style_accept_style_selector(GScanner *scanner, ChimaraGlk *glk)
356 CHIMARA_GLK_USE_PRIVATE(glk, priv);
358 GtkTextTag *current_tag;
360 GTokenType token = g_scanner_get_next_token(scanner);
361 GTokenValue value = g_scanner_cur_value(scanner);
364 token != G_TOKEN_IDENTIFIER ||
365 ( strcmp(value.v_identifier, "buffer") && strcmp(value.v_identifier, "grid") )
367 g_scanner_error(scanner, "CSS Error: buffer/grid expected");
371 field = g_strdup(value.v_identifier);
373 if( !style_accept(scanner, '.') )
376 token = g_scanner_get_next_token(scanner);
377 value = g_scanner_cur_value(scanner);
379 if(token != G_TOKEN_IDENTIFIER) {
380 g_scanner_error(scanner, "CSS Error: style selector expected");
384 if( !strcmp(field, "buffer") )
385 current_tag = g_hash_table_lookup(priv->default_styles->text_buffer, value.v_identifier);
387 current_tag = g_hash_table_lookup(priv->default_styles->text_grid, value.v_identifier);
389 if(current_tag == NULL) {
390 g_scanner_error(scanner, "CSS Error: invalid style identifier");
394 if( !style_accept(scanner, '{') )
397 while( g_scanner_peek_next_token(scanner) != '}') {
398 if( !style_accept_style_hint(scanner, current_tag) )
402 if( !style_accept(scanner, '}') )
408 /* Internal function: parses a style hint */
410 style_accept_style_hint(GScanner *scanner, GtkTextTag *current_tag)
412 GTokenType token = g_scanner_get_next_token(scanner);
413 GTokenValue value = g_scanner_cur_value(scanner);
416 if(token != G_TOKEN_IDENTIFIER) {
417 g_scanner_error(scanner, "CSS Error: style hint expected");
421 hint = g_strdup(value.v_identifier);
423 if( !style_accept(scanner, ':') )
426 token = g_scanner_get_next_token(scanner);
427 value = g_scanner_cur_value(scanner);
429 if( !strcmp(hint, "font-family") ) {
430 if(token != G_TOKEN_STRING) {
431 g_scanner_error(scanner, "CSS Error: string expected");
434 g_object_set(current_tag, "family", value.v_string, "family-set", TRUE, NULL);
436 else if( !strcmp(hint, "font-weight") ) {
437 if(token != G_TOKEN_IDENTIFIER) {
438 g_scanner_error(scanner, "CSS Error: bold/normal expected");
442 if( !strcmp(value.v_identifier, "bold") )
443 g_object_set(current_tag, "weight", PANGO_WEIGHT_BOLD, "weight-set", TRUE, NULL);
444 else if( !strcmp(value.v_identifier, "normal") )
445 g_object_set(current_tag, "weight", PANGO_WEIGHT_NORMAL, "weight-set", TRUE, NULL);
447 g_scanner_error(scanner, "CSS Error: bold/normal expected");
451 else if( !strcmp(hint, "font-style") ) {
452 if(token != G_TOKEN_IDENTIFIER) {
453 g_scanner_error(scanner, "CSS Error: italic/normal expected");
457 if( !strcmp(value.v_identifier, "italic") )
458 g_object_set(current_tag, "style", PANGO_STYLE_ITALIC, "style-set", TRUE, NULL);
459 else if( !strcmp(value.v_identifier, "normal") )
460 g_object_set(current_tag, "style", PANGO_STYLE_NORMAL, "style-set", TRUE, NULL);
462 g_scanner_error(scanner, "CSS Error: italic/normal expected");
466 else if( !strcmp(hint, "font-size") ) {
467 if(token == G_TOKEN_INT)
468 g_object_set(current_tag, "size-points", (float)value.v_int, "size-set", TRUE, NULL);
469 else if(token == G_TOKEN_FLOAT)
470 g_object_set(current_tag, "size-points", value.v_float, "size-set", TRUE, NULL);
472 g_scanner_error(scanner, "CSS Error: integer or float expected");
476 else if( !strcmp(hint, "color") ) {
477 if(token != G_TOKEN_IDENTIFIER) {
478 g_scanner_error(scanner, "CSS Error: hex color expected");
482 g_object_set(current_tag, "foreground", value.v_identifier, "foreground-set", TRUE, NULL);
484 else if( !strcmp(hint, "background-color") ) {
485 if(token != G_TOKEN_IDENTIFIER) {
486 g_scanner_error(scanner, "CSS Error: hex color expected");
489 g_object_set(current_tag, "background", value.v_identifier, "background-set", TRUE, NULL);
491 else if( !strcmp(hint, "text-align") ) {
492 if(token != G_TOKEN_IDENTIFIER) {
493 g_scanner_error(scanner, "CSS Error: left/right/center expected");
497 if( !strcmp(value.v_identifier, "left") )
498 g_object_set(current_tag, "justification", GTK_JUSTIFY_LEFT, "justification-set", TRUE, NULL);
499 else if( !strcmp(value.v_identifier, "right") )
500 g_object_set(current_tag, "justification", GTK_JUSTIFY_RIGHT, "justification-set", TRUE, NULL);
501 else if( !strcmp(value.v_identifier, "center") )
502 g_object_set(current_tag, "justification", GTK_JUSTIFY_CENTER, "justification-set", TRUE, NULL);
504 g_scanner_error(scanner, "CSS Error: left/right/center expected");
508 else if( !strcmp(hint, "margin-left") ) {
509 if(token != G_TOKEN_INT) {
510 g_scanner_error(scanner, "CSS Error: integer expected");
513 g_object_set(current_tag, "left-margin", value.v_int, "left-margin-set", TRUE, NULL);
515 else if( !strcmp(hint, "margin-right") ) {
516 if(token != G_TOKEN_INT) {
517 g_scanner_error(scanner, "CSS Error: integer expected");
520 g_object_set(current_tag, "right-margin", value.v_int, "right-margin-set", TRUE, NULL);
522 else if( !strcmp(hint, "margin-top") ) {
523 if(token != G_TOKEN_INT) {
524 g_scanner_error(scanner, "CSS Error: integer expected");
527 g_object_set(current_tag, "pixels-above-lines", value.v_int, "pixels-above-lines-set", TRUE, NULL);
529 else if( !strcmp(hint, "margin-bottom") ) {
530 if(token != G_TOKEN_INT) {
531 g_scanner_error(scanner, "CSS Error: integer expected");
534 g_object_set(current_tag, "pixels-below-lines", value.v_int, "pixels-below-lines-set", TRUE, NULL);
538 g_scanner_error(scanner, "CSS Error: invalid style hint %s", hint);
542 if( !style_accept(scanner, ';') )
548 /* Internal function: parses a glk color to a #hex-value */
550 glkcolor_to_hex(glui32 val, gchar *buffer)
552 g_return_if_fail(buffer != NULL);
554 sprintf(buffer, "#%02X%02X%02X",
555 ((val & 0xff0000) >> 16),
556 ((val & 0x00ff00) >> 8),
561 /* Internal function: parses a glk color to a GdkColor */
563 glkcolor_to_gdkcolor(glui32 val, GdkColor *color)
565 color->red = 256 * ((val & 0xff0000) >> 16);
566 color->green = 256 * ((val & 0x00ff00) >> 8);
567 color->blue = 256 * (val & 0x0000ff);
570 /* Internal function: parses a GdkColor to a glk color */
572 gdkcolor_to_glkcolor(GdkColor *color)
574 g_return_val_if_fail(color != NULL, 0);
575 return (glui32) color->pixel;
578 /* Internal function: changes a GTK tag to correspond with the given style. */
580 apply_stylehint_to_tag(GtkTextTag *tag, glui32 wintype, glui32 hint, glsi32 val)
582 g_return_if_fail(tag != NULL);
584 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
585 GObject *tag_object = G_OBJECT(tag);
587 gint reverse_color = GPOINTER_TO_INT( g_object_get_data(tag_object, "reverse-color") );
592 case stylehint_Indentation:
593 g_object_set(tag_object, "left-margin", 5*val, "left-margin-set", TRUE, NULL);
594 g_object_set(tag_object, "right-margin", 5*val, "right-margin-set", TRUE, NULL);
597 case stylehint_ParaIndentation:
598 g_object_set(tag_object, "indent", 5*val, "indent-set", TRUE, NULL);
601 case stylehint_Justification:
603 case stylehint_just_LeftFlush: i = GTK_JUSTIFY_LEFT; break;
604 case stylehint_just_LeftRight: i = GTK_JUSTIFY_FILL; break;
605 case stylehint_just_Centered: i = GTK_JUSTIFY_CENTER; break;
606 case stylehint_just_RightFlush: i = GTK_JUSTIFY_RIGHT; break;
608 WARNING("Unknown justification");
609 i = GTK_JUSTIFY_LEFT;
611 g_object_set(tag_object, "justification", i, "justification-set", TRUE, NULL);
614 case stylehint_Weight:
616 case -1: i = PANGO_WEIGHT_LIGHT; break;
617 case 0: i = PANGO_WEIGHT_NORMAL; break;
618 case 1: i = PANGO_WEIGHT_BOLD; break;
619 default: WARNING("Unknown font weight");
621 g_object_set(tag_object, "weight", i, "weight-set", TRUE, NULL);
625 g_object_set(tag_object, "size", 14+(2*val), "size-set", TRUE, NULL);
628 case stylehint_Oblique:
629 g_object_set(tag_object, "style", val ? PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL, "style-set", TRUE, NULL);
632 case stylehint_Proportional:
635 GtkTextTag *font_tag = g_hash_table_lookup(
636 wintype == wintype_TextBuffer? glk_data->default_styles->text_buffer : glk_data->default_styles->text_grid,
637 val? "normal" : "preformatted");
638 g_object_get(font_tag, "family", &font_family, NULL);
639 g_object_set(tag_object, "family", font_family, "family-set", TRUE, NULL);
644 case stylehint_TextColor:
645 glkcolor_to_hex(val, color);
648 g_object_set(tag_object, "foreground", color, "foreground-set", TRUE, NULL);
650 g_object_set(tag_object, "background", color, "background-set", TRUE, NULL);
654 case stylehint_BackColor:
655 glkcolor_to_hex(val, color);
658 g_object_set(tag_object, "background", color, "background-set", TRUE, NULL);
660 g_object_set(tag_object, "foreground", color, "background-set", TRUE, NULL);
664 case stylehint_ReverseColor:
665 if(reverse_color != val) {
666 /* Flip the fore- and background colors */
667 GdkColor* foreground_color;
668 GdkColor* background_color;
669 gint f_set, b_set = 0;
670 g_object_get(tag_object, "foreground-set", &f_set, "background-set", &b_set, NULL);
673 g_object_get(tag_object, "foreground-gdk", &foreground_color, NULL);
675 g_object_get(tag_object, "background-gdk", &background_color, NULL);
678 g_object_set(tag_object, "foreground-gdk", background_color, NULL);
680 g_object_set(tag_object, "foreground", "#ffffff", NULL);
683 g_object_set(tag_object, "background-gdk", foreground_color, NULL);
685 g_object_set(tag_object, "background", "#000000", NULL);
687 g_object_set_data( tag_object, "reverse-color", GINT_TO_POINTER(val != 0) );
692 WARNING("Unknown style hint");
696 /* Internal function: queries a text tag for the value of a given style hint */
698 query_tag(GtkTextTag *tag, glui32 wintype, glui32 hint)
703 g_return_val_if_fail(tag != NULL, 0);
705 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
708 case stylehint_Indentation:
709 g_object_get(tag, "left_margin", &intval, NULL);
712 case stylehint_ParaIndentation:
713 g_object_get(tag, "indent", &intval, NULL);
716 case stylehint_Justification:
717 g_object_get(tag, "justification", &intval, NULL);
719 case GTK_JUSTIFY_LEFT: return stylehint_just_LeftFlush;
720 case GTK_JUSTIFY_FILL: return stylehint_just_LeftRight;
721 case GTK_JUSTIFY_CENTER: return stylehint_just_Centered;
722 case GTK_JUSTIFY_RIGHT: return stylehint_just_RightFlush;
724 WARNING("Unknown justification");
725 return stylehint_just_LeftFlush;
728 case stylehint_Weight:
729 g_object_get(tag, "weight", &intval, NULL);
731 case PANGO_WEIGHT_LIGHT: return -1;
732 case PANGO_WEIGHT_NORMAL: return 0;
733 case PANGO_WEIGHT_BOLD: return 1;
734 default: WARNING("Unknown font weight"); return 0;
738 g_object_get(tag, "size", &intval, NULL);
739 return (intval/2)-14;
741 case stylehint_Oblique:
742 g_object_get(tag, "style", &intval , NULL);
743 return intval == PANGO_STYLE_ITALIC ? 1 : 0;
745 case stylehint_Proportional:
746 /* Use pango_font_family_is_monospace()? */
748 gchar *font_family, *query_font_family;
749 GtkTextTag *font_tag = g_hash_table_lookup(
750 wintype == wintype_TextBuffer? glk_data->default_styles->text_buffer : glk_data->default_styles->text_grid,
752 g_object_get(font_tag, "family", &font_family, NULL);
753 g_object_get(tag, "family", &query_font_family, NULL);
754 gint retval = strcmp(font_family, query_font_family)? 0 : 1;
756 g_free(query_font_family);
760 case stylehint_TextColor:
761 g_object_get(tag, "foreground-gdk", &colval, NULL);
762 return gdkcolor_to_glkcolor(colval);
764 case stylehint_BackColor:
765 g_object_get(tag, "background-gdk", &colval, NULL);
766 return gdkcolor_to_glkcolor(colval);
768 case stylehint_ReverseColor:
769 return GPOINTER_TO_INT( g_object_get_data(G_OBJECT(tag), "reverse_color") );
772 WARNING("Unknown style hint");
780 * @wintype: The window type to set a style hint on, or %wintype_AllTypes.
781 * @styl: The style to set a hint for.
782 * @hint: The type of style hint, one of the <code>stylehint_</code> constants.
783 * @val: The style hint. The meaning of this depends on @hint.
785 * Sets a hint about the appearance of one style for a particular type of
786 * window. You can also set @wintype to %wintype_AllTypes, which sets a hint for
787 * all types of window.
789 * There is no equivalent constant to set a hint for all styles of a single
794 glk_stylehint_set(glui32 wintype, glui32 styl, glui32 hint, glsi32 val)
796 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
798 GtkTextTag *to_change;
799 if(wintype == wintype_TextBuffer || wintype == wintype_AllTypes) {
800 to_change = g_hash_table_lookup( glk_data->current_styles->text_buffer, get_tag_name(styl) );
801 apply_stylehint_to_tag(to_change, wintype_TextBuffer, hint, val);
804 if(wintype == wintype_TextGrid || wintype == wintype_AllTypes) {
805 to_change = g_hash_table_lookup( glk_data->current_styles->text_grid, get_tag_name(styl) );
806 apply_stylehint_to_tag(to_change, wintype_TextGrid, hint, val);
811 * glk_stylehint_clear:
812 * @wintype: The window type to set a style hint on, or %wintype_AllTypes.
813 * @styl: The style to set a hint for.
814 * @hint: The type of style hint, one of the <code>stylehint_</code> constants.
816 * Clears a hint about the appearance of one style for a particular type of
817 * window to its default value. You can also set @wintype to %wintype_AllTypes,
818 * which clears a hint for all types of window.
820 * There is no equivalent constant to reset a hint for all styles of a single
825 glk_stylehint_clear(glui32 wintype, glui32 styl, glui32 hint)
827 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
831 case wintype_TextBuffer:
832 tag = g_hash_table_lookup( glk_data->default_styles->text_buffer, get_tag_name(styl) );
833 glk_stylehint_set( wintype, styl, hint, query_tag(tag, wintype, hint) );
835 case wintype_TextGrid:
836 tag = g_hash_table_lookup( glk_data->default_styles->text_grid, get_tag_name(styl) );
837 glk_stylehint_set( wintype, styl, hint, query_tag(tag, wintype, hint) );
844 * glk_style_distinguish:
845 * @win: The window in which the styles are to be distinguished.
846 * @styl1: The first style to be distinguished from the second style.
847 * @styl2: The second style to be distinguished from the first style.
849 * Decides whether two styles are visually distinguishable in the given window.
850 * The exact meaning of this is left for the library to determine.
851 * <note><title>Chimara</title><para>
852 * Currently, all styles of one window are assumed to be mutually
856 * Returns: %TRUE (1) if the two styles are visually distinguishable. If they
857 * are not, it returns %FALSE (0).
860 glk_style_distinguish(winid_t win, glui32 styl1, glui32 styl2)
863 return styl1 != styl2;
868 * @win: The window from which to take the style.
869 * @styl: The style to perform the measurement on.
870 * @hint: The stylehint to measure.
871 * @result: Address to write the result to.
873 * Tries to test an attribute of one style in the given window @win. The library
874 * may not be able to determine the attribute; if not, this returns %FALSE (0).
875 * If it can, it returns %TRUE (1) and stores the value in the location pointed
878 * As usual, it is legal for @result to be %NULL, although fairly pointless.
881 * The meaning of the value depends on the hint which was tested:
884 * <term>%stylehint_Indentation, %stylehint_ParaIndentation</term>
885 * <listitem><para>The indentation and paragraph indentation. These are in a
886 * metric which is platform-dependent.</para>
887 * <note><para>Most likely either characters or pixels.</para></note>
891 * <term>%stylehint_Justification</term>
892 * <listitem><para>One of the constants %stylehint_just_LeftFlush,
893 * %stylehint_just_LeftRight, %stylehint_just_Centered, or
894 * %stylehint_just_RightFlush.</para></listitem>
897 * <term>%stylehint_Size</term>
898 * <listitem><para>The font size. Again, this is in a platform-dependent
900 * <note><para>Pixels, points, or simply 1 if the library does not support
901 * varying font sizes.</para></note>
905 * <term>%stylehint_Weight</term>
906 * <listitem><para>1 for heavy-weight fonts (boldface), 0 for normal weight,
907 * and -1 for light-weight fonts.</para></listitem>
910 * <term>%stylehint_Oblique</term>
911 * <listitem><para>1 for oblique fonts (italic), or 0 for normal angle.</para>
915 * <term>%stylehint_Proportional</term>
916 * <listitem><para>1 for proportional-width fonts, or 0 for fixed-width.
920 * <term>%stylehint_TextColor, %stylehint_BackColor</term>
921 * <listitem><para>These are values from 0x00000000 to 0x00FFFFFF, encoded as
923 * linkend="chimara-Suggesting-the-Appearance-of-Styles">Suggesting the
924 * Appearance of Styles</link>.</para></listitem>
927 * <term>%stylehint_ReverseColor</term>
928 * <listitem><para>0 for normal printing, 1 if the foreground and background
929 * colors are reversed.</para></listitem>
933 * Returns: TRUE upon successul retrieval, otherwise FALSE.
936 glk_style_measure(winid_t win, glui32 styl, glui32 hint, glui32 *result)
938 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
942 case wintype_TextBuffer:
943 tag = g_hash_table_lookup( glk_data->current_styles->text_buffer, get_tag_name(styl) );
945 *result = query_tag(tag, win->type, hint);
947 case wintype_TextGrid:
948 tag = g_hash_table_lookup( glk_data->current_styles->text_grid, get_tag_name(styl) );
950 *result = query_tag(tag, win->type, hint);
958 /* Internal function returning the current default font for a window type
959 * This can be used later for size calculations. Only wintype_TextGrid and wintype_TextBuffer are
960 * supported for now */
961 PangoFontDescription *
962 get_current_font(guint32 wintype)
964 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
968 case wintype_TextGrid:
969 normal = g_hash_table_lookup(glk_data->current_styles->text_grid, "normal");
971 case wintype_TextBuffer:
972 normal = g_hash_table_lookup(glk_data->current_styles->text_buffer, "normal");
978 PangoFontDescription *font;
979 g_object_get( G_OBJECT(normal), "font-desc", &font, NULL );
984 /* Internal function copying the attributes of a text tag to a pango attribute list */
986 text_tag_to_attr_list(GtkTextTag *tag, PangoAttrList *list)
989 GdkColor *foreground, *background;
991 PangoFontDescription *font_desc;
992 gboolean strikethrough;
993 PangoUnderline underline;
995 g_object_get(tag, "foreground-set", &set, "foreground-gdk", &foreground, NULL);
997 pango_attr_list_insert(
999 pango_attr_foreground_new(foreground->red, foreground->green, foreground->blue)
1002 g_object_get(tag, "background-set", &set, "background-gdk", &background, NULL);
1004 pango_attr_list_insert(
1006 pango_attr_background_new(background->red, background->green, background->blue)
1009 g_object_get(tag, "language-set", &set, "language", &string, NULL);
1011 pango_attr_list_insert(
1013 pango_attr_language_new( pango_language_from_string(string) )
1017 /* Font description updates the following properties simultaniously:
1018 * family, style, weight, variant, stretch, size
1020 g_object_get(tag, "font-desc", &font_desc, NULL);
1021 pango_attr_list_insert(
1023 pango_attr_font_desc_new(font_desc)
1026 g_object_get(tag, "strikethrough-set", &set, "strikethrough", &strikethrough, NULL);
1028 pango_attr_list_insert(
1030 pango_attr_strikethrough_new(strikethrough)
1033 g_object_get(tag, "underline-set", &set, "underline", &underline, NULL);
1035 pango_attr_list_insert(
1037 pango_attr_underline_new(underline)