1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 // MARKER(update_precomp.py): autogen include statement, do not remove
23 #include "precompiled_svx.hxx"
24 #include <svx/sdrundomanager.hxx>
25 
26 //////////////////////////////////////////////////////////////////////////////
27 
SdrUndoManager(sal_uInt16 nMaxUndoActionCount)28 SdrUndoManager::SdrUndoManager(sal_uInt16 nMaxUndoActionCount)
29 :   EditUndoManager(nMaxUndoActionCount),
30     maEndTextEditHdl(),
31     mpLastUndoActionBeforeTextEdit(0),
32     mbEndTextEditTriggeredFromUndo(false)
33 {
34 }
35 
~SdrUndoManager()36 SdrUndoManager::~SdrUndoManager()
37 {
38 }
39 
Undo()40 sal_Bool SdrUndoManager::Undo()
41 {
42     if(isTextEditActive())
43     {
44         sal_Bool bRetval(sal_False);
45 
46         // we are in text edit mode
47         if(GetUndoActionCount() && mpLastUndoActionBeforeTextEdit != GetUndoAction(0))
48         {
49             // there is an undo action for text edit, trigger it
50             bRetval = EditUndoManager::Undo();
51         }
52         else
53         {
54             // no more text edit undo, end text edit
55             mbEndTextEditTriggeredFromUndo = true;
56             maEndTextEditHdl.Call(this);
57             mbEndTextEditTriggeredFromUndo = false;
58         }
59 
60         return bRetval;
61     }
62     else
63     {
64         // no undo triggered up to now, trigger local one
65         return SfxUndoManager::Undo();
66     }
67 }
68 
Redo()69 sal_Bool SdrUndoManager::Redo()
70 {
71     sal_Bool bRetval(sal_False);
72 
73     if(isTextEditActive())
74     {
75         // we are in text edit mode
76         bRetval = EditUndoManager::Redo();
77     }
78 
79     if(!bRetval)
80     {
81         // no redo triggered up to now, trigger local one
82         bRetval = SfxUndoManager::Redo();
83     }
84 
85     return bRetval;
86 }
87 
Clear()88 void SdrUndoManager::Clear()
89 {
90     if(isTextEditActive())
91     {
92         while(GetUndoActionCount() && mpLastUndoActionBeforeTextEdit != GetUndoAction(0))
93         {
94             RemoveLastUndoAction();
95         }
96 
97         // urgently needed: RemoveLastUndoAction does NOT correct the Redo stack by itself (!)
98         ClearRedo();
99     }
100     else
101     {
102         // call parent
103         EditUndoManager::Clear();
104     }
105 }
106 
SetEndTextEditHdl(const Link & rLink)107 void SdrUndoManager::SetEndTextEditHdl(const Link& rLink)
108 {
109     maEndTextEditHdl = rLink;
110 
111     if(isTextEditActive())
112     {
113         // text edit start, remember last non-textedit action for later cleanup
114         mpLastUndoActionBeforeTextEdit = GetUndoActionCount() ? GetUndoAction(0) : 0;
115     }
116     else
117     {
118         // text edit ends, pop all textedit actions up to the remembered non-textedit action from the start
119         // to set back the UndoManager to the state before text edit started. If that action is already gone
120         // (due to being removed from the undo stack in the meantime), all need to be removed anyways
121         while(GetUndoActionCount() && mpLastUndoActionBeforeTextEdit != GetUndoAction(0))
122         {
123             RemoveLastUndoAction();
124         }
125 
126         // urgently needed: RemoveLastUndoAction does NOT correct the Redo stack by itself (!)
127         ClearRedo();
128 
129         // forget marker again
130         mpLastUndoActionBeforeTextEdit = 0;
131     }
132 }
133 
isTextEditActive() const134 bool SdrUndoManager::isTextEditActive() const
135 {
136     return maEndTextEditHdl.IsSet();
137 }
138 
139 //////////////////////////////////////////////////////////////////////////////
140 // eof
141