Code

NR::Maybe => boost::optional
[inkscape.git] / src / connector-context.cpp
index 7e8058eee5c986744049567d97b91360ffabe2ea..5e2cdc2b80612d7dbd36a44a684f76c2f50e210a 100644 (file)
@@ -42,6 +42,8 @@
  */
 
 #include <gdk/gdkkeysyms.h>
+#include <string>
+#include <cstring>
 
 #include "connector-context.h"
 #include "pixmaps/cursor-connector.xpm"
@@ -71,7 +73,7 @@
 #include "sp-namedview.h"
 #include "sp-text.h"
 #include "sp-flowtext.h"
-
+#include "display/curve.h"
 
 static void sp_connector_context_class_init(SPConnectorContextClass *klass);
 static void sp_connector_context_init(SPConnectorContext *conn_context);
@@ -273,10 +275,10 @@ sp_connector_context_setup(SPEventContext *ec)
     sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(cc->red_bpath), 0x00000000,
             SP_WIND_RULE_NONZERO);
     /* Create red curve */
-    cc->red_curve = sp_curve_new_sized(4);
+    cc->red_curve = new SPCurve();
 
     /* Create green curve */
-    cc->green_curve = sp_curve_new_sized(64);
+    cc->green_curve = new SPCurve();
 
     // Notice the initial selection.
     cc_selection_changed(cc->selection, (gpointer) cc);
@@ -528,9 +530,9 @@ connector_handle_button_press(SPConnectorContext *const cc, GdkEventButton const
                     if (!cc->sid) {
                         // This is the first point, so just snap it to the grid
                         // as there's no other points to go off.
-                        SnapManager const &m = cc->desktop->namedview->snap_manager;
-                        p = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE | Inkscape::Snapper::SNAPPOINT_BBOX,
-                                       p, NULL).getPoint();
+                        SnapManager &m = cc->desktop->namedview->snap_manager;
+                        m.setup(cc->desktop);
+                        m.freeSnapReturnByRef(Inkscape::Snapper::SNAPPOINT_NODE, p);
                     }
                     spcc_connector_set_initial_point(cc, p);
 
@@ -611,7 +613,7 @@ connector_handle_motion_notify(SPConnectorContext *const cc, GdkEventMotion cons
     SPDesktop *const dt = cc->desktop;
 
     /* Find desktop coordinates */
-    NR::Point p = dt->w2d(event_w);
+    Geom::Point p = to_2geom(dt->w2d(event_w));
 
     switch (cc->state) {
         case SP_CONNECTOR_CONTEXT_DRAGGING:
@@ -620,7 +622,7 @@ connector_handle_motion_notify(SPConnectorContext *const cc, GdkEventMotion cons
 
             if ( cc->npoints > 0 ) {
                 cc->selection->clear();
-                spcc_connector_set_subsequent_point(cc, p);
+                spcc_connector_set_subsequent_point(cc, from_2geom(p));
                 ret = TRUE;
             }
             break;
@@ -630,23 +632,23 @@ connector_handle_motion_notify(SPConnectorContext *const cc, GdkEventMotion cons
             g_assert( SP_IS_PATH(cc->clickeditem));
 
             // Update the hidden path
-            NR::Matrix i2d = sp_item_i2d_affine(cc->clickeditem);
-            NR::Matrix d2i = i2d.inverse();
+            Geom::Matrix i2d = sp_item_i2d_affine(cc->clickeditem);
+            Geom::Matrix d2i = i2d.inverse();
             SPPath *path = SP_PATH(cc->clickeditem);
             SPCurve *curve = (SP_SHAPE(path))->curve;
             if (cc->clickedhandle == cc->endpt_handle[0]) {
-                NR::Point o = cc->endpt_handle[1]->pos;
-                sp_curve_stretch_endpoints(curve, p * d2i, o * d2i);
+                Geom::Point o = to_2geom(cc->endpt_handle[1]->pos);
+                curve->stretch_endpoints(p * d2i, o * d2i);
             }
             else {
-                NR::Point o = cc->endpt_handle[0]->pos;
-                sp_curve_stretch_endpoints(curve, o * d2i, p * d2i);
+                Geom::Point o = to_2geom(cc->endpt_handle[0]->pos);
+                curve->stretch_endpoints(o * d2i, p * d2i);
             }
             sp_conn_adjust_path(path);
 
             // Copy this to the temporary visible path
-            cc->red_curve = sp_curve_copy(SP_SHAPE(path)->curve);
-            sp_curve_transform(cc->red_curve, i2d);
+            cc->red_curve = SP_SHAPE(path)->curve->copy();
+            cc->red_curve->transform(i2d);
 
             sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), cc->red_curve);
             ret = TRUE;
@@ -770,7 +772,7 @@ cc_connector_rerouting_finish(SPConnectorContext *const cc, NR::Point *const p)
     SPDocument *doc = sp_desktop_document(desktop);
     
     // Clear the temporary path:
-    sp_curve_reset(cc->red_curve);
+    cc->red_curve->reset();
     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
 
     if (p != NULL)
@@ -803,10 +805,10 @@ static void
 spcc_reset_colors(SPConnectorContext *cc)
 {
     /* Red */
-    sp_curve_reset(cc->red_curve);
+    cc->red_curve->reset();
     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
 
-    sp_curve_reset(cc->green_curve);
+    cc->green_curve->reset();
     cc->npoints = 0;
 }
 
@@ -847,15 +849,15 @@ spcc_connector_set_subsequent_point(SPConnectorContext *const cc, NR::Point cons
     Avoid::PolyLine route = cc->newConnRef->route();
     cc->newConnRef->calcRouteDist();
 
-    sp_curve_reset(cc->red_curve);
+    cc->red_curve->reset();
     NR::Point pt(route.ps[0].x, route.ps[0].y);
-    sp_curve_moveto(cc->red_curve, pt);
+    cc->red_curve->moveto(pt);
 
     for (int i = 1; i < route.pn; ++i) {
         NR::Point p(route.ps[i].x, route.ps[i].y);
-        sp_curve_lineto(cc->red_curve, p);
+        cc->red_curve->lineto(p);
     }
-    sp_curve_transform(cc->red_curve, dt->doc2dt());
+    cc->red_curve->transform(to_2geom(dt->doc2dt()));
     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), cc->red_curve);
 }
 
@@ -869,19 +871,19 @@ static void
 spcc_concat_colors_and_flush(SPConnectorContext *cc)
 {
     SPCurve *c = cc->green_curve;
-    cc->green_curve = sp_curve_new_sized(64);
+    cc->green_curve = new SPCurve();
 
-    sp_curve_reset(cc->red_curve);
+    cc->red_curve->reset();
     sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), NULL);
 
-    if (sp_curve_empty(c)) {
-        sp_curve_unref(c);
+    if (c->is_empty()) {
+        c->unref();
         return;
     }
 
     spcc_flush_white(cc, c);
 
-    sp_curve_unref(c);
+    c->unref();
 }
 
 
@@ -900,27 +902,26 @@ spcc_flush_white(SPConnectorContext *cc, SPCurve *gc)
 
     if (gc) {
         c = gc;
-        sp_curve_ref(c);
+        c->ref();
     } else {
         return;
     }
 
     /* Now we have to go back to item coordinates at last */
-    sp_curve_transform(c,
-            sp_desktop_dt2root_affine(SP_EVENT_CONTEXT_DESKTOP(cc)));
+    c->transform(to_2geom(sp_desktop_dt2root_affine(SP_EVENT_CONTEXT_DESKTOP(cc))));
 
     SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(cc);
     SPDocument *doc = sp_desktop_document(desktop);
     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
 
-    if ( c && !sp_curve_empty(c) ) {
+    if ( c && !c->is_empty() ) {
         /* We actually have something to write */
 
         Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
         /* Set style */
         sp_desktop_apply_style_tool(desktop, repr, "tools.connector", false);
 
-        gchar *str = sp_svg_write_path(SP_CURVE_BPATH(c));
+        gchar *str = sp_svg_write_path( c->get_pathvector() );
         g_assert( str != NULL );
         repr->setAttribute("d", str);
         g_free(str);
@@ -929,7 +930,7 @@ spcc_flush_white(SPConnectorContext *cc, SPCurve *gc)
         cc->newconn = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
         cc->selection->set(repr);
         Inkscape::GC::release(repr);
-        cc->newconn->transform = i2i_affine(desktop->currentRoot(), desktop->currentLayer());
+        cc->newconn->transform = from_2geom(i2i_affine(desktop->currentRoot(), desktop->currentLayer()));
         cc->newconn->updateRepr();
 
         bool connection = false;
@@ -956,7 +957,7 @@ spcc_flush_white(SPConnectorContext *cc, SPCurve *gc)
         cc->newconn->updateRepr();
     }
 
-    sp_curve_unref(c);
+    c->unref();
 
     /* Flush pending updates */
     sp_document_done(doc, SP_VERB_CONTEXT_CONNECTOR, _("Create connector"));
@@ -965,16 +966,16 @@ spcc_flush_white(SPConnectorContext *cc, SPCurve *gc)
 
 
 static void
-spcc_connector_finish_segment(SPConnectorContext *const cc, NR::Point const p)
+spcc_connector_finish_segment(SPConnectorContext *const cc, NR::Point const /*p*/)
 {
-    if (!sp_curve_empty(cc->red_curve)) {
-        sp_curve_append_continuous(cc->green_curve, cc->red_curve, 0.0625);
+    if (!cc->red_curve->is_empty()) {
+        cc->green_curve->append_continuous(cc->red_curve, 0.0625);
 
         cc->p[0] = cc->p[3];
         cc->p[1] = cc->p[4];
         cc->npoints = 2;
 
-        sp_curve_reset(cc->red_curve);
+        cc->red_curve->reset();
     }
 }
 
@@ -985,7 +986,7 @@ spcc_connector_finish(SPConnectorContext *const cc)
     SPDesktop *const desktop = cc->desktop;
     desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Finishing connector"));
 
-    sp_curve_reset(cc->red_curve);
+    cc->red_curve->reset();
     spcc_concat_colors_and_flush(cc);
 
     cc->npoints = 0;
@@ -1047,7 +1048,7 @@ cc_generic_knot_handler(SPCanvasItem *, GdkEvent *event, SPKnot *knot)
 
 
 static gboolean
-endpt_handler(SPKnot *knot, GdkEvent *event, SPConnectorContext *cc)
+endpt_handler(SPKnot */*knot*/, GdkEvent *event, SPConnectorContext *cc)
 {
     g_assert( SP_IS_CONNECTOR_CONTEXT(cc) );
 
@@ -1076,11 +1077,10 @@ endpt_handler(SPKnot *knot, GdkEvent *event, SPConnectorContext *cc)
                 }
 
                 // Show the red path for dragging.
-                cc->red_curve = sp_curve_copy(SP_PATH(cc->clickeditem)->curve);
-                NR::Matrix i2d = sp_item_i2d_affine(cc->clickeditem);
-                sp_curve_transform(cc->red_curve, i2d);
-                sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath),
-                        cc->red_curve);
+                cc->red_curve = SP_PATH(cc->clickeditem)->curve->copy();
+                Geom::Matrix i2d = sp_item_i2d_affine(cc->clickeditem);
+                cc->red_curve->transform(i2d);
+                sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(cc->red_bpath), cc->red_curve);
 
                 cc->clickeditem->setHidden(true);
 
@@ -1148,10 +1148,10 @@ static void cc_set_active_shape(SPConnectorContext *cc, SPItem *item)
     }
 
 
-    NR::Maybe<NR::Rect> bbox = sp_item_bbox_desktop(cc->active_shape);
+    boost::optional<NR::Rect> bbox = sp_item_bbox_desktop(cc->active_shape);
     if (bbox) {
         NR::Point center = bbox->midpoint();
-        sp_knot_set_position(cc->connpthandle, &center, 0);
+        sp_knot_set_position(cc->connpthandle, center, 0);
         sp_knot_show(cc->connpthandle);
     } else {
         sp_knot_hide(cc->connpthandle);
@@ -1165,16 +1165,16 @@ cc_set_active_conn(SPConnectorContext *cc, SPItem *item)
     g_assert( SP_IS_PATH(item) );
 
     SPCurve *curve = SP_SHAPE(SP_PATH(item))->curve;
-    NR::Matrix i2d = sp_item_i2d_affine(item);
+    NR::Matrix i2d = from_2geom(sp_item_i2d_affine(item));
 
     if (cc->active_conn == item)
     {
         // Just adjust handle positions.
-        NR::Point startpt = sp_curve_first_point(curve) * i2d;
-        sp_knot_set_position(cc->endpt_handle[0], &startpt, 0);
+        NR::Point startpt = curve->first_point() * i2d;
+        sp_knot_set_position(cc->endpt_handle[0], startpt, 0);
 
-        NR::Point endpt = sp_curve_last_point(curve) * i2d;
-        sp_knot_set_position(cc->endpt_handle[1], &endpt, 0);
+        NR::Point endpt = curve->last_point() * i2d;
+        sp_knot_set_position(cc->endpt_handle[1], endpt, 0);
 
         return;
     }
@@ -1237,11 +1237,11 @@ cc_set_active_conn(SPConnectorContext *cc, SPItem *item)
                 G_CALLBACK(endpt_handler), cc);
     }
 
-    NR::Point startpt = sp_curve_first_point(curve) * i2d;
-    sp_knot_set_position(cc->endpt_handle[0], &startpt, 0);
+    NR::Point startpt = curve->first_point() * i2d;
+    sp_knot_set_position(cc->endpt_handle[0], startpt, 0);
 
-    NR::Point endpt = sp_curve_last_point(curve) * i2d;
-    sp_knot_set_position(cc->endpt_handle[1], &endpt, 0);
+    NR::Point endpt = curve->last_point() * i2d;
+    sp_knot_set_position(cc->endpt_handle[1], endpt, 0);
 
     sp_knot_show(cc->endpt_handle[0]);
     sp_knot_show(cc->endpt_handle[1]);
@@ -1252,7 +1252,7 @@ static bool cc_item_is_shape(SPItem *item)
 {
     if (SP_IS_PATH(item)) {
         SPCurve *curve = (SP_SHAPE(item))->curve;
-        if ( curve && !(curve->closed) ) {
+        if ( curve && !(curve->is_closed()) ) {
             // Open paths are connectors.
             return false;
         }
@@ -1271,7 +1271,7 @@ bool cc_item_is_connector(SPItem *item)
 {
     if (SP_IS_PATH(item)) {
         if (SP_PATH(item)->connEndPair.isAutoRoutingConn()) {
-            g_assert( !(SP_SHAPE(item)->curve->closed) );
+            g_assert( !(SP_SHAPE(item)->curve->is_closed()) );
             return true;
         }
     }
@@ -1348,8 +1348,8 @@ cc_selection_changed(Inkscape::Selection *selection, gpointer data)
 
 
 static void
-shape_event_attr_deleted(Inkscape::XML::Node *repr, Inkscape::XML::Node *child,
-        Inkscape::XML::Node *ref, gpointer data)
+shape_event_attr_deleted(Inkscape::XML::Node */*repr*/, Inkscape::XML::Node *child,
+                         Inkscape::XML::Node */*ref*/, gpointer data)
 {
     g_assert(data);
     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(data);
@@ -1363,8 +1363,8 @@ shape_event_attr_deleted(Inkscape::XML::Node *repr, Inkscape::XML::Node *child,
 
 static void
 shape_event_attr_changed(Inkscape::XML::Node *repr, gchar const *name,
-                            gchar const *old_value, gchar const *new_value,
-                            bool is_interactive, gpointer data)
+                         gchar const */*old_value*/, gchar const */*new_value*/,
+                         bool /*is_interactive*/, gpointer data)
 {
     g_assert(data);
     SPConnectorContext *cc = SP_CONNECTOR_CONTEXT(data);