Code

clean left warning on revision 12272
[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 gint sp_arc_context_root_handler(SPEventContext *event_context, GdkEvent *event);
54 static gint sp_arc_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
56 static void sp_arc_drag(SPArcContext *ec, Geom::Point pt, guint state);
57 static void sp_arc_finish(SPArcContext *ec);
59 static SPEventContextClass *parent_class;
61 GtkType sp_arc_context_get_type()
62 {
63     static GType type = 0;
64     if (!type) {
65         GTypeInfo info = {
66             sizeof(SPArcContextClass),
67             NULL, NULL,
68             (GClassInitFunc) sp_arc_context_class_init,
69             NULL, NULL,
70             sizeof(SPArcContext),
71             4,
72             (GInstanceInitFunc) sp_arc_context_init,
73             NULL,   /* value_table */
74         };
75         type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPArcContext", &info, (GTypeFlags) 0);
76     }
77     return type;
78 }
80 static void sp_arc_context_class_init(SPArcContextClass *klass)
81 {
83     GObjectClass *object_class = (GObjectClass *) klass;
84     SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
86     parent_class = (SPEventContextClass*) g_type_class_peek_parent(klass);
88     object_class->dispose = sp_arc_context_dispose;
90     event_context_class->setup = sp_arc_context_setup;
91     event_context_class->root_handler = sp_arc_context_root_handler;
92     event_context_class->item_handler = sp_arc_context_item_handler;
93 }
95 static void sp_arc_context_init(SPArcContext *arc_context)
96 {
97     SPEventContext *event_context = SP_EVENT_CONTEXT(arc_context);
99     event_context->cursor_shape = cursor_ellipse_xpm;
100     event_context->hot_x = 4;
101     event_context->hot_y = 4;
102     event_context->xp = 0;
103     event_context->yp = 0;
104     event_context->tolerance = 0;
105     event_context->within_tolerance = false;
106     event_context->item_to_select = NULL;
108     arc_context->item = NULL;
110     new (&arc_context->sel_changed_connection) sigc::connection();
113 static void sp_arc_context_dispose(GObject *object)
115     SPEventContext *ec = SP_EVENT_CONTEXT(object);
116     SPArcContext *ac = SP_ARC_CONTEXT(object);
118     ec->enableGrDrag(false);
120     ac->sel_changed_connection.disconnect();
121     ac->sel_changed_connection.~connection();
123     delete ec->shape_editor;
124     ec->shape_editor = NULL;
126     /* fixme: This is necessary because we do not grab */
127     if (ac->item) {
128         sp_arc_finish(ac);
129     }
131     delete ac->_message_context;
133     G_OBJECT_CLASS(parent_class)->dispose(object);
136 /**
137 \brief  Callback that processes the "changed" signal on the selection;
138 destroys old and creates new knotholder.
139 */
140 void sp_arc_context_selection_changed(Inkscape::Selection * selection, gpointer data)
142     SPArcContext *ac = SP_ARC_CONTEXT(data);
143     SPEventContext *ec = SP_EVENT_CONTEXT(ac);
145     ec->shape_editor->unset_item(SH_KNOTHOLDER);
146     SPItem *item = selection->singleItem();
147     ec->shape_editor->set_item(item, SH_KNOTHOLDER);
150 static void sp_arc_context_setup(SPEventContext *ec)
152     SPArcContext *ac = SP_ARC_CONTEXT(ec);
153     Inkscape::Selection *selection = sp_desktop_selection(ec->desktop);
155     if (((SPEventContextClass *) parent_class)->setup) {
156         ((SPEventContextClass *) parent_class)->setup(ec);
157     }
159     ec->shape_editor = new ShapeEditor(ec->desktop);
161     SPItem *item = sp_desktop_selection(ec->desktop)->singleItem();
162     if (item) {
163         ec->shape_editor->set_item(item, SH_KNOTHOLDER);
164     }
166     ac->sel_changed_connection.disconnect();
167     ac->sel_changed_connection = selection->connectChanged(
168         sigc::bind(sigc::ptr_fun(&sp_arc_context_selection_changed), (gpointer) ac)
169         );
171     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
172     if (prefs->getBool("/tools/shapes/selcue")) {
173         ec->enableSelectionCue();
174     }
176     if (prefs->getBool("/tools/shapes/gradientdrag")) {
177         ec->enableGrDrag();
178     }
180     ac->_message_context = new Inkscape::MessageContext(ec->desktop->messageStack());
183 static gint sp_arc_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
185     SPDesktop *desktop = event_context->desktop;
186     gint ret = FALSE;
188     switch (event->type) {
189         case GDK_BUTTON_PRESS:
190             if (event->button.button == 1 && !event_context->space_panning) {
191                 Inkscape::setup_for_drag_start(desktop, event_context, event);
192                 ret = TRUE;
193             }
194             break;
195             // motion and release are always on root (why?)
196         default:
197             break;
198     }
200     if (((SPEventContextClass *) parent_class)->item_handler) {
201         ret = ((SPEventContextClass *) parent_class)->item_handler(event_context, item, event);
202     }
204     return ret;
207 static gint sp_arc_context_root_handler(SPEventContext *event_context, GdkEvent *event)
209     static bool dragging;
211     SPDesktop *desktop = event_context->desktop;
212     Inkscape::Selection *selection = sp_desktop_selection(desktop);
213     SPArcContext *ac = SP_ARC_CONTEXT(event_context);
214     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
216     event_context->tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
218     gint ret = FALSE;
220     switch (event->type) {
221         case GDK_BUTTON_PRESS:
222             if (event->button.button == 1 && !event_context->space_panning) {
224                 dragging = true;
225                 sp_event_context_snap_window_open(event_context);
226                 ac->center = Inkscape::setup_for_drag_start(desktop, event_context, event);
228                 /* Snap center */
229                 SnapManager &m = desktop->namedview->snap_manager;
230                 m.setup(desktop);
231                 Geom::Point pt2g = to_2geom(ac->center);
232                 m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, pt2g, Inkscape::SNAPSOURCE_HANDLE);
233                 ac->center = from_2geom(pt2g);
235                 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
236                                     GDK_KEY_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
237                                     GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK,
238                                     NULL, event->button.time);
239                 ret = TRUE;
240             }
241             break;
242         case GDK_MOTION_NOTIFY:
243             if (dragging && (event->motion.state & GDK_BUTTON1_MASK) && !event_context->space_panning) {
245                 if ( event_context->within_tolerance
246                      && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
247                      && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
248                     break; // do not drag if we're within tolerance from origin
249                 }
250                 // Once the user has moved farther than tolerance from the original location
251                 // (indicating they intend to draw, not click), then always process the
252                 // motion notify coordinates as given (no snapping back to origin)
253                 event_context->within_tolerance = false;
255                 Geom::Point const motion_w(event->motion.x, event->motion.y);
256                 Geom::Point motion_dt(desktop->w2d(motion_w));
258                 sp_arc_drag(ac, motion_dt, event->motion.state);
260                 gobble_motion_events(GDK_BUTTON1_MASK);
262                 ret = TRUE;
263             }
264             break;
265         case GDK_BUTTON_RELEASE:
266             event_context->xp = event_context->yp = 0;
267             if (event->button.button == 1 && !event_context->space_panning) {
268                 dragging = false;
269                 sp_event_context_snap_window_closed(event_context, false); //button release will also occur on a double-click; in that case suppress warnings
270                 if (!event_context->within_tolerance) {
271                     // we've been dragging, finish the arc
272                     sp_arc_finish(ac);
273                 } else if (event_context->item_to_select) {
274                     // no dragging, select clicked item if any
275                     if (event->button.state & GDK_SHIFT_MASK) {
276                         selection->toggle(event_context->item_to_select);
277                     } else {
278                         selection->set(event_context->item_to_select);
279                     }
280                 } else {
281                     // click in an empty space
282                     selection->clear();
283                 }
284                 event_context->xp = 0;
285                 event_context->yp = 0;
286                 event_context->item_to_select = NULL;
287                 ret = TRUE;
288             }
289             sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), event->button.time);
290             break;
291         case GDK_KEY_PRESS:
292             switch (get_group0_keyval (&event->key)) {
293                 case GDK_Alt_L:
294                 case GDK_Alt_R:
295                 case GDK_Control_L:
296                 case GDK_Control_R:
297                 case GDK_Shift_L:
298                 case GDK_Shift_R:
299                 case GDK_Meta_L:  // Meta is when you press Shift+Alt (at least on my machine)
300                 case GDK_Meta_R:
301                     if (!dragging) {
302                         sp_event_show_modifier_tip(event_context->defaultMessageContext(), event,
303                                                    _("<b>Ctrl</b>: make circle or integer-ratio ellipse, snap arc/segment angle"),
304                                                    _("<b>Shift</b>: draw around the starting point"),
305                                                    NULL);
306                     }
307                     break;
308                 case GDK_Up:
309                 case GDK_Down:
310                 case GDK_KP_Up:
311                 case GDK_KP_Down:
312                     // prevent the zoom field from activation
313                     if (!MOD__CTRL_ONLY)
314                         ret = TRUE;
315                     break;
316                 case GDK_x:
317                 case GDK_X:
318                     if (MOD__ALT_ONLY) {
319                         desktop->setToolboxFocusTo ("altx-arc");
320                         ret = TRUE;
321                     }
322                     break;
323                 case GDK_Escape:
324                     sp_desktop_selection(desktop)->clear();
325                     //TODO: make dragging escapable by Esc
326                     break;
328                 case GDK_space:
329                     if (dragging) {
330                         sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
331                                               event->button.time);
332                         dragging = false;
333                         sp_event_context_snap_window_closed(event_context);
334                         if (!event_context->within_tolerance) {
335                             // we've been dragging, finish the rect
336                             sp_arc_finish(ac);
337                         }
338                         // do not return true, so that space would work switching to selector
339                     }
340                     break;
342                 default:
343                     break;
344             }
345             break;
346         case GDK_KEY_RELEASE:
347             switch (event->key.keyval) {
348                 case GDK_Alt_L:
349                 case GDK_Alt_R:
350                 case GDK_Control_L:
351                 case GDK_Control_R:
352                 case GDK_Shift_L:
353                 case GDK_Shift_R:
354                 case GDK_Meta_L:  // Meta is when you press Shift+Alt
355                 case GDK_Meta_R:
356                     event_context->defaultMessageContext()->clear();
357                     break;
358                 default:
359                     break;
360             }
361             break;
362         default:
363             break;
364     }
366     if (!ret) {
367         if (((SPEventContextClass *) parent_class)->root_handler) {
368             ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
369         }
370     }
372     return ret;
375 static void sp_arc_drag(SPArcContext *ac, Geom::Point pt, guint state)
377     SPDesktop *desktop = SP_EVENT_CONTEXT(ac)->desktop;
379     if (!ac->item) {
381         if (Inkscape::have_viable_layer(desktop, ac->_message_context) == false) {
382             return;
383         }
385         /* Create object */
386         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
387         Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
388         repr->setAttribute("sodipodi:type", "arc");
390         /* Set style */
391         sp_desktop_apply_style_tool(desktop, repr, "/tools/shapes/arc", false);
393         ac->item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
394         Inkscape::GC::release(repr);
395         ac->item->transform = sp_item_i2doc_affine(SP_ITEM(desktop->currentLayer())).inverse();
396         ac->item->updateRepr();
398         sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
399     }
401     bool ctrl_save = false;
402     if ((state & GDK_MOD1_MASK) && (state & GDK_CONTROL_MASK) && !(state & GDK_SHIFT_MASK)) {
403         // if Alt is pressed without Shift in addition to Control, temporarily drop the CONTROL mask
404         // so that the ellipse is not constrained to integer ratios
405         ctrl_save = true;
406         state = state ^ GDK_CONTROL_MASK;
407     }
408     Geom::Rect r = Inkscape::snap_rectangular_box(desktop, ac->item, pt, ac->center, state);
409     if (ctrl_save) {
410         state = state ^ GDK_CONTROL_MASK;
411     }
413     Geom::Point dir = r.dimensions() / 2;
414     if (state & GDK_MOD1_MASK) {
415         /* With Alt let the ellipse pass through the mouse pointer */
416         Geom::Point c = r.midpoint();
417         if (!ctrl_save) {
418             if (fabs(dir[Geom::X]) > 1E-6 && fabs(dir[Geom::Y]) > 1E-6) {
419                 Geom::Matrix const i2d (sp_item_i2d_affine (ac->item));
420                 Geom::Point new_dir = pt * i2d - c;
421                 new_dir[Geom::X] *= dir[Geom::Y] / dir[Geom::X];
422                 double lambda = new_dir.length() / dir[Geom::Y];
423                 r = Geom::Rect (c - lambda*dir, c + lambda*dir);
424             }
425         } else {
426             /* with Alt+Ctrl (without Shift) we generate a perfect circle
427                with diameter click point <--> mouse pointer */
428                 double l = dir.length();
429                 Geom::Point d (l, l);
430                 r = Geom::Rect (c - d, c + d);
431         }
432     }
434     sp_arc_position_set(SP_ARC(ac->item),
435                         r.midpoint()[Geom::X], r.midpoint()[Geom::Y],
436                         r.dimensions()[Geom::X] / 2, r.dimensions()[Geom::Y] / 2);
438     double rdimx = r.dimensions()[Geom::X];
439     double rdimy = r.dimensions()[Geom::Y];
440     GString *xs = SP_PX_TO_METRIC_STRING(rdimx, desktop->namedview->getDefaultMetric());
441     GString *ys = SP_PX_TO_METRIC_STRING(rdimy, desktop->namedview->getDefaultMetric());
442     if (state & GDK_CONTROL_MASK) {
443         int ratio_x, ratio_y;
444         if (fabs (rdimx) > fabs (rdimy)) {
445             ratio_x = (int) rint (rdimx / rdimy);
446             ratio_y = 1;
447         } else {
448             ratio_x = 1;
449             ratio_y = (int) rint (rdimy / rdimx);
450         }
451         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);
452     } else {
453         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);
454     }
455     g_string_free(xs, FALSE);
456     g_string_free(ys, FALSE);
459 static void sp_arc_finish(SPArcContext *ac)
461     ac->_message_context->clear();
463     if (ac->item != NULL) {
464         SPDesktop *desktop = SP_EVENT_CONTEXT(ac)->desktop;
466         SP_OBJECT(ac->item)->updateRepr();
468         sp_canvas_end_forced_full_redraws(desktop->canvas);
470         sp_desktop_selection(desktop)->set(ac->item);
471         sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_ARC,
472                          _("Create ellipse"));
474         ac->item = NULL;
475     }
479 /*
480   Local Variables:
481   mode:c++
482   c-file-style:"stroustrup"
483   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
484   indent-tabs-mode:nil
485   fill-column:99
486   End:
487 */
488 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :