Code

turns out, all these synthesize_events were not necessary at all - they just fired...
[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 "sp-metrics.h"
34 #include "xml/repr.h"
35 #include "xml/node-event-vector.h"
36 #include "object-edit.h"
37 #include "prefs-utils.h"
38 #include "message-context.h"
39 #include "desktop.h"
40 #include "desktop-style.h"
41 #include "context-fns.h"
43 #include "arc-context.h"
45 static void sp_arc_context_class_init(SPArcContextClass *klass);
46 static void sp_arc_context_init(SPArcContext *arc_context);
47 static void sp_arc_context_dispose(GObject *object);
49 static void sp_arc_context_setup(SPEventContext *ec);
50 static gint sp_arc_context_root_handler(SPEventContext *event_context, GdkEvent *event);
51 static gint sp_arc_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
53 static void sp_arc_drag(SPArcContext *ec, NR::Point pt, guint state);
54 static void sp_arc_finish(SPArcContext *ec);
56 static SPEventContextClass *parent_class;
58 GtkType sp_arc_context_get_type()
59 {
60     static GType type = 0;
61     if (!type) {
62         GTypeInfo info = {
63             sizeof(SPArcContextClass),
64             NULL, NULL,
65             (GClassInitFunc) sp_arc_context_class_init,
66             NULL, NULL,
67             sizeof(SPArcContext),
68             4,
69             (GInstanceInitFunc) sp_arc_context_init,
70             NULL,   /* value_table */
71         };
72         type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPArcContext", &info, (GTypeFlags) 0);
73     }
74     return type;
75 }
77 static void sp_arc_context_class_init(SPArcContextClass *klass)
78 {
80     GObjectClass *object_class = (GObjectClass *) klass;
81     SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
83     parent_class = (SPEventContextClass*) g_type_class_peek_parent(klass);
85     object_class->dispose = sp_arc_context_dispose;
87     event_context_class->setup = sp_arc_context_setup;
88     event_context_class->root_handler = sp_arc_context_root_handler;
89     event_context_class->item_handler = sp_arc_context_item_handler;
90 }
92 static void sp_arc_context_init(SPArcContext *arc_context)
93 {
94     SPEventContext *event_context = SP_EVENT_CONTEXT(arc_context);
96     event_context->cursor_shape = cursor_ellipse_xpm;
97     event_context->hot_x = 4;
98     event_context->hot_y = 4;
99     event_context->xp = 0;
100     event_context->yp = 0;
101     event_context->tolerance = 0;
102     event_context->within_tolerance = false;
103     event_context->item_to_select = NULL;
105     event_context->shape_repr = NULL;
106     event_context->shape_knot_holder = 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     if (ec->shape_knot_holder) {
124         sp_knot_holder_destroy(ec->shape_knot_holder);
125         ec->shape_knot_holder = NULL;
126     }
128     if (ec->shape_repr) { // remove old listener
129         sp_repr_remove_listener_by_data(ec->shape_repr, ec);
130         Inkscape::GC::release(ec->shape_repr);
131         ec->shape_repr = 0;
132     }
134     /* fixme: This is necessary because we do not grab */
135     if (ac->item) {
136         sp_arc_finish(ac);
137     }
139     delete ac->_message_context;
141     G_OBJECT_CLASS(parent_class)->dispose(object);
144 static Inkscape::XML::NodeEventVector ec_shape_repr_events = {
145     NULL, /* child_added */
146     NULL, /* child_removed */
147     ec_shape_event_attr_changed,
148     NULL, /* content_changed */
149     NULL  /* order_changed */
150 };
152 /**
153 \brief  Callback that processes the "changed" signal on the selection;
154 destroys old and creates new knotholder.
155 */
156 void sp_arc_context_selection_changed(Inkscape::Selection * selection, gpointer data)
158     SPArcContext *ac = SP_ARC_CONTEXT(data);
159     SPEventContext *ec = SP_EVENT_CONTEXT(ac);
161     if (ec->shape_knot_holder) { // desktroy knotholder
162         sp_knot_holder_destroy(ec->shape_knot_holder);
163         ec->shape_knot_holder = NULL;
164     }
166     if (ec->shape_repr) { // remove old listener
167         sp_repr_remove_listener_by_data(ec->shape_repr, ec);
168         Inkscape::GC::release(ec->shape_repr);
169         ec->shape_repr = 0;
170     }
172     SPItem *item = selection ? selection->singleItem() : NULL;
173     if (item) {
174         ec->shape_knot_holder = sp_item_knot_holder(item, ec->desktop);
175         Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
176         if (shape_repr) {
177             ec->shape_repr = shape_repr;
178             Inkscape::GC::anchor(shape_repr);
179             sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
180         }
181     }
184 static void sp_arc_context_setup(SPEventContext *ec)
186     SPArcContext *ac = SP_ARC_CONTEXT(ec);
187     Inkscape::Selection *selection = SP_DT_SELECTION(ec->desktop);
189     if (((SPEventContextClass *) parent_class)->setup) {
190         ((SPEventContextClass *) parent_class)->setup(ec);
191     }
193     SPItem *item = selection->singleItem();
195     if (item) {
196         ec->shape_knot_holder = sp_item_knot_holder(item, ec->desktop);
197         Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR(item);
198         if (shape_repr) {
199             ec->shape_repr = shape_repr;
200             Inkscape::GC::anchor(shape_repr);
201             sp_repr_add_listener(shape_repr, &ec_shape_repr_events, ec);
202         }
203     }
205     ac->sel_changed_connection.disconnect();
206     ac->sel_changed_connection = selection->connectChanged(
207         sigc::bind(sigc::ptr_fun(&sp_arc_context_selection_changed), (gpointer) ac)
208         );
210     if (prefs_get_int_attribute("tools.shapes", "selcue", 0) != 0) {
211         ec->enableSelectionCue();
212     }
214     if (prefs_get_int_attribute("tools.shapes", "gradientdrag", 0) != 0) {
215         ec->enableGrDrag();
216     }
218     ac->_message_context = new Inkscape::MessageContext(ec->desktop->messageStack());
221 static gint sp_arc_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
223     SPDesktop *desktop = event_context->desktop;
224     gint ret = FALSE;
226     switch (event->type) {
227         case GDK_BUTTON_PRESS:
228             if (event->button.button == 1) {
229                 Inkscape::setup_for_drag_start(desktop, event_context, event);
230                 ret = TRUE;
231             }
232             break;
233             // motion and release are always on root (why?)
234         default:
235             break;
236     }
238     if (((SPEventContextClass *) parent_class)->item_handler) {
239         ret = ((SPEventContextClass *) parent_class)->item_handler(event_context, item, event);
240     }
242     return ret;
245 static gint sp_arc_context_root_handler(SPEventContext *event_context, GdkEvent *event)
247     static bool dragging;
249     SPDesktop *desktop = event_context->desktop;
250     Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
251     SPArcContext *ac = SP_ARC_CONTEXT(event_context);
253     event_context->tolerance = prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100);
255     gint ret = FALSE;
257     switch (event->type) {
258         case GDK_BUTTON_PRESS:
259             if (event->button.button == 1) {
261                 dragging = true;
262                 ac->center = Inkscape::setup_for_drag_start(desktop, event_context, event);
264                 SnapManager const m(desktop->namedview);
265                 ac->center = m.freeSnap(Inkscape::Snapper::SNAP_POINT, ac->center, ac->item).getPoint();
266                 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
267                                     GDK_KEY_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
268                                     GDK_POINTER_MOTION_MASK | GDK_BUTTON_PRESS_MASK,
269                                     NULL, event->button.time);
270                 ret = TRUE;
271             }
272             break;
273         case GDK_MOTION_NOTIFY:
274             if (dragging && event->motion.state && GDK_BUTTON1_MASK) {
276                 if ( event_context->within_tolerance
277                      && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
278                      && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
279                     break; // do not drag if we're within tolerance from origin
280                 }
281                 // Once the user has moved farther than tolerance from the original location
282                 // (indicating they intend to draw, not click), then always process the
283                 // motion notify coordinates as given (no snapping back to origin)
284                 event_context->within_tolerance = false;
286                 NR::Point const motion_w(event->motion.x, event->motion.y);
287                 NR::Point const motion_dt(desktop->w2d(motion_w));
288                 sp_arc_drag(ac, motion_dt, event->motion.state);
289                 ret = TRUE;
290             }
291             break;
292         case GDK_BUTTON_RELEASE:
293             event_context->xp = event_context->yp = 0;
294             if (event->button.button == 1) {
295                 dragging = false;
296                 if (!event_context->within_tolerance) {
297                     // we've been dragging, finish the arc
298                     sp_arc_finish(ac);
299                 } else if (event_context->item_to_select) {
300                     // no dragging, select clicked item if any
301                     if (event->button.state & GDK_SHIFT_MASK) {
302                         selection->toggle(event_context->item_to_select);
303                     } else {
304                         selection->set(event_context->item_to_select);
305                     }
306                 } else {
307                     // click in an empty space
308                     selection->clear();
309                 }
310                 event_context->xp = 0;
311                 event_context->yp = 0;
312                 event_context->item_to_select = NULL;
313                 ret = TRUE;
314             }
315             sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), event->button.time);
316             break;
317         case GDK_KEY_PRESS:
318             switch (get_group0_keyval (&event->key)) {
319                 case GDK_Alt_L:
320                 case GDK_Alt_R:
321                 case GDK_Control_L:
322                 case GDK_Control_R:
323                 case GDK_Shift_L:
324                 case GDK_Shift_R:
325                 case GDK_Meta_L:  // Meta is when you press Shift+Alt (at least on my machine)
326                 case GDK_Meta_R:
327                     sp_event_show_modifier_tip(event_context->defaultMessageContext(), event,
328                                                _("<b>Ctrl</b>: make circle or integer-ratio ellipse, snap arc/segment angle"),
329                                                _("<b>Shift</b>: draw around the starting point"),
330                                                NULL);
331                     break;
332                 case GDK_Up:
333                 case GDK_Down:
334                 case GDK_KP_Up:
335                 case GDK_KP_Down:
336                     // prevent the zoom field from activation
337                     if (!MOD__CTRL_ONLY)
338                         ret = TRUE;
339                     break;
340                 case GDK_x:
341                 case GDK_X:
342                     if (MOD__ALT_ONLY) {
343                         desktop->setToolboxFocusTo ("altx-arc");
344                         ret = TRUE;
345                     }
346                     break;
347                 case GDK_Escape:
348                     SP_DT_SELECTION(desktop)->clear();
349                     //TODO: make dragging escapable by Esc
350                 default:
351                     break;
352             }
353             break;
354         case GDK_KEY_RELEASE:
355             switch (event->key.keyval) {
356                 case GDK_Alt_L:
357                 case GDK_Alt_R:
358                 case GDK_Control_L:
359                 case GDK_Control_R:
360                 case GDK_Shift_L:
361                 case GDK_Shift_R:
362                 case GDK_Meta_L:  // Meta is when you press Shift+Alt
363                 case GDK_Meta_R:
364                     event_context->defaultMessageContext()->clear();
365                     break;
366                 default:
367                     break;
368             }
369             break;
370         default:
371             break;
372     }
374     if (!ret) {
375         if (((SPEventContextClass *) parent_class)->root_handler) {
376             ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
377         }
378     }
380     return ret;
383 static void sp_arc_drag(SPArcContext *ac, NR::Point pt, guint state)
385     SPDesktop *desktop = SP_EVENT_CONTEXT(ac)->desktop;
387     if (!ac->item) {
389         if (Inkscape::have_viable_layer(desktop, ac->_message_context) == false) {
390             return;
391         }
393         /* Create object */
394         Inkscape::XML::Node *repr = sp_repr_new("svg:path");
395         repr->setAttribute("sodipodi:type", "arc");
397         /* Set style */
398         sp_desktop_apply_style_tool(desktop, repr, "tools.shapes.arc", false);
400         ac->item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
401         Inkscape::GC::release(repr);
402         ac->item->transform = SP_ITEM(desktop->currentRoot())->getRelativeTransform(desktop->currentLayer());
403         ac->item->updateRepr();
404     }
406     NR::Rect const r = Inkscape::snap_rectangular_box(desktop, ac->item, pt, ac->center, state);
408     sp_arc_position_set(SP_ARC(ac->item),
409                         r.midpoint()[NR::X], r.midpoint()[NR::Y],
410                         r.dimensions()[NR::X] / 2, r.dimensions()[NR::Y] / 2);
412     GString *xs = SP_PX_TO_METRIC_STRING(r.dimensions()[NR::X], desktop->namedview->getDefaultMetric());
413     GString *ys = SP_PX_TO_METRIC_STRING(r.dimensions()[NR::Y], desktop->namedview->getDefaultMetric());
414     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);
415     g_string_free(xs, FALSE);
416     g_string_free(ys, FALSE);
419 static void sp_arc_finish(SPArcContext *ac)
421     ac->_message_context->clear();
423     if (ac->item != NULL) {
424         SPDesktop *desktop = SP_EVENT_CONTEXT(ac)->desktop;
426         SP_OBJECT(ac->item)->updateRepr();
428         SP_DT_SELECTION(desktop)->set(ac->item);
429         sp_document_done(SP_DT_DOCUMENT(desktop));
431         ac->item = NULL;
432     }
436 /*
437   Local Variables:
438   mode:c++
439   c-file-style:"stroustrup"
440   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
441   indent-tabs-mode:nil
442   fill-column:99
443   End:
444 */
445 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :