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_PAM_HXX
20 #define INCLUDED_SW_INC_PAM_HXX
22 #include <sal/types.h>
25 #include "ndindex.hxx"
34 /// Marks a position in the document model.
35 struct SAL_WARN_UNUSED SW_DLLPUBLIC SwPosition
40 SwPosition( const SwNodeIndex
&rNode
, const SwIndex
&rContent
);
41 explicit SwPosition( const SwNodeIndex
&rNode
);
42 explicit SwPosition( const SwNode
& rNode
);
43 explicit SwPosition( SwContentNode
& rNode
, const sal_Int32 nOffset
= 0 );
46 Returns the document this position is in.
48 @return the document this position is in.
50 SwDoc
* GetDoc() const;
52 bool operator < (const SwPosition
&) const;
53 bool operator > (const SwPosition
&) const;
54 bool operator <=(const SwPosition
&) const;
55 bool operator >=(const SwPosition
&) const;
56 bool operator ==(const SwPosition
&) const;
57 bool operator !=(const SwPosition
&) const;
58 void dumpAsXml(xmlTextWriterPtr pWriter
) const;
61 SW_DLLPUBLIC
std::ostream
&operator <<(std::ostream
& s
, const SwPosition
& position
);
63 // Result of comparing positions.
64 enum class SwComparePosition
{
65 Before
, ///< Pos1 before Pos2.
66 Behind
, ///< Pos1 behind Pos2.
67 Inside
, ///< Pos1 completely contained in Pos2.
68 Outside
, ///< Pos2 completely contained in Pos1.
69 Equal
, ///< Pos1 is as large as Pos2.
70 OverlapBefore
, ///< Pos1 overlaps Pos2 at the beginning.
71 OverlapBehind
, ///< Pos1 overlaps Pos2 at the end.
72 CollideStart
, ///< Pos1 start touches at Pos2 end.
73 CollideEnd
///< Pos1 end touches at Pos2 start.
77 SwComparePosition
ComparePosition(
78 const T
& rStt1
, const T
& rEnd1
,
79 const T
& rStt2
, const T
& rEnd2
)
81 SwComparePosition nRet
;
87 nRet
= SwComparePosition::Outside
;
89 nRet
= SwComparePosition::OverlapBefore
;
92 else if( rEnd1
== rStt2
)
93 nRet
= SwComparePosition::CollideEnd
;
95 nRet
= SwComparePosition::Before
;
97 else if( rEnd2
> rStt1
)
101 if( rEnd2
== rEnd1
&& rStt2
== rStt1
)
102 nRet
= SwComparePosition::Equal
;
104 nRet
= SwComparePosition::Inside
;
109 nRet
= SwComparePosition::Outside
;
111 nRet
= SwComparePosition::OverlapBehind
;
114 else if( rEnd2
== rStt1
)
115 nRet
= SwComparePosition::CollideStart
;
117 nRet
= SwComparePosition::Behind
;
121 /// SwPointAndMark / SwPaM
122 struct SwMoveFnCollection
;
123 SW_DLLPUBLIC
extern SwMoveFnCollection
const & fnMoveForward
; ///< SwPam::Move()/Find() default argument.
124 SW_DLLPUBLIC
extern SwMoveFnCollection
const & fnMoveBackward
;
126 using SwGoInDoc
= auto (*)(SwPaM
& rPam
, SwMoveFnCollection
const & fnMove
) -> bool;
127 SW_DLLPUBLIC
bool GoInDoc( SwPaM
&, SwMoveFnCollection
const &);
128 bool GoInSection( SwPaM
&, SwMoveFnCollection
const &);
129 SW_DLLPUBLIC
bool GoInNode( SwPaM
&, SwMoveFnCollection
const &);
130 SW_DLLPUBLIC
bool GoInContent( SwPaM
&, SwMoveFnCollection
const &);
131 bool GoInContentCells( SwPaM
&, SwMoveFnCollection
const &);
132 bool GoInContentSkipHidden( SwPaM
&, SwMoveFnCollection
const &);
133 bool GoInContentCellsSkipHidden( SwPaM
&, SwMoveFnCollection
const &);
135 /// PaM is Point and Mark: a selection of the document model.
136 class SAL_WARN_UNUSED SW_DLLPUBLIC SwPaM
: public sw::Ring
<SwPaM
>
140 SwPosition
* m_pPoint
; ///< points at either m_Bound1 or m_Bound2
141 SwPosition
* m_pMark
; ///< points at either m_Bound1 or m_Bound2
142 bool m_bIsInFrontOfLabel
;
144 SwPaM(SwPaM
const& rPaM
) = delete;
147 explicit SwPaM( const SwPosition
& rPos
, SwPaM
* pRing
= nullptr );
148 SwPaM( const SwPosition
& rMk
, const SwPosition
& rPt
, SwPaM
* pRing
= nullptr );
149 SwPaM( const SwNodeIndex
& rMk
, const SwNodeIndex
& rPt
,
150 long nMkOffset
= 0, long nPtOffset
= 0, SwPaM
* pRing
= nullptr );
151 SwPaM( const SwNode
& rMk
, const SwNode
& rPt
,
152 long nMkOffset
= 0, long nPtOffset
= 0, SwPaM
* pRing
= nullptr );
153 SwPaM( const SwNodeIndex
& rMk
, sal_Int32 nMkContent
,
154 const SwNodeIndex
& rPt
, sal_Int32 nPtContent
, SwPaM
* pRing
= nullptr );
155 SwPaM( const SwNode
& rMk
, sal_Int32 nMkContent
,
156 const SwNode
& rPt
, sal_Int32 nPtContent
, SwPaM
* pRing
= nullptr );
157 SwPaM( const SwNode
& rNd
, sal_Int32 nContent
= 0, SwPaM
* pRing
= nullptr );
158 SwPaM( const SwNodeIndex
& rNd
, sal_Int32 nContent
= 0, SwPaM
* pRing
= nullptr );
159 virtual ~SwPaM() override
;
161 /// this takes a second parameter, which indicates the Ring that
162 /// the new PaM should be part of (may be null)
163 SwPaM(SwPaM
const& rPaM
, SwPaM
* pRing
);
164 /// @@@ semantic: no copy assignment for super class Ring.
165 SwPaM
& operator=( const SwPaM
& );
167 /// Movement of cursor.
168 bool Move( SwMoveFnCollection
const & fnMove
= fnMoveForward
,
169 SwGoInDoc fnGo
= GoInContent
);
171 bool IsInFrontOfLabel() const { return m_bIsInFrontOfLabel
; }
172 void SetInFrontOfLabel_( bool bNew
) { m_bIsInFrontOfLabel
= bNew
; }
174 /// Unless this is called, the getter method of Mark will return Point.
175 virtual void SetMark();
179 if (m_pMark
!= m_pPoint
)
181 /** clear the mark position; this helps if mark's SwIndex is
182 registered at some node, and that node is then deleted */
183 *m_pMark
= SwPosition( SwNodeIndex( GetNode().GetNodes() ) );
193 if (m_pPoint
!= m_pMark
)
195 SwPosition
*pTmp
= m_pPoint
;
202 /** A PaM marks a selection if Point and Mark are distinct positions.
203 @return true if the PaM spans a selection
205 bool HasMark() const { return m_pPoint
!= m_pMark
; }
207 const SwPosition
*GetPoint() const { return m_pPoint
; }
208 SwPosition
*GetPoint() { return m_pPoint
; }
209 const SwPosition
*GetMark() const { return m_pMark
; }
210 SwPosition
*GetMark() { return m_pMark
; }
212 const SwPosition
*Start() const
213 { return (*m_pPoint
) <= (*m_pMark
) ? m_pPoint
: m_pMark
; }
215 { return (*m_pPoint
) <= (*m_pMark
) ? m_pPoint
: m_pMark
; }
217 const SwPosition
*End() const
218 { return (*m_pPoint
) > (*m_pMark
) ? m_pPoint
: m_pMark
; }
220 { return (*m_pPoint
) > (*m_pMark
) ? m_pPoint
: m_pMark
; }
222 /// @return current Node at Point/Mark
223 SwNode
& GetNode ( bool bPoint
= true ) const
225 return ( bPoint
? m_pPoint
->nNode
: m_pMark
->nNode
).GetNode();
228 /// @return current ContentNode at Point/Mark
229 SwContentNode
* GetContentNode( bool bPoint
= true ) const
231 return GetNode(bPoint
).GetContentNode();
235 Normalizes PaM, i.e. sort point and mark.
237 @param bPointFirst true: If the point is behind the mark then swap.
238 false: If the mark is behind the point then swap.
240 void Normalize(bool bPointFirst
= true);
242 /// @return the document (SwDoc) at which the PaM is registered
243 SwDoc
* GetDoc() const { return m_pPoint
->nNode
.GetNode().GetDoc(); }
245 SwPosition
& GetBound( bool bOne
= true )
246 { return bOne
? m_Bound1
: m_Bound2
; }
247 const SwPosition
& GetBound( bool bOne
= true ) const
248 { return bOne
? m_Bound1
: m_Bound2
; }
250 /// Get number of page which contains cursor.
251 sal_uInt16
GetPageNum( bool bAtPoint
= true, const Point
* pLayPos
= nullptr );
253 /** Is in something protected (readonly) or selection contains
254 something protected. */
255 bool HasReadonlySel( bool bFormView
) const;
257 bool ContainsPosition(const SwPosition
& rPos
) const
259 return *Start() <= rPos
&& rPos
<= *End();
262 OUString
GetText() const;
263 void InvalidatePaM();
265 { return GetNextInRing(); }
266 const SwPaM
* GetNext() const
267 { return GetNextInRing(); }
269 { return GetPrevInRing(); }
270 const SwPaM
* GetPrev() const
271 { return GetPrevInRing(); }
272 bool IsMultiSelection() const
273 { return !unique(); }
275 void dumpAsXml(xmlTextWriterPtr pWriter
) const;
278 SW_DLLPUBLIC
std::ostream
&operator <<(std::ostream
& s
, const SwPaM
& pam
);
280 SW_DLLPUBLIC
bool CheckNodesRange(const SwNodeIndex
&, const SwNodeIndex
&, bool bChkSection
);
282 #endif // INCLUDED_SW_INC_PAM_HXX
284 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */