Code

Do not fail when svn client is not installed and .svn directory is
[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                 ac->center = Inkscape::setup_for_drag_start(desktop, event_context, event);
225                 
226                 /* Snap center */
227                 SnapManager &m = desktop->namedview->snap_manager;
228                 m.setup(desktop);
229                 Geom::Point pt2g = to_2geom(ac->center);
230                 m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, pt2g);
231                 ac->center = from_2geom(pt2g);
233                 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
234                                     GDK_KEY_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
235                                     GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK,
236                                     NULL, event->button.time);
237                 ret = TRUE;
238             }
239             break;
240         case GDK_MOTION_NOTIFY:
241             if (dragging && (event->motion.state & GDK_BUTTON1_MASK) && !event_context->space_panning) {
243                 if ( event_context->within_tolerance
244                      && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
245                      && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
246                     break; // do not drag if we're within tolerance from origin
247                 }
248                 // Once the user has moved farther than tolerance from the original location
249                 // (indicating they intend to draw, not click), then always process the
250                 // motion notify coordinates as given (no snapping back to origin)
251                 event_context->within_tolerance = false;
253                 Geom::Point const motion_w(event->motion.x, event->motion.y);
254                 Geom::Point motion_dt(desktop->w2d(motion_w));
255                 
256                 sp_arc_drag(ac, motion_dt, event->motion.state);
258                 gobble_motion_events(GDK_BUTTON1_MASK);
260                 ret = TRUE;
261             }
262             break;
263         case GDK_BUTTON_RELEASE:
264             event_context->xp = event_context->yp = 0;
265             if (event->button.button == 1 && !event_context->space_panning) {
266                 dragging = false;
267                 if (!event_context->within_tolerance) {
268                     // we've been dragging, finish the arc
269                     sp_arc_finish(ac);
270                 } else if (event_context->item_to_select) {
271                     // no dragging, select clicked item if any
272                     if (event->button.state & GDK_SHIFT_MASK) {
273                         selection->toggle(event_context->item_to_select);
274                     } else {
275                         selection->set(event_context->item_to_select);
276                     }
277                 } else {
278                     // click in an empty space
279                     selection->clear();
280                 }
281                 event_context->xp = 0;
282                 event_context->yp = 0;
283                 event_context->item_to_select = NULL;
284                 ret = TRUE;
285             }
286             sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), event->button.time);
287             break;
288         case GDK_KEY_PRESS:
289             switch (get_group0_keyval (&event->key)) {
290                 case GDK_Alt_L:
291                 case GDK_Alt_R:
292                 case GDK_Control_L:
293                 case GDK_Control_R:
294                 case GDK_Shift_L:
295                 case GDK_Shift_R:
296                 case GDK_Meta_L:  // Meta is when you press Shift+Alt (at least on my machine)
297                 case GDK_Meta_R:
298                     if (!dragging) {
299                         sp_event_show_modifier_tip(event_context->defaultMessageContext(), event,
300                                                    _("<b>Ctrl</b>: make circle or integer-ratio ellipse, snap arc/segment angle"),
301                                                    _("<b>Shift</b>: draw around the starting point"),
302                                                    NULL);
303                     }
304                     break;
305                 case GDK_Up:
306                 case GDK_Down:
307                 case GDK_KP_Up:
308                 case GDK_KP_Down:
309                     // prevent the zoom field from activation
310                     if (!MOD__CTRL_ONLY)
311                         ret = TRUE;
312                     break;
313                 case GDK_x:
314                 case GDK_X:
315                     if (MOD__ALT_ONLY) {
316                         desktop->setToolboxFocusTo ("altx-arc");
317                         ret = TRUE;
318                     }
319                     break;
320                 case GDK_Escape:
321                     sp_desktop_selection(desktop)->clear();
322                     //TODO: make dragging escapable by Esc
323                     break;
325                 case GDK_space:
326                     if (dragging) {
327                         sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
328                                               event->button.time);
329                         dragging = false;
330                         if (!event_context->within_tolerance) {
331                             // we've been dragging, finish the rect
332                             sp_arc_finish(ac);
333                         }
334                         // do not return true, so that space would work switching to selector
335                     }
336                     break;
338                 default:
339                     break;
340             }
341             break;
342         case GDK_KEY_RELEASE:
343             switch (event->key.keyval) {
344                 case GDK_Alt_L:
345                 case GDK_Alt_R:
346                 case GDK_Control_L:
347                 case GDK_Control_R:
348                 case GDK_Shift_L:
349                 case GDK_Shift_R:
350                 case GDK_Meta_L:  // Meta is when you press Shift+Alt
351                 case GDK_Meta_R:
352                     event_context->defaultMessageContext()->clear();
353                     break;
354                 default:
355                     break;
356             }
357             break;
358         default:
359             break;
360     }
362     if (!ret) {
363         if (((SPEventContextClass *) parent_class)->root_handler) {
364             ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
365         }
366     }
368     return ret;
371 static void sp_arc_drag(SPArcContext *ac, Geom::Point pt, guint state)
373     SPDesktop *desktop = SP_EVENT_CONTEXT(ac)->desktop;
375     if (!ac->item) {
377         if (Inkscape::have_viable_layer(desktop, ac->_message_context) == false) {
378             return;
379         }
381         /* Create object */
382         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
383         Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
384         repr->setAttribute("sodipodi:type", "arc");
386         /* Set style */
387         sp_desktop_apply_style_tool(desktop, repr, "/tools/shapes/arc", false);
389         ac->item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
390         Inkscape::GC::release(repr);
391         ac->item->transform = sp_item_i2doc_affine(SP_ITEM(desktop->currentLayer())).inverse();
392         ac->item->updateRepr();
394         sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
395     }
397     bool ctrl_save = false;
398     if ((state & GDK_MOD1_MASK) && (state & GDK_CONTROL_MASK) && !(state & GDK_SHIFT_MASK)) {
399         // if Alt is pressed without Shift in addition to Control, temporarily drop the CONTROL mask
400         // so that the ellipse is not constrained to integer ratios
401         ctrl_save = true;
402         state = state ^ GDK_CONTROL_MASK;
403     }
404     Geom::Rect r = Inkscape::snap_rectangular_box(desktop, ac->item, pt, ac->center, state);
405     if (ctrl_save) {
406         state = state ^ GDK_CONTROL_MASK;
407     }
409     Geom::Point dir = r.dimensions() / 2;
410     if (state & GDK_MOD1_MASK) {
411         /* With Alt let the ellipse pass through the mouse pointer */
412         Geom::Point c = r.midpoint();
413         if (!ctrl_save) {
414             if (fabs(dir[Geom::X]) > 1E-6 && fabs(dir[Geom::Y]) > 1E-6) {
415                 Geom::Matrix const i2d (sp_item_i2d_affine (ac->item));
416                 Geom::Point new_dir = pt * i2d - c;
417                 new_dir[Geom::X] *= dir[Geom::Y] / dir[Geom::X];
418                 double lambda = new_dir.length() / dir[Geom::Y];
419                 r = Geom::Rect (c - lambda*dir, c + lambda*dir);
420             }
421         } else {
422             /* with Alt+Ctrl (without Shift) we generate a perfect circle
423                with diameter click point <--> mouse pointer */
424                 double l = dir.length();
425                 Geom::Point d (l, l);
426                 r = Geom::Rect (c - d, c + d);
427         }
428     }
430     sp_arc_position_set(SP_ARC(ac->item),
431                         r.midpoint()[Geom::X], r.midpoint()[Geom::Y],
432                         r.dimensions()[Geom::X] / 2, r.dimensions()[Geom::Y] / 2);
434     double rdimx = r.dimensions()[Geom::X];
435     double rdimy = r.dimensions()[Geom::Y];
436     GString *xs = SP_PX_TO_METRIC_STRING(rdimx, desktop->namedview->getDefaultMetric());
437     GString *ys = SP_PX_TO_METRIC_STRING(rdimy, desktop->namedview->getDefaultMetric());
438     if (state & GDK_CONTROL_MASK) {
439         int ratio_x, ratio_y;
440         if (fabs (rdimx) > fabs (rdimy)) {
441             ratio_x = (int) rint (rdimx / rdimy);
442             ratio_y = 1;
443         } else {
444             ratio_x = 1;
445             ratio_y = (int) rint (rdimy / rdimx);
446         }
447         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);
448     } else {
449         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);
450     }
451     g_string_free(xs, FALSE);
452     g_string_free(ys, FALSE);
455 static void sp_arc_finish(SPArcContext *ac)
457     ac->_message_context->clear();
459     if (ac->item != NULL) {
460         SPDesktop *desktop = SP_EVENT_CONTEXT(ac)->desktop;
462         SP_OBJECT(ac->item)->updateRepr();
464         sp_canvas_end_forced_full_redraws(desktop->canvas);
465         
466         sp_desktop_selection(desktop)->set(ac->item);
467         sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_ARC, 
468                          _("Create ellipse"));
470         ac->item = NULL;
471     }
475 /*
476   Local Variables:
477   mode:c++
478   c-file-style:"stroustrup"
479   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
480   indent-tabs-mode:nil
481   fill-column:99
482   End:
483 */
484 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :