Code

Changed preference to use file chooser button
[inkscape.git] / src / sp-namedview.cpp
index 26c53c8e0d6b02cd5dd9dd9bfb75570d799539ee..fa41060c2cd26267f2616936a38a20415ccef00d 100644 (file)
@@ -55,9 +55,6 @@ static Inkscape::XML::Node *sp_namedview_write(SPObject *object, Inkscape::XML::
 
 static void sp_namedview_setup_guides(SPNamedView * nv);
 
-static void sp_namedview_setup_grid(SPNamedView * nv);
-static void sp_namedview_setup_grid_item(SPNamedView * nv, SPCanvasItem * item);
-
 static gboolean sp_str_to_bool(const gchar *str);
 static gboolean sp_nv_read_length(const gchar *str, guint base, gdouble *val, const SPUnit **unit);
 static gboolean sp_nv_read_opacity(const gchar *str, guint32 *color);
@@ -107,7 +104,6 @@ static void sp_namedview_class_init(SPNamedViewClass * klass)
 static void sp_namedview_init(SPNamedView *nv)
 {
     nv->editable = TRUE;
-    nv->showgrid = FALSE;
     nv->showguides = TRUE;
     nv->showborder = TRUE;
     nv->showpageshadow = TRUE;
@@ -134,20 +130,10 @@ 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, "showgrid");
     sp_object_read_attr(object, "showguides");
     sp_object_read_attr(object, "gridtolerance");
     sp_object_read_attr(object, "guidetolerance");
     sp_object_read_attr(object, "objecttolerance");
-    sp_object_read_attr(object, "gridoriginx");
-    sp_object_read_attr(object, "gridoriginy");
-    sp_object_read_attr(object, "gridspacingx");
-    sp_object_read_attr(object, "gridspacingy");
-    sp_object_read_attr(object, "gridempspacing");
-    sp_object_read_attr(object, "gridcolor");
-    sp_object_read_attr(object, "gridempcolor");
-    sp_object_read_attr(object, "gridopacity");
-    sp_object_read_attr(object, "gridempopacity");
     sp_object_read_attr(object, "guidecolor");
     sp_object_read_attr(object, "guideopacity");
     sp_object_read_attr(object, "guidehicolor");
@@ -167,12 +153,10 @@ 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-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:object-paths");
     sp_object_read_attr(object, "inkscape:object-nodes");
     sp_object_read_attr(object, "inkscape:current-layer");
@@ -198,11 +182,6 @@ static void sp_namedview_release(SPObject *object)
         namedview->guides = NULL;
     }
 
-    while (namedview->gridviews) {
-        gtk_object_unref(GTK_OBJECT(namedview->gridviews->data));
-        namedview->gridviews = g_slist_remove(namedview->gridviews, namedview->gridviews->data);
-    }
-
     // delete grids:
     while ( namedview->grids ) {
         Inkscape::CanvasGrid *gr = (Inkscape::CanvasGrid *)namedview->grids->data;
@@ -227,13 +206,6 @@ static void sp_namedview_set(SPObject *object, unsigned int key, const gchar *va
             nv->editable = (!value);
             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
             break;
-       case SP_ATTR_SHOWGRID:
-            nv->showgrid = sp_str_to_bool(value);
-            sp_namedview_setup_grid(nv);
-            /* Disable grid snaps if the grid is turned off */
-            nv->snap_manager.grid.setEnabled(nv->showgrid);
-            object->requestModified(SP_OBJECT_MODIFIED_FLAG);
-            break;
        case SP_ATTR_SHOWGUIDES:
             if (!value) { // show guides if not specified, for backwards compatibility
                 nv->showguides = TRUE;
@@ -259,7 +231,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) {
@@ -267,69 +239,6 @@ static void sp_namedview_set(SPObject *object, unsigned int key, const gchar *va
             }
             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
             break;
-       case SP_ATTR_GRIDORIGINX:
-       case SP_ATTR_GRIDORIGINY:
-       {
-            unsigned const d = (key == SP_ATTR_GRIDORIGINY);
-            nv->gridunit = nv->doc_units;
-            nv->gridorigin[d] = 0.0;
-            if (value) {
-                sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &nv->gridorigin[d], &nv->gridunit);
-            }
-            nv->gridorigin[d] = sp_units_get_pixels(nv->gridorigin[d], *(nv->gridunit));
-            sp_namedview_setup_grid(nv);
-            object->requestModified(SP_OBJECT_MODIFIED_FLAG);
-            break;
-       }
-       case SP_ATTR_GRIDSPACINGX:
-       case SP_ATTR_GRIDSPACINGY:
-       {
-            unsigned const d = (key == SP_ATTR_GRIDSPACINGY);
-            nv->gridunit = nv->doc_units;
-            nv->gridspacing[d] = 1.0;
-            if (value) {
-                sp_nv_read_length(value, SP_UNIT_ABSOLUTE | SP_UNIT_DEVICE, &nv->gridspacing[d], &nv->gridunit);
-            }
-            nv->gridspacing[d] = sp_units_get_pixels(nv->gridspacing[d], *(nv->gridunit));
-            sp_namedview_setup_grid(nv);
-            object->requestModified(SP_OBJECT_MODIFIED_FLAG);
-            break;
-       }
-    case SP_ATTR_GRIDCOLOR:
-            nv->gridcolor = (nv->gridcolor & 0xff) | (DEFAULTGRIDCOLOR & 0xffffff00);
-            if (value) {
-                nv->gridcolor = (nv->gridcolor & 0xff) | sp_svg_read_color(value, nv->gridcolor);
-            }
-            sp_namedview_setup_grid(nv);
-            object->requestModified(SP_OBJECT_MODIFIED_FLAG);
-            break;
-       case SP_ATTR_GRIDEMPCOLOR:
-            nv->gridempcolor = (nv->gridempcolor & 0xff) | (DEFAULTGRIDEMPCOLOR & 0xffffff00);
-            if (value) {
-                nv->gridempcolor = (nv->gridempcolor & 0xff) | sp_svg_read_color(value, nv->gridempcolor);
-            }
-            sp_namedview_setup_grid(nv);
-            object->requestModified(SP_OBJECT_MODIFIED_FLAG);
-            break;
-       case SP_ATTR_GRIDOPACITY:
-            nv->gridcolor = (nv->gridcolor & 0xffffff00) | (DEFAULTGRIDCOLOR & 0xff);
-            sp_nv_read_opacity(value, &nv->gridcolor);
-            sp_namedview_setup_grid(nv);
-            object->requestModified(SP_OBJECT_MODIFIED_FLAG);
-            break;
-       case SP_ATTR_GRIDEMPOPACITY:
-            nv->gridempcolor = (nv->gridempcolor & 0xffffff00) | (DEFAULTGRIDEMPCOLOR & 0xff);
-            sp_nv_read_opacity(value, &nv->gridempcolor);
-            sp_namedview_setup_grid(nv);
-            object->requestModified(SP_OBJECT_MODIFIED_FLAG);
-            break;
-       case SP_ATTR_GRIDEMPSPACING:
-            nv->gridempspacing = DEFAULTGRIDEMPSPACING;
-            if (value != NULL)
-                nv->gridempspacing = atoi(value);
-            sp_namedview_setup_grid(nv);
-            object->requestModified(SP_OBJECT_MODIFIED_FLAG);
-            break;
        case SP_ATTR_GUIDECOLOR:
             nv->guidecolor = (nv->guidecolor & 0xff) | (DEFAULTGUIDECOLOR & 0xffffff00);
             if (value) {
@@ -403,7 +312,7 @@ static void sp_namedview_set(SPObject *object, unsigned int key, const gchar *va
             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;
@@ -435,36 +344,28 @@ 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_GRID_BBOX:
-            nv->snap_manager.grid.setSnapTo(Inkscape::Snapper::BBOX_POINT, 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);
-            object->requestModified(SP_OBJECT_MODIFIED_FLAG);
-            break;
-        case SP_ATTR_INKSCAPE_OBJECT_PATHS:
-            nv->snap_manager.object.setSnapToPaths(value ? sp_str_to_bool(value) : TRUE);
+            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.setSnapToNodes(value ? sp_str_to_bool(value) : TRUE);
+    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_CURRENT_LAYER:
@@ -525,6 +426,53 @@ 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* 
+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
+    for (GSList *l = nv->grids; l != NULL; l = l->next) {
+        Inkscape::CanvasGrid* g = (Inkscape::CanvasGrid*) l->data;
+        if (repr == g->repr) {
+            grid = g;
+            break;
+        }
+    }
+    
+    if (!grid) {
+        //create grid object
+        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));
+        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) {
+            SPDesktop *dt = static_cast<SPDesktop*>(l->data);
+            grid->createCanvasItem(dt);
+        }
+    } else {
+        //add canvasitem only for specified desktop
+        grid->createCanvasItem(desktop);
+    }
+
+    return grid;
+}
+
 static void sp_namedview_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
 {
     SPNamedView *nv = (SPNamedView *) object;
@@ -535,18 +483,7 @@ static void sp_namedview_child_added(SPObject *object, Inkscape::XML::Node *chil
 
     const gchar *id = child->attribute("id");
     if (!strcmp(child->name(), "inkscape:grid")) {
-        // check for which grid-type
-        Inkscape::CanvasGrid* addedgrid;
-        const char * gridtype = child->attribute("type");
-        if (!gridtype) {
-            gridtype = "xygrid"; // use this as default gridtype when none is specified
-            child->setAttribute("type", gridtype);
-        }
-        addedgrid = Inkscape::CanvasGrid::NewGrid( (SPDesktop*) nv->views->data, child, gridtype);
-        if (addedgrid) {
-            nv->grids = g_slist_append(nv->grids, addedgrid);
-            addedgrid->show();
-        }
+        sp_namedview_add_grid(nv, child, NULL);
     } else {
         SPObject *no = object->document->getObjectById(id);
         g_assert(SP_IS_OBJECT(no));
@@ -579,7 +516,6 @@ static void sp_namedview_child_added(SPObject *object, Inkscape::XML::Node *chil
 
 static void sp_namedview_remove_child(SPObject *object, Inkscape::XML::Node *child)
 {
-g_message("named view:: child removed");
     SPNamedView *nv = (SPNamedView *) object;
 
     if (!strcmp(child->name(), "inkscape:grid")) {
@@ -645,42 +581,30 @@ void SPNamedView::show(SPDesktop *desktop)
 
     views = g_slist_prepend(views, desktop);
 
-    SPCanvasItem * item = sp_canvas_item_new(sp_desktop_grid(desktop), INKSCAPE_TYPE_CXYGRID, NULL);
-    // since we're keeping a copy, we need to bump up the ref count
-    gtk_object_ref(GTK_OBJECT(item));
-    gridviews = g_slist_prepend(gridviews, item);
-
     // generate grids specified in SVG:
     Inkscape::XML::Node *repr = SP_OBJECT_REPR(this);
     if (repr) {
         for (Inkscape::XML::Node * child = repr->firstChild() ; child != NULL; child = child->next() ) {
             if (!strcmp(child->name(), "inkscape:grid")) {
-                Inkscape::CanvasXYGrid* addedgrid = new Inkscape::CanvasXYGrid(desktop, child);
-                if (addedgrid) {
-                    grids = g_slist_append(grids, addedgrid);
-                    addedgrid->enable_snapping();
-                    addedgrid->show();
-                }
+                sp_namedview_add_grid(this, child, desktop);
             }
         }
     }
-
-
-    sp_namedview_setup_grid(this);
 }
 
 #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);
@@ -689,10 +613,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
@@ -742,7 +668,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();
 
@@ -754,7 +681,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);
@@ -780,20 +707,6 @@ void SPNamedView::hide(SPDesktop const *desktop)
 
     views = g_slist_remove(views, desktop);
 
-    GSList *l;
-    for (l = gridviews; l != NULL; l = l->next) {
-        if (! l->data) {
-            continue;
-        }
-        SPCanvasItem *item = static_cast<SPCanvasItem*>(l->data);
-
-        if (item->canvas == sp_desktop_canvas(desktop)) {
-            sp_canvas_item_hide(SP_CANVAS_ITEM(l->data));
-            gtk_object_unref(GTK_OBJECT(l->data));
-            gridviews = g_slist_remove(gridviews, l->data);
-        }
-    }
-
     // delete grids:
     while ( grids ) {
         Inkscape::CanvasGrid *gr = (Inkscape::CanvasGrid *)grids->data;
@@ -848,48 +761,6 @@ void sp_namedview_toggle_guides(SPDocument *doc, Inkscape::XML::Node *repr)
     sp_document_set_undo_sensitive(doc, saved);
 }
 
-void sp_namedview_toggle_grid(SPDocument *doc, Inkscape::XML::Node *repr)
-{
-    unsigned int v;
-    sp_repr_get_boolean(repr, "showgrid", &v);
-    v = !v;
-
-    bool saved = sp_document_get_undo_sensitive(doc);
-    sp_document_set_undo_sensitive(doc, false);
-
-    sp_repr_set_boolean(repr, "showgrid", v);
-
-    doc->rroot->setAttribute("sodipodi:modified", "true");
-    sp_document_set_undo_sensitive(doc, saved);
-}
-
-static void sp_namedview_setup_grid(SPNamedView *nv)
-{
-    for (GSList *l = nv->gridviews; l != NULL; l = l->next) {
-        sp_namedview_setup_grid_item(nv, SP_CANVAS_ITEM(l->data));
-    }
-}
-
-static void sp_namedview_setup_grid_item(SPNamedView *nv, SPCanvasItem *item)
-{
-    if ( nv->showgrid ) {
-        sp_canvas_item_show(item);
-    } else {
-        sp_canvas_item_hide(item);
-    }
-
-    sp_canvas_item_set((GtkObject *) item,
-                       "color", nv->gridcolor,
-                       "originx", nv->gridorigin[NR::X],
-                       "originy", nv->gridorigin[NR::Y],
-                       "spacingx", nv->gridspacing[NR::X],
-                       "spacingy", nv->gridspacing[NR::Y],
-                       "empcolor", nv->gridempcolor,
-                       "empspacing", nv->gridempspacing,
-                       NULL);
-}
-
-
 gchar const *SPNamedView::getName() const
 {
     SPException ex;
@@ -1023,6 +894,7 @@ SPMetric SPNamedView::getDefaultMetric() const
     }
 }
 
+
 /*
   Local Variables:
   mode:c++
@@ -1032,4 +904,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 :