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