bump product version to 4.1.6.2
[LibreOffice.git] / sw / inc / pam.hxx
blob73a33f725063580f25e7c1c0b2d4546c94ca074c
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 _PAM_HXX
20 #define _PAM_HXX
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.
29 #include "swdllapi.h"
31 class SwFmt;
32 class SfxPoolItem;
33 class SfxItemSet;
34 class SwDoc;
35 class SwNode;
36 class SwCntntNode;
37 class SwPaM;
38 class Point;
40 namespace com { namespace sun { namespace star { namespace util {
41 struct SearchOptions;
42 } } } }
44 namespace utl {
45 class TextSearch;
48 /// Marks a position in the document model.
49 struct SW_DLLPUBLIC SwPosition
51 SwNodeIndex nNode;
52 SwIndex nContent;
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 &);
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;
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.
91 template<typename T>
92 SwComparePosition ComparePosition(
93 const T& rStt1, const T& rEnd1,
94 const T& rStt2, const T& rEnd2 )
96 SwComparePosition nRet;
97 if( rStt1 < rStt2 )
99 if( rEnd1 > rStt2 )
101 if( rEnd1 >= rEnd2 )
102 nRet = POS_OUTSIDE;
103 else
104 nRet = POS_OVERLAP_BEFORE;
107 else if( rEnd1 == rStt2 )
108 nRet = POS_COLLIDE_END;
109 else
110 nRet = POS_BEFORE;
112 else if( rEnd2 > rStt1 )
114 if( rEnd2 >= rEnd1 )
116 if( rEnd2 == rEnd1 && rStt2 == rStt1 )
117 nRet = POS_EQUAL;
118 else
119 nRet = POS_INSIDE;
121 else
123 if (rStt1 == rStt2)
124 nRet = POS_OUTSIDE;
125 else
126 nRet = POS_OVERLAP_BEHIND;
129 else if( rEnd2 == rStt1 )
130 nRet = POS_COLLIDE_START;
131 else
132 nRet = POS_BEHIND;
133 return nRet;
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;
151 void _InitPam();
153 /// PaM is Point and Mark: a selection of the document model.
154 class SW_DLLPUBLIC SwPaM : public Ring
156 SwPosition m_Bound1;
157 SwPosition m_Bound2;
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 );
164 public:
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 );
177 virtual ~SwPaM();
179 /// @@@ semantic: no copy ctor.
180 SwPaM( SwPaM & );
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 );
188 /// Search.
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,
201 SwMoveFn fnMove,
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();
214 void DeleteMark()
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() ) );
221 m_pMark = m_pPoint;
224 #ifdef DBG_UTIL
225 void Exchange();
227 #else
228 void Exchange()
230 if (m_pPoint != m_pMark)
232 SwPosition *pTmp = m_pPoint;
233 m_pPoint = m_pMark;
234 m_pMark = pTmp;
237 #endif
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; }
251 SwPosition *Start()
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; }
256 SwPosition *End()
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 );
308 #endif // _PAM_HXX
310 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */