From 91112f9d55017b82caa02a3c4673a8420a361801 Mon Sep 17 00:00:00 2001 From: Thomas Holder Date: Fri, 14 Dec 2018 10:05:39 +0000 Subject: DocumentUndo::ScopedInsensitive --- src/document-undo.h | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) (limited to 'src/document-undo.h') diff --git a/src/document-undo.h b/src/document-undo.h index 1651bcfba..5ed9db8f6 100644 --- a/src/document-undo.h +++ b/src/document-undo.h @@ -14,11 +14,7 @@ namespace Glib { class ustring; } -typedef struct _GObject GObject; - -class SPDesktop; class SPDocument; -struct InkscapeApplication; namespace Inkscape { @@ -29,14 +25,7 @@ public: /** * Set undo sensitivity. * - * \note - * Since undo sensitivity needs to be nested, setting undo sensitivity - * should be done like this: - *\verbatim - bool saved = DocumentUndo::getUndoSensitive(document); - DocumentUndo::setUndoSensitive(document, false); - ... do stuff ... - DocumentUndo::setUndoSensitive(document, saved); \endverbatim + * Don't use this to temporarily turn sensitivity off, use ScopedInsensitive instead. */ static void setUndoSensitive(SPDocument *doc, bool sensitive); @@ -57,6 +46,30 @@ public: static gboolean undo(SPDocument *document); static gboolean redo(SPDocument *document); + + /** + * RAII-style mechanism for creating a temporary undo-insensitive context. + * + * \verbatim + { + DocumentUndo::ScopedInsensitive tmp(document); + ... do stuff ... + // "tmp" goes out of scope here and automatically restores undo-sensitivity + } \endverbatim + */ + class ScopedInsensitive { + SPDocument * m_doc; + bool m_saved; + + public: + ScopedInsensitive(SPDocument *doc) + : m_doc(doc) + { + m_saved = getUndoSensitive(doc); + setUndoSensitive(doc, false); + } + ~ScopedInsensitive() { setUndoSensitive(m_doc, m_saved); } + }; }; } // namespace Inkscape -- cgit v1.2.3