Code

allow multiple (balanced) calls to add and remove document
authormental <mental@users.sourceforge.net>
Fri, 23 Mar 2007 05:52:15 +0000 (05:52 +0000)
committermental <mental@users.sourceforge.net>
Fri, 23 Mar 2007 05:52:15 +0000 (05:52 +0000)
src/application/editor.cpp
src/application/editor.h
src/document.cpp
src/inkscape.cpp
src/ui/view/view.cpp

index 1cc45f3c7ebf5388e55d32fe5ca83aed82b0a418..56984dcbb99095d85b1c65e4ecf0221bffe198ba 100644 (file)
@@ -109,15 +109,19 @@ Editor::getActiveDocument()
 void
 Editor::addDocument (SPDocument *doc)
 {
-    g_assert (!g_slist_find (_instance->_documents, doc));
-    _instance->_documents = g_slist_append (_instance->_documents, doc);
+    if ( _instance->_document_set.find(doc) == _instance->_document_set.end() ) {
+        _instance->_documents = g_slist_append (_instance->_documents, doc);
+    }
+    _instance->_document_set.insert(doc);
 }
 
 void
 Editor::removeDocument (SPDocument *doc)
 {
-    g_assert (g_slist_find (_instance->_documents, doc));
-    _instance->_documents = g_slist_remove (_instance->_documents, doc);
+    _instance->_document_set.erase(doc);
+    if ( _instance->_document_set.find(doc) == _instance->_document_set.end() ) {
+        _instance->_documents = g_slist_remove (_instance->_documents, doc);
+    }
 }
 
 SPDesktop* 
index 9d2ce493c69bbba73fbb6b080b1a9e6d19ba1807..a6fe66688957af47f46b10ab16c512c250f2f818 100644 (file)
@@ -19,6 +19,7 @@
 #include <sigc++/sigc++.h>
 #include <glib/gslist.h>
 #include <glibmm/ustring.h>
+#include <set>
 #include "app-prototype.h"
 
 class SPDesktop;
@@ -96,6 +97,7 @@ protected:
     Editor(Editor const &);
     Editor& operator=(Editor const &);
 
+    std::multiset<SPDocument *> _document_set;
     GSList         *_documents;
     GSList         *_desktops;
     gchar          *_argv0;
index e66089fedfdcb6ee5a294c64fb1bc1e36bd4b0e1..4dd0f3d211321cf1bb7d1c88e8278327ee57d17b 100644 (file)
@@ -118,8 +118,6 @@ SPDocument::~SPDocument() {
     collectOrphans();
 
     if (priv) {
-        inkscape_remove_document(this);
-
         if (priv->partial) {
             sp_repr_free_log(priv->partial);
             priv->partial = NULL;
@@ -315,7 +313,6 @@ sp_document_create(Inkscape::XML::Document *rdoc,
         document->_selection_changed_connection = Inkscape::NSApplication::Editor::connectSelectionChanged (sigc::mem_fun (*document, &SPDocument::reset_key));
         document->_desktop_activated_connection = Inkscape::NSApplication::Editor::connectDesktopActivated (sigc::mem_fun (*document, &SPDocument::reset_key));
     }
-    inkscape_add_document(document);
 
     return document;
 }
@@ -402,7 +399,6 @@ sp_document_new_from_mem(gchar const *buffer, gint length, unsigned int keepaliv
 
 SPDocument *sp_document_new_dummy() {
     SPDocument *document = new SPDocument();
-    inkscape_add_document(document);
     return document;
 }
 
index be3ff1e7d0681de8322e13501c1762cc2bd8c46f..799227bf37c9b2bec2af6b934a6c02ab8d046f99 100644 (file)
@@ -18,6 +18,7 @@
 #endif
 
 
+#include <set>
 #include "debug/simple-event.h"
 #include "debug/event-tracker.h"
 
@@ -109,6 +110,7 @@ static bool inkscape_init_config (Inkscape::XML::Document *doc, const gchar *con
 struct Inkscape::Application {
     GObject object;
     Inkscape::XML::Document *menus;
+    std::multiset<SPDocument *> document_set;
     GSList *documents;
     GSList *desktops;
     gchar *argv0;
@@ -292,6 +294,8 @@ inkscape_init (SPObject * object)
         g_assert_not_reached ();
     }
 
+    new (&inkscape->document_set) std::multiset<SPDocument *>();
+
     inkscape->menus = sp_repr_read_mem (_(menus_skeleton), MENUS_SKELETON_SIZE, NULL);
 
     inkscape->documents = NULL;
@@ -321,6 +325,8 @@ inkscape_dispose (GObject *object)
         inkscape->menus = NULL;
     }
 
+    inkscape->document_set.~multiset();
+
     G_OBJECT_CLASS (parent_class)->dispose (object);
 
     gtk_main_quit ();
@@ -1112,8 +1118,11 @@ inkscape_add_document (SPDocument *document)
 
     if (!Inkscape::NSApplication::Application::getNewGui())
     {
-        g_assert (!g_slist_find (inkscape->documents, document));
-        inkscape->documents = g_slist_append (inkscape->documents, document);
+        if ( inkscape->document_set.find(document) != inkscape->document_set.end() ) {
+    
+            inkscape->documents = g_slist_append (inkscape->documents, document);
+        }
+        inkscape->document_set.insert(document);
     }
     else
     {
@@ -1130,8 +1139,10 @@ inkscape_remove_document (SPDocument *document)
 
     if (!Inkscape::NSApplication::Application::getNewGui())
     {
-        g_assert (g_slist_find (inkscape->documents, document));
-        inkscape->documents = g_slist_remove (inkscape->documents, document);
+        inkscape->document_set.erase(document);
+        if ( inkscape->document_set.find(document) == inkscape->document_set.end() ) {
+            inkscape->documents = g_slist_remove (inkscape->documents, document);
+        }
     }
     else
     {
index 04158ddbdd74595b759149661ca0839d0026bccf..6b6e0b8b629999b620fe30fa39504ff3552f2de1 100644 (file)
@@ -23,6 +23,7 @@
 #include "message-stack.h"
 #include "message-context.h"
 #include "verbs.h"
+#include "inkscape-private.h"
 
 namespace Inkscape {
 namespace UI {
@@ -83,6 +84,9 @@ View::View()
  */
 View::~View()
 {
+    if (_doc) {
+        inkscape_remove_document(_doc);
+    }
     _close();
 }
 
@@ -138,8 +142,11 @@ void View::setDocument(SPDocument *doc) {
     if (_doc) {
         _document_uri_set_connection.disconnect();
         _document_resized_connection.disconnect();
+        inkscape_remove_document(_doc);
     }
 
+    inkscape_add_document(doc);
+
     _doc = doc;
     _document_uri_set_connection = 
         _doc->connectURISet(sigc::bind(sigc::ptr_fun(&_onDocumentURISet), this));