Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / svx / source / svdraw / sdrundomanager.cxx
blob3d5fde475ac818fa73493d15eebf9b166f15434b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <svx/sdrundomanager.hxx>
21 #include <svx/svdundo.hxx>
22 #include <sfx2/objsh.hxx>
23 #include <svl/hint.hxx>
25 SdrUndoManager::SdrUndoManager()
26 : EditUndoManager(20 /*nMaxUndoActionCount*/)
27 , mpLastUndoActionBeforeTextEdit(nullptr)
28 , mnRedoActionCountBeforeTextEdit(0)
29 , mbEndTextEditTriggeredFromUndo(false)
30 , m_pDocSh(nullptr)
34 SdrUndoManager::~SdrUndoManager() {}
36 bool SdrUndoManager::Undo()
38 if (isTextEditActive())
40 bool bRetval(false);
42 // we are in text edit mode
43 if (GetUndoActionCount() && mpLastUndoActionBeforeTextEdit != GetUndoAction())
45 // there is an undo action for text edit, trigger it
46 bRetval = EditUndoManager::Undo();
48 else
50 // no more text edit undo, end text edit
51 mbEndTextEditTriggeredFromUndo = true;
52 maEndTextEditHdl.Call(this);
53 mbEndTextEditTriggeredFromUndo = false;
56 return bRetval;
58 else
60 // no undo triggered up to now, trigger local one
61 return SfxUndoManager::Undo();
65 bool SdrUndoManager::Redo()
67 bool bRetval(false);
68 bool bClearRedoStack(false);
70 if (isTextEditActive())
72 // we are in text edit mode
73 bRetval = EditUndoManager::Redo();
76 if (!bRetval)
78 // Check if the current and thus to-be undone UndoAction is a SdrUndoDiagramModelData action
79 const bool bCurrentIsDiagramChange(
80 GetRedoActionCount()
81 && nullptr != dynamic_cast<SdrUndoDiagramModelData*>(GetRedoAction()));
83 // no redo triggered up to now, trigger local one
84 bRetval = SfxUndoManager::Redo();
86 // it was a SdrUndoDiagramModelData action and we have more Redo actions
87 if (bCurrentIsDiagramChange && GetRedoActionCount())
89 const bool bNextIsDiagramChange(
90 nullptr != dynamic_cast<SdrUndoDiagramModelData*>(GetRedoAction()));
92 // We have more Redo-actions and the 'next' one to be executed is *not* a
93 // SdrUndoDiagramModelData-action. This means that the already executed
94 // one had done a re-Layout/Re-create of the Diagram XShape/SdrObject
95 // representation based on the restored Diagram ModelData. When the next
96 // Redo action is something else (and thus will not itself re-create
97 // XShapes/SdrShapes) it may be that it is an UnGroup/Delete where a former
98 // as-content-of-Diagram created XShape/SdrShape is referenced, an action
99 // that references a XShape/SdrShape by pointer/reference. That
100 // pointer/reference *cannot* be valid anymore (now).
102 // The problem here is that Undo/Redo actions historically reference
103 // XShapes/SdrShapes by pointer/reference, e.g. deleting means: remove
104 // from an SdrObjList and add to an Undo action. I is *not*
105 // address/incarnation-invariant in the sense to remember e.g. to
106 // remove the Nth object in the list (that would work).
108 // It might be possible to solve/correct this better, but since it's
109 // a rare corner case just avoid the possible crash when continuing Redos
110 // by clearing the Redo-Stack here as a consequence
111 bClearRedoStack = !bNextIsDiagramChange;
115 if (bClearRedoStack)
117 // clear Redo-Stack (explanation see above)
118 ClearRedo();
121 return bRetval;
124 void SdrUndoManager::Clear()
126 if (isTextEditActive())
128 while (GetUndoActionCount() && mpLastUndoActionBeforeTextEdit != GetUndoAction())
130 RemoveLastUndoAction();
133 // urgently needed: RemoveLastUndoAction does NOT correct the Redo stack by itself (!)
134 ClearRedo();
136 else
138 // call parent
139 EditUndoManager::Clear();
143 void SdrUndoManager::SetEndTextEditHdl(const Link<SdrUndoManager*, void>& rLink)
145 maEndTextEditHdl = rLink;
147 if (isTextEditActive())
149 // text edit start, remember last non-textedit action for later cleanup
150 mpLastUndoActionBeforeTextEdit = GetUndoActionCount() ? GetUndoAction() : nullptr;
151 mnRedoActionCountBeforeTextEdit = GetRedoActionCount();
153 else
155 // text edit ends, pop all textedit actions up to the remembered non-textedit action from the start
156 // to set back the UndoManager to the state before text edit started. If that action is already gone
157 // (due to being removed from the undo stack in the meantime), all need to be removed anyways
158 while (GetUndoActionCount() && mpLastUndoActionBeforeTextEdit != GetUndoAction())
160 RemoveLastUndoAction();
163 // urgently needed: RemoveLastUndoAction does NOT correct the Redo stack by itself (!)
164 ClearRedo();
166 // forget marker again
167 mpLastUndoActionBeforeTextEdit = nullptr;
168 mnRedoActionCountBeforeTextEdit = 0;
172 bool SdrUndoManager::isTextEditActive() const { return maEndTextEditHdl.IsSet(); }
174 void SdrUndoManager::SetDocShell(SfxObjectShell* pDocShell) { m_pDocSh = pDocShell; }
176 void SdrUndoManager::EmptyActionsChanged()
178 if (m_pDocSh)
180 m_pDocSh->Broadcast(SfxHint(SfxHintId::DocumentRepair));
184 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */