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 .
22 #include <stddef.h> ///< For MemPool.
23 #include <sal/types.h>
24 #include <tools/mempool.hxx>
25 #include <cshtyp.hxx> ///< For function definitions.
26 #include <ring.hxx> ///< Super class.
27 #include <index.hxx> ///< For SwIndex.
28 #include <ndindex.hxx> ///< For SwNodeIndex.
40 namespace com
{ namespace sun
{ namespace star
{ namespace util
{
48 /// Marks a position in the document model.
49 struct SW_DLLPUBLIC SwPosition
54 SwPosition( const SwNodeIndex
&rNode
, const SwIndex
&rCntnt
);
55 explicit SwPosition( const SwNodeIndex
&rNode
);
56 explicit SwPosition( const SwNode
& rNode
);
57 explicit SwPosition( SwCntntNode
& rNode
, const xub_StrLen nOffset
= 0 );
59 SwPosition( const SwPosition
& );
60 SwPosition
&operator=(const SwPosition
&);
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;
78 // Result of comparing positions.
79 enum SwComparePosition
{
80 POS_BEFORE
, ///< Pos1 before Pos2.
81 POS_BEHIND
, ///< Pos1 behind Pos2.
82 POS_INSIDE
, ///< Pos1 completely contained in Pos2.
83 POS_OUTSIDE
, ///< Pos2 completely contained in Pos1.
84 POS_EQUAL
, ///< Pos1 is as large as Pos2.
85 POS_OVERLAP_BEFORE
, ///< Pos1 overlaps Pos2 at the beginning.
86 POS_OVERLAP_BEHIND
, ///< Pos1 overlaps Pos2 at the end.
87 POS_COLLIDE_START
, ///< Pos1 start touches at Pos2 end.
88 POS_COLLIDE_END
///< Pos1 end touches at Pos2 start.
92 SwComparePosition
ComparePosition(
93 const T
& rStt1
, const T
& rEnd1
,
94 const T
& rStt2
, const T
& rEnd2
)
96 SwComparePosition nRet
;
104 nRet
= POS_OVERLAP_BEFORE
;
107 else if( rEnd1
== rStt2
)
108 nRet
= POS_COLLIDE_END
;
112 else if( rEnd2
> rStt1
)
116 if( rEnd2
== rEnd1
&& rStt2
== rStt1
)
126 nRet
= POS_OVERLAP_BEHIND
;
129 else if( rEnd2
== rStt1
)
130 nRet
= POS_COLLIDE_START
;
136 /// SwPointAndMark / SwPaM
137 struct SwMoveFnCollection
;
138 typedef SwMoveFnCollection
* SwMoveFn
;
139 SW_DLLPUBLIC
extern SwMoveFn fnMoveForward
; ///< SwPam::Move()/Find() default argument.
140 SW_DLLPUBLIC
extern SwMoveFn fnMoveBackward
;
142 typedef sal_Bool (*SwGoInDoc
)( SwPaM
& rPam
, SwMoveFn fnMove
);
143 SW_DLLPUBLIC
extern SwGoInDoc fnGoDoc
;
144 extern SwGoInDoc fnGoSection
;
145 SW_DLLPUBLIC
extern SwGoInDoc fnGoNode
;
146 SW_DLLPUBLIC
extern SwGoInDoc fnGoCntnt
; ///< SwPam::Move() default argument.
147 extern SwGoInDoc fnGoCntntCells
;
148 extern SwGoInDoc fnGoCntntSkipHidden
;
149 extern SwGoInDoc fnGoCntntCellsSkipHidden
;
153 /// PaM is Point and Mark: a selection of the document model.
154 class SW_DLLPUBLIC SwPaM
: public Ring
158 SwPosition
* m_pPoint
; ///< points at either m_Bound1 or m_Bound2
159 SwPosition
* m_pMark
; ///< points at either m_Bound1 or m_Bound2
160 bool m_bIsInFrontOfLabel
;
162 SwPaM
* MakeRegion( SwMoveFn fnMove
, const SwPaM
* pOrigRg
= 0 );
165 SwPaM( const SwPosition
& rPos
, SwPaM
* pRing
= 0 );
166 SwPaM( const SwPosition
& rMk
, const SwPosition
& rPt
, SwPaM
* pRing
= 0 );
167 SwPaM( const SwNodeIndex
& rMk
, const SwNodeIndex
& rPt
,
168 long nMkOffset
= 0, long nPtOffset
= 0, SwPaM
* pRing
= 0 );
169 SwPaM( const SwNode
& rMk
, const SwNode
& rPt
,
170 long nMkOffset
= 0, long nPtOffset
= 0, SwPaM
* pRing
= 0 );
171 SwPaM( const SwNodeIndex
& rMk
, xub_StrLen nMkCntnt
,
172 const SwNodeIndex
& rPt
, xub_StrLen nPtCntnt
, SwPaM
* pRing
= 0 );
173 SwPaM( const SwNode
& rMk
, xub_StrLen nMkCntnt
,
174 const SwNode
& rPt
, xub_StrLen nPtCntnt
, SwPaM
* pRing
= 0 );
175 SwPaM( const SwNode
& rNd
, xub_StrLen nCntnt
= 0, SwPaM
* pRing
= 0 );
176 SwPaM( const SwNodeIndex
& rNd
, xub_StrLen nCntnt
= 0, SwPaM
* pRing
= 0 );
179 /// @@@ semantic: no copy ctor.
181 /// @@@ semantic: no copy assignment for super class Ring.
182 SwPaM
& operator=( const SwPaM
& );
184 /// Movement of cursor.
185 sal_Bool
Move( SwMoveFn fnMove
= fnMoveForward
,
186 SwGoInDoc fnGo
= fnGoCntnt
);
189 sal_uInt8
Find( const com::sun::star::util::SearchOptions
& rSearchOpt
,
190 sal_Bool bSearchInNotes
,
191 utl::TextSearch
& rSTxt
,
192 SwMoveFn fnMove
= fnMoveForward
,
193 const SwPaM
*pPam
=0, sal_Bool bInReadOnly
= sal_False
);
194 sal_Bool
Find( const SwFmt
& rFmt
,
195 SwMoveFn fnMove
= fnMoveForward
,
196 const SwPaM
*pPam
=0, sal_Bool bInReadOnly
= sal_False
);
197 sal_Bool
Find( const SfxPoolItem
& rAttr
, sal_Bool bValue
= sal_True
,
198 SwMoveFn fnMove
= fnMoveForward
,
199 const SwPaM
*pPam
=0, sal_Bool bInReadOnly
= sal_False
);
200 sal_Bool
Find( const SfxItemSet
& rAttr
, sal_Bool bNoColls
,
202 const SwPaM
*pPam
, sal_Bool bInReadOnly
, sal_Bool bMoveFirst
);
204 bool DoSearch( const com::sun::star::util::SearchOptions
& rSearchOpt
, utl::TextSearch
& rSTxt
,
205 SwMoveFn fnMove
, sal_Bool bSrchForward
, sal_Bool bRegSearch
, sal_Bool bChkEmptyPara
, sal_Bool bChkParaEnd
,
206 xub_StrLen
&nStart
, xub_StrLen
&nEnde
,xub_StrLen nTxtLen
,SwNode
* pNode
, SwPaM
* pPam
);
208 inline bool IsInFrontOfLabel() const { return m_bIsInFrontOfLabel
; }
209 inline void _SetInFrontOfLabel( bool bNew
) { m_bIsInFrontOfLabel
= bNew
; }
211 /// Unless this is called, the getter method of Mark will return Point.
212 virtual void SetMark();
216 if (m_pMark
!= m_pPoint
)
218 /** clear the mark position; this helps if mark's SwIndex is
219 registered at some node, and that node is then deleted */
220 *m_pMark
= SwPosition( SwNodeIndex( GetNode()->GetNodes() ) );
230 if (m_pPoint
!= m_pMark
)
232 SwPosition
*pTmp
= m_pPoint
;
239 /** A PaM marks a selection if Point and Mark are distinct positions.
240 @return true iff the PaM spans a selection
242 bool HasMark() const { return m_pPoint
== m_pMark
? false : true; }
244 const SwPosition
*GetPoint() const { return m_pPoint
; }
245 SwPosition
*GetPoint() { return m_pPoint
; }
246 const SwPosition
*GetMark() const { return m_pMark
; }
247 SwPosition
*GetMark() { return m_pMark
; }
249 const SwPosition
*Start() const
250 { return (*m_pPoint
) <= (*m_pMark
) ? m_pPoint
: m_pMark
; }
252 { return (*m_pPoint
) <= (*m_pMark
) ? m_pPoint
: m_pMark
; }
254 const SwPosition
*End() const
255 { return (*m_pPoint
) > (*m_pMark
) ? m_pPoint
: m_pMark
; }
257 { return (*m_pPoint
) > (*m_pMark
) ? m_pPoint
: m_pMark
; }
259 /// @return current Node at Point/Mark
260 SwNode
* GetNode ( bool bPoint
= true ) const
262 return &( bPoint
? m_pPoint
->nNode
: m_pMark
->nNode
).GetNode();
265 /// @return current ContentNode at Point/Mark
266 SwCntntNode
* GetCntntNode( bool bPoint
= true ) const
268 return GetNode(bPoint
)->GetCntntNode();
272 Normalizes PaM, i.e. sort point and mark.
274 @param bPointFirst sal_True: If the point is behind the mark then swap.
275 sal_False: If the mark is behind the point then swap.
277 SwPaM
& Normalize(sal_Bool bPointFirst
= sal_True
);
279 /// @return the document (SwDoc) at which the PaM is registered
280 SwDoc
* GetDoc() const { return m_pPoint
->nNode
.GetNode().GetDoc(); }
282 SwPosition
& GetBound( bool bOne
= true )
283 { return bOne
? m_Bound1
: m_Bound2
; }
284 const SwPosition
& GetBound( bool bOne
= true ) const
285 { return bOne
? m_Bound1
: m_Bound2
; }
287 /// Get number of page which contains cursor.
288 sal_uInt16
GetPageNum( sal_Bool bAtPoint
= sal_True
, const Point
* pLayPos
= 0 );
290 /** Is in something protected (readonly) or selection contains
291 something protected. */
292 bool HasReadonlySel( bool bFormView
, bool bAnnotationMode
= false ) const;
294 sal_Bool
ContainsPosition(const SwPosition
& rPos
)
295 { return *Start() <= rPos
&& rPos
<= *End(); }
297 DECL_FIXEDMEMPOOL_NEWDEL(SwPaM
);
299 String
GetTxt() const;
300 void InvalidatePaM();
304 sal_Bool
CheckNodesRange( const SwNodeIndex
&, const SwNodeIndex
&, sal_Bool
);
305 sal_Bool
GoInCntnt( SwPaM
& rPam
, SwMoveFn fnMove
);
310 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */