Code

644aa05777bf979690454604dad5c22b17a5ce85
[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>
46 #include "connector-context.h"
47 #include "pixmaps/cursor-connector.xpm"
48 #include "xml/node-event-vector.h"
49 #include "xml/repr.h"
50 #include "svg/svg.h"
51 #include "desktop.h"
52 #include "desktop-style.h"
53 #include "desktop-affine.h"
54 #include "desktop-handles.h"
55 #include "document.h"
56 #include "message-context.h"
57 #include "message-stack.h"
58 #include "selection.h"
59 #include "inkscape.h"
60 #include "prefs-utils.h"
61 #include "sp-path.h"
62 #include "display/canvas-bpath.h"
63 #include "display/sodipodi-ctrl.h"
64 #include <glibmm/i18n.h>
65 #include "snap.h"
66 #include "knot.h"
67 #include "sp-conn-end.h"
68 #include "conn-avoid-ref.h"
69 #include "libavoid/vertices.h"
70 #include "context-fns.h"
71 #include "sp-namedview.h"
72 #include "sp-text.h"
73 #include "sp-flowtext.h"
76 static void sp_connector_context_class_init(SPConnectorContextClass *klass);
77 static void sp_connector_context_init(SPConnectorContext *conn_context);
78 static void sp_connector_context_dispose(GObject *object);
80 static void sp_connector_context_setup(SPEventContext *ec);
81 static void sp_connector_context_finish(SPEventContext *ec);
82 static gint sp_connector_context_root_handler(SPEventContext *ec, GdkEvent *event);
83 static gint sp_connector_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
85 // Stuff borrowed from DrawContext
86 static void spcc_connector_set_initial_point(SPConnectorContext *cc, NR::Point const p);
87 static void spcc_connector_set_subsequent_point(SPConnectorContext *cc, NR::Point const p);
88 static void spcc_connector_finish_segment(SPConnectorContext *cc, NR::Point p);
89 static void spcc_reset_colors(SPConnectorContext *cc);
90 static void spcc_connector_finish(SPConnectorContext *cc);
91 static void spcc_concat_colors_and_flush(SPConnectorContext *cc);
92 static void spcc_flush_white(SPConnectorContext *cc, SPCurve *gc);
94 // Context event handlers
95 static gint connector_handle_button_press(SPConnectorContext *const cc, GdkEventButton const &bevent);
96 static gint connector_handle_motion_notify(SPConnectorContext *const cc, GdkEventMotion const &mevent);
97 static gint connector_handle_button_release(SPConnectorContext *const cc, GdkEventButton const &revent);
98 static gint connector_handle_key_press(SPConnectorContext *const cc, guint const keyval);
100 static void cc_set_active_shape(SPConnectorContext *cc, SPItem *item);
101 static void cc_clear_active_shape(SPConnectorContext *cc);
102 static void cc_set_active_conn(SPConnectorContext *cc, SPItem *item);
103 static void cc_clear_active_conn(SPConnectorContext *cc);
104 static gchar *conn_pt_handle_test(SPConnectorContext *cc, NR::Point& w);
105 static bool cc_item_is_shape(SPItem *item);
106 static void cc_selection_changed(Inkscape::Selection *selection, gpointer data);
108 static void shape_event_attr_deleted(Inkscape::XML::Node *repr,
109         Inkscape::XML::Node *child, Inkscape::XML::Node *ref, gpointer data);
110 static void shape_event_attr_changed(Inkscape::XML::Node *repr, gchar const *name,
111         gchar const *old_value, gchar const *new_value, bool is_interactive,
112         gpointer data);
115 static NR::Point connector_drag_origin_w(0, 0);
116 static bool connector_within_tolerance = false;
117 static SPEventContextClass *parent_class;
120 static Inkscape::XML::NodeEventVector shape_repr_events = {
121     NULL, /* child_added */
122     NULL, /* child_added */
123     shape_event_attr_changed,
124     NULL, /* content_changed */
125     NULL  /* order_changed */
126 };
128 static Inkscape::XML::NodeEventVector layer_repr_events = {
129     NULL, /* child_added */
130     shape_event_attr_deleted,
131     NULL, /* child_added */
132     NULL, /* content_changed */
133     NULL  /* order_changed */
134 };
137 GType
138 sp_connector_context_get_type(void)
140     static GType type = 0;
141     if (!type) {
142         GTypeInfo info = {
143             sizeof(SPConnectorContextClass),
144             NULL, NULL,
145             (GClassInitFunc) sp_connector_context_class_init,
146             NULL, NULL,
147             sizeof(SPConnectorContext),
148             4,
149             (GInstanceInitFunc) sp_connector_context_init,
150             NULL,   /* value_table */
151         };
152         type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPConnectorContext", &info, (GTypeFlags)0);
153     }
154     return type;
157 static void
158 sp_connector_context_class_init(SPConnectorContextClass *klass)
160     GObjectClass *object_class;
161     SPEventContextClass *event_context_class;
163     object_class = (GObjectClass *) klass;
164     event_context_class = (SPEventContextClass *) klass;
166     parent_class = (SPEventContextClass*)g_type_class_peek_parent(klass);
168     object_class->dispose = sp_connector_context_dispose;
170     event_context_class->setup = sp_connector_context_setup;
171     event_context_class->finish = sp_connector_context_finish;
172     event_context_class->root_handler = sp_connector_context_root_handler;
173     event_context_class->item_handler = sp_connector_context_item_handler;
177 static void
178 sp_connector_context_init(SPConnectorContext *cc)
180     SPEventContext *ec = SP_EVENT_CONTEXT(cc);
182     ec->cursor_shape = cursor_connector_xpm;
183     ec->hot_x = 1;
184     ec->hot_y = 1;
185     ec->xp = 0;
186     ec->yp = 0;
188     cc->red_color = 0xff00007f;
190     cc->newconn = NULL;
191     cc->newConnRef = NULL;
193     cc->sel_changed_connection = sigc::connection();
195     cc->active_shape = NULL;
196     cc->active_shape_repr = NULL;
197     cc->active_shape_layer_repr = NULL;
199     cc->active_conn = NULL;
200     cc->active_conn_repr = NULL;
202     cc->active_handle = NULL;
204     cc->clickeditem = NULL;
205     cc->clickedhandle = NULL;
207     cc->connpthandle = NULL;
208     for (int i = 0; i < 2; ++i) {
209         cc->endpt_handle[i] = NULL;
210         cc->endpt_handler_id[i] = 0;
211     }
212     cc->sid = NULL;
213     cc->eid = NULL;
214     cc->npoints = 0;
215     cc->state = SP_CONNECTOR_CONTEXT_IDLE;
219 static void
220 sp_connector_context_dispose(GObject *object)
222     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(object);
224     cc->sel_changed_connection.disconnect();
226     if (cc->connpthandle) {
227         g_object_unref(cc->connpthandle);
228         cc->connpthandle = NULL;
229     }
230     for (int i = 0; i < 2; ++i) {
231         if (cc->endpt_handle[1]) {
232             g_object_unref(cc->endpt_handle[i]);
233             cc->endpt_handle[i] = NULL;
234         }
235     }
236     if (cc->sid) {
237         g_free(cc->sid);
238         cc->sid = NULL;
239     }
240     if (cc->eid) {
241         g_free(cc->eid);
242         cc->eid = NULL;
243     }
244     g_assert( cc->newConnRef == NULL );
246     G_OBJECT_CLASS(parent_class)->dispose(object);
250 static void
251 sp_connector_context_setup(SPEventContext *ec)
253     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(ec);
254     SPDesktop *dt = ec->desktop;
256     if (((SPEventContextClass *) parent_class)->setup) {
257         ((SPEventContextClass *) parent_class)->setup(ec);
258     }
260     cc->selection = sp_desktop_selection(dt);
262     cc->sel_changed_connection.disconnect();
263     cc->sel_changed_connection = cc->selection->connectChanged(
264             sigc::bind(sigc::ptr_fun(&cc_selection_changed),
265             (gpointer) cc));
267     /* Create red bpath */
268     cc->red_bpath = sp_canvas_bpath_new(sp_desktop_sketch(ec->desktop), NULL);
269     sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cc->red_bpath), cc->red_color,
270             1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
271     sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(cc->red_bpath), 0x00000000,
272             SP_WIND_RULE_NONZERO);
273     /* Create red curve */
274     cc->red_curve = sp_curve_new_sized(4);
276     /* Create green curve */
277     cc->green_curve = sp_curve_new_sized(64);
279     // Notice the initial selection.
280     cc_selection_changed(cc->selection, (gpointer) cc);
282     if (prefs_get_int_attribute("tools.connector", "selcue", 0) != 0) {
283         ec->enableSelectionCue();
284     }
286     // Make sure we see all enter events for canvas items,
287     // even if a mouse button is depressed.
288     dt->canvas->gen_all_enter_events = true;
292 static void
293 sp_connector_context_finish(SPEventContext *ec)
295     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(ec);
297     spcc_connector_finish(cc);
299     if (((SPEventContextClass *) parent_class)->finish) {
300         ((SPEventContextClass *) parent_class)->finish(ec);
301     }
303     if (cc->selection) {
304         cc->selection = NULL;
305     }
306     cc_clear_active_shape(cc);
307     cc_clear_active_conn(cc);
309     // Restore the default event generating behaviour.
310     SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(ec);
311     desktop->canvas->gen_all_enter_events = false;
315 //-----------------------------------------------------------------------------
318 static void
319 cc_clear_active_shape(SPConnectorContext *cc)
321     if (cc->active_shape == NULL) {
322         return;
323     }
324     g_assert( cc->active_shape_repr );
325     g_assert( cc->active_shape_layer_repr );
327     cc->active_shape = NULL;
329     if (cc->active_shape_repr) {
330         sp_repr_remove_listener_by_data(cc->active_shape_repr, cc);
331         Inkscape::GC::release(cc->active_shape_repr);
332         cc->active_shape_repr = NULL;
334         sp_repr_remove_listener_by_data(cc->active_shape_layer_repr, cc);
335         Inkscape::GC::release(cc->active_shape_layer_repr);
336         cc->active_shape_layer_repr = NULL;
337     }
339     // Hide the center connection point if it exists.
340     if (cc->connpthandle) {
341         sp_knot_hide(cc->connpthandle);
342     }
346 static void
347 cc_clear_active_conn(SPConnectorContext *cc)
349     if (cc->active_conn == NULL) {
350         return;
351     }
352     g_assert( cc->active_conn_repr );
354     cc->active_conn = NULL;
356     if (cc->active_conn_repr) {
357         sp_repr_remove_listener_by_data(cc->active_conn_repr, cc);
358         Inkscape::GC::release(cc->active_conn_repr);
359         cc->active_conn_repr = NULL;
360     }
362     // Hide the endpoint handles.
363     for (int i = 0; i < 2; ++i) {
364         if (cc->endpt_handle[i]) {
365             sp_knot_hide(cc->endpt_handle[i]);
366         }
367     }
371 static gchar *
372 conn_pt_handle_test(SPConnectorContext *cc, NR::Point& p)
374     // TODO: this will need to change when there are more connection
375     //       points available for each shape.
377     SPKnot *centerpt = cc->connpthandle;
378     if (cc->active_handle && (cc->active_handle == centerpt))
379     {
380         p = centerpt->pos;
381         return g_strdup_printf("#%s", SP_OBJECT_ID(cc->active_shape));
382     }
383     return NULL;
388 static gint
389 sp_connector_context_item_handler(SPEventContext *ec, SPItem *item, GdkEvent *event)
391     gint ret = FALSE;
393     SPDesktop *desktop = ec->desktop;
395     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(ec);
397     NR::Point p(event->button.x, event->button.y);
399     switch (event->type) {
400         case GDK_BUTTON_RELEASE:
401             if (event->button.button == 1) {
402                 if ((cc->state == SP_CONNECTOR_CONTEXT_DRAGGING) &&
403                         (connector_within_tolerance))
404                 {
405                     spcc_reset_colors(cc);
406                     cc->state = SP_CONNECTOR_CONTEXT_IDLE;
407                 }
408                 if (cc->state != SP_CONNECTOR_CONTEXT_IDLE) {
409                     // Doing simething else like rerouting.
410                     break;
411                 }
412                 // find out clicked item, disregarding groups, honoring Alt
413                 SPItem *item_ungrouped = sp_event_context_find_item(desktop,
414                         p, event->button.state & GDK_MOD1_MASK, TRUE);
416                 if (event->button.state & GDK_SHIFT_MASK) {
417                     cc->selection->toggle(item_ungrouped);
418                 } else {
419                     cc->selection->set(item_ungrouped);
420                 }
421                 ret = TRUE;
422             }
423             break;
424         case GDK_ENTER_NOTIFY:
425         {
426             if (cc_item_is_shape(item)) {
427                 // This is a shape, so show connection point(s).
428                 if (!(cc->active_shape) ||
429                         // Don't show handle for another handle.
430                         (item != ((SPItem *) cc->connpthandle))) {
431                     cc_set_active_shape(cc, item);
432                 }
433             }
434             ret = TRUE;
435             break;
436         }
437         default:
438             break;
439     }
441     return ret;
445 gint
446 sp_connector_context_root_handler(SPEventContext *ec, GdkEvent *event)
448     SPConnectorContext *const cc = SP_CONNECTOR_CONTEXT(ec);
450     gint ret = FALSE;
452     switch (event->type) {
453         case GDK_BUTTON_PRESS:
454             ret = connector_handle_button_press(cc, event->button);
455             break;
457         case GDK_MOTION_NOTIFY:
458             ret = connector_handle_motion_notify(cc, event->motion);
459             break;
461         case GDK_BUTTON_RELEASE:
462             ret = connector_handle_button_release(cc, event->button);
463             break;
464         case GDK_KEY_PRESS:
465             ret = connector_handle_key_press(cc, get_group0_keyval (&event->key));
466             break;
468         default:
469             break;
470     }
472     if (!ret) {
473         gint (*const parent_root_handler)(SPEventContext *, GdkEvent *)
474             = ((SPEventContextClass *) parent_class)->root_handler;
475         if (parent_root_handler) {
476             ret = parent_root_handler(ec, event);
477         }
478     }
480     return ret;
484 static gint
485 connector_handle_button_press(SPConnectorContext *const cc, GdkEventButton const &bevent)
487     NR::Point const event_w(bevent.x, bevent.y);
488     /* Find desktop coordinates */
489     NR::Point p = cc->desktop->w2d(event_w);
491     gint ret = FALSE;
492     if ( bevent.button == 1 ) {
494         SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
496         if (Inkscape::have_viable_layer(desktop, cc->_message_context) == false) {
497             return TRUE;
498         }
500         NR::Point const event_w(bevent.x,
501                                 bevent.y);
502         connector_drag_origin_w = event_w;
503         connector_within_tolerance = true;
505         NR::Point const event_dt = cc->desktop->w2d(event_w);
506         switch (cc->state) {
507             case SP_CONNECTOR_CONTEXT_STOP:
508                 /* This is allowed, if we just cancelled curve */
509             case SP_CONNECTOR_CONTEXT_IDLE:
510             {
511                 if ( cc->npoints == 0 ) {
512                     /* Set start anchor */
513                     NR::Point p;
515                     cc_clear_active_conn(cc);
517                     SP_EVENT_CONTEXT_DESKTOP(cc)->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating new connector"));
519                     /* Create green anchor */
520                     p = event_dt;
522                     // Test whether we clicked on a connection point
523                     cc->sid = conn_pt_handle_test(cc, p);
525                     if (!cc->sid) {
526                         // This is the first point, so just snap it to the grid
527                         // as there's no other points to go off.
528                         SnapManager const &m = cc->desktop->namedview->snap_manager;
529                         p = m.freeSnap(Inkscape::Snapper::SNAP_POINT | Inkscape::Snapper::BBOX_POINT,
530                                        p, NULL).getPoint();
531                     }
532                     spcc_connector_set_initial_point(cc, p);
534                 }
535                 cc->state = SP_CONNECTOR_CONTEXT_DRAGGING;
536                 ret = TRUE;
537                 break;
538             }
539             case SP_CONNECTOR_CONTEXT_DRAGGING:
540             {
541                 // This is the second click of a connector creation.
543                 spcc_connector_set_subsequent_point(cc, p);
544                 spcc_connector_finish_segment(cc, p);
545                 // Test whether we clicked on a connection point
546                 cc->eid = conn_pt_handle_test(cc, p);
547                 if (cc->npoints != 0) {
548                     spcc_connector_finish(cc);
549                 }
550                 cc_set_active_conn(cc, cc->newconn);
551                 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
552                 ret = TRUE;
553                 break;
554             }
555             case SP_CONNECTOR_CONTEXT_CLOSE:
556             {
557                 g_warning("Button down in CLOSE state");
558                 break;
559             }
560             default:
561                 break;
562         }
563     } else if (bevent.button == 3) {
564         if (cc->npoints != 0) {
565             spcc_connector_finish(cc);
566             ret = TRUE;
567         }
568     }
569     return ret;
573 static gint
574 connector_handle_motion_notify(SPConnectorContext *const cc, GdkEventMotion const &mevent)
576     gint ret = FALSE;
578     if (mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) {
579         // allow middle-button scrolling
580         return FALSE;
581     }
583     NR::Point const event_w(mevent.x, mevent.y);
585     if (connector_within_tolerance) {
586         gint const tolerance = prefs_get_int_attribute_limited("options.dragtolerance",
587                                                                "value", 0, 0, 100);
588         if ( NR::LInfty( event_w - connector_drag_origin_w ) < tolerance ) {
589             return FALSE;   // Do not drag if we're within tolerance from origin.
590         }
591     }
592     // Once the user has moved farther than tolerance from the original location
593     // (indicating they intend to move the object, not click), then always process
594     // the motion notify coordinates as given (no snapping back to origin)
595     connector_within_tolerance = false;
597     SPDesktop *const dt = cc->desktop;
599     /* Find desktop coordinates */
600     NR::Point p = dt->w2d(event_w);
602     switch (cc->state) {
603         case SP_CONNECTOR_CONTEXT_DRAGGING:
604         {
605             // This is movement during a connector creation.
607             if ( cc->npoints > 0 ) {
608                 cc->selection->clear();
609                 spcc_connector_set_subsequent_point(cc, p);
610                 ret = TRUE;
611             }
612             break;
613         }
614         case SP_CONNECTOR_CONTEXT_REROUTING:
615         {
616             g_assert( SP_IS_PATH(cc->clickeditem));
618             // Update the hidden path
619             NR::Matrix i2d = sp_item_i2d_affine(cc->clickeditem);
620             NR::Matrix d2i = i2d.inverse();
621             SPPath *path = SP_PATH(cc->clickeditem);
622             SPCurve *curve = (SP_SHAPE(path))->curve;
623             if (cc->clickedhandle == cc->endpt_handle[0]) {
624                 NR::Point o = cc->endpt_handle[1]->pos;
625                 sp_curve_stretch_endpoints(curve, p * d2i, o * d2i);
626             }
627             else {
628                 NR::Point o = cc->endpt_handle[0]->pos;
629                 sp_curve_stretch_endpoints(curve, o * d2i, p * d2i);
630             }
631             sp_conn_adjust_path(path);
633             // Copy this to the temporary visible path
634             cc->red_curve = sp_curve_copy(SP_SHAPE(path)->curve);
635             sp_curve_transform(cc->red_curve, i2d);
637             sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), cc->red_curve);
638             ret = TRUE;
639             break;
640         }
641         case SP_CONNECTOR_CONTEXT_STOP:
642             /* This is perfectly valid */
643             break;
644         default:
645             break;
646     }
648     return ret;
652 static gint
653 connector_handle_button_release(SPConnectorContext *const cc, GdkEventButton const &revent)
655     gint ret = FALSE;
656     if ( revent.button == 1 ) {
658         SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
659         SPDocument *doc = sp_desktop_document(desktop);
661         NR::Point const event_w(revent.x, revent.y);
663         /* Find desktop coordinates */
664         NR::Point p = cc->desktop->w2d(event_w);
666         switch (cc->state) {
667             //case SP_CONNECTOR_CONTEXT_POINT:
668             case SP_CONNECTOR_CONTEXT_DRAGGING:
669             {
670                 if (connector_within_tolerance)
671                 {
672                     spcc_connector_finish_segment(cc, p);
673                     return TRUE;
674                 }
675                 // Connector has been created via a drag, end it now.
676                 spcc_connector_set_subsequent_point(cc, p);
677                 spcc_connector_finish_segment(cc, p);
678                 // Test whether we clicked on a connection point
679                 cc->eid = conn_pt_handle_test(cc, p);
680                 if (cc->npoints != 0) {
681                     spcc_connector_finish(cc);
682                 }
683                 cc_set_active_conn(cc, cc->newconn);
684                 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
685                 break;
686             }
687             case SP_CONNECTOR_CONTEXT_REROUTING:
688             {
689                 // Clear the temporary path:
690                 sp_curve_reset(cc->red_curve);
691                 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
693                 // Test whether we clicked on a connection point
694                 gchar *shape_label = conn_pt_handle_test(cc, p);
696                 if (shape_label) {
697                     if (cc->clickedhandle == cc->endpt_handle[0]) {
698                         sp_object_setAttribute(cc->clickeditem,
699                                 "inkscape:connection-start",shape_label, false);
700                     }
701                     else {
702                         sp_object_setAttribute(cc->clickeditem,
703                                 "inkscape:connection-end",shape_label, false);
704                     }
705                     g_free(shape_label);
706                 }
707                 cc->clickeditem->setHidden(false);
708                 sp_conn_adjust_path(SP_PATH(cc->clickeditem));
709                 cc->clickeditem->updateRepr();
710                 sp_document_done(doc, SP_VERB_CONTEXT_CONNECTOR, 
711                                  _("Reroute connector"));
712                 cc_set_active_conn(cc, cc->clickeditem);
713                 sp_document_ensure_up_to_date(doc);
714                 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
715                 return TRUE;
716                 break;
717             }
718             case SP_CONNECTOR_CONTEXT_STOP:
719                 /* This is allowed, if we just cancelled curve */
720                 break;
721             default:
722                 break;
723         }
724         ret = TRUE;
725     }
727     return ret;
731 static gint
732 connector_handle_key_press(SPConnectorContext *const cc, guint const keyval)
734     gint ret = FALSE;
735     /* fixme: */
736     switch (keyval) {
737         case GDK_Return:
738         case GDK_KP_Enter:
739             if (cc->npoints != 0) {
740                 spcc_connector_finish(cc);
741                 ret = TRUE;
742             }
743             break;
744         case GDK_Escape:
745             if (cc->state == SP_CONNECTOR_CONTEXT_REROUTING) {
746                 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
747                 SPDocument *doc = sp_desktop_document(desktop);
748                 // Clear the temporary path:
749                 sp_curve_reset(cc->red_curve);
750                 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
752                 cc->clickeditem->setHidden(false);
753                 sp_document_done(doc, SP_VERB_CONTEXT_CONNECTOR, 
754                                  _("Reroute connector"));
755                 cc_set_active_conn(cc, cc->clickeditem);
756                 
757                 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
758  
759                 sp_document_undo(doc);
760                 desktop->messageStack()->flash( Inkscape::NORMAL_MESSAGE,
761                         _("Connector endpoint drag canceled."));
762                 ret = TRUE;
763             }
764             else if (cc->npoints != 0) {
765                 // if drawing, cancel, otherwise pass it up for deselecting
766                 cc->state = SP_CONNECTOR_CONTEXT_STOP;
767                 spcc_reset_colors(cc);
768                 ret = TRUE;
769             }
770             break;
771         default:
772             break;
773     }
774     return ret;
778 static void
779 spcc_reset_colors(SPConnectorContext *cc)
781     /* Red */
782     sp_curve_reset(cc->red_curve);
783     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
785     sp_curve_reset(cc->green_curve);
786     cc->npoints = 0;
790 static void
791 spcc_connector_set_initial_point(SPConnectorContext *const cc, NR::Point const p)
793     g_assert( cc->npoints == 0 );
795     cc->p[0] = p;
796     cc->p[1] = p;
797     cc->npoints = 2;
798     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
802 static void
803 spcc_connector_set_subsequent_point(SPConnectorContext *const cc, NR::Point const p)
805     g_assert( cc->npoints != 0 );
807     SPDesktop *dt = cc->desktop;
808     NR::Point o = dt->dt2doc(cc->p[0]);
809     NR::Point d = dt->dt2doc(p);
810     Avoid::Point src(o[NR::X], o[NR::Y]);
811     Avoid::Point dst(d[NR::X], d[NR::Y]);
813     if (!cc->newConnRef) {
814         Avoid::Router *router = sp_desktop_document(dt)->router;
815         cc->newConnRef = new Avoid::ConnRef(router, 0, src, dst);
816         cc->newConnRef->updateEndPoint(Avoid::VertID::src, src);
817     }
818     cc->newConnRef->updateEndPoint(Avoid::VertID::tar, dst);
820     cc->newConnRef->makePathInvalid();
821     cc->newConnRef->generatePath(src, dst);
823     Avoid::PolyLine route = cc->newConnRef->route();
824     cc->newConnRef->calcRouteDist();
826     sp_curve_reset(cc->red_curve);
827     NR::Point pt(route.ps[0].x, route.ps[0].y);
828     sp_curve_moveto(cc->red_curve, pt);
830     for (int i = 1; i < route.pn; ++i) {
831         NR::Point p(route.ps[i].x, route.ps[i].y);
832         sp_curve_lineto(cc->red_curve, p);
833     }
834     sp_curve_transform(cc->red_curve, dt->doc2dt());
835     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), cc->red_curve);
839 /**
840  * Concats red, blue and green.
841  * If any anchors are defined, process these, optionally removing curves from white list
842  * Invoke _flush_white to write result back to object.
843  */
844 static void
845 spcc_concat_colors_and_flush(SPConnectorContext *cc)
847     SPCurve *c = cc->green_curve;
848     cc->green_curve = sp_curve_new_sized(64);
850     sp_curve_reset(cc->red_curve);
851     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
853     if (sp_curve_empty(c)) {
854         sp_curve_unref(c);
855         return;
856     }
858     spcc_flush_white(cc, c);
860     sp_curve_unref(c);
864 /*
865  * Flushes white curve(s) and additional curve into object
866  *
867  * No cleaning of colored curves - this has to be done by caller
868  * No rereading of white data, so if you cannot rely on ::modified, do it in caller
869  *
870  */
872 static void
873 spcc_flush_white(SPConnectorContext *cc, SPCurve *gc)
875     SPCurve *c;
877     if (gc) {
878         c = gc;
879         sp_curve_ref(c);
880     } else {
881         return;
882     }
884     /* Now we have to go back to item coordinates at last */
885     sp_curve_transform(c,
886             sp_desktop_dt2root_affine(SP_EVENT_CONTEXT_DESKTOP(cc)));
888     SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
889     SPDocument *doc = sp_desktop_document(desktop);
890     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
892     if ( c && !sp_curve_empty(c) ) {
893         /* We actually have something to write */
895         Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
896         /* Set style */
897         sp_desktop_apply_style_tool(desktop, repr, "tools.connector", false);
899         gchar *str = sp_svg_write_path(SP_CURVE_BPATH(c));
900         g_assert( str != NULL );
901         repr->setAttribute("d", str);
902         g_free(str);
904         /* Attach repr */
905         cc->newconn = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
906         cc->selection->set(repr);
907         Inkscape::GC::release(repr);
908         cc->newconn->transform = i2i_affine(desktop->currentRoot(), desktop->currentLayer());
909         cc->newconn->updateRepr();
911         bool connection = false;
912         sp_object_setAttribute(cc->newconn, "inkscape:connector-type",
913                 "polyline", false);
914         if (cc->sid)
915         {
916             sp_object_setAttribute(cc->newconn, "inkscape:connection-start",
917                     cc->sid, false);
918             connection = true;
919         }
921         if (cc->eid)
922         {
923             sp_object_setAttribute(cc->newconn, "inkscape:connection-end",
924                     cc->eid, false);
925             connection = true;
926         }
927         cc->newconn->updateRepr();
928         if (connection) {
929             // Adjust endpoints to shape edge.
930             sp_conn_adjust_path(SP_PATH(cc->newconn));
931         }
932         cc->newconn->updateRepr();
933     }
935     sp_curve_unref(c);
937     /* Flush pending updates */
938     sp_document_done(doc, SP_VERB_CONTEXT_CONNECTOR, _("Create connector"));
939     sp_document_ensure_up_to_date(doc);
943 static void
944 spcc_connector_finish_segment(SPConnectorContext *const cc, NR::Point const p)
946     if (!sp_curve_empty(cc->red_curve)) {
947         sp_curve_append_continuous(cc->green_curve, cc->red_curve, 0.0625);
949         cc->p[0] = cc->p[3];
950         cc->p[1] = cc->p[4];
951         cc->npoints = 2;
953         sp_curve_reset(cc->red_curve);
954     }
958 static void
959 spcc_connector_finish(SPConnectorContext *const cc)
961     SPDesktop *const desktop = cc->desktop;
962     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Finishing connector"));
964     sp_curve_reset(cc->red_curve);
965     spcc_concat_colors_and_flush(cc);
967     cc->npoints = 0;
969     if (cc->newConnRef) {
970         cc->newConnRef->removeFromGraph();
971         delete cc->newConnRef;
972         cc->newConnRef = NULL;
973     }
974     cc->state = SP_CONNECTOR_CONTEXT_IDLE;
978 static gboolean
979 cc_generic_knot_handler(SPCanvasItem *, GdkEvent *event, SPKnot *knot)
981     g_assert (knot != NULL);
982     
983     g_object_ref(knot);
985     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(
986             knot->desktop->event_context);
988     gboolean consumed = FALSE;
990     switch (event->type) {
991         case GDK_ENTER_NOTIFY:
992             sp_knot_set_flag(knot, SP_KNOT_MOUSEOVER, TRUE);
993             
994             cc->active_handle = knot;
996             if (knot->tip)
997             {
998                 knot->desktop->event_context->defaultMessageContext()->set(
999                         Inkscape::NORMAL_MESSAGE, knot->tip);
1000             }
1001             
1002             consumed = TRUE;
1003             break;
1004         case GDK_LEAVE_NOTIFY:
1005             sp_knot_set_flag(knot, SP_KNOT_MOUSEOVER, FALSE);
1007             cc->active_handle = NULL;
1008             
1009             if (knot->tip) {
1010                 knot->desktop->event_context->defaultMessageContext()->clear();
1011             }
1012             
1013             consumed = TRUE;
1014             break;
1015         default:
1016             break;
1017     }
1018     
1019     g_object_unref(knot);
1021     return consumed;
1025 static gboolean
1026 endpt_handler(SPKnot *knot, GdkEvent *event, SPConnectorContext *cc)
1028     g_assert( SP_IS_CONNECTOR_CONTEXT(cc) );
1030     gboolean consumed = FALSE;
1032     switch (event->type) {
1033         case GDK_BUTTON_PRESS:
1034             g_assert( (cc->active_handle == cc->endpt_handle[0]) ||
1035                       (cc->active_handle == cc->endpt_handle[1]) );
1036             if (cc->state == SP_CONNECTOR_CONTEXT_IDLE) {
1037                 cc->clickeditem = cc->active_conn;
1038                 cc->clickedhandle = cc->active_handle;
1039                 cc_clear_active_conn(cc);
1040                 cc->state = SP_CONNECTOR_CONTEXT_REROUTING;
1042                 // Disconnect from attached shape
1043                 unsigned ind = (cc->active_handle == cc->endpt_handle[0]) ? 0 : 1;
1044                 sp_conn_end_detach(cc->clickeditem, ind);
1046                 NR::Point origin;
1047                 if (cc->clickedhandle == cc->endpt_handle[0]) {
1048                     origin = cc->endpt_handle[1]->pos;
1049                 }
1050                 else {
1051                     origin = cc->endpt_handle[0]->pos;
1052                 }
1054                 // Show the red path for dragging.
1055                 cc->red_curve = sp_curve_copy(SP_PATH(cc->clickeditem)->curve);
1056                 NR::Matrix i2d = sp_item_i2d_affine(cc->clickeditem);
1057                 sp_curve_transform(cc->red_curve, i2d);
1058                 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath),
1059                         cc->red_curve);
1061                 cc->clickeditem->setHidden(true);
1063                 // The rest of the interaction rerouting the connector is
1064                 // handled by the context root handler.
1065                 consumed = TRUE;
1066             }
1067             break;
1068         default:
1069             break;
1070     }
1072     return consumed;
1076 static void cc_set_active_shape(SPConnectorContext *cc, SPItem *item)
1078     g_assert(item != NULL );
1080     cc->active_shape = item;
1082     // Remove existing active shape listeners
1083     if (cc->active_shape_repr) {
1084         sp_repr_remove_listener_by_data(cc->active_shape_repr, cc);
1085         Inkscape::GC::release(cc->active_shape_repr);
1087         sp_repr_remove_listener_by_data(cc->active_shape_layer_repr, cc);
1088         Inkscape::GC::release(cc->active_shape_layer_repr);
1089     }
1091     // Listen in case the active shape changes
1092     cc->active_shape_repr = SP_OBJECT_REPR(item);
1093     if (cc->active_shape_repr) {
1094         Inkscape::GC::anchor(cc->active_shape_repr);
1095         sp_repr_add_listener(cc->active_shape_repr, &shape_repr_events, cc);
1097         cc->active_shape_layer_repr = cc->active_shape_repr->parent();
1098         Inkscape::GC::anchor(cc->active_shape_layer_repr);
1099         sp_repr_add_listener(cc->active_shape_layer_repr, &layer_repr_events, cc);
1100     }
1103     // Set center connection point.
1104     if ( cc->connpthandle == NULL ) {
1105         SPKnot *knot = sp_knot_new(cc->desktop, 
1106                 _("<b>Connection point</b>: click or drag to create a new connector"));
1108         knot->setShape(SP_KNOT_SHAPE_SQUARE);
1109         knot->setSize(8);
1110         knot->setAnchor(GTK_ANCHOR_CENTER);
1111         knot->setFill(0xffffff00, 0xff0000ff, 0xff0000ff);
1112         sp_knot_update_ctrl(knot);
1114         // We don't want to use the standard knot handler,
1115         //since we don't want this knot to be draggable.
1116         g_signal_handler_disconnect(G_OBJECT(knot->item),
1117                 knot->_event_handler_id);
1118         knot->_event_handler_id = 0;
1120         gtk_signal_connect(GTK_OBJECT(knot->item), "event",
1121                 GTK_SIGNAL_FUNC(cc_generic_knot_handler), knot);
1123         cc->connpthandle = knot;
1124     }
1127     NR::Rect bbox = sp_item_bbox_desktop(cc->active_shape);
1128     NR::Point center = bbox.midpoint();
1129     sp_knot_set_position(cc->connpthandle, &center, 0);
1131     sp_knot_show(cc->connpthandle);
1136 static void
1137 cc_set_active_conn(SPConnectorContext *cc, SPItem *item)
1139     g_assert( SP_IS_PATH(item) );
1141     SPCurve *curve = SP_SHAPE(SP_PATH(item))->curve;
1142     NR::Matrix i2d = sp_item_i2d_affine(item);
1144     if (cc->active_conn == item)
1145     {
1146         // Just adjust handle positions.
1147         NR::Point startpt = sp_curve_first_point(curve) * i2d;
1148         sp_knot_set_position(cc->endpt_handle[0], &startpt, 0);
1150         NR::Point endpt = sp_curve_last_point(curve) * i2d;
1151         sp_knot_set_position(cc->endpt_handle[1], &endpt, 0);
1153         return;
1154     }
1156     cc->active_conn = item;
1158     // Remove existing active conn listeners
1159     if (cc->active_conn_repr) {
1160         sp_repr_remove_listener_by_data(cc->active_conn_repr, cc);
1161         Inkscape::GC::release(cc->active_conn_repr);
1162         cc->active_conn_repr = NULL;
1163     }
1165     // Listen in case the active conn changes
1166     cc->active_conn_repr = SP_OBJECT_REPR(item);
1167     if (cc->active_conn_repr) {
1168         Inkscape::GC::anchor(cc->active_conn_repr);
1169         sp_repr_add_listener(cc->active_conn_repr, &shape_repr_events, cc);
1170     }
1172     for (int i = 0; i < 2; ++i) {
1174         // Create the handle if it doesn't exist
1175         if ( cc->endpt_handle[i] == NULL ) {
1176             SPKnot *knot = sp_knot_new(cc->desktop, 
1177                     _("<b>Connector endpoint</b>: drag to reroute or connect to new shapes"));
1179             knot->setShape(SP_KNOT_SHAPE_SQUARE);
1180             knot->setSize(7);
1181             knot->setAnchor(GTK_ANCHOR_CENTER);
1182             knot->setFill(0xffffff00, 0xff0000ff, 0xff0000ff);
1183             knot->setStroke(0x000000ff, 0x000000ff, 0x000000ff);
1184             sp_knot_update_ctrl(knot);
1186             // We don't want to use the standard knot handler,
1187             //since we don't want this knot to be draggable.
1188             g_signal_handler_disconnect(G_OBJECT(knot->item),
1189                     knot->_event_handler_id);
1190             knot->_event_handler_id = 0;
1192             gtk_signal_connect(GTK_OBJECT(knot->item), "event",
1193                     GTK_SIGNAL_FUNC(cc_generic_knot_handler), knot);
1195             cc->endpt_handle[i] = knot;
1196         }
1198         // Remove any existing handlers
1199         if (cc->endpt_handler_id[i]) {
1200             g_signal_handlers_disconnect_by_func(
1201                     G_OBJECT(cc->endpt_handle[i]->item),
1202                     (void*)G_CALLBACK(endpt_handler), (gpointer) cc );
1203             cc->endpt_handler_id[i] = 0;
1204         }
1206         // Setup handlers for connector endpoints, this is
1207         // is as 'after' so that cc_generic_knot_handler is
1208         // triggered first for any endpoint.
1209         cc->endpt_handler_id[i] = g_signal_connect_after(
1210                 G_OBJECT(cc->endpt_handle[i]->item), "event",
1211                 G_CALLBACK(endpt_handler), cc);
1212     }
1214     NR::Point startpt = sp_curve_first_point(curve) * i2d;
1215     sp_knot_set_position(cc->endpt_handle[0], &startpt, 0);
1217     NR::Point endpt = sp_curve_last_point(curve) * i2d;
1218     sp_knot_set_position(cc->endpt_handle[1], &endpt, 0);
1220     sp_knot_show(cc->endpt_handle[0]);
1221     sp_knot_show(cc->endpt_handle[1]);
1225 static bool cc_item_is_shape(SPItem *item)
1227     if (SP_IS_PATH(item)) {
1228         SPCurve *curve = (SP_SHAPE(item))->curve;
1229         if ( curve && !(curve->closed) ) {
1230             // Open paths are connectors.
1231             return false;
1232         }
1233     }
1234     else if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
1235         if (prefs_get_int_attribute("tools.connector", "ignoretext", 1) == 1) {
1236             // Don't count text as a shape we can connect connector to.
1237             return false;
1238         }
1239     }
1240     return true;
1244 bool cc_item_is_connector(SPItem *item)
1246     if (SP_IS_PATH(item)) {
1247         if (SP_PATH(item)->connEndPair.isAutoRoutingConn()) {
1248             g_assert( !(SP_SHAPE(item)->curve->closed) );
1249             return true;
1250         }
1251     }
1252     return false;
1256 void cc_selection_set_avoid(bool const set_avoid)
1258     SPDesktop *desktop = inkscape_active_desktop();
1259     if (desktop == NULL) {
1260         return;
1261     }
1263     SPDocument *document = sp_desktop_document(desktop);
1265     Inkscape::Selection *selection = sp_desktop_selection(desktop);
1267     GSList *l = (GSList *) selection->itemList();
1269     int changes = 0;
1271     while (l) {
1272         SPItem *item = (SPItem *) l->data;
1274         char const *value = (set_avoid) ? "true" : NULL;
1276         if (cc_item_is_shape(item)) {
1277             sp_object_setAttribute(item, "inkscape:connector-avoid",
1278                     value, false);
1279             item->avoidRef->handleSettingChange();
1280             changes++;
1281         }
1283         l = l->next;
1284     }
1286     if (changes == 0) {
1287         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE,
1288                 _("Select <b>at least one non-connector object</b>."));
1289         return;
1290     }
1292     char *event_desc = (set_avoid) ?
1293             _("Make connectors avoid selected objects") :
1294             _("Make connectors ignore selected objects");
1295     sp_document_done(document, SP_VERB_CONTEXT_CONNECTOR, event_desc);
1299 static void
1300 cc_selection_changed(Inkscape::Selection *selection, gpointer data)
1302     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(data);
1303     //SPEventContext *ec = SP_EVENT_CONTEXT(cc);
1305     SPItem *item = selection->singleItem();
1307     if (cc->active_conn == item)
1308     {
1309         // Nothing to change.
1310         return;
1311     }
1312     if (item == NULL)
1313     {
1314         cc_clear_active_conn(cc);
1315         return;
1316     }
1318     if (cc_item_is_connector(item)) {
1319         cc_set_active_conn(cc, item);
1320     }
1324 static void
1325 shape_event_attr_deleted(Inkscape::XML::Node *repr, Inkscape::XML::Node *child,
1326         Inkscape::XML::Node *ref, gpointer data)
1328     g_assert(data);
1329     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(data);
1331     if (child == cc->active_shape_repr) {
1332         // The active shape has been deleted.  Clear active shape.
1333         cc_clear_active_shape(cc);
1334     }
1338 static void
1339 shape_event_attr_changed(Inkscape::XML::Node *repr, gchar const *name,
1340                             gchar const *old_value, gchar const *new_value,
1341                             bool is_interactive, gpointer data)
1343     g_assert(data);
1344     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(data);
1346     // Look for changes than result in onscreen movement.
1347     if (!strcmp(name, "d") || !strcmp(name, "x") || !strcmp(name, "y") ||
1348             !strcmp(name, "width") || !strcmp(name, "height") ||
1349             !strcmp(name, "transform"))
1350     {
1351         if (repr == cc->active_shape_repr) {
1352             // Active shape has moved. Clear active shape.
1353             cc_clear_active_shape(cc);
1354         }
1355         else if (repr == cc->active_conn_repr) {
1356             // The active conn has been moved.
1357             // Set it again, which just sets new handle positions.
1358             cc_set_active_conn(cc, cc->active_conn);
1359         }
1360     }
1364 /*
1365   Local Variables:
1366   mode:c++
1367   c-file-style:"stroustrup"
1368   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1369   indent-tabs-mode:nil
1370   fill-column:99
1371   End:
1372 */
1373 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :