1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
19 #ifndef INCLUDED_SW_INC_UNDOBJ_HXX
20 #define INCLUDED_SW_INC_UNDOBJ_HXX
25 #include <svl/undo.hxx>
26 #include <tools/solar.h>
27 #include "SwRewriter.hxx"
29 #include <o3tl/typed_flags_set.hxx>
36 class SwTextFormatColl
;
42 class SwRedlineSaveDatas
;
43 enum class RedlineFlags
;
45 typedef struct _xmlTextWriter
* xmlTextWriterPtr
;
48 class UndoRedoContext
;
53 : public SfxUndoAction
56 RedlineFlags m_nOrigRedlineFlags
;
57 ViewShellId m_nViewShellId
;
58 bool m_isRepeatIgnored
; ///< for multi-selection, only repeat 1st selection
62 mutable std::optional
<OUString
> maComment
;
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
& );
68 static bool CanRedlineGroup( SwRedlineSaveDatas
& rCurr
,
69 const SwRedlineSaveDatas
& rCheck
,
73 Returns the rewriter for this object.
75 @return the rewriter for this object
77 virtual SwRewriter
GetRewriter() const;
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;
87 /// Try to obtain the view shell ID of the current view.
88 static ViewShellId
CreateViewShellId(const SwDoc
* pDoc
);
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
;
98 SwUndo(SwUndoId
const nId
, const SwDoc
* pDoc
);
99 virtual ~SwUndo() override
;
101 SwUndoId
GetId() const { return m_nId
; }
104 Returns textual comment for this undo object.
106 The textual comment is created from the resource string
107 corresponding to this object's ID. The rewriter of this object
108 is applied to the resource string to get the final comment.
110 @return textual comment for this undo object
112 virtual OUString
GetComment() const override
;
114 /// See SfxUndoAction::GetViewShellId().
115 ViewShellId
GetViewShellId() const override
;
117 // UndoObject remembers which mode was turned on.
118 // In Undo/Redo/Repeat this remembered mode is switched on.
119 RedlineFlags
GetRedlineFlags() const { return m_nOrigRedlineFlags
; }
120 void SetRedlineFlags( RedlineFlags eMode
) { m_nOrigRedlineFlags
= eMode
; }
122 bool IsDelBox() const;
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; }
133 enum class DelContentType
: sal_uInt16
140 WriterfilterHack
= 0x20,
141 ExcludeFlyAtStartEnd
= 0x40,
145 template<> struct typed_flags
<DelContentType
> : is_typed_flags
<DelContentType
, 0xfb> {};
148 /// will DelContentIndex destroy a frame anchored at character at rAnchorPos?
149 bool IsDestroyFrameAnchoredAtChar(SwPosition
const & rAnchorPos
,
150 SwPosition
const & rStart
, SwPosition
const & rEnd
,
151 DelContentType
const nDelContentType
= DelContentType::AllMask
);
152 /// is a fly anchored at paragraph at rAnchorPos selected?
153 bool IsSelectFrameAnchoredAtPara(SwPosition
const & rAnchorPos
,
154 SwPosition
const & rStart
, SwPosition
const & rEnd
,
155 DelContentType
const nDelContentType
= DelContentType::AllMask
);
156 /// check at-char and at-para flys in rDoc
157 bool IsFlySelectedByCursor(SwDoc
const & rDoc
,
158 SwPosition
const & rStart
, SwPosition
const & rEnd
);
160 // This class has to be inherited into an Undo-object if it saves content
162 class SwUndoSaveContent
166 std::unique_ptr
<SwHistory
> m_pHistory
;
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
,
178 const sal_uLong
* pEndNdIdx
= nullptr,
179 bool bForceCreateFrames
= false);
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
);
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
);
195 virtual ~SwUndoSaveContent() COVERITY_NOEXCEPT_FALSE
;
196 virtual void dumpAsXml(xmlTextWriterPtr pWriter
) const;
199 // Save a complete section in nodes-array.
200 class SwUndoSaveSection
: private SwUndoSaveContent
202 std::unique_ptr
<SwNodeIndex
> m_pMovedStart
;
203 std::unique_ptr
<SwRedlineSaveDatas
> m_pRedlineSaveData
;
204 sal_uLong m_nMoveLen
; // Index into UndoNodes-Array.
205 sal_uLong m_nStartPos
;
208 SwNodeIndex
* GetMvSttIdx() const { return m_pMovedStart
.get(); }
209 sal_uLong
GetMvNodeCnt() const { return m_nMoveLen
; }
213 ~SwUndoSaveSection();
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);
220 const SwHistory
* GetHistory() const { return m_pHistory
.get(); }
221 SwHistory
* GetHistory() { return m_pHistory
.get(); }
222 virtual void dumpAsXml(xmlTextWriterPtr pWriter
) const;
225 // This class saves the PaM as sal_uInt16's and is able to restore it
230 sal_uLong m_nSttNode
, m_nEndNode
;
231 sal_Int32 m_nSttContent
, m_nEndContent
;
234 SwUndRng( const SwPaM
& );
236 void SetValues( const SwPaM
& rPam
);
237 void SetPaM( SwPaM
&, bool bCorrToContent
= false ) const;
238 SwPaM
& AddUndoRedoPaM(
239 ::sw::UndoRedoContext
&, bool const bCorrToContent
= false) const;
242 class SwUndoInsLayFormat
;
246 std::unique_ptr
<std::vector
<SwFrameFormat
*>>
247 GetFlysAnchoredAt(SwDoc
& rDoc
, sal_uLong nSttNode
);
251 // base class for insertion of Document, Glossaries and Copy
252 class SwUndoInserts
: public SwUndo
, public SwUndRng
, private SwUndoSaveContent
254 SwTextFormatColl
*m_pTextFormatColl
, *m_pLastNodeColl
;
255 std::unique_ptr
<std::vector
<SwFrameFormat
*>> m_pFrameFormats
;
256 std::vector
< std::shared_ptr
<SwUndoInsLayFormat
> > m_FlyUndos
;
257 std::unique_ptr
<SwRedlineData
> m_pRedlineData
;
258 int m_nDeleteTextNodes
;
261 sal_uLong m_nNodeDiff
;
262 /// start of Content in UndoNodes for Redo
263 std::unique_ptr
<SwNodeIndex
> m_pUndoNodeIndex
;
264 sal_uInt16 m_nSetPos
; // Start in the history list.
266 SwUndoInserts( SwUndoId nUndoId
, const SwPaM
& );
268 virtual ~SwUndoInserts() override
;
270 virtual void UndoImpl( ::sw::UndoRedoContext
& ) override
;
271 virtual void RedoImpl( ::sw::UndoRedoContext
& ) override
;
272 virtual void RepeatImpl( ::sw::RepeatContext
& ) override
;
274 // Set destination range after reading.
275 void SetInsertRange( const SwPaM
&, bool bScanFlys
= true,
276 int nDeleteTextNodes
= 1);
278 static bool IsCreateUndoForNewFly(SwFormatAnchor
const& rAnchor
,
279 sal_uLong
const nStartNode
, sal_uLong
const nEndNode
);
280 std::vector
<SwFrameFormat
*> * GetFlysAnchoredAt() { return m_pFrameFormats
.get(); }
282 void dumpAsXml(xmlTextWriterPtr pWriter
) const override
;
285 class SwUndoInsDoc final
: public SwUndoInserts
288 SwUndoInsDoc( const SwPaM
& );
291 class SwUndoCpyDoc final
: public SwUndoInserts
294 SwUndoCpyDoc( const SwPaM
& );
297 class SwUndoFlyBase
: public SwUndo
, private SwUndoSaveSection
300 SwFrameFormat
* m_pFrameFormat
; // The saved FlyFormat.
301 sal_uLong m_nNodePagePos
;
302 sal_Int32 m_nContentPos
; // Page at/in paragraph.
304 bool m_bDelFormat
; // Delete saved format.
306 void InsFly(::sw::UndoRedoContext
& rContext
, bool bShowSel
= true);
307 void DelFly( SwDoc
* );
309 SwUndoFlyBase( SwFrameFormat
* pFormat
, SwUndoId nUndoId
);
311 SwNodeIndex
* GetMvSttIdx() const { return SwUndoSaveSection::GetMvSttIdx(); }
312 sal_uLong
GetMvNodeCnt() const { return SwUndoSaveSection::GetMvNodeCnt(); }
315 virtual ~SwUndoFlyBase() override
;
316 void dumpAsXml(xmlTextWriterPtr pWriter
) const override
;
320 class SwUndoInsLayFormat
: public SwUndoFlyBase
322 sal_uLong mnCursorSaveIndexPara
; // Cursor position
323 sal_Int32 mnCursorSaveIndexPos
; // for undo
325 SwUndoInsLayFormat( SwFrameFormat
* pFormat
, sal_uLong nNodeIdx
, sal_Int32 nCntIdx
);
327 virtual ~SwUndoInsLayFormat() override
;
329 virtual void UndoImpl( ::sw::UndoRedoContext
& ) override
;
330 virtual void RedoImpl( ::sw::UndoRedoContext
& ) override
;
331 virtual void RepeatImpl( ::sw::RepeatContext
& ) override
;
333 virtual OUString
GetComment() const override
;
337 class SwUndoDelLayFormat final
: public SwUndoFlyBase
339 bool m_bShowSelFrame
;
341 SwUndoDelLayFormat( SwFrameFormat
* pFormat
);
343 virtual void UndoImpl( ::sw::UndoRedoContext
& ) override
;
344 virtual void RedoImpl( ::sw::UndoRedoContext
& ) override
;
346 void RedoForRollback();
348 void ChgShowSel( bool bNew
) { m_bShowSelFrame
= bNew
; }
350 virtual SwRewriter
GetRewriter() const override
;
356 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */