Code

NR => Geom for context-fns
[inkscape.git] / src / arc-context.cpp
1 #define __SP_ARC_CONTEXT_C__
3 /** \file Ellipse drawing context. */
5 /*
6  * Authors:
7  *   Mitsuru Oka
8  *   Lauris Kaplinski <lauris@kaplinski.com>
9  *   bulia byak <buliabyak@users.sf.net>
10  *
11  * Copyright (C) 2006      Johan Engelen <johan@shouraizou.nl>
12  * Copyright (C) 2002      Mitsuru Oka
13  * Copyright (C) 2000-2002 Lauris Kaplinski
14  * Copyright (C) 2000-2001 Ximian, Inc.
15  *
16  * Released under GNU GPL, read the file 'COPYING' for more information
17  */
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22 #include <gdk/gdkkeysyms.h>
24 #include "macros.h"
25 #include <glibmm/i18n.h>
26 #include "display/sp-canvas.h"
27 #include "sp-ellipse.h"
28 #include "document.h"
29 #include "sp-namedview.h"
30 #include "selection.h"
31 #include "desktop-handles.h"
32 #include "snap.h"
33 #include "pixmaps/cursor-ellipse.xpm"
34 #include "sp-metrics.h"
35 #include "xml/repr.h"
36 #include "xml/node-event-vector.h"
37 #include "object-edit.h"
38 #include "prefs-utils.h"
39 #include "message-context.h"
40 #include "desktop.h"
41 #include "desktop-style.h"
42 #include "context-fns.h"
43 #include "verbs.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     event_context->shape_repr = NULL;
108     event_context->shape_knot_holder = NULL;
110     arc_context->item = NULL;
112     new (&arc_context->sel_changed_connection) sigc::connection();
115 static void sp_arc_context_dispose(GObject *object)
117     SPEventContext *ec = SP_EVENT_CONTEXT(object);
118     SPArcContext *ac = SP_ARC_CONTEXT(object);
120     ec->enableGrDrag(false);
122     ac->sel_changed_connection.disconnect();
123     ac->sel_changed_connection.~connection();
125     if (ec->shape_knot_holder) {
126         delete ec->shape_knot_holder;
127         ec->shape_knot_holder = NULL;
128     }
130     if (ec->shape_repr) { // remove old listener
131         sp_repr_remove_listener_by_data(ec->shape_repr, ec);
132         Inkscape::GC::release(ec->shape_repr);
133         ec->shape_repr = 0;
134     }
136     /* fixme: This is necessary because we do not grab */
137     if (ac->item) {
138         sp_arc_finish(ac);
139     }
141     delete ac->_message_context;
143     G_OBJECT_CLASS(parent_class)->dispose(object);
146 static Inkscape::XML::NodeEventVector ec_shape_repr_events = {
147     NULL, /* child_added */
148     NULL, /* child_removed */
149     ec_shape_event_attr_changed,
150     NULL, /* content_changed */
151     NULL  /* order_changed */
152 };
154 /**
155 \brief  Callback that processes the "changed" signal on the selection;
156 destroys old and creates new knotholder.
157 */
158 void sp_arc_context_selection_changed(Inkscape::Selection * selection, gpointer data)
160     SPArcContext *ac = SP_ARC_CONTEXT(data);
161     SPEventContext *ec = SP_EVENT_CONTEXT(ac);
163     if (ec->shape_knot_holder) { // desktroy knotholder
164         delete ec->shape_knot_holder;
165         ec->shape_knot_holder = NULL;
166     }
168     if (ec->shape_repr) { // remove old listener
169         sp_repr_remove_listener_by_data(ec->shape_repr, ec);
170         Inkscape::GC::release(ec->shape_repr);
171         ec->shape_repr = 0;
172     }
174     SPItem *item = selection ? selection->singleItem() : NULL;
175     if (item) {
176         ec->shape_knot_holder = sp_item_knot_holder(item, ec->desktop);
177         Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
178         if (shape_repr) {
179             ec->shape_repr = shape_repr;
180             Inkscape::GC::anchor(shape_repr);
181             sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
182         }
183     }
186 static void sp_arc_context_setup(SPEventContext *ec)
188     SPArcContext *ac = SP_ARC_CONTEXT(ec);
189     Inkscape::Selection *selection = sp_desktop_selection(ec->desktop);
191     if (((SPEventContextClass *) parent_class)->setup) {
192         ((SPEventContextClass *) parent_class)->setup(ec);
193     }
195     SPItem *item = selection->singleItem();
197     if (item) {
198         ec->shape_knot_holder = sp_item_knot_holder(item, ec->desktop);
199         Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
200         if (shape_repr) {
201             ec->shape_repr = shape_repr;
202             Inkscape::GC::anchor(shape_repr);
203             sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
204         }
205     }
207     ac->sel_changed_connection.disconnect();
208     ac->sel_changed_connection = selection->connectChanged(
209         sigc::bind(sigc::ptr_fun(&sp_arc_context_selection_changed), (gpointer) ac)
210         );
212     if (prefs_get_int_attribute("tools.shapes", "selcue", 0) != 0) {
213         ec->enableSelectionCue();
214     }
216     if (prefs_get_int_attribute("tools.shapes", "gradientdrag", 0) != 0) {
217         ec->enableGrDrag();
218     }
220     ac->_message_context = new Inkscape::MessageContext(ec->desktop->messageStack());
223 static gint sp_arc_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
225     SPDesktop *desktop = event_context->desktop;
226     gint ret = FALSE;
228     switch (event->type) {
229         case GDK_BUTTON_PRESS:
230             if (event->button.button == 1 && !event_context->space_panning) {
231                 Inkscape::setup_for_drag_start(desktop, event_context, event);
232                 ret = TRUE;
233             }
234             break;
235             // motion and release are always on root (why?)
236         default:
237             break;
238     }
240     if (((SPEventContextClass *) parent_class)->item_handler) {
241         ret = ((SPEventContextClass *) parent_class)->item_handler(event_context, item, event);
242     }
244     return ret;
247 static gint sp_arc_context_root_handler(SPEventContext *event_context, GdkEvent *event)
249     static bool dragging;
251     SPDesktop *desktop = event_context->desktop;
252     Inkscape::Selection *selection = sp_desktop_selection(desktop);
253     SPArcContext *ac = SP_ARC_CONTEXT(event_context);
255     event_context->tolerance = prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100);
257     gint ret = FALSE;
259     switch (event->type) {
260         case GDK_BUTTON_PRESS:
261             if (event->button.button == 1 && !event_context->space_panning) {
263                 dragging = true;
264                 ac->center = Inkscape::setup_for_drag_start(desktop, event_context, event);
265                 
266                 /* Snap center */
267                 SnapManager &m = desktop->namedview->snap_manager;
268                 m.setup(desktop);
269                 Geom::Point pt2g = to_2geom(ac->center);
270                 m.freeSnapReturnByRef(Inkscape::Snapper::SNAPPOINT_NODE, pt2g);
271                 ac->center = from_2geom(pt2g);
273                 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
274                                     GDK_KEY_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
275                                     GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK,
276                                     NULL, event->button.time);
277                 ret = TRUE;
278             }
279             break;
280         case GDK_MOTION_NOTIFY:
281             if (dragging && (event->motion.state & GDK_BUTTON1_MASK) && !event_context->space_panning) {
283                 if ( event_context->within_tolerance
284                      && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
285                      && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
286                     break; // do not drag if we're within tolerance from origin
287                 }
288                 // Once the user has moved farther than tolerance from the original location
289                 // (indicating they intend to draw, not click), then always process the
290                 // motion notify coordinates as given (no snapping back to origin)
291                 event_context->within_tolerance = false;
293                 Geom::Point const motion_w(event->motion.x, event->motion.y);
294                 Geom::Point motion_dt(desktop->w2d(motion_w));
295                 
296                 sp_arc_drag(ac, motion_dt, event->motion.state);
298                 gobble_motion_events(GDK_BUTTON1_MASK);
300                 ret = TRUE;
301             }
302             break;
303         case GDK_BUTTON_RELEASE:
304             event_context->xp = event_context->yp = 0;
305             if (event->button.button == 1 && !event_context->space_panning) {
306                 dragging = false;
307                 if (!event_context->within_tolerance) {
308                     // we've been dragging, finish the arc
309                     sp_arc_finish(ac);
310                 } else if (event_context->item_to_select) {
311                     // no dragging, select clicked item if any
312                     if (event->button.state & GDK_SHIFT_MASK) {
313                         selection->toggle(event_context->item_to_select);
314                     } else {
315                         selection->set(event_context->item_to_select);
316                     }
317                 } else {
318                     // click in an empty space
319                     selection->clear();
320                 }
321                 event_context->xp = 0;
322                 event_context->yp = 0;
323                 event_context->item_to_select = NULL;
324                 ret = TRUE;
325             }
326             sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), event->button.time);
327             break;
328         case GDK_KEY_PRESS:
329             switch (get_group0_keyval (&event->key)) {
330                 case GDK_Alt_L:
331                 case GDK_Alt_R:
332                 case GDK_Control_L:
333                 case GDK_Control_R:
334                 case GDK_Shift_L:
335                 case GDK_Shift_R:
336                 case GDK_Meta_L:  // Meta is when you press Shift+Alt (at least on my machine)
337                 case GDK_Meta_R:
338                     if (!dragging) {
339                         sp_event_show_modifier_tip(event_context->defaultMessageContext(), event,
340                                                    _("<b>Ctrl</b>: make circle or integer-ratio ellipse, snap arc/segment angle"),
341                                                    _("<b>Shift</b>: draw around the starting point"),
342                                                    NULL);
343                     }
344                     break;
345                 case GDK_Up:
346                 case GDK_Down:
347                 case GDK_KP_Up:
348                 case GDK_KP_Down:
349                     // prevent the zoom field from activation
350                     if (!MOD__CTRL_ONLY)
351                         ret = TRUE;
352                     break;
353                 case GDK_x:
354                 case GDK_X:
355                     if (MOD__ALT_ONLY) {
356                         desktop->setToolboxFocusTo ("altx-arc");
357                         ret = TRUE;
358                     }
359                     break;
360                 case GDK_Escape:
361                     sp_desktop_selection(desktop)->clear();
362                     //TODO: make dragging escapable by Esc
363                     break;
365                 case GDK_space:
366                     if (dragging) {
367                         sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
368                                               event->button.time);
369                         dragging = false;
370                         if (!event_context->within_tolerance) {
371                             // we've been dragging, finish the rect
372                             sp_arc_finish(ac);
373                         }
374                         // do not return true, so that space would work switching to selector
375                     }
376                     break;
378                 default:
379                     break;
380             }
381             break;
382         case GDK_KEY_RELEASE:
383             switch (event->key.keyval) {
384                 case GDK_Alt_L:
385                 case GDK_Alt_R:
386                 case GDK_Control_L:
387                 case GDK_Control_R:
388                 case GDK_Shift_L:
389                 case GDK_Shift_R:
390                 case GDK_Meta_L:  // Meta is when you press Shift+Alt
391                 case GDK_Meta_R:
392                     event_context->defaultMessageContext()->clear();
393                     break;
394                 default:
395                     break;
396             }
397             break;
398         default:
399             break;
400     }
402     if (!ret) {
403         if (((SPEventContextClass *) parent_class)->root_handler) {
404             ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
405         }
406     }
408     return ret;
411 static void sp_arc_drag(SPArcContext *ac, Geom::Point pt, guint state)
413     SPDesktop *desktop = SP_EVENT_CONTEXT(ac)->desktop;
415     if (!ac->item) {
417         if (Inkscape::have_viable_layer(desktop, ac->_message_context) == false) {
418             return;
419         }
421         /* Create object */
422         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
423         Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
424         repr->setAttribute("sodipodi:type", "arc");
426         /* Set style */
427         sp_desktop_apply_style_tool(desktop, repr, "tools.shapes.arc", false);
429         ac->item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
430         Inkscape::GC::release(repr);
431         ac->item->transform = SP_ITEM(desktop->currentRoot())->getRelativeTransform(desktop->currentLayer());
432         ac->item->updateRepr();
434         sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
435     }
437     bool ctrl_save = false;
438     if ((state & GDK_MOD1_MASK) && (state & GDK_CONTROL_MASK) && !(state & GDK_SHIFT_MASK)) {
439         // if Alt is pressed without Shift in addition to Control, temporarily drop the CONTROL mask
440         // so that the ellipse is not constrained to integer ratios
441         ctrl_save = true;
442         state = state ^ GDK_CONTROL_MASK;
443     }
444     Geom::Rect r = Inkscape::snap_rectangular_box(desktop, ac->item, pt, ac->center, state);
445     if (ctrl_save) {
446         state = state ^ GDK_CONTROL_MASK;
447     }
449     Geom::Point dir = r.dimensions() / 2;
450     if (state & GDK_MOD1_MASK) {
451         /* With Alt let the ellipse pass through the mouse pointer */
452         Geom::Point c = r.midpoint();
453         if (!ctrl_save) {
454             if (fabs(dir[Geom::X]) > 1E-6 && fabs(dir[Geom::Y]) > 1E-6) {
455                 Geom::Matrix const i2d (sp_item_i2d_affine (ac->item));
456                 Geom::Point new_dir = pt * i2d - c;
457                 new_dir[Geom::X] *= dir[Geom::Y] / dir[Geom::X];
458                 double lambda = new_dir.length() / dir[Geom::Y];
459                 r = Geom::Rect (c - lambda*dir, c + lambda*dir);
460             }
461         } else {
462             /* with Alt+Ctrl (without Shift) we generate a perfect circle
463                with diameter click point <--> mouse pointer */
464                 double l = dir.length();
465                 Geom::Point d (l, l);
466                 r = Geom::Rect (c - d, c + d);
467         }
468     }
470     sp_arc_position_set(SP_ARC(ac->item),
471                         r.midpoint()[Geom::X], r.midpoint()[Geom::Y],
472                         r.dimensions()[Geom::X] / 2, r.dimensions()[Geom::Y] / 2);
474     double rdimx = r.dimensions()[Geom::X];
475     double rdimy = r.dimensions()[Geom::Y];
476     GString *xs = SP_PX_TO_METRIC_STRING(rdimx, desktop->namedview->getDefaultMetric());
477     GString *ys = SP_PX_TO_METRIC_STRING(rdimy, desktop->namedview->getDefaultMetric());
478     if (state & GDK_CONTROL_MASK) {
479         int ratio_x, ratio_y;
480         if (fabs (rdimx) > fabs (rdimy)) {
481             ratio_x = (int) rint (rdimx / rdimy);
482             ratio_y = 1;
483         } else {
484             ratio_x = 1;
485             ratio_y = (int) rint (rdimy / rdimx);
486         }
487         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);
488     } else {
489         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);
490     }
491     g_string_free(xs, FALSE);
492     g_string_free(ys, FALSE);
495 static void sp_arc_finish(SPArcContext *ac)
497     ac->_message_context->clear();
499     if (ac->item != NULL) {
500         SPDesktop *desktop = SP_EVENT_CONTEXT(ac)->desktop;
502         SP_OBJECT(ac->item)->updateRepr();
504         sp_canvas_end_forced_full_redraws(desktop->canvas);
505         
506         sp_desktop_selection(desktop)->set(ac->item);
507         sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_ARC, 
508                          _("Create ellipse"));
510         ac->item = NULL;
511     }
515 /*
516   Local Variables:
517   mode:c++
518   c-file-style:"stroustrup"
519   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
520   indent-tabs-mode:nil
521   fill-column:99
522   End:
523 */
524 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :