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 }
283 // Make sure we see all enter events for canvas items,
284 // even if a mouse button is depressed.
285 dt->canvas->gen_all_enter_events = true;
286 }
289 static void
290 sp_connector_context_finish(SPEventContext *ec)
291 {
292 SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(ec);
294 spcc_connector_finish(cc);
296 if (((SPEventContextClass *) parent_class)->finish) {
297 ((SPEventContextClass *) parent_class)->finish(ec);
298 }
300 if (cc->selection) {
301 cc->selection = NULL;
302 }
303 cc_clear_active_shape(cc);
304 cc_clear_active_conn(cc);
306 // Restore the default event generating behaviour.
307 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(ec);
308 desktop->canvas->gen_all_enter_events = false;
309 }
312 //-----------------------------------------------------------------------------
315 static void
316 cc_clear_active_shape(SPConnectorContext *cc)
317 {
318 if (cc->active_shape == NULL) {
319 return;
320 }
321 g_assert( cc->active_shape_repr );
322 g_assert( cc->active_shape_layer_repr );
324 cc->active_shape = NULL;
326 if (cc->active_shape_repr) {
327 sp_repr_remove_listener_by_data(cc->active_shape_repr, cc);
328 Inkscape::GC::release(cc->active_shape_repr);
329 cc->active_shape_repr = NULL;
331 sp_repr_remove_listener_by_data(cc->active_shape_layer_repr, cc);
332 Inkscape::GC::release(cc->active_shape_layer_repr);
333 cc->active_shape_layer_repr = NULL;
334 }
336 // Hide the center connection point if it exists.
337 if (cc->connpthandle) {
338 sp_knot_hide(cc->connpthandle);
339 }
340 }
343 static void
344 cc_clear_active_conn(SPConnectorContext *cc)
345 {
346 if (cc->active_conn == NULL) {
347 return;
348 }
349 g_assert( cc->active_conn_repr );
351 cc->active_conn = NULL;
353 if (cc->active_conn_repr) {
354 sp_repr_remove_listener_by_data(cc->active_conn_repr, cc);
355 Inkscape::GC::release(cc->active_conn_repr);
356 cc->active_conn_repr = NULL;
357 }
359 // Hide the endpoint handles.
360 for (int i = 0; i < 2; ++i) {
361 if (cc->endpt_handle[i]) {
362 sp_knot_hide(cc->endpt_handle[i]);
363 }
364 }
365 }
368 static gchar *
369 conn_pt_handle_test(SPConnectorContext *cc, NR::Point& p)
370 {
371 // TODO: this will need to change when there are more connection
372 // points available for each shape.
374 SPKnot *centerpt = cc->connpthandle;
375 if (cc->active_handle && (cc->active_handle == centerpt))
376 {
377 p = centerpt->pos;
378 return g_strdup_printf("#%s", SP_OBJECT_ID(cc->active_shape));
379 }
380 return NULL;
381 }
385 static gint
386 sp_connector_context_item_handler(SPEventContext *ec, SPItem *item, GdkEvent *event)
387 {
388 gint ret = FALSE;
390 SPDesktop *desktop = ec->desktop;
392 SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(ec);
394 NR::Point p(event->button.x, event->button.y);
396 switch (event->type) {
397 case GDK_BUTTON_RELEASE:
398 if (event->button.button == 1) {
399 if ((cc->state == SP_CONNECTOR_CONTEXT_DRAGGING) &&
400 (connector_within_tolerance))
401 {
402 spcc_reset_colors(cc);
403 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
404 }
405 if (cc->state != SP_CONNECTOR_CONTEXT_IDLE) {
406 // Doing simething else like rerouting.
407 break;
408 }
409 // find out clicked item, disregarding groups, honoring Alt
410 SPItem *item_ungrouped = sp_event_context_find_item(desktop,
411 p, event->button.state & GDK_MOD1_MASK, TRUE);
413 if (event->button.state & GDK_SHIFT_MASK) {
414 cc->selection->toggle(item_ungrouped);
415 } else {
416 cc->selection->set(item_ungrouped);
417 }
418 ret = TRUE;
419 }
420 break;
421 case GDK_ENTER_NOTIFY:
422 {
423 if (cc_item_is_shape(item)) {
424 // This is a shape, so show connection point(s).
425 if (!(cc->active_shape) ||
426 // Don't show handle for another handle.
427 (item != ((SPItem *) cc->connpthandle))) {
428 cc_set_active_shape(cc, item);
429 }
430 }
431 ret = TRUE;
432 break;
433 }
434 default:
435 break;
436 }
438 return ret;
439 }
442 gint
443 sp_connector_context_root_handler(SPEventContext *ec, GdkEvent *event)
444 {
445 SPConnectorContext *const cc = SP_CONNECTOR_CONTEXT(ec);
447 gint ret = FALSE;
449 switch (event->type) {
450 case GDK_BUTTON_PRESS:
451 ret = connector_handle_button_press(cc, event->button);
452 break;
454 case GDK_MOTION_NOTIFY:
455 ret = connector_handle_motion_notify(cc, event->motion);
456 break;
458 case GDK_BUTTON_RELEASE:
459 ret = connector_handle_button_release(cc, event->button);
460 break;
461 case GDK_KEY_PRESS:
462 ret = connector_handle_key_press(cc, get_group0_keyval (&event->key));
463 break;
465 default:
466 break;
467 }
469 if (!ret) {
470 gint (*const parent_root_handler)(SPEventContext *, GdkEvent *)
471 = ((SPEventContextClass *) parent_class)->root_handler;
472 if (parent_root_handler) {
473 ret = parent_root_handler(ec, event);
474 }
475 }
477 return ret;
478 }
481 static gint
482 connector_handle_button_press(SPConnectorContext *const cc, GdkEventButton const &bevent)
483 {
484 NR::Point const event_w(bevent.x, bevent.y);
485 /* Find desktop coordinates */
486 NR::Point p = cc->desktop->w2d(event_w);
488 gint ret = FALSE;
489 if ( bevent.button == 1 ) {
491 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
493 if (Inkscape::have_viable_layer(desktop, cc->_message_context) == false) {
494 return TRUE;
495 }
497 NR::Point const event_w(bevent.x,
498 bevent.y);
499 connector_drag_origin_w = event_w;
500 connector_within_tolerance = true;
502 NR::Point const event_dt = cc->desktop->w2d(event_w);
503 switch (cc->state) {
504 case SP_CONNECTOR_CONTEXT_STOP:
505 /* This is allowed, if we just cancelled curve */
506 case SP_CONNECTOR_CONTEXT_IDLE:
507 {
508 if ( cc->npoints == 0 ) {
509 /* Set start anchor */
510 NR::Point p;
512 cc_clear_active_conn(cc);
514 SP_EVENT_CONTEXT_DESKTOP(cc)->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating new connector"));
516 /* Create green anchor */
517 p = event_dt;
519 // Test whether we clicked on a connection point
520 cc->sid = conn_pt_handle_test(cc, p);
522 if (!cc->sid) {
523 // This is the first point, so just snap it to the grid
524 // as there's no other points to go off.
525 SnapManager const m(cc->desktop->namedview);
526 p = m.freeSnap(Inkscape::Snapper::SNAP_POINT | Inkscape::Snapper::BBOX_POINT,
527 p, NULL).getPoint();
528 }
529 spcc_connector_set_initial_point(cc, p);
531 }
532 cc->state = SP_CONNECTOR_CONTEXT_DRAGGING;
533 ret = TRUE;
534 break;
535 }
536 case SP_CONNECTOR_CONTEXT_DRAGGING:
537 {
538 // This is the second click of a connector creation.
540 spcc_connector_set_subsequent_point(cc, p);
541 spcc_connector_finish_segment(cc, p);
542 // Test whether we clicked on a connection point
543 cc->eid = conn_pt_handle_test(cc, p);
544 if (cc->npoints != 0) {
545 spcc_connector_finish(cc);
546 }
547 cc_set_active_conn(cc, cc->newconn);
548 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
549 ret = TRUE;
550 break;
551 }
552 case SP_CONNECTOR_CONTEXT_CLOSE:
553 {
554 g_warning("Button down in CLOSE state");
555 break;
556 }
557 default:
558 break;
559 }
560 } else if (bevent.button == 3) {
561 if (cc->npoints != 0) {
562 spcc_connector_finish(cc);
563 ret = TRUE;
564 }
565 }
566 return ret;
567 }
570 static gint
571 connector_handle_motion_notify(SPConnectorContext *const cc, GdkEventMotion const &mevent)
572 {
573 gint ret = FALSE;
575 if (mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) {
576 // allow middle-button scrolling
577 return FALSE;
578 }
580 NR::Point const event_w(mevent.x, mevent.y);
582 if (connector_within_tolerance) {
583 gint const tolerance = prefs_get_int_attribute_limited("options.dragtolerance",
584 "value", 0, 0, 100);
585 if ( NR::LInfty( event_w - connector_drag_origin_w ) < tolerance ) {
586 return FALSE; // Do not drag if we're within tolerance from origin.
587 }
588 }
589 // Once the user has moved farther than tolerance from the original location
590 // (indicating they intend to move the object, not click), then always process
591 // the motion notify coordinates as given (no snapping back to origin)
592 connector_within_tolerance = false;
594 SPDesktop *const dt = cc->desktop;
596 /* Find desktop coordinates */
597 NR::Point p = dt->w2d(event_w);
599 switch (cc->state) {
600 case SP_CONNECTOR_CONTEXT_DRAGGING:
601 {
602 // This is movement during a connector creation.
604 if ( cc->npoints > 0 ) {
605 cc->selection->clear();
606 spcc_connector_set_subsequent_point(cc, p);
607 ret = TRUE;
608 }
609 break;
610 }
611 case SP_CONNECTOR_CONTEXT_REROUTING:
612 {
613 g_assert( SP_IS_PATH(cc->clickeditem));
615 // Update the hidden path
616 NR::Matrix i2d = sp_item_i2d_affine(cc->clickeditem);
617 NR::Matrix d2i = i2d.inverse();
618 SPPath *path = SP_PATH(cc->clickeditem);
619 SPCurve *curve = (SP_SHAPE(path))->curve;
620 if (cc->clickedhandle == cc->endpt_handle[0]) {
621 NR::Point o = cc->endpt_handle[1]->pos;
622 sp_curve_stretch_endpoints(curve, p * d2i, o * d2i);
623 }
624 else {
625 NR::Point o = cc->endpt_handle[0]->pos;
626 sp_curve_stretch_endpoints(curve, o * d2i, p * d2i);
627 }
628 sp_conn_adjust_path(path);
630 // Copy this to the temporary visible path
631 cc->red_curve = sp_curve_copy(SP_SHAPE(path)->curve);
632 sp_curve_transform(cc->red_curve, i2d);
634 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), cc->red_curve);
635 ret = TRUE;
636 break;
637 }
638 case SP_CONNECTOR_CONTEXT_STOP:
639 /* This is perfectly valid */
640 break;
641 default:
642 break;
643 }
645 return ret;
646 }
649 static gint
650 connector_handle_button_release(SPConnectorContext *const cc, GdkEventButton const &revent)
651 {
652 gint ret = FALSE;
653 if ( revent.button == 1 ) {
655 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
656 SPDocument *doc = SP_DT_DOCUMENT(desktop);
658 NR::Point const event_w(revent.x, revent.y);
660 /* Find desktop coordinates */
661 NR::Point p = cc->desktop->w2d(event_w);
663 switch (cc->state) {
664 //case SP_CONNECTOR_CONTEXT_POINT:
665 case SP_CONNECTOR_CONTEXT_DRAGGING:
666 {
667 if (connector_within_tolerance)
668 {
669 spcc_connector_finish_segment(cc, p);
670 return TRUE;
671 }
672 // Connector has been created via a drag, end it now.
673 spcc_connector_set_subsequent_point(cc, p);
674 spcc_connector_finish_segment(cc, p);
675 // Test whether we clicked on a connection point
676 cc->eid = conn_pt_handle_test(cc, p);
677 if (cc->npoints != 0) {
678 spcc_connector_finish(cc);
679 }
680 cc_set_active_conn(cc, cc->newconn);
681 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
682 break;
683 }
684 case SP_CONNECTOR_CONTEXT_REROUTING:
685 {
686 // Clear the temporary path:
687 sp_curve_reset(cc->red_curve);
688 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
690 // Test whether we clicked on a connection point
691 gchar *shape_label = conn_pt_handle_test(cc, p);
693 if (shape_label) {
694 if (cc->clickedhandle == cc->endpt_handle[0]) {
695 sp_object_setAttribute(cc->clickeditem,
696 "inkscape:connection-start",shape_label, false);
697 }
698 else {
699 sp_object_setAttribute(cc->clickeditem,
700 "inkscape:connection-end",shape_label, false);
701 }
702 g_free(shape_label);
703 }
704 cc->clickeditem->setHidden(false);
705 sp_conn_adjust_path(SP_PATH(cc->clickeditem));
706 cc->clickeditem->updateRepr();
707 sp_document_done(doc);
708 cc_set_active_conn(cc, cc->clickeditem);
709 sp_document_ensure_up_to_date(doc);
710 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
711 return TRUE;
712 break;
713 }
714 case SP_CONNECTOR_CONTEXT_STOP:
715 /* This is allowed, if we just cancelled curve */
716 break;
717 default:
718 break;
719 }
720 ret = TRUE;
721 }
723 return ret;
724 }
727 static gint
728 connector_handle_key_press(SPConnectorContext *const cc, guint const keyval)
729 {
730 gint ret = FALSE;
731 /* fixme: */
732 switch (keyval) {
733 case GDK_Return:
734 case GDK_KP_Enter:
735 if (cc->npoints != 0) {
736 spcc_connector_finish(cc);
737 ret = TRUE;
738 }
739 break;
740 case GDK_Escape:
741 if (cc->npoints != 0) {
742 // if drawing, cancel, otherwise pass it up for deselecting
743 cc->state = SP_CONNECTOR_CONTEXT_STOP;
744 spcc_reset_colors(cc);
745 ret = TRUE;
746 }
747 break;
748 default:
749 break;
750 }
751 return ret;
752 }
755 static void
756 spcc_reset_colors(SPConnectorContext *cc)
757 {
758 /* Red */
759 sp_curve_reset(cc->red_curve);
760 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
762 sp_curve_reset(cc->green_curve);
763 cc->npoints = 0;
764 }
767 static void
768 spcc_connector_set_initial_point(SPConnectorContext *const cc, NR::Point const p)
769 {
770 g_assert( cc->npoints == 0 );
772 cc->p[0] = p;
773 cc->p[1] = p;
774 cc->npoints = 2;
775 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
776 }
779 static void
780 spcc_connector_set_subsequent_point(SPConnectorContext *const cc, NR::Point const p)
781 {
782 g_assert( cc->npoints != 0 );
784 SPDesktop *dt = cc->desktop;
785 NR::Point o = dt->dt2doc(cc->p[0]);
786 NR::Point d = dt->dt2doc(p);
787 Avoid::Point src = { o[NR::X], o[NR::Y] };
788 Avoid::Point dst = { d[NR::X], d[NR::Y] };
790 if (!cc->newConnRef) {
791 Avoid::Router *router = SP_DT_DOCUMENT(dt)->router;
792 cc->newConnRef = new Avoid::ConnRef(router, 0, src, dst);
793 cc->newConnRef->updateEndPoint(Avoid::VertID::src, src);
794 }
795 cc->newConnRef->updateEndPoint(Avoid::VertID::tar, dst);
797 cc->newConnRef->makePathInvalid();
798 cc->newConnRef->generatePath(src, dst);
800 Avoid::PolyLine route = cc->newConnRef->route();
801 cc->newConnRef->calcRouteDist();
803 sp_curve_reset(cc->red_curve);
804 NR::Point pt(route.ps[0].x, route.ps[0].y);
805 sp_curve_moveto(cc->red_curve, pt);
807 for (int i = 1; i < route.pn; ++i) {
808 NR::Point p(route.ps[i].x, route.ps[i].y);
809 sp_curve_lineto(cc->red_curve, p);
810 }
811 sp_curve_transform(cc->red_curve, dt->doc2dt());
812 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), cc->red_curve);
813 }
816 /**
817 * Concats red, blue and green.
818 * If any anchors are defined, process these, optionally removing curves from white list
819 * Invoke _flush_white to write result back to object.
820 */
821 static void
822 spcc_concat_colors_and_flush(SPConnectorContext *cc)
823 {
824 SPCurve *c = cc->green_curve;
825 cc->green_curve = sp_curve_new_sized(64);
827 sp_curve_reset(cc->red_curve);
828 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
830 if (sp_curve_empty(c)) {
831 sp_curve_unref(c);
832 return;
833 }
835 spcc_flush_white(cc, c);
837 sp_curve_unref(c);
838 }
841 /*
842 * Flushes white curve(s) and additional curve into object
843 *
844 * No cleaning of colored curves - this has to be done by caller
845 * No rereading of white data, so if you cannot rely on ::modified, do it in caller
846 *
847 */
849 static void
850 spcc_flush_white(SPConnectorContext *cc, SPCurve *gc)
851 {
852 SPCurve *c;
854 if (gc) {
855 c = gc;
856 sp_curve_ref(c);
857 } else {
858 return;
859 }
861 /* Now we have to go back to item coordinates at last */
862 sp_curve_transform(c,
863 sp_desktop_dt2root_affine(SP_EVENT_CONTEXT_DESKTOP(cc)));
865 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
866 SPDocument *doc = SP_DT_DOCUMENT(desktop);
868 if ( c && !sp_curve_empty(c) ) {
869 /* We actually have something to write */
871 Inkscape::XML::Node *repr = sp_repr_new("svg:path");
872 /* Set style */
873 sp_desktop_apply_style_tool(desktop, repr, "tools.connector", false);
875 gchar *str = sp_svg_write_path(SP_CURVE_BPATH(c));
876 g_assert( str != NULL );
877 repr->setAttribute("d", str);
878 g_free(str);
880 /* Attach repr */
881 cc->newconn = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
882 cc->selection->set(repr);
883 Inkscape::GC::release(repr);
884 cc->newconn->transform = i2i_affine(desktop->currentRoot(), desktop->currentLayer());
885 cc->newconn->updateRepr();
887 bool connection = false;
888 sp_object_setAttribute(cc->newconn, "inkscape:connector-type",
889 "polyline", false);
890 if (cc->sid)
891 {
892 sp_object_setAttribute(cc->newconn, "inkscape:connection-start",
893 cc->sid, false);
894 connection = true;
895 }
897 if (cc->eid)
898 {
899 sp_object_setAttribute(cc->newconn, "inkscape:connection-end",
900 cc->eid, false);
901 connection = true;
902 }
903 cc->newconn->updateRepr();
904 if (connection) {
905 // Adjust endpoints to shape edge.
906 sp_conn_adjust_path(SP_PATH(cc->newconn));
907 }
908 cc->newconn->updateRepr();
909 }
911 sp_curve_unref(c);
913 /* Flush pending updates */
914 sp_document_done(doc);
915 sp_document_ensure_up_to_date(doc);
916 }
919 static void
920 spcc_connector_finish_segment(SPConnectorContext *const cc, NR::Point const p)
921 {
922 if (!sp_curve_empty(cc->red_curve)) {
923 sp_curve_append_continuous(cc->green_curve, cc->red_curve, 0.0625);
925 cc->p[0] = cc->p[3];
926 cc->p[1] = cc->p[4];
927 cc->npoints = 2;
929 sp_curve_reset(cc->red_curve);
930 }
931 }
934 static void
935 spcc_connector_finish(SPConnectorContext *const cc)
936 {
937 SPDesktop *const desktop = cc->desktop;
938 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Finishing connector"));
940 sp_curve_reset(cc->red_curve);
941 spcc_concat_colors_and_flush(cc);
943 cc->npoints = 0;
945 if (cc->newConnRef) {
946 cc->newConnRef->removeFromGraph();
947 delete cc->newConnRef;
948 cc->newConnRef = NULL;
949 }
950 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
951 }
954 static gboolean
955 cc_generic_knot_handler(SPCanvasItem *, GdkEvent *event, SPKnot *knot)
956 {
957 g_assert (knot != NULL);
959 SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(
960 knot->desktop->event_context);
962 gboolean consumed = FALSE;
964 switch (event->type) {
965 case GDK_ENTER_NOTIFY:
966 gtk_object_set (GTK_OBJECT (knot->item), "fill_color",
967 knot->fill [SP_KNOT_STATE_MOUSEOVER], NULL);
968 gtk_object_set (GTK_OBJECT (knot->item), "stroke_color",
969 knot->stroke [SP_KNOT_STATE_MOUSEOVER], NULL);
971 cc->active_handle = knot;
973 if (knot->tip)
974 {
975 knot->desktop->event_context->defaultMessageContext()->set(
976 Inkscape::NORMAL_MESSAGE, knot->tip);
977 }
979 consumed = TRUE;
980 break;
981 case GDK_LEAVE_NOTIFY:
982 gtk_object_set (GTK_OBJECT (knot->item), "fill_color",
983 knot->fill [SP_KNOT_STATE_NORMAL], NULL);
984 gtk_object_set (GTK_OBJECT (knot->item), "stroke_color",
985 knot->stroke [SP_KNOT_STATE_NORMAL], NULL);
987 cc->active_handle = NULL;
989 if (knot->tip) {
990 knot->desktop->event_context->defaultMessageContext()->clear();
991 }
993 consumed = TRUE;
994 break;
995 default:
996 break;
997 }
999 return consumed;
1000 }
1003 static gboolean
1004 endpt_handler(SPKnot *knot, GdkEvent *event, SPConnectorContext *cc)
1005 {
1006 g_assert( SP_IS_CONNECTOR_CONTEXT(cc) );
1008 gboolean consumed = FALSE;
1010 switch (event->type) {
1011 case GDK_BUTTON_PRESS:
1012 g_assert( (cc->active_handle == cc->endpt_handle[0]) ||
1013 (cc->active_handle == cc->endpt_handle[1]) );
1014 if (cc->state == SP_CONNECTOR_CONTEXT_IDLE) {
1015 cc->clickeditem = cc->active_conn;
1016 cc->clickedhandle = cc->active_handle;
1017 cc_clear_active_conn(cc);
1018 cc->state = SP_CONNECTOR_CONTEXT_REROUTING;
1020 // Disconnect from attached shape
1021 unsigned ind = (cc->active_handle == cc->endpt_handle[0]) ? 0 : 1;
1022 sp_conn_end_detach(cc->clickeditem, ind);
1024 NR::Point origin;
1025 if (cc->clickedhandle == cc->endpt_handle[0]) {
1026 origin = cc->endpt_handle[1]->pos;
1027 }
1028 else {
1029 origin = cc->endpt_handle[0]->pos;
1030 }
1032 // Show the red path for dragging.
1033 cc->red_curve = sp_curve_copy(SP_PATH(cc->clickeditem)->curve);
1034 NR::Matrix i2d = sp_item_i2d_affine(cc->clickeditem);
1035 sp_curve_transform(cc->red_curve, i2d);
1036 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath),
1037 cc->red_curve);
1039 cc->clickeditem->setHidden(true);
1041 // The rest of the interaction rerouting the connector is
1042 // handled by the context root handler.
1043 consumed = TRUE;
1044 }
1045 break;
1046 default:
1047 break;
1048 }
1050 return consumed;
1051 }
1054 static void cc_set_active_shape(SPConnectorContext *cc, SPItem *item)
1055 {
1056 g_assert(item != NULL );
1058 cc->active_shape = item;
1060 // Remove existing active shape listeners
1061 if (cc->active_shape_repr) {
1062 sp_repr_remove_listener_by_data(cc->active_shape_repr, cc);
1063 Inkscape::GC::release(cc->active_shape_repr);
1065 sp_repr_remove_listener_by_data(cc->active_shape_layer_repr, cc);
1066 Inkscape::GC::release(cc->active_shape_layer_repr);
1067 }
1069 // Listen in case the active shape changes
1070 cc->active_shape_repr = SP_OBJECT_REPR(item);
1071 if (cc->active_shape_repr) {
1072 Inkscape::GC::anchor(cc->active_shape_repr);
1073 sp_repr_add_listener(cc->active_shape_repr, &shape_repr_events, cc);
1075 cc->active_shape_layer_repr = cc->active_shape_repr->parent();
1076 Inkscape::GC::anchor(cc->active_shape_layer_repr);
1077 sp_repr_add_listener(cc->active_shape_layer_repr, &layer_repr_events, cc);
1078 }
1081 // Set center connection point.
1082 if ( cc->connpthandle == NULL ) {
1083 SPKnot * knot = (SPKnot*)g_object_new (SP_TYPE_KNOT, 0);
1085 knot->desktop = cc->desktop;
1086 knot->flags = SP_KNOT_VISIBLE;
1088 knot->item = sp_canvas_item_new (SP_DT_CONTROLS(cc->desktop),
1089 SP_TYPE_CTRL,
1090 "anchor", GTK_ANCHOR_CENTER,
1091 "filled", TRUE,
1092 "stroked", TRUE,
1093 "mode", SP_KNOT_MODE_XOR,
1094 NULL);
1096 gtk_signal_connect (GTK_OBJECT (knot->item), "event",
1097 GTK_SIGNAL_FUNC (cc_generic_knot_handler), knot);
1099 knot->fill [SP_KNOT_STATE_NORMAL] = 0xffffff00;
1100 knot->fill [SP_KNOT_STATE_MOUSEOVER] = 0xff0000ff;
1101 knot->stroke [SP_KNOT_STATE_NORMAL] = 0x01000000;
1103 g_object_set(G_OBJECT(knot),
1104 "shape", SP_KNOT_SHAPE_SQUARE,
1105 "size", 8,
1106 "anchor", GTK_ANCHOR_CENTER,
1107 "tip", _("<b>Connection point</b>: click or drag to create a new connector"),
1108 NULL);
1110 cc->connpthandle = knot;
1111 }
1114 NR::Rect bbox = sp_item_bbox_desktop(cc->active_shape);
1115 NR::Point center = bbox.midpoint();
1116 sp_knot_set_position(cc->connpthandle, ¢er, 0);
1118 sp_knot_show(cc->connpthandle);
1120 }
1123 static void
1124 cc_set_active_conn(SPConnectorContext *cc, SPItem *item)
1125 {
1126 g_assert( SP_IS_PATH(item) );
1128 SPCurve *curve = SP_SHAPE(SP_PATH(item))->curve;
1129 NR::Matrix i2d = sp_item_i2d_affine(item);
1131 if (cc->active_conn == item)
1132 {
1133 // Just adjust handle positions.
1134 NR::Point startpt = sp_curve_first_point(curve) * i2d;
1135 sp_knot_set_position(cc->endpt_handle[0], &startpt, 0);
1137 NR::Point endpt = sp_curve_last_point(curve) * i2d;
1138 sp_knot_set_position(cc->endpt_handle[1], &endpt, 0);
1140 return;
1141 }
1143 cc->active_conn = item;
1145 // Remove existing active conn listeners
1146 if (cc->active_conn_repr) {
1147 sp_repr_remove_listener_by_data(cc->active_conn_repr, cc);
1148 Inkscape::GC::release(cc->active_conn_repr);
1149 cc->active_conn_repr = NULL;
1150 }
1152 // Listen in case the active conn changes
1153 cc->active_conn_repr = SP_OBJECT_REPR(item);
1154 if (cc->active_conn_repr) {
1155 Inkscape::GC::anchor(cc->active_conn_repr);
1156 sp_repr_add_listener(cc->active_conn_repr, &shape_repr_events, cc);
1157 }
1159 for (int i = 0; i < 2; ++i) {
1161 // Create the handle if it doesn't exist
1162 if ( cc->endpt_handle[i] == NULL ) {
1163 SPKnot * knot = (SPKnot*) g_object_new (SP_TYPE_KNOT, 0);
1165 knot->desktop = cc->desktop;
1166 knot->flags = SP_KNOT_VISIBLE;
1168 knot->item = sp_canvas_item_new (SP_DT_CONTROLS (cc->desktop),
1169 SP_TYPE_CTRL,
1170 "anchor", GTK_ANCHOR_CENTER,
1171 "filled", TRUE,
1172 "stroked", TRUE,
1173 "mode", SP_KNOT_MODE_XOR,
1174 NULL);
1176 knot->fill [SP_KNOT_STATE_NORMAL] = 0xffffff00;
1177 knot->stroke [SP_KNOT_STATE_NORMAL] = 0x000000ff;
1178 knot->stroke [SP_KNOT_STATE_DRAGGING] = 0x000000ff;
1179 knot->stroke [SP_KNOT_STATE_MOUSEOVER] = 0x000000ff;
1181 g_object_set(G_OBJECT(knot),
1182 "shape", SP_KNOT_SHAPE_DIAMOND,
1183 "size", 10,
1184 "tip", _("<b>Connector endpoint</b>: drag to reroute or connect to new shapes"),
1185 NULL);
1187 gtk_signal_connect (GTK_OBJECT (knot->item), "event",
1188 GTK_SIGNAL_FUNC (cc_generic_knot_handler), knot);
1190 cc->endpt_handle[i] = knot;
1191 }
1193 // Remove any existing handlers
1194 if (cc->endpt_handler_id[i]) {
1195 g_signal_handlers_disconnect_by_func(
1196 G_OBJECT(cc->endpt_handle[i]->item),
1197 (void*)G_CALLBACK(endpt_handler), (gpointer) cc );
1198 cc->endpt_handler_id[i] = 0;
1199 }
1201 // Setup handlers for connector endpoints, this is
1202 // is as 'after' so that cc_generic_knot_handler is
1203 // triggered first for any endpoint.
1204 cc->endpt_handler_id[i] = g_signal_connect_after(
1205 G_OBJECT(cc->endpt_handle[i]->item), "event",
1206 G_CALLBACK(endpt_handler), cc);
1207 }
1209 NR::Point startpt = sp_curve_first_point(curve) * i2d;
1210 sp_knot_set_position(cc->endpt_handle[0], &startpt, 0);
1212 NR::Point endpt = sp_curve_last_point(curve) * i2d;
1213 sp_knot_set_position(cc->endpt_handle[1], &endpt, 0);
1215 sp_knot_show(cc->endpt_handle[0]);
1216 sp_knot_show(cc->endpt_handle[1]);
1217 }
1220 static bool cc_item_is_shape(SPItem *item)
1221 {
1222 if (SP_IS_PATH(item)) {
1223 SPCurve *curve = (SP_SHAPE(item))->curve;
1224 if ( curve && !(curve->closed) ) {
1225 // Open paths are connectors.
1226 return false;
1227 }
1228 }
1229 return true;
1230 }
1233 bool cc_item_is_connector(SPItem *item)
1234 {
1235 if (SP_IS_PATH(item)) {
1236 if (SP_PATH(item)->connEndPair.isAutoRoutingConn()) {
1237 g_assert( !(SP_SHAPE(item)->curve->closed) );
1238 return true;
1239 }
1240 }
1241 return false;
1242 }
1245 void cc_selection_set_avoid(bool const set_avoid)
1246 {
1247 SPDesktop *desktop = inkscape_active_desktop();
1248 if (desktop == NULL) {
1249 return;
1250 }
1252 SPDocument *document = SP_DT_DOCUMENT(desktop);
1254 Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
1256 GSList *l = (GSList *) selection->itemList();
1258 int changes = 0;
1260 while (l) {
1261 SPItem *item = (SPItem *) l->data;
1263 char const *value = (set_avoid) ? "true" : NULL;
1265 if (cc_item_is_shape(item)) {
1266 sp_object_setAttribute(item, "inkscape:connector-avoid",
1267 value, false);
1268 item->avoidRef->handleSettingChange();
1269 changes++;
1270 }
1272 l = l->next;
1273 }
1275 if (changes == 0) {
1276 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE,
1277 _("Select <b>at least one non-connector object</b>."));
1278 return;
1279 }
1281 sp_document_done(document);
1282 }
1285 static void
1286 cc_selection_changed(Inkscape::Selection *selection, gpointer data)
1287 {
1288 SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(data);
1289 //SPEventContext *ec = SP_EVENT_CONTEXT(cc);
1291 SPItem *item = selection->singleItem();
1293 if (cc->active_conn == item)
1294 {
1295 // Nothing to change.
1296 return;
1297 }
1298 if (item == NULL)
1299 {
1300 cc_clear_active_conn(cc);
1301 return;
1302 }
1304 if (cc_item_is_connector(item)) {
1305 cc_set_active_conn(cc, item);
1306 }
1307 }
1310 static void
1311 shape_event_attr_deleted(Inkscape::XML::Node *repr, Inkscape::XML::Node *child,
1312 Inkscape::XML::Node *ref, gpointer data)
1313 {
1314 g_assert(data);
1315 SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(data);
1317 if (child == cc->active_shape_repr) {
1318 // The active shape has been deleted. Clear active shape.
1319 cc_clear_active_shape(cc);
1320 }
1321 }
1324 static void
1325 shape_event_attr_changed(Inkscape::XML::Node *repr, gchar const *name,
1326 gchar const *old_value, gchar const *new_value,
1327 bool is_interactive, gpointer data)
1328 {
1329 g_assert(data);
1330 SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(data);
1332 // Look for changes than result in onscreen movement.
1333 if (!strcmp(name, "d") || !strcmp(name, "x") || !strcmp(name, "y") ||
1334 !strcmp(name, "width") || !strcmp(name, "height") ||
1335 !strcmp(name, "transform"))
1336 {
1337 if (repr == cc->active_shape_repr) {
1338 // Active shape has moved. Clear active shape.
1339 cc_clear_active_shape(cc);
1340 }
1341 else if (repr == cc->active_conn_repr) {
1342 // The active conn has been moved.
1343 // Set it again, which just sets new handle positions.
1344 cc_set_active_conn(cc, cc->active_conn);
1345 }
1346 }
1347 }
1350 /*
1351 Local Variables:
1352 mode:c++
1353 c-file-style:"stroustrup"
1354 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1355 indent-tabs-mode:nil
1356 fill-column:99
1357 End:
1358 */
1359 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :