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 if (event->button.button == 1) {
128 // save drag origin
129 xp = (gint) event->button.x;
130 yp = (gint) event->button.y;
131 within_tolerance = true;
133 NR::Point const button_w(event->button.x, event->button.y);
134 NR::Point const button_dt(desktop->w2d(button_w));
135 Inkscape::Rubberband::get()->start(desktop, button_dt);
137 escaped = false;
139 ret = TRUE;
140 }
141 break;
143 case GDK_MOTION_NOTIFY:
144 if (event->motion.state & GDK_BUTTON1_MASK) {
145 ret = TRUE;
147 if ( within_tolerance
148 && ( abs( (gint) event->motion.x - xp ) < tolerance )
149 && ( abs( (gint) event->motion.y - yp ) < tolerance ) ) {
150 break; // do not drag if we're within tolerance from origin
151 }
152 // Once the user has moved farther than tolerance from the original location
153 // (indicating they intend to move the object, not click), then always process the
154 // motion notify coordinates as given (no snapping back to origin)
155 within_tolerance = false;
157 NR::Point const motion_w(event->motion.x, event->motion.y);
158 NR::Point const motion_dt(desktop->w2d(motion_w));
159 Inkscape::Rubberband::get()->move(motion_dt);
160 }
161 break;
163 case GDK_BUTTON_RELEASE:
164 if ( event->button.button == 1 ) {
165 NR::Maybe<NR::Rect> const b = Inkscape::Rubberband::get()->getRectangle();
166 if (b != NR::Nothing() && !within_tolerance) {
167 desktop->set_display_area(b.assume(), 10);
168 } else if (!escaped) {
169 NR::Point const button_w(event->button.x, event->button.y);
170 NR::Point const button_dt(desktop->w2d(button_w));
171 double const zoom_rel( (event->button.state & GDK_SHIFT_MASK)
172 ? 1 / zoom_inc
173 : zoom_inc );
174 desktop->zoom_relative_keep_point(button_dt, zoom_rel);
175 }
176 ret = TRUE;
177 }
178 Inkscape::Rubberband::get()->stop();
179 xp = yp = 0;
180 escaped = false;
181 break;
183 case GDK_KEY_PRESS:
184 switch (get_group0_keyval (&event->key)) {
185 case GDK_Escape:
186 Inkscape::Rubberband::get()->stop();
187 xp = yp = 0;
188 escaped = true;
189 ret = TRUE;
190 break;
191 case GDK_Up:
192 case GDK_Down:
193 case GDK_KP_Up:
194 case GDK_KP_Down:
195 // prevent the zoom field from activation
196 if (!MOD__CTRL_ONLY)
197 ret = TRUE;
198 break;
199 case GDK_Shift_L:
200 case GDK_Shift_R:
201 event_context->cursor_shape = cursor_zoom_out_xpm;
202 sp_event_context_update_cursor(event_context);
203 break;
204 default:
205 break;
206 }
207 break;
208 case GDK_KEY_RELEASE:
209 switch (get_group0_keyval (&event->key)) {
210 case GDK_Shift_L:
211 case GDK_Shift_R:
212 event_context->cursor_shape = cursor_zoom_xpm;
213 sp_event_context_update_cursor(event_context);
214 break;
215 default:
216 break;
217 }
218 break;
219 default:
220 break;
221 }
223 if (!ret) {
224 if (((SPEventContextClass *) parent_class)->root_handler) {
225 ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
226 }
227 }
229 return ret;
230 }
232 /*
233 Local Variables:
234 mode:c++
235 c-file-style:"stroustrup"
236 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
237 indent-tabs-mode:nil
238 fill-column:99
239 End:
240 */
241 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :