Code

Make curvature work again by fixing a minor omission
[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"
44 #include "event-context.h"
46 #include "arc-context.h"
48 static void sp_arc_context_class_init(SPArcContextClass *klass);
49 static void sp_arc_context_init(SPArcContext *arc_context);
50 static void sp_arc_context_dispose(GObject *object);
52 static void sp_arc_context_setup(SPEventContext *ec);
53 static void sp_arc_context_finish(SPEventContext *ec);
54 static gint sp_arc_context_root_handler(SPEventContext *event_context, GdkEvent *event);
55 static gint sp_arc_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
57 static void sp_arc_drag(SPArcContext *ec, Geom::Point pt, guint state);
58 static void sp_arc_finish(SPArcContext *ec);
59 static void sp_arc_cancel(SPArcContext *ec);
61 static SPEventContextClass *parent_class;
63 GtkType sp_arc_context_get_type()
64 {
65     static GType type = 0;
66     if (!type) {
67         GTypeInfo info = {
68             sizeof(SPArcContextClass),
69             NULL, NULL,
70             (GClassInitFunc) sp_arc_context_class_init,
71             NULL, NULL,
72             sizeof(SPArcContext),
73             4,
74             (GInstanceInitFunc) sp_arc_context_init,
75             NULL,   /* value_table */
76         };
77         type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPArcContext", &info, (GTypeFlags) 0);
78     }
79     return type;
80 }
82 static void sp_arc_context_class_init(SPArcContextClass *klass)
83 {
85     GObjectClass *object_class = (GObjectClass *) klass;
86     SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
88     parent_class = (SPEventContextClass*) g_type_class_peek_parent(klass);
90     object_class->dispose = sp_arc_context_dispose;
92     event_context_class->setup = sp_arc_context_setup;
93     event_context_class->finish = sp_arc_context_finish;
94     event_context_class->root_handler = sp_arc_context_root_handler;
95     event_context_class->item_handler = sp_arc_context_item_handler;
96 }
98 static void sp_arc_context_init(SPArcContext *arc_context)
99 {
100     SPEventContext *event_context = SP_EVENT_CONTEXT(arc_context);
102     event_context->cursor_shape = cursor_ellipse_xpm;
103     event_context->hot_x = 4;
104     event_context->hot_y = 4;
105     event_context->xp = 0;
106     event_context->yp = 0;
107     event_context->tolerance = 0;
108     event_context->within_tolerance = false;
109     event_context->item_to_select = NULL;
111     arc_context->item = NULL;
113     new (&arc_context->sel_changed_connection) sigc::connection();
116 static void sp_arc_context_finish(SPEventContext *ec)
118     SPArcContext *ac = SP_ARC_CONTEXT(ec);
119         SPDesktop *desktop = ec->desktop;
121         sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), GDK_CURRENT_TIME);
122         sp_arc_finish(ac);
123     ac->sel_changed_connection.disconnect();
125     if (((SPEventContextClass *) parent_class)->finish) {
126                 ((SPEventContextClass *) parent_class)->finish(ec);
127         }
130 static void sp_arc_context_dispose(GObject *object)
132     SPEventContext *ec = SP_EVENT_CONTEXT(object);
133     SPArcContext *ac = SP_ARC_CONTEXT(object);
135     ec->enableGrDrag(false);
137     ac->sel_changed_connection.disconnect();
138     ac->sel_changed_connection.~connection();
140     delete ec->shape_editor;
141     ec->shape_editor = NULL;
143     /* fixme: This is necessary because we do not grab */
144     if (ac->item) {
145         sp_arc_finish(ac);
146     }
148     delete ac->_message_context;
150     G_OBJECT_CLASS(parent_class)->dispose(object);
153 /**
154 \brief  Callback that processes the "changed" signal on the selection;
155 destroys old and creates new knotholder.
156 */
157 void sp_arc_context_selection_changed(Inkscape::Selection * selection, gpointer data)
159     SPArcContext *ac = SP_ARC_CONTEXT(data);
160     SPEventContext *ec = SP_EVENT_CONTEXT(ac);
162     ec->shape_editor->unset_item(SH_KNOTHOLDER);
163     SPItem *item = selection->singleItem();
164     ec->shape_editor->set_item(item, SH_KNOTHOLDER);
167 static void sp_arc_context_setup(SPEventContext *ec)
169     SPArcContext *ac = SP_ARC_CONTEXT(ec);
170     Inkscape::Selection *selection = sp_desktop_selection(ec->desktop);
172     if (((SPEventContextClass *) parent_class)->setup) {
173         ((SPEventContextClass *) parent_class)->setup(ec);
174     }
176     ec->shape_editor = new ShapeEditor(ec->desktop);
178     SPItem *item = sp_desktop_selection(ec->desktop)->singleItem();
179     if (item) {
180         ec->shape_editor->set_item(item, SH_KNOTHOLDER);
181     }
183     ac->sel_changed_connection.disconnect();
184     ac->sel_changed_connection = selection->connectChanged(
185         sigc::bind(sigc::ptr_fun(&sp_arc_context_selection_changed), (gpointer) ac)
186         );
188     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
189     if (prefs->getBool("/tools/shapes/selcue")) {
190         ec->enableSelectionCue();
191     }
193     if (prefs->getBool("/tools/shapes/gradientdrag")) {
194         ec->enableGrDrag();
195     }
197     ac->_message_context = new Inkscape::MessageContext(ec->desktop->messageStack());
200 static gint sp_arc_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
202     SPDesktop *desktop = event_context->desktop;
203     gint ret = FALSE;
205     switch (event->type) {
206         case GDK_BUTTON_PRESS:
207             if (event->button.button == 1 && !event_context->space_panning) {
208                 Inkscape::setup_for_drag_start(desktop, event_context, event);
209                 ret = TRUE;
210             }
211             break;
212             // motion and release are always on root (why?)
213         default:
214             break;
215     }
217     if (((SPEventContextClass *) parent_class)->item_handler) {
218         ret = ((SPEventContextClass *) parent_class)->item_handler(event_context, item, event);
219     }
221     return ret;
224 static gint sp_arc_context_root_handler(SPEventContext *event_context, GdkEvent *event)
226     static bool dragging;
228     SPDesktop *desktop = event_context->desktop;
229     Inkscape::Selection *selection = sp_desktop_selection(desktop);
230     SPArcContext *ac = SP_ARC_CONTEXT(event_context);
231     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
233     event_context->tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
235     gint ret = FALSE;
237     switch (event->type) {
238         case GDK_BUTTON_PRESS:
239             if (event->button.button == 1 && !event_context->space_panning) {
241                 dragging = true;
242                 ac->center = Inkscape::setup_for_drag_start(desktop, event_context, event);
244                 /* Snap center */
245                 SnapManager &m = desktop->namedview->snap_manager;
246                 m.setup(desktop);
247                 Geom::Point pt2g = to_2geom(ac->center);
248                 m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, pt2g, Inkscape::SNAPSOURCE_HANDLE);
249                 ac->center = from_2geom(pt2g);
251                 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
252                                     GDK_KEY_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
253                                     GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK,
254                                     NULL, event->button.time);
255                 ret = TRUE;
256             }
257             break;
258         case GDK_MOTION_NOTIFY:
259             if (dragging && (event->motion.state & GDK_BUTTON1_MASK) && !event_context->space_panning) {
261                 if ( event_context->within_tolerance
262                      && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
263                      && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
264                     break; // do not drag if we're within tolerance from origin
265                 }
266                 // Once the user has moved farther than tolerance from the original location
267                 // (indicating they intend to draw, not click), then always process the
268                 // motion notify coordinates as given (no snapping back to origin)
269                 event_context->within_tolerance = false;
271                 Geom::Point const motion_w(event->motion.x, event->motion.y);
272                 Geom::Point motion_dt(desktop->w2d(motion_w));
274                 sp_arc_drag(ac, motion_dt, event->motion.state);
276                 gobble_motion_events(GDK_BUTTON1_MASK);
278                 ret = TRUE;
279             }
280             break;
281         case GDK_BUTTON_RELEASE:
282             event_context->xp = event_context->yp = 0;
283             if (event->button.button == 1 && !event_context->space_panning) {
284                 dragging = false;
285                 sp_event_context_discard_delayed_snap_event(event_context);
286                 if (!event_context->within_tolerance) {
287                     // we've been dragging, finish the arc
288                         sp_arc_finish(ac);
289                 } else if (event_context->item_to_select) {
290                     // no dragging, select clicked item if any
291                     if (event->button.state & GDK_SHIFT_MASK) {
292                         selection->toggle(event_context->item_to_select);
293                     } else {
294                         selection->set(event_context->item_to_select);
295                     }
296                 } else {
297                     // click in an empty space
298                     selection->clear();
299                 }
300                 event_context->xp = 0;
301                 event_context->yp = 0;
302                 event_context->item_to_select = NULL;
303                 ret = TRUE;
304             }
305             sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), event->button.time);
306             break;
307         case GDK_KEY_PRESS:
308             switch (get_group0_keyval (&event->key)) {
309                 case GDK_Alt_L:
310                 case GDK_Alt_R:
311                 case GDK_Control_L:
312                 case GDK_Control_R:
313                 case GDK_Shift_L:
314                 case GDK_Shift_R:
315                 case GDK_Meta_L:  // Meta is when you press Shift+Alt (at least on my machine)
316                 case GDK_Meta_R:
317                     if (!dragging) {
318                         sp_event_show_modifier_tip(event_context->defaultMessageContext(), event,
319                                                    _("<b>Ctrl</b>: make circle or integer-ratio ellipse, snap arc/segment angle"),
320                                                    _("<b>Shift</b>: draw around the starting point"),
321                                                    NULL);
322                     }
323                     break;
324                 case GDK_Up:
325                 case GDK_Down:
326                 case GDK_KP_Up:
327                 case GDK_KP_Down:
328                     // prevent the zoom field from activation
329                     if (!MOD__CTRL_ONLY)
330                         ret = TRUE;
331                     break;
332                 case GDK_x:
333                 case GDK_X:
334                     if (MOD__ALT_ONLY) {
335                         desktop->setToolboxFocusTo ("altx-arc");
336                         ret = TRUE;
337                     }
338                     break;
339                 case GDK_Escape:
340                         if (dragging) {
341                                 dragging = false;
342                                 sp_event_context_discard_delayed_snap_event(event_context);
343                                 // if drawing, cancel, otherwise pass it up for deselecting
344                                                 sp_arc_cancel(ac);
345                                                 ret = TRUE;
346                                         }
347                         break;
348                 case GDK_space:
349                     if (dragging) {
350                         sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
351                                               event->button.time);
352                         dragging = false;
353                         sp_event_context_discard_delayed_snap_event(event_context);
354                         if (!event_context->within_tolerance) {
355                             // we've been dragging, finish the arc
356                             sp_arc_finish(ac);
357                         }
358                         // do not return true, so that space would work switching to selector
359                     }
360                     break;
362                 default:
363                     break;
364             }
365             break;
366         case GDK_KEY_RELEASE:
367             switch (event->key.keyval) {
368                 case GDK_Alt_L:
369                 case GDK_Alt_R:
370                 case GDK_Control_L:
371                 case GDK_Control_R:
372                 case GDK_Shift_L:
373                 case GDK_Shift_R:
374                 case GDK_Meta_L:  // Meta is when you press Shift+Alt
375                 case GDK_Meta_R:
376                     event_context->defaultMessageContext()->clear();
377                     break;
378                 default:
379                     break;
380             }
381             break;
382         default:
383             break;
384     }
386     if (!ret) {
387         if (((SPEventContextClass *) parent_class)->root_handler) {
388             ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
389         }
390     }
392     return ret;
395 static void sp_arc_drag(SPArcContext *ac, Geom::Point pt, guint state)
397     SPDesktop *desktop = SP_EVENT_CONTEXT(ac)->desktop;
399     if (!ac->item) {
401         if (Inkscape::have_viable_layer(desktop, ac->_message_context) == false) {
402             return;
403         }
405         /* Create object */
406         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
407         Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
408         repr->setAttribute("sodipodi:type", "arc");
410         /* Set style */
411         sp_desktop_apply_style_tool(desktop, repr, "/tools/shapes/arc", false);
413         ac->item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
414         Inkscape::GC::release(repr);
415         ac->item->transform = sp_item_i2doc_affine(SP_ITEM(desktop->currentLayer())).inverse();
416         ac->item->updateRepr();
418         sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
419     }
421     bool ctrl_save = false;
422     if ((state & GDK_MOD1_MASK) && (state & GDK_CONTROL_MASK) && !(state & GDK_SHIFT_MASK)) {
423         // if Alt is pressed without Shift in addition to Control, temporarily drop the CONTROL mask
424         // so that the ellipse is not constrained to integer ratios
425         ctrl_save = true;
426         state = state ^ GDK_CONTROL_MASK;
427     }
428     Geom::Rect r = Inkscape::snap_rectangular_box(desktop, ac->item, pt, ac->center, state);
429     if (ctrl_save) {
430         state = state ^ GDK_CONTROL_MASK;
431     }
433     Geom::Point dir = r.dimensions() / 2;
434     if (state & GDK_MOD1_MASK) {
435         /* With Alt let the ellipse pass through the mouse pointer */
436         Geom::Point c = r.midpoint();
437         if (!ctrl_save) {
438             if (fabs(dir[Geom::X]) > 1E-6 && fabs(dir[Geom::Y]) > 1E-6) {
439                 Geom::Matrix const i2d (sp_item_i2d_affine (ac->item));
440                 Geom::Point new_dir = pt * i2d - c;
441                 new_dir[Geom::X] *= dir[Geom::Y] / dir[Geom::X];
442                 double lambda = new_dir.length() / dir[Geom::Y];
443                 r = Geom::Rect (c - lambda*dir, c + lambda*dir);
444             }
445         } else {
446             /* with Alt+Ctrl (without Shift) we generate a perfect circle
447                with diameter click point <--> mouse pointer */
448                 double l = dir.length();
449                 Geom::Point d (l, l);
450                 r = Geom::Rect (c - d, c + d);
451         }
452     }
454     sp_arc_position_set(SP_ARC(ac->item),
455                         r.midpoint()[Geom::X], r.midpoint()[Geom::Y],
456                         r.dimensions()[Geom::X] / 2, r.dimensions()[Geom::Y] / 2);
458     double rdimx = r.dimensions()[Geom::X];
459     double rdimy = r.dimensions()[Geom::Y];
460     GString *xs = SP_PX_TO_METRIC_STRING(rdimx, desktop->namedview->getDefaultMetric());
461     GString *ys = SP_PX_TO_METRIC_STRING(rdimy, desktop->namedview->getDefaultMetric());
462     if (state & GDK_CONTROL_MASK) {
463         int ratio_x, ratio_y;
464         if (fabs (rdimx) > fabs (rdimy)) {
465             ratio_x = (int) rint (rdimx / rdimy);
466             ratio_y = 1;
467         } else {
468             ratio_x = 1;
469             ratio_y = (int) rint (rdimy / rdimx);
470         }
471         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);
472     } else {
473         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);
474     }
475     g_string_free(xs, FALSE);
476     g_string_free(ys, FALSE);
479 static void sp_arc_finish(SPArcContext *ac)
481     ac->_message_context->clear();
483     if (ac->item != NULL) {
485         SPGenericEllipse *ge = SP_GENERICELLIPSE(SP_ARC(ac->item));
486                 if (ge->rx.computed == 0 || ge->ry.computed == 0) {
487                         sp_arc_cancel(ac); // Don't allow the creating of zero sized arc, for example when the start and and point snap to the snap grid point
488                         return;
489                 }
491         SPDesktop *desktop = SP_EVENT_CONTEXT(ac)->desktop;
493         SP_OBJECT(ac->item)->updateRepr();
495         sp_canvas_end_forced_full_redraws(desktop->canvas);
497         sp_desktop_selection(desktop)->set(ac->item);
498         sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_ARC,
499                          _("Create ellipse"));
501         ac->item = NULL;
502     }
505 static void sp_arc_cancel(SPArcContext *ac)
507         SPDesktop *desktop = SP_EVENT_CONTEXT(ac)->desktop;
509         sp_desktop_selection(desktop)->clear();
510         sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), 0);
512     if (ac->item != NULL) {
513         SP_OBJECT(ac->item)->deleteObject();
514         ac->item = NULL;
515     }
517     ac->within_tolerance = false;
518     ac->xp = 0;
519     ac->yp = 0;
520     ac->item_to_select = NULL;
522     sp_canvas_end_forced_full_redraws(desktop->canvas);
524     sp_document_cancel(sp_desktop_document(desktop));
528 /*
529   Local Variables:
530   mode:c++
531   c-file-style:"stroustrup"
532   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
533   indent-tabs-mode:nil
534   fill-column:99
535   End:
536 */
537 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :