From 8dec73c013626b82d5a02dd312c88ee071edbdbd Mon Sep 17 00:00:00 2001 From: Philip Chimento Date: Sun, 15 Nov 2009 16:29:27 +0000 Subject: [PATCH] Input history for text buffers and text grids. git-svn-id: http://lassie.dyndns-server.com/svn/gargoyle-gtk@164 ddfedd41-794f-dd11-ae45-00112f111e67 --- libchimara/input.c | 68 ++++++++++++++++++++++++++++++++++++++++++++- libchimara/input.h | 2 ++ libchimara/window.c | 9 +++--- libchimara/window.h | 1 + 4 files changed, 74 insertions(+), 6 deletions(-) diff --git a/libchimara/input.c b/libchimara/input.c index c355645..605616d 100644 --- a/libchimara/input.c +++ b/libchimara/input.c @@ -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) { diff --git a/libchimara/input.h b/libchimara/input.h index bf98dc3..fcba5a0 100644 --- a/libchimara/input.h +++ b/libchimara/input.h @@ -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); diff --git a/libchimara/window.c b/libchimara/window.c index cbfc986..d78853d 100644 --- a/libchimara/window.c +++ b/libchimara/window.c @@ -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) */ diff --git a/libchimara/window.h b/libchimara/window.h index 544d0ce..853f896 100644 --- a/libchimara/window.h +++ b/libchimara/window.h @@ -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; -- 2.30.2