Redone flush_buffer logic, fixes #39
authorMarijn van Vliet <w.m.vanvliet@gmail.com>
Sat, 15 Sep 2012 16:11:55 +0000 (18:11 +0200)
committerMarijn van Vliet <w.m.vanvliet@gmail.com>
Sat, 15 Sep 2012 16:11:55 +0000 (18:11 +0200)
libchimara/strio.c
libchimara/style.c
libchimara/style.h

index d8dda82a00627d273e705ffd8cf3e0ec2ed0fbf0..b6e8f32403604ccfbdb68a2c964e9a1519f8cbd7 100644 (file)
@@ -45,7 +45,7 @@ write_utf8_to_window_buffer(winid_t win, gchar *s)
        // Write to the buffer  
        g_string_append(win->buffer, s);
 }
-       
+
 /* Internal function: flush a window's text buffer to the screen. */
 void
 flush_window_buffer(winid_t win)
@@ -70,42 +70,10 @@ flush_window_buffer(winid_t win)
                gtk_text_buffer_get_end_iter(buffer, &end);
                gint start_offset;
 
-               GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
-
-               GtkTextTag *default_tag = gtk_text_tag_table_lookup(tags, "default");
-               GtkTextTag *style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->style);
-               GtkTextTag *glk_style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->glk_style);
-
                start_offset = gtk_text_iter_get_offset(&end);
                gtk_text_buffer_insert(buffer, &end, win->buffer->str, -1);
                gtk_text_buffer_get_iter_at_offset(buffer, &start, start_offset);
-
-               // Player's style overrides
-               gtk_text_buffer_apply_tag(buffer, style_tag, &start, &end);
-
-               // GLK Program's style overrides
-               gtk_text_buffer_apply_tag(buffer, glk_style_tag, &start, &end);
-
-               // Default style
-               gtk_text_buffer_apply_tag(buffer, default_tag, &start, &end);
-
-               // Link style overrides
-               if(win->window_stream->hyperlink_mode) {
-                       GtkTextTag *link_style_tag = gtk_text_tag_table_lookup(tags, "hyperlink");
-                       GtkTextTag *link_tag = win->current_hyperlink->tag;
-                       gtk_text_buffer_apply_tag(buffer, link_style_tag, &start, &end);
-                       gtk_text_buffer_apply_tag(buffer, link_tag, &start, &end);
-               }
-
-               // GLK Program's style overrides using garglk_set_zcolors()
-               if(win->zcolor != NULL) {
-                       gtk_text_buffer_apply_tag(buffer, win->zcolor, &start, &end);
-               }
-
-               // GLK Program's style overrides using garglk_set_reversevideo()
-               if(win->zcolor_reversed != NULL) {
-                       gtk_text_buffer_apply_tag(buffer, win->zcolor_reversed, &start, &end);
-               }
+               style_apply(win, &start, &end);
 
                ChimaraGlk *glk = CHIMARA_GLK(gtk_widget_get_ancestor(win->widget, CHIMARA_TYPE_GLK));
                g_assert(glk);
@@ -126,92 +94,32 @@ flush_window_buffer(winid_t win)
                gint start_offset;
 
                gtk_text_buffer_get_iter_at_mark(buffer, &insert, cursor);
-               /* Spaces available on this line */
-               gint available_space = win->width - gtk_text_iter_get_line_offset(&insert);
-               
-               GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
 
-               GtkTextTag *default_tag = gtk_text_tag_table_lookup(tags, "default");
-               GtkTextTag *style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->style);
-               GtkTextTag *glk_style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->glk_style);
-               GtkTextTag *link_style_tag = gtk_text_tag_table_lookup(tags, "hyperlink");
-
-               while(chars_left > available_space && !gtk_text_iter_is_end(&insert))
+               while(chars_left > 0 && !gtk_text_iter_is_end(&insert))
                {
+                       /* Spaces available on this line */
+                       gint available_space = win->width - gtk_text_iter_get_line_offset(&insert);
+               
                        GtkTextIter end = insert;
-                       gtk_text_iter_forward_to_line_end(&end);
-                       gtk_text_buffer_delete(buffer, &insert, &end);
-
-                       start_offset = gtk_text_iter_get_offset(&insert);
-                       gtk_text_buffer_insert(buffer, &insert, win->buffer->str + (length - chars_left), available_space);
-                       gtk_text_buffer_get_iter_at_offset(buffer, &start, start_offset);
-
-                       // Default style
-                       gtk_text_buffer_apply_tag(buffer, default_tag, &start, &insert);
-
-                       // Player's style overrides
-                       gtk_text_buffer_apply_tag(buffer, style_tag, &start, &insert);
-
-                       // GLK Program's style overrides
-                       gtk_text_buffer_apply_tag(buffer, glk_style_tag, &start, &insert);
-
-                       // Link style overrides
-                       if(win->window_stream->hyperlink_mode) {
-                               GtkTextTag *link_tag = win->current_hyperlink->tag;
-                               gtk_text_buffer_apply_tag(buffer, link_style_tag, &start, &insert);
-                               gtk_text_buffer_apply_tag(buffer, link_tag, &start, &insert);
-                       }
-
-                       // GLK Program's style overrides using garglk_set_zcolors()
-                       if(win->zcolor != NULL)
-                               gtk_text_buffer_apply_tag(buffer, win->zcolor, &start, &insert);
-
-                       // GLK Program's style overrides using garglk_set_reversevideo()
-                       if(win->zcolor_reversed != NULL) {
-                               gtk_text_buffer_apply_tag(buffer, win->zcolor_reversed, &start, &insert);
-                       }
+                       if(chars_left <= available_space)
+                               gtk_text_iter_forward_chars(&end, chars_left);
+                       else
+                               gtk_text_iter_forward_to_line_end(&end);
 
-                       chars_left -= available_space;
-                       gtk_text_iter_forward_line(&insert);
-                       available_space = win->width;
-               }
-               if(!gtk_text_iter_is_end(&insert))
-               {
-                       GtkTextIter end = insert;
-                       gtk_text_iter_forward_chars(&end, chars_left);
                        gtk_text_buffer_delete(buffer, &insert, &end);
 
                        start_offset = gtk_text_iter_get_offset(&insert);
-                       gtk_text_buffer_insert(buffer, &insert, win->buffer->str + (length - chars_left), -1);
+                       gtk_text_buffer_insert(buffer, &insert, win->buffer->str + (length - chars_left), MIN(chars_left, available_space));
                        gtk_text_buffer_get_iter_at_offset(buffer, &start, start_offset);
+                       style_apply(win, &start, &insert);
+                       
+                       chars_left -= available_space;
 
-                       // Default style
-                       gtk_text_buffer_apply_tag(buffer, default_tag, &start, &insert);
-
-                       // Player's style overrides
-                       gtk_text_buffer_apply_tag(buffer, style_tag, &start, &insert);
-
-                       // GLK Program's style overrides
-                       gtk_text_buffer_apply_tag(buffer, glk_style_tag, &start, &insert);
-
-                       // Link style overrides
-                       if(win->window_stream->hyperlink_mode) {
-                               GtkTextTag *link_tag = win->current_hyperlink->tag;
-                               gtk_text_buffer_apply_tag(buffer, link_style_tag, &start, &insert);
-                               gtk_text_buffer_apply_tag(buffer, link_tag, &start, &insert);
-                       }
-
-                       // GLK Program's style overrides using garglk_set_zcolors()
-                       if(win->zcolor != NULL)
-                               gtk_text_buffer_apply_tag(buffer, win->zcolor, &start, &insert);
-
-                       // GLK Program's style overrides using garglk_set_reversevideo()
-                       if(win->zcolor_reversed != NULL) {
-                               gtk_text_buffer_apply_tag(buffer, win->zcolor_reversed, &start, &insert);
-                       }
+                       if(gtk_text_iter_get_line_offset(&insert) >= win->width)
+                               gtk_text_iter_forward_line(&insert);
                }
-               
-               gtk_text_buffer_move_mark(buffer, cursor, &start);
+
+               gtk_text_buffer_move_mark(buffer, cursor, &insert);
        }
                break;
        }
index f9b231bf40bd4aff4e6a425b096b6578a12f212e..dc0d4bb942acb13d4e73ddbeeca3bac7131e1f4f 100644 (file)
@@ -1272,3 +1272,44 @@ style_stream_colors(strid_t str, GdkColor **foreground, GdkColor **background)
                        g_object_get(str->window->zcolor, "background-gdk", background, NULL);
        }
 }
+
+/* Apply styles to a segment of text in a GtkTextBuffer
+ */
+void
+style_apply(winid_t win, GtkTextIter *start, GtkTextIter *end)
+{
+       GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
+       GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
+
+       GtkTextTag *default_tag = gtk_text_tag_table_lookup(tags, "default");
+       GtkTextTag *style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->style);
+       GtkTextTag *glk_style_tag = gtk_text_tag_table_lookup(tags, win->window_stream->glk_style);
+
+       // Player's style overrides
+       gtk_text_buffer_apply_tag(buffer, style_tag, start, end);
+
+       // GLK Program's style overrides
+       gtk_text_buffer_apply_tag(buffer, glk_style_tag, start, end);
+
+       // Default style
+       gtk_text_buffer_apply_tag(buffer, default_tag, start, end);
+
+       // Link style overrides
+       if(win->window_stream->hyperlink_mode) {
+               GtkTextTag *link_style_tag = gtk_text_tag_table_lookup(tags, "hyperlink");
+               GtkTextTag *link_tag = win->current_hyperlink->tag;
+               gtk_text_buffer_apply_tag(buffer, link_style_tag, start, end);
+               gtk_text_buffer_apply_tag(buffer, link_tag, start, end);
+       }
+
+       // GLK Program's style overrides using garglk_set_zcolors()
+       if(win->zcolor != NULL) {
+               gtk_text_buffer_apply_tag(buffer, win->zcolor, start, end);
+       }
+
+       // GLK Program's style overrides using garglk_set_reversevideo()
+       if(win->zcolor_reversed != NULL) {
+               gtk_text_buffer_apply_tag(buffer, win->zcolor_reversed, start, end);
+       }
+}
+       
index 614047342a4048af82d2c24b98b89005afb29415..ba9939a1f3218eb22bd5aa6949e1a1fdddbbd488 100644 (file)
@@ -22,6 +22,7 @@ G_GNUC_INTERNAL void glkcolor_to_gdkcolor(glui32 val, GdkColor *color);
 G_GNUC_INTERNAL gchar* glkcolor_to_hex(glui32 val);
 G_GNUC_INTERNAL gchar* gdkcolor_to_hex(GdkColor *color);
 G_GNUC_INTERNAL void style_stream_colors(strid_t str, GdkColor **foreground, GdkColor **background);
+G_GNUC_INTERNAL void style_apply(winid_t win, GtkTextIter *start, GtkTextIter *end);
 
 typedef struct StyleSet {
        GHashTable *text_grid;