Code

NEW: "+LaTeX" option for PDF/EPS/PS export
[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 bool conn_pt_handle_test(SPConnectorContext *cc, Geom::Point& p, gchar **href, gchar **cpid);
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->shref = NULL;
342     cc->scpid = NULL;
343     cc->ehref = NULL;
344     cc->ecpid = NULL;
345     cc->npoints = 0;
346     cc->state = SP_CONNECTOR_CONTEXT_IDLE;
350 static void
351 sp_connector_context_dispose(GObject *object)
353     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(object);
355     cc->sel_changed_connection.disconnect();
357     if (!cc->connpthandles.empty()) {
358         for (ConnectionPointMap::iterator it = cc->connpthandles.begin();
359                 it != cc->connpthandles.end(); ++it) {
360             g_object_unref(it->first);
361         }
362         cc->connpthandles.clear();
363     }
364     cc->connpthandles.~ConnectionPointMap();
365     for (int i = 0; i < 2; ++i) {
366         if (cc->endpt_handle[1]) {
367             g_object_unref(cc->endpt_handle[i]);
368             cc->endpt_handle[i] = NULL;
369         }
370     }
371     if (cc->shref) {
372         g_free(cc->shref);
373         cc->shref = NULL;
374     }
375     if (cc->scpid) {
376         g_free(cc->scpid);
377         cc->scpid = NULL;
378     }
379     if (cc->ehref) {
380         g_free(cc->shref);
381         cc->shref = NULL;
382     }
383     if (cc->ecpid) {
384         g_free(cc->scpid);
385         cc->scpid = NULL;
386     }
387     g_assert( cc->newConnRef == NULL );
389     G_OBJECT_CLASS(parent_class)->dispose(object);
393 static void
394 sp_connector_context_setup(SPEventContext *ec)
396     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(ec);
397     SPDesktop *dt = ec->desktop;
399     if (((SPEventContextClass *) parent_class)->setup) {
400         ((SPEventContextClass *) parent_class)->setup(ec);
401     }
403     cc->selection = sp_desktop_selection(dt);
405     cc->sel_changed_connection.disconnect();
406     cc->sel_changed_connection = cc->selection->connectChanged(
407             sigc::bind(sigc::ptr_fun(&cc_selection_changed),
408             (gpointer) cc));
410     /* Create red bpath */
411     cc->red_bpath = sp_canvas_bpath_new(sp_desktop_sketch(ec->desktop), NULL);
412     sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cc->red_bpath), cc->red_color,
413             1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
414     sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(cc->red_bpath), 0x00000000,
415             SP_WIND_RULE_NONZERO);
416     /* Create red curve */
417     cc->red_curve = new SPCurve();
419     /* Create green curve */
420     cc->green_curve = new SPCurve();
422     // Notice the initial selection.
423     cc_selection_changed(cc->selection, (gpointer) cc);
425     cc->within_tolerance = false;
427     sp_event_context_read(ec, "curvature");
428     sp_event_context_read(ec, "orthogonal");
429     sp_event_context_read(ec, "mode");
430     cc->knot_tip = cc->mode == SP_CONNECTOR_CONTEXT_DRAWING_MODE ? cc_knot_tips[0] : cc_knot_tips[1];
431     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
432     if (prefs->getBool("/tools/connector/selcue", 0)) {
433         ec->enableSelectionCue();
434     }
436     // Make sure we see all enter events for canvas items,
437     // even if a mouse button is depressed.
438     dt->canvas->gen_all_enter_events = true;
442 static void
443 sp_connector_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val)
445     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(ec);
447     /* fixme: Proper error handling for non-numeric data.  Use a locale-independent function like
448      * g_ascii_strtod (or a thin wrapper that does the right thing for invalid values inf/nan). */
449     Glib::ustring name = val->getEntryName();
450     if ( name == "curvature" ) {
451         cc->curvature = val->getDoubleLimited(); // prevents NaN and +/-Inf from messing up
452     }
453     else if ( name == "orthogonal" ) {
454         cc->isOrthogonal = val->getBool();
455     }
456     else if ( name == "mode")
457     {
458         sp_connector_context_switch_mode(ec, val->getBool() ? SP_CONNECTOR_CONTEXT_EDITING_MODE : SP_CONNECTOR_CONTEXT_DRAWING_MODE);
459     }
462 void sp_connector_context_switch_mode(SPEventContext* ec, unsigned int newMode)
464     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(ec);
466     cc->mode = newMode;
467     if ( cc->mode == SP_CONNECTOR_CONTEXT_DRAWING_MODE )
468     {
469         ec->cursor_shape = cursor_connector_xpm;
470         cc->knot_tip = cc_knot_tips[0];
471         if (cc->selected_handle)
472             cc_deselect_handle( cc->selected_handle );
473         cc->selected_handle = NULL;
474         // Show all default connection points
476     }
477     else if ( cc->mode == SP_CONNECTOR_CONTEXT_EDITING_MODE )
478     {
479         ec->cursor_shape = cursor_node_xpm;
480         cc->knot_tip = cc_knot_tips[1];
481 /*            if (cc->active_shape)
482         {
483             cc->selection->set( SP_OBJECT( cc->active_shape ) );
484         }
485         else
486         {
487             SPItem* item = cc->selection->singleItem();
488             if ( item )
489             {
490                 cc_set_active_shape(cc, item);
491                 cc->selection->set( SP_OBJECT( item ) );
492             }
493         }*/
494     }
495     sp_event_context_update_cursor(ec);
500 static void
501 sp_connector_context_finish(SPEventContext *ec)
503     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(ec);
505     spcc_connector_finish(cc);
506     cc->state = SP_CONNECTOR_CONTEXT_IDLE;
508     if (((SPEventContextClass *) parent_class)->finish) {
509         ((SPEventContextClass *) parent_class)->finish(ec);
510     }
512     if (cc->selection) {
513         cc->selection = NULL;
514     }
515     cc_clear_active_shape(cc);
516     cc_clear_active_conn(cc);
518     // Restore the default event generating behaviour.
519     SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(ec);
520     desktop->canvas->gen_all_enter_events = false;
524 //-----------------------------------------------------------------------------
527 static void
528 cc_clear_active_shape(SPConnectorContext *cc)
530     if (cc->active_shape == NULL) {
531         return;
532     }
533     g_assert( cc->active_shape_repr );
534     g_assert( cc->active_shape_layer_repr );
536     cc->active_shape = NULL;
538     if (cc->active_shape_repr) {
539         sp_repr_remove_listener_by_data(cc->active_shape_repr, cc);
540         Inkscape::GC::release(cc->active_shape_repr);
541         cc->active_shape_repr = NULL;
543         sp_repr_remove_listener_by_data(cc->active_shape_layer_repr, cc);
544         Inkscape::GC::release(cc->active_shape_layer_repr);
545         cc->active_shape_layer_repr = NULL;
546     }
548     // Hide the connection points if they exist.
549     if (cc->connpthandles.size()) {
550         for (ConnectionPointMap::iterator it = cc->connpthandles.begin();
551                 it != cc->connpthandles.end(); ++it) {
552             sp_knot_hide(it->first);
553         }
554     }
558 static void
559 cc_clear_active_conn(SPConnectorContext *cc)
561     if (cc->active_conn == NULL) {
562         return;
563     }
564     g_assert( cc->active_conn_repr );
566     cc->active_conn = NULL;
568     if (cc->active_conn_repr) {
569         sp_repr_remove_listener_by_data(cc->active_conn_repr, cc);
570         Inkscape::GC::release(cc->active_conn_repr);
571         cc->active_conn_repr = NULL;
572     }
574     // Hide the endpoint handles.
575     for (int i = 0; i < 2; ++i) {
576         if (cc->endpt_handle[i]) {
577             sp_knot_hide(cc->endpt_handle[i]);
578         }
579     }
583 static bool
584 conn_pt_handle_test(SPConnectorContext *cc, Geom::Point& p, gchar **href, gchar **cpid)
586     // TODO: this will need to change when there are more connection
587     //       points available for each shape.
589     if (cc->active_handle && (cc->connpthandles.find(cc->active_handle) != cc->connpthandles.end()))
590     {
591         p = cc->active_handle->pos;
592         const ConnectionPoint& cp = cc->connpthandles[cc->active_handle];
593         *href = g_strdup_printf("#%s", SP_OBJECT_ID(cc->active_shape));
594         *cpid = g_strdup_printf("%c%d", cp.type == ConnPointDefault ? 'd' : 'u' , cp.id);
595         return true;
596     }
597     *href = NULL;
598     *cpid = NULL;
599     return false;
602 static void
603 cc_select_handle(SPKnot* knot)
605     knot->setShape(SP_KNOT_SHAPE_SQUARE);
606     knot->setSize(10);
607     knot->setAnchor(GTK_ANCHOR_CENTER);
608     knot->setFill(0x0000ffff, 0x0000ffff, 0x0000ffff);
609     sp_knot_update_ctrl(knot);
612 static void
613 cc_deselect_handle(SPKnot* knot)
615     knot->setShape(SP_KNOT_SHAPE_SQUARE);
616     knot->setSize(8);
617     knot->setAnchor(GTK_ANCHOR_CENTER);
618     knot->setFill(0xffffff00, 0xff0000ff, 0xff0000ff);
619     sp_knot_update_ctrl(knot);
622 static gint
623 sp_connector_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
625     gint ret = FALSE;
627     SPDesktop *desktop = event_context->desktop;
629     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(event_context);
631     Geom::Point p(event->button.x, event->button.y);
633     switch (event->type) {
634         case GDK_BUTTON_RELEASE:
635             if (event->button.button == 1 && !event_context->space_panning) {
636                 if ((cc->state == SP_CONNECTOR_CONTEXT_DRAGGING) &&
637                         (event_context->within_tolerance))
638                 {
639                     spcc_reset_colors(cc);
640                     cc->state = SP_CONNECTOR_CONTEXT_IDLE;
641                 }
642                 if (cc->state != SP_CONNECTOR_CONTEXT_IDLE) {
643                     // Doing something else like rerouting.
644                     break;
645                 }
646                 // find out clicked item, honoring Alt
647                 SPItem *item = sp_event_context_find_item(desktop,
648                         p, event->button.state & GDK_MOD1_MASK, FALSE);
650                 if (event->button.state & GDK_SHIFT_MASK) {
651                     cc->selection->toggle(item);
652                 } else {
653                     cc->selection->set(item);
654                     if ( cc->mode == SP_CONNECTOR_CONTEXT_EDITING_MODE && cc->selected_handle )
655                     {
656                         cc_deselect_handle( cc->selected_handle );
657                         cc->selected_handle = NULL;
658                     }
659                     /* When selecting a new item,
660                        do not allow showing connection points
661                        on connectors. (yet?)
662                     */
663                     if ( item != cc->active_shape && !cc_item_is_connector( item ) )
664                         cc_set_active_shape( cc, item );
665                 }
666                 ret = TRUE;
668             }
669             break;
670         case GDK_ENTER_NOTIFY:
671         {
672             if (cc->mode == SP_CONNECTOR_CONTEXT_DRAWING_MODE || (cc->mode == SP_CONNECTOR_CONTEXT_EDITING_MODE && !cc->selected_handle))
673             {
674                 if (cc_item_is_shape(item)) {
676                     // I don't really understand what the above does,
677                     // so I commented it.
678                     // This is a shape, so show connection point(s).
679     /*                if (!(cc->active_shape)
680                             // Don't show handle for another handle.
681     //                         || (cc->connpthandles.find((SPKnot*) item) != cc->connpthandles.end())
682                         )
683                     {
684                         cc_set_active_shape(cc, item);
685                     }*/
686                     cc_set_active_shape(cc, item);
687                 }
688                 ret = TRUE;
689             }
690             break;
691         }
692         default:
693             break;
694     }
696     return ret;
700 gint
701 sp_connector_context_root_handler(SPEventContext *ec, GdkEvent *event)
703     SPConnectorContext *const cc = SP_CONNECTOR_CONTEXT(ec);
705     gint ret = FALSE;
707     switch (event->type) {
708         case GDK_BUTTON_PRESS:
709             ret = connector_handle_button_press(cc, event->button);
710             break;
712         case GDK_MOTION_NOTIFY:
713             ret = connector_handle_motion_notify(cc, event->motion);
714             break;
716         case GDK_BUTTON_RELEASE:
717             ret = connector_handle_button_release(cc, event->button);
718             break;
719         case GDK_KEY_PRESS:
720             ret = connector_handle_key_press(cc, get_group0_keyval (&event->key));
721             break;
723         default:
724             break;
725     }
727     if (!ret) {
728         gint (*const parent_root_handler)(SPEventContext *, GdkEvent *)
729             = ((SPEventContextClass *) parent_class)->root_handler;
730         if (parent_root_handler) {
731             ret = parent_root_handler(ec, event);
732         }
733     }
735     return ret;
739 static gint
740 connector_handle_button_press(SPConnectorContext *const cc, GdkEventButton const &bevent)
742     Geom::Point const event_w(bevent.x, bevent.y);
743     /* Find desktop coordinates */
744     Geom::Point p = cc->desktop->w2d(event_w);
745     SPEventContext *event_context = SP_EVENT_CONTEXT(cc);
747     gint ret = FALSE;
748     if ( cc->mode == SP_CONNECTOR_CONTEXT_DRAWING_MODE )
749     {
750         if ( bevent.button == 1 && !event_context->space_panning ) {
752             SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
754             if (Inkscape::have_viable_layer(desktop, cc->_message_context) == false) {
755                 return TRUE;
756             }
758             Geom::Point const event_w(bevent.x,
759                                     bevent.y);
760 //             connector_drag_origin_w = event_w;
761             cc->xp = bevent.x;
762             cc->yp = bevent.y;
763             cc->within_tolerance = true;
765             Geom::Point const event_dt = cc->desktop->w2d(event_w);
767             SnapManager &m = cc->desktop->namedview->snap_manager;
768             m.setup(cc->desktop);
770             switch (cc->state) {
771                 case SP_CONNECTOR_CONTEXT_STOP:
772                     /* This is allowed, if we just canceled curve */
773                 case SP_CONNECTOR_CONTEXT_IDLE:
774                 {
775                     if ( cc->npoints == 0 ) {
776                         cc_clear_active_conn(cc);
778                         SP_EVENT_CONTEXT_DESKTOP(cc)->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating new connector"));
780                         /* Set start anchor */
781                         /* Create green anchor */
782                         Geom::Point p = event_dt;
784                         // Test whether we clicked on a connection point
785                         bool found = conn_pt_handle_test(cc, p, &cc->shref, &cc->scpid);
787                         if (!found) {
788                             // This is the first point, so just snap it to the grid
789                             // as there's no other points to go off.
790                             m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE);
791                         }
792                         spcc_connector_set_initial_point(cc, p);
794                     }
795                     cc->state = SP_CONNECTOR_CONTEXT_DRAGGING;
796                     ret = TRUE;
797                     break;
798                 }
799                 case SP_CONNECTOR_CONTEXT_DRAGGING:
800                 {
801                     // This is the second click of a connector creation.
802                     m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE);
804                     spcc_connector_set_subsequent_point(cc, p);
805                     spcc_connector_finish_segment(cc, p);
806                     // Test whether we clicked on a connection point
807                     /*bool found = */conn_pt_handle_test(cc, p, &cc->ehref, &cc->ecpid);
808                     if (cc->npoints != 0) {
809                         spcc_connector_finish(cc);
810                     }
811                     cc_set_active_conn(cc, cc->newconn);
812                     cc->state = SP_CONNECTOR_CONTEXT_IDLE;
813                     ret = TRUE;
814                     break;
815                 }
816                 case SP_CONNECTOR_CONTEXT_CLOSE:
817                 {
818                     g_warning("Button down in CLOSE state");
819                     break;
820                 }
821                 default:
822                     break;
823             }
824         } else if (bevent.button == 3) {
825             if (cc->state == SP_CONNECTOR_CONTEXT_REROUTING) {
826                 // A context menu is going to be triggered here,
827                 // so end the rerouting operation.
828                 cc_connector_rerouting_finish(cc, &p);
830                 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
832                 // Don't set ret to TRUE, so we drop through to the
833                 // parent handler which will open the context menu.
834             }
835             else if (cc->npoints != 0) {
836                 spcc_connector_finish(cc);
837                 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
838                 ret = TRUE;
839             }
840         }
841     }
842     else if ( cc->mode == SP_CONNECTOR_CONTEXT_EDITING_MODE )
843     {
844         if ( bevent.button == 1 && !event_context->space_panning )
845         {
846             // Initialize variables in case of dragging
848             SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
850             if (Inkscape::have_viable_layer(desktop, cc->_message_context) == false) {
851                 return TRUE;
852             }
854             cc->xp = bevent.x;
855             cc->yp = bevent.y;
856             cc->within_tolerance = true;
858             ConnectionPointMap::iterator const& active_knot_it = cc->connpthandles.find( cc->active_handle );
860             switch (cc->state)
861             {
862                 case SP_CONNECTOR_CONTEXT_IDLE:
863                     if ( active_knot_it != cc->connpthandles.end() )
864                     {
865                         // We do not allow selecting and, thereby, moving default knots
866                         if ( active_knot_it->second.type != ConnPointDefault)
867                         {
868                             if (cc->selected_handle != cc->active_handle)
869                             {
870                                 if ( cc->selected_handle )
871                                     cc_deselect_handle( cc->selected_handle );
872                                 cc->selected_handle = cc->active_handle;
873                                 cc_select_handle( cc->selected_handle );
874                             }
875                         }
876                         else
877                             // Just ignore the default connection point
878                             return FALSE;
879                     }
880                     else
881                         if ( cc->selected_handle )
882                         {
883                             cc_deselect_handle( cc->selected_handle );
884                             cc->selected_handle = NULL;
885                         }
887                     if ( cc->selected_handle )
888                     {
889                         cc->state = SP_CONNECTOR_CONTEXT_DRAGGING;
890                         cc->selection->set( SP_OBJECT( cc->active_shape ) );
891                     }
893                     ret = TRUE;
894                     break;
895                 // Dragging valid because of the way we create
896                 // new connection points.
897                 case SP_CONNECTOR_CONTEXT_DRAGGING:
898                     // Do nothing.
899                     ret = TRUE;
900                     break;
901             }
902         }
903     }
904     return ret;
908 static gint
909 connector_handle_motion_notify(SPConnectorContext *const cc, GdkEventMotion const &mevent)
911     gint ret = FALSE;
912     SPEventContext *event_context = SP_EVENT_CONTEXT(cc);
913     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
915     if (event_context->space_panning || mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) {
916         // allow middle-button scrolling
917         return FALSE;
918     }
920     Geom::Point const event_w(mevent.x, mevent.y);
922     if (cc->within_tolerance) {
923         cc->tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
924         if ( ( abs( (gint) mevent.x - cc->xp ) < cc->tolerance ) &&
925              ( abs( (gint) mevent.y - cc->yp ) < cc->tolerance ) ) {
926             return FALSE;   // Do not drag if we're within tolerance from origin.
927         }
928     }
929     // Once the user has moved farther than tolerance from the original location
930     // (indicating they intend to move the object, not click), then always process
931     // the motion notify coordinates as given (no snapping back to origin)
932     cc->within_tolerance = false;
934     SPDesktop *const dt = cc->desktop;
936     /* Find desktop coordinates */
937     Geom::Point p = dt->w2d(event_w);
939     if ( cc->mode == SP_CONNECTOR_CONTEXT_DRAWING_MODE )
940     {
941         SnapManager &m = dt->namedview->snap_manager;
942         m.setup(dt);
944         switch (cc->state) {
945             case SP_CONNECTOR_CONTEXT_DRAGGING:
946             {
947                 gobble_motion_events(mevent.state);
948                 // This is movement during a connector creation.
949                 if ( cc->npoints > 0 ) {
950                     m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE);
951                     cc->selection->clear();
952                     spcc_connector_set_subsequent_point(cc, p);
953                     ret = TRUE;
954                 }
955                 break;
956             }
957             case SP_CONNECTOR_CONTEXT_REROUTING:
958             {
959                 gobble_motion_events(GDK_BUTTON1_MASK);
960                 g_assert( SP_IS_PATH(cc->clickeditem));
962                 m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE);
964                 // Update the hidden path
965                 Geom::Matrix i2d = sp_item_i2d_affine(cc->clickeditem);
966                 Geom::Matrix d2i = i2d.inverse();
967                 SPPath *path = SP_PATH(cc->clickeditem);
968                 SPCurve *curve = path->original_curve ? path->original_curve : path->curve;
969                 if (cc->clickedhandle == cc->endpt_handle[0]) {
970                     Geom::Point o = cc->endpt_handle[1]->pos;
971                     curve->stretch_endpoints(p * d2i, o * d2i);
972                 }
973                 else {
974                     Geom::Point o = cc->endpt_handle[0]->pos;
975                     curve->stretch_endpoints(o * d2i, p * d2i);
976                 }
977                 sp_conn_reroute_path_immediate(path);
979                 // Copy this to the temporary visible path
980                 cc->red_curve = path->original_curve ?
981                         path->original_curve->copy() : path->curve->copy();
982                 cc->red_curve->transform(i2d);
984                 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), cc->red_curve);
985                 ret = TRUE;
986                 break;
987             }
988             case SP_CONNECTOR_CONTEXT_STOP:
989                 /* This is perfectly valid */
990                 break;
991             default:
992                 if (!sp_event_context_knot_mouseover(cc)) {
993                     m.preSnap(Inkscape::SnapCandidatePoint(p, Inkscape::SNAPSOURCE_OTHER_HANDLE));
994                 }
995                 break;
996         }
997     }
998     else if ( cc->mode == SP_CONNECTOR_CONTEXT_EDITING_MODE )
999     {
1000         switch ( cc->state )
1001         {
1002             case SP_CONNECTOR_CONTEXT_DRAGGING:
1003                 sp_knot_set_position(cc->selected_handle, p, 0);
1004                 ret = TRUE;
1005                 break;
1006             case SP_CONNECTOR_CONTEXT_NEWCONNPOINT:
1007                 sp_knot_set_position(cc->selected_handle, p, 0);
1008                 ret = TRUE;
1009                 break;
1010         }
1011     }
1013     return ret;
1017 static gint
1018 connector_handle_button_release(SPConnectorContext *const cc, GdkEventButton const &revent)
1020     gint ret = FALSE;
1021     SPEventContext *event_context = SP_EVENT_CONTEXT(cc);
1022     if ( revent.button == 1 && !event_context->space_panning ) {
1024         SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
1025         SPDocument *doc = sp_desktop_document(desktop);
1027         SnapManager &m = desktop->namedview->snap_manager;
1028         m.setup(desktop);
1030         Geom::Point const event_w(revent.x, revent.y);
1032         /* Find desktop coordinates */
1033         Geom::Point p = cc->desktop->w2d(event_w);
1034         if ( cc->mode == SP_CONNECTOR_CONTEXT_DRAWING_MODE )
1035         {
1036             switch (cc->state) {
1037                 //case SP_CONNECTOR_CONTEXT_POINT:
1038                 case SP_CONNECTOR_CONTEXT_DRAGGING:
1039                 {
1040                     m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE);
1042                     if (cc->within_tolerance)
1043                     {
1044                         spcc_connector_finish_segment(cc, p);
1045                         return TRUE;
1046                     }
1047                     // Connector has been created via a drag, end it now.
1048                     spcc_connector_set_subsequent_point(cc, p);
1049                     spcc_connector_finish_segment(cc, p);
1050                     // Test whether we clicked on a connection point
1051                     /*bool found = */conn_pt_handle_test(cc, p, &cc->ehref, &cc->ecpid);
1052                     if (cc->npoints != 0) {
1053                         spcc_connector_finish(cc);
1054                     }
1055                     cc_set_active_conn(cc, cc->newconn);
1056                     cc->state = SP_CONNECTOR_CONTEXT_IDLE;
1057                     break;
1058                 }
1059                 case SP_CONNECTOR_CONTEXT_REROUTING:
1060                 {
1061                     m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE);
1062                     cc_connector_rerouting_finish(cc, &p);
1064                     sp_document_ensure_up_to_date(doc);
1065                     cc->state = SP_CONNECTOR_CONTEXT_IDLE;
1066                     return TRUE;
1067                     break;
1068                 }
1069                 case SP_CONNECTOR_CONTEXT_STOP:
1070                     /* This is allowed, if we just cancelled curve */
1071                     break;
1072                 default:
1073                     break;
1074             }
1075             ret = TRUE;
1076         }
1077         else if ( cc->mode == SP_CONNECTOR_CONTEXT_EDITING_MODE )
1078         {
1079             switch ( cc->state )
1080             {
1081                 case SP_CONNECTOR_CONTEXT_DRAGGING:
1083                     if (!cc->within_tolerance)
1084                     {
1085                         m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE);
1086                         sp_knot_set_position(cc->selected_handle, p, 0);
1087                         ConnectionPoint& cp = cc->connpthandles[cc->selected_handle];
1088                         cp.pos = p * sp_item_dt2i_affine(cc->active_shape);
1089                         cc->active_shape->avoidRef->updateConnectionPoint(cp);
1090                     }
1092                     cc->state = SP_CONNECTOR_CONTEXT_IDLE;
1093                     ret = TRUE;
1094                     break;
1097                 case SP_CONNECTOR_CONTEXT_NEWCONNPOINT:
1098                     m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE);
1100                     sp_knot_set_position(cc->selected_handle, p, 0);
1102                     ConnectionPoint cp;
1103                     cp.type = ConnPointUserDefined;
1104                     cp.pos = p * sp_item_dt2i_affine(cc->active_shape);
1105                     cp.dir = Avoid::ConnDirAll;
1106                     g_object_unref(cc->selected_handle);
1107                     cc->active_shape->avoidRef->addConnectionPoint(cp);
1108                     sp_document_ensure_up_to_date(doc);
1109                     for (ConnectionPointMap::iterator it = cc->connpthandles.begin(); it != cc->connpthandles.end(); ++it)
1110                         if (it->second.type == ConnPointUserDefined && it->second.id == cp.id)
1111                         {
1112                             cc->selected_handle = it->first;
1113                             break;
1114                         }
1115                     cc_select_handle( cc->selected_handle );
1116                     cc->state = SP_CONNECTOR_CONTEXT_IDLE;
1117                     ret = TRUE;
1118                     break;
1119             }
1120         }
1121     }
1124     return ret;
1128 static gint
1129 connector_handle_key_press(SPConnectorContext *const cc, guint const keyval)
1131     gint ret = FALSE;
1132     /* fixme: */
1133     if ( cc->mode == SP_CONNECTOR_CONTEXT_DRAWING_MODE )
1134     {
1135         switch (keyval) {
1136             case GDK_Return:
1137             case GDK_KP_Enter:
1138                 if (cc->npoints != 0) {
1139                     spcc_connector_finish(cc);
1140                     cc->state = SP_CONNECTOR_CONTEXT_IDLE;
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                     desktop->messageStack()->flash( Inkscape::NORMAL_MESSAGE,
1156                             _("Connector endpoint drag cancelled."));
1157                     ret = TRUE;
1158                 }
1159                 else if (cc->npoints != 0) {
1160                     // if drawing, cancel, otherwise pass it up for deselecting
1161                     cc->state = SP_CONNECTOR_CONTEXT_STOP;
1162                     spcc_reset_colors(cc);
1163                     ret = TRUE;
1164                 }
1165                 break;
1166             default:
1167                 break;
1168         }
1169     }
1170     else if ( cc->mode == SP_CONNECTOR_CONTEXT_EDITING_MODE )
1171     {
1172         switch ( cc->state )
1173         {
1174             case SP_CONNECTOR_CONTEXT_DRAGGING:
1175                 if ( keyval == GDK_Escape )
1176                 {
1177                     // Cancel connection point dragging
1179                     // Obtain original position
1180                     ConnectionPoint const& cp = cc->connpthandles[cc->selected_handle];
1181                     SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
1182                     const Geom::Matrix& i2doc = sp_item_i2doc_affine(cc->active_shape);
1183                     sp_knot_set_position(cc->selected_handle, cp.pos * i2doc * desktop->doc2dt(), 0);
1184                     cc->state = SP_CONNECTOR_CONTEXT_IDLE;
1185                     desktop->messageStack()->flash( Inkscape::NORMAL_MESSAGE,
1186                         _("Connection point drag cancelled."));
1187                     ret = TRUE;
1188                 }
1189                 else if ( keyval == GDK_Return || keyval == GDK_KP_Enter )
1190                 {
1191                     // Put connection point at current position
1193                     SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
1194                     SnapManager &m = desktop->namedview->snap_manager;
1195                     m.setup(desktop);
1196                     Geom::Point p = cc->selected_handle->pos;
1197 //                     SPEventContext* event_context = SP_EVENT_CONTEXT( cc );
1199                     if (!cc->within_tolerance)
1200                     {
1201                         m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE);
1202                         sp_knot_set_position(cc->selected_handle, p, 0);
1203                         ConnectionPoint& cp = cc->connpthandles[cc->selected_handle];
1204                         cp.pos = p * sp_item_dt2i_affine(cc->active_shape);
1205                         cc->active_shape->avoidRef->updateConnectionPoint(cp);
1206                     }
1208                     cc->state = SP_CONNECTOR_CONTEXT_IDLE;
1209                     ret = TRUE;
1210                 }
1211                 break;
1212             case SP_CONNECTOR_CONTEXT_NEWCONNPOINT:
1213                 if ( keyval == GDK_Escape )
1214                 {
1215                     // Just destroy the knot
1216                     g_object_unref( cc->selected_handle );
1217                     cc->selected_handle = NULL;
1218                     cc->state = SP_CONNECTOR_CONTEXT_IDLE;
1219                     ret = TRUE;
1220                 }
1221                 else if ( keyval == GDK_Return || keyval == GDK_KP_Enter )
1222                 {
1223                     SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
1224                     SPDocument *doc = sp_desktop_document(desktop);
1225                     SnapManager &m = desktop->namedview->snap_manager;
1226                     m.setup(desktop);
1227                     Geom::Point p = cc->selected_handle->pos;
1229                     m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE);
1231                     sp_knot_set_position(cc->selected_handle, p, 0);
1233                     ConnectionPoint cp;
1234                     cp.type = ConnPointUserDefined;
1235                     cp.pos = p * sp_item_dt2i_affine(cc->active_shape);
1236                     cp.dir = Avoid::ConnDirAll;
1237                     g_object_unref(cc->selected_handle);
1238                     cc->active_shape->avoidRef->addConnectionPoint(cp);
1239                     sp_document_ensure_up_to_date(doc);
1240                     for (ConnectionPointMap::iterator it = cc->connpthandles.begin(); it != cc->connpthandles.end(); ++it)
1241                         if (it->second.type == ConnPointUserDefined && it->second.id == cp.id)
1242                         {
1243                             cc->selected_handle = it->first;
1244                             break;
1245                         }
1246                     cc_select_handle( cc->selected_handle );
1247                     cc->state = SP_CONNECTOR_CONTEXT_IDLE;
1248                     ret = TRUE;
1249                 }
1251                 break;
1252             case SP_CONNECTOR_CONTEXT_IDLE:
1253                 if ( keyval == GDK_Delete && cc->selected_handle )
1254                 {
1255                     cc->active_shape->avoidRef->deleteConnectionPoint(cc->connpthandles[cc->selected_handle]);
1256                     cc->selected_handle = NULL;
1257                     ret = TRUE;
1258                 }
1260                 break;
1261         }
1262     }
1264     return ret;
1268 static void
1269 cc_connector_rerouting_finish(SPConnectorContext *const cc, Geom::Point *const p)
1271     SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
1272     SPDocument *doc = sp_desktop_document(desktop);
1274     // Clear the temporary path:
1275     cc->red_curve->reset();
1276     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
1278     if (p != NULL)
1279     {
1280         // Test whether we clicked on a connection point
1281         gchar *shape_label, *cpid;
1282         bool found = conn_pt_handle_test(cc, *p, &shape_label, &cpid);
1284         if (found) {
1285             if (cc->clickedhandle == cc->endpt_handle[0]) {
1286                 sp_object_setAttribute(cc->clickeditem,
1287                         "inkscape:connection-start", shape_label, false);
1288                 sp_object_setAttribute(cc->clickeditem,
1289                         "inkscape:connection-start-point", cpid, false);
1290             }
1291             else {
1292                 sp_object_setAttribute(cc->clickeditem,
1293                         "inkscape:connection-end", shape_label, false);
1294                 sp_object_setAttribute(cc->clickeditem,
1295                         "inkscape:connection-end-point", cpid, false);
1296             }
1297             g_free(shape_label);
1298         }
1299     }
1300     cc->clickeditem->setHidden(false);
1301     sp_conn_reroute_path_immediate(SP_PATH(cc->clickeditem));
1302     cc->clickeditem->updateRepr();
1303     sp_document_done(doc, SP_VERB_CONTEXT_CONNECTOR,
1304                      _("Reroute connector"));
1305     cc_set_active_conn(cc, cc->clickeditem);
1309 static void
1310 spcc_reset_colors(SPConnectorContext *cc)
1312     /* Red */
1313     cc->red_curve->reset();
1314     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
1316     cc->green_curve->reset();
1317     cc->npoints = 0;
1321 static void
1322 spcc_connector_set_initial_point(SPConnectorContext *const cc, Geom::Point const p)
1324     g_assert( cc->npoints == 0 );
1326     cc->p[0] = p;
1327     cc->p[1] = p;
1328     cc->npoints = 2;
1329     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
1333 static void
1334 spcc_connector_set_subsequent_point(SPConnectorContext *const cc, Geom::Point const p)
1336     g_assert( cc->npoints != 0 );
1338     SPDesktop *dt = cc->desktop;
1339     Geom::Point o = dt->dt2doc(cc->p[0]);
1340     Geom::Point d = dt->dt2doc(p);
1341     Avoid::Point src(o[Geom::X], o[Geom::Y]);
1342     Avoid::Point dst(d[Geom::X], d[Geom::Y]);
1344     if (!cc->newConnRef) {
1345         Avoid::Router *router = sp_desktop_document(dt)->router;
1346         cc->newConnRef = new Avoid::ConnRef(router);
1347         cc->newConnRef->setEndpoint(Avoid::VertID::src, src);
1348         if (cc->isOrthogonal)
1349             cc->newConnRef->setRoutingType(Avoid::ConnType_Orthogonal);
1350         else
1351             cc->newConnRef->setRoutingType(Avoid::ConnType_PolyLine);
1352     }
1353     // Set new endpoint.
1354     cc->newConnRef->setEndpoint(Avoid::VertID::tar, dst);
1355     // Immediately generate new routes for connector.
1356     cc->newConnRef->makePathInvalid();
1357     cc->newConnRef->router()->processTransaction();
1358     // Recreate curve from libavoid route.
1359     recreateCurve( cc->red_curve, cc->newConnRef, cc->curvature );
1360     cc->red_curve->transform(dt->doc2dt());
1361     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), cc->red_curve);
1365 /**
1366  * Concats red, blue and green.
1367  * If any anchors are defined, process these, optionally removing curves from white list
1368  * Invoke _flush_white to write result back to object.
1369  */
1370 static void
1371 spcc_concat_colors_and_flush(SPConnectorContext *cc)
1373     SPCurve *c = cc->green_curve;
1374     cc->green_curve = new SPCurve();
1376     cc->red_curve->reset();
1377     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
1379     if (c->is_empty()) {
1380         c->unref();
1381         return;
1382     }
1384     spcc_flush_white(cc, c);
1386     c->unref();
1390 /*
1391  * Flushes white curve(s) and additional curve into object
1392  *
1393  * No cleaning of colored curves - this has to be done by caller
1394  * No rereading of white data, so if you cannot rely on ::modified, do it in caller
1395  *
1396  */
1398 static void
1399 spcc_flush_white(SPConnectorContext *cc, SPCurve *gc)
1401     SPCurve *c;
1403     if (gc) {
1404         c = gc;
1405         c->ref();
1406     } else {
1407         return;
1408     }
1410     /* Now we have to go back to item coordinates at last */
1411     c->transform(SP_EVENT_CONTEXT_DESKTOP(cc)->dt2doc());
1413     SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
1414     SPDocument *doc = sp_desktop_document(desktop);
1415     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
1417     if ( c && !c->is_empty() ) {
1418         /* We actually have something to write */
1420         Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
1421         /* Set style */
1422         sp_desktop_apply_style_tool(desktop, repr, "/tools/connector", false);
1424         gchar *str = sp_svg_write_path( c->get_pathvector() );
1425         g_assert( str != NULL );
1426         repr->setAttribute("d", str);
1427         g_free(str);
1429         /* Attach repr */
1430         cc->newconn = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
1431         cc->newconn->transform = sp_item_i2doc_affine(SP_ITEM(desktop->currentLayer())).inverse();
1433         bool connection = false;
1434         sp_object_setAttribute(cc->newconn, "inkscape:connector-type",
1435                 cc->isOrthogonal ? "orthogonal" : "polyline", false);
1436         sp_object_setAttribute(cc->newconn, "inkscape:connector-curvature",
1437                 Glib::Ascii::dtostr(cc->curvature).c_str(), false);
1438         if (cc->shref)
1439         {
1440             sp_object_setAttribute(cc->newconn, "inkscape:connection-start",
1441                     cc->shref, false);
1442             if (cc->scpid)
1443                 sp_object_setAttribute(cc->newconn, "inkscape:connection-start-point",
1444                         cc->scpid, false);
1445             connection = true;
1446         }
1448         if (cc->ehref)
1449         {
1450             sp_object_setAttribute(cc->newconn, "inkscape:connection-end",
1451                     cc->ehref, false);
1452             if (cc->ecpid)
1453                 sp_object_setAttribute(cc->newconn, "inkscape:connection-end-point",
1454                         cc->ecpid, false);
1455             connection = true;
1456         }
1457         // Process pending updates.
1458         cc->newconn->updateRepr();
1459         sp_document_ensure_up_to_date(doc);
1461         if (connection) {
1462             // Adjust endpoints to shape edge.
1463             sp_conn_reroute_path_immediate(SP_PATH(cc->newconn));
1464             cc->newconn->updateRepr();
1465         }
1467         // Only set the selection after we are finished with creating the attributes of
1468         // the connector.  Otherwise, the selection change may alter the defaults for
1469         // values like curvature in the connector context, preventing subsequent lookup
1470         // of their original values.
1471         cc->selection->set(repr);
1472         Inkscape::GC::release(repr);
1473     }
1475     c->unref();
1477     sp_document_done(doc, SP_VERB_CONTEXT_CONNECTOR, _("Create connector"));
1481 static void
1482 spcc_connector_finish_segment(SPConnectorContext *const cc, Geom::Point const /*p*/)
1484     if (!cc->red_curve->is_empty()) {
1485         cc->green_curve->append_continuous(cc->red_curve, 0.0625);
1487         cc->p[0] = cc->p[3];
1488         cc->p[1] = cc->p[4];
1489         cc->npoints = 2;
1491         cc->red_curve->reset();
1492     }
1496 static void
1497 spcc_connector_finish(SPConnectorContext *const cc)
1499     SPDesktop *const desktop = cc->desktop;
1500     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Finishing connector"));
1502     cc->red_curve->reset();
1503     spcc_concat_colors_and_flush(cc);
1505     cc->npoints = 0;
1507     if (cc->newConnRef) {
1508         cc->newConnRef->removeFromGraph();
1509         delete cc->newConnRef;
1510         cc->newConnRef = NULL;
1511     }
1515 static gboolean
1516 cc_generic_knot_handler(SPCanvasItem *, GdkEvent *event, SPKnot *knot)
1518     g_assert (knot != NULL);
1520     g_object_ref(knot);
1522     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(
1523             knot->desktop->event_context);
1525     gboolean consumed = FALSE;
1527     gchar* knot_tip = knot->tip ? knot->tip : cc->knot_tip;
1528     switch (event->type) {
1529         case GDK_ENTER_NOTIFY:
1530             sp_knot_set_flag(knot, SP_KNOT_MOUSEOVER, TRUE);
1532             cc->active_handle = knot;
1533             if (knot_tip)
1534             {
1535                 knot->desktop->event_context->defaultMessageContext()->set(
1536                         Inkscape::NORMAL_MESSAGE, knot_tip);
1537             }
1539             consumed = TRUE;
1540             break;
1541         case GDK_LEAVE_NOTIFY:
1542             sp_knot_set_flag(knot, SP_KNOT_MOUSEOVER, FALSE);
1544             cc->active_handle = NULL;
1546             if (knot_tip) {
1547                 knot->desktop->event_context->defaultMessageContext()->clear();
1548             }
1550             consumed = TRUE;
1551             break;
1552         default:
1553             break;
1554     }
1556     g_object_unref(knot);
1558     return consumed;
1562 static gboolean
1563 endpt_handler(SPKnot */*knot*/, GdkEvent *event, SPConnectorContext *cc)
1565     g_assert( SP_IS_CONNECTOR_CONTEXT(cc) );
1567     gboolean consumed = FALSE;
1569     switch (event->type) {
1570         case GDK_BUTTON_PRESS:
1571             g_assert( (cc->active_handle == cc->endpt_handle[0]) ||
1572                       (cc->active_handle == cc->endpt_handle[1]) );
1573             if (cc->state == SP_CONNECTOR_CONTEXT_IDLE) {
1574                 cc->clickeditem = cc->active_conn;
1575                 cc->clickedhandle = cc->active_handle;
1576                 cc_clear_active_conn(cc);
1577                 cc->state = SP_CONNECTOR_CONTEXT_REROUTING;
1579                 // Disconnect from attached shape
1580                 unsigned ind = (cc->active_handle == cc->endpt_handle[0]) ? 0 : 1;
1581                 sp_conn_end_detach(cc->clickeditem, ind);
1583                 Geom::Point origin;
1584                 if (cc->clickedhandle == cc->endpt_handle[0]) {
1585                     origin = cc->endpt_handle[1]->pos;
1586                 }
1587                 else {
1588                     origin = cc->endpt_handle[0]->pos;
1589                 }
1591                 // Show the red path for dragging.
1592                 cc->red_curve = SP_PATH(cc->clickeditem)->original_curve ? SP_PATH(cc->clickeditem)->original_curve->copy() : SP_PATH(cc->clickeditem)->curve->copy();
1593                 Geom::Matrix i2d = sp_item_i2d_affine(cc->clickeditem);
1594                 cc->red_curve->transform(i2d);
1595                 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), cc->red_curve);
1597                 cc->clickeditem->setHidden(true);
1599                 // The rest of the interaction rerouting the connector is
1600                 // handled by the context root handler.
1601                 consumed = TRUE;
1602             }
1603             break;
1604         default:
1605             break;
1606     }
1608     return consumed;
1611 static void cc_active_shape_add_knot(SPDesktop* desktop, SPItem* item, ConnectionPointMap &cphandles, ConnectionPoint& cp)
1613         SPKnot *knot = sp_knot_new(desktop, 0);
1615         knot->setShape(SP_KNOT_SHAPE_SQUARE);
1616         knot->setSize(8);
1617         knot->setAnchor(GTK_ANCHOR_CENTER);
1618         knot->setFill(0xffffff00, 0xff0000ff, 0xff0000ff);
1619         sp_knot_update_ctrl(knot);
1621         // We don't want to use the standard knot handler.
1622         g_signal_handler_disconnect(G_OBJECT(knot->item),
1623                 knot->_event_handler_id);
1624         knot->_event_handler_id = 0;
1626         gtk_signal_connect(GTK_OBJECT(knot->item), "event",
1627                 GTK_SIGNAL_FUNC(cc_generic_knot_handler), knot);
1628         sp_knot_set_position(knot, item->avoidRef->getConnectionPointPos(cp.type, cp.id) * desktop->doc2dt(), 0);
1629         sp_knot_show(knot);
1630         cphandles[knot] = cp;
1633 static void cc_set_active_shape(SPConnectorContext *cc, SPItem *item)
1635     g_assert(item != NULL );
1637     std::map<int, ConnectionPoint>* connpts = &item->avoidRef->connection_points;
1639     if (cc->active_shape != item)
1640     {
1641         // The active shape has changed
1642         // Rebuild everything
1643         cc->active_shape = item;
1644         // Remove existing active shape listeners
1645         if (cc->active_shape_repr) {
1646             sp_repr_remove_listener_by_data(cc->active_shape_repr, cc);
1647             Inkscape::GC::release(cc->active_shape_repr);
1649             sp_repr_remove_listener_by_data(cc->active_shape_layer_repr, cc);
1650             Inkscape::GC::release(cc->active_shape_layer_repr);
1651         }
1653         // Listen in case the active shape changes
1654         cc->active_shape_repr = SP_OBJECT_REPR(item);
1655         if (cc->active_shape_repr) {
1656             Inkscape::GC::anchor(cc->active_shape_repr);
1657             sp_repr_add_listener(cc->active_shape_repr, &shape_repr_events, cc);
1659             cc->active_shape_layer_repr = cc->active_shape_repr->parent();
1660             Inkscape::GC::anchor(cc->active_shape_layer_repr);
1661             sp_repr_add_listener(cc->active_shape_layer_repr, &layer_repr_events, cc);
1662         }
1665         // Set the connection points.
1666         if ( cc->connpthandles.size() )
1667             // destroy the old list
1668             while (! cc->connpthandles.empty() )
1669             {
1670                 g_object_unref(cc->connpthandles.begin()->first);
1671                 cc->connpthandles.erase(cc->connpthandles.begin());
1672             }
1673         // build the new one
1674         if ( connpts->size() )
1675         for (std::map<int, ConnectionPoint>::iterator it = connpts->begin(); it != connpts->end(); ++it)
1676             cc_active_shape_add_knot(cc->desktop, item, cc->connpthandles, it->second);
1678         // Also add default connection points
1679         // For now, only centre default connection point will
1680         // be available
1681         ConnectionPoint centre;
1682         centre.type = ConnPointDefault;
1683         centre.id = ConnPointPosCC;
1684         cc_active_shape_add_knot(cc->desktop, item, cc->connpthandles, centre);
1685     }
1686     else
1687     {
1688         // The active shape didn't change
1689         // Update only the connection point knots
1691         // Ensure the item's connection_points map
1692         // has been updated
1693         sp_document_ensure_up_to_date(SP_OBJECT_DOCUMENT(item));
1695         std::set<int> seen;
1696         for  ( ConnectionPointMap::iterator it = cc->connpthandles.begin(); it != cc->connpthandles.end() ;)
1697         {
1698             bool removed = false;
1699             if ( it->second.type == ConnPointUserDefined )
1700             {
1701                 std::map<int, ConnectionPoint>::iterator p = connpts->find(it->second.id);
1702                 if (p != connpts->end())
1703                 {
1704                     if ( it->second != p->second )
1705                         // Connection point position has changed
1706                         // Update knot position
1707                         sp_knot_set_position(it->first,
1708                                              item->avoidRef->getConnectionPointPos(it->second.type, it->second.id) * cc->desktop->doc2dt(), 0);
1709                     seen.insert(it->second.id);
1710                     sp_knot_show(it->first);
1711                 }
1712                 else
1713                 {
1714                     // This connection point does no longer exist,
1715                     // remove the knot
1716                     ConnectionPointMap::iterator curr = it;
1717                     ++it;
1718                     g_object_unref( curr->first );
1719                     cc->connpthandles.erase(curr);
1720                     removed = true;
1721                 }
1722             }
1723             else
1724             {
1725                 // It's a default connection point
1726                 // Just make sure it's position is correct
1727                 sp_knot_set_position(it->first,
1728                                      item->avoidRef->getConnectionPointPos(it->second.type, it->second.id) * cc->desktop->doc2dt(), 0);
1729                 sp_knot_show(it->first);
1731             }
1732             if ( !removed )
1733                 ++it;
1734         }
1735         // Add knots for new connection points.
1736         if (connpts->size())
1737             for ( std::map<int, ConnectionPoint>::iterator it = connpts->begin(); it != connpts->end(); ++it )
1738                 if ( seen.find(it->first) == seen.end() )
1739                     // A new connection point has been added
1740                     // to the shape. Add a knot for it.
1741                     cc_active_shape_add_knot(cc->desktop, item, cc->connpthandles, it->second);
1742     }
1746 static void
1747 cc_set_active_conn(SPConnectorContext *cc, SPItem *item)
1749     g_assert( SP_IS_PATH(item) );
1751     SPCurve *curve = SP_PATH(item)->original_curve ? SP_PATH(item)->original_curve : SP_PATH(item)->curve;
1752     Geom::Matrix i2d = sp_item_i2d_affine(item);
1754     if (cc->active_conn == item)
1755     {
1756         // Just adjust handle positions.
1757         Geom::Point startpt = *(curve->first_point()) * i2d;
1758         sp_knot_set_position(cc->endpt_handle[0], startpt, 0);
1760         Geom::Point endpt = *(curve->last_point()) * i2d;
1761         sp_knot_set_position(cc->endpt_handle[1], endpt, 0);
1763         return;
1764     }
1766     cc->active_conn = item;
1768     // Remove existing active conn listeners
1769     if (cc->active_conn_repr) {
1770         sp_repr_remove_listener_by_data(cc->active_conn_repr, cc);
1771         Inkscape::GC::release(cc->active_conn_repr);
1772         cc->active_conn_repr = NULL;
1773     }
1775     // Listen in case the active conn changes
1776     cc->active_conn_repr = SP_OBJECT_REPR(item);
1777     if (cc->active_conn_repr) {
1778         Inkscape::GC::anchor(cc->active_conn_repr);
1779         sp_repr_add_listener(cc->active_conn_repr, &shape_repr_events, cc);
1780     }
1782     for (int i = 0; i < 2; ++i) {
1784         // Create the handle if it doesn't exist
1785         if ( cc->endpt_handle[i] == NULL ) {
1786             SPKnot *knot = sp_knot_new(cc->desktop,
1787                     _("<b>Connector endpoint</b>: drag to reroute or connect to new shapes"));
1789             knot->setShape(SP_KNOT_SHAPE_SQUARE);
1790             knot->setSize(7);
1791             knot->setAnchor(GTK_ANCHOR_CENTER);
1792             knot->setFill(0xffffff00, 0xff0000ff, 0xff0000ff);
1793             knot->setStroke(0x000000ff, 0x000000ff, 0x000000ff);
1794             sp_knot_update_ctrl(knot);
1796             // We don't want to use the standard knot handler,
1797             // since we don't want this knot to be draggable.
1798             g_signal_handler_disconnect(G_OBJECT(knot->item),
1799                     knot->_event_handler_id);
1800             knot->_event_handler_id = 0;
1802             gtk_signal_connect(GTK_OBJECT(knot->item), "event",
1803                     GTK_SIGNAL_FUNC(cc_generic_knot_handler), knot);
1805             cc->endpt_handle[i] = knot;
1806         }
1808         // Remove any existing handlers
1809         if (cc->endpt_handler_id[i]) {
1810             g_signal_handlers_disconnect_by_func(
1811                     G_OBJECT(cc->endpt_handle[i]->item),
1812                     (void*)G_CALLBACK(endpt_handler), (gpointer) cc );
1813             cc->endpt_handler_id[i] = 0;
1814         }
1816         // Setup handlers for connector endpoints, this is
1817         // is as 'after' so that cc_generic_knot_handler is
1818         // triggered first for any endpoint.
1819         cc->endpt_handler_id[i] = g_signal_connect_after(
1820                 G_OBJECT(cc->endpt_handle[i]->item), "event",
1821                 G_CALLBACK(endpt_handler), cc);
1822     }
1824     Geom::Point startpt = *(curve->first_point()) * i2d;
1825     sp_knot_set_position(cc->endpt_handle[0], startpt, 0);
1827     Geom::Point endpt = *(curve->last_point()) * i2d;
1828     sp_knot_set_position(cc->endpt_handle[1], endpt, 0);
1830     sp_knot_show(cc->endpt_handle[0]);
1831     sp_knot_show(cc->endpt_handle[1]);
1834 void cc_create_connection_point(SPConnectorContext* cc)
1836     if (cc->active_shape && cc->state == SP_CONNECTOR_CONTEXT_IDLE)
1837     {
1838         if (cc->selected_handle)
1839         {
1840             cc_deselect_handle( cc->selected_handle );
1841         }
1842         SPKnot *knot = sp_knot_new(cc->desktop, 0);
1843         // We do not process events on this knot.
1844         g_signal_handler_disconnect(G_OBJECT(knot->item),
1845                                     knot->_event_handler_id);
1846         knot->_event_handler_id = 0;
1848         cc_select_handle( knot );
1849         cc->selected_handle = knot;
1850         sp_knot_show(cc->selected_handle);
1851         cc->state = SP_CONNECTOR_CONTEXT_NEWCONNPOINT;
1852     }
1855 void cc_remove_connection_point(SPConnectorContext* cc)
1857     if (cc->selected_handle && cc->state == SP_CONNECTOR_CONTEXT_IDLE )
1858     {
1859         cc->active_shape->avoidRef->deleteConnectionPoint(cc->connpthandles[cc->selected_handle]);
1860         cc->selected_handle = NULL;
1861     }
1864 static bool cc_item_is_shape(SPItem *item)
1866     if (SP_IS_PATH(item)) {
1867         SPCurve *curve = (SP_SHAPE(item))->curve;
1868         if ( curve && !(curve->is_closed()) ) {
1869             // Open paths are connectors.
1870             return false;
1871         }
1872     }
1873     else if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
1874         Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1875         if (prefs->getBool("/tools/connector/ignoretext", true)) {
1876             // Don't count text as a shape we can connect connector to.
1877             return false;
1878         }
1879     }
1880     return true;
1884 bool cc_item_is_connector(SPItem *item)
1886     if (SP_IS_PATH(item)) {
1887         if (SP_PATH(item)->connEndPair.isAutoRoutingConn()) {
1888             g_assert( SP_PATH(item)->original_curve ? !(SP_PATH(item)->original_curve->is_closed()) : !(SP_PATH(item)->curve->is_closed()) );
1889             return true;
1890         }
1891     }
1892     return false;
1896 void cc_selection_set_avoid(bool const set_avoid)
1898     SPDesktop *desktop = inkscape_active_desktop();
1899     if (desktop == NULL) {
1900         return;
1901     }
1903     SPDocument *document = sp_desktop_document(desktop);
1905     Inkscape::Selection *selection = sp_desktop_selection(desktop);
1907     GSList *l = (GSList *) selection->itemList();
1909     int changes = 0;
1911     while (l) {
1912         SPItem *item = (SPItem *) l->data;
1914         char const *value = (set_avoid) ? "true" : NULL;
1916         if (cc_item_is_shape(item)) {
1917             sp_object_setAttribute(item, "inkscape:connector-avoid",
1918                     value, false);
1919             item->avoidRef->handleSettingChange();
1920             changes++;
1921         }
1923         l = l->next;
1924     }
1926     if (changes == 0) {
1927         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE,
1928                 _("Select <b>at least one non-connector object</b>."));
1929         return;
1930     }
1932     char *event_desc = (set_avoid) ?
1933             _("Make connectors avoid selected objects") :
1934             _("Make connectors ignore selected objects");
1935     sp_document_done(document, SP_VERB_CONTEXT_CONNECTOR, event_desc);
1939 static void
1940 cc_selection_changed(Inkscape::Selection *selection, gpointer data)
1942     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(data);
1943     //SPEventContext *ec = SP_EVENT_CONTEXT(cc);
1945     SPItem *item = selection->singleItem();
1947     if (cc->active_conn == item)
1948     {
1949         // Nothing to change.
1950         return;
1951     }
1952     if (item == NULL)
1953     {
1954         cc_clear_active_conn(cc);
1955         return;
1956     }
1958     if (cc_item_is_connector(item)) {
1959         cc_set_active_conn(cc, item);
1960     }
1964 static void
1965 shape_event_attr_deleted(Inkscape::XML::Node */*repr*/, Inkscape::XML::Node *child,
1966                          Inkscape::XML::Node */*ref*/, gpointer data)
1968     g_assert(data);
1969     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(data);
1971     if (child == cc->active_shape_repr) {
1972         // The active shape has been deleted.  Clear active shape.
1973         cc_clear_active_shape(cc);
1974     }
1978 static void
1979 shape_event_attr_changed(Inkscape::XML::Node *repr, gchar const *name,
1980                          gchar const */*old_value*/, gchar const */*new_value*/,
1981                          bool /*is_interactive*/, gpointer data)
1983     g_assert(data);
1984     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(data);
1986     // Look for changes that result in onscreen movement.
1987     if (!strcmp(name, "d") || !strcmp(name, "x") || !strcmp(name, "y") ||
1988             !strcmp(name, "width") || !strcmp(name, "height") ||
1989             !strcmp(name, "transform"))
1990     {
1991         if (repr == cc->active_shape_repr) {
1992             // Active shape has moved. Clear active shape.
1993             cc_clear_active_shape(cc);
1994         }
1995         else if (repr == cc->active_conn_repr) {
1996             // The active conn has been moved.
1997             // Set it again, which just sets new handle positions.
1998             cc_set_active_conn(cc, cc->active_conn);
1999         }
2000     }
2001     else
2002         if ( !strcmp(name, "inkscape:connection-points") )
2003             if (repr == cc->active_shape_repr)
2004                 // The connection points of the active shape
2005                 // have changed. Update them.
2006                 cc_set_active_shape(cc, cc->active_shape);
2010 /*
2011   Local Variables:
2012   mode:c++
2013   c-file-style:"stroustrup"
2014   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
2015   indent-tabs-mode:nil
2016   fill-column:99
2017   End:
2018 */
2019 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :