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)
11 while( gtk_events_pending() ) {
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(textview, &newpager, visiblerect.x + visiblerect.width, visiblerect.y + visiblerect.height);
23 gtk_text_buffer_get_iter_at_mark(buffer, &oldpager, pager);
25 gtk_text_buffer_move_mark(buffer, pager, &newpager);
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);
33 /*g_printerr("View height = %d\n", visiblerect.height);
34 g_printerr("End - Pager = %d\n", endpos.y - pagerpos.y);*/
36 *view_height = visiblerect.height;
37 *scroll_distance = endpos.y - pagerpos.y;
40 /* Helper function: turn on paging for this textview */
42 start_paging(winid_t win)
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);
49 /* Helper function: turn off paging for this textview */
51 stop_paging(winid_t win)
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);
58 /* Update the pager position after new text is inserted in the buffer and the
59 text view has calculated where it is */
61 pager_after_size_allocate(GtkTextView *view, GtkAllocation *allocation, winid_t win)
63 while(gtk_events_pending())
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 );
71 /* Paging is unusable when window is too small */
74 if(!win->currently_paging) {
75 if(scroll_distance > view_height) {
79 #if GTK_CHECK_VERSION(2,14,0)
80 gdk_window_invalidate_rect(gtk_widget_get_window(win->widget), NULL, TRUE);
82 gdk_window_invalidate_rect(win->widget->window, NULL, TRUE);
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() ) {
96 pager_after_adjustment_changed(GtkAdjustment *adj, winid_t win)
98 while(gtk_events_pending())
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 );
105 if(scroll_distance > 0 && !win->currently_paging)
107 else if(scroll_distance == 0 && win->currently_paging)
110 /* Refresh the widget so that any extra "more" prompts disappear */
111 gtk_widget_queue_draw(win->widget);
114 /* Handle key press events in the textview while paging is active */
116 pager_on_key_press_event(GtkTextView *textview, GdkEventKey *event, winid_t win)
118 GtkAdjustment *adj = gtk_scrolled_window_get_vadjustment( GTK_SCROLLED_WINDOW(win->frame) );
119 gdouble step_increment, page_size, upper, lower, value;
121 "page-size", &page_size,
122 "step-increment", &step_increment,
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));
132 case GDK_Page_Up: case GDK_KP_Page_Up:
133 gtk_adjustment_set_value(adj, CLAMP(value - page_size, lower, upper - page_size));
135 case GDK_Return: case GDK_KP_Enter:
136 gtk_adjustment_set_value(adj, CLAMP(value + step_increment, lower, upper - page_size));
138 /* don't handle "up" and "down", they're used for input history */
141 return FALSE; /* if the key wasn't handled here, pass it to other handlers */
144 /* Draw the "more" prompt on top of the buffer, after the regular expose event has run */
146 pager_on_expose(GtkTextView *textview, GdkEventExpose *event, winid_t win)
148 /* Calculate the position of the 'more' tag */
149 gint promptwidth, promptheight;
150 pango_layout_get_pixel_size(win->pager_layout, &promptwidth, &promptheight);
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);
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,
163 return FALSE; /* Propagate event further */