1 /*
2 * Connector creation tool
3 *
4 * Authors:
5 * Michael Wybrow <mjwybrow@users.sourceforge.net>
6 *
7 * Copyright (C) 2005 Michael Wybrow
8 *
9 * Released under GNU GPL, read the file 'COPYING' for more information
10 *
11 * TODO:
12 * o Have shapes avoid coonvex hulls of objects, rather than their
13 * bounding box. Possibly implement the unfinished ConvexHull
14 * class in libnr.
15 * (HOWEVER, using the convex hull C of a shape S does the wrong thing if a
16 * connector starts outside of S but inside C, or if the best route around
17 * an object involves going inside C but without entering S.)
18 * o Draw connectors to shape edges rather than bounding box.
19 * o Show a visual indicator for objects with the 'avoid' property set.
20 * o Create an interface for setting markers (arrow heads).
21 * o Better distinguish between paths and connectors to prevent problems
22 * in the node tool and paths accidently being turned into connectors
23 * in the connector tool. Perhaps have a way to convert between.
24 * o Only call libavoid's updateEndPoint as required. Currently we do it
25 * for both endpoints, even if only one is moving.
26 * o Cleanup to remove unecessary borrowed DrawContext code.
27 * o Allow user-placeable connection points.
28 * o Deal sanely with connectors with both endpoints attached to the
29 * same connection point, and drawing of connectors attaching
30 * overlaping shapes (currently tries to adjust connector to be
31 * outside both bounding boxes).
32 * o Fix many special cases related to connectors updating,
33 * e.g., copying a couple of shapes and a connector that are
34 * attached to each other.
35 * e.g., detach connector when it is moved or transformed in
36 * one of the other contexts.
37 * o Cope with shapes whose ids change when they have attached
38 * connectors.
39 * o gobble_motion_events(GDK_BUTTON1_MASK)?;
40 *
41 */
43 #include <gdk/gdkkeysyms.h>
45 #include "connector-context.h"
46 #include "pixmaps/cursor-connector.xpm"
47 #include "xml/node-event-vector.h"
48 #include "xml/repr.h"
49 #include "svg/svg.h"
50 #include "desktop.h"
51 #include "desktop-style.h"
52 #include "desktop-affine.h"
53 #include "desktop-handles.h"
54 #include "document.h"
55 #include "message-context.h"
56 #include "message-stack.h"
57 #include "selection.h"
58 #include "inkscape.h"
59 #include "prefs-utils.h"
60 #include "sp-path.h"
61 #include "display/canvas-bpath.h"
62 #include "display/sodipodi-ctrl.h"
63 #include <glibmm/i18n.h>
64 #include "snap.h"
65 #include "knot.h"
66 #include "sp-conn-end.h"
67 #include "conn-avoid-ref.h"
68 #include "libavoid/vertices.h"
69 #include "context-fns.h"
73 static void sp_connector_context_class_init(SPConnectorContextClass *klass);
74 static void sp_connector_context_init(SPConnectorContext *conn_context);
75 static void sp_connector_context_dispose(GObject *object);
77 static void sp_connector_context_setup(SPEventContext *ec);
78 static void sp_connector_context_finish(SPEventContext *ec);
79 static gint sp_connector_context_root_handler(SPEventContext *ec, GdkEvent *event);
80 static gint sp_connector_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
82 // Stuff borrowed from DrawContext
83 static void spcc_connector_set_initial_point(SPConnectorContext *cc, NR::Point const p);
84 static void spcc_connector_set_subsequent_point(SPConnectorContext *cc, NR::Point const p);
85 static void spcc_connector_finish_segment(SPConnectorContext *cc, NR::Point p);
86 static void spcc_reset_colors(SPConnectorContext *cc);
87 static void spcc_connector_finish(SPConnectorContext *cc);
88 static void spcc_concat_colors_and_flush(SPConnectorContext *cc);
89 static void spcc_flush_white(SPConnectorContext *cc, SPCurve *gc);
91 // Context event handlers
92 static gint connector_handle_button_press(SPConnectorContext *const cc, GdkEventButton const &bevent);
93 static gint connector_handle_motion_notify(SPConnectorContext *const cc, GdkEventMotion const &mevent);
94 static gint connector_handle_button_release(SPConnectorContext *const cc, GdkEventButton const &revent);
95 static gint connector_handle_key_press(SPConnectorContext *const cc, guint const keyval);
97 static void cc_set_active_shape(SPConnectorContext *cc, SPItem *item);
98 static void cc_clear_active_shape(SPConnectorContext *cc);
99 static void cc_set_active_conn(SPConnectorContext *cc, SPItem *item);
100 static void cc_clear_active_conn(SPConnectorContext *cc);
101 static gchar *conn_pt_handle_test(SPConnectorContext *cc, NR::Point& w);
102 static bool cc_item_is_shape(SPItem *item);
103 static void cc_selection_changed(Inkscape::Selection *selection, gpointer data);
105 static void shape_event_attr_deleted(Inkscape::XML::Node *repr,
106 Inkscape::XML::Node *child, Inkscape::XML::Node *ref, gpointer data);
107 static void shape_event_attr_changed(Inkscape::XML::Node *repr, gchar const *name,
108 gchar const *old_value, gchar const *new_value, bool is_interactive,
109 gpointer data);
112 static NR::Point connector_drag_origin_w(0, 0);
113 static bool connector_within_tolerance = false;
114 static SPEventContextClass *parent_class;
117 static Inkscape::XML::NodeEventVector shape_repr_events = {
118 NULL, /* child_added */
119 NULL, /* child_added */
120 shape_event_attr_changed,
121 NULL, /* content_changed */
122 NULL /* order_changed */
123 };
125 static Inkscape::XML::NodeEventVector layer_repr_events = {
126 NULL, /* child_added */
127 shape_event_attr_deleted,
128 NULL, /* child_added */
129 NULL, /* content_changed */
130 NULL /* order_changed */
131 };
134 GType
135 sp_connector_context_get_type(void)
136 {
137 static GType type = 0;
138 if (!type) {
139 GTypeInfo info = {
140 sizeof(SPConnectorContextClass),
141 NULL, NULL,
142 (GClassInitFunc) sp_connector_context_class_init,
143 NULL, NULL,
144 sizeof(SPConnectorContext),
145 4,
146 (GInstanceInitFunc) sp_connector_context_init,
147 NULL, /* value_table */
148 };
149 type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPConnectorContext", &info, (GTypeFlags)0);
150 }
151 return type;
152 }
154 static void
155 sp_connector_context_class_init(SPConnectorContextClass *klass)
156 {
157 GObjectClass *object_class;
158 SPEventContextClass *event_context_class;
160 object_class = (GObjectClass *) klass;
161 event_context_class = (SPEventContextClass *) klass;
163 parent_class = (SPEventContextClass*)g_type_class_peek_parent(klass);
165 object_class->dispose = sp_connector_context_dispose;
167 event_context_class->setup = sp_connector_context_setup;
168 event_context_class->finish = sp_connector_context_finish;
169 event_context_class->root_handler = sp_connector_context_root_handler;
170 event_context_class->item_handler = sp_connector_context_item_handler;
171 }
174 static void
175 sp_connector_context_init(SPConnectorContext *cc)
176 {
177 SPEventContext *ec = SP_EVENT_CONTEXT(cc);
179 ec->cursor_shape = cursor_connector_xpm;
180 ec->hot_x = 1;
181 ec->hot_y = 1;
182 ec->xp = 0;
183 ec->yp = 0;
185 cc->red_color = 0xff00007f;
187 cc->newconn = NULL;
188 cc->newConnRef = NULL;
190 cc->sel_changed_connection = sigc::connection();
192 cc->active_shape = NULL;
193 cc->active_shape_repr = NULL;
194 cc->active_shape_layer_repr = NULL;
196 cc->active_conn = NULL;
197 cc->active_conn_repr = NULL;
199 cc->active_handle = NULL;
201 cc->clickeditem = NULL;
202 cc->clickedhandle = NULL;
204 cc->connpthandle = NULL;
205 for (int i = 0; i < 2; ++i) {
206 cc->endpt_handle[i] = NULL;
207 cc->endpt_handler_id[i] = 0;
208 }
209 cc->sid = NULL;
210 cc->eid = NULL;
211 cc->npoints = 0;
212 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
213 }
216 static void
217 sp_connector_context_dispose(GObject *object)
218 {
219 SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(object);
221 cc->sel_changed_connection.disconnect();
223 if (cc->connpthandle) {
224 g_object_unref(cc->connpthandle);
225 cc->connpthandle = NULL;
226 }
227 for (int i = 0; i < 2; ++i) {
228 if (cc->endpt_handle[1]) {
229 g_object_unref(cc->endpt_handle[i]);
230 cc->endpt_handle[i] = NULL;
231 }
232 }
233 if (cc->sid) {
234 g_free(cc->sid);
235 cc->sid = NULL;
236 }
237 if (cc->eid) {
238 g_free(cc->eid);
239 cc->eid = NULL;
240 }
241 g_assert( cc->newConnRef == NULL );
243 G_OBJECT_CLASS(parent_class)->dispose(object);
244 }
247 static void
248 sp_connector_context_setup(SPEventContext *ec)
249 {
250 SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(ec);
251 SPDesktop *dt = ec->desktop;
253 if (((SPEventContextClass *) parent_class)->setup) {
254 ((SPEventContextClass *) parent_class)->setup(ec);
255 }
257 cc->selection = SP_DT_SELECTION(dt);
259 cc->sel_changed_connection.disconnect();
260 cc->sel_changed_connection = cc->selection->connectChanged(
261 sigc::bind(sigc::ptr_fun(&cc_selection_changed),
262 (gpointer) cc));
264 /* Create red bpath */
265 cc->red_bpath = sp_canvas_bpath_new(SP_DT_SKETCH(ec->desktop), NULL);
266 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cc->red_bpath), cc->red_color,
267 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
268 sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(cc->red_bpath), 0x00000000,
269 SP_WIND_RULE_NONZERO);
270 /* Create red curve */
271 cc->red_curve = sp_curve_new_sized(4);
273 /* Create green curve */
274 cc->green_curve = sp_curve_new_sized(64);
276 // Notice the initial selection.
277 cc_selection_changed(cc->selection, (gpointer) cc);
279 if (prefs_get_int_attribute("tools.connector", "selcue", 0) != 0) {
280 ec->enableSelectionCue();
281 }
282 }
285 static void
286 sp_connector_context_finish(SPEventContext *ec)
287 {
288 SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(ec);
290 spcc_connector_finish(cc);
292 if (((SPEventContextClass *) parent_class)->finish) {
293 ((SPEventContextClass *) parent_class)->finish(ec);
294 }
296 if (cc->selection) {
297 cc->selection = NULL;
298 }
299 cc_clear_active_shape(cc);
300 cc_clear_active_conn(cc);
301 }
304 //-----------------------------------------------------------------------------
307 static void
308 cc_clear_active_shape(SPConnectorContext *cc)
309 {
310 if (cc->active_shape == NULL) {
311 return;
312 }
313 g_assert( cc->active_shape_repr );
314 g_assert( cc->active_shape_layer_repr );
316 cc->active_shape = NULL;
318 if (cc->active_shape_repr) {
319 sp_repr_remove_listener_by_data(cc->active_shape_repr, cc);
320 Inkscape::GC::release(cc->active_shape_repr);
321 cc->active_shape_repr = NULL;
323 sp_repr_remove_listener_by_data(cc->active_shape_layer_repr, cc);
324 Inkscape::GC::release(cc->active_shape_layer_repr);
325 cc->active_shape_layer_repr = NULL;
326 }
328 // Hide the center connection point if it exists.
329 if (cc->connpthandle) {
330 sp_knot_hide(cc->connpthandle);
331 }
332 }
335 static void
336 cc_clear_active_conn(SPConnectorContext *cc)
337 {
338 if (cc->active_conn == NULL) {
339 return;
340 }
341 g_assert( cc->active_conn_repr );
343 cc->active_conn = NULL;
345 if (cc->active_conn_repr) {
346 sp_repr_remove_listener_by_data(cc->active_conn_repr, cc);
347 Inkscape::GC::release(cc->active_conn_repr);
348 cc->active_conn_repr = NULL;
349 }
351 // Hide the endpoint handles.
352 for (int i = 0; i < 2; ++i) {
353 if (cc->endpt_handle[i]) {
354 sp_knot_hide(cc->endpt_handle[i]);
355 }
356 }
357 }
360 static gchar *
361 conn_pt_handle_test(SPConnectorContext *cc, NR::Point& p)
362 {
363 // TODO: this will need to change when there are more connection
364 // points available for each shape.
366 SPKnot *centerpt = cc->connpthandle;
367 if (cc->active_handle && (cc->active_handle == centerpt))
368 {
369 p = centerpt->pos;
370 return g_strdup_printf("#%s", SP_OBJECT_ID(cc->active_shape));
371 }
372 return NULL;
373 }
377 static gint
378 sp_connector_context_item_handler(SPEventContext *ec, SPItem *item, GdkEvent *event)
379 {
380 gint ret = FALSE;
382 SPDesktop *desktop = ec->desktop;
384 SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(ec);
386 NR::Point p(event->button.x, event->button.y);
388 switch (event->type) {
389 case GDK_BUTTON_RELEASE:
390 if (event->button.button == 1) {
391 if ((cc->state == SP_CONNECTOR_CONTEXT_DRAGGING) &&
392 (connector_within_tolerance))
393 {
394 spcc_reset_colors(cc);
395 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
396 }
397 if (cc->state != SP_CONNECTOR_CONTEXT_IDLE) {
398 // Doing simething else like rerouting.
399 break;
400 }
401 // find out clicked item, disregarding groups, honoring Alt
402 SPItem *item_ungrouped = sp_event_context_find_item(desktop,
403 p, event->button.state & GDK_MOD1_MASK, TRUE);
405 if (event->button.state & GDK_SHIFT_MASK) {
406 cc->selection->toggle(item_ungrouped);
407 } else {
408 cc->selection->set(item_ungrouped);
409 }
410 ret = TRUE;
411 }
412 break;
413 case GDK_ENTER_NOTIFY:
414 {
415 if (cc_item_is_shape(item)) {
416 // This is a shape, so show connection point(s).
417 if (!(cc->active_shape) ||
418 // Don't show handle for another handle.
419 (item != ((SPItem *) cc->connpthandle))) {
420 cc_set_active_shape(cc, item);
421 }
422 }
423 ret = TRUE;
424 break;
425 }
426 default:
427 break;
428 }
430 return ret;
431 }
434 gint
435 sp_connector_context_root_handler(SPEventContext *ec, GdkEvent *event)
436 {
437 SPConnectorContext *const cc = SP_CONNECTOR_CONTEXT(ec);
439 gint ret = FALSE;
441 switch (event->type) {
442 case GDK_BUTTON_PRESS:
443 ret = connector_handle_button_press(cc, event->button);
444 break;
446 case GDK_MOTION_NOTIFY:
447 ret = connector_handle_motion_notify(cc, event->motion);
448 break;
450 case GDK_BUTTON_RELEASE:
451 ret = connector_handle_button_release(cc, event->button);
452 break;
453 case GDK_KEY_PRESS:
454 ret = connector_handle_key_press(cc, get_group0_keyval (&event->key));
455 break;
457 default:
458 break;
459 }
461 if (!ret) {
462 gint (*const parent_root_handler)(SPEventContext *, GdkEvent *)
463 = ((SPEventContextClass *) parent_class)->root_handler;
464 if (parent_root_handler) {
465 ret = parent_root_handler(ec, event);
466 }
467 }
469 return ret;
470 }
473 static gint
474 connector_handle_button_press(SPConnectorContext *const cc, GdkEventButton const &bevent)
475 {
476 NR::Point const event_w(bevent.x, bevent.y);
477 /* Find desktop coordinates */
478 NR::Point p = cc->desktop->w2d(event_w);
480 gint ret = FALSE;
481 if ( bevent.button == 1 ) {
483 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
485 if (Inkscape::have_viable_layer(desktop, cc->_message_context) == false) {
486 return TRUE;
487 }
489 NR::Point const event_w(bevent.x,
490 bevent.y);
491 connector_drag_origin_w = event_w;
492 connector_within_tolerance = true;
494 NR::Point const event_dt = cc->desktop->w2d(event_w);
495 switch (cc->state) {
496 case SP_CONNECTOR_CONTEXT_STOP:
497 /* This is allowed, if we just cancelled curve */
498 case SP_CONNECTOR_CONTEXT_IDLE:
499 {
500 if ( cc->npoints == 0 ) {
501 /* Set start anchor */
502 NR::Point p;
504 cc_clear_active_conn(cc);
506 SP_EVENT_CONTEXT_DESKTOP(cc)->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating new connector"));
508 /* Create green anchor */
509 p = event_dt;
511 // Test whether we clicked on a connection point
512 cc->sid = conn_pt_handle_test(cc, p);
514 if (!cc->sid) {
515 // This is the first point, so just snap it to the grid
516 // as there's no other points to go off.
517 SnapManager const m(cc->desktop->namedview);
518 p = m.freeSnap(Inkscape::Snapper::SNAP_POINT | Inkscape::Snapper::BBOX_POINT,
519 p, NULL).getPoint();
520 }
521 spcc_connector_set_initial_point(cc, p);
523 }
524 cc->state = SP_CONNECTOR_CONTEXT_DRAGGING;
525 ret = TRUE;
526 break;
527 }
528 case SP_CONNECTOR_CONTEXT_DRAGGING:
529 {
530 // This is the second click of a connector creation.
532 spcc_connector_set_subsequent_point(cc, p);
533 spcc_connector_finish_segment(cc, p);
534 // Test whether we clicked on a connection point
535 cc->eid = conn_pt_handle_test(cc, p);
536 if (cc->npoints != 0) {
537 spcc_connector_finish(cc);
538 }
539 cc_set_active_conn(cc, cc->newconn);
540 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
541 ret = TRUE;
542 break;
543 }
544 case SP_CONNECTOR_CONTEXT_CLOSE:
545 {
546 g_warning("Button down in CLOSE state");
547 break;
548 }
549 default:
550 break;
551 }
552 } else if (bevent.button == 3) {
553 if (cc->npoints != 0) {
554 spcc_connector_finish(cc);
555 ret = TRUE;
556 }
557 }
558 return ret;
559 }
562 static gint
563 connector_handle_motion_notify(SPConnectorContext *const cc, GdkEventMotion const &mevent)
564 {
565 gint ret = FALSE;
567 if (mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) {
568 // allow middle-button scrolling
569 return FALSE;
570 }
572 NR::Point const event_w(mevent.x, mevent.y);
574 if (connector_within_tolerance) {
575 gint const tolerance = prefs_get_int_attribute_limited("options.dragtolerance",
576 "value", 0, 0, 100);
577 if ( NR::LInfty( event_w - connector_drag_origin_w ) < tolerance ) {
578 return FALSE; // Do not drag if we're within tolerance from origin.
579 }
580 }
581 // Once the user has moved farther than tolerance from the original location
582 // (indicating they intend to move the object, not click), then always process
583 // the motion notify coordinates as given (no snapping back to origin)
584 connector_within_tolerance = false;
586 SPDesktop *const dt = cc->desktop;
588 /* Find desktop coordinates */
589 NR::Point p = dt->w2d(event_w);
591 switch (cc->state) {
592 case SP_CONNECTOR_CONTEXT_DRAGGING:
593 {
594 // This is movement during a connector creation.
596 if ( cc->npoints > 0 ) {
597 cc->selection->clear();
598 spcc_connector_set_subsequent_point(cc, p);
599 ret = TRUE;
600 }
601 break;
602 }
603 case SP_CONNECTOR_CONTEXT_REROUTING:
604 {
605 g_assert( SP_IS_PATH(cc->clickeditem));
607 // Update the hidden path
608 NR::Matrix i2d = sp_item_i2d_affine(cc->clickeditem);
609 NR::Matrix d2i = i2d.inverse();
610 SPPath *path = SP_PATH(cc->clickeditem);
611 SPCurve *curve = (SP_SHAPE(path))->curve;
612 if (cc->clickedhandle == cc->endpt_handle[0]) {
613 NR::Point o = cc->endpt_handle[1]->pos;
614 sp_curve_stretch_endpoints(curve, p * d2i, o * d2i);
615 }
616 else {
617 NR::Point o = cc->endpt_handle[0]->pos;
618 sp_curve_stretch_endpoints(curve, o * d2i, p * d2i);
619 }
620 sp_conn_adjust_path(path);
622 // Copy this to the temporary visible path
623 cc->red_curve = sp_curve_copy(SP_SHAPE(path)->curve);
624 sp_curve_transform(cc->red_curve, i2d);
626 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), cc->red_curve);
627 ret = TRUE;
628 break;
629 }
630 case SP_CONNECTOR_CONTEXT_STOP:
631 /* This is perfectly valid */
632 break;
633 default:
634 break;
635 }
637 return ret;
638 }
641 static gint
642 connector_handle_button_release(SPConnectorContext *const cc, GdkEventButton const &revent)
643 {
644 gint ret = FALSE;
645 if ( revent.button == 1 ) {
647 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
648 SPDocument *doc = SP_DT_DOCUMENT(desktop);
650 NR::Point const event_w(revent.x, revent.y);
652 /* Find desktop coordinates */
653 NR::Point p = cc->desktop->w2d(event_w);
655 switch (cc->state) {
656 //case SP_CONNECTOR_CONTEXT_POINT:
657 case SP_CONNECTOR_CONTEXT_DRAGGING:
658 {
659 if (connector_within_tolerance)
660 {
661 spcc_connector_finish_segment(cc, p);
662 return TRUE;
663 }
664 // Connector has been created via a drag, end it now.
665 spcc_connector_set_subsequent_point(cc, p);
666 spcc_connector_finish_segment(cc, p);
667 // Test whether we clicked on a connection point
668 cc->eid = conn_pt_handle_test(cc, p);
669 if (cc->npoints != 0) {
670 spcc_connector_finish(cc);
671 }
672 cc_set_active_conn(cc, cc->newconn);
673 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
674 break;
675 }
676 case SP_CONNECTOR_CONTEXT_REROUTING:
677 {
678 // Clear the temporary path:
679 sp_curve_reset(cc->red_curve);
680 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
682 // Test whether we clicked on a connection point
683 gchar *shape_label = conn_pt_handle_test(cc, p);
685 if (shape_label) {
686 if (cc->clickedhandle == cc->endpt_handle[0]) {
687 sp_object_setAttribute(cc->clickeditem,
688 "inkscape:connection-start",shape_label, false);
689 }
690 else {
691 sp_object_setAttribute(cc->clickeditem,
692 "inkscape:connection-end",shape_label, false);
693 }
694 g_free(shape_label);
695 }
696 cc->clickeditem->setHidden(false);
697 sp_conn_adjust_path(SP_PATH(cc->clickeditem));
698 cc->clickeditem->updateRepr();
699 sp_document_done(doc);
700 cc_set_active_conn(cc, cc->clickeditem);
701 sp_document_ensure_up_to_date(doc);
702 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
703 return TRUE;
704 break;
705 }
706 case SP_CONNECTOR_CONTEXT_STOP:
707 /* This is allowed, if we just cancelled curve */
708 break;
709 default:
710 break;
711 }
712 ret = TRUE;
713 }
715 return ret;
716 }
719 static gint
720 connector_handle_key_press(SPConnectorContext *const cc, guint const keyval)
721 {
722 gint ret = FALSE;
723 /* fixme: */
724 switch (keyval) {
725 case GDK_Return:
726 case GDK_KP_Enter:
727 if (cc->npoints != 0) {
728 spcc_connector_finish(cc);
729 ret = TRUE;
730 }
731 break;
732 case GDK_Escape:
733 if (cc->npoints != 0) {
734 // if drawing, cancel, otherwise pass it up for deselecting
735 cc->state = SP_CONNECTOR_CONTEXT_STOP;
736 spcc_reset_colors(cc);
737 ret = TRUE;
738 }
739 break;
740 default:
741 break;
742 }
743 return ret;
744 }
747 static void
748 spcc_reset_colors(SPConnectorContext *cc)
749 {
750 /* Red */
751 sp_curve_reset(cc->red_curve);
752 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
754 sp_curve_reset(cc->green_curve);
755 cc->npoints = 0;
756 }
759 static void
760 spcc_connector_set_initial_point(SPConnectorContext *const cc, NR::Point const p)
761 {
762 g_assert( cc->npoints == 0 );
764 cc->p[0] = p;
765 cc->p[1] = p;
766 cc->npoints = 2;
767 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
768 }
771 static void
772 spcc_connector_set_subsequent_point(SPConnectorContext *const cc, NR::Point const p)
773 {
774 g_assert( cc->npoints != 0 );
776 SPDesktop *dt = cc->desktop;
777 NR::Point o = dt->dt2doc(cc->p[0]);
778 NR::Point d = dt->dt2doc(p);
779 Avoid::Point src = { o[NR::X], o[NR::Y] };
780 Avoid::Point dst = { d[NR::X], d[NR::Y] };
782 if (!cc->newConnRef) {
783 cc->newConnRef = new Avoid::ConnRef(0, src, dst);
784 cc->newConnRef->updateEndPoint(Avoid::VertID::src, src);
785 }
786 cc->newConnRef->updateEndPoint(Avoid::VertID::tar, dst);
788 cc->newConnRef->makePathInvalid();
789 cc->newConnRef->generatePath(src, dst);
791 Avoid::PolyLine route = cc->newConnRef->route();
792 cc->newConnRef->calcRouteDist();
794 sp_curve_reset(cc->red_curve);
795 NR::Point pt(route.ps[0].x, route.ps[0].y);
796 sp_curve_moveto(cc->red_curve, pt);
798 for (int i = 1; i < route.pn; ++i) {
799 NR::Point p(route.ps[i].x, route.ps[i].y);
800 sp_curve_lineto(cc->red_curve, p);
801 }
802 sp_curve_transform(cc->red_curve, dt->doc2dt());
803 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), cc->red_curve);
804 }
807 /**
808 * Concats red, blue and green.
809 * If any anchors are defined, process these, optionally removing curves from white list
810 * Invoke _flush_white to write result back to object.
811 */
812 static void
813 spcc_concat_colors_and_flush(SPConnectorContext *cc)
814 {
815 SPCurve *c = cc->green_curve;
816 cc->green_curve = sp_curve_new_sized(64);
818 sp_curve_reset(cc->red_curve);
819 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
821 if (sp_curve_empty(c)) {
822 sp_curve_unref(c);
823 return;
824 }
826 spcc_flush_white(cc, c);
828 sp_curve_unref(c);
829 }
832 /*
833 * Flushes white curve(s) and additional curve into object
834 *
835 * No cleaning of colored curves - this has to be done by caller
836 * No rereading of white data, so if you cannot rely on ::modified, do it in caller
837 *
838 */
840 static void
841 spcc_flush_white(SPConnectorContext *cc, SPCurve *gc)
842 {
843 SPCurve *c;
845 if (gc) {
846 c = gc;
847 sp_curve_ref(c);
848 } else {
849 return;
850 }
852 /* Now we have to go back to item coordinates at last */
853 sp_curve_transform(c,
854 sp_desktop_dt2root_affine(SP_EVENT_CONTEXT_DESKTOP(cc)));
856 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
857 SPDocument *doc = SP_DT_DOCUMENT(desktop);
859 if ( c && !sp_curve_empty(c) ) {
860 /* We actually have something to write */
862 Inkscape::XML::Node *repr = sp_repr_new("svg:path");
863 /* Set style */
864 sp_desktop_apply_style_tool(desktop, repr, "tools.connector", false);
866 gchar *str = sp_svg_write_path(SP_CURVE_BPATH(c));
867 g_assert( str != NULL );
868 repr->setAttribute("d", str);
869 g_free(str);
871 /* Attach repr */
872 cc->newconn = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
873 cc->selection->set(repr);
874 Inkscape::GC::release(repr);
875 cc->newconn->transform = i2i_affine(desktop->currentRoot(), desktop->currentLayer());
876 cc->newconn->updateRepr();
878 bool connection = false;
879 sp_object_setAttribute(cc->newconn, "inkscape:connector-type",
880 "polyline", false);
881 if (cc->sid)
882 {
883 sp_object_setAttribute(cc->newconn, "inkscape:connection-start",
884 cc->sid, false);
885 connection = true;
886 }
888 if (cc->eid)
889 {
890 sp_object_setAttribute(cc->newconn, "inkscape:connection-end",
891 cc->eid, false);
892 connection = true;
893 }
894 cc->newconn->updateRepr();
895 if (connection) {
896 // Adjust endpoints to shape edge.
897 sp_conn_adjust_path(SP_PATH(cc->newconn));
898 }
899 cc->newconn->updateRepr();
900 }
902 sp_curve_unref(c);
904 /* Flush pending updates */
905 sp_document_done(doc);
906 sp_document_ensure_up_to_date(doc);
907 }
910 static void
911 spcc_connector_finish_segment(SPConnectorContext *const cc, NR::Point const p)
912 {
913 if (!sp_curve_empty(cc->red_curve)) {
914 sp_curve_append_continuous(cc->green_curve, cc->red_curve, 0.0625);
916 cc->p[0] = cc->p[3];
917 cc->p[1] = cc->p[4];
918 cc->npoints = 2;
920 sp_curve_reset(cc->red_curve);
921 }
922 }
925 static void
926 spcc_connector_finish(SPConnectorContext *const cc)
927 {
928 SPDesktop *const desktop = cc->desktop;
929 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Finishing connector"));
931 sp_curve_reset(cc->red_curve);
932 spcc_concat_colors_and_flush(cc);
934 cc->npoints = 0;
936 if (cc->newConnRef) {
937 cc->newConnRef->removeFromGraph();
938 delete cc->newConnRef;
939 cc->newConnRef = NULL;
940 }
941 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
942 }
945 static gboolean
946 cc_generic_knot_handler(SPCanvasItem *, GdkEvent *event, SPKnot *knot)
947 {
948 g_assert (knot != NULL);
950 SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(
951 knot->desktop->event_context);
953 gboolean consumed = FALSE;
955 switch (event->type) {
956 case GDK_ENTER_NOTIFY:
957 gtk_object_set (GTK_OBJECT (knot->item), "fill_color",
958 knot->fill [SP_KNOT_STATE_MOUSEOVER], NULL);
959 gtk_object_set (GTK_OBJECT (knot->item), "stroke_color",
960 knot->stroke [SP_KNOT_STATE_MOUSEOVER], NULL);
962 cc->active_handle = knot;
964 if (knot->tip)
965 {
966 knot->desktop->event_context->defaultMessageContext()->set(
967 Inkscape::NORMAL_MESSAGE, knot->tip);
968 }
970 consumed = TRUE;
971 break;
972 case GDK_LEAVE_NOTIFY:
973 gtk_object_set (GTK_OBJECT (knot->item), "fill_color",
974 knot->fill [SP_KNOT_STATE_NORMAL], NULL);
975 gtk_object_set (GTK_OBJECT (knot->item), "stroke_color",
976 knot->stroke [SP_KNOT_STATE_NORMAL], NULL);
978 cc->active_handle = NULL;
980 if (knot->tip) {
981 knot->desktop->event_context->defaultMessageContext()->clear();
982 }
984 consumed = TRUE;
985 break;
986 default:
987 break;
988 }
990 return consumed;
991 }
994 static gboolean
995 endpt_handler(SPKnot *knot, GdkEvent *event, SPConnectorContext *cc)
996 {
997 g_assert( SP_IS_CONNECTOR_CONTEXT(cc) );
999 gboolean consumed = FALSE;
1001 switch (event->type) {
1002 case GDK_BUTTON_PRESS:
1003 g_assert( (cc->active_handle == cc->endpt_handle[0]) ||
1004 (cc->active_handle == cc->endpt_handle[1]) );
1005 if (cc->state == SP_CONNECTOR_CONTEXT_IDLE) {
1006 cc->clickeditem = cc->active_conn;
1007 cc->clickedhandle = cc->active_handle;
1008 cc_clear_active_conn(cc);
1009 cc->state = SP_CONNECTOR_CONTEXT_REROUTING;
1011 // Disconnect from attached shape
1012 unsigned ind = (cc->active_handle == cc->endpt_handle[0]) ? 0 : 1;
1013 sp_conn_end_detach(cc->clickeditem, ind);
1015 NR::Point origin;
1016 if (cc->clickedhandle == cc->endpt_handle[0]) {
1017 origin = cc->endpt_handle[1]->pos;
1018 }
1019 else {
1020 origin = cc->endpt_handle[0]->pos;
1021 }
1023 // Show the red path for dragging.
1024 cc->red_curve = sp_curve_copy(SP_PATH(cc->clickeditem)->curve);
1025 NR::Matrix i2d = sp_item_i2d_affine(cc->clickeditem);
1026 sp_curve_transform(cc->red_curve, i2d);
1027 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath),
1028 cc->red_curve);
1030 cc->clickeditem->setHidden(true);
1032 // The rest of the interaction rerouting the connector is
1033 // handled by the context root handler.
1034 consumed = TRUE;
1035 }
1036 break;
1037 default:
1038 break;
1039 }
1041 return consumed;
1042 }
1045 static void cc_set_active_shape(SPConnectorContext *cc, SPItem *item)
1046 {
1047 g_assert(item != NULL );
1049 cc->active_shape = item;
1051 // Remove existing active shape listeners
1052 if (cc->active_shape_repr) {
1053 sp_repr_remove_listener_by_data(cc->active_shape_repr, cc);
1054 Inkscape::GC::release(cc->active_shape_repr);
1056 sp_repr_remove_listener_by_data(cc->active_shape_layer_repr, cc);
1057 Inkscape::GC::release(cc->active_shape_layer_repr);
1058 }
1060 // Listen in case the active shape changes
1061 cc->active_shape_repr = SP_OBJECT_REPR(item);
1062 if (cc->active_shape_repr) {
1063 Inkscape::GC::anchor(cc->active_shape_repr);
1064 sp_repr_add_listener(cc->active_shape_repr, &shape_repr_events, cc);
1066 cc->active_shape_layer_repr = cc->active_shape_repr->parent();
1067 Inkscape::GC::anchor(cc->active_shape_layer_repr);
1068 sp_repr_add_listener(cc->active_shape_layer_repr, &layer_repr_events, cc);
1069 }
1072 // Set center connection point.
1073 if ( cc->connpthandle == NULL ) {
1074 SPKnot * knot = (SPKnot*)g_object_new (SP_TYPE_KNOT, 0);
1076 knot->desktop = cc->desktop;
1077 knot->flags = SP_KNOT_VISIBLE;
1079 knot->item = sp_canvas_item_new (SP_DT_CONTROLS(cc->desktop),
1080 SP_TYPE_CTRL,
1081 "anchor", GTK_ANCHOR_CENTER,
1082 "filled", TRUE,
1083 "stroked", TRUE,
1084 "mode", SP_KNOT_MODE_XOR,
1085 NULL);
1087 gtk_signal_connect (GTK_OBJECT (knot->item), "event",
1088 GTK_SIGNAL_FUNC (cc_generic_knot_handler), knot);
1090 knot->fill [SP_KNOT_STATE_NORMAL] = 0xffffff00;
1091 knot->fill [SP_KNOT_STATE_MOUSEOVER] = 0xff0000ff;
1092 knot->stroke [SP_KNOT_STATE_NORMAL] = 0x01000000;
1094 g_object_set(G_OBJECT(knot),
1095 "shape", SP_KNOT_SHAPE_SQUARE,
1096 "size", 8,
1097 "anchor", GTK_ANCHOR_CENTER,
1098 "tip", _("<b>Connection point</b>: click or drag to create a new connector"),
1099 NULL);
1101 cc->connpthandle = knot;
1102 }
1105 NR::Rect bbox = sp_item_bbox_desktop(cc->active_shape);
1106 NR::Point center = bbox.midpoint();
1107 sp_knot_set_position(cc->connpthandle, ¢er, 0);
1109 sp_knot_show(cc->connpthandle);
1111 }
1114 static void
1115 cc_set_active_conn(SPConnectorContext *cc, SPItem *item)
1116 {
1117 g_assert( SP_IS_PATH(item) );
1119 SPCurve *curve = SP_SHAPE(SP_PATH(item))->curve;
1120 NR::Matrix i2d = sp_item_i2d_affine(item);
1122 if (cc->active_conn == item)
1123 {
1124 // Just adjust handle positions.
1125 NR::Point startpt = sp_curve_first_point(curve) * i2d;
1126 sp_knot_set_position(cc->endpt_handle[0], &startpt, 0);
1128 NR::Point endpt = sp_curve_last_point(curve) * i2d;
1129 sp_knot_set_position(cc->endpt_handle[1], &endpt, 0);
1131 return;
1132 }
1134 cc->active_conn = item;
1136 // Remove existing active conn listeners
1137 if (cc->active_conn_repr) {
1138 sp_repr_remove_listener_by_data(cc->active_conn_repr, cc);
1139 Inkscape::GC::release(cc->active_conn_repr);
1140 cc->active_conn_repr = NULL;
1141 }
1143 // Listen in case the active conn changes
1144 cc->active_conn_repr = SP_OBJECT_REPR(item);
1145 if (cc->active_conn_repr) {
1146 Inkscape::GC::anchor(cc->active_conn_repr);
1147 sp_repr_add_listener(cc->active_conn_repr, &shape_repr_events, cc);
1148 }
1150 for (int i = 0; i < 2; ++i) {
1152 // Create the handle if it doesn't exist
1153 if ( cc->endpt_handle[i] == NULL ) {
1154 SPKnot * knot = (SPKnot*) g_object_new (SP_TYPE_KNOT, 0);
1156 knot->desktop = cc->desktop;
1157 knot->flags = SP_KNOT_VISIBLE;
1159 knot->item = sp_canvas_item_new (SP_DT_CONTROLS (cc->desktop),
1160 SP_TYPE_CTRL,
1161 "anchor", GTK_ANCHOR_CENTER,
1162 "filled", TRUE,
1163 "stroked", TRUE,
1164 "mode", SP_KNOT_MODE_XOR,
1165 NULL);
1167 knot->fill [SP_KNOT_STATE_NORMAL] = 0xffffff00;
1168 knot->stroke [SP_KNOT_STATE_NORMAL] = 0x000000ff;
1169 knot->stroke [SP_KNOT_STATE_DRAGGING] = 0x000000ff;
1170 knot->stroke [SP_KNOT_STATE_MOUSEOVER] = 0x000000ff;
1172 g_object_set(G_OBJECT(knot),
1173 "shape", SP_KNOT_SHAPE_DIAMOND,
1174 "size", 10,
1175 "tip", _("<b>Connector endpoint</b>: drag to reroute or connect to new shapes"),
1176 NULL);
1178 gtk_signal_connect (GTK_OBJECT (knot->item), "event",
1179 GTK_SIGNAL_FUNC (cc_generic_knot_handler), knot);
1181 cc->endpt_handle[i] = knot;
1182 }
1184 // Remove any existing handlers
1185 if (cc->endpt_handler_id[i]) {
1186 g_signal_handlers_disconnect_by_func(
1187 G_OBJECT(cc->endpt_handle[i]->item),
1188 (void*)G_CALLBACK(endpt_handler), (gpointer) cc );
1189 cc->endpt_handler_id[i] = 0;
1190 }
1192 // Setup handlers for connector endpoints, this is
1193 // is as 'after' so that cc_generic_knot_handler is
1194 // triggered first for any endpoint.
1195 cc->endpt_handler_id[i] = g_signal_connect_after(
1196 G_OBJECT(cc->endpt_handle[i]->item), "event",
1197 G_CALLBACK(endpt_handler), cc);
1198 }
1200 NR::Point startpt = sp_curve_first_point(curve) * i2d;
1201 sp_knot_set_position(cc->endpt_handle[0], &startpt, 0);
1203 NR::Point endpt = sp_curve_last_point(curve) * i2d;
1204 sp_knot_set_position(cc->endpt_handle[1], &endpt, 0);
1206 sp_knot_show(cc->endpt_handle[0]);
1207 sp_knot_show(cc->endpt_handle[1]);
1208 }
1211 static bool cc_item_is_shape(SPItem *item)
1212 {
1213 if (SP_IS_PATH(item)) {
1214 SPCurve *curve = (SP_SHAPE(item))->curve;
1215 if ( curve && !(curve->closed) ) {
1216 // Open paths are connectors.
1217 return false;
1218 }
1219 }
1220 return true;
1221 }
1224 bool cc_item_is_connector(SPItem *item)
1225 {
1226 if (SP_IS_PATH(item)) {
1227 if (SP_PATH(item)->connEndPair.isAutoRoutingConn()) {
1228 g_assert( !(SP_SHAPE(item)->curve->closed) );
1229 return true;
1230 }
1231 }
1232 return false;
1233 }
1236 void cc_selection_set_avoid(bool const set_avoid)
1237 {
1238 SPDesktop *desktop = inkscape_active_desktop();
1239 if (desktop == NULL) {
1240 return;
1241 }
1243 SPDocument *document = SP_DT_DOCUMENT(desktop);
1245 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
1247 GSList *l = (GSList *) selection->itemList();
1249 int changes = 0;
1251 while (l) {
1252 SPItem *item = (SPItem *) l->data;
1254 char const *value = (set_avoid) ? "true" : NULL;
1256 if (cc_item_is_shape(item)) {
1257 sp_object_setAttribute(item, "inkscape:connector-avoid",
1258 value, false);
1259 item->avoidRef->handleSettingChange();
1260 changes++;
1261 }
1263 l = l->next;
1264 }
1266 if (changes == 0) {
1267 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE,
1268 _("Select <b>at least one non-connector object</b>."));
1269 return;
1270 }
1272 sp_document_done(document);
1273 }
1276 static void
1277 cc_selection_changed(Inkscape::Selection *selection, gpointer data)
1278 {
1279 SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(data);
1280 //SPEventContext *ec = SP_EVENT_CONTEXT(cc);
1282 SPItem *item = selection->singleItem();
1284 if (cc->active_conn == item)
1285 {
1286 // Nothing to change.
1287 return;
1288 }
1289 if (item == NULL)
1290 {
1291 cc_clear_active_conn(cc);
1292 return;
1293 }
1295 if (cc_item_is_connector(item)) {
1296 cc_set_active_conn(cc, item);
1297 }
1298 }
1301 static void
1302 shape_event_attr_deleted(Inkscape::XML::Node *repr, Inkscape::XML::Node *child,
1303 Inkscape::XML::Node *ref, gpointer data)
1304 {
1305 g_assert(data);
1306 SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(data);
1308 if (child == cc->active_shape_repr) {
1309 // The active shape has been deleted. Clear active shape.
1310 cc_clear_active_shape(cc);
1311 }
1312 }
1315 static void
1316 shape_event_attr_changed(Inkscape::XML::Node *repr, gchar const *name,
1317 gchar const *old_value, gchar const *new_value,
1318 bool is_interactive, gpointer data)
1319 {
1320 g_assert(data);
1321 SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(data);
1323 // Look for changes than result in onscreen movement.
1324 if (!strcmp(name, "d") || !strcmp(name, "x") || !strcmp(name, "y") ||
1325 !strcmp(name, "width") || !strcmp(name, "height") ||
1326 !strcmp(name, "transform"))
1327 {
1328 if (repr == cc->active_shape_repr) {
1329 // Active shape has moved. Clear active shape.
1330 cc_clear_active_shape(cc);
1331 }
1332 else if (repr == cc->active_conn_repr) {
1333 // The active conn has been moved.
1334 // Set it again, which just sets new handle positions.
1335 cc_set_active_conn(cc, cc->active_conn);
1336 }
1337 }
1338 }
1341 /*
1342 Local Variables:
1343 mode:c++
1344 c-file-style:"stroustrup"
1345 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1346 indent-tabs-mode:nil
1347 fill-column:99
1348 End:
1349 */
1350 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :