1 /*
2 * Connector creation tool
3 *
4 * Authors:
5 * Michael Wybrow <mjwybrow@users.sourceforge.net>
6 *
7 * Copyright (C) 2005 Michael Wybrow
8 *
9 * Released under GNU GPL, read the file 'COPYING' for more information
10 *
11 * TODO:
12 * o Have shapes avoid coonvex hulls of objects, rather than their
13 * bounding box. Possibly implement the unfinished ConvexHull
14 * class in libnr.
15 * (HOWEVER, using the convex hull C of a shape S does the wrong thing if a
16 * connector starts outside of S but inside C, or if the best route around
17 * an object involves going inside C but without entering S.)
18 * o Draw connectors to shape edges rather than bounding box.
19 * o Show a visual indicator for objects with the 'avoid' property set.
20 * o 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 "pixmaps/cursor-connector.pixbuf"
49 #include "xml/node-event-vector.h"
50 #include "xml/repr.h"
51 #include "svg/svg.h"
52 #include "desktop.h"
53 #include "desktop-style.h"
54 #include "desktop-affine.h"
55 #include "desktop-handles.h"
56 #include "document.h"
57 #include "message-context.h"
58 #include "message-stack.h"
59 #include "selection.h"
60 #include "inkscape.h"
61 #include "prefs-utils.h"
62 #include "sp-path.h"
63 #include "display/canvas-bpath.h"
64 #include "display/sodipodi-ctrl.h"
65 #include <glibmm/i18n.h>
66 #include "snap.h"
67 #include "knot.h"
68 #include "sp-conn-end.h"
69 #include "conn-avoid-ref.h"
70 #include "libavoid/vertices.h"
71 #include "context-fns.h"
72 #include "sp-namedview.h"
73 #include "sp-text.h"
74 #include "sp-flowtext.h"
77 static void sp_connector_context_class_init(SPConnectorContextClass *klass);
78 static void sp_connector_context_init(SPConnectorContext *conn_context);
79 static void sp_connector_context_dispose(GObject *object);
81 static void sp_connector_context_setup(SPEventContext *ec);
82 static void sp_connector_context_finish(SPEventContext *ec);
83 static gint sp_connector_context_root_handler(SPEventContext *ec, GdkEvent *event);
84 static gint sp_connector_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
86 // Stuff borrowed from DrawContext
87 static void spcc_connector_set_initial_point(SPConnectorContext *cc, NR::Point const p);
88 static void spcc_connector_set_subsequent_point(SPConnectorContext *cc, NR::Point const p);
89 static void spcc_connector_finish_segment(SPConnectorContext *cc, NR::Point p);
90 static void spcc_reset_colors(SPConnectorContext *cc);
91 static void spcc_connector_finish(SPConnectorContext *cc);
92 static void spcc_concat_colors_and_flush(SPConnectorContext *cc);
93 static void spcc_flush_white(SPConnectorContext *cc, SPCurve *gc);
95 // Context event handlers
96 static gint connector_handle_button_press(SPConnectorContext *const cc, GdkEventButton const &bevent);
97 static gint connector_handle_motion_notify(SPConnectorContext *const cc, GdkEventMotion const &mevent);
98 static gint connector_handle_button_release(SPConnectorContext *const cc, GdkEventButton const &revent);
99 static gint connector_handle_key_press(SPConnectorContext *const cc, guint const keyval);
101 static void cc_set_active_shape(SPConnectorContext *cc, SPItem *item);
102 static void cc_clear_active_shape(SPConnectorContext *cc);
103 static void cc_set_active_conn(SPConnectorContext *cc, SPItem *item);
104 static void cc_clear_active_conn(SPConnectorContext *cc);
105 static gchar *conn_pt_handle_test(SPConnectorContext *cc, NR::Point& w);
106 static bool cc_item_is_shape(SPItem *item);
107 static void cc_selection_changed(Inkscape::Selection *selection, gpointer data);
109 static void shape_event_attr_deleted(Inkscape::XML::Node *repr,
110 Inkscape::XML::Node *child, Inkscape::XML::Node *ref, gpointer data);
111 static void shape_event_attr_changed(Inkscape::XML::Node *repr, gchar const *name,
112 gchar const *old_value, gchar const *new_value, bool is_interactive,
113 gpointer data);
116 static NR::Point connector_drag_origin_w(0, 0);
117 static bool connector_within_tolerance = false;
118 static SPEventContextClass *parent_class;
121 static Inkscape::XML::NodeEventVector shape_repr_events = {
122 NULL, /* child_added */
123 NULL, /* child_added */
124 shape_event_attr_changed,
125 NULL, /* content_changed */
126 NULL /* order_changed */
127 };
129 static Inkscape::XML::NodeEventVector layer_repr_events = {
130 NULL, /* child_added */
131 shape_event_attr_deleted,
132 NULL, /* child_added */
133 NULL, /* content_changed */
134 NULL /* order_changed */
135 };
138 GType
139 sp_connector_context_get_type(void)
140 {
141 static GType type = 0;
142 if (!type) {
143 GTypeInfo info = {
144 sizeof(SPConnectorContextClass),
145 NULL, NULL,
146 (GClassInitFunc) sp_connector_context_class_init,
147 NULL, NULL,
148 sizeof(SPConnectorContext),
149 4,
150 (GInstanceInitFunc) sp_connector_context_init,
151 NULL, /* value_table */
152 };
153 type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPConnectorContext", &info, (GTypeFlags)0);
154 }
155 return type;
156 }
158 static void
159 sp_connector_context_class_init(SPConnectorContextClass *klass)
160 {
161 GObjectClass *object_class;
162 SPEventContextClass *event_context_class;
164 object_class = (GObjectClass *) klass;
165 event_context_class = (SPEventContextClass *) klass;
167 parent_class = (SPEventContextClass*)g_type_class_peek_parent(klass);
169 object_class->dispose = sp_connector_context_dispose;
171 event_context_class->setup = sp_connector_context_setup;
172 event_context_class->finish = sp_connector_context_finish;
173 event_context_class->root_handler = sp_connector_context_root_handler;
174 event_context_class->item_handler = sp_connector_context_item_handler;
175 }
178 static void
179 sp_connector_context_init(SPConnectorContext *cc)
180 {
181 SPEventContext *ec = SP_EVENT_CONTEXT(cc);
183 ec->cursor_shape = cursor_connector_xpm;
184 ec->cursor_pixbuf = gdk_pixbuf_new_from_inline(
185 -1,
186 cursor_connector_pixbuf,
187 FALSE,
188 NULL);
189 ec->hot_x = 1;
190 ec->hot_y = 1;
191 ec->xp = 0;
192 ec->yp = 0;
194 cc->red_color = 0xff00007f;
196 cc->newconn = NULL;
197 cc->newConnRef = NULL;
199 cc->sel_changed_connection = sigc::connection();
201 cc->active_shape = NULL;
202 cc->active_shape_repr = NULL;
203 cc->active_shape_layer_repr = NULL;
205 cc->active_conn = NULL;
206 cc->active_conn_repr = NULL;
208 cc->active_handle = NULL;
210 cc->clickeditem = NULL;
211 cc->clickedhandle = NULL;
213 cc->connpthandle = NULL;
214 for (int i = 0; i < 2; ++i) {
215 cc->endpt_handle[i] = NULL;
216 cc->endpt_handler_id[i] = 0;
217 }
218 cc->sid = NULL;
219 cc->eid = NULL;
220 cc->npoints = 0;
221 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
222 }
225 static void
226 sp_connector_context_dispose(GObject *object)
227 {
228 SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(object);
230 cc->sel_changed_connection.disconnect();
232 if (cc->connpthandle) {
233 g_object_unref(cc->connpthandle);
234 cc->connpthandle = NULL;
235 }
236 for (int i = 0; i < 2; ++i) {
237 if (cc->endpt_handle[1]) {
238 g_object_unref(cc->endpt_handle[i]);
239 cc->endpt_handle[i] = NULL;
240 }
241 }
242 if (cc->sid) {
243 g_free(cc->sid);
244 cc->sid = NULL;
245 }
246 if (cc->eid) {
247 g_free(cc->eid);
248 cc->eid = NULL;
249 }
250 g_assert( cc->newConnRef == NULL );
252 G_OBJECT_CLASS(parent_class)->dispose(object);
253 }
256 static void
257 sp_connector_context_setup(SPEventContext *ec)
258 {
259 SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(ec);
260 SPDesktop *dt = ec->desktop;
262 if (((SPEventContextClass *) parent_class)->setup) {
263 ((SPEventContextClass *) parent_class)->setup(ec);
264 }
266 cc->selection = sp_desktop_selection(dt);
268 cc->sel_changed_connection.disconnect();
269 cc->sel_changed_connection = cc->selection->connectChanged(
270 sigc::bind(sigc::ptr_fun(&cc_selection_changed),
271 (gpointer) cc));
273 /* Create red bpath */
274 cc->red_bpath = sp_canvas_bpath_new(sp_desktop_sketch(ec->desktop), NULL);
275 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cc->red_bpath), cc->red_color,
276 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
277 sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(cc->red_bpath), 0x00000000,
278 SP_WIND_RULE_NONZERO);
279 /* Create red curve */
280 cc->red_curve = sp_curve_new_sized(4);
282 /* Create green curve */
283 cc->green_curve = sp_curve_new_sized(64);
285 // Notice the initial selection.
286 cc_selection_changed(cc->selection, (gpointer) cc);
288 if (prefs_get_int_attribute("tools.connector", "selcue", 0) != 0) {
289 ec->enableSelectionCue();
290 }
292 // Make sure we see all enter events for canvas items,
293 // even if a mouse button is depressed.
294 dt->canvas->gen_all_enter_events = true;
295 }
298 static void
299 sp_connector_context_finish(SPEventContext *ec)
300 {
301 SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(ec);
303 spcc_connector_finish(cc);
305 if (((SPEventContextClass *) parent_class)->finish) {
306 ((SPEventContextClass *) parent_class)->finish(ec);
307 }
309 if (cc->selection) {
310 cc->selection = NULL;
311 }
312 cc_clear_active_shape(cc);
313 cc_clear_active_conn(cc);
315 // Restore the default event generating behaviour.
316 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(ec);
317 desktop->canvas->gen_all_enter_events = false;
318 }
321 //-----------------------------------------------------------------------------
324 static void
325 cc_clear_active_shape(SPConnectorContext *cc)
326 {
327 if (cc->active_shape == NULL) {
328 return;
329 }
330 g_assert( cc->active_shape_repr );
331 g_assert( cc->active_shape_layer_repr );
333 cc->active_shape = NULL;
335 if (cc->active_shape_repr) {
336 sp_repr_remove_listener_by_data(cc->active_shape_repr, cc);
337 Inkscape::GC::release(cc->active_shape_repr);
338 cc->active_shape_repr = NULL;
340 sp_repr_remove_listener_by_data(cc->active_shape_layer_repr, cc);
341 Inkscape::GC::release(cc->active_shape_layer_repr);
342 cc->active_shape_layer_repr = NULL;
343 }
345 // Hide the center connection point if it exists.
346 if (cc->connpthandle) {
347 sp_knot_hide(cc->connpthandle);
348 }
349 }
352 static void
353 cc_clear_active_conn(SPConnectorContext *cc)
354 {
355 if (cc->active_conn == NULL) {
356 return;
357 }
358 g_assert( cc->active_conn_repr );
360 cc->active_conn = NULL;
362 if (cc->active_conn_repr) {
363 sp_repr_remove_listener_by_data(cc->active_conn_repr, cc);
364 Inkscape::GC::release(cc->active_conn_repr);
365 cc->active_conn_repr = NULL;
366 }
368 // Hide the endpoint handles.
369 for (int i = 0; i < 2; ++i) {
370 if (cc->endpt_handle[i]) {
371 sp_knot_hide(cc->endpt_handle[i]);
372 }
373 }
374 }
377 static gchar *
378 conn_pt_handle_test(SPConnectorContext *cc, NR::Point& p)
379 {
380 // TODO: this will need to change when there are more connection
381 // points available for each shape.
383 SPKnot *centerpt = cc->connpthandle;
384 if (cc->active_handle && (cc->active_handle == centerpt))
385 {
386 p = centerpt->pos;
387 return g_strdup_printf("#%s", SP_OBJECT_ID(cc->active_shape));
388 }
389 return NULL;
390 }
394 static gint
395 sp_connector_context_item_handler(SPEventContext *ec, SPItem *item, GdkEvent *event)
396 {
397 gint ret = FALSE;
399 SPDesktop *desktop = ec->desktop;
401 SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(ec);
403 NR::Point p(event->button.x, event->button.y);
405 switch (event->type) {
406 case GDK_BUTTON_RELEASE:
407 if (event->button.button == 1) {
408 if ((cc->state == SP_CONNECTOR_CONTEXT_DRAGGING) &&
409 (connector_within_tolerance))
410 {
411 spcc_reset_colors(cc);
412 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
413 }
414 if (cc->state != SP_CONNECTOR_CONTEXT_IDLE) {
415 // Doing simething else like rerouting.
416 break;
417 }
418 // find out clicked item, disregarding groups, honoring Alt
419 SPItem *item_ungrouped = sp_event_context_find_item(desktop,
420 p, event->button.state & GDK_MOD1_MASK, TRUE);
422 if (event->button.state & GDK_SHIFT_MASK) {
423 cc->selection->toggle(item_ungrouped);
424 } else {
425 cc->selection->set(item_ungrouped);
426 }
427 ret = TRUE;
428 }
429 break;
430 case GDK_ENTER_NOTIFY:
431 {
432 if (cc_item_is_shape(item)) {
433 // This is a shape, so show connection point(s).
434 if (!(cc->active_shape) ||
435 // Don't show handle for another handle.
436 (item != ((SPItem *) cc->connpthandle))) {
437 cc_set_active_shape(cc, item);
438 }
439 }
440 ret = TRUE;
441 break;
442 }
443 default:
444 break;
445 }
447 return ret;
448 }
451 gint
452 sp_connector_context_root_handler(SPEventContext *ec, GdkEvent *event)
453 {
454 SPConnectorContext *const cc = SP_CONNECTOR_CONTEXT(ec);
456 gint ret = FALSE;
458 switch (event->type) {
459 case GDK_BUTTON_PRESS:
460 ret = connector_handle_button_press(cc, event->button);
461 break;
463 case GDK_MOTION_NOTIFY:
464 ret = connector_handle_motion_notify(cc, event->motion);
465 break;
467 case GDK_BUTTON_RELEASE:
468 ret = connector_handle_button_release(cc, event->button);
469 break;
470 case GDK_KEY_PRESS:
471 ret = connector_handle_key_press(cc, get_group0_keyval (&event->key));
472 break;
474 default:
475 break;
476 }
478 if (!ret) {
479 gint (*const parent_root_handler)(SPEventContext *, GdkEvent *)
480 = ((SPEventContextClass *) parent_class)->root_handler;
481 if (parent_root_handler) {
482 ret = parent_root_handler(ec, event);
483 }
484 }
486 return ret;
487 }
490 static gint
491 connector_handle_button_press(SPConnectorContext *const cc, GdkEventButton const &bevent)
492 {
493 NR::Point const event_w(bevent.x, bevent.y);
494 /* Find desktop coordinates */
495 NR::Point p = cc->desktop->w2d(event_w);
497 gint ret = FALSE;
498 if ( bevent.button == 1 ) {
500 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
502 if (Inkscape::have_viable_layer(desktop, cc->_message_context) == false) {
503 return TRUE;
504 }
506 NR::Point const event_w(bevent.x,
507 bevent.y);
508 connector_drag_origin_w = event_w;
509 connector_within_tolerance = true;
511 NR::Point const event_dt = cc->desktop->w2d(event_w);
512 switch (cc->state) {
513 case SP_CONNECTOR_CONTEXT_STOP:
514 /* This is allowed, if we just cancelled curve */
515 case SP_CONNECTOR_CONTEXT_IDLE:
516 {
517 if ( cc->npoints == 0 ) {
518 /* Set start anchor */
519 NR::Point p;
521 cc_clear_active_conn(cc);
523 SP_EVENT_CONTEXT_DESKTOP(cc)->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating new connector"));
525 /* Create green anchor */
526 p = event_dt;
528 // Test whether we clicked on a connection point
529 cc->sid = conn_pt_handle_test(cc, p);
531 if (!cc->sid) {
532 // This is the first point, so just snap it to the grid
533 // as there's no other points to go off.
534 SnapManager const &m = cc->desktop->namedview->snap_manager;
535 p = m.freeSnap(Inkscape::Snapper::SNAP_POINT | Inkscape::Snapper::BBOX_POINT,
536 p, NULL).getPoint();
537 }
538 spcc_connector_set_initial_point(cc, p);
540 }
541 cc->state = SP_CONNECTOR_CONTEXT_DRAGGING;
542 ret = TRUE;
543 break;
544 }
545 case SP_CONNECTOR_CONTEXT_DRAGGING:
546 {
547 // This is the second click of a connector creation.
549 spcc_connector_set_subsequent_point(cc, p);
550 spcc_connector_finish_segment(cc, p);
551 // Test whether we clicked on a connection point
552 cc->eid = conn_pt_handle_test(cc, p);
553 if (cc->npoints != 0) {
554 spcc_connector_finish(cc);
555 }
556 cc_set_active_conn(cc, cc->newconn);
557 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
558 ret = TRUE;
559 break;
560 }
561 case SP_CONNECTOR_CONTEXT_CLOSE:
562 {
563 g_warning("Button down in CLOSE state");
564 break;
565 }
566 default:
567 break;
568 }
569 } else if (bevent.button == 3) {
570 if (cc->npoints != 0) {
571 spcc_connector_finish(cc);
572 ret = TRUE;
573 }
574 }
575 return ret;
576 }
579 static gint
580 connector_handle_motion_notify(SPConnectorContext *const cc, GdkEventMotion const &mevent)
581 {
582 gint ret = FALSE;
584 if (mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) {
585 // allow middle-button scrolling
586 return FALSE;
587 }
589 NR::Point const event_w(mevent.x, mevent.y);
591 if (connector_within_tolerance) {
592 gint const tolerance = prefs_get_int_attribute_limited("options.dragtolerance",
593 "value", 0, 0, 100);
594 if ( NR::LInfty( event_w - connector_drag_origin_w ) < tolerance ) {
595 return FALSE; // Do not drag if we're within tolerance from origin.
596 }
597 }
598 // Once the user has moved farther than tolerance from the original location
599 // (indicating they intend to move the object, not click), then always process
600 // the motion notify coordinates as given (no snapping back to origin)
601 connector_within_tolerance = false;
603 SPDesktop *const dt = cc->desktop;
605 /* Find desktop coordinates */
606 NR::Point p = dt->w2d(event_w);
608 switch (cc->state) {
609 case SP_CONNECTOR_CONTEXT_DRAGGING:
610 {
611 // This is movement during a connector creation.
613 if ( cc->npoints > 0 ) {
614 cc->selection->clear();
615 spcc_connector_set_subsequent_point(cc, p);
616 ret = TRUE;
617 }
618 break;
619 }
620 case SP_CONNECTOR_CONTEXT_REROUTING:
621 {
622 g_assert( SP_IS_PATH(cc->clickeditem));
624 // Update the hidden path
625 NR::Matrix i2d = sp_item_i2d_affine(cc->clickeditem);
626 NR::Matrix d2i = i2d.inverse();
627 SPPath *path = SP_PATH(cc->clickeditem);
628 SPCurve *curve = (SP_SHAPE(path))->curve;
629 if (cc->clickedhandle == cc->endpt_handle[0]) {
630 NR::Point o = cc->endpt_handle[1]->pos;
631 sp_curve_stretch_endpoints(curve, p * d2i, o * d2i);
632 }
633 else {
634 NR::Point o = cc->endpt_handle[0]->pos;
635 sp_curve_stretch_endpoints(curve, o * d2i, p * d2i);
636 }
637 sp_conn_adjust_path(path);
639 // Copy this to the temporary visible path
640 cc->red_curve = sp_curve_copy(SP_SHAPE(path)->curve);
641 sp_curve_transform(cc->red_curve, i2d);
643 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), cc->red_curve);
644 ret = TRUE;
645 break;
646 }
647 case SP_CONNECTOR_CONTEXT_STOP:
648 /* This is perfectly valid */
649 break;
650 default:
651 break;
652 }
654 return ret;
655 }
658 static gint
659 connector_handle_button_release(SPConnectorContext *const cc, GdkEventButton const &revent)
660 {
661 gint ret = FALSE;
662 if ( revent.button == 1 ) {
664 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
665 SPDocument *doc = sp_desktop_document(desktop);
667 NR::Point const event_w(revent.x, revent.y);
669 /* Find desktop coordinates */
670 NR::Point p = cc->desktop->w2d(event_w);
672 switch (cc->state) {
673 //case SP_CONNECTOR_CONTEXT_POINT:
674 case SP_CONNECTOR_CONTEXT_DRAGGING:
675 {
676 if (connector_within_tolerance)
677 {
678 spcc_connector_finish_segment(cc, p);
679 return TRUE;
680 }
681 // Connector has been created via a drag, end it now.
682 spcc_connector_set_subsequent_point(cc, p);
683 spcc_connector_finish_segment(cc, p);
684 // Test whether we clicked on a connection point
685 cc->eid = conn_pt_handle_test(cc, p);
686 if (cc->npoints != 0) {
687 spcc_connector_finish(cc);
688 }
689 cc_set_active_conn(cc, cc->newconn);
690 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
691 break;
692 }
693 case SP_CONNECTOR_CONTEXT_REROUTING:
694 {
695 // Clear the temporary path:
696 sp_curve_reset(cc->red_curve);
697 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
699 // Test whether we clicked on a connection point
700 gchar *shape_label = conn_pt_handle_test(cc, p);
702 if (shape_label) {
703 if (cc->clickedhandle == cc->endpt_handle[0]) {
704 sp_object_setAttribute(cc->clickeditem,
705 "inkscape:connection-start",shape_label, false);
706 }
707 else {
708 sp_object_setAttribute(cc->clickeditem,
709 "inkscape:connection-end",shape_label, false);
710 }
711 g_free(shape_label);
712 }
713 cc->clickeditem->setHidden(false);
714 sp_conn_adjust_path(SP_PATH(cc->clickeditem));
715 cc->clickeditem->updateRepr();
716 sp_document_done(doc);
717 cc_set_active_conn(cc, cc->clickeditem);
718 sp_document_ensure_up_to_date(doc);
719 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
720 return TRUE;
721 break;
722 }
723 case SP_CONNECTOR_CONTEXT_STOP:
724 /* This is allowed, if we just cancelled curve */
725 break;
726 default:
727 break;
728 }
729 ret = TRUE;
730 }
732 return ret;
733 }
736 static gint
737 connector_handle_key_press(SPConnectorContext *const cc, guint const keyval)
738 {
739 gint ret = FALSE;
740 /* fixme: */
741 switch (keyval) {
742 case GDK_Return:
743 case GDK_KP_Enter:
744 if (cc->npoints != 0) {
745 spcc_connector_finish(cc);
746 ret = TRUE;
747 }
748 break;
749 case GDK_Escape:
750 if (cc->npoints != 0) {
751 // if drawing, cancel, otherwise pass it up for deselecting
752 cc->state = SP_CONNECTOR_CONTEXT_STOP;
753 spcc_reset_colors(cc);
754 ret = TRUE;
755 }
756 break;
757 default:
758 break;
759 }
760 return ret;
761 }
764 static void
765 spcc_reset_colors(SPConnectorContext *cc)
766 {
767 /* Red */
768 sp_curve_reset(cc->red_curve);
769 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
771 sp_curve_reset(cc->green_curve);
772 cc->npoints = 0;
773 }
776 static void
777 spcc_connector_set_initial_point(SPConnectorContext *const cc, NR::Point const p)
778 {
779 g_assert( cc->npoints == 0 );
781 cc->p[0] = p;
782 cc->p[1] = p;
783 cc->npoints = 2;
784 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
785 }
788 static void
789 spcc_connector_set_subsequent_point(SPConnectorContext *const cc, NR::Point const p)
790 {
791 g_assert( cc->npoints != 0 );
793 SPDesktop *dt = cc->desktop;
794 NR::Point o = dt->dt2doc(cc->p[0]);
795 NR::Point d = dt->dt2doc(p);
796 Avoid::Point src = { o[NR::X], o[NR::Y] };
797 Avoid::Point dst = { d[NR::X], d[NR::Y] };
799 if (!cc->newConnRef) {
800 Avoid::Router *router = sp_desktop_document(dt)->router;
801 cc->newConnRef = new Avoid::ConnRef(router, 0, src, dst);
802 cc->newConnRef->updateEndPoint(Avoid::VertID::src, src);
803 }
804 cc->newConnRef->updateEndPoint(Avoid::VertID::tar, dst);
806 cc->newConnRef->makePathInvalid();
807 cc->newConnRef->generatePath(src, dst);
809 Avoid::PolyLine route = cc->newConnRef->route();
810 cc->newConnRef->calcRouteDist();
812 sp_curve_reset(cc->red_curve);
813 NR::Point pt(route.ps[0].x, route.ps[0].y);
814 sp_curve_moveto(cc->red_curve, pt);
816 for (int i = 1; i < route.pn; ++i) {
817 NR::Point p(route.ps[i].x, route.ps[i].y);
818 sp_curve_lineto(cc->red_curve, p);
819 }
820 sp_curve_transform(cc->red_curve, dt->doc2dt());
821 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), cc->red_curve);
822 }
825 /**
826 * Concats red, blue and green.
827 * If any anchors are defined, process these, optionally removing curves from white list
828 * Invoke _flush_white to write result back to object.
829 */
830 static void
831 spcc_concat_colors_and_flush(SPConnectorContext *cc)
832 {
833 SPCurve *c = cc->green_curve;
834 cc->green_curve = sp_curve_new_sized(64);
836 sp_curve_reset(cc->red_curve);
837 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
839 if (sp_curve_empty(c)) {
840 sp_curve_unref(c);
841 return;
842 }
844 spcc_flush_white(cc, c);
846 sp_curve_unref(c);
847 }
850 /*
851 * Flushes white curve(s) and additional curve into object
852 *
853 * No cleaning of colored curves - this has to be done by caller
854 * No rereading of white data, so if you cannot rely on ::modified, do it in caller
855 *
856 */
858 static void
859 spcc_flush_white(SPConnectorContext *cc, SPCurve *gc)
860 {
861 SPCurve *c;
863 if (gc) {
864 c = gc;
865 sp_curve_ref(c);
866 } else {
867 return;
868 }
870 /* Now we have to go back to item coordinates at last */
871 sp_curve_transform(c,
872 sp_desktop_dt2root_affine(SP_EVENT_CONTEXT_DESKTOP(cc)));
874 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
875 SPDocument *doc = sp_desktop_document(desktop);
877 if ( c && !sp_curve_empty(c) ) {
878 /* We actually have something to write */
880 Inkscape::XML::Node *repr = sp_repr_new("svg:path");
881 /* Set style */
882 sp_desktop_apply_style_tool(desktop, repr, "tools.connector", false);
884 gchar *str = sp_svg_write_path(SP_CURVE_BPATH(c));
885 g_assert( str != NULL );
886 repr->setAttribute("d", str);
887 g_free(str);
889 /* Attach repr */
890 cc->newconn = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
891 cc->selection->set(repr);
892 Inkscape::GC::release(repr);
893 cc->newconn->transform = i2i_affine(desktop->currentRoot(), desktop->currentLayer());
894 cc->newconn->updateRepr();
896 bool connection = false;
897 sp_object_setAttribute(cc->newconn, "inkscape:connector-type",
898 "polyline", false);
899 if (cc->sid)
900 {
901 sp_object_setAttribute(cc->newconn, "inkscape:connection-start",
902 cc->sid, false);
903 connection = true;
904 }
906 if (cc->eid)
907 {
908 sp_object_setAttribute(cc->newconn, "inkscape:connection-end",
909 cc->eid, false);
910 connection = true;
911 }
912 cc->newconn->updateRepr();
913 if (connection) {
914 // Adjust endpoints to shape edge.
915 sp_conn_adjust_path(SP_PATH(cc->newconn));
916 }
917 cc->newconn->updateRepr();
918 }
920 sp_curve_unref(c);
922 /* Flush pending updates */
923 sp_document_done(doc);
924 sp_document_ensure_up_to_date(doc);
925 }
928 static void
929 spcc_connector_finish_segment(SPConnectorContext *const cc, NR::Point const p)
930 {
931 if (!sp_curve_empty(cc->red_curve)) {
932 sp_curve_append_continuous(cc->green_curve, cc->red_curve, 0.0625);
934 cc->p[0] = cc->p[3];
935 cc->p[1] = cc->p[4];
936 cc->npoints = 2;
938 sp_curve_reset(cc->red_curve);
939 }
940 }
943 static void
944 spcc_connector_finish(SPConnectorContext *const cc)
945 {
946 SPDesktop *const desktop = cc->desktop;
947 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Finishing connector"));
949 sp_curve_reset(cc->red_curve);
950 spcc_concat_colors_and_flush(cc);
952 cc->npoints = 0;
954 if (cc->newConnRef) {
955 cc->newConnRef->removeFromGraph();
956 delete cc->newConnRef;
957 cc->newConnRef = NULL;
958 }
959 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
960 }
963 static gboolean
964 cc_generic_knot_handler(SPCanvasItem *, GdkEvent *event, SPKnot *knot)
965 {
966 g_assert (knot != NULL);
968 g_object_ref(knot);
970 SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(
971 knot->desktop->event_context);
973 gboolean consumed = FALSE;
975 switch (event->type) {
976 case GDK_ENTER_NOTIFY:
977 sp_knot_set_flag(knot, SP_KNOT_MOUSEOVER, TRUE);
979 cc->active_handle = knot;
981 if (knot->tip)
982 {
983 knot->desktop->event_context->defaultMessageContext()->set(
984 Inkscape::NORMAL_MESSAGE, knot->tip);
985 }
987 consumed = TRUE;
988 break;
989 case GDK_LEAVE_NOTIFY:
990 sp_knot_set_flag(knot, SP_KNOT_MOUSEOVER, FALSE);
992 cc->active_handle = NULL;
994 if (knot->tip) {
995 knot->desktop->event_context->defaultMessageContext()->clear();
996 }
998 consumed = TRUE;
999 break;
1000 default:
1001 break;
1002 }
1004 g_object_unref(knot);
1006 return consumed;
1007 }
1010 static gboolean
1011 endpt_handler(SPKnot *knot, GdkEvent *event, SPConnectorContext *cc)
1012 {
1013 g_assert( SP_IS_CONNECTOR_CONTEXT(cc) );
1015 gboolean consumed = FALSE;
1017 switch (event->type) {
1018 case GDK_BUTTON_PRESS:
1019 g_assert( (cc->active_handle == cc->endpt_handle[0]) ||
1020 (cc->active_handle == cc->endpt_handle[1]) );
1021 if (cc->state == SP_CONNECTOR_CONTEXT_IDLE) {
1022 cc->clickeditem = cc->active_conn;
1023 cc->clickedhandle = cc->active_handle;
1024 cc_clear_active_conn(cc);
1025 cc->state = SP_CONNECTOR_CONTEXT_REROUTING;
1027 // Disconnect from attached shape
1028 unsigned ind = (cc->active_handle == cc->endpt_handle[0]) ? 0 : 1;
1029 sp_conn_end_detach(cc->clickeditem, ind);
1031 NR::Point origin;
1032 if (cc->clickedhandle == cc->endpt_handle[0]) {
1033 origin = cc->endpt_handle[1]->pos;
1034 }
1035 else {
1036 origin = cc->endpt_handle[0]->pos;
1037 }
1039 // Show the red path for dragging.
1040 cc->red_curve = sp_curve_copy(SP_PATH(cc->clickeditem)->curve);
1041 NR::Matrix i2d = sp_item_i2d_affine(cc->clickeditem);
1042 sp_curve_transform(cc->red_curve, i2d);
1043 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath),
1044 cc->red_curve);
1046 cc->clickeditem->setHidden(true);
1048 // The rest of the interaction rerouting the connector is
1049 // handled by the context root handler.
1050 consumed = TRUE;
1051 }
1052 break;
1053 default:
1054 break;
1055 }
1057 return consumed;
1058 }
1061 static void cc_set_active_shape(SPConnectorContext *cc, SPItem *item)
1062 {
1063 g_assert(item != NULL );
1065 cc->active_shape = item;
1067 // Remove existing active shape listeners
1068 if (cc->active_shape_repr) {
1069 sp_repr_remove_listener_by_data(cc->active_shape_repr, cc);
1070 Inkscape::GC::release(cc->active_shape_repr);
1072 sp_repr_remove_listener_by_data(cc->active_shape_layer_repr, cc);
1073 Inkscape::GC::release(cc->active_shape_layer_repr);
1074 }
1076 // Listen in case the active shape changes
1077 cc->active_shape_repr = SP_OBJECT_REPR(item);
1078 if (cc->active_shape_repr) {
1079 Inkscape::GC::anchor(cc->active_shape_repr);
1080 sp_repr_add_listener(cc->active_shape_repr, &shape_repr_events, cc);
1082 cc->active_shape_layer_repr = cc->active_shape_repr->parent();
1083 Inkscape::GC::anchor(cc->active_shape_layer_repr);
1084 sp_repr_add_listener(cc->active_shape_layer_repr, &layer_repr_events, cc);
1085 }
1088 // Set center connection point.
1089 if ( cc->connpthandle == NULL ) {
1090 SPKnot *knot = sp_knot_new(cc->desktop,
1091 _("<b>Connection point</b>: click or drag to create a new connector"));
1093 knot->setShape(SP_KNOT_SHAPE_SQUARE);
1094 knot->setSize(8);
1095 knot->setAnchor(GTK_ANCHOR_CENTER);
1096 knot->setFill(0xffffff00, 0xff0000ff, 0xff0000ff);
1097 sp_knot_update_ctrl(knot);
1099 // We don't want to use the standard knot handler,
1100 //since we don't want this knot to be draggable.
1101 g_signal_handler_disconnect(G_OBJECT(knot->item),
1102 knot->_event_handler_id);
1103 knot->_event_handler_id = 0;
1105 gtk_signal_connect(GTK_OBJECT(knot->item), "event",
1106 GTK_SIGNAL_FUNC(cc_generic_knot_handler), knot);
1108 cc->connpthandle = knot;
1109 }
1112 NR::Rect bbox = sp_item_bbox_desktop(cc->active_shape);
1113 NR::Point center = bbox.midpoint();
1114 sp_knot_set_position(cc->connpthandle, ¢er, 0);
1116 sp_knot_show(cc->connpthandle);
1118 }
1121 static void
1122 cc_set_active_conn(SPConnectorContext *cc, SPItem *item)
1123 {
1124 g_assert( SP_IS_PATH(item) );
1126 SPCurve *curve = SP_SHAPE(SP_PATH(item))->curve;
1127 NR::Matrix i2d = sp_item_i2d_affine(item);
1129 if (cc->active_conn == item)
1130 {
1131 // Just adjust handle positions.
1132 NR::Point startpt = sp_curve_first_point(curve) * i2d;
1133 sp_knot_set_position(cc->endpt_handle[0], &startpt, 0);
1135 NR::Point endpt = sp_curve_last_point(curve) * i2d;
1136 sp_knot_set_position(cc->endpt_handle[1], &endpt, 0);
1138 return;
1139 }
1141 cc->active_conn = item;
1143 // Remove existing active conn listeners
1144 if (cc->active_conn_repr) {
1145 sp_repr_remove_listener_by_data(cc->active_conn_repr, cc);
1146 Inkscape::GC::release(cc->active_conn_repr);
1147 cc->active_conn_repr = NULL;
1148 }
1150 // Listen in case the active conn changes
1151 cc->active_conn_repr = SP_OBJECT_REPR(item);
1152 if (cc->active_conn_repr) {
1153 Inkscape::GC::anchor(cc->active_conn_repr);
1154 sp_repr_add_listener(cc->active_conn_repr, &shape_repr_events, cc);
1155 }
1157 for (int i = 0; i < 2; ++i) {
1159 // Create the handle if it doesn't exist
1160 if ( cc->endpt_handle[i] == NULL ) {
1161 SPKnot *knot = sp_knot_new(cc->desktop,
1162 _("<b>Connector endpoint</b>: drag to reroute or connect to new shapes"));
1164 knot->setShape(SP_KNOT_SHAPE_SQUARE);
1165 knot->setSize(7);
1166 knot->setAnchor(GTK_ANCHOR_CENTER);
1167 knot->setFill(0xffffff00, 0xff0000ff, 0xff0000ff);
1168 knot->setStroke(0x000000ff, 0x000000ff, 0x000000ff);
1169 sp_knot_update_ctrl(knot);
1171 // We don't want to use the standard knot handler,
1172 //since we don't want this knot to be draggable.
1173 g_signal_handler_disconnect(G_OBJECT(knot->item),
1174 knot->_event_handler_id);
1175 knot->_event_handler_id = 0;
1177 gtk_signal_connect(GTK_OBJECT(knot->item), "event",
1178 GTK_SIGNAL_FUNC(cc_generic_knot_handler), knot);
1180 cc->endpt_handle[i] = knot;
1181 }
1183 // Remove any existing handlers
1184 if (cc->endpt_handler_id[i]) {
1185 g_signal_handlers_disconnect_by_func(
1186 G_OBJECT(cc->endpt_handle[i]->item),
1187 (void*)G_CALLBACK(endpt_handler), (gpointer) cc );
1188 cc->endpt_handler_id[i] = 0;
1189 }
1191 // Setup handlers for connector endpoints, this is
1192 // is as 'after' so that cc_generic_knot_handler is
1193 // triggered first for any endpoint.
1194 cc->endpt_handler_id[i] = g_signal_connect_after(
1195 G_OBJECT(cc->endpt_handle[i]->item), "event",
1196 G_CALLBACK(endpt_handler), cc);
1197 }
1199 NR::Point startpt = sp_curve_first_point(curve) * i2d;
1200 sp_knot_set_position(cc->endpt_handle[0], &startpt, 0);
1202 NR::Point endpt = sp_curve_last_point(curve) * i2d;
1203 sp_knot_set_position(cc->endpt_handle[1], &endpt, 0);
1205 sp_knot_show(cc->endpt_handle[0]);
1206 sp_knot_show(cc->endpt_handle[1]);
1207 }
1210 static bool cc_item_is_shape(SPItem *item)
1211 {
1212 if (SP_IS_PATH(item)) {
1213 SPCurve *curve = (SP_SHAPE(item))->curve;
1214 if ( curve && !(curve->closed) ) {
1215 // Open paths are connectors.
1216 return false;
1217 }
1218 }
1219 else if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
1220 if (prefs_get_int_attribute("tools.connector", "ignoretext", 1) == 1) {
1221 // Don't count text as a shape we can connect connector to.
1222 return false;
1223 }
1224 }
1225 return true;
1226 }
1229 bool cc_item_is_connector(SPItem *item)
1230 {
1231 if (SP_IS_PATH(item)) {
1232 if (SP_PATH(item)->connEndPair.isAutoRoutingConn()) {
1233 g_assert( !(SP_SHAPE(item)->curve->closed) );
1234 return true;
1235 }
1236 }
1237 return false;
1238 }
1241 void cc_selection_set_avoid(bool const set_avoid)
1242 {
1243 SPDesktop *desktop = inkscape_active_desktop();
1244 if (desktop == NULL) {
1245 return;
1246 }
1248 SPDocument *document = sp_desktop_document(desktop);
1250 Inkscape::Selection *selection = sp_desktop_selection(desktop);
1252 GSList *l = (GSList *) selection->itemList();
1254 int changes = 0;
1256 while (l) {
1257 SPItem *item = (SPItem *) l->data;
1259 char const *value = (set_avoid) ? "true" : NULL;
1261 if (cc_item_is_shape(item)) {
1262 sp_object_setAttribute(item, "inkscape:connector-avoid",
1263 value, false);
1264 item->avoidRef->handleSettingChange();
1265 changes++;
1266 }
1268 l = l->next;
1269 }
1271 if (changes == 0) {
1272 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE,
1273 _("Select <b>at least one non-connector object</b>."));
1274 return;
1275 }
1277 sp_document_done(document);
1278 }
1281 static void
1282 cc_selection_changed(Inkscape::Selection *selection, gpointer data)
1283 {
1284 SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(data);
1285 //SPEventContext *ec = SP_EVENT_CONTEXT(cc);
1287 SPItem *item = selection->singleItem();
1289 if (cc->active_conn == item)
1290 {
1291 // Nothing to change.
1292 return;
1293 }
1294 if (item == NULL)
1295 {
1296 cc_clear_active_conn(cc);
1297 return;
1298 }
1300 if (cc_item_is_connector(item)) {
1301 cc_set_active_conn(cc, item);
1302 }
1303 }
1306 static void
1307 shape_event_attr_deleted(Inkscape::XML::Node *repr, Inkscape::XML::Node *child,
1308 Inkscape::XML::Node *ref, gpointer data)
1309 {
1310 g_assert(data);
1311 SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(data);
1313 if (child == cc->active_shape_repr) {
1314 // The active shape has been deleted. Clear active shape.
1315 cc_clear_active_shape(cc);
1316 }
1317 }
1320 static void
1321 shape_event_attr_changed(Inkscape::XML::Node *repr, gchar const *name,
1322 gchar const *old_value, gchar const *new_value,
1323 bool is_interactive, gpointer data)
1324 {
1325 g_assert(data);
1326 SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(data);
1328 // Look for changes than result in onscreen movement.
1329 if (!strcmp(name, "d") || !strcmp(name, "x") || !strcmp(name, "y") ||
1330 !strcmp(name, "width") || !strcmp(name, "height") ||
1331 !strcmp(name, "transform"))
1332 {
1333 if (repr == cc->active_shape_repr) {
1334 // Active shape has moved. Clear active shape.
1335 cc_clear_active_shape(cc);
1336 }
1337 else if (repr == cc->active_conn_repr) {
1338 // The active conn has been moved.
1339 // Set it again, which just sets new handle positions.
1340 cc_set_active_conn(cc, cc->active_conn);
1341 }
1342 }
1343 }
1346 /*
1347 Local Variables:
1348 mode:c++
1349 c-file-style:"stroustrup"
1350 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1351 indent-tabs-mode:nil
1352 fill-column:99
1353 End:
1354 */
1355 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :