android: Update app-specific/MIME type icons
[LibreOffice.git] / sw / inc / pam.hxx
blob62721beb0c4c3291070ebbacc03dfeaf5aa7ceae
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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_PAM_HXX
20 #define INCLUDED_SW_INC_PAM_HXX
22 #include <sal/types.h>
23 #include "ring.hxx"
24 #include "contentindex.hxx"
25 #include "ndindex.hxx"
26 #include "swdllapi.h"
27 #include "nodeoffset.hxx"
29 #include <iostream>
30 #include <utility>
32 class SwDoc;
33 class SwPaM;
34 class Point;
36 /// Marks a position in the document model.
37 struct SAL_WARN_UNUSED SW_DLLPUBLIC SwPosition
39 SwNodeIndex nNode;
40 SwContentIndex nContent;
42 SwPosition( const SwNodeIndex &rNode, const SwContentIndex &rContent );
43 SwPosition( const SwNode &rNode, const SwContentIndex &rContent );
44 explicit SwPosition( SwNodes& rNodes, SwNodeOffset nIndex = SwNodeOffset(0) );
45 explicit SwPosition( const SwNodeIndex &rNode, SwNodeOffset nDiff = SwNodeOffset(0) );
46 explicit SwPosition( const SwNode& rNode, SwNodeOffset nDiff = SwNodeOffset(0) );
47 explicit SwPosition( const SwContentNode& rNode, sal_Int32 nContentOffset = 0 );
48 SwPosition( const SwNodeIndex &rNode, const SwContentNode*, sal_Int32 nContentOffset );
49 SwPosition( const SwNode &rNode, const SwContentNode*, sal_Int32 nContentOffset );
50 SwPosition( const SwNodeIndex &rNode, SwNodeOffset nDiff, const SwContentNode*, sal_Int32 nContentOffset );
51 SwPosition( const SwNode &rNode, SwNodeOffset nDiff, const SwContentNode*, sal_Int32 nContentOffset );
52 SwPosition( const SwContentIndex &, short nDiff );
54 // callers should be using one of the other constructors to avoid creating a temporary
55 SwPosition( SwNodeIndex && ) = delete;
56 SwPosition( const SwNodeIndex &, SwContentIndex && ) = delete;
57 SwPosition( SwNodeIndex &&, SwContentIndex && ) = delete;
58 SwPosition( SwNodeIndex &&, const SwContentNode*, sal_Int32 ) = delete;
59 SwPosition( SwNodeIndex &&, SwNodeOffset ) = delete;
60 SwPosition( SwContentIndex &&, short ) = delete;
62 /**
63 Returns the document this position is in.
65 @return the document this position is in.
67 SwDoc& GetDoc() const;
69 bool operator < (const SwPosition &) const;
70 bool operator > (const SwPosition &) const;
71 bool operator <=(const SwPosition &) const;
72 bool operator >=(const SwPosition &) const;
73 bool operator ==(const SwPosition &) const;
74 bool operator !=(const SwPosition &) const;
75 void dumpAsXml(xmlTextWriterPtr pWriter) const;
78 SwNodeOffset GetNodeIndex() const { return nNode.GetIndex(); }
79 const SwNodes& GetNodes() const { return nNode.GetNodes(); }
80 SwNodes& GetNodes() { return nNode.GetNodes(); }
81 SwNode& GetNode() const { return nNode.GetNode(); }
84 const SwContentNode* GetContentNode() const { return nContent.GetContentNode(); }
85 sal_Int32 GetContentIndex() const { return nContent.GetIndex(); }
86 void SetMark(const sw::mark::IMark* pMark) { nContent.SetMark(pMark); }
87 void SetRedline(SwRangeRedline* pRangeRedline) { nContent.SetRedline(pRangeRedline); }
89 /// These all set both nNode and nContent
90 void Assign( const SwNode& rNd, SwNodeOffset nDelta, sal_Int32 nContentOffset = 0 );
91 void Assign( SwNodeOffset nNodeOffset, sal_Int32 nContentOffset = 0 );
92 void Assign( const SwContentNode& rNode, sal_Int32 nContentOffset = 0 );
93 void Assign( const SwNode& rNd, sal_Int32 nContentOffset = 0 );
94 void Assign( const SwNodeIndex& rNdIdx, sal_Int32 nContentOffset = 0 );
95 /// Set nNode to rNd, and nContent to the beginning of rNd
96 void AssignStartIndex( const SwContentNode& rNd );
97 /// Set nNode to rNd, and nContent to the end of rNd
98 void AssignEndIndex( const SwContentNode& rNd );
99 /// Adjust node position, and resets content position to zero
100 void Adjust( SwNodeOffset nDelta );
101 /// Adjust content index, only valid to call this if the position points to a SwContentNode subclass
102 void AdjustContent( sal_Int32 nDelta );
103 /// Set content index, only valid to call this if the position points to a SwContentNode subclass
104 void SetContent( sal_Int32 nContentIndex );
107 SW_DLLPUBLIC std::ostream &operator <<(std::ostream& s, const SwPosition& position);
109 // Result of comparing positions.
110 enum class SwComparePosition {
111 Before, ///< Pos1 before Pos2.
112 Behind, ///< Pos1 behind Pos2.
113 Inside, ///< Pos1 completely contained in Pos2.
114 Outside, ///< Pos2 completely contained in Pos1.
115 Equal, ///< Pos1 is as large as Pos2.
116 OverlapBefore, ///< Pos1 overlaps Pos2 at the beginning.
117 OverlapBehind, ///< Pos1 overlaps Pos2 at the end.
118 CollideStart, ///< Pos1 start touches at Pos2 end.
119 CollideEnd ///< Pos1 end touches at Pos2 start.
122 template<typename T>
123 SwComparePosition ComparePosition(
124 const T& rStt1, const T& rEnd1,
125 const T& rStt2, const T& rEnd2 )
127 SwComparePosition nRet;
128 if( rStt1 < rStt2 )
130 if( rEnd1 > rStt2 )
132 if( rEnd1 >= rEnd2 )
133 nRet = SwComparePosition::Outside;
134 else
135 nRet = SwComparePosition::OverlapBefore;
138 else if( rEnd1 == rStt2 )
139 nRet = SwComparePosition::CollideEnd;
140 else
141 nRet = SwComparePosition::Before;
143 else if( rEnd2 > rStt1 )
145 if( rEnd2 >= rEnd1 )
147 if( rEnd2 == rEnd1 && rStt2 == rStt1 )
148 nRet = SwComparePosition::Equal;
149 else
150 nRet = SwComparePosition::Inside;
152 else
154 if (rStt1 == rStt2)
155 nRet = SwComparePosition::Outside;
156 else
157 nRet = SwComparePosition::OverlapBehind;
160 else if( rEnd2 == rStt1 )
161 nRet = SwComparePosition::CollideStart;
162 else
163 nRet = SwComparePosition::Behind;
164 return nRet;
167 /// SwPointAndMark / SwPaM
168 struct SwMoveFnCollection;
169 SW_DLLPUBLIC extern SwMoveFnCollection const & fnMoveForward; ///< SwPam::Move()/Find() default argument.
170 SW_DLLPUBLIC extern SwMoveFnCollection const & fnMoveBackward;
172 using SwGoInDoc = auto (*)(SwPaM& rPam, SwMoveFnCollection const & fnMove) -> bool;
173 SW_DLLPUBLIC bool GoInDoc( SwPaM&, SwMoveFnCollection const &);
174 bool GoInSection( SwPaM&, SwMoveFnCollection const &);
175 SW_DLLPUBLIC bool GoInNode( SwPaM&, SwMoveFnCollection const &);
176 SW_DLLPUBLIC bool GoInContent( SwPaM&, SwMoveFnCollection const &);
177 bool GoInContentCells( SwPaM&, SwMoveFnCollection const &);
178 bool GoInContentSkipHidden( SwPaM&, SwMoveFnCollection const &);
179 bool GoInContentCellsSkipHidden( SwPaM&, SwMoveFnCollection const &);
182 * PaM is Point and Mark: a selection of the document model.
184 * The reason for the distinction is that the point moves around during adjusting the selection with
185 * shift-arrow keys, while the mark remains where it is.
187 class SAL_WARN_UNUSED SW_DLLPUBLIC SwPaM : public sw::Ring<SwPaM>
189 SwPosition m_Bound1;
190 SwPosition m_Bound2;
191 SwPosition * m_pPoint; ///< points at either m_Bound1 or m_Bound2
192 SwPosition * m_pMark; ///< points at either m_Bound1 or m_Bound2
193 bool m_bIsInFrontOfLabel;
195 SwPaM(SwPaM const& rPaM) = delete;
197 public:
198 explicit SwPaM( const SwPosition& rPos, SwPaM* pRing = nullptr );
199 SwPaM( const SwPosition& rMk, const SwPosition& rPt, SwPaM* pRing = nullptr );
200 SwPaM( const SwNodeIndex& rMk, const SwNodeIndex& rPt,
201 SwNodeOffset nMkOffset = SwNodeOffset(0), SwNodeOffset nPtOffset = SwNodeOffset(0), SwPaM* pRing = nullptr );
202 SwPaM( const SwNode& rMk, const SwNode& rPt,
203 SwNodeOffset nMkOffset = SwNodeOffset(0), SwNodeOffset nPtOffset = SwNodeOffset(0), SwPaM* pRing = nullptr );
204 SwPaM( const SwNodeIndex& rMk, sal_Int32 nMkContent,
205 const SwNodeIndex& rPt, sal_Int32 nPtContent, SwPaM* pRing = nullptr );
206 SwPaM( const SwNode& rMk, sal_Int32 nMkContent,
207 const SwNode& rPt, sal_Int32 nPtContent, SwPaM* pRing = nullptr );
208 SwPaM( const SwNode& rMk, SwNodeOffset nMkOffset, sal_Int32 nMkContent,
209 const SwNode& rPt, SwNodeOffset nPtOffset, sal_Int32 nPtContent, SwPaM* pRing = nullptr );
210 SwPaM( const SwNode& rNd, SwNodeOffset nNdOffset, sal_Int32 nContent = 0, SwPaM* pRing = nullptr );
211 explicit SwPaM( const SwNode& rNd, sal_Int32 nContent = 0, SwPaM* pRing = nullptr );
212 explicit SwPaM( const SwNodeIndex& rNd, sal_Int32 nContent = 0, SwPaM* pRing = nullptr );
213 explicit SwPaM( SwNodes& rNds, SwNodeOffset nMkOffset = SwNodeOffset(0), SwPaM* pRing = nullptr );
214 virtual ~SwPaM() override;
216 /// this takes a second parameter, which indicates the Ring that
217 /// the new PaM should be part of (may be null)
218 SwPaM(SwPaM const& rPaM, SwPaM * pRing);
219 /// @@@ semantic: no copy assignment for super class Ring.
220 SwPaM& operator=( const SwPaM & );
222 /// Movement of cursor.
223 bool Move( SwMoveFnCollection const & fnMove = fnMoveForward,
224 SwGoInDoc fnGo = GoInContent );
226 bool IsInFrontOfLabel() const { return m_bIsInFrontOfLabel; }
227 void SetInFrontOfLabel_( bool bNew ) { m_bIsInFrontOfLabel = bNew; }
229 /// Unless this is called, the getter method of Mark will return Point.
230 virtual void SetMark();
232 void DeleteMark()
234 if (HasMark())
236 /** clear the mark position; this helps if mark's SwContentIndex is
237 registered at some node, and that node is then deleted */
238 m_pMark->Assign( *GetPointNode().GetNodes()[SwNodeOffset(0)] );
239 m_pMark = m_pPoint;
242 void Exchange()
244 if (HasMark())
245 std::swap(m_pPoint, m_pMark);
248 /** A PaM marks a selection if Point and Mark are distinct positions.
249 @return true if the PaM spans a selection
251 bool HasMark() const { return m_pPoint != m_pMark; }
253 const SwPosition *GetPoint() const { return m_pPoint; }
254 SwPosition *GetPoint() { return m_pPoint; }
255 const SwPosition *GetMark() const { return m_pMark; }
256 SwPosition *GetMark() { return m_pMark; }
258 const SwPosition *Start() const
259 { return (*m_pPoint) <= (*m_pMark) ? m_pPoint : m_pMark; }
260 SwPosition *Start()
261 { return (*m_pPoint) <= (*m_pMark) ? m_pPoint : m_pMark; }
263 const SwPosition *End() const
264 { return (*m_pPoint) > (*m_pMark) ? m_pPoint : m_pMark; }
265 SwPosition *End()
266 { return (*m_pPoint) > (*m_pMark) ? m_pPoint : m_pMark; }
268 /// Because sometimes the cost of the operator<= can add up
269 std::pair<const SwPosition *, const SwPosition *> StartEnd() const
270 { if ((*m_pPoint) <= (*m_pMark)) return { m_pPoint, m_pMark }; else return { m_pMark, m_pPoint }; }
271 std::pair<SwPosition *, SwPosition *> StartEnd()
272 { if ((*m_pPoint) <= (*m_pMark)) return { m_pPoint, m_pMark }; else return { m_pMark, m_pPoint }; }
274 /// @return current Node at Point/Mark
275 SwNode& GetPointNode() const { return m_pPoint->nNode.GetNode(); }
276 SwNode& GetMarkNode() const { return m_pMark->nNode.GetNode(); }
278 /// @return current ContentNode at Point/Mark
279 SwContentNode* GetPointContentNode() const { return m_pPoint->nNode.GetNode().GetContentNode(); }
280 SwContentNode* GetMarkContentNode() const { return m_pMark->nNode.GetNode().GetContentNode(); }
283 Normalizes PaM, i.e. sort point and mark.
285 @param bPointFirst true: If the point is behind the mark then swap.
286 false: If the mark is behind the point then swap.
288 void Normalize(bool bPointFirst = true);
290 /// @return the document (SwDoc) at which the PaM is registered
291 SwDoc& GetDoc() const { return m_pPoint->nNode.GetNode().GetDoc(); }
293 SwPosition& GetBound( bool bOne = true )
294 { return bOne ? m_Bound1 : m_Bound2; }
295 const SwPosition& GetBound( bool bOne = true ) const
296 { return bOne ? m_Bound1 : m_Bound2; }
298 /// Get number of page which contains cursor.
299 sal_uInt16 GetPageNum( bool bAtPoint = true, const Point* pLayPos = nullptr );
301 /** Is in something protected (readonly) or selection contains
302 something protected. */
303 bool HasReadonlySel(bool bFormView, bool isReplace) const;
304 /** Is there hidden sections in the selected area. */
305 bool HasHiddenSections() const;
307 bool ContainsPosition(const SwPosition & rPos) const
309 return *Start() <= rPos && rPos <= *End();
312 OUString GetText() const;
313 void InvalidatePaM();
314 SwPaM* GetNext()
315 { return GetNextInRing(); }
316 const SwPaM* GetNext() const
317 { return GetNextInRing(); }
318 SwPaM* GetPrev()
319 { return GetPrevInRing(); }
320 const SwPaM* GetPrev() const
321 { return GetPrevInRing(); }
322 bool IsMultiSelection() const
323 { return !unique(); }
325 void dumpAsXml(xmlTextWriterPtr pWriter) const;
328 SW_DLLPUBLIC std::ostream &operator <<(std::ostream& s, const SwPaM& pam);
330 bool CheckNodesRange(const SwNode&, const SwNode&, bool bChkSection);
332 #endif // INCLUDED_SW_INC_PAM_HXX
334 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */