Updated documentation to match API 0.7.2
[projects/chimara/chimara.git] / libchimara / input.c
index 9e7eea0cf533242cc7ccbc15f0ed82beefe9385c..7f8038c38793de0242dfa47577f1d98894361f2a 100644 (file)
@@ -1,6 +1,7 @@
 #include "charset.h"
 #include "magic.h"
 #include "input.h"
+#include "pager.h"
 #include "chimara-glk-private.h"
 
 extern GPrivate *glk_data_key;
@@ -8,15 +9,17 @@ extern GPrivate *glk_data_key;
 /* Forward declarations */
 static int finish_text_buffer_line_input(winid_t win, gboolean emit_signal);
 static int finish_text_grid_line_input(winid_t win, gboolean emit_signal);
+static void cancel_old_input_request(winid_t win);
 
 /* Internal function: code common to both flavors of char event request */
 void
 request_char_event_common(winid_t win, gboolean unicode)
 {
        VALID_WINDOW(win, return);
-       g_return_if_fail(win->input_request_type == INPUT_REQUEST_NONE);
        g_return_if_fail(win->type != wintype_TextBuffer || win->type != wintype_TextGrid);
 
+       cancel_old_input_request(win);
+
        flush_window_buffer(win);
 
        ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
@@ -25,20 +28,6 @@ request_char_event_common(winid_t win, gboolean unicode)
        g_signal_handler_unblock( win->widget, win->char_input_keypress_handler );
 
        gdk_threads_enter();
-
-       /* If the request is in a text buffer window, scroll to the end of the
-        text buffer. TODO: This may scroll text off the top of the window that the
-        user hasn't read yet. We need to implement a paging mechanism. */
-       if(win->type == wintype_TextBuffer)
-       {
-               GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
-               GtkTextIter iter;
-               gtk_text_buffer_get_end_iter(buffer, &iter);
-               gtk_text_buffer_place_cursor(buffer, &iter);
-               gtk_text_view_scroll_mark_onscreen(GTK_TEXT_VIEW(win->widget), gtk_text_buffer_get_insert(buffer));
-               /* Why doesn't this always work?? */
-       }
-
        gtk_widget_grab_focus( GTK_WIDGET(win->widget) );
        gdk_threads_leave();
 
@@ -139,7 +128,11 @@ text_grid_request_line_event_common(winid_t win, glui32 maxlen, gboolean insert,
        /* Make the entry as small as possible to fit with the text */
        gtk_entry_set_has_frame(GTK_ENTRY(win->input_entry), FALSE);
        GtkBorder border = { 0, 0, 0, 0 };
+
+       /* COMPAT: */
+#if GTK_CHECK_VERSION(2,10,0)
        gtk_entry_set_inner_border(GTK_ENTRY(win->input_entry), &border);
+#endif
     gtk_entry_set_max_length(GTK_ENTRY(win->input_entry), win->input_length);
     gtk_entry_set_width_chars(GTK_ENTRY(win->input_entry), win->input_length);
 
@@ -193,9 +186,6 @@ text_buffer_request_line_event_common(winid_t win, glui32 maxlen, gboolean inser
                gtk_text_buffer_get_end_iter(buffer, &end_iter); /* update after text insertion */
        }
 
-    /* Scroll to input point */
-    gtk_text_view_scroll_mark_onscreen(GTK_TEXT_VIEW(win->widget), input_position);
-
        /* Apply the correct style to the input prompt */
        GtkTextIter input_iter;
     gtk_text_buffer_get_iter_at_mark(buffer, &input_iter, input_position);
@@ -239,10 +229,11 @@ glk_request_line_event(winid_t win, char *buf, glui32 maxlen, glui32 initlen)
 {
        VALID_WINDOW(win, return);
        g_return_if_fail(buf);
-       g_return_if_fail(win->input_request_type == INPUT_REQUEST_NONE);
        g_return_if_fail(win->type != wintype_TextBuffer || win->type != wintype_TextGrid);
        g_return_if_fail(initlen <= maxlen);
 
+       cancel_old_input_request(win);
+
        ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
 
        /* Register the buffer */
@@ -282,27 +273,46 @@ glk_request_line_event(winid_t win, char *buf, glui32 maxlen, glui32 initlen)
  * <type>glui32</type> values instead of an array of characters, and the values
  * may be any valid Unicode code points.
  *
- * The result will be in Unicode Normalization Form C. This basically means that
- * composite characters will be single characters where possible, instead of
- * sequences of base and combining marks. See
- * <ulink url="http://www.unicode.org/reports/tr15/">Unicode Standard Annex
- * #15</ulink> for the details.
+ * If possible, the library should return fully composed Unicode characters,
+ * rather than strings of base and composition characters.
+ *
+ * <note><para>
+ *   Fully-composed characters are the norm for Unicode text, so an
+ *   implementation that ignores this issue will probably produce the right
+ *   result. However, the game may not want to rely on that. Another factor is
+ *   that case-folding can (occasionally) produce non-normalized text.
+ *   Therefore, to cover all its bases, a game should call
+ *   glk_buffer_to_lower_case_uni(), followed by
+ *   glk_buffer_canon_normalize_uni(), before parsing.
+ * </para></note>
+ *
+ * <note><para>
+ *   Earlier versions of this spec said that line input must always be in
+ *   Unicode Normalization Form C. However, this has not been universally
+ *   implemented. It is also somewhat redundant, for the results noted above.
+ *   Therefore, we now merely recommend that line input be fully composed. The
+ *   game is ultimately responsible for all case-folding and normalization. See
+ *   <link linkend="chimara-Unicode-String-Normalization">Unicode String
+ *   Normalization</link>.
+ * </para></note>
  */
 void
 glk_request_line_event_uni(winid_t win, glui32 *buf, glui32 maxlen, glui32 initlen)
 {
        VALID_WINDOW(win, return);
        g_return_if_fail(buf);
-       g_return_if_fail(win->input_request_type == INPUT_REQUEST_NONE);
        g_return_if_fail(win->type != wintype_TextBuffer || win->type != wintype_TextGrid);
        g_return_if_fail(initlen <= maxlen);
 
+       cancel_old_input_request(win);
        ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
 
        /* Register the buffer */
        if(glk_data->register_arr)
         win->buffer_rock = (*glk_data->register_arr)(buf, maxlen, "&+#!Iu");
 
+
+
        win->input_request_type = INPUT_REQUEST_LINE_UNICODE;
        win->line_input_buffer_unicode = buf;
        win->line_input_buffer_max_len = maxlen;
@@ -368,6 +378,7 @@ glk_cancel_line_event(winid_t win, event_t *event)
 
        int chars_written = 0;
 
+       gdk_threads_enter();
        if(win->type == wintype_TextGrid) {
                chars_written = finish_text_grid_line_input(win, FALSE);
        } else if(win->type == wintype_TextBuffer) {
@@ -376,6 +387,7 @@ glk_cancel_line_event(winid_t win, event_t *event)
                g_signal_handler_block(window_buffer, win->insert_text_handler);
                chars_written = finish_text_buffer_line_input(win, FALSE);
        }
+       gdk_threads_leave();
 
        ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
        if(glk_data->unregister_arr)
@@ -425,6 +437,15 @@ on_shutdown_key_press_event(GtkWidget *widget, GdkEventKey *event, winid_t win)
 gboolean
 on_char_input_key_press_event(GtkWidget *widget, GdkEventKey *event, winid_t win)
 {
+       /* Ignore modifier keys, otherwise the char input will already trigger on 
+       the shift key when the user tries to type a capital letter */
+       if(event->is_modifier)
+               return FALSE; /* don't stop the event */
+
+       /* All text up to the input position is now regarded as being read by the user */
+       if(win->type == wintype_TextBuffer)
+               pager_update(win);
+       
        glui32 keycode = keyval_to_glk_keycode(event->keyval, win->input_request_type == INPUT_REQUEST_CHARACTER_UNICODE);
 
        ChimaraGlk *glk = CHIMARA_GLK(gtk_widget_get_ancestor(widget, CHIMARA_TYPE_GLK));
@@ -434,7 +455,7 @@ on_char_input_key_press_event(GtkWidget *widget, GdkEventKey *event, winid_t win
 
        /* Only one keypress will be handled */
        win->input_request_type = INPUT_REQUEST_NONE;
-       g_signal_handler_block( win->widget, win->char_input_keypress_handler );
+       g_signal_handler_block(win->widget, win->char_input_keypress_handler);
 
        return TRUE;
 }
@@ -445,10 +466,16 @@ on_line_input_key_press_event(GtkWidget *widget, GdkEventKey *event, winid_t win
        switch(win->type)
        {
                case wintype_TextBuffer:
+                       /* All text up to the input position is now regarded as being read by the user */
+                       pager_update(win);
+
+                       /* History up/down */
                        if(event->keyval == GDK_Up || event->keyval == GDK_KP_Up
                                || event->keyval == GDK_Down || event->keyval == GDK_KP_Down)
                        {
                                /* Prevent falling off the end of the history list */
+                               if(win->history == NULL)
+                                       return TRUE;
                                if( (event->keyval == GDK_Up || event->keyval == GDK_KP_Up)
                                        && win->history_pos && win->history_pos->next == NULL)
                                        return TRUE;
@@ -485,10 +512,37 @@ on_line_input_key_press_event(GtkWidget *widget, GdkEventKey *event, winid_t win
                                gtk_text_buffer_get_end_iter(buffer, &end);
 
                                g_signal_handler_block(buffer, win->insert_text_handler);
-                               gtk_text_buffer_insert_with_tags_by_name(buffer, &end, win->history_pos->data, -1, "input", NULL);
+                               gtk_text_buffer_insert_with_tags_by_name(buffer, &end, win->history_pos->data, -1, "default", "input", NULL);
                                g_signal_handler_unblock(buffer, win->insert_text_handler);
                                return TRUE;
                        }
+
+                       /* Move to beginning/end of input field */
+                       else if(event->keyval == GDK_Home) {
+                               GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(win->widget));
+                               GtkTextIter input_iter;
+                               GtkTextMark *input_position = gtk_text_buffer_get_mark(buffer, "input_position");
+                               gtk_text_buffer_get_iter_at_mark(buffer, &input_iter, input_position);
+                               gtk_text_buffer_place_cursor(buffer, &input_iter);
+                               return TRUE;
+                       }
+                       else if(event->keyval == GDK_End) {
+                               GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(win->widget));
+                               GtkTextIter end_iter;
+                               gtk_text_buffer_get_end_iter(buffer, &end_iter);
+                               gtk_text_buffer_place_cursor(buffer, &end_iter);
+                               return TRUE;
+                       }
+
+                       /* Handle the enter key, which could occur in the middle of the sentence. */
+                       else if(event->keyval == GDK_Return || event->keyval == GDK_KP_Enter) {
+                               GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(win->widget));
+                               GtkTextIter end_iter;
+                               gtk_text_buffer_get_end_iter(buffer, &end_iter);
+                               gtk_text_buffer_place_cursor(buffer, &end_iter);
+                               return FALSE; 
+                       }
+
                        return FALSE;
 
                /* If this is a text grid window, then redirect the key press to the line input GtkEntry */
@@ -875,7 +929,7 @@ force_line_input_from_queue(winid_t win, event_t *event)
                /* Insert the forced input into the window */
                gtk_text_buffer_get_end_iter(buffer, &end);
                gchar *text_to_insert = g_strconcat(text, "\n", NULL);
-               gtk_text_buffer_insert_with_tags_by_name(buffer, &end, text_to_insert, -1, "input", NULL);
+               gtk_text_buffer_insert_with_tags_by_name(buffer, &end, text_to_insert, -1, "default", "input", NULL);
                chars_written = finish_text_buffer_line_input(win, TRUE);
        }
        else if(win->type == wintype_TextGrid)
@@ -894,3 +948,99 @@ force_line_input_from_queue(winid_t win, event_t *event)
        event->val1 = chars_written;
        event->val2 = 0;
 }
+
+/*** Internal function: cancels any pending input requests on the window and presents a warning if not INPUT_REQUEST_NONE ***/
+void
+cancel_old_input_request(winid_t win)
+{
+       switch(win->input_request_type) {
+       case INPUT_REQUEST_NONE:
+               break; /* All is well */
+       case INPUT_REQUEST_CHARACTER:
+       case INPUT_REQUEST_CHARACTER_UNICODE:
+               glk_cancel_char_event(win);
+               WARNING("Cancelling pending char event");
+               break;
+       case INPUT_REQUEST_LINE:
+       case INPUT_REQUEST_LINE_UNICODE:
+               glk_cancel_line_event(win, NULL);
+               WARNING("Cancelling pending line event");
+               break;
+       default:
+               WARNING("Could not cancel pending input request: unknown input request");
+       }
+}
+
+/**
+ * glk_set_echo_line_event:
+ * @win: The window in which to change the echoing behavior.
+ * @val: Zero to turn off echoing, nonzero for normal echoing.
+ *
+ * Normally, after line input is completed or cancelled in a buffer window, the
+ * library ensures that the complete input line (or its latest state, after
+ * cancelling) is displayed at the end of the buffer, followed by a newline.
+ * This call allows you to suppress this behavior. If the @val argument is zero,
+ * all <emphasis>subsequent</emphasis> line input requests in the given window
+ * will leave the buffer unchanged after the input is completed or cancelled;
+ * the player's input will not be printed. If @val is nonzero, subsequent input
+ * requests will have the normal printing behavior.
+ *
+ * <note><para>
+ *   Note that this feature is unrelated to the window's echo stream.
+ * </para></note>
+ *
+ * If you turn off line input echoing, you can reproduce the standard input
+ * behavior by following each line input event (or line input cancellation) by
+ * printing the input line, in the Input style, followed by a newline in the
+ * original style.
+ *
+ * The glk_set_echo_line_event() does not affect a pending line input request.
+ * It also has no effect in non-buffer windows.
+ * <note><para>
+ *   In a grid window, the game can overwrite the input area at will, so there
+ *   is no need for this distinction.
+ * </para></note>
+ *
+ * Not all libraries support this feature. You can test for it with
+ * %gestalt_LineInputEcho.
+ */
+void
+glk_set_echo_line_event(winid_t win, glui32 val)
+{
+       VALID_WINDOW(win, return);
+}
+
+/**
+ * glk_set_terminators_line_event:
+ * @win: The window for which to set the line input terminator keys.
+ * @keycodes: An array of <code>keycode_</code> constants, of length @count.
+ * @count: The array length of @keycodes.
+ *
+ * It is possible to request that other keystrokes complete line input as well.
+ * (This allows a game to intercept function keys or other special keys during
+ * line input.) To do this, call glk_set_terminators_line_event(), and pass an
+ * array of @count keycodes. These must all be special keycodes (see <link
+ * linkend="chimara-Character-Input">Character Input</link>). Do not include
+ * regular printable characters in the array, nor %keycode_Return (which
+ * represents the default <keycap>enter</keycap> key and will always be
+ * recognized). To return to the default behavior, pass a %NULL or empty array.
+ *
+ * The glk_set_terminators_line_event() affects <emphasis>subsequent</emphasis>
+ * line input requests in the given window. It does not affect a pending line
+ * input request.
+ *
+ * <note><para>
+ *   This distinction makes life easier for interpreters that set up UI
+ *   callbacks only at the start of input.
+ * </para></note>
+ *
+ * A library may not support this feature; if it does, it may not support all
+ * special keys as terminators. (Some keystrokes are reserved for OS or
+ * interpreter control.) You can test for this with %gestalt_LineTerminators and
+ * %gestalt_LineTerminatorKey.
+ */
+void
+glk_set_terminators_line_event(winid_t win, glui32 *keycodes, glui32 count)
+{
+       VALID_WINDOW(win, return);
+}
\ No newline at end of file