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 "pixmaps/cursor-zoom.pixbuf"
25 #include "pixmaps/cursor-zoom-out.pixbuf"
26 #include "prefs-utils.h"
28 #include "zoom-context.h"
30 static void sp_zoom_context_class_init(SPZoomContextClass *klass);
31 static void sp_zoom_context_init(SPZoomContext *zoom_context);
32 static void sp_zoom_context_setup(SPEventContext *ec);
33 static void sp_zoom_context_finish (SPEventContext *ec);
35 static gint sp_zoom_context_root_handler(SPEventContext *event_context, GdkEvent *event);
36 static gint sp_zoom_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
38 static SPEventContextClass *parent_class;
40 static gint xp = 0, yp = 0; // where drag started
41 static gint tolerance = 0;
42 static bool within_tolerance = false;
43 static bool escaped;
45 GType sp_zoom_context_get_type(void)
46 {
47 static GType type = 0;
49 if (!type) {
50 GTypeInfo info = {
51 sizeof(SPZoomContextClass),
52 NULL, NULL,
53 (GClassInitFunc) sp_zoom_context_class_init,
54 NULL, NULL,
55 sizeof(SPZoomContext),
56 4,
57 (GInstanceInitFunc) sp_zoom_context_init,
58 NULL, /* value_table */
59 };
60 type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPZoomContext", &info, (GTypeFlags) 0);
61 }
63 return type;
64 }
66 static void sp_zoom_context_class_init(SPZoomContextClass *klass)
67 {
68 SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
70 parent_class = (SPEventContextClass*) g_type_class_peek_parent(klass);
72 event_context_class->setup = sp_zoom_context_setup;
73 event_context_class->finish = sp_zoom_context_finish;
75 event_context_class->root_handler = sp_zoom_context_root_handler;
76 event_context_class->item_handler = sp_zoom_context_item_handler;
77 }
79 static void sp_zoom_context_init (SPZoomContext *zoom_context)
80 {
81 SPEventContext *event_context = SP_EVENT_CONTEXT(zoom_context);
83 event_context->cursor_shape = cursor_zoom_xpm;
84 event_context->cursor_pixbuf = gdk_pixbuf_new_from_inline(
85 -1,
86 cursor_zoom_pixbuf,
87 FALSE,
88 NULL);
89 event_context->hot_x = 9;
90 event_context->hot_y = 9;
91 }
93 static void
94 sp_zoom_context_finish (SPEventContext *ec)
95 {
96 ec->enableGrDrag(false);
97 }
99 static void sp_zoom_context_setup(SPEventContext *ec)
100 {
101 if (prefs_get_int_attribute("tools.zoom", "selcue", 0) != 0) {
102 ec->enableSelectionCue();
103 }
104 if (prefs_get_int_attribute("tools.zoom", "gradientdrag", 0) != 0) {
105 ec->enableGrDrag();
106 }
108 if (((SPEventContextClass *) parent_class)->setup) {
109 ((SPEventContextClass *) parent_class)->setup(ec);
110 }
111 }
113 static gint sp_zoom_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
114 {
115 gint ret = FALSE;
117 if (((SPEventContextClass *) parent_class)->item_handler) {
118 ret = ((SPEventContextClass *) parent_class)->item_handler (event_context, item, event);
119 }
121 return ret;
122 }
124 static gint sp_zoom_context_root_handler(SPEventContext *event_context, GdkEvent *event)
125 {
126 SPDesktop *desktop = event_context->desktop;
127 tolerance = prefs_get_int_attribute_limited ("options.dragtolerance", "value", 0, 0, 100);
128 double const zoom_inc = prefs_get_double_attribute_limited("options.zoomincrement", "value", M_SQRT2, 1.01, 10);
130 gint ret = FALSE;
132 switch (event->type) {
133 case GDK_BUTTON_PRESS:
134 if (event->button.button == 1) {
135 // save drag origin
136 xp = (gint) event->button.x;
137 yp = (gint) event->button.y;
138 within_tolerance = true;
140 NR::Point const button_w(event->button.x, event->button.y);
141 NR::Point const button_dt(desktop->w2d(button_w));
142 Inkscape::Rubberband::get()->start(desktop, button_dt);
144 escaped = false;
146 ret = TRUE;
147 }
148 break;
150 case GDK_MOTION_NOTIFY:
151 if (event->motion.state & GDK_BUTTON1_MASK) {
152 ret = TRUE;
154 if ( within_tolerance
155 && ( abs( (gint) event->motion.x - xp ) < tolerance )
156 && ( abs( (gint) event->motion.y - yp ) < tolerance ) ) {
157 break; // do not drag if we're within tolerance from origin
158 }
159 // Once the user has moved farther than tolerance from the original location
160 // (indicating they intend to move the object, not click), then always process the
161 // motion notify coordinates as given (no snapping back to origin)
162 within_tolerance = false;
164 NR::Point const motion_w(event->motion.x, event->motion.y);
165 NR::Point const motion_dt(desktop->w2d(motion_w));
166 Inkscape::Rubberband::get()->move(motion_dt);
167 }
168 break;
170 case GDK_BUTTON_RELEASE:
171 if ( event->button.button == 1 ) {
172 NR::Maybe<NR::Rect> const b = Inkscape::Rubberband::get()->getRectangle();
173 if (b != NR::Nothing() && !within_tolerance) {
174 desktop->set_display_area(b.assume(), 10);
175 } else if (!escaped) {
176 NR::Point const button_w(event->button.x, event->button.y);
177 NR::Point const button_dt(desktop->w2d(button_w));
178 double const zoom_rel( (event->button.state & GDK_SHIFT_MASK)
179 ? 1 / zoom_inc
180 : zoom_inc );
181 desktop->zoom_relative_keep_point(button_dt, zoom_rel);
182 }
183 ret = TRUE;
184 }
185 Inkscape::Rubberband::get()->stop();
186 xp = yp = 0;
187 escaped = false;
188 break;
190 case GDK_KEY_PRESS:
191 switch (get_group0_keyval (&event->key)) {
192 case GDK_Escape:
193 Inkscape::Rubberband::get()->stop();
194 xp = yp = 0;
195 escaped = true;
196 ret = TRUE;
197 break;
198 case GDK_Up:
199 case GDK_Down:
200 case GDK_KP_Up:
201 case GDK_KP_Down:
202 // prevent the zoom field from activation
203 if (!MOD__CTRL_ONLY)
204 ret = TRUE;
205 break;
206 case GDK_Shift_L:
207 case GDK_Shift_R:
208 event_context->cursor_shape = cursor_zoom_out_xpm;
209 event_context->cursor_pixbuf = gdk_pixbuf_new_from_inline(
210 -1,
211 cursor_zoom_out_pixbuf,
212 FALSE,
213 NULL);
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 event_context->cursor_pixbuf = gdk_pixbuf_new_from_inline(
226 -1,
227 cursor_zoom_pixbuf,
228 FALSE,
229 NULL);
230 sp_event_context_update_cursor(event_context);
231 break;
232 default:
233 break;
234 }
235 break;
236 default:
237 break;
238 }
240 if (!ret) {
241 if (((SPEventContextClass *) parent_class)->root_handler) {
242 ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
243 }
244 }
246 return ret;
247 }
249 /*
250 Local Variables:
251 mode:c++
252 c-file-style:"stroustrup"
253 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
254 indent-tabs-mode:nil
255 fill-column:99
256 End:
257 */
258 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :