From 634693fc827c6ef7ce2f324e4950b6d7354c394d Mon Sep 17 00:00:00 2001 From: Philip Chimento Date: Tue, 2 Feb 2010 20:46:19 +0000 Subject: [PATCH] Barf improvements -- the paging functionality should now be ready to move into the main program. Both paging behaviors I mentioned are available for testing - see the #define constant at the top of the file. git-svn-id: http://lassie.dyndns-server.com/svn/gargoyle-gtk@221 ddfedd41-794f-dd11-ae45-00112f111e67 --- tests/barf.c | 114 ++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 86 insertions(+), 28 deletions(-) diff --git a/tests/barf.c b/tests/barf.c index 0bf2a93..36e5151 100644 --- a/tests/barf.c +++ b/tests/barf.c @@ -4,6 +4,10 @@ */ #include +#include + +/* Uncomment to try the other paging method */ +/* #define PAGE_ONLY_UNSEEN 1 */ static gulong pager_handler = 0; static gulong expose_handler = 0; @@ -16,33 +20,49 @@ quit() return TRUE; } +/* Vomit a load of text onto the screen */ static void -barf(GtkButton *button, GtkTextBuffer *buffer) +barf(GtkButton *button, GtkTextView *textview) { GtkTextIter end; + GtkTextBuffer *buffer = gtk_text_view_get_buffer(textview); gtk_text_buffer_get_end_iter(buffer, &end); - gtk_text_buffer_move_mark_by_name(buffer, "pager_position", &end); - gchar *loremipsum; g_file_get_contents("loremipsum.txt", &loremipsum, NULL, NULL); gtk_text_buffer_insert(buffer, &end, loremipsum, -1); g_free(loremipsum); + + gtk_widget_grab_focus(GTK_WIDGET(textview)); } +/* Helper function: move the pager to the last visible position in the buffer, + and return the distance between the pager and the end of the buffer in buffer + coordinates */ static gint move_pager_and_get_scroll_distance(GtkTextView *textview) { GdkRectangle pagerpos, endpos, visiblerect; - GtkTextIter pager, end; + GtkTextIter oldpager, newpager, end; GtkTextBuffer *buffer = gtk_text_view_get_buffer(textview); + GtkTextMark *pager = gtk_text_buffer_get_mark(buffer, "pager_position"); + /* Get an iter at the lower right corner of the visible part of the buffer */ gtk_text_view_get_visible_rect(textview, &visiblerect); - gtk_text_view_get_iter_at_location(textview, &pager, visiblerect.x + visiblerect.width, visiblerect.y + visiblerect.height); - gtk_text_buffer_move_mark_by_name(buffer, "pager_position", &pager); - + gtk_text_view_get_iter_at_location(textview, &newpager, visiblerect.x + visiblerect.width, visiblerect.y + visiblerect.height); + gtk_text_buffer_get_iter_at_mark(buffer, &oldpager, pager); + +#ifdef PAGE_ONLY_UNSEEN + if(gtk_text_iter_compare(&oldpager, &newpager) < 0) + gtk_text_buffer_move_mark(buffer, pager, &newpager); +#else + gtk_text_buffer_move_mark(buffer, pager, &newpager); +#endif + + /* Get the buffer coordinates of the pager and the end iter */ gtk_text_buffer_get_end_iter(buffer, &end); - gtk_text_view_get_iter_location(textview, &pager, &pagerpos); + gtk_text_buffer_get_iter_at_mark(buffer, &newpager, pager); + gtk_text_view_get_iter_location(textview, &newpager, &pagerpos); gtk_text_view_get_iter_location(textview, &end, &endpos); g_printerr("View height = %d\n", visiblerect.height); @@ -51,6 +71,7 @@ move_pager_and_get_scroll_distance(GtkTextView *textview) return endpos.y - pagerpos.y; } +/* Helper function: turn on paging for this textview */ static void start_paging(GtkTextView *textview) { @@ -59,6 +80,7 @@ start_paging(GtkTextView *textview) g_signal_handler_unblock(textview, pager_handler); } +/* Helper function: turn off paging for this textview */ static void stop_paging(GtkTextView *textview) { @@ -67,6 +89,7 @@ stop_paging(GtkTextView *textview) g_signal_handler_block(textview, pager_handler); } +/* Update the pager position after new text is inserted in the buffer */ static void after_insert(GtkTextBuffer *buffer, GtkTextIter *location, gchar *text, gint len, GtkTextView *textview) { @@ -76,41 +99,68 @@ after_insert(GtkTextBuffer *buffer, GtkTextIter *location, gchar *text, gint len /* Move the pager to the last visible character in the buffer */ gint scroll_distance = move_pager_and_get_scroll_distance(textview); - /* Wait for a keypress to advance the pager */ if(scroll_distance > 0 && !currently_paging) start_paging(textview); } +static void +adjustment_changed(GtkAdjustment *adjustment, GtkTextView *textview) +{ + while(gtk_events_pending()) + gtk_main_iteration(); + + /* Move the pager, etc. */ + gint scroll_distance = move_pager_and_get_scroll_distance(textview); + + if(scroll_distance > 0 && !currently_paging) + start_paging(textview); + else if(scroll_distance == 0 && currently_paging) + stop_paging(textview); + + /* Refresh the widget so that any extra "more" prompts disappear */ + gtk_widget_queue_draw(GTK_WIDGET(textview)); +} + +/* Handle key press events in the textview while paging is active */ static gboolean pager_wait(GtkTextView *textview, GdkEventKey *event, GtkTextBuffer *buffer) { + /*** ALTERNATIVE, POSSIBLY INFERIOR, METHOD OF SCROLLING *** 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); - - /*** ALTERNATIVE, POSSIBLY BETTER, METHOD OF SCROLLING *** - GtkWidget *scrolledwindow = gtk_widget_get_parent(GTK_WIDGET(textview)); - GtkAdjustment *adjustment = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(scrolledwindow)); - gdouble page_size; // SUCKY_DEBIAN: use gtk_adjustment_get_page_size() post 2.12 - g_object_get(adjustment, "page-size", &page_size, NULL); - gtk_adjustment_set_value(adjustment, gtk_adjustment_get_value(adjustment) + page_size); */ - while(gtk_events_pending()) - gtk_main_iteration(); - - /* Move the pager to the last visible character in the buffer */ - gint scroll_distance = move_pager_and_get_scroll_distance(textview); - - if(scroll_distance == 0) - stop_paging(textview); - - return TRUE; /* block further handlers */ + GtkWidget *scrolledwindow = gtk_widget_get_parent(GTK_WIDGET(textview)); + GtkAdjustment *adj = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(scrolledwindow)); + gdouble step_increment, page_size, upper, lower, value; + g_object_get(adj, + "page-size", &page_size, + "step-increment", &step_increment, + "upper", &upper, + "lower", &lower, + "value", &value, + NULL); + + switch (event->keyval) { + case GDK_space: case GDK_KP_Space: case GDK_Page_Down: case GDK_KP_Page_Down: + gtk_adjustment_set_value(adj, CLAMP(value + page_size, lower, upper - page_size)); + return TRUE; + case GDK_Page_Up: case GDK_KP_Page_Up: + gtk_adjustment_set_value(adj, CLAMP(value - page_size, lower, upper - page_size)); + return TRUE; + case GDK_Return: case GDK_KP_Enter: + gtk_adjustment_set_value(adj, CLAMP(value + step_increment, lower, upper - page_size)); + return TRUE; + /* don't handle "up" and "down", they're used for input history */ + } + + return FALSE; /* if the key wasn't handled here, pass it to other handlers */ } +/* Draw the "more" prompt on top of the buffer, after the regular expose event has run */ static gboolean expose_prompt(GtkTextView *textview, GdkEventExpose *event) { - /* Use Cairo? Cairo supported on Iliad? */ PangoLayout *prompt = gtk_widget_create_pango_layout(GTK_WIDGET(textview), "More"); gint promptwidth, promptheight; pango_layout_get_pixel_size(prompt, &promptwidth, &promptheight); @@ -137,6 +187,7 @@ main(int argc, char **argv) { gtk_init(&argc, &argv); + /* Construct the widgets */ GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_widget_set_size_request(window, 400, 400); GtkWidget *button = gtk_button_new_with_label("Barf"); @@ -146,6 +197,7 @@ main(int argc, char **argv) gtk_text_view_set_editable(GTK_TEXT_VIEW(textview), FALSE); GtkWidget *vbox = gtk_vbox_new(FALSE, 6); + /* Pack all the widgets into their containers */ gtk_container_add(GTK_CONTAINER(scrolledwindow), textview); gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 0); gtk_box_pack_start(GTK_BOX(vbox), scrolledwindow, TRUE, TRUE, 0); @@ -155,20 +207,26 @@ main(int argc, char **argv) /* Set up the textview widget to receive exposure events, must be done after widget has been shown */ gdk_window_set_events(gtk_widget_get_window(textview), GDK_EXPOSURE_MASK); + /* Connect paging signals */ GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview)); g_signal_connect_after(buffer, "insert-text", G_CALLBACK(after_insert), textview); + GtkAdjustment *adj = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(scrolledwindow)); + g_signal_connect_after(adj, "value-changed", G_CALLBACK(adjustment_changed), textview); pager_handler = g_signal_connect(textview, "key-press-event", G_CALLBACK(pager_wait), buffer); g_signal_handler_block(textview, pager_handler); expose_handler = g_signal_connect_after(textview, "expose-event", G_CALLBACK(expose_prompt), NULL); g_signal_handler_block(textview, expose_handler); + /* Create the pager position mark; it stands for the last character in the buffer + that has been on-screen */ GtkTextIter end; gtk_text_buffer_get_end_iter(buffer, &end); GtkTextMark *pagermark = gtk_text_buffer_create_mark(buffer, "pager_position", &end, TRUE); gtk_text_mark_set_visible(pagermark, TRUE); + /* Connect "regular program" signals */ g_signal_connect(window, "delete-event", G_CALLBACK(quit), NULL); - g_signal_connect(button, "clicked", G_CALLBACK(barf), buffer); + g_signal_connect(button, "clicked", G_CALLBACK(barf), textview); gtk_main(); return 0; -- 2.30.2