sc/inc/document.hxx                          |    6 +-
 sc/qa/extras/anchor.cxx                      |    1 
 sc/qa/unit/SparklineTest.cxx                 |    1 
 sc/qa/unit/copy_paste_test.cxx               |    1 
 sc/qa/unit/filters-test.cxx                  |    1 
 sc/qa/unit/subsequent_filters_test.cxx       |    1 
 sc/qa/unit/tiledrendering/tiledrendering.cxx |   70 +++++++++++++++++++++++++--
 sc/qa/unit/ucalc.cxx                         |    1 
 sc/qa/unit/ucalc_condformat.cxx              |    1 
 sc/qa/unit/ucalc_formula.cxx                 |    1 
 sc/qa/unit/ucalc_sharedformula.cxx           |    1 
 sc/qa/unit/ucalc_sort.cxx                    |    1 
 sc/source/core/data/document.cxx             |    5 +
 sc/source/ui/docshell/docsh.cxx              |    1 
 sc/source/ui/inc/undocell.hxx                |    2 
 sc/source/ui/inc/undomanager.hxx             |   45 +++++++++++++++++
 sc/source/ui/undo/undobase.cxx               |   66 +++++++++++++++++++++++++
 sc/source/ui/view/tabvwshb.cxx               |   26 ++++++++--
 18 files changed, 218 insertions(+), 13 deletions(-)

New commits:
commit 640a6488a32e8f682788feb6cab01acfffca7fa7
Author:     Noel Grandin <noelgran...@gmail.com>
AuthorDate: Thu Jul 28 19:06:59 2022 +0200
Commit:     Noel Grandin <noel.gran...@collabora.co.uk>
CommitDate: Sat Jul 30 09:02:07 2022 +0200

    sc: allow undo of typing in 2 views independent from each other
    
    This commit follows the same pattern as
        commit c72e500ccaf0ce2261c5233b80fba9342778f810
        sw: allow undo of typing in 2 views independent from each other
    
    with some changes since calc and writer have different undo/redo
    infrastructure on top of SfxUndoManager.
    
    Change-Id: Ib6e7e21caccb94752c01c529b5013553dba8b4f9
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/137579
    Tested-by: Jenkins
    Reviewed-by: Noel Grandin <noel.gran...@collabora.co.uk>

diff --git a/sc/inc/document.hxx b/sc/inc/document.hxx
index 959960e15e03..7866b5a3cf9c 100644
--- a/sc/inc/document.hxx
+++ b/sc/inc/document.hxx
@@ -184,7 +184,7 @@ class ScLookupCache;
 struct ScLookupCacheMap;
 class ScSortedRangeCache;
 struct ScSortedRangeCacheMap;
-class SfxUndoManager;
+class ScUndoManager;
 class ScFormulaParserPool;
 struct ScClipParam;
 class ScRowBreakIterator;
@@ -364,7 +364,7 @@ private:
 
     ScCalcConfig        maCalcConfig;
 
-    SfxUndoManager*     mpUndoManager;
+    ScUndoManager*     mpUndoManager;
     std::unique_ptr<ScFieldEditEngine>  mpEditEngine;                   // 
uses pEditPool from xPoolHelper
     std::unique_ptr<ScNoteEditEngine>   mpNoteEngine;                   // 
uses pEditPool from xPoolHelper
     SfxObjectShell*     mpShell;
@@ -2531,7 +2531,7 @@ public:
     void                              SetStorageGrammar( 
formula::FormulaGrammar::Grammar eGrammar );
     formula::FormulaGrammar::Grammar  GetStorageGrammar() const { return 
eStorageGrammar; }
 
-    SC_DLLPUBLIC SfxUndoManager* GetUndoManager();
+    SC_DLLPUBLIC ScUndoManager* GetUndoManager();
     bool                         IsInVBAMode() const;
     ScRowBreakIterator*          GetRowBreakIterator(SCTAB nTab) const;
 
diff --git a/sc/qa/extras/anchor.cxx b/sc/qa/extras/anchor.cxx
index 347da5ec950f..1e9aa498c67d 100644
--- a/sc/qa/extras/anchor.cxx
+++ b/sc/qa/extras/anchor.cxx
@@ -22,6 +22,7 @@
 #include <svx/svdocirc.hxx>
 #include <vcl/scheduler.hxx>
 #include <tabvwsh.hxx>
+#include <undomanager.hxx>
 
 #include <sc.hrc>
 
diff --git a/sc/qa/unit/SparklineTest.cxx b/sc/qa/unit/SparklineTest.cxx
index ee104ef56356..f22dfa84b76f 100644
--- a/sc/qa/unit/SparklineTest.cxx
+++ b/sc/qa/unit/SparklineTest.cxx
@@ -12,6 +12,7 @@
 #include <tabvwsh.hxx>
 #include <cliputil.hxx>
 #include <docfunc.hxx>
+#include <undomanager.hxx>
 
 #include <Sparkline.hxx>
 #include <SparklineGroup.hxx>
diff --git a/sc/qa/unit/copy_paste_test.cxx b/sc/qa/unit/copy_paste_test.cxx
index bcdce8eaa5f8..456622c3db9c 100644
--- a/sc/qa/unit/copy_paste_test.cxx
+++ b/sc/qa/unit/copy_paste_test.cxx
@@ -23,6 +23,7 @@
 #include <scitems.hxx>
 #include <attrib.hxx>
 #include <userlist.hxx>
+#include <undomanager.hxx>
 
 #include <com/sun/star/frame/Desktop.hpp>
 
diff --git a/sc/qa/unit/filters-test.cxx b/sc/qa/unit/filters-test.cxx
index 44aadbbba7b7..55e845417d07 100644
--- a/sc/qa/unit/filters-test.cxx
+++ b/sc/qa/unit/filters-test.cxx
@@ -28,6 +28,7 @@
 #include <sortparam.hxx>
 #include <scopetools.hxx>
 #include <scmod.hxx>
+#include <undomanager.hxx>
 
 #include <svx/svdocapt.hxx>
 #include <svx/svdpage.hxx>
diff --git a/sc/qa/unit/subsequent_filters_test.cxx 
b/sc/qa/unit/subsequent_filters_test.cxx
index 1dea49286b1a..a8adde706f0a 100644
--- a/sc/qa/unit/subsequent_filters_test.cxx
+++ b/sc/qa/unit/subsequent_filters_test.cxx
@@ -45,6 +45,7 @@
 #include <detfunc.hxx>
 #include <cellmergeoption.hxx>
 #include <sortparam.hxx>
+#include <undomanager.hxx>
 
 #include <orcusfilters.hxx>
 #include <filter.hxx>
diff --git a/sc/qa/unit/tiledrendering/tiledrendering.cxx 
b/sc/qa/unit/tiledrendering/tiledrendering.cxx
index 73288fd565bc..f8498fae1b95 100644
--- a/sc/qa/unit/tiledrendering/tiledrendering.cxx
+++ b/sc/qa/unit/tiledrendering/tiledrendering.cxx
@@ -52,6 +52,7 @@
 #include <docuno.hxx>
 #include <drwlayer.hxx>
 #include <editutil.hxx>
+#include <undomanager.hxx>
 
 using namespace css;
 
@@ -127,6 +128,7 @@ public:
     void testTextBoxInsert();
     void testCommentCellCopyPaste();
     void testInvalidEntrySave();
+    void testUndoReordering();
 
     CPPUNIT_TEST_SUITE(ScTiledRenderingTest);
     CPPUNIT_TEST(testRowColumnHeaders);
@@ -184,6 +186,7 @@ public:
     CPPUNIT_TEST(testTextBoxInsert);
     CPPUNIT_TEST(testCommentCellCopyPaste);
     CPPUNIT_TEST(testInvalidEntrySave);
+    CPPUNIT_TEST(testUndoReordering);
     CPPUNIT_TEST_SUITE_END();
 
 private:
@@ -936,7 +939,7 @@ void ScTiledRenderingTest::testUndoShells()
     auto pDocShell = dynamic_cast<ScDocShell*>(pModelObj->GetEmbeddedObject());
     CPPUNIT_ASSERT(pDocShell);
     ScDocument& rDoc = pDocShell->GetDocument();
-    SfxUndoManager* pUndoManager = rDoc.GetUndoManager();
+    ScUndoManager* pUndoManager = rDoc.GetUndoManager();
     CPPUNIT_ASSERT(pUndoManager);
     CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), 
pUndoManager->GetUndoActionCount());
     sal_Int32 nView1 = SfxLokHelper::getView();
@@ -1344,7 +1347,7 @@ void ScTiledRenderingTest::testUndoLimiting()
     CPPUNIT_ASSERT(pModelObj);
     ScDocument* pDoc = pModelObj->GetDocument();
     CPPUNIT_ASSERT(pDoc);
-    SfxUndoManager* pUndoManager = pDoc->GetUndoManager();
+    ScUndoManager* pUndoManager = pDoc->GetUndoManager();
     CPPUNIT_ASSERT(pUndoManager);
 
     // view #1
@@ -1406,7 +1409,7 @@ void ScTiledRenderingTest::testUndoRepairDispatch()
     CPPUNIT_ASSERT(pModelObj);
     ScDocument* pDoc = pModelObj->GetDocument();
     CPPUNIT_ASSERT(pDoc);
-    SfxUndoManager* pUndoManager = pDoc->GetUndoManager();
+    ScUndoManager* pUndoManager = pDoc->GetUndoManager();
     CPPUNIT_ASSERT(pUndoManager);
 
     // view #1
@@ -2983,6 +2986,67 @@ void ScTiledRenderingTest::testInvalidEntrySave()
     CPPUNIT_ASSERT_EQUAL(double(41463), pDoc->GetValue(ScAddress(0, 7, 0)));
 }
 
+void ScTiledRenderingTest::testUndoReordering()
+{
+    ScModelObj* pModelObj = createDoc("small.ods");
+    CPPUNIT_ASSERT(pModelObj);
+    ScDocument* pDoc = pModelObj->GetDocument();
+    CPPUNIT_ASSERT(pDoc);
+    ScUndoManager* pUndoManager = pDoc->GetUndoManager();
+    CPPUNIT_ASSERT(pUndoManager);
+
+    // view #1
+    int nView1 = SfxLokHelper::getView();
+    ViewCallback aView1;
+
+    // view #2
+    SfxLokHelper::createView();
+    int nView2 = SfxLokHelper::getView();
+    
pModelObj->initializeForTiledRendering(uno::Sequence<beans::PropertyValue>());
+    ViewCallback aView2;
+
+    // text edit a cell in view #1
+    SfxLokHelper::setView(nView1);
+    pModelObj->postKeyEvent(LOK_KEYEVENT_KEYINPUT, 'x', 0);
+    pModelObj->postKeyEvent(LOK_KEYEVENT_KEYUP, 'x', 0);
+    pModelObj->postKeyEvent(LOK_KEYEVENT_KEYINPUT, 0, awt::Key::RETURN);
+    pModelObj->postKeyEvent(LOK_KEYEVENT_KEYUP, 0, awt::Key::RETURN);
+    Scheduler::ProcessEventsToIdle();
+
+    // check that undo action count is not 0
+    CPPUNIT_ASSERT_EQUAL(std::size_t(1), pUndoManager->GetUndoActionCount());
+
+    // text edit a different cell in view #2
+    SfxLokHelper::setView(nView2);
+    pModelObj->postKeyEvent(LOK_KEYEVENT_KEYINPUT, 0, KEY_DOWN);
+    pModelObj->postKeyEvent(LOK_KEYEVENT_KEYUP, 0, KEY_DOWN);
+    Scheduler::ProcessEventsToIdle();
+    pModelObj->postKeyEvent(LOK_KEYEVENT_KEYINPUT, 0, KEY_DOWN);
+    pModelObj->postKeyEvent(LOK_KEYEVENT_KEYUP, 0, KEY_DOWN);
+    pModelObj->postKeyEvent(LOK_KEYEVENT_KEYINPUT, 'x', 0);
+    pModelObj->postKeyEvent(LOK_KEYEVENT_KEYUP, 'x', 0);
+    pModelObj->postKeyEvent(LOK_KEYEVENT_KEYINPUT, 0, awt::Key::RETURN);
+    pModelObj->postKeyEvent(LOK_KEYEVENT_KEYUP, 0, awt::Key::RETURN);
+    Scheduler::ProcessEventsToIdle();
+
+    // check that undo action count is not 1
+    CPPUNIT_ASSERT_EQUAL(std::size_t(2), pUndoManager->GetUndoActionCount());
+
+    // try to execute undo in view #1
+    SfxLokHelper::setView(nView1);
+    comphelper::dispatchCommand(".uno:Undo", {});
+    Scheduler::ProcessEventsToIdle();
+    // check that undo has been executed on view #1
+    CPPUNIT_ASSERT_EQUAL(std::size_t(1), pUndoManager->GetUndoActionCount());
+
+    // try to execute undo in view #2
+    SfxLokHelper::setView(nView2);
+    comphelper::dispatchCommand(".uno:Undo", {});
+    Scheduler::ProcessEventsToIdle();
+    // check that undo has been executed on view #2
+    CPPUNIT_ASSERT_EQUAL(std::size_t(0), pUndoManager->GetUndoActionCount());
+}
+
 }
 
 CPPUNIT_TEST_SUITE_REGISTRATION(ScTiledRenderingTest);
diff --git a/sc/qa/unit/ucalc.cxx b/sc/qa/unit/ucalc.cxx
index 8aa2fabbfebd..2a631c12d296 100644
--- a/sc/qa/unit/ucalc.cxx
+++ b/sc/qa/unit/ucalc.cxx
@@ -43,6 +43,7 @@
 
 #include <editable.hxx>
 #include <tabprotection.hxx>
+#include <undomanager.hxx>
 
 #include <formula/IFunctionDescription.hxx>
 
diff --git a/sc/qa/unit/ucalc_condformat.cxx b/sc/qa/unit/ucalc_condformat.cxx
index c783b8808c46..652ca8b059c6 100644
--- a/sc/qa/unit/ucalc_condformat.cxx
+++ b/sc/qa/unit/ucalc_condformat.cxx
@@ -20,6 +20,7 @@
 #include <attrib.hxx>
 #include <fillinfo.hxx>
 #include <compiler.hxx>
+#include <undomanager.hxx>
 
 #include <svl/sharedstringpool.hxx>
 
diff --git a/sc/qa/unit/ucalc_formula.cxx b/sc/qa/unit/ucalc_formula.cxx
index e1df10374d43..480c009b13dc 100644
--- a/sc/qa/unit/ucalc_formula.cxx
+++ b/sc/qa/unit/ucalc_formula.cxx
@@ -26,6 +26,7 @@
 #include <externalrefmgr.hxx>
 #include <scdll.hxx>
 #include <scmod.hxx>
+#include <undomanager.hxx>
 
 #include <formula/vectortoken.hxx>
 #include <svl/broadcast.hxx>
diff --git a/sc/qa/unit/ucalc_sharedformula.cxx 
b/sc/qa/unit/ucalc_sharedformula.cxx
index 69f3e3be3d42..e670af33f1fc 100644
--- a/sc/qa/unit/ucalc_sharedformula.cxx
+++ b/sc/qa/unit/ucalc_sharedformula.cxx
@@ -20,6 +20,7 @@
 #include <dbdata.hxx>
 #include <bcaslot.hxx>
 #include <scdll.hxx>
+#include <undomanager.hxx>
 
 #include <sfx2/docfile.hxx>
 #include <test/bootstrapfixture.hxx>
diff --git a/sc/qa/unit/ucalc_sort.cxx b/sc/qa/unit/ucalc_sort.cxx
index 27839c28e1d0..2af82ec07429 100644
--- a/sc/qa/unit/ucalc_sort.cxx
+++ b/sc/qa/unit/ucalc_sort.cxx
@@ -24,6 +24,7 @@
 #include <editutil.hxx>
 #include <drwlayer.hxx>
 #include <queryiter.hxx>
+#include <undomanager.hxx>
 
 #include <editeng/wghtitem.hxx>
 #include <editeng/postitem.hxx>
diff --git a/sc/source/core/data/document.cxx b/sc/source/core/data/document.cxx
index 9c6f040961f1..53c9223ad5bd 100644
--- a/sc/source/core/data/document.cxx
+++ b/sc/source/core/data/document.cxx
@@ -88,6 +88,7 @@
 #include <recursionhelper.hxx>
 #include <SparklineGroup.hxx>
 #include <SparklineList.hxx>
+#include <undomanager.hxx>
 
 #include <formula/vectortoken.hxx>
 
@@ -6482,14 +6483,14 @@ bool ScDocument::NeedPageResetAfterTab( SCTAB nTab ) 
const
     return false;       // otherwise not
 }
 
-SfxUndoManager* ScDocument::GetUndoManager()
+ScUndoManager* ScDocument::GetUndoManager()
 {
     if (!mpUndoManager)
     {
         // to support enhanced text edit for draw objects, use an 
SdrUndoManager
         ScMutationGuard aGuard(*this, ScMutationGuardFlags::CORE);
 
-        SdrUndoManager* pUndoManager = new SdrUndoManager;
+        ScUndoManager* pUndoManager = new ScUndoManager;
         pUndoManager->SetDocShell(GetDocumentShell());
         mpUndoManager = pUndoManager;
     }
diff --git a/sc/source/ui/docshell/docsh.cxx b/sc/source/ui/docshell/docsh.cxx
index 74b174479f72..3198b5a2325f 100644
--- a/sc/source/ui/docshell/docsh.cxx
+++ b/sc/source/ui/docshell/docsh.cxx
@@ -117,6 +117,7 @@
 #include <docparam.hxx>
 #include "docshimp.hxx"
 #include <sizedev.hxx>
+#include <undomanager.hxx>
 #include <refreshtimerprotector.hxx>
 
 #include <officecfg/Office/Calc.hxx>
diff --git a/sc/source/ui/inc/undocell.hxx b/sc/source/ui/inc/undocell.hxx
index b013e084fee5..f47aa6ca18bb 100644
--- a/sc/source/ui/inc/undocell.hxx
+++ b/sc/source/ui/inc/undocell.hxx
@@ -94,6 +94,8 @@ public:
 
     virtual OUString GetComment() const override;
 
+    const ScAddress& GetPositionAddress() const { return maPos; }
+
 private:
     ValuesType maOldValues;
 
diff --git a/sc/source/ui/inc/undomanager.hxx b/sc/source/ui/inc/undomanager.hxx
new file mode 100644
index 000000000000..4f3ddc282fb4
--- /dev/null
+++ b/sc/source/ui/inc/undomanager.hxx
@@ -0,0 +1,45 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; 
fill-column: 100 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+#pragma once
+
+#include <svx/sdrundomanager.hxx>
+
+class SfxViewShell;
+
+class SC_DLLPUBLIC ScUndoManager : public SdrUndoManager
+{
+public:
+    ~ScUndoManager();
+
+    /**
+     * Checks if the topmost undo action owned by pView is independent from 
the topmost action undo
+     * action.
+     */
+    bool IsViewUndoActionIndependent(const SfxViewShell* pView) const;
+
+    /// Make these public
+    using SdrUndoManager::UndoWithContext;
+    using SdrUndoManager::RedoWithContext;
+
+private:
+    static std::optional<ScRange> getAffectedRangeFromUndo(const 
SfxUndoAction*);
+};
+
+class ScUndoRedoContext final : public SfxUndoContext
+{
+public:
+    void SetUndoOffset(size_t nUndoOffset) { m_nUndoOffset = nUndoOffset; }
+
+    size_t GetUndoOffset() override { return m_nUndoOffset; }
+
+private:
+    size_t m_nUndoOffset = 0;
+};
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s 
cinkeys+=0=break: */
diff --git a/sc/source/ui/undo/undobase.cxx b/sc/source/ui/undo/undobase.cxx
index 8ce8bc9e82f0..b591b38aabc7 100644
--- a/sc/source/ui/undo/undobase.cxx
+++ b/sc/source/ui/undo/undobase.cxx
@@ -21,6 +21,7 @@
 #include <svx/svdundo.hxx>
 
 #include <undobase.hxx>
+#include <undocell.hxx>
 #include <refundo.hxx>
 #include <docsh.hxx>
 #include <tabvwsh.hxx>
@@ -34,6 +35,7 @@
 #include <column.hxx>
 #include <sortparam.hxx>
 #include <columnspanset.hxx>
+#include <undomanager.hxx>
 
 
 ScSimpleUndo::ScSimpleUndo( ScDocShell* pDocSh ) :
@@ -613,4 +615,68 @@ bool ScUndoWrapper::CanRepeat(SfxRepeatTarget& rTarget) 
const
         return false;
 }
 
+ScUndoManager::~ScUndoManager() {}
+
+/**
+ * Checks if the topmost undo action owned by pView is independent from the 
topmost action undo
+ * action.
+ */
+bool ScUndoManager::IsViewUndoActionIndependent(const SfxViewShell* pView) 
const
+{
+    if (GetUndoActionCount() <= 1 || SdrUndoManager::GetRedoActionCount() > 0)
+    {
+        // Single or less undo, owned by another view; or redo actions that 
might depend on the
+        // current undo order.
+        return false;
+    }
+
+    if (!pView)
+    {
+        return false;
+    }
+
+    // Last undo action that doesn't belong to the view.
+    const SfxUndoAction* pTopAction = GetUndoAction();
+
+    ViewShellId nViewId = pView->GetViewShellId();
+
+    // Earlier undo action that belongs to the view, but is not the top one.
+    const SfxUndoAction* pViewAction = nullptr;
+    const SfxUndoAction* pAction = GetUndoAction(1);
+    if (pAction->GetViewShellId() == nViewId)
+    {
+        pViewAction = pAction;
+    }
+
+    if (!pViewAction)
+    {
+        // Found no earlier undo action that belongs to the view.
+        return false;
+    }
+
+    std::optional<ScRange> topRange = getAffectedRangeFromUndo(pTopAction);
+    if (!topRange)
+        return false;
+
+    std::optional<ScRange> viewRange = getAffectedRangeFromUndo(pViewAction);
+    if (!viewRange)
+        return false;
+
+    return !topRange->Intersects(*viewRange);
+}
+
+std::optional<ScRange> ScUndoManager::getAffectedRangeFromUndo(const 
SfxUndoAction* pAction)
+{
+    auto pListAction = dynamic_cast<const SfxListUndoAction*>(pAction);
+    if (!pListAction)
+        return std::nullopt;
+    if (pListAction->maUndoActions.size() > 1)
+        return std::nullopt;
+    auto pTopScUndoEnterData = 
dynamic_cast<ScUndoEnterData*>(pListAction->maUndoActions[0].pAction.get());
+    if (!pTopScUndoEnterData)
+        return std::nullopt;
+    return pTopScUndoEnterData->GetPositionAddress();
+}
+
+
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sc/source/ui/view/tabvwshb.cxx b/sc/source/ui/view/tabvwshb.cxx
index c7265cab1e3b..02ea57f74a5e 100644
--- a/sc/source/ui/view/tabvwshb.cxx
+++ b/sc/source/ui/view/tabvwshb.cxx
@@ -59,6 +59,7 @@
 #include <drawview.hxx>
 #include <ChartRangeSelectionListener.hxx>
 #include <gridwin.hxx>
+#include <undomanager.hxx>
 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
 #include <svx/svdpagv.hxx>
 
@@ -709,7 +710,7 @@ bool ScTabViewShell::IsSignatureLineSigned()
 void ScTabViewShell::ExecuteUndo(SfxRequest& rReq)
 {
     SfxShell* pSh = GetViewData().GetDispatcher().GetShell(0);
-    SfxUndoManager* pUndoManager = pSh->GetUndoManager();
+    ScUndoManager* pUndoManager = 
static_cast<ScUndoManager*>(pSh->GetUndoManager());
 
     const SfxItemSet* pReqArgs = rReq.GetArgs();
     ScDocShell* pDocSh = GetViewData().GetDocShell();
@@ -734,6 +735,7 @@ void ScTabViewShell::ExecuteUndo(SfxRequest& rReq)
                 if (pReqArgs && pReqArgs->GetItemState(SID_REPAIRPACKAGE, 
false, &pItem) == SfxItemState::SET)
                     bRepair = static_cast<const 
SfxBoolItem*>(pItem)->GetValue();
 
+                sal_uInt16 nUndoOffset = 0;
                 if (comphelper::LibreOfficeKit::isActive() && !bRepair)
                 {
                     SfxUndoAction* pAction = nullptr;
@@ -749,11 +751,22 @@ void ScTabViewShell::ExecuteUndo(SfxRequest& rReq)
                     }
                     if (pAction)
                     {
+                        // If another view created the undo action, prevent 
undoing it from this view.
+                        // Unless we know that the other view's undo action is 
independent from us.
                         ViewShellId nViewShellId = GetViewShellId();
                         if (pAction->GetViewShellId() != nViewShellId)
                         {
-                            rReq.SetReturnValue(SfxUInt32Item(SID_UNDO, 
static_cast<sal_uInt32>(SID_REPAIRPACKAGE)));
-                            return;
+                            if 
(pUndoManager->IsViewUndoActionIndependent(this))
+                            {
+                                // Execute the undo with an offset: don't undo 
the top action, but an
+                                // earlier one, since it's independent and 
that belongs to our view.
+                                nUndoOffset = 1;
+                            }
+                            else
+                            {
+                                rReq.SetReturnValue(SfxUInt32Item(SID_UNDO, 
static_cast<sal_uInt32>(SID_REPAIRPACKAGE)));
+                                return;
+                            }
                         }
                     }
                 }
@@ -765,12 +778,15 @@ void ScTabViewShell::ExecuteUndo(SfxRequest& rReq)
 
                 try
                 {
+                    ScUndoRedoContext aUndoRedoContext;
+                    aUndoRedoContext.SetUndoOffset(nUndoOffset);
+
                     for (sal_uInt16 i=0; i<nCount; i++)
                     {
                         if ( bIsUndo )
-                            pUndoManager->Undo();
+                            pUndoManager->UndoWithContext(aUndoRedoContext);
                         else
-                            pUndoManager->Redo();
+                            pUndoManager->RedoWithContext(aUndoRedoContext);
                     }
                 }
                 catch ( const uno::Exception& )

Reply via email to