Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sw / source / core / edit / edundo.cxx
blob576b092214df1d58a1f7842d2ed70d426d027b9c
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/svdmark.hxx>
21 #include <comphelper/diagnose_ex.hxx>
23 #include <editsh.hxx>
24 #include <fesh.hxx>
25 #include <doc.hxx>
26 #include <IDocumentUndoRedo.hxx>
27 #include <IDocumentRedlineAccess.hxx>
28 #include <UndoCore.hxx>
29 #include <swundo.hxx>
30 #include <flyfrm.hxx>
31 #include <frmfmt.hxx>
32 #include <docsh.hxx>
33 #include <pagefrm.hxx>
34 #include <textboxhelper.hxx>
35 #include <fmtanchr.hxx>
37 #include <wrtsh.hxx>
39 /** helper function to select all objects in an SdrMarkList;
40 * implementation: see below */
41 static void lcl_SelectSdrMarkList( SwEditShell* pShell,
42 const SdrMarkList* pSdrMarkList );
44 bool SwEditShell::CursorsLocked() const
46 return GetDoc()->GetDocShell()->GetModel()->hasControllersLocked();
49 void SwEditShell::HandleUndoRedoContext(::sw::UndoRedoContext & rContext)
51 // do nothing if somebody has locked controllers!
52 if (CursorsLocked())
54 return;
57 SwFrameFormat * pSelFormat(nullptr);
58 SdrMarkList * pMarkList(nullptr);
59 rContext.GetSelections(pSelFormat, pMarkList);
61 if (pSelFormat) // select frame
63 if (RES_DRAWFRMFMT == pSelFormat->Which())
65 SdrObject* pSObj = pSelFormat->FindSdrObject();
67 // Before layout calc, inline anchored textboxes have to be synced unless crash.
68 if (pSelFormat->GetAnchor().GetAnchorId() == RndStdIds::FLY_AS_CHAR
69 && pSelFormat->GetOtherTextBoxFormats())
70 SwTextBoxHelper::synchronizeGroupTextBoxProperty(SwTextBoxHelper::changeAnchor,
71 pSelFormat, pSObj);
73 static_cast<SwFEShell*>(this)->SelectObj(
74 pSObj->GetCurrentBoundRect().Center() );
76 else
78 Point aPt;
79 SwFlyFrame *const pFly =
80 static_cast<SwFlyFrameFormat*>(pSelFormat)->GetFrame(& aPt);
81 if (pFly)
83 // fdo#36681: Invalidate the content and layout to refresh
84 // the picture anchoring properly
85 SwPageFrame* pPageFrame = pFly->FindPageFrameOfAnchor();
86 pPageFrame->InvalidateFlyLayout();
87 pPageFrame->InvalidateContent();
89 static_cast<SwFEShell*>(this)->SelectFlyFrame(*pFly);
93 else if (pMarkList)
95 lcl_SelectSdrMarkList( this, pMarkList );
97 else if (GetCursor()->GetNext() != GetCursor())
99 // current cursor is the last one:
100 // go around the ring, to the first cursor
101 GoNextCursor();
105 void SwEditShell::Undo(sal_uInt16 const nCount, sal_uInt16 nOffset)
107 MakeAllOutlineContentTemporarilyVisible a(GetDoc(), true);
109 CurrShell aCurr( this );
111 // current undo state was not saved
112 ::sw::UndoGuard const undoGuard(GetDoc()->GetIDocumentUndoRedo());
113 bool bRet = false;
115 StartAllAction();
117 // Actually it should be enough to just work on the current Cursor, i.e. if there is a cycle
118 // cancel the latter temporarily, so that an insert during Undo is not done in all areas.
119 KillPams();
120 SetMark(); // Bound1 and Bound2 in the same Node
121 ClearMark();
123 // Keep Cursor - so that we're able to set it at
124 // the same position for autoformat or autocorrection
125 SwUndoId nLastUndoId(SwUndoId::EMPTY);
126 GetLastUndoInfo(nullptr, & nLastUndoId);
127 const bool bRestoreCursor = nCount == 1
128 && ( SwUndoId::AUTOFORMAT == nLastUndoId
129 || SwUndoId::AUTOCORRECT == nLastUndoId
130 || SwUndoId::SETDEFTATTR == nLastUndoId );
131 Push();
133 // Destroy stored TableBoxPtr. A detection is only permitted for the new "Box"!
134 ClearTableBoxContent();
136 const RedlineFlags eOld = GetDoc()->getIDocumentRedlineAccess().GetRedlineFlags();
138 try {
139 for (sal_uInt16 i = 0; i < nCount; ++i)
141 bRet = GetDoc()->GetIDocumentUndoRedo().UndoWithOffset(nOffset) || bRet;
143 } catch (const css::uno::Exception &) {
144 TOOLS_WARN_EXCEPTION("sw.core", "SwEditShell::Undo()");
147 if (bRestoreCursor)
148 { // fdo#39003 Pop does not touch the rest of the cursor ring
149 KillPams(); // so call this first to get rid of unwanted cursors
151 Pop(bRestoreCursor ? PopMode::DeleteCurrent : PopMode::DeleteStack);
153 GetDoc()->getIDocumentRedlineAccess().SetRedlineFlags( eOld );
154 GetDoc()->getIDocumentRedlineAccess().CompressRedlines();
156 // automatic detection of the new "Box"
157 SaveTableBoxContent();
159 EndAllAction();
162 void SwEditShell::Redo(sal_uInt16 const nCount)
164 MakeAllOutlineContentTemporarilyVisible a(GetDoc(), true);
166 CurrShell aCurr( this );
168 bool bRet = false;
170 // undo state was not saved
171 ::sw::UndoGuard const undoGuard(GetDoc()->GetIDocumentUndoRedo());
173 StartAllAction();
176 // Actually it should be enough to just work on the current Cursor, i.e. if there is a cycle
177 // cancel the latter temporarily, so that an insert during Undo is not done in all areas.
178 KillPams();
179 SetMark(); // Bound1 and Bound2 in the same Node
180 ClearMark();
182 SwUndoId nFirstRedoId(SwUndoId::EMPTY);
183 GetDoc()->GetIDocumentUndoRedo().GetFirstRedoInfo(nullptr, & nFirstRedoId);
184 const bool bRestoreCursor = nCount == 1 && SwUndoId::SETDEFTATTR == nFirstRedoId;
185 Push();
187 // Destroy stored TableBoxPtr. A detection is only permitted for the new "Box"!
188 ClearTableBoxContent();
190 RedlineFlags eOld = GetDoc()->getIDocumentRedlineAccess().GetRedlineFlags();
192 try {
193 for (sal_uInt16 i = 0; i < nCount; ++i)
195 bRet = GetDoc()->GetIDocumentUndoRedo().Redo()
196 || bRet;
198 } catch (const css::uno::Exception &) {
199 TOOLS_WARN_EXCEPTION("sw.core", "SwEditShell::Redo()");
202 Pop(bRestoreCursor ? PopMode::DeleteCurrent : PopMode::DeleteStack);
204 GetDoc()->getIDocumentRedlineAccess().SetRedlineFlags( eOld );
205 GetDoc()->getIDocumentRedlineAccess().CompressRedlines();
207 // automatic detection of the new "Box"
208 SaveTableBoxContent();
211 EndAllAction();
214 void SwEditShell::Repeat(sal_uInt16 const nCount)
216 CurrShell aCurr( this );
218 StartAllAction();
220 try {
221 ::sw::RepeatContext context(*GetDoc(), *GetCursor());
222 GetDoc()->GetIDocumentUndoRedo().Repeat( context, nCount );
223 } catch (const css::uno::Exception &) {
224 TOOLS_WARN_EXCEPTION("sw.core", "SwEditShell::Repeat()");
227 EndAllAction();
230 static void lcl_SelectSdrMarkList( SwEditShell* pShell,
231 const SdrMarkList* pSdrMarkList )
233 OSL_ENSURE( pShell != nullptr, "need shell!" );
234 OSL_ENSURE( pSdrMarkList != nullptr, "need mark list" );
236 SwFEShell* pFEShell = dynamic_cast<SwFEShell*>( pShell );
237 if( !pFEShell )
238 return;
240 bool bFirst = true;
241 for( size_t i = 0; i < pSdrMarkList->GetMarkCount(); ++i )
243 SdrObject *pObj = pSdrMarkList->GetMark( i )->GetMarkedSdrObj();
244 if( pObj )
246 pFEShell->SelectObj( Point(), bFirst ? 0 : SW_ADD_SELECT, pObj );
247 bFirst = false;
251 // the old implementation would always unselect
252 // objects, even if no new ones were selected. If this
253 // is a problem, we need to re-work this a little.
254 OSL_ENSURE( pSdrMarkList->GetMarkCount() != 0, "empty mark list" );
257 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */