Code

NR::Maybe => boost::optional
[inkscape.git] / src / selection-chemistry.cpp
index 0f89464411e395c9a6235bdd24fd5d1b80b54418..114ed9c645a67470811fe9d9502201820303eab0 100644 (file)
@@ -27,7 +27,7 @@
 #include "svg/svg.h"
 #include "inkscape.h"
 #include "desktop.h"
-//#include "desktop-style.h"
+#include "desktop-style.h"
 #include "selection.h"
 #include "tools-switch.h"
 #include "desktop-handles.h"
@@ -130,7 +130,7 @@ void sp_selection_copy_impl (GSList const *items, GSList **clip, Inkscape::XML::
 
     // Copy item reprs:
     for (GSList *i = (GSList *) sorted_items; i != NULL; i = i->next) {
-        sp_selection_copy_one (SP_OBJECT_REPR (i->data), sp_item_i2doc_affine(SP_ITEM (i->data)), clip, xml_doc);
+        sp_selection_copy_one (SP_OBJECT_REPR (i->data), from_2geom(sp_item_i2doc_affine(SP_ITEM (i->data))), clip, xml_doc);
     }
 
     *clip = g_slist_reverse(*clip);
@@ -148,7 +148,7 @@ GSList *sp_selection_paste_impl (SPDocument *doc, SPObject *parent, GSList **cli
         Inkscape::XML::Node *copy = repr->duplicate(xml_doc);
 
         // premultiply the item transform by the accumulated parent transform in the paste layer
-        NR::Matrix local = sp_item_i2doc_affine(SP_ITEM(parent));
+        NR::Matrix local = from_2geom(sp_item_i2doc_affine(SP_ITEM(parent)));
         if (!local.test_identity()) {
             gchar const *t_str = copy->attribute("transform");
             NR::Matrix item_t (NR::identity());
@@ -221,7 +221,7 @@ void sp_selection_delete()
 }
 
 /* fixme: sequencing */
-void sp_selection_duplicate()
+void sp_selection_duplicate(bool suppressDone)
 {
     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
     if (desktop == NULL)
@@ -256,8 +256,10 @@ void sp_selection_duplicate()
         Inkscape::GC::release(copy);
     }
 
-    sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_DUPLICATE,
-                     _("Duplicate"));
+    if ( !suppressDone ) {
+        sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_DUPLICATE,
+                         _("Duplicate"));
+    }
 
     selection->setReprList(newsel);
 
@@ -442,7 +444,7 @@ void sp_selection_group()
                 NR::Matrix item_t (NR::identity());
                 if (t_str)
                     sp_svg_transform_read(t_str, &item_t);
-                item_t *= sp_item_i2doc_affine(SP_ITEM(doc->getObjectByRepr(current->parent())));
+                item_t *= from_2geom(sp_item_i2doc_affine(SP_ITEM(doc->getObjectByRepr(current->parent()))));
                 //FIXME: when moving both clone and original from a transformed group (either by
                 //grouping into another parent, or by cut/paste) the transform from the original's
                 //parent becomes embedded into original itself, and this affects its clones. Fix
@@ -565,12 +567,12 @@ sp_item_list_common_parent_group(GSList const *items)
 }
 
 /** Finds out the minimum common bbox of the selected items. */
-static NR::Maybe<NR::Rect>
+static boost::optional<NR::Rect>
 enclose_items(GSList const *items)
 {
     g_assert(items != NULL);
 
-    NR::Maybe<NR::Rect> r = NR::Nothing();
+    boost::optional<NR::Rect> r;
     for (GSList const *i = items; i; i = i->next) {
         r = NR::union_bounds(r, sp_item_bbox_desktop((SPItem *) i->data));
     }
@@ -619,7 +621,7 @@ sp_selection_raise()
     rev = g_slist_sort(rev, (GCompareFunc) sp_item_repr_compare_position);
 
     // Determine the common bbox of the selected items.
-    NR::Maybe<NR::Rect> selected = enclose_items(items);
+    boost::optional<NR::Rect> selected = enclose_items(items);
 
     // Iterate over all objects in the selection (starting from top).
     if (selected) {
@@ -629,7 +631,7 @@ sp_selection_raise()
             for (SPObject *newref = child->next; newref; newref = newref->next) {
                 // if the sibling is an item AND overlaps our selection,
                 if (SP_IS_ITEM(newref)) {
-                    NR::Maybe<NR::Rect> newref_bbox = sp_item_bbox_desktop(SP_ITEM(newref));
+                    boost::optional<NR::Rect> newref_bbox = sp_item_bbox_desktop(SP_ITEM(newref));
                     if ( newref_bbox && selected->intersects(*newref_bbox) ) {
                         // AND if it's not one of our selected objects,
                         if (!g_slist_find((GSList *) items, newref)) {
@@ -711,7 +713,7 @@ sp_selection_lower()
     Inkscape::XML::Node *grepr = SP_OBJECT_REPR(group);
 
     // Determine the common bbox of the selected items.
-    NR::Maybe<NR::Rect> selected = enclose_items(items);
+    boost::optional<NR::Rect> selected = enclose_items(items);
 
     /* Construct direct-ordered list of selected children. */
     GSList *rev = g_slist_copy((GSList *) items);
@@ -726,7 +728,7 @@ sp_selection_lower()
             for (SPObject *newref = prev_sibling(child); newref; newref = prev_sibling(newref)) {
                 // if the sibling is an item AND overlaps our selection,
                 if (SP_IS_ITEM(newref)) {
-                    NR::Maybe<NR::Rect> ref_bbox = sp_item_bbox_desktop(SP_ITEM(newref));
+                    boost::optional<NR::Rect> ref_bbox = sp_item_bbox_desktop(SP_ITEM(newref));
                     if ( ref_bbox && selected->intersects(*ref_bbox) ) {
                         // AND if it's not one of our selected objects,
                         if (!g_slist_find((GSList *) items, newref)) {
@@ -851,7 +853,7 @@ take_style_from_item (SPItem *item)
     }
 
     // FIXME: also transform gradient/pattern fills, by forking? NO, this must be nondestructive
-    double ex = NR::expansion(sp_item_i2doc_affine(item));
+    double ex = NR::expansion(from_2geom(sp_item_i2doc_affine(item)));
     if (ex != 1.0) {
         css = sp_css_attr_scale (css, ex);
     }
@@ -893,7 +895,7 @@ void sp_selection_paste_livepatheffect()
 void sp_selection_remove_livepatheffect_impl(SPItem *item)
 {
     if ( item && SP_IS_LPE_ITEM(item) ) {
-        sp_lpe_item_remove_path_effect(SP_LPE_ITEM(item), false);
+        sp_lpe_item_remove_all_path_effects(SP_LPE_ITEM(item), false);
     }
 }
 
@@ -917,10 +919,33 @@ void sp_selection_remove_livepatheffect()
 
     }
 
-    sp_document_done(sp_desktop_document (desktop), SP_VERB_EDIT_PASTE_LIVEPATHEFFECT,
+    sp_document_done(sp_desktop_document (desktop), SP_VERB_EDIT_REMOVE_LIVEPATHEFFECT,
                      _("Remove live path effect"));
 }
 
+void sp_selection_remove_filter ()
+{
+    SPDesktop *desktop = SP_ACTIVE_DESKTOP;
+    if (desktop == NULL) return;
+
+    Inkscape::Selection *selection = sp_desktop_selection(desktop);
+
+    // check if something is selected
+    if (selection->isEmpty()) {
+        desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to remove filters from."));
+        return;
+    }
+
+    SPCSSAttr *css = sp_repr_css_attr_new();
+    sp_repr_css_unset_property(css, "filter");
+    sp_desktop_set_style(desktop, css);
+    sp_repr_css_attr_unref(css);
+
+    sp_document_done(sp_desktop_document (desktop), SP_VERB_EDIT_REMOVE_FILTER,
+                     _("Remove filter"));
+}
+
+
 void sp_selection_paste_size (bool apply_x, bool apply_y)
 {
     Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
@@ -937,7 +962,7 @@ void sp_selection_paste_size_separately (bool apply_x, bool apply_y)
                      _("Paste size separately"));
 }
 
-void sp_selection_to_next_layer ()
+void sp_selection_to_next_layer(bool suppressDone)
 {
     SPDesktop *dt = SP_ACTIVE_DESKTOP;
 
@@ -969,8 +994,10 @@ void sp_selection_to_next_layer ()
         g_slist_free (copied);
         if (temp_clip) g_slist_free (temp_clip);
         if (next) dt->setCurrentLayer(next);
-        sp_document_done(sp_desktop_document (dt), SP_VERB_LAYER_MOVE_TO_NEXT,
-                         _("Raise to next layer"));
+        if ( !suppressDone ) {
+            sp_document_done(sp_desktop_document (dt), SP_VERB_LAYER_MOVE_TO_NEXT,
+                             _("Raise to next layer"));
+        }
     } else {
         no_more = true;
     }
@@ -982,7 +1009,7 @@ void sp_selection_to_next_layer ()
     g_slist_free ((GSList *) items);
 }
 
-void sp_selection_to_prev_layer ()
+void sp_selection_to_prev_layer(bool suppressDone)
 {
     SPDesktop *dt = SP_ACTIVE_DESKTOP;
 
@@ -1014,8 +1041,10 @@ void sp_selection_to_prev_layer ()
         g_slist_free (copied);
         if (temp_clip) g_slist_free (temp_clip);
         if (next) dt->setCurrentLayer(next);
-        sp_document_done(sp_desktop_document (dt), SP_VERB_LAYER_MOVE_TO_PREV,
-                         _("Lower to previous layer"));
+        if ( !suppressDone ) {
+            sp_document_done(sp_desktop_document (dt), SP_VERB_LAYER_MOVE_TO_PREV,
+                             _("Lower to previous layer"));
+        }
     } else {
         no_more = true;
     }
@@ -1149,9 +1178,9 @@ void sp_selection_apply_affine(Inkscape::Selection *selection, NR::Matrix const
             sp_object_read_attr (SP_OBJECT (item), "transform");
 
             // calculate the matrix we need to apply to the clone to cancel its induced transform from its original
-            NR::Matrix parent_transform = sp_item_i2root_affine(SP_ITEM(SP_OBJECT_PARENT (item)));
-            NR::Matrix t = parent_transform * matrix_to_desktop (matrix_from_desktop (affine, item), item) * parent_transform.inverse();
-            NR::Matrix t_inv =parent_transform * matrix_to_desktop (matrix_from_desktop (affine.inverse(), item), item) * parent_transform.inverse();
+            NR::Matrix parent_transform = from_2geom(sp_item_i2root_affine(SP_ITEM(SP_OBJECT_PARENT (item))));
+            NR::Matrix t = parent_transform * from_2geom(matrix_to_desktop (matrix_from_desktop (to_2geom(affine), item), item)) * parent_transform.inverse();
+            NR::Matrix t_inv =parent_transform * from_2geom(matrix_to_desktop (matrix_from_desktop (to_2geom(affine.inverse()), item), item)) * parent_transform.inverse();
             NR::Matrix result = t_inv * item->transform * t;
 
             if ((prefs_parallel || prefs_unmoved) && affine.is_translation()) {
@@ -1179,7 +1208,7 @@ void sp_selection_apply_affine(Inkscape::Selection *selection, NR::Matrix const
 
         } else {
             if (set_i2d) {
-                sp_item_set_i2d_affine(item, sp_item_i2d_affine(item) * affine);
+                sp_item_set_i2d_affine(item, sp_item_i2d_affine(item) * to_2geom(affine));
             }
             sp_item_write_transform(item, SP_OBJECT_REPR(item), item->transform, NULL);
         }
@@ -1219,7 +1248,7 @@ sp_selection_scale_absolute(Inkscape::Selection *selection,
     if (selection->isEmpty())
         return;
 
-    NR::Maybe<NR::Rect> const bbox(selection->bounds());
+    boost::optional<NR::Rect> const bbox(selection->bounds());
     if ( !bbox || bbox->isEmpty() ) {
         return;
     }
@@ -1241,7 +1270,7 @@ void sp_selection_scale_relative(Inkscape::Selection *selection, NR::Point const
     if (selection->isEmpty())
         return;
 
-    NR::Maybe<NR::Rect> const bbox(selection->bounds());
+    boost::optional<NR::Rect> const bbox(selection->bounds());
 
     if ( !bbox || bbox->isEmpty() ) {
         return;
@@ -1350,7 +1379,7 @@ sp_selection_rotate(Inkscape::Selection *selection, gdouble const angle_degrees)
     if (selection->isEmpty())
         return;
 
-    NR::Maybe<NR::Point> center = selection->center();
+    boost::optional<NR::Point> center = selection->center();
     if (!center) {
         return;
     }
@@ -1374,8 +1403,8 @@ sp_selection_rotate_screen(Inkscape::Selection *selection, gdouble angle)
     if (selection->isEmpty())
         return;
 
-    NR::Maybe<NR::Rect> const bbox(selection->bounds());
-    NR::Maybe<NR::Point> center = selection->center();
+    boost::optional<NR::Rect> const bbox(selection->bounds());
+    boost::optional<NR::Point> center = selection->center();
 
     if ( !bbox || !center ) {
         return;
@@ -1403,7 +1432,7 @@ sp_selection_scale(Inkscape::Selection *selection, gdouble grow)
     if (selection->isEmpty())
         return;
 
-    NR::Maybe<NR::Rect> const bbox(selection->bounds());
+    boost::optional<NR::Rect> const bbox(selection->bounds());
     if (!bbox) {
         return;
     }
@@ -1440,7 +1469,7 @@ sp_selection_scale_times(Inkscape::Selection *selection, gdouble times)
     if (selection->isEmpty())
         return;
 
-    NR::Maybe<NR::Rect> sel_bbox = selection->bounds();
+    boost::optional<NR::Rect> sel_bbox = selection->bounds();
 
     if (!sel_bbox) {
         return;
@@ -1654,7 +1683,9 @@ void sp_selection_edit_clip_or_mask(SPDesktop * dt, bool clip)
                         }
 
                         ShapeEditor * shape_editor = SP_NODE_CONTEXT( dt->event_context )->shape_editor;
-                        shape_editor->set_item(SP_ITEM(child));
+                        // TODO: should we set the item for nodepath or knotholder or both? seems to work with both.
+                        shape_editor->set_item(SP_ITEM(child), SH_NODEPATH);
+                        shape_editor->set_item(SP_ITEM(child), SH_KNOTHOLDER);
                         Inkscape::NodePath::Path *np = shape_editor->get_nodepath();
                         if (np) {
                             // take colors from prefs (same as used in outline mode)
@@ -1764,7 +1795,7 @@ SPItem *next_item(SPDesktop *desktop, GSList *path, SPObject *root,
 void scroll_to_show_item(SPDesktop *desktop, SPItem *item)
 {
     NR::Rect dbox = desktop->get_display_area();
-    NR::Maybe<NR::Rect> sbox = sp_item_bbox_desktop(item);
+    boost::optional<NR::Rect> sbox = sp_item_bbox_desktop(item);
 
     if ( sbox && dbox.contains(*sbox) == false ) {
         NR::Point const s_dt = sbox->midpoint();
@@ -1835,6 +1866,55 @@ sp_selection_clone()
     g_slist_free(newsel);
 }
 
+void
+sp_selection_relink()
+{
+    SPDesktop *desktop = SP_ACTIVE_DESKTOP;
+    if (!desktop)
+        return;
+
+    Inkscape::Selection *selection = sp_desktop_selection(desktop);
+
+    if (selection->isEmpty()) {
+        desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>clones</b> to relink."));
+        return;
+    }
+
+    Inkscape::UI::ClipboardManager *cm = Inkscape::UI::ClipboardManager::get();
+    const gchar *newid = cm->getFirstObjectID();
+    if (!newid) {
+        desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Copy an <b>object</b> to clipboard to relink clones to."));
+        return;
+    }
+    gchar *newref = g_strdup_printf ("#%s", newid);
+
+    // Get a copy of current selection.
+    bool relinked = false;
+    for (GSList *items = (GSList *) selection->itemList();
+         items != NULL;
+         items = items->next)
+    {
+        SPItem *item = (SPItem *) items->data;
+
+        if (!SP_IS_USE(item))
+            continue;
+
+        SP_OBJECT_REPR(item)->setAttribute("xlink:href", newref);
+        SP_OBJECT(item)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
+        relinked = true;
+    }
+
+    g_free(newref);
+
+    if (!relinked) {
+        desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No clones to relink</b> in the selection."));
+    } else {
+        sp_document_done(sp_desktop_document(desktop), SP_VERB_EDIT_UNLINK_CLONE,
+                     _("Relink clone"));
+    }
+}
+
+
 void
 sp_selection_unlink()
 {
@@ -1845,7 +1925,7 @@ sp_selection_unlink()
     Inkscape::Selection *selection = sp_desktop_selection(desktop);
 
     if (selection->isEmpty()) {
-        desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select a <b>clone</b> to unlink."));
+        desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>clones</b> to unlink."));
         return;
     }
 
@@ -1974,8 +2054,8 @@ void sp_selection_to_marker(bool apply)
     }
 
     sp_document_ensure_up_to_date(doc);
-    NR::Maybe<NR::Rect> r = selection->bounds();
-    NR::Maybe<NR::Point> c = selection->center();
+    boost::optional<NR::Rect> r = selection->bounds();
+    boost::optional<NR::Point> c = selection->center();
     if ( !r || !c || r->isEmpty() ) {
         return;
     }
@@ -1992,7 +2072,7 @@ void sp_selection_to_marker(bool apply)
     // bottommost object, after sorting
     SPObject *parent = SP_OBJECT_PARENT (items->data);
 
-    NR::Matrix parent_transform = sp_item_i2root_affine(SP_ITEM(parent));
+    NR::Matrix parent_transform = from_2geom(sp_item_i2root_affine(SP_ITEM(parent)));
 
     // remember the position of the first item
     gint pos = SP_OBJECT_REPR (items->data)->position();
@@ -2097,7 +2177,7 @@ sp_selection_tile(bool apply)
     }
 
     sp_document_ensure_up_to_date(doc);
-    NR::Maybe<NR::Rect> r = selection->bounds();
+    boost::optional<NR::Rect> r = selection->bounds();
     if ( !r || r->isEmpty() ) {
         return;
     }
@@ -2114,7 +2194,7 @@ sp_selection_tile(bool apply)
     // bottommost object, after sorting
     SPObject *parent = SP_OBJECT_PARENT (items->data);
 
-    NR::Matrix parent_transform = sp_item_i2root_affine(SP_ITEM(parent));
+    NR::Matrix parent_transform = from_2geom(sp_item_i2root_affine(SP_ITEM(parent)));
 
     // remember the position of the first item
     gint pos = SP_OBJECT_REPR (items->data)->position();
@@ -2342,11 +2422,16 @@ sp_selection_create_bitmap_copy ()
         return;
     }
 
+    desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, _("Rendering bitmap..."));
+    // set "busy" cursor
+    desktop->setWaitingCursor();
+
     // Get the bounding box of the selection
     NRRect bbox;
     sp_document_ensure_up_to_date (document);
     selection->bounds(&bbox);
     if (NR_RECT_DFLS_TEST_EMPTY(&bbox)) {
+        desktop->clearWaitingCursor();
         return; // exceptional situation, so not bother with a translatable error message, just quit quietly
     }
 
@@ -2410,7 +2495,7 @@ sp_selection_create_bitmap_copy ()
     unsigned width = (unsigned) floor ((bbox.x1 - bbox.x0) * res / PX_PER_IN);
     unsigned height =(unsigned) floor ((bbox.y1 - bbox.y0) * res / PX_PER_IN);
 
-    // Find out if we have to run a filter
+    // Find out if we have to run an external filter
     gchar const *run = NULL;
     gchar const *filter = prefs_get_string_attribute ("options.createbitmap", "filter");
     if (filter) {
@@ -2435,7 +2520,7 @@ sp_selection_create_bitmap_copy ()
     }
 
     // Calculate the matrix that will be applied to the image so that it exactly overlaps the source objects
-    NR::Matrix eek = sp_item_i2d_affine (SP_ITEM(parent_object));
+    NR::Matrix eek = from_2geom(sp_item_i2d_affine (SP_ITEM(parent_object)));
     NR::Matrix t;
 
     double shift_x = bbox.x0;
@@ -2460,7 +2545,8 @@ sp_selection_create_bitmap_copy ()
     // Run filter, if any
     if (run) {
         g_print ("Running external filter: %s\n", run);
-        system (run);
+        int retval;
+        retval = system (run);
     }
 
     // Import the image back
@@ -2502,6 +2588,8 @@ sp_selection_create_bitmap_copy ()
                           _("Create bitmap"));
     }
 
+    desktop->clearWaitingCursor();
+
     g_free (filename);
     g_free (filepath);
 }
@@ -2663,7 +2751,9 @@ void sp_selection_unset_mask(bool apply_clip_path) {
     sp_document_ensure_up_to_date(doc);
 
     gchar const *attributeName = apply_clip_path ? "clip-path" : "mask";
-    std::map<SPObject*,SPItem*> referenced_objects;
+    std::map<SPObject*,SPItem*> referenced_objects; 
+    // SPObject* refers to a group containing the clipped path or mask itself, 
+    // whereas SPItem* refers to the item being clipped or masked
     for (GSList const *i = selection->itemList(); NULL != i; i = i->next) {
         if (remove_original) {
             // remember referenced mask/clippath, so orphaned masks can be moved back to document
@@ -2687,9 +2777,10 @@ void sp_selection_unset_mask(bool apply_clip_path) {
 
     // restore mask objects into a document
     for ( std::map<SPObject*,SPItem*>::iterator it = referenced_objects.begin() ; it != referenced_objects.end() ; ++it) {
-        SPObject *obj = (*it).first;
+        SPObject *obj = (*it).first; // Group containing the clipped paths or masks
         GSList *items_to_move = NULL;
         for (SPObject *child = sp_object_first_child(obj) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
+            // Collect all clipped paths and masks within a single group 
             Inkscape::XML::Node *copy = SP_OBJECT_REPR(child)->duplicate(xml_doc);
             items_to_move = g_slist_prepend (items_to_move, copy);
         }
@@ -2703,6 +2794,7 @@ void sp_selection_unset_mask(bool apply_clip_path) {
         Inkscape::XML::Node *parent = SP_OBJECT_REPR((*it).second)->parent();
         gint pos = SP_OBJECT_REPR((*it).second)->position();
 
+        // Iterate through all clipped paths / masks
         for (GSList *i = items_to_move; NULL != i; i = i->next) {
             Inkscape::XML::Node *repr = (Inkscape::XML::Node *)i->data;
 
@@ -2728,33 +2820,67 @@ void sp_selection_unset_mask(bool apply_clip_path) {
         sp_document_done (doc, SP_VERB_OBJECT_UNSET_MASK, _("Release mask"));
 }
 
-void fit_canvas_to_selection(SPDesktop *desktop) {
-    g_return_if_fail(desktop != NULL);
+/**
+ * Returns true if an undoable change should be recorded.
+ */
+bool
+fit_canvas_to_selection(SPDesktop *desktop)
+{
+    g_return_val_if_fail(desktop != NULL, false);
     SPDocument *doc = sp_desktop_document(desktop);
 
-    g_return_if_fail(doc != NULL);
-    g_return_if_fail(desktop->selection != NULL);
+    g_return_val_if_fail(doc != NULL, false);
+    g_return_val_if_fail(desktop->selection != NULL, false);
 
     if (desktop->selection->isEmpty()) {
         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>object(s)</b> to fit canvas to."));
-        return;
+        return false;
     }
-    NR::Maybe<NR::Rect> const bbox(desktop->selection->bounds());
+    boost::optional<NR::Rect> const bbox(desktop->selection->bounds());
     if (bbox && !bbox->isEmpty()) {
         doc->fitToRect(*bbox);
+        return true;
+    } else {
+        return false;
     }
-};
+}
 
-void fit_canvas_to_drawing(SPDocument *doc) {
-    g_return_if_fail(doc != NULL);
+/**
+ * Fit canvas to the bounding box of the selection, as an undoable action.
+ */
+void
+verb_fit_canvas_to_selection(SPDesktop *const desktop)
+{
+    if (fit_canvas_to_selection(desktop)) {
+        sp_document_done(sp_desktop_document(desktop), SP_VERB_FIT_CANVAS_TO_SELECTION,
+                         _("Fit Page to Selection"));
+    }
+}
+
+bool
+fit_canvas_to_drawing(SPDocument *doc)
+{
+    g_return_val_if_fail(doc != NULL, false);
 
     sp_document_ensure_up_to_date(doc);
     SPItem const *const root = SP_ITEM(doc->root);
-    NR::Maybe<NR::Rect> const bbox(root->getBounds(sp_item_i2r_affine(root)));
+    boost::optional<NR::Rect> const bbox(root->getBounds(from_2geom(sp_item_i2r_affine(root))));
     if (bbox && !bbox->isEmpty()) {
         doc->fitToRect(*bbox);
+        return true;
+    } else {
+        return false;
     }
-};
+}
+
+void
+verb_fit_canvas_to_drawing(SPDesktop *desktop)
+{
+    if (fit_canvas_to_drawing(sp_desktop_document(desktop))) {
+        sp_document_done(sp_desktop_document(desktop), SP_VERB_FIT_CANVAS_TO_DRAWING,
+                         _("Fit Page to Drawing"));
+    }
+}
 
 void fit_canvas_to_selection_or_drawing(SPDesktop *desktop) {
     g_return_if_fail(desktop != NULL);
@@ -2763,14 +2889,13 @@ void fit_canvas_to_selection_or_drawing(SPDesktop *desktop) {
     g_return_if_fail(doc != NULL);
     g_return_if_fail(desktop->selection != NULL);
 
-    if (desktop->selection->isEmpty()) {
-        fit_canvas_to_drawing(doc);
-    } else {
-        fit_canvas_to_selection(desktop);
+    bool const changed = ( desktop->selection->isEmpty()
+                           ? fit_canvas_to_drawing(doc)
+                           : fit_canvas_to_selection(desktop) );
+    if (changed) {
+        sp_document_done(sp_desktop_document(desktop), SP_VERB_FIT_CANVAS_TO_SELECTION_OR_DRAWING,
+                         _("Fit Page to Selection or Drawing"));
     }
-
-    sp_document_done(doc, SP_VERB_FIT_CANVAS_TO_DRAWING,
-                     _("Fit page to selection"));
 };
 
 static void itemtree_map(void (*f)(SPItem *, SPDesktop *), SPObject *root, SPDesktop *desktop) {