LibreOffice Module sw (master)  1
edundo.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 
20 #include <svx/svdmark.hxx>
21 #include <tools/diagnose_ex.h>
22 
23 #include <editsh.hxx>
24 #include <fesh.hxx>
25 #include <doc.hxx>
26 #include <IDocumentUndoRedo.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 
35 #include <wrtsh.hxx>
36 
39 static void lcl_SelectSdrMarkList( SwEditShell* pShell,
40  const SdrMarkList* pSdrMarkList );
41 
43 {
44  return GetDoc()->GetDocShell()->GetModel()->hasControllersLocked();
45 }
46 
48 {
49  // do nothing if somebody has locked controllers!
50  if (CursorsLocked())
51  {
52  return;
53  }
54 
55  SwFrameFormat * pSelFormat(nullptr);
56  SdrMarkList * pMarkList(nullptr);
57  rContext.GetSelections(pSelFormat, pMarkList);
58 
59  if (pSelFormat) // select frame
60  {
61  if (RES_DRAWFRMFMT == pSelFormat->Which())
62  {
63  SdrObject* pSObj = pSelFormat->FindSdrObject();
64  static_cast<SwFEShell*>(this)->SelectObj(
65  pSObj->GetCurrentBoundRect().Center() );
66  }
67  else
68  {
69  Point aPt;
70  SwFlyFrame *const pFly =
71  static_cast<SwFlyFrameFormat*>(pSelFormat)->GetFrame(& aPt);
72  if (pFly)
73  {
74  // fdo#36681: Invalidate the content and layout to refresh
75  // the picture anchoring properly
76  SwPageFrame* pPageFrame = pFly->FindPageFrameOfAnchor();
77  pPageFrame->InvalidateFlyLayout();
78  pPageFrame->InvalidateContent();
79 
80  static_cast<SwFEShell*>(this)->SelectFlyFrame(*pFly);
81  }
82  }
83  }
84  else if (pMarkList)
85  {
86  lcl_SelectSdrMarkList( this, pMarkList );
87  }
88  else if (GetCursor()->GetNext() != GetCursor())
89  {
90  // current cursor is the last one:
91  // go around the ring, to the first cursor
92  GoNextCursor();
93  }
94 }
95 
96 void SwEditShell::Undo(sal_uInt16 const nCount, sal_uInt16 nOffset)
97 {
99 
100  CurrShell aCurr( this );
101 
102  // current undo state was not saved
103  ::sw::UndoGuard const undoGuard(GetDoc()->GetIDocumentUndoRedo());
104  bool bRet = false;
105 
106  StartAllAction();
107  {
108  // Actually it should be enough to just work on the current Cursor, i.e. if there is a cycle
109  // cancel the latter temporarily, so that an insert during Undo is not done in all areas.
110  KillPams();
111  SetMark(); // Bound1 and Bound2 in the same Node
112  ClearMark();
113 
114  // Keep Cursor - so that we're able to set it at
115  // the same position for autoformat or autocorrection
116  SwUndoId nLastUndoId(SwUndoId::EMPTY);
117  GetLastUndoInfo(nullptr, & nLastUndoId);
118  const bool bRestoreCursor = nCount == 1
119  && ( SwUndoId::AUTOFORMAT == nLastUndoId
120  || SwUndoId::AUTOCORRECT == nLastUndoId
121  || SwUndoId::SETDEFTATTR == nLastUndoId );
122  Push();
123 
124  // Destroy stored TableBoxPtr. A detection is only permitted for the new "Box"!
126 
128 
129  try {
130  for (sal_uInt16 i = 0; i < nCount; ++i)
131  {
132  bRet = GetDoc()->GetIDocumentUndoRedo().UndoWithOffset(nOffset) || bRet;
133  }
134  } catch (const css::uno::Exception &) {
135  TOOLS_WARN_EXCEPTION("sw.core", "SwEditShell::Undo()");
136  }
137 
138  if (bRestoreCursor)
139  { // fdo#39003 Pop does not touch the rest of the cursor ring
140  KillPams(); // so call this first to get rid of unwanted cursors
141  }
142  Pop(bRestoreCursor ? PopMode::DeleteCurrent : PopMode::DeleteStack);
143 
146 
147  // automatic detection of the new "Box"
149  }
150  EndAllAction();
151 }
152 
153 void SwEditShell::Redo(sal_uInt16 const nCount)
154 {
156 
157  CurrShell aCurr( this );
158 
159  bool bRet = false;
160 
161  // undo state was not saved
162  ::sw::UndoGuard const undoGuard(GetDoc()->GetIDocumentUndoRedo());
163 
164  StartAllAction();
165 
166  {
167  // Actually it should be enough to just work on the current Cursor, i.e. if there is a cycle
168  // cancel the latter temporarily, so that an insert during Undo is not done in all areas.
169  KillPams();
170  SetMark(); // Bound1 and Bound2 in the same Node
171  ClearMark();
172 
173  SwUndoId nFirstRedoId(SwUndoId::EMPTY);
174  GetDoc()->GetIDocumentUndoRedo().GetFirstRedoInfo(nullptr, & nFirstRedoId);
175  const bool bRestoreCursor = nCount == 1 && SwUndoId::SETDEFTATTR == nFirstRedoId;
176  Push();
177 
178  // Destroy stored TableBoxPtr. A detection is only permitted for the new "Box"!
180 
182 
183  try {
184  for (sal_uInt16 i = 0; i < nCount; ++i)
185  {
186  bRet = GetDoc()->GetIDocumentUndoRedo().Redo()
187  || bRet;
188  }
189  } catch (const css::uno::Exception &) {
190  TOOLS_WARN_EXCEPTION("sw.core", "SwEditShell::Redo()");
191  }
192 
193  Pop(bRestoreCursor ? PopMode::DeleteCurrent : PopMode::DeleteStack);
194 
197 
198  // automatic detection of the new "Box"
200  }
201 
202  EndAllAction();
203 }
204 
205 void SwEditShell::Repeat(sal_uInt16 const nCount)
206 {
207  CurrShell aCurr( this );
208 
209  StartAllAction();
210 
211  try {
212  ::sw::RepeatContext context(*GetDoc(), *GetCursor());
213  GetDoc()->GetIDocumentUndoRedo().Repeat( context, nCount );
214  } catch (const css::uno::Exception &) {
215  TOOLS_WARN_EXCEPTION("sw.core", "SwEditShell::Repeat()");
216  }
217 
218  EndAllAction();
219 }
220 
221 static void lcl_SelectSdrMarkList( SwEditShell* pShell,
222  const SdrMarkList* pSdrMarkList )
223 {
224  OSL_ENSURE( pShell != nullptr, "need shell!" );
225  OSL_ENSURE( pSdrMarkList != nullptr, "need mark list" );
226 
227  SwFEShell* pFEShell = dynamic_cast<SwFEShell*>( pShell );
228  if( !pFEShell )
229  return;
230 
231  bool bFirst = true;
232  for( size_t i = 0; i < pSdrMarkList->GetMarkCount(); ++i )
233  {
234  SdrObject *pObj = pSdrMarkList->GetMark( i )->GetMarkedSdrObj();
235  if( pObj )
236  {
237  pFEShell->SelectObj( Point(), bFirst ? 0 : SW_ADD_SELECT, pObj );
238  bFirst = false;
239  }
240  }
241 
242  // the old implementation would always unselect
243  // objects, even if no new ones were selected. If this
244  // is a problem, we need to re-work this a little.
245  OSL_ENSURE( pSdrMarkList->GetMarkCount() != 0, "empty mark list" );
246 }
247 
248 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void KillPams()
Definition: crsrsh.cxx:1022
size_t GetMarkCount() const
virtual const tools::Rectangle & GetCurrentBoundRect() const
void SaveTableBoxContent(const SwPosition *pPos=nullptr)
Definition: trvltbl.cxx:870
bool GoNextCursor()
go to the next SSelection
Definition: crsrsh.cxx:1357
SwUndoId
Definition: swundo.hxx:29
SwDocShell * GetDocShell()
Definition: doc.hxx:1351
void ClearTableBoxContent()
Definition: trvltbl.cxx:909
bool GetLastUndoInfo(OUString *const o_pStr, SwUndoId *const o_pId, const SwView *pView=nullptr) const
Definition: edws.cxx:237
css::uno::Reference< css::frame::XModel3 > GetModel() const
virtual bool Redo()=0
Execute Redo.
virtual bool UndoWithOffset(size_t nUndoOffset)=0
Zero offset means undoing the top undo action.
SdrMark * GetMark(size_t nNum) const
IDocumentUndoRedo & GetIDocumentUndoRedo()
Definition: doc.cxx:144
void EndAllAction()
Definition: edws.cxx:97
void Redo(sal_uInt16 const nCount=1)
Definition: edundo.cxx:153
constexpr TypedWhichId< SwDrawFrameFormat > RES_DRAWFRMFMT(159)
virtual bool GetFirstRedoInfo(OUString *const o_pStr, SwUndoId *const o_pId, const SwView *pView=nullptr) const =0
Get Id and comment of first Redo action.
SwDoc * GetDoc() const
Definition: viewsh.hxx:282
void InvalidateContent() const
Definition: pagefrm.hxx:380
virtual void CompressRedlines()=0
virtual bool Repeat(::sw::RepeatContext &rContext, sal_uInt16 const nRepeatCnt)=0
Repeat the last Undo action.
SdrObject * GetMarkedSdrObj() const
Style of a layout element.
Definition: frmfmt.hxx:59
#define TOOLS_WARN_EXCEPTION(area, stream)
int i
uno_Any a
void Push()
store a copy of the current cursor on the cursor stack
Definition: crsrsh.cxx:2248
void Undo(sal_uInt16 const nCount=1, sal_uInt16 nOffset=0)
Definition: edundo.cxx:96
void Repeat(sal_uInt16 const nCount)
Definition: edundo.cxx:205
void SetMark()
Definition: crsrsh.hxx:884
#define SW_ADD_SELECT
Definition: fesh.hxx:166
void InvalidateFlyLayout() const
Validate, invalidate and query the Page status Layout/Content and Fly/non-Fly respectively are inspec...
Definition: pagefrm.hxx:364
A page of the document layout.
Definition: pagefrm.hxx:57
bool CursorsLocked() const
is it forbidden to modify cursors via API calls?
Definition: edundo.cxx:42
constexpr Point Center() const
sal_uInt16 Which() const
for Querying of Writer-functions.
Definition: format.hxx:82
IDocumentUndoRedo const & GetIDocumentUndoRedo() const
Provides access to the document undo/redo interface.
Definition: viewsh.cxx:2795
IDocumentRedlineAccess const & getIDocumentRedlineAccess() const
Definition: doc.cxx:335
SwCursor * GetCursor(bool bMakeTableCursor=true) const
Return pointer to the current shell cursor.
Definition: crsrsh.cxx:195
general base class for all free-flowing frames
Definition: flyfrm.hxx:78
void GetSelections(SwFrameFormat *&o_rpSelFormat, SdrMarkList *&o_rpMarkList)
Definition: UndoCore.hxx:104
SwPageFrame * FindPageFrameOfAnchor()
method to determine the page frame, on which the 'anchor' of the given anchored object is...
void HandleUndoRedoContext(::sw::UndoRedoContext &rContext)
set selections to those contained in the UndoRedoContext should only be called by sw::UndoManager! ...
Definition: edundo.cxx:47
virtual void SetRedlineFlags(RedlineFlags eMode)=0
Set a new redline mode.
static void lcl_SelectSdrMarkList(SwEditShell *pShell, const SdrMarkList *pSdrMarkList)
helper function to select all objects in an SdrMarkList; implementation: see below ...
Definition: edundo.cxx:221
bool SelectObj(const Point &rSelPt, sal_uInt8 nFlag=0, SdrObject *pObj=nullptr)
If an object has been given, exactly this object is selected (instead of searching over position)...
Definition: feshview.cxx:177
void ClearMark()
Definition: crsrsh.cxx:939
virtual RedlineFlags GetRedlineFlags() const =0
Query the currently set redline mode.
bool Pop(PopMode)
delete cursor
Definition: crsrsh.cxx:2270
void StartAllAction()
For all views of this document.
Definition: edws.cxx:86
SdrObject * FindSdrObject()
Definition: frmfmt.hxx:140