Line data Source code
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 :
20 :
21 : #include <UndoManager.hxx>
22 :
23 : #include <vcl/wrkwin.hxx>
24 :
25 : #include <svx/svdmodel.hxx>
26 :
27 : #include <swmodule.hxx>
28 : #include <doc.hxx>
29 : #include <ndarr.hxx>
30 : #include <pam.hxx>
31 : #include <ndtxt.hxx>
32 : #include <swundo.hxx>
33 : #include <UndoCore.hxx>
34 : #include <rolbck.hxx>
35 : #include <undo.hrc>
36 : #include <editsh.hxx>
37 : #include <unobaseclass.hxx>
38 : #include <limits>
39 :
40 : using namespace ::com::sun::star;
41 :
42 :
43 : // the undo array should never grow beyond this limit:
44 : #define UNDO_ACTION_LIMIT (USHRT_MAX - 1000)
45 :
46 :
47 : // UndoManager ///////////////////////////////////////////////////////////
48 :
49 : namespace sw {
50 :
51 : SAL_WNODEPRECATED_DECLARATIONS_PUSH
52 276 : UndoManager::UndoManager(::std::auto_ptr<SwNodes> pUndoNodes,
53 : IDocumentDrawModelAccess & rDrawModelAccess,
54 : IDocumentRedlineAccess & rRedlineAccess,
55 : IDocumentState & rState)
56 : : m_rDrawModelAccess(rDrawModelAccess)
57 : , m_rRedlineAccess(rRedlineAccess)
58 : , m_rState(rState)
59 : , m_pUndoNodes(pUndoNodes)
60 : , m_bGroupUndo(true)
61 : , m_bDrawUndo(true)
62 : , m_bLockUndoNoModifiedPosition(false)
63 276 : , m_UndoSaveMark(MARK_INVALID)
64 : {
65 : OSL_ASSERT(m_pUndoNodes.get());
66 : // writer expects it to be disabled initially
67 : // Undo is enabled by SwEditShell constructor
68 276 : SfxUndoManager::EnableUndo(false);
69 276 : }
70 : SAL_WNODEPRECATED_DECLARATIONS_POP
71 :
72 0 : SwNodes const& UndoManager::GetUndoNodes() const
73 : {
74 0 : return *m_pUndoNodes;
75 : }
76 :
77 378 : SwNodes & UndoManager::GetUndoNodes()
78 : {
79 378 : return *m_pUndoNodes;
80 : }
81 :
82 741 : bool UndoManager::IsUndoNodes(SwNodes const& rNodes) const
83 : {
84 741 : return & rNodes == m_pUndoNodes.get();
85 : }
86 :
87 5832 : void UndoManager::DoUndo(bool const bDoUndo)
88 : {
89 5832 : EnableUndo(bDoUndo);
90 :
91 5832 : SdrModel *const pSdrModel = m_rDrawModelAccess.GetDrawModel();
92 5832 : if( pSdrModel )
93 : {
94 4519 : pSdrModel->EnableUndo(bDoUndo);
95 : }
96 5832 : }
97 :
98 109530 : bool UndoManager::DoesUndo() const
99 : {
100 109530 : return IsUndoEnabled();
101 : }
102 :
103 1478 : void UndoManager::DoGroupUndo(bool const bDoUndo)
104 : {
105 1478 : m_bGroupUndo = bDoUndo;
106 1478 : }
107 :
108 739 : bool UndoManager::DoesGroupUndo() const
109 : {
110 739 : return m_bGroupUndo;
111 : }
112 :
113 28 : void UndoManager::DoDrawUndo(bool const bDoUndo)
114 : {
115 28 : m_bDrawUndo = bDoUndo;
116 28 : }
117 :
118 14 : bool UndoManager::DoesDrawUndo() const
119 : {
120 14 : return m_bDrawUndo;
121 : }
122 :
123 :
124 474 : bool UndoManager::IsUndoNoResetModified() const
125 : {
126 474 : return MARK_INVALID == m_UndoSaveMark;
127 : }
128 :
129 11 : void UndoManager::SetUndoNoResetModified()
130 : {
131 11 : if (MARK_INVALID != m_UndoSaveMark)
132 : {
133 11 : RemoveMark(m_UndoSaveMark);
134 11 : m_UndoSaveMark = MARK_INVALID;
135 : }
136 11 : }
137 :
138 2914 : void UndoManager::SetUndoNoModifiedPosition()
139 : {
140 2914 : if (!m_bLockUndoNoModifiedPosition)
141 : {
142 2908 : m_UndoSaveMark = MarkTopUndoAction();
143 : }
144 2914 : }
145 :
146 2 : void UndoManager::LockUndoNoModifiedPosition()
147 : {
148 2 : m_bLockUndoNoModifiedPosition = true;
149 2 : }
150 :
151 0 : void UndoManager::UnLockUndoNoModifiedPosition()
152 : {
153 0 : m_bLockUndoNoModifiedPosition = false;
154 0 : }
155 :
156 :
157 0 : SwUndo* UndoManager::GetLastUndo()
158 : {
159 0 : if (!SfxUndoManager::GetUndoActionCount(CurrentLevel))
160 : {
161 0 : return 0;
162 : }
163 0 : SfxUndoAction *const pAction( SfxUndoManager::GetUndoAction(0) );
164 0 : return dynamic_cast<SwUndo*>(pAction);
165 : }
166 :
167 0 : void UndoManager::AppendUndo(SwUndo *const pUndo)
168 : {
169 0 : AddUndoAction(pUndo);
170 0 : }
171 :
172 0 : void UndoManager::ClearRedo()
173 : {
174 0 : return SfxUndoManager::ImplClearRedo_NoLock(TopLevel);
175 : }
176 :
177 111 : void UndoManager::DelAllUndoObj()
178 : {
179 111 : ::sw::UndoGuard const undoGuard(*this);
180 :
181 111 : SfxUndoManager::ClearAllLevels();
182 :
183 111 : m_UndoSaveMark = MARK_INVALID;
184 111 : }
185 :
186 :
187 : /**************** UNDO ******************/
188 :
189 : SwUndoId
190 3034 : UndoManager::StartUndo(SwUndoId const i_eUndoId,
191 : SwRewriter const*const pRewriter)
192 : {
193 3034 : if (!IsUndoEnabled())
194 : {
195 3013 : return UNDO_EMPTY;
196 : }
197 :
198 21 : SwUndoId const eUndoId( (0 == i_eUndoId) ? UNDO_START : i_eUndoId );
199 :
200 : OSL_ASSERT(UNDO_END != eUndoId);
201 : String comment( (UNDO_START == eUndoId)
202 : ? String("??", RTL_TEXTENCODING_ASCII_US)
203 21 : : String(SW_RES(UNDO_BASE + eUndoId)) );
204 21 : if (pRewriter)
205 : {
206 : OSL_ASSERT(UNDO_START != eUndoId);
207 0 : comment = pRewriter->Apply(comment);
208 : }
209 :
210 21 : SfxUndoManager::EnterListAction(comment, comment, eUndoId);
211 :
212 21 : return eUndoId;
213 : }
214 :
215 :
216 : SwUndoId
217 3002 : UndoManager::EndUndo(SwUndoId const i_eUndoId, SwRewriter const*const pRewriter)
218 : {
219 3002 : if (!IsUndoEnabled())
220 : {
221 2981 : return UNDO_EMPTY;
222 : }
223 :
224 : SwUndoId const eUndoId( ((0 == i_eUndoId) || (UNDO_START == i_eUndoId))
225 21 : ? UNDO_END : i_eUndoId );
226 : OSL_ENSURE(!((UNDO_END == eUndoId) && pRewriter),
227 : "EndUndo(): no Undo ID, but rewriter given?");
228 :
229 : SfxUndoAction *const pLastUndo(
230 21 : (0 == SfxUndoManager::GetUndoActionCount(CurrentLevel))
231 21 : ? 0 : SfxUndoManager::GetUndoAction(0) );
232 :
233 21 : int const nCount = LeaveListAction();
234 :
235 21 : if (nCount) // otherwise: empty list action not inserted!
236 : {
237 : OSL_ASSERT(pLastUndo);
238 : OSL_ASSERT(UNDO_START != eUndoId);
239 0 : SfxUndoAction *const pUndoAction(SfxUndoManager::GetUndoAction(0));
240 : SfxListUndoAction *const pListAction(
241 0 : dynamic_cast<SfxListUndoAction*>(pUndoAction));
242 : OSL_ASSERT(pListAction);
243 0 : if (pListAction)
244 : {
245 0 : if (UNDO_END != eUndoId)
246 : {
247 : OSL_ENSURE(pListAction->GetId() == eUndoId,
248 : "EndUndo(): given ID different from StartUndo()");
249 : // comment set by caller of EndUndo
250 0 : String comment = String(SW_RES(UNDO_BASE + eUndoId));
251 0 : if (pRewriter)
252 : {
253 0 : comment = pRewriter->Apply(comment);
254 : }
255 0 : pListAction->SetComment(comment);
256 : }
257 0 : else if ((UNDO_START != pListAction->GetId()))
258 : {
259 : // comment set by caller of StartUndo: nothing to do here
260 : }
261 0 : else if (pLastUndo)
262 : {
263 : // comment was not set at StartUndo or EndUndo:
264 : // take comment of last contained action
265 : // (note that this works recursively, i.e. the last contained
266 : // action may be a list action created by StartUndo/EndUndo)
267 0 : String const comment(pLastUndo->GetComment());
268 0 : pListAction->SetComment(comment);
269 : }
270 : else
271 : {
272 : OSL_ENSURE(false, "EndUndo(): no comment?");
273 : }
274 : }
275 : }
276 :
277 21 : return eUndoId;
278 : }
279 :
280 : bool
281 0 : UndoManager::GetLastUndoInfo(
282 : ::rtl::OUString *const o_pStr, SwUndoId *const o_pId) const
283 : {
284 : // this is actually expected to work on the current level,
285 : // but that was really not obvious from the previous implementation...
286 0 : if (!SfxUndoManager::GetUndoActionCount(CurrentLevel))
287 : {
288 0 : return false;
289 : }
290 :
291 0 : SfxUndoAction *const pAction( SfxUndoManager::GetUndoAction(0) );
292 0 : if (o_pStr)
293 : {
294 0 : *o_pStr = pAction->GetComment();
295 : }
296 0 : if (o_pId)
297 : {
298 0 : sal_uInt16 const nId(pAction->GetId());
299 0 : *o_pId = static_cast<SwUndoId>(nId);
300 : }
301 :
302 0 : return true;
303 : }
304 :
305 0 : SwUndoComments_t UndoManager::GetUndoComments() const
306 : {
307 : OSL_ENSURE(!SfxUndoManager::IsInListAction(),
308 : "GetUndoComments() called while in list action?");
309 :
310 0 : SwUndoComments_t ret;
311 0 : sal_uInt16 const nUndoCount(SfxUndoManager::GetUndoActionCount(TopLevel));
312 0 : for (sal_uInt16 n = 0; n < nUndoCount; ++n)
313 : {
314 : ::rtl::OUString const comment(
315 0 : SfxUndoManager::GetUndoActionComment(n, TopLevel));
316 0 : ret.push_back(comment);
317 0 : }
318 :
319 0 : return ret;
320 : }
321 :
322 :
323 : /**************** REDO ******************/
324 0 : bool UndoManager::GetFirstRedoInfo(::rtl::OUString *const o_pStr) const
325 : {
326 0 : if (!SfxUndoManager::GetRedoActionCount(CurrentLevel))
327 : {
328 0 : return false;
329 : }
330 :
331 0 : if (o_pStr)
332 : {
333 0 : *o_pStr = SfxUndoManager::GetRedoActionComment(0, CurrentLevel);
334 : }
335 :
336 0 : return true;
337 : }
338 :
339 :
340 0 : SwUndoComments_t UndoManager::GetRedoComments() const
341 : {
342 : OSL_ENSURE(!SfxUndoManager::IsInListAction(),
343 : "GetRedoComments() called while in list action?");
344 :
345 0 : SwUndoComments_t ret;
346 0 : sal_uInt16 const nRedoCount(SfxUndoManager::GetRedoActionCount(TopLevel));
347 0 : for (sal_uInt16 n = 0; n < nRedoCount; ++n)
348 : {
349 : ::rtl::OUString const comment(
350 0 : SfxUndoManager::GetRedoActionComment(n, TopLevel));
351 0 : ret.push_back(comment);
352 0 : }
353 :
354 0 : return ret;
355 : }
356 :
357 : /**************** REPEAT ******************/
358 :
359 0 : SwUndoId UndoManager::GetRepeatInfo(::rtl::OUString *const o_pStr) const
360 : {
361 0 : SwUndoId nRepeatId(UNDO_EMPTY);
362 0 : GetLastUndoInfo(o_pStr, & nRepeatId);
363 0 : if( REPEAT_START <= nRepeatId && REPEAT_END > nRepeatId )
364 : {
365 0 : return nRepeatId;
366 : }
367 0 : if (o_pStr) // not repeatable -> clear comment
368 : {
369 0 : *o_pStr = String();
370 : }
371 0 : return UNDO_EMPTY;
372 : }
373 :
374 0 : SwUndo * UndoManager::RemoveLastUndo()
375 : {
376 0 : if (SfxUndoManager::GetRedoActionCount(CurrentLevel) ||
377 0 : SfxUndoManager::GetRedoActionCount(TopLevel))
378 : {
379 : OSL_ENSURE(false, "RemoveLastUndoAction(): there are Redo actions?");
380 0 : return 0;
381 : }
382 0 : if (!SfxUndoManager::GetUndoActionCount(CurrentLevel))
383 : {
384 : OSL_ENSURE(false, "RemoveLastUndoAction(): no Undo actions");
385 0 : return 0;
386 : }
387 0 : SfxUndoAction *const pLastUndo(GetUndoAction(0));
388 0 : SfxUndoManager::RemoveLastUndoAction();
389 0 : return dynamic_cast<SwUndo *>(pLastUndo);
390 : }
391 :
392 : // svl::IUndoManager /////////////////////////////////////////////////////
393 :
394 5832 : void UndoManager::EnableUndo(bool bEnable)
395 : {
396 : // SfxUndoManager does not have a counter anymore, but reverted to the old behavior of
397 : // having a simple boolean flag for locking. So, simply forward.
398 5832 : SfxUndoManager::EnableUndo(bEnable);
399 5832 : }
400 :
401 0 : void UndoManager::AddUndoAction(SfxUndoAction *pAction, sal_Bool bTryMerge)
402 : {
403 0 : SwUndo *const pUndo( dynamic_cast<SwUndo *>(pAction) );
404 0 : if (pUndo)
405 : {
406 0 : if (nsRedlineMode_t::REDLINE_NONE == pUndo->GetRedlineMode())
407 : {
408 0 : pUndo->SetRedlineMode( m_rRedlineAccess.GetRedlineMode() );
409 : }
410 : }
411 0 : SfxUndoManager::AddUndoAction(pAction, bTryMerge);
412 : // if the undo nodes array is too large, delete some actions
413 0 : while (UNDO_ACTION_LIMIT < GetUndoNodes().Count())
414 : {
415 0 : RemoveOldestUndoActions(1);
416 : }
417 0 : }
418 :
419 : class CursorGuard
420 : {
421 : public:
422 0 : CursorGuard(SwEditShell & rShell, bool const bSave)
423 : : m_rShell(rShell)
424 0 : , m_bSaveCursor(bSave)
425 : {
426 0 : if (m_bSaveCursor)
427 : {
428 0 : m_rShell.Push(); // prevent modification of current cursor
429 : }
430 0 : }
431 0 : ~CursorGuard()
432 : {
433 0 : if (m_bSaveCursor)
434 : {
435 0 : m_rShell.Pop();
436 : }
437 0 : }
438 : private:
439 : SwEditShell & m_rShell;
440 : bool const m_bSaveCursor;
441 : };
442 :
443 0 : bool UndoManager::impl_DoUndoRedo(UndoOrRedo_t const undoOrRedo)
444 : {
445 0 : SwDoc & rDoc(*GetUndoNodes().GetDoc());
446 :
447 0 : UnoActionContext c(& rDoc); // exception-safe StartAllAction/EndAllAction
448 :
449 0 : SwEditShell *const pEditShell( rDoc.GetEditShell() );
450 :
451 : OSL_ENSURE(pEditShell, "sw::UndoManager needs a SwEditShell!");
452 0 : if (!pEditShell)
453 : {
454 0 : throw uno::RuntimeException();
455 : }
456 :
457 : // in case the model has controllers locked, the Undo should not
458 : // change the view cursors!
459 0 : bool const bSaveCursors(pEditShell->CursorsLocked());
460 0 : CursorGuard(*pEditShell, bSaveCursors);
461 0 : if (!bSaveCursors)
462 : {
463 : // (in case Undo was called via API) clear the cursors:
464 0 : pEditShell->KillPams();
465 0 : pEditShell->SetMark();
466 0 : pEditShell->ClearMark();
467 : }
468 :
469 0 : bool bRet(false);
470 :
471 0 : ::sw::UndoRedoContext context(rDoc, *pEditShell);
472 :
473 : // N.B. these may throw!
474 0 : if (UNDO == undoOrRedo)
475 : {
476 0 : bRet = SfxUndoManager::UndoWithContext(context);
477 : }
478 : else
479 : {
480 0 : bRet = SfxUndoManager::RedoWithContext(context);
481 : }
482 :
483 0 : if (bRet)
484 : {
485 : // if we are at the "last save" position, the document is not modified
486 0 : if (SfxUndoManager::HasTopUndoActionMark(m_UndoSaveMark))
487 : {
488 0 : m_rState.ResetModified();
489 : }
490 : else
491 : {
492 0 : m_rState.SetModified();
493 : }
494 : }
495 :
496 0 : pEditShell->HandleUndoRedoContext(context);
497 :
498 0 : return bRet;
499 : }
500 :
501 0 : sal_Bool UndoManager::Undo()
502 : {
503 0 : bool const bRet = impl_DoUndoRedo(UNDO);
504 0 : return bRet;
505 : }
506 :
507 0 : sal_Bool UndoManager::Redo()
508 : {
509 0 : bool const bRet = impl_DoUndoRedo(REDO);
510 0 : return bRet;
511 : }
512 :
513 : /** N.B.: this does _not_ call SfxUndoManager::Repeat because it is not
514 : possible to wrap a list action around it:
515 : calling EnterListAction here will cause SfxUndoManager::Repeat
516 : to repeat the list action!
517 : */
518 : bool
519 0 : UndoManager::Repeat(::sw::RepeatContext & rContext,
520 : sal_uInt16 const nRepeatCount)
521 : {
522 0 : if (SfxUndoManager::IsInListAction())
523 : {
524 : OSL_ENSURE(false, "repeat in open list action???");
525 0 : return false;
526 : }
527 0 : if (!SfxUndoManager::GetUndoActionCount(TopLevel))
528 : {
529 0 : return false;
530 : }
531 0 : SfxUndoAction *const pRepeatAction(GetUndoAction(0));
532 : OSL_ASSERT(pRepeatAction);
533 0 : if (!pRepeatAction || !pRepeatAction->CanRepeat(rContext))
534 : {
535 0 : return false;
536 : }
537 :
538 0 : ::rtl::OUString const comment(pRepeatAction->GetComment());
539 0 : ::rtl::OUString const rcomment(pRepeatAction->GetRepeatComment(rContext));
540 0 : sal_uInt16 const nId(pRepeatAction->GetId());
541 0 : if (DoesUndo())
542 : {
543 0 : EnterListAction(comment, rcomment, nId);
544 : }
545 :
546 0 : SwPaM *const pFirstCursor(& rContext.GetRepeatPaM());
547 0 : do { // iterate over ring
548 0 : for (sal_uInt16 nRptCnt = nRepeatCount; nRptCnt > 0; --nRptCnt)
549 : {
550 0 : pRepeatAction->Repeat(rContext);
551 : }
552 0 : rContext.m_bDeleteRepeated = false; // reset for next PaM
553 : rContext.m_pCurrentPaM =
554 0 : static_cast<SwPaM*>(rContext.m_pCurrentPaM->GetNext());
555 0 : } while (pFirstCursor != & rContext.GetRepeatPaM());
556 :
557 0 : if (DoesUndo())
558 : {
559 0 : LeaveListAction();
560 : }
561 0 : return true;
562 : }
563 :
564 : } // namespace sw
565 :
566 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|