3 extern ChimaraGlkPrivate *glk_data;
7 * @styl The style to apply
9 * This changes the style of the current output stream. After a style change,
10 * new text which is printed to that stream will be given the new style. For a
11 * window stream, the text will appear in that style. For other types of
12 * streams, this has no effect.
15 glk_set_style(glui32 style)
17 g_return_if_fail(glk_data->current_stream != NULL);
18 glk_set_style_stream(glk_data->current_stream, style);
21 /* Internal function: mapping from style enum to tag name */
23 get_tag_name(glui32 style)
26 case style_Normal: return "normal";
27 case style_Emphasized: return "emphasized";
28 case style_Preformatted: return "preformatted";
29 case style_Header: return "header";
30 case style_Subheader: return "subheader";
31 case style_Alert: return "alert";
32 case style_Note: return "note";
33 case style_BlockQuote: return "block-quote";
34 case style_Input: return "input";
35 case style_User1: return "user1";
36 case style_User2: return "user2";
39 WARNING("Unsupported style");
44 glk_set_style_stream(strid_t stream, glui32 style) {
45 stream->style = get_tag_name(style);
48 /* Internal function: call this to initialize the default styles to a textbuffer. */
50 style_init_textbuffer(GtkTextBuffer *buffer)
52 g_return_if_fail(buffer != NULL);
54 gtk_text_buffer_create_tag(buffer, "normal", NULL);
55 gtk_text_buffer_create_tag(buffer, "emphasized", "style", PANGO_STYLE_ITALIC, NULL);
56 gtk_text_buffer_create_tag(buffer, "preformatted", "font-desc", glk_data->monospace_font_desc, NULL);
57 gtk_text_buffer_create_tag(buffer, "header", "size-points", 16.0, "weight", PANGO_WEIGHT_BOLD, NULL);
58 gtk_text_buffer_create_tag(buffer, "subheader", "size-points", 12.0, "weight", PANGO_WEIGHT_BOLD, NULL);
59 gtk_text_buffer_create_tag(buffer, "alert", "foreground", "#aa0000", "weight", PANGO_WEIGHT_BOLD, NULL);
60 gtk_text_buffer_create_tag(buffer, "note", "foreground", "#aaaa00", "weight", PANGO_WEIGHT_BOLD, NULL);
61 gtk_text_buffer_create_tag(buffer, "block-quote", "justification", GTK_JUSTIFY_CENTER, "style", PANGO_STYLE_ITALIC, NULL);
62 gtk_text_buffer_create_tag(buffer, "input", NULL);
63 gtk_text_buffer_create_tag(buffer, "user1", NULL);
64 gtk_text_buffer_create_tag(buffer, "user2", NULL);
68 color_format(glui32 val, gchar *buffer)
70 sprintf(buffer, "#%02X%02X%02X",
71 ((val & 0xff0000) >> 16),
72 ((val & 0x00ff00) >> 8),
77 /* Internal function: changes a GTK tag to correspond with the given style. */
79 apply_stylehint_to_tag(GtkTextTag *tag, glui32 hint, glsi32 val)
81 g_return_if_fail(tag != NULL);
83 GObject *tag_object = G_OBJECT(tag);
84 gint reverse_color = 0;
86 /* FIXME where should we keep track of this?
87 g_object_get(tag, "reverse_color", &reverse_color, NULL);
93 case stylehint_Indentation:
94 g_object_set(tag_object, "left_margin", 5*val, NULL);
95 g_object_set(tag_object, "right_margin", 5*val, NULL);
98 case stylehint_ParaIndentation:
99 g_object_set(tag_object, "indent", 5*val, NULL);
102 case stylehint_Justification:
104 case stylehint_just_LeftFlush: i = GTK_JUSTIFY_LEFT; break;
105 case stylehint_just_LeftRight: i = GTK_JUSTIFY_FILL; break;
106 case stylehint_just_Centered: i = GTK_JUSTIFY_CENTER; break;
107 case stylehint_just_RightFlush: i = GTK_JUSTIFY_RIGHT; break;
109 WARNING("Unknown justification");
110 i = GTK_JUSTIFY_LEFT;
112 g_object_set(tag_object, "justification", i, NULL);
115 case stylehint_Weight:
117 case -1: i = PANGO_WEIGHT_LIGHT; break;
118 case 0: i = PANGO_WEIGHT_NORMAL; break;
119 case 1: i = PANGO_WEIGHT_BOLD; break;
120 default: WARNING("Unknown font weight");
122 g_object_set(tag_object, "weight", i, NULL);
126 g_object_set(tag_object, "size", 14+(2*val), NULL);
129 case stylehint_Oblique:
130 g_object_set(tag_object, "style", val ? PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL, NULL);
133 case stylehint_Proportional:
134 g_object_set(tag_object, "font-desc", val ? glk_data->default_font_desc : glk_data->monospace_font_desc, NULL);
137 case stylehint_TextColor:
138 color_format(val, color);
141 g_object_set(tag_object, "foreground", color, NULL);
143 g_object_set(tag_object, "background", color, NULL);
147 case stylehint_BackColor:
148 color_format(val, color);
151 g_object_set(tag_object, "background", color, NULL);
153 g_object_set(tag_object, "foreground", color, NULL);
157 case stylehint_ReverseColor:
158 if(reverse_color != val) {
159 /* Flip the fore- and background colors */
160 gchar* foreground_color;
161 gchar* background_color;
162 g_object_get(tag_object, "foreground", &foreground_color, NULL);
163 g_object_get(tag_object, "background", &background_color, NULL);
164 g_object_set(tag_object, "foreground", background_color, NULL);
165 g_object_set(tag_object, "background", foreground_color, NULL);
166 g_free(foreground_color);
167 g_free(background_color);
172 WARNING("Unknown style hint");
177 glk_stylehint_set(glui32 wintype, glui32 style, glui32 hint, glsi32 val)
180 gchar *tag_name = get_tag_name(style);
182 /* Iterate over all the window and update their styles if nessecary */
183 winid_t win = glk_window_iterate(NULL, NULL);
185 if(wintype != wintype_TextBuffer)
186 continue; /* FIXME: add support for text grid windows */
188 if(wintype == wintype_AllTypes || glk_window_get_type(win) == wintype) {
189 GtkWidget *textview = win->widget;
190 GtkTextBuffer *textbuffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(textview) );
191 GtkTextTagTable *table = gtk_text_buffer_get_tag_table(textbuffer);
192 GtkTextTag *to_change = gtk_text_tag_table_lookup(table, tag_name);
194 apply_stylehint_to_tag(to_change, hint, val);
200 glk_stylehint_clear(glui32 wintype, glui32 styl, glui32 hint)
205 glk_style_distinguish(winid_t win, glui32 styl1, glui32 styl2)
207 return styl1 != styl2;