eebc50a98891c1cfeebdc2ec4a35cc57af13e781
1 #define __SP_DOCUMENT_C__
3 /** \file
4 * SPDocument manipulation
5 *
6 * Authors:
7 * Lauris Kaplinski <lauris@kaplinski.com>
8 * MenTaLguY <mental@rydia.net>
9 * bulia byak <buliabyak@users.sf.net>
10 *
11 * Copyright (C) 2004-2005 MenTaLguY
12 * Copyright (C) 1999-2002 Lauris Kaplinski
13 * Copyright (C) 2000-2001 Ximian, Inc.
14 *
15 * Released under GNU GPL, read the file 'COPYING' for more information
16 */
18 /** \class SPDocument
19 * SPDocument serves as the container of both model trees (agnostic XML
20 * and typed object tree), and implements all of the document-level
21 * functionality used by the program. Many document level operations, like
22 * load, save, print, export and so on, use SPDocument as their basic datatype.
23 *
24 * SPDocument implements undo and redo stacks and an id-based object
25 * dictionary. Thanks to unique id attributes, the latter can be used to
26 * map from the XML tree back to the object tree.
27 *
28 * SPDocument performs the basic operations needed for asynchronous
29 * update notification (SPObject ::modified virtual method), and implements
30 * the 'modified' signal, as well.
31 */
34 #define noSP_DOCUMENT_DEBUG_IDLE
35 #define noSP_DOCUMENT_DEBUG_UNDO
37 #ifdef HAVE_CONFIG_H
38 # include "config.h"
39 #endif
40 #include <gtk/gtkmain.h>
41 #include <string>
42 #include <cstring>
44 #include "application/application.h"
45 #include "application/editor.h"
46 #include "desktop.h"
47 #include "dir-util.h"
48 #include "display/nr-arena-item.h"
49 #include "document-private.h"
50 #include "helper/units.h"
51 #include "inkscape-private.h"
52 #include "inkscape-version.h"
53 #include "libavoid/router.h"
54 #include "persp3d.h"
55 #include "preferences.h"
56 #include "profile-manager.h"
57 #include "rdf.h"
58 #include "sp-item-group.h"
59 #include "sp-namedview.h"
60 #include "sp-object-repr.h"
61 #include "transf_mat_3x4.h"
62 #include "unit-constants.h"
63 #include "xml/repr.h"
64 #include "xml/rebase-hrefs.h"
66 // Higher number means lower priority.
67 #define SP_DOCUMENT_UPDATE_PRIORITY (G_PRIORITY_HIGH_IDLE - 2)
69 // Should have a lower priority than SP_DOCUMENT_UPDATE_PRIORITY,
70 // since we want it to happen when there are no more updates.
71 #define SP_DOCUMENT_REROUTING_PRIORITY (G_PRIORITY_HIGH_IDLE - 1)
74 static gint sp_document_idle_handler(gpointer data);
75 static gint sp_document_rerouting_handler(gpointer data);
77 gboolean sp_document_resource_list_free(gpointer key, gpointer value, gpointer data);
79 static gint doc_count = 0;
81 static unsigned long next_serial = 0;
83 SPDocument::SPDocument() :
84 keepalive(FALSE),
85 virgin(TRUE),
86 modified_since_save(FALSE),
87 rdoc(0),
88 rroot(0),
89 root(0),
90 style_cascade(cr_cascade_new(NULL, NULL, NULL)),
91 uri(0),
92 base(0),
93 name(0),
94 priv(0), // reset in ctor
95 actionkey(0),
96 modified_id(0),
97 rerouting_handler_id(0),
98 profileManager(0), // deferred until after other initialization
99 router(new Avoid::Router(Avoid::PolyLineRouting|Avoid::OrthogonalRouting)),
100 _collection_queue(0),
101 oldSignalsConnected(false),
102 current_persp3d(0)
103 {
104 // Penalise libavoid for choosing paths with needless extra segments.
105 // This results in much better looking orthogonal connector paths.
106 router->setRoutingPenalty(Avoid::segmentPenalty);
108 SPDocumentPrivate *p = new SPDocumentPrivate();
110 p->serial = next_serial++;
112 p->iddef = g_hash_table_new(g_direct_hash, g_direct_equal);
113 p->reprdef = g_hash_table_new(g_direct_hash, g_direct_equal);
115 p->resources = g_hash_table_new(g_str_hash, g_str_equal);
117 p->sensitive = FALSE;
118 p->partial = NULL;
119 p->history_size = 0;
120 p->undo = NULL;
121 p->redo = NULL;
122 p->seeking = false;
124 priv = p;
126 // Once things are set, hook in the manager
127 profileManager = new Inkscape::ProfileManager(this);
129 // XXX only for testing!
130 priv->undoStackObservers.add(p->console_output_undo_observer);
131 }
133 SPDocument::~SPDocument() {
134 collectOrphans();
136 // kill/unhook this first
137 if ( profileManager ) {
138 delete profileManager;
139 profileManager = 0;
140 }
142 if (router) {
143 delete router;
144 router = NULL;
145 }
147 if (priv) {
148 if (priv->partial) {
149 sp_repr_free_log(priv->partial);
150 priv->partial = NULL;
151 }
153 sp_document_clear_redo(this);
154 sp_document_clear_undo(this);
156 if (root) {
157 root->releaseReferences();
158 sp_object_unref(root);
159 root = NULL;
160 }
162 if (priv->iddef) g_hash_table_destroy(priv->iddef);
163 if (priv->reprdef) g_hash_table_destroy(priv->reprdef);
165 if (rdoc) Inkscape::GC::release(rdoc);
167 /* Free resources */
168 g_hash_table_foreach_remove(priv->resources, sp_document_resource_list_free, this);
169 g_hash_table_destroy(priv->resources);
171 delete priv;
172 priv = NULL;
173 }
175 cr_cascade_unref(style_cascade);
176 style_cascade = NULL;
178 if (name) {
179 g_free(name);
180 name = NULL;
181 }
182 if (base) {
183 g_free(base);
184 base = NULL;
185 }
186 if (uri) {
187 g_free(uri);
188 uri = NULL;
189 }
191 if (modified_id) {
192 g_source_remove(modified_id);
193 modified_id = 0;
194 }
196 if (rerouting_handler_id) {
197 g_source_remove(rerouting_handler_id);
198 rerouting_handler_id = 0;
199 }
201 if (oldSignalsConnected) {
202 g_signal_handlers_disconnect_by_func(G_OBJECT(INKSCAPE),
203 reinterpret_cast<gpointer>(sp_document_reset_key),
204 static_cast<gpointer>(this));
205 } else {
206 _selection_changed_connection.disconnect();
207 _desktop_activated_connection.disconnect();
208 }
210 if (keepalive) {
211 inkscape_unref();
212 keepalive = FALSE;
213 }
214 if (actionkey) {
215 g_free(actionkey);
216 actionkey = NULL;
217 }
218 //delete this->_whiteboard_session_manager;
219 }
221 Persp3D *
222 SPDocument::getCurrentPersp3D() {
223 // Check if current_persp3d is still valid
224 std::vector<Persp3D*> plist;
225 getPerspectivesInDefs(plist);
226 for (unsigned int i = 0; i < plist.size(); ++i) {
227 if (current_persp3d == plist[i])
228 return current_persp3d;
229 }
231 // If not, return the first perspective in defs (which may be NULL of none exists)
232 current_persp3d = persp3d_document_first_persp (this);
234 return current_persp3d;
235 }
237 Persp3DImpl *
238 SPDocument::getCurrentPersp3DImpl() {
239 return current_persp3d_impl;
240 }
242 void
243 SPDocument::setCurrentPersp3D(Persp3D * const persp) {
244 current_persp3d = persp;
245 //current_persp3d_impl = persp->perspective_impl;
246 }
248 void
249 SPDocument::getPerspectivesInDefs(std::vector<Persp3D*> &list) {
250 SPDefs *defs = SP_ROOT(this->root)->defs;
251 for (SPObject *i = sp_object_first_child(SP_OBJECT(defs)); i != NULL; i = SP_OBJECT_NEXT(i) ) {
252 if (SP_IS_PERSP3D(i))
253 list.push_back(SP_PERSP3D(i));
254 }
255 }
257 /**
258 void SPDocument::initialize_current_persp3d()
259 {
260 this->current_persp3d = persp3d_document_first_persp(this);
261 if (!this->current_persp3d) {
262 this->current_persp3d = persp3d_create_xml_element(this);
263 }
264 }
265 **/
267 unsigned long SPDocument::serial() const {
268 return priv->serial;
269 }
271 void SPDocument::queueForOrphanCollection(SPObject *object) {
272 g_return_if_fail(object != NULL);
273 g_return_if_fail(SP_OBJECT_DOCUMENT(object) == this);
275 sp_object_ref(object, NULL);
276 _collection_queue = g_slist_prepend(_collection_queue, object);
277 }
279 void SPDocument::collectOrphans() {
280 while (_collection_queue) {
281 GSList *objects=_collection_queue;
282 _collection_queue = NULL;
283 for ( GSList *iter=objects ; iter ; iter = iter->next ) {
284 SPObject *object=reinterpret_cast<SPObject *>(iter->data);
285 object->collectOrphan();
286 sp_object_unref(object, NULL);
287 }
288 g_slist_free(objects);
289 }
290 }
292 void SPDocument::reset_key (void */*dummy*/)
293 {
294 actionkey = NULL;
295 }
297 SPDocument *
298 sp_document_create(Inkscape::XML::Document *rdoc,
299 gchar const *uri,
300 gchar const *base,
301 gchar const *name,
302 unsigned int keepalive)
303 {
304 SPDocument *document;
305 Inkscape::XML::Node *rroot;
306 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
308 rroot = rdoc->root();
310 document = new SPDocument();
312 document->keepalive = keepalive;
314 document->rdoc = rdoc;
315 document->rroot = rroot;
317 #ifndef WIN32
318 document->uri = prepend_current_dir_if_relative(uri);
319 #else
320 // FIXME: it may be that prepend_current_dir_if_relative works OK on windows too, test!
321 document->uri = uri? g_strdup(uri) : NULL;
322 #endif
324 // base is simply the part of the path before filename; e.g. when running "inkscape ../file.svg" the base is "../"
325 // which is why we use g_get_current_dir() in calculating the abs path above
326 //This is NULL for a new document
327 if (base)
328 document->base = g_strdup(base);
329 else
330 document->base = NULL;
331 document->name = g_strdup(name);
333 document->root = sp_object_repr_build_tree(document, rroot);
335 /* fixme: Not sure about this, but lets assume ::build updates */
336 rroot->setAttribute("inkscape:version", Inkscape::version_string);
337 /* fixme: Again, I moved these here to allow version determining in ::build (Lauris) */
339 /* Quick hack 2 - get default image size into document */
340 if (!rroot->attribute("width")) rroot->setAttribute("width", "100%");
341 if (!rroot->attribute("height")) rroot->setAttribute("height", "100%");
342 /* End of quick hack 2 */
344 /* Quick hack 3 - Set uri attributes */
345 if (uri) {
346 rroot->setAttribute("sodipodi:docname", uri);
347 }
348 /* End of quick hack 3 */
350 /* Eliminate obsolete sodipodi:docbase, for privacy reasons */
351 rroot->setAttribute("sodipodi:docbase", NULL);
353 /* Eliminate any claim to adhere to a profile, as we don't try to */
354 rroot->setAttribute("baseProfile", NULL);
356 // creating namedview
357 if (!sp_item_group_get_child_by_name((SPGroup *) document->root, NULL, "sodipodi:namedview")) {
358 // if there's none in the document already,
359 Inkscape::XML::Node *rnew = NULL;
361 rnew = rdoc->createElement("sodipodi:namedview");
362 //rnew->setAttribute("id", "base");
364 // Add namedview data from the preferences
365 // we can't use getAllEntries because this could produce non-SVG doubles
366 Glib::ustring pagecolor = prefs->getString("/template/base/pagecolor");
367 if (!pagecolor.empty()) {
368 rnew->setAttribute("pagecolor", pagecolor.data());
369 }
370 Glib::ustring bordercolor = prefs->getString("/template/base/bordercolor");
371 if (!bordercolor.empty()) {
372 rnew->setAttribute("bordercolor", bordercolor.data());
373 }
374 sp_repr_set_svg_double(rnew, "borderopacity",
375 prefs->getDouble("/template/base/borderopacity", 1.0));
376 sp_repr_set_svg_double(rnew, "objecttolerance",
377 prefs->getDouble("/template/base/objecttolerance", 10.0));
378 sp_repr_set_svg_double(rnew, "gridtolerance",
379 prefs->getDouble("/template/base/gridtolerance", 10.0));
380 sp_repr_set_svg_double(rnew, "guidetolerance",
381 prefs->getDouble("/template/base/guidetolerance", 10.0));
382 sp_repr_set_svg_double(rnew, "inkscape:pageopacity",
383 prefs->getDouble("/template/base/inkscape:pageopacity", 0.0));
384 sp_repr_set_int(rnew, "inkscape:pageshadow",
385 prefs->getInt("/template/base/inkscape:pageshadow", 2));
386 sp_repr_set_int(rnew, "inkscape:window-width",
387 prefs->getInt("/template/base/inkscape:window-width", 640));
388 sp_repr_set_int(rnew, "inkscape:window-height",
389 prefs->getInt("/template/base/inkscape:window-height", 480));
391 // insert into the document
392 rroot->addChild(rnew, NULL);
393 // clean up
394 Inkscape::GC::release(rnew);
395 }
397 /* Defs */
398 if (!SP_ROOT(document->root)->defs) {
399 Inkscape::XML::Node *r;
400 r = rdoc->createElement("svg:defs");
401 rroot->addChild(r, NULL);
402 Inkscape::GC::release(r);
403 g_assert(SP_ROOT(document->root)->defs);
404 }
406 /* Default RDF */
407 rdf_set_defaults( document );
409 if (keepalive) {
410 inkscape_ref();
411 }
413 // Check if the document already has a perspective (e.g., when opening an existing
414 // document). If not, create a new one and set it as the current perspective.
415 document->setCurrentPersp3D(persp3d_document_first_persp(document));
416 if (!document->getCurrentPersp3D()) {
417 //document->setCurrentPersp3D(persp3d_create_xml_element (document));
418 Persp3DImpl *persp_impl = new Persp3DImpl();
419 document->setCurrentPersp3DImpl(persp_impl);
420 }
422 sp_document_set_undo_sensitive(document, true);
424 // reset undo key when selection changes, so that same-key actions on different objects are not coalesced
425 if (!Inkscape::NSApplication::Application::getNewGui()) {
426 g_signal_connect(G_OBJECT(INKSCAPE), "change_selection",
427 G_CALLBACK(sp_document_reset_key), document);
428 g_signal_connect(G_OBJECT(INKSCAPE), "activate_desktop",
429 G_CALLBACK(sp_document_reset_key), document);
430 document->oldSignalsConnected = true;
431 } else {
432 document->_selection_changed_connection = Inkscape::NSApplication::Editor::connectSelectionChanged (sigc::mem_fun (*document, &SPDocument::reset_key));
433 document->_desktop_activated_connection = Inkscape::NSApplication::Editor::connectDesktopActivated (sigc::mem_fun (*document, &SPDocument::reset_key));
434 document->oldSignalsConnected = false;
435 }
437 return document;
438 }
440 /**
441 * Fetches document from URI, or creates new, if NULL; public document
442 * appears in document list.
443 */
444 SPDocument *
445 sp_document_new(gchar const *uri, unsigned int keepalive, bool make_new)
446 {
447 SPDocument *doc;
448 Inkscape::XML::Document *rdoc;
449 gchar *base = NULL;
450 gchar *name = NULL;
452 if (uri) {
453 Inkscape::XML::Node *rroot;
454 gchar *s, *p;
455 /* Try to fetch repr from file */
456 rdoc = sp_repr_read_file(uri, SP_SVG_NS_URI);
457 /* If file cannot be loaded, return NULL without warning */
458 if (rdoc == NULL) return NULL;
459 rroot = rdoc->root();
460 /* If xml file is not svg, return NULL without warning */
461 /* fixme: destroy document */
462 if (strcmp(rroot->name(), "svg:svg") != 0) return NULL;
463 s = g_strdup(uri);
464 p = strrchr(s, '/');
465 if (p) {
466 name = g_strdup(p + 1);
467 p[1] = '\0';
468 base = g_strdup(s);
469 } else {
470 base = NULL;
471 name = g_strdup(uri);
472 }
473 g_free(s);
474 } else {
475 rdoc = sp_repr_document_new("svg:svg");
476 }
478 if (make_new) {
479 base = NULL;
480 uri = NULL;
481 name = g_strdup_printf(_("New document %d"), ++doc_count);
482 }
484 //# These should be set by now
485 g_assert(name);
487 doc = sp_document_create(rdoc, uri, base, name, keepalive);
489 g_free(base);
490 g_free(name);
492 return doc;
493 }
495 SPDocument *
496 sp_document_new_from_mem(gchar const *buffer, gint length, unsigned int keepalive)
497 {
498 SPDocument *doc;
499 Inkscape::XML::Document *rdoc;
500 Inkscape::XML::Node *rroot;
501 gchar *name;
503 rdoc = sp_repr_read_mem(buffer, length, SP_SVG_NS_URI);
505 /* If it cannot be loaded, return NULL without warning */
506 if (rdoc == NULL) return NULL;
508 rroot = rdoc->root();
509 /* If xml file is not svg, return NULL without warning */
510 /* fixme: destroy document */
511 if (strcmp(rroot->name(), "svg:svg") != 0) return NULL;
513 name = g_strdup_printf(_("Memory document %d"), ++doc_count);
515 doc = sp_document_create(rdoc, NULL, NULL, name, keepalive);
517 return doc;
518 }
520 SPDocument *
521 sp_document_ref(SPDocument *doc)
522 {
523 g_return_val_if_fail(doc != NULL, NULL);
524 Inkscape::GC::anchor(doc);
525 return doc;
526 }
528 SPDocument *
529 sp_document_unref(SPDocument *doc)
530 {
531 g_return_val_if_fail(doc != NULL, NULL);
532 Inkscape::GC::release(doc);
533 return NULL;
534 }
536 gdouble sp_document_width(SPDocument *document)
537 {
538 g_return_val_if_fail(document != NULL, 0.0);
539 g_return_val_if_fail(document->priv != NULL, 0.0);
540 g_return_val_if_fail(document->root != NULL, 0.0);
542 SPRoot *root = SP_ROOT(document->root);
544 if (root->width.unit == SVGLength::PERCENT && root->viewBox_set)
545 return root->viewBox.x1 - root->viewBox.x0;
546 return root->width.computed;
547 }
549 void
550 sp_document_set_width (SPDocument *document, gdouble width, const SPUnit *unit)
551 {
552 SPRoot *root = SP_ROOT(document->root);
554 if (root->width.unit == SVGLength::PERCENT && root->viewBox_set) { // set to viewBox=
555 root->viewBox.x1 = root->viewBox.x0 + sp_units_get_pixels (width, *unit);
556 } else { // set to width=
557 gdouble old_computed = root->width.computed;
558 root->width.computed = sp_units_get_pixels (width, *unit);
559 /* SVG does not support meters as a unit, so we must translate meters to
560 * cm when writing */
561 if (!strcmp(unit->abbr, "m")) {
562 root->width.value = 100*width;
563 root->width.unit = SVGLength::CM;
564 } else {
565 root->width.value = width;
566 root->width.unit = (SVGLength::Unit) sp_unit_get_svg_unit(unit);
567 }
569 if (root->viewBox_set)
570 root->viewBox.x1 = root->viewBox.x0 + (root->width.computed / old_computed) * (root->viewBox.x1 - root->viewBox.x0);
571 }
573 SP_OBJECT (root)->updateRepr();
574 }
576 void sp_document_set_height (SPDocument * document, gdouble height, const SPUnit *unit)
577 {
578 SPRoot *root = SP_ROOT(document->root);
580 if (root->height.unit == SVGLength::PERCENT && root->viewBox_set) { // set to viewBox=
581 root->viewBox.y1 = root->viewBox.y0 + sp_units_get_pixels (height, *unit);
582 } else { // set to height=
583 gdouble old_computed = root->height.computed;
584 root->height.computed = sp_units_get_pixels (height, *unit);
585 /* SVG does not support meters as a unit, so we must translate meters to
586 * cm when writing */
587 if (!strcmp(unit->abbr, "m")) {
588 root->height.value = 100*height;
589 root->height.unit = SVGLength::CM;
590 } else {
591 root->height.value = height;
592 root->height.unit = (SVGLength::Unit) sp_unit_get_svg_unit(unit);
593 }
595 if (root->viewBox_set)
596 root->viewBox.y1 = root->viewBox.y0 + (root->height.computed / old_computed) * (root->viewBox.y1 - root->viewBox.y0);
597 }
599 SP_OBJECT (root)->updateRepr();
600 }
602 gdouble sp_document_height(SPDocument *document)
603 {
604 g_return_val_if_fail(document != NULL, 0.0);
605 g_return_val_if_fail(document->priv != NULL, 0.0);
606 g_return_val_if_fail(document->root != NULL, 0.0);
608 SPRoot *root = SP_ROOT(document->root);
610 if (root->height.unit == SVGLength::PERCENT && root->viewBox_set)
611 return root->viewBox.y1 - root->viewBox.y0;
612 return root->height.computed;
613 }
615 Geom::Point sp_document_dimensions(SPDocument *doc)
616 {
617 return Geom::Point(sp_document_width(doc), sp_document_height(doc));
618 }
620 /**
621 * Gets page fitting margin information from the namedview node in the XML.
622 * \param nv_repr reference to this document's namedview
623 * \param key the same key used by the RegisteredScalarUnit in
624 * ui/widget/page-sizer.cpp
625 * \param margin_units units for the margin
626 * \param return_units units to return the result in
627 * \param width width in px (for percentage margins)
628 * \param height height in px (for percentage margins)
629 * \param use_width true if the this key is left or right margins, false
630 * otherwise. Used for percentage margins.
631 * \return the margin size in px, else 0.0 if anything is invalid.
632 */
633 static double getMarginLength(Inkscape::XML::Node * const nv_repr,
634 gchar const * const key,
635 SPUnit const * const margin_units,
636 SPUnit const * const return_units,
637 double const width,
638 double const height,
639 bool const use_width)
640 {
641 double value;
642 if (!sp_repr_get_double (nv_repr, key, &value)) {
643 return 0.0;
644 }
645 if (margin_units == &sp_unit_get_by_id (SP_UNIT_PERCENT)) {
646 return (use_width)? width * value : height * value;
647 }
648 if (!sp_convert_distance (&value, margin_units, return_units)) {
649 return 0.0;
650 }
651 return value;
652 }
654 /**
655 * Given a Geom::Rect that may, for example, correspond to the bbox of an object,
656 * this function fits the canvas to that rect by resizing the canvas
657 * and translating the document root into position.
658 * \param rect fit document size to this
659 * \param with_margins add margins to rect, by taking margins from this
660 * document's namedview (<sodipodi:namedview> "fit-margin-..."
661 * attributes, and "units")
662 */
663 void SPDocument::fitToRect(Geom::Rect const &rect, bool with_margins)
664 {
665 double const w = rect.width();
666 double const h = rect.height();
668 double const old_height = sp_document_height(this);
669 SPUnit const &px(sp_unit_get_by_id(SP_UNIT_PX));
671 /* in px */
672 double margin_top = 0.0;
673 double margin_left = 0.0;
674 double margin_right = 0.0;
675 double margin_bottom = 0.0;
677 SPNamedView *nv = sp_document_namedview(this, 0);
679 if (with_margins && nv) {
680 Inkscape::XML::Node *nv_repr = SP_OBJECT_REPR (nv);
681 if (nv_repr != NULL) {
682 gchar const * const units_abbr = nv_repr->attribute("units");
683 SPUnit const *margin_units = NULL;
684 if (units_abbr != NULL) {
685 margin_units = sp_unit_get_by_abbreviation(units_abbr);
686 }
687 if (margin_units == NULL) {
688 margin_units = &px;
689 }
690 margin_top = getMarginLength(nv_repr, "fit-margin-top",
691 margin_units, &px, w, h, false);
692 margin_left = getMarginLength(nv_repr, "fit-margin-left",
693 margin_units, &px, w, h, true);
694 margin_right = getMarginLength(nv_repr, "fit-margin-right",
695 margin_units, &px, w, h, true);
696 margin_bottom = getMarginLength(nv_repr, "fit-margin-bottom",
697 margin_units, &px, w, h, false);
698 }
699 }
701 Geom::Rect const rect_with_margins(
702 rect.min() - Geom::Point(margin_left, margin_bottom),
703 rect.max() + Geom::Point(margin_right, margin_top));
706 sp_document_set_width(this, rect_with_margins.width(), &px);
707 sp_document_set_height(this, rect_with_margins.height(), &px);
709 Geom::Translate const tr(
710 Geom::Point(0, old_height - rect_with_margins.height())
711 - to_2geom(rect_with_margins.min()));
712 SP_GROUP(root)->translateChildItems(tr);
714 if(nv) {
715 Geom::Translate tr2(-rect_with_margins.min());
716 nv->translateGuides(tr2);
718 // update the viewport so the drawing appears to stay where it was
719 nv->scrollAllDesktops(-tr2[0], tr2[1], false);
720 }
721 }
723 static void
724 do_change_uri(SPDocument *const document, gchar const *const filename, bool const rebase)
725 {
726 g_return_if_fail(document != NULL);
728 gchar *new_base;
729 gchar *new_name;
730 gchar *new_uri;
731 if (filename) {
733 #ifndef WIN32
734 new_uri = prepend_current_dir_if_relative(filename);
735 #else
736 // FIXME: it may be that prepend_current_dir_if_relative works OK on windows too, test!
737 new_uri = g_strdup(filename);
738 #endif
740 new_base = g_path_get_dirname(new_uri);
741 new_name = g_path_get_basename(new_uri);
742 } else {
743 new_uri = g_strdup_printf(_("Unnamed document %d"), ++doc_count);
744 new_base = NULL;
745 new_name = g_strdup(document->uri);
746 }
748 // Update saveable repr attributes.
749 Inkscape::XML::Node *repr = sp_document_repr_root(document);
751 // Changing uri in the document repr must not be not undoable.
752 bool const saved = sp_document_get_undo_sensitive(document);
753 sp_document_set_undo_sensitive(document, false);
755 if (rebase) {
756 Inkscape::XML::rebase_hrefs(document, new_base, true);
757 }
759 repr->setAttribute("sodipodi:docname", document->name);
760 sp_document_set_undo_sensitive(document, saved);
763 g_free(document->name);
764 g_free(document->base);
765 g_free(document->uri);
766 document->name = new_name;
767 document->base = new_base;
768 document->uri = new_uri;
770 document->priv->uri_set_signal.emit(document->uri);
771 }
773 /**
774 * Sets base, name and uri members of \a document. Doesn't update
775 * any relative hrefs in the document: thus, this is primarily for
776 * newly-created documents.
777 *
778 * \see sp_document_change_uri_and_hrefs
779 */
780 void sp_document_set_uri(SPDocument *document, gchar const *filename)
781 {
782 g_return_if_fail(document != NULL);
784 do_change_uri(document, filename, false);
785 }
787 /**
788 * Changes the base, name and uri members of \a document, and updates any
789 * relative hrefs in the document to be relative to the new base.
790 *
791 * \see sp_document_set_uri
792 */
793 void sp_document_change_uri_and_hrefs(SPDocument *document, gchar const *filename)
794 {
795 g_return_if_fail(document != NULL);
797 do_change_uri(document, filename, true);
798 }
800 void
801 sp_document_resized_signal_emit(SPDocument *doc, gdouble width, gdouble height)
802 {
803 g_return_if_fail(doc != NULL);
805 doc->priv->resized_signal.emit(width, height);
806 }
808 sigc::connection SPDocument::connectModified(SPDocument::ModifiedSignal::slot_type slot)
809 {
810 return priv->modified_signal.connect(slot);
811 }
813 sigc::connection SPDocument::connectURISet(SPDocument::URISetSignal::slot_type slot)
814 {
815 return priv->uri_set_signal.connect(slot);
816 }
818 sigc::connection SPDocument::connectResized(SPDocument::ResizedSignal::slot_type slot)
819 {
820 return priv->resized_signal.connect(slot);
821 }
823 sigc::connection
824 SPDocument::connectReconstructionStart(SPDocument::ReconstructionStart::slot_type slot)
825 {
826 return priv->_reconstruction_start_signal.connect(slot);
827 }
829 void
830 SPDocument::emitReconstructionStart(void)
831 {
832 // printf("Starting Reconstruction\n");
833 priv->_reconstruction_start_signal.emit();
834 return;
835 }
837 sigc::connection
838 SPDocument::connectReconstructionFinish(SPDocument::ReconstructionFinish::slot_type slot)
839 {
840 return priv->_reconstruction_finish_signal.connect(slot);
841 }
843 void
844 SPDocument::emitReconstructionFinish(void)
845 {
846 // printf("Finishing Reconstruction\n");
847 priv->_reconstruction_finish_signal.emit();
849 /**
850 // Reference to the old persp3d object is invalid after reconstruction.
851 initialize_current_persp3d();
853 return;
854 **/
855 }
857 sigc::connection SPDocument::connectCommit(SPDocument::CommitSignal::slot_type slot)
858 {
859 return priv->commit_signal.connect(slot);
860 }
864 void SPDocument::_emitModified() {
865 static guint const flags = SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG | SP_OBJECT_PARENT_MODIFIED_FLAG;
866 root->emitModified(0);
867 priv->modified_signal.emit(flags);
868 }
870 void SPDocument::bindObjectToId(gchar const *id, SPObject *object) {
871 GQuark idq = g_quark_from_string(id);
873 if (object) {
874 g_assert(g_hash_table_lookup(priv->iddef, GINT_TO_POINTER(idq)) == NULL);
875 g_hash_table_insert(priv->iddef, GINT_TO_POINTER(idq), object);
876 } else {
877 g_assert(g_hash_table_lookup(priv->iddef, GINT_TO_POINTER(idq)) != NULL);
878 g_hash_table_remove(priv->iddef, GINT_TO_POINTER(idq));
879 }
881 SPDocumentPrivate::IDChangedSignalMap::iterator pos;
883 pos = priv->id_changed_signals.find(idq);
884 if ( pos != priv->id_changed_signals.end() ) {
885 if (!(*pos).second.empty()) {
886 (*pos).second.emit(object);
887 } else { // discard unused signal
888 priv->id_changed_signals.erase(pos);
889 }
890 }
891 }
893 void
894 SPDocument::addUndoObserver(Inkscape::UndoStackObserver& observer)
895 {
896 this->priv->undoStackObservers.add(observer);
897 }
899 void
900 SPDocument::removeUndoObserver(Inkscape::UndoStackObserver& observer)
901 {
902 this->priv->undoStackObservers.remove(observer);
903 }
905 SPObject *SPDocument::getObjectById(gchar const *id) {
906 g_return_val_if_fail(id != NULL, NULL);
908 GQuark idq = g_quark_from_string(id);
909 return (SPObject*)g_hash_table_lookup(priv->iddef, GINT_TO_POINTER(idq));
910 }
912 sigc::connection SPDocument::connectIdChanged(gchar const *id,
913 SPDocument::IDChangedSignal::slot_type slot)
914 {
915 return priv->id_changed_signals[g_quark_from_string(id)].connect(slot);
916 }
918 void SPDocument::bindObjectToRepr(Inkscape::XML::Node *repr, SPObject *object) {
919 if (object) {
920 g_assert(g_hash_table_lookup(priv->reprdef, repr) == NULL);
921 g_hash_table_insert(priv->reprdef, repr, object);
922 } else {
923 g_assert(g_hash_table_lookup(priv->reprdef, repr) != NULL);
924 g_hash_table_remove(priv->reprdef, repr);
925 }
926 }
928 SPObject *SPDocument::getObjectByRepr(Inkscape::XML::Node *repr) {
929 g_return_val_if_fail(repr != NULL, NULL);
930 return (SPObject*)g_hash_table_lookup(priv->reprdef, repr);
931 }
933 Glib::ustring SPDocument::getLanguage() {
934 gchar const *document_language = rdf_get_work_entity(this, rdf_find_entity("language"));
935 if (document_language) {
936 while (isspace(*document_language))
937 document_language++;
938 }
939 if ( !document_language || 0 == *document_language) {
940 // retrieve system language
941 document_language = getenv("LC_ALL");
942 if ( NULL == document_language || *document_language == 0 ) {
943 document_language = getenv ("LC_MESSAGES");
944 }
945 if ( NULL == document_language || *document_language == 0 ) {
946 document_language = getenv ("LANG");
947 }
949 if ( NULL != document_language ) {
950 const char *pos = strchr(document_language, '_');
951 if ( NULL != pos ) {
952 return Glib::ustring(document_language, pos - document_language);
953 }
954 }
955 }
957 if ( NULL == document_language )
958 return Glib::ustring();
959 return document_language;
960 }
962 /* Object modification root handler */
964 void
965 sp_document_request_modified(SPDocument *doc)
966 {
967 if (!doc->modified_id) {
968 doc->modified_id = g_idle_add_full(SP_DOCUMENT_UPDATE_PRIORITY,
969 sp_document_idle_handler, doc, NULL);
970 }
971 if (!doc->rerouting_handler_id) {
972 doc->rerouting_handler_id = g_idle_add_full(SP_DOCUMENT_REROUTING_PRIORITY,
973 sp_document_rerouting_handler, doc, NULL);
974 }
975 }
977 void
978 sp_document_setup_viewport (SPDocument *doc, SPItemCtx *ctx)
979 {
980 ctx->ctx.flags = 0;
981 ctx->i2doc = Geom::identity();
982 /* Set up viewport in case svg has it defined as percentages */
983 if (SP_ROOT(doc->root)->viewBox_set) { // if set, take from viewBox
984 ctx->vp.x0 = SP_ROOT(doc->root)->viewBox.x0;
985 ctx->vp.y0 = SP_ROOT(doc->root)->viewBox.y0;
986 ctx->vp.x1 = SP_ROOT(doc->root)->viewBox.x1;
987 ctx->vp.y1 = SP_ROOT(doc->root)->viewBox.y1;
988 } else { // as a last resort, set size to A4
989 ctx->vp.x0 = 0.0;
990 ctx->vp.y0 = 0.0;
991 ctx->vp.x1 = 210 * PX_PER_MM;
992 ctx->vp.y1 = 297 * PX_PER_MM;
993 }
994 ctx->i2vp = Geom::identity();
995 }
997 /**
998 * Tries to update the document state based on the modified and
999 * "update required" flags, and return true if the document has
1000 * been brought fully up to date.
1001 */
1002 bool
1003 SPDocument::_updateDocument()
1004 {
1005 /* Process updates */
1006 if (this->root->uflags || this->root->mflags) {
1007 if (this->root->uflags) {
1008 SPItemCtx ctx;
1009 sp_document_setup_viewport (this, &ctx);
1011 bool saved = sp_document_get_undo_sensitive(this);
1012 sp_document_set_undo_sensitive(this, false);
1014 this->root->updateDisplay((SPCtx *)&ctx, 0);
1016 sp_document_set_undo_sensitive(this, saved);
1017 }
1018 this->_emitModified();
1019 }
1021 return !(this->root->uflags || this->root->mflags);
1022 }
1025 /**
1026 * Repeatedly works on getting the document updated, since sometimes
1027 * it takes more than one pass to get the document updated. But it
1028 * usually should not take more than a few loops, and certainly never
1029 * more than 32 iterations. So we bail out if we hit 32 iterations,
1030 * since this typically indicates we're stuck in an update loop.
1031 */
1032 gint
1033 sp_document_ensure_up_to_date(SPDocument *doc)
1034 {
1035 // Bring the document up-to-date, specifically via the following:
1036 // 1a) Process all document updates.
1037 // 1b) When completed, process connector routing changes.
1038 // 2a) Process any updates resulting from connector reroutings.
1039 int counter = 32;
1040 for (unsigned int pass = 1; pass <= 2; ++pass) {
1041 // Process document updates.
1042 while (!doc->_updateDocument()) {
1043 if (counter == 0) {
1044 g_warning("More than 32 iteration while updating document '%s'", doc->uri);
1045 break;
1046 }
1047 counter--;
1048 }
1049 if (counter == 0)
1050 {
1051 break;
1052 }
1054 // After updates on the first pass we get libavoid to process all the
1055 // changed objects and provide new routings. This may cause some objects
1056 // to be modified, hence the second update pass.
1057 if (pass == 1) {
1058 doc->router->processTransaction();
1059 }
1060 }
1062 if (doc->modified_id) {
1063 /* Remove handler */
1064 g_source_remove(doc->modified_id);
1065 doc->modified_id = 0;
1066 }
1067 if (doc->rerouting_handler_id) {
1068 /* Remove handler */
1069 g_source_remove(doc->rerouting_handler_id);
1070 doc->rerouting_handler_id = 0;
1071 }
1072 return counter>0;
1073 }
1075 /**
1076 * An idle handler to update the document. Returns true if
1077 * the document needs further updates.
1078 */
1079 static gint
1080 sp_document_idle_handler(gpointer data)
1081 {
1082 SPDocument *doc = static_cast<SPDocument *>(data);
1083 if (doc->_updateDocument()) {
1084 doc->modified_id = 0;
1085 return false;
1086 } else {
1087 return true;
1088 }
1089 }
1091 /**
1092 * An idle handler to reroute connectors in the document.
1093 */
1094 static gint
1095 sp_document_rerouting_handler(gpointer data)
1096 {
1097 // Process any queued movement actions and determine new routings for
1098 // object-avoiding connectors. Callbacks will be used to update and
1099 // redraw affected connectors.
1100 SPDocument *doc = static_cast<SPDocument *>(data);
1101 doc->router->processTransaction();
1103 // We don't need to handle rerouting again until there are further
1104 // diagram updates.
1105 doc->rerouting_handler_id = 0;
1106 return false;
1107 }
1109 static bool is_within(Geom::Rect const &area, Geom::Rect const &box)
1110 {
1111 return area.contains(box);
1112 }
1114 static bool overlaps(Geom::Rect const &area, Geom::Rect const &box)
1115 {
1116 return area.intersects(box);
1117 }
1119 static GSList *find_items_in_area(GSList *s, SPGroup *group, unsigned int dkey, Geom::Rect const &area,
1120 bool (*test)(Geom::Rect const &, Geom::Rect const &), bool take_insensitive = false)
1121 {
1122 g_return_val_if_fail(SP_IS_GROUP(group), s);
1124 for (SPObject *o = sp_object_first_child(SP_OBJECT(group)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
1125 if (!SP_IS_ITEM(o)) {
1126 continue;
1127 }
1128 if (SP_IS_GROUP(o) && SP_GROUP(o)->effectiveLayerMode(dkey) == SPGroup::LAYER ) {
1129 s = find_items_in_area(s, SP_GROUP(o), dkey, area, test);
1130 } else {
1131 SPItem *child = SP_ITEM(o);
1132 Geom::OptRect box = sp_item_bbox_desktop(child);
1133 if ( box && test(area, *box) && (take_insensitive || child->isVisibleAndUnlocked(dkey))) {
1134 s = g_slist_append(s, child);
1135 }
1136 }
1137 }
1139 return s;
1140 }
1142 /**
1143 Returns true if an item is among the descendants of group (recursively).
1144 */
1145 bool item_is_in_group(SPItem *item, SPGroup *group)
1146 {
1147 for (SPObject *o = sp_object_first_child(SP_OBJECT(group)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
1148 if (!SP_IS_ITEM(o)) continue;
1149 if (SP_ITEM(o) == item)
1150 return true;
1151 if (SP_IS_GROUP(o))
1152 if (item_is_in_group(item, SP_GROUP(o)))
1153 return true;
1154 }
1155 return false;
1156 }
1158 /**
1159 Returns the bottommost item from the list which is at the point, or NULL if none.
1160 */
1161 SPItem*
1162 sp_document_item_from_list_at_point_bottom(unsigned int dkey, SPGroup *group, GSList const *list,
1163 Geom::Point const p, bool take_insensitive)
1164 {
1165 g_return_val_if_fail(group, NULL);
1166 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1167 gdouble delta = prefs->getDouble("/options/cursortolerance/value", 1.0);
1169 for (SPObject *o = sp_object_first_child(SP_OBJECT(group)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
1171 if (!SP_IS_ITEM(o)) continue;
1173 SPItem *item = SP_ITEM(o);
1174 NRArenaItem *arenaitem = sp_item_get_arenaitem(item, dkey);
1175 if (arenaitem && nr_arena_item_invoke_pick(arenaitem, p, delta, 1) != NULL
1176 && (take_insensitive || item->isVisibleAndUnlocked(dkey))) {
1177 if (g_slist_find((GSList *) list, item) != NULL)
1178 return item;
1179 }
1181 if (SP_IS_GROUP(o)) {
1182 SPItem *found = sp_document_item_from_list_at_point_bottom(dkey, SP_GROUP(o), list, p, take_insensitive);
1183 if (found)
1184 return found;
1185 }
1187 }
1188 return NULL;
1189 }
1191 /**
1192 Returns the topmost (in z-order) item from the descendants of group (recursively) which
1193 is at the point p, or NULL if none. Honors into_groups on whether to recurse into
1194 non-layer groups or not. Honors take_insensitive on whether to return insensitive
1195 items. If upto != NULL, then if item upto is encountered (at any level), stops searching
1196 upwards in z-order and returns what it has found so far (i.e. the found item is
1197 guaranteed to be lower than upto).
1198 */
1199 SPItem*
1200 find_item_at_point(unsigned int dkey, SPGroup *group, Geom::Point const p, gboolean into_groups, bool take_insensitive = false, SPItem *upto = NULL)
1201 {
1202 SPItem *seen = NULL, *newseen = NULL;
1203 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1204 gdouble delta = prefs->getDouble("/options/cursortolerance/value", 1.0);
1206 for (SPObject *o = sp_object_first_child(SP_OBJECT(group)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
1207 if (!SP_IS_ITEM(o)) continue;
1209 if (upto && SP_ITEM(o) == upto)
1210 break;
1212 if (SP_IS_GROUP(o) && (SP_GROUP(o)->effectiveLayerMode(dkey) == SPGroup::LAYER || into_groups)) {
1213 // if nothing found yet, recurse into the group
1214 newseen = find_item_at_point(dkey, SP_GROUP(o), p, into_groups, take_insensitive, upto);
1215 if (newseen) {
1216 seen = newseen;
1217 newseen = NULL;
1218 }
1220 if (item_is_in_group(upto, SP_GROUP(o)))
1221 break;
1223 } else {
1224 SPItem *child = SP_ITEM(o);
1225 NRArenaItem *arenaitem = sp_item_get_arenaitem(child, dkey);
1227 // seen remembers the last (topmost) of items pickable at this point
1228 if (arenaitem && nr_arena_item_invoke_pick(arenaitem, p, delta, 1) != NULL
1229 && (take_insensitive || child->isVisibleAndUnlocked(dkey))) {
1230 seen = child;
1231 }
1232 }
1233 }
1234 return seen;
1235 }
1237 /**
1238 Returns the topmost non-layer group from the descendants of group which is at point
1239 p, or NULL if none. Recurses into layers but not into groups.
1240 */
1241 SPItem*
1242 find_group_at_point(unsigned int dkey, SPGroup *group, Geom::Point const p)
1243 {
1244 SPItem *seen = NULL;
1245 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1246 gdouble delta = prefs->getDouble("/options/cursortolerance/value", 1.0);
1248 for (SPObject *o = sp_object_first_child(SP_OBJECT(group)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
1249 if (!SP_IS_ITEM(o)) continue;
1250 if (SP_IS_GROUP(o) && SP_GROUP(o)->effectiveLayerMode(dkey) == SPGroup::LAYER) {
1251 SPItem *newseen = find_group_at_point(dkey, SP_GROUP(o), p);
1252 if (newseen) {
1253 seen = newseen;
1254 }
1255 }
1256 if (SP_IS_GROUP(o) && SP_GROUP(o)->effectiveLayerMode(dkey) != SPGroup::LAYER ) {
1257 SPItem *child = SP_ITEM(o);
1258 NRArenaItem *arenaitem = sp_item_get_arenaitem(child, dkey);
1260 // seen remembers the last (topmost) of groups pickable at this point
1261 if (arenaitem && nr_arena_item_invoke_pick(arenaitem, p, delta, 1) != NULL) {
1262 seen = child;
1263 }
1264 }
1265 }
1266 return seen;
1267 }
1269 /*
1270 * Return list of items, contained in box
1271 *
1272 * Assumes box is normalized (and g_asserts it!)
1273 *
1274 */
1276 GSList *sp_document_items_in_box(SPDocument *document, unsigned int dkey, Geom::Rect const &box)
1277 {
1278 g_return_val_if_fail(document != NULL, NULL);
1279 g_return_val_if_fail(document->priv != NULL, NULL);
1281 return find_items_in_area(NULL, SP_GROUP(document->root), dkey, box, is_within);
1282 }
1284 /*
1285 * Return list of items, that the parts of the item contained in box
1286 *
1287 * Assumes box is normalized (and g_asserts it!)
1288 *
1289 */
1291 GSList *sp_document_partial_items_in_box(SPDocument *document, unsigned int dkey, Geom::Rect const &box)
1292 {
1293 g_return_val_if_fail(document != NULL, NULL);
1294 g_return_val_if_fail(document->priv != NULL, NULL);
1296 return find_items_in_area(NULL, SP_GROUP(document->root), dkey, box, overlaps);
1297 }
1299 GSList *
1300 sp_document_items_at_points(SPDocument *document, unsigned const key, std::vector<Geom::Point> points)
1301 {
1302 GSList *items = NULL;
1303 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1305 // When picking along the path, we don't want small objects close together
1306 // (such as hatching strokes) to obscure each other by their deltas,
1307 // so we temporarily set delta to a small value
1308 gdouble saved_delta = prefs->getDouble("/options/cursortolerance/value", 1.0);
1309 prefs->setDouble("/options/cursortolerance/value", 0.25);
1311 for(unsigned int i = 0; i < points.size(); i++) {
1312 SPItem *item = sp_document_item_at_point(document, key, points[i],
1313 false, NULL);
1314 if (item && !g_slist_find(items, item))
1315 items = g_slist_prepend (items, item);
1316 }
1318 // and now we restore it back
1319 prefs->setDouble("/options/cursortolerance/value", saved_delta);
1321 return items;
1322 }
1324 SPItem *
1325 sp_document_item_at_point(SPDocument *document, unsigned const key, Geom::Point const p,
1326 gboolean const into_groups, SPItem *upto)
1327 {
1328 g_return_val_if_fail(document != NULL, NULL);
1329 g_return_val_if_fail(document->priv != NULL, NULL);
1331 return find_item_at_point(key, SP_GROUP(document->root), p, into_groups, false, upto);
1332 }
1334 SPItem*
1335 sp_document_group_at_point(SPDocument *document, unsigned int key, Geom::Point const p)
1336 {
1337 g_return_val_if_fail(document != NULL, NULL);
1338 g_return_val_if_fail(document->priv != NULL, NULL);
1340 return find_group_at_point(key, SP_GROUP(document->root), p);
1341 }
1344 /* Resource management */
1346 gboolean
1347 sp_document_add_resource(SPDocument *document, gchar const *key, SPObject *object)
1348 {
1349 GSList *rlist;
1350 GQuark q = g_quark_from_string(key);
1352 g_return_val_if_fail(document != NULL, FALSE);
1353 g_return_val_if_fail(key != NULL, FALSE);
1354 g_return_val_if_fail(*key != '\0', FALSE);
1355 g_return_val_if_fail(object != NULL, FALSE);
1356 g_return_val_if_fail(SP_IS_OBJECT(object), FALSE);
1358 if (SP_OBJECT_IS_CLONED(object))
1359 return FALSE;
1361 rlist = (GSList*)g_hash_table_lookup(document->priv->resources, key);
1362 g_return_val_if_fail(!g_slist_find(rlist, object), FALSE);
1363 rlist = g_slist_prepend(rlist, object);
1364 g_hash_table_insert(document->priv->resources, (gpointer) key, rlist);
1366 document->priv->resources_changed_signals[q].emit();
1368 return TRUE;
1369 }
1371 gboolean
1372 sp_document_remove_resource(SPDocument *document, gchar const *key, SPObject *object)
1373 {
1374 GSList *rlist;
1375 GQuark q = g_quark_from_string(key);
1377 g_return_val_if_fail(document != NULL, FALSE);
1378 g_return_val_if_fail(key != NULL, FALSE);
1379 g_return_val_if_fail(*key != '\0', FALSE);
1380 g_return_val_if_fail(object != NULL, FALSE);
1381 g_return_val_if_fail(SP_IS_OBJECT(object), FALSE);
1383 if (SP_OBJECT_IS_CLONED(object))
1384 return FALSE;
1386 rlist = (GSList*)g_hash_table_lookup(document->priv->resources, key);
1387 g_return_val_if_fail(rlist != NULL, FALSE);
1388 g_return_val_if_fail(g_slist_find(rlist, object), FALSE);
1389 rlist = g_slist_remove(rlist, object);
1390 g_hash_table_insert(document->priv->resources, (gpointer) key, rlist);
1392 document->priv->resources_changed_signals[q].emit();
1394 return TRUE;
1395 }
1397 GSList const *
1398 sp_document_get_resource_list(SPDocument *document, gchar const *key)
1399 {
1400 g_return_val_if_fail(document != NULL, NULL);
1401 g_return_val_if_fail(key != NULL, NULL);
1402 g_return_val_if_fail(*key != '\0', NULL);
1404 return (GSList*)g_hash_table_lookup(document->priv->resources, key);
1405 }
1407 sigc::connection sp_document_resources_changed_connect(SPDocument *document,
1408 gchar const *key,
1409 SPDocument::ResourcesChangedSignal::slot_type slot)
1410 {
1411 GQuark q = g_quark_from_string(key);
1412 return document->priv->resources_changed_signals[q].connect(slot);
1413 }
1415 /* Helpers */
1417 gboolean
1418 sp_document_resource_list_free(gpointer /*key*/, gpointer value, gpointer /*data*/)
1419 {
1420 g_slist_free((GSList *) value);
1421 return TRUE;
1422 }
1424 unsigned int
1425 count_objects_recursive(SPObject *obj, unsigned int count)
1426 {
1427 count++; // obj itself
1429 for (SPObject *i = sp_object_first_child(obj); i != NULL; i = SP_OBJECT_NEXT(i)) {
1430 count = count_objects_recursive(i, count);
1431 }
1433 return count;
1434 }
1436 unsigned int
1437 objects_in_document(SPDocument *document)
1438 {
1439 return count_objects_recursive(SP_DOCUMENT_ROOT(document), 0);
1440 }
1442 void
1443 vacuum_document_recursive(SPObject *obj)
1444 {
1445 if (SP_IS_DEFS(obj)) {
1446 for (SPObject *def = obj->firstChild(); def; def = SP_OBJECT_NEXT(def)) {
1447 /* fixme: some inkscape-internal nodes in the future might not be collectable */
1448 def->requestOrphanCollection();
1449 }
1450 } else {
1451 for (SPObject *i = sp_object_first_child(obj); i != NULL; i = SP_OBJECT_NEXT(i)) {
1452 vacuum_document_recursive(i);
1453 }
1454 }
1455 }
1457 unsigned int
1458 vacuum_document(SPDocument *document)
1459 {
1460 unsigned int start = objects_in_document(document);
1461 unsigned int end;
1462 unsigned int newend = start;
1464 unsigned int iterations = 0;
1466 do {
1467 end = newend;
1469 vacuum_document_recursive(SP_DOCUMENT_ROOT(document));
1470 document->collectOrphans();
1471 iterations++;
1473 newend = objects_in_document(document);
1475 } while (iterations < 100 && newend < end);
1477 return start - newend;
1478 }
1480 bool SPDocument::isSeeking() const {
1481 return priv->seeking;
1482 }
1485 /*
1486 Local Variables:
1487 mode:c++
1488 c-file-style:"stroustrup"
1489 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1490 indent-tabs-mode:nil
1491 fill-column:99
1492 End:
1493 */
1494 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :