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 "application/application.h"
42 #include "application/editor.h"
43 #include "libnr/nr-matrix-fns.h"
44 #include "xml/repr.h"
45 #include "helper/units.h"
46 #include "inkscape-private.h"
47 #include "inkscape_version.h"
48 #include "sp-object-repr.h"
49 #include "document-private.h"
50 #include "dir-util.h"
51 #include "unit-constants.h"
52 #include "prefs-utils.h"
53 #include "libavoid/router.h"
54 #include "libnr/nr-rect.h"
55 #include "sp-item-group.h"
57 #include "display/nr-arena-item.h"
59 #include "dialogs/rdf.h"
61 #define A4_WIDTH_STR "210mm"
62 #define A4_HEIGHT_STR "297mm"
64 #define SP_DOCUMENT_UPDATE_PRIORITY (G_PRIORITY_HIGH_IDLE - 1)
67 static gint sp_document_idle_handler(gpointer data);
69 gboolean sp_document_resource_list_free(gpointer key, gpointer value, gpointer data);
71 static gint doc_count = 0;
73 static unsigned long next_serial = 0;
75 SPDocument::SPDocument() {
76 SPDocumentPrivate *p;
78 keepalive = FALSE;
79 virgin = TRUE;
81 modified_id = 0;
83 rdoc = NULL;
84 rroot = NULL;
85 root = NULL;
86 style_cascade = cr_cascade_new(NULL, NULL, NULL);
88 uri = NULL;
89 base = NULL;
90 name = NULL;
92 _collection_queue = NULL;
94 // Initialise instance of connector router.
95 router = new Avoid::Router();
96 // Don't use the Consolidate moves optimisation.
97 router->ConsolidateMoves = false;
99 p = new SPDocumentPrivate();
101 p->serial = next_serial++;
103 p->iddef = g_hash_table_new(g_direct_hash, g_direct_equal);
104 p->reprdef = g_hash_table_new(g_direct_hash, g_direct_equal);
106 p->resources = g_hash_table_new(g_str_hash, g_str_equal);
108 p->sensitive = FALSE;
109 p->partial = NULL;
110 p->history_size = 0;
111 p->undo = NULL;
112 p->redo = NULL;
113 p->seeking = false;
115 priv = p;
117 // XXX only for testing!
118 priv->undoStackObservers.add(p->console_output_undo_observer);
119 }
121 SPDocument::~SPDocument() {
122 collectOrphans();
124 if (priv) {
125 if (priv->partial) {
126 sp_repr_free_log(priv->partial);
127 priv->partial = NULL;
128 }
130 sp_document_clear_redo(this);
131 sp_document_clear_undo(this);
133 if (root) {
134 root->releaseReferences();
135 sp_object_unref(root);
136 root = NULL;
137 }
139 if (priv->iddef) g_hash_table_destroy(priv->iddef);
140 if (priv->reprdef) g_hash_table_destroy(priv->reprdef);
142 if (rdoc) Inkscape::GC::release(rdoc);
144 /* Free resources */
145 g_hash_table_foreach_remove(priv->resources, sp_document_resource_list_free, this);
146 g_hash_table_destroy(priv->resources);
148 delete priv;
149 priv = NULL;
150 }
152 cr_cascade_unref(style_cascade);
153 style_cascade = NULL;
155 if (name) {
156 g_free(name);
157 name = NULL;
158 }
159 if (base) {
160 g_free(base);
161 base = NULL;
162 }
163 if (uri) {
164 g_free(uri);
165 uri = NULL;
166 }
168 if (modified_id) {
169 gtk_idle_remove(modified_id);
170 modified_id = 0;
171 }
173 _selection_changed_connection.disconnect();
174 _desktop_activated_connection.disconnect();
176 if (keepalive) {
177 inkscape_unref();
178 keepalive = FALSE;
179 }
181 if (router) {
182 delete router;
183 router = NULL;
184 }
186 //delete this->_whiteboard_session_manager;
187 }
189 unsigned long SPDocument::serial() const {
190 return priv->serial;
191 }
193 void SPDocument::queueForOrphanCollection(SPObject *object) {
194 g_return_if_fail(object != NULL);
195 g_return_if_fail(SP_OBJECT_DOCUMENT(object) == this);
197 sp_object_ref(object, NULL);
198 _collection_queue = g_slist_prepend(_collection_queue, object);
199 }
201 void SPDocument::collectOrphans() {
202 while (_collection_queue) {
203 GSList *objects=_collection_queue;
204 _collection_queue = NULL;
205 for ( GSList *iter=objects ; iter ; iter = iter->next ) {
206 SPObject *object=reinterpret_cast<SPObject *>(iter->data);
207 object->collectOrphan();
208 sp_object_unref(object, NULL);
209 }
210 g_slist_free(objects);
211 }
212 }
214 void SPDocument::reset_key (void *dummy)
215 {
216 actionkey = NULL;
217 }
219 SPDocument *
220 sp_document_create(Inkscape::XML::Document *rdoc,
221 gchar const *uri,
222 gchar const *base,
223 gchar const *name,
224 unsigned int keepalive)
225 {
226 SPDocument *document;
227 Inkscape::XML::Node *rroot;
228 Inkscape::Version sodipodi_version;
230 rroot = rdoc->root();
232 document = new SPDocument();
234 document->keepalive = keepalive;
236 document->rdoc = rdoc;
237 document->rroot = rroot;
239 #ifndef WIN32
240 prepend_current_dir_if_relative(&(document->uri), uri);
241 #else
242 // FIXME: it may be that prepend_current_dir_if_relative works OK on windows too, test!
243 document->uri = uri? g_strdup(uri) : NULL;
244 #endif
246 // base is simply the part of the path before filename; e.g. when running "inkscape ../file.svg" the base is "../"
247 // which is why we use g_get_current_dir() in calculating the abs path above
248 //This is NULL for a new document
249 if (base)
250 document->base = g_strdup(base);
251 else
252 document->base = NULL;
253 document->name = g_strdup(name);
255 document->root = sp_object_repr_build_tree(document, rroot);
257 sodipodi_version = SP_ROOT(document->root)->version.sodipodi;
259 /* fixme: Not sure about this, but lets assume ::build updates */
260 rroot->setAttribute("sodipodi:version", SODIPODI_VERSION);
261 rroot->setAttribute("inkscape:version", INKSCAPE_VERSION);
262 /* fixme: Again, I moved these here to allow version determining in ::build (Lauris) */
264 /* Quick hack 2 - get default image size into document */
265 if (!rroot->attribute("width")) rroot->setAttribute("width", A4_WIDTH_STR);
266 if (!rroot->attribute("height")) rroot->setAttribute("height", A4_HEIGHT_STR);
267 /* End of quick hack 2 */
269 /* Quick hack 3 - Set uri attributes */
270 if (uri) {
271 rroot->setAttribute("sodipodi:docname", uri);
272 }
273 /* End of quick hack 3 */
275 // creating namedview
276 if (!sp_item_group_get_child_by_name((SPGroup *) document->root, NULL, "sodipodi:namedview")) {
277 // if there's none in the document already,
278 Inkscape::XML::Node *r = NULL;
279 Inkscape::XML::Node *rnew = NULL;
280 r = inkscape_get_repr(INKSCAPE, "template.base");
281 // see if there's a template with id="base" in the preferences
282 if (!r) {
283 // if there's none, create an empty element
284 rnew = rdoc->createElement("sodipodi:namedview");
285 rnew->setAttribute("id", "base");
286 } else {
287 // otherwise, take from preferences
288 rnew = r->duplicate(rroot->document());
289 }
290 // insert into the document
291 rroot->addChild(rnew, NULL);
292 // clean up
293 Inkscape::GC::release(rnew);
294 }
296 /* Defs */
297 if (!SP_ROOT(document->root)->defs) {
298 Inkscape::XML::Node *r;
299 r = rdoc->createElement("svg:defs");
300 rroot->addChild(r, NULL);
301 Inkscape::GC::release(r);
302 g_assert(SP_ROOT(document->root)->defs);
303 }
305 /* Default RDF */
306 rdf_set_defaults( document );
308 if (keepalive) {
309 inkscape_ref();
310 }
312 sp_document_set_undo_sensitive(document, true);
314 // reset undo key when selection changes, so that same-key actions on different objects are not coalesced
315 if (!Inkscape::NSApplication::Application::getNewGui()) {
316 g_signal_connect(G_OBJECT(INKSCAPE), "change_selection",
317 G_CALLBACK(sp_document_reset_key), document);
318 g_signal_connect(G_OBJECT(INKSCAPE), "activate_desktop",
319 G_CALLBACK(sp_document_reset_key), document);
320 } else {
321 document->_selection_changed_connection = Inkscape::NSApplication::Editor::connectSelectionChanged (sigc::mem_fun (*document, &SPDocument::reset_key));
322 document->_desktop_activated_connection = Inkscape::NSApplication::Editor::connectDesktopActivated (sigc::mem_fun (*document, &SPDocument::reset_key));
323 }
325 return document;
326 }
328 /**
329 * Fetches document from URI, or creates new, if NULL; public document
330 * appears in document list.
331 */
332 SPDocument *
333 sp_document_new(gchar const *uri, unsigned int keepalive, bool make_new)
334 {
335 SPDocument *doc;
336 Inkscape::XML::Document *rdoc;
337 gchar *base = NULL;
338 gchar *name = NULL;
340 if (uri) {
341 Inkscape::XML::Node *rroot;
342 gchar *s, *p;
343 /* Try to fetch repr from file */
344 rdoc = sp_repr_read_file(uri, SP_SVG_NS_URI);
345 /* If file cannot be loaded, return NULL without warning */
346 if (rdoc == NULL) return NULL;
347 rroot = rdoc->root();
348 /* If xml file is not svg, return NULL without warning */
349 /* fixme: destroy document */
350 if (strcmp(rroot->name(), "svg:svg") != 0) return NULL;
351 s = g_strdup(uri);
352 p = strrchr(s, '/');
353 if (p) {
354 name = g_strdup(p + 1);
355 p[1] = '\0';
356 base = g_strdup(s);
357 } else {
358 base = NULL;
359 name = g_strdup(uri);
360 }
361 g_free(s);
362 } else {
363 rdoc = sp_repr_document_new("svg:svg");
364 }
366 if (make_new) {
367 base = NULL;
368 uri = NULL;
369 name = g_strdup_printf(_("New document %d"), ++doc_count);
370 }
372 //# These should be set by now
373 g_assert(name);
375 doc = sp_document_create(rdoc, uri, base, name, keepalive);
377 g_free(base);
378 g_free(name);
380 return doc;
381 }
383 SPDocument *
384 sp_document_new_from_mem(gchar const *buffer, gint length, unsigned int keepalive)
385 {
386 SPDocument *doc;
387 Inkscape::XML::Document *rdoc;
388 Inkscape::XML::Node *rroot;
389 gchar *name;
391 rdoc = sp_repr_read_mem(buffer, length, SP_SVG_NS_URI);
393 /* If it cannot be loaded, return NULL without warning */
394 if (rdoc == NULL) return NULL;
396 rroot = rdoc->root();
397 /* If xml file is not svg, return NULL without warning */
398 /* fixme: destroy document */
399 if (strcmp(rroot->name(), "svg:svg") != 0) return NULL;
401 name = g_strdup_printf(_("Memory document %d"), ++doc_count);
403 doc = sp_document_create(rdoc, NULL, NULL, name, keepalive);
405 return doc;
406 }
408 SPDocument *
409 sp_document_ref(SPDocument *doc)
410 {
411 g_return_val_if_fail(doc != NULL, NULL);
412 Inkscape::GC::anchor(doc);
413 return doc;
414 }
416 SPDocument *
417 sp_document_unref(SPDocument *doc)
418 {
419 g_return_val_if_fail(doc != NULL, NULL);
420 Inkscape::GC::release(doc);
421 return NULL;
422 }
424 gdouble sp_document_width(SPDocument *document)
425 {
426 g_return_val_if_fail(document != NULL, 0.0);
427 g_return_val_if_fail(document->priv != NULL, 0.0);
428 g_return_val_if_fail(document->root != NULL, 0.0);
430 return SP_ROOT(document->root)->width.computed;
431 }
433 void
434 sp_document_set_width (SPDocument *document, gdouble width, const SPUnit *unit)
435 {
436 SPRoot *root = SP_ROOT(document->root);
438 if (root->width.unit == SVGLength::PERCENT && root->viewBox_set) { // set to viewBox=
439 root->viewBox.x1 = root->viewBox.x0 + sp_units_get_pixels (width, *unit);
440 } else { // set to width=
441 root->width.computed = sp_units_get_pixels (width, *unit);
442 /* SVG does not support meters as a unit, so we must translate meters to
443 * cm when writing */
444 if (!strcmp(unit->abbr, "m")) {
445 root->width.value = 100*width;
446 root->width.unit = SVGLength::CM;
447 } else {
448 root->width.value = width;
449 root->width.unit = (SVGLength::Unit) sp_unit_get_svg_unit(unit);
450 }
451 }
453 SP_OBJECT (root)->updateRepr();
454 }
456 void sp_document_set_height (SPDocument * document, gdouble height, const SPUnit *unit)
457 {
458 SPRoot *root = SP_ROOT(document->root);
460 if (root->height.unit == SVGLength::PERCENT && root->viewBox_set) { // set to viewBox=
461 root->viewBox.y1 = root->viewBox.y0 + sp_units_get_pixels (height, *unit);
462 } else { // set to height=
463 root->height.computed = sp_units_get_pixels (height, *unit);
464 /* SVG does not support meters as a unit, so we must translate meters to
465 * cm when writing */
466 if (!strcmp(unit->abbr, "m")) {
467 root->height.value = 100*height;
468 root->height.unit = SVGLength::CM;
469 } else {
470 root->height.value = height;
471 root->height.unit = (SVGLength::Unit) sp_unit_get_svg_unit(unit);
472 }
473 }
475 SP_OBJECT (root)->updateRepr();
476 }
478 gdouble sp_document_height(SPDocument *document)
479 {
480 g_return_val_if_fail(document != NULL, 0.0);
481 g_return_val_if_fail(document->priv != NULL, 0.0);
482 g_return_val_if_fail(document->root != NULL, 0.0);
484 return SP_ROOT(document->root)->height.computed;
485 }
487 /**
488 * Given an NR::Rect that may, for example, correspond to the bbox of an object,
489 * this function fits the canvas to that rect by resizing the canvas
490 * and translating the document root into position.
491 */
492 void SPDocument::fitToRect(NR::Rect const &rect)
493 {
494 g_return_if_fail(!rect.isEmpty());
496 using NR::X; using NR::Y;
497 double const w = rect.extent(X);
498 double const h = rect.extent(Y);
500 double const old_height = sp_document_height(this);
501 SPUnit const &px(sp_unit_get_by_id(SP_UNIT_PX));
502 sp_document_set_width(this, w, &px);
503 sp_document_set_height(this, h, &px);
505 NR::translate const tr(NR::Point(0, (old_height - h))
506 - rect.min());
507 SP_GROUP(root)->translateChildItems(tr);
508 }
510 void sp_document_set_uri(SPDocument *document, gchar const *uri)
511 {
512 g_return_if_fail(document != NULL);
514 if (document->name) {
515 g_free(document->name);
516 document->name = NULL;
517 }
518 if (document->base) {
519 g_free(document->base);
520 document->base = NULL;
521 }
522 if (document->uri) {
523 g_free(document->uri);
524 document->uri = NULL;
525 }
527 if (uri) {
529 #ifndef WIN32
530 prepend_current_dir_if_relative(&(document->uri), uri);
531 #else
532 // FIXME: it may be that prepend_current_dir_if_relative works OK on windows too, test!
533 document->uri = g_strdup(uri);
534 #endif
536 /* fixme: Think, what this means for images (Lauris) */
537 document->base = g_path_get_dirname(document->uri);
538 document->name = g_path_get_basename(document->uri);
540 } else {
541 document->uri = g_strdup_printf(_("Unnamed document %d"), ++doc_count);
542 document->base = NULL;
543 document->name = g_strdup(document->uri);
544 }
546 // Update saveable repr attributes.
547 Inkscape::XML::Node *repr = sp_document_repr_root(document);
548 // changing uri in the document repr must not be not undoable
549 bool saved = sp_document_get_undo_sensitive(document);
550 sp_document_set_undo_sensitive(document, false);
552 repr->setAttribute("sodipodi:docname", document->name);
553 sp_document_set_undo_sensitive(document, saved);
555 document->priv->uri_set_signal.emit(document->uri);
556 }
558 void
559 sp_document_resized_signal_emit(SPDocument *doc, gdouble width, gdouble height)
560 {
561 g_return_if_fail(doc != NULL);
563 doc->priv->resized_signal.emit(width, height);
564 }
566 sigc::connection SPDocument::connectModified(SPDocument::ModifiedSignal::slot_type slot)
567 {
568 return priv->modified_signal.connect(slot);
569 }
571 sigc::connection SPDocument::connectURISet(SPDocument::URISetSignal::slot_type slot)
572 {
573 return priv->uri_set_signal.connect(slot);
574 }
576 sigc::connection SPDocument::connectResized(SPDocument::ResizedSignal::slot_type slot)
577 {
578 return priv->resized_signal.connect(slot);
579 }
581 sigc::connection
582 SPDocument::connectReconstructionStart(SPDocument::ReconstructionStart::slot_type slot)
583 {
584 return priv->_reconstruction_start_signal.connect(slot);
585 }
587 void
588 SPDocument::emitReconstructionStart(void)
589 {
590 // printf("Starting Reconstruction\n");
591 priv->_reconstruction_start_signal.emit();
592 return;
593 }
595 sigc::connection
596 SPDocument::connectReconstructionFinish(SPDocument::ReconstructionFinish::slot_type slot)
597 {
598 return priv->_reconstruction_finish_signal.connect(slot);
599 }
601 void
602 SPDocument::emitReconstructionFinish(void)
603 {
604 // printf("Finishing Reconstruction\n");
605 priv->_reconstruction_finish_signal.emit();
606 return;
607 }
609 sigc::connection SPDocument::connectCommit(SPDocument::CommitSignal::slot_type slot)
610 {
611 return priv->commit_signal.connect(slot);
612 }
616 void SPDocument::_emitModified() {
617 static guint const flags = SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG | SP_OBJECT_PARENT_MODIFIED_FLAG;
618 root->emitModified(0);
619 priv->modified_signal.emit(flags);
620 }
622 void SPDocument::bindObjectToId(gchar const *id, SPObject *object) {
623 GQuark idq = g_quark_from_string(id);
625 if (object) {
626 g_assert(g_hash_table_lookup(priv->iddef, GINT_TO_POINTER(idq)) == NULL);
627 g_hash_table_insert(priv->iddef, GINT_TO_POINTER(idq), object);
628 } else {
629 g_assert(g_hash_table_lookup(priv->iddef, GINT_TO_POINTER(idq)) != NULL);
630 g_hash_table_remove(priv->iddef, GINT_TO_POINTER(idq));
631 }
633 SPDocumentPrivate::IDChangedSignalMap::iterator pos;
635 pos = priv->id_changed_signals.find(idq);
636 if ( pos != priv->id_changed_signals.end() ) {
637 if (!(*pos).second.empty()) {
638 (*pos).second.emit(object);
639 } else { // discard unused signal
640 priv->id_changed_signals.erase(pos);
641 }
642 }
643 }
645 void
646 SPDocument::addUndoObserver(Inkscape::UndoStackObserver& observer)
647 {
648 this->priv->undoStackObservers.add(observer);
649 }
651 void
652 SPDocument::removeUndoObserver(Inkscape::UndoStackObserver& observer)
653 {
654 this->priv->undoStackObservers.remove(observer);
655 }
657 SPObject *SPDocument::getObjectById(gchar const *id) {
658 g_return_val_if_fail(id != NULL, NULL);
660 GQuark idq = g_quark_from_string(id);
661 return (SPObject*)g_hash_table_lookup(priv->iddef, GINT_TO_POINTER(idq));
662 }
664 sigc::connection SPDocument::connectIdChanged(gchar const *id,
665 SPDocument::IDChangedSignal::slot_type slot)
666 {
667 return priv->id_changed_signals[g_quark_from_string(id)].connect(slot);
668 }
670 void SPDocument::bindObjectToRepr(Inkscape::XML::Node *repr, SPObject *object) {
671 if (object) {
672 g_assert(g_hash_table_lookup(priv->reprdef, repr) == NULL);
673 g_hash_table_insert(priv->reprdef, repr, object);
674 } else {
675 g_assert(g_hash_table_lookup(priv->reprdef, repr) != NULL);
676 g_hash_table_remove(priv->reprdef, repr);
677 }
678 }
680 SPObject *SPDocument::getObjectByRepr(Inkscape::XML::Node *repr) {
681 g_return_val_if_fail(repr != NULL, NULL);
682 return (SPObject*)g_hash_table_lookup(priv->reprdef, repr);
683 }
685 Glib::ustring SPDocument::getLanguage() {
686 gchar const *document_language = rdf_get_work_entity(this, rdf_find_entity("language"));
687 if (document_language) {
688 while (isspace(*document_language))
689 document_language++;
690 }
691 if ( !document_language || 0 == *document_language) {
692 // retrieve system language
693 document_language = getenv("LC_ALL");
694 if ( NULL == document_language || *document_language == 0 ) {
695 document_language = getenv ("LC_MESSAGES");
696 }
697 if ( NULL == document_language || *document_language == 0 ) {
698 document_language = getenv ("LANG");
699 }
701 if ( NULL != document_language ) {
702 gchar *pos = strchr(document_language, '_');
703 if ( NULL != pos ) {
704 return Glib::ustring(document_language, pos - document_language);
705 }
706 }
707 }
709 if ( NULL == document_language )
710 return Glib::ustring();
711 return document_language;
712 }
714 /* Object modification root handler */
716 void
717 sp_document_request_modified(SPDocument *doc)
718 {
719 if (!doc->modified_id) {
720 doc->modified_id = gtk_idle_add_priority(SP_DOCUMENT_UPDATE_PRIORITY, sp_document_idle_handler, doc);
721 }
722 }
724 void
725 sp_document_setup_viewport (SPDocument *doc, SPItemCtx *ctx)
726 {
727 ctx->ctx.flags = 0;
728 ctx->i2doc = NR::identity();
729 /* Set up viewport in case svg has it defined as percentages */
730 if (SP_ROOT(doc->root)->viewBox_set) { // if set, take from viewBox
731 ctx->vp.x0 = SP_ROOT(doc->root)->viewBox.x0;
732 ctx->vp.y0 = SP_ROOT(doc->root)->viewBox.y0;
733 ctx->vp.x1 = SP_ROOT(doc->root)->viewBox.x1;
734 ctx->vp.y1 = SP_ROOT(doc->root)->viewBox.y1;
735 } else { // as a last resort, set size to A4
736 ctx->vp.x0 = 0.0;
737 ctx->vp.y0 = 0.0;
738 ctx->vp.x1 = 210 * PX_PER_MM;
739 ctx->vp.y1 = 297 * PX_PER_MM;
740 }
741 ctx->i2vp = NR::identity();
742 }
744 /**
745 * Tries to update the document state based on the modified and
746 * "update required" flags, and return true if the document has
747 * been brought fully up to date.
748 */
749 bool
750 SPDocument::_updateDocument()
751 {
752 /* Process updates */
753 if (this->root->uflags || this->root->mflags) {
754 if (this->root->uflags) {
755 SPItemCtx ctx;
756 sp_document_setup_viewport (this, &ctx);
758 bool saved = sp_document_get_undo_sensitive(this);
759 sp_document_set_undo_sensitive(this, false);
761 this->root->updateDisplay((SPCtx *)&ctx, 0);
763 sp_document_set_undo_sensitive(this, saved);
764 }
765 this->_emitModified();
766 }
768 return !(this->root->uflags || this->root->mflags);
769 }
772 /**
773 * Repeatedly works on getting the document updated, since sometimes
774 * it takes more than one pass to get the document updated. But it
775 * usually should not take more than a few loops, and certainly never
776 * more than 32 iterations. So we bail out if we hit 32 iterations,
777 * since this typically indicates we're stuck in an update loop.
778 */
779 gint
780 sp_document_ensure_up_to_date(SPDocument *doc)
781 {
782 int counter = 32;
783 while (!doc->_updateDocument()) {
784 if (counter == 0) {
785 g_warning("More than 32 iteration while updating document '%s'", doc->uri);
786 break;
787 }
788 counter--;
789 }
791 if (doc->modified_id) {
792 /* Remove handler */
793 gtk_idle_remove(doc->modified_id);
794 doc->modified_id = 0;
795 }
796 return counter>0;
797 }
799 /**
800 * An idle handler to update the document. Returns true if
801 * the document needs further updates.
802 */
803 static gint
804 sp_document_idle_handler(gpointer data)
805 {
806 SPDocument *doc = static_cast<SPDocument *>(data);
807 if (doc->_updateDocument()) {
808 doc->modified_id = 0;
809 return false;
810 } else {
811 return true;
812 }
813 }
815 static bool is_within(NR::Rect const &area, NR::Rect const &box)
816 {
817 return area.contains(box);
818 }
820 static bool overlaps(NR::Rect const &area, NR::Rect const &box)
821 {
822 return area.intersects(box);
823 }
825 static GSList *find_items_in_area(GSList *s, SPGroup *group, unsigned int dkey, NR::Rect const &area,
826 bool (*test)(NR::Rect const &, NR::Rect const &), bool take_insensitive = false)
827 {
828 g_return_val_if_fail(SP_IS_GROUP(group), s);
830 for (SPObject *o = sp_object_first_child(SP_OBJECT(group)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
831 if (!SP_IS_ITEM(o)) {
832 continue;
833 }
834 if (SP_IS_GROUP(o) && SP_GROUP(o)->effectiveLayerMode(dkey) == SPGroup::LAYER ) {
835 s = find_items_in_area(s, SP_GROUP(o), dkey, area, test);
836 } else {
837 SPItem *child = SP_ITEM(o);
838 NR::Maybe<NR::Rect> box = sp_item_bbox_desktop(child);
839 if ( box && test(area, *box) && (take_insensitive || child->isVisibleAndUnlocked(dkey))) {
840 s = g_slist_append(s, child);
841 }
842 }
843 }
845 return s;
846 }
848 /**
849 Returns true if an item is among the descendants of group (recursively).
850 */
851 bool item_is_in_group(SPItem *item, SPGroup *group)
852 {
853 for (SPObject *o = sp_object_first_child(SP_OBJECT(group)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
854 if (!SP_IS_ITEM(o)) continue;
855 if (SP_ITEM(o) == item)
856 return true;
857 if (SP_IS_GROUP(o))
858 if (item_is_in_group(item, SP_GROUP(o)))
859 return true;
860 }
861 return false;
862 }
864 /**
865 Returns the bottommost item from the list which is at the point, or NULL if none.
866 */
867 SPItem*
868 sp_document_item_from_list_at_point_bottom(unsigned int dkey, SPGroup *group, GSList const *list,
869 NR::Point const p, bool take_insensitive)
870 {
871 g_return_val_if_fail(group, NULL);
873 gdouble delta = prefs_get_double_attribute ("options.cursortolerance", "value", 1.0);
875 for (SPObject *o = sp_object_first_child(SP_OBJECT(group)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
877 if (!SP_IS_ITEM(o)) continue;
879 SPItem *item = SP_ITEM(o);
880 NRArenaItem *arenaitem = sp_item_get_arenaitem(item, dkey);
881 if (arenaitem && nr_arena_item_invoke_pick(arenaitem, p, delta, 1) != NULL
882 && (take_insensitive || item->isVisibleAndUnlocked(dkey))) {
883 if (g_slist_find((GSList *) list, item) != NULL)
884 return item;
885 }
887 if (SP_IS_GROUP(o)) {
888 SPItem *found = sp_document_item_from_list_at_point_bottom(dkey, SP_GROUP(o), list, p, take_insensitive);
889 if (found)
890 return found;
891 }
893 }
894 return NULL;
895 }
897 /**
898 Returns the topmost (in z-order) item from the descendants of group (recursively) which
899 is at the point p, or NULL if none. Honors into_groups on whether to recurse into
900 non-layer groups or not. Honors take_insensitive on whether to return insensitive
901 items. If upto != NULL, then if item upto is encountered (at any level), stops searching
902 upwards in z-order and returns what it has found so far (i.e. the found item is
903 guaranteed to be lower than upto).
904 */
905 SPItem*
906 find_item_at_point(unsigned int dkey, SPGroup *group, NR::Point const p, gboolean into_groups, bool take_insensitive = false, SPItem *upto = NULL)
907 {
908 SPItem *seen = NULL, *newseen = NULL;
910 gdouble delta = prefs_get_double_attribute ("options.cursortolerance", "value", 1.0);
912 for (SPObject *o = sp_object_first_child(SP_OBJECT(group)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
913 if (!SP_IS_ITEM(o)) continue;
915 if (upto && SP_ITEM(o) == upto)
916 break;
918 if (SP_IS_GROUP(o) && (SP_GROUP(o)->effectiveLayerMode(dkey) == SPGroup::LAYER || into_groups)) {
919 // if nothing found yet, recurse into the group
920 newseen = find_item_at_point(dkey, SP_GROUP(o), p, into_groups, take_insensitive, upto);
921 if (newseen) {
922 seen = newseen;
923 newseen = NULL;
924 }
926 if (item_is_in_group(upto, SP_GROUP(o)))
927 break;
929 } else {
930 SPItem *child = SP_ITEM(o);
931 NRArenaItem *arenaitem = sp_item_get_arenaitem(child, dkey);
933 // seen remembers the last (topmost) of items pickable at this point
934 if (arenaitem && nr_arena_item_invoke_pick(arenaitem, p, delta, 1) != NULL
935 && (take_insensitive || child->isVisibleAndUnlocked(dkey))) {
936 seen = child;
937 }
938 }
939 }
940 return seen;
941 }
943 /**
944 Returns the topmost non-layer group from the descendants of group which is at point
945 p, or NULL if none. Recurses into layers but not into groups.
946 */
947 SPItem*
948 find_group_at_point(unsigned int dkey, SPGroup *group, NR::Point const p)
949 {
950 SPItem *seen = NULL;
952 gdouble delta = prefs_get_double_attribute ("options.cursortolerance", "value", 1.0);
954 for (SPObject *o = sp_object_first_child(SP_OBJECT(group)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
955 if (!SP_IS_ITEM(o)) continue;
956 if (SP_IS_GROUP(o) && SP_GROUP(o)->effectiveLayerMode(dkey) == SPGroup::LAYER) {
957 SPItem *newseen = find_group_at_point(dkey, SP_GROUP(o), p);
958 if (newseen) {
959 seen = newseen;
960 }
961 }
962 if (SP_IS_GROUP(o) && SP_GROUP(o)->effectiveLayerMode(dkey) != SPGroup::LAYER ) {
963 SPItem *child = SP_ITEM(o);
964 NRArenaItem *arenaitem = sp_item_get_arenaitem(child, dkey);
966 // seen remembers the last (topmost) of groups pickable at this point
967 if (arenaitem && nr_arena_item_invoke_pick(arenaitem, p, delta, 1) != NULL) {
968 seen = child;
969 }
970 }
971 }
972 return seen;
973 }
975 /*
976 * Return list of items, contained in box
977 *
978 * Assumes box is normalized (and g_asserts it!)
979 *
980 */
982 GSList *sp_document_items_in_box(SPDocument *document, unsigned int dkey, NR::Rect const &box)
983 {
984 g_return_val_if_fail(document != NULL, NULL);
985 g_return_val_if_fail(document->priv != NULL, NULL);
987 return find_items_in_area(NULL, SP_GROUP(document->root), dkey, box, is_within);
988 }
990 /*
991 * Return list of items, that the parts of the item contained in box
992 *
993 * Assumes box is normalized (and g_asserts it!)
994 *
995 */
997 GSList *sp_document_partial_items_in_box(SPDocument *document, unsigned int dkey, NR::Rect const &box)
998 {
999 g_return_val_if_fail(document != NULL, NULL);
1000 g_return_val_if_fail(document->priv != NULL, NULL);
1002 return find_items_in_area(NULL, SP_GROUP(document->root), dkey, box, overlaps);
1003 }
1005 GSList *
1006 sp_document_items_at_points(SPDocument *document, unsigned const key, std::vector<NR::Point> points)
1007 {
1008 GSList *items = NULL;
1010 // When picking along the path, we don't want small objects close together
1011 // (such as hatching strokes) to obscure each other by their deltas,
1012 // so we temporarily set delta to a small value
1013 gdouble saved_delta = prefs_get_double_attribute ("options.cursortolerance", "value", 1.0);
1014 prefs_set_double_attribute ("options.cursortolerance", "value", 0.25);
1016 for(unsigned int i = 0; i < points.size(); i++) {
1017 SPItem *item = sp_document_item_at_point(document, key, points[i],
1018 false, NULL);
1019 if (item && !g_slist_find(items, item))
1020 items = g_slist_prepend (items, item);
1021 }
1023 // and now we restore it back
1024 prefs_set_double_attribute ("options.cursortolerance", "value", saved_delta);
1026 return items;
1027 }
1029 SPItem *
1030 sp_document_item_at_point(SPDocument *document, unsigned const key, NR::Point const p,
1031 gboolean const into_groups, SPItem *upto)
1032 {
1033 g_return_val_if_fail(document != NULL, NULL);
1034 g_return_val_if_fail(document->priv != NULL, NULL);
1036 return find_item_at_point(key, SP_GROUP(document->root), p, into_groups, false, upto);
1037 }
1039 SPItem*
1040 sp_document_group_at_point(SPDocument *document, unsigned int key, NR::Point const p)
1041 {
1042 g_return_val_if_fail(document != NULL, NULL);
1043 g_return_val_if_fail(document->priv != NULL, NULL);
1045 return find_group_at_point(key, SP_GROUP(document->root), p);
1046 }
1049 /* Resource management */
1051 gboolean
1052 sp_document_add_resource(SPDocument *document, gchar const *key, SPObject *object)
1053 {
1054 GSList *rlist;
1055 GQuark q = g_quark_from_string(key);
1057 g_return_val_if_fail(document != NULL, FALSE);
1058 g_return_val_if_fail(key != NULL, FALSE);
1059 g_return_val_if_fail(*key != '\0', FALSE);
1060 g_return_val_if_fail(object != NULL, FALSE);
1061 g_return_val_if_fail(SP_IS_OBJECT(object), FALSE);
1063 if (SP_OBJECT_IS_CLONED(object))
1064 return FALSE;
1066 rlist = (GSList*)g_hash_table_lookup(document->priv->resources, key);
1067 g_return_val_if_fail(!g_slist_find(rlist, object), FALSE);
1068 rlist = g_slist_prepend(rlist, object);
1069 g_hash_table_insert(document->priv->resources, (gpointer) key, rlist);
1071 document->priv->resources_changed_signals[q].emit();
1073 return TRUE;
1074 }
1076 gboolean
1077 sp_document_remove_resource(SPDocument *document, gchar const *key, SPObject *object)
1078 {
1079 GSList *rlist;
1080 GQuark q = g_quark_from_string(key);
1082 g_return_val_if_fail(document != NULL, FALSE);
1083 g_return_val_if_fail(key != NULL, FALSE);
1084 g_return_val_if_fail(*key != '\0', FALSE);
1085 g_return_val_if_fail(object != NULL, FALSE);
1086 g_return_val_if_fail(SP_IS_OBJECT(object), FALSE);
1088 if (SP_OBJECT_IS_CLONED(object))
1089 return FALSE;
1091 rlist = (GSList*)g_hash_table_lookup(document->priv->resources, key);
1092 g_return_val_if_fail(rlist != NULL, FALSE);
1093 g_return_val_if_fail(g_slist_find(rlist, object), FALSE);
1094 rlist = g_slist_remove(rlist, object);
1095 g_hash_table_insert(document->priv->resources, (gpointer) key, rlist);
1097 document->priv->resources_changed_signals[q].emit();
1099 return TRUE;
1100 }
1102 GSList const *
1103 sp_document_get_resource_list(SPDocument *document, gchar const *key)
1104 {
1105 g_return_val_if_fail(document != NULL, NULL);
1106 g_return_val_if_fail(key != NULL, NULL);
1107 g_return_val_if_fail(*key != '\0', NULL);
1109 return (GSList*)g_hash_table_lookup(document->priv->resources, key);
1110 }
1112 sigc::connection sp_document_resources_changed_connect(SPDocument *document,
1113 gchar const *key,
1114 SPDocument::ResourcesChangedSignal::slot_type slot)
1115 {
1116 GQuark q = g_quark_from_string(key);
1117 return document->priv->resources_changed_signals[q].connect(slot);
1118 }
1120 /* Helpers */
1122 gboolean
1123 sp_document_resource_list_free(gpointer key, gpointer value, gpointer data)
1124 {
1125 g_slist_free((GSList *) value);
1126 return TRUE;
1127 }
1129 unsigned int
1130 count_objects_recursive(SPObject *obj, unsigned int count)
1131 {
1132 count++; // obj itself
1134 for (SPObject *i = sp_object_first_child(obj); i != NULL; i = SP_OBJECT_NEXT(i)) {
1135 count = count_objects_recursive(i, count);
1136 }
1138 return count;
1139 }
1141 unsigned int
1142 objects_in_document(SPDocument *document)
1143 {
1144 return count_objects_recursive(SP_DOCUMENT_ROOT(document), 0);
1145 }
1147 void
1148 vacuum_document_recursive(SPObject *obj)
1149 {
1150 if (SP_IS_DEFS(obj)) {
1151 for (SPObject *def = obj->firstChild(); def; def = SP_OBJECT_NEXT(def)) {
1152 /* fixme: some inkscape-internal nodes in the future might not be collectable */
1153 def->requestOrphanCollection();
1154 }
1155 } else {
1156 for (SPObject *i = sp_object_first_child(obj); i != NULL; i = SP_OBJECT_NEXT(i)) {
1157 vacuum_document_recursive(i);
1158 }
1159 }
1160 }
1162 unsigned int
1163 vacuum_document(SPDocument *document)
1164 {
1165 unsigned int start = objects_in_document(document);
1166 unsigned int end;
1167 unsigned int newend = start;
1169 unsigned int iterations = 0;
1171 do {
1172 end = newend;
1174 vacuum_document_recursive(SP_DOCUMENT_ROOT(document));
1175 document->collectOrphans();
1176 iterations++;
1178 newend = objects_in_document(document);
1180 } while (iterations < 100 && newend < end);
1182 return start - newend;
1183 }
1185 bool SPDocument::isSeeking() const {
1186 return priv->seeking;
1187 }
1190 /*
1191 Local Variables:
1192 mode:c++
1193 c-file-style:"stroustrup"
1194 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1195 indent-tabs-mode:nil
1196 fill-column:99
1197 End:
1198 */
1199 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :