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 "prefs-utils.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 if (prefs_get_int_attribute("tools.zoom", "selcue", 0) != 0) {
95 ec->enableSelectionCue();
96 }
97 if (prefs_get_int_attribute("tools.zoom", "gradientdrag", 0) != 0) {
98 ec->enableGrDrag();
99 }
101 if (((SPEventContextClass *) parent_class)->setup) {
102 ((SPEventContextClass *) parent_class)->setup(ec);
103 }
104 }
106 static gint sp_zoom_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
107 {
108 gint ret = FALSE;
110 if (((SPEventContextClass *) parent_class)->item_handler) {
111 ret = ((SPEventContextClass *) parent_class)->item_handler (event_context, item, event);
112 }
114 return ret;
115 }
117 static gint sp_zoom_context_root_handler(SPEventContext *event_context, GdkEvent *event)
118 {
119 SPDesktop *desktop = event_context->desktop;
120 tolerance = prefs_get_int_attribute_limited ("options.dragtolerance", "value", 0, 0, 100);
121 double const zoom_inc = prefs_get_double_attribute_limited("options.zoomincrement", "value", M_SQRT2, 1.01, 10);
123 gint ret = FALSE;
125 switch (event->type) {
126 case GDK_BUTTON_PRESS:
127 {
128 NR::Point const button_w(event->button.x, event->button.y);
129 NR::Point const button_dt(desktop->w2d(button_w));
130 if (event->button.button == 1 && !event_context->space_panning) {
131 // save drag origin
132 xp = (gint) event->button.x;
133 yp = (gint) event->button.y;
134 within_tolerance = true;
136 Inkscape::Rubberband::get()->start(desktop, button_dt);
138 escaped = false;
140 ret = TRUE;
141 } else if (event->button.button == 3) {
142 double const zoom_rel( (event->button.state & GDK_SHIFT_MASK)
143 ? zoom_inc
144 : 1 / zoom_inc );
145 desktop->zoom_relative_keep_point(button_dt, zoom_rel);
146 ret = TRUE;
147 }
148 break;
149 }
151 case GDK_MOTION_NOTIFY:
152 if (event->motion.state & GDK_BUTTON1_MASK && !event_context->space_panning) {
153 ret = TRUE;
155 if ( within_tolerance
156 && ( abs( (gint) event->motion.x - xp ) < tolerance )
157 && ( abs( (gint) event->motion.y - yp ) < tolerance ) ) {
158 break; // do not drag if we're within tolerance from origin
159 }
160 // Once the user has moved farther than tolerance from the original location
161 // (indicating they intend to move the object, not click), then always process the
162 // motion notify coordinates as given (no snapping back to origin)
163 within_tolerance = false;
165 NR::Point const motion_w(event->motion.x, event->motion.y);
166 NR::Point const motion_dt(desktop->w2d(motion_w));
167 Inkscape::Rubberband::get()->move(motion_dt);
168 gobble_motion_events(GDK_BUTTON1_MASK);
169 }
170 break;
172 case GDK_BUTTON_RELEASE:
173 {
174 NR::Point const button_w(event->button.x, event->button.y);
175 NR::Point const button_dt(desktop->w2d(button_w));
176 if ( event->button.button == 1 && !event_context->space_panning) {
177 NR::Maybe<NR::Rect> const b = Inkscape::Rubberband::get()->getRectangle();
178 if (b && !within_tolerance) {
179 desktop->set_display_area(*b, 10);
180 } else if (!escaped) {
181 double const zoom_rel( (event->button.state & GDK_SHIFT_MASK)
182 ? 1 / zoom_inc
183 : zoom_inc );
184 desktop->zoom_relative_keep_point(button_dt, zoom_rel);
185 }
186 ret = TRUE;
187 }
188 Inkscape::Rubberband::get()->stop();
189 xp = yp = 0;
190 escaped = false;
191 break;
192 }
193 case GDK_KEY_PRESS:
194 switch (get_group0_keyval (&event->key)) {
195 case GDK_Escape:
196 Inkscape::Rubberband::get()->stop();
197 xp = yp = 0;
198 escaped = true;
199 ret = TRUE;
200 break;
201 case GDK_Up:
202 case GDK_Down:
203 case GDK_KP_Up:
204 case GDK_KP_Down:
205 // prevent the zoom field from activation
206 if (!MOD__CTRL_ONLY)
207 ret = TRUE;
208 break;
209 case GDK_Shift_L:
210 case GDK_Shift_R:
211 event_context->cursor_shape = cursor_zoom_out_xpm;
212 sp_event_context_update_cursor(event_context);
213 break;
214 default:
215 break;
216 }
217 break;
218 case GDK_KEY_RELEASE:
219 switch (get_group0_keyval (&event->key)) {
220 case GDK_Shift_L:
221 case GDK_Shift_R:
222 event_context->cursor_shape = cursor_zoom_xpm;
223 sp_event_context_update_cursor(event_context);
224 break;
225 default:
226 break;
227 }
228 break;
229 default:
230 break;
231 }
233 if (!ret) {
234 if (((SPEventContextClass *) parent_class)->root_handler) {
235 ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
236 }
237 }
239 return ret;
240 }
242 /*
243 Local Variables:
244 mode:c++
245 c-file-style:"stroustrup"
246 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
247 indent-tabs-mode:nil
248 fill-column:99
249 End:
250 */
251 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :