Code

show waiting cursor while ExecutionEnv is working
[inkscape.git] / src / arc-context.cpp
1 /** @file
2  * @brief Ellipse drawing context
3  */
4 /* Authors:
5  *   Mitsuru Oka
6  *   Lauris Kaplinski <lauris@kaplinski.com>
7  *   bulia byak <buliabyak@users.sf.net>
8  *   Johan Engelen <johan@shouraizou.nl>
9  *
10  * Copyright (C) 2000-2006 Authors
11  * Copyright (C) 2000-2001 Ximian, Inc.
12  *
13  * Released under GNU GPL, read the file 'COPYING' for more information
14  */
16 #define __SP_ARC_CONTEXT_C__
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21 #include <gdk/gdkkeysyms.h>
23 #include "macros.h"
24 #include <glibmm/i18n.h>
25 #include "display/sp-canvas.h"
26 #include "sp-ellipse.h"
27 #include "document.h"
28 #include "sp-namedview.h"
29 #include "selection.h"
30 #include "desktop-handles.h"
31 #include "snap.h"
32 #include "pixmaps/cursor-ellipse.xpm"
33 #include "sp-metrics.h"
34 #include "xml/repr.h"
35 #include "xml/node-event-vector.h"
36 #include "object-edit.h"
37 #include "preferences.h"
38 #include "message-context.h"
39 #include "desktop.h"
40 #include "desktop-style.h"
41 #include "context-fns.h"
42 #include "verbs.h"
43 #include "shape-editor.h"
45 #include "arc-context.h"
47 static void sp_arc_context_class_init(SPArcContextClass *klass);
48 static void sp_arc_context_init(SPArcContext *arc_context);
49 static void sp_arc_context_dispose(GObject *object);
51 static void sp_arc_context_setup(SPEventContext *ec);
52 static gint sp_arc_context_root_handler(SPEventContext *event_context, GdkEvent *event);
53 static gint sp_arc_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
55 static void sp_arc_drag(SPArcContext *ec, Geom::Point pt, guint state);
56 static void sp_arc_finish(SPArcContext *ec);
58 static SPEventContextClass *parent_class;
60 GtkType sp_arc_context_get_type()
61 {
62     static GType type = 0;
63     if (!type) {
64         GTypeInfo info = {
65             sizeof(SPArcContextClass),
66             NULL, NULL,
67             (GClassInitFunc) sp_arc_context_class_init,
68             NULL, NULL,
69             sizeof(SPArcContext),
70             4,
71             (GInstanceInitFunc) sp_arc_context_init,
72             NULL,   /* value_table */
73         };
74         type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPArcContext", &info, (GTypeFlags) 0);
75     }
76     return type;
77 }
79 static void sp_arc_context_class_init(SPArcContextClass *klass)
80 {
82     GObjectClass *object_class = (GObjectClass *) klass;
83     SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
85     parent_class = (SPEventContextClass*) g_type_class_peek_parent(klass);
87     object_class->dispose = sp_arc_context_dispose;
89     event_context_class->setup = sp_arc_context_setup;
90     event_context_class->root_handler = sp_arc_context_root_handler;
91     event_context_class->item_handler = sp_arc_context_item_handler;
92 }
94 static void sp_arc_context_init(SPArcContext *arc_context)
95 {
96     SPEventContext *event_context = SP_EVENT_CONTEXT(arc_context);
98     event_context->cursor_shape = cursor_ellipse_xpm;
99     event_context->hot_x = 4;
100     event_context->hot_y = 4;
101     event_context->xp = 0;
102     event_context->yp = 0;
103     event_context->tolerance = 0;
104     event_context->within_tolerance = false;
105     event_context->item_to_select = NULL;
107     arc_context->item = NULL;
109     new (&arc_context->sel_changed_connection) sigc::connection();
112 static void sp_arc_context_dispose(GObject *object)
114     SPEventContext *ec = SP_EVENT_CONTEXT(object);
115     SPArcContext *ac = SP_ARC_CONTEXT(object);
117     ec->enableGrDrag(false);
119     ac->sel_changed_connection.disconnect();
120     ac->sel_changed_connection.~connection();
122     delete ec->shape_editor;
123     ec->shape_editor = NULL;
125     /* fixme: This is necessary because we do not grab */
126     if (ac->item) {
127         sp_arc_finish(ac);
128     }
130     delete ac->_message_context;
132     G_OBJECT_CLASS(parent_class)->dispose(object);
135 /**
136 \brief  Callback that processes the "changed" signal on the selection;
137 destroys old and creates new knotholder.
138 */
139 void sp_arc_context_selection_changed(Inkscape::Selection * selection, gpointer data)
141     SPArcContext *ac = SP_ARC_CONTEXT(data);
142     SPEventContext *ec = SP_EVENT_CONTEXT(ac);
144     ec->shape_editor->unset_item(SH_KNOTHOLDER);
145     SPItem *item = selection->singleItem();
146     ec->shape_editor->set_item(item, SH_KNOTHOLDER);
149 static void sp_arc_context_setup(SPEventContext *ec)
151     SPArcContext *ac = SP_ARC_CONTEXT(ec);
152     Inkscape::Selection *selection = sp_desktop_selection(ec->desktop);
154     if (((SPEventContextClass *) parent_class)->setup) {
155         ((SPEventContextClass *) parent_class)->setup(ec);
156     }
158     ec->shape_editor = new ShapeEditor(ec->desktop);
160     SPItem *item = sp_desktop_selection(ec->desktop)->singleItem();
161     if (item) {
162         ec->shape_editor->set_item(item, SH_KNOTHOLDER);
163     }
165     ac->sel_changed_connection.disconnect();
166     ac->sel_changed_connection = selection->connectChanged(
167         sigc::bind(sigc::ptr_fun(&sp_arc_context_selection_changed), (gpointer) ac)
168         );
170     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
171     if (prefs->getBool("/tools/shapes/selcue")) {
172         ec->enableSelectionCue();
173     }
175     if (prefs->getBool("/tools/shapes/gradientdrag")) {
176         ec->enableGrDrag();
177     }
179     ac->_message_context = new Inkscape::MessageContext(ec->desktop->messageStack());
182 static gint sp_arc_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
184     SPDesktop *desktop = event_context->desktop;
185     gint ret = FALSE;
187     switch (event->type) {
188         case GDK_BUTTON_PRESS:
189             if (event->button.button == 1 && !event_context->space_panning) {
190                 Inkscape::setup_for_drag_start(desktop, event_context, event);
191                 ret = TRUE;
192             }
193             break;
194             // motion and release are always on root (why?)
195         default:
196             break;
197     }
199     if (((SPEventContextClass *) parent_class)->item_handler) {
200         ret = ((SPEventContextClass *) parent_class)->item_handler(event_context, item, event);
201     }
203     return ret;
206 static gint sp_arc_context_root_handler(SPEventContext *event_context, GdkEvent *event)
208     static bool dragging;
210     SPDesktop *desktop = event_context->desktop;
211     Inkscape::Selection *selection = sp_desktop_selection(desktop);
212     SPArcContext *ac = SP_ARC_CONTEXT(event_context);
213     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
215     event_context->tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
217     gint ret = FALSE;
219     switch (event->type) {
220         case GDK_BUTTON_PRESS:
221             if (event->button.button == 1 && !event_context->space_panning) {
223                 dragging = true;
224                 sp_canvas_set_snap_delay_active(desktop->canvas, true);
225                 ac->center = Inkscape::setup_for_drag_start(desktop, event_context, event);
227                 /* Snap center */
228                 SnapManager &m = desktop->namedview->snap_manager;
229                 m.setup(desktop);
230                 Geom::Point pt2g = to_2geom(ac->center);
231                 m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, pt2g);
232                 ac->center = from_2geom(pt2g);
234                 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
235                                     GDK_KEY_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
236                                     GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK,
237                                     NULL, event->button.time);
238                 ret = TRUE;
239             }
240             break;
241         case GDK_MOTION_NOTIFY:
242             if (dragging && (event->motion.state & GDK_BUTTON1_MASK) && !event_context->space_panning) {
244                 if ( event_context->within_tolerance
245                      && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
246                      && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
247                     break; // do not drag if we're within tolerance from origin
248                 }
249                 // Once the user has moved farther than tolerance from the original location
250                 // (indicating they intend to draw, not click), then always process the
251                 // motion notify coordinates as given (no snapping back to origin)
252                 event_context->within_tolerance = false;
254                 Geom::Point const motion_w(event->motion.x, event->motion.y);
255                 Geom::Point motion_dt(desktop->w2d(motion_w));
257                 sp_arc_drag(ac, motion_dt, event->motion.state);
259                 gobble_motion_events(GDK_BUTTON1_MASK);
261                 ret = TRUE;
262             }
263             break;
264         case GDK_BUTTON_RELEASE:
265             event_context->xp = event_context->yp = 0;
266             if (event->button.button == 1 && !event_context->space_panning) {
267                 dragging = false;
268                 sp_canvas_set_snap_delay_active(desktop->canvas, false);
269                 if (!event_context->within_tolerance) {
270                     // we've been dragging, finish the arc
271                     sp_arc_finish(ac);
272                 } else if (event_context->item_to_select) {
273                     // no dragging, select clicked item if any
274                     if (event->button.state & GDK_SHIFT_MASK) {
275                         selection->toggle(event_context->item_to_select);
276                     } else {
277                         selection->set(event_context->item_to_select);
278                     }
279                 } else {
280                     // click in an empty space
281                     selection->clear();
282                 }
283                 event_context->xp = 0;
284                 event_context->yp = 0;
285                 event_context->item_to_select = NULL;
286                 ret = TRUE;
287             }
288             sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), event->button.time);
289             break;
290         case GDK_KEY_PRESS:
291             switch (get_group0_keyval (&event->key)) {
292                 case GDK_Alt_L:
293                 case GDK_Alt_R:
294                 case GDK_Control_L:
295                 case GDK_Control_R:
296                 case GDK_Shift_L:
297                 case GDK_Shift_R:
298                 case GDK_Meta_L:  // Meta is when you press Shift+Alt (at least on my machine)
299                 case GDK_Meta_R:
300                     if (!dragging) {
301                         sp_event_show_modifier_tip(event_context->defaultMessageContext(), event,
302                                                    _("<b>Ctrl</b>: make circle or integer-ratio ellipse, snap arc/segment angle"),
303                                                    _("<b>Shift</b>: draw around the starting point"),
304                                                    NULL);
305                     }
306                     break;
307                 case GDK_Up:
308                 case GDK_Down:
309                 case GDK_KP_Up:
310                 case GDK_KP_Down:
311                     // prevent the zoom field from activation
312                     if (!MOD__CTRL_ONLY)
313                         ret = TRUE;
314                     break;
315                 case GDK_x:
316                 case GDK_X:
317                     if (MOD__ALT_ONLY) {
318                         desktop->setToolboxFocusTo ("altx-arc");
319                         ret = TRUE;
320                     }
321                     break;
322                 case GDK_Escape:
323                     sp_desktop_selection(desktop)->clear();
324                     //TODO: make dragging escapable by Esc
325                     break;
327                 case GDK_space:
328                     if (dragging) {
329                         sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
330                                               event->button.time);
331                         dragging = false;
332                         sp_canvas_set_snap_delay_active(desktop->canvas, false);
333                         if (!event_context->within_tolerance) {
334                             // we've been dragging, finish the rect
335                             sp_arc_finish(ac);
336                         }
337                         // do not return true, so that space would work switching to selector
338                     }
339                     break;
341                 default:
342                     break;
343             }
344             break;
345         case GDK_KEY_RELEASE:
346             switch (event->key.keyval) {
347                 case GDK_Alt_L:
348                 case GDK_Alt_R:
349                 case GDK_Control_L:
350                 case GDK_Control_R:
351                 case GDK_Shift_L:
352                 case GDK_Shift_R:
353                 case GDK_Meta_L:  // Meta is when you press Shift+Alt
354                 case GDK_Meta_R:
355                     event_context->defaultMessageContext()->clear();
356                     break;
357                 default:
358                     break;
359             }
360             break;
361         default:
362             break;
363     }
365     if (!ret) {
366         if (((SPEventContextClass *) parent_class)->root_handler) {
367             ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
368         }
369     }
371     return ret;
374 static void sp_arc_drag(SPArcContext *ac, Geom::Point pt, guint state)
376     SPDesktop *desktop = SP_EVENT_CONTEXT(ac)->desktop;
378     if (!ac->item) {
380         if (Inkscape::have_viable_layer(desktop, ac->_message_context) == false) {
381             return;
382         }
384         /* Create object */
385         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
386         Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
387         repr->setAttribute("sodipodi:type", "arc");
389         /* Set style */
390         sp_desktop_apply_style_tool(desktop, repr, "/tools/shapes/arc", false);
392         ac->item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
393         Inkscape::GC::release(repr);
394         ac->item->transform = sp_item_i2doc_affine(SP_ITEM(desktop->currentLayer())).inverse();
395         ac->item->updateRepr();
397         sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
398     }
400     bool ctrl_save = false;
401     if ((state & GDK_MOD1_MASK) && (state & GDK_CONTROL_MASK) && !(state & GDK_SHIFT_MASK)) {
402         // if Alt is pressed without Shift in addition to Control, temporarily drop the CONTROL mask
403         // so that the ellipse is not constrained to integer ratios
404         ctrl_save = true;
405         state = state ^ GDK_CONTROL_MASK;
406     }
407     Geom::Rect r = Inkscape::snap_rectangular_box(desktop, ac->item, pt, ac->center, state);
408     if (ctrl_save) {
409         state = state ^ GDK_CONTROL_MASK;
410     }
412     Geom::Point dir = r.dimensions() / 2;
413     if (state & GDK_MOD1_MASK) {
414         /* With Alt let the ellipse pass through the mouse pointer */
415         Geom::Point c = r.midpoint();
416         if (!ctrl_save) {
417             if (fabs(dir[Geom::X]) > 1E-6 && fabs(dir[Geom::Y]) > 1E-6) {
418                 Geom::Matrix const i2d (sp_item_i2d_affine (ac->item));
419                 Geom::Point new_dir = pt * i2d - c;
420                 new_dir[Geom::X] *= dir[Geom::Y] / dir[Geom::X];
421                 double lambda = new_dir.length() / dir[Geom::Y];
422                 r = Geom::Rect (c - lambda*dir, c + lambda*dir);
423             }
424         } else {
425             /* with Alt+Ctrl (without Shift) we generate a perfect circle
426                with diameter click point <--> mouse pointer */
427                 double l = dir.length();
428                 Geom::Point d (l, l);
429                 r = Geom::Rect (c - d, c + d);
430         }
431     }
433     sp_arc_position_set(SP_ARC(ac->item),
434                         r.midpoint()[Geom::X], r.midpoint()[Geom::Y],
435                         r.dimensions()[Geom::X] / 2, r.dimensions()[Geom::Y] / 2);
437     double rdimx = r.dimensions()[Geom::X];
438     double rdimy = r.dimensions()[Geom::Y];
439     GString *xs = SP_PX_TO_METRIC_STRING(rdimx, desktop->namedview->getDefaultMetric());
440     GString *ys = SP_PX_TO_METRIC_STRING(rdimy, desktop->namedview->getDefaultMetric());
441     if (state & GDK_CONTROL_MASK) {
442         int ratio_x, ratio_y;
443         if (fabs (rdimx) > fabs (rdimy)) {
444             ratio_x = (int) rint (rdimx / rdimy);
445             ratio_y = 1;
446         } else {
447             ratio_x = 1;
448             ratio_y = (int) rint (rdimy / rdimx);
449         }
450         ac->_message_context->setF(Inkscape::IMMEDIATE_MESSAGE, _("<b>Ellipse</b>: %s &#215; %s (constrained to ratio %d:%d); with <b>Shift</b> to draw around the starting point"), xs->str, ys->str, ratio_x, ratio_y);
451     } else {
452         ac->_message_context->setF(Inkscape::IMMEDIATE_MESSAGE, _("<b>Ellipse</b>: %s &#215; %s; with <b>Ctrl</b> to make square or integer-ratio ellipse; with <b>Shift</b> to draw around the starting point"), xs->str, ys->str);
453     }
454     g_string_free(xs, FALSE);
455     g_string_free(ys, FALSE);
458 static void sp_arc_finish(SPArcContext *ac)
460     ac->_message_context->clear();
462     if (ac->item != NULL) {
463         SPDesktop *desktop = SP_EVENT_CONTEXT(ac)->desktop;
465         SP_OBJECT(ac->item)->updateRepr();
467         sp_canvas_end_forced_full_redraws(desktop->canvas);
469         sp_desktop_selection(desktop)->set(ac->item);
470         sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_ARC,
471                          _("Create ellipse"));
473         ac->item = NULL;
474     }
478 /*
479   Local Variables:
480   mode:c++
481   c-file-style:"stroustrup"
482   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
483   indent-tabs-mode:nil
484   fill-column:99
485   End:
486 */
487 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :