Code

79872536351f4f6e9f9ce72dc2f5bc7591298830
[inkscape.git] / src / connector-context.cpp
1 /*
2  * Connector creation tool
3  *
4  * Authors:
5  *   Michael Wybrow <mjwybrow@users.sourceforge.net>
6  *
7  * Copyright (C) 2005 Michael Wybrow
8  *
9  * Released under GNU GPL, read the file 'COPYING' for more information
10  *
11  * TODO:
12  *  o  Have shapes avoid coonvex hulls of objects, rather than their
13  *     bounding box.  Possibly implement the unfinished ConvexHull
14  *     class in libnr.
15  *     (HOWEVER, using the convex hull C of a shape S does the wrong thing if a
16  *     connector starts outside of S but inside C, or if the best route around
17  *     an object involves going inside C but without entering S.)
18  *  o  Draw connectors to shape edges rather than bounding box.
19  *  o  Show a visual indicator for objects with the 'avoid' property set.
20  *  o  Create an interface for setting markers (arrow heads).
21  *  o  Better distinguish between paths and connectors to prevent problems
22  *     in the node tool and paths accidently being turned into connectors
23  *     in the connector tool.  Perhaps have a way to convert between.
24  *  o  Only call libavoid's updateEndPoint as required.  Currently we do it
25  *     for both endpoints, even if only one is moving.
26  *  o  Cleanup to remove unecessary borrowed DrawContext code.
27  *  o  Allow user-placeable connection points.
28  *  o  Deal sanely with connectors with both endpoints attached to the
29  *     same connection point, and drawing of connectors attaching
30  *     overlaping shapes (currently tries to adjust connector to be
31  *     outside both bounding boxes).
32  *  o  Fix many special cases related to connectors updating,
33  *     e.g., copying a couple of shapes and a connector that are
34  *           attached to each other.
35  *     e.g., detach connector when it is moved or transformed in
36  *           one of the other contexts.
37  *  o  Cope with shapes whose ids change when they have attached
38  *     connectors.
39  *  o  gobble_motion_events(GDK_BUTTON1_MASK)?;
40  *
41  */
43 #include <gdk/gdkkeysyms.h>
45 #include "connector-context.h"
46 #include "pixmaps/cursor-connector.xpm"
47 #include "xml/node-event-vector.h"
48 #include "xml/repr.h"
49 #include "svg/svg.h"
50 #include "desktop.h"
51 #include "desktop-style.h"
52 #include "desktop-affine.h"
53 #include "desktop-handles.h"
54 #include "document.h"
55 #include "message-context.h"
56 #include "message-stack.h"
57 #include "selection.h"
58 #include "inkscape.h"
59 #include "prefs-utils.h"
60 #include "sp-path.h"
61 #include "display/canvas-bpath.h"
62 #include "display/sodipodi-ctrl.h"
63 #include <glibmm/i18n.h>
64 #include "snap.h"
65 #include "knot.h"
66 #include "sp-conn-end.h"
67 #include "conn-avoid-ref.h"
68 #include "libavoid/vertices.h"
69 #include "context-fns.h"
70 #include "sp-namedview.h"
72 static void sp_connector_context_class_init(SPConnectorContextClass *klass);
73 static void sp_connector_context_init(SPConnectorContext *conn_context);
74 static void sp_connector_context_dispose(GObject *object);
76 static void sp_connector_context_setup(SPEventContext *ec);
77 static void sp_connector_context_finish(SPEventContext *ec);
78 static gint sp_connector_context_root_handler(SPEventContext *ec, GdkEvent *event);
79 static gint sp_connector_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
81 // Stuff borrowed from DrawContext
82 static void spcc_connector_set_initial_point(SPConnectorContext *cc, NR::Point const p);
83 static void spcc_connector_set_subsequent_point(SPConnectorContext *cc, NR::Point const p);
84 static void spcc_connector_finish_segment(SPConnectorContext *cc, NR::Point p);
85 static void spcc_reset_colors(SPConnectorContext *cc);
86 static void spcc_connector_finish(SPConnectorContext *cc);
87 static void spcc_concat_colors_and_flush(SPConnectorContext *cc);
88 static void spcc_flush_white(SPConnectorContext *cc, SPCurve *gc);
90 // Context event handlers
91 static gint connector_handle_button_press(SPConnectorContext *const cc, GdkEventButton const &bevent);
92 static gint connector_handle_motion_notify(SPConnectorContext *const cc, GdkEventMotion const &mevent);
93 static gint connector_handle_button_release(SPConnectorContext *const cc, GdkEventButton const &revent);
94 static gint connector_handle_key_press(SPConnectorContext *const cc, guint const keyval);
96 static void cc_set_active_shape(SPConnectorContext *cc, SPItem *item);
97 static void cc_clear_active_shape(SPConnectorContext *cc);
98 static void cc_set_active_conn(SPConnectorContext *cc, SPItem *item);
99 static void cc_clear_active_conn(SPConnectorContext *cc);
100 static gchar *conn_pt_handle_test(SPConnectorContext *cc, NR::Point& w);
101 static bool cc_item_is_shape(SPItem *item);
102 static void cc_selection_changed(Inkscape::Selection *selection, gpointer data);
104 static void shape_event_attr_deleted(Inkscape::XML::Node *repr,
105         Inkscape::XML::Node *child, Inkscape::XML::Node *ref, gpointer data);
106 static void shape_event_attr_changed(Inkscape::XML::Node *repr, gchar const *name,
107         gchar const *old_value, gchar const *new_value, bool is_interactive,
108         gpointer data);
111 static NR::Point connector_drag_origin_w(0, 0);
112 static bool connector_within_tolerance = false;
113 static SPEventContextClass *parent_class;
116 static Inkscape::XML::NodeEventVector shape_repr_events = {
117     NULL, /* child_added */
118     NULL, /* child_added */
119     shape_event_attr_changed,
120     NULL, /* content_changed */
121     NULL  /* order_changed */
122 };
124 static Inkscape::XML::NodeEventVector layer_repr_events = {
125     NULL, /* child_added */
126     shape_event_attr_deleted,
127     NULL, /* child_added */
128     NULL, /* content_changed */
129     NULL  /* order_changed */
130 };
133 GType
134 sp_connector_context_get_type(void)
136     static GType type = 0;
137     if (!type) {
138         GTypeInfo info = {
139             sizeof(SPConnectorContextClass),
140             NULL, NULL,
141             (GClassInitFunc) sp_connector_context_class_init,
142             NULL, NULL,
143             sizeof(SPConnectorContext),
144             4,
145             (GInstanceInitFunc) sp_connector_context_init,
146             NULL,   /* value_table */
147         };
148         type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPConnectorContext", &info, (GTypeFlags)0);
149     }
150     return type;
153 static void
154 sp_connector_context_class_init(SPConnectorContextClass *klass)
156     GObjectClass *object_class;
157     SPEventContextClass *event_context_class;
159     object_class = (GObjectClass *) klass;
160     event_context_class = (SPEventContextClass *) klass;
162     parent_class = (SPEventContextClass*)g_type_class_peek_parent(klass);
164     object_class->dispose = sp_connector_context_dispose;
166     event_context_class->setup = sp_connector_context_setup;
167     event_context_class->finish = sp_connector_context_finish;
168     event_context_class->root_handler = sp_connector_context_root_handler;
169     event_context_class->item_handler = sp_connector_context_item_handler;
173 static void
174 sp_connector_context_init(SPConnectorContext *cc)
176     SPEventContext *ec = SP_EVENT_CONTEXT(cc);
178     ec->cursor_shape = cursor_connector_xpm;
179     ec->hot_x = 1;
180     ec->hot_y = 1;
181     ec->xp = 0;
182     ec->yp = 0;
184     cc->red_color = 0xff00007f;
186     cc->newconn = NULL;
187     cc->newConnRef = NULL;
189     cc->sel_changed_connection = sigc::connection();
191     cc->active_shape = NULL;
192     cc->active_shape_repr = NULL;
193     cc->active_shape_layer_repr = NULL;
195     cc->active_conn = NULL;
196     cc->active_conn_repr = NULL;
198     cc->active_handle = NULL;
200     cc->clickeditem = NULL;
201     cc->clickedhandle = NULL;
203     cc->connpthandle = NULL;
204     for (int i = 0; i < 2; ++i) {
205         cc->endpt_handle[i] = NULL;
206         cc->endpt_handler_id[i] = 0;
207     }
208     cc->sid = NULL;
209     cc->eid = NULL;
210     cc->npoints = 0;
211     cc->state = SP_CONNECTOR_CONTEXT_IDLE;
215 static void
216 sp_connector_context_dispose(GObject *object)
218     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(object);
220     cc->sel_changed_connection.disconnect();
222     if (cc->connpthandle) {
223         g_object_unref(cc->connpthandle);
224         cc->connpthandle = NULL;
225     }
226     for (int i = 0; i < 2; ++i) {
227         if (cc->endpt_handle[1]) {
228             g_object_unref(cc->endpt_handle[i]);
229             cc->endpt_handle[i] = NULL;
230         }
231     }
232     if (cc->sid) {
233         g_free(cc->sid);
234         cc->sid = NULL;
235     }
236     if (cc->eid) {
237         g_free(cc->eid);
238         cc->eid = NULL;
239     }
240     g_assert( cc->newConnRef == NULL );
242     G_OBJECT_CLASS(parent_class)->dispose(object);
246 static void
247 sp_connector_context_setup(SPEventContext *ec)
249     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(ec);
250     SPDesktop *dt = ec->desktop;
252     if (((SPEventContextClass *) parent_class)->setup) {
253         ((SPEventContextClass *) parent_class)->setup(ec);
254     }
256     cc->selection = sp_desktop_selection(dt);
258     cc->sel_changed_connection.disconnect();
259     cc->sel_changed_connection = cc->selection->connectChanged(
260             sigc::bind(sigc::ptr_fun(&cc_selection_changed),
261             (gpointer) cc));
263     /* Create red bpath */
264     cc->red_bpath = sp_canvas_bpath_new(sp_desktop_sketch(ec->desktop), NULL);
265     sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cc->red_bpath), cc->red_color,
266             1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
267     sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(cc->red_bpath), 0x00000000,
268             SP_WIND_RULE_NONZERO);
269     /* Create red curve */
270     cc->red_curve = sp_curve_new_sized(4);
272     /* Create green curve */
273     cc->green_curve = sp_curve_new_sized(64);
275     // Notice the initial selection.
276     cc_selection_changed(cc->selection, (gpointer) cc);
278     if (prefs_get_int_attribute("tools.connector", "selcue", 0) != 0) {
279         ec->enableSelectionCue();
280     }
282     // Make sure we see all enter events for canvas items,
283     // even if a mouse button is depressed.
284     dt->canvas->gen_all_enter_events = true;
288 static void
289 sp_connector_context_finish(SPEventContext *ec)
291     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(ec);
293     spcc_connector_finish(cc);
295     if (((SPEventContextClass *) parent_class)->finish) {
296         ((SPEventContextClass *) parent_class)->finish(ec);
297     }
299     if (cc->selection) {
300         cc->selection = NULL;
301     }
302     cc_clear_active_shape(cc);
303     cc_clear_active_conn(cc);
305     // Restore the default event generating behaviour.
306     SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(ec);
307     desktop->canvas->gen_all_enter_events = false;
311 //-----------------------------------------------------------------------------
314 static void
315 cc_clear_active_shape(SPConnectorContext *cc)
317     if (cc->active_shape == NULL) {
318         return;
319     }
320     g_assert( cc->active_shape_repr );
321     g_assert( cc->active_shape_layer_repr );
323     cc->active_shape = NULL;
325     if (cc->active_shape_repr) {
326         sp_repr_remove_listener_by_data(cc->active_shape_repr, cc);
327         Inkscape::GC::release(cc->active_shape_repr);
328         cc->active_shape_repr = NULL;
330         sp_repr_remove_listener_by_data(cc->active_shape_layer_repr, cc);
331         Inkscape::GC::release(cc->active_shape_layer_repr);
332         cc->active_shape_layer_repr = NULL;
333     }
335     // Hide the center connection point if it exists.
336     if (cc->connpthandle) {
337         sp_knot_hide(cc->connpthandle);
338     }
342 static void
343 cc_clear_active_conn(SPConnectorContext *cc)
345     if (cc->active_conn == NULL) {
346         return;
347     }
348     g_assert( cc->active_conn_repr );
350     cc->active_conn = NULL;
352     if (cc->active_conn_repr) {
353         sp_repr_remove_listener_by_data(cc->active_conn_repr, cc);
354         Inkscape::GC::release(cc->active_conn_repr);
355         cc->active_conn_repr = NULL;
356     }
358     // Hide the endpoint handles.
359     for (int i = 0; i < 2; ++i) {
360         if (cc->endpt_handle[i]) {
361             sp_knot_hide(cc->endpt_handle[i]);
362         }
363     }
367 static gchar *
368 conn_pt_handle_test(SPConnectorContext *cc, NR::Point& p)
370     // TODO: this will need to change when there are more connection
371     //       points available for each shape.
373     SPKnot *centerpt = cc->connpthandle;
374     if (cc->active_handle && (cc->active_handle == centerpt))
375     {
376         p = centerpt->pos;
377         return g_strdup_printf("#%s", SP_OBJECT_ID(cc->active_shape));
378     }
379     return NULL;
384 static gint
385 sp_connector_context_item_handler(SPEventContext *ec, SPItem *item, GdkEvent *event)
387     gint ret = FALSE;
389     SPDesktop *desktop = ec->desktop;
391     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(ec);
393     NR::Point p(event->button.x, event->button.y);
395     switch (event->type) {
396         case GDK_BUTTON_RELEASE:
397             if (event->button.button == 1) {
398                 if ((cc->state == SP_CONNECTOR_CONTEXT_DRAGGING) &&
399                         (connector_within_tolerance))
400                 {
401                     spcc_reset_colors(cc);
402                     cc->state = SP_CONNECTOR_CONTEXT_IDLE;
403                 }
404                 if (cc->state != SP_CONNECTOR_CONTEXT_IDLE) {
405                     // Doing simething else like rerouting.
406                     break;
407                 }
408                 // find out clicked item, disregarding groups, honoring Alt
409                 SPItem *item_ungrouped = sp_event_context_find_item(desktop,
410                         p, event->button.state & GDK_MOD1_MASK, TRUE);
412                 if (event->button.state & GDK_SHIFT_MASK) {
413                     cc->selection->toggle(item_ungrouped);
414                 } else {
415                     cc->selection->set(item_ungrouped);
416                 }
417                 ret = TRUE;
418             }
419             break;
420         case GDK_ENTER_NOTIFY:
421         {
422             if (cc_item_is_shape(item)) {
423                 // This is a shape, so show connection point(s).
424                 if (!(cc->active_shape) ||
425                         // Don't show handle for another handle.
426                         (item != ((SPItem *) cc->connpthandle))) {
427                     cc_set_active_shape(cc, item);
428                 }
429             }
430             ret = TRUE;
431             break;
432         }
433         default:
434             break;
435     }
437     return ret;
441 gint
442 sp_connector_context_root_handler(SPEventContext *ec, GdkEvent *event)
444     SPConnectorContext *const cc = SP_CONNECTOR_CONTEXT(ec);
446     gint ret = FALSE;
448     switch (event->type) {
449         case GDK_BUTTON_PRESS:
450             ret = connector_handle_button_press(cc, event->button);
451             break;
453         case GDK_MOTION_NOTIFY:
454             ret = connector_handle_motion_notify(cc, event->motion);
455             break;
457         case GDK_BUTTON_RELEASE:
458             ret = connector_handle_button_release(cc, event->button);
459             break;
460         case GDK_KEY_PRESS:
461             ret = connector_handle_key_press(cc, get_group0_keyval (&event->key));
462             break;
464         default:
465             break;
466     }
468     if (!ret) {
469         gint (*const parent_root_handler)(SPEventContext *, GdkEvent *)
470             = ((SPEventContextClass *) parent_class)->root_handler;
471         if (parent_root_handler) {
472             ret = parent_root_handler(ec, event);
473         }
474     }
476     return ret;
480 static gint
481 connector_handle_button_press(SPConnectorContext *const cc, GdkEventButton const &bevent)
483     NR::Point const event_w(bevent.x, bevent.y);
484     /* Find desktop coordinates */
485     NR::Point p = cc->desktop->w2d(event_w);
487     gint ret = FALSE;
488     if ( bevent.button == 1 ) {
490         SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
492         if (Inkscape::have_viable_layer(desktop, cc->_message_context) == false) {
493             return TRUE;
494         }
496         NR::Point const event_w(bevent.x,
497                                 bevent.y);
498         connector_drag_origin_w = event_w;
499         connector_within_tolerance = true;
501         NR::Point const event_dt = cc->desktop->w2d(event_w);
502         switch (cc->state) {
503             case SP_CONNECTOR_CONTEXT_STOP:
504                 /* This is allowed, if we just cancelled curve */
505             case SP_CONNECTOR_CONTEXT_IDLE:
506             {
507                 if ( cc->npoints == 0 ) {
508                     /* Set start anchor */
509                     NR::Point p;
511                     cc_clear_active_conn(cc);
513                     SP_EVENT_CONTEXT_DESKTOP(cc)->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating new connector"));
515                     /* Create green anchor */
516                     p = event_dt;
518                     // Test whether we clicked on a connection point
519                     cc->sid = conn_pt_handle_test(cc, p);
521                     if (!cc->sid) {
522                         // This is the first point, so just snap it to the grid
523                         // as there's no other points to go off.
524                         SnapManager const &m = cc->desktop->namedview->snap_manager;
525                         p = m.freeSnap(Inkscape::Snapper::SNAP_POINT | Inkscape::Snapper::BBOX_POINT,
526                                        p, NULL).getPoint();
527                     }
528                     spcc_connector_set_initial_point(cc, p);
530                 }
531                 cc->state = SP_CONNECTOR_CONTEXT_DRAGGING;
532                 ret = TRUE;
533                 break;
534             }
535             case SP_CONNECTOR_CONTEXT_DRAGGING:
536             {
537                 // This is the second click of a connector creation.
539                 spcc_connector_set_subsequent_point(cc, p);
540                 spcc_connector_finish_segment(cc, p);
541                 // Test whether we clicked on a connection point
542                 cc->eid = conn_pt_handle_test(cc, p);
543                 if (cc->npoints != 0) {
544                     spcc_connector_finish(cc);
545                 }
546                 cc_set_active_conn(cc, cc->newconn);
547                 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
548                 ret = TRUE;
549                 break;
550             }
551             case SP_CONNECTOR_CONTEXT_CLOSE:
552             {
553                 g_warning("Button down in CLOSE state");
554                 break;
555             }
556             default:
557                 break;
558         }
559     } else if (bevent.button == 3) {
560         if (cc->npoints != 0) {
561             spcc_connector_finish(cc);
562             ret = TRUE;
563         }
564     }
565     return ret;
569 static gint
570 connector_handle_motion_notify(SPConnectorContext *const cc, GdkEventMotion const &mevent)
572     gint ret = FALSE;
574     if (mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) {
575         // allow middle-button scrolling
576         return FALSE;
577     }
579     NR::Point const event_w(mevent.x, mevent.y);
581     if (connector_within_tolerance) {
582         gint const tolerance = prefs_get_int_attribute_limited("options.dragtolerance",
583                                                                "value", 0, 0, 100);
584         if ( NR::LInfty( event_w - connector_drag_origin_w ) < tolerance ) {
585             return FALSE;   // Do not drag if we're within tolerance from origin.
586         }
587     }
588     // Once the user has moved farther than tolerance from the original location
589     // (indicating they intend to move the object, not click), then always process
590     // the motion notify coordinates as given (no snapping back to origin)
591     connector_within_tolerance = false;
593     SPDesktop *const dt = cc->desktop;
595     /* Find desktop coordinates */
596     NR::Point p = dt->w2d(event_w);
598     switch (cc->state) {
599         case SP_CONNECTOR_CONTEXT_DRAGGING:
600         {
601             // This is movement during a connector creation.
603             if ( cc->npoints > 0 ) {
604                 cc->selection->clear();
605                 spcc_connector_set_subsequent_point(cc, p);
606                 ret = TRUE;
607             }
608             break;
609         }
610         case SP_CONNECTOR_CONTEXT_REROUTING:
611         {
612             g_assert( SP_IS_PATH(cc->clickeditem));
614             // Update the hidden path
615             NR::Matrix i2d = sp_item_i2d_affine(cc->clickeditem);
616             NR::Matrix d2i = i2d.inverse();
617             SPPath *path = SP_PATH(cc->clickeditem);
618             SPCurve *curve = (SP_SHAPE(path))->curve;
619             if (cc->clickedhandle == cc->endpt_handle[0]) {
620                 NR::Point o = cc->endpt_handle[1]->pos;
621                 sp_curve_stretch_endpoints(curve, p * d2i, o * d2i);
622             }
623             else {
624                 NR::Point o = cc->endpt_handle[0]->pos;
625                 sp_curve_stretch_endpoints(curve, o * d2i, p * d2i);
626             }
627             sp_conn_adjust_path(path);
629             // Copy this to the temporary visible path
630             cc->red_curve = sp_curve_copy(SP_SHAPE(path)->curve);
631             sp_curve_transform(cc->red_curve, i2d);
633             sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), cc->red_curve);
634             ret = TRUE;
635             break;
636         }
637         case SP_CONNECTOR_CONTEXT_STOP:
638             /* This is perfectly valid */
639             break;
640         default:
641             break;
642     }
644     return ret;
648 static gint
649 connector_handle_button_release(SPConnectorContext *const cc, GdkEventButton const &revent)
651     gint ret = FALSE;
652     if ( revent.button == 1 ) {
654         SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
655         SPDocument *doc = sp_desktop_document(desktop);
657         NR::Point const event_w(revent.x, revent.y);
659         /* Find desktop coordinates */
660         NR::Point p = cc->desktop->w2d(event_w);
662         switch (cc->state) {
663             //case SP_CONNECTOR_CONTEXT_POINT:
664             case SP_CONNECTOR_CONTEXT_DRAGGING:
665             {
666                 if (connector_within_tolerance)
667                 {
668                     spcc_connector_finish_segment(cc, p);
669                     return TRUE;
670                 }
671                 // Connector has been created via a drag, end it now.
672                 spcc_connector_set_subsequent_point(cc, p);
673                 spcc_connector_finish_segment(cc, p);
674                 // Test whether we clicked on a connection point
675                 cc->eid = conn_pt_handle_test(cc, p);
676                 if (cc->npoints != 0) {
677                     spcc_connector_finish(cc);
678                 }
679                 cc_set_active_conn(cc, cc->newconn);
680                 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
681                 break;
682             }
683             case SP_CONNECTOR_CONTEXT_REROUTING:
684             {
685                 // Clear the temporary path:
686                 sp_curve_reset(cc->red_curve);
687                 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
689                 // Test whether we clicked on a connection point
690                 gchar *shape_label = conn_pt_handle_test(cc, p);
692                 if (shape_label) {
693                     if (cc->clickedhandle == cc->endpt_handle[0]) {
694                         sp_object_setAttribute(cc->clickeditem,
695                                 "inkscape:connection-start",shape_label, false);
696                     }
697                     else {
698                         sp_object_setAttribute(cc->clickeditem,
699                                 "inkscape:connection-end",shape_label, false);
700                     }
701                     g_free(shape_label);
702                 }
703                 cc->clickeditem->setHidden(false);
704                 sp_conn_adjust_path(SP_PATH(cc->clickeditem));
705                 cc->clickeditem->updateRepr();
706                 sp_document_done(doc);
707                 cc_set_active_conn(cc, cc->clickeditem);
708                 sp_document_ensure_up_to_date(doc);
709                 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
710                 return TRUE;
711                 break;
712             }
713             case SP_CONNECTOR_CONTEXT_STOP:
714                 /* This is allowed, if we just cancelled curve */
715                 break;
716             default:
717                 break;
718         }
719         ret = TRUE;
720     }
722     return ret;
726 static gint
727 connector_handle_key_press(SPConnectorContext *const cc, guint const keyval)
729     gint ret = FALSE;
730     /* fixme: */
731     switch (keyval) {
732         case GDK_Return:
733         case GDK_KP_Enter:
734             if (cc->npoints != 0) {
735                 spcc_connector_finish(cc);
736                 ret = TRUE;
737             }
738             break;
739         case GDK_Escape:
740             if (cc->npoints != 0) {
741                 // if drawing, cancel, otherwise pass it up for deselecting
742                 cc->state = SP_CONNECTOR_CONTEXT_STOP;
743                 spcc_reset_colors(cc);
744                 ret = TRUE;
745             }
746             break;
747         default:
748             break;
749     }
750     return ret;
754 static void
755 spcc_reset_colors(SPConnectorContext *cc)
757     /* Red */
758     sp_curve_reset(cc->red_curve);
759     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
761     sp_curve_reset(cc->green_curve);
762     cc->npoints = 0;
766 static void
767 spcc_connector_set_initial_point(SPConnectorContext *const cc, NR::Point const p)
769     g_assert( cc->npoints == 0 );
771     cc->p[0] = p;
772     cc->p[1] = p;
773     cc->npoints = 2;
774     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
778 static void
779 spcc_connector_set_subsequent_point(SPConnectorContext *const cc, NR::Point const p)
781     g_assert( cc->npoints != 0 );
783     SPDesktop *dt = cc->desktop;
784     NR::Point o = dt->dt2doc(cc->p[0]);
785     NR::Point d = dt->dt2doc(p);
786     Avoid::Point src = { o[NR::X], o[NR::Y] };
787     Avoid::Point dst = { d[NR::X], d[NR::Y] };
789     if (!cc->newConnRef) {
790         Avoid::Router *router = sp_desktop_document(dt)->router;
791         cc->newConnRef = new Avoid::ConnRef(router, 0, src, dst);
792         cc->newConnRef->updateEndPoint(Avoid::VertID::src, src);
793     }
794     cc->newConnRef->updateEndPoint(Avoid::VertID::tar, dst);
796     cc->newConnRef->makePathInvalid();
797     cc->newConnRef->generatePath(src, dst);
799     Avoid::PolyLine route = cc->newConnRef->route();
800     cc->newConnRef->calcRouteDist();
802     sp_curve_reset(cc->red_curve);
803     NR::Point pt(route.ps[0].x, route.ps[0].y);
804     sp_curve_moveto(cc->red_curve, pt);
806     for (int i = 1; i < route.pn; ++i) {
807         NR::Point p(route.ps[i].x, route.ps[i].y);
808         sp_curve_lineto(cc->red_curve, p);
809     }
810     sp_curve_transform(cc->red_curve, dt->doc2dt());
811     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), cc->red_curve);
815 /**
816  * Concats red, blue and green.
817  * If any anchors are defined, process these, optionally removing curves from white list
818  * Invoke _flush_white to write result back to object.
819  */
820 static void
821 spcc_concat_colors_and_flush(SPConnectorContext *cc)
823     SPCurve *c = cc->green_curve;
824     cc->green_curve = sp_curve_new_sized(64);
826     sp_curve_reset(cc->red_curve);
827     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
829     if (sp_curve_empty(c)) {
830         sp_curve_unref(c);
831         return;
832     }
834     spcc_flush_white(cc, c);
836     sp_curve_unref(c);
840 /*
841  * Flushes white curve(s) and additional curve into object
842  *
843  * No cleaning of colored curves - this has to be done by caller
844  * No rereading of white data, so if you cannot rely on ::modified, do it in caller
845  *
846  */
848 static void
849 spcc_flush_white(SPConnectorContext *cc, SPCurve *gc)
851     SPCurve *c;
853     if (gc) {
854         c = gc;
855         sp_curve_ref(c);
856     } else {
857         return;
858     }
860     /* Now we have to go back to item coordinates at last */
861     sp_curve_transform(c,
862             sp_desktop_dt2root_affine(SP_EVENT_CONTEXT_DESKTOP(cc)));
864     SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
865     SPDocument *doc = sp_desktop_document(desktop);
867     if ( c && !sp_curve_empty(c) ) {
868         /* We actually have something to write */
870         Inkscape::XML::Node *repr = sp_repr_new("svg:path");
871         /* Set style */
872         sp_desktop_apply_style_tool(desktop, repr, "tools.connector", false);
874         gchar *str = sp_svg_write_path(SP_CURVE_BPATH(c));
875         g_assert( str != NULL );
876         repr->setAttribute("d", str);
877         g_free(str);
879         /* Attach repr */
880         cc->newconn = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
881         cc->selection->set(repr);
882         Inkscape::GC::release(repr);
883         cc->newconn->transform = i2i_affine(desktop->currentRoot(), desktop->currentLayer());
884         cc->newconn->updateRepr();
886         bool connection = false;
887         sp_object_setAttribute(cc->newconn, "inkscape:connector-type",
888                 "polyline", false);
889         if (cc->sid)
890         {
891             sp_object_setAttribute(cc->newconn, "inkscape:connection-start",
892                     cc->sid, false);
893             connection = true;
894         }
896         if (cc->eid)
897         {
898             sp_object_setAttribute(cc->newconn, "inkscape:connection-end",
899                     cc->eid, false);
900             connection = true;
901         }
902         cc->newconn->updateRepr();
903         if (connection) {
904             // Adjust endpoints to shape edge.
905             sp_conn_adjust_path(SP_PATH(cc->newconn));
906         }
907         cc->newconn->updateRepr();
908     }
910     sp_curve_unref(c);
912     /* Flush pending updates */
913     sp_document_done(doc);
914     sp_document_ensure_up_to_date(doc);
918 static void
919 spcc_connector_finish_segment(SPConnectorContext *const cc, NR::Point const p)
921     if (!sp_curve_empty(cc->red_curve)) {
922         sp_curve_append_continuous(cc->green_curve, cc->red_curve, 0.0625);
924         cc->p[0] = cc->p[3];
925         cc->p[1] = cc->p[4];
926         cc->npoints = 2;
928         sp_curve_reset(cc->red_curve);
929     }
933 static void
934 spcc_connector_finish(SPConnectorContext *const cc)
936     SPDesktop *const desktop = cc->desktop;
937     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Finishing connector"));
939     sp_curve_reset(cc->red_curve);
940     spcc_concat_colors_and_flush(cc);
942     cc->npoints = 0;
944     if (cc->newConnRef) {
945         cc->newConnRef->removeFromGraph();
946         delete cc->newConnRef;
947         cc->newConnRef = NULL;
948     }
949     cc->state = SP_CONNECTOR_CONTEXT_IDLE;
953 static gboolean
954 cc_generic_knot_handler(SPCanvasItem *, GdkEvent *event, SPKnot *knot)
956     g_assert (knot != NULL);
957     
958     g_object_ref(knot);
960     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(
961             knot->desktop->event_context);
963     gboolean consumed = FALSE;
965     switch (event->type) {
966         case GDK_ENTER_NOTIFY:
967             sp_knot_set_flag(knot, SP_KNOT_MOUSEOVER, TRUE);
968             
969             cc->active_handle = knot;
971             if (knot->tip)
972             {
973                 knot->desktop->event_context->defaultMessageContext()->set(
974                         Inkscape::NORMAL_MESSAGE, knot->tip);
975             }
976             
977             consumed = TRUE;
978             break;
979         case GDK_LEAVE_NOTIFY:
980             sp_knot_set_flag(knot, SP_KNOT_MOUSEOVER, FALSE);
982             cc->active_handle = NULL;
983             
984             if (knot->tip) {
985                 knot->desktop->event_context->defaultMessageContext()->clear();
986             }
987             
988             consumed = TRUE;
989             break;
990         default:
991             break;
992     }
993     
994     g_object_unref(knot);
996     return consumed;
1000 static gboolean
1001 endpt_handler(SPKnot *knot, GdkEvent *event, SPConnectorContext *cc)
1003     g_assert( SP_IS_CONNECTOR_CONTEXT(cc) );
1005     gboolean consumed = FALSE;
1007     switch (event->type) {
1008         case GDK_BUTTON_PRESS:
1009             g_assert( (cc->active_handle == cc->endpt_handle[0]) ||
1010                       (cc->active_handle == cc->endpt_handle[1]) );
1011             if (cc->state == SP_CONNECTOR_CONTEXT_IDLE) {
1012                 cc->clickeditem = cc->active_conn;
1013                 cc->clickedhandle = cc->active_handle;
1014                 cc_clear_active_conn(cc);
1015                 cc->state = SP_CONNECTOR_CONTEXT_REROUTING;
1017                 // Disconnect from attached shape
1018                 unsigned ind = (cc->active_handle == cc->endpt_handle[0]) ? 0 : 1;
1019                 sp_conn_end_detach(cc->clickeditem, ind);
1021                 NR::Point origin;
1022                 if (cc->clickedhandle == cc->endpt_handle[0]) {
1023                     origin = cc->endpt_handle[1]->pos;
1024                 }
1025                 else {
1026                     origin = cc->endpt_handle[0]->pos;
1027                 }
1029                 // Show the red path for dragging.
1030                 cc->red_curve = sp_curve_copy(SP_PATH(cc->clickeditem)->curve);
1031                 NR::Matrix i2d = sp_item_i2d_affine(cc->clickeditem);
1032                 sp_curve_transform(cc->red_curve, i2d);
1033                 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath),
1034                         cc->red_curve);
1036                 cc->clickeditem->setHidden(true);
1038                 // The rest of the interaction rerouting the connector is
1039                 // handled by the context root handler.
1040                 consumed = TRUE;
1041             }
1042             break;
1043         default:
1044             break;
1045     }
1047     return consumed;
1051 static void cc_set_active_shape(SPConnectorContext *cc, SPItem *item)
1053     g_assert(item != NULL );
1055     cc->active_shape = item;
1057     // Remove existing active shape listeners
1058     if (cc->active_shape_repr) {
1059         sp_repr_remove_listener_by_data(cc->active_shape_repr, cc);
1060         Inkscape::GC::release(cc->active_shape_repr);
1062         sp_repr_remove_listener_by_data(cc->active_shape_layer_repr, cc);
1063         Inkscape::GC::release(cc->active_shape_layer_repr);
1064     }
1066     // Listen in case the active shape changes
1067     cc->active_shape_repr = SP_OBJECT_REPR(item);
1068     if (cc->active_shape_repr) {
1069         Inkscape::GC::anchor(cc->active_shape_repr);
1070         sp_repr_add_listener(cc->active_shape_repr, &shape_repr_events, cc);
1072         cc->active_shape_layer_repr = cc->active_shape_repr->parent();
1073         Inkscape::GC::anchor(cc->active_shape_layer_repr);
1074         sp_repr_add_listener(cc->active_shape_layer_repr, &layer_repr_events, cc);
1075     }
1078     // Set center connection point.
1079     if ( cc->connpthandle == NULL ) {
1080         SPKnot *knot = sp_knot_new(cc->desktop, 
1081                 _("<b>Connection point</b>: click or drag to create a new connector"));
1083         knot->setShape(SP_KNOT_SHAPE_SQUARE);
1084         knot->setSize(8);
1085         knot->setAnchor(GTK_ANCHOR_CENTER);
1086         knot->setFill(0xffffff00, 0xff0000ff, 0xff0000ff);
1087         sp_knot_update_ctrl(knot);
1089         // We don't want to use the standard knot handler,
1090         //since we don't want this knot to be draggable.
1091         g_signal_handler_disconnect(G_OBJECT(knot->item),
1092                 knot->_event_handler_id);
1093         knot->_event_handler_id = 0;
1095         gtk_signal_connect(GTK_OBJECT(knot->item), "event",
1096                 GTK_SIGNAL_FUNC(cc_generic_knot_handler), knot);
1098         cc->connpthandle = knot;
1099     }
1102     NR::Rect bbox = sp_item_bbox_desktop(cc->active_shape);
1103     NR::Point center = bbox.midpoint();
1104     sp_knot_set_position(cc->connpthandle, &center, 0);
1106     sp_knot_show(cc->connpthandle);
1111 static void
1112 cc_set_active_conn(SPConnectorContext *cc, SPItem *item)
1114     g_assert( SP_IS_PATH(item) );
1116     SPCurve *curve = SP_SHAPE(SP_PATH(item))->curve;
1117     NR::Matrix i2d = sp_item_i2d_affine(item);
1119     if (cc->active_conn == item)
1120     {
1121         // Just adjust handle positions.
1122         NR::Point startpt = sp_curve_first_point(curve) * i2d;
1123         sp_knot_set_position(cc->endpt_handle[0], &startpt, 0);
1125         NR::Point endpt = sp_curve_last_point(curve) * i2d;
1126         sp_knot_set_position(cc->endpt_handle[1], &endpt, 0);
1128         return;
1129     }
1131     cc->active_conn = item;
1133     // Remove existing active conn listeners
1134     if (cc->active_conn_repr) {
1135         sp_repr_remove_listener_by_data(cc->active_conn_repr, cc);
1136         Inkscape::GC::release(cc->active_conn_repr);
1137         cc->active_conn_repr = NULL;
1138     }
1140     // Listen in case the active conn changes
1141     cc->active_conn_repr = SP_OBJECT_REPR(item);
1142     if (cc->active_conn_repr) {
1143         Inkscape::GC::anchor(cc->active_conn_repr);
1144         sp_repr_add_listener(cc->active_conn_repr, &shape_repr_events, cc);
1145     }
1147     for (int i = 0; i < 2; ++i) {
1149         // Create the handle if it doesn't exist
1150         if ( cc->endpt_handle[i] == NULL ) {
1151             SPKnot *knot = sp_knot_new(cc->desktop, 
1152                     _("<b>Connector endpoint</b>: drag to reroute or connect to new shapes"));
1154             knot->setShape(SP_KNOT_SHAPE_SQUARE);
1155             knot->setSize(7);
1156             knot->setAnchor(GTK_ANCHOR_CENTER);
1157             knot->setFill(0xffffff00, 0xff0000ff, 0xff0000ff);
1158             knot->setStroke(0x000000ff, 0x000000ff, 0x000000ff);
1159             sp_knot_update_ctrl(knot);
1161             // We don't want to use the standard knot handler,
1162             //since we don't want this knot to be draggable.
1163             g_signal_handler_disconnect(G_OBJECT(knot->item),
1164                     knot->_event_handler_id);
1165             knot->_event_handler_id = 0;
1167             gtk_signal_connect(GTK_OBJECT(knot->item), "event",
1168                     GTK_SIGNAL_FUNC(cc_generic_knot_handler), knot);
1170             cc->endpt_handle[i] = knot;
1171         }
1173         // Remove any existing handlers
1174         if (cc->endpt_handler_id[i]) {
1175             g_signal_handlers_disconnect_by_func(
1176                     G_OBJECT(cc->endpt_handle[i]->item),
1177                     (void*)G_CALLBACK(endpt_handler), (gpointer) cc );
1178             cc->endpt_handler_id[i] = 0;
1179         }
1181         // Setup handlers for connector endpoints, this is
1182         // is as 'after' so that cc_generic_knot_handler is
1183         // triggered first for any endpoint.
1184         cc->endpt_handler_id[i] = g_signal_connect_after(
1185                 G_OBJECT(cc->endpt_handle[i]->item), "event",
1186                 G_CALLBACK(endpt_handler), cc);
1187     }
1189     NR::Point startpt = sp_curve_first_point(curve) * i2d;
1190     sp_knot_set_position(cc->endpt_handle[0], &startpt, 0);
1192     NR::Point endpt = sp_curve_last_point(curve) * i2d;
1193     sp_knot_set_position(cc->endpt_handle[1], &endpt, 0);
1195     sp_knot_show(cc->endpt_handle[0]);
1196     sp_knot_show(cc->endpt_handle[1]);
1200 static bool cc_item_is_shape(SPItem *item)
1202     if (SP_IS_PATH(item)) {
1203         SPCurve *curve = (SP_SHAPE(item))->curve;
1204         if ( curve && !(curve->closed) ) {
1205             // Open paths are connectors.
1206             return false;
1207         }
1208     }
1209     return true;
1213 bool cc_item_is_connector(SPItem *item)
1215     if (SP_IS_PATH(item)) {
1216         if (SP_PATH(item)->connEndPair.isAutoRoutingConn()) {
1217             g_assert( !(SP_SHAPE(item)->curve->closed) );
1218             return true;
1219         }
1220     }
1221     return false;
1225 void cc_selection_set_avoid(bool const set_avoid)
1227     SPDesktop *desktop = inkscape_active_desktop();
1228     if (desktop == NULL) {
1229         return;
1230     }
1232     SPDocument *document = sp_desktop_document(desktop);
1234     Inkscape::Selection *selection = sp_desktop_selection(desktop);
1236     GSList *l = (GSList *) selection->itemList();
1238     int changes = 0;
1240     while (l) {
1241         SPItem *item = (SPItem *) l->data;
1243         char const *value = (set_avoid) ? "true" : NULL;
1245         if (cc_item_is_shape(item)) {
1246             sp_object_setAttribute(item, "inkscape:connector-avoid",
1247                     value, false);
1248             item->avoidRef->handleSettingChange();
1249             changes++;
1250         }
1252         l = l->next;
1253     }
1255     if (changes == 0) {
1256         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE,
1257                 _("Select <b>at least one non-connector object</b>."));
1258         return;
1259     }
1261     sp_document_done(document);
1265 static void
1266 cc_selection_changed(Inkscape::Selection *selection, gpointer data)
1268     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(data);
1269     //SPEventContext *ec = SP_EVENT_CONTEXT(cc);
1271     SPItem *item = selection->singleItem();
1273     if (cc->active_conn == item)
1274     {
1275         // Nothing to change.
1276         return;
1277     }
1278     if (item == NULL)
1279     {
1280         cc_clear_active_conn(cc);
1281         return;
1282     }
1284     if (cc_item_is_connector(item)) {
1285         cc_set_active_conn(cc, item);
1286     }
1290 static void
1291 shape_event_attr_deleted(Inkscape::XML::Node *repr, Inkscape::XML::Node *child,
1292         Inkscape::XML::Node *ref, gpointer data)
1294     g_assert(data);
1295     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(data);
1297     if (child == cc->active_shape_repr) {
1298         // The active shape has been deleted.  Clear active shape.
1299         cc_clear_active_shape(cc);
1300     }
1304 static void
1305 shape_event_attr_changed(Inkscape::XML::Node *repr, gchar const *name,
1306                             gchar const *old_value, gchar const *new_value,
1307                             bool is_interactive, gpointer data)
1309     g_assert(data);
1310     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(data);
1312     // Look for changes than result in onscreen movement.
1313     if (!strcmp(name, "d") || !strcmp(name, "x") || !strcmp(name, "y") ||
1314             !strcmp(name, "width") || !strcmp(name, "height") ||
1315             !strcmp(name, "transform"))
1316     {
1317         if (repr == cc->active_shape_repr) {
1318             // Active shape has moved. Clear active shape.
1319             cc_clear_active_shape(cc);
1320         }
1321         else if (repr == cc->active_conn_repr) {
1322             // The active conn has been moved.
1323             // Set it again, which just sets new handle positions.
1324             cc_set_active_conn(cc, cc->active_conn);
1325         }
1326     }
1330 /*
1331   Local Variables:
1332   mode:c++
1333   c-file-style:"stroustrup"
1334   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1335   indent-tabs-mode:nil
1336   fill-column:99
1337   End:
1338 */
1339 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :