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"
54 #include "display/nr-arena-item.h"
56 #include "dialogs/rdf.h"
58 #define A4_WIDTH_STR "210mm"
59 #define A4_HEIGHT_STR "297mm"
61 #define SP_DOCUMENT_UPDATE_PRIORITY (G_PRIORITY_HIGH_IDLE - 1)
64 static gint sp_document_idle_handler(gpointer data);
66 gboolean sp_document_resource_list_free(gpointer key, gpointer value, gpointer data);
68 static gint doc_count = 0;
70 SPDocument::SPDocument() {
71 SPDocumentPrivate *p;
73 keepalive = FALSE;
74 virgin = TRUE;
76 modified_id = 0;
78 rdoc = NULL;
79 rroot = NULL;
80 root = NULL;
81 style_cascade = cr_cascade_new(NULL, NULL, NULL);
83 uri = NULL;
84 base = NULL;
85 name = NULL;
87 _collection_queue = NULL;
89 p = new SPDocumentPrivate();
91 p->iddef = g_hash_table_new(g_direct_hash, g_direct_equal);
92 p->reprdef = g_hash_table_new(g_direct_hash, g_direct_equal);
94 p->resources = g_hash_table_new(g_str_hash, g_str_equal);
96 p->sensitive = FALSE;
97 p->partial = NULL;
98 p->history_size = 0;
99 p->undo = NULL;
100 p->redo = NULL;
102 priv = p;
103 }
105 SPDocument::~SPDocument() {
106 collectOrphans();
108 if (priv) {
109 inkscape_remove_document(this);
111 if (priv->partial) {
112 sp_repr_free_log(priv->partial);
113 priv->partial = NULL;
114 }
116 sp_document_clear_redo(this);
117 sp_document_clear_undo(this);
119 if (root) {
120 sp_object_invoke_release(root);
121 g_object_unref(G_OBJECT(root));
122 root = NULL;
123 }
125 if (priv->iddef) g_hash_table_destroy(priv->iddef);
126 if (priv->reprdef) g_hash_table_destroy(priv->reprdef);
128 if (rdoc) Inkscape::GC::release(rdoc);
130 /* Free resources */
131 g_hash_table_foreach_remove(priv->resources, sp_document_resource_list_free, this);
132 g_hash_table_destroy(priv->resources);
134 delete priv;
135 priv = NULL;
136 }
138 cr_cascade_unref(style_cascade);
139 style_cascade = NULL;
141 if (name) {
142 g_free(name);
143 name = NULL;
144 }
145 if (base) {
146 g_free(base);
147 base = NULL;
148 }
149 if (uri) {
150 g_free(uri);
151 uri = NULL;
152 }
154 if (modified_id) {
155 gtk_idle_remove(modified_id);
156 modified_id = 0;
157 }
159 _selection_changed_connection.disconnect();
160 _desktop_activated_connection.disconnect();
162 if (keepalive) {
163 inkscape_unref();
164 keepalive = FALSE;
165 }
167 //delete this->_whiteboard_session_manager;
168 }
170 void SPDocument::queueForOrphanCollection(SPObject *object) {
171 g_return_if_fail(object != NULL);
172 g_return_if_fail(SP_OBJECT_DOCUMENT(object) == this);
174 sp_object_ref(object, NULL);
175 _collection_queue = g_slist_prepend(_collection_queue, object);
176 }
178 void SPDocument::collectOrphans() {
179 while (_collection_queue) {
180 GSList *objects=_collection_queue;
181 _collection_queue = NULL;
182 for ( GSList *iter=objects ; iter ; iter = iter->next ) {
183 SPObject *object=reinterpret_cast<SPObject *>(iter->data);
184 object->collectOrphan();
185 sp_object_unref(object, NULL);
186 }
187 g_slist_free(objects);
188 }
189 }
191 void SPDocument::reset_key (void *dummy)
192 {
193 actionkey = NULL;
194 }
196 static SPDocument *
197 sp_document_create(Inkscape::XML::Document *rdoc,
198 gchar const *uri,
199 gchar const *base,
200 gchar const *name,
201 unsigned int keepalive)
202 {
203 SPDocument *document;
204 Inkscape::XML::Node *rroot;
205 Inkscape::Version sodipodi_version;
207 rroot = sp_repr_document_root(rdoc);
209 document = new SPDocument();
211 document->keepalive = keepalive;
213 document->rdoc = rdoc;
214 document->rroot = rroot;
216 #ifndef WIN32
217 prepend_current_dir_if_relative(&(document->uri), uri);
218 #else
219 // FIXME: it may be that prepend_current_dir_if_relative works OK on windows too, test!
220 document->uri = uri? g_strdup(uri) : NULL;
221 #endif
223 // base is simply the part of the path before filename; e.g. when running "inkscape ../file.svg" the base is "../"
224 // which is why we use g_get_current_dir() in calculating the abs path above
225 //This is NULL for a new document
226 if (base)
227 document->base = g_strdup(base);
228 else
229 document->base = NULL;
230 document->name = g_strdup(name);
232 document->root = sp_object_repr_build_tree(document, rroot);
234 sodipodi_version = SP_ROOT(document->root)->version.sodipodi;
236 /* fixme: Not sure about this, but lets assume ::build updates */
237 rroot->setAttribute("sodipodi:version", SODIPODI_VERSION);
238 rroot->setAttribute("inkscape:version", INKSCAPE_VERSION);
239 /* fixme: Again, I moved these here to allow version determining in ::build (Lauris) */
241 /* Quick hack 2 - get default image size into document */
242 if (!rroot->attribute("width")) rroot->setAttribute("width", A4_WIDTH_STR);
243 if (!rroot->attribute("height")) rroot->setAttribute("height", A4_HEIGHT_STR);
244 /* End of quick hack 2 */
246 /* Quick hack 3 - Set uri attributes */
247 if (uri) {
248 /* fixme: Think, what this means for images (Lauris) */
249 rroot->setAttribute("sodipodi:docname", uri);
250 if (document->base)
251 rroot->setAttribute("sodipodi:docbase", document->base);
252 }
253 /* End of quick hack 3 */
255 // creating namedview
256 if (!sp_item_group_get_child_by_name((SPGroup *) document->root, NULL, "sodipodi:namedview")) {
257 // if there's none in the document already,
258 Inkscape::XML::Node *r = NULL;
259 Inkscape::XML::Node *rnew = NULL;
260 r = inkscape_get_repr(INKSCAPE, "template.base");
261 // see if there's a template with id="base" in the preferences
262 if (!r) {
263 // if there's none, create an empty element
264 rnew = sp_repr_new("sodipodi:namedview");
265 rnew->setAttribute("id", "base");
266 } else {
267 // otherwise, take from preferences
268 rnew = r->duplicate();
269 }
270 // insert into the document
271 rroot->addChild(rnew, NULL);
272 // clean up
273 Inkscape::GC::release(rnew);
274 }
276 /* Defs */
277 if (!SP_ROOT(document->root)->defs) {
278 Inkscape::XML::Node *r;
279 r = sp_repr_new("svg:defs");
280 rroot->addChild(r, NULL);
281 Inkscape::GC::release(r);
282 g_assert(SP_ROOT(document->root)->defs);
283 }
285 /* Default RDF */
286 rdf_set_defaults( document );
288 if (keepalive) {
289 inkscape_ref();
290 }
292 sp_document_set_undo_sensitive(document, TRUE);
294 // reset undo key when selection changes, so that same-key actions on different objects are not coalesced
295 if (!Inkscape::NSApplication::Application::getNewGui()) {
296 g_signal_connect(G_OBJECT(INKSCAPE), "change_selection",
297 G_CALLBACK(sp_document_reset_key), document);
298 g_signal_connect(G_OBJECT(INKSCAPE), "activate_desktop",
299 G_CALLBACK(sp_document_reset_key), document);
300 } else {
301 document->_selection_changed_connection = Inkscape::NSApplication::Editor::connectSelectionChanged (sigc::mem_fun (*document, &SPDocument::reset_key));
302 document->_desktop_activated_connection = Inkscape::NSApplication::Editor::connectDesktopActivated (sigc::mem_fun (*document, &SPDocument::reset_key));
303 }
304 inkscape_add_document(document);
306 return document;
307 }
309 /**
310 * Fetches document from URI, or creates new, if NULL; public document
311 * appears in document list.
312 */
313 SPDocument *
314 sp_document_new(gchar const *uri, unsigned int keepalive, bool make_new)
315 {
316 SPDocument *doc;
317 Inkscape::XML::Document *rdoc;
318 gchar *base = NULL;
319 gchar *name = NULL;
321 if (uri) {
322 Inkscape::XML::Node *rroot;
323 gchar *s, *p;
324 /* Try to fetch repr from file */
325 rdoc = sp_repr_read_file(uri, SP_SVG_NS_URI);
326 /* If file cannot be loaded, return NULL without warning */
327 if (rdoc == NULL) return NULL;
328 rroot = sp_repr_document_root(rdoc);
329 /* If xml file is not svg, return NULL without warning */
330 /* fixme: destroy document */
331 if (strcmp(rroot->name(), "svg:svg") != 0) return NULL;
332 s = g_strdup(uri);
333 p = strrchr(s, '/');
334 if (p) {
335 name = g_strdup(p + 1);
336 p[1] = '\0';
337 base = g_strdup(s);
338 } else {
339 base = NULL;
340 name = g_strdup(uri);
341 }
342 g_free(s);
343 } else {
344 rdoc = sp_repr_document_new("svg:svg");
345 }
347 if (make_new) {
348 base = NULL;
349 uri = NULL;
350 name = g_strdup_printf(_("New document %d"), ++doc_count);
351 }
353 //# These should be set by now
354 g_assert(name);
356 doc = sp_document_create(rdoc, uri, base, name, keepalive);
358 g_free(base);
359 g_free(name);
361 return doc;
362 }
364 SPDocument *
365 sp_document_new_from_mem(gchar const *buffer, gint length, unsigned int keepalive)
366 {
367 SPDocument *doc;
368 Inkscape::XML::Document *rdoc;
369 Inkscape::XML::Node *rroot;
370 gchar *name;
372 rdoc = sp_repr_read_mem(buffer, length, SP_SVG_NS_URI);
374 /* If it cannot be loaded, return NULL without warning */
375 if (rdoc == NULL) return NULL;
377 rroot = sp_repr_document_root(rdoc);
378 /* If xml file is not svg, return NULL without warning */
379 /* fixme: destroy document */
380 if (strcmp(rroot->name(), "svg:svg") != 0) return NULL;
382 name = g_strdup_printf(_("Memory document %d"), ++doc_count);
384 doc = sp_document_create(rdoc, NULL, NULL, name, keepalive);
386 return doc;
387 }
389 SPDocument *sp_document_new_dummy() {
390 SPDocument *document = new SPDocument();
391 inkscape_add_document(document);
392 return document;
393 }
395 SPDocument *
396 sp_document_ref(SPDocument *doc)
397 {
398 g_return_val_if_fail(doc != NULL, NULL);
399 Inkscape::GC::anchor(doc);
400 return doc;
401 }
403 SPDocument *
404 sp_document_unref(SPDocument *doc)
405 {
406 g_return_val_if_fail(doc != NULL, NULL);
407 Inkscape::GC::release(doc);
408 return NULL;
409 }
411 gdouble sp_document_width(SPDocument *document)
412 {
413 g_return_val_if_fail(document != NULL, 0.0);
414 g_return_val_if_fail(document->priv != NULL, 0.0);
415 g_return_val_if_fail(document->root != NULL, 0.0);
417 return SP_ROOT(document->root)->width.computed;
418 }
420 void
421 sp_document_set_width (SPDocument *document, gdouble width, const SPUnit *unit)
422 {
423 SPRoot *root = SP_ROOT(document->root);
425 if (root->width.unit == SVGLength::PERCENT && root->viewBox_set) { // set to viewBox=
426 root->viewBox.x1 = root->viewBox.x0 + sp_units_get_pixels (width, *unit);
427 } else { // set to width=
428 root->width.computed = sp_units_get_pixels (width, *unit);
429 /* SVG does not support meters as a unit, so we must translate meters to
430 * cm when writing */
431 if (!strcmp(unit->abbr, "m")) {
432 root->width.value = 100*width;
433 root->width.unit = SVGLength::CM;
434 } else {
435 root->width.value = width;
436 root->width.unit = (SVGLength::Unit) sp_unit_get_svg_unit(unit);
437 }
438 }
440 SP_OBJECT (root)->updateRepr();
441 }
443 void sp_document_set_height (SPDocument * document, gdouble height, const SPUnit *unit)
444 {
445 SPRoot *root = SP_ROOT(document->root);
447 if (root->height.unit == SVGLength::PERCENT && root->viewBox_set) { // set to viewBox=
448 root->viewBox.y1 = root->viewBox.y0 + sp_units_get_pixels (height, *unit);
449 } else { // set to height=
450 root->height.computed = sp_units_get_pixels (height, *unit);
451 /* SVG does not support meters as a unit, so we must translate meters to
452 * cm when writing */
453 if (!strcmp(unit->abbr, "m")) {
454 root->height.value = 100*height;
455 root->height.unit = SVGLength::CM;
456 } else {
457 root->height.value = height;
458 root->height.unit = (SVGLength::Unit) sp_unit_get_svg_unit(unit);
459 }
460 }
462 SP_OBJECT (root)->updateRepr();
463 }
465 gdouble sp_document_height(SPDocument *document)
466 {
467 g_return_val_if_fail(document != NULL, 0.0);
468 g_return_val_if_fail(document->priv != NULL, 0.0);
469 g_return_val_if_fail(document->root != NULL, 0.0);
471 return SP_ROOT(document->root)->height.computed;
472 }
474 void sp_document_set_uri(SPDocument *document, gchar const *uri)
475 {
476 g_return_if_fail(document != NULL);
478 if (document->name) {
479 g_free(document->name);
480 document->name = NULL;
481 }
482 if (document->base) {
483 g_free(document->base);
484 document->base = NULL;
485 }
486 if (document->uri) {
487 g_free(document->uri);
488 document->uri = NULL;
489 }
491 if (uri) {
493 #ifndef WIN32
494 prepend_current_dir_if_relative(&(document->uri), uri);
495 #else
496 // FIXME: it may be that prepend_current_dir_if_relative works OK on windows too, test!
497 document->uri = g_strdup(uri);
498 #endif
500 /* fixme: Think, what this means for images (Lauris) */
501 document->base = g_path_get_dirname(document->uri);
502 document->name = g_path_get_basename(document->uri);
504 } else {
505 document->uri = g_strdup_printf(_("Unnamed document %d"), ++doc_count);
506 document->base = NULL;
507 document->name = g_strdup(document->uri);
508 }
510 // Update saveable repr attributes.
511 Inkscape::XML::Node *repr = sp_document_repr_root(document);
512 // changing uri in the document repr must not be not undoable
513 gboolean saved = sp_document_get_undo_sensitive(document);
514 sp_document_set_undo_sensitive(document, FALSE);
515 if (document->base)
516 repr->setAttribute("sodipodi:docbase", document->base);
518 repr->setAttribute("sodipodi:docname", document->name);
519 sp_document_set_undo_sensitive(document, saved);
521 document->priv->uri_set_signal.emit(document->uri);
522 }
524 void
525 sp_document_resized_signal_emit(SPDocument *doc, gdouble width, gdouble height)
526 {
527 g_return_if_fail(doc != NULL);
529 doc->priv->resized_signal.emit(width, height);
530 }
532 sigc::connection SPDocument::connectModified(SPDocument::ModifiedSignal::slot_type slot)
533 {
534 return priv->modified_signal.connect(slot);
535 }
537 sigc::connection SPDocument::connectURISet(SPDocument::URISetSignal::slot_type slot)
538 {
539 return priv->uri_set_signal.connect(slot);
540 }
542 sigc::connection SPDocument::connectResized(SPDocument::ResizedSignal::slot_type slot)
543 {
544 return priv->resized_signal.connect(slot);
545 }
547 sigc::connection
548 SPDocument::connectReconstructionStart(SPDocument::ReconstructionStart::slot_type slot)
549 {
550 return priv->_reconstruction_start_signal.connect(slot);
551 }
553 void
554 SPDocument::emitReconstructionStart(void)
555 {
556 // printf("Starting Reconstruction\n");
557 priv->_reconstruction_start_signal.emit();
558 return;
559 }
561 sigc::connection
562 SPDocument::connectReconstructionFinish(SPDocument::ReconstructionFinish::slot_type slot)
563 {
564 return priv->_reconstruction_finish_signal.connect(slot);
565 }
567 void
568 SPDocument::emitReconstructionFinish(void)
569 {
570 // printf("Finishing Reconstruction\n");
571 priv->_reconstruction_finish_signal.emit();
572 return;
573 }
576 void SPDocument::_emitModified() {
577 static guint const flags = SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG | SP_OBJECT_PARENT_MODIFIED_FLAG;
578 root->emitModified(0);
579 priv->modified_signal.emit(flags);
580 }
582 void SPDocument::bindObjectToId(gchar const *id, SPObject *object) {
583 GQuark idq = g_quark_from_string(id);
585 if (object) {
586 g_assert(g_hash_table_lookup(priv->iddef, GINT_TO_POINTER(idq)) == NULL);
587 g_hash_table_insert(priv->iddef, GINT_TO_POINTER(idq), object);
588 } else {
589 g_assert(g_hash_table_lookup(priv->iddef, GINT_TO_POINTER(idq)) != NULL);
590 g_hash_table_remove(priv->iddef, GINT_TO_POINTER(idq));
591 }
593 SPDocumentPrivate::IDChangedSignalMap::iterator pos;
595 pos = priv->id_changed_signals.find(idq);
596 if ( pos != priv->id_changed_signals.end() ) {
597 if (!(*pos).second.empty()) {
598 (*pos).second.emit(object);
599 } else { // discard unused signal
600 priv->id_changed_signals.erase(pos);
601 }
602 }
603 }
605 void
606 SPDocument::addUndoObserver(Inkscape::UndoStackObserver& observer)
607 {
608 this->priv->undoStackObservers.add(observer);
609 }
611 void
612 SPDocument::removeUndoObserver(Inkscape::UndoStackObserver& observer)
613 {
614 this->priv->undoStackObservers.remove(observer);
615 }
617 SPObject *SPDocument::getObjectById(gchar const *id) {
618 g_return_val_if_fail(id != NULL, NULL);
620 GQuark idq = g_quark_from_string(id);
621 return (SPObject*)g_hash_table_lookup(priv->iddef, GINT_TO_POINTER(idq));
622 }
624 sigc::connection SPDocument::connectIdChanged(gchar const *id,
625 SPDocument::IDChangedSignal::slot_type slot)
626 {
627 return priv->id_changed_signals[g_quark_from_string(id)].connect(slot);
628 }
630 void SPDocument::bindObjectToRepr(Inkscape::XML::Node *repr, SPObject *object) {
631 if (object) {
632 g_assert(g_hash_table_lookup(priv->reprdef, repr) == NULL);
633 g_hash_table_insert(priv->reprdef, repr, object);
634 } else {
635 g_assert(g_hash_table_lookup(priv->reprdef, repr) != NULL);
636 g_hash_table_remove(priv->reprdef, repr);
637 }
638 }
640 SPObject *SPDocument::getObjectByRepr(Inkscape::XML::Node *repr) {
641 g_return_val_if_fail(repr != NULL, NULL);
642 return (SPObject*)g_hash_table_lookup(priv->reprdef, repr);
643 }
645 /* Object modification root handler */
647 void
648 sp_document_request_modified(SPDocument *doc)
649 {
650 if (!doc->modified_id) {
651 doc->modified_id = gtk_idle_add_priority(SP_DOCUMENT_UPDATE_PRIORITY, sp_document_idle_handler, doc);
652 }
653 }
655 void
656 sp_document_setup_viewport (SPDocument *doc, SPItemCtx *ctx)
657 {
658 ctx->ctx.flags = 0;
659 ctx->i2doc = NR::identity();
660 /* Set up viewport in case svg has it defined as percentages */
661 if (SP_ROOT(doc->root)->viewBox_set) { // if set, take from viewBox
662 ctx->vp.x0 = SP_ROOT(doc->root)->viewBox.x0;
663 ctx->vp.y0 = SP_ROOT(doc->root)->viewBox.y0;
664 ctx->vp.x1 = SP_ROOT(doc->root)->viewBox.x1;
665 ctx->vp.y1 = SP_ROOT(doc->root)->viewBox.y1;
666 } else { // as a last resort, set size to A4
667 ctx->vp.x0 = 0.0;
668 ctx->vp.y0 = 0.0;
669 ctx->vp.x1 = 210 * PX_PER_MM;
670 ctx->vp.y1 = 297 * PX_PER_MM;
671 }
672 ctx->i2vp = NR::identity();
673 }
675 gint
676 sp_document_ensure_up_to_date(SPDocument *doc)
677 {
678 int lc;
679 lc = 32;
680 while (doc->root->uflags || doc->root->mflags) {
681 lc -= 1;
682 if (lc < 0) {
683 g_warning("More than 32 iterations while updating document '%s'", doc->uri);
684 if (doc->modified_id) {
685 /* Remove handler */
686 gtk_idle_remove(doc->modified_id);
687 doc->modified_id = 0;
688 }
689 return FALSE;
690 }
691 /* Process updates */
692 if (doc->root->uflags) {
693 SPItemCtx ctx;
694 sp_document_setup_viewport (doc, &ctx);
695 doc->root->updateDisplay((SPCtx *)&ctx, 0);
696 }
697 doc->_emitModified();
698 }
699 if (doc->modified_id) {
700 /* Remove handler */
701 gtk_idle_remove(doc->modified_id);
702 doc->modified_id = 0;
703 }
704 return TRUE;
705 }
707 static gint
708 sp_document_idle_handler(gpointer data)
709 {
710 SPDocument *doc;
711 int repeat;
713 doc = static_cast<SPDocument *>(data);
715 #ifdef SP_DOCUMENT_DEBUG_IDLE
716 g_print("->\n");
717 #endif
719 /* Process updates */
720 if (doc->root->uflags) {
721 SPItemCtx ctx;
722 sp_document_setup_viewport (doc, &ctx);
724 gboolean saved = sp_document_get_undo_sensitive(doc);
725 sp_document_set_undo_sensitive(doc, FALSE);
727 doc->root->updateDisplay((SPCtx *)&ctx, 0);
729 sp_document_set_undo_sensitive(doc, saved);
730 /* if (doc->root->uflags & SP_OBJECT_MODIFIED_FLAG) return TRUE; */
731 }
733 doc->_emitModified();
735 repeat = (doc->root->uflags || doc->root->mflags);
736 if (!repeat) doc->modified_id = 0;
737 return repeat;
738 }
740 static bool is_within(NR::Rect const &area, NR::Rect const &box)
741 {
742 return area.contains(box);
743 }
745 static bool overlaps(NR::Rect const &area, NR::Rect const &box)
746 {
747 return area.intersects(box);
748 }
750 static GSList *find_items_in_area(GSList *s, SPGroup *group, unsigned int dkey, NR::Rect const &area,
751 bool (*test)(NR::Rect const &, NR::Rect const &), bool take_insensitive = false)
752 {
753 g_return_val_if_fail(SP_IS_GROUP(group), s);
755 for (SPObject *o = sp_object_first_child(SP_OBJECT(group)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
756 if (!SP_IS_ITEM(o)) {
757 continue;
758 }
759 if (SP_IS_GROUP(o) && SP_GROUP(o)->effectiveLayerMode(dkey) == SPGroup::LAYER ) {
760 s = find_items_in_area(s, SP_GROUP(o), dkey, area, test);
761 } else {
762 SPItem *child = SP_ITEM(o);
763 NR::Rect box = sp_item_bbox_desktop(child);
764 if (test(area, box) && (take_insensitive || child->isVisibleAndUnlocked(dkey))) {
765 s = g_slist_append(s, child);
766 }
767 }
768 }
770 return s;
771 }
773 /**
774 Returns true if an item is among the descendants of group (recursively).
775 */
776 bool item_is_in_group(SPItem *item, SPGroup *group)
777 {
778 for (SPObject *o = sp_object_first_child(SP_OBJECT(group)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
779 if (!SP_IS_ITEM(o)) continue;
780 if (SP_ITEM(o) == item)
781 return true;
782 if (SP_IS_GROUP(o))
783 if (item_is_in_group(item, SP_GROUP(o)))
784 return true;
785 }
786 return false;
787 }
789 /**
790 Returns the bottommost item from the list which is at the point, or NULL if none.
791 */
792 SPItem*
793 sp_document_item_from_list_at_point_bottom(unsigned int dkey, SPGroup *group, GSList const *list,
794 NR::Point const p, bool take_insensitive)
795 {
796 g_return_val_if_fail(group, NULL);
798 gdouble delta = prefs_get_double_attribute ("options.cursortolerance", "value", 1.0);
800 for (SPObject *o = sp_object_first_child(SP_OBJECT(group)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
802 if (!SP_IS_ITEM(o)) continue;
804 SPItem *item = SP_ITEM(o);
805 NRArenaItem *arenaitem = sp_item_get_arenaitem(item, dkey);
806 if (arenaitem && nr_arena_item_invoke_pick(arenaitem, p, delta, 1) != NULL
807 && (take_insensitive || item->isVisibleAndUnlocked(dkey))) {
808 if (g_slist_find((GSList *) list, item) != NULL)
809 return item;
810 }
812 if (SP_IS_GROUP(o)) {
813 SPItem *found = sp_document_item_from_list_at_point_bottom(dkey, SP_GROUP(o), list, p, take_insensitive);
814 if (found)
815 return found;
816 }
818 }
819 return NULL;
820 }
822 /**
823 Returns the topmost (in z-order) item from the descendants of group (recursively) which
824 is at the point p, or NULL if none. Honors into_groups on whether to recurse into
825 non-layer groups or not. Honors take_insensitive on whether to return insensitive
826 items. If upto != NULL, then if item upto is encountered (at any level), stops searching
827 upwards in z-order and returns what it has found so far (i.e. the found item is
828 guaranteed to be lower than upto).
829 */
830 SPItem*
831 find_item_at_point(unsigned int dkey, SPGroup *group, NR::Point const p, gboolean into_groups, bool take_insensitive = false, SPItem *upto = NULL)
832 {
833 SPItem *seen = NULL, *newseen = NULL;
835 gdouble delta = prefs_get_double_attribute ("options.cursortolerance", "value", 1.0);
837 for (SPObject *o = sp_object_first_child(SP_OBJECT(group)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
838 if (!SP_IS_ITEM(o)) continue;
840 if (upto && SP_ITEM(o) == upto)
841 break;
843 if (SP_IS_GROUP(o) && (SP_GROUP(o)->effectiveLayerMode(dkey) == SPGroup::LAYER || into_groups)) {
844 // if nothing found yet, recurse into the group
845 newseen = find_item_at_point(dkey, SP_GROUP(o), p, into_groups, take_insensitive, upto);
846 if (newseen) {
847 seen = newseen;
848 newseen = NULL;
849 }
851 if (item_is_in_group(upto, SP_GROUP(o)))
852 break;
854 } else {
855 SPItem *child = SP_ITEM(o);
856 NRArenaItem *arenaitem = sp_item_get_arenaitem(child, dkey);
858 // seen remembers the last (topmost) of items pickable at this point
859 if (arenaitem && nr_arena_item_invoke_pick(arenaitem, p, delta, 1) != NULL
860 && (take_insensitive || child->isVisibleAndUnlocked(dkey))) {
861 seen = child;
862 }
863 }
864 }
865 return seen;
866 }
868 /**
869 Returns the topmost non-layer group from the descendants of group which is at point
870 p, or NULL if none. Recurses into layers but not into groups.
871 */
872 SPItem*
873 find_group_at_point(unsigned int dkey, SPGroup *group, NR::Point const p)
874 {
875 SPItem *seen = NULL;
877 gdouble delta = prefs_get_double_attribute ("options.cursortolerance", "value", 1.0);
879 for (SPObject *o = sp_object_first_child(SP_OBJECT(group)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
880 if (!SP_IS_ITEM(o)) continue;
881 if (SP_IS_GROUP(o) && SP_GROUP(o)->effectiveLayerMode(dkey) == SPGroup::LAYER) {
882 SPItem *newseen = find_group_at_point(dkey, SP_GROUP(o), p);
883 if (newseen) {
884 seen = newseen;
885 }
886 }
887 if (SP_IS_GROUP(o) && SP_GROUP(o)->effectiveLayerMode(dkey) != SPGroup::LAYER ) {
888 SPItem *child = SP_ITEM(o);
889 NRArenaItem *arenaitem = sp_item_get_arenaitem(child, dkey);
891 // seen remembers the last (topmost) of groups pickable at this point
892 if (arenaitem && nr_arena_item_invoke_pick(arenaitem, p, delta, 1) != NULL) {
893 seen = child;
894 }
895 }
896 }
897 return seen;
898 }
900 /*
901 * Return list of items, contained in box
902 *
903 * Assumes box is normalized (and g_asserts it!)
904 *
905 */
907 GSList *sp_document_items_in_box(SPDocument *document, unsigned int dkey, NR::Rect const &box)
908 {
909 g_return_val_if_fail(document != NULL, NULL);
910 g_return_val_if_fail(document->priv != NULL, NULL);
912 return find_items_in_area(NULL, SP_GROUP(document->root), dkey, box, is_within);
913 }
915 /*
916 * Return list of items, that the parts of the item contained in box
917 *
918 * Assumes box is normalized (and g_asserts it!)
919 *
920 */
922 GSList *sp_document_partial_items_in_box(SPDocument *document, unsigned int dkey, NR::Rect const &box)
923 {
924 g_return_val_if_fail(document != NULL, NULL);
925 g_return_val_if_fail(document->priv != NULL, NULL);
927 return find_items_in_area(NULL, SP_GROUP(document->root), dkey, box, overlaps);
928 }
930 SPItem *
931 sp_document_item_at_point(SPDocument *document, unsigned const key, NR::Point const p,
932 gboolean const into_groups, SPItem *upto)
933 {
934 g_return_val_if_fail(document != NULL, NULL);
935 g_return_val_if_fail(document->priv != NULL, NULL);
937 return find_item_at_point(key, SP_GROUP(document->root), p, into_groups, false, upto);
938 }
940 SPItem*
941 sp_document_group_at_point(SPDocument *document, unsigned int key, NR::Point const p)
942 {
943 g_return_val_if_fail(document != NULL, NULL);
944 g_return_val_if_fail(document->priv != NULL, NULL);
946 return find_group_at_point(key, SP_GROUP(document->root), p);
947 }
950 /* Resource management */
952 gboolean
953 sp_document_add_resource(SPDocument *document, gchar const *key, SPObject *object)
954 {
955 GSList *rlist;
956 GQuark q = g_quark_from_string(key);
958 g_return_val_if_fail(document != NULL, FALSE);
959 g_return_val_if_fail(key != NULL, FALSE);
960 g_return_val_if_fail(*key != '\0', FALSE);
961 g_return_val_if_fail(object != NULL, FALSE);
962 g_return_val_if_fail(SP_IS_OBJECT(object), FALSE);
964 if (SP_OBJECT_IS_CLONED(object))
965 return FALSE;
967 rlist = (GSList*)g_hash_table_lookup(document->priv->resources, key);
968 g_return_val_if_fail(!g_slist_find(rlist, object), FALSE);
969 rlist = g_slist_prepend(rlist, object);
970 g_hash_table_insert(document->priv->resources, (gpointer) key, rlist);
972 document->priv->resources_changed_signals[q].emit();
974 return TRUE;
975 }
977 gboolean
978 sp_document_remove_resource(SPDocument *document, gchar const *key, SPObject *object)
979 {
980 GSList *rlist;
981 GQuark q = g_quark_from_string(key);
983 g_return_val_if_fail(document != NULL, FALSE);
984 g_return_val_if_fail(key != NULL, FALSE);
985 g_return_val_if_fail(*key != '\0', FALSE);
986 g_return_val_if_fail(object != NULL, FALSE);
987 g_return_val_if_fail(SP_IS_OBJECT(object), FALSE);
989 if (SP_OBJECT_IS_CLONED(object))
990 return FALSE;
992 rlist = (GSList*)g_hash_table_lookup(document->priv->resources, key);
993 g_return_val_if_fail(rlist != NULL, FALSE);
994 g_return_val_if_fail(g_slist_find(rlist, object), FALSE);
995 rlist = g_slist_remove(rlist, object);
996 g_hash_table_insert(document->priv->resources, (gpointer) key, rlist);
998 document->priv->resources_changed_signals[q].emit();
1000 return TRUE;
1001 }
1003 GSList const *
1004 sp_document_get_resource_list(SPDocument *document, gchar const *key)
1005 {
1006 g_return_val_if_fail(document != NULL, NULL);
1007 g_return_val_if_fail(key != NULL, NULL);
1008 g_return_val_if_fail(*key != '\0', NULL);
1010 return (GSList*)g_hash_table_lookup(document->priv->resources, key);
1011 }
1013 sigc::connection sp_document_resources_changed_connect(SPDocument *document,
1014 gchar const *key,
1015 SPDocument::ResourcesChangedSignal::slot_type slot)
1016 {
1017 GQuark q = g_quark_from_string(key);
1018 return document->priv->resources_changed_signals[q].connect(slot);
1019 }
1021 /* Helpers */
1023 gboolean
1024 sp_document_resource_list_free(gpointer key, gpointer value, gpointer data)
1025 {
1026 g_slist_free((GSList *) value);
1027 return TRUE;
1028 }
1030 unsigned int
1031 count_objects_recursive(SPObject *obj, unsigned int count)
1032 {
1033 count++; // obj itself
1035 for (SPObject *i = sp_object_first_child(obj); i != NULL; i = SP_OBJECT_NEXT(i)) {
1036 count = count_objects_recursive(i, count);
1037 }
1039 return count;
1040 }
1042 unsigned int
1043 objects_in_document(SPDocument *document)
1044 {
1045 return count_objects_recursive(SP_DOCUMENT_ROOT(document), 0);
1046 }
1048 void
1049 vacuum_document_recursive(SPObject *obj)
1050 {
1051 if (SP_IS_DEFS(obj)) {
1052 for (SPObject *def = obj->firstChild(); def; def = SP_OBJECT_NEXT(def)) {
1053 /* fixme: some inkscape-internal nodes in the future might not be collectable */
1054 def->requestOrphanCollection();
1055 }
1056 } else {
1057 for (SPObject *i = sp_object_first_child(obj); i != NULL; i = SP_OBJECT_NEXT(i)) {
1058 vacuum_document_recursive(i);
1059 }
1060 }
1061 }
1063 unsigned int
1064 vacuum_document(SPDocument *document)
1065 {
1066 unsigned int start = objects_in_document(document);
1067 unsigned int end;
1068 unsigned int newend = start;
1070 unsigned int iterations = 0;
1072 do {
1073 end = newend;
1075 vacuum_document_recursive(SP_DOCUMENT_ROOT(document));
1076 document->collectOrphans();
1077 iterations++;
1079 newend = objects_in_document(document);
1081 } while (iterations < 100 && newend < end);
1083 return start - newend;
1084 }
1087 /*
1088 Local Variables:
1089 mode:c++
1090 c-file-style:"stroustrup"
1091 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1092 indent-tabs-mode:nil
1093 fill-column:99
1094 End:
1095 */
1096 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :