Code

Fix a regression in the snapper, caused by me. Sorry!
[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 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 "prefs-utils.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, NR::Point const p);
89 static void spcc_connector_set_subsequent_point(SPConnectorContext *cc, NR::Point const p);
90 static void spcc_connector_finish_segment(SPConnectorContext *cc, NR::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, NR::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         NR::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 NR::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)
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;
161 static void
162 sp_connector_context_class_init(SPConnectorContextClass *klass)
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;
181 static void
182 sp_connector_context_init(SPConnectorContext *cc)
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;
223 static void
224 sp_connector_context_dispose(GObject *object)
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);
254 static void
255 sp_connector_context_setup(SPEventContext *ec)
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(4);
280     /* Create green curve */
281     cc->green_curve = new SPCurve(64);
283     // Notice the initial selection.
284     cc_selection_changed(cc->selection, (gpointer) cc);
286     if (prefs_get_int_attribute("tools.connector", "selcue", 0) != 0) {
287         ec->enableSelectionCue();
288     }
290     // Make sure we see all enter events for canvas items,
291     // even if a mouse button is depressed.
292     dt->canvas->gen_all_enter_events = true;
296 static void
297 sp_connector_context_finish(SPEventContext *ec)
299     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(ec);
301     spcc_connector_finish(cc);
303     if (((SPEventContextClass *) parent_class)->finish) {
304         ((SPEventContextClass *) parent_class)->finish(ec);
305     }
307     if (cc->selection) {
308         cc->selection = NULL;
309     }
310     cc_clear_active_shape(cc);
311     cc_clear_active_conn(cc);
313     // Restore the default event generating behaviour.
314     SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(ec);
315     desktop->canvas->gen_all_enter_events = false;
319 //-----------------------------------------------------------------------------
322 static void
323 cc_clear_active_shape(SPConnectorContext *cc)
325     if (cc->active_shape == NULL) {
326         return;
327     }
328     g_assert( cc->active_shape_repr );
329     g_assert( cc->active_shape_layer_repr );
331     cc->active_shape = NULL;
333     if (cc->active_shape_repr) {
334         sp_repr_remove_listener_by_data(cc->active_shape_repr, cc);
335         Inkscape::GC::release(cc->active_shape_repr);
336         cc->active_shape_repr = NULL;
338         sp_repr_remove_listener_by_data(cc->active_shape_layer_repr, cc);
339         Inkscape::GC::release(cc->active_shape_layer_repr);
340         cc->active_shape_layer_repr = NULL;
341     }
343     // Hide the center connection point if it exists.
344     if (cc->connpthandle) {
345         sp_knot_hide(cc->connpthandle);
346     }
350 static void
351 cc_clear_active_conn(SPConnectorContext *cc)
353     if (cc->active_conn == NULL) {
354         return;
355     }
356     g_assert( cc->active_conn_repr );
358     cc->active_conn = NULL;
360     if (cc->active_conn_repr) {
361         sp_repr_remove_listener_by_data(cc->active_conn_repr, cc);
362         Inkscape::GC::release(cc->active_conn_repr);
363         cc->active_conn_repr = NULL;
364     }
366     // Hide the endpoint handles.
367     for (int i = 0; i < 2; ++i) {
368         if (cc->endpt_handle[i]) {
369             sp_knot_hide(cc->endpt_handle[i]);
370         }
371     }
375 static gchar *
376 conn_pt_handle_test(SPConnectorContext *cc, NR::Point& p)
378     // TODO: this will need to change when there are more connection
379     //       points available for each shape.
381     SPKnot *centerpt = cc->connpthandle;
382     if (cc->active_handle && (cc->active_handle == centerpt))
383     {
384         p = centerpt->pos;
385         return g_strdup_printf("#%s", SP_OBJECT_ID(cc->active_shape));
386     }
387     return NULL;
392 static gint
393 sp_connector_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event)
395     gint ret = FALSE;
397     SPDesktop *desktop = event_context->desktop;
399     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(event_context);
401     NR::Point p(event->button.x, event->button.y);
403     switch (event->type) {
404         case GDK_BUTTON_RELEASE:
405             if (event->button.button == 1 && !event_context->space_panning) {
406                 if ((cc->state == SP_CONNECTOR_CONTEXT_DRAGGING) &&
407                         (connector_within_tolerance))
408                 {
409                     spcc_reset_colors(cc);
410                     cc->state = SP_CONNECTOR_CONTEXT_IDLE;
411                 }
412                 if (cc->state != SP_CONNECTOR_CONTEXT_IDLE) {
413                     // Doing simething else like rerouting.
414                     break;
415                 }
416                 // find out clicked item, disregarding groups, honoring Alt
417                 SPItem *item_ungrouped = sp_event_context_find_item(desktop,
418                         p, event->button.state & GDK_MOD1_MASK, TRUE);
420                 if (event->button.state & GDK_SHIFT_MASK) {
421                     cc->selection->toggle(item_ungrouped);
422                 } else {
423                     cc->selection->set(item_ungrouped);
424                 }
425                 ret = TRUE;
426             }
427             break;
428         case GDK_ENTER_NOTIFY:
429         {
430             if (cc_item_is_shape(item)) {
431                 // This is a shape, so show connection point(s).
432                 if (!(cc->active_shape) ||
433                         // Don't show handle for another handle.
434                         (item != ((SPItem *) cc->connpthandle))) {
435                     cc_set_active_shape(cc, item);
436                 }
437             }
438             ret = TRUE;
439             break;
440         }
441         default:
442             break;
443     }
445     return ret;
449 gint
450 sp_connector_context_root_handler(SPEventContext *ec, GdkEvent *event)
452     SPConnectorContext *const cc = SP_CONNECTOR_CONTEXT(ec);
454     gint ret = FALSE;
456     switch (event->type) {
457         case GDK_BUTTON_PRESS:
458             ret = connector_handle_button_press(cc, event->button);
459             break;
461         case GDK_MOTION_NOTIFY:
462             ret = connector_handle_motion_notify(cc, event->motion);
463             break;
465         case GDK_BUTTON_RELEASE:
466             ret = connector_handle_button_release(cc, event->button);
467             break;
468         case GDK_KEY_PRESS:
469             ret = connector_handle_key_press(cc, get_group0_keyval (&event->key));
470             break;
472         default:
473             break;
474     }
476     if (!ret) {
477         gint (*const parent_root_handler)(SPEventContext *, GdkEvent *)
478             = ((SPEventContextClass *) parent_class)->root_handler;
479         if (parent_root_handler) {
480             ret = parent_root_handler(ec, event);
481         }
482     }
484     return ret;
488 static gint
489 connector_handle_button_press(SPConnectorContext *const cc, GdkEventButton const &bevent)
491     NR::Point const event_w(bevent.x, bevent.y);
492     /* Find desktop coordinates */
493     NR::Point p = cc->desktop->w2d(event_w);
494     SPEventContext *event_context = SP_EVENT_CONTEXT(cc);
496     gint ret = FALSE;
497     if ( bevent.button == 1 && !event_context->space_panning ) {
499         SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
501         if (Inkscape::have_viable_layer(desktop, cc->_message_context) == false) {
502             return TRUE;
503         }
505         NR::Point const event_w(bevent.x,
506                                 bevent.y);
507         connector_drag_origin_w = event_w;
508         connector_within_tolerance = true;
510         NR::Point const event_dt = cc->desktop->w2d(event_w);
511         switch (cc->state) {
512             case SP_CONNECTOR_CONTEXT_STOP:
513                 /* This is allowed, if we just cancelled curve */
514             case SP_CONNECTOR_CONTEXT_IDLE:
515             {
516                 if ( cc->npoints == 0 ) {
517                     /* Set start anchor */
518                     NR::Point p;
520                     cc_clear_active_conn(cc);
522                     SP_EVENT_CONTEXT_DESKTOP(cc)->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating new connector"));
524                     /* Create green anchor */
525                     p = event_dt;
527                     // Test whether we clicked on a connection point
528                     cc->sid = conn_pt_handle_test(cc, p);
530                     if (!cc->sid) {
531                         // This is the first point, so just snap it to the grid
532                         // as there's no other points to go off.
533                         SnapManager &m = cc->desktop->namedview->snap_manager;
534                         m.setup(cc->desktop);
535                         m.freeSnapReturnByRef(Inkscape::Snapper::SNAPPOINT_NODE, p);
536                     }
537                     spcc_connector_set_initial_point(cc, p);
539                 }
540                 cc->state = SP_CONNECTOR_CONTEXT_DRAGGING;
541                 ret = TRUE;
542                 break;
543             }
544             case SP_CONNECTOR_CONTEXT_DRAGGING:
545             {
546                 // This is the second click of a connector creation.
548                 spcc_connector_set_subsequent_point(cc, p);
549                 spcc_connector_finish_segment(cc, p);
550                 // Test whether we clicked on a connection point
551                 cc->eid = conn_pt_handle_test(cc, p);
552                 if (cc->npoints != 0) {
553                     spcc_connector_finish(cc);
554                 }
555                 cc_set_active_conn(cc, cc->newconn);
556                 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
557                 ret = TRUE;
558                 break;
559             }
560             case SP_CONNECTOR_CONTEXT_CLOSE:
561             {
562                 g_warning("Button down in CLOSE state");
563                 break;
564             }
565             default:
566                 break;
567         }
568     } else if (bevent.button == 3) {
569         if (cc->state == SP_CONNECTOR_CONTEXT_REROUTING) {
570             // A context menu is going to be triggered here, 
571             // so end the rerouting operation.
572             cc_connector_rerouting_finish(cc, &p);
573                 
574             cc->state = SP_CONNECTOR_CONTEXT_IDLE;
575             
576             // Don't set ret to TRUE, so we drop through to the
577             // parent handler which will open the context menu.
578         }
579         else if (cc->npoints != 0) {
580             spcc_connector_finish(cc);
581             ret = TRUE;
582         }
583     }
584     return ret;
588 static gint
589 connector_handle_motion_notify(SPConnectorContext *const cc, GdkEventMotion const &mevent)
591     gint ret = FALSE;
592     SPEventContext *event_context = SP_EVENT_CONTEXT(cc);
594     if (event_context->space_panning || mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) {
595         // allow middle-button scrolling
596         return FALSE;
597     }
599     NR::Point const event_w(mevent.x, mevent.y);
601     if (connector_within_tolerance) {
602         gint const tolerance = prefs_get_int_attribute_limited("options.dragtolerance",
603                                                                "value", 0, 0, 100);
604         if ( NR::LInfty( event_w - connector_drag_origin_w ) < tolerance ) {
605             return FALSE;   // Do not drag if we're within tolerance from origin.
606         }
607     }
608     // Once the user has moved farther than tolerance from the original location
609     // (indicating they intend to move the object, not click), then always process
610     // the motion notify coordinates as given (no snapping back to origin)
611     connector_within_tolerance = false;
613     SPDesktop *const dt = cc->desktop;
615     /* Find desktop coordinates */
616     NR::Point p = dt->w2d(event_w);
618     switch (cc->state) {
619         case SP_CONNECTOR_CONTEXT_DRAGGING:
620         {
621             // This is movement during a connector creation.
623             if ( cc->npoints > 0 ) {
624                 cc->selection->clear();
625                 spcc_connector_set_subsequent_point(cc, p);
626                 ret = TRUE;
627             }
628             break;
629         }
630         case SP_CONNECTOR_CONTEXT_REROUTING:
631         {
632             g_assert( SP_IS_PATH(cc->clickeditem));
634             // Update the hidden path
635             NR::Matrix i2d = sp_item_i2d_affine(cc->clickeditem);
636             NR::Matrix d2i = i2d.inverse();
637             SPPath *path = SP_PATH(cc->clickeditem);
638             SPCurve *curve = (SP_SHAPE(path))->curve;
639             if (cc->clickedhandle == cc->endpt_handle[0]) {
640                 NR::Point o = cc->endpt_handle[1]->pos;
641                 curve->stretch_endpoints(p * d2i, o * d2i);
642             }
643             else {
644                 NR::Point o = cc->endpt_handle[0]->pos;
645                 curve->stretch_endpoints(o * d2i, p * d2i);
646             }
647             sp_conn_adjust_path(path);
649             // Copy this to the temporary visible path
650             cc->red_curve = SP_SHAPE(path)->curve->copy();
651             cc->red_curve->transform(i2d);
653             sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), cc->red_curve);
654             ret = TRUE;
655             break;
656         }
657         case SP_CONNECTOR_CONTEXT_STOP:
658             /* This is perfectly valid */
659             break;
660         default:
661             break;
662     }
664     return ret;
668 static gint
669 connector_handle_button_release(SPConnectorContext *const cc, GdkEventButton const &revent)
671     gint ret = FALSE;
672     SPEventContext *event_context = SP_EVENT_CONTEXT(cc);
673     if ( revent.button == 1 && !event_context->space_panning ) {
675         SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
676         SPDocument *doc = sp_desktop_document(desktop);
678         NR::Point const event_w(revent.x, revent.y);
680         /* Find desktop coordinates */
681         NR::Point p = cc->desktop->w2d(event_w);
683         switch (cc->state) {
684             //case SP_CONNECTOR_CONTEXT_POINT:
685             case SP_CONNECTOR_CONTEXT_DRAGGING:
686             {
687                 if (connector_within_tolerance)
688                 {
689                     spcc_connector_finish_segment(cc, p);
690                     return TRUE;
691                 }
692                 // Connector has been created via a drag, end it now.
693                 spcc_connector_set_subsequent_point(cc, p);
694                 spcc_connector_finish_segment(cc, p);
695                 // Test whether we clicked on a connection point
696                 cc->eid = conn_pt_handle_test(cc, p);
697                 if (cc->npoints != 0) {
698                     spcc_connector_finish(cc);
699                 }
700                 cc_set_active_conn(cc, cc->newconn);
701                 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
702                 break;
703             }
704             case SP_CONNECTOR_CONTEXT_REROUTING:
705             {
706                 cc_connector_rerouting_finish(cc, &p);
707                 
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->state == SP_CONNECTOR_CONTEXT_REROUTING) {
741                 
742                 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
743                 SPDocument *doc = sp_desktop_document(desktop);
745                 cc_connector_rerouting_finish(cc, NULL);
746                 
747                 sp_document_undo(doc);
748                 
749                 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
750                 desktop->messageStack()->flash( Inkscape::NORMAL_MESSAGE,
751                         _("Connector endpoint drag cancelled."));
752                 ret = TRUE;
753             }
754             else if (cc->npoints != 0) {
755                 // if drawing, cancel, otherwise pass it up for deselecting
756                 cc->state = SP_CONNECTOR_CONTEXT_STOP;
757                 spcc_reset_colors(cc);
758                 ret = TRUE;
759             }
760             break;
761         default:
762             break;
763     }
764     return ret;
768 static void
769 cc_connector_rerouting_finish(SPConnectorContext *const cc, NR::Point *const p)
771     SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
772     SPDocument *doc = sp_desktop_document(desktop);
773     
774     // Clear the temporary path:
775     cc->red_curve->reset();
776     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
778     if (p != NULL)
779     {
780         // Test whether we clicked on a connection point
781         gchar *shape_label = conn_pt_handle_test(cc, *p);
783         if (shape_label) {
784             if (cc->clickedhandle == cc->endpt_handle[0]) {
785                 sp_object_setAttribute(cc->clickeditem,
786                         "inkscape:connection-start",shape_label, false);
787             }
788             else {
789                 sp_object_setAttribute(cc->clickeditem,
790                         "inkscape:connection-end",shape_label, false);
791             }
792             g_free(shape_label);
793         }
794     }
795     cc->clickeditem->setHidden(false);
796     sp_conn_adjust_path(SP_PATH(cc->clickeditem));
797     cc->clickeditem->updateRepr();
798     sp_document_done(doc, SP_VERB_CONTEXT_CONNECTOR, 
799                      _("Reroute connector"));
800     cc_set_active_conn(cc, cc->clickeditem);
804 static void
805 spcc_reset_colors(SPConnectorContext *cc)
807     /* Red */
808     cc->red_curve->reset();
809     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
811     cc->green_curve->reset();
812     cc->npoints = 0;
816 static void
817 spcc_connector_set_initial_point(SPConnectorContext *const cc, NR::Point const p)
819     g_assert( cc->npoints == 0 );
821     cc->p[0] = p;
822     cc->p[1] = p;
823     cc->npoints = 2;
824     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
828 static void
829 spcc_connector_set_subsequent_point(SPConnectorContext *const cc, NR::Point const p)
831     g_assert( cc->npoints != 0 );
833     SPDesktop *dt = cc->desktop;
834     NR::Point o = dt->dt2doc(cc->p[0]);
835     NR::Point d = dt->dt2doc(p);
836     Avoid::Point src(o[NR::X], o[NR::Y]);
837     Avoid::Point dst(d[NR::X], d[NR::Y]);
839     if (!cc->newConnRef) {
840         Avoid::Router *router = sp_desktop_document(dt)->router;
841         cc->newConnRef = new Avoid::ConnRef(router, 0, src, dst);
842         cc->newConnRef->updateEndPoint(Avoid::VertID::src, src);
843     }
844     cc->newConnRef->updateEndPoint(Avoid::VertID::tar, dst);
846     cc->newConnRef->makePathInvalid();
847     cc->newConnRef->generatePath(src, dst);
849     Avoid::PolyLine route = cc->newConnRef->route();
850     cc->newConnRef->calcRouteDist();
852     cc->red_curve->reset();
853     NR::Point pt(route.ps[0].x, route.ps[0].y);
854     cc->red_curve->moveto(pt);
856     for (int i = 1; i < route.pn; ++i) {
857         NR::Point p(route.ps[i].x, route.ps[i].y);
858         cc->red_curve->lineto(p);
859     }
860     cc->red_curve->transform(dt->doc2dt());
861     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), cc->red_curve);
865 /**
866  * Concats red, blue and green.
867  * If any anchors are defined, process these, optionally removing curves from white list
868  * Invoke _flush_white to write result back to object.
869  */
870 static void
871 spcc_concat_colors_and_flush(SPConnectorContext *cc)
873     SPCurve *c = cc->green_curve;
874     cc->green_curve = new SPCurve(64);
876     cc->red_curve->reset();
877     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
879     if (c->is_empty()) {
880         c->unref();
881         return;
882     }
884     spcc_flush_white(cc, c);
886     c->unref();
890 /*
891  * Flushes white curve(s) and additional curve into object
892  *
893  * No cleaning of colored curves - this has to be done by caller
894  * No rereading of white data, so if you cannot rely on ::modified, do it in caller
895  *
896  */
898 static void
899 spcc_flush_white(SPConnectorContext *cc, SPCurve *gc)
901     SPCurve *c;
903     if (gc) {
904         c = gc;
905         c->ref();
906     } else {
907         return;
908     }
910     /* Now we have to go back to item coordinates at last */
911     c->transform(sp_desktop_dt2root_affine(SP_EVENT_CONTEXT_DESKTOP(cc)));
913     SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
914     SPDocument *doc = sp_desktop_document(desktop);
915     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
917     if ( c && !c->is_empty() ) {
918         /* We actually have something to write */
920         Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
921         /* Set style */
922         sp_desktop_apply_style_tool(desktop, repr, "tools.connector", false);
924         gchar *str = sp_svg_write_path(SP_CURVE_BPATH(c));
925         g_assert( str != NULL );
926         repr->setAttribute("d", str);
927         g_free(str);
929         /* Attach repr */
930         cc->newconn = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
931         cc->selection->set(repr);
932         Inkscape::GC::release(repr);
933         cc->newconn->transform = i2i_affine(desktop->currentRoot(), desktop->currentLayer());
934         cc->newconn->updateRepr();
936         bool connection = false;
937         sp_object_setAttribute(cc->newconn, "inkscape:connector-type",
938                 "polyline", false);
939         if (cc->sid)
940         {
941             sp_object_setAttribute(cc->newconn, "inkscape:connection-start",
942                     cc->sid, false);
943             connection = true;
944         }
946         if (cc->eid)
947         {
948             sp_object_setAttribute(cc->newconn, "inkscape:connection-end",
949                     cc->eid, false);
950             connection = true;
951         }
952         cc->newconn->updateRepr();
953         if (connection) {
954             // Adjust endpoints to shape edge.
955             sp_conn_adjust_path(SP_PATH(cc->newconn));
956         }
957         cc->newconn->updateRepr();
958     }
960     c->unref();
962     /* Flush pending updates */
963     sp_document_done(doc, SP_VERB_CONTEXT_CONNECTOR, _("Create connector"));
964     sp_document_ensure_up_to_date(doc);
968 static void
969 spcc_connector_finish_segment(SPConnectorContext *const cc, NR::Point const /*p*/)
971     if (!cc->red_curve->is_empty()) {
972         cc->green_curve->append_continuous(cc->red_curve, 0.0625);
974         cc->p[0] = cc->p[3];
975         cc->p[1] = cc->p[4];
976         cc->npoints = 2;
978         cc->red_curve->reset();
979     }
983 static void
984 spcc_connector_finish(SPConnectorContext *const cc)
986     SPDesktop *const desktop = cc->desktop;
987     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Finishing connector"));
989     cc->red_curve->reset();
990     spcc_concat_colors_and_flush(cc);
992     cc->npoints = 0;
994     if (cc->newConnRef) {
995         cc->newConnRef->removeFromGraph();
996         delete cc->newConnRef;
997         cc->newConnRef = NULL;
998     }
999     cc->state = SP_CONNECTOR_CONTEXT_IDLE;
1003 static gboolean
1004 cc_generic_knot_handler(SPCanvasItem *, GdkEvent *event, SPKnot *knot)
1006     g_assert (knot != NULL);
1007     
1008     g_object_ref(knot);
1010     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(
1011             knot->desktop->event_context);
1013     gboolean consumed = FALSE;
1015     switch (event->type) {
1016         case GDK_ENTER_NOTIFY:
1017             sp_knot_set_flag(knot, SP_KNOT_MOUSEOVER, TRUE);
1018             
1019             cc->active_handle = knot;
1021             if (knot->tip)
1022             {
1023                 knot->desktop->event_context->defaultMessageContext()->set(
1024                         Inkscape::NORMAL_MESSAGE, knot->tip);
1025             }
1026             
1027             consumed = TRUE;
1028             break;
1029         case GDK_LEAVE_NOTIFY:
1030             sp_knot_set_flag(knot, SP_KNOT_MOUSEOVER, FALSE);
1032             cc->active_handle = NULL;
1033             
1034             if (knot->tip) {
1035                 knot->desktop->event_context->defaultMessageContext()->clear();
1036             }
1037             
1038             consumed = TRUE;
1039             break;
1040         default:
1041             break;
1042     }
1043     
1044     g_object_unref(knot);
1046     return consumed;
1050 static gboolean
1051 endpt_handler(SPKnot */*knot*/, GdkEvent *event, SPConnectorContext *cc)
1053     g_assert( SP_IS_CONNECTOR_CONTEXT(cc) );
1055     gboolean consumed = FALSE;
1057     switch (event->type) {
1058         case GDK_BUTTON_PRESS:
1059             g_assert( (cc->active_handle == cc->endpt_handle[0]) ||
1060                       (cc->active_handle == cc->endpt_handle[1]) );
1061             if (cc->state == SP_CONNECTOR_CONTEXT_IDLE) {
1062                 cc->clickeditem = cc->active_conn;
1063                 cc->clickedhandle = cc->active_handle;
1064                 cc_clear_active_conn(cc);
1065                 cc->state = SP_CONNECTOR_CONTEXT_REROUTING;
1067                 // Disconnect from attached shape
1068                 unsigned ind = (cc->active_handle == cc->endpt_handle[0]) ? 0 : 1;
1069                 sp_conn_end_detach(cc->clickeditem, ind);
1071                 NR::Point origin;
1072                 if (cc->clickedhandle == cc->endpt_handle[0]) {
1073                     origin = cc->endpt_handle[1]->pos;
1074                 }
1075                 else {
1076                     origin = cc->endpt_handle[0]->pos;
1077                 }
1079                 // Show the red path for dragging.
1080                 cc->red_curve = SP_PATH(cc->clickeditem)->curve->copy();
1081                 NR::Matrix i2d = sp_item_i2d_affine(cc->clickeditem);
1082                 cc->red_curve->transform(i2d);
1083                 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), cc->red_curve);
1085                 cc->clickeditem->setHidden(true);
1087                 // The rest of the interaction rerouting the connector is
1088                 // handled by the context root handler.
1089                 consumed = TRUE;
1090             }
1091             break;
1092         default:
1093             break;
1094     }
1096     return consumed;
1100 static void cc_set_active_shape(SPConnectorContext *cc, SPItem *item)
1102     g_assert(item != NULL );
1104     cc->active_shape = item;
1106     // Remove existing active shape listeners
1107     if (cc->active_shape_repr) {
1108         sp_repr_remove_listener_by_data(cc->active_shape_repr, cc);
1109         Inkscape::GC::release(cc->active_shape_repr);
1111         sp_repr_remove_listener_by_data(cc->active_shape_layer_repr, cc);
1112         Inkscape::GC::release(cc->active_shape_layer_repr);
1113     }
1115     // Listen in case the active shape changes
1116     cc->active_shape_repr = SP_OBJECT_REPR(item);
1117     if (cc->active_shape_repr) {
1118         Inkscape::GC::anchor(cc->active_shape_repr);
1119         sp_repr_add_listener(cc->active_shape_repr, &shape_repr_events, cc);
1121         cc->active_shape_layer_repr = cc->active_shape_repr->parent();
1122         Inkscape::GC::anchor(cc->active_shape_layer_repr);
1123         sp_repr_add_listener(cc->active_shape_layer_repr, &layer_repr_events, cc);
1124     }
1127     // Set center connection point.
1128     if ( cc->connpthandle == NULL ) {
1129         SPKnot *knot = sp_knot_new(cc->desktop, 
1130                 _("<b>Connection point</b>: click or drag to create a new connector"));
1132         knot->setShape(SP_KNOT_SHAPE_SQUARE);
1133         knot->setSize(8);
1134         knot->setAnchor(GTK_ANCHOR_CENTER);
1135         knot->setFill(0xffffff00, 0xff0000ff, 0xff0000ff);
1136         sp_knot_update_ctrl(knot);
1138         // We don't want to use the standard knot handler,
1139         //since we don't want this knot to be draggable.
1140         g_signal_handler_disconnect(G_OBJECT(knot->item),
1141                 knot->_event_handler_id);
1142         knot->_event_handler_id = 0;
1144         gtk_signal_connect(GTK_OBJECT(knot->item), "event",
1145                 GTK_SIGNAL_FUNC(cc_generic_knot_handler), knot);
1147         cc->connpthandle = knot;
1148     }
1151     NR::Maybe<NR::Rect> bbox = sp_item_bbox_desktop(cc->active_shape);
1152     if (bbox) {
1153         NR::Point center = bbox->midpoint();
1154         sp_knot_set_position(cc->connpthandle, &center, 0);
1155         sp_knot_show(cc->connpthandle);
1156     } else {
1157         sp_knot_hide(cc->connpthandle);
1158     }
1162 static void
1163 cc_set_active_conn(SPConnectorContext *cc, SPItem *item)
1165     g_assert( SP_IS_PATH(item) );
1167     SPCurve *curve = SP_SHAPE(SP_PATH(item))->curve;
1168     NR::Matrix i2d = sp_item_i2d_affine(item);
1170     if (cc->active_conn == item)
1171     {
1172         // Just adjust handle positions.
1173         NR::Point startpt = curve->first_point() * i2d;
1174         sp_knot_set_position(cc->endpt_handle[0], &startpt, 0);
1176         NR::Point endpt = curve->last_point() * i2d;
1177         sp_knot_set_position(cc->endpt_handle[1], &endpt, 0);
1179         return;
1180     }
1182     cc->active_conn = item;
1184     // Remove existing active conn listeners
1185     if (cc->active_conn_repr) {
1186         sp_repr_remove_listener_by_data(cc->active_conn_repr, cc);
1187         Inkscape::GC::release(cc->active_conn_repr);
1188         cc->active_conn_repr = NULL;
1189     }
1191     // Listen in case the active conn changes
1192     cc->active_conn_repr = SP_OBJECT_REPR(item);
1193     if (cc->active_conn_repr) {
1194         Inkscape::GC::anchor(cc->active_conn_repr);
1195         sp_repr_add_listener(cc->active_conn_repr, &shape_repr_events, cc);
1196     }
1198     for (int i = 0; i < 2; ++i) {
1200         // Create the handle if it doesn't exist
1201         if ( cc->endpt_handle[i] == NULL ) {
1202             SPKnot *knot = sp_knot_new(cc->desktop, 
1203                     _("<b>Connector endpoint</b>: drag to reroute or connect to new shapes"));
1205             knot->setShape(SP_KNOT_SHAPE_SQUARE);
1206             knot->setSize(7);
1207             knot->setAnchor(GTK_ANCHOR_CENTER);
1208             knot->setFill(0xffffff00, 0xff0000ff, 0xff0000ff);
1209             knot->setStroke(0x000000ff, 0x000000ff, 0x000000ff);
1210             sp_knot_update_ctrl(knot);
1212             // We don't want to use the standard knot handler,
1213             //since we don't want this knot to be draggable.
1214             g_signal_handler_disconnect(G_OBJECT(knot->item),
1215                     knot->_event_handler_id);
1216             knot->_event_handler_id = 0;
1218             gtk_signal_connect(GTK_OBJECT(knot->item), "event",
1219                     GTK_SIGNAL_FUNC(cc_generic_knot_handler), knot);
1221             cc->endpt_handle[i] = knot;
1222         }
1224         // Remove any existing handlers
1225         if (cc->endpt_handler_id[i]) {
1226             g_signal_handlers_disconnect_by_func(
1227                     G_OBJECT(cc->endpt_handle[i]->item),
1228                     (void*)G_CALLBACK(endpt_handler), (gpointer) cc );
1229             cc->endpt_handler_id[i] = 0;
1230         }
1232         // Setup handlers for connector endpoints, this is
1233         // is as 'after' so that cc_generic_knot_handler is
1234         // triggered first for any endpoint.
1235         cc->endpt_handler_id[i] = g_signal_connect_after(
1236                 G_OBJECT(cc->endpt_handle[i]->item), "event",
1237                 G_CALLBACK(endpt_handler), cc);
1238     }
1240     NR::Point startpt = curve->first_point() * i2d;
1241     sp_knot_set_position(cc->endpt_handle[0], &startpt, 0);
1243     NR::Point endpt = curve->last_point() * i2d;
1244     sp_knot_set_position(cc->endpt_handle[1], &endpt, 0);
1246     sp_knot_show(cc->endpt_handle[0]);
1247     sp_knot_show(cc->endpt_handle[1]);
1251 static bool cc_item_is_shape(SPItem *item)
1253     if (SP_IS_PATH(item)) {
1254         SPCurve *curve = (SP_SHAPE(item))->curve;
1255         if ( curve && !(curve->is_closed()) ) {
1256             // Open paths are connectors.
1257             return false;
1258         }
1259     }
1260     else if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
1261         if (prefs_get_int_attribute("tools.connector", "ignoretext", 1) == 1) {
1262             // Don't count text as a shape we can connect connector to.
1263             return false;
1264         }
1265     }
1266     return true;
1270 bool cc_item_is_connector(SPItem *item)
1272     if (SP_IS_PATH(item)) {
1273         if (SP_PATH(item)->connEndPair.isAutoRoutingConn()) {
1274             g_assert( !(SP_SHAPE(item)->curve->is_closed()) );
1275             return true;
1276         }
1277     }
1278     return false;
1282 void cc_selection_set_avoid(bool const set_avoid)
1284     SPDesktop *desktop = inkscape_active_desktop();
1285     if (desktop == NULL) {
1286         return;
1287     }
1289     SPDocument *document = sp_desktop_document(desktop);
1291     Inkscape::Selection *selection = sp_desktop_selection(desktop);
1293     GSList *l = (GSList *) selection->itemList();
1295     int changes = 0;
1297     while (l) {
1298         SPItem *item = (SPItem *) l->data;
1300         char const *value = (set_avoid) ? "true" : NULL;
1302         if (cc_item_is_shape(item)) {
1303             sp_object_setAttribute(item, "inkscape:connector-avoid",
1304                     value, false);
1305             item->avoidRef->handleSettingChange();
1306             changes++;
1307         }
1309         l = l->next;
1310     }
1312     if (changes == 0) {
1313         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE,
1314                 _("Select <b>at least one non-connector object</b>."));
1315         return;
1316     }
1318     char *event_desc = (set_avoid) ?
1319             _("Make connectors avoid selected objects") :
1320             _("Make connectors ignore selected objects");
1321     sp_document_done(document, SP_VERB_CONTEXT_CONNECTOR, event_desc);
1325 static void
1326 cc_selection_changed(Inkscape::Selection *selection, gpointer data)
1328     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(data);
1329     //SPEventContext *ec = SP_EVENT_CONTEXT(cc);
1331     SPItem *item = selection->singleItem();
1333     if (cc->active_conn == item)
1334     {
1335         // Nothing to change.
1336         return;
1337     }
1338     if (item == NULL)
1339     {
1340         cc_clear_active_conn(cc);
1341         return;
1342     }
1344     if (cc_item_is_connector(item)) {
1345         cc_set_active_conn(cc, item);
1346     }
1350 static void
1351 shape_event_attr_deleted(Inkscape::XML::Node */*repr*/, Inkscape::XML::Node *child,
1352                          Inkscape::XML::Node */*ref*/, gpointer data)
1354     g_assert(data);
1355     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(data);
1357     if (child == cc->active_shape_repr) {
1358         // The active shape has been deleted.  Clear active shape.
1359         cc_clear_active_shape(cc);
1360     }
1364 static void
1365 shape_event_attr_changed(Inkscape::XML::Node *repr, gchar const *name,
1366                          gchar const */*old_value*/, gchar const */*new_value*/,
1367                          bool /*is_interactive*/, gpointer data)
1369     g_assert(data);
1370     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(data);
1372     // Look for changes than result in onscreen movement.
1373     if (!strcmp(name, "d") || !strcmp(name, "x") || !strcmp(name, "y") ||
1374             !strcmp(name, "width") || !strcmp(name, "height") ||
1375             !strcmp(name, "transform"))
1376     {
1377         if (repr == cc->active_shape_repr) {
1378             // Active shape has moved. Clear active shape.
1379             cc_clear_active_shape(cc);
1380         }
1381         else if (repr == cc->active_conn_repr) {
1382             // The active conn has been moved.
1383             // Set it again, which just sets new handle positions.
1384             cc_set_active_conn(cc, cc->active_conn);
1385         }
1386     }
1390 /*
1391   Local Variables:
1392   mode:c++
1393   c-file-style:"stroustrup"
1394   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1395   indent-tabs-mode:nil
1396   fill-column:99
1397   End:
1398 */
1399 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :