Code

update 2geom
[inkscape.git] / src / sp-namedview.cpp
index a26d1ce0e0fbb47e912f51ed87fc51c1576b9a32..54e84927a768c73995bdbc3529042aa6e91d6314 100644 (file)
@@ -8,13 +8,15 @@
  *   bulia byak <buliabyak@users.sf.net>
  *
  * Copyright (C) 2006      Johan Engelen <johan@shouraizou.nl>
- * Copyright (C) 1999-2005 Authors
+ * Copyright (C) 1999-2008 Authors
  * Copyright (C) 2000-2001 Ximian, Inc.
  *
  * Released under GNU GPL, read the file 'COPYING' for more information
  */
 
 #include "config.h"
+#include <cstring>
+#include <string>
 
 #include "display/canvas-grid.h"
 #include "helper/units.h"
@@ -51,7 +53,7 @@ static void sp_namedview_release(SPObject *object);
 static void sp_namedview_set(SPObject *object, unsigned int key, const gchar *value);
 static void sp_namedview_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref);
 static void sp_namedview_remove_child(SPObject *object, Inkscape::XML::Node *child);
-static Inkscape::XML::Node *sp_namedview_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
+static Inkscape::XML::Node *sp_namedview_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
 
 static void sp_namedview_setup_guides(SPNamedView * nv);
 
@@ -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,12 +107,14 @@ static void sp_namedview_init(SPNamedView *nv)
 {
     nv->editable = TRUE;
     nv->showguides = TRUE;
+    nv->grids_visible = false;
     nv->showborder = TRUE;
     nv->showpageshadow = TRUE;
 
     nv->guides = NULL;
     nv->viewcount = 0;
     nv->grids = NULL;
+    nv->snapindicator = false;
 
     nv->default_layer_id = 0;
 
@@ -119,6 +123,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;
@@ -154,21 +246,23 @@ 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:snap-global");
+    sp_object_read_attr(object, "inkscape:snap-indicator");
     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-snap-intersection-line-segments");
+    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: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);
@@ -176,6 +270,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)
@@ -189,9 +286,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) {
@@ -221,7 +318,7 @@ static void sp_namedview_set(SPObject *object, unsigned int key, const gchar *va
             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
             break;
     case SP_ATTR_SHOWGRIDS:
-            if (!value) { // show grids if not specified, for backwards compatibility
+            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);
@@ -304,12 +401,12 @@ static void sp_namedview_set(SPObject *object, unsigned int key, const gchar *va
             }
             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);
@@ -357,8 +454,16 @@ static void sp_namedview_set(SPObject *object, unsigned int key, const gchar *va
             nv->window_y = value ? atoi(value) : -1; // -1 means not set
             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
             break;
+    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_SNAP_INDICATOR:
+            nv->snapindicator = (value) ? sp_str_to_bool (value) : TRUE;
+            object->requestModified(SP_OBJECT_MODIFIED_FLAG);
+            break;
     case SP_ATTR_INKSCAPE_SNAP_BBOX:
-               nv->snap_manager.setSnapModeBBox(value ? sp_str_to_bool(value) : FALSE);
+            nv->snap_manager.setSnapModeBBox(value ? sp_str_to_bool(value) : FALSE);
             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
             break;
     case SP_ATTR_INKSCAPE_SNAP_NODES:
@@ -374,7 +479,7 @@ static void sp_namedview_set(SPObject *object, unsigned int key, const gchar *va
             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
             break;
     case SP_ATTR_INKSCAPE_SNAP_INTERS_GRIDGUIDE:
-            nv->snap_manager.setSnapIntersectionGG(value ? sp_str_to_bool(value) : FALSE);
+            nv->snap_manager.setSnapIntersectionGG(value ? sp_str_to_bool(value) : TRUE);
             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
             break;
     case SP_ATTR_INKSCAPE_SNAP_INTERS_LINESEGM:
@@ -394,9 +499,13 @@ static void sp_namedview_set(SPObject *object, unsigned int key, const gchar *va
             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
             break;
     case SP_ATTR_INKSCAPE_BBOX_NODES:
-            nv->snap_manager.object.setSnapToBBoxNode(value ? sp_str_to_bool(value) : FALSE);            
+            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);
@@ -479,6 +588,10 @@ sp_namedview_add_grid(SPNamedView *nv, Inkscape::XML::Node *repr, SPDesktop *des
             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, doc, gridtype);
         nv->grids = g_slist_append(nv->grids, grid);
         //Initialize the snapping parameters for the new grid
@@ -510,11 +623,10 @@ 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 if (id) {
-        SPObject *no = object->document->getObjectById(id);
+    } else {
+        SPObject *no = object->document->getObjectByRepr(child);
         if ( !SP_IS_OBJECT(no) )
             return;
 
@@ -575,7 +687,7 @@ static void sp_namedview_remove_child(SPObject *object, Inkscape::XML::Node *chi
     }
 }
 
-static Inkscape::XML::Node *sp_namedview_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
+static Inkscape::XML::Node *sp_namedview_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags)
 {
     if ( ( flags & SP_OBJECT_WRITE_EXT ) &&
          repr != SP_OBJECT_REPR(object) )
@@ -583,8 +695,7 @@ static Inkscape::XML::Node *sp_namedview_write(SPObject *object, Inkscape::XML::
         if (repr) {
             repr->mergeFrom(SP_OBJECT_REPR(object), "id");
         } else {
-             /// \todo FIXME:  Plumb an appropriate XML::Document into this
-             repr = SP_OBJECT_REPR(object)->duplicate(NULL);
+             repr = SP_OBJECT_REPR(object)->duplicate(doc);
         }
     }
 
@@ -654,9 +765,9 @@ void sp_namedview_window_from_document(SPDesktop *desktop)
     }
 
     // restore zoom and view
-    if (nv->zoom != 0 && nv->zoom != HUGE_VAL && !isNaN(nv->zoom)
-        && nv->cx != HUGE_VAL && !isNaN(nv->cx)
-        && nv->cy != HUGE_VAL && !isNaN(nv->cy)) {
+    if (nv->zoom != 0 && nv->zoom != HUGE_VAL && !IS_NAN(nv->zoom)
+        && nv->cx != HUGE_VAL && !IS_NAN(nv->cx)
+        && nv->cy != HUGE_VAL && !IS_NAN(nv->cy)) {
         desktop->zoom_absolute(nv->cx, nv->cy, nv->zoom);
     } else if (sp_desktop_document(desktop)) { // document without saved zoom, zoom to its page
         desktop->zoom_page();
@@ -738,13 +849,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)
@@ -786,11 +890,10 @@ 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);
-
-    doc->rroot->setAttribute("sodipodi:modified", "true");
     sp_document_set_undo_sensitive(doc, saved);
+
+    doc->setModifiedSinceSave();
 }
 
 void sp_namedview_show_grids(SPNamedView * namedview, bool show, bool dirty_document)
@@ -802,15 +905,14 @@ void sp_namedview_show_grids(SPNamedView * namedview, bool show, bool dirty_docu
 
     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->rroot->setAttribute("sodipodi:modified", "true");
+        doc->setModifiedSinceSave();
     }
-    sp_document_set_undo_sensitive(doc, saved);
 }
 
 gchar const *SPNamedView::getName() const
@@ -946,6 +1048,37 @@ 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;
+}
+
+void SPNamedView::translateGuides(NR::translate const &tr) {
+    for (GSList *l = guides; l != NULL; l = l->next) {
+        SPGuide &guide = *SP_GUIDE(l->data);
+        Geom::Point point_on_line = guide.point_on_line;
+        point_on_line[0] += tr[0];
+        point_on_line[1] += tr[1];
+        sp_guide_moveto(guide, point_on_line, true);
+    }
+}
+
+void SPNamedView::scrollAllDesktops(double dx, double dy, bool is_scrolling) {
+        for(GSList *l = views; l; l = l->next) {
+            SPDesktop *desktop = static_cast<SPDesktop *>(l->data);
+            desktop->scroll_world_in_svg_coords(dx, dy, is_scrolling);
+        }
+}
+
 
 /*
   Local Variables: