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 
38 #include <wrtsh.hxx>
39 
42 static void lcl_SelectSdrMarkList( SwEditShell* pShell,
43  const SdrMarkList* pSdrMarkList );
44 
46 {
47  return GetDoc()->GetDocShell()->GetModel()->hasControllersLocked();
48 }
49 
51 {
52  // do nothing if somebody has locked controllers!
53  if (CursorsLocked())
54  {
55  return;
56  }
57 
58  SwFrameFormat * pSelFormat(nullptr);
59  SdrMarkList * pMarkList(nullptr);
60  rContext.GetSelections(pSelFormat, pMarkList);
61 
62  if (pSelFormat) // select frame
63  {
64  if (RES_DRAWFRMFMT == pSelFormat->Which())
65  {
66  SdrObject* pSObj = pSelFormat->FindSdrObject();
67  static_cast<SwFEShell*>(this)->SelectObj(
68  pSObj->GetCurrentBoundRect().Center() );
69  }
70  else
71  {
72  Point aPt;
73  SwFlyFrame *const pFly =
74  static_cast<SwFlyFrameFormat*>(pSelFormat)->GetFrame(& aPt);
75  if (pFly)
76  {
77  // fdo#36681: Invalidate the content and layout to refresh
78  // the picture anchoring properly
79  SwPageFrame* pPageFrame = pFly->FindPageFrameOfAnchor();
80  pPageFrame->InvalidateFlyLayout();
81  pPageFrame->InvalidateContent();
82 
83  static_cast<SwFEShell*>(this)->SelectFlyFrame(*pFly);
84  }
85  }
86  }
87  else if (pMarkList)
88  {
89  lcl_SelectSdrMarkList( this, pMarkList );
90  }
91  else if (GetCursor()->GetNext() != GetCursor())
92  {
93  // current cursor is the last one:
94  // go around the ring, to the first cursor
95  GoNextCursor();
96  }
97 }
98 
99 void SwEditShell::Undo(sal_uInt16 const nCount)
100 {
102 
103  CurrShell aCurr( 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 detection 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 {
160 
161  CurrShell aCurr( this );
162 
163  bool bRet = false;
164 
165  // undo state was not saved
166  ::sw::UndoGuard const undoGuard(GetDoc()->GetIDocumentUndoRedo());
167 
168  StartAllAction();
169 
170  {
171  // Actually it should be enough to just work on the current Cursor, i.e. if there is a cycle
172  // cancel the latter temporarily, so that an insert during Undo is not done in all areas.
173  KillPams();
174  SetMark(); // Bound1 and Bound2 in the same Node
175  ClearMark();
176 
177  SwUndoId nFirstRedoId(SwUndoId::EMPTY);
178  GetDoc()->GetIDocumentUndoRedo().GetFirstRedoInfo(nullptr, & nFirstRedoId);
179  const bool bRestoreCursor = nCount == 1 && SwUndoId::SETDEFTATTR == nFirstRedoId;
180  Push();
181 
182  // Destroy stored TableBoxPtr. A detection is only permitted for the new "Box"!
184 
186 
187  try {
188  for (sal_uInt16 i = 0; i < nCount; ++i)
189  {
190  bRet = GetDoc()->GetIDocumentUndoRedo().Redo()
191  || bRet;
192  }
193  } catch (const css::uno::Exception &) {
194  TOOLS_WARN_EXCEPTION("sw.core", "SwEditShell::Redo()");
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  CurrShell aCurr( 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  TOOLS_WARN_EXCEPTION("sw.core", "SwEditShell::Repeat()");
220  }
221 
222  EndAllAction();
223 }
224 
225 static void lcl_SelectSdrMarkList( SwEditShell* pShell,
226  const SdrMarkList* pSdrMarkList )
227 {
228  OSL_ENSURE( pShell != nullptr, "need shell!" );
229  OSL_ENSURE( pSdrMarkList != nullptr, "need mark list" );
230 
231  SwFEShell* pFEShell = dynamic_cast<SwFEShell*>( pShell );
232  if( !pFEShell )
233  return;
234 
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 /* 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:1352
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.
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:157
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:281
virtual bool Undo()=0
Execute Undo.
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:2240
void Repeat(sal_uInt16 const nCount)
Definition: edundo.cxx:209
void SetMark()
Definition: crsrsh.hxx:869
#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: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:45
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:2679
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:50
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:225
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:2262
void StartAllAction()
For all views of this document.
Definition: edws.cxx:86
void Undo(sal_uInt16 const nCount=1)
Definition: edundo.cxx:99
SdrObject * FindSdrObject()
Definition: frmfmt.hxx:140