Code

fixed broken page unit changing in Document Properties
[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 coonvex 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  Create an interface for setting markers (arrow heads).
21  *  o  Better distinguish between paths and connectors to prevent problems
22  *     in the node tool and paths accidently being turned into connectors
23  *     in the connector tool.  Perhaps have a way to convert between.
24  *  o  Only call libavoid's updateEndPoint as required.  Currently we do it
25  *     for both endpoints, even if only one is moving.
26  *  o  Cleanup to remove unecessary borrowed DrawContext code.
27  *  o  Allow user-placeable connection points.
28  *  o  Deal sanely with connectors with both endpoints attached to the
29  *     same connection point, and drawing of connectors attaching
30  *     overlaping shapes (currently tries to adjust connector to be
31  *     outside both bounding boxes).
32  *  o  Fix many special cases related to connectors updating,
33  *     e.g., copying a couple of shapes and a connector that are
34  *           attached to each other.
35  *     e.g., detach connector when it is moved or transformed in
36  *           one of the other contexts.
37  *  o  Cope with shapes whose ids change when they have attached
38  *     connectors.
39  *  o  gobble_motion_events(GDK_BUTTON1_MASK)?;
40  *
41  */
43 #include <gdk/gdkkeysyms.h>
45 #include "connector-context.h"
46 #include "pixmaps/cursor-connector.xpm"
47 #include "xml/node-event-vector.h"
48 #include "xml/repr.h"
49 #include "svg/svg.h"
50 #include "desktop.h"
51 #include "desktop-style.h"
52 #include "desktop-affine.h"
53 #include "desktop-handles.h"
54 #include "document.h"
55 #include "message-context.h"
56 #include "message-stack.h"
57 #include "selection.h"
58 #include "inkscape.h"
59 #include "prefs-utils.h"
60 #include "sp-path.h"
61 #include "display/canvas-bpath.h"
62 #include "display/sodipodi-ctrl.h"
63 #include <glibmm/i18n.h>
64 #include "snap.h"
65 #include "knot.h"
66 #include "sp-conn-end.h"
67 #include "conn-avoid-ref.h"
68 #include "libavoid/vertices.h"
69 #include "context-fns.h"
70 #include "sp-namedview.h"
72 static void sp_connector_context_class_init(SPConnectorContextClass *klass);
73 static void sp_connector_context_init(SPConnectorContext *conn_context);
74 static void sp_connector_context_dispose(GObject *object);
76 static void sp_connector_context_setup(SPEventContext *ec);
77 static void sp_connector_context_finish(SPEventContext *ec);
78 static gint sp_connector_context_root_handler(SPEventContext *ec, GdkEvent *event);
79 static gint sp_connector_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
81 // Stuff borrowed from DrawContext
82 static void spcc_connector_set_initial_point(SPConnectorContext *cc, NR::Point const p);
83 static void spcc_connector_set_subsequent_point(SPConnectorContext *cc, NR::Point const p);
84 static void spcc_connector_finish_segment(SPConnectorContext *cc, NR::Point p);
85 static void spcc_reset_colors(SPConnectorContext *cc);
86 static void spcc_connector_finish(SPConnectorContext *cc);
87 static void spcc_concat_colors_and_flush(SPConnectorContext *cc);
88 static void spcc_flush_white(SPConnectorContext *cc, SPCurve *gc);
90 // Context event handlers
91 static gint connector_handle_button_press(SPConnectorContext *const cc, GdkEventButton const &bevent);
92 static gint connector_handle_motion_notify(SPConnectorContext *const cc, GdkEventMotion const &mevent);
93 static gint connector_handle_button_release(SPConnectorContext *const cc, GdkEventButton const &revent);
94 static gint connector_handle_key_press(SPConnectorContext *const cc, guint const keyval);
96 static void cc_set_active_shape(SPConnectorContext *cc, SPItem *item);
97 static void cc_clear_active_shape(SPConnectorContext *cc);
98 static void cc_set_active_conn(SPConnectorContext *cc, SPItem *item);
99 static void cc_clear_active_conn(SPConnectorContext *cc);
100 static gchar *conn_pt_handle_test(SPConnectorContext *cc, NR::Point& w);
101 static bool cc_item_is_shape(SPItem *item);
102 static void cc_selection_changed(Inkscape::Selection *selection, gpointer data);
104 static void shape_event_attr_deleted(Inkscape::XML::Node *repr,
105         Inkscape::XML::Node *child, Inkscape::XML::Node *ref, gpointer data);
106 static void shape_event_attr_changed(Inkscape::XML::Node *repr, gchar const *name,
107         gchar const *old_value, gchar const *new_value, bool is_interactive,
108         gpointer data);
111 static NR::Point connector_drag_origin_w(0, 0);
112 static bool connector_within_tolerance = false;
113 static SPEventContextClass *parent_class;
116 static Inkscape::XML::NodeEventVector shape_repr_events = {
117     NULL, /* child_added */
118     NULL, /* child_added */
119     shape_event_attr_changed,
120     NULL, /* content_changed */
121     NULL  /* order_changed */
122 };
124 static Inkscape::XML::NodeEventVector layer_repr_events = {
125     NULL, /* child_added */
126     shape_event_attr_deleted,
127     NULL, /* child_added */
128     NULL, /* content_changed */
129     NULL  /* order_changed */
130 };
133 GType
134 sp_connector_context_get_type(void)
136     static GType type = 0;
137     if (!type) {
138         GTypeInfo info = {
139             sizeof(SPConnectorContextClass),
140             NULL, NULL,
141             (GClassInitFunc) sp_connector_context_class_init,
142             NULL, NULL,
143             sizeof(SPConnectorContext),
144             4,
145             (GInstanceInitFunc) sp_connector_context_init,
146             NULL,   /* value_table */
147         };
148         type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPConnectorContext", &info, (GTypeFlags)0);
149     }
150     return type;
153 static void
154 sp_connector_context_class_init(SPConnectorContextClass *klass)
156     GObjectClass *object_class;
157     SPEventContextClass *event_context_class;
159     object_class = (GObjectClass *) klass;
160     event_context_class = (SPEventContextClass *) klass;
162     parent_class = (SPEventContextClass*)g_type_class_peek_parent(klass);
164     object_class->dispose = sp_connector_context_dispose;
166     event_context_class->setup = sp_connector_context_setup;
167     event_context_class->finish = sp_connector_context_finish;
168     event_context_class->root_handler = sp_connector_context_root_handler;
169     event_context_class->item_handler = sp_connector_context_item_handler;
173 static void
174 sp_connector_context_init(SPConnectorContext *cc)
176     SPEventContext *ec = SP_EVENT_CONTEXT(cc);
178     ec->cursor_shape = cursor_connector_xpm;
179     ec->hot_x = 1;
180     ec->hot_y = 1;
181     ec->xp = 0;
182     ec->yp = 0;
184     cc->red_color = 0xff00007f;
186     cc->newconn = NULL;
187     cc->newConnRef = NULL;
189     cc->sel_changed_connection = sigc::connection();
191     cc->active_shape = NULL;
192     cc->active_shape_repr = NULL;
193     cc->active_shape_layer_repr = NULL;
195     cc->active_conn = NULL;
196     cc->active_conn_repr = NULL;
198     cc->active_handle = NULL;
200     cc->clickeditem = NULL;
201     cc->clickedhandle = NULL;
203     cc->connpthandle = NULL;
204     for (int i = 0; i < 2; ++i) {
205         cc->endpt_handle[i] = NULL;
206         cc->endpt_handler_id[i] = 0;
207     }
208     cc->sid = NULL;
209     cc->eid = NULL;
210     cc->npoints = 0;
211     cc->state = SP_CONNECTOR_CONTEXT_IDLE;
215 static void
216 sp_connector_context_dispose(GObject *object)
218     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(object);
220     cc->sel_changed_connection.disconnect();
222     if (cc->connpthandle) {
223         g_object_unref(cc->connpthandle);
224         cc->connpthandle = NULL;
225     }
226     for (int i = 0; i < 2; ++i) {
227         if (cc->endpt_handle[1]) {
228             g_object_unref(cc->endpt_handle[i]);
229             cc->endpt_handle[i] = NULL;
230         }
231     }
232     if (cc->sid) {
233         g_free(cc->sid);
234         cc->sid = NULL;
235     }
236     if (cc->eid) {
237         g_free(cc->eid);
238         cc->eid = NULL;
239     }
240     g_assert( cc->newConnRef == NULL );
242     G_OBJECT_CLASS(parent_class)->dispose(object);
246 static void
247 sp_connector_context_setup(SPEventContext *ec)
249     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(ec);
250     SPDesktop *dt = ec->desktop;
252     if (((SPEventContextClass *) parent_class)->setup) {
253         ((SPEventContextClass *) parent_class)->setup(ec);
254     }
256     cc->selection = sp_desktop_selection(dt);
258     cc->sel_changed_connection.disconnect();
259     cc->sel_changed_connection = cc->selection->connectChanged(
260             sigc::bind(sigc::ptr_fun(&cc_selection_changed),
261             (gpointer) cc));
263     /* Create red bpath */
264     cc->red_bpath = sp_canvas_bpath_new(sp_desktop_sketch(ec->desktop), NULL);
265     sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cc->red_bpath), cc->red_color,
266             1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
267     sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(cc->red_bpath), 0x00000000,
268             SP_WIND_RULE_NONZERO);
269     /* Create red curve */
270     cc->red_curve = sp_curve_new_sized(4);
272     /* Create green curve */
273     cc->green_curve = sp_curve_new_sized(64);
275     // Notice the initial selection.
276     cc_selection_changed(cc->selection, (gpointer) cc);
278     if (prefs_get_int_attribute("tools.connector", "selcue", 0) != 0) {
279         ec->enableSelectionCue();
280     }
282     // Make sure we see all enter events for canvas items,
283     // even if a mouse button is depressed.
284     dt->canvas->gen_all_enter_events = true;
288 static void
289 sp_connector_context_finish(SPEventContext *ec)
291     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(ec);
293     spcc_connector_finish(cc);
295     if (((SPEventContextClass *) parent_class)->finish) {
296         ((SPEventContextClass *) parent_class)->finish(ec);
297     }
299     if (cc->selection) {
300         cc->selection = NULL;
301     }
302     cc_clear_active_shape(cc);
303     cc_clear_active_conn(cc);
305     // Restore the default event generating behaviour.
306     SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(ec);
307     desktop->canvas->gen_all_enter_events = false;
311 //-----------------------------------------------------------------------------
314 static void
315 cc_clear_active_shape(SPConnectorContext *cc)
317     if (cc->active_shape == NULL) {
318         return;
319     }
320     g_assert( cc->active_shape_repr );
321     g_assert( cc->active_shape_layer_repr );
323     cc->active_shape = NULL;
325     if (cc->active_shape_repr) {
326         sp_repr_remove_listener_by_data(cc->active_shape_repr, cc);
327         Inkscape::GC::release(cc->active_shape_repr);
328         cc->active_shape_repr = NULL;
330         sp_repr_remove_listener_by_data(cc->active_shape_layer_repr, cc);
331         Inkscape::GC::release(cc->active_shape_layer_repr);
332         cc->active_shape_layer_repr = NULL;
333     }
335     // Hide the center connection point if it exists.
336     if (cc->connpthandle) {
337         sp_knot_hide(cc->connpthandle);
338     }
342 static void
343 cc_clear_active_conn(SPConnectorContext *cc)
345     if (cc->active_conn == NULL) {
346         return;
347     }
348     g_assert( cc->active_conn_repr );
350     cc->active_conn = NULL;
352     if (cc->active_conn_repr) {
353         sp_repr_remove_listener_by_data(cc->active_conn_repr, cc);
354         Inkscape::GC::release(cc->active_conn_repr);
355         cc->active_conn_repr = NULL;
356     }
358     // Hide the endpoint handles.
359     for (int i = 0; i < 2; ++i) {
360         if (cc->endpt_handle[i]) {
361             sp_knot_hide(cc->endpt_handle[i]);
362         }
363     }
367 static gchar *
368 conn_pt_handle_test(SPConnectorContext *cc, NR::Point& p)
370     // TODO: this will need to change when there are more connection
371     //       points available for each shape.
373     SPKnot *centerpt = cc->connpthandle;
374     if (cc->active_handle && (cc->active_handle == centerpt))
375     {
376         p = centerpt->pos;
377         return g_strdup_printf("#%s", SP_OBJECT_ID(cc->active_shape));
378     }
379     return NULL;
384 static gint
385 sp_connector_context_item_handler(SPEventContext *ec, SPItem *item, GdkEvent *event)
387     gint ret = FALSE;
389     SPDesktop *desktop = ec->desktop;
391     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(ec);
393     NR::Point p(event->button.x, event->button.y);
395     switch (event->type) {
396         case GDK_BUTTON_RELEASE:
397             if (event->button.button == 1) {
398                 if ((cc->state == SP_CONNECTOR_CONTEXT_DRAGGING) &&
399                         (connector_within_tolerance))
400                 {
401                     spcc_reset_colors(cc);
402                     cc->state = SP_CONNECTOR_CONTEXT_IDLE;
403                 }
404                 if (cc->state != SP_CONNECTOR_CONTEXT_IDLE) {
405                     // Doing simething else like rerouting.
406                     break;
407                 }
408                 // find out clicked item, disregarding groups, honoring Alt
409                 SPItem *item_ungrouped = sp_event_context_find_item(desktop,
410                         p, event->button.state & GDK_MOD1_MASK, TRUE);
412                 if (event->button.state & GDK_SHIFT_MASK) {
413                     cc->selection->toggle(item_ungrouped);
414                 } else {
415                     cc->selection->set(item_ungrouped);
416                 }
417                 ret = TRUE;
418             }
419             break;
420         case GDK_ENTER_NOTIFY:
421         {
422             if (cc_item_is_shape(item)) {
423                 // This is a shape, so show connection point(s).
424                 if (!(cc->active_shape) ||
425                         // Don't show handle for another handle.
426                         (item != ((SPItem *) cc->connpthandle))) {
427                     cc_set_active_shape(cc, item);
428                 }
429             }
430             ret = TRUE;
431             break;
432         }
433         default:
434             break;
435     }
437     return ret;
441 gint
442 sp_connector_context_root_handler(SPEventContext *ec, GdkEvent *event)
444     SPConnectorContext *const cc = SP_CONNECTOR_CONTEXT(ec);
446     gint ret = FALSE;
448     switch (event->type) {
449         case GDK_BUTTON_PRESS:
450             ret = connector_handle_button_press(cc, event->button);
451             break;
453         case GDK_MOTION_NOTIFY:
454             ret = connector_handle_motion_notify(cc, event->motion);
455             break;
457         case GDK_BUTTON_RELEASE:
458             ret = connector_handle_button_release(cc, event->button);
459             break;
460         case GDK_KEY_PRESS:
461             ret = connector_handle_key_press(cc, get_group0_keyval (&event->key));
462             break;
464         default:
465             break;
466     }
468     if (!ret) {
469         gint (*const parent_root_handler)(SPEventContext *, GdkEvent *)
470             = ((SPEventContextClass *) parent_class)->root_handler;
471         if (parent_root_handler) {
472             ret = parent_root_handler(ec, event);
473         }
474     }
476     return ret;
480 static gint
481 connector_handle_button_press(SPConnectorContext *const cc, GdkEventButton const &bevent)
483     NR::Point const event_w(bevent.x, bevent.y);
484     /* Find desktop coordinates */
485     NR::Point p = cc->desktop->w2d(event_w);
487     gint ret = FALSE;
488     if ( bevent.button == 1 ) {
490         SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
492         if (Inkscape::have_viable_layer(desktop, cc->_message_context) == false) {
493             return TRUE;
494         }
496         NR::Point const event_w(bevent.x,
497                                 bevent.y);
498         connector_drag_origin_w = event_w;
499         connector_within_tolerance = true;
501         NR::Point const event_dt = cc->desktop->w2d(event_w);
502         switch (cc->state) {
503             case SP_CONNECTOR_CONTEXT_STOP:
504                 /* This is allowed, if we just cancelled curve */
505             case SP_CONNECTOR_CONTEXT_IDLE:
506             {
507                 if ( cc->npoints == 0 ) {
508                     /* Set start anchor */
509                     NR::Point p;
511                     cc_clear_active_conn(cc);
513                     SP_EVENT_CONTEXT_DESKTOP(cc)->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating new connector"));
515                     /* Create green anchor */
516                     p = event_dt;
518                     // Test whether we clicked on a connection point
519                     cc->sid = conn_pt_handle_test(cc, p);
521                     if (!cc->sid) {
522                         // This is the first point, so just snap it to the grid
523                         // as there's no other points to go off.
524                         SnapManager const &m = cc->desktop->namedview->snap_manager;
525                         p = m.freeSnap(Inkscape::Snapper::SNAP_POINT | Inkscape::Snapper::BBOX_POINT,
526                                        p, NULL).getPoint();
527                     }
528                     spcc_connector_set_initial_point(cc, p);
530                 }
531                 cc->state = SP_CONNECTOR_CONTEXT_DRAGGING;
532                 ret = TRUE;
533                 break;
534             }
535             case SP_CONNECTOR_CONTEXT_DRAGGING:
536             {
537                 // This is the second click of a connector creation.
539                 spcc_connector_set_subsequent_point(cc, p);
540                 spcc_connector_finish_segment(cc, p);
541                 // Test whether we clicked on a connection point
542                 cc->eid = conn_pt_handle_test(cc, p);
543                 if (cc->npoints != 0) {
544                     spcc_connector_finish(cc);
545                 }
546                 cc_set_active_conn(cc, cc->newconn);
547                 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
548                 ret = TRUE;
549                 break;
550             }
551             case SP_CONNECTOR_CONTEXT_CLOSE:
552             {
553                 g_warning("Button down in CLOSE state");
554                 break;
555             }
556             default:
557                 break;
558         }
559     } else if (bevent.button == 3) {
560         if (cc->npoints != 0) {
561             spcc_connector_finish(cc);
562             ret = TRUE;
563         }
564     }
565     return ret;
569 static gint
570 connector_handle_motion_notify(SPConnectorContext *const cc, GdkEventMotion const &mevent)
572     gint ret = FALSE;
574     if (mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) {
575         // allow middle-button scrolling
576         return FALSE;
577     }
579     NR::Point const event_w(mevent.x, mevent.y);
581     if (connector_within_tolerance) {
582         gint const tolerance = prefs_get_int_attribute_limited("options.dragtolerance",
583                                                                "value", 0, 0, 100);
584         if ( NR::LInfty( event_w - connector_drag_origin_w ) < tolerance ) {
585             return FALSE;   // Do not drag if we're within tolerance from origin.
586         }
587     }
588     // Once the user has moved farther than tolerance from the original location
589     // (indicating they intend to move the object, not click), then always process
590     // the motion notify coordinates as given (no snapping back to origin)
591     connector_within_tolerance = false;
593     SPDesktop *const dt = cc->desktop;
595     /* Find desktop coordinates */
596     NR::Point p = dt->w2d(event_w);
598     switch (cc->state) {
599         case SP_CONNECTOR_CONTEXT_DRAGGING:
600         {
601             // This is movement during a connector creation.
603             if ( cc->npoints > 0 ) {
604                 cc->selection->clear();
605                 spcc_connector_set_subsequent_point(cc, p);
606                 ret = TRUE;
607             }
608             break;
609         }
610         case SP_CONNECTOR_CONTEXT_REROUTING:
611         {
612             g_assert( SP_IS_PATH(cc->clickeditem));
614             // Update the hidden path
615             NR::Matrix i2d = sp_item_i2d_affine(cc->clickeditem);
616             NR::Matrix d2i = i2d.inverse();
617             SPPath *path = SP_PATH(cc->clickeditem);
618             SPCurve *curve = (SP_SHAPE(path))->curve;
619             if (cc->clickedhandle == cc->endpt_handle[0]) {
620                 NR::Point o = cc->endpt_handle[1]->pos;
621                 sp_curve_stretch_endpoints(curve, p * d2i, o * d2i);
622             }
623             else {
624                 NR::Point o = cc->endpt_handle[0]->pos;
625                 sp_curve_stretch_endpoints(curve, o * d2i, p * d2i);
626             }
627             sp_conn_adjust_path(path);
629             // Copy this to the temporary visible path
630             cc->red_curve = sp_curve_copy(SP_SHAPE(path)->curve);
631             sp_curve_transform(cc->red_curve, i2d);
633             sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), cc->red_curve);
634             ret = TRUE;
635             break;
636         }
637         case SP_CONNECTOR_CONTEXT_STOP:
638             /* This is perfectly valid */
639             break;
640         default:
641             break;
642     }
644     return ret;
648 static gint
649 connector_handle_button_release(SPConnectorContext *const cc, GdkEventButton const &revent)
651     gint ret = FALSE;
652     if ( revent.button == 1 ) {
654         SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
655         SPDocument *doc = sp_desktop_document(desktop);
657         NR::Point const event_w(revent.x, revent.y);
659         /* Find desktop coordinates */
660         NR::Point p = cc->desktop->w2d(event_w);
662         switch (cc->state) {
663             //case SP_CONNECTOR_CONTEXT_POINT:
664             case SP_CONNECTOR_CONTEXT_DRAGGING:
665             {
666                 if (connector_within_tolerance)
667                 {
668                     spcc_connector_finish_segment(cc, p);
669                     return TRUE;
670                 }
671                 // Connector has been created via a drag, end it now.
672                 spcc_connector_set_subsequent_point(cc, p);
673                 spcc_connector_finish_segment(cc, p);
674                 // Test whether we clicked on a connection point
675                 cc->eid = conn_pt_handle_test(cc, p);
676                 if (cc->npoints != 0) {
677                     spcc_connector_finish(cc);
678                 }
679                 cc_set_active_conn(cc, cc->newconn);
680                 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
681                 break;
682             }
683             case SP_CONNECTOR_CONTEXT_REROUTING:
684             {
685                 // Clear the temporary path:
686                 sp_curve_reset(cc->red_curve);
687                 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
689                 // Test whether we clicked on a connection point
690                 gchar *shape_label = conn_pt_handle_test(cc, p);
692                 if (shape_label) {
693                     if (cc->clickedhandle == cc->endpt_handle[0]) {
694                         sp_object_setAttribute(cc->clickeditem,
695                                 "inkscape:connection-start",shape_label, false);
696                     }
697                     else {
698                         sp_object_setAttribute(cc->clickeditem,
699                                 "inkscape:connection-end",shape_label, false);
700                     }
701                     g_free(shape_label);
702                 }
703                 cc->clickeditem->setHidden(false);
704                 sp_conn_adjust_path(SP_PATH(cc->clickeditem));
705                 cc->clickeditem->updateRepr();
706                 sp_document_done(doc);
707                 cc_set_active_conn(cc, cc->clickeditem);
708                 sp_document_ensure_up_to_date(doc);
709                 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
710                 return TRUE;
711                 break;
712             }
713             case SP_CONNECTOR_CONTEXT_STOP:
714                 /* This is allowed, if we just cancelled curve */
715                 break;
716             default:
717                 break;
718         }
719         ret = TRUE;
720     }
722     return ret;
726 static gint
727 connector_handle_key_press(SPConnectorContext *const cc, guint const keyval)
729     gint ret = FALSE;
730     /* fixme: */
731     switch (keyval) {
732         case GDK_Return:
733         case GDK_KP_Enter:
734             if (cc->npoints != 0) {
735                 spcc_connector_finish(cc);
736                 ret = TRUE;
737             }
738             break;
739         case GDK_Escape:
740             if (cc->npoints != 0) {
741                 // if drawing, cancel, otherwise pass it up for deselecting
742                 cc->state = SP_CONNECTOR_CONTEXT_STOP;
743                 spcc_reset_colors(cc);
744                 ret = TRUE;
745             }
746             break;
747         default:
748             break;
749     }
750     return ret;
754 static void
755 spcc_reset_colors(SPConnectorContext *cc)
757     /* Red */
758     sp_curve_reset(cc->red_curve);
759     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
761     sp_curve_reset(cc->green_curve);
762     cc->npoints = 0;
766 static void
767 spcc_connector_set_initial_point(SPConnectorContext *const cc, NR::Point const p)
769     g_assert( cc->npoints == 0 );
771     cc->p[0] = p;
772     cc->p[1] = p;
773     cc->npoints = 2;
774     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
778 static void
779 spcc_connector_set_subsequent_point(SPConnectorContext *const cc, NR::Point const p)
781     g_assert( cc->npoints != 0 );
783     SPDesktop *dt = cc->desktop;
784     NR::Point o = dt->dt2doc(cc->p[0]);
785     NR::Point d = dt->dt2doc(p);
786     Avoid::Point src = { o[NR::X], o[NR::Y] };
787     Avoid::Point dst = { d[NR::X], d[NR::Y] };
789     if (!cc->newConnRef) {
790         Avoid::Router *router = sp_desktop_document(dt)->router;
791         cc->newConnRef = new Avoid::ConnRef(router, 0, src, dst);
792         cc->newConnRef->updateEndPoint(Avoid::VertID::src, src);
793     }
794     cc->newConnRef->updateEndPoint(Avoid::VertID::tar, dst);
796     cc->newConnRef->makePathInvalid();
797     cc->newConnRef->generatePath(src, dst);
799     Avoid::PolyLine route = cc->newConnRef->route();
800     cc->newConnRef->calcRouteDist();
802     sp_curve_reset(cc->red_curve);
803     NR::Point pt(route.ps[0].x, route.ps[0].y);
804     sp_curve_moveto(cc->red_curve, pt);
806     for (int i = 1; i < route.pn; ++i) {
807         NR::Point p(route.ps[i].x, route.ps[i].y);
808         sp_curve_lineto(cc->red_curve, p);
809     }
810     sp_curve_transform(cc->red_curve, dt->doc2dt());
811     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), cc->red_curve);
815 /**
816  * Concats red, blue and green.
817  * If any anchors are defined, process these, optionally removing curves from white list
818  * Invoke _flush_white to write result back to object.
819  */
820 static void
821 spcc_concat_colors_and_flush(SPConnectorContext *cc)
823     SPCurve *c = cc->green_curve;
824     cc->green_curve = sp_curve_new_sized(64);
826     sp_curve_reset(cc->red_curve);
827     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
829     if (sp_curve_empty(c)) {
830         sp_curve_unref(c);
831         return;
832     }
834     spcc_flush_white(cc, c);
836     sp_curve_unref(c);
840 /*
841  * Flushes white curve(s) and additional curve into object
842  *
843  * No cleaning of colored curves - this has to be done by caller
844  * No rereading of white data, so if you cannot rely on ::modified, do it in caller
845  *
846  */
848 static void
849 spcc_flush_white(SPConnectorContext *cc, SPCurve *gc)
851     SPCurve *c;
853     if (gc) {
854         c = gc;
855         sp_curve_ref(c);
856     } else {
857         return;
858     }
860     /* Now we have to go back to item coordinates at last */
861     sp_curve_transform(c,
862             sp_desktop_dt2root_affine(SP_EVENT_CONTEXT_DESKTOP(cc)));
864     SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
865     SPDocument *doc = sp_desktop_document(desktop);
867     if ( c && !sp_curve_empty(c) ) {
868         /* We actually have something to write */
870         Inkscape::XML::Node *repr = sp_repr_new("svg:path");
871         /* Set style */
872         sp_desktop_apply_style_tool(desktop, repr, "tools.connector", false);
874         gchar *str = sp_svg_write_path(SP_CURVE_BPATH(c));
875         g_assert( str != NULL );
876         repr->setAttribute("d", str);
877         g_free(str);
879         /* Attach repr */
880         cc->newconn = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
881         cc->selection->set(repr);
882         Inkscape::GC::release(repr);
883         cc->newconn->transform = i2i_affine(desktop->currentRoot(), desktop->currentLayer());
884         cc->newconn->updateRepr();
886         bool connection = false;
887         sp_object_setAttribute(cc->newconn, "inkscape:connector-type",
888                 "polyline", false);
889         if (cc->sid)
890         {
891             sp_object_setAttribute(cc->newconn, "inkscape:connection-start",
892                     cc->sid, false);
893             connection = true;
894         }
896         if (cc->eid)
897         {
898             sp_object_setAttribute(cc->newconn, "inkscape:connection-end",
899                     cc->eid, false);
900             connection = true;
901         }
902         cc->newconn->updateRepr();
903         if (connection) {
904             // Adjust endpoints to shape edge.
905             sp_conn_adjust_path(SP_PATH(cc->newconn));
906         }
907         cc->newconn->updateRepr();
908     }
910     sp_curve_unref(c);
912     /* Flush pending updates */
913     sp_document_done(doc);
914     sp_document_ensure_up_to_date(doc);
918 static void
919 spcc_connector_finish_segment(SPConnectorContext *const cc, NR::Point const p)
921     if (!sp_curve_empty(cc->red_curve)) {
922         sp_curve_append_continuous(cc->green_curve, cc->red_curve, 0.0625);
924         cc->p[0] = cc->p[3];
925         cc->p[1] = cc->p[4];
926         cc->npoints = 2;
928         sp_curve_reset(cc->red_curve);
929     }
933 static void
934 spcc_connector_finish(SPConnectorContext *const cc)
936     SPDesktop *const desktop = cc->desktop;
937     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Finishing connector"));
939     sp_curve_reset(cc->red_curve);
940     spcc_concat_colors_and_flush(cc);
942     cc->npoints = 0;
944     if (cc->newConnRef) {
945         cc->newConnRef->removeFromGraph();
946         delete cc->newConnRef;
947         cc->newConnRef = NULL;
948     }
949     cc->state = SP_CONNECTOR_CONTEXT_IDLE;
953 static gboolean
954 cc_generic_knot_handler(SPCanvasItem *, GdkEvent *event, SPKnot *knot)
956     g_assert (knot != NULL);
958     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(
959             knot->desktop->event_context);
961     gboolean consumed = FALSE;
963     switch (event->type) {
964         case GDK_ENTER_NOTIFY:
965             gtk_object_set (GTK_OBJECT (knot->item), "fill_color",
966                     knot->fill [SP_KNOT_STATE_MOUSEOVER], NULL);
967             gtk_object_set (GTK_OBJECT (knot->item), "stroke_color",
968                     knot->stroke [SP_KNOT_STATE_MOUSEOVER], NULL);
970             cc->active_handle = knot;
972             if (knot->tip)
973             {
974                 knot->desktop->event_context->defaultMessageContext()->set(
975                         Inkscape::NORMAL_MESSAGE, knot->tip);
976             }
978             consumed = TRUE;
979             break;
980         case GDK_LEAVE_NOTIFY:
981             gtk_object_set (GTK_OBJECT (knot->item), "fill_color",
982                     knot->fill [SP_KNOT_STATE_NORMAL], NULL);
983             gtk_object_set (GTK_OBJECT (knot->item), "stroke_color",
984                     knot->stroke [SP_KNOT_STATE_NORMAL], NULL);
986             cc->active_handle = NULL;
988             if (knot->tip) {
989                 knot->desktop->event_context->defaultMessageContext()->clear();
990             }
992             consumed = TRUE;
993             break;
994         default:
995             break;
996     }
998     return consumed;
1002 static gboolean
1003 endpt_handler(SPKnot *knot, GdkEvent *event, SPConnectorContext *cc)
1005     g_assert( SP_IS_CONNECTOR_CONTEXT(cc) );
1007     gboolean consumed = FALSE;
1009     switch (event->type) {
1010         case GDK_BUTTON_PRESS:
1011             g_assert( (cc->active_handle == cc->endpt_handle[0]) ||
1012                       (cc->active_handle == cc->endpt_handle[1]) );
1013             if (cc->state == SP_CONNECTOR_CONTEXT_IDLE) {
1014                 cc->clickeditem = cc->active_conn;
1015                 cc->clickedhandle = cc->active_handle;
1016                 cc_clear_active_conn(cc);
1017                 cc->state = SP_CONNECTOR_CONTEXT_REROUTING;
1019                 // Disconnect from attached shape
1020                 unsigned ind = (cc->active_handle == cc->endpt_handle[0]) ? 0 : 1;
1021                 sp_conn_end_detach(cc->clickeditem, ind);
1023                 NR::Point origin;
1024                 if (cc->clickedhandle == cc->endpt_handle[0]) {
1025                     origin = cc->endpt_handle[1]->pos;
1026                 }
1027                 else {
1028                     origin = cc->endpt_handle[0]->pos;
1029                 }
1031                 // Show the red path for dragging.
1032                 cc->red_curve = sp_curve_copy(SP_PATH(cc->clickeditem)->curve);
1033                 NR::Matrix i2d = sp_item_i2d_affine(cc->clickeditem);
1034                 sp_curve_transform(cc->red_curve, i2d);
1035                 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath),
1036                         cc->red_curve);
1038                 cc->clickeditem->setHidden(true);
1040                 // The rest of the interaction rerouting the connector is
1041                 // handled by the context root handler.
1042                 consumed = TRUE;
1043             }
1044             break;
1045         default:
1046             break;
1047     }
1049     return consumed;
1053 static void cc_set_active_shape(SPConnectorContext *cc, SPItem *item)
1055     g_assert(item != NULL );
1057     cc->active_shape = item;
1059     // Remove existing active shape listeners
1060     if (cc->active_shape_repr) {
1061         sp_repr_remove_listener_by_data(cc->active_shape_repr, cc);
1062         Inkscape::GC::release(cc->active_shape_repr);
1064         sp_repr_remove_listener_by_data(cc->active_shape_layer_repr, cc);
1065         Inkscape::GC::release(cc->active_shape_layer_repr);
1066     }
1068     // Listen in case the active shape changes
1069     cc->active_shape_repr = SP_OBJECT_REPR(item);
1070     if (cc->active_shape_repr) {
1071         Inkscape::GC::anchor(cc->active_shape_repr);
1072         sp_repr_add_listener(cc->active_shape_repr, &shape_repr_events, cc);
1074         cc->active_shape_layer_repr = cc->active_shape_repr->parent();
1075         Inkscape::GC::anchor(cc->active_shape_layer_repr);
1076         sp_repr_add_listener(cc->active_shape_layer_repr, &layer_repr_events, cc);
1077     }
1080     // Set center connection point.
1081     if ( cc->connpthandle == NULL ) {
1082         SPKnot * knot = (SPKnot*)g_object_new (SP_TYPE_KNOT, 0);
1084         knot->desktop = cc->desktop;
1085         knot->flags = SP_KNOT_VISIBLE;
1087         knot->item = sp_canvas_item_new (sp_desktop_controls(cc->desktop),
1088                 SP_TYPE_CTRL,
1089                 "anchor", GTK_ANCHOR_CENTER,
1090                 "filled", TRUE,
1091                 "stroked", TRUE,
1092                 "mode", SP_KNOT_MODE_XOR,
1093                 NULL);
1095         gtk_signal_connect (GTK_OBJECT (knot->item), "event",
1096                 GTK_SIGNAL_FUNC (cc_generic_knot_handler), knot);
1098         knot->fill [SP_KNOT_STATE_NORMAL] = 0xffffff00;
1099         knot->fill [SP_KNOT_STATE_MOUSEOVER] = 0xff0000ff;
1100         knot->stroke [SP_KNOT_STATE_NORMAL] = 0x01000000;
1102         g_object_set(G_OBJECT(knot),
1103                      "shape", SP_KNOT_SHAPE_SQUARE,
1104                      "size", 8,
1105                      "anchor", GTK_ANCHOR_CENTER,
1106                      "tip", _("<b>Connection point</b>: click or drag to create a new connector"),
1107                      NULL);
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 = (SPKnot*) g_object_new (SP_TYPE_KNOT, 0);
1164             knot->desktop = cc->desktop;
1165             knot->flags = SP_KNOT_VISIBLE;
1167             knot->item = sp_canvas_item_new (sp_desktop_controls (cc->desktop),
1168                     SP_TYPE_CTRL,
1169                     "anchor", GTK_ANCHOR_CENTER,
1170                     "filled", TRUE,
1171                     "stroked", TRUE,
1172                     "mode", SP_KNOT_MODE_XOR,
1173                     NULL);
1175             knot->fill [SP_KNOT_STATE_NORMAL] = 0xffffff00;
1176             knot->stroke [SP_KNOT_STATE_NORMAL] = 0x000000ff;
1177             knot->stroke [SP_KNOT_STATE_DRAGGING] = 0x000000ff;
1178             knot->stroke [SP_KNOT_STATE_MOUSEOVER] = 0x000000ff;
1180             g_object_set(G_OBJECT(knot),
1181                     "shape", SP_KNOT_SHAPE_DIAMOND,
1182                     "size", 10,
1183                     "tip", _("<b>Connector endpoint</b>: drag to reroute or connect to new shapes"),
1184                     NULL);
1186             gtk_signal_connect (GTK_OBJECT (knot->item), "event",
1187                     GTK_SIGNAL_FUNC (cc_generic_knot_handler), knot);
1189             cc->endpt_handle[i] = knot;
1190         }
1192         // Remove any existing handlers
1193         if (cc->endpt_handler_id[i]) {
1194             g_signal_handlers_disconnect_by_func(
1195                     G_OBJECT(cc->endpt_handle[i]->item),
1196                     (void*)G_CALLBACK(endpt_handler), (gpointer) cc );
1197             cc->endpt_handler_id[i] = 0;
1198         }
1200         // Setup handlers for connector endpoints, this is
1201         // is as 'after' so that cc_generic_knot_handler is
1202         // triggered first for any endpoint.
1203         cc->endpt_handler_id[i] = g_signal_connect_after(
1204                 G_OBJECT(cc->endpt_handle[i]->item), "event",
1205                 G_CALLBACK(endpt_handler), cc);
1206     }
1208     NR::Point startpt = sp_curve_first_point(curve) * i2d;
1209     sp_knot_set_position(cc->endpt_handle[0], &startpt, 0);
1211     NR::Point endpt = sp_curve_last_point(curve) * i2d;
1212     sp_knot_set_position(cc->endpt_handle[1], &endpt, 0);
1214     sp_knot_show(cc->endpt_handle[0]);
1215     sp_knot_show(cc->endpt_handle[1]);
1219 static bool cc_item_is_shape(SPItem *item)
1221     if (SP_IS_PATH(item)) {
1222         SPCurve *curve = (SP_SHAPE(item))->curve;
1223         if ( curve && !(curve->closed) ) {
1224             // Open paths are connectors.
1225             return false;
1226         }
1227     }
1228     return true;
1232 bool cc_item_is_connector(SPItem *item)
1234     if (SP_IS_PATH(item)) {
1235         if (SP_PATH(item)->connEndPair.isAutoRoutingConn()) {
1236             g_assert( !(SP_SHAPE(item)->curve->closed) );
1237             return true;
1238         }
1239     }
1240     return false;
1244 void cc_selection_set_avoid(bool const set_avoid)
1246     SPDesktop *desktop = inkscape_active_desktop();
1247     if (desktop == NULL) {
1248         return;
1249     }
1251     SPDocument *document = sp_desktop_document(desktop);
1253     Inkscape::Selection *selection = sp_desktop_selection(desktop);
1255     GSList *l = (GSList *) selection->itemList();
1257     int changes = 0;
1259     while (l) {
1260         SPItem *item = (SPItem *) l->data;
1262         char const *value = (set_avoid) ? "true" : NULL;
1264         if (cc_item_is_shape(item)) {
1265             sp_object_setAttribute(item, "inkscape:connector-avoid",
1266                     value, false);
1267             item->avoidRef->handleSettingChange();
1268             changes++;
1269         }
1271         l = l->next;
1272     }
1274     if (changes == 0) {
1275         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE,
1276                 _("Select <b>at least one non-connector object</b>."));
1277         return;
1278     }
1280     sp_document_done(document);
1284 static void
1285 cc_selection_changed(Inkscape::Selection *selection, gpointer data)
1287     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(data);
1288     //SPEventContext *ec = SP_EVENT_CONTEXT(cc);
1290     SPItem *item = selection->singleItem();
1292     if (cc->active_conn == item)
1293     {
1294         // Nothing to change.
1295         return;
1296     }
1297     if (item == NULL)
1298     {
1299         cc_clear_active_conn(cc);
1300         return;
1301     }
1303     if (cc_item_is_connector(item)) {
1304         cc_set_active_conn(cc, item);
1305     }
1309 static void
1310 shape_event_attr_deleted(Inkscape::XML::Node *repr, Inkscape::XML::Node *child,
1311         Inkscape::XML::Node *ref, gpointer data)
1313     g_assert(data);
1314     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(data);
1316     if (child == cc->active_shape_repr) {
1317         // The active shape has been deleted.  Clear active shape.
1318         cc_clear_active_shape(cc);
1319     }
1323 static void
1324 shape_event_attr_changed(Inkscape::XML::Node *repr, gchar const *name,
1325                             gchar const *old_value, gchar const *new_value,
1326                             bool is_interactive, gpointer data)
1328     g_assert(data);
1329     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(data);
1331     // Look for changes than result in onscreen movement.
1332     if (!strcmp(name, "d") || !strcmp(name, "x") || !strcmp(name, "y") ||
1333             !strcmp(name, "width") || !strcmp(name, "height") ||
1334             !strcmp(name, "transform"))
1335     {
1336         if (repr == cc->active_shape_repr) {
1337             // Active shape has moved. Clear active shape.
1338             cc_clear_active_shape(cc);
1339         }
1340         else if (repr == cc->active_conn_repr) {
1341             // The active conn has been moved.
1342             // Set it again, which just sets new handle positions.
1343             cc_set_active_conn(cc, cc->active_conn);
1344         }
1345     }
1349 /*
1350   Local Variables:
1351   mode:c++
1352   c-file-style:"stroustrup"
1353   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1354   indent-tabs-mode:nil
1355   fill-column:99
1356   End:
1357 */
1358 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :