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();
95 p = new SPDocumentPrivate();
97 p->iddef = g_hash_table_new(g_direct_hash, g_direct_equal);
98 p->reprdef = g_hash_table_new(g_direct_hash, g_direct_equal);
100 p->resources = g_hash_table_new(g_str_hash, g_str_equal);
102 p->sensitive = FALSE;
103 p->partial = NULL;
104 p->history_size = 0;
105 p->undo = NULL;
106 p->redo = NULL;
108 p->undoStackObservers.add(p->event_log);
110 priv = p;
112 // XXX only for testing!
113 priv->undoStackObservers.add(p->console_output_undo_observer);
114 }
116 SPDocument::~SPDocument() {
117 collectOrphans();
119 if (priv) {
120 inkscape_remove_document(this);
122 if (priv->partial) {
123 sp_repr_free_log(priv->partial);
124 priv->partial = NULL;
125 }
127 sp_document_clear_redo(this);
128 sp_document_clear_undo(this);
130 if (root) {
131 sp_object_invoke_release(root);
132 sp_object_unref(root);
133 root = NULL;
134 }
136 if (priv->iddef) g_hash_table_destroy(priv->iddef);
137 if (priv->reprdef) g_hash_table_destroy(priv->reprdef);
139 if (rdoc) Inkscape::GC::release(rdoc);
141 /* Free resources */
142 g_hash_table_foreach_remove(priv->resources, sp_document_resource_list_free, this);
143 g_hash_table_destroy(priv->resources);
145 delete priv;
146 priv = NULL;
147 }
149 cr_cascade_unref(style_cascade);
150 style_cascade = NULL;
152 if (name) {
153 g_free(name);
154 name = NULL;
155 }
156 if (base) {
157 g_free(base);
158 base = NULL;
159 }
160 if (uri) {
161 g_free(uri);
162 uri = NULL;
163 }
165 if (modified_id) {
166 gtk_idle_remove(modified_id);
167 modified_id = 0;
168 }
170 _selection_changed_connection.disconnect();
171 _desktop_activated_connection.disconnect();
173 if (keepalive) {
174 inkscape_unref();
175 keepalive = FALSE;
176 }
178 if (router) {
179 delete router;
180 router = NULL;
181 }
183 //delete this->_whiteboard_session_manager;
184 }
186 void SPDocument::queueForOrphanCollection(SPObject *object) {
187 g_return_if_fail(object != NULL);
188 g_return_if_fail(SP_OBJECT_DOCUMENT(object) == this);
190 sp_object_ref(object, NULL);
191 _collection_queue = g_slist_prepend(_collection_queue, object);
192 }
194 void SPDocument::collectOrphans() {
195 while (_collection_queue) {
196 GSList *objects=_collection_queue;
197 _collection_queue = NULL;
198 for ( GSList *iter=objects ; iter ; iter = iter->next ) {
199 SPObject *object=reinterpret_cast<SPObject *>(iter->data);
200 object->collectOrphan();
201 sp_object_unref(object, NULL);
202 }
203 g_slist_free(objects);
204 }
205 }
207 void SPDocument::reset_key (void *dummy)
208 {
209 actionkey = NULL;
210 }
212 SPDocument *
213 sp_document_create(Inkscape::XML::Document *rdoc,
214 gchar const *uri,
215 gchar const *base,
216 gchar const *name,
217 unsigned int keepalive)
218 {
219 SPDocument *document;
220 Inkscape::XML::Node *rroot;
221 Inkscape::Version sodipodi_version;
223 rroot = sp_repr_document_root(rdoc);
225 document = new SPDocument();
227 document->keepalive = keepalive;
229 document->rdoc = rdoc;
230 document->rroot = rroot;
232 #ifndef WIN32
233 prepend_current_dir_if_relative(&(document->uri), uri);
234 #else
235 // FIXME: it may be that prepend_current_dir_if_relative works OK on windows too, test!
236 document->uri = uri? g_strdup(uri) : NULL;
237 #endif
239 // base is simply the part of the path before filename; e.g. when running "inkscape ../file.svg" the base is "../"
240 // which is why we use g_get_current_dir() in calculating the abs path above
241 //This is NULL for a new document
242 if (base)
243 document->base = g_strdup(base);
244 else
245 document->base = NULL;
246 document->name = g_strdup(name);
248 document->root = sp_object_repr_build_tree(document, rroot);
250 sodipodi_version = SP_ROOT(document->root)->version.sodipodi;
252 /* fixme: Not sure about this, but lets assume ::build updates */
253 rroot->setAttribute("sodipodi:version", SODIPODI_VERSION);
254 rroot->setAttribute("inkscape:version", INKSCAPE_VERSION);
255 /* fixme: Again, I moved these here to allow version determining in ::build (Lauris) */
257 /* Quick hack 2 - get default image size into document */
258 if (!rroot->attribute("width")) rroot->setAttribute("width", A4_WIDTH_STR);
259 if (!rroot->attribute("height")) rroot->setAttribute("height", A4_HEIGHT_STR);
260 /* End of quick hack 2 */
262 /* Quick hack 3 - Set uri attributes */
263 if (uri) {
264 /* fixme: Think, what this means for images (Lauris) */
265 rroot->setAttribute("sodipodi:docname", uri);
266 if (document->base)
267 rroot->setAttribute("sodipodi:docbase", document->base);
268 }
269 /* End of quick hack 3 */
271 // creating namedview
272 if (!sp_item_group_get_child_by_name((SPGroup *) document->root, NULL, "sodipodi:namedview")) {
273 // if there's none in the document already,
274 Inkscape::XML::Node *r = NULL;
275 Inkscape::XML::Node *rnew = NULL;
276 r = inkscape_get_repr(INKSCAPE, "template.base");
277 // see if there's a template with id="base" in the preferences
278 if (!r) {
279 // if there's none, create an empty element
280 rnew = sp_repr_new("sodipodi:namedview");
281 rnew->setAttribute("id", "base");
282 } else {
283 // otherwise, take from preferences
284 rnew = r->duplicate();
285 }
286 // insert into the document
287 rroot->addChild(rnew, NULL);
288 // clean up
289 Inkscape::GC::release(rnew);
290 }
292 /* Defs */
293 if (!SP_ROOT(document->root)->defs) {
294 Inkscape::XML::Node *r;
295 r = sp_repr_new("svg:defs");
296 rroot->addChild(r, NULL);
297 Inkscape::GC::release(r);
298 g_assert(SP_ROOT(document->root)->defs);
299 }
301 /* Default RDF */
302 rdf_set_defaults( document );
304 if (keepalive) {
305 inkscape_ref();
306 }
308 sp_document_set_undo_sensitive(document, TRUE);
310 // reset undo key when selection changes, so that same-key actions on different objects are not coalesced
311 if (!Inkscape::NSApplication::Application::getNewGui()) {
312 g_signal_connect(G_OBJECT(INKSCAPE), "change_selection",
313 G_CALLBACK(sp_document_reset_key), document);
314 g_signal_connect(G_OBJECT(INKSCAPE), "activate_desktop",
315 G_CALLBACK(sp_document_reset_key), document);
316 } else {
317 document->_selection_changed_connection = Inkscape::NSApplication::Editor::connectSelectionChanged (sigc::mem_fun (*document, &SPDocument::reset_key));
318 document->_desktop_activated_connection = Inkscape::NSApplication::Editor::connectDesktopActivated (sigc::mem_fun (*document, &SPDocument::reset_key));
319 }
320 inkscape_add_document(document);
322 return document;
323 }
325 /**
326 * Fetches document from URI, or creates new, if NULL; public document
327 * appears in document list.
328 */
329 SPDocument *
330 sp_document_new(gchar const *uri, unsigned int keepalive, bool make_new)
331 {
332 SPDocument *doc;
333 Inkscape::XML::Document *rdoc;
334 gchar *base = NULL;
335 gchar *name = NULL;
337 if (uri) {
338 Inkscape::XML::Node *rroot;
339 gchar *s, *p;
340 /* Try to fetch repr from file */
341 rdoc = sp_repr_read_file(uri, SP_SVG_NS_URI);
342 /* If file cannot be loaded, return NULL without warning */
343 if (rdoc == NULL) return NULL;
344 rroot = sp_repr_document_root(rdoc);
345 /* If xml file is not svg, return NULL without warning */
346 /* fixme: destroy document */
347 if (strcmp(rroot->name(), "svg:svg") != 0) return NULL;
348 s = g_strdup(uri);
349 p = strrchr(s, '/');
350 if (p) {
351 name = g_strdup(p + 1);
352 p[1] = '\0';
353 base = g_strdup(s);
354 } else {
355 base = NULL;
356 name = g_strdup(uri);
357 }
358 g_free(s);
359 } else {
360 rdoc = sp_repr_document_new("svg:svg");
361 }
363 if (make_new) {
364 base = NULL;
365 uri = NULL;
366 name = g_strdup_printf(_("New document %d"), ++doc_count);
367 }
369 //# These should be set by now
370 g_assert(name);
372 doc = sp_document_create(rdoc, uri, base, name, keepalive);
374 g_free(base);
375 g_free(name);
377 return doc;
378 }
380 SPDocument *
381 sp_document_new_from_mem(gchar const *buffer, gint length, unsigned int keepalive)
382 {
383 SPDocument *doc;
384 Inkscape::XML::Document *rdoc;
385 Inkscape::XML::Node *rroot;
386 gchar *name;
388 rdoc = sp_repr_read_mem(buffer, length, SP_SVG_NS_URI);
390 /* If it cannot be loaded, return NULL without warning */
391 if (rdoc == NULL) return NULL;
393 rroot = sp_repr_document_root(rdoc);
394 /* If xml file is not svg, return NULL without warning */
395 /* fixme: destroy document */
396 if (strcmp(rroot->name(), "svg:svg") != 0) return NULL;
398 name = g_strdup_printf(_("Memory document %d"), ++doc_count);
400 doc = sp_document_create(rdoc, NULL, NULL, name, keepalive);
402 return doc;
403 }
405 SPDocument *sp_document_new_dummy() {
406 SPDocument *document = new SPDocument();
407 inkscape_add_document(document);
408 return document;
409 }
411 SPDocument *
412 sp_document_ref(SPDocument *doc)
413 {
414 g_return_val_if_fail(doc != NULL, NULL);
415 Inkscape::GC::anchor(doc);
416 return doc;
417 }
419 SPDocument *
420 sp_document_unref(SPDocument *doc)
421 {
422 g_return_val_if_fail(doc != NULL, NULL);
423 Inkscape::GC::release(doc);
424 return NULL;
425 }
427 gdouble sp_document_width(SPDocument *document)
428 {
429 g_return_val_if_fail(document != NULL, 0.0);
430 g_return_val_if_fail(document->priv != NULL, 0.0);
431 g_return_val_if_fail(document->root != NULL, 0.0);
433 return SP_ROOT(document->root)->width.computed;
434 }
436 void
437 sp_document_set_width (SPDocument *document, gdouble width, const SPUnit *unit)
438 {
439 SPRoot *root = SP_ROOT(document->root);
441 if (root->width.unit == SVGLength::PERCENT && root->viewBox_set) { // set to viewBox=
442 root->viewBox.x1 = root->viewBox.x0 + sp_units_get_pixels (width, *unit);
443 } else { // set to width=
444 root->width.computed = sp_units_get_pixels (width, *unit);
445 /* SVG does not support meters as a unit, so we must translate meters to
446 * cm when writing */
447 if (!strcmp(unit->abbr, "m")) {
448 root->width.value = 100*width;
449 root->width.unit = SVGLength::CM;
450 } else {
451 root->width.value = width;
452 root->width.unit = (SVGLength::Unit) sp_unit_get_svg_unit(unit);
453 }
454 }
456 SP_OBJECT (root)->updateRepr();
457 }
459 void sp_document_set_height (SPDocument * document, gdouble height, const SPUnit *unit)
460 {
461 SPRoot *root = SP_ROOT(document->root);
463 if (root->height.unit == SVGLength::PERCENT && root->viewBox_set) { // set to viewBox=
464 root->viewBox.y1 = root->viewBox.y0 + sp_units_get_pixels (height, *unit);
465 } else { // set to height=
466 root->height.computed = sp_units_get_pixels (height, *unit);
467 /* SVG does not support meters as a unit, so we must translate meters to
468 * cm when writing */
469 if (!strcmp(unit->abbr, "m")) {
470 root->height.value = 100*height;
471 root->height.unit = SVGLength::CM;
472 } else {
473 root->height.value = height;
474 root->height.unit = (SVGLength::Unit) sp_unit_get_svg_unit(unit);
475 }
476 }
478 SP_OBJECT (root)->updateRepr();
479 }
481 gdouble sp_document_height(SPDocument *document)
482 {
483 g_return_val_if_fail(document != NULL, 0.0);
484 g_return_val_if_fail(document->priv != NULL, 0.0);
485 g_return_val_if_fail(document->root != NULL, 0.0);
487 return SP_ROOT(document->root)->height.computed;
488 }
490 /**
491 * Given an NRRect that may, for example, correspond to the bbox of an object
492 * this function fits the canvas to that rect by resizing the canvas
493 * and translating the document root into position.
494 */
495 void SPDocument::fitToRect(NRRect const & rect)
496 {
497 g_return_if_fail(!empty(rect));
499 gdouble w = rect.x1 - rect.x0;
500 gdouble h = rect.y1 - rect.y0;
501 gdouble old_height = sp_document_height(this);
502 SPUnit unit = sp_unit_get_by_id(SP_UNIT_PX);
503 sp_document_set_width(this, w, &unit);
504 sp_document_set_height(this, h, &unit);
506 NR::translate tr = NR::translate::translate(-rect.x0,-(rect.y0 + (h - old_height)));
507 static_cast<SPGroup *>(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 gboolean saved = sp_document_get_undo_sensitive(document);
550 sp_document_set_undo_sensitive(document, FALSE);
551 if (document->base)
552 repr->setAttribute("sodipodi:docbase", document->base);
554 repr->setAttribute("sodipodi:docname", document->name);
555 sp_document_set_undo_sensitive(document, saved);
557 document->priv->uri_set_signal.emit(document->uri);
558 }
560 void
561 sp_document_resized_signal_emit(SPDocument *doc, gdouble width, gdouble height)
562 {
563 g_return_if_fail(doc != NULL);
565 doc->priv->resized_signal.emit(width, height);
566 }
568 sigc::connection SPDocument::connectModified(SPDocument::ModifiedSignal::slot_type slot)
569 {
570 return priv->modified_signal.connect(slot);
571 }
573 sigc::connection SPDocument::connectURISet(SPDocument::URISetSignal::slot_type slot)
574 {
575 return priv->uri_set_signal.connect(slot);
576 }
578 sigc::connection SPDocument::connectResized(SPDocument::ResizedSignal::slot_type slot)
579 {
580 return priv->resized_signal.connect(slot);
581 }
583 sigc::connection
584 SPDocument::connectReconstructionStart(SPDocument::ReconstructionStart::slot_type slot)
585 {
586 return priv->_reconstruction_start_signal.connect(slot);
587 }
589 void
590 SPDocument::emitReconstructionStart(void)
591 {
592 // printf("Starting Reconstruction\n");
593 priv->_reconstruction_start_signal.emit();
594 return;
595 }
597 sigc::connection
598 SPDocument::connectReconstructionFinish(SPDocument::ReconstructionFinish::slot_type slot)
599 {
600 return priv->_reconstruction_finish_signal.connect(slot);
601 }
603 void
604 SPDocument::emitReconstructionFinish(void)
605 {
606 // printf("Finishing Reconstruction\n");
607 priv->_reconstruction_finish_signal.emit();
608 return;
609 }
612 void SPDocument::_emitModified() {
613 static guint const flags = SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG | SP_OBJECT_PARENT_MODIFIED_FLAG;
614 root->emitModified(0);
615 priv->modified_signal.emit(flags);
616 }
618 void SPDocument::bindObjectToId(gchar const *id, SPObject *object) {
619 GQuark idq = g_quark_from_string(id);
621 if (object) {
622 g_assert(g_hash_table_lookup(priv->iddef, GINT_TO_POINTER(idq)) == NULL);
623 g_hash_table_insert(priv->iddef, GINT_TO_POINTER(idq), object);
624 } else {
625 g_assert(g_hash_table_lookup(priv->iddef, GINT_TO_POINTER(idq)) != NULL);
626 g_hash_table_remove(priv->iddef, GINT_TO_POINTER(idq));
627 }
629 SPDocumentPrivate::IDChangedSignalMap::iterator pos;
631 pos = priv->id_changed_signals.find(idq);
632 if ( pos != priv->id_changed_signals.end() ) {
633 if (!(*pos).second.empty()) {
634 (*pos).second.emit(object);
635 } else { // discard unused signal
636 priv->id_changed_signals.erase(pos);
637 }
638 }
639 }
641 void
642 SPDocument::addUndoObserver(Inkscape::UndoStackObserver& observer)
643 {
644 this->priv->undoStackObservers.add(observer);
645 }
647 void
648 SPDocument::removeUndoObserver(Inkscape::UndoStackObserver& observer)
649 {
650 this->priv->undoStackObservers.remove(observer);
651 }
653 Inkscape::EventLog&
654 SPDocument::getEventLog() const
655 {
656 return priv->event_log;
657 }
659 SPObject *SPDocument::getObjectById(gchar const *id) {
660 g_return_val_if_fail(id != NULL, NULL);
662 GQuark idq = g_quark_from_string(id);
663 return (SPObject*)g_hash_table_lookup(priv->iddef, GINT_TO_POINTER(idq));
664 }
666 sigc::connection SPDocument::connectIdChanged(gchar const *id,
667 SPDocument::IDChangedSignal::slot_type slot)
668 {
669 return priv->id_changed_signals[g_quark_from_string(id)].connect(slot);
670 }
672 void SPDocument::bindObjectToRepr(Inkscape::XML::Node *repr, SPObject *object) {
673 if (object) {
674 g_assert(g_hash_table_lookup(priv->reprdef, repr) == NULL);
675 g_hash_table_insert(priv->reprdef, repr, object);
676 } else {
677 g_assert(g_hash_table_lookup(priv->reprdef, repr) != NULL);
678 g_hash_table_remove(priv->reprdef, repr);
679 }
680 }
682 SPObject *SPDocument::getObjectByRepr(Inkscape::XML::Node *repr) {
683 g_return_val_if_fail(repr != NULL, NULL);
684 return (SPObject*)g_hash_table_lookup(priv->reprdef, repr);
685 }
687 Glib::ustring SPDocument::getLanguage() {
688 gchar const *document_language = rdf_get_work_entity(this, rdf_find_entity("language"));
689 if (document_language) {
690 while (isspace(*document_language))
691 document_language++;
692 }
693 if ( !document_language || 0 == *document_language) {
694 // retrieve system language
695 document_language = getenv("LC_ALL");
696 if ( NULL == document_language || *document_language == 0 ) {
697 document_language = getenv ("LC_MESSAGES");
698 }
699 if ( NULL == document_language || *document_language == 0 ) {
700 document_language = getenv ("LANG");
701 }
703 if ( NULL != document_language ) {
704 gchar *pos = strchr(document_language, '_');
705 if ( NULL != pos ) {
706 return Glib::ustring(document_language, pos - document_language);
707 }
708 }
709 }
711 if ( NULL == document_language )
712 return Glib::ustring();
713 return document_language;
714 }
716 /* Object modification root handler */
718 void
719 sp_document_request_modified(SPDocument *doc)
720 {
721 if (!doc->modified_id) {
722 doc->modified_id = gtk_idle_add_priority(SP_DOCUMENT_UPDATE_PRIORITY, sp_document_idle_handler, doc);
723 }
724 }
726 void
727 sp_document_setup_viewport (SPDocument *doc, SPItemCtx *ctx)
728 {
729 ctx->ctx.flags = 0;
730 ctx->i2doc = NR::identity();
731 /* Set up viewport in case svg has it defined as percentages */
732 if (SP_ROOT(doc->root)->viewBox_set) { // if set, take from viewBox
733 ctx->vp.x0 = SP_ROOT(doc->root)->viewBox.x0;
734 ctx->vp.y0 = SP_ROOT(doc->root)->viewBox.y0;
735 ctx->vp.x1 = SP_ROOT(doc->root)->viewBox.x1;
736 ctx->vp.y1 = SP_ROOT(doc->root)->viewBox.y1;
737 } else { // as a last resort, set size to A4
738 ctx->vp.x0 = 0.0;
739 ctx->vp.y0 = 0.0;
740 ctx->vp.x1 = 210 * PX_PER_MM;
741 ctx->vp.y1 = 297 * PX_PER_MM;
742 }
743 ctx->i2vp = NR::identity();
744 }
746 gint
747 sp_document_ensure_up_to_date(SPDocument *doc)
748 {
749 int lc;
750 lc = 32;
751 while (doc->root->uflags || doc->root->mflags) {
752 lc -= 1;
753 if (lc < 0) {
754 g_warning("More than 32 iterations while updating document '%s'", doc->uri);
755 if (doc->modified_id) {
756 /* Remove handler */
757 gtk_idle_remove(doc->modified_id);
758 doc->modified_id = 0;
759 }
760 return FALSE;
761 }
762 /* Process updates */
763 if (doc->root->uflags) {
764 SPItemCtx ctx;
765 sp_document_setup_viewport (doc, &ctx);
766 doc->root->updateDisplay((SPCtx *)&ctx, 0);
767 }
768 doc->_emitModified();
769 }
770 if (doc->modified_id) {
771 /* Remove handler */
772 gtk_idle_remove(doc->modified_id);
773 doc->modified_id = 0;
774 }
775 return TRUE;
776 }
778 static gint
779 sp_document_idle_handler(gpointer data)
780 {
781 SPDocument *doc;
782 int repeat;
784 doc = static_cast<SPDocument *>(data);
786 #ifdef SP_DOCUMENT_DEBUG_IDLE
787 g_print("->\n");
788 #endif
790 /* Process updates */
791 if (doc->root->uflags) {
792 SPItemCtx ctx;
793 sp_document_setup_viewport (doc, &ctx);
795 gboolean saved = sp_document_get_undo_sensitive(doc);
796 sp_document_set_undo_sensitive(doc, FALSE);
798 doc->root->updateDisplay((SPCtx *)&ctx, 0);
800 sp_document_set_undo_sensitive(doc, saved);
801 /* if (doc->root->uflags & SP_OBJECT_MODIFIED_FLAG) return TRUE; */
802 }
804 doc->_emitModified();
806 repeat = (doc->root->uflags || doc->root->mflags);
807 if (!repeat) doc->modified_id = 0;
808 return repeat;
809 }
811 static bool is_within(NR::Rect const &area, NR::Rect const &box)
812 {
813 return area.contains(box);
814 }
816 static bool overlaps(NR::Rect const &area, NR::Rect const &box)
817 {
818 return area.intersects(box);
819 }
821 static GSList *find_items_in_area(GSList *s, SPGroup *group, unsigned int dkey, NR::Rect const &area,
822 bool (*test)(NR::Rect const &, NR::Rect const &), bool take_insensitive = false)
823 {
824 g_return_val_if_fail(SP_IS_GROUP(group), s);
826 for (SPObject *o = sp_object_first_child(SP_OBJECT(group)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
827 if (!SP_IS_ITEM(o)) {
828 continue;
829 }
830 if (SP_IS_GROUP(o) && SP_GROUP(o)->effectiveLayerMode(dkey) == SPGroup::LAYER ) {
831 s = find_items_in_area(s, SP_GROUP(o), dkey, area, test);
832 } else {
833 SPItem *child = SP_ITEM(o);
834 NR::Rect box = sp_item_bbox_desktop(child);
835 if (test(area, box) && (take_insensitive || child->isVisibleAndUnlocked(dkey))) {
836 s = g_slist_append(s, child);
837 }
838 }
839 }
841 return s;
842 }
844 /**
845 Returns true if an item is among the descendants of group (recursively).
846 */
847 bool item_is_in_group(SPItem *item, SPGroup *group)
848 {
849 for (SPObject *o = sp_object_first_child(SP_OBJECT(group)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
850 if (!SP_IS_ITEM(o)) continue;
851 if (SP_ITEM(o) == item)
852 return true;
853 if (SP_IS_GROUP(o))
854 if (item_is_in_group(item, SP_GROUP(o)))
855 return true;
856 }
857 return false;
858 }
860 /**
861 Returns the bottommost item from the list which is at the point, or NULL if none.
862 */
863 SPItem*
864 sp_document_item_from_list_at_point_bottom(unsigned int dkey, SPGroup *group, GSList const *list,
865 NR::Point const p, bool take_insensitive)
866 {
867 g_return_val_if_fail(group, NULL);
869 gdouble delta = prefs_get_double_attribute ("options.cursortolerance", "value", 1.0);
871 for (SPObject *o = sp_object_first_child(SP_OBJECT(group)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
873 if (!SP_IS_ITEM(o)) continue;
875 SPItem *item = SP_ITEM(o);
876 NRArenaItem *arenaitem = sp_item_get_arenaitem(item, dkey);
877 if (arenaitem && nr_arena_item_invoke_pick(arenaitem, p, delta, 1) != NULL
878 && (take_insensitive || item->isVisibleAndUnlocked(dkey))) {
879 if (g_slist_find((GSList *) list, item) != NULL)
880 return item;
881 }
883 if (SP_IS_GROUP(o)) {
884 SPItem *found = sp_document_item_from_list_at_point_bottom(dkey, SP_GROUP(o), list, p, take_insensitive);
885 if (found)
886 return found;
887 }
889 }
890 return NULL;
891 }
893 /**
894 Returns the topmost (in z-order) item from the descendants of group (recursively) which
895 is at the point p, or NULL if none. Honors into_groups on whether to recurse into
896 non-layer groups or not. Honors take_insensitive on whether to return insensitive
897 items. If upto != NULL, then if item upto is encountered (at any level), stops searching
898 upwards in z-order and returns what it has found so far (i.e. the found item is
899 guaranteed to be lower than upto).
900 */
901 SPItem*
902 find_item_at_point(unsigned int dkey, SPGroup *group, NR::Point const p, gboolean into_groups, bool take_insensitive = false, SPItem *upto = NULL)
903 {
904 SPItem *seen = NULL, *newseen = NULL;
906 gdouble delta = prefs_get_double_attribute ("options.cursortolerance", "value", 1.0);
908 for (SPObject *o = sp_object_first_child(SP_OBJECT(group)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
909 if (!SP_IS_ITEM(o)) continue;
911 if (upto && SP_ITEM(o) == upto)
912 break;
914 if (SP_IS_GROUP(o) && (SP_GROUP(o)->effectiveLayerMode(dkey) == SPGroup::LAYER || into_groups)) {
915 // if nothing found yet, recurse into the group
916 newseen = find_item_at_point(dkey, SP_GROUP(o), p, into_groups, take_insensitive, upto);
917 if (newseen) {
918 seen = newseen;
919 newseen = NULL;
920 }
922 if (item_is_in_group(upto, SP_GROUP(o)))
923 break;
925 } else {
926 SPItem *child = SP_ITEM(o);
927 NRArenaItem *arenaitem = sp_item_get_arenaitem(child, dkey);
929 // seen remembers the last (topmost) of items pickable at this point
930 if (arenaitem && nr_arena_item_invoke_pick(arenaitem, p, delta, 1) != NULL
931 && (take_insensitive || child->isVisibleAndUnlocked(dkey))) {
932 seen = child;
933 }
934 }
935 }
936 return seen;
937 }
939 /**
940 Returns the topmost non-layer group from the descendants of group which is at point
941 p, or NULL if none. Recurses into layers but not into groups.
942 */
943 SPItem*
944 find_group_at_point(unsigned int dkey, SPGroup *group, NR::Point const p)
945 {
946 SPItem *seen = NULL;
948 gdouble delta = prefs_get_double_attribute ("options.cursortolerance", "value", 1.0);
950 for (SPObject *o = sp_object_first_child(SP_OBJECT(group)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
951 if (!SP_IS_ITEM(o)) continue;
952 if (SP_IS_GROUP(o) && SP_GROUP(o)->effectiveLayerMode(dkey) == SPGroup::LAYER) {
953 SPItem *newseen = find_group_at_point(dkey, SP_GROUP(o), p);
954 if (newseen) {
955 seen = newseen;
956 }
957 }
958 if (SP_IS_GROUP(o) && SP_GROUP(o)->effectiveLayerMode(dkey) != SPGroup::LAYER ) {
959 SPItem *child = SP_ITEM(o);
960 NRArenaItem *arenaitem = sp_item_get_arenaitem(child, dkey);
962 // seen remembers the last (topmost) of groups pickable at this point
963 if (arenaitem && nr_arena_item_invoke_pick(arenaitem, p, delta, 1) != NULL) {
964 seen = child;
965 }
966 }
967 }
968 return seen;
969 }
971 /*
972 * Return list of items, contained in box
973 *
974 * Assumes box is normalized (and g_asserts it!)
975 *
976 */
978 GSList *sp_document_items_in_box(SPDocument *document, unsigned int dkey, NR::Rect const &box)
979 {
980 g_return_val_if_fail(document != NULL, NULL);
981 g_return_val_if_fail(document->priv != NULL, NULL);
983 return find_items_in_area(NULL, SP_GROUP(document->root), dkey, box, is_within);
984 }
986 /*
987 * Return list of items, that the parts of the item contained in box
988 *
989 * Assumes box is normalized (and g_asserts it!)
990 *
991 */
993 GSList *sp_document_partial_items_in_box(SPDocument *document, unsigned int dkey, NR::Rect const &box)
994 {
995 g_return_val_if_fail(document != NULL, NULL);
996 g_return_val_if_fail(document->priv != NULL, NULL);
998 return find_items_in_area(NULL, SP_GROUP(document->root), dkey, box, overlaps);
999 }
1001 SPItem *
1002 sp_document_item_at_point(SPDocument *document, unsigned const key, NR::Point const p,
1003 gboolean const into_groups, SPItem *upto)
1004 {
1005 g_return_val_if_fail(document != NULL, NULL);
1006 g_return_val_if_fail(document->priv != NULL, NULL);
1008 return find_item_at_point(key, SP_GROUP(document->root), p, into_groups, false, upto);
1009 }
1011 SPItem*
1012 sp_document_group_at_point(SPDocument *document, unsigned int key, NR::Point const p)
1013 {
1014 g_return_val_if_fail(document != NULL, NULL);
1015 g_return_val_if_fail(document->priv != NULL, NULL);
1017 return find_group_at_point(key, SP_GROUP(document->root), p);
1018 }
1021 /* Resource management */
1023 gboolean
1024 sp_document_add_resource(SPDocument *document, gchar const *key, SPObject *object)
1025 {
1026 GSList *rlist;
1027 GQuark q = g_quark_from_string(key);
1029 g_return_val_if_fail(document != NULL, FALSE);
1030 g_return_val_if_fail(key != NULL, FALSE);
1031 g_return_val_if_fail(*key != '\0', FALSE);
1032 g_return_val_if_fail(object != NULL, FALSE);
1033 g_return_val_if_fail(SP_IS_OBJECT(object), FALSE);
1035 if (SP_OBJECT_IS_CLONED(object))
1036 return FALSE;
1038 rlist = (GSList*)g_hash_table_lookup(document->priv->resources, key);
1039 g_return_val_if_fail(!g_slist_find(rlist, object), FALSE);
1040 rlist = g_slist_prepend(rlist, object);
1041 g_hash_table_insert(document->priv->resources, (gpointer) key, rlist);
1043 document->priv->resources_changed_signals[q].emit();
1045 return TRUE;
1046 }
1048 gboolean
1049 sp_document_remove_resource(SPDocument *document, gchar const *key, SPObject *object)
1050 {
1051 GSList *rlist;
1052 GQuark q = g_quark_from_string(key);
1054 g_return_val_if_fail(document != NULL, FALSE);
1055 g_return_val_if_fail(key != NULL, FALSE);
1056 g_return_val_if_fail(*key != '\0', FALSE);
1057 g_return_val_if_fail(object != NULL, FALSE);
1058 g_return_val_if_fail(SP_IS_OBJECT(object), FALSE);
1060 if (SP_OBJECT_IS_CLONED(object))
1061 return FALSE;
1063 rlist = (GSList*)g_hash_table_lookup(document->priv->resources, key);
1064 g_return_val_if_fail(rlist != NULL, FALSE);
1065 g_return_val_if_fail(g_slist_find(rlist, object), FALSE);
1066 rlist = g_slist_remove(rlist, object);
1067 g_hash_table_insert(document->priv->resources, (gpointer) key, rlist);
1069 document->priv->resources_changed_signals[q].emit();
1071 return TRUE;
1072 }
1074 GSList const *
1075 sp_document_get_resource_list(SPDocument *document, gchar const *key)
1076 {
1077 g_return_val_if_fail(document != NULL, NULL);
1078 g_return_val_if_fail(key != NULL, NULL);
1079 g_return_val_if_fail(*key != '\0', NULL);
1081 return (GSList*)g_hash_table_lookup(document->priv->resources, key);
1082 }
1084 sigc::connection sp_document_resources_changed_connect(SPDocument *document,
1085 gchar const *key,
1086 SPDocument::ResourcesChangedSignal::slot_type slot)
1087 {
1088 GQuark q = g_quark_from_string(key);
1089 return document->priv->resources_changed_signals[q].connect(slot);
1090 }
1092 /* Helpers */
1094 gboolean
1095 sp_document_resource_list_free(gpointer key, gpointer value, gpointer data)
1096 {
1097 g_slist_free((GSList *) value);
1098 return TRUE;
1099 }
1101 unsigned int
1102 count_objects_recursive(SPObject *obj, unsigned int count)
1103 {
1104 count++; // obj itself
1106 for (SPObject *i = sp_object_first_child(obj); i != NULL; i = SP_OBJECT_NEXT(i)) {
1107 count = count_objects_recursive(i, count);
1108 }
1110 return count;
1111 }
1113 unsigned int
1114 objects_in_document(SPDocument *document)
1115 {
1116 return count_objects_recursive(SP_DOCUMENT_ROOT(document), 0);
1117 }
1119 void
1120 vacuum_document_recursive(SPObject *obj)
1121 {
1122 if (SP_IS_DEFS(obj)) {
1123 for (SPObject *def = obj->firstChild(); def; def = SP_OBJECT_NEXT(def)) {
1124 /* fixme: some inkscape-internal nodes in the future might not be collectable */
1125 def->requestOrphanCollection();
1126 }
1127 } else {
1128 for (SPObject *i = sp_object_first_child(obj); i != NULL; i = SP_OBJECT_NEXT(i)) {
1129 vacuum_document_recursive(i);
1130 }
1131 }
1132 }
1134 unsigned int
1135 vacuum_document(SPDocument *document)
1136 {
1137 unsigned int start = objects_in_document(document);
1138 unsigned int end;
1139 unsigned int newend = start;
1141 unsigned int iterations = 0;
1143 do {
1144 end = newend;
1146 vacuum_document_recursive(SP_DOCUMENT_ROOT(document));
1147 document->collectOrphans();
1148 iterations++;
1150 newend = objects_in_document(document);
1152 } while (iterations < 100 && newend < end);
1154 return start - newend;
1155 }
1158 /*
1159 Local Variables:
1160 mode:c++
1161 c-file-style:"stroustrup"
1162 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1163 indent-tabs-mode:nil
1164 fill-column:99
1165 End:
1166 */
1167 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :