1 #include <gdk/gdkkeysyms.h>
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
9 move_pager_and_get_scroll_distance(GtkTextView *textview, gint *view_height, gint *scroll_distance, gboolean move )
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");
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(
21 visiblerect.x + visiblerect.width,
22 visiblerect.y + visiblerect.height
24 gtk_text_buffer_get_iter_at_mark(buffer, &oldpager, pager);
27 gtk_text_buffer_move_mark(buffer, pager, &newpager);
29 /* Get the buffer coordinates of the pager and the end iter */
30 gtk_text_buffer_get_end_iter(buffer, &end);
31 gtk_text_buffer_get_iter_at_mark(buffer, &newpager, pager);
32 gtk_text_view_get_iter_location(textview, &newpager, &pagerpos);
33 gtk_text_view_get_iter_location(textview, &end, &endpos);
36 g_printerr("View height = %d\n", visiblerect.height);
37 g_printerr("End - Pager = %d - %d = %d\n", endpos.y, pagerpos.y, endpos.y - pagerpos.y);
40 *view_height = visiblerect.height;
41 *scroll_distance = endpos.y - pagerpos.y;
44 /* Helper function: turn on paging for this textview */
46 start_paging(winid_t win)
48 win->currently_paging = TRUE;
49 g_signal_handler_unblock(win->widget, win->pager_expose_handler);
50 g_signal_handler_unblock(win->widget, win->pager_keypress_handler);
53 /* Helper function: turn off paging for this textview */
55 stop_paging(winid_t win)
57 win->currently_paging = FALSE;
58 g_signal_handler_block(win->widget, win->pager_expose_handler);
59 g_signal_handler_block(win->widget, win->pager_keypress_handler);
62 /* When the user scrolls up in a textbuffer, start paging. */
64 pager_after_adjustment_changed(GtkAdjustment *adj, winid_t win)
66 /* Move the pager, etc. */
67 gint scroll_distance, view_height;
68 move_pager_and_get_scroll_distance( GTK_TEXT_VIEW(win->widget), &view_height, &scroll_distance, TRUE );
70 if(scroll_distance > 0 && !win->currently_paging)
72 else if(scroll_distance == 0 && win->currently_paging)
75 /* Refresh the widget so that any extra "more" prompts disappear */
76 gtk_widget_queue_draw(win->widget);
79 /* Handle key press events in the textview while paging is active */
81 pager_on_key_press_event(GtkTextView *textview, GdkEventKey *event, winid_t win)
83 GtkAdjustment *adj = gtk_scrolled_window_get_vadjustment( GTK_SCROLLED_WINDOW(win->frame) );
84 gdouble page_size, upper, lower, value;
86 "page-size", &page_size,
92 switch (event->keyval) {
93 case GDK_space: case GDK_KP_Space:
94 case GDK_Page_Down: case GDK_KP_Page_Down:
95 case GDK_Return: case GDK_KP_Enter:
96 gtk_adjustment_set_value(adj, CLAMP(value + page_size, lower, upper - page_size));
98 case GDK_Page_Up: case GDK_KP_Page_Up:
99 gtk_adjustment_set_value(adj, CLAMP(value - page_size, lower, upper - page_size));
101 /* don't handle "up" and "down", they're used for input history */
104 return FALSE; /* if the key wasn't handled here, pass it to other handlers */
107 /* Draw the "more" prompt on top of the buffer, after the regular expose event has run */
109 pager_on_expose(GtkTextView *textview, GdkEventExpose *event, winid_t win)
111 /* Calculate the position of the 'more' tag */
112 gint promptwidth, promptheight;
113 pango_layout_get_pixel_size(win->pager_layout, &promptwidth, &promptheight);
115 gint winx, winy, winwidth, winheight;
116 gdk_window_get_position(event->window, &winx, &winy);
117 gdk_drawable_get_size(GDK_DRAWABLE(event->window), &winwidth, &winheight);
119 /* Draw the 'more' tag */
120 GdkGC *context = gdk_gc_new(GDK_DRAWABLE(event->window));
121 gdk_draw_layout(event->window, context,
122 winx + winwidth - promptwidth,
123 winy + winheight - promptheight,
126 return FALSE; /* Propagate event further */
129 /* Check whether paging should be done. This function is called after the
130 * textview has finished validating text positions. */
132 pager_after_size_request(GtkTextView *textview, GtkRequisition *requisition, winid_t win)
134 /* Move the pager to the last visible character in the buffer */
135 gint view_height, scroll_distance;
136 move_pager_and_get_scroll_distance(GTK_TEXT_VIEW(win->widget), &view_height, &scroll_distance, FALSE);
139 /* Paging is unusable when window is too small */
142 /* If not in interactive mode, then just scroll to the bottom. */
143 ChimaraGlk *glk = CHIMARA_GLK(gtk_widget_get_ancestor(GTK_WIDGET(textview), CHIMARA_TYPE_GLK));
145 if(!chimara_glk_get_interactive(glk)) {
147 gtk_text_buffer_get_end_iter(gtk_text_view_get_buffer(textview), &end);
148 gtk_text_view_scroll_to_iter(textview, &end, 0.0, TRUE, 0.0, 0.0);
152 /* Scroll past text already read by user. This is automatic scrolling, so disable the pager_ajustment_handler
153 * first, that acts on the belief the scolling is performed by the user. */
154 GtkAdjustment *adj = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(win->frame));
155 g_signal_handler_block(adj, win->pager_adjustment_handler);
156 GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(win->widget));
157 GtkTextMark *pager_position = gtk_text_buffer_get_mark(buffer, "pager_position");
158 gtk_text_view_scroll_to_mark(GTK_TEXT_VIEW(win->widget), pager_position, 0.0, TRUE, 0.0, 0.0);
159 g_signal_handler_unblock(adj, win->pager_adjustment_handler);
161 if(!win->currently_paging) {
162 if(scroll_distance > view_height) {
166 #if GTK_CHECK_VERSION(2,14,0)
167 gdk_window_invalidate_rect(gtk_widget_get_window(win->widget), NULL, TRUE);
169 gdk_window_invalidate_rect(win->widget->window, NULL, TRUE);
176 pager_update(winid_t win)
178 GtkTextIter input_iter;
179 GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(win->widget));
180 GtkTextMark *input_position = gtk_text_buffer_get_mark(buffer, "input_position");
181 GtkTextMark *pager_position = gtk_text_buffer_get_mark(buffer, "pager_position");
182 gtk_text_buffer_get_iter_at_mark(buffer, &input_iter, input_position);
183 gtk_text_buffer_move_mark(buffer, pager_position, &input_iter);