1 #define __SP_ZOOM_CONTEXT_C__
3 /*
4 * Handy zooming tool
5 *
6 * Authors:
7 * Lauris Kaplinski <lauris@kaplinski.com>
8 * Frank Felfe <innerspace@iname.com>
9 * bulia byak <buliabyak@users.sf.net>
10 *
11 * Copyright (C) 1999-2002 Authors
12 *
13 * Released under GNU GPL, read the file 'COPYING' for more information
14 */
17 #include <gdk/gdkkeysyms.h>
19 #include "macros.h"
20 #include "rubberband.h"
21 #include "desktop.h"
22 #include "pixmaps/cursor-zoom.xpm"
23 #include "pixmaps/cursor-zoom-out.xpm"
24 #include "preferences.h"
26 #include "zoom-context.h"
28 static void sp_zoom_context_class_init(SPZoomContextClass *klass);
29 static void sp_zoom_context_init(SPZoomContext *zoom_context);
30 static void sp_zoom_context_setup(SPEventContext *ec);
31 static void sp_zoom_context_finish (SPEventContext *ec);
33 static gint sp_zoom_context_root_handler(SPEventContext *event_context, GdkEvent *event);
34 static gint sp_zoom_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
36 static SPEventContextClass *parent_class;
38 static gint xp = 0, yp = 0; // where drag started
39 static gint tolerance = 0;
40 static bool within_tolerance = false;
41 static bool escaped;
43 GType sp_zoom_context_get_type(void)
44 {
45 static GType type = 0;
47 if (!type) {
48 GTypeInfo info = {
49 sizeof(SPZoomContextClass),
50 NULL, NULL,
51 (GClassInitFunc) sp_zoom_context_class_init,
52 NULL, NULL,
53 sizeof(SPZoomContext),
54 4,
55 (GInstanceInitFunc) sp_zoom_context_init,
56 NULL, /* value_table */
57 };
58 type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPZoomContext", &info, (GTypeFlags) 0);
59 }
61 return type;
62 }
64 static void sp_zoom_context_class_init(SPZoomContextClass *klass)
65 {
66 SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
68 parent_class = (SPEventContextClass*) g_type_class_peek_parent(klass);
70 event_context_class->setup = sp_zoom_context_setup;
71 event_context_class->finish = sp_zoom_context_finish;
73 event_context_class->root_handler = sp_zoom_context_root_handler;
74 event_context_class->item_handler = sp_zoom_context_item_handler;
75 }
77 static void sp_zoom_context_init (SPZoomContext *zoom_context)
78 {
79 SPEventContext *event_context = SP_EVENT_CONTEXT(zoom_context);
81 event_context->cursor_shape = cursor_zoom_xpm;
82 event_context->hot_x = 6;
83 event_context->hot_y = 6;
84 }
86 static void
87 sp_zoom_context_finish (SPEventContext *ec)
88 {
89 ec->enableGrDrag(false);
90 }
92 static void sp_zoom_context_setup(SPEventContext *ec)
93 {
94 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
95 if (prefs->getBool("/tools/zoom/selcue")) {
96 ec->enableSelectionCue();
97 }
98 if (prefs->getBool("/tools/zoom/gradientdrag")) {
99 ec->enableGrDrag();
100 }
102 if (((SPEventContextClass *) parent_class)->setup) {
103 ((SPEventContextClass *) parent_class)->setup(ec);
104 }
105 }
107 static gint sp_zoom_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
108 {
109 gint ret = FALSE;
111 if (((SPEventContextClass *) parent_class)->item_handler) {
112 ret = ((SPEventContextClass *) parent_class)->item_handler (event_context, item, event);
113 }
115 return ret;
116 }
118 static gint sp_zoom_context_root_handler(SPEventContext *event_context, GdkEvent *event)
119 {
120 SPDesktop *desktop = event_context->desktop;
121 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
122 tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
123 double const zoom_inc = prefs->getDoubleLimited("/options/zoomincrement/value", M_SQRT2, 1.01, 10);
125 gint ret = FALSE;
127 switch (event->type) {
128 case GDK_BUTTON_PRESS:
129 {
130 Geom::Point const button_w(event->button.x, event->button.y);
131 Geom::Point const button_dt(desktop->w2d(button_w));
132 if (event->button.button == 1 && !event_context->space_panning) {
133 // save drag origin
134 xp = (gint) event->button.x;
135 yp = (gint) event->button.y;
136 within_tolerance = true;
138 Inkscape::Rubberband::get(desktop)->start(desktop, button_dt);
140 escaped = false;
142 ret = TRUE;
143 } else if (event->button.button == 3) {
144 double const zoom_rel( (event->button.state & GDK_SHIFT_MASK)
145 ? zoom_inc
146 : 1 / zoom_inc );
147 desktop->zoom_relative_keep_point(button_dt, zoom_rel);
148 ret = TRUE;
149 }
150 break;
151 }
153 case GDK_MOTION_NOTIFY:
154 if (event->motion.state & GDK_BUTTON1_MASK && !event_context->space_panning) {
155 ret = TRUE;
157 if ( within_tolerance
158 && ( abs( (gint) event->motion.x - xp ) < tolerance )
159 && ( abs( (gint) event->motion.y - yp ) < tolerance ) ) {
160 break; // do not drag if we're within tolerance from origin
161 }
162 // Once the user has moved farther than tolerance from the original location
163 // (indicating they intend to move the object, not click), then always process the
164 // motion notify coordinates as given (no snapping back to origin)
165 within_tolerance = false;
167 Geom::Point const motion_w(event->motion.x, event->motion.y);
168 Geom::Point const motion_dt(desktop->w2d(motion_w));
169 Inkscape::Rubberband::get(desktop)->move(motion_dt);
170 gobble_motion_events(GDK_BUTTON1_MASK);
171 }
172 break;
174 case GDK_BUTTON_RELEASE:
175 {
176 Geom::Point const button_w(event->button.x, event->button.y);
177 Geom::Point const button_dt(desktop->w2d(button_w));
178 if ( event->button.button == 1 && !event_context->space_panning) {
179 Geom::OptRect const b = Inkscape::Rubberband::get(desktop)->getRectangle();
180 if (b && !within_tolerance) {
181 desktop->set_display_area(*b, 10);
182 } else if (!escaped) {
183 double const zoom_rel( (event->button.state & GDK_SHIFT_MASK)
184 ? 1 / zoom_inc
185 : zoom_inc );
186 desktop->zoom_relative_keep_point(button_dt, zoom_rel);
187 }
188 ret = TRUE;
189 }
190 Inkscape::Rubberband::get(desktop)->stop();
191 xp = yp = 0;
192 escaped = false;
193 break;
194 }
195 case GDK_KEY_PRESS:
196 switch (get_group0_keyval (&event->key)) {
197 case GDK_Escape:
198 Inkscape::Rubberband::get(desktop)->stop();
199 xp = yp = 0;
200 escaped = true;
201 ret = TRUE;
202 break;
203 case GDK_Up:
204 case GDK_Down:
205 case GDK_KP_Up:
206 case GDK_KP_Down:
207 // prevent the zoom field from activation
208 if (!MOD__CTRL_ONLY)
209 ret = TRUE;
210 break;
211 case GDK_Shift_L:
212 case GDK_Shift_R:
213 event_context->cursor_shape = cursor_zoom_out_xpm;
214 sp_event_context_update_cursor(event_context);
215 break;
216 default:
217 break;
218 }
219 break;
220 case GDK_KEY_RELEASE:
221 switch (get_group0_keyval (&event->key)) {
222 case GDK_Shift_L:
223 case GDK_Shift_R:
224 event_context->cursor_shape = cursor_zoom_xpm;
225 sp_event_context_update_cursor(event_context);
226 break;
227 default:
228 break;
229 }
230 break;
231 default:
232 break;
233 }
235 if (!ret) {
236 if (((SPEventContextClass *) parent_class)->root_handler) {
237 ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
238 }
239 }
241 return ret;
242 }
244 /*
245 Local Variables:
246 mode:c++
247 c-file-style:"stroustrup"
248 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
249 indent-tabs-mode:nil
250 fill-column:99
251 End:
252 */
253 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :