Code

r11516@tres: ted | 2006-04-26 21:30:18 -0700
[inkscape.git] / src / document.cpp
index f69c480f75e4a3f108cfdc9ce792283d166a3d4f..71f607b6524dcdc2e88a1c974c565243c1d830fc 100644 (file)
@@ -18,7 +18,7 @@
 /** \class SPDocument
  * SPDocument serves as the container of both model trees (agnostic XML
  * and typed object tree), and implements all of the document-level
- * functionality used by the program. Many document level operations, like 
+ * functionality used by the program. Many document level operations, like
  * load, save, print, export and so on, use SPDocument as their basic datatype.
  *
  * SPDocument implements undo and redo stacks and an id-based object
@@ -50,6 +50,9 @@
 #include "dir-util.h"
 #include "unit-constants.h"
 #include "prefs-utils.h"
+#include "libavoid/router.h"
+#include "libnr/nr-rect.h"
+#include "sp-item-group.h"
 
 #include "display/nr-arena-item.h"
 
@@ -86,6 +89,9 @@ SPDocument::SPDocument() {
 
     _collection_queue = NULL;
 
+    // Initialise instance of connector router.
+    router = new Avoid::Router();
+
     p = new SPDocumentPrivate();
 
     p->iddef = g_hash_table_new(g_direct_hash, g_direct_equal);
@@ -118,7 +124,7 @@ SPDocument::~SPDocument() {
 
         if (root) {
             sp_object_invoke_release(root);
-            g_object_unref(G_OBJECT(root));
+            sp_object_unref(root);
             root = NULL;
         }
 
@@ -164,6 +170,11 @@ SPDocument::~SPDocument() {
         keepalive = FALSE;
     }
 
+    if (router) {
+        delete router;
+        router = NULL;
+    }
+
     //delete this->_whiteboard_session_manager;
 }
 
@@ -192,7 +203,7 @@ void SPDocument::reset_key (void *dummy)
 {
     actionkey = NULL;
 }
-    
+
 static SPDocument *
 sp_document_create(Inkscape::XML::Document *rdoc,
                    gchar const *uri,
@@ -307,7 +318,7 @@ sp_document_create(Inkscape::XML::Document *rdoc,
 }
 
 /**
- * Fetches document from URI, or creates new, if NULL; public document 
+ * Fetches document from URI, or creates new, if NULL; public document
  * appears in document list.
  */
 SPDocument *
@@ -471,6 +482,26 @@ gdouble sp_document_height(SPDocument *document)
     return SP_ROOT(document->root)->height.computed;
 }
 
+/**
+ * Given an NRRect that may, for example, correspond to the bbox of an object
+ * this function fits the canvas to that rect by resizing the canvas
+ * and translating the document root into position.
+ */
+void SPDocument::fitToRect(NRRect const & rect)
+{
+    g_return_if_fail(!empty(rect));
+    
+    gdouble w = rect.x1 - rect.x0;
+    gdouble h = rect.y1 - rect.y0;
+    gdouble old_height = sp_document_height(this);
+    SPUnit unit = sp_unit_get_by_id(SP_UNIT_PX);
+    sp_document_set_width(this, w, &unit);
+    sp_document_set_height(this, h, &unit);
+
+    NR::translate tr = NR::translate::translate(-rect.x0,-(rect.y0 + (h - old_height)));
+    static_cast<SPGroup *>(root)->translateChildItems(tr);
+}
+
 void sp_document_set_uri(SPDocument *document, gchar const *uri)
 {
     g_return_if_fail(document != NULL);
@@ -642,6 +673,35 @@ SPObject *SPDocument::getObjectByRepr(Inkscape::XML::Node *repr) {
     return (SPObject*)g_hash_table_lookup(priv->reprdef, repr);
 }
 
+Glib::ustring SPDocument::getLanguage() {
+    gchar const *document_language = rdf_get_work_entity(this, rdf_find_entity("language"));
+    if (document_language) {
+        while (isspace(*document_language))
+            document_language++;
+    }
+    if ( !document_language || 0 == *document_language) {
+        // retrieve system language
+        document_language = getenv("LC_ALL");
+        if ( NULL == document_language || *document_language == 0 ) {
+            document_language = getenv ("LC_MESSAGES");
+        }
+        if ( NULL == document_language || *document_language == 0 ) {
+            document_language = getenv ("LANG");
+        }
+        
+        if ( NULL != document_language ) {
+            gchar *pos = strchr(document_language, '_');
+            if ( NULL != pos ) {
+                return Glib::ustring(document_language, pos - document_language);
+            }
+        }
+    }
+
+    if ( NULL == document_language )
+        return Glib::ustring();
+    return document_language;
+}
+
 /* Object modification root handler */
 
 void
@@ -986,6 +1046,9 @@ sp_document_remove_resource(SPDocument *document, gchar const *key, SPObject *ob
     g_return_val_if_fail(object != NULL, FALSE);
     g_return_val_if_fail(SP_IS_OBJECT(object), FALSE);
 
+    if (SP_OBJECT_IS_CLONED(object))
+        return FALSE;
+
     rlist = (GSList*)g_hash_table_lookup(document->priv->resources, key);
     g_return_val_if_fail(rlist != NULL, FALSE);
     g_return_val_if_fail(g_slist_find(rlist, object), FALSE);