Pager in place
[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 gint
9 move_pager_and_get_scroll_distance(GtkTextView *textview)
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(textview, &newpager, visiblerect.x + visiblerect.width, visiblerect.y + visiblerect.height);
19         gtk_text_buffer_get_iter_at_mark(buffer, &oldpager, pager);
20         
21         gtk_text_buffer_move_mark(buffer, pager, &newpager);
22
23         /* Get the buffer coordinates of the pager and the end iter */
24         gtk_text_buffer_get_end_iter(buffer, &end);
25         gtk_text_buffer_get_iter_at_mark(buffer, &newpager, pager);
26         gtk_text_view_get_iter_location(textview, &newpager, &pagerpos);
27         gtk_text_view_get_iter_location(textview, &end, &endpos);
28         
29         //g_printerr("View height = %d\n", visiblerect.height);
30         //g_printerr("End - Pager = %d\n", endpos.y - pagerpos.y);
31         
32         return endpos.y - pagerpos.y;
33 }
34
35 /* Helper function: turn on paging for this textview */
36 static void
37 start_paging(winid_t win)
38 {
39         win->currently_paging = TRUE;
40         g_signal_handler_unblock(win->widget, win->pager_expose_handler);
41         g_signal_handler_unblock(win->widget, win->pager_keypress_handler);
42 }
43
44 /* Helper function: turn off paging for this textview */
45 static void
46 stop_paging(winid_t win)
47 {
48         win->currently_paging = FALSE;
49         g_signal_handler_block(win->widget, win->pager_expose_handler);
50         g_signal_handler_block(win->widget, win->pager_keypress_handler);
51 }
52
53 /* Update the pager position after new text is inserted in the buffer */
54 void
55 pager_after_insert_text(GtkTextBuffer *buffer, GtkTextIter *location, gchar *text, gint len, winid_t win)
56 {
57         while(gtk_events_pending())
58                 gtk_main_iteration();
59         
60         /* Move the pager to the last visible character in the buffer */
61         gint scroll_distance = move_pager_and_get_scroll_distance( GTK_TEXT_VIEW(win->widget) );
62         
63         if(scroll_distance > 0 && !win->currently_paging)
64                 start_paging(win);
65 }
66
67 void
68 pager_after_adjustment_changed(GtkAdjustment *adj, winid_t win)
69 {
70         while(gtk_events_pending())
71                 gtk_main_iteration();
72         
73         /* Move the pager, etc. */
74         gint scroll_distance = move_pager_and_get_scroll_distance( GTK_TEXT_VIEW(win->widget) );
75         
76         if(scroll_distance > 0 && !win->currently_paging)
77                 start_paging(win);
78         else if(scroll_distance == 0 && win->currently_paging)
79                 stop_paging(win);
80         
81         /* Refresh the widget so that any extra "more" prompts disappear */
82         gtk_widget_queue_draw(win->widget);
83 }
84
85 /* Handle key press events in the textview while paging is active */
86 gboolean
87 pager_on_key_press_event(GtkTextView *textview, GdkEventKey *event, winid_t win)
88 {
89         /*** ALTERNATIVE, POSSIBLY INFERIOR, METHOD OF SCROLLING ***
90         GtkTextMark *pagermark = gtk_text_buffer_get_mark(buffer, "pager_position");
91         gtk_text_view_scroll_to_mark(textview, pagermark, 0.0, TRUE, 0.0, 0.0);
92         */
93
94         GtkAdjustment *adj = gtk_scrolled_window_get_vadjustment( GTK_SCROLLED_WINDOW(win->frame) );
95         gdouble step_increment, page_size, upper, lower, value;
96         g_object_get(adj, 
97                 "page-size", &page_size,
98                 "step-increment", &step_increment,
99                 "upper", &upper,
100                 "lower", &lower,
101                 "value", &value,
102                 NULL);
103         
104         switch (event->keyval) {
105                 case GDK_space: case GDK_KP_Space: case GDK_Page_Down: case GDK_KP_Page_Down:
106                         gtk_adjustment_set_value(adj, CLAMP(value + page_size, lower, upper - page_size));
107                         return TRUE;
108                 case GDK_Page_Up: case GDK_KP_Page_Up:
109                         gtk_adjustment_set_value(adj, CLAMP(value - page_size, lower, upper - page_size));
110                         return TRUE;
111                 case GDK_Return: case GDK_KP_Enter:
112                         gtk_adjustment_set_value(adj, CLAMP(value + step_increment, lower, upper - page_size));
113                         return TRUE;
114                         /* don't handle "up" and "down", they're used for input history */
115         }
116         
117         return FALSE; /* if the key wasn't handled here, pass it to other handlers */
118 }
119
120 /* Draw the "more" prompt on top of the buffer, after the regular expose event has run */
121 gboolean
122 pager_on_expose(GtkTextView *textview, GdkEventExpose *event, winid_t win)
123 {
124         /* Calculate the position of the 'more' tag */
125         gint promptwidth, promptheight;
126         pango_layout_get_pixel_size(win->pager_layout, &promptwidth, &promptheight);
127
128         gint winx, winy, winwidth, winheight;
129         gdk_window_get_position(event->window, &winx, &winy);
130         gdk_drawable_get_size(GDK_DRAWABLE(event->window), &winwidth, &winheight);
131
132         /* Draw the 'more' tag */
133         GdkGC *context = gdk_gc_new(GDK_DRAWABLE(event->window));
134         /*
135         gdk_draw_layout_with_colors(event->window, context, 
136                 winx + winwidth - promptwidth, 
137                 winy + winheight - promptheight, 
138                 prompt, &white, &red);
139         */
140         gdk_draw_layout(event->window, context, 
141                 winx + winwidth - promptwidth, 
142                 winy + winheight - promptheight, 
143                 win->pager_layout);
144
145         return FALSE; /* Propagate event further */
146 }