Code

undo annotation
[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) 2000-2002 Lauris Kaplinski
12  * Copyright (C) 2000-2001 Ximian, Inc.
13  * Copyright (C) 2002 Mitsuru Oka
14  *
15  * Released under GNU GPL, read the file 'COPYING' for more information
16  */
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 "pixmaps/cursor-ellipse.pixbuf"
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, NR::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->cursor_pixbuf = gdk_pixbuf_new_from_inline(
100             -1,
101             cursor_ellipse_pixbuf,
102             FALSE,
103             NULL);  
104     event_context->hot_x = 4;
105     event_context->hot_y = 4;
106     event_context->xp = 0;
107     event_context->yp = 0;
108     event_context->tolerance = 0;
109     event_context->within_tolerance = false;
110     event_context->item_to_select = NULL;
112     event_context->shape_repr = NULL;
113     event_context->shape_knot_holder = NULL;
115     arc_context->item = NULL;
117     new (&arc_context->sel_changed_connection) sigc::connection();
120 static void sp_arc_context_dispose(GObject *object)
122     SPEventContext *ec = SP_EVENT_CONTEXT(object);
123     SPArcContext *ac = SP_ARC_CONTEXT(object);
125     ec->enableGrDrag(false);
127     ac->sel_changed_connection.disconnect();
128     ac->sel_changed_connection.~connection();
130     if (ec->shape_knot_holder) {
131         sp_knot_holder_destroy(ec->shape_knot_holder);
132         ec->shape_knot_holder = NULL;
133     }
135     if (ec->shape_repr) { // remove old listener
136         sp_repr_remove_listener_by_data(ec->shape_repr, ec);
137         Inkscape::GC::release(ec->shape_repr);
138         ec->shape_repr = 0;
139     }
141     /* fixme: This is necessary because we do not grab */
142     if (ac->item) {
143         sp_arc_finish(ac);
144     }
146     delete ac->_message_context;
148     G_OBJECT_CLASS(parent_class)->dispose(object);
151 static Inkscape::XML::NodeEventVector ec_shape_repr_events = {
152     NULL, /* child_added */
153     NULL, /* child_removed */
154     ec_shape_event_attr_changed,
155     NULL, /* content_changed */
156     NULL  /* order_changed */
157 };
159 /**
160 \brief  Callback that processes the "changed" signal on the selection;
161 destroys old and creates new knotholder.
162 */
163 void sp_arc_context_selection_changed(Inkscape::Selection * selection, gpointer data)
165     SPArcContext *ac = SP_ARC_CONTEXT(data);
166     SPEventContext *ec = SP_EVENT_CONTEXT(ac);
168     if (ec->shape_knot_holder) { // desktroy knotholder
169         sp_knot_holder_destroy(ec->shape_knot_holder);
170         ec->shape_knot_holder = NULL;
171     }
173     if (ec->shape_repr) { // remove old listener
174         sp_repr_remove_listener_by_data(ec->shape_repr, ec);
175         Inkscape::GC::release(ec->shape_repr);
176         ec->shape_repr = 0;
177     }
179     SPItem *item = selection ? selection->singleItem() : NULL;
180     if (item) {
181         ec->shape_knot_holder = sp_item_knot_holder(item, ec->desktop);
182         Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
183         if (shape_repr) {
184             ec->shape_repr = shape_repr;
185             Inkscape::GC::anchor(shape_repr);
186             sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
187         }
188     }
191 static void sp_arc_context_setup(SPEventContext *ec)
193     SPArcContext *ac = SP_ARC_CONTEXT(ec);
194     Inkscape::Selection *selection = sp_desktop_selection(ec->desktop);
196     if (((SPEventContextClass *) parent_class)->setup) {
197         ((SPEventContextClass *) parent_class)->setup(ec);
198     }
200     SPItem *item = selection->singleItem();
202     if (item) {
203         ec->shape_knot_holder = sp_item_knot_holder(item, ec->desktop);
204         Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
205         if (shape_repr) {
206             ec->shape_repr = shape_repr;
207             Inkscape::GC::anchor(shape_repr);
208             sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
209         }
210     }
212     ac->sel_changed_connection.disconnect();
213     ac->sel_changed_connection = selection->connectChanged(
214         sigc::bind(sigc::ptr_fun(&sp_arc_context_selection_changed), (gpointer) ac)
215         );
217     if (prefs_get_int_attribute("tools.shapes", "selcue", 0) != 0) {
218         ec->enableSelectionCue();
219     }
221     if (prefs_get_int_attribute("tools.shapes", "gradientdrag", 0) != 0) {
222         ec->enableGrDrag();
223     }
225     ac->_message_context = new Inkscape::MessageContext(ec->desktop->messageStack());
228 static gint sp_arc_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
230     SPDesktop *desktop = event_context->desktop;
231     gint ret = FALSE;
233     switch (event->type) {
234         case GDK_BUTTON_PRESS:
235             if (event->button.button == 1) {
236                 Inkscape::setup_for_drag_start(desktop, event_context, event);
237                 ret = TRUE;
238             }
239             break;
240             // motion and release are always on root (why?)
241         default:
242             break;
243     }
245     if (((SPEventContextClass *) parent_class)->item_handler) {
246         ret = ((SPEventContextClass *) parent_class)->item_handler(event_context, item, event);
247     }
249     return ret;
252 static gint sp_arc_context_root_handler(SPEventContext *event_context, GdkEvent *event)
254     static bool dragging;
256     SPDesktop *desktop = event_context->desktop;
257     Inkscape::Selection *selection = sp_desktop_selection(desktop);
258     SPArcContext *ac = SP_ARC_CONTEXT(event_context);
260     event_context->tolerance = prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100);
262     gint ret = FALSE;
264     switch (event->type) {
265         case GDK_BUTTON_PRESS:
266             if (event->button.button == 1) {
268                 dragging = true;
269                 ac->center = Inkscape::setup_for_drag_start(desktop, event_context, event);
271                 SnapManager const &m = desktop->namedview->snap_manager;
272                 ac->center = m.freeSnap(Inkscape::Snapper::SNAP_POINT, ac->center, ac->item).getPoint();
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_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) {
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                 NR::Point const motion_w(event->motion.x, event->motion.y);
294                 NR::Point const motion_dt(desktop->w2d(motion_w));
295                 sp_arc_drag(ac, motion_dt, event->motion.state);
296                 ret = TRUE;
297             }
298             break;
299         case GDK_BUTTON_RELEASE:
300             event_context->xp = event_context->yp = 0;
301             if (event->button.button == 1) {
302                 dragging = false;
303                 if (!event_context->within_tolerance) {
304                     // we've been dragging, finish the arc
305                     sp_arc_finish(ac);
306                 } else if (event_context->item_to_select) {
307                     // no dragging, select clicked item if any
308                     if (event->button.state & GDK_SHIFT_MASK) {
309                         selection->toggle(event_context->item_to_select);
310                     } else {
311                         selection->set(event_context->item_to_select);
312                     }
313                 } else {
314                     // click in an empty space
315                     selection->clear();
316                 }
317                 event_context->xp = 0;
318                 event_context->yp = 0;
319                 event_context->item_to_select = NULL;
320                 ret = TRUE;
321             }
322             sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), event->button.time);
323             break;
324         case GDK_KEY_PRESS:
325             switch (get_group0_keyval (&event->key)) {
326                 case GDK_Alt_L:
327                 case GDK_Alt_R:
328                 case GDK_Control_L:
329                 case GDK_Control_R:
330                 case GDK_Shift_L:
331                 case GDK_Shift_R:
332                 case GDK_Meta_L:  // Meta is when you press Shift+Alt (at least on my machine)
333                 case GDK_Meta_R:
334                     sp_event_show_modifier_tip(event_context->defaultMessageContext(), event,
335                                                _("<b>Ctrl</b>: make circle or integer-ratio ellipse, snap arc/segment angle"),
336                                                _("<b>Shift</b>: draw around the starting point"),
337                                                NULL);
338                     break;
339                 case GDK_Up:
340                 case GDK_Down:
341                 case GDK_KP_Up:
342                 case GDK_KP_Down:
343                     // prevent the zoom field from activation
344                     if (!MOD__CTRL_ONLY)
345                         ret = TRUE;
346                     break;
347                 case GDK_x:
348                 case GDK_X:
349                     if (MOD__ALT_ONLY) {
350                         desktop->setToolboxFocusTo ("altx-arc");
351                         ret = TRUE;
352                     }
353                     break;
354                 case GDK_Escape:
355                     sp_desktop_selection(desktop)->clear();
356                     //TODO: make dragging escapable by Esc
357                 default:
358                     break;
359             }
360             break;
361         case GDK_KEY_RELEASE:
362             switch (event->key.keyval) {
363                 case GDK_Alt_L:
364                 case GDK_Alt_R:
365                 case GDK_Control_L:
366                 case GDK_Control_R:
367                 case GDK_Shift_L:
368                 case GDK_Shift_R:
369                 case GDK_Meta_L:  // Meta is when you press Shift+Alt
370                 case GDK_Meta_R:
371                     event_context->defaultMessageContext()->clear();
372                     break;
373                 default:
374                     break;
375             }
376             break;
377         default:
378             break;
379     }
381     if (!ret) {
382         if (((SPEventContextClass *) parent_class)->root_handler) {
383             ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
384         }
385     }
387     return ret;
390 static void sp_arc_drag(SPArcContext *ac, NR::Point pt, guint state)
392     SPDesktop *desktop = SP_EVENT_CONTEXT(ac)->desktop;
394     if (!ac->item) {
396         if (Inkscape::have_viable_layer(desktop, ac->_message_context) == false) {
397             return;
398         }
400         /* Create object */
401         Inkscape::XML::Node *repr = sp_repr_new("svg:path");
402         repr->setAttribute("sodipodi:type", "arc");
404         /* Set style */
405         sp_desktop_apply_style_tool(desktop, repr, "tools.shapes.arc", false);
407         ac->item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
408         Inkscape::GC::release(repr);
409         ac->item->transform = SP_ITEM(desktop->currentRoot())->getRelativeTransform(desktop->currentLayer());
410         ac->item->updateRepr();
411     }
413     NR::Rect const r = Inkscape::snap_rectangular_box(desktop, ac->item, pt, ac->center, state);
415     sp_arc_position_set(SP_ARC(ac->item),
416                         r.midpoint()[NR::X], r.midpoint()[NR::Y],
417                         r.dimensions()[NR::X] / 2, r.dimensions()[NR::Y] / 2);
419     GString *xs = SP_PX_TO_METRIC_STRING(r.dimensions()[NR::X], desktop->namedview->getDefaultMetric());
420     GString *ys = SP_PX_TO_METRIC_STRING(r.dimensions()[NR::Y], desktop->namedview->getDefaultMetric());
421     ac->_message_context->setF(Inkscape::NORMAL_MESSAGE, _("<b>Ellipse</b>: %s &#215; %s; with <b>Ctrl</b> to make circle or integer-ratio ellipse; with <b>Shift</b> to draw around the starting point"), xs->str, ys->str);
422     g_string_free(xs, FALSE);
423     g_string_free(ys, FALSE);
426 static void sp_arc_finish(SPArcContext *ac)
428     ac->_message_context->clear();
430     if (ac->item != NULL) {
431         SPDesktop *desktop = SP_EVENT_CONTEXT(ac)->desktop;
433         SP_OBJECT(ac->item)->updateRepr();
435         sp_desktop_selection(desktop)->set(ac->item);
436         sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_ARC, 
437                          _("Create ellipse"));
439         ac->item = NULL;
440     }
444 /*
445   Local Variables:
446   mode:c++
447   c-file-style:"stroustrup"
448   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
449   indent-tabs-mode:nil
450   fill-column:99
451   End:
452 */
453 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :