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 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);
26 gtk_text_buffer_move_mark(buffer, pager, &newpager);
28 /* Get the buffer coordinates of the pager and the end iter */
29 gtk_text_buffer_get_end_iter(buffer, &end);
30 gtk_text_buffer_get_iter_at_mark(buffer, &newpager, pager);
31 gtk_text_view_get_iter_location(textview, &newpager, &pagerpos);
32 gtk_text_view_get_iter_location(textview, &end, &endpos);
34 g_printerr("View height = %d\n", visiblerect.height);
35 g_printerr("End - Pager = %d\n", endpos.y - pagerpos.y);
37 *view_height = visiblerect.height;
38 *scroll_distance = endpos.y - pagerpos.y;
41 /* Helper function: turn on paging for this textview */
43 start_paging(winid_t win)
45 win->currently_paging = TRUE;
46 g_signal_handler_unblock(win->widget, win->pager_expose_handler);
47 g_signal_handler_unblock(win->widget, win->pager_keypress_handler);
50 /* Helper function: turn off paging for this textview */
52 stop_paging(winid_t win)
54 win->currently_paging = FALSE;
55 g_signal_handler_block(win->widget, win->pager_expose_handler);
56 g_signal_handler_block(win->widget, win->pager_keypress_handler);
59 /* Check whether paging should be done. This function is called inside the
60 * idle handler, after the textview has finished updating. */
62 pager_check(gpointer data)
65 printf("pager check...\n");
66 winid_t win = (winid_t) data;
69 /* Move the pager to the last visible character in the buffer */
70 gint view_height, scroll_distance;
71 move_pager_and_get_scroll_distance( GTK_TEXT_VIEW(win->widget), &view_height, &scroll_distance );
74 /* Paging is unusable when window is too small */
77 if(!win->currently_paging) {
78 if(scroll_distance > view_height) {
82 #if GTK_CHECK_VERSION(2,14,0)
83 gdk_window_invalidate_rect(gtk_widget_get_window(win->widget), NULL, TRUE);
85 gdk_window_invalidate_rect(win->widget->window, NULL, TRUE);
88 else if(scroll_distance > 0) {
89 GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(win->widget));
90 GtkTextMark *end = gtk_text_buffer_get_mark(buffer, "end_position");
93 gtk_text_view_scroll_mark_onscreen(GTK_TEXT_VIEW(win->widget), end);
98 /* Returning FALSE to prevent this function from being called multiple times */
102 /* When the user scrolls up in a textbuffer, start paging. */
104 pager_after_adjustment_changed(GtkAdjustment *adj, winid_t win)
106 /* Move the pager, etc. */
107 gint scroll_distance, view_height;
108 move_pager_and_get_scroll_distance( GTK_TEXT_VIEW(win->widget), &view_height, &scroll_distance );
110 if(scroll_distance > 0 && !win->currently_paging)
112 else if(scroll_distance == 0 && win->currently_paging)
115 /* Refresh the widget so that any extra "more" prompts disappear */
116 gtk_widget_queue_draw(win->widget);
119 /* Handle key press events in the textview while paging is active */
121 pager_on_key_press_event(GtkTextView *textview, GdkEventKey *event, winid_t win)
123 GtkAdjustment *adj = gtk_scrolled_window_get_vadjustment( GTK_SCROLLED_WINDOW(win->frame) );
124 gdouble step_increment, page_size, upper, lower, value;
126 "page-size", &page_size,
127 "step-increment", &step_increment,
133 switch (event->keyval) {
134 case GDK_space: case GDK_KP_Space: case GDK_Page_Down: case GDK_KP_Page_Down:
135 gtk_adjustment_set_value(adj, CLAMP(value + page_size, lower, upper - page_size));
137 case GDK_Page_Up: case GDK_KP_Page_Up:
138 gtk_adjustment_set_value(adj, CLAMP(value - page_size, lower, upper - page_size));
140 case GDK_Return: case GDK_KP_Enter:
141 gtk_adjustment_set_value(adj, CLAMP(value + step_increment, lower, upper - page_size));
143 /* don't handle "up" and "down", they're used for input history */
146 return FALSE; /* if the key wasn't handled here, pass it to other handlers */
149 /* Draw the "more" prompt on top of the buffer, after the regular expose event has run */
151 pager_on_expose(GtkTextView *textview, GdkEventExpose *event, winid_t win)
153 /* Calculate the position of the 'more' tag */
154 gint promptwidth, promptheight;
155 pango_layout_get_pixel_size(win->pager_layout, &promptwidth, &promptheight);
157 gint winx, winy, winwidth, winheight;
158 gdk_window_get_position(event->window, &winx, &winy);
159 gdk_drawable_get_size(GDK_DRAWABLE(event->window), &winwidth, &winheight);
161 /* Draw the 'more' tag */
162 GdkGC *context = gdk_gc_new(GDK_DRAWABLE(event->window));
163 gdk_draw_layout(event->window, context,
164 winx + winwidth - promptwidth,
165 winy + winheight - promptheight,
168 return FALSE; /* Propagate event further */