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)
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(textview, &newpager, visiblerect.x + visiblerect.width, visiblerect.y + visiblerect.height);
19 gtk_text_buffer_get_iter_at_mark(buffer, &oldpager, pager);
21 gtk_text_buffer_move_mark(buffer, pager, &newpager);
23 /* Get the buffer coordinates of the pager and the end iter */
24 gtk_text_buffer_get_end_iter(buffer, &end);
25 gtk_text_buffer_get_iter_at_mark(buffer, &newpager, pager);
26 gtk_text_view_get_iter_location(textview, &newpager, &pagerpos);
27 gtk_text_view_get_iter_location(textview, &end, &endpos);
29 //g_printerr("View height = %d\n", visiblerect.height);
30 //g_printerr("End - Pager = %d\n", endpos.y - pagerpos.y);
32 return endpos.y - pagerpos.y;
35 /* Helper function: turn on paging for this textview */
37 start_paging(winid_t win)
39 win->currently_paging = TRUE;
40 g_signal_handler_unblock(win->widget, win->pager_expose_handler);
41 g_signal_handler_unblock(win->widget, win->pager_keypress_handler);
44 /* Helper function: turn off paging for this textview */
46 stop_paging(winid_t win)
48 win->currently_paging = FALSE;
49 g_signal_handler_block(win->widget, win->pager_expose_handler);
50 g_signal_handler_block(win->widget, win->pager_keypress_handler);
53 /* Update the pager position after new text is inserted in the buffer */
55 pager_after_insert_text(GtkTextBuffer *buffer, GtkTextIter *location, gchar *text, gint len, winid_t win)
57 while(gtk_events_pending())
60 /* Move the pager to the last visible character in the buffer */
61 gint scroll_distance = move_pager_and_get_scroll_distance( GTK_TEXT_VIEW(win->widget) );
63 if(scroll_distance > 0 && !win->currently_paging)
68 pager_after_adjustment_changed(GtkAdjustment *adj, winid_t win)
70 while(gtk_events_pending())
73 /* Move the pager, etc. */
74 gint scroll_distance = move_pager_and_get_scroll_distance( GTK_TEXT_VIEW(win->widget) );
76 if(scroll_distance > 0 && !win->currently_paging)
78 else if(scroll_distance == 0 && win->currently_paging)
81 /* Refresh the widget so that any extra "more" prompts disappear */
82 gtk_widget_queue_draw(win->widget);
85 /* Handle key press events in the textview while paging is active */
87 pager_on_key_press_event(GtkTextView *textview, GdkEventKey *event, winid_t win)
89 /*** ALTERNATIVE, POSSIBLY INFERIOR, METHOD OF SCROLLING ***
90 GtkTextMark *pagermark = gtk_text_buffer_get_mark(buffer, "pager_position");
91 gtk_text_view_scroll_to_mark(textview, pagermark, 0.0, TRUE, 0.0, 0.0);
94 GtkAdjustment *adj = gtk_scrolled_window_get_vadjustment( GTK_SCROLLED_WINDOW(win->frame) );
95 gdouble step_increment, page_size, upper, lower, value;
97 "page-size", &page_size,
98 "step-increment", &step_increment,
104 switch (event->keyval) {
105 case GDK_space: case GDK_KP_Space: case GDK_Page_Down: case GDK_KP_Page_Down:
106 gtk_adjustment_set_value(adj, CLAMP(value + page_size, lower, upper - page_size));
108 case GDK_Page_Up: case GDK_KP_Page_Up:
109 gtk_adjustment_set_value(adj, CLAMP(value - page_size, lower, upper - page_size));
111 case GDK_Return: case GDK_KP_Enter:
112 gtk_adjustment_set_value(adj, CLAMP(value + step_increment, lower, upper - page_size));
114 /* don't handle "up" and "down", they're used for input history */
117 return FALSE; /* if the key wasn't handled here, pass it to other handlers */
120 /* Draw the "more" prompt on top of the buffer, after the regular expose event has run */
122 pager_on_expose(GtkTextView *textview, GdkEventExpose *event, winid_t win)
124 /* Calculate the position of the 'more' tag */
125 gint promptwidth, promptheight;
126 pango_layout_get_pixel_size(win->pager_layout, &promptwidth, &promptheight);
128 gint winx, winy, winwidth, winheight;
129 gdk_window_get_position(event->window, &winx, &winy);
130 gdk_drawable_get_size(GDK_DRAWABLE(event->window), &winwidth, &winheight);
132 /* Draw the 'more' tag */
133 GdkGC *context = gdk_gc_new(GDK_DRAWABLE(event->window));
135 gdk_draw_layout_with_colors(event->window, context,
136 winx + winwidth - promptwidth,
137 winy + winheight - promptheight,
138 prompt, &white, &red);
140 gdk_draw_layout(event->window, context,
141 winx + winwidth - promptwidth,
142 winy + winheight - promptheight,
145 return FALSE; /* Propagate event further */