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 SPDocument::SPDocument() {
74 SPDocumentPrivate *p;
76 keepalive = FALSE;
77 virgin = TRUE;
79 modified_id = 0;
81 rdoc = NULL;
82 rroot = NULL;
83 root = NULL;
84 style_cascade = cr_cascade_new(NULL, NULL, NULL);
86 uri = NULL;
87 base = NULL;
88 name = NULL;
90 _collection_queue = NULL;
92 // Initialise instance of connector router.
93 router = new Avoid::Router();
94 // Don't use the Consolidate moves optimisation.
95 router->ConsolidateMoves = false;
97 p = new SPDocumentPrivate();
99 p->iddef = g_hash_table_new(g_direct_hash, g_direct_equal);
100 p->reprdef = g_hash_table_new(g_direct_hash, g_direct_equal);
102 p->resources = g_hash_table_new(g_str_hash, g_str_equal);
104 p->sensitive = FALSE;
105 p->partial = NULL;
106 p->history_size = 0;
107 p->undo = NULL;
108 p->redo = NULL;
109 p->seeking = false;
111 priv = p;
113 // XXX only for testing!
114 priv->undoStackObservers.add(p->console_output_undo_observer);
115 }
117 SPDocument::~SPDocument() {
118 collectOrphans();
120 if (priv) {
121 inkscape_remove_document(this);
123 if (priv->partial) {
124 sp_repr_free_log(priv->partial);
125 priv->partial = NULL;
126 }
128 sp_document_clear_redo(this);
129 sp_document_clear_undo(this);
131 if (root) {
132 root->releaseReferences();
133 sp_object_unref(root);
134 root = NULL;
135 }
137 if (priv->iddef) g_hash_table_destroy(priv->iddef);
138 if (priv->reprdef) g_hash_table_destroy(priv->reprdef);
140 if (rdoc) Inkscape::GC::release(rdoc);
142 /* Free resources */
143 g_hash_table_foreach_remove(priv->resources, sp_document_resource_list_free, this);
144 g_hash_table_destroy(priv->resources);
146 delete priv;
147 priv = NULL;
148 }
150 cr_cascade_unref(style_cascade);
151 style_cascade = NULL;
153 if (name) {
154 g_free(name);
155 name = NULL;
156 }
157 if (base) {
158 g_free(base);
159 base = NULL;
160 }
161 if (uri) {
162 g_free(uri);
163 uri = NULL;
164 }
166 if (modified_id) {
167 gtk_idle_remove(modified_id);
168 modified_id = 0;
169 }
171 _selection_changed_connection.disconnect();
172 _desktop_activated_connection.disconnect();
174 if (keepalive) {
175 inkscape_unref();
176 keepalive = FALSE;
177 }
179 if (router) {
180 delete router;
181 router = NULL;
182 }
184 //delete this->_whiteboard_session_manager;
185 }
187 void SPDocument::queueForOrphanCollection(SPObject *object) {
188 g_return_if_fail(object != NULL);
189 g_return_if_fail(SP_OBJECT_DOCUMENT(object) == this);
191 sp_object_ref(object, NULL);
192 _collection_queue = g_slist_prepend(_collection_queue, object);
193 }
195 void SPDocument::collectOrphans() {
196 while (_collection_queue) {
197 GSList *objects=_collection_queue;
198 _collection_queue = NULL;
199 for ( GSList *iter=objects ; iter ; iter = iter->next ) {
200 SPObject *object=reinterpret_cast<SPObject *>(iter->data);
201 object->collectOrphan();
202 sp_object_unref(object, NULL);
203 }
204 g_slist_free(objects);
205 }
206 }
208 void SPDocument::reset_key (void *dummy)
209 {
210 actionkey = NULL;
211 }
213 SPDocument *
214 sp_document_create(Inkscape::XML::Document *rdoc,
215 gchar const *uri,
216 gchar const *base,
217 gchar const *name,
218 unsigned int keepalive)
219 {
220 SPDocument *document;
221 Inkscape::XML::Node *rroot;
222 Inkscape::Version sodipodi_version;
224 rroot = rdoc->root();
226 document = new SPDocument();
228 document->keepalive = keepalive;
230 document->rdoc = rdoc;
231 document->rroot = rroot;
233 #ifndef WIN32
234 prepend_current_dir_if_relative(&(document->uri), uri);
235 #else
236 // FIXME: it may be that prepend_current_dir_if_relative works OK on windows too, test!
237 document->uri = uri? g_strdup(uri) : NULL;
238 #endif
240 // base is simply the part of the path before filename; e.g. when running "inkscape ../file.svg" the base is "../"
241 // which is why we use g_get_current_dir() in calculating the abs path above
242 //This is NULL for a new document
243 if (base)
244 document->base = g_strdup(base);
245 else
246 document->base = NULL;
247 document->name = g_strdup(name);
249 document->root = sp_object_repr_build_tree(document, rroot);
251 sodipodi_version = SP_ROOT(document->root)->version.sodipodi;
253 /* fixme: Not sure about this, but lets assume ::build updates */
254 rroot->setAttribute("sodipodi:version", SODIPODI_VERSION);
255 rroot->setAttribute("inkscape:version", INKSCAPE_VERSION);
256 /* fixme: Again, I moved these here to allow version determining in ::build (Lauris) */
258 /* Quick hack 2 - get default image size into document */
259 if (!rroot->attribute("width")) rroot->setAttribute("width", A4_WIDTH_STR);
260 if (!rroot->attribute("height")) rroot->setAttribute("height", A4_HEIGHT_STR);
261 /* End of quick hack 2 */
263 /* Quick hack 3 - Set uri attributes */
264 if (uri) {
265 rroot->setAttribute("sodipodi:docname", uri);
266 }
267 /* End of quick hack 3 */
269 // creating namedview
270 if (!sp_item_group_get_child_by_name((SPGroup *) document->root, NULL, "sodipodi:namedview")) {
271 // if there's none in the document already,
272 Inkscape::XML::Node *r = NULL;
273 Inkscape::XML::Node *rnew = NULL;
274 r = inkscape_get_repr(INKSCAPE, "template.base");
275 // see if there's a template with id="base" in the preferences
276 if (!r) {
277 // if there's none, create an empty element
278 rnew = rdoc->createElement("sodipodi:namedview");
279 rnew->setAttribute("id", "base");
280 } else {
281 // otherwise, take from preferences
282 rnew = r->duplicate(rroot->document());
283 }
284 // insert into the document
285 rroot->addChild(rnew, NULL);
286 // clean up
287 Inkscape::GC::release(rnew);
288 }
290 /* Defs */
291 if (!SP_ROOT(document->root)->defs) {
292 Inkscape::XML::Node *r;
293 r = rdoc->createElement("svg:defs");
294 rroot->addChild(r, NULL);
295 Inkscape::GC::release(r);
296 g_assert(SP_ROOT(document->root)->defs);
297 }
299 /* Default RDF */
300 rdf_set_defaults( document );
302 if (keepalive) {
303 inkscape_ref();
304 }
306 sp_document_set_undo_sensitive(document, true);
308 // reset undo key when selection changes, so that same-key actions on different objects are not coalesced
309 if (!Inkscape::NSApplication::Application::getNewGui()) {
310 g_signal_connect(G_OBJECT(INKSCAPE), "change_selection",
311 G_CALLBACK(sp_document_reset_key), document);
312 g_signal_connect(G_OBJECT(INKSCAPE), "activate_desktop",
313 G_CALLBACK(sp_document_reset_key), document);
314 } else {
315 document->_selection_changed_connection = Inkscape::NSApplication::Editor::connectSelectionChanged (sigc::mem_fun (*document, &SPDocument::reset_key));
316 document->_desktop_activated_connection = Inkscape::NSApplication::Editor::connectDesktopActivated (sigc::mem_fun (*document, &SPDocument::reset_key));
317 }
318 inkscape_add_document(document);
320 return document;
321 }
323 /**
324 * Fetches document from URI, or creates new, if NULL; public document
325 * appears in document list.
326 */
327 SPDocument *
328 sp_document_new(gchar const *uri, unsigned int keepalive, bool make_new)
329 {
330 SPDocument *doc;
331 Inkscape::XML::Document *rdoc;
332 gchar *base = NULL;
333 gchar *name = NULL;
335 if (uri) {
336 Inkscape::XML::Node *rroot;
337 gchar *s, *p;
338 /* Try to fetch repr from file */
339 rdoc = sp_repr_read_file(uri, SP_SVG_NS_URI);
340 /* If file cannot be loaded, return NULL without warning */
341 if (rdoc == NULL) return NULL;
342 rroot = rdoc->root();
343 /* If xml file is not svg, return NULL without warning */
344 /* fixme: destroy document */
345 if (strcmp(rroot->name(), "svg:svg") != 0) return NULL;
346 s = g_strdup(uri);
347 p = strrchr(s, '/');
348 if (p) {
349 name = g_strdup(p + 1);
350 p[1] = '\0';
351 base = g_strdup(s);
352 } else {
353 base = NULL;
354 name = g_strdup(uri);
355 }
356 g_free(s);
357 } else {
358 rdoc = sp_repr_document_new("svg:svg");
359 }
361 if (make_new) {
362 base = NULL;
363 uri = NULL;
364 name = g_strdup_printf(_("New document %d"), ++doc_count);
365 }
367 //# These should be set by now
368 g_assert(name);
370 doc = sp_document_create(rdoc, uri, base, name, keepalive);
372 g_free(base);
373 g_free(name);
375 return doc;
376 }
378 SPDocument *
379 sp_document_new_from_mem(gchar const *buffer, gint length, unsigned int keepalive)
380 {
381 SPDocument *doc;
382 Inkscape::XML::Document *rdoc;
383 Inkscape::XML::Node *rroot;
384 gchar *name;
386 rdoc = sp_repr_read_mem(buffer, length, SP_SVG_NS_URI);
388 /* If it cannot be loaded, return NULL without warning */
389 if (rdoc == NULL) return NULL;
391 rroot = rdoc->root();
392 /* If xml file is not svg, return NULL without warning */
393 /* fixme: destroy document */
394 if (strcmp(rroot->name(), "svg:svg") != 0) return NULL;
396 name = g_strdup_printf(_("Memory document %d"), ++doc_count);
398 doc = sp_document_create(rdoc, NULL, NULL, name, keepalive);
400 return doc;
401 }
403 SPDocument *sp_document_new_dummy() {
404 SPDocument *document = new SPDocument();
405 inkscape_add_document(document);
406 return document;
407 }
409 SPDocument *
410 sp_document_ref(SPDocument *doc)
411 {
412 g_return_val_if_fail(doc != NULL, NULL);
413 Inkscape::GC::anchor(doc);
414 return doc;
415 }
417 SPDocument *
418 sp_document_unref(SPDocument *doc)
419 {
420 g_return_val_if_fail(doc != NULL, NULL);
421 Inkscape::GC::release(doc);
422 return NULL;
423 }
425 gdouble sp_document_width(SPDocument *document)
426 {
427 g_return_val_if_fail(document != NULL, 0.0);
428 g_return_val_if_fail(document->priv != NULL, 0.0);
429 g_return_val_if_fail(document->root != NULL, 0.0);
431 return SP_ROOT(document->root)->width.computed;
432 }
434 void
435 sp_document_set_width (SPDocument *document, gdouble width, const SPUnit *unit)
436 {
437 SPRoot *root = SP_ROOT(document->root);
439 if (root->width.unit == SVGLength::PERCENT && root->viewBox_set) { // set to viewBox=
440 root->viewBox.x1 = root->viewBox.x0 + sp_units_get_pixels (width, *unit);
441 } else { // set to width=
442 root->width.computed = sp_units_get_pixels (width, *unit);
443 /* SVG does not support meters as a unit, so we must translate meters to
444 * cm when writing */
445 if (!strcmp(unit->abbr, "m")) {
446 root->width.value = 100*width;
447 root->width.unit = SVGLength::CM;
448 } else {
449 root->width.value = width;
450 root->width.unit = (SVGLength::Unit) sp_unit_get_svg_unit(unit);
451 }
452 }
454 SP_OBJECT (root)->updateRepr();
455 }
457 void sp_document_set_height (SPDocument * document, gdouble height, const SPUnit *unit)
458 {
459 SPRoot *root = SP_ROOT(document->root);
461 if (root->height.unit == SVGLength::PERCENT && root->viewBox_set) { // set to viewBox=
462 root->viewBox.y1 = root->viewBox.y0 + sp_units_get_pixels (height, *unit);
463 } else { // set to height=
464 root->height.computed = sp_units_get_pixels (height, *unit);
465 /* SVG does not support meters as a unit, so we must translate meters to
466 * cm when writing */
467 if (!strcmp(unit->abbr, "m")) {
468 root->height.value = 100*height;
469 root->height.unit = SVGLength::CM;
470 } else {
471 root->height.value = height;
472 root->height.unit = (SVGLength::Unit) sp_unit_get_svg_unit(unit);
473 }
474 }
476 SP_OBJECT (root)->updateRepr();
477 }
479 gdouble sp_document_height(SPDocument *document)
480 {
481 g_return_val_if_fail(document != NULL, 0.0);
482 g_return_val_if_fail(document->priv != NULL, 0.0);
483 g_return_val_if_fail(document->root != NULL, 0.0);
485 return SP_ROOT(document->root)->height.computed;
486 }
488 /**
489 * Given an NRRect that may, for example, correspond to the bbox of an object
490 * this function fits the canvas to that rect by resizing the canvas
491 * and translating the document root into position.
492 */
493 void SPDocument::fitToRect(NRRect const & rect)
494 {
495 g_return_if_fail(!nr_rect_d_test_empty(&rect));
497 gdouble w = rect.x1 - rect.x0;
498 gdouble h = rect.y1 - rect.y0;
499 gdouble old_height = sp_document_height(this);
500 SPUnit unit = sp_unit_get_by_id(SP_UNIT_PX);
501 sp_document_set_width(this, w, &unit);
502 sp_document_set_height(this, h, &unit);
504 NR::translate tr = NR::translate::translate(-rect.x0,-(rect.y0 + (h - old_height)));
505 static_cast<SPGroup *>(root)->translateChildItems(tr);
506 }
508 void sp_document_set_uri(SPDocument *document, gchar const *uri)
509 {
510 g_return_if_fail(document != NULL);
512 if (document->name) {
513 g_free(document->name);
514 document->name = NULL;
515 }
516 if (document->base) {
517 g_free(document->base);
518 document->base = NULL;
519 }
520 if (document->uri) {
521 g_free(document->uri);
522 document->uri = NULL;
523 }
525 if (uri) {
527 #ifndef WIN32
528 prepend_current_dir_if_relative(&(document->uri), uri);
529 #else
530 // FIXME: it may be that prepend_current_dir_if_relative works OK on windows too, test!
531 document->uri = g_strdup(uri);
532 #endif
534 /* fixme: Think, what this means for images (Lauris) */
535 document->base = g_path_get_dirname(document->uri);
536 document->name = g_path_get_basename(document->uri);
538 } else {
539 document->uri = g_strdup_printf(_("Unnamed document %d"), ++doc_count);
540 document->base = NULL;
541 document->name = g_strdup(document->uri);
542 }
544 // Update saveable repr attributes.
545 Inkscape::XML::Node *repr = sp_document_repr_root(document);
546 // changing uri in the document repr must not be not undoable
547 bool saved = sp_document_get_undo_sensitive(document);
548 sp_document_set_undo_sensitive(document, false);
550 repr->setAttribute("sodipodi:docname", document->name);
551 sp_document_set_undo_sensitive(document, saved);
553 document->priv->uri_set_signal.emit(document->uri);
554 }
556 void
557 sp_document_resized_signal_emit(SPDocument *doc, gdouble width, gdouble height)
558 {
559 g_return_if_fail(doc != NULL);
561 doc->priv->resized_signal.emit(width, height);
562 }
564 sigc::connection SPDocument::connectModified(SPDocument::ModifiedSignal::slot_type slot)
565 {
566 return priv->modified_signal.connect(slot);
567 }
569 sigc::connection SPDocument::connectURISet(SPDocument::URISetSignal::slot_type slot)
570 {
571 return priv->uri_set_signal.connect(slot);
572 }
574 sigc::connection SPDocument::connectResized(SPDocument::ResizedSignal::slot_type slot)
575 {
576 return priv->resized_signal.connect(slot);
577 }
579 sigc::connection
580 SPDocument::connectReconstructionStart(SPDocument::ReconstructionStart::slot_type slot)
581 {
582 return priv->_reconstruction_start_signal.connect(slot);
583 }
585 void
586 SPDocument::emitReconstructionStart(void)
587 {
588 // printf("Starting Reconstruction\n");
589 priv->_reconstruction_start_signal.emit();
590 return;
591 }
593 sigc::connection
594 SPDocument::connectReconstructionFinish(SPDocument::ReconstructionFinish::slot_type slot)
595 {
596 return priv->_reconstruction_finish_signal.connect(slot);
597 }
599 void
600 SPDocument::emitReconstructionFinish(void)
601 {
602 // printf("Finishing Reconstruction\n");
603 priv->_reconstruction_finish_signal.emit();
604 return;
605 }
607 sigc::connection SPDocument::connectCommit(SPDocument::CommitSignal::slot_type slot)
608 {
609 return priv->commit_signal.connect(slot);
610 }
614 void SPDocument::_emitModified() {
615 static guint const flags = SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG | SP_OBJECT_PARENT_MODIFIED_FLAG;
616 root->emitModified(0);
617 priv->modified_signal.emit(flags);
618 }
620 void SPDocument::bindObjectToId(gchar const *id, SPObject *object) {
621 GQuark idq = g_quark_from_string(id);
623 if (object) {
624 g_assert(g_hash_table_lookup(priv->iddef, GINT_TO_POINTER(idq)) == NULL);
625 g_hash_table_insert(priv->iddef, GINT_TO_POINTER(idq), object);
626 } else {
627 g_assert(g_hash_table_lookup(priv->iddef, GINT_TO_POINTER(idq)) != NULL);
628 g_hash_table_remove(priv->iddef, GINT_TO_POINTER(idq));
629 }
631 SPDocumentPrivate::IDChangedSignalMap::iterator pos;
633 pos = priv->id_changed_signals.find(idq);
634 if ( pos != priv->id_changed_signals.end() ) {
635 if (!(*pos).second.empty()) {
636 (*pos).second.emit(object);
637 } else { // discard unused signal
638 priv->id_changed_signals.erase(pos);
639 }
640 }
641 }
643 void
644 SPDocument::addUndoObserver(Inkscape::UndoStackObserver& observer)
645 {
646 this->priv->undoStackObservers.add(observer);
647 }
649 void
650 SPDocument::removeUndoObserver(Inkscape::UndoStackObserver& observer)
651 {
652 this->priv->undoStackObservers.remove(observer);
653 }
655 SPObject *SPDocument::getObjectById(gchar const *id) {
656 g_return_val_if_fail(id != NULL, NULL);
658 GQuark idq = g_quark_from_string(id);
659 return (SPObject*)g_hash_table_lookup(priv->iddef, GINT_TO_POINTER(idq));
660 }
662 sigc::connection SPDocument::connectIdChanged(gchar const *id,
663 SPDocument::IDChangedSignal::slot_type slot)
664 {
665 return priv->id_changed_signals[g_quark_from_string(id)].connect(slot);
666 }
668 void SPDocument::bindObjectToRepr(Inkscape::XML::Node *repr, SPObject *object) {
669 if (object) {
670 g_assert(g_hash_table_lookup(priv->reprdef, repr) == NULL);
671 g_hash_table_insert(priv->reprdef, repr, object);
672 } else {
673 g_assert(g_hash_table_lookup(priv->reprdef, repr) != NULL);
674 g_hash_table_remove(priv->reprdef, repr);
675 }
676 }
678 SPObject *SPDocument::getObjectByRepr(Inkscape::XML::Node *repr) {
679 g_return_val_if_fail(repr != NULL, NULL);
680 return (SPObject*)g_hash_table_lookup(priv->reprdef, repr);
681 }
683 Glib::ustring SPDocument::getLanguage() {
684 gchar const *document_language = rdf_get_work_entity(this, rdf_find_entity("language"));
685 if (document_language) {
686 while (isspace(*document_language))
687 document_language++;
688 }
689 if ( !document_language || 0 == *document_language) {
690 // retrieve system language
691 document_language = getenv("LC_ALL");
692 if ( NULL == document_language || *document_language == 0 ) {
693 document_language = getenv ("LC_MESSAGES");
694 }
695 if ( NULL == document_language || *document_language == 0 ) {
696 document_language = getenv ("LANG");
697 }
699 if ( NULL != document_language ) {
700 gchar *pos = strchr(document_language, '_');
701 if ( NULL != pos ) {
702 return Glib::ustring(document_language, pos - document_language);
703 }
704 }
705 }
707 if ( NULL == document_language )
708 return Glib::ustring();
709 return document_language;
710 }
712 /* Object modification root handler */
714 void
715 sp_document_request_modified(SPDocument *doc)
716 {
717 if (!doc->modified_id) {
718 doc->modified_id = gtk_idle_add_priority(SP_DOCUMENT_UPDATE_PRIORITY, sp_document_idle_handler, doc);
719 }
720 }
722 void
723 sp_document_setup_viewport (SPDocument *doc, SPItemCtx *ctx)
724 {
725 ctx->ctx.flags = 0;
726 ctx->i2doc = NR::identity();
727 /* Set up viewport in case svg has it defined as percentages */
728 if (SP_ROOT(doc->root)->viewBox_set) { // if set, take from viewBox
729 ctx->vp.x0 = SP_ROOT(doc->root)->viewBox.x0;
730 ctx->vp.y0 = SP_ROOT(doc->root)->viewBox.y0;
731 ctx->vp.x1 = SP_ROOT(doc->root)->viewBox.x1;
732 ctx->vp.y1 = SP_ROOT(doc->root)->viewBox.y1;
733 } else { // as a last resort, set size to A4
734 ctx->vp.x0 = 0.0;
735 ctx->vp.y0 = 0.0;
736 ctx->vp.x1 = 210 * PX_PER_MM;
737 ctx->vp.y1 = 297 * PX_PER_MM;
738 }
739 ctx->i2vp = NR::identity();
740 }
742 /**
743 * Tries to update the document state based on the modified and
744 * "update required" flags, and return true if the document has
745 * been brought fully up to date.
746 */
747 bool
748 SPDocument::_updateDocument()
749 {
750 /* Process updates */
751 if (this->root->uflags || this->root->mflags) {
752 if (this->root->uflags) {
753 SPItemCtx ctx;
754 sp_document_setup_viewport (this, &ctx);
756 bool saved = sp_document_get_undo_sensitive(this);
757 sp_document_set_undo_sensitive(this, false);
759 this->root->updateDisplay((SPCtx *)&ctx, 0);
761 sp_document_set_undo_sensitive(this, saved);
762 }
763 this->_emitModified();
764 }
766 return !(this->root->uflags || this->root->mflags);
767 }
770 /**
771 * Repeatedly works on getting the document updated, since sometimes
772 * it takes more than one pass to get the document updated. But it
773 * usually should not take more than a few loops, and certainly never
774 * more than 32 iterations. So we bail out if we hit 32 iterations,
775 * since this typically indicates we're stuck in an update loop.
776 */
777 gint
778 sp_document_ensure_up_to_date(SPDocument *doc)
779 {
780 int counter = 32;
781 while (!doc->_updateDocument()) {
782 if (counter == 0) {
783 g_warning("More than 32 iteration while updating document '%s'", doc->uri);
784 break;
785 }
786 counter--;
787 }
789 if (doc->modified_id) {
790 /* Remove handler */
791 gtk_idle_remove(doc->modified_id);
792 doc->modified_id = 0;
793 }
794 return counter>0;
795 }
797 /**
798 * An idle handler to update the document. Returns true if
799 * the document needs further updates.
800 */
801 static gint
802 sp_document_idle_handler(gpointer data)
803 {
804 SPDocument *doc = static_cast<SPDocument *>(data);
805 if (doc->_updateDocument()) {
806 doc->modified_id = 0;
807 return false;
808 } else {
809 return true;
810 }
811 }
813 static bool is_within(NR::Rect const &area, NR::Rect const &box)
814 {
815 return area.contains(box);
816 }
818 static bool overlaps(NR::Rect const &area, NR::Rect const &box)
819 {
820 return area.intersects(box);
821 }
823 static GSList *find_items_in_area(GSList *s, SPGroup *group, unsigned int dkey, NR::Rect const &area,
824 bool (*test)(NR::Rect const &, NR::Rect const &), bool take_insensitive = false)
825 {
826 g_return_val_if_fail(SP_IS_GROUP(group), s);
828 for (SPObject *o = sp_object_first_child(SP_OBJECT(group)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
829 if (!SP_IS_ITEM(o)) {
830 continue;
831 }
832 if (SP_IS_GROUP(o) && SP_GROUP(o)->effectiveLayerMode(dkey) == SPGroup::LAYER ) {
833 s = find_items_in_area(s, SP_GROUP(o), dkey, area, test);
834 } else {
835 SPItem *child = SP_ITEM(o);
836 NR::Maybe<NR::Rect> box = sp_item_bbox_desktop(child);
837 if ( box && test(area, *box) && (take_insensitive || child->isVisibleAndUnlocked(dkey))) {
838 s = g_slist_append(s, child);
839 }
840 }
841 }
843 return s;
844 }
846 /**
847 Returns true if an item is among the descendants of group (recursively).
848 */
849 bool item_is_in_group(SPItem *item, SPGroup *group)
850 {
851 for (SPObject *o = sp_object_first_child(SP_OBJECT(group)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
852 if (!SP_IS_ITEM(o)) continue;
853 if (SP_ITEM(o) == item)
854 return true;
855 if (SP_IS_GROUP(o))
856 if (item_is_in_group(item, SP_GROUP(o)))
857 return true;
858 }
859 return false;
860 }
862 /**
863 Returns the bottommost item from the list which is at the point, or NULL if none.
864 */
865 SPItem*
866 sp_document_item_from_list_at_point_bottom(unsigned int dkey, SPGroup *group, GSList const *list,
867 NR::Point const p, bool take_insensitive)
868 {
869 g_return_val_if_fail(group, NULL);
871 gdouble delta = prefs_get_double_attribute ("options.cursortolerance", "value", 1.0);
873 for (SPObject *o = sp_object_first_child(SP_OBJECT(group)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
875 if (!SP_IS_ITEM(o)) continue;
877 SPItem *item = SP_ITEM(o);
878 NRArenaItem *arenaitem = sp_item_get_arenaitem(item, dkey);
879 if (arenaitem && nr_arena_item_invoke_pick(arenaitem, p, delta, 1) != NULL
880 && (take_insensitive || item->isVisibleAndUnlocked(dkey))) {
881 if (g_slist_find((GSList *) list, item) != NULL)
882 return item;
883 }
885 if (SP_IS_GROUP(o)) {
886 SPItem *found = sp_document_item_from_list_at_point_bottom(dkey, SP_GROUP(o), list, p, take_insensitive);
887 if (found)
888 return found;
889 }
891 }
892 return NULL;
893 }
895 /**
896 Returns the topmost (in z-order) item from the descendants of group (recursively) which
897 is at the point p, or NULL if none. Honors into_groups on whether to recurse into
898 non-layer groups or not. Honors take_insensitive on whether to return insensitive
899 items. If upto != NULL, then if item upto is encountered (at any level), stops searching
900 upwards in z-order and returns what it has found so far (i.e. the found item is
901 guaranteed to be lower than upto).
902 */
903 SPItem*
904 find_item_at_point(unsigned int dkey, SPGroup *group, NR::Point const p, gboolean into_groups, bool take_insensitive = false, SPItem *upto = NULL)
905 {
906 SPItem *seen = NULL, *newseen = NULL;
908 gdouble delta = prefs_get_double_attribute ("options.cursortolerance", "value", 1.0);
910 for (SPObject *o = sp_object_first_child(SP_OBJECT(group)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
911 if (!SP_IS_ITEM(o)) continue;
913 if (upto && SP_ITEM(o) == upto)
914 break;
916 if (SP_IS_GROUP(o) && (SP_GROUP(o)->effectiveLayerMode(dkey) == SPGroup::LAYER || into_groups)) {
917 // if nothing found yet, recurse into the group
918 newseen = find_item_at_point(dkey, SP_GROUP(o), p, into_groups, take_insensitive, upto);
919 if (newseen) {
920 seen = newseen;
921 newseen = NULL;
922 }
924 if (item_is_in_group(upto, SP_GROUP(o)))
925 break;
927 } else {
928 SPItem *child = SP_ITEM(o);
929 NRArenaItem *arenaitem = sp_item_get_arenaitem(child, dkey);
931 // seen remembers the last (topmost) of items pickable at this point
932 if (arenaitem && nr_arena_item_invoke_pick(arenaitem, p, delta, 1) != NULL
933 && (take_insensitive || child->isVisibleAndUnlocked(dkey))) {
934 seen = child;
935 }
936 }
937 }
938 return seen;
939 }
941 /**
942 Returns the topmost non-layer group from the descendants of group which is at point
943 p, or NULL if none. Recurses into layers but not into groups.
944 */
945 SPItem*
946 find_group_at_point(unsigned int dkey, SPGroup *group, NR::Point const p)
947 {
948 SPItem *seen = NULL;
950 gdouble delta = prefs_get_double_attribute ("options.cursortolerance", "value", 1.0);
952 for (SPObject *o = sp_object_first_child(SP_OBJECT(group)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
953 if (!SP_IS_ITEM(o)) continue;
954 if (SP_IS_GROUP(o) && SP_GROUP(o)->effectiveLayerMode(dkey) == SPGroup::LAYER) {
955 SPItem *newseen = find_group_at_point(dkey, SP_GROUP(o), p);
956 if (newseen) {
957 seen = newseen;
958 }
959 }
960 if (SP_IS_GROUP(o) && SP_GROUP(o)->effectiveLayerMode(dkey) != SPGroup::LAYER ) {
961 SPItem *child = SP_ITEM(o);
962 NRArenaItem *arenaitem = sp_item_get_arenaitem(child, dkey);
964 // seen remembers the last (topmost) of groups pickable at this point
965 if (arenaitem && nr_arena_item_invoke_pick(arenaitem, p, delta, 1) != NULL) {
966 seen = child;
967 }
968 }
969 }
970 return seen;
971 }
973 /*
974 * Return list of items, contained in box
975 *
976 * Assumes box is normalized (and g_asserts it!)
977 *
978 */
980 GSList *sp_document_items_in_box(SPDocument *document, unsigned int dkey, NR::Rect const &box)
981 {
982 g_return_val_if_fail(document != NULL, NULL);
983 g_return_val_if_fail(document->priv != NULL, NULL);
985 return find_items_in_area(NULL, SP_GROUP(document->root), dkey, box, is_within);
986 }
988 /*
989 * Return list of items, that the parts of the item contained in box
990 *
991 * Assumes box is normalized (and g_asserts it!)
992 *
993 */
995 GSList *sp_document_partial_items_in_box(SPDocument *document, unsigned int dkey, NR::Rect const &box)
996 {
997 g_return_val_if_fail(document != NULL, NULL);
998 g_return_val_if_fail(document->priv != NULL, NULL);
1000 return find_items_in_area(NULL, SP_GROUP(document->root), dkey, box, overlaps);
1001 }
1003 SPItem *
1004 sp_document_item_at_point(SPDocument *document, unsigned const key, NR::Point const p,
1005 gboolean const into_groups, SPItem *upto)
1006 {
1007 g_return_val_if_fail(document != NULL, NULL);
1008 g_return_val_if_fail(document->priv != NULL, NULL);
1010 return find_item_at_point(key, SP_GROUP(document->root), p, into_groups, false, upto);
1011 }
1013 SPItem*
1014 sp_document_group_at_point(SPDocument *document, unsigned int key, NR::Point const p)
1015 {
1016 g_return_val_if_fail(document != NULL, NULL);
1017 g_return_val_if_fail(document->priv != NULL, NULL);
1019 return find_group_at_point(key, SP_GROUP(document->root), p);
1020 }
1023 /* Resource management */
1025 gboolean
1026 sp_document_add_resource(SPDocument *document, gchar const *key, SPObject *object)
1027 {
1028 GSList *rlist;
1029 GQuark q = g_quark_from_string(key);
1031 g_return_val_if_fail(document != NULL, FALSE);
1032 g_return_val_if_fail(key != NULL, FALSE);
1033 g_return_val_if_fail(*key != '\0', FALSE);
1034 g_return_val_if_fail(object != NULL, FALSE);
1035 g_return_val_if_fail(SP_IS_OBJECT(object), FALSE);
1037 if (SP_OBJECT_IS_CLONED(object))
1038 return FALSE;
1040 rlist = (GSList*)g_hash_table_lookup(document->priv->resources, key);
1041 g_return_val_if_fail(!g_slist_find(rlist, object), FALSE);
1042 rlist = g_slist_prepend(rlist, object);
1043 g_hash_table_insert(document->priv->resources, (gpointer) key, rlist);
1045 document->priv->resources_changed_signals[q].emit();
1047 return TRUE;
1048 }
1050 gboolean
1051 sp_document_remove_resource(SPDocument *document, gchar const *key, SPObject *object)
1052 {
1053 GSList *rlist;
1054 GQuark q = g_quark_from_string(key);
1056 g_return_val_if_fail(document != NULL, FALSE);
1057 g_return_val_if_fail(key != NULL, FALSE);
1058 g_return_val_if_fail(*key != '\0', FALSE);
1059 g_return_val_if_fail(object != NULL, FALSE);
1060 g_return_val_if_fail(SP_IS_OBJECT(object), FALSE);
1062 if (SP_OBJECT_IS_CLONED(object))
1063 return FALSE;
1065 rlist = (GSList*)g_hash_table_lookup(document->priv->resources, key);
1066 g_return_val_if_fail(rlist != NULL, FALSE);
1067 g_return_val_if_fail(g_slist_find(rlist, object), FALSE);
1068 rlist = g_slist_remove(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 GSList const *
1077 sp_document_get_resource_list(SPDocument *document, gchar const *key)
1078 {
1079 g_return_val_if_fail(document != NULL, NULL);
1080 g_return_val_if_fail(key != NULL, NULL);
1081 g_return_val_if_fail(*key != '\0', NULL);
1083 return (GSList*)g_hash_table_lookup(document->priv->resources, key);
1084 }
1086 sigc::connection sp_document_resources_changed_connect(SPDocument *document,
1087 gchar const *key,
1088 SPDocument::ResourcesChangedSignal::slot_type slot)
1089 {
1090 GQuark q = g_quark_from_string(key);
1091 return document->priv->resources_changed_signals[q].connect(slot);
1092 }
1094 /* Helpers */
1096 gboolean
1097 sp_document_resource_list_free(gpointer key, gpointer value, gpointer data)
1098 {
1099 g_slist_free((GSList *) value);
1100 return TRUE;
1101 }
1103 unsigned int
1104 count_objects_recursive(SPObject *obj, unsigned int count)
1105 {
1106 count++; // obj itself
1108 for (SPObject *i = sp_object_first_child(obj); i != NULL; i = SP_OBJECT_NEXT(i)) {
1109 count = count_objects_recursive(i, count);
1110 }
1112 return count;
1113 }
1115 unsigned int
1116 objects_in_document(SPDocument *document)
1117 {
1118 return count_objects_recursive(SP_DOCUMENT_ROOT(document), 0);
1119 }
1121 void
1122 vacuum_document_recursive(SPObject *obj)
1123 {
1124 if (SP_IS_DEFS(obj)) {
1125 for (SPObject *def = obj->firstChild(); def; def = SP_OBJECT_NEXT(def)) {
1126 /* fixme: some inkscape-internal nodes in the future might not be collectable */
1127 def->requestOrphanCollection();
1128 }
1129 } else {
1130 for (SPObject *i = sp_object_first_child(obj); i != NULL; i = SP_OBJECT_NEXT(i)) {
1131 vacuum_document_recursive(i);
1132 }
1133 }
1134 }
1136 unsigned int
1137 vacuum_document(SPDocument *document)
1138 {
1139 unsigned int start = objects_in_document(document);
1140 unsigned int end;
1141 unsigned int newend = start;
1143 unsigned int iterations = 0;
1145 do {
1146 end = newend;
1148 vacuum_document_recursive(SP_DOCUMENT_ROOT(document));
1149 document->collectOrphans();
1150 iterations++;
1152 newend = objects_in_document(document);
1154 } while (iterations < 100 && newend < end);
1156 return start - newend;
1157 }
1159 bool SPDocument::isSeeking() const {
1160 return priv->seeking;
1161 }
1164 /*
1165 Local Variables:
1166 mode:c++
1167 c-file-style:"stroustrup"
1168 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1169 indent-tabs-mode:nil
1170 fill-column:99
1171 End:
1172 */
1173 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :