Add chimara_app_foreach_game_window()
[projects/chimara/chimara.git] / tests / barf.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(GtkTextView *textview)
81 {
82         currently_paging = TRUE;
83         g_signal_handler_unblock(textview, expose_handler);
84         g_signal_handler_unblock(textview, pager_handler);
85 }
86
87 /* Helper function: turn off paging for this textview */
88 static void
89 stop_paging(GtkTextView *textview)
90 {
91         currently_paging = FALSE;
92         g_signal_handler_block(textview, expose_handler);
93         g_signal_handler_block(textview, 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                 start_paging(textview);
108 }
109
110 static void
111 adjustment_changed(GtkAdjustment *adjustment, GtkTextView *textview)
112 {
113         while(gtk_events_pending())
114                 gtk_main_iteration();
115         
116         /* Move the pager, etc. */
117         gint scroll_distance = move_pager_and_get_scroll_distance(textview);
118         
119         if(scroll_distance > 0 && !currently_paging)
120                 start_paging(textview);
121         else if(scroll_distance == 0 && currently_paging)
122                 stop_paging(textview);
123         
124         /* Refresh the widget so that any extra "more" prompts disappear */
125         gtk_widget_queue_draw(GTK_WIDGET(textview));
126 }
127
128 /* Handle key press events in the textview while paging is active */
129 static gboolean
130 pager_wait(GtkTextView *textview, GdkEventKey *event, GtkTextBuffer *buffer)
131 {
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);
135         */
136
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;
140         g_object_get(adj, 
141                 "page-size", &page_size,
142                 "step-increment", &step_increment,
143                 "upper", &upper,
144                 "lower", &lower,
145                 "value", &value,
146                 NULL);
147         
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));
151                         break;
152                 case GDK_Page_Up: case GDK_KP_Page_Up:
153                         gtk_adjustment_set_value(adj, CLAMP(value - page_size, lower, upper - page_size));
154                         break;
155                 case GDK_Return: case GDK_KP_Enter:
156                         gtk_adjustment_set_value(adj, CLAMP(value + step_increment, lower, upper - page_size));
157                         break;
158                         /* don't handle "up" and "down", they're used for input history */
159                 default:
160                         return FALSE; /* if the key wasn't handled here, pass it to other handlers */
161         }
162
163         gdk_window_process_updates(gtk_widget_get_window(GTK_WIDGET(textview)), TRUE);
164         return TRUE; /* handled it, stop this event now */
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(GtkTextView *textview, GdkEventExpose *event)
170 {
171         GdkGC *context = gdk_gc_new(GDK_DRAWABLE(event->window));
172
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);
176
177         gdk_draw_layout_with_colors(event->window, context, 
178                 winx + winwidth - promptwidth, 
179                 winy + winheight - promptheight, 
180                 prompt, &white, &red);
181         
182         return FALSE; /* Propagate event further */
183 }
184
185 int
186 main(int argc, char **argv)
187 {
188         gtk_init(&argc, &argv);
189         
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);
199         
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);
206         
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);
209
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);
215
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);
225         
226         /* Create the pager position mark; it stands for the last character in the buffer
227          that has been on-screen */
228         GtkTextIter end;
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);
232         
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);
236         
237         gtk_main();
238
239         g_object_unref(prompt);
240         return 0;
241 }