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(GtkTextView *textview)
82 currently_paging = TRUE;
83 g_signal_handler_unblock(textview, expose_handler);
84 g_signal_handler_unblock(textview, pager_handler);
87 /* Helper function: turn off paging for this textview */
89 stop_paging(GtkTextView *textview)
91 currently_paging = FALSE;
92 g_signal_handler_block(textview, expose_handler);
93 g_signal_handler_block(textview, 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 start_paging(textview);
111 adjustment_changed(GtkAdjustment *adjustment, GtkTextView *textview)
113 while(gtk_events_pending())
114 gtk_main_iteration();
116 /* Move the pager, etc. */
117 gint scroll_distance = move_pager_and_get_scroll_distance(textview);
119 if(scroll_distance > 0 && !currently_paging)
120 start_paging(textview);
121 else if(scroll_distance == 0 && currently_paging)
122 stop_paging(textview);
124 /* Refresh the widget so that any extra "more" prompts disappear */
125 gtk_widget_queue_draw(GTK_WIDGET(textview));
128 /* Handle key press events in the textview while paging is active */
130 pager_wait(GtkTextView *textview, GdkEventKey *event, GtkTextBuffer *buffer)
132 /*** ALTERNATIVE, POSSIBLY INFERIOR, METHOD OF SCROLLING ***
133 GtkTextMark *pagermark = gtk_text_buffer_get_mark(buffer, "pager_position");
134 gtk_text_view_scroll_to_mark(textview, pagermark, 0.0, TRUE, 0.0, 0.0);
137 GtkWidget *scrolledwindow = gtk_widget_get_parent(GTK_WIDGET(textview));
138 GtkAdjustment *adj = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(scrolledwindow));
139 gdouble step_increment, page_size, upper, lower, value;
141 "page-size", &page_size,
142 "step-increment", &step_increment,
148 switch (event->keyval) {
149 case GDK_space: case GDK_KP_Space: case GDK_Page_Down: case GDK_KP_Page_Down:
150 gtk_adjustment_set_value(adj, CLAMP(value + page_size, lower, upper - page_size));
152 case GDK_Page_Up: case GDK_KP_Page_Up:
153 gtk_adjustment_set_value(adj, CLAMP(value - page_size, lower, upper - page_size));
155 case GDK_Return: case GDK_KP_Enter:
156 gtk_adjustment_set_value(adj, CLAMP(value + step_increment, lower, upper - page_size));
158 /* don't handle "up" and "down", they're used for input history */
160 return FALSE; /* if the key wasn't handled here, pass it to other handlers */
163 gdk_window_process_updates(gtk_widget_get_window(GTK_WIDGET(textview)), TRUE);
164 return TRUE; /* handled it, stop this event now */
167 /* Draw the "more" prompt on top of the buffer, after the regular expose event has run */
169 expose_prompt(GtkTextView *textview, GdkEventExpose *event)
171 GdkGC *context = gdk_gc_new(GDK_DRAWABLE(event->window));
173 gint winx, winy, winwidth, winheight;
174 gdk_window_get_position(event->window, &winx, &winy);
175 gdk_drawable_get_size(GDK_DRAWABLE(event->window), &winwidth, &winheight);
177 gdk_draw_layout_with_colors(event->window, context,
178 winx + winwidth - promptwidth,
179 winy + winheight - promptheight,
180 prompt, &white, &red);
182 return FALSE; /* Propagate event further */
186 main(int argc, char **argv)
188 gtk_init(&argc, &argv);
190 /* Construct the widgets */
191 GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
192 gtk_widget_set_size_request(window, 400, 400);
193 GtkWidget *button = gtk_button_new_with_label("Barf");
194 GtkWidget *scrolledwindow = gtk_scrolled_window_new(NULL, NULL);
195 GtkWidget *textview = gtk_text_view_new();
196 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(textview), GTK_WRAP_WORD_CHAR);
197 gtk_text_view_set_editable(GTK_TEXT_VIEW(textview), FALSE);
198 GtkWidget *vbox = gtk_vbox_new(FALSE, 6);
200 /* Pack all the widgets into their containers */
201 gtk_container_add(GTK_CONTAINER(scrolledwindow), textview);
202 gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 0);
203 gtk_box_pack_start(GTK_BOX(vbox), scrolledwindow, TRUE, TRUE, 0);
204 gtk_container_add(GTK_CONTAINER(window), vbox);
205 gtk_widget_show_all(window);
207 /* Set up the textview widget to receive exposure events, must be done after widget has been shown */
208 gdk_window_set_events(gtk_widget_get_window(textview), GDK_EXPOSURE_MASK);
210 /* Create the 'more' prompt */
211 prompt = gtk_widget_create_pango_layout(GTK_WIDGET(textview), "More");
212 pango_layout_get_pixel_size(prompt, &promptwidth, &promptheight);
213 gdk_color_parse("red", &red);
214 gdk_color_parse("white", &white);
216 /* Connect paging signals */
217 GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview));
218 g_signal_connect_after(buffer, "insert-text", G_CALLBACK(after_insert), textview);
219 GtkAdjustment *adj = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(scrolledwindow));
220 g_signal_connect_after(adj, "value-changed", G_CALLBACK(adjustment_changed), textview);
221 pager_handler = g_signal_connect(textview, "key-press-event", G_CALLBACK(pager_wait), buffer);
222 g_signal_handler_block(textview, pager_handler);
223 expose_handler = g_signal_connect_after(textview, "expose-event", G_CALLBACK(expose_prompt), NULL);
224 g_signal_handler_block(textview, expose_handler);
226 /* Create the pager position mark; it stands for the last character in the buffer
227 that has been on-screen */
229 gtk_text_buffer_get_end_iter(buffer, &end);
230 GtkTextMark *pagermark = gtk_text_buffer_create_mark(buffer, "pager_position", &end, TRUE);
231 gtk_text_mark_set_visible(pagermark, TRUE);
233 /* Connect "regular program" signals */
234 g_signal_connect(window, "delete-event", G_CALLBACK(quit), NULL);
235 g_signal_connect(button, "clicked", G_CALLBACK(barf), textview);
239 g_object_unref(prompt);