2 #include <glib/gi18n.h>
3 #include <glib/gprintf.h>
4 #include <libchimara/glk.h>
5 #include "chimara-glk-private.h"
11 extern GPrivate *glk_data_key;
14 * garglk_fileref_get_name:
15 * @fref: A file reference.
17 * Gets the actual disk filename that @fref refers to, in the platform's
18 * native filename encoding. The string is owned by @fref and must not be
21 * Returns: a string in filename encoding.
24 garglk_fileref_get_name(frefid_t fref)
26 VALID_FILEREF(fref, return NULL);
27 return fref->filename;
31 * garglk_set_program_name:
32 * @name: Name of the Glk program that is running.
34 * This function is used to let the library know the name of the currently
35 * running Glk program, in case it wants to display this information somewhere
36 * — for example, in the title bar of a window. A typical use of this
38 * |[ garglk_set_program_name("SuperGlkFrotz 0.1"); ]|
41 garglk_set_program_name(const char *name)
43 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
44 glk_data->program_name = g_strdup(name);
45 g_object_notify(G_OBJECT(glk_data->self), "program-name");
49 * garglk_set_program_info:
50 * @info: Information about the Glk program that is running.
52 * This function is used to provide the library with additional information
53 * about the currently running Glk program, in case it wants to display this
54 * information somewhere — for example, in an About box. A typical use of
55 * this function would be:
57 * garglk_set_program_info("SuperGlkFrotz, version 0.1\n"
58 * "Original Frotz by Stefan Jokisch\n"
59 * "Unix port by Jim Dunleavy and David Griffith\n"
60 * "Glk port by Tor Andersson\n"
61 * "Animation, networking, and evil AI by Sven Metcalfe");
65 garglk_set_program_info(const char *info)
67 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
68 glk_data->program_info = g_strdup(info);
69 g_object_notify(G_OBJECT(glk_data->self), "program-info");
73 * garglk_set_story_name:
74 * @name: Name of the story that the Glk program is currently interpreting.
76 * If the Glk program running is an interactive fiction interpreter, then this
77 * function can be used to let the library know the name of the story currently
78 * loaded in the interpreter, in case it wants to display this information
79 * anywhere — for example, in the title bar of a window. A typical use of
80 * this function would be:
81 * |[ garglk_set_story_name("Lighan Ses Lion, el Zarf"); ]|
84 garglk_set_story_name(const char *name)
86 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
87 glk_data->story_name = g_strdup(name);
88 g_object_notify(G_OBJECT(glk_data->self), "story-name");
92 * garglk_set_story_title:
93 * @title: Title bar text for the currently running story.
95 * This function is a hint to the library to put @title in the title bar of the
96 * window that the Glk program is running in. It overrides
97 * garglk_set_program_name() and garglk_set_story_name(), if they were displayed
98 * in the title bar, although they may still be displayed somewhere else.
100 * <warning><para>This function is not currently implemented.</para></warning>
103 garglk_set_story_title(const char *title)
105 WARNING(_("Not implemented"));
109 * garglk_unput_string:
110 * @str: a null-terminated string.
112 * Removes @str from the end of the current stream, if indeed it is there. The
113 * stream's write count is decreased accordingly, and the stream's echo stream
114 * is also modified, if it has one.
116 * <warning><para>This function is not currently implemented.</para></warning>
119 garglk_unput_string(char *str)
121 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
122 g_return_if_fail(glk_data->current_stream != NULL);
124 WARNING(_("Not implemented"));
128 * garglk_unput_string_uni:
129 * @str: a zero-terminated array of Unicode code points.
131 * Like garglk_unput_string(), but for Unicode streams.
133 * <warning><para>This function is not currently implemented.</para></warning>
136 garglk_unput_string_uni(glui32 *str)
138 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
139 g_return_if_fail(glk_data->current_stream != NULL);
141 WARNING(_("Not implemented"));
145 * garglk_set_zcolors_stream:
147 * @fg: a 24-bit foreground color.
148 * @bg: a 24-bit background color.
150 * This function changes the foreground color of @str to @fg and the background
151 * color to @bg. @fg and @bg are encoded the same way as described in
152 * %stylehint_TextColor. See garglk_set_zcolors() for more information.
155 garglk_set_zcolors_stream(strid_t str, glui32 fg, glui32 bg)
158 g_printf("garglk_set_zcolors_stream(str->rock=%d, fg=%08X, bg=%08X)\n", str->rock, fg, bg);
161 VALID_STREAM(str, return);
162 g_return_if_fail(str->window != NULL);
164 winid_t window = str->window;
168 GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(window->widget) );
169 GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
171 GdkColor *fore_pointer = NULL;
172 GdkColor *back_pointer = NULL;
177 case zcolor_Transparent:
179 WARNING(_("zcolor_Transparent, zcolor_Cursor not implemented"));
180 // Fallthrough to default
182 fore_name = g_strdup("default");
187 // Get the current foreground color
188 GdkColor *current_color;
189 g_object_get(window->zcolor, "foreground-gdk", ¤t_color, NULL);
190 fore_name = gdkcolor_to_hex(current_color);
192 // Copy the color and use it
193 fore.red = current_color->red;
194 fore.green = current_color->green;
195 fore.blue = current_color->blue;
196 fore_pointer = &fore;
198 fore_name = g_strdup("default");
203 glkcolor_to_gdkcolor(fg, &fore);
204 fore_pointer = &fore;
205 fore_name = glkcolor_to_hex(fg);
209 case zcolor_Transparent:
211 WARNING(_("zcolor_Transparent, zcolor_Cursor not implemented"));
212 // Fallthrough to default
214 back_name = g_strdup("default");
219 // Get the current background color
220 GdkColor *current_color;
221 g_object_get(window->zcolor, "background-gdk", ¤t_color, NULL);
222 back_name = gdkcolor_to_hex(current_color);
224 // Copy the color and use it
225 back.red = current_color->red;
226 back.green = current_color->green;
227 back.blue = current_color->blue;
228 back_pointer = &back;
230 back_name = g_strdup("default");
235 glkcolor_to_gdkcolor(bg, &back);
236 back_pointer = &back;
237 back_name = glkcolor_to_hex(bg);
240 if(fore_pointer == NULL && back_pointer == NULL) {
241 // NULL value means to ignore the zcolor property altogether
242 window->zcolor = NULL;
244 char *name = g_strdup_printf("zcolor:#%s/#%s", fore_name, back_name);
248 // See if we have used this color combination before
249 GtkTextTag *tag = gtk_text_tag_table_lookup(tags, name);
252 // Create a new texttag with the specified colors
253 tag = gtk_text_buffer_create_tag(
256 "foreground-gdk", fore_pointer,
257 "foreground-set", fore_pointer != NULL,
258 "background-gdk", back_pointer,
259 "background-set", back_pointer != NULL,
264 // From now on, text will be drawn in the specified colors
265 window->zcolor = tag;
267 // Update the reversed version if necessary
268 if(str->window->zcolor_reversed) {
269 gint reversed = GPOINTER_TO_INT( g_object_get_data( G_OBJECT(str->window->zcolor_reversed), "reverse-color" ) );
272 garglk_set_reversevideo_stream(str, reversed != 0);
282 * garglk_set_zcolors:
283 * @fg: a 24-bit foreground color.
284 * @bg: a 24-bit background color.
286 * Glk works with styles, not specific colors. This is not quite compatible with
287 * the Z-machine, so this Glk extension implements Z-machine style colors.
289 * This function changes the foreground color of the current stream to @fg and
290 * the background color to @bg. @fg and @bg are encoded the same way as
291 * described in %stylehint_TextColor.
294 garglk_set_zcolors(glui32 fg, glui32 bg)
296 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
297 g_return_if_fail(glk_data->current_stream != NULL);
299 garglk_set_zcolors_stream(glk_data->current_stream, fg, bg);
303 * garglk_set_reversevideo_stream:
305 * @reverse: nonzero for reverse colors, zero for normal colors.
307 * If @reverse is not zero, uses the foreground color of @str as its background
308 * and vice versa. If @reverse is zero, changes the colors of @str back to
312 garglk_set_reversevideo_stream(strid_t str, glui32 reverse)
315 g_printf("garglk_set_reversevideo_stream(str->rock=%d, reverse=%d)\n", str->rock, reverse);
318 VALID_STREAM(str, return);
319 g_return_if_fail(str->window != NULL);
320 g_return_if_fail(str->window->type != wintype_TextBuffer || str->window->type != wintype_TextGrid);
322 // Determine the current colors
324 // If all fails, use black/white
325 // FIXME: Use system theme here
326 GdkColor foreground, background;
327 gdk_color_parse("black", &foreground);
328 gdk_color_parse("white", &background);
329 GdkColor *current_foreground = &foreground;
330 GdkColor *current_background = &background;
334 style_stream_colors(str, ¤t_foreground, ¤t_background);
337 GdkColor *temp = current_foreground;
338 current_foreground = current_background;
339 current_background = temp;
343 gchar *name = g_strdup_printf(
344 "zcolor:#%04X%04X%04X/#%04X%04X%04X",
345 current_foreground->red,
346 current_foreground->green,
347 current_foreground->blue,
348 current_background->red,
349 current_background->green,
350 current_background->blue
353 // Create a tag for the new colors if it doesn't exist yet
354 GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(str->window->widget) );
355 GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
356 GtkTextTag *tag = gtk_text_tag_table_lookup(tags, name);
358 tag = gtk_text_buffer_create_tag(
361 "foreground-gdk", current_foreground,
362 "foreground-set", TRUE,
363 "background-gdk", current_background,
364 "background-set", TRUE,
367 g_object_set_data( G_OBJECT(tag), "reverse-color", GINT_TO_POINTER(reverse) );
370 // From now on, text will be drawn in the specified colors
371 str->window->zcolor_reversed = tag;
373 // Update the background of the gtktextview to correspond with the current background color
374 if(current_background != NULL) {
375 gtk_widget_modify_base(str->window->widget, GTK_STATE_NORMAL, current_background);
382 * garglk_set_reversevideo:
383 * @reverse: nonzero for reverse colors, zero for normal colors.
385 * If @reverse is not zero, uses the foreground color of the current stream as
386 * its background and vice versa. If @reverse is zero, changes the colors of the
387 * current stream back to normal.
390 garglk_set_reversevideo(glui32 reverse)
392 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
393 g_return_if_fail(glk_data->current_stream != NULL);
394 g_return_if_fail(glk_data->current_stream->window != NULL);
396 garglk_set_reversevideo_stream(glk_data->current_stream, reverse);