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 <com/sun/star/frame/XModel.hpp>
24 
25 #include <editsh.hxx>
26 #include <fesh.hxx>
27 #include <doc.hxx>
28 #include <IDocumentUndoRedo.hxx>
30 #include <pam.hxx>
31 #include <UndoCore.hxx>
32 #include <swundo.hxx>
33 #include <flyfrm.hxx>
34 #include <frmfmt.hxx>
35 #include <docsh.hxx>
36 #include <pagefrm.hxx>
37 
40 static void lcl_SelectSdrMarkList( SwEditShell* pShell,
41  const SdrMarkList* pSdrMarkList );
42 
44 {
45  return GetDoc()->GetDocShell()->GetModel()->hasControllersLocked();
46 }
47 
49 {
50  // do nothing if somebody has locked controllers!
51  if (CursorsLocked())
52  {
53  return;
54  }
55 
56  SwFrameFormat * pSelFormat(nullptr);
57  SdrMarkList * pMarkList(nullptr);
58  rContext.GetSelections(pSelFormat, pMarkList);
59 
60  if (pSelFormat) // select frame
61  {
62  if (RES_DRAWFRMFMT == pSelFormat->Which())
63  {
64  SdrObject* pSObj = pSelFormat->FindSdrObject();
65  static_cast<SwFEShell*>(this)->SelectObj(
66  pSObj->GetCurrentBoundRect().Center() );
67  }
68  else
69  {
70  Point aPt;
71  SwFlyFrame *const pFly =
72  static_cast<SwFlyFrameFormat*>(pSelFormat)->GetFrame(& aPt);
73  if (pFly)
74  {
75  // fdo#36681: Invalidate the content and layout to refresh
76  // the picture anchoring properly
77  SwPageFrame* pPageFrame = pFly->FindPageFrameOfAnchor();
78  pPageFrame->InvalidateFlyLayout();
79  pPageFrame->InvalidateContent();
80 
81  static_cast<SwFEShell*>(this)->SelectFlyFrame(*pFly);
82  }
83  }
84  }
85  else if (pMarkList)
86  {
87  lcl_SelectSdrMarkList( this, pMarkList );
88  }
89  else if (GetCursor()->GetNext() != GetCursor())
90  {
91  // current cursor is the last one:
92  // go around the ring, to the first cursor
93  GoNextCursor();
94  }
95 }
96 
97 void SwEditShell::Undo(sal_uInt16 const nCount)
98 {
99  SET_CURR_SHELL( this );
100 
101  // current undo state was not saved
102  ::sw::UndoGuard const undoGuard(GetDoc()->GetIDocumentUndoRedo());
103  bool bRet = false;
104 
105  StartAllAction();
106  {
107  // Actually it should be enough to just work on the current Cursor, i.e. if there is a cycle
108  // cancel the latter temporarily, so that an insert during Undo is not done in all areas.
109  KillPams();
110  SetMark(); // Bound1 and Bound2 in the same Node
111  ClearMark();
112 
113  // Keep Cursor - so that we're able to set it at
114  // the same position for autoformat or autocorrection
115  SwUndoId nLastUndoId(SwUndoId::EMPTY);
116  GetLastUndoInfo(nullptr, & nLastUndoId);
117  const bool bRestoreCursor = nCount == 1
118  && ( SwUndoId::AUTOFORMAT == nLastUndoId
119  || SwUndoId::AUTOCORRECT == nLastUndoId
120  || SwUndoId::SETDEFTATTR == nLastUndoId );
121  Push();
122 
123  // Destroy stored TableBoxPtr. A detection is only permitted for the new "Box"!
125 
127 
128  try {
129  for (sal_uInt16 i = 0; i < nCount; ++i)
130  {
131  bRet = GetDoc()->GetIDocumentUndoRedo().Undo()
132  || 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 {
155  SET_CURR_SHELL( this );
156 
157  bool bRet = false;
158 
159  // undo state was not saved
160  ::sw::UndoGuard const undoGuard(GetDoc()->GetIDocumentUndoRedo());
161 
162  StartAllAction();
163 
164  {
165  // Actually it should be enough to just work on the current Cursor, i.e. if there is a cycle
166  // cancel the latter temporarily, so that an insert during Undo is not done in all areas.
167  KillPams();
168  SetMark(); // Bound1 and Bound2 in the same Node
169  ClearMark();
170 
171  SwUndoId nFirstRedoId(SwUndoId::EMPTY);
172  GetDoc()->GetIDocumentUndoRedo().GetFirstRedoInfo(nullptr, & nFirstRedoId);
173  const bool bRestoreCursor = nCount == 1 && SwUndoId::SETDEFTATTR == nFirstRedoId;
174  Push();
175 
176  // Destroy stored TableBoxPtr. A detection is only permitted for the new "Box"!
178 
180 
181  try {
182  for (sal_uInt16 i = 0; i < nCount; ++i)
183  {
184  bRet = GetDoc()->GetIDocumentUndoRedo().Redo()
185  || bRet;
186  }
187  } catch (const css::uno::Exception &) {
188  TOOLS_WARN_EXCEPTION("sw.core", "SwEditShell::Redo()");
189  }
190 
191  Pop(bRestoreCursor ? PopMode::DeleteCurrent : PopMode::DeleteStack);
192 
195 
196  // automatic detection of the new "Box"
198  }
199 
200  EndAllAction();
201 }
202 
203 void SwEditShell::Repeat(sal_uInt16 const nCount)
204 {
205  SET_CURR_SHELL( this );
206 
207  StartAllAction();
208 
209  try {
210  ::sw::RepeatContext context(*GetDoc(), *GetCursor());
211  GetDoc()->GetIDocumentUndoRedo().Repeat( context, nCount );
212  } catch (const css::uno::Exception &) {
213  TOOLS_WARN_EXCEPTION("sw.core", "SwEditShell::Repeat()");
214  }
215 
216  EndAllAction();
217 }
218 
219 static void lcl_SelectSdrMarkList( SwEditShell* pShell,
220  const SdrMarkList* pSdrMarkList )
221 {
222  OSL_ENSURE( pShell != nullptr, "need shell!" );
223  OSL_ENSURE( pSdrMarkList != nullptr, "need mark list" );
224 
225  if( dynamic_cast<const SwFEShell*>( pShell) != nullptr )
226  {
227  SwFEShell* pFEShell = static_cast<SwFEShell*>( pShell );
228  bool bFirst = true;
229  for( size_t i = 0; i < pSdrMarkList->GetMarkCount(); ++i )
230  {
231  SdrObject *pObj = pSdrMarkList->GetMark( i )->GetMarkedSdrObj();
232  if( pObj )
233  {
234  pFEShell->SelectObj( Point(), bFirst ? 0 : SW_ADD_SELECT, pObj );
235  bFirst = false;
236  }
237  }
238 
239  // the old implementation would always unselect
240  // objects, even if no new ones were selected. If this
241  // is a problem, we need to re-work this a little.
242  OSL_ENSURE( pSdrMarkList->GetMarkCount() != 0, "empty mark list" );
243  }
244 }
245 
246 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void KillPams()
Definition: crsrsh.cxx:1019
size_t GetMarkCount() const
virtual const tools::Rectangle & GetCurrentBoundRect() const
SwPaM * GetCursor(bool bMakeTableCursor=true) const
Return pointer to the current shell cursor.
Definition: crsrsh.cxx:188
void SaveTableBoxContent(const SwPosition *pPos=nullptr)
Definition: trvltbl.cxx:868
bool GoNextCursor()
go to the next SSelection
Definition: crsrsh.cxx:1354
SwUndoId
Definition: swundo.hxx:29
SwDocShell * GetDocShell()
Definition: doc.hxx:1350
void ClearTableBoxContent()
Definition: trvltbl.cxx:907
bool GetLastUndoInfo(OUString *const o_pStr, SwUndoId *const o_pId, const SwView *pView=nullptr) const
Definition: edws.cxx:237
virtual bool Redo()=0
Execute Redo.
SdrMark * GetMark(size_t nNum) const
IDocumentUndoRedo & GetIDocumentUndoRedo()
Definition: doc.cxx:143
css::uno::Reference< css::frame::XModel > GetModel() const
void EndAllAction()
Definition: edws.cxx:97
void Redo(sal_uInt16 const nCount=1)
Definition: edundo.cxx:153
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:284
virtual bool Undo()=0
Execute Undo.
void InvalidateContent() const
Definition: pagefrm.hxx:363
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:57
#define TOOLS_WARN_EXCEPTION(area, stream)
int i
void Push()
store a copy of the current cursor on the cursor stack
Definition: crsrsh.cxx:2235
void Repeat(sal_uInt16 const nCount)
Definition: edundo.cxx:203
void SetMark()
Definition: crsrsh.hxx:879
#define SET_CURR_SHELL(shell)
Definition: swtypes.hxx:101
#define SW_ADD_SELECT
Definition: fesh.hxx:165
void InvalidateFlyLayout() const
Validate, invalidate and query the Page status Layout/Content and Fly/non-Fly respectively are inspec...
Definition: pagefrm.hxx:347
A page of the document layout.
Definition: pagefrm.hxx:40
bool CursorsLocked() const
is it forbidden to modify cursors via API calls?
Definition: edundo.cxx:43
sal_uInt16 Which() const
for Querying of Writer-functions.
Definition: format.hxx:78
IDocumentUndoRedo const & GetIDocumentUndoRedo() const
Provides access to the document undo/redo interface.
Definition: viewsh.cxx:2603
IDocumentRedlineAccess const & getIDocumentRedlineAccess() const
Definition: doc.cxx:334
general base class for all free-flowing frames
Definition: flyfrm.hxx:60
void GetSelections(SwFrameFormat *&o_rpSelFormat, SdrMarkList *&o_rpMarkList)
Definition: UndoCore.hxx:103
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:48
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:219
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:176
constexpr TypedWhichId< SwDrawFrameFormat > RES_DRAWFRMFMT(157)
void ClearMark()
Definition: crsrsh.cxx:936
virtual RedlineFlags GetRedlineFlags() const =0
Query the currently set redline mode.
bool Pop(PopMode)
delete cursor
Definition: crsrsh.cxx:2257
void StartAllAction()
For all views of this document.
Definition: edws.cxx:86
void Undo(sal_uInt16 const nCount=1)
Definition: edundo.cxx:97
SdrObject * FindSdrObject()
Definition: frmfmt.hxx:137
Point Center() const