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 convex 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 Allow user to change a object between a path and connector through
21 * the interface.
22 * o Create an interface for setting markers (arrow heads).
23 * o Better distinguish between paths and connectors to prevent problems
24 * in the node tool and paths accidently being turned into connectors
25 * in the connector tool. Perhaps have a way to convert between.
26 * o Only call libavoid's updateEndPoint as required. Currently we do it
27 * for both endpoints, even if only one is moving.
28 * o Allow user-placeable connection points.
29 * o Deal sanely with connectors with both endpoints attached to the
30 * same connection point, and drawing of connectors attaching
31 * overlaping shapes (currently tries to adjust connector to be
32 * outside both bounding boxes).
33 * o Fix many special cases related to connectors updating,
34 * e.g., copying a couple of shapes and a connector that are
35 * attached to each other.
36 * e.g., detach connector when it is moved or transformed in
37 * one of the other contexts.
38 * o Cope with shapes whose ids change when they have attached
39 * connectors.
40 * o gobble_motion_events(GDK_BUTTON1_MASK)?;
41 *
42 */
44 #include <gdk/gdkkeysyms.h>
46 #include "connector-context.h"
47 #include "pixmaps/cursor-connector.xpm"
48 #include "xml/node-event-vector.h"
49 #include "xml/repr.h"
50 #include "svg/svg.h"
51 #include "desktop.h"
52 #include "desktop-style.h"
53 #include "desktop-affine.h"
54 #include "desktop-handles.h"
55 #include "document.h"
56 #include "message-context.h"
57 #include "message-stack.h"
58 #include "selection.h"
59 #include "inkscape.h"
60 #include "prefs-utils.h"
61 #include "sp-path.h"
62 #include "display/canvas-bpath.h"
63 #include "display/sodipodi-ctrl.h"
64 #include <glibmm/i18n.h>
65 #include "snap.h"
66 #include "knot.h"
67 #include "sp-conn-end.h"
68 #include "conn-avoid-ref.h"
69 #include "libavoid/vertices.h"
70 #include "context-fns.h"
71 #include "sp-namedview.h"
72 #include "sp-text.h"
73 #include "sp-flowtext.h"
76 static void sp_connector_context_class_init(SPConnectorContextClass *klass);
77 static void sp_connector_context_init(SPConnectorContext *conn_context);
78 static void sp_connector_context_dispose(GObject *object);
80 static void sp_connector_context_setup(SPEventContext *ec);
81 static void sp_connector_context_finish(SPEventContext *ec);
82 static gint sp_connector_context_root_handler(SPEventContext *ec, GdkEvent *event);
83 static gint sp_connector_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
85 // Stuff borrowed from DrawContext
86 static void spcc_connector_set_initial_point(SPConnectorContext *cc, NR::Point const p);
87 static void spcc_connector_set_subsequent_point(SPConnectorContext *cc, NR::Point const p);
88 static void spcc_connector_finish_segment(SPConnectorContext *cc, NR::Point p);
89 static void spcc_reset_colors(SPConnectorContext *cc);
90 static void spcc_connector_finish(SPConnectorContext *cc);
91 static void spcc_concat_colors_and_flush(SPConnectorContext *cc);
92 static void spcc_flush_white(SPConnectorContext *cc, SPCurve *gc);
94 // Context event handlers
95 static gint connector_handle_button_press(SPConnectorContext *const cc, GdkEventButton const &bevent);
96 static gint connector_handle_motion_notify(SPConnectorContext *const cc, GdkEventMotion const &mevent);
97 static gint connector_handle_button_release(SPConnectorContext *const cc, GdkEventButton const &revent);
98 static gint connector_handle_key_press(SPConnectorContext *const cc, guint const keyval);
100 static void cc_set_active_shape(SPConnectorContext *cc, SPItem *item);
101 static void cc_clear_active_shape(SPConnectorContext *cc);
102 static void cc_set_active_conn(SPConnectorContext *cc, SPItem *item);
103 static void cc_clear_active_conn(SPConnectorContext *cc);
104 static gchar *conn_pt_handle_test(SPConnectorContext *cc, NR::Point& w);
105 static bool cc_item_is_shape(SPItem *item);
106 static void cc_selection_changed(Inkscape::Selection *selection, gpointer data);
107 static void cc_connector_rerouting_finish(SPConnectorContext *const cc,
108 NR::Point *const p);
110 static void shape_event_attr_deleted(Inkscape::XML::Node *repr,
111 Inkscape::XML::Node *child, Inkscape::XML::Node *ref, gpointer data);
112 static void shape_event_attr_changed(Inkscape::XML::Node *repr, gchar const *name,
113 gchar const *old_value, gchar const *new_value, bool is_interactive,
114 gpointer data);
117 static NR::Point connector_drag_origin_w(0, 0);
118 static bool connector_within_tolerance = false;
119 static SPEventContextClass *parent_class;
122 static Inkscape::XML::NodeEventVector shape_repr_events = {
123 NULL, /* child_added */
124 NULL, /* child_added */
125 shape_event_attr_changed,
126 NULL, /* content_changed */
127 NULL /* order_changed */
128 };
130 static Inkscape::XML::NodeEventVector layer_repr_events = {
131 NULL, /* child_added */
132 shape_event_attr_deleted,
133 NULL, /* child_added */
134 NULL, /* content_changed */
135 NULL /* order_changed */
136 };
139 GType
140 sp_connector_context_get_type(void)
141 {
142 static GType type = 0;
143 if (!type) {
144 GTypeInfo info = {
145 sizeof(SPConnectorContextClass),
146 NULL, NULL,
147 (GClassInitFunc) sp_connector_context_class_init,
148 NULL, NULL,
149 sizeof(SPConnectorContext),
150 4,
151 (GInstanceInitFunc) sp_connector_context_init,
152 NULL, /* value_table */
153 };
154 type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPConnectorContext", &info, (GTypeFlags)0);
155 }
156 return type;
157 }
159 static void
160 sp_connector_context_class_init(SPConnectorContextClass *klass)
161 {
162 GObjectClass *object_class;
163 SPEventContextClass *event_context_class;
165 object_class = (GObjectClass *) klass;
166 event_context_class = (SPEventContextClass *) klass;
168 parent_class = (SPEventContextClass*)g_type_class_peek_parent(klass);
170 object_class->dispose = sp_connector_context_dispose;
172 event_context_class->setup = sp_connector_context_setup;
173 event_context_class->finish = sp_connector_context_finish;
174 event_context_class->root_handler = sp_connector_context_root_handler;
175 event_context_class->item_handler = sp_connector_context_item_handler;
176 }
179 static void
180 sp_connector_context_init(SPConnectorContext *cc)
181 {
182 SPEventContext *ec = SP_EVENT_CONTEXT(cc);
184 ec->cursor_shape = cursor_connector_xpm;
185 ec->hot_x = 1;
186 ec->hot_y = 1;
187 ec->xp = 0;
188 ec->yp = 0;
190 cc->red_color = 0xff00007f;
192 cc->newconn = NULL;
193 cc->newConnRef = NULL;
195 cc->sel_changed_connection = sigc::connection();
197 cc->active_shape = NULL;
198 cc->active_shape_repr = NULL;
199 cc->active_shape_layer_repr = NULL;
201 cc->active_conn = NULL;
202 cc->active_conn_repr = NULL;
204 cc->active_handle = NULL;
206 cc->clickeditem = NULL;
207 cc->clickedhandle = NULL;
209 cc->connpthandle = NULL;
210 for (int i = 0; i < 2; ++i) {
211 cc->endpt_handle[i] = NULL;
212 cc->endpt_handler_id[i] = 0;
213 }
214 cc->sid = NULL;
215 cc->eid = NULL;
216 cc->npoints = 0;
217 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
218 }
221 static void
222 sp_connector_context_dispose(GObject *object)
223 {
224 SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(object);
226 cc->sel_changed_connection.disconnect();
228 if (cc->connpthandle) {
229 g_object_unref(cc->connpthandle);
230 cc->connpthandle = NULL;
231 }
232 for (int i = 0; i < 2; ++i) {
233 if (cc->endpt_handle[1]) {
234 g_object_unref(cc->endpt_handle[i]);
235 cc->endpt_handle[i] = NULL;
236 }
237 }
238 if (cc->sid) {
239 g_free(cc->sid);
240 cc->sid = NULL;
241 }
242 if (cc->eid) {
243 g_free(cc->eid);
244 cc->eid = NULL;
245 }
246 g_assert( cc->newConnRef == NULL );
248 G_OBJECT_CLASS(parent_class)->dispose(object);
249 }
252 static void
253 sp_connector_context_setup(SPEventContext *ec)
254 {
255 SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(ec);
256 SPDesktop *dt = ec->desktop;
258 if (((SPEventContextClass *) parent_class)->setup) {
259 ((SPEventContextClass *) parent_class)->setup(ec);
260 }
262 cc->selection = sp_desktop_selection(dt);
264 cc->sel_changed_connection.disconnect();
265 cc->sel_changed_connection = cc->selection->connectChanged(
266 sigc::bind(sigc::ptr_fun(&cc_selection_changed),
267 (gpointer) cc));
269 /* Create red bpath */
270 cc->red_bpath = sp_canvas_bpath_new(sp_desktop_sketch(ec->desktop), NULL);
271 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cc->red_bpath), cc->red_color,
272 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
273 sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(cc->red_bpath), 0x00000000,
274 SP_WIND_RULE_NONZERO);
275 /* Create red curve */
276 cc->red_curve = sp_curve_new_sized(4);
278 /* Create green curve */
279 cc->green_curve = sp_curve_new_sized(64);
281 // Notice the initial selection.
282 cc_selection_changed(cc->selection, (gpointer) cc);
284 if (prefs_get_int_attribute("tools.connector", "selcue", 0) != 0) {
285 ec->enableSelectionCue();
286 }
288 // Make sure we see all enter events for canvas items,
289 // even if a mouse button is depressed.
290 dt->canvas->gen_all_enter_events = true;
291 }
294 static void
295 sp_connector_context_finish(SPEventContext *ec)
296 {
297 SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(ec);
299 spcc_connector_finish(cc);
301 if (((SPEventContextClass *) parent_class)->finish) {
302 ((SPEventContextClass *) parent_class)->finish(ec);
303 }
305 if (cc->selection) {
306 cc->selection = NULL;
307 }
308 cc_clear_active_shape(cc);
309 cc_clear_active_conn(cc);
311 // Restore the default event generating behaviour.
312 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(ec);
313 desktop->canvas->gen_all_enter_events = false;
314 }
317 //-----------------------------------------------------------------------------
320 static void
321 cc_clear_active_shape(SPConnectorContext *cc)
322 {
323 if (cc->active_shape == NULL) {
324 return;
325 }
326 g_assert( cc->active_shape_repr );
327 g_assert( cc->active_shape_layer_repr );
329 cc->active_shape = NULL;
331 if (cc->active_shape_repr) {
332 sp_repr_remove_listener_by_data(cc->active_shape_repr, cc);
333 Inkscape::GC::release(cc->active_shape_repr);
334 cc->active_shape_repr = NULL;
336 sp_repr_remove_listener_by_data(cc->active_shape_layer_repr, cc);
337 Inkscape::GC::release(cc->active_shape_layer_repr);
338 cc->active_shape_layer_repr = NULL;
339 }
341 // Hide the center connection point if it exists.
342 if (cc->connpthandle) {
343 sp_knot_hide(cc->connpthandle);
344 }
345 }
348 static void
349 cc_clear_active_conn(SPConnectorContext *cc)
350 {
351 if (cc->active_conn == NULL) {
352 return;
353 }
354 g_assert( cc->active_conn_repr );
356 cc->active_conn = NULL;
358 if (cc->active_conn_repr) {
359 sp_repr_remove_listener_by_data(cc->active_conn_repr, cc);
360 Inkscape::GC::release(cc->active_conn_repr);
361 cc->active_conn_repr = NULL;
362 }
364 // Hide the endpoint handles.
365 for (int i = 0; i < 2; ++i) {
366 if (cc->endpt_handle[i]) {
367 sp_knot_hide(cc->endpt_handle[i]);
368 }
369 }
370 }
373 static gchar *
374 conn_pt_handle_test(SPConnectorContext *cc, NR::Point& p)
375 {
376 // TODO: this will need to change when there are more connection
377 // points available for each shape.
379 SPKnot *centerpt = cc->connpthandle;
380 if (cc->active_handle && (cc->active_handle == centerpt))
381 {
382 p = centerpt->pos;
383 return g_strdup_printf("#%s", SP_OBJECT_ID(cc->active_shape));
384 }
385 return NULL;
386 }
390 static gint
391 sp_connector_context_item_handler(SPEventContext *ec, SPItem *item, GdkEvent *event)
392 {
393 gint ret = FALSE;
395 SPDesktop *desktop = ec->desktop;
397 SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(ec);
399 NR::Point p(event->button.x, event->button.y);
401 switch (event->type) {
402 case GDK_BUTTON_RELEASE:
403 if (event->button.button == 1) {
404 if ((cc->state == SP_CONNECTOR_CONTEXT_DRAGGING) &&
405 (connector_within_tolerance))
406 {
407 spcc_reset_colors(cc);
408 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
409 }
410 if (cc->state != SP_CONNECTOR_CONTEXT_IDLE) {
411 // Doing simething else like rerouting.
412 break;
413 }
414 // find out clicked item, disregarding groups, honoring Alt
415 SPItem *item_ungrouped = sp_event_context_find_item(desktop,
416 p, event->button.state & GDK_MOD1_MASK, TRUE);
418 if (event->button.state & GDK_SHIFT_MASK) {
419 cc->selection->toggle(item_ungrouped);
420 } else {
421 cc->selection->set(item_ungrouped);
422 }
423 ret = TRUE;
424 }
425 break;
426 case GDK_ENTER_NOTIFY:
427 {
428 if (cc_item_is_shape(item)) {
429 // This is a shape, so show connection point(s).
430 if (!(cc->active_shape) ||
431 // Don't show handle for another handle.
432 (item != ((SPItem *) cc->connpthandle))) {
433 cc_set_active_shape(cc, item);
434 }
435 }
436 ret = TRUE;
437 break;
438 }
439 default:
440 break;
441 }
443 return ret;
444 }
447 gint
448 sp_connector_context_root_handler(SPEventContext *ec, GdkEvent *event)
449 {
450 SPConnectorContext *const cc = SP_CONNECTOR_CONTEXT(ec);
452 gint ret = FALSE;
454 switch (event->type) {
455 case GDK_BUTTON_PRESS:
456 ret = connector_handle_button_press(cc, event->button);
457 break;
459 case GDK_MOTION_NOTIFY:
460 ret = connector_handle_motion_notify(cc, event->motion);
461 break;
463 case GDK_BUTTON_RELEASE:
464 ret = connector_handle_button_release(cc, event->button);
465 break;
466 case GDK_KEY_PRESS:
467 ret = connector_handle_key_press(cc, get_group0_keyval (&event->key));
468 break;
470 default:
471 break;
472 }
474 if (!ret) {
475 gint (*const parent_root_handler)(SPEventContext *, GdkEvent *)
476 = ((SPEventContextClass *) parent_class)->root_handler;
477 if (parent_root_handler) {
478 ret = parent_root_handler(ec, event);
479 }
480 }
482 return ret;
483 }
486 static gint
487 connector_handle_button_press(SPConnectorContext *const cc, GdkEventButton const &bevent)
488 {
489 NR::Point const event_w(bevent.x, bevent.y);
490 /* Find desktop coordinates */
491 NR::Point p = cc->desktop->w2d(event_w);
493 gint ret = FALSE;
494 if ( bevent.button == 1 ) {
496 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
498 if (Inkscape::have_viable_layer(desktop, cc->_message_context) == false) {
499 return TRUE;
500 }
502 NR::Point const event_w(bevent.x,
503 bevent.y);
504 connector_drag_origin_w = event_w;
505 connector_within_tolerance = true;
507 NR::Point const event_dt = cc->desktop->w2d(event_w);
508 switch (cc->state) {
509 case SP_CONNECTOR_CONTEXT_STOP:
510 /* This is allowed, if we just cancelled curve */
511 case SP_CONNECTOR_CONTEXT_IDLE:
512 {
513 if ( cc->npoints == 0 ) {
514 /* Set start anchor */
515 NR::Point p;
517 cc_clear_active_conn(cc);
519 SP_EVENT_CONTEXT_DESKTOP(cc)->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating new connector"));
521 /* Create green anchor */
522 p = event_dt;
524 // Test whether we clicked on a connection point
525 cc->sid = conn_pt_handle_test(cc, p);
527 if (!cc->sid) {
528 // This is the first point, so just snap it to the grid
529 // as there's no other points to go off.
530 SnapManager const &m = cc->desktop->namedview->snap_manager;
531 p = m.freeSnap(Inkscape::Snapper::SNAP_POINT | Inkscape::Snapper::BBOX_POINT,
532 p, NULL).getPoint();
533 }
534 spcc_connector_set_initial_point(cc, p);
536 }
537 cc->state = SP_CONNECTOR_CONTEXT_DRAGGING;
538 ret = TRUE;
539 break;
540 }
541 case SP_CONNECTOR_CONTEXT_DRAGGING:
542 {
543 // This is the second click of a connector creation.
545 spcc_connector_set_subsequent_point(cc, p);
546 spcc_connector_finish_segment(cc, p);
547 // Test whether we clicked on a connection point
548 cc->eid = conn_pt_handle_test(cc, p);
549 if (cc->npoints != 0) {
550 spcc_connector_finish(cc);
551 }
552 cc_set_active_conn(cc, cc->newconn);
553 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
554 ret = TRUE;
555 break;
556 }
557 case SP_CONNECTOR_CONTEXT_CLOSE:
558 {
559 g_warning("Button down in CLOSE state");
560 break;
561 }
562 default:
563 break;
564 }
565 } else if (bevent.button == 3) {
566 if (cc->state == SP_CONNECTOR_CONTEXT_REROUTING) {
567 // A context menu is going to be triggered here,
568 // so end the rerouting operation.
569 cc_connector_rerouting_finish(cc, &p);
571 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
573 // Don't set ret to TRUE, so we drop through to the
574 // parent handler which will open the context menu.
575 }
576 else if (cc->npoints != 0) {
577 spcc_connector_finish(cc);
578 ret = TRUE;
579 }
580 }
581 return ret;
582 }
585 static gint
586 connector_handle_motion_notify(SPConnectorContext *const cc, GdkEventMotion const &mevent)
587 {
588 gint ret = FALSE;
590 if (mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) {
591 // allow middle-button scrolling
592 return FALSE;
593 }
595 NR::Point const event_w(mevent.x, mevent.y);
597 if (connector_within_tolerance) {
598 gint const tolerance = prefs_get_int_attribute_limited("options.dragtolerance",
599 "value", 0, 0, 100);
600 if ( NR::LInfty( event_w - connector_drag_origin_w ) < tolerance ) {
601 return FALSE; // Do not drag if we're within tolerance from origin.
602 }
603 }
604 // Once the user has moved farther than tolerance from the original location
605 // (indicating they intend to move the object, not click), then always process
606 // the motion notify coordinates as given (no snapping back to origin)
607 connector_within_tolerance = false;
609 SPDesktop *const dt = cc->desktop;
611 /* Find desktop coordinates */
612 NR::Point p = dt->w2d(event_w);
614 switch (cc->state) {
615 case SP_CONNECTOR_CONTEXT_DRAGGING:
616 {
617 // This is movement during a connector creation.
619 if ( cc->npoints > 0 ) {
620 cc->selection->clear();
621 spcc_connector_set_subsequent_point(cc, p);
622 ret = TRUE;
623 }
624 break;
625 }
626 case SP_CONNECTOR_CONTEXT_REROUTING:
627 {
628 g_assert( SP_IS_PATH(cc->clickeditem));
630 // Update the hidden path
631 NR::Matrix i2d = sp_item_i2d_affine(cc->clickeditem);
632 NR::Matrix d2i = i2d.inverse();
633 SPPath *path = SP_PATH(cc->clickeditem);
634 SPCurve *curve = (SP_SHAPE(path))->curve;
635 if (cc->clickedhandle == cc->endpt_handle[0]) {
636 NR::Point o = cc->endpt_handle[1]->pos;
637 sp_curve_stretch_endpoints(curve, p * d2i, o * d2i);
638 }
639 else {
640 NR::Point o = cc->endpt_handle[0]->pos;
641 sp_curve_stretch_endpoints(curve, o * d2i, p * d2i);
642 }
643 sp_conn_adjust_path(path);
645 // Copy this to the temporary visible path
646 cc->red_curve = sp_curve_copy(SP_SHAPE(path)->curve);
647 sp_curve_transform(cc->red_curve, i2d);
649 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), cc->red_curve);
650 ret = TRUE;
651 break;
652 }
653 case SP_CONNECTOR_CONTEXT_STOP:
654 /* This is perfectly valid */
655 break;
656 default:
657 break;
658 }
660 return ret;
661 }
664 static gint
665 connector_handle_button_release(SPConnectorContext *const cc, GdkEventButton const &revent)
666 {
667 gint ret = FALSE;
668 if ( revent.button == 1 ) {
670 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
671 SPDocument *doc = sp_desktop_document(desktop);
673 NR::Point const event_w(revent.x, revent.y);
675 /* Find desktop coordinates */
676 NR::Point p = cc->desktop->w2d(event_w);
678 switch (cc->state) {
679 //case SP_CONNECTOR_CONTEXT_POINT:
680 case SP_CONNECTOR_CONTEXT_DRAGGING:
681 {
682 if (connector_within_tolerance)
683 {
684 spcc_connector_finish_segment(cc, p);
685 return TRUE;
686 }
687 // Connector has been created via a drag, end it now.
688 spcc_connector_set_subsequent_point(cc, p);
689 spcc_connector_finish_segment(cc, p);
690 // Test whether we clicked on a connection point
691 cc->eid = conn_pt_handle_test(cc, p);
692 if (cc->npoints != 0) {
693 spcc_connector_finish(cc);
694 }
695 cc_set_active_conn(cc, cc->newconn);
696 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
697 break;
698 }
699 case SP_CONNECTOR_CONTEXT_REROUTING:
700 {
701 cc_connector_rerouting_finish(cc, &p);
703 sp_document_ensure_up_to_date(doc);
704 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
705 return TRUE;
706 break;
707 }
708 case SP_CONNECTOR_CONTEXT_STOP:
709 /* This is allowed, if we just cancelled curve */
710 break;
711 default:
712 break;
713 }
714 ret = TRUE;
715 }
717 return ret;
718 }
721 static gint
722 connector_handle_key_press(SPConnectorContext *const cc, guint const keyval)
723 {
724 gint ret = FALSE;
725 /* fixme: */
726 switch (keyval) {
727 case GDK_Return:
728 case GDK_KP_Enter:
729 if (cc->npoints != 0) {
730 spcc_connector_finish(cc);
731 ret = TRUE;
732 }
733 break;
734 case GDK_Escape:
735 if (cc->state == SP_CONNECTOR_CONTEXT_REROUTING) {
737 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
738 SPDocument *doc = sp_desktop_document(desktop);
740 cc_connector_rerouting_finish(cc, NULL);
742 sp_document_undo(doc);
744 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
745 desktop->messageStack()->flash( Inkscape::NORMAL_MESSAGE,
746 _("Connector endpoint drag canceled."));
747 ret = TRUE;
748 }
749 else if (cc->npoints != 0) {
750 // if drawing, cancel, otherwise pass it up for deselecting
751 cc->state = SP_CONNECTOR_CONTEXT_STOP;
752 spcc_reset_colors(cc);
753 ret = TRUE;
754 }
755 break;
756 default:
757 break;
758 }
759 return ret;
760 }
763 static void
764 cc_connector_rerouting_finish(SPConnectorContext *const cc, NR::Point *const p)
765 {
766 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
767 SPDocument *doc = sp_desktop_document(desktop);
769 // Clear the temporary path:
770 sp_curve_reset(cc->red_curve);
771 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
773 if (p != NULL)
774 {
775 // Test whether we clicked on a connection point
776 gchar *shape_label = conn_pt_handle_test(cc, *p);
778 if (shape_label) {
779 if (cc->clickedhandle == cc->endpt_handle[0]) {
780 sp_object_setAttribute(cc->clickeditem,
781 "inkscape:connection-start",shape_label, false);
782 }
783 else {
784 sp_object_setAttribute(cc->clickeditem,
785 "inkscape:connection-end",shape_label, false);
786 }
787 g_free(shape_label);
788 }
789 }
790 cc->clickeditem->setHidden(false);
791 sp_conn_adjust_path(SP_PATH(cc->clickeditem));
792 cc->clickeditem->updateRepr();
793 sp_document_done(doc, SP_VERB_CONTEXT_CONNECTOR,
794 _("Reroute connector"));
795 cc_set_active_conn(cc, cc->clickeditem);
796 }
799 static void
800 spcc_reset_colors(SPConnectorContext *cc)
801 {
802 /* Red */
803 sp_curve_reset(cc->red_curve);
804 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
806 sp_curve_reset(cc->green_curve);
807 cc->npoints = 0;
808 }
811 static void
812 spcc_connector_set_initial_point(SPConnectorContext *const cc, NR::Point const p)
813 {
814 g_assert( cc->npoints == 0 );
816 cc->p[0] = p;
817 cc->p[1] = p;
818 cc->npoints = 2;
819 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
820 }
823 static void
824 spcc_connector_set_subsequent_point(SPConnectorContext *const cc, NR::Point const p)
825 {
826 g_assert( cc->npoints != 0 );
828 SPDesktop *dt = cc->desktop;
829 NR::Point o = dt->dt2doc(cc->p[0]);
830 NR::Point d = dt->dt2doc(p);
831 Avoid::Point src(o[NR::X], o[NR::Y]);
832 Avoid::Point dst(d[NR::X], d[NR::Y]);
834 if (!cc->newConnRef) {
835 Avoid::Router *router = sp_desktop_document(dt)->router;
836 cc->newConnRef = new Avoid::ConnRef(router, 0, src, dst);
837 cc->newConnRef->updateEndPoint(Avoid::VertID::src, src);
838 }
839 cc->newConnRef->updateEndPoint(Avoid::VertID::tar, dst);
841 cc->newConnRef->makePathInvalid();
842 cc->newConnRef->generatePath(src, dst);
844 Avoid::PolyLine route = cc->newConnRef->route();
845 cc->newConnRef->calcRouteDist();
847 sp_curve_reset(cc->red_curve);
848 NR::Point pt(route.ps[0].x, route.ps[0].y);
849 sp_curve_moveto(cc->red_curve, pt);
851 for (int i = 1; i < route.pn; ++i) {
852 NR::Point p(route.ps[i].x, route.ps[i].y);
853 sp_curve_lineto(cc->red_curve, p);
854 }
855 sp_curve_transform(cc->red_curve, dt->doc2dt());
856 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), cc->red_curve);
857 }
860 /**
861 * Concats red, blue and green.
862 * If any anchors are defined, process these, optionally removing curves from white list
863 * Invoke _flush_white to write result back to object.
864 */
865 static void
866 spcc_concat_colors_and_flush(SPConnectorContext *cc)
867 {
868 SPCurve *c = cc->green_curve;
869 cc->green_curve = sp_curve_new_sized(64);
871 sp_curve_reset(cc->red_curve);
872 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
874 if (sp_curve_empty(c)) {
875 sp_curve_unref(c);
876 return;
877 }
879 spcc_flush_white(cc, c);
881 sp_curve_unref(c);
882 }
885 /*
886 * Flushes white curve(s) and additional curve into object
887 *
888 * No cleaning of colored curves - this has to be done by caller
889 * No rereading of white data, so if you cannot rely on ::modified, do it in caller
890 *
891 */
893 static void
894 spcc_flush_white(SPConnectorContext *cc, SPCurve *gc)
895 {
896 SPCurve *c;
898 if (gc) {
899 c = gc;
900 sp_curve_ref(c);
901 } else {
902 return;
903 }
905 /* Now we have to go back to item coordinates at last */
906 sp_curve_transform(c,
907 sp_desktop_dt2root_affine(SP_EVENT_CONTEXT_DESKTOP(cc)));
909 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
910 SPDocument *doc = sp_desktop_document(desktop);
911 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
913 if ( c && !sp_curve_empty(c) ) {
914 /* We actually have something to write */
916 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
917 /* Set style */
918 sp_desktop_apply_style_tool(desktop, repr, "tools.connector", false);
920 gchar *str = sp_svg_write_path(SP_CURVE_BPATH(c));
921 g_assert( str != NULL );
922 repr->setAttribute("d", str);
923 g_free(str);
925 /* Attach repr */
926 cc->newconn = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
927 cc->selection->set(repr);
928 Inkscape::GC::release(repr);
929 cc->newconn->transform = i2i_affine(desktop->currentRoot(), desktop->currentLayer());
930 cc->newconn->updateRepr();
932 bool connection = false;
933 sp_object_setAttribute(cc->newconn, "inkscape:connector-type",
934 "polyline", false);
935 if (cc->sid)
936 {
937 sp_object_setAttribute(cc->newconn, "inkscape:connection-start",
938 cc->sid, false);
939 connection = true;
940 }
942 if (cc->eid)
943 {
944 sp_object_setAttribute(cc->newconn, "inkscape:connection-end",
945 cc->eid, false);
946 connection = true;
947 }
948 cc->newconn->updateRepr();
949 if (connection) {
950 // Adjust endpoints to shape edge.
951 sp_conn_adjust_path(SP_PATH(cc->newconn));
952 }
953 cc->newconn->updateRepr();
954 }
956 sp_curve_unref(c);
958 /* Flush pending updates */
959 sp_document_done(doc, SP_VERB_CONTEXT_CONNECTOR, _("Create connector"));
960 sp_document_ensure_up_to_date(doc);
961 }
964 static void
965 spcc_connector_finish_segment(SPConnectorContext *const cc, NR::Point const p)
966 {
967 if (!sp_curve_empty(cc->red_curve)) {
968 sp_curve_append_continuous(cc->green_curve, cc->red_curve, 0.0625);
970 cc->p[0] = cc->p[3];
971 cc->p[1] = cc->p[4];
972 cc->npoints = 2;
974 sp_curve_reset(cc->red_curve);
975 }
976 }
979 static void
980 spcc_connector_finish(SPConnectorContext *const cc)
981 {
982 SPDesktop *const desktop = cc->desktop;
983 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Finishing connector"));
985 sp_curve_reset(cc->red_curve);
986 spcc_concat_colors_and_flush(cc);
988 cc->npoints = 0;
990 if (cc->newConnRef) {
991 cc->newConnRef->removeFromGraph();
992 delete cc->newConnRef;
993 cc->newConnRef = NULL;
994 }
995 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
996 }
999 static gboolean
1000 cc_generic_knot_handler(SPCanvasItem *, GdkEvent *event, SPKnot *knot)
1001 {
1002 g_assert (knot != NULL);
1004 g_object_ref(knot);
1006 SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(
1007 knot->desktop->event_context);
1009 gboolean consumed = FALSE;
1011 switch (event->type) {
1012 case GDK_ENTER_NOTIFY:
1013 sp_knot_set_flag(knot, SP_KNOT_MOUSEOVER, TRUE);
1015 cc->active_handle = knot;
1017 if (knot->tip)
1018 {
1019 knot->desktop->event_context->defaultMessageContext()->set(
1020 Inkscape::NORMAL_MESSAGE, knot->tip);
1021 }
1023 consumed = TRUE;
1024 break;
1025 case GDK_LEAVE_NOTIFY:
1026 sp_knot_set_flag(knot, SP_KNOT_MOUSEOVER, FALSE);
1028 cc->active_handle = NULL;
1030 if (knot->tip) {
1031 knot->desktop->event_context->defaultMessageContext()->clear();
1032 }
1034 consumed = TRUE;
1035 break;
1036 default:
1037 break;
1038 }
1040 g_object_unref(knot);
1042 return consumed;
1043 }
1046 static gboolean
1047 endpt_handler(SPKnot *knot, GdkEvent *event, SPConnectorContext *cc)
1048 {
1049 g_assert( SP_IS_CONNECTOR_CONTEXT(cc) );
1051 gboolean consumed = FALSE;
1053 switch (event->type) {
1054 case GDK_BUTTON_PRESS:
1055 g_assert( (cc->active_handle == cc->endpt_handle[0]) ||
1056 (cc->active_handle == cc->endpt_handle[1]) );
1057 if (cc->state == SP_CONNECTOR_CONTEXT_IDLE) {
1058 cc->clickeditem = cc->active_conn;
1059 cc->clickedhandle = cc->active_handle;
1060 cc_clear_active_conn(cc);
1061 cc->state = SP_CONNECTOR_CONTEXT_REROUTING;
1063 // Disconnect from attached shape
1064 unsigned ind = (cc->active_handle == cc->endpt_handle[0]) ? 0 : 1;
1065 sp_conn_end_detach(cc->clickeditem, ind);
1067 NR::Point origin;
1068 if (cc->clickedhandle == cc->endpt_handle[0]) {
1069 origin = cc->endpt_handle[1]->pos;
1070 }
1071 else {
1072 origin = cc->endpt_handle[0]->pos;
1073 }
1075 // Show the red path for dragging.
1076 cc->red_curve = sp_curve_copy(SP_PATH(cc->clickeditem)->curve);
1077 NR::Matrix i2d = sp_item_i2d_affine(cc->clickeditem);
1078 sp_curve_transform(cc->red_curve, i2d);
1079 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath),
1080 cc->red_curve);
1082 cc->clickeditem->setHidden(true);
1084 // The rest of the interaction rerouting the connector is
1085 // handled by the context root handler.
1086 consumed = TRUE;
1087 }
1088 break;
1089 default:
1090 break;
1091 }
1093 return consumed;
1094 }
1097 static void cc_set_active_shape(SPConnectorContext *cc, SPItem *item)
1098 {
1099 g_assert(item != NULL );
1101 cc->active_shape = item;
1103 // Remove existing active shape listeners
1104 if (cc->active_shape_repr) {
1105 sp_repr_remove_listener_by_data(cc->active_shape_repr, cc);
1106 Inkscape::GC::release(cc->active_shape_repr);
1108 sp_repr_remove_listener_by_data(cc->active_shape_layer_repr, cc);
1109 Inkscape::GC::release(cc->active_shape_layer_repr);
1110 }
1112 // Listen in case the active shape changes
1113 cc->active_shape_repr = SP_OBJECT_REPR(item);
1114 if (cc->active_shape_repr) {
1115 Inkscape::GC::anchor(cc->active_shape_repr);
1116 sp_repr_add_listener(cc->active_shape_repr, &shape_repr_events, cc);
1118 cc->active_shape_layer_repr = cc->active_shape_repr->parent();
1119 Inkscape::GC::anchor(cc->active_shape_layer_repr);
1120 sp_repr_add_listener(cc->active_shape_layer_repr, &layer_repr_events, cc);
1121 }
1124 // Set center connection point.
1125 if ( cc->connpthandle == NULL ) {
1126 SPKnot *knot = sp_knot_new(cc->desktop,
1127 _("<b>Connection point</b>: click or drag to create a new connector"));
1129 knot->setShape(SP_KNOT_SHAPE_SQUARE);
1130 knot->setSize(8);
1131 knot->setAnchor(GTK_ANCHOR_CENTER);
1132 knot->setFill(0xffffff00, 0xff0000ff, 0xff0000ff);
1133 sp_knot_update_ctrl(knot);
1135 // We don't want to use the standard knot handler,
1136 //since we don't want this knot to be draggable.
1137 g_signal_handler_disconnect(G_OBJECT(knot->item),
1138 knot->_event_handler_id);
1139 knot->_event_handler_id = 0;
1141 gtk_signal_connect(GTK_OBJECT(knot->item), "event",
1142 GTK_SIGNAL_FUNC(cc_generic_knot_handler), knot);
1144 cc->connpthandle = knot;
1145 }
1148 NR::Rect bbox = sp_item_bbox_desktop(cc->active_shape);
1149 NR::Point center = bbox.midpoint();
1150 sp_knot_set_position(cc->connpthandle, ¢er, 0);
1152 sp_knot_show(cc->connpthandle);
1154 }
1157 static void
1158 cc_set_active_conn(SPConnectorContext *cc, SPItem *item)
1159 {
1160 g_assert( SP_IS_PATH(item) );
1162 SPCurve *curve = SP_SHAPE(SP_PATH(item))->curve;
1163 NR::Matrix i2d = sp_item_i2d_affine(item);
1165 if (cc->active_conn == item)
1166 {
1167 // Just adjust handle positions.
1168 NR::Point startpt = sp_curve_first_point(curve) * i2d;
1169 sp_knot_set_position(cc->endpt_handle[0], &startpt, 0);
1171 NR::Point endpt = sp_curve_last_point(curve) * i2d;
1172 sp_knot_set_position(cc->endpt_handle[1], &endpt, 0);
1174 return;
1175 }
1177 cc->active_conn = item;
1179 // Remove existing active conn listeners
1180 if (cc->active_conn_repr) {
1181 sp_repr_remove_listener_by_data(cc->active_conn_repr, cc);
1182 Inkscape::GC::release(cc->active_conn_repr);
1183 cc->active_conn_repr = NULL;
1184 }
1186 // Listen in case the active conn changes
1187 cc->active_conn_repr = SP_OBJECT_REPR(item);
1188 if (cc->active_conn_repr) {
1189 Inkscape::GC::anchor(cc->active_conn_repr);
1190 sp_repr_add_listener(cc->active_conn_repr, &shape_repr_events, cc);
1191 }
1193 for (int i = 0; i < 2; ++i) {
1195 // Create the handle if it doesn't exist
1196 if ( cc->endpt_handle[i] == NULL ) {
1197 SPKnot *knot = sp_knot_new(cc->desktop,
1198 _("<b>Connector endpoint</b>: drag to reroute or connect to new shapes"));
1200 knot->setShape(SP_KNOT_SHAPE_SQUARE);
1201 knot->setSize(7);
1202 knot->setAnchor(GTK_ANCHOR_CENTER);
1203 knot->setFill(0xffffff00, 0xff0000ff, 0xff0000ff);
1204 knot->setStroke(0x000000ff, 0x000000ff, 0x000000ff);
1205 sp_knot_update_ctrl(knot);
1207 // We don't want to use the standard knot handler,
1208 //since we don't want this knot to be draggable.
1209 g_signal_handler_disconnect(G_OBJECT(knot->item),
1210 knot->_event_handler_id);
1211 knot->_event_handler_id = 0;
1213 gtk_signal_connect(GTK_OBJECT(knot->item), "event",
1214 GTK_SIGNAL_FUNC(cc_generic_knot_handler), knot);
1216 cc->endpt_handle[i] = knot;
1217 }
1219 // Remove any existing handlers
1220 if (cc->endpt_handler_id[i]) {
1221 g_signal_handlers_disconnect_by_func(
1222 G_OBJECT(cc->endpt_handle[i]->item),
1223 (void*)G_CALLBACK(endpt_handler), (gpointer) cc );
1224 cc->endpt_handler_id[i] = 0;
1225 }
1227 // Setup handlers for connector endpoints, this is
1228 // is as 'after' so that cc_generic_knot_handler is
1229 // triggered first for any endpoint.
1230 cc->endpt_handler_id[i] = g_signal_connect_after(
1231 G_OBJECT(cc->endpt_handle[i]->item), "event",
1232 G_CALLBACK(endpt_handler), cc);
1233 }
1235 NR::Point startpt = sp_curve_first_point(curve) * i2d;
1236 sp_knot_set_position(cc->endpt_handle[0], &startpt, 0);
1238 NR::Point endpt = sp_curve_last_point(curve) * i2d;
1239 sp_knot_set_position(cc->endpt_handle[1], &endpt, 0);
1241 sp_knot_show(cc->endpt_handle[0]);
1242 sp_knot_show(cc->endpt_handle[1]);
1243 }
1246 static bool cc_item_is_shape(SPItem *item)
1247 {
1248 if (SP_IS_PATH(item)) {
1249 SPCurve *curve = (SP_SHAPE(item))->curve;
1250 if ( curve && !(curve->closed) ) {
1251 // Open paths are connectors.
1252 return false;
1253 }
1254 }
1255 else if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
1256 if (prefs_get_int_attribute("tools.connector", "ignoretext", 1) == 1) {
1257 // Don't count text as a shape we can connect connector to.
1258 return false;
1259 }
1260 }
1261 return true;
1262 }
1265 bool cc_item_is_connector(SPItem *item)
1266 {
1267 if (SP_IS_PATH(item)) {
1268 if (SP_PATH(item)->connEndPair.isAutoRoutingConn()) {
1269 g_assert( !(SP_SHAPE(item)->curve->closed) );
1270 return true;
1271 }
1272 }
1273 return false;
1274 }
1277 void cc_selection_set_avoid(bool const set_avoid)
1278 {
1279 SPDesktop *desktop = inkscape_active_desktop();
1280 if (desktop == NULL) {
1281 return;
1282 }
1284 SPDocument *document = sp_desktop_document(desktop);
1286 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1288 GSList *l = (GSList *) selection->itemList();
1290 int changes = 0;
1292 while (l) {
1293 SPItem *item = (SPItem *) l->data;
1295 char const *value = (set_avoid) ? "true" : NULL;
1297 if (cc_item_is_shape(item)) {
1298 sp_object_setAttribute(item, "inkscape:connector-avoid",
1299 value, false);
1300 item->avoidRef->handleSettingChange();
1301 changes++;
1302 }
1304 l = l->next;
1305 }
1307 if (changes == 0) {
1308 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE,
1309 _("Select <b>at least one non-connector object</b>."));
1310 return;
1311 }
1313 char *event_desc = (set_avoid) ?
1314 _("Make connectors avoid selected objects") :
1315 _("Make connectors ignore selected objects");
1316 sp_document_done(document, SP_VERB_CONTEXT_CONNECTOR, event_desc);
1317 }
1320 static void
1321 cc_selection_changed(Inkscape::Selection *selection, gpointer data)
1322 {
1323 SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(data);
1324 //SPEventContext *ec = SP_EVENT_CONTEXT(cc);
1326 SPItem *item = selection->singleItem();
1328 if (cc->active_conn == item)
1329 {
1330 // Nothing to change.
1331 return;
1332 }
1333 if (item == NULL)
1334 {
1335 cc_clear_active_conn(cc);
1336 return;
1337 }
1339 if (cc_item_is_connector(item)) {
1340 cc_set_active_conn(cc, item);
1341 }
1342 }
1345 static void
1346 shape_event_attr_deleted(Inkscape::XML::Node *repr, Inkscape::XML::Node *child,
1347 Inkscape::XML::Node *ref, gpointer data)
1348 {
1349 g_assert(data);
1350 SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(data);
1352 if (child == cc->active_shape_repr) {
1353 // The active shape has been deleted. Clear active shape.
1354 cc_clear_active_shape(cc);
1355 }
1356 }
1359 static void
1360 shape_event_attr_changed(Inkscape::XML::Node *repr, gchar const *name,
1361 gchar const *old_value, gchar const *new_value,
1362 bool is_interactive, gpointer data)
1363 {
1364 g_assert(data);
1365 SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(data);
1367 // Look for changes than result in onscreen movement.
1368 if (!strcmp(name, "d") || !strcmp(name, "x") || !strcmp(name, "y") ||
1369 !strcmp(name, "width") || !strcmp(name, "height") ||
1370 !strcmp(name, "transform"))
1371 {
1372 if (repr == cc->active_shape_repr) {
1373 // Active shape has moved. Clear active shape.
1374 cc_clear_active_shape(cc);
1375 }
1376 else if (repr == cc->active_conn_repr) {
1377 // The active conn has been moved.
1378 // Set it again, which just sets new handle positions.
1379 cc_set_active_conn(cc, cc->active_conn);
1380 }
1381 }
1382 }
1385 /*
1386 Local Variables:
1387 mode:c++
1388 c-file-style:"stroustrup"
1389 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1390 indent-tabs-mode:nil
1391 fill-column:99
1392 End:
1393 */
1394 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :