LibreOffice Module sw (master)  1
undobj.hxx
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 #ifndef INCLUDED_SW_INC_UNDOBJ_HXX
20 #define INCLUDED_SW_INC_UNDOBJ_HXX
21 
22 #include <vector>
23 #include <memory>
24 
25 #include <svl/undo.hxx>
26 #include <tools/solar.h>
27 #include "SwRewriter.hxx"
28 #include "swundo.hxx"
29 #include <o3tl/typed_flags_set.hxx>
30 #include <o3tl/optional.hxx>
31 
32 class SwHistory;
33 class SwPaM;
34 struct SwPosition;
35 class SwDoc;
36 class SwTextFormatColl;
37 class SwFrameFormat;
38 class SwFormatAnchor;
39 class SwNodeIndex;
40 class SwNodeRange;
41 class SwRedlineData;
42 class SwRedlineSaveDatas;
43 enum class RedlineFlags;
44 enum class RndStdIds;
45 
46 namespace sw {
47  class UndoRedoContext;
48  class RepeatContext;
49 }
50 
51 class SwUndo
52  : public SfxUndoAction
53 {
54  SwUndoId const m_nId;
58 
59 protected:
61  mutable o3tl::optional<OUString> maComment;
62 
63  static void RemoveIdxFromSection( SwDoc&, sal_uLong nSttIdx, const sal_uLong* pEndIdx = nullptr );
64  static void RemoveIdxFromRange( SwPaM& rPam, bool bMoveNext );
65  static void RemoveIdxRel( sal_uLong, const SwPosition& );
66 
67  static bool CanRedlineGroup( SwRedlineSaveDatas& rCurr,
68  const SwRedlineSaveDatas& rCheck,
69  bool bCurrIsEnd );
70 
76  virtual SwRewriter GetRewriter() const;
77 
78  // the 4 methods that derived classes have to override
79  // base implementation does nothing
80  virtual void RepeatImpl( ::sw::RepeatContext & );
81 public: // should not be public, but ran into trouble in untbl.cxx
82  virtual void UndoImpl( ::sw::UndoRedoContext & ) = 0;
83  virtual void RedoImpl( ::sw::UndoRedoContext & ) = 0;
84 
85 private:
87  static ViewShellId CreateViewShellId(const SwDoc* pDoc);
88  // SfxUndoAction
89  virtual void Undo() override;
90  virtual void Redo() override;
91  virtual void UndoWithContext(SfxUndoContext &) override;
92  virtual void RedoWithContext(SfxUndoContext &) override;
93  virtual void Repeat(SfxRepeatTarget &) override;
94  virtual bool CanRepeat(SfxRepeatTarget &) const override;
95 
96 public:
97  SwUndo(SwUndoId const nId, const SwDoc* pDoc);
98  virtual ~SwUndo() override;
99 
100  SwUndoId GetId() const { return m_nId; }
101 
111  virtual OUString GetComment() const override;
112 
114  ViewShellId GetViewShellId() const override;
115 
116  // UndoObject remembers which mode was turned on.
117  // In Undo/Redo/Repeat this remembered mode is switched on.
119  void SetRedlineFlags( RedlineFlags eMode ) { m_nOrigRedlineFlags = eMode; }
120 
121  bool IsDelBox() const;
122 
123  // Save and set Redline data.
124  static bool FillSaveData( const SwPaM& rRange, SwRedlineSaveDatas& rSData,
125  bool bDelRange = true, bool bCopyNext = true );
126  static bool FillSaveDataForFormat( const SwPaM& , SwRedlineSaveDatas& );
127  static void SetSaveData( SwDoc& rDoc, SwRedlineSaveDatas& rSData );
128  static bool HasHiddenRedlines( const SwRedlineSaveDatas& rSData );
129  void IgnoreRepeat() { m_isRepeatIgnored = true; }
130 };
131 
132 enum class DelContentType : sal_uInt16
133 {
134  Ftn = 0x01,
135  Fly = 0x02,
136  Bkm = 0x08,
137  AllMask = 0x0b,
138  WriterfilterHack = 0x20,
139  ExcludeFlyAtStartEnd = 0x40,
140  CheckNoCntnt = 0x80,
141 };
142 namespace o3tl {
143  template<> struct typed_flags<DelContentType> : is_typed_flags<DelContentType, 0xeb> {};
144 }
145 
147 bool IsDestroyFrameAnchoredAtChar(SwPosition const & rAnchorPos,
148  SwPosition const & rStart, SwPosition const & rEnd,
149  DelContentType const nDelContentType = DelContentType::AllMask);
151 bool IsSelectFrameAnchoredAtPara(SwPosition const & rAnchorPos,
152  SwPosition const & rStart, SwPosition const & rEnd,
153  DelContentType const nDelContentType = DelContentType::AllMask);
154 
155 // This class has to be inherited into an Undo-object if it saves content
156 // for Redo/Undo...
158 {
159 protected:
160 
161  std::unique_ptr<SwHistory> m_pHistory;
162 
163  // Needed for deletion of content. For Redo content is moved into the
164  // UndoNodesArray. These methods always create a new node to insert
165  // content. So the attributes do not get expanded.
166  // MoveTo: moves from the NodesArray into the UndoNodesArray.
167  // MoveFrom: moves from the UndoNodesArray into the NodesArray.
168  static void MoveToUndoNds( SwPaM& rPam,
169  SwNodeIndex* pNodeIdx,
170  sal_uLong* pEndNdIdx = nullptr );
171  static void MoveFromUndoNds( SwDoc& rDoc, sal_uLong nNodeIdx,
172  SwPosition& rInsPos,
173  const sal_uLong* pEndNdIdx = nullptr,
174  bool bForceCreateFrames = false);
175 
176  // These two methods move the SPoint back/forth from PaM. With it
177  // a range can be spanned for Undo/Redo. (In this case the SPoint
178  // is before the manipulated range!!)
179  // The flag indicates if there is content before the SPoint.
180  static bool MovePtBackward( SwPaM& rPam );
181  static void MovePtForward( SwPaM& rPam, bool bMvBkwrd );
182 
183  // Before moving stuff into UndoNodes-Array care has to be taken that
184  // the content-bearing attributes are removed from the nodes-array.
185  void DelContentIndex( const SwPosition& pMark, const SwPosition& pPoint,
186  DelContentType nDelContentType = DelContentType::AllMask );
187 
188 public:
190  ~SwUndoSaveContent() COVERITY_NOEXCEPT_FALSE;
191 };
192 
193 // Save a complete section in nodes-array.
195 {
196  std::unique_ptr<SwNodeIndex> m_pMovedStart;
197  std::unique_ptr<SwRedlineSaveDatas> m_pRedlineSaveData;
198  sal_uLong m_nMoveLen; // Index into UndoNodes-Array.
200 
201 protected:
202  SwNodeIndex* GetMvSttIdx() const { return m_pMovedStart.get(); }
203  sal_uLong GetMvNodeCnt() const { return m_nMoveLen; }
204 
205 public:
208 
209  void SaveSection( const SwNodeIndex& rSttIdx );
210  void SaveSection(const SwNodeRange& rRange, bool bExpandNodes = true);
211  void RestoreSection( SwDoc* pDoc, SwNodeIndex* pIdx, sal_uInt16 nSectType );
212  void RestoreSection(SwDoc* pDoc, const SwNodeIndex& rInsPos, bool bForceCreateFrames = false);
213 
214  const SwHistory* GetHistory() const { return m_pHistory.get(); }
215  SwHistory* GetHistory() { return m_pHistory.get(); }
216 };
217 
218 // This class saves the PaM as sal_uInt16's and is able to restore it
219 // into a PaM.
220 class SwUndRng
221 {
222 public:
223  sal_uLong m_nSttNode, m_nEndNode;
224  sal_Int32 m_nSttContent, m_nEndContent;
225 
226  SwUndRng();
227  SwUndRng( const SwPaM& );
228 
229  void SetValues( const SwPaM& rPam );
230  void SetPaM( SwPaM&, bool bCorrToContent = false ) const;
231  SwPaM & AddUndoRedoPaM(
232  ::sw::UndoRedoContext &, bool const bCorrToContent = false) const;
233 };
234 
235 class SwUndoInsLayFormat;
236 
237 namespace sw {
238 
239 std::unique_ptr<std::vector<SwFrameFormat*>>
240 GetFlysAnchoredAt(SwDoc & rDoc, sal_uLong nSttNode);
241 
242 }
243 
244 // base class for insertion of Document, Glossaries and Copy
245 class SwUndoInserts : public SwUndo, public SwUndRng, private SwUndoSaveContent
246 {
248  std::unique_ptr<std::vector<SwFrameFormat*>> m_pFrameFormats;
249  std::vector< std::shared_ptr<SwUndoInsLayFormat> > m_FlyUndos;
250  std::unique_ptr<SwRedlineData> m_pRedlineData;
252 protected:
255  std::unique_ptr<SwNodeIndex> m_pUndoNodeIndex;
256  sal_uInt16 m_nSetPos; // Start in the history list.
257 
258  SwUndoInserts( SwUndoId nUndoId, const SwPaM& );
259 public:
260  virtual ~SwUndoInserts() override;
261 
262  virtual void UndoImpl( ::sw::UndoRedoContext & ) override;
263  virtual void RedoImpl( ::sw::UndoRedoContext & ) override;
264  virtual void RepeatImpl( ::sw::RepeatContext & ) override;
265 
266  // Set destination range after reading.
267  void SetInsertRange( const SwPaM&, bool bScanFlys = true,
268  bool bSttWasTextNd = true );
269 
270  static bool IsCreateUndoForNewFly(SwFormatAnchor const& rAnchor,
271  sal_uLong const nStartNode, sal_uLong const nEndNode);
272  std::vector<SwFrameFormat*> * GetFlysAnchoredAt() { return m_pFrameFormats.get(); }
273 };
274 
275 class SwUndoInsDoc final : public SwUndoInserts
276 {
277 public:
278  SwUndoInsDoc( const SwPaM& );
279 };
280 
281 class SwUndoCpyDoc final : public SwUndoInserts
282 {
283 public:
284  SwUndoCpyDoc( const SwPaM& );
285 };
286 
287 class SwUndoFlyBase : public SwUndo, private SwUndoSaveSection
288 {
289 protected:
290  SwFrameFormat* m_pFrameFormat; // The saved FlyFormat.
292  sal_Int32 m_nContentPos; // Page at/in paragraph.
294  bool m_bDelFormat; // Delete saved format.
295 
296  void InsFly(::sw::UndoRedoContext & rContext, bool bShowSel = true);
297  void DelFly( SwDoc* );
298 
299  SwUndoFlyBase( SwFrameFormat* pFormat, SwUndoId nUndoId );
300 
303 
304 public:
305  virtual ~SwUndoFlyBase() override;
306 
307 };
308 
310 {
311  sal_uLong mnCursorSaveIndexPara; // Cursor position
312  sal_Int32 const mnCursorSaveIndexPos; // for undo
313 public:
314  SwUndoInsLayFormat( SwFrameFormat* pFormat, sal_uLong nNodeIdx, sal_Int32 nCntIdx );
315 
316  virtual ~SwUndoInsLayFormat() override;
317 
318  virtual void UndoImpl( ::sw::UndoRedoContext & ) override;
319  virtual void RedoImpl( ::sw::UndoRedoContext & ) override;
320  virtual void RepeatImpl( ::sw::RepeatContext & ) override;
321 
322  virtual OUString GetComment() const override;
323 
324 };
325 
326 class SwUndoDelLayFormat final : public SwUndoFlyBase
327 {
329 public:
330  SwUndoDelLayFormat( SwFrameFormat* pFormat );
331 
332  virtual void UndoImpl( ::sw::UndoRedoContext & ) override;
333  virtual void RedoImpl( ::sw::UndoRedoContext & ) override;
334 
335  void RedoForRollback();
336 
337  void ChgShowSel( bool bNew ) { m_bShowSelFrame = bNew; }
338 
339  virtual SwRewriter GetRewriter() const override;
340 
341 };
342 
343 #endif
344 
345 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::vector< std::shared_ptr< SwUndoInsLayFormat > > m_FlyUndos
Definition: undobj.hxx:249
sal_uLong m_nStartPos
Definition: undobj.hxx:199
std::unique_ptr< SwRedlineSaveDatas > m_pRedlineSaveData
Definition: undobj.hxx:197
Represents the style of a paragraph.
Definition: fmtcol.hxx:55
Marks a position in the document model.
Definition: pam.hxx:35
std::unique_ptr< SwHistory > m_pHistory
Definition: undobj.hxx:161
std::unique_ptr< SwRedlineData > m_pRedlineData
Definition: undobj.hxx:250
SwUndoId
Definition: swundo.hxx:29
static bool FillSaveDataForFormat(const SwPaM &, SwRedlineSaveDatas &)
Definition: undobj.cxx:1409
virtual void Repeat(SfxRepeatTarget &) override
Definition: undobj.cxx:244
sal_uIntPtr sal_uLong
std::vector< SwFrameFormat * > * GetFlysAnchoredAt()
Definition: undobj.hxx:272
static ViewShellId CreateViewShellId(const SwDoc *pDoc)
Try to obtain the view shell ID of the current view.
Definition: undobj.cxx:166
Definition: doc.hxx:185
~SwUndoSaveContent() COVERITY_NOEXCEPT_FALSE
Definition: undobj.cxx:700
void SetRedlineFlags(RedlineFlags eMode)
Definition: undobj.hxx:119
sal_Int32 m_nContentPos
Definition: undobj.hxx:292
const SwHistory * GetHistory() const
Definition: undobj.hxx:214
Dialog to specify the properties of date form field.
std::unique_ptr< std::vector< SwFrameFormat * > > m_pFrameFormats
Definition: undobj.hxx:248
sal_Int32 const mnCursorSaveIndexPos
Definition: undobj.hxx:312
static void RemoveIdxFromSection(SwDoc &, sal_uLong nSttIdx, const sal_uLong *pEndIdx=nullptr)
Definition: undobj.cxx:113
void ChgShowSel(bool bNew)
Definition: undobj.hxx:337
SwUndoId GetId() const
Definition: undobj.hxx:100
virtual void RepeatImpl(::sw::RepeatContext &)
Definition: undobj.cxx:264
SwUndoId const m_nId
Definition: undobj.hxx:54
sal_uLong m_nMoveLen
Definition: undobj.hxx:198
RedlineFlags m_nOrigRedlineFlags
Definition: undobj.hxx:55
virtual SwRewriter GetRewriter() const
Returns the rewriter for this object.
Definition: undobj.cxx:690
static bool CanRedlineGroup(SwRedlineSaveDatas &rCurr, const SwRedlineSaveDatas &rCheck, bool bCurrIsEnd)
Definition: undobj.cxx:1466
SwHistory * GetHistory()
Definition: undobj.hxx:215
static void MoveToUndoNds(SwPaM &rPam, SwNodeIndex *pNodeIdx, sal_uLong *pEndNdIdx=nullptr)
Definition: undobj.cxx:712
static void RemoveIdxFromRange(SwPaM &rPam, bool bMoveNext)
Definition: undobj.cxx:123
virtual bool CanRepeat(SfxRepeatTarget &) const override
Definition: undobj.cxx:256
RedlineFlags GetRedlineFlags() const
Definition: undobj.hxx:118
sal_uLong m_nNodePagePos
Definition: undobj.hxx:291
SwNodeIndex * GetMvSttIdx() const
Definition: undobj.hxx:301
static void MovePtForward(SwPaM &rPam, bool bMvBkwrd)
Definition: undobj.cxx:821
PaM is Point and Mark: a selection of the document model.
Definition: pam.hxx:136
virtual void UndoImpl(::sw::UndoRedoContext &)=0
Style of a layout element.
Definition: frmfmt.hxx:57
static bool HasHiddenRedlines(const SwRedlineSaveDatas &rSData)
Definition: undobj.cxx:1458
SwFrameFormat * m_pFrameFormat
Definition: undobj.hxx:290
void DelContentIndex(const SwPosition &pMark, const SwPosition &pPoint, DelContentType nDelContentType=DelContentType::AllMask)
Definition: undobj.cxx:846
static void SetSaveData(SwDoc &rDoc, SwRedlineSaveDatas &rSData)
Definition: undobj.cxx:1439
FlyAnchors.
Definition: fmtanchr.hxx:34
virtual void UndoWithContext(SfxUndoContext &) override
Definition: undobj.cxx:226
bool m_bDelFormat
Definition: undobj.hxx:294
RndStdIds m_nRndId
Definition: undobj.hxx:293
Marks a node in the document model.
Definition: ndindex.hxx:31
bool m_bCacheComment
Definition: undobj.hxx:60
static bool MovePtBackward(SwPaM &rPam)
Definition: undobj.cxx:808
sal_uLong m_nNodeDiff
Definition: undobj.hxx:253
virtual OUString GetComment() const override
Returns textual comment for this undo object.
Definition: undobj.cxx:656
std::unique_ptr< SwNodeIndex > m_pMovedStart
Definition: undobj.hxx:196
sal_uLong GetMvNodeCnt() const
Definition: undobj.hxx:302
sal_uLong m_nSttNode
Definition: undobj.hxx:223
sal_uLong GetMvNodeCnt() const
Definition: undobj.hxx:203
static void MoveFromUndoNds(SwDoc &rDoc, sal_uLong nNodeIdx, SwPosition &rInsPos, const sal_uLong *pEndNdIdx=nullptr, bool bForceCreateFrames=false)
Definition: undobj.cxx:749
DelContentType
Definition: undobj.hxx:132
ViewShellId const m_nViewShellId
Definition: undobj.hxx:56
virtual ~SwUndo() override
Definition: undobj.cxx:185
bool IsDestroyFrameAnchoredAtChar(SwPosition const &rAnchorPos, SwPosition const &rStart, SwPosition const &rEnd, DelContentType const nDelContentType=DelContentType::AllMask)
will DelContentIndex destroy a frame anchored at character at rAnchorPos?
Definition: undobj.cxx:1546
SwTextFormatColl * m_pTextFormatColl
Definition: undobj.hxx:247
SwUndo(SwUndoId const nId, const SwDoc *pDoc)
Definition: undobj.cxx:158
bool m_isRepeatIgnored
for multi-selection, only repeat 1st selection
Definition: undobj.hxx:57
virtual void Redo() override
Definition: undobj.cxx:221
void IgnoreRepeat()
Definition: undobj.hxx:129
SwNodeIndex * GetMvSttIdx() const
Definition: undobj.hxx:202
virtual void RedoWithContext(SfxUndoContext &) override
Definition: undobj.cxx:235
o3tl::optional< OUString > maComment
Definition: undobj.hxx:61
static void RemoveIdxRel(sal_uLong, const SwPosition &)
Definition: undobj.cxx:150
std::unique_ptr< std::vector< SwFrameFormat * > > GetFlysAnchoredAt(SwDoc &rDoc, sal_uLong const nSttNode)
Definition: untblk.cxx:37
virtual void RedoImpl(::sw::UndoRedoContext &)=0
bool IsSelectFrameAnchoredAtPara(SwPosition const &rAnchorPos, SwPosition const &rStart, SwPosition const &rEnd, DelContentType const nDelContentType=DelContentType::AllMask)
is a fly anchored at paragraph at rAnchorPos selected?
Definition: undobj.cxx:1583
virtual void Undo() override
Definition: undobj.cxx:216
bool m_bStartWasTextNode
Definition: undobj.hxx:251
RndStdIds
static bool FillSaveData(const SwPaM &rRange, SwRedlineSaveDatas &rSData, bool bDelRange=true, bool bCopyNext=true)
Definition: undobj.cxx:1374
ViewShellId GetViewShellId() const override
See SfxUndoAction::GetViewShellId().
Definition: undobj.cxx:685
sal_uInt16 m_nSetPos
Definition: undobj.hxx:256
bool IsDelBox() const
Definition: undobj.cxx:179
sal_Int32 m_nSttContent
Definition: undobj.hxx:224
sal_uLong mnCursorSaveIndexPara
Definition: undobj.hxx:311
std::unique_ptr< SwNodeIndex > m_pUndoNodeIndex
start of Content in UndoNodes for Redo
Definition: undobj.hxx:255