Fixing #15: handling newlines in textgrids
authorMarijn van Vliet <w.m.vanvliet@student.utwente.nl>
Mon, 7 Feb 2011 14:55:37 +0000 (15:55 +0100)
committerMarijn van Vliet <w.m.vanvliet@student.utwente.nl>
Mon, 7 Feb 2011 14:55:37 +0000 (15:55 +0100)
libchimara/strio.c
tests/gridtest.c

index d440832b69f9739c372cfc7794a66d34d87c94d2..ab8443f5f8eb2823576f995bf6cafb6976b7db33 100644 (file)
@@ -220,6 +220,44 @@ write_buffer_to_stream(strid_t str, gchar *buf, glui32 len)
                                        
                            /* Text grid/buffer windows */
                            case wintype_TextGrid:
+                               {
+                               gchar *utf8 = convert_latin1_to_utf8(buf, len);
+                               if(utf8 != NULL) {
+                                               /* Deal with newlines */
+                                               int i;
+                                               gchar *line = utf8;
+                                               for(i=0; i<len; i++) {
+                                                       if(utf8[i] == '\n') {
+                                                               utf8[i] = '\0';
+                                                               write_utf8_to_window_buffer(str->window, line);
+                                                               flush_window_buffer(str->window);
+
+                                                               /* Move cursor position forward to the next line */
+                                                               gdk_threads_enter();
+                                                               GtkTextIter cursor_pos;
+                                                               GtkTextView *textview = GTK_TEXT_VIEW(str->window->widget);
+                                                               GtkTextBuffer *buffer = gtk_text_view_get_buffer(textview);
+                                                               GtkTextMark *cursor_mark = gtk_text_buffer_get_mark(buffer, "cursor_position");
+
+                                                           gtk_text_buffer_get_iter_at_mark( buffer, &cursor_pos, cursor_mark);
+                                                               gtk_text_view_forward_display_line(textview, &cursor_pos);
+                                                               gtk_text_view_backward_display_line_start(textview, &cursor_pos);
+                                                               gtk_text_buffer_move_mark(buffer, cursor_mark, &cursor_pos);
+                                                               gdk_threads_leave();
+
+                                                               line = utf8 + (i < len-1 ? (i+1):(len-1));
+                                                       }
+                                               }
+                                                               
+                                               /* No more newlines left. */
+                                               write_utf8_to_window_buffer(str->window, line);
+                                               g_free(utf8);
+                                       }
+
+                                       str->write_count += len;
+                               }
+                                       break;
+
                                case wintype_TextBuffer:
                            {
                                gchar *utf8 = convert_latin1_to_utf8(buf, len);
index 59804940d8528dffc31e9bbe0dab583c4d638dfd..7f25db161fa7318c477dae01504ef7e1ccb5b8eb 100644 (file)
@@ -3,6 +3,7 @@
 #include <stdlib.h>
 #include <assert.h>
 #include <libchimara/glk.h>
+#include <libchimara/garglk.h>
 
 void glk_main(void)
 {
@@ -19,12 +20,37 @@ void glk_main(void)
        glk_put_string("Window not big enough");
        glk_exit();
     }
-    x = width / 2 - 10;
-    y = height / 2;
-    
+
     char *buffer = calloc(256, sizeof(char));
     assert(buffer);
     
+       garglk_set_reversevideo(1);
+       for(y=0; y<height; y++) {
+               snprintf(buffer, 256, "%02d\n", y);
+               glk_put_string(buffer);
+       }
+       garglk_set_reversevideo(0);
+
+       glk_set_style(style_Emphasized);
+       for(y=0; y<height; y++) {
+               snprintf(buffer, 256, "%02d", y);
+               glk_window_move_cursor(mainwin, (y+1)*width-2, 0);
+               glk_put_string(buffer);
+       }
+       glk_set_style(style_Normal);
+
+    glk_window_move_cursor(mainwin, 2, 0);
+       glk_put_string("+");
+    glk_window_move_cursor(mainwin, width-3, 0);
+       glk_put_string("+");
+    glk_window_move_cursor(mainwin, 2, height-1);
+       glk_put_string("+");
+    glk_window_move_cursor(mainwin, width-3, height-1);
+       glk_put_string("+");
+
+    x = width / 2 - 10;
+    y = height / 2;
+    
     glk_window_move_cursor(mainwin, x, y - 1);
     glk_put_string("Enter text, or 'quit'");
     glk_window_move_cursor(mainwin, x, y);