Code

Warning cleanup
[inkscape.git] / src / connector-context.cpp
1 /*
2  * Connector creation tool
3  *
4  * Authors:
5  *   Michael Wybrow <mjwybrow@users.sourceforge.net>
6  *
7  * Copyright (C) 2005-2008  Michael Wybrow
8  * Copyright (C) 2009  Monash University
9  *
10  * Released under GNU GPL, read the file 'COPYING' for more information
11  *
12  * TODO:
13  *  o  Show a visual indicator for objects with the 'avoid' property set.
14  *  o  Allow user to change a object between a path and connector through
15  *     the interface.
16  *  o  Create an interface for setting markers (arrow heads).
17  *  o  Better distinguish between paths and connectors to prevent problems
18  *     in the node tool and paths accidentally being turned into connectors
19  *     in the connector tool.  Perhaps have a way to convert between.
20  *  o  Only call libavoid's updateEndPoint as required.  Currently we do it
21  *     for both endpoints, even if only one is moving.
22  *  o  Allow user-placeable connection points.
23  *  o  Deal sanely with connectors with both endpoints attached to the
24  *     same connection point, and drawing of connectors attaching
25  *     overlapping shapes (currently tries to adjust connector to be
26  *     outside both bounding boxes).
27  *  o  Fix many special cases related to connectors updating,
28  *     e.g., copying a couple of shapes and a connector that are
29  *           attached to each other.
30  *     e.g., detach connector when it is moved or transformed in
31  *           one of the other contexts.
32  *  o  Cope with shapes whose ids change when they have attached
33  *     connectors.
34  *  o  During dragging motion, gobble up to and use the final motion event.
35  *     Gobbling away all duplicates after the current can occasionally result
36  *     in the path lagging behind the mouse cursor if it is no longer being
37  *     dragged.
38  *  o  Fix up libavoid's representation after undo actions.  It doesn't see
39  *     any transform signals and hence doesn't know shapes have moved back to
40  *     there earlier positions.
41  *  o  Decide whether drawing/editing mode should be an Inkscape preference
42  *     or the connector tool should always start in drawing mode.
43  *  o  Correct the problem with switching to the select tool when pressing
44  *     space bar (there are moments when it refuses to do so).
45  *
46  * ----------------------------------------------------------------------------
47  *
48  * mjwybrow's observations on acracan's Summer of Code connector work:
49  *
50  *  -  GUI comments:
51  *
52  *      -  Buttons for adding and removing user-specified connection
53  *      points should probably have "+" and "-" symbols on them so they
54  *      are consistent with the similar buttons for the node tool.
55  *      -  Controls on the connector tool be should be reordered logically,
56  *      possibly as follows:
57  *
58  *      *Connector*: [Polyline-radio-button] [Orthgonal-radio-button]
59  *        [Curvature-control] | *Shape*: [Avoid-button] [Dont-avoid-button]
60  *        [Spacing-control] | *Connection pts*: [Edit-mode] [Add-pt] [Rm-pt]
61  *
62  *      I think that the network layout controls be moved to the
63  *      Align and Distribute dialog (there is already the layout button
64  *      there, but no options are exposed).
65  *
66  *      I think that the style change between polyline and orthogonal
67  *      would be much clearer with two buttons (radio behaviour -- just
68  *      one is true).
69  *
70  *      The other tools show a label change from "New:" to "Change:"
71  *      depending on whether an object is selected.  We could consider
72  *      this but there may not be space.
73  *
74  *      The Add-pt and Rm-pt buttons should be greyed out (inactive) if
75  *      we are not in connection point editing mode.  And probably also
76  *      if there is no shape selected, i.e. at the times they have no
77  *      effect when clicked.
78  *
79  *      Likewise for the avoid/ignore shapes buttons.  These should be
80  *      inactive when a shape is not selected in the connector context.
81  *
82  *  -  When creating/editing connection points:
83  *
84  *      -  Strange things can happen if you have connectors selected, or
85  *      try rerouting connectors by dragging their endpoints when in
86  *      connection point editing mode.
87  *
88  *      -  Possibly the selected shape's connection points should always
89  *      be shown (i.e., have knots) when in editing mode.
90  *
91  *      -  It is a little strange to be able to place connection points
92  *      competely outside shapes.  Especially when you later can't draw
93  *      connectors to them since the knots are only visible when you
94  *      are over the shape.  I think that you should only be able to
95  *      place connection points inside or on the boundary of the shape
96  *      itself.
97  *
98  *      -  The intended ability to place a new point at the current cursor
99  *      position by pressing RETURN does not seem to work.
100  *
101  *      -  The Status bar tooltip should change to reflect editing mode
102  *      and tell the user about RETURN and how to use the tool.
103  *
104  *  -  Connection points general:
105  *
106  *      -  Connection points that were inside the shape can end up outside
107  *      after a rotation is applied to the shape in the select tool.
108  *      It doesn't seem like the correct transform is being applied to
109  *      these, or it is being applied at the wrong time.  I'd expect
110  *      connection points to rotate with the shape, and stay at the
111  *      same position "on the shape"
112  *
113  *      -  I was able to make the connectors attached to a shape fall off
114  *      the shape after scaling it.  Not sure the exact cause, but may
115  *      require more investigation/debugging.
116  *
117  *      -  The user-defined connection points should be either absolute
118  *      (as the current ones are) or defined as a percentage of the
119  *      shape.  These would be based on a toggle setting on the
120  *      toolbar, and they would be placed in exactly the same way by
121  *      the user.  The only difference would be that they would be
122  *      store as percentage positions in the SVG connection-points
123  *      property and that they would update/move automatically if the
124  *      object was resized or scaled.
125  *
126  *      -  Thinking more, I think you always want to store and think about
127  *      the positions of connection points to be pre-transform, but
128  *      obviously the shape transform is applied to them.  That way,
129  *      they will rotate and scale automatically with the shape, when
130  *      the shape transform is altered.  The Percentage version would
131  *      compute their position from the pre-transform dimensions and
132  *      then have the transform applied to them, for example.
133  *
134  *      -  The connection points in the test_connection_points.svg file
135  *      seem to follow the shape when it is moved, but connection
136  *      points I add to new shapes, do not follow the shape, either
137  *      when the shape is just moved or transformed.  There is
138  *      something wrong here.  What exactly should the behaviour be
139  *      currently?
140  *
141  *      -  I see that connection points are specified at absolute canvas
142  *      positions.  I really think that they should be specified in
143  *      shape coordinated relative to the shapes.  There may be
144  *      transforms applied to layers and the canvas which would make
145  *      specifying them quite difficult.  I'd expect a position of 0, 0
146  *      to be on the shape in question or very close to it, for example.
147  *
148  */
152 #include <gdk/gdkkeysyms.h>
153 #include <string>
154 #include <cstring>
156 #include "connector-context.h"
157 #include "pixmaps/cursor-connector.xpm"
158 #include "pixmaps/cursor-node.xpm"
159 //#include "pixmaps/cursor-node-m.xpm"
160 //#include "pixmaps/cursor-node-d.xpm"
161 #include "xml/node-event-vector.h"
162 #include "xml/repr.h"
163 #include "svg/svg.h"
164 #include "desktop.h"
165 #include "desktop-style.h"
166 #include "desktop-handles.h"
167 #include "document.h"
168 #include "message-context.h"
169 #include "message-stack.h"
170 #include "selection.h"
171 #include "inkscape.h"
172 #include "preferences.h"
173 #include "sp-path.h"
174 #include "display/canvas-bpath.h"
175 #include "display/sodipodi-ctrl.h"
176 #include <glibmm/i18n.h>
177 #include <glibmm/stringutils.h>
178 #include "snap.h"
179 #include "knot.h"
180 #include "sp-conn-end.h"
181 #include "sp-conn-end-pair.h"
182 #include "conn-avoid-ref.h"
183 #include "libavoid/vertices.h"
184 #include "libavoid/router.h"
185 #include "context-fns.h"
186 #include "sp-namedview.h"
187 #include "sp-text.h"
188 #include "sp-flowtext.h"
189 #include "display/curve.h"
191 static void sp_connector_context_class_init(SPConnectorContextClass *klass);
192 static void sp_connector_context_init(SPConnectorContext *conn_context);
193 static void sp_connector_context_dispose(GObject *object);
195 static void sp_connector_context_setup(SPEventContext *ec);
196 static void sp_connector_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val);
197 static void sp_connector_context_finish(SPEventContext *ec);
198 static gint sp_connector_context_root_handler(SPEventContext *ec, GdkEvent *event);
199 static gint sp_connector_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
201 // Stuff borrowed from DrawContext
202 static void spcc_connector_set_initial_point(SPConnectorContext *cc, Geom::Point const p);
203 static void spcc_connector_set_subsequent_point(SPConnectorContext *cc, Geom::Point const p);
204 static void spcc_connector_finish_segment(SPConnectorContext *cc, Geom::Point p);
205 static void spcc_reset_colors(SPConnectorContext *cc);
206 static void spcc_connector_finish(SPConnectorContext *cc);
207 static void spcc_concat_colors_and_flush(SPConnectorContext *cc);
208 static void spcc_flush_white(SPConnectorContext *cc, SPCurve *gc);
210 // Context event handlers
211 static gint connector_handle_button_press(SPConnectorContext *const cc, GdkEventButton const &bevent);
212 static gint connector_handle_motion_notify(SPConnectorContext *const cc, GdkEventMotion const &mevent);
213 static gint connector_handle_button_release(SPConnectorContext *const cc, GdkEventButton const &revent);
214 static gint connector_handle_key_press(SPConnectorContext *const cc, guint const keyval);
216 static void cc_active_shape_add_knot(SPDesktop* desktop, SPItem* item, ConnectionPointMap &cphandles, ConnectionPoint& cp);
217 static void cc_set_active_shape(SPConnectorContext *cc, SPItem *item);
218 static void cc_clear_active_shape(SPConnectorContext *cc);
219 static void cc_set_active_conn(SPConnectorContext *cc, SPItem *item);
220 static void cc_clear_active_conn(SPConnectorContext *cc);
221 static gchar *conn_pt_handle_test(SPConnectorContext *cc, Geom::Point& w);
222 static void cc_select_handle(SPKnot* knot);
223 static void cc_deselect_handle(SPKnot* knot);
224 static bool cc_item_is_shape(SPItem *item);
225 static void cc_selection_changed(Inkscape::Selection *selection, gpointer data);
226 static void cc_connector_rerouting_finish(SPConnectorContext *const cc,
227         Geom::Point *const p);
229 static void shape_event_attr_deleted(Inkscape::XML::Node *repr,
230         Inkscape::XML::Node *child, Inkscape::XML::Node *ref, gpointer data);
231 static void shape_event_attr_changed(Inkscape::XML::Node *repr, gchar const *name,
232         gchar const *old_value, gchar const *new_value, bool is_interactive,
233         gpointer data);
236 /*static Geom::Point connector_drag_origin_w(0, 0);
237 static bool connector_within_tolerance = false;*/
238 static SPEventContextClass *parent_class;
241 static Inkscape::XML::NodeEventVector shape_repr_events = {
242     NULL, /* child_added */
243     NULL, /* child_added */
244     shape_event_attr_changed,
245     NULL, /* content_changed */
246     NULL  /* order_changed */
247 };
249 static Inkscape::XML::NodeEventVector layer_repr_events = {
250     NULL, /* child_added */
251     shape_event_attr_deleted,
252     NULL, /* child_added */
253     NULL, /* content_changed */
254     NULL  /* order_changed */
255 };
258 GType
259 sp_connector_context_get_type(void)
261     static GType type = 0;
262     if (!type) {
263         GTypeInfo info = {
264             sizeof(SPConnectorContextClass),
265             NULL, NULL,
266             (GClassInitFunc) sp_connector_context_class_init,
267             NULL, NULL,
268             sizeof(SPConnectorContext),
269             4,
270             (GInstanceInitFunc) sp_connector_context_init,
271             NULL,   /* value_table */
272         };
273         type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPConnectorContext", &info, (GTypeFlags)0);
274     }
275     return type;
278 static void
279 sp_connector_context_class_init(SPConnectorContextClass *klass)
281     GObjectClass *object_class;
282     SPEventContextClass *event_context_class;
284     object_class = (GObjectClass *) klass;
285     event_context_class = (SPEventContextClass *) klass;
287     parent_class = (SPEventContextClass*)g_type_class_peek_parent(klass);
289     object_class->dispose = sp_connector_context_dispose;
291     event_context_class->setup = sp_connector_context_setup;
292     event_context_class->set = sp_connector_context_set;
293     event_context_class->finish = sp_connector_context_finish;
294     event_context_class->root_handler = sp_connector_context_root_handler;
295     event_context_class->item_handler = sp_connector_context_item_handler;
299 static void
300 sp_connector_context_init(SPConnectorContext *cc)
302     SPEventContext *ec = SP_EVENT_CONTEXT(cc);
304     ec->cursor_shape = cursor_connector_xpm;
305     ec->hot_x = 1;
306     ec->hot_y = 1;
307     ec->xp = 0;
308     ec->yp = 0;
310     cc->mode = SP_CONNECTOR_CONTEXT_DRAWING_MODE;
311     cc->knot_tip = 0;
313     cc->red_color = 0xff00007f;
315     cc->newconn = NULL;
316     cc->newConnRef = NULL;
317     cc->curvature = 0.0;
319     cc->sel_changed_connection = sigc::connection();
321     cc->active_shape = NULL;
322     cc->active_shape_repr = NULL;
323     cc->active_shape_layer_repr = NULL;
325     cc->active_conn = NULL;
326     cc->active_conn_repr = NULL;
328     cc->active_handle = NULL;
330     cc->selected_handle = NULL;
332     cc->clickeditem = NULL;
333     cc->clickedhandle = NULL;
335     new (&cc->connpthandles) ConnectionPointMap();
337     for (int i = 0; i < 2; ++i) {
338         cc->endpt_handle[i] = NULL;
339         cc->endpt_handler_id[i] = 0;
340     }
341     cc->sid = NULL;
342     cc->eid = NULL;
343     cc->npoints = 0;
344     cc->state = SP_CONNECTOR_CONTEXT_IDLE;
348 static void
349 sp_connector_context_dispose(GObject *object)
351     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(object);
353     cc->sel_changed_connection.disconnect();
355     if (!cc->connpthandles.empty()) {
356         for (ConnectionPointMap::iterator it = cc->connpthandles.begin();
357                 it != cc->connpthandles.end(); ++it) {
358             g_object_unref(it->first);
359         }
360         cc->connpthandles.clear();
361     }
362     cc->connpthandles.~ConnectionPointMap();
363     for (int i = 0; i < 2; ++i) {
364         if (cc->endpt_handle[1]) {
365             g_object_unref(cc->endpt_handle[i]);
366             cc->endpt_handle[i] = NULL;
367         }
368     }
369     if (cc->sid) {
370         g_free(cc->sid);
371         cc->sid = NULL;
372     }
373     if (cc->eid) {
374         g_free(cc->eid);
375         cc->eid = NULL;
376     }
377     g_assert( cc->newConnRef == NULL );
379     G_OBJECT_CLASS(parent_class)->dispose(object);
383 static void
384 sp_connector_context_setup(SPEventContext *ec)
386     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(ec);
387     SPDesktop *dt = ec->desktop;
389     if (((SPEventContextClass *) parent_class)->setup) {
390         ((SPEventContextClass *) parent_class)->setup(ec);
391     }
393     cc->selection = sp_desktop_selection(dt);
395     cc->sel_changed_connection.disconnect();
396     cc->sel_changed_connection = cc->selection->connectChanged(
397             sigc::bind(sigc::ptr_fun(&cc_selection_changed),
398             (gpointer) cc));
400     /* Create red bpath */
401     cc->red_bpath = sp_canvas_bpath_new(sp_desktop_sketch(ec->desktop), NULL);
402     sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cc->red_bpath), cc->red_color,
403             1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
404     sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(cc->red_bpath), 0x00000000,
405             SP_WIND_RULE_NONZERO);
406     /* Create red curve */
407     cc->red_curve = new SPCurve();
409     /* Create green curve */
410     cc->green_curve = new SPCurve();
412     // Notice the initial selection.
413     cc_selection_changed(cc->selection, (gpointer) cc);
415     cc->within_tolerance = false;
417     sp_event_context_read(ec, "curvature");
418     sp_event_context_read(ec, "orthogonal");
419     sp_event_context_read(ec, "mode");
420     cc->knot_tip = cc->mode == SP_CONNECTOR_CONTEXT_DRAWING_MODE ? cc_knot_tips[0] : cc_knot_tips[1];
421     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
422     if (prefs->getBool("/tools/connector/selcue", 0)) {
423         ec->enableSelectionCue();
424     }
426     // Make sure we see all enter events for canvas items,
427     // even if a mouse button is depressed.
428     dt->canvas->gen_all_enter_events = true;
432 static void
433 sp_connector_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val)
435     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(ec);
437     /* fixme: Proper error handling for non-numeric data.  Use a locale-independent function like
438      * g_ascii_strtod (or a thin wrapper that does the right thing for invalid values inf/nan). */
439     Glib::ustring name = val->getEntryName();
440     if ( name == "curvature" ) {
441         cc->curvature = val->getDoubleLimited(); // prevents NaN and +/-Inf from messing up
442     }
443     else if ( name == "orthogonal" ) {
444         cc->isOrthogonal = val->getBool();
445     }
446     else if ( name == "mode")
447     {
448         sp_connector_context_switch_mode(ec, val->getBool() ? SP_CONNECTOR_CONTEXT_EDITING_MODE : SP_CONNECTOR_CONTEXT_DRAWING_MODE);
449     }
452 void sp_connector_context_switch_mode(SPEventContext* ec, unsigned int newMode)
454     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(ec);
456     cc->mode = newMode;
457     if ( cc->mode == SP_CONNECTOR_CONTEXT_DRAWING_MODE )
458     {
459         ec->cursor_shape = cursor_connector_xpm;
460         cc->knot_tip = cc_knot_tips[0];
461         if (cc->selected_handle)
462             cc_deselect_handle( cc->selected_handle );
463         cc->selected_handle = NULL;
464         // Show all default connection points
466     }
467     else if ( cc->mode == SP_CONNECTOR_CONTEXT_EDITING_MODE )
468     {
469         ec->cursor_shape = cursor_node_xpm;
470         cc->knot_tip = cc_knot_tips[1];
471 /*            if (cc->active_shape)
472         {
473             cc->selection->set( SP_OBJECT( cc->active_shape ) );
474         }
475         else
476         {
477             SPItem* item = cc->selection->singleItem();
478             if ( item )
479             {
480                 cc_set_active_shape(cc, item);
481                 cc->selection->set( SP_OBJECT( item ) );
482             }
483         }*/
484     }
485     sp_event_context_update_cursor(ec);
490 static void
491 sp_connector_context_finish(SPEventContext *ec)
493     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(ec);
495     spcc_connector_finish(cc);
496     cc->state = SP_CONNECTOR_CONTEXT_IDLE;
498     if (((SPEventContextClass *) parent_class)->finish) {
499         ((SPEventContextClass *) parent_class)->finish(ec);
500     }
502     if (cc->selection) {
503         cc->selection = NULL;
504     }
505     cc_clear_active_shape(cc);
506     cc_clear_active_conn(cc);
508     // Restore the default event generating behaviour.
509     SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(ec);
510     desktop->canvas->gen_all_enter_events = false;
514 //-----------------------------------------------------------------------------
517 static void
518 cc_clear_active_shape(SPConnectorContext *cc)
520     if (cc->active_shape == NULL) {
521         return;
522     }
523     g_assert( cc->active_shape_repr );
524     g_assert( cc->active_shape_layer_repr );
526     cc->active_shape = NULL;
528     if (cc->active_shape_repr) {
529         sp_repr_remove_listener_by_data(cc->active_shape_repr, cc);
530         Inkscape::GC::release(cc->active_shape_repr);
531         cc->active_shape_repr = NULL;
533         sp_repr_remove_listener_by_data(cc->active_shape_layer_repr, cc);
534         Inkscape::GC::release(cc->active_shape_layer_repr);
535         cc->active_shape_layer_repr = NULL;
536     }
538     // Hide the connection points if they exist.
539     if (cc->connpthandles.size()) {
540         for (ConnectionPointMap::iterator it = cc->connpthandles.begin();
541                 it != cc->connpthandles.end(); ++it) {
542             sp_knot_hide(it->first);
543         }
544     }
548 static void
549 cc_clear_active_conn(SPConnectorContext *cc)
551     if (cc->active_conn == NULL) {
552         return;
553     }
554     g_assert( cc->active_conn_repr );
556     cc->active_conn = NULL;
558     if (cc->active_conn_repr) {
559         sp_repr_remove_listener_by_data(cc->active_conn_repr, cc);
560         Inkscape::GC::release(cc->active_conn_repr);
561         cc->active_conn_repr = NULL;
562     }
564     // Hide the endpoint handles.
565     for (int i = 0; i < 2; ++i) {
566         if (cc->endpt_handle[i]) {
567             sp_knot_hide(cc->endpt_handle[i]);
568         }
569     }
573 static gchar *
574 conn_pt_handle_test(SPConnectorContext *cc, Geom::Point& p)
576     // TODO: this will need to change when there are more connection
577     //       points available for each shape.
579     if (cc->active_handle && (cc->connpthandles.find(cc->active_handle) != cc->connpthandles.end()))
580     {
581         p = cc->active_handle->pos;
582         const ConnectionPoint& cp = cc->connpthandles[cc->active_handle];
583         return g_strdup_printf("#%s_%c_%d", SP_OBJECT_ID(cc->active_shape),
584                                cp.type == ConnPointDefault ? 'd' : 'u' , cp.id);
585     }
586     return NULL;
589 static void
590 cc_select_handle(SPKnot* knot)
592     knot->setShape(SP_KNOT_SHAPE_SQUARE);
593     knot->setSize(10);
594     knot->setAnchor(GTK_ANCHOR_CENTER);
595     knot->setFill(0x0000ffff, 0x0000ffff, 0x0000ffff);
596     sp_knot_update_ctrl(knot);
599 static void
600 cc_deselect_handle(SPKnot* knot)
602     knot->setShape(SP_KNOT_SHAPE_SQUARE);
603     knot->setSize(8);
604     knot->setAnchor(GTK_ANCHOR_CENTER);
605     knot->setFill(0xffffff00, 0xff0000ff, 0xff0000ff);
606     sp_knot_update_ctrl(knot);
609 static gint
610 sp_connector_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
612     gint ret = FALSE;
614     SPDesktop *desktop = event_context->desktop;
616     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(event_context);
618     Geom::Point p(event->button.x, event->button.y);
620     switch (event->type) {
621         case GDK_BUTTON_RELEASE:
622             if (event->button.button == 1 && !event_context->space_panning) {
623                 if ((cc->state == SP_CONNECTOR_CONTEXT_DRAGGING) &&
624                         (event_context->within_tolerance))
625                 {
626                     spcc_reset_colors(cc);
627                     cc->state = SP_CONNECTOR_CONTEXT_IDLE;
628 //                    sp_event_context_snap_window_closed(event_context);
629                 }
630                 if (cc->state != SP_CONNECTOR_CONTEXT_IDLE) {
631                     // Doing something else like rerouting.
632                     break;
633                 }
634                 // find out clicked item, honoring Alt
635                 SPItem *item = sp_event_context_find_item(desktop,
636                         p, event->button.state & GDK_MOD1_MASK, FALSE);
638                 if (event->button.state & GDK_SHIFT_MASK) {
639                     cc->selection->toggle(item);
640                 } else {
641                     cc->selection->set(item);
642                     if ( cc->mode == SP_CONNECTOR_CONTEXT_EDITING_MODE && cc->selected_handle )
643                     {
644                         cc_deselect_handle( cc->selected_handle );
645                         cc->selected_handle = NULL;
646                     }
647                     /* When selecting a new item,
648                        do not allow showing connection points
649                        on connectors. (yet?)
650                     */
651                     if ( item != cc->active_shape && !cc_item_is_connector( item ) )
652                         cc_set_active_shape( cc, item );
653                 }
654                 ret = TRUE;
656             }
657             break;
658         case GDK_ENTER_NOTIFY:
659         {
660             if (cc->mode == SP_CONNECTOR_CONTEXT_DRAWING_MODE || (cc->mode == SP_CONNECTOR_CONTEXT_EDITING_MODE && !cc->selected_handle))
661             {
662                 if (cc_item_is_shape(item)) {
664                     // I don't really understand what the above does,
665                     // so I commented it.
666                     // This is a shape, so show connection point(s).
667     /*                if (!(cc->active_shape)
668                             // Don't show handle for another handle.
669     //                         || (cc->connpthandles.find((SPKnot*) item) != cc->connpthandles.end())
670                         )
671                     {
672                         cc_set_active_shape(cc, item);
673                     }*/
674                     cc_set_active_shape(cc, item);
675                 }
676                 ret = TRUE;
677             }
678             break;
679         }
680         default:
681             break;
682     }
684     return ret;
688 gint
689 sp_connector_context_root_handler(SPEventContext *ec, GdkEvent *event)
691     SPConnectorContext *const cc = SP_CONNECTOR_CONTEXT(ec);
693     gint ret = FALSE;
695     switch (event->type) {
696         case GDK_BUTTON_PRESS:
697             ret = connector_handle_button_press(cc, event->button);
698             break;
700         case GDK_MOTION_NOTIFY:
701             ret = connector_handle_motion_notify(cc, event->motion);
702             break;
704         case GDK_BUTTON_RELEASE:
705             ret = connector_handle_button_release(cc, event->button);
706             break;
707         case GDK_KEY_PRESS:
708             ret = connector_handle_key_press(cc, get_group0_keyval (&event->key));
709             break;
711         default:
712             break;
713     }
715     if (!ret) {
716         gint (*const parent_root_handler)(SPEventContext *, GdkEvent *)
717             = ((SPEventContextClass *) parent_class)->root_handler;
718         if (parent_root_handler) {
719             ret = parent_root_handler(ec, event);
720         }
721     }
723     return ret;
727 static gint
728 connector_handle_button_press(SPConnectorContext *const cc, GdkEventButton const &bevent)
730     Geom::Point const event_w(bevent.x, bevent.y);
731     /* Find desktop coordinates */
732     Geom::Point p = cc->desktop->w2d(event_w);
733     SPEventContext *event_context = SP_EVENT_CONTEXT(cc);
735     gint ret = FALSE;
736     if ( cc->mode == SP_CONNECTOR_CONTEXT_DRAWING_MODE )
737     {
738         if ( bevent.button == 1 && !event_context->space_panning ) {
740             SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
742             if (Inkscape::have_viable_layer(desktop, cc->_message_context) == false) {
743                 return TRUE;
744             }
746             Geom::Point const event_w(bevent.x,
747                                     bevent.y);
748 //             connector_drag_origin_w = event_w;
749             cc->xp = bevent.x;
750             cc->yp = bevent.y;
751             cc->within_tolerance = true;
753             Geom::Point const event_dt = cc->desktop->w2d(event_w);
755             SnapManager &m = cc->desktop->namedview->snap_manager;
756             m.setup(cc->desktop);
758             switch (cc->state) {
759                 case SP_CONNECTOR_CONTEXT_STOP:
760                     /* This is allowed, if we just cancelled curve */
761                 case SP_CONNECTOR_CONTEXT_IDLE:
762                 {
763                     if ( cc->npoints == 0 ) {
764                         cc_clear_active_conn(cc);
766                         SP_EVENT_CONTEXT_DESKTOP(cc)->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating new connector"));
768                         /* Set start anchor */
769                         /* Create green anchor */
770                         Geom::Point p = event_dt;
772                         // Test whether we clicked on a connection point
773                         cc->sid = conn_pt_handle_test(cc, p);
775 //                        sp_event_context_snap_window_open(event_context);
777                         if (!cc->sid) {
778                             // This is the first point, so just snap it to the grid
779                             // as there's no other points to go off.
780                             m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, p, Inkscape::SNAPSOURCE_HANDLE);
781                         }
782                         spcc_connector_set_initial_point(cc, p);
784                     }
785                     cc->state = SP_CONNECTOR_CONTEXT_DRAGGING;
786                     ret = TRUE;
787                     break;
788                 }
789                 case SP_CONNECTOR_CONTEXT_DRAGGING:
790                 {
791                     // This is the second click of a connector creation.
792                     m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, p, Inkscape::SNAPSOURCE_HANDLE);
794                     spcc_connector_set_subsequent_point(cc, p);
795                     spcc_connector_finish_segment(cc, p);
796                     // Test whether we clicked on a connection point
797                     cc->eid = conn_pt_handle_test(cc, p);
798                     if (cc->npoints != 0) {
799                         spcc_connector_finish(cc);
800                     }
801                     cc_set_active_conn(cc, cc->newconn);
802                     cc->state = SP_CONNECTOR_CONTEXT_IDLE;
803 //                    sp_event_context_snap_window_closed(event_context);
804                     ret = TRUE;
805                     break;
806                 }
807                 case SP_CONNECTOR_CONTEXT_CLOSE:
808                 {
809                     g_warning("Button down in CLOSE state");
810                     break;
811                 }
812                 default:
813                     break;
814             }
815         } else if (bevent.button == 3) {
816             if (cc->state == SP_CONNECTOR_CONTEXT_REROUTING) {
817                 // A context menu is going to be triggered here,
818                 // so end the rerouting operation.
819                 cc_connector_rerouting_finish(cc, &p);
821                 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
822 //                sp_event_context_snap_window_closed(event_context);
824                 // Don't set ret to TRUE, so we drop through to the
825                 // parent handler which will open the context menu.
826             }
827             else if (cc->npoints != 0) {
828                 spcc_connector_finish(cc);
829                 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
830 //                sp_event_context_snap_window_closed(event_context);
831                 ret = TRUE;
832             }
833         }
834     }
835     else if ( cc->mode == SP_CONNECTOR_CONTEXT_EDITING_MODE )
836     {
837         if ( bevent.button == 1 && !event_context->space_panning )
838         {
839             // Initialize variables in case of dragging
841             SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
843             if (Inkscape::have_viable_layer(desktop, cc->_message_context) == false) {
844                 return TRUE;
845             }
847             cc->xp = bevent.x;
848             cc->yp = bevent.y;
849             cc->within_tolerance = true;
851             ConnectionPointMap::iterator const& active_knot_it = cc->connpthandles.find( cc->active_handle );
853             switch (cc->state)
854             {
855                 case SP_CONNECTOR_CONTEXT_IDLE:
856                     if ( active_knot_it != cc->connpthandles.end() )
857                     {
858                         // We do not allow selecting and, thereby, moving default knots
859                         if ( active_knot_it->second.type != ConnPointDefault)
860                         {
861                             if (cc->selected_handle != cc->active_handle)
862                             {
863                                 if ( cc->selected_handle )
864                                     cc_deselect_handle( cc->selected_handle );
865                                 cc->selected_handle = cc->active_handle;
866                                 cc_select_handle( cc->selected_handle );
867                             }
868                         }
869                         else
870                             // Just ignore the default connection point
871                             return FALSE;
872                     }
873                     else
874                         if ( cc->selected_handle )
875                         {
876                             cc_deselect_handle( cc->selected_handle );
877                             cc->selected_handle = NULL;
878                         }
880                     if ( cc->selected_handle )
881                     {
882 //                         sp_event_context_snap_window_open(event_context);
883                         cc->state = SP_CONNECTOR_CONTEXT_DRAGGING;
884                         cc->selection->set( SP_OBJECT( cc->active_shape ) );
885                     }
887                     ret = TRUE;
888                     break;
889                 // Dragging valid because of the way we create
890                 // new connection points.
891                 case SP_CONNECTOR_CONTEXT_DRAGGING:
892                     // Do nothing.
893                     ret = TRUE;
894                     break;
895             }
896         }
897     }
898     return ret;
902 static gint
903 connector_handle_motion_notify(SPConnectorContext *const cc, GdkEventMotion const &mevent)
905     gint ret = FALSE;
906     SPEventContext *event_context = SP_EVENT_CONTEXT(cc);
907     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
909     if (event_context->space_panning || mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) {
910         // allow middle-button scrolling
911         return FALSE;
912     }
914     Geom::Point const event_w(mevent.x, mevent.y);
916     if (cc->within_tolerance) {
917         cc->tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
918         if ( ( abs( (gint) mevent.x - cc->xp ) < cc->tolerance ) &&
919              ( abs( (gint) mevent.y - cc->yp ) < cc->tolerance ) ) {
920             return FALSE;   // Do not drag if we're within tolerance from origin.
921         }
922     }
923     // Once the user has moved farther than tolerance from the original location
924     // (indicating they intend to move the object, not click), then always process
925     // the motion notify coordinates as given (no snapping back to origin)
926     cc->within_tolerance = false;
928     SPDesktop *const dt = cc->desktop;
930     /* Find desktop coordinates */
931     Geom::Point p = dt->w2d(event_w);
933     if ( cc->mode == SP_CONNECTOR_CONTEXT_DRAWING_MODE )
934     {
935         SnapManager &m = dt->namedview->snap_manager;
936         m.setup(dt);
938         switch (cc->state) {
939             case SP_CONNECTOR_CONTEXT_DRAGGING:
940             {
941                 gobble_motion_events(mevent.state);
942                 // This is movement during a connector creation.
943                 if ( cc->npoints > 0 ) {
944                     m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, p,
945                             Inkscape::SNAPSOURCE_HANDLE);
946                     cc->selection->clear();
947                     spcc_connector_set_subsequent_point(cc, p);
948                     ret = TRUE;
949                 }
950                 break;
951             }
952             case SP_CONNECTOR_CONTEXT_REROUTING:
953             {
954                 gobble_motion_events(GDK_BUTTON1_MASK);
955                 g_assert( SP_IS_PATH(cc->clickeditem));
957                 m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, p,
958                         Inkscape::SNAPSOURCE_HANDLE);
960                 // Update the hidden path
961                 Geom::Matrix i2d = sp_item_i2d_affine(cc->clickeditem);
962                 Geom::Matrix d2i = i2d.inverse();
963                 SPPath *path = SP_PATH(cc->clickeditem);
964                 SPCurve *curve = path->original_curve ? path->original_curve : path->curve;
965                 if (cc->clickedhandle == cc->endpt_handle[0]) {
966                     Geom::Point o = cc->endpt_handle[1]->pos;
967                     curve->stretch_endpoints(p * d2i, o * d2i);
968                 }
969                 else {
970                     Geom::Point o = cc->endpt_handle[0]->pos;
971                     curve->stretch_endpoints(o * d2i, p * d2i);
972                 }
973                 sp_conn_reroute_path_immediate(path);
975                 // Copy this to the temporary visible path
976                 cc->red_curve = path->original_curve ?
977                         path->original_curve->copy() : path->curve->copy();
978                 cc->red_curve->transform(i2d);
980                 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), cc->red_curve);
981                 ret = TRUE;
982                 break;
983             }
984             case SP_CONNECTOR_CONTEXT_STOP:
985                 /* This is perfectly valid */
986                 break;
987             default:
988                 break;
989         }
990     }
991     else if ( cc->mode == SP_CONNECTOR_CONTEXT_EDITING_MODE )
992     {
993         switch ( cc->state )
994         {
995             case SP_CONNECTOR_CONTEXT_DRAGGING:
996                 sp_knot_set_position(cc->selected_handle, p, 0);
997                 ret = TRUE;
998                 break;
999             case SP_CONNECTOR_CONTEXT_NEWCONNPOINT:
1000                 sp_knot_set_position(cc->selected_handle, p, 0);
1001                 ret = TRUE;
1002                 break;
1003         }
1004     }
1006     return ret;
1010 static gint
1011 connector_handle_button_release(SPConnectorContext *const cc, GdkEventButton const &revent)
1013     gint ret = FALSE;
1014     SPEventContext *event_context = SP_EVENT_CONTEXT(cc);
1015     if ( revent.button == 1 && !event_context->space_panning ) {
1017         SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
1018         SPDocument *doc = sp_desktop_document(desktop);
1020         SnapManager &m = desktop->namedview->snap_manager;
1021         m.setup(desktop);
1023         Geom::Point const event_w(revent.x, revent.y);
1025         /* Find desktop coordinates */
1026         Geom::Point p = cc->desktop->w2d(event_w);
1027         if ( cc->mode == SP_CONNECTOR_CONTEXT_DRAWING_MODE )
1028         {
1029             switch (cc->state) {
1030                 //case SP_CONNECTOR_CONTEXT_POINT:
1031                 case SP_CONNECTOR_CONTEXT_DRAGGING:
1032                 {
1033                     m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, p, Inkscape::SNAPSOURCE_HANDLE);
1035                     if (cc->within_tolerance)
1036                     {
1037                         spcc_connector_finish_segment(cc, p);
1038                         return TRUE;
1039                     }
1040                     // Connector has been created via a drag, end it now.
1041                     spcc_connector_set_subsequent_point(cc, p);
1042                     spcc_connector_finish_segment(cc, p);
1043                     // Test whether we clicked on a connection point
1044                     cc->eid = conn_pt_handle_test(cc, p);
1045                     if (cc->npoints != 0) {
1046                         spcc_connector_finish(cc);
1047                     }
1048                     cc_set_active_conn(cc, cc->newconn);
1049                     cc->state = SP_CONNECTOR_CONTEXT_IDLE;
1050 //                    sp_event_context_snap_window_closed(event_context);
1051                     break;
1052                 }
1053                 case SP_CONNECTOR_CONTEXT_REROUTING:
1054                 {
1055                     m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, p, Inkscape::SNAPSOURCE_HANDLE);
1056                     cc_connector_rerouting_finish(cc, &p);
1058                     sp_document_ensure_up_to_date(doc);
1059                     cc->state = SP_CONNECTOR_CONTEXT_IDLE;
1060 //                    sp_event_context_snap_window_closed(event_context);
1061                     return TRUE;
1062                     break;
1063                 }
1064                 case SP_CONNECTOR_CONTEXT_STOP:
1065                     /* This is allowed, if we just cancelled curve */
1066                     break;
1067                 default:
1068                     break;
1069             }
1070             ret = TRUE;
1071         }
1072         else if ( cc->mode == SP_CONNECTOR_CONTEXT_EDITING_MODE )
1073         {
1074             switch ( cc->state )
1075             {
1076                 case SP_CONNECTOR_CONTEXT_DRAGGING:
1078                     if (!cc->within_tolerance)
1079                     {
1080 //                        sp_event_context_snap_window_open(event_context);
1081                         m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, p, Inkscape::SNAPSOURCE_HANDLE);
1082                         sp_knot_set_position(cc->selected_handle, p, 0);
1083 //                        sp_event_context_snap_window_closed(event_context);
1084                         ConnectionPoint& cp = cc->connpthandles[cc->selected_handle];
1085                         cp.pos = p * sp_item_dt2i_affine(cc->active_shape);
1086                         cc->active_shape->avoidRef->updateConnectionPoint(cp);
1087                     }
1089                     cc->state = SP_CONNECTOR_CONTEXT_IDLE;
1090                     ret = TRUE;
1091                     break;
1094                 case SP_CONNECTOR_CONTEXT_NEWCONNPOINT:
1095 //                    sp_event_context_snap_window_open( event_context );
1096                     m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, p, Inkscape::SNAPSOURCE_HANDLE);
1098                     sp_knot_set_position(cc->selected_handle, p, 0);
1099 //                    sp_event_context_snap_window_closed(event_context);
1101                     ConnectionPoint cp;
1102                     cp.type = ConnPointUserDefined;
1103                     cp.pos = p * sp_item_dt2i_affine(cc->active_shape);
1104                     cp.dir = Avoid::ConnDirAll;
1105                     g_object_unref(cc->selected_handle);
1106                     cc->active_shape->avoidRef->addConnectionPoint(cp);
1107                     sp_document_ensure_up_to_date(doc);
1108                     for (ConnectionPointMap::iterator it = cc->connpthandles.begin(); it != cc->connpthandles.end(); ++it)
1109                         if (it->second.type == ConnPointUserDefined && it->second.id == cp.id)
1110                         {
1111                             cc->selected_handle = it->first;
1112                             break;
1113                         }
1114                     cc_select_handle( cc->selected_handle );
1115                     cc->state = SP_CONNECTOR_CONTEXT_IDLE;
1116                     ret = TRUE;
1117                     break;
1118             }
1119         }
1120     }
1123     return ret;
1127 static gint
1128 connector_handle_key_press(SPConnectorContext *const cc, guint const keyval)
1130     gint ret = FALSE;
1131     /* fixme: */
1132     if ( cc->mode == SP_CONNECTOR_CONTEXT_DRAWING_MODE )
1133     {
1134         switch (keyval) {
1135             case GDK_Return:
1136             case GDK_KP_Enter:
1137                 if (cc->npoints != 0) {
1138                     spcc_connector_finish(cc);
1139                     cc->state = SP_CONNECTOR_CONTEXT_IDLE;
1140 //                    sp_event_context_snap_window_closed(SP_EVENT_CONTEXT(cc));
1141                     ret = TRUE;
1142                 }
1143                 break;
1144             case GDK_Escape:
1145                 if (cc->state == SP_CONNECTOR_CONTEXT_REROUTING) {
1147                     SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
1148                     SPDocument *doc = sp_desktop_document(desktop);
1150                     cc_connector_rerouting_finish(cc, NULL);
1152                     sp_document_undo(doc);
1154                     cc->state = SP_CONNECTOR_CONTEXT_IDLE;
1155 //                    sp_event_context_snap_window_closed(SP_EVENT_CONTEXT(cc));
1156                     desktop->messageStack()->flash( Inkscape::NORMAL_MESSAGE,
1157                             _("Connector endpoint drag cancelled."));
1158                     ret = TRUE;
1159                 }
1160                 else if (cc->npoints != 0) {
1161                     // if drawing, cancel, otherwise pass it up for deselecting
1162                     cc->state = SP_CONNECTOR_CONTEXT_STOP;
1163 //                    sp_event_context_snap_window_closed(SP_EVENT_CONTEXT(cc));
1164                     spcc_reset_colors(cc);
1165                     ret = TRUE;
1166                 }
1167                 break;
1168             default:
1169                 break;
1170         }
1171     }
1172     else if ( cc->mode == SP_CONNECTOR_CONTEXT_EDITING_MODE )
1173     {
1174         switch ( cc->state )
1175         {
1176             case SP_CONNECTOR_CONTEXT_DRAGGING:
1177                 if ( keyval == GDK_Escape )
1178                 {
1179                     // Cancel connection point dragging
1181                     // Obtain original position
1182                     ConnectionPoint const& cp = cc->connpthandles[cc->selected_handle];
1183                     SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
1184                     const Geom::Matrix& i2doc = sp_item_i2doc_affine(cc->active_shape);
1185                     sp_knot_set_position(cc->selected_handle, cp.pos * i2doc * desktop->doc2dt(), 0);
1186                     cc->state = SP_CONNECTOR_CONTEXT_IDLE;
1187                     desktop->messageStack()->flash( Inkscape::NORMAL_MESSAGE,
1188                         _("Connection point drag cancelled."));
1189                     ret = TRUE;
1190                 }
1191                 else if ( keyval == GDK_Return || keyval == GDK_KP_Enter )
1192                 {
1193                     // Put connection point at current position
1195                     SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
1196                     SnapManager &m = desktop->namedview->snap_manager;
1197                     m.setup(desktop);
1198                     Geom::Point p = cc->selected_handle->pos;
1199 //                     SPEventContext* event_context = SP_EVENT_CONTEXT( cc );
1201                     if (!cc->within_tolerance)
1202                     {
1203 //                        sp_event_context_snap_window_open(event_context);
1204                         m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, p, Inkscape::SNAPSOURCE_HANDLE);
1205                         sp_knot_set_position(cc->selected_handle, p, 0);
1206 //                        sp_event_context_snap_window_closed(event_context);
1207                         ConnectionPoint& cp = cc->connpthandles[cc->selected_handle];
1208                         cp.pos = p * sp_item_dt2i_affine(cc->active_shape);
1209                         cc->active_shape->avoidRef->updateConnectionPoint(cp);
1210                     }
1212                     cc->state = SP_CONNECTOR_CONTEXT_IDLE;
1213                     ret = TRUE;
1214                 }
1215                 break;
1216             case SP_CONNECTOR_CONTEXT_NEWCONNPOINT:
1217                 if ( keyval == GDK_Escape )
1218                 {
1219                     // Just destroy the knot
1220                     g_object_unref( cc->selected_handle );
1221                     cc->selected_handle = NULL;
1222                     cc->state = SP_CONNECTOR_CONTEXT_IDLE;
1223                     ret = TRUE;
1224                 }
1225                 else if ( keyval == GDK_Return || keyval == GDK_KP_Enter )
1226                 {
1227                     SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
1228                     SPDocument *doc = sp_desktop_document(desktop);
1229                     SnapManager &m = desktop->namedview->snap_manager;
1230                     m.setup(desktop);
1231                     Geom::Point p = cc->selected_handle->pos;
1232 //                     SPEventContext* event_context = SP_EVENT_CONTEXT( cc );
1234 //                    sp_event_context_snap_window_open( event_context );
1235                     m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, p, Inkscape::SNAPSOURCE_HANDLE);
1237                     sp_knot_set_position(cc->selected_handle, p, 0);
1238 //                    sp_event_context_snap_window_closed(event_context);
1240                     ConnectionPoint cp;
1241                     cp.type = ConnPointUserDefined;
1242                     cp.pos = p * sp_item_dt2i_affine(cc->active_shape);
1243                     cp.dir = Avoid::ConnDirAll;
1244                     g_object_unref(cc->selected_handle);
1245                     cc->active_shape->avoidRef->addConnectionPoint(cp);
1246                     sp_document_ensure_up_to_date(doc);
1247                     for (ConnectionPointMap::iterator it = cc->connpthandles.begin(); it != cc->connpthandles.end(); ++it)
1248                         if (it->second.type == ConnPointUserDefined && it->second.id == cp.id)
1249                         {
1250                             cc->selected_handle = it->first;
1251                             break;
1252                         }
1253                     cc_select_handle( cc->selected_handle );
1254                     cc->state = SP_CONNECTOR_CONTEXT_IDLE;
1255                     ret = TRUE;
1256                 }
1258                 break;
1259             case SP_CONNECTOR_CONTEXT_IDLE:
1260                 if ( keyval == GDK_Delete && cc->selected_handle )
1261                 {
1262                     cc->active_shape->avoidRef->deleteConnectionPoint(cc->connpthandles[cc->selected_handle]);
1263                     cc->selected_handle = NULL;
1264                     ret = TRUE;
1265                 }
1267                 break;
1268         }
1269     }
1271     return ret;
1275 static void
1276 cc_connector_rerouting_finish(SPConnectorContext *const cc, Geom::Point *const p)
1278     SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
1279     SPDocument *doc = sp_desktop_document(desktop);
1281     // Clear the temporary path:
1282     cc->red_curve->reset();
1283     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
1285     if (p != NULL)
1286     {
1287         // Test whether we clicked on a connection point
1288         gchar *shape_label = conn_pt_handle_test(cc, *p);
1290         if (shape_label) {
1291             if (cc->clickedhandle == cc->endpt_handle[0]) {
1292                 sp_object_setAttribute(cc->clickeditem,
1293                         "inkscape:connection-start",shape_label, false);
1294             }
1295             else {
1296                 sp_object_setAttribute(cc->clickeditem,
1297                         "inkscape:connection-end",shape_label, false);
1298             }
1299             g_free(shape_label);
1300         }
1301     }
1302     cc->clickeditem->setHidden(false);
1303     sp_conn_reroute_path_immediate(SP_PATH(cc->clickeditem));
1304     cc->clickeditem->updateRepr();
1305     sp_document_done(doc, SP_VERB_CONTEXT_CONNECTOR,
1306                      _("Reroute connector"));
1307     cc_set_active_conn(cc, cc->clickeditem);
1311 static void
1312 spcc_reset_colors(SPConnectorContext *cc)
1314     /* Red */
1315     cc->red_curve->reset();
1316     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
1318     cc->green_curve->reset();
1319     cc->npoints = 0;
1323 static void
1324 spcc_connector_set_initial_point(SPConnectorContext *const cc, Geom::Point const p)
1326     g_assert( cc->npoints == 0 );
1328     cc->p[0] = p;
1329     cc->p[1] = p;
1330     cc->npoints = 2;
1331     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
1335 static void
1336 spcc_connector_set_subsequent_point(SPConnectorContext *const cc, Geom::Point const p)
1338     g_assert( cc->npoints != 0 );
1340     SPDesktop *dt = cc->desktop;
1341     Geom::Point o = dt->dt2doc(cc->p[0]);
1342     Geom::Point d = dt->dt2doc(p);
1343     Avoid::Point src(o[Geom::X], o[Geom::Y]);
1344     Avoid::Point dst(d[Geom::X], d[Geom::Y]);
1346     if (!cc->newConnRef) {
1347         Avoid::Router *router = sp_desktop_document(dt)->router;
1348         cc->newConnRef = new Avoid::ConnRef(router);
1349         cc->newConnRef->setEndpoint(Avoid::VertID::src, src);
1350         if (cc->isOrthogonal)
1351             cc->newConnRef->setRoutingType(Avoid::ConnType_Orthogonal);
1352         else
1353             cc->newConnRef->setRoutingType(Avoid::ConnType_PolyLine);
1354     }
1355     // Set new endpoint.
1356     cc->newConnRef->setEndpoint(Avoid::VertID::tar, dst);
1357     // Immediately generate new routes for connector.
1358     cc->newConnRef->makePathInvalid();
1359     cc->newConnRef->router()->processTransaction();
1360     // Recreate curve from libavoid route.
1361     recreateCurve( cc->red_curve, cc->newConnRef, cc->curvature );
1362     cc->red_curve->transform(dt->doc2dt());
1363     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), cc->red_curve);
1367 /**
1368  * Concats red, blue and green.
1369  * If any anchors are defined, process these, optionally removing curves from white list
1370  * Invoke _flush_white to write result back to object.
1371  */
1372 static void
1373 spcc_concat_colors_and_flush(SPConnectorContext *cc)
1375     SPCurve *c = cc->green_curve;
1376     cc->green_curve = new SPCurve();
1378     cc->red_curve->reset();
1379     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
1381     if (c->is_empty()) {
1382         c->unref();
1383         return;
1384     }
1386     spcc_flush_white(cc, c);
1388     c->unref();
1392 /*
1393  * Flushes white curve(s) and additional curve into object
1394  *
1395  * No cleaning of colored curves - this has to be done by caller
1396  * No rereading of white data, so if you cannot rely on ::modified, do it in caller
1397  *
1398  */
1400 static void
1401 spcc_flush_white(SPConnectorContext *cc, SPCurve *gc)
1403     SPCurve *c;
1405     if (gc) {
1406         c = gc;
1407         c->ref();
1408     } else {
1409         return;
1410     }
1412     /* Now we have to go back to item coordinates at last */
1413     c->transform(SP_EVENT_CONTEXT_DESKTOP(cc)->dt2doc());
1415     SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
1416     SPDocument *doc = sp_desktop_document(desktop);
1417     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
1419     if ( c && !c->is_empty() ) {
1420         /* We actually have something to write */
1422         Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
1423         /* Set style */
1424         sp_desktop_apply_style_tool(desktop, repr, "/tools/connector", false);
1426         gchar *str = sp_svg_write_path( c->get_pathvector() );
1427         g_assert( str != NULL );
1428         repr->setAttribute("d", str);
1429         g_free(str);
1431         /* Attach repr */
1432         cc->newconn = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
1433         cc->newconn->transform = sp_item_i2doc_affine(SP_ITEM(desktop->currentLayer())).inverse();
1435         bool connection = false;
1436         sp_object_setAttribute(cc->newconn, "inkscape:connector-type",
1437                 cc->isOrthogonal ? "orthogonal" : "polyline", false);
1438         sp_object_setAttribute(cc->newconn, "inkscape:connector-curvature",
1439                 Glib::Ascii::dtostr(cc->curvature).c_str(), false);
1440         if (cc->sid)
1441         {
1442             sp_object_setAttribute(cc->newconn, "inkscape:connection-start",
1443                     cc->sid, false);
1444             connection = true;
1445         }
1447         if (cc->eid)
1448         {
1449             sp_object_setAttribute(cc->newconn, "inkscape:connection-end",
1450                     cc->eid, false);
1451             connection = true;
1452         }
1453         // Process pending updates.
1454         cc->newconn->updateRepr();
1455         sp_document_ensure_up_to_date(doc);
1457         if (connection) {
1458             // Adjust endpoints to shape edge.
1459             sp_conn_reroute_path_immediate(SP_PATH(cc->newconn));
1460             cc->newconn->updateRepr();
1461         }
1463         // Only set the selection after we are finished with creating the attributes of
1464         // the connector.  Otherwise, the selection change may alter the defaults for
1465         // values like curvature in the connector context, preventing subsequent lookup
1466         // of their original values.
1467         cc->selection->set(repr);
1468         Inkscape::GC::release(repr);
1469     }
1471     c->unref();
1473     sp_document_done(doc, SP_VERB_CONTEXT_CONNECTOR, _("Create connector"));
1477 static void
1478 spcc_connector_finish_segment(SPConnectorContext *const cc, Geom::Point const /*p*/)
1480     if (!cc->red_curve->is_empty()) {
1481         cc->green_curve->append_continuous(cc->red_curve, 0.0625);
1483         cc->p[0] = cc->p[3];
1484         cc->p[1] = cc->p[4];
1485         cc->npoints = 2;
1487         cc->red_curve->reset();
1488     }
1492 static void
1493 spcc_connector_finish(SPConnectorContext *const cc)
1495     SPDesktop *const desktop = cc->desktop;
1496     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Finishing connector"));
1498     cc->red_curve->reset();
1499     spcc_concat_colors_and_flush(cc);
1501     cc->npoints = 0;
1503     if (cc->newConnRef) {
1504         cc->newConnRef->removeFromGraph();
1505         delete cc->newConnRef;
1506         cc->newConnRef = NULL;
1507     }
1511 static gboolean
1512 cc_generic_knot_handler(SPCanvasItem *, GdkEvent *event, SPKnot *knot)
1514     g_assert (knot != NULL);
1516     g_object_ref(knot);
1518     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(
1519             knot->desktop->event_context);
1521     gboolean consumed = FALSE;
1523     gchar* knot_tip = knot->tip ? knot->tip : cc->knot_tip;
1524     switch (event->type) {
1525         case GDK_ENTER_NOTIFY:
1526             sp_knot_set_flag(knot, SP_KNOT_MOUSEOVER, TRUE);
1528             cc->active_handle = knot;
1529             if (knot_tip)
1530             {
1531                 knot->desktop->event_context->defaultMessageContext()->set(
1532                         Inkscape::NORMAL_MESSAGE, knot_tip);
1533             }
1535             consumed = TRUE;
1536             break;
1537         case GDK_LEAVE_NOTIFY:
1538             sp_knot_set_flag(knot, SP_KNOT_MOUSEOVER, FALSE);
1540             cc->active_handle = NULL;
1542             if (knot_tip) {
1543                 knot->desktop->event_context->defaultMessageContext()->clear();
1544             }
1546             consumed = TRUE;
1547             break;
1548         default:
1549             break;
1550     }
1552     g_object_unref(knot);
1554     return consumed;
1558 static gboolean
1559 endpt_handler(SPKnot */*knot*/, GdkEvent *event, SPConnectorContext *cc)
1561     g_assert( SP_IS_CONNECTOR_CONTEXT(cc) );
1563     gboolean consumed = FALSE;
1565     switch (event->type) {
1566         case GDK_BUTTON_PRESS:
1567             g_assert( (cc->active_handle == cc->endpt_handle[0]) ||
1568                       (cc->active_handle == cc->endpt_handle[1]) );
1569             if (cc->state == SP_CONNECTOR_CONTEXT_IDLE) {
1570                 cc->clickeditem = cc->active_conn;
1571                 cc->clickedhandle = cc->active_handle;
1572                 cc_clear_active_conn(cc);
1573                 cc->state = SP_CONNECTOR_CONTEXT_REROUTING;
1574 //                sp_event_context_snap_window_open(SP_EVENT_CONTEXT(cc));
1576                 // Disconnect from attached shape
1577                 unsigned ind = (cc->active_handle == cc->endpt_handle[0]) ? 0 : 1;
1578                 sp_conn_end_detach(cc->clickeditem, ind);
1580                 Geom::Point origin;
1581                 if (cc->clickedhandle == cc->endpt_handle[0]) {
1582                     origin = cc->endpt_handle[1]->pos;
1583                 }
1584                 else {
1585                     origin = cc->endpt_handle[0]->pos;
1586                 }
1588                 // Show the red path for dragging.
1589                 cc->red_curve = SP_PATH(cc->clickeditem)->original_curve ? SP_PATH(cc->clickeditem)->original_curve->copy() : SP_PATH(cc->clickeditem)->curve->copy();
1590                 Geom::Matrix i2d = sp_item_i2d_affine(cc->clickeditem);
1591                 cc->red_curve->transform(i2d);
1592                 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), cc->red_curve);
1594                 cc->clickeditem->setHidden(true);
1596                 // The rest of the interaction rerouting the connector is
1597                 // handled by the context root handler.
1598                 consumed = TRUE;
1599             }
1600             break;
1601         default:
1602             break;
1603     }
1605     return consumed;
1608 static void cc_active_shape_add_knot(SPDesktop* desktop, SPItem* item, ConnectionPointMap &cphandles, ConnectionPoint& cp)
1610         SPKnot *knot = sp_knot_new(desktop, 0);
1612         knot->setShape(SP_KNOT_SHAPE_SQUARE);
1613         knot->setSize(8);
1614         knot->setAnchor(GTK_ANCHOR_CENTER);
1615         knot->setFill(0xffffff00, 0xff0000ff, 0xff0000ff);
1616         sp_knot_update_ctrl(knot);
1618         // We don't want to use the standard knot handler.
1619         g_signal_handler_disconnect(G_OBJECT(knot->item),
1620                 knot->_event_handler_id);
1621         knot->_event_handler_id = 0;
1623         gtk_signal_connect(GTK_OBJECT(knot->item), "event",
1624                 GTK_SIGNAL_FUNC(cc_generic_knot_handler), knot);
1625         sp_knot_set_position(knot, item->avoidRef->getConnectionPointPos(cp.type, cp.id) * desktop->doc2dt(), 0);
1626         sp_knot_show(knot);
1627         cphandles[knot] = cp;
1630 static void cc_set_active_shape(SPConnectorContext *cc, SPItem *item)
1632     g_assert(item != NULL );
1634     std::map<int, ConnectionPoint>* connpts = &item->avoidRef->connection_points;
1636     if (cc->active_shape != item)
1637     {
1638         // The active shape has changed
1639         // Rebuild everything
1640         cc->active_shape = item;
1641         // Remove existing active shape listeners
1642         if (cc->active_shape_repr) {
1643             sp_repr_remove_listener_by_data(cc->active_shape_repr, cc);
1644             Inkscape::GC::release(cc->active_shape_repr);
1646             sp_repr_remove_listener_by_data(cc->active_shape_layer_repr, cc);
1647             Inkscape::GC::release(cc->active_shape_layer_repr);
1648         }
1650         // Listen in case the active shape changes
1651         cc->active_shape_repr = SP_OBJECT_REPR(item);
1652         if (cc->active_shape_repr) {
1653             Inkscape::GC::anchor(cc->active_shape_repr);
1654             sp_repr_add_listener(cc->active_shape_repr, &shape_repr_events, cc);
1656             cc->active_shape_layer_repr = cc->active_shape_repr->parent();
1657             Inkscape::GC::anchor(cc->active_shape_layer_repr);
1658             sp_repr_add_listener(cc->active_shape_layer_repr, &layer_repr_events, cc);
1659         }
1662         // Set the connection points.
1663         if ( cc->connpthandles.size() )
1664             // destroy the old list
1665             while (! cc->connpthandles.empty() )
1666             {
1667                 g_object_unref(cc->connpthandles.begin()->first);
1668                 cc->connpthandles.erase(cc->connpthandles.begin());
1669             }
1670         // build the new one
1671         if ( connpts->size() )
1672         for (std::map<int, ConnectionPoint>::iterator it = connpts->begin(); it != connpts->end(); ++it)
1673             cc_active_shape_add_knot(cc->desktop, item, cc->connpthandles, it->second);
1675         // Also add default connection points
1676         // For now, only centre default connection point will
1677         // be available
1678         ConnectionPoint centre;
1679         centre.type = ConnPointDefault;
1680         centre.id = ConnPointPosCC;
1681         cc_active_shape_add_knot(cc->desktop, item, cc->connpthandles, centre);
1682     }
1683     else
1684     {
1685         // The active shape didn't change
1686         // Update only the connection point knots
1688         // Ensure the item's connection_points map
1689         // has been updated
1690         sp_document_ensure_up_to_date(SP_OBJECT_DOCUMENT(item));
1692         std::set<int> seen;
1693         for  ( ConnectionPointMap::iterator it = cc->connpthandles.begin(); it != cc->connpthandles.end() ;)
1694         {
1695             bool removed = false;
1696             if ( it->second.type == ConnPointUserDefined )
1697             {
1698                 std::map<int, ConnectionPoint>::iterator p = connpts->find(it->second.id);
1699                 if (p != connpts->end())
1700                 {
1701                     if ( it->second != p->second )
1702                         // Connection point position has changed
1703                         // Update knot position
1704                         sp_knot_set_position(it->first,
1705                                              item->avoidRef->getConnectionPointPos(it->second.type, it->second.id) * cc->desktop->doc2dt(), 0);
1706                     seen.insert(it->second.id);
1707                     sp_knot_show(it->first);
1708                 }
1709                 else
1710                 {
1711                     // This connection point does no longer exist,
1712                     // remove the knot
1713                     ConnectionPointMap::iterator curr = it;
1714                     ++it;
1715                     g_object_unref( curr->first );
1716                     cc->connpthandles.erase(curr);
1717                     removed = true;
1718                 }
1719             }
1720             else
1721             {
1722                 // It's a default connection point
1723                 // Just make sure it's position is correct
1724                 sp_knot_set_position(it->first,
1725                                      item->avoidRef->getConnectionPointPos(it->second.type, it->second.id) * cc->desktop->doc2dt(), 0);
1726                 sp_knot_show(it->first);
1728             }
1729             if ( !removed )
1730                 ++it;
1731         }
1732         // Add knots for new connection points.
1733         if (connpts->size())
1734             for ( std::map<int, ConnectionPoint>::iterator it = connpts->begin(); it != connpts->end(); ++it )
1735                 if ( seen.find(it->first) == seen.end() )
1736                     // A new connection point has been added
1737                     // to the shape. Add a knot for it.
1738                     cc_active_shape_add_knot(cc->desktop, item, cc->connpthandles, it->second);
1739     }
1743 static void
1744 cc_set_active_conn(SPConnectorContext *cc, SPItem *item)
1746     g_assert( SP_IS_PATH(item) );
1748     SPCurve *curve = SP_PATH(item)->original_curve ? SP_PATH(item)->original_curve : SP_PATH(item)->curve;
1749     Geom::Matrix i2d = sp_item_i2d_affine(item);
1751     if (cc->active_conn == item)
1752     {
1753         // Just adjust handle positions.
1754         Geom::Point startpt = *(curve->first_point()) * i2d;
1755         sp_knot_set_position(cc->endpt_handle[0], startpt, 0);
1757         Geom::Point endpt = *(curve->last_point()) * i2d;
1758         sp_knot_set_position(cc->endpt_handle[1], endpt, 0);
1760         return;
1761     }
1763     cc->active_conn = item;
1765     // Remove existing active conn listeners
1766     if (cc->active_conn_repr) {
1767         sp_repr_remove_listener_by_data(cc->active_conn_repr, cc);
1768         Inkscape::GC::release(cc->active_conn_repr);
1769         cc->active_conn_repr = NULL;
1770     }
1772     // Listen in case the active conn changes
1773     cc->active_conn_repr = SP_OBJECT_REPR(item);
1774     if (cc->active_conn_repr) {
1775         Inkscape::GC::anchor(cc->active_conn_repr);
1776         sp_repr_add_listener(cc->active_conn_repr, &shape_repr_events, cc);
1777     }
1779     for (int i = 0; i < 2; ++i) {
1781         // Create the handle if it doesn't exist
1782         if ( cc->endpt_handle[i] == NULL ) {
1783             SPKnot *knot = sp_knot_new(cc->desktop,
1784                     _("<b>Connector endpoint</b>: drag to reroute or connect to new shapes"));
1786             knot->setShape(SP_KNOT_SHAPE_SQUARE);
1787             knot->setSize(7);
1788             knot->setAnchor(GTK_ANCHOR_CENTER);
1789             knot->setFill(0xffffff00, 0xff0000ff, 0xff0000ff);
1790             knot->setStroke(0x000000ff, 0x000000ff, 0x000000ff);
1791             sp_knot_update_ctrl(knot);
1793             // We don't want to use the standard knot handler,
1794             // since we don't want this knot to be draggable.
1795             g_signal_handler_disconnect(G_OBJECT(knot->item),
1796                     knot->_event_handler_id);
1797             knot->_event_handler_id = 0;
1799             gtk_signal_connect(GTK_OBJECT(knot->item), "event",
1800                     GTK_SIGNAL_FUNC(cc_generic_knot_handler), knot);
1802             cc->endpt_handle[i] = knot;
1803         }
1805         // Remove any existing handlers
1806         if (cc->endpt_handler_id[i]) {
1807             g_signal_handlers_disconnect_by_func(
1808                     G_OBJECT(cc->endpt_handle[i]->item),
1809                     (void*)G_CALLBACK(endpt_handler), (gpointer) cc );
1810             cc->endpt_handler_id[i] = 0;
1811         }
1813         // Setup handlers for connector endpoints, this is
1814         // is as 'after' so that cc_generic_knot_handler is
1815         // triggered first for any endpoint.
1816         cc->endpt_handler_id[i] = g_signal_connect_after(
1817                 G_OBJECT(cc->endpt_handle[i]->item), "event",
1818                 G_CALLBACK(endpt_handler), cc);
1819     }
1821     Geom::Point startpt = *(curve->first_point()) * i2d;
1822     sp_knot_set_position(cc->endpt_handle[0], startpt, 0);
1824     Geom::Point endpt = *(curve->last_point()) * i2d;
1825     sp_knot_set_position(cc->endpt_handle[1], endpt, 0);
1827     sp_knot_show(cc->endpt_handle[0]);
1828     sp_knot_show(cc->endpt_handle[1]);
1831 void cc_create_connection_point(SPConnectorContext* cc)
1833     if (cc->active_shape && cc->state == SP_CONNECTOR_CONTEXT_IDLE)
1834     {
1835         if (cc->selected_handle)
1836         {
1837             cc_deselect_handle( cc->selected_handle );
1838         }
1839         SPKnot *knot = sp_knot_new(cc->desktop, 0);
1840         // We do not process events on this knot.
1841         g_signal_handler_disconnect(G_OBJECT(knot->item),
1842                                     knot->_event_handler_id);
1843         knot->_event_handler_id = 0;
1845         cc_select_handle( knot );
1846         cc->selected_handle = knot;
1847         sp_knot_show(cc->selected_handle);
1848         cc->state = SP_CONNECTOR_CONTEXT_NEWCONNPOINT;
1849     }
1852 void cc_remove_connection_point(SPConnectorContext* cc)
1854     if (cc->selected_handle && cc->state == SP_CONNECTOR_CONTEXT_IDLE )
1855     {
1856         cc->active_shape->avoidRef->deleteConnectionPoint(cc->connpthandles[cc->selected_handle]);
1857         cc->selected_handle = NULL;
1858     }
1861 static bool cc_item_is_shape(SPItem *item)
1863     if (SP_IS_PATH(item)) {
1864         SPCurve *curve = (SP_SHAPE(item))->curve;
1865         if ( curve && !(curve->is_closed()) ) {
1866             // Open paths are connectors.
1867             return false;
1868         }
1869     }
1870     else if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
1871         Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1872         if (prefs->getBool("/tools/connector/ignoretext", true)) {
1873             // Don't count text as a shape we can connect connector to.
1874             return false;
1875         }
1876     }
1877     return true;
1881 bool cc_item_is_connector(SPItem *item)
1883     if (SP_IS_PATH(item)) {
1884         if (SP_PATH(item)->connEndPair.isAutoRoutingConn()) {
1885             g_assert( SP_PATH(item)->original_curve ? !(SP_PATH(item)->original_curve->is_closed()) : !(SP_PATH(item)->curve->is_closed()) );
1886             return true;
1887         }
1888     }
1889     return false;
1893 void cc_selection_set_avoid(bool const set_avoid)
1895     SPDesktop *desktop = inkscape_active_desktop();
1896     if (desktop == NULL) {
1897         return;
1898     }
1900     SPDocument *document = sp_desktop_document(desktop);
1902     Inkscape::Selection *selection = sp_desktop_selection(desktop);
1904     GSList *l = (GSList *) selection->itemList();
1906     int changes = 0;
1908     while (l) {
1909         SPItem *item = (SPItem *) l->data;
1911         char const *value = (set_avoid) ? "true" : NULL;
1913         if (cc_item_is_shape(item)) {
1914             sp_object_setAttribute(item, "inkscape:connector-avoid",
1915                     value, false);
1916             item->avoidRef->handleSettingChange();
1917             changes++;
1918         }
1920         l = l->next;
1921     }
1923     if (changes == 0) {
1924         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE,
1925                 _("Select <b>at least one non-connector object</b>."));
1926         return;
1927     }
1929     char *event_desc = (set_avoid) ?
1930             _("Make connectors avoid selected objects") :
1931             _("Make connectors ignore selected objects");
1932     sp_document_done(document, SP_VERB_CONTEXT_CONNECTOR, event_desc);
1936 static void
1937 cc_selection_changed(Inkscape::Selection *selection, gpointer data)
1939     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(data);
1940     //SPEventContext *ec = SP_EVENT_CONTEXT(cc);
1942     SPItem *item = selection->singleItem();
1944     if (cc->active_conn == item)
1945     {
1946         // Nothing to change.
1947         return;
1948     }
1949     if (item == NULL)
1950     {
1951         cc_clear_active_conn(cc);
1952         return;
1953     }
1955     if (cc_item_is_connector(item)) {
1956         cc_set_active_conn(cc, item);
1957     }
1961 static void
1962 shape_event_attr_deleted(Inkscape::XML::Node */*repr*/, Inkscape::XML::Node *child,
1963                          Inkscape::XML::Node */*ref*/, gpointer data)
1965     g_assert(data);
1966     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(data);
1968     if (child == cc->active_shape_repr) {
1969         // The active shape has been deleted.  Clear active shape.
1970         cc_clear_active_shape(cc);
1971     }
1975 static void
1976 shape_event_attr_changed(Inkscape::XML::Node *repr, gchar const *name,
1977                          gchar const */*old_value*/, gchar const */*new_value*/,
1978                          bool /*is_interactive*/, gpointer data)
1980     g_assert(data);
1981     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(data);
1983     // Look for changes that result in onscreen movement.
1984     if (!strcmp(name, "d") || !strcmp(name, "x") || !strcmp(name, "y") ||
1985             !strcmp(name, "width") || !strcmp(name, "height") ||
1986             !strcmp(name, "transform"))
1987     {
1988         if (repr == cc->active_shape_repr) {
1989             // Active shape has moved. Clear active shape.
1990             cc_clear_active_shape(cc);
1991         }
1992         else if (repr == cc->active_conn_repr) {
1993             // The active conn has been moved.
1994             // Set it again, which just sets new handle positions.
1995             cc_set_active_conn(cc, cc->active_conn);
1996         }
1997     }
1998     else
1999         if ( !strcmp(name, "inkscape:connection-points") )
2000             if (repr == cc->active_shape_repr)
2001                 // The connection points of the active shape
2002                 // have changed. Update them.
2003                 cc_set_active_shape(cc, cc->active_shape);
2007 /*
2008   Local Variables:
2009   mode:c++
2010   c-file-style:"stroustrup"
2011   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
2012   indent-tabs-mode:nil
2013   fill-column:99
2014   End:
2015 */
2016 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :