5 #include "chimara-glk-private.h"
7 extern GPrivate *glk_data_key;
9 /* Forward declarations */
10 static int finish_text_buffer_line_input(winid_t win, gboolean emit_signal);
11 static int finish_text_grid_line_input(winid_t win, gboolean emit_signal);
12 static void cancel_old_input_request(winid_t win);
14 /* Internal function: code common to both flavors of char event request */
16 request_char_event_common(winid_t win, gboolean unicode)
18 VALID_WINDOW(win, return);
19 g_return_if_fail(win->type != wintype_TextBuffer || win->type != wintype_TextGrid);
21 cancel_old_input_request(win);
23 flush_window_buffer(win);
25 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
27 win->input_request_type = unicode? INPUT_REQUEST_CHARACTER_UNICODE : INPUT_REQUEST_CHARACTER;
28 g_signal_handler_unblock( win->widget, win->char_input_keypress_handler );
31 gtk_widget_grab_focus( GTK_WIDGET(win->widget) );
34 /* Emit the "waiting" signal to let listeners know we are ready for input */
35 g_signal_emit_by_name(glk_data->self, "waiting");
37 /* Schedule a check for the pager */
39 if(win->type == wintype_TextBuffer)
40 g_idle_add(pager_check, win);
45 * glk_request_char_event:
46 * @win: A window to request char events from.
48 * Request input of a Latin-1 character or special key. A window cannot have
49 * requests for both character and line input at the same time. Nor can it have
50 * requests for character input of both types (Latin-1 and Unicode). It is
51 * illegal to call glk_request_char_event() if the window already has a pending
52 * request for either character or line input.
55 glk_request_char_event(winid_t win)
57 request_char_event_common(win, FALSE);
61 * glk_request_char_event_uni:
62 * @win: A window to request char events from.
64 * Request input of a Unicode character or special key. See
65 * glk_request_char_event().
68 glk_request_char_event_uni(winid_t win)
70 request_char_event_common(win, TRUE);
74 * glk_cancel_char_event:
75 * @win: A window to cancel the latest char event request on.
77 * This cancels a pending request for character input. (Either Latin-1 or
78 * Unicode.) For convenience, it is legal to call glk_cancel_char_event() even
79 * if there is no charcter input request on that window. Glk will ignore the
83 glk_cancel_char_event(winid_t win)
85 VALID_WINDOW(win, return);
86 g_return_if_fail(win->type != wintype_TextBuffer || win->type != wintype_TextGrid);
88 if(win->input_request_type == INPUT_REQUEST_CHARACTER || win->input_request_type == INPUT_REQUEST_CHARACTER_UNICODE)
90 win->input_request_type = INPUT_REQUEST_NONE;
91 g_signal_handler_block( win->widget, win->char_input_keypress_handler );
95 /* Internal function: Request either latin-1 or unicode line input, in a text grid window. */
97 text_grid_request_line_event_common(winid_t win, glui32 maxlen, gboolean insert, gchar *inserttext)
101 GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
103 GtkTextMark *cursor = gtk_text_buffer_get_mark(buffer, "cursor_position");
104 GtkTextIter start_iter, end_iter;
105 gtk_text_buffer_get_iter_at_mark(buffer, &start_iter, cursor);
107 /* Determine the maximum length of the line input */
108 gint cursorpos = gtk_text_iter_get_line_offset(&start_iter);
109 /* Odd; the Glk spec says the maximum input length is
110 windowwidth - 1 - cursorposition. I say no, because if cursorposition is
111 zero, then the input should fill the whole line. FIXME??? */
112 win->input_length = MIN(win->width - cursorpos, win->line_input_buffer_max_len);
113 end_iter = start_iter;
114 gtk_text_iter_set_line_offset(&end_iter, cursorpos + win->input_length);
116 /* If the buffer currently has a selection with one bound in the middle of
117 the input field, then deselect it. Otherwise the input field gets trashed */
118 GtkTextIter start_sel, end_sel;
119 if( gtk_text_buffer_get_selection_bounds(buffer, &start_sel, &end_sel) )
121 if( gtk_text_iter_in_range(&start_sel, &start_iter, &end_iter) )
122 gtk_text_buffer_place_cursor(buffer, &end_sel);
123 if( gtk_text_iter_in_range(&end_sel, &start_iter, &end_iter) )
124 gtk_text_buffer_place_cursor(buffer, &start_sel);
127 /* Erase the text currently in the input field and replace it with a GtkEntry */
128 gtk_text_buffer_delete(buffer, &start_iter, &end_iter);
129 win->input_anchor = gtk_text_buffer_create_child_anchor(buffer, &start_iter);
130 win->input_entry = gtk_entry_new();
131 /* Set the entry's font to match that of the window */
132 GtkRcStyle *style = gtk_widget_get_modifier_style(win->widget); /* Don't free */
133 gtk_widget_modify_font(win->input_entry, style->font_desc);
134 /* Make the entry as small as possible to fit with the text */
135 gtk_entry_set_has_frame(GTK_ENTRY(win->input_entry), FALSE);
136 GtkBorder border = { 0, 0, 0, 0 };
139 #if GTK_CHECK_VERSION(2,10,0)
140 gtk_entry_set_inner_border(GTK_ENTRY(win->input_entry), &border);
142 gtk_entry_set_max_length(GTK_ENTRY(win->input_entry), win->input_length);
143 gtk_entry_set_width_chars(GTK_ENTRY(win->input_entry), win->input_length);
145 /* Insert pre-entered text if needed */
147 gtk_entry_set_text(GTK_ENTRY(win->input_entry), inserttext);
149 /* Set background color of entry (TODO: implement as property) */
151 gdk_color_parse("grey", &background);
152 gtk_widget_modify_base(win->input_entry, GTK_STATE_NORMAL, &background);
154 g_signal_connect(win->input_entry, "activate", G_CALLBACK(on_input_entry_activate), win);
155 g_signal_connect(win->input_entry, "key-press-event", G_CALLBACK(on_input_entry_key_press_event), win);
156 win->line_input_entry_changed = g_signal_connect(win->input_entry, "changed", G_CALLBACK(on_input_entry_changed), win);
158 gtk_widget_show(win->input_entry);
159 gtk_text_view_add_child_at_anchor(GTK_TEXT_VIEW(win->widget), win->input_entry, win->input_anchor);
161 gtk_widget_grab_focus(win->input_entry);
166 /* Internal function: Request either latin-1 or unicode line input, in a text buffer window. */
168 text_buffer_request_line_event_common(winid_t win, glui32 maxlen, gboolean insert, gchar *inserttext)
170 flush_window_buffer(win);
174 GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
176 /* Move the input_position mark to the end of the window_buffer */
177 GtkTextMark *input_position = gtk_text_buffer_get_mark(buffer, "input_position");
178 GtkTextIter end_iter;
179 gtk_text_buffer_get_end_iter(buffer, &end_iter);
180 gtk_text_buffer_move_mark(buffer, input_position, &end_iter);
182 /* Set the entire contents of the window_buffer as uneditable
183 * (so input can only be entered at the end) */
184 GtkTextIter start_iter;
185 gtk_text_buffer_get_start_iter(buffer, &start_iter);
186 gtk_text_buffer_remove_tag_by_name(buffer, "uneditable", &start_iter, &end_iter);
187 gtk_text_buffer_apply_tag_by_name(buffer, "uneditable", &start_iter, &end_iter);
189 /* Insert pre-entered text if needed */
191 gtk_text_buffer_insert(buffer, &end_iter, inserttext, -1);
192 gtk_text_buffer_get_end_iter(buffer, &end_iter); /* update after text insertion */
195 /* Apply the correct style to the input prompt */
196 GtkTextIter input_iter;
197 gtk_text_buffer_get_iter_at_mark(buffer, &input_iter, input_position);
198 gtk_text_buffer_apply_tag_by_name(buffer, "input", &input_iter, &end_iter);
200 gtk_text_view_set_editable(GTK_TEXT_VIEW(win->widget), TRUE);
202 g_signal_handler_unblock(buffer, win->insert_text_handler);
203 gtk_widget_grab_focus(win->widget);
207 /* Schedule a check for the pager */
209 g_idle_add(pager_check, win);
214 * glk_request_line_event:
215 * @win: A text buffer or text grid window to request line input on.
216 * @buf: A buffer of at least @maxlen bytes.
217 * @maxlen: Length of the buffer.
218 * @initlen: The number of characters in @buf to pre-enter.
220 * Requests input of a line of Latin-1 characters. A window cannot have requests
221 * for both character and line input at the same time. Nor can it have requests
222 * for line input of both types (Latin-1 and Unicode). It is illegal to call
223 * glk_request_line_event() if the window already has a pending request for
224 * either character or line input.
226 * The @buf argument is a pointer to space where the line input will be stored.
227 * (This may not be %NULL.) @maxlen is the length of this space, in bytes; the
228 * library will not accept more characters than this. If @initlen is nonzero,
229 * then the first @initlen bytes of @buf will be entered as pre-existing input
230 * — just as if the player had typed them himself. (The player can continue
231 * composing after this pre-entered input, or delete it or edit as usual.)
233 * The contents of the buffer are undefined until the input is completed (either
234 * by a line input event, or glk_cancel_line_event(). The library may or may not
235 * fill in the buffer as the player composes, while the input is still pending;
236 * it is illegal to change the contents of the buffer yourself.
239 glk_request_line_event(winid_t win, char *buf, glui32 maxlen, glui32 initlen)
241 VALID_WINDOW(win, return);
242 g_return_if_fail(buf);
243 g_return_if_fail(win->type != wintype_TextBuffer || win->type != wintype_TextGrid);
244 g_return_if_fail(initlen <= maxlen);
246 cancel_old_input_request(win);
248 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
250 /* Register the buffer */
251 if(glk_data->register_arr)
252 win->buffer_rock = (*glk_data->register_arr)(buf, maxlen, "&+#!Cn");
254 win->input_request_type = INPUT_REQUEST_LINE;
255 win->line_input_buffer = buf;
256 win->line_input_buffer_max_len = maxlen;
258 gchar *inserttext = (initlen > 0)? g_strndup(buf, initlen) : g_strdup("");
261 case wintype_TextBuffer:
262 text_buffer_request_line_event_common(win, maxlen, (initlen > 0), inserttext);
264 case wintype_TextGrid:
265 text_grid_request_line_event_common(win, maxlen, (initlen > 0), inserttext);
269 g_signal_handler_unblock(win->widget, win->line_input_keypress_handler);
271 /* Emit the "waiting" signal to let listeners know we are ready for input */
272 g_signal_emit_by_name(glk_data->self, "waiting");
276 * glk_request_line_event_uni:
277 * @win: A text buffer or text grid window to request line input on.
278 * @buf: A buffer of at least @maxlen characters.
279 * @maxlen: Length of the buffer.
280 * @initlen: The number of characters in @buf to pre-enter.
282 * Request input of a line of Unicode characters. This works the same as
283 * glk_request_line_event(), except the result is stored in an array of
284 * <type>glui32</type> values instead of an array of characters, and the values
285 * may be any valid Unicode code points.
287 * The result will be in Unicode Normalization Form C. This basically means that
288 * composite characters will be single characters where possible, instead of
289 * sequences of base and combining marks. See
290 * <ulink url="http://www.unicode.org/reports/tr15/">Unicode Standard Annex
291 * #15</ulink> for the details.
294 glk_request_line_event_uni(winid_t win, glui32 *buf, glui32 maxlen, glui32 initlen)
296 VALID_WINDOW(win, return);
297 g_return_if_fail(buf);
298 g_return_if_fail(win->type != wintype_TextBuffer || win->type != wintype_TextGrid);
299 g_return_if_fail(initlen <= maxlen);
301 cancel_old_input_request(win);
302 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
304 /* Register the buffer */
305 if(glk_data->register_arr)
306 win->buffer_rock = (*glk_data->register_arr)(buf, maxlen, "&+#!Iu");
310 win->input_request_type = INPUT_REQUEST_LINE_UNICODE;
311 win->line_input_buffer_unicode = buf;
312 win->line_input_buffer_max_len = maxlen;
316 utf8 = convert_ucs4_to_utf8(buf, initlen);
325 case wintype_TextBuffer:
326 text_buffer_request_line_event_common(win, maxlen, (initlen > 0), utf8);
328 case wintype_TextGrid:
329 text_grid_request_line_event_common(win, maxlen, (initlen > 0), utf8);
332 g_signal_handler_unblock(win->widget, win->line_input_keypress_handler);
335 /* Emit the "waiting" signal to let listeners know we are ready for input */
336 g_signal_emit_by_name(glk_data->self, "waiting");
340 * glk_cancel_line_event:
341 * @win: A text buffer or text grid window to cancel line input on.
342 * @event: Will be filled in if the user had already input something.
344 * This cancels a pending request for line input. (Either Latin-1 or Unicode.)
346 * The event pointed to by the event argument will be filled in as if the
347 * player had hit <keycap>enter</keycap>, and the input composed so far will be
348 * stored in the buffer; see below. If you do not care about this information,
349 * pass %NULL as the @event argument. (The buffer will still be filled.)
351 * For convenience, it is legal to call glk_cancel_line_event() even if there
352 * is no line input request on that window. The event type will be set to
353 * %evtype_None in this case.
356 glk_cancel_line_event(winid_t win, event_t *event)
358 VALID_WINDOW(win, return);
359 g_return_if_fail(win->type != wintype_TextBuffer || win->type != wintype_TextGrid);
362 event->type = evtype_None;
368 if(win->input_request_type != INPUT_REQUEST_LINE && win->input_request_type != INPUT_REQUEST_LINE_UNICODE)
371 g_signal_handler_block( win->widget, win->line_input_keypress_handler );
373 int chars_written = 0;
376 if(win->type == wintype_TextGrid) {
377 chars_written = finish_text_grid_line_input(win, FALSE);
378 } else if(win->type == wintype_TextBuffer) {
379 gtk_text_view_set_editable( GTK_TEXT_VIEW(win->widget), FALSE );
380 GtkTextBuffer *window_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
381 g_signal_handler_block(window_buffer, win->insert_text_handler);
382 chars_written = finish_text_buffer_line_input(win, FALSE);
386 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
387 if(glk_data->unregister_arr)
389 if(win->input_request_type == INPUT_REQUEST_LINE_UNICODE)
390 (*glk_data->unregister_arr)(win->line_input_buffer_unicode, win->line_input_buffer_max_len, "&+#!Iu", win->buffer_rock);
392 (*glk_data->unregister_arr)(win->line_input_buffer, win->line_input_buffer_max_len, "&+#!Cn", win->buffer_rock);
395 if(event != NULL && chars_written > 0) {
396 event->type = evtype_LineInput;
397 event->val1 = chars_written;
401 /* Helper function: Turn off shutdown key-press-event signal handler */
403 turn_off_handler(GNode *node)
405 winid_t win = node->data;
406 g_signal_handler_block(win->widget, win->shutdown_keypress_handler);
407 return FALSE; /* don't stop */
410 /* Internal function: Callback for signal key-press-event while waiting for shutdown. */
412 on_shutdown_key_press_event(GtkWidget *widget, GdkEventKey *event, winid_t win)
414 ChimaraGlk *glk = CHIMARA_GLK(gtk_widget_get_ancestor(widget, CHIMARA_TYPE_GLK));
416 CHIMARA_GLK_USE_PRIVATE(glk, priv);
418 /* Turn off all the signal handlers */
419 if(priv->root_window)
420 g_node_traverse(priv->root_window, G_IN_ORDER, G_TRAVERSE_LEAVES, -1, (GNodeTraverseFunc)turn_off_handler, NULL);
422 /* Signal the Glk library that it can shut everything down now */
423 g_mutex_lock(priv->shutdown_lock);
424 g_cond_signal(priv->shutdown_key_pressed);
425 g_mutex_unlock(priv->shutdown_lock);
427 return TRUE; /* block the event */
430 /* 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. */
432 on_char_input_key_press_event(GtkWidget *widget, GdkEventKey *event, winid_t win)
434 /* Ignore modifier keys, otherwise the char input will already trigger on
435 the shift key when the user tries to type a capital letter */
436 if(event->is_modifier)
437 return FALSE; /* don't stop the event */
439 glui32 keycode = keyval_to_glk_keycode(event->keyval, win->input_request_type == INPUT_REQUEST_CHARACTER_UNICODE);
441 ChimaraGlk *glk = CHIMARA_GLK(gtk_widget_get_ancestor(widget, CHIMARA_TYPE_GLK));
443 event_throw(glk, evtype_CharInput, win, keycode, 0);
444 g_signal_emit_by_name(glk, "char-input", win->rock, event->keyval);
446 /* Only one keypress will be handled */
447 win->input_request_type = INPUT_REQUEST_NONE;
448 g_signal_handler_block(win->widget, win->char_input_keypress_handler);
454 on_line_input_key_press_event(GtkWidget *widget, GdkEventKey *event, winid_t win)
458 case wintype_TextBuffer:
460 /* History up/down */
461 if(event->keyval == GDK_Up || event->keyval == GDK_KP_Up
462 || event->keyval == GDK_Down || event->keyval == GDK_KP_Down)
464 /* Prevent falling off the end of the history list */
465 if(win->history == NULL)
467 if( (event->keyval == GDK_Up || event->keyval == GDK_KP_Up)
468 && win->history_pos && win->history_pos->next == NULL)
470 if( (event->keyval == GDK_Down || event->keyval == GDK_KP_Down)
471 && (win->history_pos == NULL || win->history_pos->prev == NULL) )
474 GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(win->widget));
475 GtkTextIter start, end;
476 /* Erase any text that was already typed */
477 GtkTextMark *input_position = gtk_text_buffer_get_mark(buffer, "input_position");
478 gtk_text_buffer_get_iter_at_mark(buffer, &start, input_position);
479 gtk_text_buffer_get_end_iter(buffer, &end);
481 if(win->history_pos == NULL) {
482 gchar *current_input = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
483 win->history = g_list_prepend(win->history, current_input);
484 win->history_pos = win->history;
487 gtk_text_buffer_delete(buffer, &start, &end);
489 if(event->keyval == GDK_Up || event->keyval == GDK_KP_Up)
492 win->history_pos = g_list_next(win->history_pos);
494 win->history_pos = win->history;
497 win->history_pos = g_list_previous(win->history_pos);
499 /* Insert the history item into the window */
500 gtk_text_buffer_get_end_iter(buffer, &end);
502 g_signal_handler_block(buffer, win->insert_text_handler);
503 gtk_text_buffer_insert_with_tags_by_name(buffer, &end, win->history_pos->data, -1, "input", NULL);
504 g_signal_handler_unblock(buffer, win->insert_text_handler);
508 /* Move to beginning/end of input field */
509 else if(event->keyval == GDK_Home) {
510 GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(win->widget));
511 GtkTextIter input_iter;
512 GtkTextMark *input_position = gtk_text_buffer_get_mark(buffer, "input_position");
513 gtk_text_buffer_get_iter_at_mark(buffer, &input_iter, input_position);
514 gtk_text_buffer_place_cursor(buffer, &input_iter);
517 else if(event->keyval == GDK_End) {
518 GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(win->widget));
519 GtkTextIter end_iter;
520 gtk_text_buffer_get_end_iter(buffer, &end_iter);
521 gtk_text_buffer_place_cursor(buffer, &end_iter);
525 /* Handle the enter key, which could occur in the middle of the sentence. */
526 else if(event->keyval == GDK_Return || event->keyval == GDK_KP_Enter) {
527 GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(win->widget));
528 GtkTextIter end_iter;
529 gtk_text_buffer_get_end_iter(buffer, &end_iter);
530 gtk_text_buffer_place_cursor(buffer, &end_iter);
536 /* If this is a text grid window, then redirect the key press to the line input GtkEntry */
537 case wintype_TextGrid:
539 if(event->keyval == GDK_Up || event->keyval == GDK_KP_Up
540 || event->keyval == GDK_Down || event->keyval == GDK_KP_Down
541 || event->keyval == GDK_Left || event->keyval == GDK_KP_Left
542 || event->keyval == GDK_Right || event->keyval == GDK_KP_Right
543 || event->keyval == GDK_Tab || event->keyval == GDK_KP_Tab
544 || event->keyval == GDK_Page_Up || event->keyval == GDK_KP_Page_Up
545 || event->keyval == GDK_Page_Down || event->keyval == GDK_KP_Page_Down
546 || event->keyval == GDK_Home || event->keyval == GDK_KP_Home
547 || event->keyval == GDK_End || event->keyval == GDK_KP_End)
548 return FALSE; /* Don't redirect these keys */
549 gtk_widget_grab_focus(win->input_entry);
550 gtk_editable_set_position(GTK_EDITABLE(win->input_entry), -1);
551 gboolean retval = TRUE;
552 g_signal_emit_by_name(win->input_entry, "key-press-event", event, &retval);
553 return retval; /* Block this key event if the entry handled it */
559 /* Internal function: finish handling a line input request, for both text grid and text buffer windows. */
561 write_to_window_buffer(winid_t win, const gchar *inserted_text)
565 /* Convert the string from UTF-8 to Latin-1 or Unicode */
566 if(win->input_request_type == INPUT_REQUEST_LINE)
569 gchar *latin1 = convert_utf8_to_latin1(inserted_text, &bytes_written);
574 /* Place input in the echo stream */
575 if(win->echo_stream != NULL)
576 glk_put_string_stream(win->echo_stream, latin1);
578 /* Copy the string (bytes_written does not include the NULL at the end) */
579 copycount = MIN(win->line_input_buffer_max_len, bytes_written);
580 memcpy(win->line_input_buffer, latin1, copycount);
583 else if(win->input_request_type == INPUT_REQUEST_LINE_UNICODE)
586 gunichar *unicode = convert_utf8_to_ucs4(inserted_text, &items_written);
591 /* Place input in the echo stream */
592 if(win->echo_stream != NULL)
593 glk_put_string_stream_uni(win->echo_stream, unicode);
595 /* Copy the string (but not the NULL at the end) */
596 copycount = MIN(win->line_input_buffer_max_len, items_written);
597 memcpy(win->line_input_buffer_unicode, unicode, copycount * sizeof(gunichar));
601 WARNING("Wrong input request type");
603 win->input_request_type = INPUT_REQUEST_NONE;
607 /* Internal function: Retrieves the input of a TextBuffer window and stores it in the window buffer.
608 * Returns the number of characters written, suitable for inclusion in a line input event. */
610 finish_text_buffer_line_input(winid_t win, gboolean emit_signal)
612 VALID_WINDOW(win, return 0);
613 g_return_val_if_fail(win->type == wintype_TextBuffer, 0);
615 GtkTextIter start_iter, end_iter, last_character;
617 GtkTextBuffer *window_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
618 GtkTextMark *input_position = gtk_text_buffer_get_mark(window_buffer, "input_position");
619 gtk_text_buffer_get_iter_at_mark(window_buffer, &start_iter, input_position);
620 gtk_text_buffer_get_end_iter(window_buffer, &end_iter);
621 gtk_text_buffer_get_end_iter(window_buffer, &last_character);
622 gtk_text_iter_backward_cursor_position(&last_character);
624 gchar* last_char = gtk_text_buffer_get_text(window_buffer, &last_character, &end_iter, FALSE);
626 if( strchr(last_char, '\n') != NULL )
627 gtk_text_iter_backward_cursor_position(&end_iter);
629 gchar* inserted_text = gtk_text_buffer_get_text(window_buffer, &start_iter, &end_iter, FALSE);
631 int chars_written = write_to_window_buffer(win, inserted_text);
634 ChimaraGlk *glk = CHIMARA_GLK(gtk_widget_get_ancestor(win->widget, CHIMARA_TYPE_GLK));
636 g_signal_emit_by_name(glk, "line-input", win->rock, inserted_text);
639 /* Add the text to the window input history */
640 if(win->history_pos != NULL)
642 g_free(win->history->data);
643 win->history = g_list_delete_link(win->history, win->history);
645 if(*inserted_text != 0)
646 win->history = g_list_prepend(win->history, g_strdup(inserted_text));
648 win->history_pos = NULL;
650 g_free(inserted_text);
652 return chars_written;
655 /* Internal function: Retrieves the input of a TextGrid window and stores it in the window buffer.
656 * Returns the number of characters written, suitable for inclusion in a line input event. */
658 finish_text_grid_line_input(winid_t win, gboolean emit_signal)
660 VALID_WINDOW(win, return 0);
661 g_return_val_if_fail(win->type == wintype_TextGrid, 0);
663 GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
665 gchar *text = g_strdup( gtk_entry_get_text(GTK_ENTRY(win->input_entry)) );
666 /* Move the focus back into the text view */
667 gtk_widget_grab_focus(win->widget);
668 /* Remove entry widget from text view */
669 /* Should be ok even though this is the widget's own signal handler */
670 gtk_container_remove( GTK_CONTAINER(win->widget), GTK_WIDGET(win->input_entry) );
671 win->input_entry = NULL;
672 /* Delete the child anchor */
673 GtkTextIter start, end;
674 gtk_text_buffer_get_iter_at_child_anchor(buffer, &start, win->input_anchor);
676 gtk_text_iter_forward_char(&end); /* Point after the child anchor */
677 gtk_text_buffer_delete(buffer, &start, &end);
678 win->input_anchor = NULL;
680 gchar *spaces = g_strnfill(win->input_length - g_utf8_strlen(text, -1), ' ');
681 gchar *text_to_insert = g_strconcat(text, spaces, NULL);
683 gtk_text_buffer_insert(buffer, &start, text_to_insert, -1);
684 g_free(text_to_insert);
686 int chars_written = write_to_window_buffer(win, text);
689 ChimaraGlk *glk = CHIMARA_GLK(gtk_widget_get_ancestor(win->widget, CHIMARA_TYPE_GLK));
691 g_signal_emit_by_name(glk, "line-input", win->rock, text);
694 /* Add the text to the window input history */
695 if(win->history_pos != NULL)
697 g_free(win->history->data);
698 win->history = g_list_delete_link(win->history, win->history);
701 win->history = g_list_prepend(win->history, g_strdup(text));
702 win->history_pos = NULL;
705 return chars_written;
708 /* Internal function: Callback for signal insert-text on a text buffer window.
709 Runs after the default handler has already inserted the text.
710 FIXME: This function assumes that newline was the last character typed into the
711 window. That assumption is wrong if, for example, text containing a newline was
712 pasted into the window. */
714 after_window_insert_text(GtkTextBuffer *textbuffer, GtkTextIter *location, gchar *text, gint len, winid_t win)
716 GtkTextBuffer *window_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
718 /* Set the history position to NULL and erase the text we were already editing */
719 if(win->history_pos != NULL)
721 g_free(win->history->data);
722 win->history = g_list_delete_link(win->history, win->history);
723 win->history_pos = NULL;
725 if( strchr(text, '\n') != NULL )
727 /* Remove signal handlers */
728 g_signal_handler_block(window_buffer, win->insert_text_handler);
729 g_signal_handler_block(win->widget, win->line_input_keypress_handler);
731 /* Make the window uneditable again and retrieve the text that was input */
732 gtk_text_view_set_editable(GTK_TEXT_VIEW(win->widget), FALSE);
734 int chars_written = finish_text_buffer_line_input(win, TRUE);
735 ChimaraGlk *glk = CHIMARA_GLK(gtk_widget_get_ancestor(win->widget, CHIMARA_TYPE_GLK));
736 event_throw(glk, evtype_LineInput, win, chars_written, 0);
739 /* Apply the 'input' style to the text that was entered */
740 GtkTextIter end_iter;
741 gtk_text_buffer_get_end_iter(window_buffer, &end_iter);
742 GtkTextIter input_iter;
743 GtkTextMark *input_position = gtk_text_buffer_get_mark(window_buffer, "input_position");
744 gtk_text_buffer_get_iter_at_mark(window_buffer, &input_iter, input_position);
745 gtk_text_buffer_apply_tag_by_name(window_buffer, "input", &input_iter, &end_iter);
748 /* Internal function: Callback for signal activate on the line input GtkEntry
749 in a text grid window. */
751 on_input_entry_activate(GtkEntry *input_entry, winid_t win)
753 g_signal_handler_block(win->widget, win->line_input_keypress_handler);
755 int chars_written = finish_text_grid_line_input(win, TRUE);
756 ChimaraGlk *glk = CHIMARA_GLK(gtk_widget_get_ancestor(win->widget, CHIMARA_TYPE_GLK));
757 event_throw(glk, evtype_LineInput, win, chars_written, 0);
760 /* Internal function: Callback for signal key-press-event on the line input
761 GtkEntry in a text grid window. */
763 on_input_entry_key_press_event(GtkEntry *input_entry, GdkEventKey *event, winid_t win)
765 if(event->keyval == GDK_Up || event->keyval == GDK_KP_Up
766 || event->keyval == GDK_Down || event->keyval == GDK_KP_Down)
768 /* Prevent falling off the end of the history list */
769 if( (event->keyval == GDK_Up || event->keyval == GDK_KP_Up)
770 && win->history_pos && win->history_pos->next == NULL)
772 if( (event->keyval == GDK_Down || event->keyval == GDK_KP_Down)
773 && (win->history_pos == NULL || win->history_pos->prev == NULL) )
776 if(win->history_pos == NULL)
778 const gchar *current_input = gtk_entry_get_text(input_entry);
779 win->history = g_list_prepend(win->history, g_strdup(current_input));
780 win->history_pos = win->history;
783 if(event->keyval == GDK_Up || event->keyval == GDK_KP_Up)
786 win->history_pos = g_list_next(win->history_pos);
788 win->history_pos = win->history;
791 win->history_pos = g_list_previous(win->history_pos);
793 /* Insert the history item into the window */
794 g_signal_handler_block(input_entry, win->line_input_entry_changed);
795 gtk_entry_set_text(input_entry, win->history_pos->data);
796 g_signal_handler_unblock(input_entry, win->line_input_entry_changed);
803 on_input_entry_changed(GtkEditable *editable, winid_t win)
805 /* Set the history position to NULL and erase the text we were already editing */
806 if(win->history_pos != NULL)
808 g_free(win->history->data);
809 win->history = g_list_delete_link(win->history, win->history);
810 win->history_pos = NULL;
815 keyval_to_glk_keycode(guint keyval, gboolean unicode)
820 case GDK_KP_Up: return keycode_Up;
822 case GDK_KP_Down: return keycode_Down;
824 case GDK_KP_Left: return keycode_Left;
826 case GDK_KP_Right: return keycode_Right;
829 case GDK_KP_Enter: return keycode_Return;
832 case GDK_KP_Delete: return keycode_Delete;
833 case GDK_Escape: return keycode_Escape;
835 case GDK_KP_Tab: return keycode_Tab;
837 case GDK_KP_Page_Up: return keycode_PageUp;
839 case GDK_KP_Page_Down: return keycode_PageDown;
841 case GDK_KP_Home: return keycode_Home;
843 case GDK_KP_End: return keycode_End;
845 case GDK_KP_F1: return keycode_Func1;
847 case GDK_KP_F2: return keycode_Func2;
849 case GDK_KP_F3: return keycode_Func3;
851 case GDK_KP_F4: return keycode_Func4;
852 case GDK_F5: return keycode_Func5;
853 case GDK_F6: return keycode_Func6;
854 case GDK_F7: return keycode_Func7;
855 case GDK_F8: return keycode_Func8;
856 case GDK_F9: return keycode_Func9;
857 case GDK_F10: return keycode_Func10;
858 case GDK_F11: return keycode_Func11;
859 case GDK_F12: return keycode_Func12;
861 keycode = gdk_keyval_to_unicode(keyval);
862 /* If keycode is 0, then keyval was not recognized; also return
863 unknown if Latin-1 input was requested and the character is not in
865 if(keycode == 0 || (!unicode && keycode > 255))
866 return keycode_Unknown;
872 force_char_input_from_queue(winid_t win, event_t *event)
874 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
875 guint keyval = GPOINTER_TO_UINT(g_async_queue_pop(glk_data->char_input_queue));
877 glk_cancel_char_event(win);
880 ChimaraGlk *glk = CHIMARA_GLK(gtk_widget_get_ancestor(win->widget, CHIMARA_TYPE_GLK));
882 g_signal_emit_by_name(glk, "char-input", win->rock, keyval);
885 event->type = evtype_CharInput;
887 event->val1 = keyval_to_glk_keycode(keyval, win->input_request_type == INPUT_REQUEST_CHARACTER_UNICODE);
892 force_line_input_from_queue(winid_t win, event_t *event)
894 ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
895 const gchar *text = g_async_queue_pop(glk_data->line_input_queue);
896 glui32 chars_written = 0;
899 if(win->type == wintype_TextBuffer)
901 GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
902 GtkTextIter start, end;
904 /* Remove signal handlers so the line input doesn't get picked up again */
905 g_signal_handler_block(buffer, win->insert_text_handler);
906 g_signal_handler_block(win->widget, win->line_input_keypress_handler);
908 /* Erase any text that was already typed */
909 GtkTextMark *input_position = gtk_text_buffer_get_mark(buffer, "input_position");
910 gtk_text_buffer_get_iter_at_mark(buffer, &start, input_position);
911 gtk_text_buffer_get_end_iter(buffer, &end);
912 gtk_text_buffer_delete(buffer, &start, &end);
914 /* Make the window uneditable again */
915 gtk_text_view_set_editable(GTK_TEXT_VIEW(win->widget), FALSE);
917 /* Insert the forced input into the window */
918 gtk_text_buffer_get_end_iter(buffer, &end);
919 gchar *text_to_insert = g_strconcat(text, "\n", NULL);
920 gtk_text_buffer_insert_with_tags_by_name(buffer, &end, text_to_insert, -1, "input", NULL);
921 chars_written = finish_text_buffer_line_input(win, TRUE);
923 else if(win->type == wintype_TextGrid)
925 /* Remove signal handlers so the line input doesn't get picked up again */
926 g_signal_handler_block(win->widget, win->char_input_keypress_handler);
928 /* Insert the forced input into the window */
929 gtk_entry_set_text(GTK_ENTRY(win->input_entry), text);
930 chars_written = finish_text_grid_line_input(win, TRUE);
934 event->type = evtype_LineInput;
936 event->val1 = chars_written;
940 /*** Internal function: cancels any pending input requests on the window and presents a warning if not INPUT_REQUEST_NONE ***/
942 cancel_old_input_request(winid_t win)
944 switch(win->input_request_type) {
945 case INPUT_REQUEST_NONE:
946 break; /* All is well */
947 case INPUT_REQUEST_CHARACTER:
948 case INPUT_REQUEST_CHARACTER_UNICODE:
949 glk_cancel_char_event(win);
950 WARNING("Cancelling pending char event");
952 case INPUT_REQUEST_LINE:
953 case INPUT_REQUEST_LINE_UNICODE:
954 glk_cancel_line_event(win, NULL);
955 WARNING("Cancelling pending line event");
958 WARNING("Could not cancel pending input request: unknown input request");