chiark / gitweb /
Don't attempt to build debs for dapper (which is where the
[disorder] / disobedience / autoscroll.c
CommitLineData
df73cc67
RK
1/* Derived from gtktreeview.c
2 * Copyright (C) 2000 Red Hat, Inc., Jonathan Blandford <jrb@redhat.com>
3 * Portions copyright (C) 2009 Richard Kettlewell
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
19 */
20/** @file disobedience/autoscroll.c
21 * @brief Automatic scrolling of a GtkTreeView
22 *
23 * GTK+ doesn't expose the automatic scrolling support if you don't use its
24 * high-level treeview drag+drop features.
25 *
26 * Adapted from GTK+ upstream as of commit
27 * 7fda8e6378d90bc8cf670ffe9dea682911e5e241.
28 */
29
30#include <config.h>
31
32#include <glib.h>
33#include <gtk/gtk.h>
34
35#include "autoscroll.h"
36
37/** @brief Object data key used to track the autoscroll timeout */
38#define AUTOSCROLL_KEY "autoscroll.greenend.org.uk"
39
40/** @brief Controls size of edge region that provokes scrolling
41 *
42 * Actually this is half the size of the scroll region. In isolation this may
43 * seem bizarre, but GTK+ uses the value internally for other purposes.
44 */
45#define SCROLL_EDGE_SIZE 15
46
47/** @brief Called from time to time to check whether auto-scrolling is needed
48 * @param data The GtkTreeView
49 * @return TRUE, to keep on truckin'
50 */
51static gboolean autoscroll_timeout(gpointer data) {
52 GtkTreeView *tree_view = data;
53 GdkRectangle visible_rect;
54 gint wx, wy, tx, ty;
55 gint offset;
56 gfloat value;
57
58 /* First we must find the pointer Y position in tree coordinates. GTK+
59 * natively knows what the bin window is and can get the pointer in bin
60 * coords and convert to tree coords. But there is no published way for us
61 * to find the bin window, so we must start in widget coords. */
62 gtk_widget_get_pointer(GTK_WIDGET(tree_view), &wx, &wy);
63 //fprintf(stderr, "widget coords: %d, %d\n", wx, wy);
64 gtk_tree_view_convert_widget_to_tree_coords(tree_view, wx, wy, &tx, &ty);
65 //fprintf(stderr, "tree coords: %d, %d\n", tx, ty);
66
67 gtk_tree_view_get_visible_rect (tree_view, &visible_rect);
68
69 /* see if we are near the edge. */
70 offset = ty - (visible_rect.y + 2 * SCROLL_EDGE_SIZE);
71 if (offset > 0)
72 {
73 offset = ty - (visible_rect.y + visible_rect.height - 2 * SCROLL_EDGE_SIZE);
74 if (offset < 0)
75 return TRUE;
76 }
77
78 GtkAdjustment *vadjustment = gtk_tree_view_get_vadjustment(tree_view);
79
80 value = CLAMP (vadjustment->value + offset, 0.0,
81 vadjustment->upper - vadjustment->page_size);
82 gtk_adjustment_set_value (vadjustment, value);
83
84 return TRUE;
85}
86
87/** @brief Enable autoscrolling
88 * @param tree_view Tree view to enable autoscrolling
89 *
90 * It's harmless to call this if autoscrolling is already enabled.
91 *
92 * It's up to you to cancel the callback when no longer required (including
93 * object destruction).
94 */
95void autoscroll_add(GtkTreeView *tree_view) {
96 guint *scrolldata = g_object_get_data(G_OBJECT(tree_view), AUTOSCROLL_KEY);
97 if(!scrolldata) {
98 /* Set up the callback */
99 scrolldata = g_new(guint, 1);
100 *scrolldata = gdk_threads_add_timeout(150, autoscroll_timeout, tree_view);
101 g_object_set_data(G_OBJECT(tree_view), AUTOSCROLL_KEY, scrolldata);
102 //fprintf(stderr, "autoscroll enabled\n");
103 }
104}
105
106/** @brief Disable autoscrolling
107 * @param tree_view Tree view to enable autoscrolling
108 *
109 * It's harmless to call this if autoscrolling is not enabled.
110 */
111void autoscroll_remove(GtkTreeView *tree_view) {
112 guint *scrolldata = g_object_get_data(G_OBJECT(tree_view), AUTOSCROLL_KEY);
113 if(scrolldata) {
114 g_object_set_data(G_OBJECT(tree_view), AUTOSCROLL_KEY, NULL);
115 g_source_remove(*scrolldata);
116 g_free(scrolldata);
117 //fprintf(stderr, "autoscroll disabled\n");
118 }
119}
120
121/*
122Local Variables:
123c-basic-offset:2
124comment-column:40
125fill-column:79
126indent-tabs-mode:nil
127End:
128*/