3 /** glk_request_char_event:
4 * @win: A window to request char events from.
6 * Request input of a Latin-1 character or special key. A window cannot have
7 * requests for both character and line input at the same time. Nor can it have
8 * requests for character input of both types (Latin-1 and Unicode). It is
9 * illegal to call glk_request_char_event() if the window already has a pending
10 * request for either character or line input.
13 glk_request_char_event(winid_t win)
15 g_return_if_fail(win);
16 g_return_if_fail(win->input_request_type == INPUT_REQUEST_NONE);
17 g_return_if_fail(win->type != wintype_TextBuffer || win->type != wintype_TextGrid);
19 win->input_request_type = INPUT_REQUEST_CHARACTER;
20 g_signal_handler_unblock( G_OBJECT(win->widget), win->keypress_handler );
23 /** glk_request_char_event_uni:
24 * @win: A window to request char events from.
26 * Request input of a Unicode character or special key. See
27 * glk_request_char_event().
30 glk_request_char_event_uni(winid_t win)
32 g_return_if_fail(win);
33 g_return_if_fail(win->input_request_type == INPUT_REQUEST_NONE);
34 g_return_if_fail(win->type != wintype_TextBuffer || win->type != wintype_TextGrid);
36 win->input_request_type = INPUT_REQUEST_CHARACTER_UNICODE;
37 g_signal_handler_unblock( G_OBJECT(win->widget), win->keypress_handler );
40 /* Internal function: Request either latin-1 or unicode line input. */
42 request_line_event_common(winid_t win, gboolean insert, gchar *inserttext)
44 GtkTextBuffer *window_buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(win->widget));
46 /* Move the input_position mark to the end of the window_buffer */
47 GtkTextMark *input_position = gtk_text_buffer_get_mark(window_buffer, "input_position");
49 gtk_text_buffer_get_end_iter(window_buffer, &end_iter);
50 gtk_text_buffer_move_mark(window_buffer, input_position, &end_iter);
52 /* Set the entire contents of the window_buffer as uneditable
53 * (so input can only be entered at the end) */
54 GtkTextIter start_iter;
55 gtk_text_buffer_get_start_iter(window_buffer, &start_iter);
56 gtk_text_buffer_remove_tag_by_name(window_buffer, "uneditable", &start_iter, &end_iter);
57 gtk_text_buffer_apply_tag_by_name(window_buffer, "uneditable", &start_iter, &end_iter);
59 /* Insert pre-entered text if needed */
61 gtk_text_buffer_insert(window_buffer, &end_iter, inserttext, -1);
63 /* Scroll to input point */
64 gtk_text_view_scroll_mark_onscreen(GTK_TEXT_VIEW(win->widget), input_position);
65 g_signal_handler_unblock( G_OBJECT(window_buffer), win->insert_text_handler );
66 gtk_text_view_set_editable(GTK_TEXT_VIEW(win->widget), TRUE);
70 * glk_request_line_event:
71 * @win: A text buffer or text grid window to request line input on.
72 * @buf: A buffer of at least @maxlen bytes.
73 * @maxlen: Length of the buffer.
74 * @initlen: The number of characters in @buf to pre-enter.
76 * Requests input of a line of Latin-1 characters. A window cannot have requests
77 * for both character and line input at the same time. Nor can it have requests
78 * for line input of both types (Latin-1 and Unicode). It is illegal to call
79 * glk_request_line_event() if the window already has a pending request for
80 * either character or line input.
82 * The @buf argument is a pointer to space where the line input will be stored.
83 * (This may not be %NULL.) @maxlen is the length of this space, in bytes; the
84 * library will not accept more characters than this. If @initlen is nonzero,
85 * then the first @initlen bytes of @buf will be entered as pre-existing input
86 * -- just as if the player had typed them himself. (The player can continue
87 * composing after this pre-entered input, or delete it or edit as usual.)
89 * The contents of the buffer are undefined until the input is completed (either
90 * by a line input event, or glk_cancel_line_event(). The library may or may not
91 * fill in the buffer as the player composes, while the input is still pending;
92 * it is illegal to change the contents of the buffer yourself.
95 glk_request_line_event(winid_t win, char* buf, glui32 maxlen, glui32 initlen)
97 g_return_if_fail(win);
98 g_return_if_fail(buf);
99 g_return_if_fail(win->input_request_type == INPUT_REQUEST_NONE);
100 g_return_if_fail(win->type != wintype_TextBuffer || win->type != wintype_TextGrid);
102 win->input_request_type = INPUT_REQUEST_LINE;
103 win->line_input_buffer = buf;
104 win->line_input_buffer_max_len = maxlen;
106 gchar *inserttext = (initlen > 0)? g_strndup(buf, initlen) : g_strdup("");
107 request_line_event_common(win, (initlen > 0), inserttext);
112 * glk_request_line_event_uni:
113 * @win: A text buffer or text grid window to request line input on.
114 * @buf: A buffer of at least @maxlen characters.
115 * @maxlen: Length of the buffer.
116 * @initlen: The number of characters in @buf to pre-enter.
118 * Request input of a line of Unicode characters. This works the same as
119 * glk_request_line_event(), except the result is stored in an array of
120 * <type>glui32</type> values instead of an array of characters, and the values
121 * may be any valid Unicode code points.
123 * The result will be in Unicode Normalization Form C. This basically means that
124 * composite characters will be single characters where possible, instead of
125 * sequences of base and combining marks. See
126 * <ulink url="http://www.unicode.org/reports/tr15/">Unicode Standard Annex #15
127 * </ulink> for the details.
130 glk_request_line_event_uni(winid_t win, glui32 *buf, glui32 maxlen, glui32 initlen)
132 g_return_if_fail(win);
133 g_return_if_fail(buf);
134 g_return_if_fail(win->input_request_type == INPUT_REQUEST_NONE);
135 g_return_if_fail(win->type != wintype_TextBuffer || win->type != wintype_TextGrid);
137 win->input_request_type = INPUT_REQUEST_LINE_UNICODE;
138 win->line_input_buffer_unicode = buf;
139 win->line_input_buffer_max_len = maxlen;
143 GError *error = NULL;
144 utf8 = g_ucs4_to_utf8(buf, initlen, NULL, NULL, &error);
148 g_warning("Error during unicode->utf8 conversion: %s", error->message);
155 request_line_event_common(win, (initlen > 0), utf8);
159 /* Internal function: Callback for signal key-press-event on a text buffer
162 on_window_key_press_event(GtkWidget *widget, GdkEventKey *event, winid_t win)
164 if(win->input_request_type != INPUT_REQUEST_CHARACTER &&
165 win->input_request_type != INPUT_REQUEST_CHARACTER_UNICODE)
170 switch(event->keyval) {
172 case GDK_KP_Up: keycode = keycode_Up; break;
174 case GDK_KP_Down: keycode = keycode_Down; break;
176 case GDK_KP_Left: keycode = keycode_Left; break;
178 case GDK_KP_Right: keycode = keycode_Right; break;
181 case GDK_KP_Enter: keycode = keycode_Return; break;
184 case GDK_KP_Delete: keycode = keycode_Delete; break;
185 case GDK_Escape: keycode = keycode_Escape; break;
187 case GDK_KP_Tab: keycode = keycode_Tab; break;
189 case GDK_KP_Page_Up: keycode = keycode_PageUp; break;
191 case GDK_KP_Page_Down: keycode = keycode_PageDown; break;
193 case GDK_KP_Home: keycode = keycode_Home; break;
195 case GDK_KP_End: keycode = keycode_End; break;
197 case GDK_KP_F1: keycode = keycode_Func1; break;
199 case GDK_KP_F2: keycode = keycode_Func2; break;
201 case GDK_KP_F3: keycode = keycode_Func3; break;
203 case GDK_KP_F4: keycode = keycode_Func4; break;
204 case GDK_F5: keycode = keycode_Func5; break;
205 case GDK_F6: keycode = keycode_Func6; break;
206 case GDK_F7: keycode = keycode_Func7; break;
207 case GDK_F8: keycode = keycode_Func8; break;
208 case GDK_F9: keycode = keycode_Func9; break;
209 case GDK_F10: keycode = keycode_Func10; break;
210 case GDK_F11: keycode = keycode_Func11; break;
211 case GDK_F12: keycode = keycode_Func12; break;
213 keycode = gdk_keyval_to_unicode(event->keyval);
214 /* If keycode is 0, then keyval was not recognized; also return
215 unknown if Latin-1 input was requested and the character is not in
217 if(keycode == 0 || (win->input_request_type == INPUT_REQUEST_CHARACTER && keycode > 255))
218 keycode = keycode_Unknown;
221 event_throw(evtype_CharInput, win, keycode, 0);
223 /* Only one keypress will be handled */
224 win->input_request_type = INPUT_REQUEST_NONE;
225 g_signal_handler_block( G_OBJECT(win->widget), win->keypress_handler );
230 /* Internal function: Callback for signal insert-text on a text buffer window.
231 Not used; cannot modify the text buffer from within the callback?? Segfault! */
233 on_window_insert_text(GtkTextBuffer *textbuffer, GtkTextIter *location, gchar *text, gint len, gpointer user_data)
235 gchar *newline_pos = strchr(text, '\n');
236 if(newline_pos != NULL) {
237 printf("position: %d\n", newline_pos-text);
242 /* Internal function: Callback for signal insert-text on a text buffer window.
243 Runs after the default handler has already inserted the text.*/
245 after_window_insert_text(GtkTextBuffer *textbuffer, GtkTextIter *location, gchar *text, gint len, winid_t win)
247 if( strchr(text, '\n') != NULL )
249 /* Make the window uneditable again and remove signal handlers */
250 GtkTextBuffer *window_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
251 gtk_text_view_set_editable(GTK_TEXT_VIEW(win->widget), FALSE);
252 g_signal_handler_block(window_buffer, win->insert_text_handler);
254 /* Retrieve the text that was input */
255 GtkTextMark *input_position = gtk_text_buffer_get_mark(window_buffer, "input_position");
256 GtkTextIter start_iter;
257 GtkTextIter end_iter;
258 gtk_text_buffer_get_iter_at_mark(window_buffer, &start_iter, input_position);
259 gtk_text_buffer_get_end_iter(window_buffer, &end_iter);
260 gchar *inserted_text = gtk_text_buffer_get_text(window_buffer, &start_iter, &end_iter, FALSE);
262 /* Convert the string from UTF-8 to Latin-1 or Unicode */
263 if(win->input_request_type == INPUT_REQUEST_LINE)
265 GError *error = NULL;
268 latin1 = g_convert_with_fallback(inserted_text, -1, "ISO-8859-1", "UTF-8", "?", NULL, &bytes_written, &error);
269 g_free(inserted_text);
273 g_warning("Error during utf8->latin1 conversion: %s", error->message);
274 event_throw(evtype_LineInput, win, 0, 0);
278 /* Place input in the echo stream */
279 if(win->echo_stream != NULL)
280 glk_put_string_stream(win->echo_stream, latin1);
282 /* Copy the string (but not the NULL at the end) */
283 int copycount = MIN(win->line_input_buffer_max_len, bytes_written - 1);
284 memcpy(win->line_input_buffer, latin1, copycount);
286 event_throw(evtype_LineInput, win, copycount, 0);
288 else if(win->input_request_type == INPUT_REQUEST_LINE_UNICODE)
292 unicode = g_utf8_to_ucs4_fast(inserted_text, -1, &items_written);
293 g_free(inserted_text);
297 g_warning("Error during utf8->unicode conversion");
298 event_throw(evtype_LineInput, win, 0, 0);
302 /* Place input in the echo stream */
304 if(win->echo_stream != NULL)
305 glk_put_string_stream_uni(window->echo_stream, unicode);*/
307 /* Copy the string (but not the NULL at the end) */
308 int copycount = MIN(win->line_input_buffer_max_len, items_written);
309 memcpy(win->line_input_buffer_unicode, unicode, copycount * sizeof(gunichar));
311 event_throw(evtype_LineInput, win, copycount, 0);
314 g_warning("%s: Wrong input request type.", __func__);
316 win->input_request_type = INPUT_REQUEST_NONE;