Code

No more NRMatrix or NRPoint.
[inkscape.git] / src / sp-namedview.cpp
index c3a1c58ad8b2da66da4d05b351568c48ccd13c3f..ec0552653b76c3106575eb7eb431d6800a9a6680 100644 (file)
@@ -15,6 +15,8 @@
  */
 
 #include "config.h"
+#include <cstring>
+#include <string>
 
 #include "display/canvas-grid.h"
 #include "helper/units.h"
@@ -68,15 +70,15 @@ sp_namedview_get_type()
     if (!namedview_type) {
         GTypeInfo namedview_info = {
             sizeof(SPNamedViewClass),
-            NULL,      /* base_init */
-            NULL,      /* base_finalize */
+            NULL,       /* base_init */
+            NULL,       /* base_finalize */
             (GClassInitFunc) sp_namedview_class_init,
-            NULL,      /* class_finalize */
-            NULL,      /* class_data */
+            NULL,       /* class_finalize */
+            NULL,       /* class_data */
             sizeof(SPNamedView),
-            16,        /* n_preallocs */
+            16, /* n_preallocs */
             (GInstanceInitFunc) sp_namedview_init,
-            NULL,      /* value_table */
+            NULL,       /* value_table */
         };
         namedview_type = g_type_register_static(SP_TYPE_OBJECTGROUP, "SPNamedView", &namedview_info, (GTypeFlags)0);
     }
@@ -105,6 +107,7 @@ static void sp_namedview_init(SPNamedView *nv)
 {
     nv->editable = TRUE;
     nv->showguides = TRUE;
+    nv->grids_visible = false;
     nv->showborder = TRUE;
     nv->showpageshadow = TRUE;
 
@@ -119,6 +122,94 @@ static void sp_namedview_init(SPNamedView *nv)
     new (&nv->snap_manager) SnapManager(nv);
 }
 
+static void sp_namedview_generate_old_grid(SPNamedView * /*nv*/, SPDocument *document, Inkscape::XML::Node *repr) {
+    bool old_grid_settings_present = false;
+
+    // set old settings
+    const char* gridspacingx    = "1px";
+    const char* gridspacingy    = "1px";
+    const char* gridoriginy     = "0px";
+    const char* gridoriginx     = "0px";
+    const char* gridempspacing  = "5";
+    const char* gridcolor       = "#0000ff";
+    const char* gridempcolor    = "#0000ff";
+    const char* gridopacity     = "0.2";
+    const char* gridempopacity  = "0.4";
+
+    const char* value = NULL;
+    if ((value = repr->attribute("gridoriginx"))) {
+        gridspacingx = value;
+        old_grid_settings_present = true;
+    }
+    if ((value = repr->attribute("gridoriginy"))) {
+        gridoriginy = value;
+        old_grid_settings_present = true;
+    }
+    if ((value = repr->attribute("gridspacingx"))) {
+        gridspacingx = value;
+        old_grid_settings_present = true;
+    }
+    if ((value = repr->attribute("gridspacingy"))) {
+        gridspacingy = value;
+        old_grid_settings_present = true;
+    }
+    if ((value = repr->attribute("gridcolor"))) {
+        gridcolor = value;
+        old_grid_settings_present = true;
+    }
+    if ((value = repr->attribute("gridempcolor"))) {
+        gridempcolor = value;
+        old_grid_settings_present = true;
+    }
+    if ((value = repr->attribute("gridempspacing"))) {
+        gridempspacing = value;
+        old_grid_settings_present = true;
+    }
+    if ((value = repr->attribute("gridopacity"))) {
+        gridopacity = value;
+        old_grid_settings_present = true;
+    }
+    if ((value = repr->attribute("gridempopacity"))) {
+        gridempopacity = value;
+        old_grid_settings_present = true;
+    }
+
+    if (old_grid_settings_present) {
+        // generate new xy grid with the correct settings
+        // first create the child xml node, then hook it to repr. This order is important, to not set off listeners to repr before the new node is complete.
+
+        Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
+        Inkscape::XML::Node *newnode = xml_doc->createElement("inkscape:grid");
+        newnode->setAttribute("id", "GridFromPre046Settings");
+        newnode->setAttribute("type", Inkscape::CanvasGrid::getSVGName(Inkscape::GRID_RECTANGULAR));
+        newnode->setAttribute("originx", gridoriginx);
+        newnode->setAttribute("originy", gridoriginy);
+        newnode->setAttribute("spacingx", gridspacingx);
+        newnode->setAttribute("spacingy", gridspacingy);
+        newnode->setAttribute("color", gridcolor);
+        newnode->setAttribute("empcolor", gridempcolor);
+        newnode->setAttribute("opacity", gridopacity);
+        newnode->setAttribute("empopacity", gridempopacity);
+        newnode->setAttribute("empspacing", gridempspacing);
+
+        repr->appendChild(newnode);
+        Inkscape::GC::release(newnode);
+
+        // remove all old settings
+        repr->setAttribute("gridoriginx", NULL);
+        repr->setAttribute("gridoriginy", NULL);
+        repr->setAttribute("gridspacingx", NULL);
+        repr->setAttribute("gridspacingy", NULL);
+        repr->setAttribute("gridcolor", NULL);
+        repr->setAttribute("gridempcolor", NULL);
+        repr->setAttribute("gridopacity", NULL);
+        repr->setAttribute("gridempopacity", NULL);
+        repr->setAttribute("gridempspacing", NULL);
+
+//        sp_document_done(doc, SP_VERB_DIALOG_NAMEDVIEW, _("Create new grid from pre0.46 grid settings"));
+    }
+}
+
 static void sp_namedview_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
 {
     SPNamedView *nv = (SPNamedView *) object;
@@ -131,6 +222,7 @@ static void sp_namedview_build(SPObject *object, SPDocument *document, Inkscape:
     sp_object_read_attr(object, "inkscape:document-units");
     sp_object_read_attr(object, "viewonly");
     sp_object_read_attr(object, "showguides");
+    sp_object_read_attr(object, "showgrid");
     sp_object_read_attr(object, "gridtolerance");
     sp_object_read_attr(object, "guidetolerance");
     sp_object_read_attr(object, "objecttolerance");
@@ -153,19 +245,22 @@ static void sp_namedview_build(SPObject *object, SPDocument *document, Inkscape:
     sp_object_read_attr(object, "inkscape:window-height");
     sp_object_read_attr(object, "inkscape:window-x");
     sp_object_read_attr(object, "inkscape:window-y");
-/*  sp_object_read_attr(object, "inkscape:grid-bbox");
-    sp_object_read_attr(object, "inkscape:guide-bbox");
-    sp_object_read_attr(object, "inkscape:object-bbox");
-    sp_object_read_attr(object, "inkscape:grid-points");
-    sp_object_read_attr(object, "inkscape:guide-points");
-    sp_object_read_attr(object, "inkscape:object-points");*/
+    sp_object_read_attr(object, "inkscape:snap-global");
+    sp_object_read_attr(object, "inkscape:snap-bbox");
+    sp_object_read_attr(object, "inkscape:snap-nodes");
+    sp_object_read_attr(object, "inkscape:snap-guide");
+    sp_object_read_attr(object, "inkscape:snap-center");
+    sp_object_read_attr(object, "inkscape:snap-intersection-grid-guide");
+    sp_object_read_attr(object, "inkscape:snap-intersection-line-segments");
     sp_object_read_attr(object, "inkscape:object-paths");
     sp_object_read_attr(object, "inkscape:object-nodes");
+    sp_object_read_attr(object, "inkscape:bbox-paths");
+    sp_object_read_attr(object, "inkscape:bbox-nodes");
+    sp_object_read_attr(object, "inkscape:snap-page");
     sp_object_read_attr(object, "inkscape:current-layer");
     sp_object_read_attr(object, "inkscape:connector-spacing");
 
     /* Construct guideline list */
-
     for (SPObject *o = sp_object_first_child(SP_OBJECT(og)) ; o != NULL; o = SP_OBJECT_NEXT(o) ) {
         if (SP_IS_GUIDE(o)) {
             SPGuide * g = SP_GUIDE(o);
@@ -173,6 +268,9 @@ static void sp_namedview_build(SPObject *object, SPDocument *document, Inkscape:
             g_object_set(G_OBJECT(g), "color", nv->guidecolor, "hicolor", nv->guidehicolor, NULL);
         }
     }
+
+    // backwards compatibility with grid settings (pre 0.46)
+    sp_namedview_generate_old_grid(nv, document, repr);
 }
 
 static void sp_namedview_release(SPObject *object)
@@ -186,9 +284,9 @@ static void sp_namedview_release(SPObject *object)
 
     // delete grids:
     while ( namedview->grids ) {
-        Inkscape::CanvasGrid *gr = (Inkscape::CanvasGrid *)namedview->grids->data;
+        Inkscape::CanvasGrid *gr = (Inkscape::CanvasGrid *)namedview->grids->data; // get first entry
         delete gr;
-        namedview->grids = g_slist_remove_link(namedview->grids, namedview->grids);
+        namedview->grids = g_slist_remove_link(namedview->grids, namedview->grids); // deletes first entry
     }
 
     if (((SPObjectClass *) parent_class)->release) {
@@ -204,11 +302,11 @@ static void sp_namedview_set(SPObject *object, unsigned int key, const gchar *va
     SPUnit const &px = sp_unit_get_by_id(SP_UNIT_PX);
 
     switch (key) {
-       case SP_ATTR_VIEWONLY:
+    case SP_ATTR_VIEWONLY:
             nv->editable = (!value);
             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
             break;
-       case SP_ATTR_SHOWGUIDES:
+    case SP_ATTR_SHOWGUIDES:
             if (!value) { // show guides if not specified, for backwards compatibility
                 nv->showguides = TRUE;
             } else {
@@ -217,7 +315,15 @@ static void sp_namedview_set(SPObject *object, unsigned int key, const gchar *va
             sp_namedview_setup_guides(nv);
             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
             break;
-       case SP_ATTR_GRIDTOLERANCE:
+    case SP_ATTR_SHOWGRIDS:
+            if (!value) { // don't show grids if not specified, for backwards compatibility
+                nv->grids_visible = false;
+            } else {
+                nv->grids_visible = sp_str_to_bool(value);
+            }
+            object->requestModified(SP_OBJECT_MODIFIED_FLAG);
+            break;
+    case SP_ATTR_GRIDTOLERANCE:
             nv->gridtoleranceunit = &px;
             nv->gridtolerance = DEFAULTTOLERANCE;
             if (value) {
@@ -225,7 +331,7 @@ static void sp_namedview_set(SPObject *object, unsigned int key, const gchar *va
             }
             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
             break;
-       case SP_ATTR_GUIDETOLERANCE:
+    case SP_ATTR_GUIDETOLERANCE:
             nv->guidetoleranceunit = &px;
             nv->guidetolerance = DEFAULTTOLERANCE;
             if (value) {
@@ -233,7 +339,7 @@ static void sp_namedview_set(SPObject *object, unsigned int key, const gchar *va
             }
             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
             break;
-        case SP_ATTR_OBJECTTOLERANCE:
+    case SP_ATTR_OBJECTTOLERANCE:
             nv->objecttoleranceunit = &px;
             nv->objecttolerance = DEFAULTTOLERANCE;
             if (value) {
@@ -241,7 +347,7 @@ static void sp_namedview_set(SPObject *object, unsigned int key, const gchar *va
             }
             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
             break;
-       case SP_ATTR_GUIDECOLOR:
+    case SP_ATTR_GUIDECOLOR:
             nv->guidecolor = (nv->guidecolor & 0xff) | (DEFAULTGUIDECOLOR & 0xffffff00);
             if (value) {
                 nv->guidecolor = (nv->guidecolor & 0xff) | sp_svg_read_color(value, nv->guidecolor);
@@ -251,7 +357,7 @@ static void sp_namedview_set(SPObject *object, unsigned int key, const gchar *va
             }
             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
             break;
-       case SP_ATTR_GUIDEOPACITY:
+    case SP_ATTR_GUIDEOPACITY:
             nv->guidecolor = (nv->guidecolor & 0xffffff00) | (DEFAULTGUIDECOLOR & 0xff);
             sp_nv_read_opacity(value, &nv->guidecolor);
             for (GSList *l = nv->guides; l != NULL; l = l->next) {
@@ -259,7 +365,7 @@ static void sp_namedview_set(SPObject *object, unsigned int key, const gchar *va
             }
             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
             break;
-       case SP_ATTR_GUIDEHICOLOR:
+    case SP_ATTR_GUIDEHICOLOR:
             nv->guidehicolor = (nv->guidehicolor & 0xff) | (DEFAULTGUIDEHICOLOR & 0xffffff00);
             if (value) {
                 nv->guidehicolor = (nv->guidehicolor & 0xff) | sp_svg_read_color(value, nv->guidehicolor);
@@ -269,7 +375,7 @@ static void sp_namedview_set(SPObject *object, unsigned int key, const gchar *va
             }
             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
             break;
-       case SP_ATTR_GUIDEHIOPACITY:
+    case SP_ATTR_GUIDEHIOPACITY:
             nv->guidehicolor = (nv->guidehicolor & 0xffffff00) | (DEFAULTGUIDEHICOLOR & 0xff);
             sp_nv_read_opacity(value, &nv->guidehicolor);
             for (GSList *l = nv->guides; l != NULL; l = l->next) {
@@ -277,118 +383,133 @@ static void sp_namedview_set(SPObject *object, unsigned int key, const gchar *va
             }
             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
             break;
-       case SP_ATTR_SHOWBORDER:
+    case SP_ATTR_SHOWBORDER:
             nv->showborder = (value) ? sp_str_to_bool (value) : TRUE;
             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
             break;
-       case SP_ATTR_BORDERLAYER:
+    case SP_ATTR_BORDERLAYER:
             nv->borderlayer = SP_BORDER_LAYER_BOTTOM;
             if (value && !strcasecmp(value, "true")) nv->borderlayer = SP_BORDER_LAYER_TOP;
             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
             break;
-       case SP_ATTR_BORDERCOLOR:
+    case SP_ATTR_BORDERCOLOR:
             nv->bordercolor = (nv->bordercolor & 0xff) | (DEFAULTBORDERCOLOR & 0xffffff00);
             if (value) {
                 nv->bordercolor = (nv->bordercolor & 0xff) | sp_svg_read_color (value, nv->bordercolor);
             }
             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
             break;
-       case SP_ATTR_BORDEROPACITY:
+    case SP_ATTR_BORDEROPACITY:
             nv->bordercolor = (nv->bordercolor & 0xffffff00) | (DEFAULTBORDERCOLOR & 0xff);
             sp_nv_read_opacity(value, &nv->bordercolor);
             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
             break;
-       case SP_ATTR_PAGECOLOR:
+    case SP_ATTR_PAGECOLOR:
             nv->pagecolor = (nv->pagecolor & 0xff) | (DEFAULTPAGECOLOR & 0xffffff00);
             if (value) {
                 nv->pagecolor = (nv->pagecolor & 0xff) | sp_svg_read_color(value, nv->pagecolor);
             }
             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
             break;
-       case SP_ATTR_INKSCAPE_PAGEOPACITY:
+    case SP_ATTR_INKSCAPE_PAGEOPACITY:
             nv->pagecolor = (nv->pagecolor & 0xffffff00) | (DEFAULTPAGECOLOR & 0xff);
             sp_nv_read_opacity(value, &nv->pagecolor);
             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
             break;
-       case SP_ATTR_INKSCAPE_PAGESHADOW:
+    case SP_ATTR_INKSCAPE_PAGESHADOW:
             nv->pageshadow = value? atoi(value) : 2; // 2 is the default
             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
             break;
-        case SP_ATTR_SHOWPAGESHADOW:
+    case SP_ATTR_SHOWPAGESHADOW:
             nv->showpageshadow = (value) ? sp_str_to_bool(value) : TRUE;
             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
             break;
-       case SP_ATTR_INKSCAPE_ZOOM:
+    case SP_ATTR_INKSCAPE_ZOOM:
             nv->zoom = value ? g_ascii_strtod(value, NULL) : 0; // zero means not set
             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
             break;
-       case SP_ATTR_INKSCAPE_CX:
+    case SP_ATTR_INKSCAPE_CX:
             nv->cx = value ? g_ascii_strtod(value, NULL) : HUGE_VAL; // HUGE_VAL means not set
             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
             break;
-       case SP_ATTR_INKSCAPE_CY:
+    case SP_ATTR_INKSCAPE_CY:
             nv->cy = value ? g_ascii_strtod(value, NULL) : HUGE_VAL; // HUGE_VAL means not set
             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
             break;
-       case SP_ATTR_INKSCAPE_WINDOW_WIDTH:
+    case SP_ATTR_INKSCAPE_WINDOW_WIDTH:
             nv->window_width = value? atoi(value) : -1; // -1 means not set
             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
             break;
-       case SP_ATTR_INKSCAPE_WINDOW_HEIGHT:
+    case SP_ATTR_INKSCAPE_WINDOW_HEIGHT:
             nv->window_height = value ? atoi(value) : -1; // -1 means not set
             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
             break;
-       case SP_ATTR_INKSCAPE_WINDOW_X:
+    case SP_ATTR_INKSCAPE_WINDOW_X:
             nv->window_x = value ? atoi(value) : -1; // -1 means not set
             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
             break;
-       case SP_ATTR_INKSCAPE_WINDOW_Y:
+    case SP_ATTR_INKSCAPE_WINDOW_Y:
             nv->window_y = value ? atoi(value) : -1; // -1 means not set
             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
             break;
-/*     case SP_ATTR_INKSCAPE_GRID_BBOX:
-            nv->snap_manager.grid.setSnapTo(Inkscape::Snapper::BBOX_POINT, value ? sp_str_to_bool(value) : TRUE);
+    case SP_ATTR_INKSCAPE_SNAP_GLOBAL:
+            nv->snap_manager.setSnapEnabledGlobally(value ? sp_str_to_bool(value) : TRUE);
             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
             break;
-       case SP_ATTR_INKSCAPE_GRID_POINTS:
-            nv->snap_manager.grid.setSnapTo(Inkscape::Snapper::SNAP_POINT, value ? sp_str_to_bool(value) : FALSE);
+    case SP_ATTR_INKSCAPE_SNAP_BBOX:
+            nv->snap_manager.setSnapModeBBox(value ? sp_str_to_bool(value) : FALSE);
             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
             break;
-       case SP_ATTR_INKSCAPE_GUIDE_BBOX:
-            nv->snap_manager.guide.setSnapTo(Inkscape::Snapper::BBOX_POINT, value ? sp_str_to_bool(value) : TRUE);
+    case SP_ATTR_INKSCAPE_SNAP_NODES:
+            nv->snap_manager.setSnapModeNode(value ? sp_str_to_bool(value) : TRUE);
             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
             break;
-       case SP_ATTR_INKSCAPE_GUIDE_POINTS:
-            nv->snap_manager.guide.setSnapTo(Inkscape::Snapper::SNAP_POINT, value ? sp_str_to_bool(value) : FALSE);
+    case SP_ATTR_INKSCAPE_SNAP_CENTER:
+            nv->snap_manager.setIncludeItemCenter(value ? sp_str_to_bool(value) : FALSE);
             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
             break;
-        case SP_ATTR_INKSCAPE_OBJECT_BBOX:
-            nv->snap_manager.object.setSnapTo(Inkscape::Snapper::BBOX_POINT, (value) ? sp_str_to_bool(value) : FALSE);
+    case SP_ATTR_INKSCAPE_SNAP_GUIDE:
+            nv->snap_manager.setSnapModeGuide(value ? sp_str_to_bool(value) : FALSE);
             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
             break;
-        case SP_ATTR_INKSCAPE_OBJECT_POINTS:
-            nv->snap_manager.object.setSnapTo(Inkscape::Snapper::SNAP_POINT, (value) ? sp_str_to_bool(value) : FALSE);
+    case SP_ATTR_INKSCAPE_SNAP_INTERS_GRIDGUIDE:
+            nv->snap_manager.setSnapIntersectionGG(value ? sp_str_to_bool(value) : TRUE);
             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
             break;
-*/
-        case SP_ATTR_INKSCAPE_OBJECT_PATHS:
-            nv->snap_manager.object.setSnapToPaths(value ? sp_str_to_bool(value) : FALSE);
+    case SP_ATTR_INKSCAPE_SNAP_INTERS_LINESEGM:
+            nv->snap_manager.setSnapIntersectionLS(value ? sp_str_to_bool(value) : FALSE);
+            object->requestModified(SP_OBJECT_MODIFIED_FLAG);
+            break;
+    case SP_ATTR_INKSCAPE_OBJECT_PATHS:
+            nv->snap_manager.object.setSnapToItemPath(value ? sp_str_to_bool(value) : FALSE);
+            object->requestModified(SP_OBJECT_MODIFIED_FLAG);
+            break;
+    case SP_ATTR_INKSCAPE_OBJECT_NODES:
+            nv->snap_manager.object.setSnapToItemNode(value ? sp_str_to_bool(value) : FALSE);
             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
             break;
-        case SP_ATTR_INKSCAPE_OBJECT_NODES:
-            nv->snap_manager.object.setSnapToNodes(value ? sp_str_to_bool(value) : FALSE);
+    case SP_ATTR_INKSCAPE_BBOX_PATHS:
+            nv->snap_manager.object.setSnapToBBoxPath(value ? sp_str_to_bool(value) : FALSE);
             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
             break;
-       case SP_ATTR_INKSCAPE_CURRENT_LAYER:
+    case SP_ATTR_INKSCAPE_BBOX_NODES:
+            nv->snap_manager.object.setSnapToBBoxNode(value ? sp_str_to_bool(value) : FALSE);
+            object->requestModified(SP_OBJECT_MODIFIED_FLAG);
+            break;
+    case SP_ATTR_INKSCAPE_SNAP_PAGE:
+            nv->snap_manager.object.setSnapToPageBorder(value ? sp_str_to_bool(value) : FALSE);
+            object->requestModified(SP_OBJECT_MODIFIED_FLAG);
+            break;    
+    case SP_ATTR_INKSCAPE_CURRENT_LAYER:
             nv->default_layer_id = value ? g_quark_from_string(value) : 0;
             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
             break;
-       case SP_ATTR_INKSCAPE_CONNECTOR_SPACING:
+    case SP_ATTR_INKSCAPE_CONNECTOR_SPACING:
             nv->connector_spacing = value ? g_ascii_strtod(value, NULL) :
                     defaultConnSpacing;
             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
             break;
-       case SP_ATTR_INKSCAPE_DOCUMENT_UNITS: {
+    case SP_ATTR_INKSCAPE_DOCUMENT_UNITS: {
             /* The default unit if the document doesn't override this: e.g. for files saved as
              * `plain SVG', or non-inkscape files, or files created by an inkscape 0.40 &
              * earlier.
@@ -428,8 +549,8 @@ static void sp_namedview_set(SPObject *object, unsigned int key, const gchar *va
             nv->doc_units = new_unit;
             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
             break;
-       }
-       default:
+    }
+    default:
             if (((SPObjectClass *) (parent_class))->set) {
                 ((SPObjectClass *) (parent_class))->set(object, key, value);
             }
@@ -441,7 +562,7 @@ static void sp_namedview_set(SPObject *object, unsigned int key, const gchar *va
 * add a grid item from SVG-repr. Check if this namedview already has a gridobject for this one! If desktop=null, add grid-canvasitem to all desktops of this namedview,
 * otherwise only add it to the specified desktop.
 */
-static Inkscape::CanvasGrid* 
+static Inkscape::CanvasGrid*
 sp_namedview_add_grid(SPNamedView *nv, Inkscape::XML::Node *repr, SPDesktop *desktop) {
     Inkscape::CanvasGrid* grid = NULL;
     //check if namedview already has an object for this grid
@@ -452,18 +573,28 @@ sp_namedview_add_grid(SPNamedView *nv, Inkscape::XML::Node *repr, SPDesktop *des
             break;
         }
     }
-    
+
     if (!grid) {
         //create grid object
-        const char * gridtype = repr->attribute("type");
-        if (!gridtype) {
-            gridtype = "xygrid"; // use this as default gridtype when none is specified
-            repr->setAttribute("type", gridtype);
+        Inkscape::GridType gridtype = Inkscape::CanvasGrid::getGridTypeFromSVGName(repr->attribute("type"));
+        SPDocument *doc = NULL;
+        if (desktop)
+            doc = sp_desktop_document(desktop);
+        else
+            doc = sp_desktop_document(static_cast<SPDesktop*>(nv->views->data));
+        if (!doc) {
+            g_warning("sp_namedview_add_grid - how come doc is null here?!");
+            return NULL;
         }
-        grid = Inkscape::CanvasGrid::NewGrid(nv, repr, gridtype);
+        grid = Inkscape::CanvasGrid::NewGrid(nv, repr, doc, gridtype);
         nv->grids = g_slist_append(nv->grids, grid);
+        //Initialize the snapping parameters for the new grid
+        bool enabled_node = nv->snap_manager.getSnapModeNode();
+        bool enabled_bbox = nv->snap_manager.getSnapModeBBox();
+        grid->snapper->setSnapFrom(Inkscape::Snapper::SNAPPOINT_NODE, enabled_node);
+        grid->snapper->setSnapFrom(Inkscape::Snapper::SNAPPOINT_BBOX, enabled_bbox);
     }
-    
+
     if (!desktop) {
         //add canvasitem to all desktops
         for (GSList *l = nv->views; l != NULL; l = l->next) {
@@ -486,12 +617,12 @@ static void sp_namedview_child_added(SPObject *object, Inkscape::XML::Node *chil
         (* ((SPObjectClass *) (parent_class))->child_added)(object, child, ref);
     }
 
-    const gchar *id = child->attribute("id");
     if (!strcmp(child->name(), "inkscape:grid")) {
         sp_namedview_add_grid(nv, child, NULL);
     } else {
-        SPObject *no = object->document->getObjectById(id);
-        g_assert(SP_IS_OBJECT(no));
+        SPObject *no = object->document->getObjectByRepr(child);
+        if ( !SP_IS_OBJECT(no) )
+            return;
 
         if (SP_IS_GUIDE(no)) {
             SPGuide *g = (SPGuide *) no;
@@ -595,20 +726,23 @@ void SPNamedView::show(SPDesktop *desktop)
             }
         }
     }
+
+    desktop->showGrids(grids_visible, false);
 }
 
 #define MIN_ONSCREEN_DISTANCE 50
 
 /*
- * Restores window geometry from the document settings
+ * Restores window geometry from the document settings or defaults in prefs
  */
 void sp_namedview_window_from_document(SPDesktop *desktop)
 {
     SPNamedView *nv = desktop->namedview;
-    gint save_geometry = prefs_get_int_attribute("options.savewindowgeometry", "value", 0);
+    gint geometry_from_file =
+        (1==prefs_get_int_attribute("options.savewindowgeometry", "value", 0));
 
-    // restore window size and position
-    if (save_geometry) {
+    // restore window size and position stored with the document
+    if (geometry_from_file) {
         gint w = MIN(gdk_screen_width(), nv->window_width);
         gint h = MIN(gdk_screen_height(), nv->window_height);
         gint x = MIN(gdk_screen_width() - MIN_ONSCREEN_DISTANCE, nv->window_x);
@@ -617,10 +751,12 @@ void sp_namedview_window_from_document(SPDesktop *desktop)
             x = MIN(gdk_screen_width() - w, x);
             y = MIN(gdk_screen_height() - h, y);
         }
-        if (w>0 && h>0)
+        if (w>0 && h>0) {
             desktop->setWindowSize(w, h);
-        if (x>0 && y>0)
+        }
+        if (x>0 && y>0) {
             desktop->setWindowPosition(NR::Point(x, y));
+        }
     }
 
     // restore zoom and view
@@ -670,7 +806,8 @@ void sp_namedview_update_layers_from_document (SPDesktop *desktop)
 
 void sp_namedview_document_from_window(SPDesktop *desktop)
 {
-    gint save_geometry = prefs_get_int_attribute("options.savewindowgeometry", "value", 0);
+    gint save_geometry_in_file =
+        (1==prefs_get_int_attribute("options.savewindowgeometry", "value", 0));
     Inkscape::XML::Node *view = SP_OBJECT_REPR(desktop->namedview);
     NR::Rect const r = desktop->get_display_area();
 
@@ -682,7 +819,7 @@ void sp_namedview_document_from_window(SPDesktop *desktop)
     sp_repr_set_svg_double(view, "inkscape:cx", r.midpoint()[NR::X]);
     sp_repr_set_svg_double(view, "inkscape:cy", r.midpoint()[NR::Y]);
 
-    if (save_geometry) {
+    if (save_geometry_in_file) {
         gint w, h, x, y;
         desktop->getWindowGeometry(x, y, w, h);
         sp_repr_set_int(view, "inkscape:window-width", w);
@@ -707,13 +844,6 @@ void SPNamedView::hide(SPDesktop const *desktop)
     }
 
     views = g_slist_remove(views, desktop);
-
-    // delete grids:
-    while ( grids ) {
-        Inkscape::CanvasGrid *gr = (Inkscape::CanvasGrid *)grids->data;
-        delete gr;
-        grids = g_slist_remove_link(grids, grids);
-    }
 }
 
 void SPNamedView::activateGuides(gpointer desktop, gboolean active)
@@ -755,11 +885,29 @@ void sp_namedview_toggle_guides(SPDocument *doc, Inkscape::XML::Node *repr)
 
     bool saved = sp_document_get_undo_sensitive(doc);
     sp_document_set_undo_sensitive(doc, false);
-
     sp_repr_set_boolean(repr, "showguides", v);
+    sp_document_set_undo_sensitive(doc, saved);
+
+    doc->setModifiedSinceSave();
+}
+
+void sp_namedview_show_grids(SPNamedView * namedview, bool show, bool dirty_document)
+{
+    namedview->grids_visible = show;
+
+    SPDocument *doc = SP_OBJECT_DOCUMENT (namedview);
+    Inkscape::XML::Node *repr = SP_OBJECT_REPR(namedview);
 
-    doc->rroot->setAttribute("sodipodi:modified", "true");
+    bool saved = sp_document_get_undo_sensitive(doc);
+    sp_document_set_undo_sensitive(doc, false);
+    sp_repr_set_boolean(repr, "showgrid", namedview->grids_visible);
     sp_document_set_undo_sensitive(doc, saved);
+
+    /* we don't want the document to get dirty on startup; that's when
+       we call this function with dirty_document = false */
+    if (dirty_document) {
+        doc->setModifiedSinceSave();
+    }
 }
 
 gchar const *SPNamedView::getName() const
@@ -895,6 +1043,21 @@ SPMetric SPNamedView::getDefaultMetric() const
     }
 }
 
+/**
+ * Returns the first grid it could find that isEnabled(). Returns NULL, if none is enabled
+ */
+Inkscape::CanvasGrid * sp_namedview_get_first_enabled_grid(SPNamedView *namedview)
+{
+    for (GSList const * l = namedview->grids; l != NULL; l = l->next) {
+        Inkscape::CanvasGrid * grid = (Inkscape::CanvasGrid*) l->data;
+        if (grid->isEnabled())
+            return grid;
+    }
+
+    return NULL;
+}
+
+
 /*
   Local Variables:
   mode:c++
@@ -904,4 +1067,4 @@ SPMetric SPNamedView::getDefaultMetric() const
   fill-column:99
   End:
 */
-// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtab
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :