Code

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