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/svdview.hxx>
21 #include <sal/log.hxx>
22 #include <tools/diagnose_ex.h>
23 
24 #include <com/sun/star/frame/XModel.hpp>
25 
26 #include <editsh.hxx>
27 #include <fesh.hxx>
28 #include <doc.hxx>
29 #include <IDocumentUndoRedo.hxx>
32 #include <pam.hxx>
33 #include <UndoCore.hxx>
34 #include <swundo.hxx>
35 #include <dcontact.hxx>
36 #include <flyfrm.hxx>
37 #include <frmfmt.hxx>
38 #include <viewimp.hxx>
39 #include <docsh.hxx>
40 #include <pagefrm.hxx>
41 
44 static void lcl_SelectSdrMarkList( SwEditShell* pShell,
45  const SdrMarkList* pSdrMarkList );
46 
48 {
49  return GetDoc()->GetDocShell()->GetModel()->hasControllersLocked();
50 }
51 
53 {
54  // do nothing if somebody has locked controllers!
55  if (CursorsLocked())
56  {
57  return;
58  }
59 
60  SwFrameFormat * pSelFormat(nullptr);
61  SdrMarkList * pMarkList(nullptr);
62  rContext.GetSelections(pSelFormat, pMarkList);
63 
64  if (pSelFormat) // select frame
65  {
66  if (RES_DRAWFRMFMT == pSelFormat->Which())
67  {
68  SdrObject* pSObj = pSelFormat->FindSdrObject();
69  static_cast<SwFEShell*>(this)->SelectObj(
70  pSObj->GetCurrentBoundRect().Center() );
71  }
72  else
73  {
74  Point aPt;
75  SwFlyFrame *const pFly =
76  static_cast<SwFlyFrameFormat*>(pSelFormat)->GetFrame(& aPt);
77  if (pFly)
78  {
79  // fdo#36681: Invalidate the content and layout to refresh
80  // the picture anchoring properly
81  SwPageFrame* pPageFrame = pFly->FindPageFrameOfAnchor();
82  pPageFrame->InvalidateFlyLayout();
83  pPageFrame->InvalidateContent();
84 
85  static_cast<SwFEShell*>(this)->SelectFlyFrame(*pFly);
86  }
87  }
88  }
89  else if (pMarkList)
90  {
91  lcl_SelectSdrMarkList( this, pMarkList );
92  }
93  else if (GetCursor()->GetNext() != GetCursor())
94  {
95  // current cursor is the last one:
96  // go around the ring, to the first cursor
97  GoNextCursor();
98  }
99 }
100 
101 void SwEditShell::Undo(sal_uInt16 const nCount)
102 {
103  SET_CURR_SHELL( this );
104 
105  // current undo state was not saved
106  ::sw::UndoGuard const undoGuard(GetDoc()->GetIDocumentUndoRedo());
107  bool bRet = false;
108 
109  StartAllAction();
110  {
111  // Actually it should be enough to just work on the current Cursor, i.e. if there is a cycle
112  // cancel the latter temporarily, so that an insert during Undo is not done in all areas.
113  KillPams();
114  SetMark(); // Bound1 and Bound2 in the same Node
115  ClearMark();
116 
117  // Keep Cursor - so that we're able to set it at
118  // the same position for autoformat or autocorrection
119  SwUndoId nLastUndoId(SwUndoId::EMPTY);
120  GetLastUndoInfo(nullptr, & nLastUndoId);
121  const bool bRestoreCursor = nCount == 1
122  && ( SwUndoId::AUTOFORMAT == nLastUndoId
123  || SwUndoId::AUTOCORRECT == nLastUndoId
124  || SwUndoId::SETDEFTATTR == nLastUndoId );
125  Push();
126 
127  // Destroy stored TableBoxPtr. A dection is only permitted for the new "Box"!
129 
131 
132  try {
133  for (sal_uInt16 i = 0; i < nCount; ++i)
134  {
135  bRet = GetDoc()->GetIDocumentUndoRedo().Undo()
136  || bRet;
137  }
138  } catch (const css::uno::Exception &) {
139  css::uno::Any ex( cppu::getCaughtException() );
140  SAL_WARN("sw.core", "SwEditShell::Undo(): exception caught: " << exceptionToString(ex));
141  }
142 
143  if (bRestoreCursor)
144  { // fdo#39003 Pop does not touch the rest of the cursor ring
145  KillPams(); // so call this first to get rid of unwanted cursors
146  }
147  Pop(bRestoreCursor ? PopMode::DeleteCurrent : PopMode::DeleteStack);
148 
151 
152  // automatic detection of the new "Box"
154  }
155  EndAllAction();
156 }
157 
158 void SwEditShell::Redo(sal_uInt16 const nCount)
159 {
160  SET_CURR_SHELL( this );
161 
162  bool bRet = false;
163 
164  // undo state was not saved
165  ::sw::UndoGuard const undoGuard(GetDoc()->GetIDocumentUndoRedo());
166 
167  StartAllAction();
168 
169  {
170  // Actually it should be enough to just work on the current Cursor, i.e. if there is a cycle
171  // cancel the latter temporarily, so that an insert during Undo is not done in all areas.
172  KillPams();
173  SetMark(); // Bound1 and Bound2 in the same Node
174  ClearMark();
175 
176  SwUndoId nFirstRedoId(SwUndoId::EMPTY);
177  GetDoc()->GetIDocumentUndoRedo().GetFirstRedoInfo(nullptr, & nFirstRedoId);
178  const bool bRestoreCursor = nCount == 1 && SwUndoId::SETDEFTATTR == nFirstRedoId;
179  Push();
180 
181  // Destroy stored TableBoxPtr. A dection is only permitted for the new "Box"!
183 
185 
186  try {
187  for (sal_uInt16 i = 0; i < nCount; ++i)
188  {
189  bRet = GetDoc()->GetIDocumentUndoRedo().Redo()
190  || bRet;
191  }
192  } catch (const css::uno::Exception &) {
193  css::uno::Any ex( cppu::getCaughtException() );
194  SAL_WARN("sw.core", "SwEditShell::Redo(): exception caught: " << exceptionToString(ex));
195  }
196 
197  Pop(bRestoreCursor ? PopMode::DeleteCurrent : PopMode::DeleteStack);
198 
201 
202  // automatic detection of the new "Box"
204  }
205 
206  EndAllAction();
207 }
208 
209 void SwEditShell::Repeat(sal_uInt16 const nCount)
210 {
211  SET_CURR_SHELL( this );
212 
213  StartAllAction();
214 
215  try {
216  ::sw::RepeatContext context(*GetDoc(), *GetCursor());
217  GetDoc()->GetIDocumentUndoRedo().Repeat( context, nCount );
218  } catch (const css::uno::Exception &) {
219  css::uno::Any ex( cppu::getCaughtException() );
220  SAL_WARN("sw.core", "SwEditShell::Repeat(): exception caught: " << exceptionToString(ex));
221  }
222 
223  EndAllAction();
224 }
225 
226 static void lcl_SelectSdrMarkList( SwEditShell* pShell,
227  const SdrMarkList* pSdrMarkList )
228 {
229  OSL_ENSURE( pShell != nullptr, "need shell!" );
230  OSL_ENSURE( pSdrMarkList != nullptr, "need mark list" );
231 
232  if( dynamic_cast<const SwFEShell*>( pShell) != nullptr )
233  {
234  SwFEShell* pFEShell = static_cast<SwFEShell*>( pShell );
235  bool bFirst = true;
236  for( size_t i = 0; i < pSdrMarkList->GetMarkCount(); ++i )
237  {
238  SdrObject *pObj = pSdrMarkList->GetMark( i )->GetMarkedSdrObj();
239  if( pObj )
240  {
241  pFEShell->SelectObj( Point(), bFirst ? 0 : SW_ADD_SELECT, pObj );
242  bFirst = false;
243  }
244  }
245 
246  // the old implementation would always unselect
247  // objects, even if no new ones were selected. If this
248  // is a problem, we need to re-work this a little.
249  OSL_ENSURE( pSdrMarkList->GetMarkCount() != 0, "empty mark list" );
250  }
251 }
252 
253 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void KillPams()
Definition: crsrsh.cxx:1010
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:185
void SaveTableBoxContent(const SwPosition *pPos=nullptr)
Definition: trvltbl.cxx:873
bool GoNextCursor()
go to the next SSelection
Definition: crsrsh.cxx:1345
SwUndoId
Definition: swundo.hxx:29
SwDocShell * GetDocShell()
Definition: doc.hxx:1340
void ClearTableBoxContent()
Definition: trvltbl.cxx:912
bool GetLastUndoInfo(OUString *const o_pStr, SwUndoId *const o_pId, const SwView *pView=nullptr) const
Definition: edws.cxx:236
virtual bool Redo()=0
Execute Redo.
SdrMark * GetMark(size_t nNum) const
IDocumentUndoRedo & GetIDocumentUndoRedo()
Definition: doc.cxx:176
css::uno::Reference< css::frame::XModel > GetModel() const
void EndAllAction()
Definition: edws.cxx:96
void Redo(sal_uInt16 const nCount=1)
Definition: edundo.cxx:158
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.
Any SAL_CALL getCaughtException()
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
void Push()
store a copy of the current cursor on the cursor stack
Definition: crsrsh.cxx:2145
int i
void Repeat(sal_uInt16 const nCount)
Definition: edundo.cxx:209
void SetMark()
Definition: crsrsh.hxx:873
#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:47
OString exceptionToString(const css::uno::Any &caught)
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:2606
IDocumentRedlineAccess const & getIDocumentRedlineAccess() const
Definition: doc.cxx:367
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...
#define RES_DRAWFRMFMT
Definition: hintids.hxx:277
void HandleUndoRedoContext(::sw::UndoRedoContext &rContext)
set selections to those contained in the UndoRedoContext should only be called by sw::UndoManager! ...
Definition: edundo.cxx:52
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:226
bool SelectObj(const Point &rSelPt, sal_uInt8 nFlag=0, SdrObject *pObj=nullptr)
If an object as been given, exactly this object is selected (instead of searching over position)...
Definition: feshview.cxx:185
void ClearMark()
Definition: crsrsh.cxx:927
virtual RedlineFlags GetRedlineFlags() const =0
Query the currently set redline mode.
#define SAL_WARN(area, stream)
bool Pop(PopMode)
delete cursor
Definition: crsrsh.cxx:2167
void StartAllAction()
For all views of this document.
Definition: edws.cxx:85
void Undo(sal_uInt16 const nCount=1)
Definition: edundo.cxx:101
SdrObject * FindSdrObject()
Definition: frmfmt.hxx:137
Point Center() const