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