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 char* cc_knot_tips[] = { _("<b>Connection point</b>: click or drag to create a new connector"),
237 _("<b>Connection point</b>: click to select, drag to move") };
239 /*static Geom::Point connector_drag_origin_w(0, 0);
240 static bool connector_within_tolerance = false;*/
241 static SPEventContextClass *parent_class;
244 static Inkscape::XML::NodeEventVector shape_repr_events = {
245 NULL, /* child_added */
246 NULL, /* child_added */
247 shape_event_attr_changed,
248 NULL, /* content_changed */
249 NULL /* order_changed */
250 };
252 static Inkscape::XML::NodeEventVector layer_repr_events = {
253 NULL, /* child_added */
254 shape_event_attr_deleted,
255 NULL, /* child_added */
256 NULL, /* content_changed */
257 NULL /* order_changed */
258 };
261 GType
262 sp_connector_context_get_type(void)
263 {
264 static GType type = 0;
265 if (!type) {
266 GTypeInfo info = {
267 sizeof(SPConnectorContextClass),
268 NULL, NULL,
269 (GClassInitFunc) sp_connector_context_class_init,
270 NULL, NULL,
271 sizeof(SPConnectorContext),
272 4,
273 (GInstanceInitFunc) sp_connector_context_init,
274 NULL, /* value_table */
275 };
276 type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPConnectorContext", &info, (GTypeFlags)0);
277 }
278 return type;
279 }
281 static void
282 sp_connector_context_class_init(SPConnectorContextClass *klass)
283 {
284 GObjectClass *object_class;
285 SPEventContextClass *event_context_class;
287 object_class = (GObjectClass *) klass;
288 event_context_class = (SPEventContextClass *) klass;
290 parent_class = (SPEventContextClass*)g_type_class_peek_parent(klass);
292 object_class->dispose = sp_connector_context_dispose;
294 event_context_class->setup = sp_connector_context_setup;
295 event_context_class->set = sp_connector_context_set;
296 event_context_class->finish = sp_connector_context_finish;
297 event_context_class->root_handler = sp_connector_context_root_handler;
298 event_context_class->item_handler = sp_connector_context_item_handler;
299 }
302 static void
303 sp_connector_context_init(SPConnectorContext *cc)
304 {
305 SPEventContext *ec = SP_EVENT_CONTEXT(cc);
307 ec->cursor_shape = cursor_connector_xpm;
308 ec->hot_x = 1;
309 ec->hot_y = 1;
310 ec->xp = 0;
311 ec->yp = 0;
313 cc->mode = SP_CONNECTOR_CONTEXT_DRAWING_MODE;
314 cc->knot_tip = 0;
316 cc->red_color = 0xff00007f;
318 cc->newconn = NULL;
319 cc->newConnRef = NULL;
320 cc->curvature = 0.0;
322 cc->sel_changed_connection = sigc::connection();
324 cc->active_shape = NULL;
325 cc->active_shape_repr = NULL;
326 cc->active_shape_layer_repr = NULL;
328 cc->active_conn = NULL;
329 cc->active_conn_repr = NULL;
331 cc->active_handle = NULL;
333 cc->selected_handle = NULL;
335 cc->clickeditem = NULL;
336 cc->clickedhandle = NULL;
338 new (&cc->connpthandles) ConnectionPointMap();
340 for (int i = 0; i < 2; ++i) {
341 cc->endpt_handle[i] = NULL;
342 cc->endpt_handler_id[i] = 0;
343 }
344 cc->shref = NULL;
345 cc->scpid = NULL;
346 cc->ehref = NULL;
347 cc->ecpid = NULL;
348 cc->npoints = 0;
349 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
350 }
353 static void
354 sp_connector_context_dispose(GObject *object)
355 {
356 SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(object);
358 cc->sel_changed_connection.disconnect();
360 if (!cc->connpthandles.empty()) {
361 for (ConnectionPointMap::iterator it = cc->connpthandles.begin();
362 it != cc->connpthandles.end(); ++it) {
363 g_object_unref(it->first);
364 }
365 cc->connpthandles.clear();
366 }
367 cc->connpthandles.~ConnectionPointMap();
368 for (int i = 0; i < 2; ++i) {
369 if (cc->endpt_handle[1]) {
370 g_object_unref(cc->endpt_handle[i]);
371 cc->endpt_handle[i] = NULL;
372 }
373 }
374 if (cc->shref) {
375 g_free(cc->shref);
376 cc->shref = NULL;
377 }
378 if (cc->scpid) {
379 g_free(cc->scpid);
380 cc->scpid = NULL;
381 }
382 if (cc->ehref) {
383 g_free(cc->shref);
384 cc->shref = NULL;
385 }
386 if (cc->ecpid) {
387 g_free(cc->scpid);
388 cc->scpid = NULL;
389 }
390 g_assert( cc->newConnRef == NULL );
392 G_OBJECT_CLASS(parent_class)->dispose(object);
393 }
396 static void
397 sp_connector_context_setup(SPEventContext *ec)
398 {
399 SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(ec);
400 SPDesktop *dt = ec->desktop;
402 if (((SPEventContextClass *) parent_class)->setup) {
403 ((SPEventContextClass *) parent_class)->setup(ec);
404 }
406 cc->selection = sp_desktop_selection(dt);
408 cc->sel_changed_connection.disconnect();
409 cc->sel_changed_connection = cc->selection->connectChanged(
410 sigc::bind(sigc::ptr_fun(&cc_selection_changed),
411 (gpointer) cc));
413 /* Create red bpath */
414 cc->red_bpath = sp_canvas_bpath_new(sp_desktop_sketch(ec->desktop), NULL);
415 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cc->red_bpath), cc->red_color,
416 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
417 sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(cc->red_bpath), 0x00000000,
418 SP_WIND_RULE_NONZERO);
419 /* Create red curve */
420 cc->red_curve = new SPCurve();
422 /* Create green curve */
423 cc->green_curve = new SPCurve();
425 // Notice the initial selection.
426 cc_selection_changed(cc->selection, (gpointer) cc);
428 cc->within_tolerance = false;
430 sp_event_context_read(ec, "curvature");
431 sp_event_context_read(ec, "orthogonal");
432 sp_event_context_read(ec, "mode");
433 cc->knot_tip = cc->mode == SP_CONNECTOR_CONTEXT_DRAWING_MODE ? cc_knot_tips[0] : cc_knot_tips[1];
434 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
435 if (prefs->getBool("/tools/connector/selcue", 0)) {
436 ec->enableSelectionCue();
437 }
439 // Make sure we see all enter events for canvas items,
440 // even if a mouse button is depressed.
441 dt->canvas->gen_all_enter_events = true;
442 }
445 static void
446 sp_connector_context_set(SPEventContext *ec, Inkscape::Preferences::Entry *val)
447 {
448 SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(ec);
450 /* fixme: Proper error handling for non-numeric data. Use a locale-independent function like
451 * g_ascii_strtod (or a thin wrapper that does the right thing for invalid values inf/nan). */
452 Glib::ustring name = val->getEntryName();
453 if ( name == "curvature" ) {
454 cc->curvature = val->getDoubleLimited(); // prevents NaN and +/-Inf from messing up
455 }
456 else if ( name == "orthogonal" ) {
457 cc->isOrthogonal = val->getBool();
458 }
459 else if ( name == "mode")
460 {
461 sp_connector_context_switch_mode(ec, val->getBool() ? SP_CONNECTOR_CONTEXT_EDITING_MODE : SP_CONNECTOR_CONTEXT_DRAWING_MODE);
462 }
463 }
465 void sp_connector_context_switch_mode(SPEventContext* ec, unsigned int newMode)
466 {
467 SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(ec);
469 cc->mode = newMode;
470 if ( cc->mode == SP_CONNECTOR_CONTEXT_DRAWING_MODE )
471 {
472 ec->cursor_shape = cursor_connector_xpm;
473 cc->knot_tip = cc_knot_tips[0];
474 if (cc->selected_handle)
475 cc_deselect_handle( cc->selected_handle );
476 cc->selected_handle = NULL;
477 // Show all default connection points
479 }
480 else if ( cc->mode == SP_CONNECTOR_CONTEXT_EDITING_MODE )
481 {
482 ec->cursor_shape = cursor_node_xpm;
483 cc->knot_tip = cc_knot_tips[1];
484 /* if (cc->active_shape)
485 {
486 cc->selection->set( SP_OBJECT( cc->active_shape ) );
487 }
488 else
489 {
490 SPItem* item = cc->selection->singleItem();
491 if ( item )
492 {
493 cc_set_active_shape(cc, item);
494 cc->selection->set( SP_OBJECT( item ) );
495 }
496 }*/
497 }
498 sp_event_context_update_cursor(ec);
500 }
503 static void
504 sp_connector_context_finish(SPEventContext *ec)
505 {
506 SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(ec);
508 spcc_connector_finish(cc);
509 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
511 if (((SPEventContextClass *) parent_class)->finish) {
512 ((SPEventContextClass *) parent_class)->finish(ec);
513 }
515 if (cc->selection) {
516 cc->selection = NULL;
517 }
518 cc_clear_active_shape(cc);
519 cc_clear_active_conn(cc);
521 // Restore the default event generating behaviour.
522 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(ec);
523 desktop->canvas->gen_all_enter_events = false;
524 }
527 //-----------------------------------------------------------------------------
530 static void
531 cc_clear_active_shape(SPConnectorContext *cc)
532 {
533 if (cc->active_shape == NULL) {
534 return;
535 }
536 g_assert( cc->active_shape_repr );
537 g_assert( cc->active_shape_layer_repr );
539 cc->active_shape = NULL;
541 if (cc->active_shape_repr) {
542 sp_repr_remove_listener_by_data(cc->active_shape_repr, cc);
543 Inkscape::GC::release(cc->active_shape_repr);
544 cc->active_shape_repr = NULL;
546 sp_repr_remove_listener_by_data(cc->active_shape_layer_repr, cc);
547 Inkscape::GC::release(cc->active_shape_layer_repr);
548 cc->active_shape_layer_repr = NULL;
549 }
551 // Hide the connection points if they exist.
552 if (cc->connpthandles.size()) {
553 for (ConnectionPointMap::iterator it = cc->connpthandles.begin();
554 it != cc->connpthandles.end(); ++it) {
555 sp_knot_hide(it->first);
556 }
557 }
558 }
561 static void
562 cc_clear_active_conn(SPConnectorContext *cc)
563 {
564 if (cc->active_conn == NULL) {
565 return;
566 }
567 g_assert( cc->active_conn_repr );
569 cc->active_conn = NULL;
571 if (cc->active_conn_repr) {
572 sp_repr_remove_listener_by_data(cc->active_conn_repr, cc);
573 Inkscape::GC::release(cc->active_conn_repr);
574 cc->active_conn_repr = NULL;
575 }
577 // Hide the endpoint handles.
578 for (int i = 0; i < 2; ++i) {
579 if (cc->endpt_handle[i]) {
580 sp_knot_hide(cc->endpt_handle[i]);
581 }
582 }
583 }
586 static bool
587 conn_pt_handle_test(SPConnectorContext *cc, Geom::Point& p, gchar **href, gchar **cpid)
588 {
589 // TODO: this will need to change when there are more connection
590 // points available for each shape.
592 if (cc->active_handle && (cc->connpthandles.find(cc->active_handle) != cc->connpthandles.end()))
593 {
594 p = cc->active_handle->pos;
595 const ConnectionPoint& cp = cc->connpthandles[cc->active_handle];
596 *href = g_strdup_printf("#%s", cc->active_shape->getId());
597 *cpid = g_strdup_printf("%c%d", cp.type == ConnPointDefault ? 'd' : 'u' , cp.id);
598 return true;
599 }
600 *href = NULL;
601 *cpid = NULL;
602 return false;
603 }
605 static void
606 cc_select_handle(SPKnot* knot)
607 {
608 knot->setShape(SP_KNOT_SHAPE_SQUARE);
609 knot->setSize(10);
610 knot->setAnchor(GTK_ANCHOR_CENTER);
611 knot->setFill(0x0000ffff, 0x0000ffff, 0x0000ffff);
612 sp_knot_update_ctrl(knot);
613 }
615 static void
616 cc_deselect_handle(SPKnot* knot)
617 {
618 knot->setShape(SP_KNOT_SHAPE_SQUARE);
619 knot->setSize(8);
620 knot->setAnchor(GTK_ANCHOR_CENTER);
621 knot->setFill(0xffffff00, 0xff0000ff, 0xff0000ff);
622 sp_knot_update_ctrl(knot);
623 }
625 static gint
626 sp_connector_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
627 {
628 gint ret = FALSE;
630 SPDesktop *desktop = event_context->desktop;
632 SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(event_context);
634 Geom::Point p(event->button.x, event->button.y);
636 switch (event->type) {
637 case GDK_BUTTON_RELEASE:
638 if (event->button.button == 1 && !event_context->space_panning) {
639 if ((cc->state == SP_CONNECTOR_CONTEXT_DRAGGING) &&
640 (event_context->within_tolerance))
641 {
642 spcc_reset_colors(cc);
643 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
644 }
645 if (cc->state != SP_CONNECTOR_CONTEXT_IDLE) {
646 // Doing something else like rerouting.
647 break;
648 }
649 // find out clicked item, honoring Alt
650 SPItem *item = sp_event_context_find_item(desktop,
651 p, event->button.state & GDK_MOD1_MASK, FALSE);
653 if (event->button.state & GDK_SHIFT_MASK) {
654 cc->selection->toggle(item);
655 } else {
656 cc->selection->set(item);
657 if ( cc->mode == SP_CONNECTOR_CONTEXT_EDITING_MODE && cc->selected_handle )
658 {
659 cc_deselect_handle( cc->selected_handle );
660 cc->selected_handle = NULL;
661 }
662 /* When selecting a new item,
663 do not allow showing connection points
664 on connectors. (yet?)
665 */
666 if ( item != cc->active_shape && !cc_item_is_connector( item ) )
667 cc_set_active_shape( cc, item );
668 }
669 ret = TRUE;
671 }
672 break;
673 case GDK_ENTER_NOTIFY:
674 {
675 if (cc->mode == SP_CONNECTOR_CONTEXT_DRAWING_MODE || (cc->mode == SP_CONNECTOR_CONTEXT_EDITING_MODE && !cc->selected_handle))
676 {
677 if (cc_item_is_shape(item)) {
679 // I don't really understand what the above does,
680 // so I commented it.
681 // This is a shape, so show connection point(s).
682 /* if (!(cc->active_shape)
683 // Don't show handle for another handle.
684 // || (cc->connpthandles.find((SPKnot*) item) != cc->connpthandles.end())
685 )
686 {
687 cc_set_active_shape(cc, item);
688 }*/
689 cc_set_active_shape(cc, item);
690 }
691 ret = TRUE;
692 }
693 break;
694 }
695 default:
696 break;
697 }
699 return ret;
700 }
703 gint
704 sp_connector_context_root_handler(SPEventContext *ec, GdkEvent *event)
705 {
706 SPConnectorContext *const cc = SP_CONNECTOR_CONTEXT(ec);
708 gint ret = FALSE;
710 switch (event->type) {
711 case GDK_BUTTON_PRESS:
712 ret = connector_handle_button_press(cc, event->button);
713 break;
715 case GDK_MOTION_NOTIFY:
716 ret = connector_handle_motion_notify(cc, event->motion);
717 break;
719 case GDK_BUTTON_RELEASE:
720 ret = connector_handle_button_release(cc, event->button);
721 break;
722 case GDK_KEY_PRESS:
723 ret = connector_handle_key_press(cc, get_group0_keyval (&event->key));
724 break;
726 default:
727 break;
728 }
730 if (!ret) {
731 gint (*const parent_root_handler)(SPEventContext *, GdkEvent *)
732 = ((SPEventContextClass *) parent_class)->root_handler;
733 if (parent_root_handler) {
734 ret = parent_root_handler(ec, event);
735 }
736 }
738 return ret;
739 }
742 static gint
743 connector_handle_button_press(SPConnectorContext *const cc, GdkEventButton const &bevent)
744 {
745 Geom::Point const event_w(bevent.x, bevent.y);
746 /* Find desktop coordinates */
747 Geom::Point p = cc->desktop->w2d(event_w);
748 SPEventContext *event_context = SP_EVENT_CONTEXT(cc);
750 gint ret = FALSE;
751 if ( cc->mode == SP_CONNECTOR_CONTEXT_DRAWING_MODE )
752 {
753 if ( bevent.button == 1 && !event_context->space_panning ) {
755 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
757 if (Inkscape::have_viable_layer(desktop, cc->_message_context) == false) {
758 return TRUE;
759 }
761 Geom::Point const event_w(bevent.x,
762 bevent.y);
763 // connector_drag_origin_w = event_w;
764 cc->xp = bevent.x;
765 cc->yp = bevent.y;
766 cc->within_tolerance = true;
768 Geom::Point const event_dt = cc->desktop->w2d(event_w);
770 SnapManager &m = cc->desktop->namedview->snap_manager;
772 switch (cc->state) {
773 case SP_CONNECTOR_CONTEXT_STOP:
774 /* This is allowed, if we just canceled curve */
775 case SP_CONNECTOR_CONTEXT_IDLE:
776 {
777 if ( cc->npoints == 0 ) {
778 cc_clear_active_conn(cc);
780 SP_EVENT_CONTEXT_DESKTOP(cc)->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating new connector"));
782 /* Set start anchor */
783 /* Create green anchor */
784 Geom::Point p = event_dt;
786 // Test whether we clicked on a connection point
787 bool found = conn_pt_handle_test(cc, p, &cc->shref, &cc->scpid);
789 if (!found) {
790 // This is the first point, so just snap it to the grid
791 // as there's no other points to go off.
792 m.setup(cc->desktop);
793 m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE);
794 m.unSetup();
795 }
796 spcc_connector_set_initial_point(cc, p);
798 }
799 cc->state = SP_CONNECTOR_CONTEXT_DRAGGING;
800 ret = TRUE;
801 break;
802 }
803 case SP_CONNECTOR_CONTEXT_DRAGGING:
804 {
805 // This is the second click of a connector creation.
806 m.setup(cc->desktop);
807 m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE);
808 m.unSetup();
810 spcc_connector_set_subsequent_point(cc, p);
811 spcc_connector_finish_segment(cc, p);
812 // Test whether we clicked on a connection point
813 /*bool found = */conn_pt_handle_test(cc, p, &cc->ehref, &cc->ecpid);
814 if (cc->npoints != 0) {
815 spcc_connector_finish(cc);
816 }
817 cc_set_active_conn(cc, cc->newconn);
818 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
819 ret = TRUE;
820 break;
821 }
822 case SP_CONNECTOR_CONTEXT_CLOSE:
823 {
824 g_warning("Button down in CLOSE state");
825 break;
826 }
827 default:
828 break;
829 }
830 } else if (bevent.button == 3) {
831 if (cc->state == SP_CONNECTOR_CONTEXT_REROUTING) {
832 // A context menu is going to be triggered here,
833 // so end the rerouting operation.
834 cc_connector_rerouting_finish(cc, &p);
836 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
838 // Don't set ret to TRUE, so we drop through to the
839 // parent handler which will open the context menu.
840 }
841 else if (cc->npoints != 0) {
842 spcc_connector_finish(cc);
843 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
844 ret = TRUE;
845 }
846 }
847 }
848 else if ( cc->mode == SP_CONNECTOR_CONTEXT_EDITING_MODE )
849 {
850 if ( bevent.button == 1 && !event_context->space_panning )
851 {
852 // Initialize variables in case of dragging
854 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
856 if (Inkscape::have_viable_layer(desktop, cc->_message_context) == false) {
857 return TRUE;
858 }
860 cc->xp = bevent.x;
861 cc->yp = bevent.y;
862 cc->within_tolerance = true;
864 ConnectionPointMap::iterator const& active_knot_it = cc->connpthandles.find( cc->active_handle );
866 switch (cc->state)
867 {
868 case SP_CONNECTOR_CONTEXT_IDLE:
869 if ( active_knot_it != cc->connpthandles.end() )
870 {
871 // We do not allow selecting and, thereby, moving default knots
872 if ( active_knot_it->second.type != ConnPointDefault)
873 {
874 if (cc->selected_handle != cc->active_handle)
875 {
876 if ( cc->selected_handle )
877 cc_deselect_handle( cc->selected_handle );
878 cc->selected_handle = cc->active_handle;
879 cc_select_handle( cc->selected_handle );
880 }
881 }
882 else
883 // Just ignore the default connection point
884 return FALSE;
885 }
886 else
887 if ( cc->selected_handle )
888 {
889 cc_deselect_handle( cc->selected_handle );
890 cc->selected_handle = NULL;
891 }
893 if ( cc->selected_handle )
894 {
895 cc->state = SP_CONNECTOR_CONTEXT_DRAGGING;
896 cc->selection->set( SP_OBJECT( cc->active_shape ) );
897 }
899 ret = TRUE;
900 break;
901 // Dragging valid because of the way we create
902 // new connection points.
903 case SP_CONNECTOR_CONTEXT_DRAGGING:
904 // Do nothing.
905 ret = TRUE;
906 break;
907 }
908 }
909 }
910 return ret;
911 }
914 static gint
915 connector_handle_motion_notify(SPConnectorContext *const cc, GdkEventMotion const &mevent)
916 {
917 gint ret = FALSE;
918 SPEventContext *event_context = SP_EVENT_CONTEXT(cc);
919 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
921 if (event_context->space_panning || mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) {
922 // allow middle-button scrolling
923 return FALSE;
924 }
926 Geom::Point const event_w(mevent.x, mevent.y);
928 if (cc->within_tolerance) {
929 cc->tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
930 if ( ( abs( (gint) mevent.x - cc->xp ) < cc->tolerance ) &&
931 ( abs( (gint) mevent.y - cc->yp ) < cc->tolerance ) ) {
932 return FALSE; // Do not drag if we're within tolerance from origin.
933 }
934 }
935 // Once the user has moved farther than tolerance from the original location
936 // (indicating they intend to move the object, not click), then always process
937 // the motion notify coordinates as given (no snapping back to origin)
938 cc->within_tolerance = false;
940 SPDesktop *const dt = cc->desktop;
942 /* Find desktop coordinates */
943 Geom::Point p = dt->w2d(event_w);
945 if ( cc->mode == SP_CONNECTOR_CONTEXT_DRAWING_MODE )
946 {
947 SnapManager &m = dt->namedview->snap_manager;
949 switch (cc->state) {
950 case SP_CONNECTOR_CONTEXT_DRAGGING:
951 {
952 gobble_motion_events(mevent.state);
953 // This is movement during a connector creation.
954 if ( cc->npoints > 0 ) {
955 m.setup(dt);
956 m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE);
957 m.unSetup();
958 cc->selection->clear();
959 spcc_connector_set_subsequent_point(cc, p);
960 ret = TRUE;
961 }
962 break;
963 }
964 case SP_CONNECTOR_CONTEXT_REROUTING:
965 {
966 gobble_motion_events(GDK_BUTTON1_MASK);
967 g_assert( SP_IS_PATH(cc->clickeditem));
969 m.setup(dt);
970 m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE);
971 m.unSetup();
973 // Update the hidden path
974 Geom::Matrix i2d = sp_item_i2d_affine(cc->clickeditem);
975 Geom::Matrix d2i = i2d.inverse();
976 SPPath *path = SP_PATH(cc->clickeditem);
977 SPCurve *curve = path->original_curve ? path->original_curve : path->curve;
978 if (cc->clickedhandle == cc->endpt_handle[0]) {
979 Geom::Point o = cc->endpt_handle[1]->pos;
980 curve->stretch_endpoints(p * d2i, o * d2i);
981 }
982 else {
983 Geom::Point o = cc->endpt_handle[0]->pos;
984 curve->stretch_endpoints(o * d2i, p * d2i);
985 }
986 sp_conn_reroute_path_immediate(path);
988 // Copy this to the temporary visible path
989 cc->red_curve = path->original_curve ?
990 path->original_curve->copy() : path->curve->copy();
991 cc->red_curve->transform(i2d);
993 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), cc->red_curve);
994 ret = TRUE;
995 break;
996 }
997 case SP_CONNECTOR_CONTEXT_STOP:
998 /* This is perfectly valid */
999 break;
1000 default:
1001 if (!sp_event_context_knot_mouseover(cc)) {
1002 m.setup(dt);
1003 m.preSnap(Inkscape::SnapCandidatePoint(p, Inkscape::SNAPSOURCE_OTHER_HANDLE));
1004 m.unSetup();
1005 }
1006 break;
1007 }
1008 }
1009 else if ( cc->mode == SP_CONNECTOR_CONTEXT_EDITING_MODE )
1010 {
1011 switch ( cc->state )
1012 {
1013 case SP_CONNECTOR_CONTEXT_DRAGGING:
1014 sp_knot_set_position(cc->selected_handle, p, 0);
1015 ret = TRUE;
1016 break;
1017 case SP_CONNECTOR_CONTEXT_NEWCONNPOINT:
1018 sp_knot_set_position(cc->selected_handle, p, 0);
1019 ret = TRUE;
1020 break;
1021 }
1022 }
1024 return ret;
1025 }
1028 static gint
1029 connector_handle_button_release(SPConnectorContext *const cc, GdkEventButton const &revent)
1030 {
1031 gint ret = FALSE;
1032 SPEventContext *event_context = SP_EVENT_CONTEXT(cc);
1033 if ( revent.button == 1 && !event_context->space_panning ) {
1035 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
1036 SPDocument *doc = sp_desktop_document(desktop);
1038 SnapManager &m = desktop->namedview->snap_manager;
1040 Geom::Point const event_w(revent.x, revent.y);
1042 /* Find desktop coordinates */
1043 Geom::Point p = cc->desktop->w2d(event_w);
1044 if ( cc->mode == SP_CONNECTOR_CONTEXT_DRAWING_MODE )
1045 {
1046 switch (cc->state) {
1047 //case SP_CONNECTOR_CONTEXT_POINT:
1048 case SP_CONNECTOR_CONTEXT_DRAGGING:
1049 {
1050 m.setup(desktop);
1051 m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE);
1052 m.unSetup();
1054 if (cc->within_tolerance)
1055 {
1056 spcc_connector_finish_segment(cc, p);
1057 return TRUE;
1058 }
1059 // Connector has been created via a drag, end it now.
1060 spcc_connector_set_subsequent_point(cc, p);
1061 spcc_connector_finish_segment(cc, p);
1062 // Test whether we clicked on a connection point
1063 /*bool found = */conn_pt_handle_test(cc, p, &cc->ehref, &cc->ecpid);
1064 if (cc->npoints != 0) {
1065 spcc_connector_finish(cc);
1066 }
1067 cc_set_active_conn(cc, cc->newconn);
1068 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
1069 break;
1070 }
1071 case SP_CONNECTOR_CONTEXT_REROUTING:
1072 {
1073 m.setup(desktop);
1074 m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE);
1075 m.unSetup();
1076 cc_connector_rerouting_finish(cc, &p);
1078 sp_document_ensure_up_to_date(doc);
1079 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
1080 return TRUE;
1081 break;
1082 }
1083 case SP_CONNECTOR_CONTEXT_STOP:
1084 /* This is allowed, if we just cancelled curve */
1085 break;
1086 default:
1087 break;
1088 }
1089 ret = TRUE;
1090 }
1091 else if ( cc->mode == SP_CONNECTOR_CONTEXT_EDITING_MODE )
1092 {
1093 switch ( cc->state )
1094 {
1095 case SP_CONNECTOR_CONTEXT_DRAGGING:
1097 if (!cc->within_tolerance)
1098 {
1099 m.setup(desktop);
1100 m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE);
1101 m.unSetup();
1102 sp_knot_set_position(cc->selected_handle, p, 0);
1103 ConnectionPoint& cp = cc->connpthandles[cc->selected_handle];
1104 cp.pos = p * sp_item_dt2i_affine(cc->active_shape);
1105 cc->active_shape->avoidRef->updateConnectionPoint(cp);
1106 }
1108 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
1109 ret = TRUE;
1110 break;
1113 case SP_CONNECTOR_CONTEXT_NEWCONNPOINT:
1114 m.setup(desktop);
1115 m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE);
1116 m.unSetup();
1118 sp_knot_set_position(cc->selected_handle, p, 0);
1120 ConnectionPoint cp;
1121 cp.type = ConnPointUserDefined;
1122 cp.pos = p * sp_item_dt2i_affine(cc->active_shape);
1123 cp.dir = Avoid::ConnDirAll;
1124 g_object_unref(cc->selected_handle);
1125 cc->active_shape->avoidRef->addConnectionPoint(cp);
1126 sp_document_ensure_up_to_date(doc);
1127 for (ConnectionPointMap::iterator it = cc->connpthandles.begin(); it != cc->connpthandles.end(); ++it)
1128 if (it->second.type == ConnPointUserDefined && it->second.id == cp.id)
1129 {
1130 cc->selected_handle = it->first;
1131 break;
1132 }
1133 cc_select_handle( cc->selected_handle );
1134 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
1135 ret = TRUE;
1136 break;
1137 }
1138 }
1139 }
1142 return ret;
1143 }
1146 static gint
1147 connector_handle_key_press(SPConnectorContext *const cc, guint const keyval)
1148 {
1149 gint ret = FALSE;
1150 /* fixme: */
1151 if ( cc->mode == SP_CONNECTOR_CONTEXT_DRAWING_MODE )
1152 {
1153 switch (keyval) {
1154 case GDK_Return:
1155 case GDK_KP_Enter:
1156 if (cc->npoints != 0) {
1157 spcc_connector_finish(cc);
1158 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
1159 ret = TRUE;
1160 }
1161 break;
1162 case GDK_Escape:
1163 if (cc->state == SP_CONNECTOR_CONTEXT_REROUTING) {
1165 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
1166 SPDocument *doc = sp_desktop_document(desktop);
1168 cc_connector_rerouting_finish(cc, NULL);
1170 sp_document_undo(doc);
1172 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
1173 desktop->messageStack()->flash( Inkscape::NORMAL_MESSAGE,
1174 _("Connector endpoint drag cancelled."));
1175 ret = TRUE;
1176 }
1177 else if (cc->npoints != 0) {
1178 // if drawing, cancel, otherwise pass it up for deselecting
1179 cc->state = SP_CONNECTOR_CONTEXT_STOP;
1180 spcc_reset_colors(cc);
1181 ret = TRUE;
1182 }
1183 break;
1184 default:
1185 break;
1186 }
1187 }
1188 else if ( cc->mode == SP_CONNECTOR_CONTEXT_EDITING_MODE )
1189 {
1190 switch ( cc->state )
1191 {
1192 case SP_CONNECTOR_CONTEXT_DRAGGING:
1193 if ( keyval == GDK_Escape )
1194 {
1195 // Cancel connection point dragging
1197 // Obtain original position
1198 ConnectionPoint const& cp = cc->connpthandles[cc->selected_handle];
1199 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
1200 const Geom::Matrix& i2doc = sp_item_i2doc_affine(cc->active_shape);
1201 sp_knot_set_position(cc->selected_handle, cp.pos * i2doc * desktop->doc2dt(), 0);
1202 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
1203 desktop->messageStack()->flash( Inkscape::NORMAL_MESSAGE,
1204 _("Connection point drag cancelled."));
1205 ret = TRUE;
1206 }
1207 else if ( keyval == GDK_Return || keyval == GDK_KP_Enter )
1208 {
1209 // Put connection point at current position
1211 Geom::Point p = cc->selected_handle->pos;
1213 if (!cc->within_tolerance)
1214 {
1215 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
1216 SnapManager &m = desktop->namedview->snap_manager;
1217 m.setup(desktop);
1218 m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE);
1219 m.unSetup();
1220 sp_knot_set_position(cc->selected_handle, p, 0);
1221 ConnectionPoint& cp = cc->connpthandles[cc->selected_handle];
1222 cp.pos = p * sp_item_dt2i_affine(cc->active_shape);
1223 cc->active_shape->avoidRef->updateConnectionPoint(cp);
1224 }
1226 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
1227 ret = TRUE;
1228 }
1229 break;
1230 case SP_CONNECTOR_CONTEXT_NEWCONNPOINT:
1231 if ( keyval == GDK_Escape )
1232 {
1233 // Just destroy the knot
1234 g_object_unref( cc->selected_handle );
1235 cc->selected_handle = NULL;
1236 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
1237 ret = TRUE;
1238 }
1239 else if ( keyval == GDK_Return || keyval == GDK_KP_Enter )
1240 {
1241 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
1242 SPDocument *doc = sp_desktop_document(desktop);
1243 SnapManager &m = desktop->namedview->snap_manager;
1244 m.setup(desktop);
1245 Geom::Point p = cc->selected_handle->pos;
1246 m.freeSnapReturnByRef(p, Inkscape::SNAPSOURCE_OTHER_HANDLE);
1247 m.unSetup();
1248 sp_knot_set_position(cc->selected_handle, p, 0);
1250 ConnectionPoint cp;
1251 cp.type = ConnPointUserDefined;
1252 cp.pos = p * sp_item_dt2i_affine(cc->active_shape);
1253 cp.dir = Avoid::ConnDirAll;
1254 g_object_unref(cc->selected_handle);
1255 cc->active_shape->avoidRef->addConnectionPoint(cp);
1256 sp_document_ensure_up_to_date(doc);
1257 for (ConnectionPointMap::iterator it = cc->connpthandles.begin(); it != cc->connpthandles.end(); ++it)
1258 if (it->second.type == ConnPointUserDefined && it->second.id == cp.id)
1259 {
1260 cc->selected_handle = it->first;
1261 break;
1262 }
1263 cc_select_handle( cc->selected_handle );
1264 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
1265 ret = TRUE;
1266 }
1268 break;
1269 case SP_CONNECTOR_CONTEXT_IDLE:
1270 if ( keyval == GDK_Delete && cc->selected_handle )
1271 {
1272 cc->active_shape->avoidRef->deleteConnectionPoint(cc->connpthandles[cc->selected_handle]);
1273 cc->selected_handle = NULL;
1274 ret = TRUE;
1275 }
1277 break;
1278 }
1279 }
1281 return ret;
1282 }
1285 static void
1286 cc_connector_rerouting_finish(SPConnectorContext *const cc, Geom::Point *const p)
1287 {
1288 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
1289 SPDocument *doc = sp_desktop_document(desktop);
1291 // Clear the temporary path:
1292 cc->red_curve->reset();
1293 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
1295 if (p != NULL)
1296 {
1297 // Test whether we clicked on a connection point
1298 gchar *shape_label, *cpid;
1299 bool found = conn_pt_handle_test(cc, *p, &shape_label, &cpid);
1301 if (found) {
1302 if (cc->clickedhandle == cc->endpt_handle[0]) {
1303 sp_object_setAttribute(cc->clickeditem,
1304 "inkscape:connection-start", shape_label, false);
1305 sp_object_setAttribute(cc->clickeditem,
1306 "inkscape:connection-start-point", cpid, false);
1307 }
1308 else {
1309 sp_object_setAttribute(cc->clickeditem,
1310 "inkscape:connection-end", shape_label, false);
1311 sp_object_setAttribute(cc->clickeditem,
1312 "inkscape:connection-end-point", cpid, false);
1313 }
1314 g_free(shape_label);
1315 }
1316 }
1317 cc->clickeditem->setHidden(false);
1318 sp_conn_reroute_path_immediate(SP_PATH(cc->clickeditem));
1319 cc->clickeditem->updateRepr();
1320 sp_document_done(doc, SP_VERB_CONTEXT_CONNECTOR,
1321 _("Reroute connector"));
1322 cc_set_active_conn(cc, cc->clickeditem);
1323 }
1326 static void
1327 spcc_reset_colors(SPConnectorContext *cc)
1328 {
1329 /* Red */
1330 cc->red_curve->reset();
1331 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
1333 cc->green_curve->reset();
1334 cc->npoints = 0;
1335 }
1338 static void
1339 spcc_connector_set_initial_point(SPConnectorContext *const cc, Geom::Point const p)
1340 {
1341 g_assert( cc->npoints == 0 );
1343 cc->p[0] = p;
1344 cc->p[1] = p;
1345 cc->npoints = 2;
1346 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
1347 }
1350 static void
1351 spcc_connector_set_subsequent_point(SPConnectorContext *const cc, Geom::Point const p)
1352 {
1353 g_assert( cc->npoints != 0 );
1355 SPDesktop *dt = cc->desktop;
1356 Geom::Point o = dt->dt2doc(cc->p[0]);
1357 Geom::Point d = dt->dt2doc(p);
1358 Avoid::Point src(o[Geom::X], o[Geom::Y]);
1359 Avoid::Point dst(d[Geom::X], d[Geom::Y]);
1361 if (!cc->newConnRef) {
1362 Avoid::Router *router = sp_desktop_document(dt)->router;
1363 cc->newConnRef = new Avoid::ConnRef(router);
1364 cc->newConnRef->setEndpoint(Avoid::VertID::src, src);
1365 if (cc->isOrthogonal)
1366 cc->newConnRef->setRoutingType(Avoid::ConnType_Orthogonal);
1367 else
1368 cc->newConnRef->setRoutingType(Avoid::ConnType_PolyLine);
1369 }
1370 // Set new endpoint.
1371 cc->newConnRef->setEndpoint(Avoid::VertID::tar, dst);
1372 // Immediately generate new routes for connector.
1373 cc->newConnRef->makePathInvalid();
1374 cc->newConnRef->router()->processTransaction();
1375 // Recreate curve from libavoid route.
1376 recreateCurve( cc->red_curve, cc->newConnRef, cc->curvature );
1377 cc->red_curve->transform(dt->doc2dt());
1378 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), cc->red_curve);
1379 }
1382 /**
1383 * Concats red, blue and green.
1384 * If any anchors are defined, process these, optionally removing curves from white list
1385 * Invoke _flush_white to write result back to object.
1386 */
1387 static void
1388 spcc_concat_colors_and_flush(SPConnectorContext *cc)
1389 {
1390 SPCurve *c = cc->green_curve;
1391 cc->green_curve = new SPCurve();
1393 cc->red_curve->reset();
1394 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
1396 if (c->is_empty()) {
1397 c->unref();
1398 return;
1399 }
1401 spcc_flush_white(cc, c);
1403 c->unref();
1404 }
1407 /*
1408 * Flushes white curve(s) and additional curve into object
1409 *
1410 * No cleaning of colored curves - this has to be done by caller
1411 * No rereading of white data, so if you cannot rely on ::modified, do it in caller
1412 *
1413 */
1415 static void
1416 spcc_flush_white(SPConnectorContext *cc, SPCurve *gc)
1417 {
1418 SPCurve *c;
1420 if (gc) {
1421 c = gc;
1422 c->ref();
1423 } else {
1424 return;
1425 }
1427 /* Now we have to go back to item coordinates at last */
1428 c->transform(SP_EVENT_CONTEXT_DESKTOP(cc)->dt2doc());
1430 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
1431 SPDocument *doc = sp_desktop_document(desktop);
1432 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
1434 if ( c && !c->is_empty() ) {
1435 /* We actually have something to write */
1437 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
1438 /* Set style */
1439 sp_desktop_apply_style_tool(desktop, repr, "/tools/connector", false);
1441 gchar *str = sp_svg_write_path( c->get_pathvector() );
1442 g_assert( str != NULL );
1443 repr->setAttribute("d", str);
1444 g_free(str);
1446 /* Attach repr */
1447 cc->newconn = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
1448 cc->newconn->transform = sp_item_i2doc_affine(SP_ITEM(desktop->currentLayer())).inverse();
1450 bool connection = false;
1451 sp_object_setAttribute(cc->newconn, "inkscape:connector-type",
1452 cc->isOrthogonal ? "orthogonal" : "polyline", false);
1453 sp_object_setAttribute(cc->newconn, "inkscape:connector-curvature",
1454 Glib::Ascii::dtostr(cc->curvature).c_str(), false);
1455 if (cc->shref)
1456 {
1457 sp_object_setAttribute(cc->newconn, "inkscape:connection-start",
1458 cc->shref, false);
1459 if (cc->scpid)
1460 sp_object_setAttribute(cc->newconn, "inkscape:connection-start-point",
1461 cc->scpid, false);
1462 connection = true;
1463 }
1465 if (cc->ehref)
1466 {
1467 sp_object_setAttribute(cc->newconn, "inkscape:connection-end",
1468 cc->ehref, false);
1469 if (cc->ecpid)
1470 sp_object_setAttribute(cc->newconn, "inkscape:connection-end-point",
1471 cc->ecpid, false);
1472 connection = true;
1473 }
1474 // Process pending updates.
1475 cc->newconn->updateRepr();
1476 sp_document_ensure_up_to_date(doc);
1478 if (connection) {
1479 // Adjust endpoints to shape edge.
1480 sp_conn_reroute_path_immediate(SP_PATH(cc->newconn));
1481 cc->newconn->updateRepr();
1482 }
1484 // Only set the selection after we are finished with creating the attributes of
1485 // the connector. Otherwise, the selection change may alter the defaults for
1486 // values like curvature in the connector context, preventing subsequent lookup
1487 // of their original values.
1488 cc->selection->set(repr);
1489 Inkscape::GC::release(repr);
1490 }
1492 c->unref();
1494 sp_document_done(doc, SP_VERB_CONTEXT_CONNECTOR, _("Create connector"));
1495 }
1498 static void
1499 spcc_connector_finish_segment(SPConnectorContext *const cc, Geom::Point const /*p*/)
1500 {
1501 if (!cc->red_curve->is_empty()) {
1502 cc->green_curve->append_continuous(cc->red_curve, 0.0625);
1504 cc->p[0] = cc->p[3];
1505 cc->p[1] = cc->p[4];
1506 cc->npoints = 2;
1508 cc->red_curve->reset();
1509 }
1510 }
1513 static void
1514 spcc_connector_finish(SPConnectorContext *const cc)
1515 {
1516 SPDesktop *const desktop = cc->desktop;
1517 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Finishing connector"));
1519 cc->red_curve->reset();
1520 spcc_concat_colors_and_flush(cc);
1522 cc->npoints = 0;
1524 if (cc->newConnRef) {
1525 cc->newConnRef->removeFromGraph();
1526 delete cc->newConnRef;
1527 cc->newConnRef = NULL;
1528 }
1529 }
1532 static gboolean
1533 cc_generic_knot_handler(SPCanvasItem *, GdkEvent *event, SPKnot *knot)
1534 {
1535 g_assert (knot != NULL);
1537 g_object_ref(knot);
1539 SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(
1540 knot->desktop->event_context);
1542 gboolean consumed = FALSE;
1544 gchar* knot_tip = knot->tip ? knot->tip : cc->knot_tip;
1545 switch (event->type) {
1546 case GDK_ENTER_NOTIFY:
1547 sp_knot_set_flag(knot, SP_KNOT_MOUSEOVER, TRUE);
1549 cc->active_handle = knot;
1550 if (knot_tip)
1551 {
1552 knot->desktop->event_context->defaultMessageContext()->set(
1553 Inkscape::NORMAL_MESSAGE, knot_tip);
1554 }
1556 consumed = TRUE;
1557 break;
1558 case GDK_LEAVE_NOTIFY:
1559 sp_knot_set_flag(knot, SP_KNOT_MOUSEOVER, FALSE);
1561 cc->active_handle = NULL;
1563 if (knot_tip) {
1564 knot->desktop->event_context->defaultMessageContext()->clear();
1565 }
1567 consumed = TRUE;
1568 break;
1569 default:
1570 break;
1571 }
1573 g_object_unref(knot);
1575 return consumed;
1576 }
1579 static gboolean
1580 endpt_handler(SPKnot */*knot*/, GdkEvent *event, SPConnectorContext *cc)
1581 {
1582 g_assert( SP_IS_CONNECTOR_CONTEXT(cc) );
1584 gboolean consumed = FALSE;
1586 switch (event->type) {
1587 case GDK_BUTTON_PRESS:
1588 g_assert( (cc->active_handle == cc->endpt_handle[0]) ||
1589 (cc->active_handle == cc->endpt_handle[1]) );
1590 if (cc->state == SP_CONNECTOR_CONTEXT_IDLE) {
1591 cc->clickeditem = cc->active_conn;
1592 cc->clickedhandle = cc->active_handle;
1593 cc_clear_active_conn(cc);
1594 cc->state = SP_CONNECTOR_CONTEXT_REROUTING;
1596 // Disconnect from attached shape
1597 unsigned ind = (cc->active_handle == cc->endpt_handle[0]) ? 0 : 1;
1598 sp_conn_end_detach(cc->clickeditem, ind);
1600 Geom::Point origin;
1601 if (cc->clickedhandle == cc->endpt_handle[0]) {
1602 origin = cc->endpt_handle[1]->pos;
1603 }
1604 else {
1605 origin = cc->endpt_handle[0]->pos;
1606 }
1608 // Show the red path for dragging.
1609 cc->red_curve = SP_PATH(cc->clickeditem)->original_curve ? SP_PATH(cc->clickeditem)->original_curve->copy() : SP_PATH(cc->clickeditem)->curve->copy();
1610 Geom::Matrix i2d = sp_item_i2d_affine(cc->clickeditem);
1611 cc->red_curve->transform(i2d);
1612 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), cc->red_curve);
1614 cc->clickeditem->setHidden(true);
1616 // The rest of the interaction rerouting the connector is
1617 // handled by the context root handler.
1618 consumed = TRUE;
1619 }
1620 break;
1621 default:
1622 break;
1623 }
1625 return consumed;
1626 }
1628 static void cc_active_shape_add_knot(SPDesktop* desktop, SPItem* item, ConnectionPointMap &cphandles, ConnectionPoint& cp)
1629 {
1630 SPKnot *knot = sp_knot_new(desktop, 0);
1632 knot->setShape(SP_KNOT_SHAPE_SQUARE);
1633 knot->setSize(8);
1634 knot->setAnchor(GTK_ANCHOR_CENTER);
1635 knot->setFill(0xffffff00, 0xff0000ff, 0xff0000ff);
1636 sp_knot_update_ctrl(knot);
1638 // We don't want to use the standard knot handler.
1639 g_signal_handler_disconnect(G_OBJECT(knot->item),
1640 knot->_event_handler_id);
1641 knot->_event_handler_id = 0;
1643 gtk_signal_connect(GTK_OBJECT(knot->item), "event",
1644 GTK_SIGNAL_FUNC(cc_generic_knot_handler), knot);
1645 sp_knot_set_position(knot, item->avoidRef->getConnectionPointPos(cp.type, cp.id) * desktop->doc2dt(), 0);
1646 sp_knot_show(knot);
1647 cphandles[knot] = cp;
1648 }
1650 static void cc_set_active_shape(SPConnectorContext *cc, SPItem *item)
1651 {
1652 g_assert(item != NULL );
1654 std::map<int, ConnectionPoint>* connpts = &item->avoidRef->connection_points;
1656 if (cc->active_shape != item)
1657 {
1658 // The active shape has changed
1659 // Rebuild everything
1660 cc->active_shape = item;
1661 // Remove existing active shape listeners
1662 if (cc->active_shape_repr) {
1663 sp_repr_remove_listener_by_data(cc->active_shape_repr, cc);
1664 Inkscape::GC::release(cc->active_shape_repr);
1666 sp_repr_remove_listener_by_data(cc->active_shape_layer_repr, cc);
1667 Inkscape::GC::release(cc->active_shape_layer_repr);
1668 }
1670 // Listen in case the active shape changes
1671 cc->active_shape_repr = SP_OBJECT_REPR(item);
1672 if (cc->active_shape_repr) {
1673 Inkscape::GC::anchor(cc->active_shape_repr);
1674 sp_repr_add_listener(cc->active_shape_repr, &shape_repr_events, cc);
1676 cc->active_shape_layer_repr = cc->active_shape_repr->parent();
1677 Inkscape::GC::anchor(cc->active_shape_layer_repr);
1678 sp_repr_add_listener(cc->active_shape_layer_repr, &layer_repr_events, cc);
1679 }
1682 // Set the connection points.
1683 if ( cc->connpthandles.size() )
1684 // destroy the old list
1685 while (! cc->connpthandles.empty() )
1686 {
1687 g_object_unref(cc->connpthandles.begin()->first);
1688 cc->connpthandles.erase(cc->connpthandles.begin());
1689 }
1690 // build the new one
1691 if ( connpts->size() )
1692 for (std::map<int, ConnectionPoint>::iterator it = connpts->begin(); it != connpts->end(); ++it)
1693 cc_active_shape_add_knot(cc->desktop, item, cc->connpthandles, it->second);
1695 // Also add default connection points
1696 // For now, only centre default connection point will
1697 // be available
1698 ConnectionPoint centre;
1699 centre.type = ConnPointDefault;
1700 centre.id = ConnPointPosCC;
1701 cc_active_shape_add_knot(cc->desktop, item, cc->connpthandles, centre);
1702 }
1703 else
1704 {
1705 // The active shape didn't change
1706 // Update only the connection point knots
1708 // Ensure the item's connection_points map
1709 // has been updated
1710 sp_document_ensure_up_to_date(SP_OBJECT_DOCUMENT(item));
1712 std::set<int> seen;
1713 for ( ConnectionPointMap::iterator it = cc->connpthandles.begin(); it != cc->connpthandles.end() ;)
1714 {
1715 bool removed = false;
1716 if ( it->second.type == ConnPointUserDefined )
1717 {
1718 std::map<int, ConnectionPoint>::iterator p = connpts->find(it->second.id);
1719 if (p != connpts->end())
1720 {
1721 if ( it->second != p->second )
1722 // Connection point position has changed
1723 // Update knot position
1724 sp_knot_set_position(it->first,
1725 item->avoidRef->getConnectionPointPos(it->second.type, it->second.id) * cc->desktop->doc2dt(), 0);
1726 seen.insert(it->second.id);
1727 sp_knot_show(it->first);
1728 }
1729 else
1730 {
1731 // This connection point does no longer exist,
1732 // remove the knot
1733 ConnectionPointMap::iterator curr = it;
1734 ++it;
1735 g_object_unref( curr->first );
1736 cc->connpthandles.erase(curr);
1737 removed = true;
1738 }
1739 }
1740 else
1741 {
1742 // It's a default connection point
1743 // Just make sure it's position is correct
1744 sp_knot_set_position(it->first,
1745 item->avoidRef->getConnectionPointPos(it->second.type, it->second.id) * cc->desktop->doc2dt(), 0);
1746 sp_knot_show(it->first);
1748 }
1749 if ( !removed )
1750 ++it;
1751 }
1752 // Add knots for new connection points.
1753 if (connpts->size())
1754 for ( std::map<int, ConnectionPoint>::iterator it = connpts->begin(); it != connpts->end(); ++it )
1755 if ( seen.find(it->first) == seen.end() )
1756 // A new connection point has been added
1757 // to the shape. Add a knot for it.
1758 cc_active_shape_add_knot(cc->desktop, item, cc->connpthandles, it->second);
1759 }
1760 }
1763 static void
1764 cc_set_active_conn(SPConnectorContext *cc, SPItem *item)
1765 {
1766 g_assert( SP_IS_PATH(item) );
1768 SPCurve *curve = SP_PATH(item)->original_curve ? SP_PATH(item)->original_curve : SP_PATH(item)->curve;
1769 Geom::Matrix i2d = sp_item_i2d_affine(item);
1771 if (cc->active_conn == item)
1772 {
1773 if (curve->is_empty())
1774 {
1775 // Connector is invisible because it is clipped to the boundary of
1776 // two overlpapping shapes.
1777 sp_knot_hide(cc->endpt_handle[0]);
1778 sp_knot_hide(cc->endpt_handle[1]);
1779 }
1780 else
1781 {
1782 // Just adjust handle positions.
1783 Geom::Point startpt = *(curve->first_point()) * i2d;
1784 sp_knot_set_position(cc->endpt_handle[0], startpt, 0);
1786 Geom::Point endpt = *(curve->last_point()) * i2d;
1787 sp_knot_set_position(cc->endpt_handle[1], endpt, 0);
1788 }
1790 return;
1791 }
1793 cc->active_conn = item;
1795 // Remove existing active conn listeners
1796 if (cc->active_conn_repr) {
1797 sp_repr_remove_listener_by_data(cc->active_conn_repr, cc);
1798 Inkscape::GC::release(cc->active_conn_repr);
1799 cc->active_conn_repr = NULL;
1800 }
1802 // Listen in case the active conn changes
1803 cc->active_conn_repr = SP_OBJECT_REPR(item);
1804 if (cc->active_conn_repr) {
1805 Inkscape::GC::anchor(cc->active_conn_repr);
1806 sp_repr_add_listener(cc->active_conn_repr, &shape_repr_events, cc);
1807 }
1809 for (int i = 0; i < 2; ++i) {
1811 // Create the handle if it doesn't exist
1812 if ( cc->endpt_handle[i] == NULL ) {
1813 SPKnot *knot = sp_knot_new(cc->desktop,
1814 _("<b>Connector endpoint</b>: drag to reroute or connect to new shapes"));
1816 knot->setShape(SP_KNOT_SHAPE_SQUARE);
1817 knot->setSize(7);
1818 knot->setAnchor(GTK_ANCHOR_CENTER);
1819 knot->setFill(0xffffff00, 0xff0000ff, 0xff0000ff);
1820 knot->setStroke(0x000000ff, 0x000000ff, 0x000000ff);
1821 sp_knot_update_ctrl(knot);
1823 // We don't want to use the standard knot handler,
1824 // since we don't want this knot to be draggable.
1825 g_signal_handler_disconnect(G_OBJECT(knot->item),
1826 knot->_event_handler_id);
1827 knot->_event_handler_id = 0;
1829 gtk_signal_connect(GTK_OBJECT(knot->item), "event",
1830 GTK_SIGNAL_FUNC(cc_generic_knot_handler), knot);
1832 cc->endpt_handle[i] = knot;
1833 }
1835 // Remove any existing handlers
1836 if (cc->endpt_handler_id[i]) {
1837 g_signal_handlers_disconnect_by_func(
1838 G_OBJECT(cc->endpt_handle[i]->item),
1839 (void*)G_CALLBACK(endpt_handler), (gpointer) cc );
1840 cc->endpt_handler_id[i] = 0;
1841 }
1843 // Setup handlers for connector endpoints, this is
1844 // is as 'after' so that cc_generic_knot_handler is
1845 // triggered first for any endpoint.
1846 cc->endpt_handler_id[i] = g_signal_connect_after(
1847 G_OBJECT(cc->endpt_handle[i]->item), "event",
1848 G_CALLBACK(endpt_handler), cc);
1849 }
1851 if (curve->is_empty())
1852 {
1853 // Connector is invisible because it is clipped to the boundary
1854 // of two overlpapping shapes. So, it doesn't need endpoints.
1855 return;
1856 }
1858 Geom::Point startpt = *(curve->first_point()) * i2d;
1859 sp_knot_set_position(cc->endpt_handle[0], startpt, 0);
1861 Geom::Point endpt = *(curve->last_point()) * i2d;
1862 sp_knot_set_position(cc->endpt_handle[1], endpt, 0);
1864 sp_knot_show(cc->endpt_handle[0]);
1865 sp_knot_show(cc->endpt_handle[1]);
1866 }
1868 void cc_create_connection_point(SPConnectorContext* cc)
1869 {
1870 if (cc->active_shape && cc->state == SP_CONNECTOR_CONTEXT_IDLE)
1871 {
1872 if (cc->selected_handle)
1873 {
1874 cc_deselect_handle( cc->selected_handle );
1875 }
1876 SPKnot *knot = sp_knot_new(cc->desktop, 0);
1877 // We do not process events on this knot.
1878 g_signal_handler_disconnect(G_OBJECT(knot->item),
1879 knot->_event_handler_id);
1880 knot->_event_handler_id = 0;
1882 cc_select_handle( knot );
1883 cc->selected_handle = knot;
1884 sp_knot_show(cc->selected_handle);
1885 cc->state = SP_CONNECTOR_CONTEXT_NEWCONNPOINT;
1886 }
1887 }
1889 void cc_remove_connection_point(SPConnectorContext* cc)
1890 {
1891 if (cc->selected_handle && cc->state == SP_CONNECTOR_CONTEXT_IDLE )
1892 {
1893 cc->active_shape->avoidRef->deleteConnectionPoint(cc->connpthandles[cc->selected_handle]);
1894 cc->selected_handle = NULL;
1895 }
1896 }
1898 static bool cc_item_is_shape(SPItem *item)
1899 {
1900 if (SP_IS_PATH(item)) {
1901 SPCurve *curve = (SP_SHAPE(item))->curve;
1902 if ( curve && !(curve->is_closed()) ) {
1903 // Open paths are connectors.
1904 return false;
1905 }
1906 }
1907 else if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
1908 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1909 if (prefs->getBool("/tools/connector/ignoretext", true)) {
1910 // Don't count text as a shape we can connect connector to.
1911 return false;
1912 }
1913 }
1914 return true;
1915 }
1918 bool cc_item_is_connector(SPItem *item)
1919 {
1920 if (SP_IS_PATH(item)) {
1921 bool closed = SP_PATH(item)->original_curve ? SP_PATH(item)->original_curve->is_closed() : SP_PATH(item)->curve->is_closed();
1922 if (SP_PATH(item)->connEndPair.isAutoRoutingConn() && !closed) {
1923 // To be considered a connector, an object must be a non-closed
1924 // path that is marked with a "inkscape:connector-type" attribute.
1925 return true;
1926 }
1927 }
1928 return false;
1929 }
1932 void cc_selection_set_avoid(bool const set_avoid)
1933 {
1934 SPDesktop *desktop = inkscape_active_desktop();
1935 if (desktop == NULL) {
1936 return;
1937 }
1939 SPDocument *document = sp_desktop_document(desktop);
1941 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1943 GSList *l = (GSList *) selection->itemList();
1945 int changes = 0;
1947 while (l) {
1948 SPItem *item = (SPItem *) l->data;
1950 char const *value = (set_avoid) ? "true" : NULL;
1952 if (cc_item_is_shape(item)) {
1953 sp_object_setAttribute(item, "inkscape:connector-avoid",
1954 value, false);
1955 item->avoidRef->handleSettingChange();
1956 changes++;
1957 }
1959 l = l->next;
1960 }
1962 if (changes == 0) {
1963 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE,
1964 _("Select <b>at least one non-connector object</b>."));
1965 return;
1966 }
1968 char *event_desc = (set_avoid) ?
1969 _("Make connectors avoid selected objects") :
1970 _("Make connectors ignore selected objects");
1971 sp_document_done(document, SP_VERB_CONTEXT_CONNECTOR, event_desc);
1972 }
1975 static void
1976 cc_selection_changed(Inkscape::Selection *selection, gpointer data)
1977 {
1978 SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(data);
1979 //SPEventContext *ec = SP_EVENT_CONTEXT(cc);
1981 SPItem *item = selection->singleItem();
1983 if (cc->active_conn == item)
1984 {
1985 // Nothing to change.
1986 return;
1987 }
1988 if (item == NULL)
1989 {
1990 cc_clear_active_conn(cc);
1991 return;
1992 }
1994 if (cc_item_is_connector(item)) {
1995 cc_set_active_conn(cc, item);
1996 }
1997 }
2000 static void
2001 shape_event_attr_deleted(Inkscape::XML::Node */*repr*/, Inkscape::XML::Node *child,
2002 Inkscape::XML::Node */*ref*/, gpointer data)
2003 {
2004 g_assert(data);
2005 SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(data);
2007 if (child == cc->active_shape_repr) {
2008 // The active shape has been deleted. Clear active shape.
2009 cc_clear_active_shape(cc);
2010 }
2011 }
2014 static void
2015 shape_event_attr_changed(Inkscape::XML::Node *repr, gchar const *name,
2016 gchar const */*old_value*/, gchar const */*new_value*/,
2017 bool /*is_interactive*/, gpointer data)
2018 {
2019 g_assert(data);
2020 SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(data);
2022 // Look for changes that result in onscreen movement.
2023 if (!strcmp(name, "d") || !strcmp(name, "x") || !strcmp(name, "y") ||
2024 !strcmp(name, "width") || !strcmp(name, "height") ||
2025 !strcmp(name, "transform"))
2026 {
2027 if (repr == cc->active_shape_repr) {
2028 // Active shape has moved. Clear active shape.
2029 cc_clear_active_shape(cc);
2030 }
2031 else if (repr == cc->active_conn_repr) {
2032 // The active conn has been moved.
2033 // Set it again, which just sets new handle positions.
2034 cc_set_active_conn(cc, cc->active_conn);
2035 }
2036 }
2037 else
2038 if ( !strcmp(name, "inkscape:connection-points") )
2039 if (repr == cc->active_shape_repr)
2040 // The connection points of the active shape
2041 // have changed. Update them.
2042 cc_set_active_shape(cc, cc->active_shape);
2043 }
2046 /*
2047 Local Variables:
2048 mode:c++
2049 c-file-style:"stroustrup"
2050 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
2051 indent-tabs-mode:nil
2052 fill-column:99
2053 End:
2054 */
2055 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :