LibreOffice Module svx (master) 1
sdrundomanager.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
21#include <svx/svdundo.hxx>
22#include <sfx2/objsh.hxx>
23#include <svl/hint.hxx>
24
26 : EditUndoManager(20 /*nMaxUndoActionCount*/)
27 , mpLastUndoActionBeforeTextEdit(nullptr)
28 , mnRedoActionCountBeforeTextEdit(0)
29 , mbEndTextEditTriggeredFromUndo(false)
30 , m_pDocSh(nullptr)
31{
32}
33
35
37{
38 if (isTextEditActive())
39 {
40 bool bRetval(false);
41
42 // we are in text edit mode
43 if (GetUndoActionCount() && mpLastUndoActionBeforeTextEdit != GetUndoAction())
44 {
45 // there is an undo action for text edit, trigger it
46 bRetval = EditUndoManager::Undo();
47 }
48 else
49 {
50 // no more text edit undo, end text edit
54 }
55
56 return bRetval;
57 }
58 else
59 {
60 // no undo triggered up to now, trigger local one
61 return SfxUndoManager::Undo();
62 }
63}
64
66{
67 bool bRetval(false);
68 bool bClearRedoStack(false);
69
70 if (isTextEditActive())
71 {
72 // we are in text edit mode
73 bRetval = EditUndoManager::Redo();
74 }
75
76 if (!bRetval)
77 {
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()));
82
83 // no redo triggered up to now, trigger local one
84 bRetval = SfxUndoManager::Redo();
85
86 // it was a SdrUndoDiagramModelData action and we have more Redo actions
87 if (bCurrentIsDiagramChange && GetRedoActionCount())
88 {
89 const bool bNextIsDiagramChange(
90 nullptr != dynamic_cast<SdrUndoDiagramModelData*>(GetRedoAction()));
91
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).
101
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).
107
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;
112 }
113 }
114
115 if (bClearRedoStack)
116 {
117 // clear Redo-Stack (explanation see above)
118 ClearRedo();
119 }
120
121 return bRetval;
122}
123
125{
126 if (isTextEditActive())
127 {
128 while (GetUndoActionCount() && mpLastUndoActionBeforeTextEdit != GetUndoAction())
129 {
130 RemoveLastUndoAction();
131 }
132
133 // urgently needed: RemoveLastUndoAction does NOT correct the Redo stack by itself (!)
134 ClearRedo();
135 }
136 else
137 {
138 // call parent
139 EditUndoManager::Clear();
140 }
141}
142
144{
145 maEndTextEditHdl = rLink;
146
147 if (isTextEditActive())
148 {
149 // text edit start, remember last non-textedit action for later cleanup
150 mpLastUndoActionBeforeTextEdit = GetUndoActionCount() ? GetUndoAction() : nullptr;
151 mnRedoActionCountBeforeTextEdit = GetRedoActionCount();
152 }
153 else
154 {
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())
159 {
160 RemoveLastUndoAction();
161 }
162
163 // urgently needed: RemoveLastUndoAction does NOT correct the Redo stack by itself (!)
164 ClearRedo();
165
166 // forget marker again
169 }
170}
171
173
174void SdrUndoManager::SetDocShell(SfxObjectShell* pDocShell) { m_pDocSh = pDocShell; }
175
177{
178 if (m_pDocSh)
179 {
180 m_pDocSh->Broadcast(SfxHint(SfxHintId::DocumentRepair));
181 }
182}
183
184/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual bool Redo() override
virtual bool Undo() override
virtual void EmptyActionsChanged() override
virtual ~SdrUndoManager() override
virtual bool Redo() override
void SetEndTextEditHdl(const Link< SdrUndoManager *, void > &rLink)
bool isTextEditActive() const
bool mbEndTextEditTriggeredFromUndo
virtual void Clear() override
void SetDocShell(SfxObjectShell *pDocShell)
SfxObjectShell * m_pDocSh
SfxUndoAction * mpLastUndoActionBeforeTextEdit
virtual bool Undo() override
react depending on edit mode and if no more undo is possible
Link< SdrUndoManager *, void > maEndTextEditHdl
size_t mnRedoActionCountBeforeTextEdit
virtual bool Undo()
virtual bool Redo()