Clear GModule pointer to prevent accidental re-use
[projects/chimara/chimara.git] / tests / barf-eventbox.c
1 /*
2  * Compile me with:
3  * gcc -g -O0 -Wall -pthread -o barf barf.c `pkg-config --cflags --libs gtk+-2.0`
4  */
5
6 #include <gtk/gtk.h>
7 #include <gdk/gdkkeysyms.h>
8
9 /* Uncomment to try the other paging method */
10 /* #define PAGE_ONLY_UNSEEN 1 */
11
12 static gulong pager_handler = 0;
13 static gulong expose_handler = 0;
14 static gboolean currently_paging = FALSE;
15
16 static gint promptwidth, promptheight;
17 static PangoLayout *prompt;
18 static GdkColor red, white;
19
20 static gboolean
21 quit()
22 {
23         gtk_main_quit();
24         return TRUE;
25 }
26
27 /* Vomit a load of text onto the screen */
28 static void
29 barf(GtkButton *button, GtkTextView *textview)
30 {
31         GtkTextIter end;
32         GtkTextBuffer *buffer = gtk_text_view_get_buffer(textview);
33         gtk_text_buffer_get_end_iter(buffer, &end);
34         
35         gchar *loremipsum;
36         g_file_get_contents("loremipsum.txt", &loremipsum, NULL, NULL);
37         gtk_text_buffer_insert(buffer, &end, loremipsum, -1);
38         g_free(loremipsum);
39         
40         gtk_widget_grab_focus(GTK_WIDGET(textview));
41 }
42
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
45  coordinates */
46 static gint
47 move_pager_and_get_scroll_distance(GtkTextView *textview)
48 {
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");
53         
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);
58         
59 #ifdef PAGE_ONLY_UNSEEN
60         if(gtk_text_iter_compare(&oldpager, &newpager) < 0)
61                 gtk_text_buffer_move_mark(buffer, pager, &newpager);
62 #else
63         gtk_text_buffer_move_mark(buffer, pager, &newpager);
64 #endif
65
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);
71         
72         //g_printerr("View height = %d\n", visiblerect.height);
73         //g_printerr("End - Pager = %d\n", endpos.y - pagerpos.y);
74         
75         return endpos.y - pagerpos.y;
76 }
77
78 /* Helper function: turn on paging for this textview */
79 static void
80 start_paging(GtkWidget *eventbox)
81 {
82         currently_paging = TRUE;
83         g_signal_handler_unblock(eventbox, expose_handler);
84         g_signal_handler_unblock(eventbox, pager_handler);
85 }
86
87 /* Helper function: turn off paging for this textview */
88 static void
89 stop_paging(GtkWidget *eventbox)
90 {
91         currently_paging = FALSE;
92         g_signal_handler_block(eventbox, expose_handler);
93         g_signal_handler_block(eventbox, pager_handler);
94 }
95
96 /* Update the pager position after new text is inserted in the buffer */
97 static void
98 after_insert(GtkTextBuffer *buffer, GtkTextIter *location, gchar *text, gint len, GtkTextView *textview)
99 {
100         while(gtk_events_pending())
101                 gtk_main_iteration();
102         
103         /* Move the pager to the last visible character in the buffer */
104         gint scroll_distance = move_pager_and_get_scroll_distance(textview);
105         
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);
109         }
110 }
111
112 static void
113 adjustment_changed(GtkAdjustment *adjustment, GtkTextView *textview)
114 {
115         while(gtk_events_pending())
116                 gtk_main_iteration();
117         
118         /* Move the pager, etc. */
119         gint scroll_distance = move_pager_and_get_scroll_distance(textview);
120         
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);
126         
127         /* Refresh the widget so that any extra "more" prompts disappear */
128         gtk_widget_queue_draw(GTK_WIDGET(eventbox));
129 }
130
131 /* Handle key press events in the textview while paging is active */
132 static gboolean
133 pager_wait(GtkTextView *eventbox, GdkEventKey *event, GtkTextBuffer *buffer)
134 {
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);
138         */
139
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;
143         g_object_get(adj, 
144                 "page-size", &page_size,
145                 "step-increment", &step_increment,
146                 "upper", &upper,
147                 "lower", &lower,
148                 "value", &value,
149                 NULL);
150         
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));
154                         return TRUE;
155                 case GDK_Page_Up: case GDK_KP_Page_Up:
156                         gtk_adjustment_set_value(adj, CLAMP(value - page_size, lower, upper - page_size));
157                         return TRUE;
158                 case GDK_Return: case GDK_KP_Enter:
159                         gtk_adjustment_set_value(adj, CLAMP(value + step_increment, lower, upper - page_size));
160                         return TRUE;
161                         /* don't handle "up" and "down", they're used for input history */
162         }
163         
164         return FALSE; /* if the key wasn't handled here, pass it to other handlers */
165 }
166
167 /* Draw the "more" prompt on top of the buffer, after the regular expose event has run */
168 static gboolean
169 expose_prompt(GtkEventBox *eventbox, GdkEventExpose *event)
170 {
171         g_printerr("Eventbox expose callback!\n");
172
173         while(gtk_events_pending())
174                 gtk_main_iteration();
175         
176         GdkGC *context = gdk_gc_new(GDK_DRAWABLE(event->window));
177
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);
181
182         gdk_draw_layout_with_colors(event->window, context, 
183                 winx + winwidth - promptwidth, 
184                 //winy + winheight - promptheight, 
185                 winy - 5, 
186                 prompt, &white, &red);
187         
188         return FALSE; /* Propagate event further */
189 }
190
191 static gboolean
192 expose_scroll(GtkScrolledWindow *scroll, GdkEventExpose *event)
193 {
194         g_printerr("Scrolled window expose callback!\n");
195         return FALSE;
196 }
197
198 static gboolean
199 expose_textview(GtkTextView *textview, GdkEventExpose *event)
200 {
201         g_printerr("Text view expose callback!\n");
202         return FALSE;
203 }
204
205 int
206 main(int argc, char **argv)
207 {
208         gtk_init(&argc, &argv);
209         
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);
222         
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);
230         
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);
233
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);
239
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);
249
250         g_signal_connect(scrolledwindow, "expose-event", G_CALLBACK(expose_scroll), NULL);
251         g_signal_connect(textview, "expose-event", G_CALLBACK(expose_textview), NULL);
252         
253         /* Create the pager position mark; it stands for the last character in the buffer
254          that has been on-screen */
255         GtkTextIter end;
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);
259         
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);
263         
264         gtk_main();
265
266         g_object_unref(prompt);
267         return 0;
268 }