Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 : #ifndef _PAM_HXX
20 : #define _PAM_HXX
21 :
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"
30 :
31 : class SwFmt;
32 : class SfxPoolItem;
33 : class SfxItemSet;
34 : class SwDoc;
35 : class SwNode;
36 : class SwCntntNode;
37 : class SwPaM;
38 : class Point;
39 :
40 : namespace com { namespace sun { namespace star { namespace util {
41 : struct SearchOptions;
42 : } } } }
43 :
44 : namespace utl {
45 : class TextSearch;
46 : }
47 :
48 : /// Marks a position in the document model.
49 959634 : struct SW_DLLPUBLIC SwPosition
50 : {
51 : SwNodeIndex nNode;
52 : SwIndex nContent;
53 :
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 );
58 :
59 : SwPosition( const SwPosition & );
60 : SwPosition &operator=(const SwPosition &);
61 :
62 : /**
63 : Returns the document this position is in.
64 :
65 : @return the document this position is in.
66 : */
67 : SwDoc * GetDoc() const;
68 :
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 : };
76 :
77 :
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.
89 : };
90 :
91 : template<typename T>
92 703 : SwComparePosition ComparePosition(
93 : const T& rStt1, const T& rEnd1,
94 : const T& rStt2, const T& rEnd2 )
95 : {
96 : SwComparePosition nRet;
97 703 : if( rStt1 < rStt2 )
98 : {
99 244 : if( rEnd1 > rStt2 )
100 : {
101 244 : if( rEnd1 >= rEnd2 )
102 239 : nRet = POS_OUTSIDE;
103 : else
104 5 : nRet = POS_OVERLAP_BEFORE;
105 :
106 : }
107 0 : else if( rEnd1 == rStt2 )
108 0 : nRet = POS_COLLIDE_END;
109 : else
110 0 : nRet = POS_BEFORE;
111 : }
112 459 : else if( rEnd2 > rStt1 )
113 : {
114 10 : if( rEnd2 >= rEnd1 )
115 : {
116 1 : if( rEnd2 == rEnd1 && rStt2 == rStt1 )
117 0 : nRet = POS_EQUAL;
118 : else
119 1 : nRet = POS_INSIDE;
120 : }
121 : else
122 : {
123 9 : if (rStt1 == rStt2)
124 4 : nRet = POS_OUTSIDE;
125 : else
126 5 : nRet = POS_OVERLAP_BEHIND;
127 : }
128 : }
129 449 : else if( rEnd2 == rStt1 )
130 331 : nRet = POS_COLLIDE_START;
131 : else
132 118 : nRet = POS_BEHIND;
133 703 : return nRet;
134 : }
135 :
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;
141 :
142 : typedef 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;
150 :
151 : void _InitPam();
152 :
153 : /// PaM is Point and Mark: a selection of the document model.
154 : class SW_DLLPUBLIC SwPaM : public Ring
155 : {
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;
161 :
162 : SwPaM* MakeRegion( SwMoveFn fnMove, const SwPaM * pOrigRg = 0 );
163 :
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();
178 :
179 : /// @@@ semantic: no copy ctor.
180 : SwPaM( SwPaM & );
181 : /// @@@ semantic: no copy assignment for super class Ring.
182 : SwPaM& operator=( const SwPaM & );
183 :
184 : /// Movement of cursor.
185 : bool Move( SwMoveFn fnMove = fnMoveForward,
186 : SwGoInDoc fnGo = fnGoCntnt );
187 :
188 : /// Search.
189 : bool Find( const com::sun::star::util::SearchOptions& rSearchOpt,
190 : bool bSearchInNotes,
191 : utl::TextSearch& rSTxt,
192 : SwMoveFn fnMove = fnMoveForward,
193 : const SwPaM *pPam =0, bool bInReadOnly = false);
194 : bool Find( const SwFmt& rFmt,
195 : SwMoveFn fnMove = fnMoveForward,
196 : const SwPaM *pPam =0, bool bInReadOnly = false);
197 : bool Find( const SfxPoolItem& rAttr, bool bValue = true,
198 : SwMoveFn fnMove = fnMoveForward,
199 : const SwPaM *pPam =0, bool bInReadOnly = false );
200 : bool Find( const SfxItemSet& rAttr, bool bNoColls,
201 : SwMoveFn fnMove,
202 : const SwPaM *pPam, bool bInReadOnly, bool bMoveFirst );
203 :
204 : bool DoSearch( const com::sun::star::util::SearchOptions& rSearchOpt, utl::TextSearch& rSTxt,
205 : SwMoveFn fnMove, bool bSrchForward, bool bRegSearch, bool bChkEmptyPara, bool bChkParaEnd,
206 : xub_StrLen &nStart, xub_StrLen &nEnde,xub_StrLen nTxtLen,SwNode* pNode, SwPaM* pPam);
207 :
208 72218 : inline bool IsInFrontOfLabel() const { return m_bIsInFrontOfLabel; }
209 0 : inline void _SetInFrontOfLabel( bool bNew ) { m_bIsInFrontOfLabel = bNew; }
210 :
211 : /// Unless this is called, the getter method of Mark will return Point.
212 : virtual void SetMark();
213 :
214 17232 : void DeleteMark()
215 : {
216 17232 : if (m_pMark != m_pPoint)
217 : {
218 : /** clear the mark position; this helps if mark's SwIndex is
219 : registered at some node, and that node is then deleted */
220 6913 : *m_pMark = SwPosition( SwNodeIndex( GetNode()->GetNodes() ) );
221 6913 : m_pMark = m_pPoint;
222 : }
223 17232 : }
224 : #ifdef DBG_UTIL
225 : void Exchange();
226 :
227 : #else
228 2262 : void Exchange()
229 : {
230 2262 : if (m_pPoint != m_pMark)
231 : {
232 2105 : SwPosition *pTmp = m_pPoint;
233 2105 : m_pPoint = m_pMark;
234 2105 : m_pMark = pTmp;
235 : }
236 2262 : }
237 : #endif
238 :
239 : /** A PaM marks a selection if Point and Mark are distinct positions.
240 : @return true iff the PaM spans a selection
241 : */
242 328174 : bool HasMark() const { return m_pPoint == m_pMark ? false : true; }
243 :
244 457030 : const SwPosition *GetPoint() const { return m_pPoint; }
245 1274676 : SwPosition *GetPoint() { return m_pPoint; }
246 82215 : const SwPosition *GetMark() const { return m_pMark; }
247 217994 : SwPosition *GetMark() { return m_pMark; }
248 :
249 49575 : const SwPosition *Start() const
250 49575 : { return (*m_pPoint) <= (*m_pMark) ? m_pPoint : m_pMark; }
251 116493 : SwPosition *Start()
252 116493 : { return (*m_pPoint) <= (*m_pMark) ? m_pPoint : m_pMark; }
253 :
254 42498 : const SwPosition *End() const
255 42498 : { return (*m_pPoint) > (*m_pMark) ? m_pPoint : m_pMark; }
256 76228 : SwPosition *End()
257 76228 : { return (*m_pPoint) > (*m_pMark) ? m_pPoint : m_pMark; }
258 :
259 : /// @return current Node at Point/Mark
260 271191 : SwNode * GetNode ( bool bPoint = true ) const
261 : {
262 271191 : return &( bPoint ? m_pPoint->nNode : m_pMark->nNode ).GetNode();
263 : }
264 :
265 : /// @return current ContentNode at Point/Mark
266 154902 : SwCntntNode* GetCntntNode( bool bPoint = true ) const
267 : {
268 154902 : return GetNode(bPoint)->GetCntntNode();
269 : }
270 :
271 : /**
272 : Normalizes PaM, i.e. sort point and mark.
273 :
274 : @param bPointFirst true: If the point is behind the mark then swap.
275 : false: If the mark is behind the point then swap.
276 : */
277 : SwPaM & Normalize(bool bPointFirst = true);
278 :
279 : /// @return the document (SwDoc) at which the PaM is registered
280 250055 : SwDoc* GetDoc() const { return m_pPoint->nNode.GetNode().GetDoc(); }
281 :
282 24501 : SwPosition& GetBound( bool bOne = true )
283 24501 : { return bOne ? m_Bound1 : m_Bound2; }
284 9870 : const SwPosition& GetBound( bool bOne = true ) const
285 9870 : { return bOne ? m_Bound1 : m_Bound2; }
286 :
287 : /// Get number of page which contains cursor.
288 : sal_uInt16 GetPageNum( bool bAtPoint = true, const Point* pLayPos = 0 );
289 :
290 : /** Is in something protected (readonly) or selection contains
291 : something protected. */
292 : bool HasReadonlySel( bool bFormView, bool bAnnotationMode = false ) const;
293 :
294 7591 : bool ContainsPosition(const SwPosition & rPos)
295 : {
296 7591 : return *Start() <= rPos && rPos <= *End();
297 : }
298 :
299 32369 : DECL_FIXEDMEMPOOL_NEWDEL(SwPaM);
300 :
301 : OUString GetTxt() const;
302 : void InvalidatePaM();
303 : };
304 :
305 :
306 : bool CheckNodesRange( const SwNodeIndex&, const SwNodeIndex&, bool bChkSection );
307 : bool GoInCntnt( SwPaM & rPam, SwMoveFn fnMove );
308 :
309 :
310 : #endif // _PAM_HXX
311 :
312 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|