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 = g_strdup_printf("%02X%02X%02X", current_color->red, current_color->green, current_color->blue);
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 = g_strdup_printf("%02X%02X%02X",
218 ((fg & 0xff0000) >> 16),
219 ((fg & 0x00ff00) >> 8),
225 case zcolor_Transparent:
227 WARNING(_("zcolor_Transparent, zcolor_Cursor not implemented"));
228 // Fallthrough to default
230 back_name = g_strdup("default");
235 // Get the current background color
236 GdkColor *current_color;
237 g_object_get(window->zcolor, "background-gdk", ¤t_color, NULL);
238 back_name = g_strdup_printf("%02X%02X%02X", current_color->red, current_color->green, current_color->blue);
240 // Copy the color and use it
241 back.red = current_color->red;
242 back.green = current_color->green;
243 back.blue = current_color->blue;
244 back_pointer = &back;
246 back_name = g_strdup("default");
251 glkcolor_to_gdkcolor(bg, &back);
252 back_pointer = &back;
253 back_name = g_strdup_printf("%02X%02X%02X",
254 ((bg & 0xff0000) >> 16),
255 ((bg & 0x00ff00) >> 8),
260 if(fore_pointer == NULL && back_pointer == NULL) {
261 // NULL value means to ignore the zcolor property altogether
262 window->zcolor = NULL;
264 char *name = g_strdup_printf("zcolor:#%s/#%s", fore_name, back_name);
268 // See if we have used this color combination before
269 GtkTextTag *tag = gtk_text_tag_table_lookup(tags, name);
272 // Create a new texttag with the specified colors
273 tag = gtk_text_buffer_create_tag(
276 "foreground-gdk", fore_pointer,
277 "foreground-set", fore_pointer != NULL,
278 "background-gdk", back_pointer,
279 "background-set", back_pointer != NULL,
284 // From now on, text will be drawn in the specified colors
285 window->zcolor = tag;
292 * garglk_set_zcolors:
293 * @fg: one of the <code>zcolor_</code> constants.
294 * @bg: one of the <code>zcolor_</code> constants.
296 * Glk works with styles, not specific colors. This is not quite compatible with
297 * the Z-machine, so this Glk extension implements Z-machine style colors.
299 * This function changes the foreground color of the current stream to @fg and
300 * the background color to @bg.
302 * <warning><para>This function is not currently implemented.</para></warning>
305 garglk_set_zcolors(glui32 fg, glui32 bg)
307 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
308 g_return_if_fail(glk_data->current_stream != NULL);
310 garglk_set_zcolors_stream(glk_data->current_stream, fg, bg);
315 garglk_set_reversevideo_stream(strid_t str, glui32 reverse)
318 g_printf("garglk_set_reversevideo_stream(str->rock=%d, reverse=%d)\n", str->rock, reverse);
321 VALID_STREAM(str, return);
322 g_return_if_fail(str->window != NULL);
323 g_return_if_fail(str->window->type != wintype_TextBuffer || str->window->type != wintype_TextGrid);
325 // Determine the current colors
327 // If all fails, use black/white
328 // FIXME: Use system theme here
329 GdkColor foreground, background;
330 gdk_color_parse("black", &foreground);
331 gdk_color_parse("white", &background);
332 GdkColor *current_foreground = &foreground;
333 GdkColor *current_background = &background;
337 style_stream_colors(str, ¤t_foreground, ¤t_background);
340 GdkColor *temp = current_foreground;
341 current_foreground = current_background;
342 current_background = temp;
346 gchar *name = g_strdup_printf(
347 "zcolor:#%04X%04X%04X/#%04X%04X%04X",
348 current_foreground->red,
349 current_foreground->green,
350 current_foreground->blue,
351 current_background->red,
352 current_background->green,
353 current_background->blue
356 // Create a tag for the new colors if it doesn't exist yet
357 GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(str->window->widget) );
358 GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
359 GtkTextTag *tag = gtk_text_tag_table_lookup(tags, name);
361 tag = gtk_text_buffer_create_tag(
364 "foreground-gdk", current_foreground,
365 "foreground-set", TRUE,
366 "background-gdk", current_background,
367 "background-set", TRUE,
372 // From now on, text will be drawn in the specified colors
373 str->window->zcolor_reversed = tag;
379 * garglk_set_reversevideo:
380 * @reverse: nonzero for reverse colors, zero for normal colors.
382 * If @reverse is not zero, uses the foreground color of the current stream as
383 * its background and vice versa. If @reverse is zero, changes the colors of the
384 * current stream back to normal.
387 garglk_set_reversevideo(glui32 reverse)
389 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
390 g_return_if_fail(glk_data->current_stream != NULL);
391 g_return_if_fail(glk_data->current_stream->window != NULL);
393 garglk_set_reversevideo_stream(glk_data->current_stream, reverse);