+/* 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)
+ {
+ const gchar *current_input = gtk_entry_get_text(input_entry);
+ win->history = g_list_prepend(win->history, g_strdup(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;
+ }
+}
+