Ported babeltest to libsoup
[projects/chimara/chimara.git] / libchimara / pager.c
1 #include <gdk/gdkkeysyms.h>
2
3 #include "pager.h"
4
5 /* Helper function: move the pager to the last visible position in the buffer,
6  and return the distance between the pager and the end of the buffer in buffer
7  coordinates */
8 static void
9 move_pager_and_get_scroll_distance(GtkTextView *textview, gint *view_height, gint *scroll_distance, gboolean move )
10 {
11         GdkRectangle pagerpos, endpos, visiblerect;
12         GtkTextIter oldpager, newpager, end;
13         GtkTextBuffer *buffer = gtk_text_view_get_buffer(textview);
14         GtkTextMark *pager = gtk_text_buffer_get_mark(buffer, "pager_position");
15         
16         /* Get an iter at the lower right corner of the visible part of the buffer */
17         gtk_text_view_get_visible_rect(textview, &visiblerect);
18         gtk_text_view_get_iter_at_location(
19                 textview,
20                 &newpager,
21                 visiblerect.x + visiblerect.width,
22                 visiblerect.y + visiblerect.height
23         );
24         gtk_text_buffer_get_iter_at_mark(buffer, &oldpager, pager);
25         
26         if(move)
27                 gtk_text_buffer_move_mark(buffer, pager, &newpager);
28
29         /* Get the buffer coordinates of the pager and the end iter */
30         gtk_text_buffer_get_end_iter(buffer, &end);
31         gtk_text_buffer_get_iter_at_mark(buffer, &newpager, pager);
32         gtk_text_view_get_iter_location(textview, &newpager, &pagerpos);
33         gtk_text_view_get_iter_location(textview, &end, &endpos);
34
35         /*
36         g_printerr("View height = %d\n", visiblerect.height);
37         g_printerr("End - Pager = %d - %d = %d\n", endpos.y, pagerpos.y, endpos.y - pagerpos.y);
38         */
39         
40         *view_height = visiblerect.height;
41         *scroll_distance = endpos.y - pagerpos.y;
42 }
43
44 /* Helper function: turn on paging for this textview */
45 static void
46 start_paging(winid_t win)
47 {
48         win->currently_paging = TRUE;
49         g_signal_handler_unblock(win->widget, win->pager_expose_handler);
50         g_signal_handler_unblock(win->widget, win->pager_keypress_handler);
51 }
52
53 /* Helper function: turn off paging for this textview */
54 static void
55 stop_paging(winid_t win)
56 {
57         win->currently_paging = FALSE;
58         g_signal_handler_block(win->widget, win->pager_expose_handler);
59         g_signal_handler_block(win->widget, win->pager_keypress_handler);
60 }
61
62 /* When the user scrolls up in a textbuffer, start paging. */
63 void
64 pager_after_adjustment_changed(GtkAdjustment *adj, winid_t win)
65 {
66         /* Move the pager, etc. */
67         gint scroll_distance, view_height;
68         move_pager_and_get_scroll_distance( GTK_TEXT_VIEW(win->widget), &view_height, &scroll_distance, TRUE );
69
70         if(scroll_distance > 0 && !win->currently_paging)
71                 start_paging(win);
72         else if(scroll_distance == 0 && win->currently_paging)
73                 stop_paging(win);
74         
75         /* Refresh the widget so that any extra "more" prompts disappear */
76         gtk_widget_queue_draw(win->widget);
77 }
78
79 /* Handle key press events in the textview while paging is active */
80 gboolean
81 pager_on_key_press_event(GtkTextView *textview, GdkEventKey *event, winid_t win)
82 {
83         GtkAdjustment *adj = gtk_scrolled_window_get_vadjustment( GTK_SCROLLED_WINDOW(win->frame) );
84         gdouble page_size, upper, lower, value;
85         g_object_get(adj, 
86                 "page-size", &page_size,
87                 "upper", &upper,
88                 "lower", &lower,
89                 "value", &value,
90                 NULL);
91         
92         switch (event->keyval) {
93                 case GDK_space: case GDK_KP_Space:
94                 case GDK_Page_Down: case GDK_KP_Page_Down:
95                 case GDK_Return: case GDK_KP_Enter:
96                         gtk_adjustment_set_value(adj, CLAMP(value + page_size, lower, upper - page_size));
97                         return TRUE;
98                 case GDK_Page_Up: case GDK_KP_Page_Up:
99                         gtk_adjustment_set_value(adj, CLAMP(value - page_size, lower, upper - page_size));
100                         return TRUE;
101                         /* don't handle "up" and "down", they're used for input history */
102         }
103         
104         return FALSE; /* if the key wasn't handled here, pass it to other handlers */
105 }
106
107 /* Draw the "more" prompt on top of the buffer, after the regular expose event has run */
108 gboolean
109 pager_on_expose(GtkTextView *textview, GdkEventExpose *event, winid_t win)
110 {
111         /* Calculate the position of the 'more' tag */
112         gint promptwidth, promptheight;
113         pango_layout_get_pixel_size(win->pager_layout, &promptwidth, &promptheight);
114
115         gint winx, winy, winwidth, winheight;
116         gdk_window_get_position(event->window, &winx, &winy);
117         gdk_drawable_get_size(GDK_DRAWABLE(event->window), &winwidth, &winheight);
118
119         /* Draw the 'more' tag */
120         GdkGC *context = gdk_gc_new(GDK_DRAWABLE(event->window));
121         gdk_draw_layout(event->window, context, 
122                 winx + winwidth - promptwidth, 
123                 winy + winheight - promptheight, 
124                 win->pager_layout);
125
126         return FALSE; /* Propagate event further */
127 }
128
129 /* Check whether paging should be done. This function is called after the
130  * textview has finished validating text positions. */
131 void 
132 pager_after_size_request(GtkTextView *textview, GtkRequisition *requisition, winid_t win) 
133 {
134         /* Move the pager to the last visible character in the buffer */
135         gint view_height, scroll_distance; 
136         move_pager_and_get_scroll_distance(GTK_TEXT_VIEW(win->widget), &view_height, &scroll_distance, FALSE);
137
138         if(view_height <= 1)
139                 /* Paging is unusable when window is too small */
140                 return;
141
142         /* If not in interactive mode, then just scroll to the bottom. */
143         ChimaraGlk *glk = CHIMARA_GLK(gtk_widget_get_ancestor(GTK_WIDGET(textview), CHIMARA_TYPE_GLK));
144         g_assert(glk);
145         if(!chimara_glk_get_interactive(glk)) {
146                 GtkTextIter end;
147                 gtk_text_buffer_get_end_iter(gtk_text_view_get_buffer(textview), &end);
148                 gtk_text_view_scroll_to_iter(textview, &end, 0.0, TRUE, 0.0, 0.0);
149                 return;
150         }
151         
152         /* Scroll past text already read by user. This is automatic scrolling, so disable the pager_ajustment_handler
153          * first, that acts on the belief the scolling is performed by the user. */
154         GtkAdjustment *adj = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(win->frame));
155         g_signal_handler_block(adj, win->pager_adjustment_handler);
156         GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(win->widget));
157         GtkTextMark *pager_position = gtk_text_buffer_get_mark(buffer, "pager_position");
158         gtk_text_view_scroll_to_mark(GTK_TEXT_VIEW(win->widget), pager_position, 0.0, TRUE, 0.0, 0.0);
159         g_signal_handler_unblock(adj, win->pager_adjustment_handler);
160         
161         if(!win->currently_paging) {
162                 if(scroll_distance > view_height) {
163                         start_paging(win);
164                         /* Seriously... */
165                         /* COMPAT: */
166 #if GTK_CHECK_VERSION(2,14,0)
167                         gdk_window_invalidate_rect(gtk_widget_get_window(win->widget), NULL, TRUE);
168 #else
169                         gdk_window_invalidate_rect(win->widget->window, NULL, TRUE);
170 #endif
171                 }
172         }
173 }
174
175 void
176 pager_update(winid_t win)
177 {
178         GtkTextIter input_iter;
179         GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(win->widget));
180         GtkTextMark *input_position = gtk_text_buffer_get_mark(buffer, "input_position");
181         GtkTextMark *pager_position = gtk_text_buffer_get_mark(buffer, "pager_position");
182         gtk_text_buffer_get_iter_at_mark(buffer, &input_iter, input_position);
183         gtk_text_buffer_move_mark(buffer, pager_position, &input_iter);
184 }