a8904cf2a9cf2bb4bcc808ce1e25779301cf9596
[rodin/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)
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         gtk_text_buffer_move_mark(buffer, pager, &newpager);
27
28         /* Get the buffer coordinates of the pager and the end iter */
29         gtk_text_buffer_get_end_iter(buffer, &end);
30         gtk_text_buffer_get_iter_at_mark(buffer, &newpager, pager);
31         gtk_text_view_get_iter_location(textview, &newpager, &pagerpos);
32         gtk_text_view_get_iter_location(textview, &end, &endpos);
33
34         g_printerr("View height = %d\n", visiblerect.height);
35         g_printerr("End - Pager = %d\n", endpos.y - pagerpos.y);
36         
37         *view_height = visiblerect.height;
38         *scroll_distance = endpos.y - pagerpos.y;
39 }
40
41 /* Helper function: turn on paging for this textview */
42 static void
43 start_paging(winid_t win)
44 {
45         win->currently_paging = TRUE;
46         g_signal_handler_unblock(win->widget, win->pager_expose_handler);
47         g_signal_handler_unblock(win->widget, win->pager_keypress_handler);
48 }
49
50 /* Helper function: turn off paging for this textview */
51 static void
52 stop_paging(winid_t win)
53 {
54         win->currently_paging = FALSE;
55         g_signal_handler_block(win->widget, win->pager_expose_handler);
56         g_signal_handler_block(win->widget, win->pager_keypress_handler);
57 }
58
59 /* Check whether paging should be done. This function is called inside the 
60  * idle handler, after the textview has finished updating. */
61 gboolean
62 pager_check(gpointer data)
63 {
64
65         printf("pager check...\n");
66         winid_t win = (winid_t) data;
67
68
69         /* Move the pager to the last visible character in the buffer */
70         gint view_height, scroll_distance;
71         move_pager_and_get_scroll_distance( GTK_TEXT_VIEW(win->widget), &view_height, &scroll_distance );
72
73         if(view_height <= 1)
74                 /* Paging is unusable when window is too small */
75                 return FALSE;
76         
77         if(!win->currently_paging) {
78                 if(scroll_distance > view_height) {
79                         start_paging(win);
80                         /* Seriously... */
81                         /* COMPAT: */
82 #if GTK_CHECK_VERSION(2,14,0)
83                         gdk_window_invalidate_rect(gtk_widget_get_window(win->widget), NULL, TRUE);
84 #else
85                         gdk_window_invalidate_rect(win->widget->window, NULL, TRUE);
86 #endif
87                 }
88                 else if(scroll_distance > 0) {
89                         GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(win->widget));
90                         GtkTextMark *end = gtk_text_buffer_get_mark(buffer, "end_position");
91
92                         gdk_threads_enter();
93                         gtk_text_view_scroll_mark_onscreen(GTK_TEXT_VIEW(win->widget), end);
94                         gdk_threads_leave();
95                 }
96         }
97
98         /* Returning FALSE to prevent this function from being called multiple times */
99         return FALSE;
100 }
101
102 /* When the user scrolls up in a textbuffer, start paging. */
103 void
104 pager_after_adjustment_changed(GtkAdjustment *adj, winid_t win)
105 {
106         /* Move the pager, etc. */
107         gint scroll_distance, view_height;
108         move_pager_and_get_scroll_distance( GTK_TEXT_VIEW(win->widget), &view_height, &scroll_distance );
109         
110         if(scroll_distance > 0 && !win->currently_paging)
111                 start_paging(win);
112         else if(scroll_distance == 0 && win->currently_paging)
113                 stop_paging(win);
114         
115         /* Refresh the widget so that any extra "more" prompts disappear */
116         gtk_widget_queue_draw(win->widget);
117 }
118
119 /* Handle key press events in the textview while paging is active */
120 gboolean
121 pager_on_key_press_event(GtkTextView *textview, GdkEventKey *event, winid_t win)
122 {
123         GtkAdjustment *adj = gtk_scrolled_window_get_vadjustment( GTK_SCROLLED_WINDOW(win->frame) );
124         gdouble step_increment, page_size, upper, lower, value;
125         g_object_get(adj, 
126                 "page-size", &page_size,
127                 "step-increment", &step_increment,
128                 "upper", &upper,
129                 "lower", &lower,
130                 "value", &value,
131                 NULL);
132         
133         switch (event->keyval) {
134                 case GDK_space: case GDK_KP_Space: case GDK_Page_Down: case GDK_KP_Page_Down:
135                         gtk_adjustment_set_value(adj, CLAMP(value + page_size, lower, upper - page_size));
136                         return TRUE;
137                 case GDK_Page_Up: case GDK_KP_Page_Up:
138                         gtk_adjustment_set_value(adj, CLAMP(value - page_size, lower, upper - page_size));
139                         return TRUE;
140                 case GDK_Return: case GDK_KP_Enter:
141                         gtk_adjustment_set_value(adj, CLAMP(value + step_increment, lower, upper - page_size));
142                         return TRUE;
143                         /* don't handle "up" and "down", they're used for input history */
144         }
145         
146         return FALSE; /* if the key wasn't handled here, pass it to other handlers */
147 }
148
149 /* Draw the "more" prompt on top of the buffer, after the regular expose event has run */
150 gboolean
151 pager_on_expose(GtkTextView *textview, GdkEventExpose *event, winid_t win)
152 {
153         /* Calculate the position of the 'more' tag */
154         gint promptwidth, promptheight;
155         pango_layout_get_pixel_size(win->pager_layout, &promptwidth, &promptheight);
156
157         gint winx, winy, winwidth, winheight;
158         gdk_window_get_position(event->window, &winx, &winy);
159         gdk_drawable_get_size(GDK_DRAWABLE(event->window), &winwidth, &winheight);
160
161         /* Draw the 'more' tag */
162         GdkGC *context = gdk_gc_new(GDK_DRAWABLE(event->window));
163         gdk_draw_layout(event->window, context, 
164                 winx + winwidth - promptwidth, 
165                 winy + winheight - promptheight, 
166                 win->pager_layout);
167
168         return FALSE; /* Propagate event further */
169 }
170