Code

Split SPCanvasItem and SPCanvasGroup to individual .h files. Removed forward header.
[inkscape.git] / src / zoom-context.cpp
1 /*
2  * Handy zooming tool
3  *
4  * Authors:
5  *   Lauris Kaplinski <lauris@kaplinski.com>
6  *   Frank Felfe <innerspace@iname.com>
7  *   bulia byak <buliabyak@users.sf.net>
8  *
9  * Copyright (C) 1999-2002 Authors
10  *
11  * Released under GNU GPL, read the file 'COPYING' for more information
12  */
15 #include <gdk/gdkkeysyms.h>
17 #include "macros.h"
18 #include "rubberband.h"
19 #include "display/sp-canvas-item.h"
20 #include "display/sp-canvas-util.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         SPZoomContext *zc = SP_ZOOM_CONTEXT(ec);
90         
91         ec->enableGrDrag(false);
92         
93     if (zc->grabbed) {
94         sp_canvas_item_ungrab(zc->grabbed, GDK_CURRENT_TIME);
95         zc->grabbed = NULL;
96     }
97 }
99 static void sp_zoom_context_setup(SPEventContext *ec)
101     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
102     if (prefs->getBool("/tools/zoom/selcue")) {
103         ec->enableSelectionCue();
104     }
105     if (prefs->getBool("/tools/zoom/gradientdrag")) {
106         ec->enableGrDrag();
107     }
109     if (((SPEventContextClass *) parent_class)->setup) {
110         ((SPEventContextClass *) parent_class)->setup(ec);
111     }
114 static gint sp_zoom_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
116     gint ret = FALSE;
118     if (((SPEventContextClass *) parent_class)->item_handler) {
119         ret = ((SPEventContextClass *) parent_class)->item_handler (event_context, item, event);
120     }
122     return ret;
125 static gint sp_zoom_context_root_handler(SPEventContext *event_context, GdkEvent *event)
127     SPDesktop *desktop = event_context->desktop;
128     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
129         
130         SPZoomContext *zc = SP_ZOOM_CONTEXT(event_context);
131     tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
132     double const zoom_inc = prefs->getDoubleLimited("/options/zoomincrement/value", M_SQRT2, 1.01, 10);
134     gint ret = FALSE;
136     switch (event->type) {
137         case GDK_BUTTON_PRESS:
138         {
139             Geom::Point const button_w(event->button.x, event->button.y);
140             Geom::Point const button_dt(desktop->w2d(button_w));
141             if (event->button.button == 1 && !event_context->space_panning) {
142                 // save drag origin
143                 xp = (gint) event->button.x;
144                 yp = (gint) event->button.y;
145                 within_tolerance = true;
147                 Inkscape::Rubberband::get(desktop)->start(desktop, button_dt);
149                 escaped = false;
151                 ret = TRUE;
152             } else if (event->button.button == 3) {
153                 double const zoom_rel( (event->button.state & GDK_SHIFT_MASK)
154                                        ? zoom_inc
155                                        : 1 / zoom_inc );
156                 desktop->zoom_relative_keep_point(button_dt, zoom_rel);
157                 ret = TRUE;
158             }
159                         
160                         sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
161                                                                 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,
162                                                                 NULL, event->button.time);
163                         zc->grabbed = SP_CANVAS_ITEM(desktop->acetate);
164                                 
165             break;
166         }
168         case GDK_MOTION_NOTIFY:
169             if (event->motion.state & GDK_BUTTON1_MASK && !event_context->space_panning) {
170                 ret = TRUE;
172                 if ( within_tolerance
173                      && ( abs( (gint) event->motion.x - xp ) < tolerance )
174                      && ( abs( (gint) event->motion.y - yp ) < tolerance ) ) {
175                     break; // do not drag if we're within tolerance from origin
176                 }
177                 // Once the user has moved farther than tolerance from the original location
178                 // (indicating they intend to move the object, not click), then always process the
179                 // motion notify coordinates as given (no snapping back to origin)
180                 within_tolerance = false;
182                 Geom::Point const motion_w(event->motion.x, event->motion.y);
183                 Geom::Point const motion_dt(desktop->w2d(motion_w));
184                 Inkscape::Rubberband::get(desktop)->move(motion_dt);
185                 gobble_motion_events(GDK_BUTTON1_MASK);
186             }
187             break;
189         case GDK_BUTTON_RELEASE:
190         {
191             Geom::Point const button_w(event->button.x, event->button.y);
192             Geom::Point const button_dt(desktop->w2d(button_w));
193             if ( event->button.button == 1  && !event_context->space_panning) {
194                 Geom::OptRect const b = Inkscape::Rubberband::get(desktop)->getRectangle();
195                 if (b && !within_tolerance) {
196                     desktop->set_display_area(*b, 10);
197                 } else if (!escaped) {
198                     double const zoom_rel( (event->button.state & GDK_SHIFT_MASK)
199                                            ? 1 / zoom_inc
200                                            : zoom_inc );
201                     desktop->zoom_relative_keep_point(button_dt, zoom_rel);
202                 }
203                 ret = TRUE;
204             } 
205             Inkscape::Rubberband::get(desktop)->stop();
206                         
207                         if (zc->grabbed) {
208                                 sp_canvas_item_ungrab(zc->grabbed, event->button.time);
209                                 zc->grabbed = NULL;
210                         }
211                         
212             xp = yp = 0;
213             escaped = false;
214             break;
215         }
216         case GDK_KEY_PRESS:
217             switch (get_group0_keyval (&event->key)) {
218                 case GDK_Escape:
219                     Inkscape::Rubberband::get(desktop)->stop();
220                     xp = yp = 0;
221                     escaped = true;
222                     ret = TRUE;
223                     break;
224                 case GDK_Up:
225                 case GDK_Down:
226                 case GDK_KP_Up:
227                 case GDK_KP_Down:
228                     // prevent the zoom field from activation
229                     if (!MOD__CTRL_ONLY)
230                         ret = TRUE;
231                     break;
232                 case GDK_Shift_L:
233                 case GDK_Shift_R:
234                     event_context->cursor_shape = cursor_zoom_out_xpm;
235                     sp_event_context_update_cursor(event_context);
236                     break;
237                 default:
238                         break;
239                 }
240                 break;
241         case GDK_KEY_RELEASE:
242             switch (get_group0_keyval (&event->key)) {
243                 case GDK_Shift_L:
244                 case GDK_Shift_R:
245                     event_context->cursor_shape = cursor_zoom_xpm;
246                     sp_event_context_update_cursor(event_context);
247                     break;
248                 default:
249                     break;
250                 }
251             break;
252         default:
253             break;
254     }
256     if (!ret) {
257         if (((SPEventContextClass *) parent_class)->root_handler) {
258             ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
259         }
260     }
262     return ret;
265 /*
266   Local Variables:
267   mode:c++
268   c-file-style:"stroustrup"
269   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
270   indent-tabs-mode:nil
271   fill-column:99
272   End:
273 */
274 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :