Code

drop more missing files from autoconf
[inkscape.git] / src / conn-avoid-ref.cpp
index d7cca60f915f2e27de08e24f584fffb66c3f110c..540e8205fbbf191e093619c4e60e601c3faa5fa7 100644 (file)
@@ -15,7 +15,7 @@
 #include "conn-avoid-ref.h"
 #include "libnr/nr-rect-ops.h"
 #include "libavoid/polyutil.h"
-#include "libavoid/incremental.h"
+#include "libavoid/router.h"
 #include "libavoid/connector.h"
 #include "xml/simple-node.cpp"
 #include "document.h"
@@ -27,6 +27,8 @@
 #include "inkscape.h"
 
 
+using Avoid::Router;
+
 static Avoid::Polygn avoid_item_poly(SPItem const *item);
 
 
@@ -44,9 +46,10 @@ SPAvoidRef::~SPAvoidRef()
 {
     _transformed_connection.disconnect();
     if (shapeRef) {
+        Router *router = shapeRef->router();
         // shapeRef is finalised by delShape,
         // so no memory is lost here.
-        Avoid::delShape(shapeRef);
+        router->delShape(shapeRef);
         shapeRef = NULL;
     }
 }
@@ -71,12 +74,24 @@ void SPAvoidRef::handleSettingChange(void)
     if (desktop == NULL) {
         return;
     }
-    
+    if (sp_desktop_document(desktop) != item->document) {
+        // We don't want to go any further if the active desktop's document
+        // isn't the same as the document that this item is part of.  This
+        // case can happen if a new document is loaded from the file chooser
+        // or via the recent file menu.  In this case, we can end up here
+        // as a rersult of a sp_document_ensure_up_to_date performed on a
+        // document not yet attached to the active desktop.
+        return;
+    }
+
     if (new_setting == setting) {
         // Don't need to make any changes
         return;
     }
+    setting = new_setting;
 
+    Router *router = item->document->router;
+    
     _transformed_connection.disconnect();
     if (new_setting) {
         _transformed_connection = item->connectTransformed(
@@ -90,10 +105,10 @@ void SPAvoidRef::handleSettingChange(void)
             // Get a unique ID for the item.
             GQuark itemID = g_quark_from_string(id);
 
-            shapeRef = new Avoid::ShapeRef(itemID, poly);
+            shapeRef = new Avoid::ShapeRef(router, itemID, poly);
             Avoid::freePoly(poly);
         
-            Avoid::addShape(shapeRef);
+            router->addShape(shapeRef);
         }
     }
     else
@@ -102,10 +117,33 @@ void SPAvoidRef::handleSettingChange(void)
         
         // shapeRef is finalised by delShape,
         // so no memory is lost here.
-        Avoid::delShape(shapeRef);
+        router->delShape(shapeRef);
         shapeRef = NULL;
     }
-    setting = new_setting;
+}
+
+
+GSList *SPAvoidRef::getAttachedShapes(const unsigned int type)
+{
+    GSList *list = NULL;
+
+    Avoid::IntList shapes;
+    GQuark shapeId = g_quark_from_string(item->id);
+    item->document->router->attachedShapes(shapes, shapeId, type);
+    
+    Avoid::IntList::iterator finish = shapes.end();
+    for (Avoid::IntList::iterator i = shapes.begin(); i != finish; ++i) {
+        const gchar *connId = g_quark_to_string(*i);
+        SPObject *obj = item->document->getObjectById(connId);
+        if (obj == NULL) {
+            g_warning("getAttachedShapes: Object with id=\"%s\" is not "
+                    "found. Skipping.", connId);
+            continue;
+        }
+        SPItem *shapeItem = SP_ITEM(obj);
+        list = g_slist_prepend(list, shapeItem);
+    }
+    return list;
 }
 
 
@@ -115,14 +153,19 @@ GSList *SPAvoidRef::getAttachedConnectors(const unsigned int type)
 
     Avoid::IntList conns;
     GQuark shapeId = g_quark_from_string(item->id);
-    Avoid::attachedToShape(conns, shapeId, type);
+    item->document->router->attachedConns(conns, shapeId, type);
     
     Avoid::IntList::iterator finish = conns.end();
     for (Avoid::IntList::iterator i = conns.begin(); i != finish; ++i) {
         const gchar *connId = g_quark_to_string(*i);
-        SPItem *citem = SP_ITEM(item->document->getObjectById(connId));
-        g_assert(citem != NULL);
-        list = g_slist_prepend(list, citem);
+        SPObject *obj = item->document->getObjectById(connId);
+        if (obj == NULL) {
+            g_warning("getAttachedConnectors: Object with id=\"%s\" is not "
+                    "found. Skipping.", connId);
+            continue;
+        }
+        SPItem *connItem = SP_ITEM(obj);
+        list = g_slist_prepend(list, connItem);
     }
     return list;
 }
@@ -143,18 +186,20 @@ static Avoid::Polygn avoid_item_poly(SPItem const *item)
     //       some convex hull code, though not NR::ConvexHull as this
     //       only keeps the bounding box of the convex hull currently.
 
-    // TODO: SPItem::invokeBbox gives the wrong result for some objects
+    // TODO: SPItem::getBounds gives the wrong result for some objects
     //       that have internal representations that are updated later
     //       by the sp_*_update functions, e.g., text.
     sp_document_ensure_up_to_date(item->document);
     
-    NR::Rect rHull = item->invokeBbox(sp_item_i2doc_affine(item));
-
+    NR::Maybe<NR::Rect> rHull = item->getBounds(sp_item_i2doc_affine(item));
+    if (!rHull) {
+        return Avoid::newPoly(0);
+    }
 
     double spacing = desktop->namedview->connector_spacing;
 
     // Add a little buffer around the edge of each object.
-    NR::Rect rExpandedHull = NR::expand(rHull, -spacing); 
+    NR::Rect rExpandedHull = NR::expand(*rHull, -spacing); 
     poly = Avoid::newPoly(4);
 
     for (unsigned n = 0; n < 4; ++n) {
@@ -216,10 +261,10 @@ void avoid_item_move(NR::Matrix const *mp, SPItem *moved_item)
     Avoid::ShapeRef *shapeRef = moved_item->avoidRef->shapeRef;
     g_assert(shapeRef);
 
+    Router *router = moved_item->document->router;
     Avoid::Polygn poly = avoid_item_poly(moved_item);
     if (poly.pn > 0) {
-        // moveShape actually destroys the old shapeRef and returns a new one.
-        moved_item->avoidRef->shapeRef = Avoid::moveShape(shapeRef, &poly);
+        router->moveShape(shapeRef, &poly);
         Avoid::freePoly(poly);
     }
 }
@@ -229,9 +274,9 @@ void init_avoided_shape_geometry(SPDesktop *desktop)
 {
     // Don't count this as changes to the document,
     // it is basically just llate initialisation.
-    SPDocument *document = SP_DT_DOCUMENT(desktop);
-    gboolean saved = sp_document_get_undo_sensitive(document);
-    sp_document_set_undo_sensitive(document, FALSE);
+    SPDocument *document = sp_desktop_document(desktop);
+    bool saved = sp_document_get_undo_sensitive(document);
+    sp_document_set_undo_sensitive(document, false);
     
     bool initialised = false;
     GSList *items = get_avoided_items(NULL, desktop->currentRoot(), desktop,