1 #include <gdk/gdkkeysyms.h>
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
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
13 move_pager_and_get_scroll_distance(GtkTextView *textview, gint *view_height, gint *scroll_distance, gboolean move )
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");
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(
25 visiblerect.x + visiblerect.width,
26 visiblerect.y + visiblerect.height
28 gtk_text_buffer_get_iter_at_mark(buffer, &oldpager, pager);
31 gtk_text_buffer_move_mark(buffer, pager, &newpager);
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);
39 *view_height = visiblerect.height;
40 *scroll_distance = endpos.y - pagerpos.y;
43 /* Helper function: turn on paging for this textview */
45 start_paging(winid_t win)
47 win->currently_paging = TRUE;
48 gtk_widget_show(win->pager);
49 g_signal_handler_unblock(win->widget, win->pager_keypress_handler);
52 /* Helper function: turn off paging for this textview */
54 stop_paging(winid_t win)
56 win->currently_paging = FALSE;
57 gtk_widget_hide(win->pager);
58 g_signal_handler_block(win->widget, win->pager_keypress_handler);
61 /* Helper function: If the adjustment is at its maximum value, stop paging */
63 check_paging(GtkAdjustment *adj, winid_t win)
65 double page_size, upper, value;
67 "page-size", &page_size,
71 if(value + PAGER_FUZZINESS >= upper - page_size && win->currently_paging)
76 pager_on_clicked(GtkButton *pager, winid_t win)
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);
86 /* When the user scrolls up in a textbuffer, start paging. */
88 pager_after_adjustment_changed(GtkAdjustment *adj, winid_t win)
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 );
94 if(scroll_distance > 0 && !win->currently_paging)
99 else if(scroll_distance == 0 && win->currently_paging)
105 check_paging(adj, win);
108 /* Handle key press events in the textview while paging is active */
110 pager_on_key_press_event(GtkTextView *textview, GdkEventKey *event, winid_t win)
112 GtkAdjustment *adj = gtk_scrolled_window_get_vadjustment( GTK_SCROLLED_WINDOW(win->scrolledwindow) );
113 gdouble page_size, upper, lower, value;
115 "page-size", &page_size,
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);
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);
132 case GDK_KEY_End: case GDK_KEY_KP_End:
133 gtk_adjustment_set_value(adj, upper - page_size);
134 check_paging(adj, win);
136 /* don't handle "up" and "down", they're used for input history */
139 return FALSE; /* if the key wasn't handled here, pass it to other handlers */
142 /* Check whether paging should be done. This function is called after the
143 * textview has finished validating text positions. */
145 pager_after_size_allocate(GtkTextView *textview, GdkRectangle *allocation, winid_t win)
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);
152 /* Paging is unusable when window is too small */
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));
158 if(!chimara_glk_get_interactive(glk)) {
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);
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);
174 if(!win->currently_paging) {
175 if(scroll_distance > view_height) {
177 gdk_window_invalidate_rect(gtk_widget_get_window(win->widget), NULL, TRUE);
183 pager_update(winid_t win)
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);