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