Code

init matrix variable, removes compiler warnings
[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  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);
711                 cc_set_active_conn(cc, cc->clickeditem);
712                 sp_document_ensure_up_to_date(doc);
713                 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
714                 return TRUE;
715                 break;
716             }
717             case SP_CONNECTOR_CONTEXT_STOP:
718                 /* This is allowed, if we just cancelled curve */
719                 break;
720             default:
721                 break;
722         }
723         ret = TRUE;
724     }
726     return ret;
730 static gint
731 connector_handle_key_press(SPConnectorContext *const cc, guint const keyval)
733     gint ret = FALSE;
734     /* fixme: */
735     switch (keyval) {
736         case GDK_Return:
737         case GDK_KP_Enter:
738             if (cc->npoints != 0) {
739                 spcc_connector_finish(cc);
740                 ret = TRUE;
741             }
742             break;
743         case GDK_Escape:
744             if (cc->npoints != 0) {
745                 // if drawing, cancel, otherwise pass it up for deselecting
746                 cc->state = SP_CONNECTOR_CONTEXT_STOP;
747                 spcc_reset_colors(cc);
748                 ret = TRUE;
749             }
750             break;
751         default:
752             break;
753     }
754     return ret;
758 static void
759 spcc_reset_colors(SPConnectorContext *cc)
761     /* Red */
762     sp_curve_reset(cc->red_curve);
763     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
765     sp_curve_reset(cc->green_curve);
766     cc->npoints = 0;
770 static void
771 spcc_connector_set_initial_point(SPConnectorContext *const cc, NR::Point const p)
773     g_assert( cc->npoints == 0 );
775     cc->p[0] = p;
776     cc->p[1] = p;
777     cc->npoints = 2;
778     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
782 static void
783 spcc_connector_set_subsequent_point(SPConnectorContext *const cc, NR::Point const p)
785     g_assert( cc->npoints != 0 );
787     SPDesktop *dt = cc->desktop;
788     NR::Point o = dt->dt2doc(cc->p[0]);
789     NR::Point d = dt->dt2doc(p);
790     Avoid::Point src = { o[NR::X], o[NR::Y] };
791     Avoid::Point dst = { d[NR::X], d[NR::Y] };
793     if (!cc->newConnRef) {
794         Avoid::Router *router = sp_desktop_document(dt)->router;
795         cc->newConnRef = new Avoid::ConnRef(router, 0, src, dst);
796         cc->newConnRef->updateEndPoint(Avoid::VertID::src, src);
797     }
798     cc->newConnRef->updateEndPoint(Avoid::VertID::tar, dst);
800     cc->newConnRef->makePathInvalid();
801     cc->newConnRef->generatePath(src, dst);
803     Avoid::PolyLine route = cc->newConnRef->route();
804     cc->newConnRef->calcRouteDist();
806     sp_curve_reset(cc->red_curve);
807     NR::Point pt(route.ps[0].x, route.ps[0].y);
808     sp_curve_moveto(cc->red_curve, pt);
810     for (int i = 1; i < route.pn; ++i) {
811         NR::Point p(route.ps[i].x, route.ps[i].y);
812         sp_curve_lineto(cc->red_curve, p);
813     }
814     sp_curve_transform(cc->red_curve, dt->doc2dt());
815     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), cc->red_curve);
819 /**
820  * Concats red, blue and green.
821  * If any anchors are defined, process these, optionally removing curves from white list
822  * Invoke _flush_white to write result back to object.
823  */
824 static void
825 spcc_concat_colors_and_flush(SPConnectorContext *cc)
827     SPCurve *c = cc->green_curve;
828     cc->green_curve = sp_curve_new_sized(64);
830     sp_curve_reset(cc->red_curve);
831     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
833     if (sp_curve_empty(c)) {
834         sp_curve_unref(c);
835         return;
836     }
838     spcc_flush_white(cc, c);
840     sp_curve_unref(c);
844 /*
845  * Flushes white curve(s) and additional curve into object
846  *
847  * No cleaning of colored curves - this has to be done by caller
848  * No rereading of white data, so if you cannot rely on ::modified, do it in caller
849  *
850  */
852 static void
853 spcc_flush_white(SPConnectorContext *cc, SPCurve *gc)
855     SPCurve *c;
857     if (gc) {
858         c = gc;
859         sp_curve_ref(c);
860     } else {
861         return;
862     }
864     /* Now we have to go back to item coordinates at last */
865     sp_curve_transform(c,
866             sp_desktop_dt2root_affine(SP_EVENT_CONTEXT_DESKTOP(cc)));
868     SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
869     SPDocument *doc = sp_desktop_document(desktop);
871     if ( c && !sp_curve_empty(c) ) {
872         /* We actually have something to write */
874         Inkscape::XML::Node *repr = sp_repr_new("svg:path");
875         /* Set style */
876         sp_desktop_apply_style_tool(desktop, repr, "tools.connector", false);
878         gchar *str = sp_svg_write_path(SP_CURVE_BPATH(c));
879         g_assert( str != NULL );
880         repr->setAttribute("d", str);
881         g_free(str);
883         /* Attach repr */
884         cc->newconn = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
885         cc->selection->set(repr);
886         Inkscape::GC::release(repr);
887         cc->newconn->transform = i2i_affine(desktop->currentRoot(), desktop->currentLayer());
888         cc->newconn->updateRepr();
890         bool connection = false;
891         sp_object_setAttribute(cc->newconn, "inkscape:connector-type",
892                 "polyline", false);
893         if (cc->sid)
894         {
895             sp_object_setAttribute(cc->newconn, "inkscape:connection-start",
896                     cc->sid, false);
897             connection = true;
898         }
900         if (cc->eid)
901         {
902             sp_object_setAttribute(cc->newconn, "inkscape:connection-end",
903                     cc->eid, false);
904             connection = true;
905         }
906         cc->newconn->updateRepr();
907         if (connection) {
908             // Adjust endpoints to shape edge.
909             sp_conn_adjust_path(SP_PATH(cc->newconn));
910         }
911         cc->newconn->updateRepr();
912     }
914     sp_curve_unref(c);
916     /* Flush pending updates */
917     sp_document_done(doc);
918     sp_document_ensure_up_to_date(doc);
922 static void
923 spcc_connector_finish_segment(SPConnectorContext *const cc, NR::Point const p)
925     if (!sp_curve_empty(cc->red_curve)) {
926         sp_curve_append_continuous(cc->green_curve, cc->red_curve, 0.0625);
928         cc->p[0] = cc->p[3];
929         cc->p[1] = cc->p[4];
930         cc->npoints = 2;
932         sp_curve_reset(cc->red_curve);
933     }
937 static void
938 spcc_connector_finish(SPConnectorContext *const cc)
940     SPDesktop *const desktop = cc->desktop;
941     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Finishing connector"));
943     sp_curve_reset(cc->red_curve);
944     spcc_concat_colors_and_flush(cc);
946     cc->npoints = 0;
948     if (cc->newConnRef) {
949         cc->newConnRef->removeFromGraph();
950         delete cc->newConnRef;
951         cc->newConnRef = NULL;
952     }
953     cc->state = SP_CONNECTOR_CONTEXT_IDLE;
957 static gboolean
958 cc_generic_knot_handler(SPCanvasItem *, GdkEvent *event, SPKnot *knot)
960     g_assert (knot != NULL);
961     
962     g_object_ref(knot);
964     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(
965             knot->desktop->event_context);
967     gboolean consumed = FALSE;
969     switch (event->type) {
970         case GDK_ENTER_NOTIFY:
971             sp_knot_set_flag(knot, SP_KNOT_MOUSEOVER, TRUE);
972             
973             cc->active_handle = knot;
975             if (knot->tip)
976             {
977                 knot->desktop->event_context->defaultMessageContext()->set(
978                         Inkscape::NORMAL_MESSAGE, knot->tip);
979             }
980             
981             consumed = TRUE;
982             break;
983         case GDK_LEAVE_NOTIFY:
984             sp_knot_set_flag(knot, SP_KNOT_MOUSEOVER, FALSE);
986             cc->active_handle = NULL;
987             
988             if (knot->tip) {
989                 knot->desktop->event_context->defaultMessageContext()->clear();
990             }
991             
992             consumed = TRUE;
993             break;
994         default:
995             break;
996     }
997     
998     g_object_unref(knot);
1000     return consumed;
1004 static gboolean
1005 endpt_handler(SPKnot *knot, GdkEvent *event, SPConnectorContext *cc)
1007     g_assert( SP_IS_CONNECTOR_CONTEXT(cc) );
1009     gboolean consumed = FALSE;
1011     switch (event->type) {
1012         case GDK_BUTTON_PRESS:
1013             g_assert( (cc->active_handle == cc->endpt_handle[0]) ||
1014                       (cc->active_handle == cc->endpt_handle[1]) );
1015             if (cc->state == SP_CONNECTOR_CONTEXT_IDLE) {
1016                 cc->clickeditem = cc->active_conn;
1017                 cc->clickedhandle = cc->active_handle;
1018                 cc_clear_active_conn(cc);
1019                 cc->state = SP_CONNECTOR_CONTEXT_REROUTING;
1021                 // Disconnect from attached shape
1022                 unsigned ind = (cc->active_handle == cc->endpt_handle[0]) ? 0 : 1;
1023                 sp_conn_end_detach(cc->clickeditem, ind);
1025                 NR::Point origin;
1026                 if (cc->clickedhandle == cc->endpt_handle[0]) {
1027                     origin = cc->endpt_handle[1]->pos;
1028                 }
1029                 else {
1030                     origin = cc->endpt_handle[0]->pos;
1031                 }
1033                 // Show the red path for dragging.
1034                 cc->red_curve = sp_curve_copy(SP_PATH(cc->clickeditem)->curve);
1035                 NR::Matrix i2d = sp_item_i2d_affine(cc->clickeditem);
1036                 sp_curve_transform(cc->red_curve, i2d);
1037                 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath),
1038                         cc->red_curve);
1040                 cc->clickeditem->setHidden(true);
1042                 // The rest of the interaction rerouting the connector is
1043                 // handled by the context root handler.
1044                 consumed = TRUE;
1045             }
1046             break;
1047         default:
1048             break;
1049     }
1051     return consumed;
1055 static void cc_set_active_shape(SPConnectorContext *cc, SPItem *item)
1057     g_assert(item != NULL );
1059     cc->active_shape = item;
1061     // Remove existing active shape listeners
1062     if (cc->active_shape_repr) {
1063         sp_repr_remove_listener_by_data(cc->active_shape_repr, cc);
1064         Inkscape::GC::release(cc->active_shape_repr);
1066         sp_repr_remove_listener_by_data(cc->active_shape_layer_repr, cc);
1067         Inkscape::GC::release(cc->active_shape_layer_repr);
1068     }
1070     // Listen in case the active shape changes
1071     cc->active_shape_repr = SP_OBJECT_REPR(item);
1072     if (cc->active_shape_repr) {
1073         Inkscape::GC::anchor(cc->active_shape_repr);
1074         sp_repr_add_listener(cc->active_shape_repr, &shape_repr_events, cc);
1076         cc->active_shape_layer_repr = cc->active_shape_repr->parent();
1077         Inkscape::GC::anchor(cc->active_shape_layer_repr);
1078         sp_repr_add_listener(cc->active_shape_layer_repr, &layer_repr_events, cc);
1079     }
1082     // Set center connection point.
1083     if ( cc->connpthandle == NULL ) {
1084         SPKnot *knot = sp_knot_new(cc->desktop, 
1085                 _("<b>Connection point</b>: click or drag to create a new connector"));
1087         knot->setShape(SP_KNOT_SHAPE_SQUARE);
1088         knot->setSize(8);
1089         knot->setAnchor(GTK_ANCHOR_CENTER);
1090         knot->setFill(0xffffff00, 0xff0000ff, 0xff0000ff);
1091         sp_knot_update_ctrl(knot);
1093         // We don't want to use the standard knot handler,
1094         //since we don't want this knot to be draggable.
1095         g_signal_handler_disconnect(G_OBJECT(knot->item),
1096                 knot->_event_handler_id);
1097         knot->_event_handler_id = 0;
1099         gtk_signal_connect(GTK_OBJECT(knot->item), "event",
1100                 GTK_SIGNAL_FUNC(cc_generic_knot_handler), knot);
1102         cc->connpthandle = knot;
1103     }
1106     NR::Rect bbox = sp_item_bbox_desktop(cc->active_shape);
1107     NR::Point center = bbox.midpoint();
1108     sp_knot_set_position(cc->connpthandle, &center, 0);
1110     sp_knot_show(cc->connpthandle);
1115 static void
1116 cc_set_active_conn(SPConnectorContext *cc, SPItem *item)
1118     g_assert( SP_IS_PATH(item) );
1120     SPCurve *curve = SP_SHAPE(SP_PATH(item))->curve;
1121     NR::Matrix i2d = sp_item_i2d_affine(item);
1123     if (cc->active_conn == item)
1124     {
1125         // Just adjust handle positions.
1126         NR::Point startpt = sp_curve_first_point(curve) * i2d;
1127         sp_knot_set_position(cc->endpt_handle[0], &startpt, 0);
1129         NR::Point endpt = sp_curve_last_point(curve) * i2d;
1130         sp_knot_set_position(cc->endpt_handle[1], &endpt, 0);
1132         return;
1133     }
1135     cc->active_conn = item;
1137     // Remove existing active conn listeners
1138     if (cc->active_conn_repr) {
1139         sp_repr_remove_listener_by_data(cc->active_conn_repr, cc);
1140         Inkscape::GC::release(cc->active_conn_repr);
1141         cc->active_conn_repr = NULL;
1142     }
1144     // Listen in case the active conn changes
1145     cc->active_conn_repr = SP_OBJECT_REPR(item);
1146     if (cc->active_conn_repr) {
1147         Inkscape::GC::anchor(cc->active_conn_repr);
1148         sp_repr_add_listener(cc->active_conn_repr, &shape_repr_events, cc);
1149     }
1151     for (int i = 0; i < 2; ++i) {
1153         // Create the handle if it doesn't exist
1154         if ( cc->endpt_handle[i] == NULL ) {
1155             SPKnot *knot = sp_knot_new(cc->desktop, 
1156                     _("<b>Connector endpoint</b>: drag to reroute or connect to new shapes"));
1158             knot->setShape(SP_KNOT_SHAPE_SQUARE);
1159             knot->setSize(7);
1160             knot->setAnchor(GTK_ANCHOR_CENTER);
1161             knot->setFill(0xffffff00, 0xff0000ff, 0xff0000ff);
1162             knot->setStroke(0x000000ff, 0x000000ff, 0x000000ff);
1163             sp_knot_update_ctrl(knot);
1165             // We don't want to use the standard knot handler,
1166             //since we don't want this knot to be draggable.
1167             g_signal_handler_disconnect(G_OBJECT(knot->item),
1168                     knot->_event_handler_id);
1169             knot->_event_handler_id = 0;
1171             gtk_signal_connect(GTK_OBJECT(knot->item), "event",
1172                     GTK_SIGNAL_FUNC(cc_generic_knot_handler), knot);
1174             cc->endpt_handle[i] = knot;
1175         }
1177         // Remove any existing handlers
1178         if (cc->endpt_handler_id[i]) {
1179             g_signal_handlers_disconnect_by_func(
1180                     G_OBJECT(cc->endpt_handle[i]->item),
1181                     (void*)G_CALLBACK(endpt_handler), (gpointer) cc );
1182             cc->endpt_handler_id[i] = 0;
1183         }
1185         // Setup handlers for connector endpoints, this is
1186         // is as 'after' so that cc_generic_knot_handler is
1187         // triggered first for any endpoint.
1188         cc->endpt_handler_id[i] = g_signal_connect_after(
1189                 G_OBJECT(cc->endpt_handle[i]->item), "event",
1190                 G_CALLBACK(endpt_handler), cc);
1191     }
1193     NR::Point startpt = sp_curve_first_point(curve) * i2d;
1194     sp_knot_set_position(cc->endpt_handle[0], &startpt, 0);
1196     NR::Point endpt = sp_curve_last_point(curve) * i2d;
1197     sp_knot_set_position(cc->endpt_handle[1], &endpt, 0);
1199     sp_knot_show(cc->endpt_handle[0]);
1200     sp_knot_show(cc->endpt_handle[1]);
1204 static bool cc_item_is_shape(SPItem *item)
1206     if (SP_IS_PATH(item)) {
1207         SPCurve *curve = (SP_SHAPE(item))->curve;
1208         if ( curve && !(curve->closed) ) {
1209             // Open paths are connectors.
1210             return false;
1211         }
1212     }
1213     else if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
1214         if (prefs_get_int_attribute("tools.connector", "ignoretext", 1) == 1) {
1215             // Don't count text as a shape we can connect connector to.
1216             return false;
1217         }
1218     }
1219     return true;
1223 bool cc_item_is_connector(SPItem *item)
1225     if (SP_IS_PATH(item)) {
1226         if (SP_PATH(item)->connEndPair.isAutoRoutingConn()) {
1227             g_assert( !(SP_SHAPE(item)->curve->closed) );
1228             return true;
1229         }
1230     }
1231     return false;
1235 void cc_selection_set_avoid(bool const set_avoid)
1237     SPDesktop *desktop = inkscape_active_desktop();
1238     if (desktop == NULL) {
1239         return;
1240     }
1242     SPDocument *document = sp_desktop_document(desktop);
1244     Inkscape::Selection *selection = sp_desktop_selection(desktop);
1246     GSList *l = (GSList *) selection->itemList();
1248     int changes = 0;
1250     while (l) {
1251         SPItem *item = (SPItem *) l->data;
1253         char const *value = (set_avoid) ? "true" : NULL;
1255         if (cc_item_is_shape(item)) {
1256             sp_object_setAttribute(item, "inkscape:connector-avoid",
1257                     value, false);
1258             item->avoidRef->handleSettingChange();
1259             changes++;
1260         }
1262         l = l->next;
1263     }
1265     if (changes == 0) {
1266         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE,
1267                 _("Select <b>at least one non-connector object</b>."));
1268         return;
1269     }
1271     sp_document_done(document);
1275 static void
1276 cc_selection_changed(Inkscape::Selection *selection, gpointer data)
1278     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(data);
1279     //SPEventContext *ec = SP_EVENT_CONTEXT(cc);
1281     SPItem *item = selection->singleItem();
1283     if (cc->active_conn == item)
1284     {
1285         // Nothing to change.
1286         return;
1287     }
1288     if (item == NULL)
1289     {
1290         cc_clear_active_conn(cc);
1291         return;
1292     }
1294     if (cc_item_is_connector(item)) {
1295         cc_set_active_conn(cc, item);
1296     }
1300 static void
1301 shape_event_attr_deleted(Inkscape::XML::Node *repr, Inkscape::XML::Node *child,
1302         Inkscape::XML::Node *ref, gpointer data)
1304     g_assert(data);
1305     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(data);
1307     if (child == cc->active_shape_repr) {
1308         // The active shape has been deleted.  Clear active shape.
1309         cc_clear_active_shape(cc);
1310     }
1314 static void
1315 shape_event_attr_changed(Inkscape::XML::Node *repr, gchar const *name,
1316                             gchar const *old_value, gchar const *new_value,
1317                             bool is_interactive, gpointer data)
1319     g_assert(data);
1320     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(data);
1322     // Look for changes than result in onscreen movement.
1323     if (!strcmp(name, "d") || !strcmp(name, "x") || !strcmp(name, "y") ||
1324             !strcmp(name, "width") || !strcmp(name, "height") ||
1325             !strcmp(name, "transform"))
1326     {
1327         if (repr == cc->active_shape_repr) {
1328             // Active shape has moved. Clear active shape.
1329             cc_clear_active_shape(cc);
1330         }
1331         else if (repr == cc->active_conn_repr) {
1332             // The active conn has been moved.
1333             // Set it again, which just sets new handle positions.
1334             cc_set_active_conn(cc, cc->active_conn);
1335         }
1336     }
1340 /*
1341   Local Variables:
1342   mode:c++
1343   c-file-style:"stroustrup"
1344   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1345   indent-tabs-mode:nil
1346   fill-column:99
1347   End:
1348 */
1349 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :