Code

From trunk
[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"
44 #include "arc-context.h"
46 static void sp_arc_context_class_init(SPArcContextClass *klass);
47 static void sp_arc_context_init(SPArcContext *arc_context);
48 static void sp_arc_context_dispose(GObject *object);
50 static void sp_arc_context_setup(SPEventContext *ec);
51 static gint sp_arc_context_root_handler(SPEventContext *event_context, GdkEvent *event);
52 static gint sp_arc_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
54 static void sp_arc_drag(SPArcContext *ec, Geom::Point pt, guint state);
55 static void sp_arc_finish(SPArcContext *ec);
57 static SPEventContextClass *parent_class;
59 GtkType sp_arc_context_get_type()
60 {
61     static GType type = 0;
62     if (!type) {
63         GTypeInfo info = {
64             sizeof(SPArcContextClass),
65             NULL, NULL,
66             (GClassInitFunc) sp_arc_context_class_init,
67             NULL, NULL,
68             sizeof(SPArcContext),
69             4,
70             (GInstanceInitFunc) sp_arc_context_init,
71             NULL,   /* value_table */
72         };
73         type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPArcContext", &info, (GTypeFlags) 0);
74     }
75     return type;
76 }
78 static void sp_arc_context_class_init(SPArcContextClass *klass)
79 {
81     GObjectClass *object_class = (GObjectClass *) klass;
82     SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
84     parent_class = (SPEventContextClass*) g_type_class_peek_parent(klass);
86     object_class->dispose = sp_arc_context_dispose;
88     event_context_class->setup = sp_arc_context_setup;
89     event_context_class->root_handler = sp_arc_context_root_handler;
90     event_context_class->item_handler = sp_arc_context_item_handler;
91 }
93 static void sp_arc_context_init(SPArcContext *arc_context)
94 {
95     SPEventContext *event_context = SP_EVENT_CONTEXT(arc_context);
97     event_context->cursor_shape = cursor_ellipse_xpm;
98     event_context->hot_x = 4;
99     event_context->hot_y = 4;
100     event_context->xp = 0;
101     event_context->yp = 0;
102     event_context->tolerance = 0;
103     event_context->within_tolerance = false;
104     event_context->item_to_select = NULL;
106     event_context->shape_repr = NULL;
107     event_context->shape_knot_holder = NULL;
109     arc_context->item = NULL;
111     new (&arc_context->sel_changed_connection) sigc::connection();
114 static void sp_arc_context_dispose(GObject *object)
116     SPEventContext *ec = SP_EVENT_CONTEXT(object);
117     SPArcContext *ac = SP_ARC_CONTEXT(object);
119     ec->enableGrDrag(false);
121     ac->sel_changed_connection.disconnect();
122     ac->sel_changed_connection.~connection();
124     if (ec->shape_knot_holder) {
125         delete ec->shape_knot_holder;
126         ec->shape_knot_holder = NULL;
127     }
129     if (ec->shape_repr) { // remove old listener
130         sp_repr_remove_listener_by_data(ec->shape_repr, ec);
131         Inkscape::GC::release(ec->shape_repr);
132         ec->shape_repr = 0;
133     }
135     /* fixme: This is necessary because we do not grab */
136     if (ac->item) {
137         sp_arc_finish(ac);
138     }
140     delete ac->_message_context;
142     G_OBJECT_CLASS(parent_class)->dispose(object);
145 static Inkscape::XML::NodeEventVector ec_shape_repr_events = {
146     NULL, /* child_added */
147     NULL, /* child_removed */
148     ec_shape_event_attr_changed,
149     NULL, /* content_changed */
150     NULL  /* order_changed */
151 };
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     if (ec->shape_knot_holder) { // desktroy knotholder
163         delete ec->shape_knot_holder;
164         ec->shape_knot_holder = NULL;
165     }
167     if (ec->shape_repr) { // remove old listener
168         sp_repr_remove_listener_by_data(ec->shape_repr, ec);
169         Inkscape::GC::release(ec->shape_repr);
170         ec->shape_repr = 0;
171     }
173     SPItem *item = selection ? selection->singleItem() : NULL;
174     if (item) {
175         ec->shape_knot_holder = sp_item_knot_holder(item, ec->desktop);
176         Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
177         if (shape_repr) {
178             ec->shape_repr = shape_repr;
179             Inkscape::GC::anchor(shape_repr);
180             sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
181         }
182     }
185 static void sp_arc_context_setup(SPEventContext *ec)
187     SPArcContext *ac = SP_ARC_CONTEXT(ec);
188     Inkscape::Selection *selection = sp_desktop_selection(ec->desktop);
190     if (((SPEventContextClass *) parent_class)->setup) {
191         ((SPEventContextClass *) parent_class)->setup(ec);
192     }
194     SPItem *item = selection->singleItem();
196     if (item) {
197         ec->shape_knot_holder = sp_item_knot_holder(item, ec->desktop);
198         Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
199         if (shape_repr) {
200             ec->shape_repr = shape_repr;
201             Inkscape::GC::anchor(shape_repr);
202             sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
203         }
204     }
206     ac->sel_changed_connection.disconnect();
207     ac->sel_changed_connection = selection->connectChanged(
208         sigc::bind(sigc::ptr_fun(&sp_arc_context_selection_changed), (gpointer) ac)
209         );
211     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
212     if (prefs->getBool("/tools/shapes/selcue")) {
213         ec->enableSelectionCue();
214     }
216     if (prefs->getBool("/tools/shapes/gradientdrag")) {
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);
254     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
256     event_context->tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
258     gint ret = FALSE;
260     switch (event->type) {
261         case GDK_BUTTON_PRESS:
262             if (event->button.button == 1 && !event_context->space_panning) {
264                 dragging = true;
265                 ac->center = Inkscape::setup_for_drag_start(desktop, event_context, event);
266                 
267                 /* Snap center */
268                 SnapManager &m = desktop->namedview->snap_manager;
269                 m.setup(desktop);
270                 Geom::Point pt2g = to_2geom(ac->center);
271                 m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, pt2g);
272                 ac->center = from_2geom(pt2g);
274                 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
275                                     GDK_KEY_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
276                                     GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK,
277                                     NULL, event->button.time);
278                 ret = TRUE;
279             }
280             break;
281         case GDK_MOTION_NOTIFY:
282             if (dragging && (event->motion.state & GDK_BUTTON1_MASK) && !event_context->space_panning) {
284                 if ( event_context->within_tolerance
285                      && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
286                      && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
287                     break; // do not drag if we're within tolerance from origin
288                 }
289                 // Once the user has moved farther than tolerance from the original location
290                 // (indicating they intend to draw, not click), then always process the
291                 // motion notify coordinates as given (no snapping back to origin)
292                 event_context->within_tolerance = false;
294                 Geom::Point const motion_w(event->motion.x, event->motion.y);
295                 Geom::Point motion_dt(desktop->w2d(motion_w));
296                 
297                 sp_arc_drag(ac, motion_dt, event->motion.state);
299                 gobble_motion_events(GDK_BUTTON1_MASK);
301                 ret = TRUE;
302             }
303             break;
304         case GDK_BUTTON_RELEASE:
305             event_context->xp = event_context->yp = 0;
306             if (event->button.button == 1 && !event_context->space_panning) {
307                 dragging = false;
308                 if (!event_context->within_tolerance) {
309                     // we've been dragging, finish the arc
310                     sp_arc_finish(ac);
311                 } else if (event_context->item_to_select) {
312                     // no dragging, select clicked item if any
313                     if (event->button.state & GDK_SHIFT_MASK) {
314                         selection->toggle(event_context->item_to_select);
315                     } else {
316                         selection->set(event_context->item_to_select);
317                     }
318                 } else {
319                     // click in an empty space
320                     selection->clear();
321                 }
322                 event_context->xp = 0;
323                 event_context->yp = 0;
324                 event_context->item_to_select = NULL;
325                 ret = TRUE;
326             }
327             sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), event->button.time);
328             break;
329         case GDK_KEY_PRESS:
330             switch (get_group0_keyval (&event->key)) {
331                 case GDK_Alt_L:
332                 case GDK_Alt_R:
333                 case GDK_Control_L:
334                 case GDK_Control_R:
335                 case GDK_Shift_L:
336                 case GDK_Shift_R:
337                 case GDK_Meta_L:  // Meta is when you press Shift+Alt (at least on my machine)
338                 case GDK_Meta_R:
339                     if (!dragging) {
340                         sp_event_show_modifier_tip(event_context->defaultMessageContext(), event,
341                                                    _("<b>Ctrl</b>: make circle or integer-ratio ellipse, snap arc/segment angle"),
342                                                    _("<b>Shift</b>: draw around the starting point"),
343                                                    NULL);
344                     }
345                     break;
346                 case GDK_Up:
347                 case GDK_Down:
348                 case GDK_KP_Up:
349                 case GDK_KP_Down:
350                     // prevent the zoom field from activation
351                     if (!MOD__CTRL_ONLY)
352                         ret = TRUE;
353                     break;
354                 case GDK_x:
355                 case GDK_X:
356                     if (MOD__ALT_ONLY) {
357                         desktop->setToolboxFocusTo ("altx-arc");
358                         ret = TRUE;
359                     }
360                     break;
361                 case GDK_Escape:
362                     sp_desktop_selection(desktop)->clear();
363                     //TODO: make dragging escapable by Esc
364                     break;
366                 case GDK_space:
367                     if (dragging) {
368                         sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
369                                               event->button.time);
370                         dragging = false;
371                         if (!event_context->within_tolerance) {
372                             // we've been dragging, finish the rect
373                             sp_arc_finish(ac);
374                         }
375                         // do not return true, so that space would work switching to selector
376                     }
377                     break;
379                 default:
380                     break;
381             }
382             break;
383         case GDK_KEY_RELEASE:
384             switch (event->key.keyval) {
385                 case GDK_Alt_L:
386                 case GDK_Alt_R:
387                 case GDK_Control_L:
388                 case GDK_Control_R:
389                 case GDK_Shift_L:
390                 case GDK_Shift_R:
391                 case GDK_Meta_L:  // Meta is when you press Shift+Alt
392                 case GDK_Meta_R:
393                     event_context->defaultMessageContext()->clear();
394                     break;
395                 default:
396                     break;
397             }
398             break;
399         default:
400             break;
401     }
403     if (!ret) {
404         if (((SPEventContextClass *) parent_class)->root_handler) {
405             ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
406         }
407     }
409     return ret;
412 static void sp_arc_drag(SPArcContext *ac, Geom::Point pt, guint state)
414     SPDesktop *desktop = SP_EVENT_CONTEXT(ac)->desktop;
416     if (!ac->item) {
418         if (Inkscape::have_viable_layer(desktop, ac->_message_context) == false) {
419             return;
420         }
422         /* Create object */
423         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
424         Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
425         repr->setAttribute("sodipodi:type", "arc");
427         /* Set style */
428         sp_desktop_apply_style_tool(desktop, repr, "/tools/shapes/arc", false);
430         ac->item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
431         Inkscape::GC::release(repr);
432         ac->item->transform = SP_ITEM(desktop->currentRoot())->getRelativeTransform(desktop->currentLayer());
433         ac->item->updateRepr();
435         sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
436     }
438     bool ctrl_save = false;
439     if ((state & GDK_MOD1_MASK) && (state & GDK_CONTROL_MASK) && !(state & GDK_SHIFT_MASK)) {
440         // if Alt is pressed without Shift in addition to Control, temporarily drop the CONTROL mask
441         // so that the ellipse is not constrained to integer ratios
442         ctrl_save = true;
443         state = state ^ GDK_CONTROL_MASK;
444     }
445     Geom::Rect r = Inkscape::snap_rectangular_box(desktop, ac->item, pt, ac->center, state);
446     if (ctrl_save) {
447         state = state ^ GDK_CONTROL_MASK;
448     }
450     Geom::Point dir = r.dimensions() / 2;
451     if (state & GDK_MOD1_MASK) {
452         /* With Alt let the ellipse pass through the mouse pointer */
453         Geom::Point c = r.midpoint();
454         if (!ctrl_save) {
455             if (fabs(dir[Geom::X]) > 1E-6 && fabs(dir[Geom::Y]) > 1E-6) {
456                 Geom::Matrix const i2d (sp_item_i2d_affine (ac->item));
457                 Geom::Point new_dir = pt * i2d - c;
458                 new_dir[Geom::X] *= dir[Geom::Y] / dir[Geom::X];
459                 double lambda = new_dir.length() / dir[Geom::Y];
460                 r = Geom::Rect (c - lambda*dir, c + lambda*dir);
461             }
462         } else {
463             /* with Alt+Ctrl (without Shift) we generate a perfect circle
464                with diameter click point <--> mouse pointer */
465                 double l = dir.length();
466                 Geom::Point d (l, l);
467                 r = Geom::Rect (c - d, c + d);
468         }
469     }
471     sp_arc_position_set(SP_ARC(ac->item),
472                         r.midpoint()[Geom::X], r.midpoint()[Geom::Y],
473                         r.dimensions()[Geom::X] / 2, r.dimensions()[Geom::Y] / 2);
475     double rdimx = r.dimensions()[Geom::X];
476     double rdimy = r.dimensions()[Geom::Y];
477     GString *xs = SP_PX_TO_METRIC_STRING(rdimx, desktop->namedview->getDefaultMetric());
478     GString *ys = SP_PX_TO_METRIC_STRING(rdimy, desktop->namedview->getDefaultMetric());
479     if (state & GDK_CONTROL_MASK) {
480         int ratio_x, ratio_y;
481         if (fabs (rdimx) > fabs (rdimy)) {
482             ratio_x = (int) rint (rdimx / rdimy);
483             ratio_y = 1;
484         } else {
485             ratio_x = 1;
486             ratio_y = (int) rint (rdimy / rdimx);
487         }
488         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);
489     } else {
490         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);
491     }
492     g_string_free(xs, FALSE);
493     g_string_free(ys, FALSE);
496 static void sp_arc_finish(SPArcContext *ac)
498     ac->_message_context->clear();
500     if (ac->item != NULL) {
501         SPDesktop *desktop = SP_EVENT_CONTEXT(ac)->desktop;
503         SP_OBJECT(ac->item)->updateRepr();
505         sp_canvas_end_forced_full_redraws(desktop->canvas);
506         
507         sp_desktop_selection(desktop)->set(ac->item);
508         sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_ARC, 
509                          _("Create ellipse"));
511         ac->item = NULL;
512     }
516 /*
517   Local Variables:
518   mode:c++
519   c-file-style:"stroustrup"
520   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
521   indent-tabs-mode:nil
522   fill-column:99
523   End:
524 */
525 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :