Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : : #ifndef _PAM_HXX
29 : : #define _PAM_HXX
30 : :
31 : : #include <stddef.h> // For MemPool.
32 : : #include <tools/gen.hxx>
33 : : #include <tools/mempool.hxx>
34 : : #include <cshtyp.hxx> // For function definitions.
35 : : #include <ring.hxx> // Super class.
36 : : #include <index.hxx> // For SwIndex.
37 : : #include <ndindex.hxx> // For SwNodeIndex.
38 : : #include "swdllapi.h"
39 : :
40 : : class SwFmt;
41 : : class SfxPoolItem;
42 : : class SfxItemSet;
43 : : class SwDoc;
44 : : class SwNode;
45 : : class SwCntntNode;
46 : : class SwPaM;
47 : :
48 : : namespace com { namespace sun { namespace star { namespace util {
49 : : struct SearchOptions;
50 : : } } } }
51 : :
52 : : namespace utl {
53 : : class TextSearch;
54 : : }
55 : :
56 : : /// Marks a position in the document model.
57 [ + - ]: 1649360 : struct SW_DLLPUBLIC SwPosition
58 : : {
59 : : SwNodeIndex nNode;
60 : : SwIndex nContent;
61 : :
62 : : SwPosition( const SwNodeIndex &rNode, const SwIndex &rCntnt );
63 : : explicit SwPosition( const SwNodeIndex &rNode );
64 : : explicit SwPosition( const SwNode& rNode );
65 : : explicit SwPosition( SwCntntNode& rNode, const xub_StrLen nOffset = 0 );
66 : :
67 : : SwPosition( const SwPosition & );
68 : : SwPosition &operator=(const SwPosition &);
69 : :
70 : : /**
71 : : Returns the document this position is in.
72 : :
73 : : @return the document this position is in.
74 : : */
75 : : SwDoc * GetDoc() const;
76 : :
77 : : sal_Bool operator < (const SwPosition &) const;
78 : : sal_Bool operator > (const SwPosition &) const;
79 : : sal_Bool operator <=(const SwPosition &) const;
80 : : sal_Bool operator >=(const SwPosition &) const;
81 : : sal_Bool operator ==(const SwPosition &) const;
82 : : sal_Bool operator !=(const SwPosition &) const;
83 : : };
84 : :
85 : :
86 : : // Result of comparing positions.
87 : : enum SwComparePosition {
88 : : POS_BEFORE, // Pos1 before Pos2.
89 : : POS_BEHIND, // Pos1 behind Pos2.
90 : : POS_INSIDE, // Pos1 completely contained in Pos2.
91 : : POS_OUTSIDE, // Pos2 completely contained in Pos1.
92 : : POS_EQUAL, // Pos1 is as large as Pos2.
93 : : POS_OVERLAP_BEFORE, // Pos1 overlaps Pos2 at the beginning.
94 : : POS_OVERLAP_BEHIND, // Pos1 overlaps Pos2 at the end.
95 : : POS_COLLIDE_START, // Pos1 start touches at Pos2 end.
96 : : POS_COLLIDE_END // Pos1 end touches at Pos2 start.
97 : : };
98 : :
99 : : template<typename T>
100 : 2142 : SwComparePosition ComparePosition(
101 : : const T& rStt1, const T& rEnd1,
102 : : const T& rStt2, const T& rEnd2 )
103 : : {
104 : : SwComparePosition nRet;
105 [ + + + ]: 2142 : if( rStt1 < rStt2 )
106 : : {
107 [ + + - ]: 848 : if( rEnd1 > rStt2 )
108 : : {
109 [ - + - ]: 848 : if( rEnd1 >= rEnd2 )
110 : 838 : nRet = POS_OUTSIDE;
111 : : else
112 : 10 : nRet = POS_OVERLAP_BEFORE;
113 : :
114 : : }
115 [ # # # ]: 0 : else if( rEnd1 == rStt2 )
116 : 0 : nRet = POS_COLLIDE_END;
117 : : else
118 : 0 : nRet = POS_BEFORE;
119 : : }
120 [ + + + ]: 1294 : else if( rEnd2 > rStt1 )
121 : : {
122 [ + + + ]: 26 : if( rEnd2 >= rEnd1 )
123 : : {
124 [ + - # ]: 2 : if( rEnd2 == rEnd1 && rStt2 == rStt1 )
[ - + # # ]
[ # # ]
125 : 0 : nRet = POS_EQUAL;
126 : : else
127 : 2 : nRet = POS_INSIDE;
128 : : }
129 : : else
130 : : {
131 [ - + - ]: 24 : if (rStt1 == rStt2)
132 : 14 : nRet = POS_OUTSIDE;
133 : : else
134 : 10 : nRet = POS_OVERLAP_BEHIND;
135 : : }
136 : : }
137 [ + + + ]: 1268 : else if( rEnd2 == rStt1 )
138 : 881 : nRet = POS_COLLIDE_START;
139 : : else
140 : 387 : nRet = POS_BEHIND;
141 : 2142 : return nRet;
142 : : }
143 : :
144 : : // SwPointAndMark / SwPaM
145 : : struct SwMoveFnCollection;
146 : : typedef SwMoveFnCollection* SwMoveFn;
147 : : SW_DLLPUBLIC extern SwMoveFn fnMoveForward; // SwPam::Move()/Find() default argument.
148 : : SW_DLLPUBLIC extern SwMoveFn fnMoveBackward;
149 : :
150 : : typedef sal_Bool (*SwGoInDoc)( SwPaM& rPam, SwMoveFn fnMove );
151 : : SW_DLLPUBLIC extern SwGoInDoc fnGoDoc;
152 : : extern SwGoInDoc fnGoSection;
153 : : SW_DLLPUBLIC extern SwGoInDoc fnGoNode;
154 : : SW_DLLPUBLIC extern SwGoInDoc fnGoCntnt; // SwPam::Move() default argument.
155 : : extern SwGoInDoc fnGoCntntCells;
156 : : extern SwGoInDoc fnGoCntntSkipHidden;
157 : : extern SwGoInDoc fnGoCntntCellsSkipHidden;
158 : :
159 : : void _InitPam();
160 : :
161 : : /// PaM is Point and Mark: a selection of the document model.
162 : : class SW_DLLPUBLIC SwPaM : public Ring
163 : : {
164 : : SwPosition m_Bound1;
165 : : SwPosition m_Bound2;
166 : : SwPosition * m_pPoint; // points at either m_Bound1 or m_Bound2
167 : : SwPosition * m_pMark; // points at either m_Bound1 or m_Bound2
168 : : bool m_bIsInFrontOfLabel;
169 : :
170 : : SwPaM* MakeRegion( SwMoveFn fnMove, const SwPaM * pOrigRg = 0 );
171 : :
172 : : public:
173 : : SwPaM( const SwPosition& rPos, SwPaM* pRing = 0 );
174 : : SwPaM( const SwPosition& rMk, const SwPosition& rPt, SwPaM* pRing = 0 );
175 : : SwPaM( const SwNodeIndex& rMk, const SwNodeIndex& rPt,
176 : : long nMkOffset = 0, long nPtOffset = 0, SwPaM* pRing = 0 );
177 : : SwPaM( const SwNode& rMk, const SwNode& rPt,
178 : : long nMkOffset = 0, long nPtOffset = 0, SwPaM* pRing = 0 );
179 : : SwPaM( const SwNodeIndex& rMk, xub_StrLen nMkCntnt,
180 : : const SwNodeIndex& rPt, xub_StrLen nPtCntnt, SwPaM* pRing = 0 );
181 : : SwPaM( const SwNode& rMk, xub_StrLen nMkCntnt,
182 : : const SwNode& rPt, xub_StrLen nPtCntnt, SwPaM* pRing = 0 );
183 : : SwPaM( const SwNode& rNd, xub_StrLen nCntnt = 0, SwPaM* pRing = 0 );
184 : : SwPaM( const SwNodeIndex& rNd, xub_StrLen nCntnt = 0, SwPaM* pRing = 0 );
185 : : virtual ~SwPaM();
186 : :
187 : : // @@@ semantic: no copy ctor.
188 : : SwPaM( SwPaM & );
189 : : // @@@ semantic: no copy assignment for super class Ring.
190 : : SwPaM& operator=( const SwPaM & );
191 : :
192 : : // Movement of cursor.
193 : : sal_Bool Move( SwMoveFn fnMove = fnMoveForward,
194 : : SwGoInDoc fnGo = fnGoCntnt );
195 : :
196 : : // Search.
197 : : sal_uInt8 Find( const com::sun::star::util::SearchOptions& rSearchOpt,
198 : : sal_Bool bSearchInNotes,
199 : : utl::TextSearch& rSTxt,
200 : : SwMoveFn fnMove = fnMoveForward,
201 : : const SwPaM *pPam =0, sal_Bool bInReadOnly = sal_False);
202 : : sal_Bool Find( const SwFmt& rFmt,
203 : : SwMoveFn fnMove = fnMoveForward,
204 : : const SwPaM *pPam =0, sal_Bool bInReadOnly = sal_False);
205 : : sal_Bool Find( const SfxPoolItem& rAttr, sal_Bool bValue = sal_True,
206 : : SwMoveFn fnMove = fnMoveForward,
207 : : const SwPaM *pPam =0, sal_Bool bInReadOnly = sal_False );
208 : : sal_Bool Find( const SfxItemSet& rAttr, sal_Bool bNoColls,
209 : : SwMoveFn fnMove,
210 : : const SwPaM *pPam, sal_Bool bInReadOnly, sal_Bool bMoveFirst );
211 : :
212 : : bool DoSearch( const com::sun::star::util::SearchOptions& rSearchOpt, utl::TextSearch& rSTxt,
213 : : SwMoveFn fnMove, sal_Bool bSrchForward, sal_Bool bRegSearch, sal_Bool bChkEmptyPara, sal_Bool bChkParaEnd,
214 : : xub_StrLen &nStart, xub_StrLen &nEnde,xub_StrLen nTxtLen,SwNode* pNode, SwPaM* pPam);
215 : :
216 : 124239 : inline bool IsInFrontOfLabel() const { return m_bIsInFrontOfLabel; }
217 : 0 : inline void _SetInFrontOfLabel( bool bNew ) { m_bIsInFrontOfLabel = bNew; }
218 : :
219 : : virtual void SetMark();
220 : :
221 : 25548 : void DeleteMark()
222 : : {
223 [ + + ]: 25548 : if (m_pMark != m_pPoint)
224 : : {
225 : : // clear the mark position; this helps if mark's SwIndex is
226 : : // registered at some node, and that node is then deleted
227 [ + - ][ + - ]: 12491 : *m_pMark = SwPosition( SwNodeIndex( GetNode()->GetNodes() ) );
[ + - ]
228 : 12491 : m_pMark = m_pPoint;
229 : : }
230 : 25548 : }
231 : : #ifdef DBG_UTIL
232 : : void Exchange();
233 : :
234 : : #else
235 : 3038 : void Exchange()
236 : : {
237 [ + + ]: 3038 : if (m_pPoint != m_pMark)
238 : : {
239 : 2999 : SwPosition *pTmp = m_pPoint;
240 : 2999 : m_pPoint = m_pMark;
241 : 2999 : m_pMark = pTmp;
242 : : }
243 : 3038 : }
244 : : #endif
245 : :
246 : : /** A PaM marks a selection if Point and Mark are distinct positions.
247 : : @return true iff the PaM spans a selection
248 : : */
249 : 613618 : bool HasMark() const { return m_pPoint == m_pMark ? false : true; }
250 : :
251 : 826060 : const SwPosition *GetPoint() const { return m_pPoint; }
252 : 2384481 : SwPosition *GetPoint() { return m_pPoint; }
253 : 110221 : const SwPosition *GetMark() const { return m_pMark; }
254 : 429074 : SwPosition *GetMark() { return m_pMark; }
255 : :
256 : 77353 : const SwPosition *Start() const
257 [ + + ]: 77353 : { return (*m_pPoint) <= (*m_pMark) ? m_pPoint : m_pMark; }
258 : 229554 : SwPosition *Start()
259 [ + + ]: 229554 : { return (*m_pPoint) <= (*m_pMark) ? m_pPoint : m_pMark; }
260 : :
261 : 60618 : const SwPosition *End() const
262 [ + + ]: 60618 : { return (*m_pPoint) > (*m_pMark) ? m_pPoint : m_pMark; }
263 : 168627 : SwPosition *End()
264 [ + + ]: 168627 : { return (*m_pPoint) > (*m_pMark) ? m_pPoint : m_pMark; }
265 : :
266 : : /// @return current Node at Point/Mark
267 : 520858 : SwNode * GetNode ( bool bPoint = true ) const
268 : : {
269 [ + + ]: 520858 : return &( bPoint ? m_pPoint->nNode : m_pMark->nNode ).GetNode();
270 : : }
271 : :
272 : : /// @return current ContentNode at Point/Mark
273 : 317010 : SwCntntNode* GetCntntNode( bool bPoint = true ) const
274 : : {
275 : 317010 : return GetNode(bPoint)->GetCntntNode();
276 : : }
277 : :
278 : : /**
279 : : Normalizes PaM, i.e. sort point and mark.
280 : :
281 : : @param bPointFirst sal_True: If the point is behind the mark then swap.
282 : : sal_False: If the mark is behind the point then swap.
283 : : */
284 : : SwPaM & Normalize(sal_Bool bPointFirst = sal_True);
285 : :
286 : : /// @return the document (SwDoc) at which the PaM is registered
287 : 464207 : SwDoc* GetDoc() const { return m_pPoint->nNode.GetNode().GetDoc(); }
288 : :
289 : 42665 : SwPosition& GetBound( bool bOne = true )
290 [ + + ]: 42665 : { return bOne ? m_Bound1 : m_Bound2; }
291 : 18282 : const SwPosition& GetBound( bool bOne = true ) const
292 [ + + ]: 18282 : { return bOne ? m_Bound1 : m_Bound2; }
293 : :
294 : : // Get number of page which contains cursor.
295 : : sal_uInt16 GetPageNum( sal_Bool bAtPoint = sal_True, const Point* pLayPos = 0 );
296 : :
297 : : // Is in something protected (readonly) or selection contains
298 : : // something protected.
299 : : sal_Bool HasReadonlySel( bool bFormView ) const;
300 : :
301 : 19239 : sal_Bool ContainsPosition(const SwPosition & rPos)
302 [ + + ][ + + ]: 19239 : { return *Start() <= rPos && rPos <= *End(); }
303 : :
304 [ + + ][ + + ]: 55059 : DECL_FIXEDMEMPOOL_NEWDEL(SwPaM);
305 : :
306 : : String GetTxt() const;
307 : : void InvalidatePaM();
308 : : };
309 : :
310 : :
311 : : sal_Bool CheckNodesRange( const SwNodeIndex&, const SwNodeIndex&, sal_Bool );
312 : : sal_Bool GoInCntnt( SwPaM & rPam, SwMoveFn fnMove );
313 : :
314 : :
315 : : #endif // _PAM_HXX
316 : :
317 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|