Code

Fixed signed/unsigned problem with precision calc. Fixes bug #399604.
[inkscape.git] / src / zoom-context.cpp
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 "display/sp-canvas-util.h"
22 #include "desktop.h"
23 #include "pixmaps/cursor-zoom.xpm"
24 #include "pixmaps/cursor-zoom-out.xpm"
25 #include "preferences.h"
27 #include "zoom-context.h"
29 static void sp_zoom_context_class_init(SPZoomContextClass *klass);
30 static void sp_zoom_context_init(SPZoomContext *zoom_context);
31 static void sp_zoom_context_setup(SPEventContext *ec);
32 static void sp_zoom_context_finish (SPEventContext *ec);
34 static gint sp_zoom_context_root_handler(SPEventContext *event_context, GdkEvent *event);
35 static gint sp_zoom_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
37 static SPEventContextClass *parent_class;
39 static gint xp = 0, yp = 0; // where drag started
40 static gint tolerance = 0;
41 static bool within_tolerance = false;
42 static bool escaped;
44 GType sp_zoom_context_get_type(void)
45 {
46     static GType type = 0;
48     if (!type) {
49         GTypeInfo info = {
50             sizeof(SPZoomContextClass),
51             NULL, NULL,
52             (GClassInitFunc) sp_zoom_context_class_init,
53             NULL, NULL,
54             sizeof(SPZoomContext),
55             4,
56             (GInstanceInitFunc) sp_zoom_context_init,
57             NULL,       /* value_table */
58         };
59         type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPZoomContext", &info, (GTypeFlags) 0);
60     }
62     return type;
63 }
65 static void sp_zoom_context_class_init(SPZoomContextClass *klass)
66 {
67     SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
69     parent_class = (SPEventContextClass*) g_type_class_peek_parent(klass);
71     event_context_class->setup = sp_zoom_context_setup;
72     event_context_class->finish = sp_zoom_context_finish;
74     event_context_class->root_handler = sp_zoom_context_root_handler;
75     event_context_class->item_handler = sp_zoom_context_item_handler;
76 }
78 static void sp_zoom_context_init (SPZoomContext *zoom_context)
79 {
80     SPEventContext *event_context = SP_EVENT_CONTEXT(zoom_context);
82     event_context->cursor_shape = cursor_zoom_xpm;
83     event_context->hot_x = 6;
84     event_context->hot_y = 6;
85 }
87 static void
88 sp_zoom_context_finish (SPEventContext *ec)
89 {
90         SPZoomContext *zc = SP_ZOOM_CONTEXT(ec);
91         
92         ec->enableGrDrag(false);
93         
94     if (zc->grabbed) {
95         sp_canvas_item_ungrab(zc->grabbed, GDK_CURRENT_TIME);
96         zc->grabbed = NULL;
97     }
98 }
100 static void sp_zoom_context_setup(SPEventContext *ec)
102     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
103     if (prefs->getBool("/tools/zoom/selcue")) {
104         ec->enableSelectionCue();
105     }
106     if (prefs->getBool("/tools/zoom/gradientdrag")) {
107         ec->enableGrDrag();
108     }
110     if (((SPEventContextClass *) parent_class)->setup) {
111         ((SPEventContextClass *) parent_class)->setup(ec);
112     }
115 static gint sp_zoom_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
117     gint ret = FALSE;
119     if (((SPEventContextClass *) parent_class)->item_handler) {
120         ret = ((SPEventContextClass *) parent_class)->item_handler (event_context, item, event);
121     }
123     return ret;
126 static gint sp_zoom_context_root_handler(SPEventContext *event_context, GdkEvent *event)
128     SPDesktop *desktop = event_context->desktop;
129     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
130         
131         SPZoomContext *zc = SP_ZOOM_CONTEXT(event_context);
132     tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
133     double const zoom_inc = prefs->getDoubleLimited("/options/zoomincrement/value", M_SQRT2, 1.01, 10);
135     gint ret = FALSE;
137     switch (event->type) {
138         case GDK_BUTTON_PRESS:
139         {
140             Geom::Point const button_w(event->button.x, event->button.y);
141             Geom::Point const button_dt(desktop->w2d(button_w));
142             if (event->button.button == 1 && !event_context->space_panning) {
143                 // save drag origin
144                 xp = (gint) event->button.x;
145                 yp = (gint) event->button.y;
146                 within_tolerance = true;
148                 Inkscape::Rubberband::get(desktop)->start(desktop, button_dt);
150                 escaped = false;
152                 ret = TRUE;
153             } else if (event->button.button == 3) {
154                 double const zoom_rel( (event->button.state & GDK_SHIFT_MASK)
155                                        ? zoom_inc
156                                        : 1 / zoom_inc );
157                 desktop->zoom_relative_keep_point(button_dt, zoom_rel);
158                 ret = TRUE;
159             }
160                         
161                         sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
162                                                                 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK | GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK,
163                                                                 NULL, event->button.time);
164                         zc->grabbed = SP_CANVAS_ITEM(desktop->acetate);
165                                 
166             break;
167         }
169         case GDK_MOTION_NOTIFY:
170             if (event->motion.state & GDK_BUTTON1_MASK && !event_context->space_panning) {
171                 ret = TRUE;
173                 if ( within_tolerance
174                      && ( abs( (gint) event->motion.x - xp ) < tolerance )
175                      && ( abs( (gint) event->motion.y - yp ) < tolerance ) ) {
176                     break; // do not drag if we're within tolerance from origin
177                 }
178                 // Once the user has moved farther than tolerance from the original location
179                 // (indicating they intend to move the object, not click), then always process the
180                 // motion notify coordinates as given (no snapping back to origin)
181                 within_tolerance = false;
183                 Geom::Point const motion_w(event->motion.x, event->motion.y);
184                 Geom::Point const motion_dt(desktop->w2d(motion_w));
185                 Inkscape::Rubberband::get(desktop)->move(motion_dt);
186                 gobble_motion_events(GDK_BUTTON1_MASK);
187             }
188             break;
190         case GDK_BUTTON_RELEASE:
191         {
192             Geom::Point const button_w(event->button.x, event->button.y);
193             Geom::Point const button_dt(desktop->w2d(button_w));
194             if ( event->button.button == 1  && !event_context->space_panning) {
195                 Geom::OptRect const b = Inkscape::Rubberband::get(desktop)->getRectangle();
196                 if (b && !within_tolerance) {
197                     desktop->set_display_area(*b, 10);
198                 } else if (!escaped) {
199                     double const zoom_rel( (event->button.state & GDK_SHIFT_MASK)
200                                            ? 1 / zoom_inc
201                                            : zoom_inc );
202                     desktop->zoom_relative_keep_point(button_dt, zoom_rel);
203                 }
204                 ret = TRUE;
205             } 
206             Inkscape::Rubberband::get(desktop)->stop();
207                         
208                         if (zc->grabbed) {
209                                 sp_canvas_item_ungrab(zc->grabbed, event->button.time);
210                                 zc->grabbed = NULL;
211                         }
212                         
213             xp = yp = 0;
214             escaped = false;
215             break;
216         }
217         case GDK_KEY_PRESS:
218             switch (get_group0_keyval (&event->key)) {
219                 case GDK_Escape:
220                     Inkscape::Rubberband::get(desktop)->stop();
221                     xp = yp = 0;
222                     escaped = true;
223                     ret = TRUE;
224                     break;
225                 case GDK_Up:
226                 case GDK_Down:
227                 case GDK_KP_Up:
228                 case GDK_KP_Down:
229                     // prevent the zoom field from activation
230                     if (!MOD__CTRL_ONLY)
231                         ret = TRUE;
232                     break;
233                 case GDK_Shift_L:
234                 case GDK_Shift_R:
235                     event_context->cursor_shape = cursor_zoom_out_xpm;
236                     sp_event_context_update_cursor(event_context);
237                     break;
238                 default:
239                         break;
240                 }
241                 break;
242         case GDK_KEY_RELEASE:
243             switch (get_group0_keyval (&event->key)) {
244                 case GDK_Shift_L:
245                 case GDK_Shift_R:
246                     event_context->cursor_shape = cursor_zoom_xpm;
247                     sp_event_context_update_cursor(event_context);
248                     break;
249                 default:
250                     break;
251                 }
252             break;
253         default:
254             break;
255     }
257     if (!ret) {
258         if (((SPEventContextClass *) parent_class)->root_handler) {
259             ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
260         }
261     }
263     return ret;
266 /*
267   Local Variables:
268   mode:c++
269   c-file-style:"stroustrup"
270   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
271   indent-tabs-mode:nil
272   fill-column:99
273   End:
274 */
275 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :