Debian Codegraph @

src/tools/dlgedit/gui_scrollable.cc

raw
1 /*
2 Copyright (C) 2009 Kai Sterker <[email protected]>
3 Part of the Adonthell Project <http://adonthell.nongnu.org>
4
5 Dlgedit is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 Dlgedit 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
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with Dlgedit. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /**
20 * @file common/gui_scrollable.cc
21 *
22 * @author Kai Sterker
23 * @brief Basic functionality for automated scolling.
24 */
25
26 #include <gtk/gtk.h>
27
28 #include "gui_scrollable.h"
29
30 /// the area around the view sensitive to scrolling
31 #define ACTIVE_BORDER 40
32 /// amount of pixels the view is moved per iteration
33 #define SCROLL_OFFSET 15
34
35 /// width and length of the cursor
36 #define SCROLL_CURSOR_SIZE (24)
37 /// a cursor to indicate scrolling
38 static const guint8 SCROLL_CURSOR_pixel_data[24 * 24 * 4 + 1] =
39 "\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377"
40 "\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377"
41 "\377\0\0\0\0\377\0\0\0\377\377\377\377\0\377\377\377\0\377\377\377\0\377\377"
42 "\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377"
43 "\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377"
44 "\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377"
45 "\377\0\377\377\377\0\0\0\0\377\200\200\200\377\200\200\200\377\0\0\0\377\377"
46 "\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377"
47 "\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377"
48 "\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377"
49 "\377\377\0\377\377\377\0\377\377\377\0\0\0\0\377\200\200\200\377\377\377\377"
50 "\377\377\377\377\377\200\200\200\377\0\0\0\377\377\377\377\0\377\377\377\0"
51 "\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377"
52 "\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377"
53 "\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\0\0\0\377\200"
54 "\200\200\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377"
55 "\200\200\200\377\0\0\0\377\377\377\377\0\377\377\377\0\377\377\377\0\377\377"
56 "\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377"
57 "\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377"
58 "\377\377\0\0\0\0\377\0\0\0\377\0\0\0\377\0\0\0\377\0\0\0\377\0\0\0\377\0\0"
59 "\0\377\0\0\0\377\0\0\0\377\0\0\0\377\377\377\377\0\377\377\377\0\377\377\377"
60 "\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377"
61 "\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377"
62 "\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377"
63 "\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377"
64 "\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377"
65 "\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377"
66 "\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377"
67 "\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377"
68 "\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377"
69 "\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377"
70 "\377\377\0\377\377\377\0\377\377\377\0\0\0\0\377\377\377\377\0\377\377\377"
71 "\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377"
72 "\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377"
73 "\377\0\377\377\377\0\0\0\0\377\377\377\377\0\377\377\377\0\377\377\377\0\377"
74 "\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\0\0\0\377\0\0\0\377\377"
75 "\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377"
76 "\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377"
77 "\0\377\377\377\0\377\377\377\0\377\377\377\0\0\0\0\377\0\0\0\377\377\377\377"
78 "\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\0\0\0\377\200\200"
79 "\200\377\0\0\0\344\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0"
80 "\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377"
81 "\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\0\0\0\377"
82 "\200\200\200\377\0\0\0\377\377\377\377\0\377\377\377\0\377\377\377\0\0\0\0"
83 "\377\200\200\200\377\377\377\377\377\0\0\0\377\377\377\377\0\377\377\377\0"
84 "\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\0\0\0\377\0\0\0\377"
85 "\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377"
86 "\377\377\0\0\0\0\377\377\377\377\377\200\200\200\377\0\0\0\377\377\377\377"
87 "\0\0\0\0\377\200\200\200\377\377\377\377\377\377\377\377\377\0\0\0\377\377"
88 "\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\0\0\0\377"
89 "\377\377\377\377\377\377\377\377\0\0\0\377\377\377\377\0\377\377\377\0\377"
90 "\377\377\0\377\377\377\0\377\377\377\0\0\0\0\377\377\377\377\377\377\377\377"
91 "\377\200\200\200\377\0\0\0\377\0\0\0\377\200\200\200\377\377\377\377\377\377"
92 "\377\377\377\0\0\0\377\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377"
93 "\0\377\377\377\0\0\0\0\377\377\377\377\377\377\377\377\377\0\0\0\377\377\377"
94 "\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\0\0\0\377\377"
95 "\377\377\377\377\377\377\377\200\200\200\377\0\0\0\377\377\377\377\0\0\0\0"
96 "\377\200\200\200\377\377\377\377\377\0\0\0\377\377\377\377\0\377\377\377\0"
97 "\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\0\0\0\377\0\0\0\377"
98 "\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377"
99 "\377\377\0\0\0\0\377\377\377\377\377\200\200\200\377\0\0\0\377\377\377\377"
100 "\0\377\377\377\0\377\377\377\0\0\0\0\377\200\200\200\377\0\0\0\302\377\377"
101 "\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377"
102 "\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377"
103 "\377\377\0\377\377\377\0\377\377\377\0\0\0\0\377\200\200\200\377\0\0\0\377"
104 "\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\0\0"
105 "\0\350\0\0\0\345\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377"
106 "\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377"
107 "\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\0\0\0\377\0"
108 "\0\0\377\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377"
109 "\0\377\377\377\0\377\377\377\0\0\0\0\377\377\377\377\0\377\377\377\0\377\377"
110 "\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377"
111 "\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377"
112 "\377\377\0\0\0\0\377\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377"
113 "\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377"
114 "\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377"
115 "\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377"
116 "\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377"
117 "\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377"
118 "\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377"
119 "\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377"
120 "\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377"
121 "\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377"
122 "\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377"
123 "\377\377\0\377\377\377\0\0\0\0\377\0\0\0\377\0\0\0\377\0\0\0\377\0\0\0\377"
124 "\0\0\0\377\0\0\0\377\0\0\0\377\0\0\0\377\0\0\0\377\377\377\377\0\377\377\377"
125 "\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377"
126 "\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377"
127 "\377\0\377\377\377\0\377\377\377\0\0\0\0\377\200\200\200\377\377\377\377\377"
128 "\377\377\377\377\377\377\377\377\377\377\377\377\200\200\200\377\0\0\0\377"
129 "\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377"
130 "\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377"
131 "\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377"
132 "\0\377\377\377\0\0\0\0\377\200\200\200\377\377\377\377\377\377\377\377\377"
133 "\200\200\200\377\0\0\0\377\377\377\377\0\377\377\377\0\377\377\377\0\377\377"
134 "\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377"
135 "\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377"
136 "\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\0\0\0\377"
137 "\200\200\200\377\200\200\200\377\0\0\0\377\377\377\377\0\377\377\377\0\377"
138 "\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377"
139 "\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377"
140 "\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377"
141 "\377\377\0\377\377\377\0\377\377\377\0\0\0\0\377\0\0\0\377\377\377\377\0\377"
142 "\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377"
143 "\377\0\377\377\377\0\377\377\377\0\377\377\377\0\377\377\377\0";
144
145 /**
146 * Callback for autoscrolling the graph view.
147 *
148 * Once 'auto-scrolling' is activated, this function is called every
149 * 10th of a second until it returns FALSE
150 */
151 int on_scroll_graph (gpointer data)
152 {
153 int x, y;
154 static int delay = 0;
155 Scrollable *scroller = (Scrollable *) data;
156 GtkWidget *widget = scroller->drawingArea ();
157
158 // check that parent window is active
159 GtkWidget *window = gtk_widget_get_ancestor (widget, GTK_TYPE_WINDOW);
160 if (window != NULL && !gtk_window_is_active (GTK_WINDOW(window)))
161 {
162 scroller->stopScrolling ();
163 delay = 0;
164 return FALSE;
165 }
166
167 // get the present cursor position (relative to the graph)
168 gtk_widget_get_pointer (widget, &x, &y);
169
170 GtkAllocation allocation;
171 gtk_widget_get_allocation (widget, &allocation);
172
173 // stop scrolling if outside widget or too far from widget's border
174 if (x < 0 || x > allocation.width ||
175 y < 0 || y > allocation.height ||
176 !scroller->isScrolling ())
177 {
178 scroller->stopScrolling ();
179 delay = 0;
180 return FALSE;
181 }
182
183 // wait approx. 0.8 seconds before starting to scroll
184 if (delay++ < 5) return TRUE;
185
186 // move the view
187 scroller->scroll ();
188
189 return TRUE;
190 }
191
192
193 // ctor
194 Scrollable::Scrollable()
195 {
196 scrolling = false;
197 scroll_offset.x = 0;
198 scroll_offset.y = 0;
199
200 createScrollCursor();
201 }
202
203 // dtor
204 Scrollable::~Scrollable()
205 {
206 gdk_cursor_unref (scrollCursor);
207 }
208
209 // prepare everything for 'auto-scrolling' (TM) ;-)
210 void Scrollable::prepareScrolling (const GdkPoint & point)
211 {
212 if (!scrollingAllowed ()) return;
213
214 int scroll_x = 0;
215 int scroll_y = 0;
216
217 GtkWidget *widget = drawingArea ();
218
219 GtkAllocation allocation;
220 gtk_widget_get_allocation (widget, &allocation);
221
222 // set scrolling offset and direction
223 if (point.x < ACTIVE_BORDER) scroll_x = SCROLL_OFFSET;
224 if (point.y < ACTIVE_BORDER) scroll_y = SCROLL_OFFSET;
225 if (point.x + ACTIVE_BORDER > allocation.width) scroll_x = -SCROLL_OFFSET;
226 if (point.y + ACTIVE_BORDER > allocation.height) scroll_y = -SCROLL_OFFSET;
227
228 // enable scrolling
229 if (scroll_x || scroll_y)
230 {
231 scroll_offset.x = scroll_x;
232 scroll_offset.y = scroll_y;
233
234 if (!scrolling)
235 {
236 GdkWindow *window = gtk_widget_get_window (widget);
237 gdk_window_set_cursor (window, scrollCursor);
238
239 scrolling = true;
240
241 // make sure that drawing has priority over scrolling
242 g_timeout_add_full (G_PRIORITY_DEFAULT_IDLE, 100, on_scroll_graph, this, NULL);
243 }
244 }
245 else stopScrolling();
246 }
247
248 // scrolling or not?
249 bool Scrollable::isScrolling () const
250 {
251 return scrolling;
252 }
253
254 // stop scrolling
255 void Scrollable::stopScrolling ()
256 {
257 GdkWindow *window = gtk_widget_get_window (drawingArea());
258 gdk_window_set_cursor (window, NULL);
259
260 scrolling = false;
261 }
262
263 // the cursor displayed while in the scrolling region
264 void Scrollable::createScrollCursor()
265 {
266 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_data (SCROLL_CURSOR_pixel_data,
267 GDK_COLORSPACE_RGB, TRUE, 8,
268 SCROLL_CURSOR_SIZE, SCROLL_CURSOR_SIZE, SCROLL_CURSOR_SIZE * 4,
269 NULL, NULL);
270 scrollCursor = gdk_cursor_new_from_pixbuf (gdk_display_get_default(),
271 pixbuf, SCROLL_CURSOR_SIZE / 2, SCROLL_CURSOR_SIZE / 2);
272 g_object_unref (pixbuf);
273 }