Code

more correct double opacity fix: do not multiply by opacity if item->render_opacity...
[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);
872     if ( c && !sp_curve_empty(c) ) {
873         /* We actually have something to write */
875         Inkscape::XML::Node *repr = sp_repr_new("svg:path");
876         /* Set style */
877         sp_desktop_apply_style_tool(desktop, repr, "tools.connector", false);
879         gchar *str = sp_svg_write_path(SP_CURVE_BPATH(c));
880         g_assert( str != NULL );
881         repr->setAttribute("d", str);
882         g_free(str);
884         /* Attach repr */
885         cc->newconn = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
886         cc->selection->set(repr);
887         Inkscape::GC::release(repr);
888         cc->newconn->transform = i2i_affine(desktop->currentRoot(), desktop->currentLayer());
889         cc->newconn->updateRepr();
891         bool connection = false;
892         sp_object_setAttribute(cc->newconn, "inkscape:connector-type",
893                 "polyline", false);
894         if (cc->sid)
895         {
896             sp_object_setAttribute(cc->newconn, "inkscape:connection-start",
897                     cc->sid, false);
898             connection = true;
899         }
901         if (cc->eid)
902         {
903             sp_object_setAttribute(cc->newconn, "inkscape:connection-end",
904                     cc->eid, false);
905             connection = true;
906         }
907         cc->newconn->updateRepr();
908         if (connection) {
909             // Adjust endpoints to shape edge.
910             sp_conn_adjust_path(SP_PATH(cc->newconn));
911         }
912         cc->newconn->updateRepr();
913     }
915     sp_curve_unref(c);
917     /* Flush pending updates */
918     sp_document_done(doc, SP_VERB_CONTEXT_CONNECTOR, _("Create connector"));
919     sp_document_ensure_up_to_date(doc);
923 static void
924 spcc_connector_finish_segment(SPConnectorContext *const cc, NR::Point const p)
926     if (!sp_curve_empty(cc->red_curve)) {
927         sp_curve_append_continuous(cc->green_curve, cc->red_curve, 0.0625);
929         cc->p[0] = cc->p[3];
930         cc->p[1] = cc->p[4];
931         cc->npoints = 2;
933         sp_curve_reset(cc->red_curve);
934     }
938 static void
939 spcc_connector_finish(SPConnectorContext *const cc)
941     SPDesktop *const desktop = cc->desktop;
942     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Finishing connector"));
944     sp_curve_reset(cc->red_curve);
945     spcc_concat_colors_and_flush(cc);
947     cc->npoints = 0;
949     if (cc->newConnRef) {
950         cc->newConnRef->removeFromGraph();
951         delete cc->newConnRef;
952         cc->newConnRef = NULL;
953     }
954     cc->state = SP_CONNECTOR_CONTEXT_IDLE;
958 static gboolean
959 cc_generic_knot_handler(SPCanvasItem *, GdkEvent *event, SPKnot *knot)
961     g_assert (knot != NULL);
962     
963     g_object_ref(knot);
965     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(
966             knot->desktop->event_context);
968     gboolean consumed = FALSE;
970     switch (event->type) {
971         case GDK_ENTER_NOTIFY:
972             sp_knot_set_flag(knot, SP_KNOT_MOUSEOVER, TRUE);
973             
974             cc->active_handle = knot;
976             if (knot->tip)
977             {
978                 knot->desktop->event_context->defaultMessageContext()->set(
979                         Inkscape::NORMAL_MESSAGE, knot->tip);
980             }
981             
982             consumed = TRUE;
983             break;
984         case GDK_LEAVE_NOTIFY:
985             sp_knot_set_flag(knot, SP_KNOT_MOUSEOVER, FALSE);
987             cc->active_handle = NULL;
988             
989             if (knot->tip) {
990                 knot->desktop->event_context->defaultMessageContext()->clear();
991             }
992             
993             consumed = TRUE;
994             break;
995         default:
996             break;
997     }
998     
999     g_object_unref(knot);
1001     return consumed;
1005 static gboolean
1006 endpt_handler(SPKnot *knot, GdkEvent *event, SPConnectorContext *cc)
1008     g_assert( SP_IS_CONNECTOR_CONTEXT(cc) );
1010     gboolean consumed = FALSE;
1012     switch (event->type) {
1013         case GDK_BUTTON_PRESS:
1014             g_assert( (cc->active_handle == cc->endpt_handle[0]) ||
1015                       (cc->active_handle == cc->endpt_handle[1]) );
1016             if (cc->state == SP_CONNECTOR_CONTEXT_IDLE) {
1017                 cc->clickeditem = cc->active_conn;
1018                 cc->clickedhandle = cc->active_handle;
1019                 cc_clear_active_conn(cc);
1020                 cc->state = SP_CONNECTOR_CONTEXT_REROUTING;
1022                 // Disconnect from attached shape
1023                 unsigned ind = (cc->active_handle == cc->endpt_handle[0]) ? 0 : 1;
1024                 sp_conn_end_detach(cc->clickeditem, ind);
1026                 NR::Point origin;
1027                 if (cc->clickedhandle == cc->endpt_handle[0]) {
1028                     origin = cc->endpt_handle[1]->pos;
1029                 }
1030                 else {
1031                     origin = cc->endpt_handle[0]->pos;
1032                 }
1034                 // Show the red path for dragging.
1035                 cc->red_curve = sp_curve_copy(SP_PATH(cc->clickeditem)->curve);
1036                 NR::Matrix i2d = sp_item_i2d_affine(cc->clickeditem);
1037                 sp_curve_transform(cc->red_curve, i2d);
1038                 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath),
1039                         cc->red_curve);
1041                 cc->clickeditem->setHidden(true);
1043                 // The rest of the interaction rerouting the connector is
1044                 // handled by the context root handler.
1045                 consumed = TRUE;
1046             }
1047             break;
1048         default:
1049             break;
1050     }
1052     return consumed;
1056 static void cc_set_active_shape(SPConnectorContext *cc, SPItem *item)
1058     g_assert(item != NULL );
1060     cc->active_shape = item;
1062     // Remove existing active shape listeners
1063     if (cc->active_shape_repr) {
1064         sp_repr_remove_listener_by_data(cc->active_shape_repr, cc);
1065         Inkscape::GC::release(cc->active_shape_repr);
1067         sp_repr_remove_listener_by_data(cc->active_shape_layer_repr, cc);
1068         Inkscape::GC::release(cc->active_shape_layer_repr);
1069     }
1071     // Listen in case the active shape changes
1072     cc->active_shape_repr = SP_OBJECT_REPR(item);
1073     if (cc->active_shape_repr) {
1074         Inkscape::GC::anchor(cc->active_shape_repr);
1075         sp_repr_add_listener(cc->active_shape_repr, &shape_repr_events, cc);
1077         cc->active_shape_layer_repr = cc->active_shape_repr->parent();
1078         Inkscape::GC::anchor(cc->active_shape_layer_repr);
1079         sp_repr_add_listener(cc->active_shape_layer_repr, &layer_repr_events, cc);
1080     }
1083     // Set center connection point.
1084     if ( cc->connpthandle == NULL ) {
1085         SPKnot *knot = sp_knot_new(cc->desktop, 
1086                 _("<b>Connection point</b>: click or drag to create a new connector"));
1088         knot->setShape(SP_KNOT_SHAPE_SQUARE);
1089         knot->setSize(8);
1090         knot->setAnchor(GTK_ANCHOR_CENTER);
1091         knot->setFill(0xffffff00, 0xff0000ff, 0xff0000ff);
1092         sp_knot_update_ctrl(knot);
1094         // We don't want to use the standard knot handler,
1095         //since we don't want this knot to be draggable.
1096         g_signal_handler_disconnect(G_OBJECT(knot->item),
1097                 knot->_event_handler_id);
1098         knot->_event_handler_id = 0;
1100         gtk_signal_connect(GTK_OBJECT(knot->item), "event",
1101                 GTK_SIGNAL_FUNC(cc_generic_knot_handler), knot);
1103         cc->connpthandle = knot;
1104     }
1107     NR::Rect bbox = sp_item_bbox_desktop(cc->active_shape);
1108     NR::Point center = bbox.midpoint();
1109     sp_knot_set_position(cc->connpthandle, &center, 0);
1111     sp_knot_show(cc->connpthandle);
1116 static void
1117 cc_set_active_conn(SPConnectorContext *cc, SPItem *item)
1119     g_assert( SP_IS_PATH(item) );
1121     SPCurve *curve = SP_SHAPE(SP_PATH(item))->curve;
1122     NR::Matrix i2d = sp_item_i2d_affine(item);
1124     if (cc->active_conn == item)
1125     {
1126         // Just adjust handle positions.
1127         NR::Point startpt = sp_curve_first_point(curve) * i2d;
1128         sp_knot_set_position(cc->endpt_handle[0], &startpt, 0);
1130         NR::Point endpt = sp_curve_last_point(curve) * i2d;
1131         sp_knot_set_position(cc->endpt_handle[1], &endpt, 0);
1133         return;
1134     }
1136     cc->active_conn = item;
1138     // Remove existing active conn listeners
1139     if (cc->active_conn_repr) {
1140         sp_repr_remove_listener_by_data(cc->active_conn_repr, cc);
1141         Inkscape::GC::release(cc->active_conn_repr);
1142         cc->active_conn_repr = NULL;
1143     }
1145     // Listen in case the active conn changes
1146     cc->active_conn_repr = SP_OBJECT_REPR(item);
1147     if (cc->active_conn_repr) {
1148         Inkscape::GC::anchor(cc->active_conn_repr);
1149         sp_repr_add_listener(cc->active_conn_repr, &shape_repr_events, cc);
1150     }
1152     for (int i = 0; i < 2; ++i) {
1154         // Create the handle if it doesn't exist
1155         if ( cc->endpt_handle[i] == NULL ) {
1156             SPKnot *knot = sp_knot_new(cc->desktop, 
1157                     _("<b>Connector endpoint</b>: drag to reroute or connect to new shapes"));
1159             knot->setShape(SP_KNOT_SHAPE_SQUARE);
1160             knot->setSize(7);
1161             knot->setAnchor(GTK_ANCHOR_CENTER);
1162             knot->setFill(0xffffff00, 0xff0000ff, 0xff0000ff);
1163             knot->setStroke(0x000000ff, 0x000000ff, 0x000000ff);
1164             sp_knot_update_ctrl(knot);
1166             // We don't want to use the standard knot handler,
1167             //since we don't want this knot to be draggable.
1168             g_signal_handler_disconnect(G_OBJECT(knot->item),
1169                     knot->_event_handler_id);
1170             knot->_event_handler_id = 0;
1172             gtk_signal_connect(GTK_OBJECT(knot->item), "event",
1173                     GTK_SIGNAL_FUNC(cc_generic_knot_handler), knot);
1175             cc->endpt_handle[i] = knot;
1176         }
1178         // Remove any existing handlers
1179         if (cc->endpt_handler_id[i]) {
1180             g_signal_handlers_disconnect_by_func(
1181                     G_OBJECT(cc->endpt_handle[i]->item),
1182                     (void*)G_CALLBACK(endpt_handler), (gpointer) cc );
1183             cc->endpt_handler_id[i] = 0;
1184         }
1186         // Setup handlers for connector endpoints, this is
1187         // is as 'after' so that cc_generic_knot_handler is
1188         // triggered first for any endpoint.
1189         cc->endpt_handler_id[i] = g_signal_connect_after(
1190                 G_OBJECT(cc->endpt_handle[i]->item), "event",
1191                 G_CALLBACK(endpt_handler), cc);
1192     }
1194     NR::Point startpt = sp_curve_first_point(curve) * i2d;
1195     sp_knot_set_position(cc->endpt_handle[0], &startpt, 0);
1197     NR::Point endpt = sp_curve_last_point(curve) * i2d;
1198     sp_knot_set_position(cc->endpt_handle[1], &endpt, 0);
1200     sp_knot_show(cc->endpt_handle[0]);
1201     sp_knot_show(cc->endpt_handle[1]);
1205 static bool cc_item_is_shape(SPItem *item)
1207     if (SP_IS_PATH(item)) {
1208         SPCurve *curve = (SP_SHAPE(item))->curve;
1209         if ( curve && !(curve->closed) ) {
1210             // Open paths are connectors.
1211             return false;
1212         }
1213     }
1214     else if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
1215         if (prefs_get_int_attribute("tools.connector", "ignoretext", 1) == 1) {
1216             // Don't count text as a shape we can connect connector to.
1217             return false;
1218         }
1219     }
1220     return true;
1224 bool cc_item_is_connector(SPItem *item)
1226     if (SP_IS_PATH(item)) {
1227         if (SP_PATH(item)->connEndPair.isAutoRoutingConn()) {
1228             g_assert( !(SP_SHAPE(item)->curve->closed) );
1229             return true;
1230         }
1231     }
1232     return false;
1236 void cc_selection_set_avoid(bool const set_avoid)
1238     SPDesktop *desktop = inkscape_active_desktop();
1239     if (desktop == NULL) {
1240         return;
1241     }
1243     SPDocument *document = sp_desktop_document(desktop);
1245     Inkscape::Selection *selection = sp_desktop_selection(desktop);
1247     GSList *l = (GSList *) selection->itemList();
1249     int changes = 0;
1251     while (l) {
1252         SPItem *item = (SPItem *) l->data;
1254         char const *value = (set_avoid) ? "true" : NULL;
1256         if (cc_item_is_shape(item)) {
1257             sp_object_setAttribute(item, "inkscape:connector-avoid",
1258                     value, false);
1259             item->avoidRef->handleSettingChange();
1260             changes++;
1261         }
1263         l = l->next;
1264     }
1266     if (changes == 0) {
1267         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE,
1268                 _("Select <b>at least one non-connector object</b>."));
1269         return;
1270     }
1272     char *event_desc = (set_avoid) ?
1273             _("Make connectors avoid selected objects") :
1274             _("Make connectors ignore selected objects");
1275     sp_document_done(document, SP_VERB_CONTEXT_CONNECTOR, event_desc);
1279 static void
1280 cc_selection_changed(Inkscape::Selection *selection, gpointer data)
1282     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(data);
1283     //SPEventContext *ec = SP_EVENT_CONTEXT(cc);
1285     SPItem *item = selection->singleItem();
1287     if (cc->active_conn == item)
1288     {
1289         // Nothing to change.
1290         return;
1291     }
1292     if (item == NULL)
1293     {
1294         cc_clear_active_conn(cc);
1295         return;
1296     }
1298     if (cc_item_is_connector(item)) {
1299         cc_set_active_conn(cc, item);
1300     }
1304 static void
1305 shape_event_attr_deleted(Inkscape::XML::Node *repr, Inkscape::XML::Node *child,
1306         Inkscape::XML::Node *ref, gpointer data)
1308     g_assert(data);
1309     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(data);
1311     if (child == cc->active_shape_repr) {
1312         // The active shape has been deleted.  Clear active shape.
1313         cc_clear_active_shape(cc);
1314     }
1318 static void
1319 shape_event_attr_changed(Inkscape::XML::Node *repr, gchar const *name,
1320                             gchar const *old_value, gchar const *new_value,
1321                             bool is_interactive, gpointer data)
1323     g_assert(data);
1324     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(data);
1326     // Look for changes than result in onscreen movement.
1327     if (!strcmp(name, "d") || !strcmp(name, "x") || !strcmp(name, "y") ||
1328             !strcmp(name, "width") || !strcmp(name, "height") ||
1329             !strcmp(name, "transform"))
1330     {
1331         if (repr == cc->active_shape_repr) {
1332             // Active shape has moved. Clear active shape.
1333             cc_clear_active_shape(cc);
1334         }
1335         else if (repr == cc->active_conn_repr) {
1336             // The active conn has been moved.
1337             // Set it again, which just sets new handle positions.
1338             cc_set_active_conn(cc, cc->active_conn);
1339         }
1340     }
1344 /*
1345   Local Variables:
1346   mode:c++
1347   c-file-style:"stroustrup"
1348   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1349   indent-tabs-mode:nil
1350   fill-column:99
1351   End:
1352 */
1353 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :