Code

* src/display/sp-canvas.cpp, display/sp-canvas.h, connector-context.cpp:
[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"
73 static void sp_connector_context_class_init(SPConnectorContextClass *klass);
74 static void sp_connector_context_init(SPConnectorContext *conn_context);
75 static void sp_connector_context_dispose(GObject *object);
77 static void sp_connector_context_setup(SPEventContext *ec);
78 static void sp_connector_context_finish(SPEventContext *ec);
79 static gint sp_connector_context_root_handler(SPEventContext *ec, GdkEvent *event);
80 static gint sp_connector_context_item_handler(SPEventContext *event_context, SPItem *item, GdkEvent *event);
82 // Stuff borrowed from DrawContext
83 static void spcc_connector_set_initial_point(SPConnectorContext *cc, NR::Point const p);
84 static void spcc_connector_set_subsequent_point(SPConnectorContext *cc, NR::Point const p);
85 static void spcc_connector_finish_segment(SPConnectorContext *cc, NR::Point p);
86 static void spcc_reset_colors(SPConnectorContext *cc);
87 static void spcc_connector_finish(SPConnectorContext *cc);
88 static void spcc_concat_colors_and_flush(SPConnectorContext *cc);
89 static void spcc_flush_white(SPConnectorContext *cc, SPCurve *gc);
91 // Context event handlers
92 static gint connector_handle_button_press(SPConnectorContext *const cc, GdkEventButton const &bevent);
93 static gint connector_handle_motion_notify(SPConnectorContext *const cc, GdkEventMotion const &mevent);
94 static gint connector_handle_button_release(SPConnectorContext *const cc, GdkEventButton const &revent);
95 static gint connector_handle_key_press(SPConnectorContext *const cc, guint const keyval);
97 static void cc_set_active_shape(SPConnectorContext *cc, SPItem *item);
98 static void cc_clear_active_shape(SPConnectorContext *cc);
99 static void cc_set_active_conn(SPConnectorContext *cc, SPItem *item);
100 static void cc_clear_active_conn(SPConnectorContext *cc);
101 static gchar *conn_pt_handle_test(SPConnectorContext *cc, NR::Point& w);
102 static bool cc_item_is_shape(SPItem *item);
103 static void cc_selection_changed(Inkscape::Selection *selection, gpointer data);
105 static void shape_event_attr_deleted(Inkscape::XML::Node *repr,
106         Inkscape::XML::Node *child, Inkscape::XML::Node *ref, gpointer data);
107 static void shape_event_attr_changed(Inkscape::XML::Node *repr, gchar const *name,
108         gchar const *old_value, gchar const *new_value, bool is_interactive,
109         gpointer data);
112 static NR::Point connector_drag_origin_w(0, 0);
113 static bool connector_within_tolerance = false;
114 static SPEventContextClass *parent_class;
117 static Inkscape::XML::NodeEventVector shape_repr_events = {
118     NULL, /* child_added */
119     NULL, /* child_added */
120     shape_event_attr_changed,
121     NULL, /* content_changed */
122     NULL  /* order_changed */
123 };
125 static Inkscape::XML::NodeEventVector layer_repr_events = {
126     NULL, /* child_added */
127     shape_event_attr_deleted,
128     NULL, /* child_added */
129     NULL, /* content_changed */
130     NULL  /* order_changed */
131 };
134 GType
135 sp_connector_context_get_type(void)
137     static GType type = 0;
138     if (!type) {
139         GTypeInfo info = {
140             sizeof(SPConnectorContextClass),
141             NULL, NULL,
142             (GClassInitFunc) sp_connector_context_class_init,
143             NULL, NULL,
144             sizeof(SPConnectorContext),
145             4,
146             (GInstanceInitFunc) sp_connector_context_init,
147             NULL,   /* value_table */
148         };
149         type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPConnectorContext", &info, (GTypeFlags)0);
150     }
151     return type;
154 static void
155 sp_connector_context_class_init(SPConnectorContextClass *klass)
157     GObjectClass *object_class;
158     SPEventContextClass *event_context_class;
160     object_class = (GObjectClass *) klass;
161     event_context_class = (SPEventContextClass *) klass;
163     parent_class = (SPEventContextClass*)g_type_class_peek_parent(klass);
165     object_class->dispose = sp_connector_context_dispose;
167     event_context_class->setup = sp_connector_context_setup;
168     event_context_class->finish = sp_connector_context_finish;
169     event_context_class->root_handler = sp_connector_context_root_handler;
170     event_context_class->item_handler = sp_connector_context_item_handler;
174 static void
175 sp_connector_context_init(SPConnectorContext *cc)
177     SPEventContext *ec = SP_EVENT_CONTEXT(cc);
179     ec->cursor_shape = cursor_connector_xpm;
180     ec->hot_x = 1;
181     ec->hot_y = 1;
182     ec->xp = 0;
183     ec->yp = 0;
185     cc->red_color = 0xff00007f;
186     
187     cc->newconn = NULL;
188     cc->newConnRef = NULL;
189    
190     cc->sel_changed_connection = sigc::connection();
191     
192     cc->active_shape = NULL;
193     cc->active_shape_repr = NULL;
194     cc->active_shape_layer_repr = NULL;
196     cc->active_conn = NULL;
197     cc->active_conn_repr = NULL;
199     cc->active_handle = NULL;
200     
201     cc->clickeditem = NULL;
202     cc->clickedhandle = NULL;
203     
204     cc->connpthandle = NULL;
205     for (int i = 0; i < 2; ++i) {
206         cc->endpt_handle[i] = NULL;
207         cc->endpt_handler_id[i] = 0;
208     }
209     cc->sid = NULL;
210     cc->eid = NULL;
211     cc->npoints = 0;
212     cc->state = SP_CONNECTOR_CONTEXT_IDLE;
216 static void
217 sp_connector_context_dispose(GObject *object)
219     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(object);
221     cc->sel_changed_connection.disconnect();
223     if (cc->connpthandle) {
224         g_object_unref(cc->connpthandle);
225         cc->connpthandle = NULL;
226     }
227     for (int i = 0; i < 2; ++i) {
228         if (cc->endpt_handle[1]) {
229             g_object_unref(cc->endpt_handle[i]);
230             cc->endpt_handle[i] = NULL;
231         }
232     }
233     if (cc->sid) {
234         g_free(cc->sid);
235         cc->sid = NULL;
236     }
237     if (cc->eid) {
238         g_free(cc->eid);
239         cc->eid = NULL;
240     }
241     g_assert( cc->newConnRef == NULL );
243     G_OBJECT_CLASS(parent_class)->dispose(object);
247 static void
248 sp_connector_context_setup(SPEventContext *ec)
250     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(ec);
251     SPDesktop *dt = ec->desktop;
253     if (((SPEventContextClass *) parent_class)->setup) {
254         ((SPEventContextClass *) parent_class)->setup(ec);
255     }
257     cc->selection = SP_DT_SELECTION(dt);
259     cc->sel_changed_connection.disconnect();
260     cc->sel_changed_connection = cc->selection->connectChanged(
261             sigc::bind(sigc::ptr_fun(&cc_selection_changed),
262             (gpointer) cc));
263     
264     /* Create red bpath */
265     cc->red_bpath = sp_canvas_bpath_new(SP_DT_SKETCH(ec->desktop), NULL);
266     sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cc->red_bpath), cc->red_color,
267             1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
268     sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(cc->red_bpath), 0x00000000,
269             SP_WIND_RULE_NONZERO);
270     /* Create red curve */
271     cc->red_curve = sp_curve_new_sized(4);
273     /* Create green curve */
274     cc->green_curve = sp_curve_new_sized(64);
276     // Notice the initial selection.
277     cc_selection_changed(cc->selection, (gpointer) cc);
278     
279     if (prefs_get_int_attribute("tools.connector", "selcue", 0) != 0) {
280         ec->enableSelectionCue();
281     }
283     // Make sure we see all enter events for canvas items,
284     // even if a mouse button is depressed.
285     dt->canvas->gen_all_enter_events = true;
289 static void
290 sp_connector_context_finish(SPEventContext *ec)
292     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(ec);
293     
294     spcc_connector_finish(cc);
296     if (((SPEventContextClass *) parent_class)->finish) {
297         ((SPEventContextClass *) parent_class)->finish(ec);
298     }
299     
300     if (cc->selection) {
301         cc->selection = NULL;
302     }
303     cc_clear_active_shape(cc);
304     cc_clear_active_conn(cc);
306     // Restore the default event generating behaviour.
307     SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(ec);
308     desktop->canvas->gen_all_enter_events = false;
312 //-----------------------------------------------------------------------------
315 static void
316 cc_clear_active_shape(SPConnectorContext *cc)
318     if (cc->active_shape == NULL) {
319         return;
320     }
321     g_assert( cc->active_shape_repr );
322     g_assert( cc->active_shape_layer_repr );
324     cc->active_shape = NULL;
326     if (cc->active_shape_repr) {
327         sp_repr_remove_listener_by_data(cc->active_shape_repr, cc);
328         Inkscape::GC::release(cc->active_shape_repr);
329         cc->active_shape_repr = NULL;
330         
331         sp_repr_remove_listener_by_data(cc->active_shape_layer_repr, cc);
332         Inkscape::GC::release(cc->active_shape_layer_repr);
333         cc->active_shape_layer_repr = NULL;
334     }
336     // Hide the center connection point if it exists.
337     if (cc->connpthandle) {
338         sp_knot_hide(cc->connpthandle);
339     }
343 static void
344 cc_clear_active_conn(SPConnectorContext *cc)
346     if (cc->active_conn == NULL) {
347         return;
348     }
349     g_assert( cc->active_conn_repr );
351     cc->active_conn = NULL;
353     if (cc->active_conn_repr) {
354         sp_repr_remove_listener_by_data(cc->active_conn_repr, cc);
355         Inkscape::GC::release(cc->active_conn_repr);
356         cc->active_conn_repr = NULL;
357     }
359     // Hide the endpoint handles.
360     for (int i = 0; i < 2; ++i) {
361         if (cc->endpt_handle[i]) {
362             sp_knot_hide(cc->endpt_handle[i]);
363         }
364     }
368 static gchar *
369 conn_pt_handle_test(SPConnectorContext *cc, NR::Point& p)
371     // TODO: this will need to change when there are more connection
372     //       points available for each shape.
373    
374     SPKnot *centerpt = cc->connpthandle;
375     if (cc->active_handle && (cc->active_handle == centerpt))
376     {
377         p = centerpt->pos;
378         return g_strdup_printf("#%s", SP_OBJECT_ID(cc->active_shape));
379     }
380     return NULL;
385 static gint
386 sp_connector_context_item_handler(SPEventContext *ec, SPItem *item, GdkEvent *event)
388     gint ret = FALSE;
390     SPDesktop *desktop = ec->desktop;
392     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(ec);
394     NR::Point p(event->button.x, event->button.y);
396     switch (event->type) {
397         case GDK_BUTTON_RELEASE:
398             if (event->button.button == 1) {
399                 if ((cc->state == SP_CONNECTOR_CONTEXT_DRAGGING) &&
400                         (connector_within_tolerance))
401                 {
402                     spcc_reset_colors(cc);
403                     cc->state = SP_CONNECTOR_CONTEXT_IDLE;
404                 }
405                 if (cc->state != SP_CONNECTOR_CONTEXT_IDLE) {
406                     // Doing simething else like rerouting.
407                     break;
408                 }
409                 // find out clicked item, disregarding groups, honoring Alt
410                 SPItem *item_ungrouped = sp_event_context_find_item(desktop,
411                         p, event->button.state & GDK_MOD1_MASK, TRUE);
413                 if (event->button.state & GDK_SHIFT_MASK) {
414                     cc->selection->toggle(item_ungrouped);
415                 } else {
416                     cc->selection->set(item_ungrouped);
417                 }
418                 ret = TRUE;
419             }
420             break;
421         case GDK_ENTER_NOTIFY:
422         {
423             if (cc_item_is_shape(item)) {
424                 // This is a shape, so show connection point(s).
425                 if (!(cc->active_shape) ||
426                         // Don't show handle for another handle.
427                         (item != ((SPItem *) cc->connpthandle))) {
428                     cc_set_active_shape(cc, item);
429                 }
430             }
431             ret = TRUE;
432             break;
433         }
434         default:
435             break;
436     }
438     return ret;
442 gint
443 sp_connector_context_root_handler(SPEventContext *ec, GdkEvent *event)
445     SPConnectorContext *const cc = SP_CONNECTOR_CONTEXT(ec);
447     gint ret = FALSE;
449     switch (event->type) {
450         case GDK_BUTTON_PRESS:
451             ret = connector_handle_button_press(cc, event->button);
452             break;
454         case GDK_MOTION_NOTIFY:
455             ret = connector_handle_motion_notify(cc, event->motion);
456             break;
458         case GDK_BUTTON_RELEASE:
459             ret = connector_handle_button_release(cc, event->button);
460             break;
461         case GDK_KEY_PRESS:
462             ret = connector_handle_key_press(cc, get_group0_keyval (&event->key));
463             break;
465         default:
466             break;
467     }
469     if (!ret) {
470         gint (*const parent_root_handler)(SPEventContext *, GdkEvent *)
471             = ((SPEventContextClass *) parent_class)->root_handler;
472         if (parent_root_handler) {
473             ret = parent_root_handler(ec, event);
474         }
475     }
477     return ret;
481 static gint
482 connector_handle_button_press(SPConnectorContext *const cc, GdkEventButton const &bevent)
484     NR::Point const event_w(bevent.x, bevent.y);
485     /* Find desktop coordinates */
486     NR::Point p = cc->desktop->w2d(event_w);
488     gint ret = FALSE;
489     if ( bevent.button == 1 ) {
491         SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
493         if (Inkscape::have_viable_layer(desktop, cc->_message_context) == false) {
494             return TRUE;
495         }
496         
497         NR::Point const event_w(bevent.x,
498                                 bevent.y);
499         connector_drag_origin_w = event_w;
500         connector_within_tolerance = true;
502         NR::Point const event_dt = cc->desktop->w2d(event_w);
503         switch (cc->state) {
504             case SP_CONNECTOR_CONTEXT_STOP:
505                 /* This is allowed, if we just cancelled curve */
506             case SP_CONNECTOR_CONTEXT_IDLE:
507             {
508                 if ( cc->npoints == 0 ) {
509                     /* Set start anchor */
510                     NR::Point p;
511                         
512                     cc_clear_active_conn(cc);
514                     SP_EVENT_CONTEXT_DESKTOP(cc)->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating new connector"));
516                     /* Create green anchor */
517                     p = event_dt;
518                     
519                     // Test whether we clicked on a connection point
520                     cc->sid = conn_pt_handle_test(cc, p);
521                     
522                     if (!cc->sid) {
523                         // This is the first point, so just snap it to the grid
524                         // as there's no other points to go off.
525                         SnapManager const m(cc->desktop->namedview);
526                         p = m.freeSnap(Inkscape::Snapper::SNAP_POINT | Inkscape::Snapper::BBOX_POINT,
527                                        p, NULL).getPoint();
528                     }                        
529                     spcc_connector_set_initial_point(cc, p);
530                     
531                 }
532                 cc->state = SP_CONNECTOR_CONTEXT_DRAGGING;
533                 ret = TRUE;
534                 break;
535             }   
536             case SP_CONNECTOR_CONTEXT_DRAGGING:
537             {
538                 // This is the second click of a connector creation.
539                 
540                 spcc_connector_set_subsequent_point(cc, p);
541                 spcc_connector_finish_segment(cc, p);
542                 // Test whether we clicked on a connection point
543                 cc->eid = conn_pt_handle_test(cc, p);
544                 if (cc->npoints != 0) {
545                     spcc_connector_finish(cc);
546                 }
547                 cc_set_active_conn(cc, cc->newconn);
548                 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
549                 ret = TRUE;
550                 break;
551             }
552             case SP_CONNECTOR_CONTEXT_CLOSE:
553             {
554                 g_warning("Button down in CLOSE state");
555                 break;
556             }
557             default:
558                 break;
559         }
560     } else if (bevent.button == 3) {
561         if (cc->npoints != 0) {
562             spcc_connector_finish(cc);
563             ret = TRUE;
564         }
565     }
566     return ret;
570 static gint
571 connector_handle_motion_notify(SPConnectorContext *const cc, GdkEventMotion const &mevent)
573     gint ret = FALSE;
575     if (mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) {
576         // allow middle-button scrolling
577         return FALSE;
578     }
580     NR::Point const event_w(mevent.x, mevent.y);
582     if (connector_within_tolerance) {
583         gint const tolerance = prefs_get_int_attribute_limited("options.dragtolerance",
584                                                                "value", 0, 0, 100);
585         if ( NR::LInfty( event_w - connector_drag_origin_w ) < tolerance ) {
586             return FALSE;   // Do not drag if we're within tolerance from origin.
587         }
588     }
589     // Once the user has moved farther than tolerance from the original location
590     // (indicating they intend to move the object, not click), then always process
591     // the motion notify coordinates as given (no snapping back to origin)
592     connector_within_tolerance = false;
594     SPDesktop *const dt = cc->desktop;
596     /* Find desktop coordinates */
597     NR::Point p = dt->w2d(event_w);
599     switch (cc->state) {
600         case SP_CONNECTOR_CONTEXT_DRAGGING:
601         {
602             // This is movement during a connector creation.
603             
604             if ( cc->npoints > 0 ) {
605                 cc->selection->clear();
606                 spcc_connector_set_subsequent_point(cc, p);
607                 ret = TRUE;
608             }
609             break;
610         }
611         case SP_CONNECTOR_CONTEXT_REROUTING:
612         {
613             g_assert( SP_IS_PATH(cc->clickeditem));
615             // Update the hidden path
616             NR::Matrix i2d = sp_item_i2d_affine(cc->clickeditem);
617             NR::Matrix d2i = i2d.inverse();
618             SPPath *path = SP_PATH(cc->clickeditem);
619             SPCurve *curve = (SP_SHAPE(path))->curve;
620             if (cc->clickedhandle == cc->endpt_handle[0]) {
621                 NR::Point o = cc->endpt_handle[1]->pos;
622                 sp_curve_stretch_endpoints(curve, p * d2i, o * d2i);
623             }
624             else {
625                 NR::Point o = cc->endpt_handle[0]->pos;
626                 sp_curve_stretch_endpoints(curve, o * d2i, p * d2i);
627             }
628             sp_conn_adjust_path(path);
630             // Copy this to the temporary visible path
631             cc->red_curve = sp_curve_copy(SP_SHAPE(path)->curve);
632             sp_curve_transform(cc->red_curve, i2d);
634             sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), cc->red_curve);
635             ret = TRUE;
636             break;
637         }
638         case SP_CONNECTOR_CONTEXT_STOP:
639             /* This is perfectly valid */
640             break;
641         default:
642             break;
643     }
645     return ret;
649 static gint
650 connector_handle_button_release(SPConnectorContext *const cc, GdkEventButton const &revent)
652     gint ret = FALSE;
653     if ( revent.button == 1 ) {
655         SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
656         SPDocument *doc = SP_DT_DOCUMENT(desktop);
658         NR::Point const event_w(revent.x, revent.y);
660         /* Find desktop coordinates */
661         NR::Point p = cc->desktop->w2d(event_w);
663         switch (cc->state) {
664             //case SP_CONNECTOR_CONTEXT_POINT:
665             case SP_CONNECTOR_CONTEXT_DRAGGING:
666             {
667                 if (connector_within_tolerance)
668                 {
669                     spcc_connector_finish_segment(cc, p);
670                     return TRUE;
671                 }
672                 // Connector has been created via a drag, end it now.
673                 spcc_connector_set_subsequent_point(cc, p);
674                 spcc_connector_finish_segment(cc, p);
675                 // Test whether we clicked on a connection point
676                 cc->eid = conn_pt_handle_test(cc, p);
677                 if (cc->npoints != 0) {
678                     spcc_connector_finish(cc);
679                 }
680                 cc_set_active_conn(cc, cc->newconn);
681                 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
682                 break;
683             }
684             case SP_CONNECTOR_CONTEXT_REROUTING:
685             {
686                 // Clear the temporary path:
687                 sp_curve_reset(cc->red_curve);
688                 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
690                 // Test whether we clicked on a connection point
691                 gchar *shape_label = conn_pt_handle_test(cc, p);
693                 if (shape_label) {
694                     if (cc->clickedhandle == cc->endpt_handle[0]) {
695                         sp_object_setAttribute(cc->clickeditem,
696                                 "inkscape:connection-start",shape_label, false);
697                     }
698                     else {
699                         sp_object_setAttribute(cc->clickeditem,
700                                 "inkscape:connection-end",shape_label, false);
701                     }
702                     g_free(shape_label);
703                 }
704                 cc->clickeditem->setHidden(false);
705                 sp_conn_adjust_path(SP_PATH(cc->clickeditem));
706                 cc->clickeditem->updateRepr(); 
707                 sp_document_done(doc);
708                 cc_set_active_conn(cc, cc->clickeditem);
709                 sp_document_ensure_up_to_date(doc);
710                 cc->state = SP_CONNECTOR_CONTEXT_IDLE;
711                 return TRUE;
712                 break;
713             }
714             case SP_CONNECTOR_CONTEXT_STOP:
715                 /* This is allowed, if we just cancelled curve */
716                 break;
717             default:
718                 break;
719         }
720         ret = TRUE;
721     }
723     return ret;
727 static gint
728 connector_handle_key_press(SPConnectorContext *const cc, guint const keyval)
730     gint ret = FALSE;
731     /* fixme: */
732     switch (keyval) {
733         case GDK_Return:
734         case GDK_KP_Enter:
735             if (cc->npoints != 0) {
736                 spcc_connector_finish(cc);
737                 ret = TRUE;
738             }
739             break;
740         case GDK_Escape:
741             if (cc->npoints != 0) {
742                 // if drawing, cancel, otherwise pass it up for deselecting
743                 cc->state = SP_CONNECTOR_CONTEXT_STOP;
744                 spcc_reset_colors(cc);
745                 ret = TRUE;
746             }
747             break;
748         default:
749             break;
750     }
751     return ret;
755 static void
756 spcc_reset_colors(SPConnectorContext *cc)
758     /* Red */
759     sp_curve_reset(cc->red_curve);
760     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
761     
762     sp_curve_reset(cc->green_curve);
763     cc->npoints = 0;
767 static void
768 spcc_connector_set_initial_point(SPConnectorContext *const cc, NR::Point const p)
770     g_assert( cc->npoints == 0 );
772     cc->p[0] = p;
773     cc->p[1] = p;
774     cc->npoints = 2;
775     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
779 static void
780 spcc_connector_set_subsequent_point(SPConnectorContext *const cc, NR::Point const p)
782     g_assert( cc->npoints != 0 );
784     SPDesktop *dt = cc->desktop;
785     NR::Point o = dt->dt2doc(cc->p[0]);
786     NR::Point d = dt->dt2doc(p);
787     Avoid::Point src = { o[NR::X], o[NR::Y] };
788     Avoid::Point dst = { d[NR::X], d[NR::Y] };
790     if (!cc->newConnRef) {
791         cc->newConnRef = new Avoid::ConnRef(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_DT_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;
943     
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);
985             
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;
1018                 
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);
1022               
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 );
1056    
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);
1063         
1064         sp_repr_remove_listener_by_data(cc->active_shape_layer_repr, cc);
1065         Inkscape::GC::release(cc->active_shape_layer_repr);
1066     }
1067     
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);
1073     
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     }
1079  
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_DT_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);
1108         
1109         cc->connpthandle = knot;
1110     }
1111     
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);
1118     
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);
1129     
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);
1135         
1136         NR::Point endpt = sp_curve_last_point(curve) * i2d;
1137         sp_knot_set_position(cc->endpt_handle[1], &endpt, 0);
1138         
1139         return;
1140     }
1141     
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     }
1150     
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_DT_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         }
1191     
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     }
1207     
1208     NR::Point startpt = sp_curve_first_point(curve) * i2d;
1209     sp_knot_set_position(cc->endpt_handle[0], &startpt, 0);
1210     
1211     NR::Point endpt = sp_curve_last_point(curve) * i2d;
1212     sp_knot_set_position(cc->endpt_handle[1], &endpt, 0);
1213     
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;
1243     
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_DT_DOCUMENT(desktop);
1253     Inkscape::Selection *selection = SP_DT_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;
1263         
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         }
1270         
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);
1282     
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();
1291     
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     }
1302     
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);
1330   
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 :