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  TOOLS_WARN_EXCEPTION("sw.core", "SwEditShell::Undo()");
140  }
141 
142  if (bRestoreCursor)
143  { // fdo#39003 Pop does not touch the rest of the cursor ring
144  KillPams(); // so call this first to get rid of unwanted cursors
145  }
146  Pop(bRestoreCursor ? PopMode::DeleteCurrent : PopMode::DeleteStack);
147 
150 
151  // automatic detection of the new "Box"
153  }
154  EndAllAction();
155 }
156 
157 void SwEditShell::Redo(sal_uInt16 const nCount)
158 {
159  SET_CURR_SHELL( this );
160 
161  bool bRet = false;
162 
163  // undo state was not saved
164  ::sw::UndoGuard const undoGuard(GetDoc()->GetIDocumentUndoRedo());
165 
166  StartAllAction();
167 
168  {
169  // Actually it should be enough to just work on the current Cursor, i.e. if there is a cycle
170  // cancel the latter temporarily, so that an insert during Undo is not done in all areas.
171  KillPams();
172  SetMark(); // Bound1 and Bound2 in the same Node
173  ClearMark();
174 
175  SwUndoId nFirstRedoId(SwUndoId::EMPTY);
176  GetDoc()->GetIDocumentUndoRedo().GetFirstRedoInfo(nullptr, & nFirstRedoId);
177  const bool bRestoreCursor = nCount == 1 && SwUndoId::SETDEFTATTR == nFirstRedoId;
178  Push();
179 
180  // Destroy stored TableBoxPtr. A dection is only permitted for the new "Box"!
182 
184 
185  try {
186  for (sal_uInt16 i = 0; i < nCount; ++i)
187  {
188  bRet = GetDoc()->GetIDocumentUndoRedo().Redo()
189  || bRet;
190  }
191  } catch (const css::uno::Exception &) {
192  TOOLS_WARN_EXCEPTION("sw.core", "SwEditShell::Redo()");
193  }
194 
195  Pop(bRestoreCursor ? PopMode::DeleteCurrent : PopMode::DeleteStack);
196 
199 
200  // automatic detection of the new "Box"
202  }
203 
204  EndAllAction();
205 }
206 
207 void SwEditShell::Repeat(sal_uInt16 const nCount)
208 {
209  SET_CURR_SHELL( this );
210 
211  StartAllAction();
212 
213  try {
214  ::sw::RepeatContext context(*GetDoc(), *GetCursor());
215  GetDoc()->GetIDocumentUndoRedo().Repeat( context, nCount );
216  } catch (const css::uno::Exception &) {
217  TOOLS_WARN_EXCEPTION("sw.core", "SwEditShell::Repeat()");
218  }
219 
220  EndAllAction();
221 }
222 
223 static void lcl_SelectSdrMarkList( SwEditShell* pShell,
224  const SdrMarkList* pSdrMarkList )
225 {
226  OSL_ENSURE( pShell != nullptr, "need shell!" );
227  OSL_ENSURE( pSdrMarkList != nullptr, "need mark list" );
228 
229  if( dynamic_cast<const SwFEShell*>( pShell) != nullptr )
230  {
231  SwFEShell* pFEShell = static_cast<SwFEShell*>( pShell );
232  bool bFirst = true;
233  for( size_t i = 0; i < pSdrMarkList->GetMarkCount(); ++i )
234  {
235  SdrObject *pObj = pSdrMarkList->GetMark( i )->GetMarkedSdrObj();
236  if( pObj )
237  {
238  pFEShell->SelectObj( Point(), bFirst ? 0 : SW_ADD_SELECT, pObj );
239  bFirst = false;
240  }
241  }
242 
243  // the old implementation would always unselect
244  // objects, even if no new ones were selected. If this
245  // is a problem, we need to re-work this a little.
246  OSL_ENSURE( pSdrMarkList->GetMarkCount() != 0, "empty mark list" );
247  }
248 }
249 
250 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void KillPams()
Definition: crsrsh.cxx:1009
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:184
void SaveTableBoxContent(const SwPosition *pPos=nullptr)
Definition: trvltbl.cxx:868
bool GoNextCursor()
go to the next SSelection
Definition: crsrsh.cxx:1344
SwUndoId
Definition: swundo.hxx:29
SwDocShell * GetDocShell()
Definition: doc.hxx:1342
void ClearTableBoxContent()
Definition: trvltbl.cxx:907
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:143
css::uno::Reference< css::frame::XModel > GetModel() const
void EndAllAction()
Definition: edws.cxx:96
void Redo(sal_uInt16 const nCount=1)
Definition: edundo.cxx:157
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)
void Push()
store a copy of the current cursor on the cursor stack
Definition: crsrsh.cxx:2221
int i
void Repeat(sal_uInt16 const nCount)
Definition: edundo.cxx:207
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:47
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:2609
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...
#define RES_DRAWFRMFMT
Definition: hintids.hxx:279
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:223
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:185
void ClearMark()
Definition: crsrsh.cxx:926
virtual RedlineFlags GetRedlineFlags() const =0
Query the currently set redline mode.
bool Pop(PopMode)
delete cursor
Definition: crsrsh.cxx:2243
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