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_line_terminators:
94 * @keycodes: An array of <code>keycode_</code> constants.
95 * @numkeycodes: The length of @keycodes.
97 * Amends the current line input request of @win to include terminating key
98 * codes. Any of the specified key codes will terminate the line input request
99 * (without printing a newline).
101 * Usually, in the event structure returned from a line input request, @val2 is
102 * zero, but if garglk_set_line_terminators() has been called during that input
103 * request, @val2 will be filled in with the key code that terminated the input
106 * This function only applies to one input request; any subsequent line input
107 * requests on that window are treated normally.
109 * If @numkeycodes is zero, then any previous call to
110 * garglk_set_line_terminators() is cancelled and the input request is treated
113 * <warning><para>This function is not currently implemented.</para></warning>
116 garglk_set_line_terminators(winid_t win, const glui32 *keycodes, glui32 numkeycodes)
118 VALID_WINDOW(win, return);
119 g_return_if_fail(win->type != wintype_TextBuffer || win->type != wintype_TextGrid);
121 if(win->input_request_type != INPUT_REQUEST_LINE && win->input_request_type != INPUT_REQUEST_LINE_UNICODE) {
122 ILLEGAL(_("Tried to set the line terminators on a window without a line input request."));
126 WARNING(_("Not implemented"));
130 * garglk_unput_string:
131 * @str: a null-terminated string.
133 * Removes @str from the end of the current stream, if indeed it is there. The
134 * stream's write count is decreased accordingly, and the stream's echo stream
135 * is also modified, if it has one.
137 * <warning><para>This function is not currently implemented.</para></warning>
140 garglk_unput_string(char *str)
142 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
143 g_return_if_fail(glk_data->current_stream != NULL);
145 WARNING(_("Not implemented"));
149 * garglk_unput_string_uni:
150 * @str: a zero-terminated array of Unicode code points.
152 * Like garglk_unput_string(), but for Unicode streams.
154 * <warning><para>This function is not currently implemented.</para></warning>
157 garglk_unput_string_uni(glui32 *str)
159 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
160 g_return_if_fail(glk_data->current_stream != NULL);
162 WARNING(_("Not implemented"));
167 garglk_set_zcolors_stream(strid_t str, glui32 fg, glui32 bg)
170 g_printf("garglk_set_zcolors_stream(str->rock=%d, fg=%08X, bg=%08X)\n", str->rock, fg, bg);
173 VALID_STREAM(str, return);
174 g_return_if_fail(str->window != NULL);
176 winid_t window = str->window;
180 GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(window->widget) );
181 GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
183 GdkColor *fore_pointer = NULL;
184 GdkColor *back_pointer = NULL;
189 case zcolor_Transparent:
191 WARNING(_("zcolor_Transparent, zcolor_Cursor not implemented"));
192 // Fallthrough to default
194 fore_name = g_strdup("default");
199 // Get the current foreground color
200 GdkColor *current_color;
201 g_object_get(window->zcolor, "foreground-gdk", ¤t_color, NULL);
202 fore_name = gdkcolor_to_hex(current_color);
204 // Copy the color and use it
205 fore.red = current_color->red;
206 fore.green = current_color->green;
207 fore.blue = current_color->blue;
208 fore_pointer = &fore;
210 fore_name = g_strdup("default");
215 glkcolor_to_gdkcolor(fg, &fore);
216 fore_pointer = &fore;
217 fore_name = glkcolor_to_hex(fg);
221 case zcolor_Transparent:
223 WARNING(_("zcolor_Transparent, zcolor_Cursor not implemented"));
224 // Fallthrough to default
226 back_name = g_strdup("default");
231 // Get the current background color
232 GdkColor *current_color;
233 g_object_get(window->zcolor, "background-gdk", ¤t_color, NULL);
234 back_name = gdkcolor_to_hex(current_color);
236 // Copy the color and use it
237 back.red = current_color->red;
238 back.green = current_color->green;
239 back.blue = current_color->blue;
240 back_pointer = &back;
242 back_name = g_strdup("default");
247 glkcolor_to_gdkcolor(bg, &back);
248 back_pointer = &back;
249 back_name = glkcolor_to_hex(bg);
252 if(fore_pointer == NULL && back_pointer == NULL) {
253 // NULL value means to ignore the zcolor property altogether
254 window->zcolor = NULL;
256 char *name = g_strdup_printf("zcolor:#%s/#%s", fore_name, back_name);
260 // See if we have used this color combination before
261 GtkTextTag *tag = gtk_text_tag_table_lookup(tags, name);
264 // Create a new texttag with the specified colors
265 tag = gtk_text_buffer_create_tag(
268 "foreground-gdk", fore_pointer,
269 "foreground-set", fore_pointer != NULL,
270 "background-gdk", back_pointer,
271 "background-set", back_pointer != NULL,
276 // From now on, text will be drawn in the specified colors
277 window->zcolor = tag;
279 // Update the reversed version if necessary
280 if(str->window->zcolor_reversed) {
281 gint reversed = GPOINTER_TO_INT( g_object_get_data( G_OBJECT(str->window->zcolor_reversed), "reverse-color" ) );
284 garglk_set_reversevideo_stream(str, reversed != 0);
294 * garglk_set_zcolors:
295 * @fg: one of the <code>zcolor_</code> constants.
296 * @bg: one of the <code>zcolor_</code> constants.
298 * Glk works with styles, not specific colors. This is not quite compatible with
299 * the Z-machine, so this Glk extension implements Z-machine style colors.
301 * This function changes the foreground color of the current stream to @fg and
302 * the background color to @bg.
304 * <warning><para>This function is not currently implemented.</para></warning>
307 garglk_set_zcolors(glui32 fg, glui32 bg)
309 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
310 g_return_if_fail(glk_data->current_stream != NULL);
312 garglk_set_zcolors_stream(glk_data->current_stream, fg, bg);
317 garglk_set_reversevideo_stream(strid_t str, glui32 reverse)
320 g_printf("garglk_set_reversevideo_stream(str->rock=%d, reverse=%d)\n", str->rock, reverse);
323 VALID_STREAM(str, return);
324 g_return_if_fail(str->window != NULL);
325 g_return_if_fail(str->window->type != wintype_TextBuffer || str->window->type != wintype_TextGrid);
327 // Determine the current colors
329 // If all fails, use black/white
330 // FIXME: Use system theme here
331 GdkColor foreground, background;
332 gdk_color_parse("black", &foreground);
333 gdk_color_parse("white", &background);
334 GdkColor *current_foreground = &foreground;
335 GdkColor *current_background = &background;
339 style_stream_colors(str, ¤t_foreground, ¤t_background);
342 GdkColor *temp = current_foreground;
343 current_foreground = current_background;
344 current_background = temp;
348 gchar *name = g_strdup_printf(
349 "zcolor:#%04X%04X%04X/#%04X%04X%04X",
350 current_foreground->red,
351 current_foreground->green,
352 current_foreground->blue,
353 current_background->red,
354 current_background->green,
355 current_background->blue
358 // Create a tag for the new colors if it doesn't exist yet
359 GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(str->window->widget) );
360 GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
361 GtkTextTag *tag = gtk_text_tag_table_lookup(tags, name);
363 tag = gtk_text_buffer_create_tag(
366 "foreground-gdk", current_foreground,
367 "foreground-set", TRUE,
368 "background-gdk", current_background,
369 "background-set", TRUE,
372 g_object_set_data( G_OBJECT(tag), "reverse-color", GINT_TO_POINTER(reverse) );
375 // From now on, text will be drawn in the specified colors
376 str->window->zcolor_reversed = tag;
378 // Update the background of the gtktextview to correspond with the current background color
379 if(current_background != NULL) {
380 gtk_widget_modify_base(str->window->widget, GTK_STATE_NORMAL, current_background);
387 * garglk_set_reversevideo:
388 * @reverse: nonzero for reverse colors, zero for normal colors.
390 * If @reverse is not zero, uses the foreground color of the current stream as
391 * its background and vice versa. If @reverse is zero, changes the colors of the
392 * current stream back to normal.
395 garglk_set_reversevideo(glui32 reverse)
397 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
398 g_return_if_fail(glk_data->current_stream != NULL);
399 g_return_if_fail(glk_data->current_stream->window != NULL);
401 garglk_set_reversevideo_stream(glk_data->current_stream, reverse);