Code

make spcurve::first_point and last_point boost::optional
[inkscape.git] / src / sp-root.cpp
index 1bb77ccc78f8615e31780947a66db35dad056396..0ff3b48b6d241408e5ed5f85f1438b4aedc3ac85 100644 (file)
  * Released under GNU GPL, read the file 'COPYING' for more information
  */
 
-#include "config.h"
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <cstring>
+#include <string>
 
 #include "svg/svg.h"
 #include "display/nr-arena-group.h"
@@ -43,7 +48,7 @@ static void sp_root_child_added(SPObject *object, Inkscape::XML::Node *child, In
 static void sp_root_remove_child(SPObject *object, Inkscape::XML::Node *child);
 static void sp_root_update(SPObject *object, SPCtx *ctx, guint flags);
 static void sp_root_modified(SPObject *object, guint flags);
-static Inkscape::XML::Node *sp_root_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
+static Inkscape::XML::Node *sp_root_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
 
 static NRArenaItem *sp_root_show(SPItem *item, NRArena *arena, unsigned int key, unsigned int flags);
 static void sp_root_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags);
@@ -123,7 +128,7 @@ sp_root_init(SPRoot *root)
     root->width.unset(SVGLength::PERCENT, 1.0, 1.0);
     root->height.unset(SVGLength::PERCENT, 1.0, 1.0);
 
-    /* nr_matrix_set_identity(&root->viewbox); */
+    /* root->viewbox.set_identity(); */
     root->viewBox_set = FALSE;
 
     root->c2p.set_identity();
@@ -157,6 +162,7 @@ sp_root_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
     sp_object_read_attr(object, "height");
     sp_object_read_attr(object, "viewBox");
     sp_object_read_attr(object, "preserveAspectRatio");
+    sp_object_read_attr(object, "onload");
 
     if (((SPObjectClass *) parent_class)->build)
         (* ((SPObjectClass *) parent_class)->build) (object, document, repr);
@@ -330,6 +336,9 @@ sp_root_set(SPObject *object, unsigned int key, gchar const *value)
                 root->aspect_clip = clip;
             }
             break;
+        case SP_ATTR_ONLOAD:
+            root->onload = (char *) value;
+            break;
         default:
             /* Pass the set event to the parent */
             if (((SPObjectClass *) parent_class)->set) {
@@ -353,9 +362,9 @@ sp_root_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML:
         (* ((SPObjectClass *) (parent_class))->child_added)(object, child, ref);
 
     SPObject *co = object->document->getObjectByRepr(child);
-    g_assert(co != NULL);
+    g_assert (co != NULL || !strcmp("comment", child->name())); // comment repr node has no object
 
-    if (SP_IS_DEFS(co)) {
+    if (co && SP_IS_DEFS(co)) {
         SPObject *c;
         /* We search for first <defs> node - it is not beautiful, but works */
         for (c = sp_object_first_child(SP_OBJECT(group)) ; c != NULL; c = SP_OBJECT_NEXT(c) ) {
@@ -385,7 +394,6 @@ static void sp_root_remove_child(SPObject *object, Inkscape::XML::Node *child)
         }
         if (!iter) {
             /* we should probably create a new <defs> here? */
-            g_critical("Last <defs> removed");
             root->defs = NULL;
         }
     }
@@ -573,12 +581,12 @@ sp_root_modified(SPObject *object, guint flags)
  * Writes the object into the repr object, then calls the parent's write routine.
  */
 static Inkscape::XML::Node *
-sp_root_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
+sp_root_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
 {
     SPRoot *root = SP_ROOT(object);
 
     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
-        repr = sp_repr_new("svg:svg");
+        repr = xml_doc->createElement("svg:svg");
     }
 
     if (flags & SP_OBJECT_WRITE_EXT) {
@@ -603,10 +611,10 @@ sp_root_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
         Inkscape::SVGOStringStream os;
         os << root->viewBox.x0 << " " << root->viewBox.y0 << " " << root->viewBox.x1 - root->viewBox.x0 << " " << root->viewBox.y1 - root->viewBox.y0;
         repr->setAttribute("viewBox", os.str().c_str());
-    } 
+    }
 
     if (((SPObjectClass *) (parent_class))->write)
-        ((SPObjectClass *) (parent_class))->write(object, repr, flags);
+        ((SPObjectClass *) (parent_class))->write(object, xml_doc, repr, flags);
 
     return repr;
 }