Code

start switching sp_repr_new* over to XML::Document::create*, and rename create method...
[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->npoints != 0) {
746                 // if drawing, cancel, otherwise pass it up for deselecting
747                 cc->state = SP_CONNECTOR_CONTEXT_STOP;
748                 spcc_reset_colors(cc);
749                 ret = TRUE;
750             }
751             break;
752         default:
753             break;
754     }
755     return ret;
759 static void
760 spcc_reset_colors(SPConnectorContext *cc)
762     /* Red */
763     sp_curve_reset(cc->red_curve);
764     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
766     sp_curve_reset(cc->green_curve);
767     cc->npoints = 0;
771 static void
772 spcc_connector_set_initial_point(SPConnectorContext *const cc, NR::Point const p)
774     g_assert( cc->npoints == 0 );
776     cc->p[0] = p;
777     cc->p[1] = p;
778     cc->npoints = 2;
779     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
783 static void
784 spcc_connector_set_subsequent_point(SPConnectorContext *const cc, NR::Point const p)
786     g_assert( cc->npoints != 0 );
788     SPDesktop *dt = cc->desktop;
789     NR::Point o = dt->dt2doc(cc->p[0]);
790     NR::Point d = dt->dt2doc(p);
791     Avoid::Point src(o[NR::X], o[NR::Y]);
792     Avoid::Point dst(d[NR::X], d[NR::Y]);
794     if (!cc->newConnRef) {
795         Avoid::Router *router = sp_desktop_document(dt)->router;
796         cc->newConnRef = new Avoid::ConnRef(router, 0, src, dst);
797         cc->newConnRef->updateEndPoint(Avoid::VertID::src, src);
798     }
799     cc->newConnRef->updateEndPoint(Avoid::VertID::tar, dst);
801     cc->newConnRef->makePathInvalid();
802     cc->newConnRef->generatePath(src, dst);
804     Avoid::PolyLine route = cc->newConnRef->route();
805     cc->newConnRef->calcRouteDist();
807     sp_curve_reset(cc->red_curve);
808     NR::Point pt(route.ps[0].x, route.ps[0].y);
809     sp_curve_moveto(cc->red_curve, pt);
811     for (int i = 1; i < route.pn; ++i) {
812         NR::Point p(route.ps[i].x, route.ps[i].y);
813         sp_curve_lineto(cc->red_curve, p);
814     }
815     sp_curve_transform(cc->red_curve, dt->doc2dt());
816     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), cc->red_curve);
820 /**
821  * Concats red, blue and green.
822  * If any anchors are defined, process these, optionally removing curves from white list
823  * Invoke _flush_white to write result back to object.
824  */
825 static void
826 spcc_concat_colors_and_flush(SPConnectorContext *cc)
828     SPCurve *c = cc->green_curve;
829     cc->green_curve = sp_curve_new_sized(64);
831     sp_curve_reset(cc->red_curve);
832     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
834     if (sp_curve_empty(c)) {
835         sp_curve_unref(c);
836         return;
837     }
839     spcc_flush_white(cc, c);
841     sp_curve_unref(c);
845 /*
846  * Flushes white curve(s) and additional curve into object
847  *
848  * No cleaning of colored curves - this has to be done by caller
849  * No rereading of white data, so if you cannot rely on ::modified, do it in caller
850  *
851  */
853 static void
854 spcc_flush_white(SPConnectorContext *cc, SPCurve *gc)
856     SPCurve *c;
858     if (gc) {
859         c = gc;
860         sp_curve_ref(c);
861     } else {
862         return;
863     }
865     /* Now we have to go back to item coordinates at last */
866     sp_curve_transform(c,
867             sp_desktop_dt2root_affine(SP_EVENT_CONTEXT_DESKTOP(cc)));
869     SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
870     SPDocument *doc = sp_desktop_document(desktop);
871     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
873     if ( c && !sp_curve_empty(c) ) {
874         /* We actually have something to write */
876         Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
877         /* Set style */
878         sp_desktop_apply_style_tool(desktop, repr, "tools.connector", false);
880         gchar *str = sp_svg_write_path(SP_CURVE_BPATH(c));
881         g_assert( str != NULL );
882         repr->setAttribute("d", str);
883         g_free(str);
885         /* Attach repr */
886         cc->newconn = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
887         cc->selection->set(repr);
888         Inkscape::GC::release(repr);
889         cc->newconn->transform = i2i_affine(desktop->currentRoot(), desktop->currentLayer());
890         cc->newconn->updateRepr();
892         bool connection = false;
893         sp_object_setAttribute(cc->newconn, "inkscape:connector-type",
894                 "polyline", false);
895         if (cc->sid)
896         {
897             sp_object_setAttribute(cc->newconn, "inkscape:connection-start",
898                     cc->sid, false);
899             connection = true;
900         }
902         if (cc->eid)
903         {
904             sp_object_setAttribute(cc->newconn, "inkscape:connection-end",
905                     cc->eid, false);
906             connection = true;
907         }
908         cc->newconn->updateRepr();
909         if (connection) {
910             // Adjust endpoints to shape edge.
911             sp_conn_adjust_path(SP_PATH(cc->newconn));
912         }
913         cc->newconn->updateRepr();
914     }
916     sp_curve_unref(c);
918     /* Flush pending updates */
919     sp_document_done(doc, SP_VERB_CONTEXT_CONNECTOR, _("Create connector"));
920     sp_document_ensure_up_to_date(doc);
924 static void
925 spcc_connector_finish_segment(SPConnectorContext *const cc, NR::Point const p)
927     if (!sp_curve_empty(cc->red_curve)) {
928         sp_curve_append_continuous(cc->green_curve, cc->red_curve, 0.0625);
930         cc->p[0] = cc->p[3];
931         cc->p[1] = cc->p[4];
932         cc->npoints = 2;
934         sp_curve_reset(cc->red_curve);
935     }
939 static void
940 spcc_connector_finish(SPConnectorContext *const cc)
942     SPDesktop *const desktop = cc->desktop;
943     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Finishing connector"));
945     sp_curve_reset(cc->red_curve);
946     spcc_concat_colors_and_flush(cc);
948     cc->npoints = 0;
950     if (cc->newConnRef) {
951         cc->newConnRef->removeFromGraph();
952         delete cc->newConnRef;
953         cc->newConnRef = NULL;
954     }
955     cc->state = SP_CONNECTOR_CONTEXT_IDLE;
959 static gboolean
960 cc_generic_knot_handler(SPCanvasItem *, GdkEvent *event, SPKnot *knot)
962     g_assert (knot != NULL);
963     
964     g_object_ref(knot);
966     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(
967             knot->desktop->event_context);
969     gboolean consumed = FALSE;
971     switch (event->type) {
972         case GDK_ENTER_NOTIFY:
973             sp_knot_set_flag(knot, SP_KNOT_MOUSEOVER, TRUE);
974             
975             cc->active_handle = knot;
977             if (knot->tip)
978             {
979                 knot->desktop->event_context->defaultMessageContext()->set(
980                         Inkscape::NORMAL_MESSAGE, knot->tip);
981             }
982             
983             consumed = TRUE;
984             break;
985         case GDK_LEAVE_NOTIFY:
986             sp_knot_set_flag(knot, SP_KNOT_MOUSEOVER, FALSE);
988             cc->active_handle = NULL;
989             
990             if (knot->tip) {
991                 knot->desktop->event_context->defaultMessageContext()->clear();
992             }
993             
994             consumed = TRUE;
995             break;
996         default:
997             break;
998     }
999     
1000     g_object_unref(knot);
1002     return consumed;
1006 static gboolean
1007 endpt_handler(SPKnot *knot, GdkEvent *event, SPConnectorContext *cc)
1009     g_assert( SP_IS_CONNECTOR_CONTEXT(cc) );
1011     gboolean consumed = FALSE;
1013     switch (event->type) {
1014         case GDK_BUTTON_PRESS:
1015             g_assert( (cc->active_handle == cc->endpt_handle[0]) ||
1016                       (cc->active_handle == cc->endpt_handle[1]) );
1017             if (cc->state == SP_CONNECTOR_CONTEXT_IDLE) {
1018                 cc->clickeditem = cc->active_conn;
1019                 cc->clickedhandle = cc->active_handle;
1020                 cc_clear_active_conn(cc);
1021                 cc->state = SP_CONNECTOR_CONTEXT_REROUTING;
1023                 // Disconnect from attached shape
1024                 unsigned ind = (cc->active_handle == cc->endpt_handle[0]) ? 0 : 1;
1025                 sp_conn_end_detach(cc->clickeditem, ind);
1027                 NR::Point origin;
1028                 if (cc->clickedhandle == cc->endpt_handle[0]) {
1029                     origin = cc->endpt_handle[1]->pos;
1030                 }
1031                 else {
1032                     origin = cc->endpt_handle[0]->pos;
1033                 }
1035                 // Show the red path for dragging.
1036                 cc->red_curve = sp_curve_copy(SP_PATH(cc->clickeditem)->curve);
1037                 NR::Matrix i2d = sp_item_i2d_affine(cc->clickeditem);
1038                 sp_curve_transform(cc->red_curve, i2d);
1039                 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath),
1040                         cc->red_curve);
1042                 cc->clickeditem->setHidden(true);
1044                 // The rest of the interaction rerouting the connector is
1045                 // handled by the context root handler.
1046                 consumed = TRUE;
1047             }
1048             break;
1049         default:
1050             break;
1051     }
1053     return consumed;
1057 static void cc_set_active_shape(SPConnectorContext *cc, SPItem *item)
1059     g_assert(item != NULL );
1061     cc->active_shape = item;
1063     // Remove existing active shape listeners
1064     if (cc->active_shape_repr) {
1065         sp_repr_remove_listener_by_data(cc->active_shape_repr, cc);
1066         Inkscape::GC::release(cc->active_shape_repr);
1068         sp_repr_remove_listener_by_data(cc->active_shape_layer_repr, cc);
1069         Inkscape::GC::release(cc->active_shape_layer_repr);
1070     }
1072     // Listen in case the active shape changes
1073     cc->active_shape_repr = SP_OBJECT_REPR(item);
1074     if (cc->active_shape_repr) {
1075         Inkscape::GC::anchor(cc->active_shape_repr);
1076         sp_repr_add_listener(cc->active_shape_repr, &shape_repr_events, cc);
1078         cc->active_shape_layer_repr = cc->active_shape_repr->parent();
1079         Inkscape::GC::anchor(cc->active_shape_layer_repr);
1080         sp_repr_add_listener(cc->active_shape_layer_repr, &layer_repr_events, cc);
1081     }
1084     // Set center connection point.
1085     if ( cc->connpthandle == NULL ) {
1086         SPKnot *knot = sp_knot_new(cc->desktop, 
1087                 _("<b>Connection point</b>: click or drag to create a new connector"));
1089         knot->setShape(SP_KNOT_SHAPE_SQUARE);
1090         knot->setSize(8);
1091         knot->setAnchor(GTK_ANCHOR_CENTER);
1092         knot->setFill(0xffffff00, 0xff0000ff, 0xff0000ff);
1093         sp_knot_update_ctrl(knot);
1095         // We don't want to use the standard knot handler,
1096         //since we don't want this knot to be draggable.
1097         g_signal_handler_disconnect(G_OBJECT(knot->item),
1098                 knot->_event_handler_id);
1099         knot->_event_handler_id = 0;
1101         gtk_signal_connect(GTK_OBJECT(knot->item), "event",
1102                 GTK_SIGNAL_FUNC(cc_generic_knot_handler), knot);
1104         cc->connpthandle = knot;
1105     }
1108     NR::Rect bbox = sp_item_bbox_desktop(cc->active_shape);
1109     NR::Point center = bbox.midpoint();
1110     sp_knot_set_position(cc->connpthandle, &center, 0);
1112     sp_knot_show(cc->connpthandle);
1117 static void
1118 cc_set_active_conn(SPConnectorContext *cc, SPItem *item)
1120     g_assert( SP_IS_PATH(item) );
1122     SPCurve *curve = SP_SHAPE(SP_PATH(item))->curve;
1123     NR::Matrix i2d = sp_item_i2d_affine(item);
1125     if (cc->active_conn == item)
1126     {
1127         // Just adjust handle positions.
1128         NR::Point startpt = sp_curve_first_point(curve) * i2d;
1129         sp_knot_set_position(cc->endpt_handle[0], &startpt, 0);
1131         NR::Point endpt = sp_curve_last_point(curve) * i2d;
1132         sp_knot_set_position(cc->endpt_handle[1], &endpt, 0);
1134         return;
1135     }
1137     cc->active_conn = item;
1139     // Remove existing active conn listeners
1140     if (cc->active_conn_repr) {
1141         sp_repr_remove_listener_by_data(cc->active_conn_repr, cc);
1142         Inkscape::GC::release(cc->active_conn_repr);
1143         cc->active_conn_repr = NULL;
1144     }
1146     // Listen in case the active conn changes
1147     cc->active_conn_repr = SP_OBJECT_REPR(item);
1148     if (cc->active_conn_repr) {
1149         Inkscape::GC::anchor(cc->active_conn_repr);
1150         sp_repr_add_listener(cc->active_conn_repr, &shape_repr_events, cc);
1151     }
1153     for (int i = 0; i < 2; ++i) {
1155         // Create the handle if it doesn't exist
1156         if ( cc->endpt_handle[i] == NULL ) {
1157             SPKnot *knot = sp_knot_new(cc->desktop, 
1158                     _("<b>Connector endpoint</b>: drag to reroute or connect to new shapes"));
1160             knot->setShape(SP_KNOT_SHAPE_SQUARE);
1161             knot->setSize(7);
1162             knot->setAnchor(GTK_ANCHOR_CENTER);
1163             knot->setFill(0xffffff00, 0xff0000ff, 0xff0000ff);
1164             knot->setStroke(0x000000ff, 0x000000ff, 0x000000ff);
1165             sp_knot_update_ctrl(knot);
1167             // We don't want to use the standard knot handler,
1168             //since we don't want this knot to be draggable.
1169             g_signal_handler_disconnect(G_OBJECT(knot->item),
1170                     knot->_event_handler_id);
1171             knot->_event_handler_id = 0;
1173             gtk_signal_connect(GTK_OBJECT(knot->item), "event",
1174                     GTK_SIGNAL_FUNC(cc_generic_knot_handler), knot);
1176             cc->endpt_handle[i] = knot;
1177         }
1179         // Remove any existing handlers
1180         if (cc->endpt_handler_id[i]) {
1181             g_signal_handlers_disconnect_by_func(
1182                     G_OBJECT(cc->endpt_handle[i]->item),
1183                     (void*)G_CALLBACK(endpt_handler), (gpointer) cc );
1184             cc->endpt_handler_id[i] = 0;
1185         }
1187         // Setup handlers for connector endpoints, this is
1188         // is as 'after' so that cc_generic_knot_handler is
1189         // triggered first for any endpoint.
1190         cc->endpt_handler_id[i] = g_signal_connect_after(
1191                 G_OBJECT(cc->endpt_handle[i]->item), "event",
1192                 G_CALLBACK(endpt_handler), cc);
1193     }
1195     NR::Point startpt = sp_curve_first_point(curve) * i2d;
1196     sp_knot_set_position(cc->endpt_handle[0], &startpt, 0);
1198     NR::Point endpt = sp_curve_last_point(curve) * i2d;
1199     sp_knot_set_position(cc->endpt_handle[1], &endpt, 0);
1201     sp_knot_show(cc->endpt_handle[0]);
1202     sp_knot_show(cc->endpt_handle[1]);
1206 static bool cc_item_is_shape(SPItem *item)
1208     if (SP_IS_PATH(item)) {
1209         SPCurve *curve = (SP_SHAPE(item))->curve;
1210         if ( curve && !(curve->closed) ) {
1211             // Open paths are connectors.
1212             return false;
1213         }
1214     }
1215     else if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
1216         if (prefs_get_int_attribute("tools.connector", "ignoretext", 1) == 1) {
1217             // Don't count text as a shape we can connect connector to.
1218             return false;
1219         }
1220     }
1221     return true;
1225 bool cc_item_is_connector(SPItem *item)
1227     if (SP_IS_PATH(item)) {
1228         if (SP_PATH(item)->connEndPair.isAutoRoutingConn()) {
1229             g_assert( !(SP_SHAPE(item)->curve->closed) );
1230             return true;
1231         }
1232     }
1233     return false;
1237 void cc_selection_set_avoid(bool const set_avoid)
1239     SPDesktop *desktop = inkscape_active_desktop();
1240     if (desktop == NULL) {
1241         return;
1242     }
1244     SPDocument *document = sp_desktop_document(desktop);
1246     Inkscape::Selection *selection = sp_desktop_selection(desktop);
1248     GSList *l = (GSList *) selection->itemList();
1250     int changes = 0;
1252     while (l) {
1253         SPItem *item = (SPItem *) l->data;
1255         char const *value = (set_avoid) ? "true" : NULL;
1257         if (cc_item_is_shape(item)) {
1258             sp_object_setAttribute(item, "inkscape:connector-avoid",
1259                     value, false);
1260             item->avoidRef->handleSettingChange();
1261             changes++;
1262         }
1264         l = l->next;
1265     }
1267     if (changes == 0) {
1268         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE,
1269                 _("Select <b>at least one non-connector object</b>."));
1270         return;
1271     }
1273     char *event_desc = (set_avoid) ?
1274             _("Make connectors avoid selected objects") :
1275             _("Make connectors ignore selected objects");
1276     sp_document_done(document, SP_VERB_CONTEXT_CONNECTOR, event_desc);
1280 static void
1281 cc_selection_changed(Inkscape::Selection *selection, gpointer data)
1283     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(data);
1284     //SPEventContext *ec = SP_EVENT_CONTEXT(cc);
1286     SPItem *item = selection->singleItem();
1288     if (cc->active_conn == item)
1289     {
1290         // Nothing to change.
1291         return;
1292     }
1293     if (item == NULL)
1294     {
1295         cc_clear_active_conn(cc);
1296         return;
1297     }
1299     if (cc_item_is_connector(item)) {
1300         cc_set_active_conn(cc, item);
1301     }
1305 static void
1306 shape_event_attr_deleted(Inkscape::XML::Node *repr, Inkscape::XML::Node *child,
1307         Inkscape::XML::Node *ref, gpointer data)
1309     g_assert(data);
1310     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(data);
1312     if (child == cc->active_shape_repr) {
1313         // The active shape has been deleted.  Clear active shape.
1314         cc_clear_active_shape(cc);
1315     }
1319 static void
1320 shape_event_attr_changed(Inkscape::XML::Node *repr, gchar const *name,
1321                             gchar const *old_value, gchar const *new_value,
1322                             bool is_interactive, gpointer data)
1324     g_assert(data);
1325     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(data);
1327     // Look for changes than result in onscreen movement.
1328     if (!strcmp(name, "d") || !strcmp(name, "x") || !strcmp(name, "y") ||
1329             !strcmp(name, "width") || !strcmp(name, "height") ||
1330             !strcmp(name, "transform"))
1331     {
1332         if (repr == cc->active_shape_repr) {
1333             // Active shape has moved. Clear active shape.
1334             cc_clear_active_shape(cc);
1335         }
1336         else if (repr == cc->active_conn_repr) {
1337             // The active conn has been moved.
1338             // Set it again, which just sets new handle positions.
1339             cc_set_active_conn(cc, cc->active_conn);
1340         }
1341     }
1345 /*
1346   Local Variables:
1347   mode:c++
1348   c-file-style:"stroustrup"
1349   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1350   indent-tabs-mode:nil
1351   fill-column:99
1352   End:
1353 */
1354 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :