Add Marathi autocorrect
[LibreOffice.git] / sw / source / core / edit / edundo.cxx
blob34e295427fd639692da24c52b557d475875d5d3b
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 SwDocShell* pShell = GetDoc()->GetDocShell();
47 if (!pShell)
48 return false;
49 return pShell->GetModel()->hasControllersLocked();
52 void SwEditShell::HandleUndoRedoContext(::sw::UndoRedoContext & rContext)
54 // do nothing if somebody has locked controllers!
55 if (CursorsLocked())
57 return;
60 SwFrameFormat * pSelFormat(nullptr);
61 SdrMarkList * pMarkList(nullptr);
62 rContext.GetSelections(pSelFormat, pMarkList);
64 if (pSelFormat) // select frame
66 if (RES_DRAWFRMFMT == pSelFormat->Which())
68 SdrObject* pSObj = pSelFormat->FindSdrObject();
70 // Before layout calc, inline anchored textboxes have to be synced unless crash.
71 if (pSelFormat->GetAnchor().GetAnchorId() == RndStdIds::FLY_AS_CHAR
72 && pSelFormat->GetOtherTextBoxFormats())
73 SwTextBoxHelper::synchronizeGroupTextBoxProperty(SwTextBoxHelper::changeAnchor,
74 pSelFormat, pSObj);
76 static_cast<SwFEShell*>(this)->SelectObj(
77 pSObj->GetCurrentBoundRect().Center() );
79 else
81 Point aPt;
82 SwFlyFrame *const pFly =
83 static_cast<SwFlyFrameFormat*>(pSelFormat)->GetFrame(& aPt);
84 if (pFly)
86 // fdo#36681: Invalidate the content and layout to refresh
87 // the picture anchoring properly
88 SwPageFrame* pPageFrame = pFly->FindPageFrameOfAnchor();
89 pPageFrame->InvalidateFlyLayout();
90 pPageFrame->InvalidateContent();
92 static_cast<SwFEShell*>(this)->SelectFlyFrame(*pFly);
96 else if (pMarkList)
98 lcl_SelectSdrMarkList( this, pMarkList );
100 else if (GetCursor()->GetNext() != GetCursor())
102 // current cursor is the last one:
103 // go around the ring, to the first cursor
104 GoNextCursor();
108 void SwEditShell::Undo(sal_uInt16 const nCount, sal_uInt16 nOffset)
110 MakeAllOutlineContentTemporarilyVisible a(GetDoc(), true);
112 CurrShell aCurr( this );
114 // current undo state was not saved
115 ::sw::UndoGuard const undoGuard(GetDoc()->GetIDocumentUndoRedo());
117 StartAllAction();
119 // Actually it should be enough to just work on the current Cursor, i.e. if there is a cycle
120 // cancel the latter temporarily, so that an insert during Undo is not done in all areas.
121 KillPams();
122 SetMark(); // Bound1 and Bound2 in the same Node
123 ClearMark();
125 // Keep Cursor - so that we're able to set it at
126 // the same position for autoformat or autocorrection
127 SwUndoId nLastUndoId(SwUndoId::EMPTY);
128 GetLastUndoInfo(nullptr, & nLastUndoId);
129 const bool bRestoreCursor = nCount == 1
130 && ( SwUndoId::AUTOFORMAT == nLastUndoId
131 || SwUndoId::AUTOCORRECT == nLastUndoId
132 || SwUndoId::SETDEFTATTR == nLastUndoId );
133 Push();
135 // Destroy stored TableBoxPtr. A detection is only permitted for the new "Box"!
136 ClearTableBoxContent();
138 const RedlineFlags eOld = GetDoc()->getIDocumentRedlineAccess().GetRedlineFlags();
140 try {
141 for (sal_uInt16 i = 0; i < nCount; ++i)
143 GetDoc()->GetIDocumentUndoRedo().UndoWithOffset(nOffset);
145 } catch (const css::uno::Exception &) {
146 TOOLS_WARN_EXCEPTION("sw.core", "SwEditShell::Undo()");
149 if (bRestoreCursor)
150 { // fdo#39003 Pop does not touch the rest of the cursor ring
151 KillPams(); // so call this first to get rid of unwanted cursors
153 Pop(bRestoreCursor ? PopMode::DeleteCurrent : PopMode::DeleteStack);
155 GetDoc()->getIDocumentRedlineAccess().SetRedlineFlags( eOld );
156 GetDoc()->getIDocumentRedlineAccess().CompressRedlines();
158 // automatic detection of the new "Box"
159 SaveTableBoxContent();
161 EndAllAction();
164 void SwEditShell::Redo(sal_uInt16 const nCount)
166 MakeAllOutlineContentTemporarilyVisible a(GetDoc(), true);
168 CurrShell aCurr( this );
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 GetDoc()->GetIDocumentUndoRedo().Redo();
197 } catch (const css::uno::Exception &) {
198 TOOLS_WARN_EXCEPTION("sw.core", "SwEditShell::Redo()");
201 Pop(bRestoreCursor ? PopMode::DeleteCurrent : PopMode::DeleteStack);
203 GetDoc()->getIDocumentRedlineAccess().SetRedlineFlags( eOld );
204 GetDoc()->getIDocumentRedlineAccess().CompressRedlines();
206 // automatic detection of the new "Box"
207 SaveTableBoxContent();
210 EndAllAction();
213 void SwEditShell::Repeat(sal_uInt16 const nCount)
215 CurrShell aCurr( this );
217 StartAllAction();
219 try {
220 ::sw::RepeatContext context(*GetDoc(), *GetCursor());
221 GetDoc()->GetIDocumentUndoRedo().Repeat( context, nCount );
222 } catch (const css::uno::Exception &) {
223 TOOLS_WARN_EXCEPTION("sw.core", "SwEditShell::Repeat()");
226 EndAllAction();
229 static void lcl_SelectSdrMarkList( SwEditShell* pShell,
230 const SdrMarkList* pSdrMarkList )
232 OSL_ENSURE( pShell != nullptr, "need shell!" );
234 SwFEShell* pFEShell = dynamic_cast<SwFEShell*>( pShell );
235 if( !pFEShell )
236 return;
238 assert(pSdrMarkList != nullptr && "need mark list");
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: */