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 *event_context, SPItem *item, GdkEvent *event)
392 {
393 gint ret = FALSE;
395 SPDesktop *desktop = event_context->desktop;
397 SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(event_context);
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 && !event_context->space_panning) {
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);
492 SPEventContext *event_context = SP_EVENT_CONTEXT(cc);
494 gint ret = FALSE;
495 if ( bevent.button == 1 && !event_context->space_panning ) {
497 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
499 if (Inkscape::have_viable_layer(desktop, cc->_message_context) == false) {
500 return TRUE;
501 }
503 NR::Point const event_w(bevent.x,
504 bevent.y);
505 connector_drag_origin_w = event_w;
506 connector_within_tolerance = true;
508 NR::Point const event_dt = cc->desktop->w2d(event_w);
509 switch (cc->state) {
510 case SP_CONNECTOR_CONTEXT_STOP:
511 /* This is allowed, if we just cancelled curve */
512 case SP_CONNECTOR_CONTEXT_IDLE:
513 {
514 if ( cc->npoints == 0 ) {
515 /* Set start anchor */
516 NR::Point p;
518 cc_clear_active_conn(cc);
520 SP_EVENT_CONTEXT_DESKTOP(cc)->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating new connector"));
522 /* Create green anchor */
523 p = event_dt;
525 // Test whether we clicked on a connection point
526 cc->sid = conn_pt_handle_test(cc, p);
528 if (!cc->sid) {
529 // This is the first point, so just snap it to the grid
530 // as there's no other points to go off.
531 SnapManager const &m = cc->desktop->namedview->snap_manager;
532 p = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE,
533 p, NULL).getPoint();
534 }
535 spcc_connector_set_initial_point(cc, p);
537 }
538 cc->state = SP_CONNECTOR_CONTEXT_DRAGGING;
539 ret = TRUE;
540 break;
541 }
542 case SP_CONNECTOR_CONTEXT_DRAGGING:
543 {
544 // This is the second click of a connector creation.
546 spcc_connector_set_subsequent_point(cc, p);
547 spcc_connector_finish_segment(cc, p);
548 // Test whether we clicked on a connection point
549 cc->eid = conn_pt_handle_test(cc, p);
550 if (cc->npoints != 0) {
551 spcc_connector_finish(cc);
552 }
553 cc_set_active_conn(cc, cc->newconn);
554 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
555 ret = TRUE;
556 break;
557 }
558 case SP_CONNECTOR_CONTEXT_CLOSE:
559 {
560 g_warning("Button down in CLOSE state");
561 break;
562 }
563 default:
564 break;
565 }
566 } else if (bevent.button == 3) {
567 if (cc->state == SP_CONNECTOR_CONTEXT_REROUTING) {
568 // A context menu is going to be triggered here,
569 // so end the rerouting operation.
570 cc_connector_rerouting_finish(cc, &p);
572 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
574 // Don't set ret to TRUE, so we drop through to the
575 // parent handler which will open the context menu.
576 }
577 else if (cc->npoints != 0) {
578 spcc_connector_finish(cc);
579 ret = TRUE;
580 }
581 }
582 return ret;
583 }
586 static gint
587 connector_handle_motion_notify(SPConnectorContext *const cc, GdkEventMotion const &mevent)
588 {
589 gint ret = FALSE;
590 SPEventContext *event_context = SP_EVENT_CONTEXT(cc);
592 if (event_context->space_panning || mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) {
593 // allow middle-button scrolling
594 return FALSE;
595 }
597 NR::Point const event_w(mevent.x, mevent.y);
599 if (connector_within_tolerance) {
600 gint const tolerance = prefs_get_int_attribute_limited("options.dragtolerance",
601 "value", 0, 0, 100);
602 if ( NR::LInfty( event_w - connector_drag_origin_w ) < tolerance ) {
603 return FALSE; // Do not drag if we're within tolerance from origin.
604 }
605 }
606 // Once the user has moved farther than tolerance from the original location
607 // (indicating they intend to move the object, not click), then always process
608 // the motion notify coordinates as given (no snapping back to origin)
609 connector_within_tolerance = false;
611 SPDesktop *const dt = cc->desktop;
613 /* Find desktop coordinates */
614 NR::Point p = dt->w2d(event_w);
616 switch (cc->state) {
617 case SP_CONNECTOR_CONTEXT_DRAGGING:
618 {
619 // This is movement during a connector creation.
621 if ( cc->npoints > 0 ) {
622 cc->selection->clear();
623 spcc_connector_set_subsequent_point(cc, p);
624 ret = TRUE;
625 }
626 break;
627 }
628 case SP_CONNECTOR_CONTEXT_REROUTING:
629 {
630 g_assert( SP_IS_PATH(cc->clickeditem));
632 // Update the hidden path
633 NR::Matrix i2d = sp_item_i2d_affine(cc->clickeditem);
634 NR::Matrix d2i = i2d.inverse();
635 SPPath *path = SP_PATH(cc->clickeditem);
636 SPCurve *curve = (SP_SHAPE(path))->curve;
637 if (cc->clickedhandle == cc->endpt_handle[0]) {
638 NR::Point o = cc->endpt_handle[1]->pos;
639 sp_curve_stretch_endpoints(curve, p * d2i, o * d2i);
640 }
641 else {
642 NR::Point o = cc->endpt_handle[0]->pos;
643 sp_curve_stretch_endpoints(curve, o * d2i, p * d2i);
644 }
645 sp_conn_adjust_path(path);
647 // Copy this to the temporary visible path
648 cc->red_curve = sp_curve_copy(SP_SHAPE(path)->curve);
649 sp_curve_transform(cc->red_curve, i2d);
651 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), cc->red_curve);
652 ret = TRUE;
653 break;
654 }
655 case SP_CONNECTOR_CONTEXT_STOP:
656 /* This is perfectly valid */
657 break;
658 default:
659 break;
660 }
662 return ret;
663 }
666 static gint
667 connector_handle_button_release(SPConnectorContext *const cc, GdkEventButton const &revent)
668 {
669 gint ret = FALSE;
670 SPEventContext *event_context = SP_EVENT_CONTEXT(cc);
671 if ( revent.button == 1 && !event_context->space_panning ) {
673 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
674 SPDocument *doc = sp_desktop_document(desktop);
676 NR::Point const event_w(revent.x, revent.y);
678 /* Find desktop coordinates */
679 NR::Point p = cc->desktop->w2d(event_w);
681 switch (cc->state) {
682 //case SP_CONNECTOR_CONTEXT_POINT:
683 case SP_CONNECTOR_CONTEXT_DRAGGING:
684 {
685 if (connector_within_tolerance)
686 {
687 spcc_connector_finish_segment(cc, p);
688 return TRUE;
689 }
690 // Connector has been created via a drag, end it now.
691 spcc_connector_set_subsequent_point(cc, p);
692 spcc_connector_finish_segment(cc, p);
693 // Test whether we clicked on a connection point
694 cc->eid = conn_pt_handle_test(cc, p);
695 if (cc->npoints != 0) {
696 spcc_connector_finish(cc);
697 }
698 cc_set_active_conn(cc, cc->newconn);
699 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
700 break;
701 }
702 case SP_CONNECTOR_CONTEXT_REROUTING:
703 {
704 cc_connector_rerouting_finish(cc, &p);
706 sp_document_ensure_up_to_date(doc);
707 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
708 return TRUE;
709 break;
710 }
711 case SP_CONNECTOR_CONTEXT_STOP:
712 /* This is allowed, if we just cancelled curve */
713 break;
714 default:
715 break;
716 }
717 ret = TRUE;
718 }
720 return ret;
721 }
724 static gint
725 connector_handle_key_press(SPConnectorContext *const cc, guint const keyval)
726 {
727 gint ret = FALSE;
728 /* fixme: */
729 switch (keyval) {
730 case GDK_Return:
731 case GDK_KP_Enter:
732 if (cc->npoints != 0) {
733 spcc_connector_finish(cc);
734 ret = TRUE;
735 }
736 break;
737 case GDK_Escape:
738 if (cc->state == SP_CONNECTOR_CONTEXT_REROUTING) {
740 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
741 SPDocument *doc = sp_desktop_document(desktop);
743 cc_connector_rerouting_finish(cc, NULL);
745 sp_document_undo(doc);
747 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
748 desktop->messageStack()->flash( Inkscape::NORMAL_MESSAGE,
749 _("Connector endpoint drag cancelled."));
750 ret = TRUE;
751 }
752 else if (cc->npoints != 0) {
753 // if drawing, cancel, otherwise pass it up for deselecting
754 cc->state = SP_CONNECTOR_CONTEXT_STOP;
755 spcc_reset_colors(cc);
756 ret = TRUE;
757 }
758 break;
759 default:
760 break;
761 }
762 return ret;
763 }
766 static void
767 cc_connector_rerouting_finish(SPConnectorContext *const cc, NR::Point *const p)
768 {
769 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
770 SPDocument *doc = sp_desktop_document(desktop);
772 // Clear the temporary path:
773 sp_curve_reset(cc->red_curve);
774 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
776 if (p != NULL)
777 {
778 // Test whether we clicked on a connection point
779 gchar *shape_label = conn_pt_handle_test(cc, *p);
781 if (shape_label) {
782 if (cc->clickedhandle == cc->endpt_handle[0]) {
783 sp_object_setAttribute(cc->clickeditem,
784 "inkscape:connection-start",shape_label, false);
785 }
786 else {
787 sp_object_setAttribute(cc->clickeditem,
788 "inkscape:connection-end",shape_label, false);
789 }
790 g_free(shape_label);
791 }
792 }
793 cc->clickeditem->setHidden(false);
794 sp_conn_adjust_path(SP_PATH(cc->clickeditem));
795 cc->clickeditem->updateRepr();
796 sp_document_done(doc, SP_VERB_CONTEXT_CONNECTOR,
797 _("Reroute connector"));
798 cc_set_active_conn(cc, cc->clickeditem);
799 }
802 static void
803 spcc_reset_colors(SPConnectorContext *cc)
804 {
805 /* Red */
806 sp_curve_reset(cc->red_curve);
807 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
809 sp_curve_reset(cc->green_curve);
810 cc->npoints = 0;
811 }
814 static void
815 spcc_connector_set_initial_point(SPConnectorContext *const cc, NR::Point const p)
816 {
817 g_assert( cc->npoints == 0 );
819 cc->p[0] = p;
820 cc->p[1] = p;
821 cc->npoints = 2;
822 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
823 }
826 static void
827 spcc_connector_set_subsequent_point(SPConnectorContext *const cc, NR::Point const p)
828 {
829 g_assert( cc->npoints != 0 );
831 SPDesktop *dt = cc->desktop;
832 NR::Point o = dt->dt2doc(cc->p[0]);
833 NR::Point d = dt->dt2doc(p);
834 Avoid::Point src(o[NR::X], o[NR::Y]);
835 Avoid::Point dst(d[NR::X], d[NR::Y]);
837 if (!cc->newConnRef) {
838 Avoid::Router *router = sp_desktop_document(dt)->router;
839 cc->newConnRef = new Avoid::ConnRef(router, 0, src, dst);
840 cc->newConnRef->updateEndPoint(Avoid::VertID::src, src);
841 }
842 cc->newConnRef->updateEndPoint(Avoid::VertID::tar, dst);
844 cc->newConnRef->makePathInvalid();
845 cc->newConnRef->generatePath(src, dst);
847 Avoid::PolyLine route = cc->newConnRef->route();
848 cc->newConnRef->calcRouteDist();
850 sp_curve_reset(cc->red_curve);
851 NR::Point pt(route.ps[0].x, route.ps[0].y);
852 sp_curve_moveto(cc->red_curve, pt);
854 for (int i = 1; i < route.pn; ++i) {
855 NR::Point p(route.ps[i].x, route.ps[i].y);
856 sp_curve_lineto(cc->red_curve, p);
857 }
858 sp_curve_transform(cc->red_curve, dt->doc2dt());
859 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), cc->red_curve);
860 }
863 /**
864 * Concats red, blue and green.
865 * If any anchors are defined, process these, optionally removing curves from white list
866 * Invoke _flush_white to write result back to object.
867 */
868 static void
869 spcc_concat_colors_and_flush(SPConnectorContext *cc)
870 {
871 SPCurve *c = cc->green_curve;
872 cc->green_curve = sp_curve_new_sized(64);
874 sp_curve_reset(cc->red_curve);
875 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
877 if (sp_curve_empty(c)) {
878 sp_curve_unref(c);
879 return;
880 }
882 spcc_flush_white(cc, c);
884 sp_curve_unref(c);
885 }
888 /*
889 * Flushes white curve(s) and additional curve into object
890 *
891 * No cleaning of colored curves - this has to be done by caller
892 * No rereading of white data, so if you cannot rely on ::modified, do it in caller
893 *
894 */
896 static void
897 spcc_flush_white(SPConnectorContext *cc, SPCurve *gc)
898 {
899 SPCurve *c;
901 if (gc) {
902 c = gc;
903 sp_curve_ref(c);
904 } else {
905 return;
906 }
908 /* Now we have to go back to item coordinates at last */
909 sp_curve_transform(c,
910 sp_desktop_dt2root_affine(SP_EVENT_CONTEXT_DESKTOP(cc)));
912 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
913 SPDocument *doc = sp_desktop_document(desktop);
914 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
916 if ( c && !sp_curve_empty(c) ) {
917 /* We actually have something to write */
919 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
920 /* Set style */
921 sp_desktop_apply_style_tool(desktop, repr, "tools.connector", false);
923 gchar *str = sp_svg_write_path(SP_CURVE_BPATH(c));
924 g_assert( str != NULL );
925 repr->setAttribute("d", str);
926 g_free(str);
928 /* Attach repr */
929 cc->newconn = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
930 cc->selection->set(repr);
931 Inkscape::GC::release(repr);
932 cc->newconn->transform = i2i_affine(desktop->currentRoot(), desktop->currentLayer());
933 cc->newconn->updateRepr();
935 bool connection = false;
936 sp_object_setAttribute(cc->newconn, "inkscape:connector-type",
937 "polyline", false);
938 if (cc->sid)
939 {
940 sp_object_setAttribute(cc->newconn, "inkscape:connection-start",
941 cc->sid, false);
942 connection = true;
943 }
945 if (cc->eid)
946 {
947 sp_object_setAttribute(cc->newconn, "inkscape:connection-end",
948 cc->eid, false);
949 connection = true;
950 }
951 cc->newconn->updateRepr();
952 if (connection) {
953 // Adjust endpoints to shape edge.
954 sp_conn_adjust_path(SP_PATH(cc->newconn));
955 }
956 cc->newconn->updateRepr();
957 }
959 sp_curve_unref(c);
961 /* Flush pending updates */
962 sp_document_done(doc, SP_VERB_CONTEXT_CONNECTOR, _("Create connector"));
963 sp_document_ensure_up_to_date(doc);
964 }
967 static void
968 spcc_connector_finish_segment(SPConnectorContext *const cc, NR::Point const p)
969 {
970 if (!sp_curve_empty(cc->red_curve)) {
971 sp_curve_append_continuous(cc->green_curve, cc->red_curve, 0.0625);
973 cc->p[0] = cc->p[3];
974 cc->p[1] = cc->p[4];
975 cc->npoints = 2;
977 sp_curve_reset(cc->red_curve);
978 }
979 }
982 static void
983 spcc_connector_finish(SPConnectorContext *const cc)
984 {
985 SPDesktop *const desktop = cc->desktop;
986 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Finishing connector"));
988 sp_curve_reset(cc->red_curve);
989 spcc_concat_colors_and_flush(cc);
991 cc->npoints = 0;
993 if (cc->newConnRef) {
994 cc->newConnRef->removeFromGraph();
995 delete cc->newConnRef;
996 cc->newConnRef = NULL;
997 }
998 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
999 }
1002 static gboolean
1003 cc_generic_knot_handler(SPCanvasItem *, GdkEvent *event, SPKnot *knot)
1004 {
1005 g_assert (knot != NULL);
1007 g_object_ref(knot);
1009 SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(
1010 knot->desktop->event_context);
1012 gboolean consumed = FALSE;
1014 switch (event->type) {
1015 case GDK_ENTER_NOTIFY:
1016 sp_knot_set_flag(knot, SP_KNOT_MOUSEOVER, TRUE);
1018 cc->active_handle = knot;
1020 if (knot->tip)
1021 {
1022 knot->desktop->event_context->defaultMessageContext()->set(
1023 Inkscape::NORMAL_MESSAGE, knot->tip);
1024 }
1026 consumed = TRUE;
1027 break;
1028 case GDK_LEAVE_NOTIFY:
1029 sp_knot_set_flag(knot, SP_KNOT_MOUSEOVER, FALSE);
1031 cc->active_handle = NULL;
1033 if (knot->tip) {
1034 knot->desktop->event_context->defaultMessageContext()->clear();
1035 }
1037 consumed = TRUE;
1038 break;
1039 default:
1040 break;
1041 }
1043 g_object_unref(knot);
1045 return consumed;
1046 }
1049 static gboolean
1050 endpt_handler(SPKnot *knot, GdkEvent *event, SPConnectorContext *cc)
1051 {
1052 g_assert( SP_IS_CONNECTOR_CONTEXT(cc) );
1054 gboolean consumed = FALSE;
1056 switch (event->type) {
1057 case GDK_BUTTON_PRESS:
1058 g_assert( (cc->active_handle == cc->endpt_handle[0]) ||
1059 (cc->active_handle == cc->endpt_handle[1]) );
1060 if (cc->state == SP_CONNECTOR_CONTEXT_IDLE) {
1061 cc->clickeditem = cc->active_conn;
1062 cc->clickedhandle = cc->active_handle;
1063 cc_clear_active_conn(cc);
1064 cc->state = SP_CONNECTOR_CONTEXT_REROUTING;
1066 // Disconnect from attached shape
1067 unsigned ind = (cc->active_handle == cc->endpt_handle[0]) ? 0 : 1;
1068 sp_conn_end_detach(cc->clickeditem, ind);
1070 NR::Point origin;
1071 if (cc->clickedhandle == cc->endpt_handle[0]) {
1072 origin = cc->endpt_handle[1]->pos;
1073 }
1074 else {
1075 origin = cc->endpt_handle[0]->pos;
1076 }
1078 // Show the red path for dragging.
1079 cc->red_curve = sp_curve_copy(SP_PATH(cc->clickeditem)->curve);
1080 NR::Matrix i2d = sp_item_i2d_affine(cc->clickeditem);
1081 sp_curve_transform(cc->red_curve, i2d);
1082 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath),
1083 cc->red_curve);
1085 cc->clickeditem->setHidden(true);
1087 // The rest of the interaction rerouting the connector is
1088 // handled by the context root handler.
1089 consumed = TRUE;
1090 }
1091 break;
1092 default:
1093 break;
1094 }
1096 return consumed;
1097 }
1100 static void cc_set_active_shape(SPConnectorContext *cc, SPItem *item)
1101 {
1102 g_assert(item != NULL );
1104 cc->active_shape = item;
1106 // Remove existing active shape listeners
1107 if (cc->active_shape_repr) {
1108 sp_repr_remove_listener_by_data(cc->active_shape_repr, cc);
1109 Inkscape::GC::release(cc->active_shape_repr);
1111 sp_repr_remove_listener_by_data(cc->active_shape_layer_repr, cc);
1112 Inkscape::GC::release(cc->active_shape_layer_repr);
1113 }
1115 // Listen in case the active shape changes
1116 cc->active_shape_repr = SP_OBJECT_REPR(item);
1117 if (cc->active_shape_repr) {
1118 Inkscape::GC::anchor(cc->active_shape_repr);
1119 sp_repr_add_listener(cc->active_shape_repr, &shape_repr_events, cc);
1121 cc->active_shape_layer_repr = cc->active_shape_repr->parent();
1122 Inkscape::GC::anchor(cc->active_shape_layer_repr);
1123 sp_repr_add_listener(cc->active_shape_layer_repr, &layer_repr_events, cc);
1124 }
1127 // Set center connection point.
1128 if ( cc->connpthandle == NULL ) {
1129 SPKnot *knot = sp_knot_new(cc->desktop,
1130 _("<b>Connection point</b>: click or drag to create a new connector"));
1132 knot->setShape(SP_KNOT_SHAPE_SQUARE);
1133 knot->setSize(8);
1134 knot->setAnchor(GTK_ANCHOR_CENTER);
1135 knot->setFill(0xffffff00, 0xff0000ff, 0xff0000ff);
1136 sp_knot_update_ctrl(knot);
1138 // We don't want to use the standard knot handler,
1139 //since we don't want this knot to be draggable.
1140 g_signal_handler_disconnect(G_OBJECT(knot->item),
1141 knot->_event_handler_id);
1142 knot->_event_handler_id = 0;
1144 gtk_signal_connect(GTK_OBJECT(knot->item), "event",
1145 GTK_SIGNAL_FUNC(cc_generic_knot_handler), knot);
1147 cc->connpthandle = knot;
1148 }
1151 NR::Maybe<NR::Rect> bbox = sp_item_bbox_desktop(cc->active_shape);
1152 if (bbox) {
1153 NR::Point center = bbox->midpoint();
1154 sp_knot_set_position(cc->connpthandle, ¢er, 0);
1155 sp_knot_show(cc->connpthandle);
1156 } else {
1157 sp_knot_hide(cc->connpthandle);
1158 }
1159 }
1162 static void
1163 cc_set_active_conn(SPConnectorContext *cc, SPItem *item)
1164 {
1165 g_assert( SP_IS_PATH(item) );
1167 SPCurve *curve = SP_SHAPE(SP_PATH(item))->curve;
1168 NR::Matrix i2d = sp_item_i2d_affine(item);
1170 if (cc->active_conn == item)
1171 {
1172 // Just adjust handle positions.
1173 NR::Point startpt = sp_curve_first_point(curve) * i2d;
1174 sp_knot_set_position(cc->endpt_handle[0], &startpt, 0);
1176 NR::Point endpt = sp_curve_last_point(curve) * i2d;
1177 sp_knot_set_position(cc->endpt_handle[1], &endpt, 0);
1179 return;
1180 }
1182 cc->active_conn = item;
1184 // Remove existing active conn listeners
1185 if (cc->active_conn_repr) {
1186 sp_repr_remove_listener_by_data(cc->active_conn_repr, cc);
1187 Inkscape::GC::release(cc->active_conn_repr);
1188 cc->active_conn_repr = NULL;
1189 }
1191 // Listen in case the active conn changes
1192 cc->active_conn_repr = SP_OBJECT_REPR(item);
1193 if (cc->active_conn_repr) {
1194 Inkscape::GC::anchor(cc->active_conn_repr);
1195 sp_repr_add_listener(cc->active_conn_repr, &shape_repr_events, cc);
1196 }
1198 for (int i = 0; i < 2; ++i) {
1200 // Create the handle if it doesn't exist
1201 if ( cc->endpt_handle[i] == NULL ) {
1202 SPKnot *knot = sp_knot_new(cc->desktop,
1203 _("<b>Connector endpoint</b>: drag to reroute or connect to new shapes"));
1205 knot->setShape(SP_KNOT_SHAPE_SQUARE);
1206 knot->setSize(7);
1207 knot->setAnchor(GTK_ANCHOR_CENTER);
1208 knot->setFill(0xffffff00, 0xff0000ff, 0xff0000ff);
1209 knot->setStroke(0x000000ff, 0x000000ff, 0x000000ff);
1210 sp_knot_update_ctrl(knot);
1212 // We don't want to use the standard knot handler,
1213 //since we don't want this knot to be draggable.
1214 g_signal_handler_disconnect(G_OBJECT(knot->item),
1215 knot->_event_handler_id);
1216 knot->_event_handler_id = 0;
1218 gtk_signal_connect(GTK_OBJECT(knot->item), "event",
1219 GTK_SIGNAL_FUNC(cc_generic_knot_handler), knot);
1221 cc->endpt_handle[i] = knot;
1222 }
1224 // Remove any existing handlers
1225 if (cc->endpt_handler_id[i]) {
1226 g_signal_handlers_disconnect_by_func(
1227 G_OBJECT(cc->endpt_handle[i]->item),
1228 (void*)G_CALLBACK(endpt_handler), (gpointer) cc );
1229 cc->endpt_handler_id[i] = 0;
1230 }
1232 // Setup handlers for connector endpoints, this is
1233 // is as 'after' so that cc_generic_knot_handler is
1234 // triggered first for any endpoint.
1235 cc->endpt_handler_id[i] = g_signal_connect_after(
1236 G_OBJECT(cc->endpt_handle[i]->item), "event",
1237 G_CALLBACK(endpt_handler), cc);
1238 }
1240 NR::Point startpt = sp_curve_first_point(curve) * i2d;
1241 sp_knot_set_position(cc->endpt_handle[0], &startpt, 0);
1243 NR::Point endpt = sp_curve_last_point(curve) * i2d;
1244 sp_knot_set_position(cc->endpt_handle[1], &endpt, 0);
1246 sp_knot_show(cc->endpt_handle[0]);
1247 sp_knot_show(cc->endpt_handle[1]);
1248 }
1251 static bool cc_item_is_shape(SPItem *item)
1252 {
1253 if (SP_IS_PATH(item)) {
1254 SPCurve *curve = (SP_SHAPE(item))->curve;
1255 if ( curve && !(curve->closed) ) {
1256 // Open paths are connectors.
1257 return false;
1258 }
1259 }
1260 else if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
1261 if (prefs_get_int_attribute("tools.connector", "ignoretext", 1) == 1) {
1262 // Don't count text as a shape we can connect connector to.
1263 return false;
1264 }
1265 }
1266 return true;
1267 }
1270 bool cc_item_is_connector(SPItem *item)
1271 {
1272 if (SP_IS_PATH(item)) {
1273 if (SP_PATH(item)->connEndPair.isAutoRoutingConn()) {
1274 g_assert( !(SP_SHAPE(item)->curve->closed) );
1275 return true;
1276 }
1277 }
1278 return false;
1279 }
1282 void cc_selection_set_avoid(bool const set_avoid)
1283 {
1284 SPDesktop *desktop = inkscape_active_desktop();
1285 if (desktop == NULL) {
1286 return;
1287 }
1289 SPDocument *document = sp_desktop_document(desktop);
1291 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1293 GSList *l = (GSList *) selection->itemList();
1295 int changes = 0;
1297 while (l) {
1298 SPItem *item = (SPItem *) l->data;
1300 char const *value = (set_avoid) ? "true" : NULL;
1302 if (cc_item_is_shape(item)) {
1303 sp_object_setAttribute(item, "inkscape:connector-avoid",
1304 value, false);
1305 item->avoidRef->handleSettingChange();
1306 changes++;
1307 }
1309 l = l->next;
1310 }
1312 if (changes == 0) {
1313 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE,
1314 _("Select <b>at least one non-connector object</b>."));
1315 return;
1316 }
1318 char *event_desc = (set_avoid) ?
1319 _("Make connectors avoid selected objects") :
1320 _("Make connectors ignore selected objects");
1321 sp_document_done(document, SP_VERB_CONTEXT_CONNECTOR, event_desc);
1322 }
1325 static void
1326 cc_selection_changed(Inkscape::Selection *selection, gpointer data)
1327 {
1328 SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(data);
1329 //SPEventContext *ec = SP_EVENT_CONTEXT(cc);
1331 SPItem *item = selection->singleItem();
1333 if (cc->active_conn == item)
1334 {
1335 // Nothing to change.
1336 return;
1337 }
1338 if (item == NULL)
1339 {
1340 cc_clear_active_conn(cc);
1341 return;
1342 }
1344 if (cc_item_is_connector(item)) {
1345 cc_set_active_conn(cc, item);
1346 }
1347 }
1350 static void
1351 shape_event_attr_deleted(Inkscape::XML::Node *repr, Inkscape::XML::Node *child,
1352 Inkscape::XML::Node *ref, gpointer data)
1353 {
1354 g_assert(data);
1355 SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(data);
1357 if (child == cc->active_shape_repr) {
1358 // The active shape has been deleted. Clear active shape.
1359 cc_clear_active_shape(cc);
1360 }
1361 }
1364 static void
1365 shape_event_attr_changed(Inkscape::XML::Node *repr, gchar const *name,
1366 gchar const *old_value, gchar const *new_value,
1367 bool is_interactive, gpointer data)
1368 {
1369 g_assert(data);
1370 SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(data);
1372 // Look for changes than result in onscreen movement.
1373 if (!strcmp(name, "d") || !strcmp(name, "x") || !strcmp(name, "y") ||
1374 !strcmp(name, "width") || !strcmp(name, "height") ||
1375 !strcmp(name, "transform"))
1376 {
1377 if (repr == cc->active_shape_repr) {
1378 // Active shape has moved. Clear active shape.
1379 cc_clear_active_shape(cc);
1380 }
1381 else if (repr == cc->active_conn_repr) {
1382 // The active conn has been moved.
1383 // Set it again, which just sets new handle positions.
1384 cc_set_active_conn(cc, cc->active_conn);
1385 }
1386 }
1387 }
1390 /*
1391 Local Variables:
1392 mode:c++
1393 c-file-style:"stroustrup"
1394 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1395 indent-tabs-mode:nil
1396 fill-column:99
1397 End:
1398 */
1399 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :