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>
45 #include <string>
46 #include <cstring>
48 #include "connector-context.h"
49 #include "pixmaps/cursor-connector.xpm"
50 #include "xml/node-event-vector.h"
51 #include "xml/repr.h"
52 #include "svg/svg.h"
53 #include "desktop.h"
54 #include "desktop-style.h"
55 #include "desktop-affine.h"
56 #include "desktop-handles.h"
57 #include "document.h"
58 #include "message-context.h"
59 #include "message-stack.h"
60 #include "selection.h"
61 #include "inkscape.h"
62 #include "preferences.h"
63 #include "sp-path.h"
64 #include "display/canvas-bpath.h"
65 #include "display/sodipodi-ctrl.h"
66 #include <glibmm/i18n.h>
67 #include "snap.h"
68 #include "knot.h"
69 #include "sp-conn-end.h"
70 #include "conn-avoid-ref.h"
71 #include "libavoid/vertices.h"
72 #include "context-fns.h"
73 #include "sp-namedview.h"
74 #include "sp-text.h"
75 #include "sp-flowtext.h"
76 #include "display/curve.h"
78 static void sp_connector_context_class_init(SPConnectorContextClass *klass);
79 static void sp_connector_context_init(SPConnectorContext *conn_context);
80 static void sp_connector_context_dispose(GObject *object);
82 static void sp_connector_context_setup(SPEventContext *ec);
83 static void sp_connector_context_finish(SPEventContext *ec);
84 static gint sp_connector_context_root_handler(SPEventContext *ec, GdkEvent *event);
85 static gint sp_connector_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
87 // Stuff borrowed from DrawContext
88 static void spcc_connector_set_initial_point(SPConnectorContext *cc, Geom::Point const p);
89 static void spcc_connector_set_subsequent_point(SPConnectorContext *cc, Geom::Point const p);
90 static void spcc_connector_finish_segment(SPConnectorContext *cc, Geom::Point p);
91 static void spcc_reset_colors(SPConnectorContext *cc);
92 static void spcc_connector_finish(SPConnectorContext *cc);
93 static void spcc_concat_colors_and_flush(SPConnectorContext *cc);
94 static void spcc_flush_white(SPConnectorContext *cc, SPCurve *gc);
96 // Context event handlers
97 static gint connector_handle_button_press(SPConnectorContext *const cc, GdkEventButton const &bevent);
98 static gint connector_handle_motion_notify(SPConnectorContext *const cc, GdkEventMotion const &mevent);
99 static gint connector_handle_button_release(SPConnectorContext *const cc, GdkEventButton const &revent);
100 static gint connector_handle_key_press(SPConnectorContext *const cc, guint const keyval);
102 static void cc_set_active_shape(SPConnectorContext *cc, SPItem *item);
103 static void cc_clear_active_shape(SPConnectorContext *cc);
104 static void cc_set_active_conn(SPConnectorContext *cc, SPItem *item);
105 static void cc_clear_active_conn(SPConnectorContext *cc);
106 static gchar *conn_pt_handle_test(SPConnectorContext *cc, Geom::Point& w);
107 static bool cc_item_is_shape(SPItem *item);
108 static void cc_selection_changed(Inkscape::Selection *selection, gpointer data);
109 static void cc_connector_rerouting_finish(SPConnectorContext *const cc,
110 Geom::Point *const p);
112 static void shape_event_attr_deleted(Inkscape::XML::Node *repr,
113 Inkscape::XML::Node *child, Inkscape::XML::Node *ref, gpointer data);
114 static void shape_event_attr_changed(Inkscape::XML::Node *repr, gchar const *name,
115 gchar const *old_value, gchar const *new_value, bool is_interactive,
116 gpointer data);
119 static Geom::Point connector_drag_origin_w(0, 0);
120 static bool connector_within_tolerance = false;
121 static SPEventContextClass *parent_class;
124 static Inkscape::XML::NodeEventVector shape_repr_events = {
125 NULL, /* child_added */
126 NULL, /* child_added */
127 shape_event_attr_changed,
128 NULL, /* content_changed */
129 NULL /* order_changed */
130 };
132 static Inkscape::XML::NodeEventVector layer_repr_events = {
133 NULL, /* child_added */
134 shape_event_attr_deleted,
135 NULL, /* child_added */
136 NULL, /* content_changed */
137 NULL /* order_changed */
138 };
141 GType
142 sp_connector_context_get_type(void)
143 {
144 static GType type = 0;
145 if (!type) {
146 GTypeInfo info = {
147 sizeof(SPConnectorContextClass),
148 NULL, NULL,
149 (GClassInitFunc) sp_connector_context_class_init,
150 NULL, NULL,
151 sizeof(SPConnectorContext),
152 4,
153 (GInstanceInitFunc) sp_connector_context_init,
154 NULL, /* value_table */
155 };
156 type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPConnectorContext", &info, (GTypeFlags)0);
157 }
158 return type;
159 }
161 static void
162 sp_connector_context_class_init(SPConnectorContextClass *klass)
163 {
164 GObjectClass *object_class;
165 SPEventContextClass *event_context_class;
167 object_class = (GObjectClass *) klass;
168 event_context_class = (SPEventContextClass *) klass;
170 parent_class = (SPEventContextClass*)g_type_class_peek_parent(klass);
172 object_class->dispose = sp_connector_context_dispose;
174 event_context_class->setup = sp_connector_context_setup;
175 event_context_class->finish = sp_connector_context_finish;
176 event_context_class->root_handler = sp_connector_context_root_handler;
177 event_context_class->item_handler = sp_connector_context_item_handler;
178 }
181 static void
182 sp_connector_context_init(SPConnectorContext *cc)
183 {
184 SPEventContext *ec = SP_EVENT_CONTEXT(cc);
186 ec->cursor_shape = cursor_connector_xpm;
187 ec->hot_x = 1;
188 ec->hot_y = 1;
189 ec->xp = 0;
190 ec->yp = 0;
192 cc->red_color = 0xff00007f;
194 cc->newconn = NULL;
195 cc->newConnRef = NULL;
197 cc->sel_changed_connection = sigc::connection();
199 cc->active_shape = NULL;
200 cc->active_shape_repr = NULL;
201 cc->active_shape_layer_repr = NULL;
203 cc->active_conn = NULL;
204 cc->active_conn_repr = NULL;
206 cc->active_handle = NULL;
208 cc->clickeditem = NULL;
209 cc->clickedhandle = NULL;
211 cc->connpthandle = NULL;
212 for (int i = 0; i < 2; ++i) {
213 cc->endpt_handle[i] = NULL;
214 cc->endpt_handler_id[i] = 0;
215 }
216 cc->sid = NULL;
217 cc->eid = NULL;
218 cc->npoints = 0;
219 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
220 }
223 static void
224 sp_connector_context_dispose(GObject *object)
225 {
226 SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(object);
228 cc->sel_changed_connection.disconnect();
230 if (cc->connpthandle) {
231 g_object_unref(cc->connpthandle);
232 cc->connpthandle = NULL;
233 }
234 for (int i = 0; i < 2; ++i) {
235 if (cc->endpt_handle[1]) {
236 g_object_unref(cc->endpt_handle[i]);
237 cc->endpt_handle[i] = NULL;
238 }
239 }
240 if (cc->sid) {
241 g_free(cc->sid);
242 cc->sid = NULL;
243 }
244 if (cc->eid) {
245 g_free(cc->eid);
246 cc->eid = NULL;
247 }
248 g_assert( cc->newConnRef == NULL );
250 G_OBJECT_CLASS(parent_class)->dispose(object);
251 }
254 static void
255 sp_connector_context_setup(SPEventContext *ec)
256 {
257 SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(ec);
258 SPDesktop *dt = ec->desktop;
260 if (((SPEventContextClass *) parent_class)->setup) {
261 ((SPEventContextClass *) parent_class)->setup(ec);
262 }
264 cc->selection = sp_desktop_selection(dt);
266 cc->sel_changed_connection.disconnect();
267 cc->sel_changed_connection = cc->selection->connectChanged(
268 sigc::bind(sigc::ptr_fun(&cc_selection_changed),
269 (gpointer) cc));
271 /* Create red bpath */
272 cc->red_bpath = sp_canvas_bpath_new(sp_desktop_sketch(ec->desktop), NULL);
273 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cc->red_bpath), cc->red_color,
274 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
275 sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(cc->red_bpath), 0x00000000,
276 SP_WIND_RULE_NONZERO);
277 /* Create red curve */
278 cc->red_curve = new SPCurve();
280 /* Create green curve */
281 cc->green_curve = new SPCurve();
283 // Notice the initial selection.
284 cc_selection_changed(cc->selection, (gpointer) cc);
286 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
287 if (prefs->getBool("/tools/connector/selcue", 0)) {
288 ec->enableSelectionCue();
289 }
291 // Make sure we see all enter events for canvas items,
292 // even if a mouse button is depressed.
293 dt->canvas->gen_all_enter_events = true;
295 sp_canvas_set_snap_delay_active(dt->canvas, true);
296 }
299 static void
300 sp_connector_context_finish(SPEventContext *ec)
301 {
302 SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(ec);
304 spcc_connector_finish(cc);
306 if (((SPEventContextClass *) parent_class)->finish) {
307 ((SPEventContextClass *) parent_class)->finish(ec);
308 }
310 if (cc->selection) {
311 cc->selection = NULL;
312 }
313 cc_clear_active_shape(cc);
314 cc_clear_active_conn(cc);
316 // Restore the default event generating behaviour.
317 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(ec);
318 desktop->canvas->gen_all_enter_events = false;
320 sp_canvas_set_snap_delay_active(desktop->canvas, false);
321 }
324 //-----------------------------------------------------------------------------
327 static void
328 cc_clear_active_shape(SPConnectorContext *cc)
329 {
330 if (cc->active_shape == NULL) {
331 return;
332 }
333 g_assert( cc->active_shape_repr );
334 g_assert( cc->active_shape_layer_repr );
336 cc->active_shape = NULL;
338 if (cc->active_shape_repr) {
339 sp_repr_remove_listener_by_data(cc->active_shape_repr, cc);
340 Inkscape::GC::release(cc->active_shape_repr);
341 cc->active_shape_repr = NULL;
343 sp_repr_remove_listener_by_data(cc->active_shape_layer_repr, cc);
344 Inkscape::GC::release(cc->active_shape_layer_repr);
345 cc->active_shape_layer_repr = NULL;
346 }
348 // Hide the center connection point if it exists.
349 if (cc->connpthandle) {
350 sp_knot_hide(cc->connpthandle);
351 }
352 }
355 static void
356 cc_clear_active_conn(SPConnectorContext *cc)
357 {
358 if (cc->active_conn == NULL) {
359 return;
360 }
361 g_assert( cc->active_conn_repr );
363 cc->active_conn = NULL;
365 if (cc->active_conn_repr) {
366 sp_repr_remove_listener_by_data(cc->active_conn_repr, cc);
367 Inkscape::GC::release(cc->active_conn_repr);
368 cc->active_conn_repr = NULL;
369 }
371 // Hide the endpoint handles.
372 for (int i = 0; i < 2; ++i) {
373 if (cc->endpt_handle[i]) {
374 sp_knot_hide(cc->endpt_handle[i]);
375 }
376 }
377 }
380 static gchar *
381 conn_pt_handle_test(SPConnectorContext *cc, Geom::Point& p)
382 {
383 // TODO: this will need to change when there are more connection
384 // points available for each shape.
386 SPKnot *centerpt = cc->connpthandle;
387 if (cc->active_handle && (cc->active_handle == centerpt))
388 {
389 p = centerpt->pos;
390 return g_strdup_printf("#%s", SP_OBJECT_ID(cc->active_shape));
391 }
392 return NULL;
393 }
397 static gint
398 sp_connector_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
399 {
400 gint ret = FALSE;
402 SPDesktop *desktop = event_context->desktop;
404 SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(event_context);
406 Geom::Point p(event->button.x, event->button.y);
408 switch (event->type) {
409 case GDK_BUTTON_RELEASE:
410 if (event->button.button == 1 && !event_context->space_panning) {
411 if ((cc->state == SP_CONNECTOR_CONTEXT_DRAGGING) &&
412 (connector_within_tolerance))
413 {
414 spcc_reset_colors(cc);
415 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
416 }
417 if (cc->state != SP_CONNECTOR_CONTEXT_IDLE) {
418 // Doing simething else like rerouting.
419 break;
420 }
421 // find out clicked item, disregarding groups, honoring Alt
422 SPItem *item_ungrouped = sp_event_context_find_item(desktop,
423 p, event->button.state & GDK_MOD1_MASK, TRUE);
425 if (event->button.state & GDK_SHIFT_MASK) {
426 cc->selection->toggle(item_ungrouped);
427 } else {
428 cc->selection->set(item_ungrouped);
429 }
430 ret = TRUE;
431 }
432 break;
433 case GDK_ENTER_NOTIFY:
434 {
435 if (cc_item_is_shape(item)) {
436 // This is a shape, so show connection point(s).
437 if (!(cc->active_shape) ||
438 // Don't show handle for another handle.
439 (item != ((SPItem *) cc->connpthandle))) {
440 cc_set_active_shape(cc, item);
441 }
442 }
443 ret = TRUE;
444 break;
445 }
446 default:
447 break;
448 }
450 return ret;
451 }
454 gint
455 sp_connector_context_root_handler(SPEventContext *ec, GdkEvent *event)
456 {
457 SPConnectorContext *const cc = SP_CONNECTOR_CONTEXT(ec);
459 gint ret = FALSE;
461 switch (event->type) {
462 case GDK_BUTTON_PRESS:
463 ret = connector_handle_button_press(cc, event->button);
464 break;
466 case GDK_MOTION_NOTIFY:
467 ret = connector_handle_motion_notify(cc, event->motion);
468 break;
470 case GDK_BUTTON_RELEASE:
471 ret = connector_handle_button_release(cc, event->button);
472 break;
473 case GDK_KEY_PRESS:
474 ret = connector_handle_key_press(cc, get_group0_keyval (&event->key));
475 break;
477 default:
478 break;
479 }
481 if (!ret) {
482 gint (*const parent_root_handler)(SPEventContext *, GdkEvent *)
483 = ((SPEventContextClass *) parent_class)->root_handler;
484 if (parent_root_handler) {
485 ret = parent_root_handler(ec, event);
486 }
487 }
489 return ret;
490 }
493 static gint
494 connector_handle_button_press(SPConnectorContext *const cc, GdkEventButton const &bevent)
495 {
496 Geom::Point const event_w(bevent.x, bevent.y);
497 /* Find desktop coordinates */
498 Geom::Point p = cc->desktop->w2d(event_w);
499 SPEventContext *event_context = SP_EVENT_CONTEXT(cc);
501 gint ret = FALSE;
502 if ( bevent.button == 1 && !event_context->space_panning ) {
504 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
506 if (Inkscape::have_viable_layer(desktop, cc->_message_context) == false) {
507 return TRUE;
508 }
510 Geom::Point const event_w(bevent.x,
511 bevent.y);
512 connector_drag_origin_w = event_w;
513 connector_within_tolerance = true;
515 Geom::Point const event_dt = cc->desktop->w2d(event_w);
516 switch (cc->state) {
517 case SP_CONNECTOR_CONTEXT_STOP:
518 /* This is allowed, if we just cancelled curve */
519 case SP_CONNECTOR_CONTEXT_IDLE:
520 {
521 if ( cc->npoints == 0 ) {
522 cc_clear_active_conn(cc);
524 SP_EVENT_CONTEXT_DESKTOP(cc)->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating new connector"));
526 /* Set start anchor */
527 /* Create green anchor */
528 Geom::Point p = event_dt;
530 // Test whether we clicked on a connection point
531 cc->sid = conn_pt_handle_test(cc, p);
533 Geom::Point pt2g = to_2geom(p);
535 if (!cc->sid) {
536 // This is the first point, so just snap it to the grid
537 // as there's no other points to go off.
538 SnapManager &m = cc->desktop->namedview->snap_manager;
539 m.setup(cc->desktop);
540 m.freeSnapReturnByRef(Inkscape::SnapPreferences::SNAPPOINT_NODE, pt2g);
541 }
542 spcc_connector_set_initial_point(cc, from_2geom(pt2g));
544 }
545 cc->state = SP_CONNECTOR_CONTEXT_DRAGGING;
546 ret = TRUE;
547 break;
548 }
549 case SP_CONNECTOR_CONTEXT_DRAGGING:
550 {
551 // This is the second click of a connector creation.
553 spcc_connector_set_subsequent_point(cc, p);
554 spcc_connector_finish_segment(cc, p);
555 // Test whether we clicked on a connection point
556 cc->eid = conn_pt_handle_test(cc, p);
557 if (cc->npoints != 0) {
558 spcc_connector_finish(cc);
559 }
560 cc_set_active_conn(cc, cc->newconn);
561 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
562 ret = TRUE;
563 break;
564 }
565 case SP_CONNECTOR_CONTEXT_CLOSE:
566 {
567 g_warning("Button down in CLOSE state");
568 break;
569 }
570 default:
571 break;
572 }
573 } else if (bevent.button == 3) {
574 if (cc->state == SP_CONNECTOR_CONTEXT_REROUTING) {
575 // A context menu is going to be triggered here,
576 // so end the rerouting operation.
577 cc_connector_rerouting_finish(cc, &p);
579 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
581 // Don't set ret to TRUE, so we drop through to the
582 // parent handler which will open the context menu.
583 }
584 else if (cc->npoints != 0) {
585 spcc_connector_finish(cc);
586 ret = TRUE;
587 }
588 }
589 return ret;
590 }
593 static gint
594 connector_handle_motion_notify(SPConnectorContext *const cc, GdkEventMotion const &mevent)
595 {
596 gint ret = FALSE;
597 SPEventContext *event_context = SP_EVENT_CONTEXT(cc);
598 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
600 if (event_context->space_panning || mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) {
601 // allow middle-button scrolling
602 return FALSE;
603 }
605 Geom::Point const event_w(mevent.x, mevent.y);
607 if (connector_within_tolerance) {
608 gint const tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
609 if ( Geom::LInfty( event_w - connector_drag_origin_w ) < tolerance ) {
610 return FALSE; // Do not drag if we're within tolerance from origin.
611 }
612 }
613 // Once the user has moved farther than tolerance from the original location
614 // (indicating they intend to move the object, not click), then always process
615 // the motion notify coordinates as given (no snapping back to origin)
616 connector_within_tolerance = false;
618 SPDesktop *const dt = cc->desktop;
620 /* Find desktop coordinates */
621 Geom::Point p = dt->w2d(event_w);
623 switch (cc->state) {
624 case SP_CONNECTOR_CONTEXT_DRAGGING:
625 {
626 // This is movement during a connector creation.
628 if ( cc->npoints > 0 ) {
629 cc->selection->clear();
630 spcc_connector_set_subsequent_point(cc, p);
631 ret = TRUE;
632 }
633 break;
634 }
635 case SP_CONNECTOR_CONTEXT_REROUTING:
636 {
637 g_assert( SP_IS_PATH(cc->clickeditem));
639 // Update the hidden path
640 Geom::Matrix i2d = sp_item_i2d_affine(cc->clickeditem);
641 Geom::Matrix d2i = i2d.inverse();
642 SPPath *path = SP_PATH(cc->clickeditem);
643 SPCurve *curve = (SP_SHAPE(path))->curve;
644 if (cc->clickedhandle == cc->endpt_handle[0]) {
645 Geom::Point o = cc->endpt_handle[1]->pos;
646 curve->stretch_endpoints(p * d2i, o * d2i);
647 }
648 else {
649 Geom::Point o = cc->endpt_handle[0]->pos;
650 curve->stretch_endpoints(o * d2i, p * d2i);
651 }
652 sp_conn_adjust_path(path);
654 // Copy this to the temporary visible path
655 cc->red_curve = SP_SHAPE(path)->curve->copy();
656 cc->red_curve->transform(i2d);
658 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), cc->red_curve);
659 ret = TRUE;
660 break;
661 }
662 case SP_CONNECTOR_CONTEXT_STOP:
663 /* This is perfectly valid */
664 break;
665 default:
666 break;
667 }
669 return ret;
670 }
673 static gint
674 connector_handle_button_release(SPConnectorContext *const cc, GdkEventButton const &revent)
675 {
676 gint ret = FALSE;
677 SPEventContext *event_context = SP_EVENT_CONTEXT(cc);
678 if ( revent.button == 1 && !event_context->space_panning ) {
680 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
681 SPDocument *doc = sp_desktop_document(desktop);
683 Geom::Point const event_w(revent.x, revent.y);
685 /* Find desktop coordinates */
686 Geom::Point p = cc->desktop->w2d(event_w);
688 switch (cc->state) {
689 //case SP_CONNECTOR_CONTEXT_POINT:
690 case SP_CONNECTOR_CONTEXT_DRAGGING:
691 {
692 if (connector_within_tolerance)
693 {
694 spcc_connector_finish_segment(cc, p);
695 return TRUE;
696 }
697 // Connector has been created via a drag, end it now.
698 spcc_connector_set_subsequent_point(cc, p);
699 spcc_connector_finish_segment(cc, p);
700 // Test whether we clicked on a connection point
701 cc->eid = conn_pt_handle_test(cc, p);
702 if (cc->npoints != 0) {
703 spcc_connector_finish(cc);
704 }
705 cc_set_active_conn(cc, cc->newconn);
706 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
707 break;
708 }
709 case SP_CONNECTOR_CONTEXT_REROUTING:
710 {
711 cc_connector_rerouting_finish(cc, &p);
713 sp_document_ensure_up_to_date(doc);
714 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
715 return TRUE;
716 break;
717 }
718 case SP_CONNECTOR_CONTEXT_STOP:
719 /* This is allowed, if we just cancelled curve */
720 break;
721 default:
722 break;
723 }
724 ret = TRUE;
725 }
727 return ret;
728 }
731 static gint
732 connector_handle_key_press(SPConnectorContext *const cc, guint const keyval)
733 {
734 gint ret = FALSE;
735 /* fixme: */
736 switch (keyval) {
737 case GDK_Return:
738 case GDK_KP_Enter:
739 if (cc->npoints != 0) {
740 spcc_connector_finish(cc);
741 ret = TRUE;
742 }
743 break;
744 case GDK_Escape:
745 if (cc->state == SP_CONNECTOR_CONTEXT_REROUTING) {
747 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
748 SPDocument *doc = sp_desktop_document(desktop);
750 cc_connector_rerouting_finish(cc, NULL);
752 sp_document_undo(doc);
754 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
755 desktop->messageStack()->flash( Inkscape::NORMAL_MESSAGE,
756 _("Connector endpoint drag cancelled."));
757 ret = TRUE;
758 }
759 else if (cc->npoints != 0) {
760 // if drawing, cancel, otherwise pass it up for deselecting
761 cc->state = SP_CONNECTOR_CONTEXT_STOP;
762 spcc_reset_colors(cc);
763 ret = TRUE;
764 }
765 break;
766 default:
767 break;
768 }
769 return ret;
770 }
773 static void
774 cc_connector_rerouting_finish(SPConnectorContext *const cc, Geom::Point *const p)
775 {
776 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
777 SPDocument *doc = sp_desktop_document(desktop);
779 // Clear the temporary path:
780 cc->red_curve->reset();
781 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
783 if (p != NULL)
784 {
785 // Test whether we clicked on a connection point
786 gchar *shape_label = conn_pt_handle_test(cc, *p);
788 if (shape_label) {
789 if (cc->clickedhandle == cc->endpt_handle[0]) {
790 sp_object_setAttribute(cc->clickeditem,
791 "inkscape:connection-start",shape_label, false);
792 }
793 else {
794 sp_object_setAttribute(cc->clickeditem,
795 "inkscape:connection-end",shape_label, false);
796 }
797 g_free(shape_label);
798 }
799 }
800 cc->clickeditem->setHidden(false);
801 sp_conn_adjust_path(SP_PATH(cc->clickeditem));
802 cc->clickeditem->updateRepr();
803 sp_document_done(doc, SP_VERB_CONTEXT_CONNECTOR,
804 _("Reroute connector"));
805 cc_set_active_conn(cc, cc->clickeditem);
806 }
809 static void
810 spcc_reset_colors(SPConnectorContext *cc)
811 {
812 /* Red */
813 cc->red_curve->reset();
814 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
816 cc->green_curve->reset();
817 cc->npoints = 0;
818 }
821 static void
822 spcc_connector_set_initial_point(SPConnectorContext *const cc, Geom::Point const p)
823 {
824 g_assert( cc->npoints == 0 );
826 cc->p[0] = p;
827 cc->p[1] = p;
828 cc->npoints = 2;
829 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
830 }
833 static void
834 spcc_connector_set_subsequent_point(SPConnectorContext *const cc, Geom::Point const p)
835 {
836 g_assert( cc->npoints != 0 );
838 SPDesktop *dt = cc->desktop;
839 Geom::Point o = dt->dt2doc(cc->p[0]);
840 Geom::Point d = dt->dt2doc(p);
841 Avoid::Point src(o[Geom::X], o[Geom::Y]);
842 Avoid::Point dst(d[Geom::X], d[Geom::Y]);
844 if (!cc->newConnRef) {
845 Avoid::Router *router = sp_desktop_document(dt)->router;
846 cc->newConnRef = new Avoid::ConnRef(router, 0, src, dst);
847 cc->newConnRef->updateEndPoint(Avoid::VertID::src, src);
848 }
849 cc->newConnRef->updateEndPoint(Avoid::VertID::tar, dst);
851 cc->newConnRef->makePathInvalid();
852 cc->newConnRef->generatePath(src, dst);
854 Avoid::PolyLine route = cc->newConnRef->route();
855 cc->newConnRef->calcRouteDist();
857 cc->red_curve->reset();
858 Geom::Point pt(route.ps[0].x, route.ps[0].y);
859 cc->red_curve->moveto(pt);
861 for (int i = 1; i < route.pn; ++i) {
862 Geom::Point p(route.ps[i].x, route.ps[i].y);
863 cc->red_curve->lineto(p);
864 }
865 cc->red_curve->transform(dt->doc2dt());
866 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), cc->red_curve);
867 }
870 /**
871 * Concats red, blue and green.
872 * If any anchors are defined, process these, optionally removing curves from white list
873 * Invoke _flush_white to write result back to object.
874 */
875 static void
876 spcc_concat_colors_and_flush(SPConnectorContext *cc)
877 {
878 SPCurve *c = cc->green_curve;
879 cc->green_curve = new SPCurve();
881 cc->red_curve->reset();
882 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
884 if (c->is_empty()) {
885 c->unref();
886 return;
887 }
889 spcc_flush_white(cc, c);
891 c->unref();
892 }
895 /*
896 * Flushes white curve(s) and additional curve into object
897 *
898 * No cleaning of colored curves - this has to be done by caller
899 * No rereading of white data, so if you cannot rely on ::modified, do it in caller
900 *
901 */
903 static void
904 spcc_flush_white(SPConnectorContext *cc, SPCurve *gc)
905 {
906 SPCurve *c;
908 if (gc) {
909 c = gc;
910 c->ref();
911 } else {
912 return;
913 }
915 /* Now we have to go back to item coordinates at last */
916 c->transform(sp_desktop_dt2doc_affine(SP_EVENT_CONTEXT_DESKTOP(cc)));
918 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
919 SPDocument *doc = sp_desktop_document(desktop);
920 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
922 if ( c && !c->is_empty() ) {
923 /* We actually have something to write */
925 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
926 /* Set style */
927 sp_desktop_apply_style_tool(desktop, repr, "/tools/connector", false);
929 gchar *str = sp_svg_write_path( c->get_pathvector() );
930 g_assert( str != NULL );
931 repr->setAttribute("d", str);
932 g_free(str);
934 /* Attach repr */
935 cc->newconn = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
936 cc->selection->set(repr);
937 Inkscape::GC::release(repr);
938 cc->newconn->transform = sp_item_i2doc_affine(SP_ITEM(desktop->currentLayer())).inverse();
939 cc->newconn->updateRepr();
941 bool connection = false;
942 sp_object_setAttribute(cc->newconn, "inkscape:connector-type",
943 "polyline", false);
944 if (cc->sid)
945 {
946 sp_object_setAttribute(cc->newconn, "inkscape:connection-start",
947 cc->sid, false);
948 connection = true;
949 }
951 if (cc->eid)
952 {
953 sp_object_setAttribute(cc->newconn, "inkscape:connection-end",
954 cc->eid, false);
955 connection = true;
956 }
957 cc->newconn->updateRepr();
958 if (connection) {
959 // Adjust endpoints to shape edge.
960 sp_conn_adjust_path(SP_PATH(cc->newconn));
961 }
962 cc->newconn->updateRepr();
963 }
965 c->unref();
967 /* Flush pending updates */
968 sp_document_done(doc, SP_VERB_CONTEXT_CONNECTOR, _("Create connector"));
969 sp_document_ensure_up_to_date(doc);
970 }
973 static void
974 spcc_connector_finish_segment(SPConnectorContext *const cc, Geom::Point const /*p*/)
975 {
976 if (!cc->red_curve->is_empty()) {
977 cc->green_curve->append_continuous(cc->red_curve, 0.0625);
979 cc->p[0] = cc->p[3];
980 cc->p[1] = cc->p[4];
981 cc->npoints = 2;
983 cc->red_curve->reset();
984 }
985 }
988 static void
989 spcc_connector_finish(SPConnectorContext *const cc)
990 {
991 SPDesktop *const desktop = cc->desktop;
992 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Finishing connector"));
994 cc->red_curve->reset();
995 spcc_concat_colors_and_flush(cc);
997 cc->npoints = 0;
999 if (cc->newConnRef) {
1000 cc->newConnRef->removeFromGraph();
1001 delete cc->newConnRef;
1002 cc->newConnRef = NULL;
1003 }
1004 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
1005 }
1008 static gboolean
1009 cc_generic_knot_handler(SPCanvasItem *, GdkEvent *event, SPKnot *knot)
1010 {
1011 g_assert (knot != NULL);
1013 g_object_ref(knot);
1015 SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(
1016 knot->desktop->event_context);
1018 gboolean consumed = FALSE;
1020 switch (event->type) {
1021 case GDK_ENTER_NOTIFY:
1022 sp_knot_set_flag(knot, SP_KNOT_MOUSEOVER, TRUE);
1024 cc->active_handle = knot;
1026 if (knot->tip)
1027 {
1028 knot->desktop->event_context->defaultMessageContext()->set(
1029 Inkscape::NORMAL_MESSAGE, knot->tip);
1030 }
1032 consumed = TRUE;
1033 break;
1034 case GDK_LEAVE_NOTIFY:
1035 sp_knot_set_flag(knot, SP_KNOT_MOUSEOVER, FALSE);
1037 cc->active_handle = NULL;
1039 if (knot->tip) {
1040 knot->desktop->event_context->defaultMessageContext()->clear();
1041 }
1043 consumed = TRUE;
1044 break;
1045 default:
1046 break;
1047 }
1049 g_object_unref(knot);
1051 return consumed;
1052 }
1055 static gboolean
1056 endpt_handler(SPKnot */*knot*/, GdkEvent *event, SPConnectorContext *cc)
1057 {
1058 g_assert( SP_IS_CONNECTOR_CONTEXT(cc) );
1060 gboolean consumed = FALSE;
1062 switch (event->type) {
1063 case GDK_BUTTON_PRESS:
1064 g_assert( (cc->active_handle == cc->endpt_handle[0]) ||
1065 (cc->active_handle == cc->endpt_handle[1]) );
1066 if (cc->state == SP_CONNECTOR_CONTEXT_IDLE) {
1067 cc->clickeditem = cc->active_conn;
1068 cc->clickedhandle = cc->active_handle;
1069 cc_clear_active_conn(cc);
1070 cc->state = SP_CONNECTOR_CONTEXT_REROUTING;
1072 // Disconnect from attached shape
1073 unsigned ind = (cc->active_handle == cc->endpt_handle[0]) ? 0 : 1;
1074 sp_conn_end_detach(cc->clickeditem, ind);
1076 Geom::Point origin;
1077 if (cc->clickedhandle == cc->endpt_handle[0]) {
1078 origin = cc->endpt_handle[1]->pos;
1079 }
1080 else {
1081 origin = cc->endpt_handle[0]->pos;
1082 }
1084 // Show the red path for dragging.
1085 cc->red_curve = SP_PATH(cc->clickeditem)->curve->copy();
1086 Geom::Matrix i2d = sp_item_i2d_affine(cc->clickeditem);
1087 cc->red_curve->transform(i2d);
1088 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), cc->red_curve);
1090 cc->clickeditem->setHidden(true);
1092 // The rest of the interaction rerouting the connector is
1093 // handled by the context root handler.
1094 consumed = TRUE;
1095 }
1096 break;
1097 default:
1098 break;
1099 }
1101 return consumed;
1102 }
1105 static void cc_set_active_shape(SPConnectorContext *cc, SPItem *item)
1106 {
1107 g_assert(item != NULL );
1109 cc->active_shape = item;
1111 // Remove existing active shape listeners
1112 if (cc->active_shape_repr) {
1113 sp_repr_remove_listener_by_data(cc->active_shape_repr, cc);
1114 Inkscape::GC::release(cc->active_shape_repr);
1116 sp_repr_remove_listener_by_data(cc->active_shape_layer_repr, cc);
1117 Inkscape::GC::release(cc->active_shape_layer_repr);
1118 }
1120 // Listen in case the active shape changes
1121 cc->active_shape_repr = SP_OBJECT_REPR(item);
1122 if (cc->active_shape_repr) {
1123 Inkscape::GC::anchor(cc->active_shape_repr);
1124 sp_repr_add_listener(cc->active_shape_repr, &shape_repr_events, cc);
1126 cc->active_shape_layer_repr = cc->active_shape_repr->parent();
1127 Inkscape::GC::anchor(cc->active_shape_layer_repr);
1128 sp_repr_add_listener(cc->active_shape_layer_repr, &layer_repr_events, cc);
1129 }
1132 // Set center connection point.
1133 if ( cc->connpthandle == NULL ) {
1134 SPKnot *knot = sp_knot_new(cc->desktop,
1135 _("<b>Connection point</b>: click or drag to create a new connector"));
1137 knot->setShape(SP_KNOT_SHAPE_SQUARE);
1138 knot->setSize(8);
1139 knot->setAnchor(GTK_ANCHOR_CENTER);
1140 knot->setFill(0xffffff00, 0xff0000ff, 0xff0000ff);
1141 sp_knot_update_ctrl(knot);
1143 // We don't want to use the standard knot handler,
1144 //since we don't want this knot to be draggable.
1145 g_signal_handler_disconnect(G_OBJECT(knot->item),
1146 knot->_event_handler_id);
1147 knot->_event_handler_id = 0;
1149 gtk_signal_connect(GTK_OBJECT(knot->item), "event",
1150 GTK_SIGNAL_FUNC(cc_generic_knot_handler), knot);
1152 cc->connpthandle = knot;
1153 }
1156 Geom::OptRect bbox = sp_item_bbox_desktop(cc->active_shape);
1157 if (bbox) {
1158 Geom::Point center = bbox->midpoint();
1159 sp_knot_set_position(cc->connpthandle, center, 0);
1160 sp_knot_show(cc->connpthandle);
1161 } else {
1162 sp_knot_hide(cc->connpthandle);
1163 }
1164 }
1167 static void
1168 cc_set_active_conn(SPConnectorContext *cc, SPItem *item)
1169 {
1170 g_assert( SP_IS_PATH(item) );
1172 SPCurve *curve = SP_SHAPE(SP_PATH(item))->curve;
1173 Geom::Matrix i2d = sp_item_i2d_affine(item);
1175 if (cc->active_conn == item)
1176 {
1177 // Just adjust handle positions.
1178 Geom::Point startpt = *(curve->first_point()) * i2d;
1179 sp_knot_set_position(cc->endpt_handle[0], startpt, 0);
1181 Geom::Point endpt = *(curve->last_point()) * i2d;
1182 sp_knot_set_position(cc->endpt_handle[1], endpt, 0);
1184 return;
1185 }
1187 cc->active_conn = item;
1189 // Remove existing active conn listeners
1190 if (cc->active_conn_repr) {
1191 sp_repr_remove_listener_by_data(cc->active_conn_repr, cc);
1192 Inkscape::GC::release(cc->active_conn_repr);
1193 cc->active_conn_repr = NULL;
1194 }
1196 // Listen in case the active conn changes
1197 cc->active_conn_repr = SP_OBJECT_REPR(item);
1198 if (cc->active_conn_repr) {
1199 Inkscape::GC::anchor(cc->active_conn_repr);
1200 sp_repr_add_listener(cc->active_conn_repr, &shape_repr_events, cc);
1201 }
1203 for (int i = 0; i < 2; ++i) {
1205 // Create the handle if it doesn't exist
1206 if ( cc->endpt_handle[i] == NULL ) {
1207 SPKnot *knot = sp_knot_new(cc->desktop,
1208 _("<b>Connector endpoint</b>: drag to reroute or connect to new shapes"));
1210 knot->setShape(SP_KNOT_SHAPE_SQUARE);
1211 knot->setSize(7);
1212 knot->setAnchor(GTK_ANCHOR_CENTER);
1213 knot->setFill(0xffffff00, 0xff0000ff, 0xff0000ff);
1214 knot->setStroke(0x000000ff, 0x000000ff, 0x000000ff);
1215 sp_knot_update_ctrl(knot);
1217 // We don't want to use the standard knot handler,
1218 //since we don't want this knot to be draggable.
1219 g_signal_handler_disconnect(G_OBJECT(knot->item),
1220 knot->_event_handler_id);
1221 knot->_event_handler_id = 0;
1223 gtk_signal_connect(GTK_OBJECT(knot->item), "event",
1224 GTK_SIGNAL_FUNC(cc_generic_knot_handler), knot);
1226 cc->endpt_handle[i] = knot;
1227 }
1229 // Remove any existing handlers
1230 if (cc->endpt_handler_id[i]) {
1231 g_signal_handlers_disconnect_by_func(
1232 G_OBJECT(cc->endpt_handle[i]->item),
1233 (void*)G_CALLBACK(endpt_handler), (gpointer) cc );
1234 cc->endpt_handler_id[i] = 0;
1235 }
1237 // Setup handlers for connector endpoints, this is
1238 // is as 'after' so that cc_generic_knot_handler is
1239 // triggered first for any endpoint.
1240 cc->endpt_handler_id[i] = g_signal_connect_after(
1241 G_OBJECT(cc->endpt_handle[i]->item), "event",
1242 G_CALLBACK(endpt_handler), cc);
1243 }
1245 Geom::Point startpt = *(curve->first_point()) * i2d;
1246 sp_knot_set_position(cc->endpt_handle[0], startpt, 0);
1248 Geom::Point endpt = *(curve->last_point()) * i2d;
1249 sp_knot_set_position(cc->endpt_handle[1], endpt, 0);
1251 sp_knot_show(cc->endpt_handle[0]);
1252 sp_knot_show(cc->endpt_handle[1]);
1253 }
1256 static bool cc_item_is_shape(SPItem *item)
1257 {
1258 if (SP_IS_PATH(item)) {
1259 SPCurve *curve = (SP_SHAPE(item))->curve;
1260 if ( curve && !(curve->is_closed()) ) {
1261 // Open paths are connectors.
1262 return false;
1263 }
1264 }
1265 else if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
1266 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1267 if (prefs->getBool("/tools/connector/ignoretext", true)) {
1268 // Don't count text as a shape we can connect connector to.
1269 return false;
1270 }
1271 }
1272 return true;
1273 }
1276 bool cc_item_is_connector(SPItem *item)
1277 {
1278 if (SP_IS_PATH(item)) {
1279 if (SP_PATH(item)->connEndPair.isAutoRoutingConn()) {
1280 g_assert( !(SP_SHAPE(item)->curve->is_closed()) );
1281 return true;
1282 }
1283 }
1284 return false;
1285 }
1288 void cc_selection_set_avoid(bool const set_avoid)
1289 {
1290 SPDesktop *desktop = inkscape_active_desktop();
1291 if (desktop == NULL) {
1292 return;
1293 }
1295 SPDocument *document = sp_desktop_document(desktop);
1297 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1299 GSList *l = (GSList *) selection->itemList();
1301 int changes = 0;
1303 while (l) {
1304 SPItem *item = (SPItem *) l->data;
1306 char const *value = (set_avoid) ? "true" : NULL;
1308 if (cc_item_is_shape(item)) {
1309 sp_object_setAttribute(item, "inkscape:connector-avoid",
1310 value, false);
1311 item->avoidRef->handleSettingChange();
1312 changes++;
1313 }
1315 l = l->next;
1316 }
1318 if (changes == 0) {
1319 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE,
1320 _("Select <b>at least one non-connector object</b>."));
1321 return;
1322 }
1324 char *event_desc = (set_avoid) ?
1325 _("Make connectors avoid selected objects") :
1326 _("Make connectors ignore selected objects");
1327 sp_document_done(document, SP_VERB_CONTEXT_CONNECTOR, event_desc);
1328 }
1331 static void
1332 cc_selection_changed(Inkscape::Selection *selection, gpointer data)
1333 {
1334 SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(data);
1335 //SPEventContext *ec = SP_EVENT_CONTEXT(cc);
1337 SPItem *item = selection->singleItem();
1339 if (cc->active_conn == item)
1340 {
1341 // Nothing to change.
1342 return;
1343 }
1344 if (item == NULL)
1345 {
1346 cc_clear_active_conn(cc);
1347 return;
1348 }
1350 if (cc_item_is_connector(item)) {
1351 cc_set_active_conn(cc, item);
1352 }
1353 }
1356 static void
1357 shape_event_attr_deleted(Inkscape::XML::Node */*repr*/, Inkscape::XML::Node *child,
1358 Inkscape::XML::Node */*ref*/, gpointer data)
1359 {
1360 g_assert(data);
1361 SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(data);
1363 if (child == cc->active_shape_repr) {
1364 // The active shape has been deleted. Clear active shape.
1365 cc_clear_active_shape(cc);
1366 }
1367 }
1370 static void
1371 shape_event_attr_changed(Inkscape::XML::Node *repr, gchar const *name,
1372 gchar const */*old_value*/, gchar const */*new_value*/,
1373 bool /*is_interactive*/, gpointer data)
1374 {
1375 g_assert(data);
1376 SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(data);
1378 // Look for changes than result in onscreen movement.
1379 if (!strcmp(name, "d") || !strcmp(name, "x") || !strcmp(name, "y") ||
1380 !strcmp(name, "width") || !strcmp(name, "height") ||
1381 !strcmp(name, "transform"))
1382 {
1383 if (repr == cc->active_shape_repr) {
1384 // Active shape has moved. Clear active shape.
1385 cc_clear_active_shape(cc);
1386 }
1387 else if (repr == cc->active_conn_repr) {
1388 // The active conn has been moved.
1389 // Set it again, which just sets new handle positions.
1390 cc_set_active_conn(cc, cc->active_conn);
1391 }
1392 }
1393 }
1396 /*
1397 Local Variables:
1398 mode:c++
1399 c-file-style:"stroustrup"
1400 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1401 indent-tabs-mode:nil
1402 fill-column:99
1403 End:
1404 */
1405 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :