Input history for text buffers and text grids.
authorfliep <fliep@ddfedd41-794f-dd11-ae45-00112f111e67>
Sun, 15 Nov 2009 16:29:27 +0000 (16:29 +0000)
committerfliep <fliep@ddfedd41-794f-dd11-ae45-00112f111e67>
Sun, 15 Nov 2009 16:29:27 +0000 (16:29 +0000)
libchimara/input.c
libchimara/input.h
libchimara/window.c
libchimara/window.h

index c35564593ec0a5d6990cb5d34901cb422f0a1ea7..605616d0e38c9eb94473ab292c592a984bc1af8c 100644 (file)
@@ -153,6 +153,8 @@ text_grid_request_line_event_common(winid_t win, glui32 maxlen, gboolean insert,
     gtk_widget_modify_base(win->input_entry, GTK_STATE_NORMAL, &background);
     
     g_signal_connect(win->input_entry, "activate", G_CALLBACK(on_input_entry_activate), win);
+    g_signal_connect(win->input_entry, "key-press-event", G_CALLBACK(on_input_entry_key_press_event), win);
+    win->line_input_entry_changed = g_signal_connect(win->input_entry, "changed", G_CALLBACK(on_input_entry_changed), win);
     
     gtk_widget_show(win->input_entry);
     gtk_text_view_add_child_at_anchor(GTK_TEXT_VIEW(win->widget), win->input_entry, win->input_anchor);
@@ -610,8 +612,18 @@ finish_text_grid_line_input(winid_t win, gboolean emit_signal)
                g_assert(glk);
                g_signal_emit_by_name(glk, "line-input", win->rock, text);
     }
+       
+       /* Add the text to the window input history */
+       if(win->history_pos != NULL)
+       {
+               g_free(win->history->data);
+               win->history = g_list_delete_link(win->history, win->history);
+       }
+       if(*text != 0)
+               win->history = g_list_prepend(win->history, g_strdup(text));    
+       win->history_pos = NULL;
+       
        g_free(text);
-
        return chars_written;
 }
 
@@ -657,6 +669,60 @@ on_input_entry_activate(GtkEntry *input_entry, winid_t win)
        event_throw(glk, evtype_LineInput, win, chars_written, 0);
 }
 
+/* Internal function: Callback for signal key-press-event on the line input 
+GtkEntry in a text grid window. */
+gboolean
+on_input_entry_key_press_event(GtkEntry *input_entry, GdkEventKey *event, winid_t win)
+{
+       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( (event->keyval == GDK_Up || event->keyval == GDK_KP_Up)
+                       && win->history_pos && win->history_pos->next == NULL)
+                       return TRUE;
+               if( (event->keyval == GDK_Down || event->keyval == GDK_KP_Down)
+                       && (win->history_pos == NULL || win->history_pos->prev == NULL) )
+                       return TRUE;
+       
+               if(win->history_pos == NULL) 
+               {
+                       gchar *current_input = gtk_entry_get_text(input_entry);
+                       win->history = g_list_prepend(win->history, current_input);
+                       win->history_pos = win->history;
+               }
+
+               if(event->keyval == GDK_Up || event->keyval == GDK_KP_Up)
+               {
+                       if(win->history_pos)
+                               win->history_pos = g_list_next(win->history_pos);
+                       else
+                               win->history_pos = win->history;
+               }
+               else /* down */
+                       win->history_pos = g_list_previous(win->history_pos);
+
+               /* Insert the history item into the window */
+               g_signal_handler_block(input_entry, win->line_input_entry_changed);
+               gtk_entry_set_text(input_entry, win->history_pos->data);
+               g_signal_handler_unblock(input_entry, win->line_input_entry_changed);
+               return TRUE;
+       }
+       return FALSE;
+}
+
+void
+on_input_entry_changed(GtkEditable *editable, winid_t win)
+{
+       /* Set the history position to NULL and erase the text we were already editing */
+       if(win->history_pos != NULL)
+       {
+               g_free(win->history->data);
+               win->history = g_list_delete_link(win->history, win->history);
+               win->history_pos = NULL;
+       }
+}
+
 glui32
 keyval_to_glk_keycode(guint keyval, gboolean unicode)
 {
index bf98dc3c7fe8f098b660f3b6c48787a49c3c5636..fcba5a088e56775453dd264f1f7bed0e1c15c24e 100644 (file)
@@ -14,6 +14,8 @@ G_GNUC_INTERNAL gboolean on_char_input_key_press_event(GtkWidget *widget, GdkEve
 G_GNUC_INTERNAL gboolean on_line_input_key_press_event(GtkWidget *widget, GdkEventKey *event, winid_t win);
 G_GNUC_INTERNAL void after_window_insert_text(GtkTextBuffer *textbuffer, GtkTextIter *location, gchar *text, gint len, winid_t win);
 G_GNUC_INTERNAL void on_input_entry_activate(GtkEntry *input_entry, winid_t win);
+G_GNUC_INTERNAL gboolean on_input_entry_key_press_event(GtkEntry *input_entry, GdkEventKey *event, winid_t win);
+G_GNUC_INTERNAL void on_input_entry_changed(GtkEditable *editable, winid_t win);
 G_GNUC_INTERNAL glui32 keyval_to_glk_keycode(guint keyval, gboolean unicode);
 G_GNUC_INTERNAL void force_char_input_from_queue(winid_t win, event_t *event);
 G_GNUC_INTERNAL void force_line_input_from_queue(winid_t win, event_t *event);
index cbfc9862e8911f37f2e2c27ca2f3dfb4adafc7e3..d78853d5ceda77510ba99f0cb4d016679f1489ce 100644 (file)
@@ -489,11 +489,11 @@ glk_window_open(winid_t split, glui32 method, glui32 size, glui32 wintype,
                        
                        /* Connect signal handlers */
                        win->char_input_keypress_handler = g_signal_connect( G_OBJECT(textview), "key-press-event", G_CALLBACK(on_char_input_key_press_event), win );
-                       g_signal_handler_block( G_OBJECT(textview), win->char_input_keypress_handler );
+                       g_signal_handler_block(textview, win->char_input_keypress_handler);
 
                        gtk_widget_add_events( GTK_WIDGET(textview), GDK_BUTTON_RELEASE_MASK );
                        win->mouse_click_handler = g_signal_connect_after( G_OBJECT(textview), "button-release-event", G_CALLBACK(on_window_button_release_event), win );
-                       g_signal_handler_block( G_OBJECT(textview), win->mouse_click_handler );
+                       g_signal_handler_block( textview, win->mouse_click_handler );
 
                        /* Create the styles available to the window stream */
                        style_init_textgrid(textbuffer);
@@ -536,11 +536,10 @@ glk_window_open(winid_t split, glui32 method, glui32 size, glui32 wintype,
                        
                        gtk_widget_add_events( GTK_WIDGET(textview), GDK_BUTTON_RELEASE_MASK );
                        win->mouse_click_handler = g_signal_connect_after( G_OBJECT(textview), "button-release-event", G_CALLBACK(on_window_button_release_event), win );
-                       g_signal_handler_block( G_OBJECT(textview), win->mouse_click_handler );
+                       g_signal_handler_block( textview, win->mouse_click_handler );
 
                        win->insert_text_handler = g_signal_connect_after( G_OBJECT(textbuffer), "insert-text", G_CALLBACK(after_window_insert_text), win );
-                       g_signal_handler_block( G_OBJECT(textbuffer), win->insert_text_handler );
-
+                       g_signal_handler_block( textbuffer, win->insert_text_handler );
 
                        /* Create an editable tag to indicate uneditable parts of the window
                        (for line input) */
index 544d0cec62129350e8af65aefbd5d770ff9bdd2b..853f896ab729f58bffbda25ee4b036e598918ca7 100644 (file)
@@ -65,6 +65,7 @@ struct glk_window_struct
        glui32 input_length;
        GtkTextChildAnchor *input_anchor;
        GtkWidget *input_entry;
+       gulong line_input_entry_changed;
        /* Signal handlers */
        gulong char_input_keypress_handler;
        gulong line_input_keypress_handler;