Implement line input echoing
authorP. F. Chimento <philip.chimento@gmail.com>
Wed, 20 Apr 2011 15:22:38 +0000 (17:22 +0200)
committerP. F. Chimento <philip.chimento@gmail.com>
Wed, 20 Apr 2011 15:22:38 +0000 (17:22 +0200)
libchimara/gestalt.c
libchimara/input.c
libchimara/window.c
libchimara/window.h

index 13193fcc6c485742903595e89038c9ea23a98980..22683df91d7a8d0237a5aa2c8d43434a52f80cec 100644 (file)
@@ -116,6 +116,7 @@ glk_gestalt_ext(glui32 sel, glui32 val, glui32 *arr, glui32 arrlen)
                case gestalt_GraphicsTransparency:
                case gestalt_DateTime:
                case gestalt_UnicodeNorm:
+               case gestalt_LineInputEcho:
                        return 1;
 
                /* Capabilities supported if compiled with GStreamer */
@@ -130,7 +131,6 @@ glk_gestalt_ext(glui32 sel, glui32 val, glui32 *arr, glui32 arrlen)
 #endif
                        
                /* Unsupported capabilities */
-               case gestalt_LineInputEcho:
                case gestalt_LineTerminatorKey:
                case gestalt_LineTerminators:
                /* Selector not supported */    
index 7f8038c38793de0242dfa47577f1d98894361f2a..ff3cfb8b7b9eb09db3e3bff7fae2c4f26adbe180 100644 (file)
@@ -624,21 +624,23 @@ finish_text_buffer_line_input(winid_t win, gboolean emit_signal)
        VALID_WINDOW(win, return 0);
        g_return_val_if_fail(win->type == wintype_TextBuffer, 0);
 
-       GtkTextIter start_iter, end_iter, last_character;
+       GtkTextIter start_iter, end_iter;
 
        GtkTextBuffer *window_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
        GtkTextMark *input_position = gtk_text_buffer_get_mark(window_buffer, "input_position");
        gtk_text_buffer_get_iter_at_mark(window_buffer, &start_iter, input_position);
        gtk_text_buffer_get_end_iter(window_buffer, &end_iter);
-       gtk_text_buffer_get_end_iter(window_buffer, &last_character);
-       gtk_text_iter_backward_cursor_position(&last_character);
 
-       gchar* last_char = gtk_text_buffer_get_text(window_buffer, &last_character, &end_iter, FALSE);
+       gchar *inserted_text = gtk_text_buffer_get_text(window_buffer, &start_iter, &end_iter, FALSE);
 
-       if( strchr(last_char, '\n') != NULL )
-               gtk_text_iter_backward_cursor_position(&end_iter);
+       /* If echoing is turned off, remove the text from the window */
+       if(!win->echo_line_input)
+               gtk_text_buffer_delete(window_buffer, &start_iter, &end_iter);
 
-       gchar* inserted_text = gtk_text_buffer_get_text(window_buffer, &start_iter, &end_iter, FALSE);
+       /* Don't include the newline in the input */
+       char *last_char = inserted_text + strlen(inserted_text) - 1;
+       if(*last_char == '\n')
+               *last_char = '\0';
 
        int chars_written = write_to_window_buffer(win, inserted_text);
        if(emit_signal)
@@ -927,9 +929,13 @@ force_line_input_from_queue(winid_t win, event_t *event)
                gtk_text_view_set_editable(GTK_TEXT_VIEW(win->widget), FALSE);
 
                /* 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, "default", "input", NULL);
+               if(win->echo_line_input)
+               {
+                       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, "default", "input", NULL);
+               }
+
                chars_written = finish_text_buffer_line_input(win, TRUE);
        }
        else if(win->type == wintype_TextGrid)
@@ -1008,6 +1014,11 @@ void
 glk_set_echo_line_event(winid_t win, glui32 val)
 {
        VALID_WINDOW(win, return);
+
+       if(win->type != wintype_TextBuffer)
+               return; /* Quietly do nothing */
+
+       win->echo_line_input = val? TRUE : FALSE;
 }
 
 /**
index c0b0a0c84443efe07cfaf557ddaa634424bd950a..25c06bc89b2f35d0547cc1c0f43d26c3358df88f 100644 (file)
@@ -33,6 +33,7 @@ window_new_common(glui32 rock)
        win->line_input_buffer = NULL;
        win->line_input_buffer_unicode = NULL;
        win->history = NULL;
+       win->echo_line_input = TRUE;
 
        /* Initialise the buffer */
        win->buffer = g_string_sized_new(1024);
index 1fecb5e92944339c5afa243fc447c3093284fddc..8cd6e7d693c35499c4f57f30eb5fd81e3ba40c2a 100644 (file)
@@ -64,6 +64,7 @@ struct glk_window_struct
        gboolean mouse_input_requested;
        GList *history;
        GList *history_pos;
+       gboolean echo_line_input;
        /* Line input field (text grids only) */
        glui32 input_length;
        GtkTextChildAnchor *input_anchor;