Barf still doesn't work
authorfliep <fliep@ddfedd41-794f-dd11-ae45-00112f111e67>
Wed, 23 Dec 2009 07:50:19 +0000 (07:50 +0000)
committerfliep <fliep@ddfedd41-794f-dd11-ae45-00112f111e67>
Wed, 23 Dec 2009 07:50:19 +0000 (07:50 +0000)
tests/barf.c

index 540df348539fd1748d51aa61ad576215645b9eb3..f211210a33ee0904a6f1bf6d6832be3d82c0820b 100644 (file)
@@ -1,5 +1,12 @@
+/*
+ * Compile me with:
+ * gcc -g -O0 -Wall -pthread -o barf barf.c `pkg-config --cflags --libs gtk+-2.0`
+ */
+
 #include <gtk/gtk.h>
 
+static glong pager_handler = 0;
+
 static gboolean
 quit()
 {
@@ -29,15 +36,55 @@ after_insert(GtkTextBuffer *buffer, GtkTextIter *location, gchar *text, gint len
 
        GdkRectangle pagerpos, endpos, visiblerect;
        GtkTextIter pager, end;
-       gtk_text_buffer_get_iter_at_mark(buffer, &pager, gtk_text_buffer_get_mark(buffer, "pager_position"));
        gtk_text_buffer_get_end_iter(buffer, &end);
+       gtk_text_view_get_visible_rect(textview, &visiblerect);
        
+       /* Move the pager to the last visible character in the buffer */
+       gtk_text_view_get_line_at_y(textview, &pager, visiblerect.y, NULL);
+       if(!gtk_text_iter_ends_line(&pager))
+               gtk_text_iter_forward_to_line_end(&pager);
+       gtk_text_buffer_move_mark_by_name(buffer, "pager_position", &pager);
+
        gtk_text_view_get_iter_location(textview, &pager, &pagerpos);
        gtk_text_view_get_iter_location(textview, &end, &endpos);
+       
+       g_printerr("View height = %d\n", visiblerect.height);
+       g_printerr("End - Pager = %d\n", endpos.y - pagerpos.y);
+       
+       /* Wait for a keypress to advance the pager */
+       if(endpos.y > pagerpos.y)
+               g_signal_handler_unblock(textview, pager_handler);
+}
+
+static gboolean
+pager_wait(GtkTextView *textview, GdkEventKey *event, GtkTextBuffer *buffer)
+{
+       GdkRectangle pagerpos, endpos, visiblerect;
+       GtkTextIter pager, end;
+       GtkTextMark *pagermark = gtk_text_buffer_get_mark(buffer, "pager_position");
+       gtk_text_view_scroll_to_mark(textview, pagermark, 0.0, TRUE, 0.0, 0.0);
+
+       while(gtk_events_pending())
+               gtk_main_iteration();
+
+       /* Move the pager to the last visible character in the buffer */
+       gtk_text_view_get_line_at_y(textview, &pager, visiblerect.y, NULL);
+       if(!gtk_text_iter_ends_line(&pager))
+               gtk_text_iter_forward_to_line_end(&pager);
+       gtk_text_buffer_move_mark(buffer, pagermark, &pager);
+
+       gtk_text_buffer_get_end_iter(buffer, &end);
        gtk_text_view_get_visible_rect(textview, &visiblerect);
+       gtk_text_view_get_iter_location(textview, &pager, &pagerpos);
+       gtk_text_view_get_iter_location(textview, &end, &endpos);
        
        g_printerr("View height = %d\n", visiblerect.height);
        g_printerr("End - Pager = %d\n", endpos.y - pagerpos.y);
+
+       if(endpos.y == pagerpos.y)
+               g_signal_handler_block(textview, pager_handler);
+
+       return TRUE; /* block further handlers */
 }
 
 int
@@ -51,6 +98,7 @@ main(int argc, char **argv)
        GtkWidget *scrolledwindow = gtk_scrolled_window_new(NULL, NULL);
        GtkWidget *textview = gtk_text_view_new();
        gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(textview), GTK_WRAP_WORD_CHAR);
+       gtk_text_view_set_editable(GTK_TEXT_VIEW(textview), FALSE);
        GtkWidget *vbox = gtk_vbox_new(FALSE, 6);
        
        gtk_container_add(GTK_CONTAINER(scrolledwindow), textview);
@@ -61,14 +109,16 @@ main(int argc, char **argv)
 
        GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview));
        g_signal_connect_after(buffer, "insert-text", G_CALLBACK(after_insert), textview);
+       pager_handler = g_signal_connect(textview, "key-press-event", G_CALLBACK(pager_wait), buffer);
+       g_signal_handler_block(textview, pager_handler);
        
        GtkTextIter end;
        gtk_text_buffer_get_end_iter(buffer, &end);
-       gtk_text_buffer_create_mark(buffer, "pager_position", &end, TRUE);
+       GtkTextMark *pagermark = gtk_text_buffer_create_mark(buffer, "pager_position", &end, TRUE);
+       gtk_text_mark_set_visible(pagermark, TRUE);
        
        g_signal_connect(window, "delete-event", G_CALLBACK(quit), NULL);
        g_signal_connect(button, "clicked", G_CALLBACK(barf), buffer);
-
        
        gtk_main();
        return 0;