3 * gcc -g -O0 -Wall -pthread -o barf barf.c `pkg-config --cflags --libs gtk+-2.0`
7 #include <gdk/gdkkeysyms.h>
9 /* Uncomment to try the other paging method */
10 /* #define PAGE_ONLY_UNSEEN 1 */
12 static gulong pager_handler = 0;
13 static gulong expose_handler = 0;
14 static gboolean currently_paging = FALSE;
16 static gint promptwidth, promptheight;
17 static PangoLayout *prompt;
18 static GdkColor red, white;
27 /* Vomit a load of text onto the screen */
29 barf(GtkButton *button, GtkTextView *textview)
32 GtkTextBuffer *buffer = gtk_text_view_get_buffer(textview);
33 gtk_text_buffer_get_end_iter(buffer, &end);
36 g_file_get_contents("loremipsum.txt", &loremipsum, NULL, NULL);
37 gtk_text_buffer_insert(buffer, &end, loremipsum, -1);
40 gtk_widget_grab_focus(GTK_WIDGET(textview));
43 /* Helper function: move the pager to the last visible position in the buffer,
44 and return the distance between the pager and the end of the buffer in buffer
47 move_pager_and_get_scroll_distance(GtkTextView *textview)
49 GdkRectangle pagerpos, endpos, visiblerect;
50 GtkTextIter oldpager, newpager, end;
51 GtkTextBuffer *buffer = gtk_text_view_get_buffer(textview);
52 GtkTextMark *pager = gtk_text_buffer_get_mark(buffer, "pager_position");
54 /* Get an iter at the lower right corner of the visible part of the buffer */
55 gtk_text_view_get_visible_rect(textview, &visiblerect);
56 gtk_text_view_get_iter_at_location(textview, &newpager, visiblerect.x + visiblerect.width, visiblerect.y + visiblerect.height);
57 gtk_text_buffer_get_iter_at_mark(buffer, &oldpager, pager);
59 #ifdef PAGE_ONLY_UNSEEN
60 if(gtk_text_iter_compare(&oldpager, &newpager) < 0)
61 gtk_text_buffer_move_mark(buffer, pager, &newpager);
63 gtk_text_buffer_move_mark(buffer, pager, &newpager);
66 /* Get the buffer coordinates of the pager and the end iter */
67 gtk_text_buffer_get_end_iter(buffer, &end);
68 gtk_text_buffer_get_iter_at_mark(buffer, &newpager, pager);
69 gtk_text_view_get_iter_location(textview, &newpager, &pagerpos);
70 gtk_text_view_get_iter_location(textview, &end, &endpos);
72 //g_printerr("View height = %d\n", visiblerect.height);
73 //g_printerr("End - Pager = %d\n", endpos.y - pagerpos.y);
75 return endpos.y - pagerpos.y;
78 /* Helper function: turn on paging for this textview */
80 start_paging(GtkWidget *eventbox)
82 currently_paging = TRUE;
83 g_signal_handler_unblock(eventbox, expose_handler);
84 g_signal_handler_unblock(eventbox, pager_handler);
87 /* Helper function: turn off paging for this textview */
89 stop_paging(GtkWidget *eventbox)
91 currently_paging = FALSE;
92 g_signal_handler_block(eventbox, expose_handler);
93 g_signal_handler_block(eventbox, pager_handler);
96 /* Update the pager position after new text is inserted in the buffer */
98 after_insert(GtkTextBuffer *buffer, GtkTextIter *location, gchar *text, gint len, GtkTextView *textview)
100 while(gtk_events_pending())
101 gtk_main_iteration();
103 /* Move the pager to the last visible character in the buffer */
104 gint scroll_distance = move_pager_and_get_scroll_distance(textview);
106 if(scroll_distance > 0 && !currently_paging) {
107 GtkWidget *eventbox = gtk_widget_get_parent( gtk_widget_get_parent(GTK_WIDGET(textview)) );
108 start_paging(eventbox);
113 adjustment_changed(GtkAdjustment *adjustment, GtkTextView *textview)
115 while(gtk_events_pending())
116 gtk_main_iteration();
118 /* Move the pager, etc. */
119 gint scroll_distance = move_pager_and_get_scroll_distance(textview);
121 GtkWidget *eventbox = gtk_widget_get_parent( gtk_widget_get_parent(GTK_WIDGET(textview)) );
122 if(scroll_distance > 0 && !currently_paging)
123 start_paging(eventbox);
124 else if(scroll_distance == 0 && currently_paging)
125 stop_paging(eventbox);
127 /* Refresh the widget so that any extra "more" prompts disappear */
128 gtk_widget_queue_draw(GTK_WIDGET(eventbox));
131 /* Handle key press events in the textview while paging is active */
133 pager_wait(GtkTextView *eventbox, GdkEventKey *event, GtkTextBuffer *buffer)
135 /*** ALTERNATIVE, POSSIBLY INFERIOR, METHOD OF SCROLLING ***
136 GtkTextMark *pagermark = gtk_text_buffer_get_mark(buffer, "pager_position");
137 gtk_text_view_scroll_to_mark(textview, pagermark, 0.0, TRUE, 0.0, 0.0);
140 GtkWidget *scrolledwindow = gtk_bin_get_child(GTK_BIN(eventbox));
141 GtkAdjustment *adj = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(scrolledwindow));
142 gdouble step_increment, page_size, upper, lower, value;
144 "page-size", &page_size,
145 "step-increment", &step_increment,
151 switch (event->keyval) {
152 case GDK_space: case GDK_KP_Space: case GDK_Page_Down: case GDK_KP_Page_Down:
153 gtk_adjustment_set_value(adj, CLAMP(value + page_size, lower, upper - page_size));
155 case GDK_Page_Up: case GDK_KP_Page_Up:
156 gtk_adjustment_set_value(adj, CLAMP(value - page_size, lower, upper - page_size));
158 case GDK_Return: case GDK_KP_Enter:
159 gtk_adjustment_set_value(adj, CLAMP(value + step_increment, lower, upper - page_size));
161 /* don't handle "up" and "down", they're used for input history */
164 return FALSE; /* if the key wasn't handled here, pass it to other handlers */
167 /* Draw the "more" prompt on top of the buffer, after the regular expose event has run */
169 expose_prompt(GtkEventBox *eventbox, GdkEventExpose *event)
171 g_printerr("Eventbox expose callback!\n");
173 while(gtk_events_pending())
174 gtk_main_iteration();
176 GdkGC *context = gdk_gc_new(GDK_DRAWABLE(event->window));
178 gint winx, winy, winwidth, winheight;
179 gdk_window_get_position(event->window, &winx, &winy);
180 gdk_drawable_get_size(GDK_DRAWABLE(event->window), &winwidth, &winheight);
182 gdk_draw_layout_with_colors(event->window, context,
183 winx + winwidth - promptwidth,
184 //winy + winheight - promptheight,
186 prompt, &white, &red);
188 return FALSE; /* Propagate event further */
192 expose_scroll(GtkScrolledWindow *scroll, GdkEventExpose *event)
194 g_printerr("Scrolled window expose callback!\n");
199 expose_textview(GtkTextView *textview, GdkEventExpose *event)
201 g_printerr("Text view expose callback!\n");
206 main(int argc, char **argv)
208 gtk_init(&argc, &argv);
210 /* Construct the widgets */
211 GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
212 gtk_widget_set_size_request(window, 400, 400);
213 GtkWidget *button = gtk_button_new_with_label("Barf");
214 GtkWidget *eventbox = gtk_event_box_new();
215 // gtk_event_box_set_above_child( GTK_EVENT_BOX(eventbox), TRUE );
216 GTK_WIDGET_SET_FLAGS(eventbox, GTK_APP_PAINTABLE);
217 GtkWidget *scrolledwindow = gtk_scrolled_window_new(NULL, NULL);
218 GtkWidget *textview = gtk_text_view_new();
219 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(textview), GTK_WRAP_WORD_CHAR);
220 gtk_text_view_set_editable(GTK_TEXT_VIEW(textview), FALSE);
221 GtkWidget *vbox = gtk_vbox_new(FALSE, 6);
223 /* Pack all the widgets into their containers */
224 gtk_container_add(GTK_CONTAINER(scrolledwindow), textview);
225 gtk_container_add(GTK_CONTAINER(eventbox), scrolledwindow);
226 gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 0);
227 gtk_box_pack_start(GTK_BOX(vbox), eventbox, TRUE, TRUE, 0);
228 gtk_container_add(GTK_CONTAINER(window), vbox);
229 gtk_widget_show_all(window);
231 /* Set up the textview widget to receive exposure events, must be done after widget has been shown */
232 gdk_window_set_events(gtk_widget_get_window(eventbox), GDK_EXPOSURE_MASK);
234 /* Create the 'more' prompt */
235 prompt = gtk_widget_create_pango_layout(GTK_WIDGET(textview), "More");
236 pango_layout_get_pixel_size(prompt, &promptwidth, &promptheight);
237 gdk_color_parse("red", &red);
238 gdk_color_parse("white", &white);
240 /* Connect paging signals */
241 GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview));
242 g_signal_connect_after(buffer, "insert-text", G_CALLBACK(after_insert), textview);
243 GtkAdjustment *adj = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(scrolledwindow));
244 g_signal_connect_after(adj, "value-changed", G_CALLBACK(adjustment_changed), textview);
245 pager_handler = g_signal_connect(eventbox, "key-press-event", G_CALLBACK(pager_wait), buffer);
246 g_signal_handler_block(eventbox, pager_handler);
247 expose_handler = g_signal_connect_after(eventbox, "expose-event", G_CALLBACK(expose_prompt), NULL);
248 g_signal_handler_block(eventbox, expose_handler);
250 g_signal_connect(scrolledwindow, "expose-event", G_CALLBACK(expose_scroll), NULL);
251 g_signal_connect(textview, "expose-event", G_CALLBACK(expose_textview), NULL);
253 /* Create the pager position mark; it stands for the last character in the buffer
254 that has been on-screen */
256 gtk_text_buffer_get_end_iter(buffer, &end);
257 GtkTextMark *pagermark = gtk_text_buffer_create_mark(buffer, "pager_position", &end, TRUE);
258 gtk_text_mark_set_visible(pagermark, TRUE);
260 /* Connect "regular program" signals */
261 g_signal_connect(window, "delete-event", G_CALLBACK(quit), NULL);
262 g_signal_connect(button, "clicked", G_CALLBACK(barf), textview);
266 g_object_unref(prompt);