5 #include "chimara-glk-private.h"
8 extern GPrivate *glk_data_key;
10 /* Forward declarations */
11 static int finish_text_buffer_line_input(winid_t win, gboolean emit_signal);
12 static int finish_text_grid_line_input(winid_t win, gboolean emit_signal);
13 static void cancel_old_input_request(winid_t win);
15 /* Internal function: code common to both flavors of char event request */
17 request_char_event_common(winid_t win, gboolean unicode)
19 VALID_WINDOW(win, return);
20 g_return_if_fail(win->type != wintype_TextBuffer || win->type != wintype_TextGrid);
22 cancel_old_input_request(win);
24 flush_window_buffer(win);
26 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
28 win->input_request_type = unicode? INPUT_REQUEST_CHARACTER_UNICODE : INPUT_REQUEST_CHARACTER;
29 g_signal_handler_unblock( win->widget, win->char_input_keypress_handler );
32 gtk_widget_grab_focus( GTK_WIDGET(win->widget) );
35 /* Emit the "waiting" signal to let listeners know we are ready for input */
36 g_signal_emit_by_name(glk_data->self, "waiting");
40 * glk_request_char_event:
41 * @win: A window to request char events from.
43 * Request input of a Latin-1 character or special key. A window cannot have
44 * requests for both character and line input at the same time. Nor can it have
45 * requests for character input of both types (Latin-1 and Unicode). It is
46 * illegal to call glk_request_char_event() if the window already has a pending
47 * request for either character or line input.
50 glk_request_char_event(winid_t win)
52 request_char_event_common(win, FALSE);
56 * glk_request_char_event_uni:
57 * @win: A window to request char events from.
59 * Request input of a Unicode character or special key. See
60 * glk_request_char_event().
63 glk_request_char_event_uni(winid_t win)
65 request_char_event_common(win, TRUE);
69 * glk_cancel_char_event:
70 * @win: A window to cancel the latest char event request on.
72 * This cancels a pending request for character input. (Either Latin-1 or
73 * Unicode.) For convenience, it is legal to call glk_cancel_char_event() even
74 * if there is no charcter input request on that window. Glk will ignore the
78 glk_cancel_char_event(winid_t win)
80 VALID_WINDOW(win, return);
81 g_return_if_fail(win->type != wintype_TextBuffer || win->type != wintype_TextGrid);
83 if(win->input_request_type == INPUT_REQUEST_CHARACTER || win->input_request_type == INPUT_REQUEST_CHARACTER_UNICODE)
85 win->input_request_type = INPUT_REQUEST_NONE;
86 g_signal_handler_block( win->widget, win->char_input_keypress_handler );
90 /* Internal function: Request either latin-1 or unicode line input, in a text grid window. */
92 text_grid_request_line_event_common(winid_t win, glui32 maxlen, gboolean insert, gchar *inserttext)
94 /* All outstanding printing _must_ be finished before putting an input entry
96 flush_window_buffer(win);
100 GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
102 GtkTextMark *cursor = gtk_text_buffer_get_mark(buffer, "cursor_position");
103 GtkTextIter start_iter, end_iter;
104 gtk_text_buffer_get_iter_at_mark(buffer, &start_iter, cursor);
106 /* Determine the maximum length of the line input */
107 gint cursorpos = gtk_text_iter_get_line_offset(&start_iter);
108 /* Odd; the Glk spec says the maximum input length is
109 windowwidth - 1 - cursorposition. I say no, because if cursorposition is
110 zero, then the input should fill the whole line. FIXME??? */
111 win->input_length = MIN(win->width - cursorpos, win->line_input_buffer_max_len);
112 end_iter = start_iter;
113 gtk_text_iter_set_line_offset(&end_iter, cursorpos + win->input_length);
115 /* If the buffer currently has a selection with one bound in the middle of
116 the input field, then deselect it. Otherwise the input field gets trashed */
117 GtkTextIter start_sel, end_sel;
118 if( gtk_text_buffer_get_selection_bounds(buffer, &start_sel, &end_sel) )
120 if( gtk_text_iter_in_range(&start_sel, &start_iter, &end_iter) )
121 gtk_text_buffer_place_cursor(buffer, &end_sel);
122 if( gtk_text_iter_in_range(&end_sel, &start_iter, &end_iter) )
123 gtk_text_buffer_place_cursor(buffer, &start_sel);
126 /* Erase the text currently in the input field and replace it with a GtkEntry */
127 gtk_text_buffer_delete(buffer, &start_iter, &end_iter);
128 win->input_anchor = gtk_text_buffer_create_child_anchor(buffer, &start_iter);
129 win->input_entry = gtk_entry_new();
130 /* Set the entry's font to match that of the window */
131 GtkRcStyle *style = gtk_widget_get_modifier_style(win->widget); /* Don't free */
132 gtk_widget_modify_font(win->input_entry, style->font_desc);
133 /* Make the entry as small as possible to fit with the text */
134 gtk_entry_set_has_frame(GTK_ENTRY(win->input_entry), FALSE);
135 GtkBorder border = { 0, 0, 0, 0 };
138 #if GTK_CHECK_VERSION(2,10,0)
139 gtk_entry_set_inner_border(GTK_ENTRY(win->input_entry), &border);
141 gtk_entry_set_max_length(GTK_ENTRY(win->input_entry), win->input_length);
142 gtk_entry_set_width_chars(GTK_ENTRY(win->input_entry), win->input_length);
144 /* Insert pre-entered text if needed */
146 gtk_entry_set_text(GTK_ENTRY(win->input_entry), inserttext);
148 /* Set background color of entry (TODO: implement as property) */
150 gdk_color_parse("grey", &background);
151 gtk_widget_modify_base(win->input_entry, GTK_STATE_NORMAL, &background);
153 g_signal_connect(win->input_entry, "activate", G_CALLBACK(on_input_entry_activate), win);
154 g_signal_connect(win->input_entry, "key-press-event", G_CALLBACK(on_input_entry_key_press_event), win);
155 win->line_input_entry_changed = g_signal_connect(win->input_entry, "changed", G_CALLBACK(on_input_entry_changed), win);
157 gtk_widget_show(win->input_entry);
158 gtk_text_view_add_child_at_anchor(GTK_TEXT_VIEW(win->widget), win->input_entry, win->input_anchor);
160 gtk_widget_grab_focus(win->input_entry);
165 /* Internal function: Request either latin-1 or unicode line input, in a text buffer window. */
167 text_buffer_request_line_event_common(winid_t win, glui32 maxlen, gboolean insert, gchar *inserttext)
169 flush_window_buffer(win);
173 GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
175 /* Move the input_position mark to the end of the window_buffer */
176 GtkTextMark *input_position = gtk_text_buffer_get_mark(buffer, "input_position");
177 GtkTextIter end_iter;
178 gtk_text_buffer_get_end_iter(buffer, &end_iter);
179 gtk_text_buffer_move_mark(buffer, input_position, &end_iter);
181 /* Set the entire contents of the window_buffer as uneditable
182 * (so input can only be entered at the end) */
183 GtkTextIter start_iter;
184 gtk_text_buffer_get_start_iter(buffer, &start_iter);
185 gtk_text_buffer_remove_tag_by_name(buffer, "uneditable", &start_iter, &end_iter);
186 gtk_text_buffer_apply_tag_by_name(buffer, "uneditable", &start_iter, &end_iter);
188 /* Insert pre-entered text if needed */
190 gtk_text_buffer_insert(buffer, &end_iter, inserttext, -1);
191 gtk_text_buffer_get_end_iter(buffer, &end_iter); /* update after text insertion */
194 /* Apply the correct style to the input prompt */
195 GtkTextIter input_iter;
196 gtk_text_buffer_get_iter_at_mark(buffer, &input_iter, input_position);
197 gtk_text_buffer_apply_tag_by_name(buffer, "input", &input_iter, &end_iter);
199 gtk_text_view_set_editable(GTK_TEXT_VIEW(win->widget), TRUE);
201 g_signal_handler_unblock(buffer, win->insert_text_handler);
202 gtk_widget_grab_focus(win->widget);
208 * glk_request_line_event:
209 * @win: A text buffer or text grid window to request line input on.
210 * @buf: A buffer of at least @maxlen bytes.
211 * @maxlen: Length of the buffer.
212 * @initlen: The number of characters in @buf to pre-enter.
214 * Requests input of a line of Latin-1 characters. A window cannot have requests
215 * for both character and line input at the same time. Nor can it have requests
216 * for line input of both types (Latin-1 and Unicode). It is illegal to call
217 * glk_request_line_event() if the window already has a pending request for
218 * either character or line input.
220 * The @buf argument is a pointer to space where the line input will be stored.
221 * (This may not be %NULL.) @maxlen is the length of this space, in bytes; the
222 * library will not accept more characters than this. If @initlen is nonzero,
223 * then the first @initlen bytes of @buf will be entered as pre-existing input
224 * — just as if the player had typed them himself. (The player can continue
225 * composing after this pre-entered input, or delete it or edit as usual.)
227 * The contents of the buffer are undefined until the input is completed (either
228 * by a line input event, or glk_cancel_line_event(). The library may or may not
229 * fill in the buffer as the player composes, while the input is still pending;
230 * it is illegal to change the contents of the buffer yourself.
233 glk_request_line_event(winid_t win, char *buf, glui32 maxlen, glui32 initlen)
235 VALID_WINDOW(win, return);
236 g_return_if_fail(buf);
237 g_return_if_fail(win->type != wintype_TextBuffer || win->type != wintype_TextGrid);
238 g_return_if_fail(initlen <= maxlen);
240 cancel_old_input_request(win);
242 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
244 /* Register the buffer */
245 if(glk_data->register_arr)
246 win->buffer_rock = (*glk_data->register_arr)(buf, maxlen, "&+#!Cn");
248 win->input_request_type = INPUT_REQUEST_LINE;
249 win->line_input_buffer = buf;
250 win->line_input_buffer_max_len = maxlen;
251 win->echo_current_line_input = win->echo_line_input;
252 win->current_extra_line_terminators = g_slist_copy(win->extra_line_terminators);
254 gchar *inserttext = (initlen > 0)? g_strndup(buf, initlen) : g_strdup("");
257 case wintype_TextBuffer:
258 text_buffer_request_line_event_common(win, maxlen, (initlen > 0), inserttext);
260 case wintype_TextGrid:
261 text_grid_request_line_event_common(win, maxlen, (initlen > 0), inserttext);
265 g_signal_handler_unblock(win->widget, win->line_input_keypress_handler);
267 /* Emit the "waiting" signal to let listeners know we are ready for input */
268 g_signal_emit_by_name(glk_data->self, "waiting");
272 * glk_request_line_event_uni:
273 * @win: A text buffer or text grid window to request line input on.
274 * @buf: A buffer of at least @maxlen characters.
275 * @maxlen: Length of the buffer.
276 * @initlen: The number of characters in @buf to pre-enter.
278 * Request input of a line of Unicode characters. This works the same as
279 * glk_request_line_event(), except the result is stored in an array of
280 * <type>glui32</type> values instead of an array of characters, and the values
281 * may be any valid Unicode code points.
283 * If possible, the library should return fully composed Unicode characters,
284 * rather than strings of base and composition characters.
287 * Fully-composed characters are the norm for Unicode text, so an
288 * implementation that ignores this issue will probably produce the right
289 * result. However, the game may not want to rely on that. Another factor is
290 * that case-folding can (occasionally) produce non-normalized text.
291 * Therefore, to cover all its bases, a game should call
292 * glk_buffer_to_lower_case_uni(), followed by
293 * glk_buffer_canon_normalize_uni(), before parsing.
297 * Earlier versions of this spec said that line input must always be in
298 * Unicode Normalization Form C. However, this has not been universally
299 * implemented. It is also somewhat redundant, for the results noted above.
300 * Therefore, we now merely recommend that line input be fully composed. The
301 * game is ultimately responsible for all case-folding and normalization. See
302 * <link linkend="chimara-Unicode-String-Normalization">Unicode String
303 * Normalization</link>.
307 glk_request_line_event_uni(winid_t win, glui32 *buf, glui32 maxlen, glui32 initlen)
309 VALID_WINDOW(win, return);
310 g_return_if_fail(buf);
311 g_return_if_fail(win->type != wintype_TextBuffer || win->type != wintype_TextGrid);
312 g_return_if_fail(initlen <= maxlen);
314 cancel_old_input_request(win);
315 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
317 /* Register the buffer */
318 if(glk_data->register_arr)
319 win->buffer_rock = (*glk_data->register_arr)(buf, maxlen, "&+#!Iu");
321 win->input_request_type = INPUT_REQUEST_LINE_UNICODE;
322 win->line_input_buffer_unicode = buf;
323 win->line_input_buffer_max_len = maxlen;
324 win->echo_current_line_input = win->echo_line_input;
325 win->current_extra_line_terminators = g_slist_copy(win->extra_line_terminators);
329 utf8 = convert_ucs4_to_utf8(buf, initlen);
338 case wintype_TextBuffer:
339 text_buffer_request_line_event_common(win, maxlen, (initlen > 0), utf8);
341 case wintype_TextGrid:
342 text_grid_request_line_event_common(win, maxlen, (initlen > 0), utf8);
345 g_signal_handler_unblock(win->widget, win->line_input_keypress_handler);
348 /* Emit the "waiting" signal to let listeners know we are ready for input */
349 g_signal_emit_by_name(glk_data->self, "waiting");
353 * glk_cancel_line_event:
354 * @win: A text buffer or text grid window to cancel line input on.
355 * @event: Will be filled in if the user had already input something.
357 * This cancels a pending request for line input. (Either Latin-1 or Unicode.)
359 * The event pointed to by the event argument will be filled in as if the
360 * player had hit <keycap>enter</keycap>, and the input composed so far will be
361 * stored in the buffer; see below. If you do not care about this information,
362 * pass %NULL as the @event argument. (The buffer will still be filled.)
364 * For convenience, it is legal to call glk_cancel_line_event() even if there
365 * is no line input request on that window. The event type will be set to
366 * %evtype_None in this case.
369 glk_cancel_line_event(winid_t win, event_t *event)
371 VALID_WINDOW(win, return);
372 g_return_if_fail(win->type != wintype_TextBuffer || win->type != wintype_TextGrid);
375 event->type = evtype_None;
381 if(win->input_request_type != INPUT_REQUEST_LINE && win->input_request_type != INPUT_REQUEST_LINE_UNICODE)
384 g_signal_handler_block( win->widget, win->line_input_keypress_handler );
386 int chars_written = 0;
389 if(win->type == wintype_TextGrid) {
390 chars_written = finish_text_grid_line_input(win, FALSE);
391 } else if(win->type == wintype_TextBuffer) {
392 gtk_text_view_set_editable( GTK_TEXT_VIEW(win->widget), FALSE );
393 GtkTextBuffer *window_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
394 g_signal_handler_block(window_buffer, win->insert_text_handler);
395 chars_written = finish_text_buffer_line_input(win, FALSE);
399 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
400 if(glk_data->unregister_arr)
402 if(win->input_request_type == INPUT_REQUEST_LINE_UNICODE)
403 (*glk_data->unregister_arr)(win->line_input_buffer_unicode, win->line_input_buffer_max_len, "&+#!Iu", win->buffer_rock);
405 (*glk_data->unregister_arr)(win->line_input_buffer, win->line_input_buffer_max_len, "&+#!Cn", win->buffer_rock);
408 if(event != NULL && chars_written > 0) {
409 event->type = evtype_LineInput;
410 event->val1 = chars_written;
414 /* Helper function: Turn off shutdown key-press-event signal handler */
416 turn_off_handler(GNode *node)
418 winid_t win = node->data;
419 g_signal_handler_block(win->widget, win->shutdown_keypress_handler);
420 return FALSE; /* don't stop */
423 /* Internal function: Callback for signal key-press-event while waiting for shutdown. */
425 on_shutdown_key_press_event(GtkWidget *widget, GdkEventKey *event, winid_t win)
427 ChimaraGlk *glk = CHIMARA_GLK(gtk_widget_get_ancestor(widget, CHIMARA_TYPE_GLK));
429 CHIMARA_GLK_USE_PRIVATE(glk, priv);
431 /* Turn off all the signal handlers */
432 if(priv->root_window)
433 g_node_traverse(priv->root_window, G_IN_ORDER, G_TRAVERSE_LEAVES, -1, (GNodeTraverseFunc)turn_off_handler, NULL);
435 /* Signal the Glk library that it can shut everything down now */
436 g_mutex_lock(priv->shutdown_lock);
437 g_cond_signal(priv->shutdown_key_pressed);
438 g_mutex_unlock(priv->shutdown_lock);
440 return TRUE; /* block the event */
443 /* Internal function: General callback for signal key-press-event on a text buffer or text grid window. Used in character input on both text buffers and grids. Blocked when not in use. */
445 on_char_input_key_press_event(GtkWidget *widget, GdkEventKey *event, winid_t win)
447 /* Ignore modifier keys, otherwise the char input will already trigger on
448 the shift key when the user tries to type a capital letter */
449 if(event->is_modifier)
450 return FALSE; /* don't stop the event */
452 /* All text up to the input position is now regarded as being read by the user */
453 if(win->type == wintype_TextBuffer)
456 glui32 keycode = keyval_to_glk_keycode(event->keyval, win->input_request_type == INPUT_REQUEST_CHARACTER_UNICODE);
458 ChimaraGlk *glk = CHIMARA_GLK(gtk_widget_get_ancestor(widget, CHIMARA_TYPE_GLK));
460 event_throw(glk, evtype_CharInput, win, keycode, 0);
461 g_signal_emit_by_name(glk, "char-input", win->rock, event->keyval);
463 /* Only one keypress will be handled */
464 win->input_request_type = INPUT_REQUEST_NONE;
465 g_signal_handler_block(win->widget, win->char_input_keypress_handler);
471 on_line_input_key_press_event(GtkWidget *widget, GdkEventKey *event, winid_t win)
475 case wintype_TextBuffer:
477 /* All text up to the input position is now regarded as being read by the user */
480 GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(win->widget));
482 /* History up/down */
483 if(event->keyval == GDK_Up || event->keyval == GDK_KP_Up
484 || event->keyval == GDK_Down || event->keyval == GDK_KP_Down)
486 /* Prevent falling off the end of the history list */
487 if(win->history == NULL)
489 if( (event->keyval == GDK_Up || event->keyval == GDK_KP_Up)
490 && win->history_pos && win->history_pos->next == NULL)
492 if( (event->keyval == GDK_Down || event->keyval == GDK_KP_Down)
493 && (win->history_pos == NULL || win->history_pos->prev == NULL) )
496 GtkTextIter start, end;
497 /* Erase any text that was already typed */
498 GtkTextMark *input_position = gtk_text_buffer_get_mark(buffer, "input_position");
499 gtk_text_buffer_get_iter_at_mark(buffer, &start, input_position);
500 gtk_text_buffer_get_end_iter(buffer, &end);
502 if(win->history_pos == NULL) {
503 gchar *current_input = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
504 win->history = g_list_prepend(win->history, current_input);
505 win->history_pos = win->history;
508 gtk_text_buffer_delete(buffer, &start, &end);
510 if(event->keyval == GDK_Up || event->keyval == GDK_KP_Up)
513 win->history_pos = g_list_next(win->history_pos);
515 win->history_pos = win->history;
518 win->history_pos = g_list_previous(win->history_pos);
520 /* Insert the history item into the window */
521 gtk_text_buffer_get_end_iter(buffer, &end);
523 g_signal_handler_block(buffer, win->insert_text_handler);
524 gtk_text_buffer_insert_with_tags_by_name(buffer, &end, win->history_pos->data, -1, "default", "input", NULL);
525 g_signal_handler_unblock(buffer, win->insert_text_handler);
529 /* Move to beginning/end of input field */
530 else if(event->keyval == GDK_Home) {
531 GtkTextIter input_iter;
532 GtkTextMark *input_position = gtk_text_buffer_get_mark(buffer, "input_position");
533 gtk_text_buffer_get_iter_at_mark(buffer, &input_iter, input_position);
534 gtk_text_buffer_place_cursor(buffer, &input_iter);
537 else if(event->keyval == GDK_End) {
538 GtkTextIter end_iter;
539 gtk_text_buffer_get_end_iter(buffer, &end_iter);
540 gtk_text_buffer_place_cursor(buffer, &end_iter);
544 /* Handle the line terminators */
545 else if(event->keyval == GDK_Return || event->keyval == GDK_KP_Enter
546 || g_slist_find(win->current_extra_line_terminators, GUINT_TO_POINTER(event->keyval)))
548 /* Remove signal handlers */
549 g_signal_handler_block(buffer, win->insert_text_handler);
550 g_signal_handler_block(win->widget, win->line_input_keypress_handler);
552 /* Insert a newline (even if line input was terminated with a different key */
554 gtk_text_buffer_get_end_iter(buffer, &end);
555 gtk_text_buffer_insert(buffer, &end, "\n", 1);
556 gtk_text_buffer_place_cursor(buffer, &end);
558 /* Make the window uneditable again and retrieve the text that was input */
559 gtk_text_view_set_editable(GTK_TEXT_VIEW(win->widget), FALSE);
561 int chars_written = finish_text_buffer_line_input(win, TRUE);
562 ChimaraGlk *glk = CHIMARA_GLK(gtk_widget_get_ancestor(win->widget, CHIMARA_TYPE_GLK));
563 event_throw(glk, evtype_LineInput, win, chars_written, 0);
569 /* If this is a text grid window, then redirect the key press to the line input GtkEntry */
570 case wintype_TextGrid:
572 if(event->keyval == GDK_Up || event->keyval == GDK_KP_Up
573 || event->keyval == GDK_Down || event->keyval == GDK_KP_Down
574 || event->keyval == GDK_Left || event->keyval == GDK_KP_Left
575 || event->keyval == GDK_Right || event->keyval == GDK_KP_Right
576 || event->keyval == GDK_Tab || event->keyval == GDK_KP_Tab
577 || event->keyval == GDK_Page_Up || event->keyval == GDK_KP_Page_Up
578 || event->keyval == GDK_Page_Down || event->keyval == GDK_KP_Page_Down
579 || event->keyval == GDK_Home || event->keyval == GDK_KP_Home
580 || event->keyval == GDK_End || event->keyval == GDK_KP_End)
581 return FALSE; /* Don't redirect these keys */
582 gtk_widget_grab_focus(win->input_entry);
583 gtk_editable_set_position(GTK_EDITABLE(win->input_entry), -1);
584 gboolean retval = TRUE;
585 g_signal_emit_by_name(win->input_entry, "key-press-event", event, &retval);
586 return retval; /* Block this key event if the entry handled it */
592 /* Internal function: finish handling a line input request, for both text grid and text buffer windows. */
594 write_to_window_buffer(winid_t win, const gchar *inserted_text)
598 /* Convert the string from UTF-8 to Latin-1 or Unicode */
599 if(win->input_request_type == INPUT_REQUEST_LINE)
602 gchar *latin1 = convert_utf8_to_latin1(inserted_text, &bytes_written);
607 /* Place input in the echo stream */
608 if(win->echo_stream != NULL)
609 glk_put_string_stream(win->echo_stream, latin1);
611 /* Copy the string (bytes_written does not include the NULL at the end) */
612 copycount = MIN(win->line_input_buffer_max_len, bytes_written);
613 memcpy(win->line_input_buffer, latin1, copycount);
616 else if(win->input_request_type == INPUT_REQUEST_LINE_UNICODE)
619 gunichar *unicode = convert_utf8_to_ucs4(inserted_text, &items_written);
624 /* Place input in the echo stream */
625 if(win->echo_stream != NULL)
626 glk_put_string_stream_uni(win->echo_stream, unicode);
628 /* Copy the string (but not the NULL at the end) */
629 copycount = MIN(win->line_input_buffer_max_len, items_written);
630 memcpy(win->line_input_buffer_unicode, unicode, copycount * sizeof(gunichar));
634 WARNING("Wrong input request type");
636 win->input_request_type = INPUT_REQUEST_NONE;
640 /* Internal function: Retrieves the input of a TextBuffer window and stores it in the window buffer.
641 * Returns the number of characters written, suitable for inclusion in a line input event. */
643 finish_text_buffer_line_input(winid_t win, gboolean emit_signal)
645 VALID_WINDOW(win, return 0);
646 g_return_val_if_fail(win->type == wintype_TextBuffer, 0);
648 GtkTextIter start_iter, end_iter;
650 GtkTextBuffer *window_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
651 GtkTextMark *input_position = gtk_text_buffer_get_mark(window_buffer, "input_position");
652 gtk_text_buffer_get_iter_at_mark(window_buffer, &start_iter, input_position);
653 gtk_text_buffer_get_end_iter(window_buffer, &end_iter);
655 gchar *inserted_text = gtk_text_buffer_get_text(window_buffer, &start_iter, &end_iter, FALSE);
657 /* If echoing is turned off, remove the text from the window */
658 if(!win->echo_current_line_input)
659 gtk_text_buffer_delete(window_buffer, &start_iter, &end_iter);
661 /* Don't include the newline in the input */
662 char *last_char = inserted_text + strlen(inserted_text) - 1;
663 if(*last_char == '\n')
666 int chars_written = write_to_window_buffer(win, inserted_text);
669 ChimaraGlk *glk = CHIMARA_GLK(gtk_widget_get_ancestor(win->widget, CHIMARA_TYPE_GLK));
671 g_signal_emit_by_name(glk, "line-input", win->rock, inserted_text);
674 /* Add the text to the window input history */
675 if(win->history_pos != NULL)
677 g_free(win->history->data);
678 win->history = g_list_delete_link(win->history, win->history);
680 if(*inserted_text != 0)
681 win->history = g_list_prepend(win->history, g_strdup(inserted_text));
683 win->history_pos = NULL;
685 g_free(inserted_text);
687 return chars_written;
690 /* Internal function: Retrieves the input of a TextGrid window and stores it in the window buffer.
691 * Returns the number of characters written, suitable for inclusion in a line input event. */
693 finish_text_grid_line_input(winid_t win, gboolean emit_signal)
695 VALID_WINDOW(win, return 0);
696 g_return_val_if_fail(win->type == wintype_TextGrid, 0);
698 GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
700 gchar *text = g_strdup( gtk_entry_get_text(GTK_ENTRY(win->input_entry)) );
701 /* Move the focus back into the text view */
702 gtk_widget_grab_focus(win->widget);
703 /* Remove entry widget from text view */
704 /* Should be ok even though this is the widget's own signal handler */
705 gtk_container_remove( GTK_CONTAINER(win->widget), GTK_WIDGET(win->input_entry) );
706 win->input_entry = NULL;
707 /* Delete the child anchor */
708 GtkTextIter start, end;
709 gtk_text_buffer_get_iter_at_child_anchor(buffer, &start, win->input_anchor);
711 gtk_text_iter_forward_char(&end); /* Point after the child anchor */
712 gtk_text_buffer_delete(buffer, &start, &end);
713 win->input_anchor = NULL;
715 gchar *spaces = g_strnfill(win->input_length - g_utf8_strlen(text, -1), ' ');
716 gchar *text_to_insert = g_strconcat(text, spaces, NULL);
718 gtk_text_buffer_insert(buffer, &start, text_to_insert, -1);
719 g_free(text_to_insert);
721 int chars_written = write_to_window_buffer(win, text);
724 ChimaraGlk *glk = CHIMARA_GLK(gtk_widget_get_ancestor(win->widget, CHIMARA_TYPE_GLK));
726 g_signal_emit_by_name(glk, "line-input", win->rock, text);
729 /* Add the text to the window input history */
730 if(win->history_pos != NULL)
732 g_free(win->history->data);
733 win->history = g_list_delete_link(win->history, win->history);
736 win->history = g_list_prepend(win->history, g_strdup(text));
737 win->history_pos = NULL;
740 return chars_written;
743 /* Internal function: Callback for signal insert-text on a text buffer window.
744 Runs after the default handler has already inserted the text. */
746 after_window_insert_text(GtkTextBuffer *textbuffer, GtkTextIter *location, gchar *text, gint len, winid_t win)
748 GtkTextBuffer *window_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
750 /* Set the history position to NULL and erase the text we were already editing */
751 if(win->history_pos != NULL)
753 g_free(win->history->data);
754 win->history = g_list_delete_link(win->history, win->history);
755 win->history_pos = NULL;
758 /* Apply the 'input' style to the text that was entered */
759 GtkTextIter end_iter;
760 gtk_text_buffer_get_end_iter(window_buffer, &end_iter);
761 GtkTextIter input_iter;
762 GtkTextMark *input_position = gtk_text_buffer_get_mark(window_buffer, "input_position");
763 gtk_text_buffer_get_iter_at_mark(window_buffer, &input_iter, input_position);
764 gtk_text_buffer_apply_tag_by_name(window_buffer, "input", &input_iter, &end_iter);
767 /* Internal function: Callback for signal activate on the line input GtkEntry
768 in a text grid window. */
770 on_input_entry_activate(GtkEntry *input_entry, winid_t win)
772 g_signal_handler_block(win->widget, win->line_input_keypress_handler);
774 int chars_written = finish_text_grid_line_input(win, TRUE);
775 ChimaraGlk *glk = CHIMARA_GLK(gtk_widget_get_ancestor(win->widget, CHIMARA_TYPE_GLK));
776 event_throw(glk, evtype_LineInput, win, chars_written, 0);
779 /* Internal function: Callback for signal key-press-event on the line input
780 GtkEntry in a text grid window. */
782 on_input_entry_key_press_event(GtkEntry *input_entry, GdkEventKey *event, winid_t win)
784 if(event->keyval == GDK_Up || event->keyval == GDK_KP_Up
785 || event->keyval == GDK_Down || event->keyval == GDK_KP_Down)
787 /* Prevent falling off the end of the history list */
788 if( (event->keyval == GDK_Up || event->keyval == GDK_KP_Up)
789 && win->history_pos && win->history_pos->next == NULL)
791 if( (event->keyval == GDK_Down || event->keyval == GDK_KP_Down)
792 && (win->history_pos == NULL || win->history_pos->prev == NULL) )
795 if(win->history_pos == NULL)
797 const gchar *current_input = gtk_entry_get_text(input_entry);
798 win->history = g_list_prepend(win->history, g_strdup(current_input));
799 win->history_pos = win->history;
802 if(event->keyval == GDK_Up || event->keyval == GDK_KP_Up)
805 win->history_pos = g_list_next(win->history_pos);
807 win->history_pos = win->history;
810 win->history_pos = g_list_previous(win->history_pos);
812 /* Insert the history item into the window */
813 g_signal_handler_block(input_entry, win->line_input_entry_changed);
814 gtk_entry_set_text(input_entry, win->history_pos->data);
815 g_signal_handler_unblock(input_entry, win->line_input_entry_changed);
818 else if(g_slist_find(win->current_extra_line_terminators, GUINT_TO_POINTER(event->keyval)))
820 /* If this key was a line terminator, pretend we pressed enter */
821 on_input_entry_activate(input_entry, win);
827 on_input_entry_changed(GtkEditable *editable, winid_t win)
829 /* Set the history position to NULL and erase the text we were already editing */
830 if(win->history_pos != NULL)
832 g_free(win->history->data);
833 win->history = g_list_delete_link(win->history, win->history);
834 win->history_pos = NULL;
839 keyval_to_glk_keycode(guint keyval, gboolean unicode)
844 case GDK_KP_Up: return keycode_Up;
846 case GDK_KP_Down: return keycode_Down;
848 case GDK_KP_Left: return keycode_Left;
850 case GDK_KP_Right: return keycode_Right;
853 case GDK_KP_Enter: return keycode_Return;
856 case GDK_KP_Delete: return keycode_Delete;
857 case GDK_Escape: return keycode_Escape;
859 case GDK_KP_Tab: return keycode_Tab;
861 case GDK_KP_Page_Up: return keycode_PageUp;
863 case GDK_KP_Page_Down: return keycode_PageDown;
865 case GDK_KP_Home: return keycode_Home;
867 case GDK_KP_End: return keycode_End;
869 case GDK_KP_F1: return keycode_Func1;
871 case GDK_KP_F2: return keycode_Func2;
873 case GDK_KP_F3: return keycode_Func3;
875 case GDK_KP_F4: return keycode_Func4;
876 case GDK_F5: return keycode_Func5;
877 case GDK_F6: return keycode_Func6;
878 case GDK_F7: return keycode_Func7;
879 case GDK_F8: return keycode_Func8;
880 case GDK_F9: return keycode_Func9;
881 case GDK_F10: return keycode_Func10;
882 case GDK_F11: return keycode_Func11;
883 case GDK_F12: return keycode_Func12;
885 keycode = gdk_keyval_to_unicode(keyval);
886 /* If keycode is 0, then keyval was not recognized; also return
887 unknown if Latin-1 input was requested and the character is not in
889 if(keycode == 0 || (!unicode && keycode > 255))
890 return keycode_Unknown;
896 force_char_input_from_queue(winid_t win, event_t *event)
898 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
899 guint keyval = GPOINTER_TO_UINT(g_async_queue_pop(glk_data->char_input_queue));
901 glk_cancel_char_event(win);
904 ChimaraGlk *glk = CHIMARA_GLK(gtk_widget_get_ancestor(win->widget, CHIMARA_TYPE_GLK));
906 g_signal_emit_by_name(glk, "char-input", win->rock, keyval);
909 event->type = evtype_CharInput;
911 event->val1 = keyval_to_glk_keycode(keyval, win->input_request_type == INPUT_REQUEST_CHARACTER_UNICODE);
916 force_line_input_from_queue(winid_t win, event_t *event)
918 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
919 const gchar *text = g_async_queue_pop(glk_data->line_input_queue);
920 glui32 chars_written = 0;
923 if(win->type == wintype_TextBuffer)
925 GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
926 GtkTextIter start, end;
928 /* Remove signal handlers so the line input doesn't get picked up again */
929 g_signal_handler_block(buffer, win->insert_text_handler);
930 g_signal_handler_block(win->widget, win->line_input_keypress_handler);
932 /* Erase any text that was already typed */
933 GtkTextMark *input_position = gtk_text_buffer_get_mark(buffer, "input_position");
934 gtk_text_buffer_get_iter_at_mark(buffer, &start, input_position);
935 gtk_text_buffer_get_end_iter(buffer, &end);
936 gtk_text_buffer_delete(buffer, &start, &end);
938 /* Make the window uneditable again */
939 gtk_text_view_set_editable(GTK_TEXT_VIEW(win->widget), FALSE);
941 /* Insert the forced input into the window */
942 if(win->echo_current_line_input)
944 gtk_text_buffer_get_end_iter(buffer, &end);
945 gchar *text_to_insert = g_strconcat(text, "\n", NULL);
946 gtk_text_buffer_insert_with_tags_by_name(buffer, &end, text_to_insert, -1, "default", "input", NULL);
949 chars_written = finish_text_buffer_line_input(win, TRUE);
951 else if(win->type == wintype_TextGrid)
953 /* Remove signal handlers so the line input doesn't get picked up again */
954 g_signal_handler_block(win->widget, win->char_input_keypress_handler);
956 /* Insert the forced input into the window */
957 gtk_entry_set_text(GTK_ENTRY(win->input_entry), text);
958 chars_written = finish_text_grid_line_input(win, TRUE);
962 event->type = evtype_LineInput;
964 event->val1 = chars_written;
968 /*** Internal function: cancels any pending input requests on the window and presents a warning if not INPUT_REQUEST_NONE ***/
970 cancel_old_input_request(winid_t win)
972 switch(win->input_request_type) {
973 case INPUT_REQUEST_NONE:
974 break; /* All is well */
975 case INPUT_REQUEST_CHARACTER:
976 case INPUT_REQUEST_CHARACTER_UNICODE:
977 glk_cancel_char_event(win);
978 WARNING("Cancelling pending char event");
980 case INPUT_REQUEST_LINE:
981 case INPUT_REQUEST_LINE_UNICODE:
982 glk_cancel_line_event(win, NULL);
983 WARNING("Cancelling pending line event");
986 WARNING("Could not cancel pending input request: unknown input request");
991 * glk_set_echo_line_event:
992 * @win: The window in which to change the echoing behavior.
993 * @val: Zero to turn off echoing, nonzero for normal echoing.
995 * Normally, after line input is completed or cancelled in a buffer window, the
996 * library ensures that the complete input line (or its latest state, after
997 * cancelling) is displayed at the end of the buffer, followed by a newline.
998 * This call allows you to suppress this behavior. If the @val argument is zero,
999 * all <emphasis>subsequent</emphasis> line input requests in the given window
1000 * will leave the buffer unchanged after the input is completed or cancelled;
1001 * the player's input will not be printed. If @val is nonzero, subsequent input
1002 * requests will have the normal printing behavior.
1005 * Note that this feature is unrelated to the window's echo stream.
1008 * If you turn off line input echoing, you can reproduce the standard input
1009 * behavior by following each line input event (or line input cancellation) by
1010 * printing the input line, in the Input style, followed by a newline in the
1013 * The glk_set_echo_line_event() does not affect a pending line input request.
1014 * It also has no effect in non-buffer windows.
1016 * In a grid window, the game can overwrite the input area at will, so there
1017 * is no need for this distinction.
1020 * Not all libraries support this feature. You can test for it with
1021 * %gestalt_LineInputEcho.
1024 glk_set_echo_line_event(winid_t win, glui32 val)
1026 VALID_WINDOW(win, return);
1028 if(win->type != wintype_TextBuffer)
1029 return; /* Quietly do nothing */
1031 win->echo_line_input = val? TRUE : FALSE;
1034 /* Internal function to convert from a Glk keycode to a GDK keyval. */
1036 keycode_to_gdk_keyval(glui32 keycode)
1048 case keycode_Return:
1050 case keycode_Delete:
1052 case keycode_Escape:
1056 case keycode_PageUp:
1058 case keycode_PageDown:
1059 return GDK_Page_Down;
1082 case keycode_Func10:
1084 case keycode_Func11:
1086 case keycode_Func12:
1089 return GDK_BackSpace;
1091 unsigned keyval = gdk_unicode_to_keyval(keycode);
1092 if(keyval < 0x01000000) /* magic number meaning illegal unicode point */
1097 /* Internal function to decide whether @keycode is a valid line terminator. */
1099 is_valid_line_terminator(glui32 keycode)
1102 case keycode_Escape:
1112 case keycode_Func10:
1113 case keycode_Func11:
1114 case keycode_Func12:
1121 * glk_set_terminators_line_event:
1122 * @win: The window for which to set the line input terminator keys.
1123 * @keycodes: An array of <code>keycode_</code> constants, of length @count.
1124 * @count: The array length of @keycodes.
1126 * It is possible to request that other keystrokes complete line input as well.
1127 * (This allows a game to intercept function keys or other special keys during
1128 * line input.) To do this, call glk_set_terminators_line_event(), and pass an
1129 * array of @count keycodes. These must all be special keycodes (see <link
1130 * linkend="chimara-Character-Input">Character Input</link>). Do not include
1131 * regular printable characters in the array, nor %keycode_Return (which
1132 * represents the default <keycap>enter</keycap> key and will always be
1133 * recognized). To return to the default behavior, pass a %NULL or empty array.
1135 * The glk_set_terminators_line_event() affects <emphasis>subsequent</emphasis>
1136 * line input requests in the given window. It does not affect a pending line
1140 * This distinction makes life easier for interpreters that set up UI
1141 * callbacks only at the start of input.
1144 * A library may not support this feature; if it does, it may not support all
1145 * special keys as terminators. (Some keystrokes are reserved for OS or
1146 * interpreter control.) You can test for this with %gestalt_LineTerminators and
1147 * %gestalt_LineTerminatorKey.
1150 glk_set_terminators_line_event(winid_t win, glui32 *keycodes, glui32 count)
1152 VALID_WINDOW(win, return);
1154 g_slist_free(win->extra_line_terminators);
1155 win->extra_line_terminators = NULL;
1157 if(keycodes == NULL || count == 0)
1161 for(i = 0; i < count; i++)
1163 unsigned key = keycode_to_gdk_keyval(keycodes[i]);
1164 if(is_valid_line_terminator(keycodes[i]))
1165 win->extra_line_terminators = g_slist_prepend(win->extra_line_terminators, GUINT_TO_POINTER(key));
1167 WARNING_S("Ignoring invalid line terminator", gdk_keyval_name(key));