Remove workaround for broken package in Fedora 19
[projects/chimara/chimara.git] / libchimara / pager.c
1 #include <gdk/gdkkeysyms.h>
2
3 #include "pager.h"
4
5 /* Not sure if necessary, but this is the margin within which the pager will
6 stop paging if it's close to the end of the text buffer */
7 #define PAGER_FUZZINESS 1.0
8
9 /* Helper function: move the pager to the last visible position in the buffer,
10  and return the distance between the pager and the end of the buffer in buffer
11  coordinates */
12 static void
13 move_pager_and_get_scroll_distance(GtkTextView *textview, gint *view_height, gint *scroll_distance, gboolean move )
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(
23                 textview,
24                 &newpager,
25                 visiblerect.x + visiblerect.width,
26                 visiblerect.y + visiblerect.height
27         );
28         gtk_text_buffer_get_iter_at_mark(buffer, &oldpager, pager);
29         
30         if(move)
31                 gtk_text_buffer_move_mark(buffer, pager, &newpager);
32
33         /* Get the buffer coordinates of the pager and the end iter */
34         gtk_text_buffer_get_end_iter(buffer, &end);
35         gtk_text_buffer_get_iter_at_mark(buffer, &newpager, pager);
36         gtk_text_view_get_iter_location(textview, &newpager, &pagerpos);
37         gtk_text_view_get_iter_location(textview, &end, &endpos);
38
39         *view_height = visiblerect.height;
40         *scroll_distance = endpos.y - pagerpos.y;
41 }
42
43 /* Helper function: turn on paging for this textview */
44 static void
45 start_paging(winid_t win)
46 {
47         win->currently_paging = TRUE;
48         gtk_widget_show(win->pager);
49         g_signal_handler_unblock(win->widget, win->pager_keypress_handler);
50 }
51
52 /* Helper function: turn off paging for this textview */
53 static void
54 stop_paging(winid_t win)
55 {
56         win->currently_paging = FALSE;
57         gtk_widget_hide(win->pager);
58         g_signal_handler_block(win->widget, win->pager_keypress_handler);
59 }
60
61 /* Helper function: If the adjustment is at its maximum value, stop paging */
62 static void
63 check_paging(GtkAdjustment *adj, winid_t win)
64 {
65         double page_size, upper, value;
66         g_object_get(adj,
67                 "page-size", &page_size,
68                 "upper", &upper,
69                 "value", &value,
70                 NULL);
71         if(value + PAGER_FUZZINESS >= upper - page_size && win->currently_paging)
72                 stop_paging(win);
73 }
74
75 void
76 pager_on_clicked(GtkButton *pager, winid_t win)
77 {
78         GtkAdjustment *adj = gtk_scrolled_window_get_vadjustment( GTK_SCROLLED_WINDOW(win->scrolledwindow) );
79         double upper = gtk_adjustment_get_upper(adj);
80         gtk_adjustment_set_value(adj, upper);
81         check_paging(adj, win);
82         /* Give the focus back to the text view */
83         gtk_widget_grab_focus(win->widget);
84 }
85
86 /* When the user scrolls up in a textbuffer, start paging. */
87 void
88 pager_after_adjustment_changed(GtkAdjustment *adj, winid_t win)
89 {
90         /* Move the pager, etc. */
91         gint scroll_distance, view_height;
92         move_pager_and_get_scroll_distance( GTK_TEXT_VIEW(win->widget), &view_height, &scroll_distance, TRUE );
93
94         if(scroll_distance > 0 && !win->currently_paging)
95         {
96                 start_paging(win);
97                 return;
98         }
99         else if(scroll_distance == 0 && win->currently_paging)
100         {
101                 stop_paging(win);
102                 return;
103         }
104
105         check_paging(adj, win);
106 }
107
108 /* Handle key press events in the textview while paging is active */
109 gboolean
110 pager_on_key_press_event(GtkTextView *textview, GdkEventKey *event, winid_t win)
111 {
112         GtkAdjustment *adj = gtk_scrolled_window_get_vadjustment( GTK_SCROLLED_WINDOW(win->scrolledwindow) );
113         gdouble page_size, upper, lower, value;
114         g_object_get(adj, 
115                 "page-size", &page_size,
116                 "upper", &upper,
117                 "lower", &lower,
118                 "value", &value,
119                 NULL);
120         
121         switch (event->keyval) {
122                 case GDK_KEY_space: case GDK_KEY_KP_Space:
123                 case GDK_KEY_Page_Down: case GDK_KEY_KP_Page_Down:
124                 case GDK_KEY_Return: case GDK_KEY_KP_Enter:
125                         gtk_adjustment_set_value(adj, CLAMP(value + page_size, lower, upper - page_size));
126                         check_paging(adj, win);
127                         return TRUE;
128                 case GDK_KEY_Page_Up: case GDK_KEY_KP_Page_Up:
129                         gtk_adjustment_set_value(adj, CLAMP(value - page_size, lower, upper - page_size));
130                         check_paging(adj, win);
131                         return TRUE;
132                 case GDK_KEY_End: case GDK_KEY_KP_End:
133                         gtk_adjustment_set_value(adj, upper - page_size);
134                         check_paging(adj, win);
135                         return TRUE;
136                         /* don't handle "up" and "down", they're used for input history */
137         }
138         
139         return FALSE; /* if the key wasn't handled here, pass it to other handlers */
140 }
141
142 /* Check whether paging should be done. This function is called after the
143  * textview has finished validating text positions. */
144 void 
145 pager_after_size_allocate(GtkTextView *textview, GdkRectangle *allocation, winid_t win)
146 {
147         /* Move the pager to the last visible character in the buffer */
148         gint view_height, scroll_distance; 
149         move_pager_and_get_scroll_distance(GTK_TEXT_VIEW(win->widget), &view_height, &scroll_distance, FALSE);
150
151         if(view_height <= 1)
152                 /* Paging is unusable when window is too small */
153                 return;
154
155         /* If not in interactive mode, then just scroll to the bottom. */
156         ChimaraGlk *glk = CHIMARA_GLK(gtk_widget_get_ancestor(GTK_WIDGET(textview), CHIMARA_TYPE_GLK));
157         g_assert(glk);
158         if(!chimara_glk_get_interactive(glk)) {
159                 GtkTextIter end;
160                 gtk_text_buffer_get_end_iter(gtk_text_view_get_buffer(textview), &end);
161                 gtk_text_view_scroll_to_iter(textview, &end, 0.0, TRUE, 0.0, 0.0);
162                 return;
163         }
164         
165         /* Scroll past text already read by user. This is automatic scrolling, so disable the pager_ajustment_handler
166          * first, that acts on the belief the scolling is performed by the user. */
167         GtkAdjustment *adj = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(win->scrolledwindow));
168         g_signal_handler_block(adj, win->pager_adjustment_handler);
169         GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(win->widget));
170         GtkTextMark *pager_position = gtk_text_buffer_get_mark(buffer, "pager_position");
171         gtk_text_view_scroll_to_mark(GTK_TEXT_VIEW(win->widget), pager_position, 0.0, TRUE, 0.0, 0.0);
172         g_signal_handler_unblock(adj, win->pager_adjustment_handler);
173         
174         if(!win->currently_paging) {
175                 if(scroll_distance > view_height) {
176                         start_paging(win);
177                         gdk_window_invalidate_rect(gtk_widget_get_window(win->widget), NULL, TRUE);
178                 }
179         }
180 }
181
182 void
183 pager_update(winid_t win)
184 {
185         GtkTextIter input_iter;
186         GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(win->widget));
187         GtkTextMark *input_position = gtk_text_buffer_get_mark(buffer, "input_position");
188         GtkTextMark *pager_position = gtk_text_buffer_get_mark(buffer, "pager_position");
189         gtk_text_buffer_get_iter_at_mark(buffer, &input_iter, input_position);
190         gtk_text_buffer_move_mark(buffer, pager_position, &input_iter);
191 }