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 INCLUDED_SW_INC_PAM_HXX
20 : #define INCLUDED_SW_INC_PAM_HXX
21 :
22 : #include <stddef.h>
23 : #include <sal/types.h>
24 : #include <tools/mempool.hxx>
25 : #include <cshtyp.hxx>
26 : #include <ring.hxx>
27 : #include <index.hxx>
28 : #include <ndindex.hxx>
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 6471080 : 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 sal_Int32 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 : // Result of comparing positions.
78 : enum SwComparePosition {
79 : POS_BEFORE, ///< Pos1 before Pos2.
80 : POS_BEHIND, ///< Pos1 behind Pos2.
81 : POS_INSIDE, ///< Pos1 completely contained in Pos2.
82 : POS_OUTSIDE, ///< Pos2 completely contained in Pos1.
83 : POS_EQUAL, ///< Pos1 is as large as Pos2.
84 : POS_OVERLAP_BEFORE, ///< Pos1 overlaps Pos2 at the beginning.
85 : POS_OVERLAP_BEHIND, ///< Pos1 overlaps Pos2 at the end.
86 : POS_COLLIDE_START, ///< Pos1 start touches at Pos2 end.
87 : POS_COLLIDE_END ///< Pos1 end touches at Pos2 start.
88 : };
89 :
90 : template<typename T>
91 163732 : SwComparePosition ComparePosition(
92 : const T& rStt1, const T& rEnd1,
93 : const T& rStt2, const T& rEnd2 )
94 : {
95 : SwComparePosition nRet;
96 163732 : if( rStt1 < rStt2 )
97 : {
98 8803 : if( rEnd1 > rStt2 )
99 : {
100 7937 : if( rEnd1 >= rEnd2 )
101 7927 : nRet = POS_OUTSIDE;
102 : else
103 10 : nRet = POS_OVERLAP_BEFORE;
104 :
105 : }
106 866 : else if( rEnd1 == rStt2 )
107 4 : nRet = POS_COLLIDE_END;
108 : else
109 862 : nRet = POS_BEFORE;
110 : }
111 154929 : else if( rEnd2 > rStt1 )
112 : {
113 1735 : if( rEnd2 >= rEnd1 )
114 : {
115 1712 : if( rEnd2 == rEnd1 && rStt2 == rStt1 )
116 1710 : nRet = POS_EQUAL;
117 : else
118 2 : nRet = POS_INSIDE;
119 : }
120 : else
121 : {
122 23 : if (rStt1 == rStt2)
123 13 : nRet = POS_OUTSIDE;
124 : else
125 10 : nRet = POS_OVERLAP_BEHIND;
126 : }
127 : }
128 153194 : else if( rEnd2 == rStt1 )
129 3064 : nRet = POS_COLLIDE_START;
130 : else
131 150130 : nRet = POS_BEHIND;
132 163732 : return nRet;
133 : }
134 :
135 : /// SwPointAndMark / SwPaM
136 : struct SwMoveFnCollection;
137 : typedef SwMoveFnCollection* SwMoveFn;
138 : SW_DLLPUBLIC extern SwMoveFn fnMoveForward; ///< SwPam::Move()/Find() default argument.
139 : SW_DLLPUBLIC extern SwMoveFn fnMoveBackward;
140 :
141 : typedef bool (*SwGoInDoc)( SwPaM& rPam, SwMoveFn fnMove );
142 : SW_DLLPUBLIC extern SwGoInDoc fnGoDoc;
143 : extern SwGoInDoc fnGoSection;
144 : SW_DLLPUBLIC extern SwGoInDoc fnGoNode;
145 : SW_DLLPUBLIC extern SwGoInDoc fnGoCntnt; ///< SwPam::Move() default argument.
146 : extern SwGoInDoc fnGoCntntCells;
147 : extern SwGoInDoc fnGoCntntSkipHidden;
148 : extern SwGoInDoc fnGoCntntCellsSkipHidden;
149 :
150 : void _InitPam();
151 :
152 : /// PaM is Point and Mark: a selection of the document model.
153 : class SW_DLLPUBLIC SwPaM : public Ring
154 : {
155 : SwPosition m_Bound1;
156 : SwPosition m_Bound2;
157 : SwPosition * m_pPoint; ///< points at either m_Bound1 or m_Bound2
158 : SwPosition * m_pMark; ///< points at either m_Bound1 or m_Bound2
159 : bool m_bIsInFrontOfLabel;
160 :
161 : SwPaM* MakeRegion( SwMoveFn fnMove, const SwPaM * pOrigRg = 0 );
162 :
163 : public:
164 : SwPaM( const SwPosition& rPos, SwPaM* pRing = 0 );
165 : SwPaM( const SwPosition& rMk, const SwPosition& rPt, SwPaM* pRing = 0 );
166 : SwPaM( const SwNodeIndex& rMk, const SwNodeIndex& rPt,
167 : long nMkOffset = 0, long nPtOffset = 0, SwPaM* pRing = 0 );
168 : SwPaM( const SwNode& rMk, const SwNode& rPt,
169 : long nMkOffset = 0, long nPtOffset = 0, SwPaM* pRing = 0 );
170 : SwPaM( const SwNodeIndex& rMk, sal_Int32 nMkCntnt,
171 : const SwNodeIndex& rPt, sal_Int32 nPtCntnt, SwPaM* pRing = 0 );
172 : SwPaM( const SwNode& rMk, sal_Int32 nMkCntnt,
173 : const SwNode& rPt, sal_Int32 nPtCntnt, SwPaM* pRing = 0 );
174 : SwPaM( const SwNode& rNd, sal_Int32 nCntnt = 0, SwPaM* pRing = 0 );
175 : SwPaM( const SwNodeIndex& rNd, sal_Int32 nCntnt = 0, SwPaM* pRing = 0 );
176 : virtual ~SwPaM();
177 :
178 : /// @@@ semantic: no copy ctor.
179 : SwPaM( SwPaM & );
180 : /// @@@ semantic: no copy assignment for super class Ring.
181 : SwPaM& operator=( const SwPaM & );
182 :
183 : /// Movement of cursor.
184 : bool Move( SwMoveFn fnMove = fnMoveForward,
185 : SwGoInDoc fnGo = fnGoCntnt );
186 :
187 : /// Search.
188 : bool Find( const com::sun::star::util::SearchOptions& rSearchOpt,
189 : bool bSearchInNotes,
190 : utl::TextSearch& rSTxt,
191 : SwMoveFn fnMove = fnMoveForward,
192 : const SwPaM *pPam =0, bool bInReadOnly = false);
193 : bool Find( const SwFmt& rFmt,
194 : SwMoveFn fnMove = fnMoveForward,
195 : const SwPaM *pPam =0, bool bInReadOnly = false);
196 : bool Find( const SfxPoolItem& rAttr, bool bValue = true,
197 : SwMoveFn fnMove = fnMoveForward,
198 : const SwPaM *pPam =0, bool bInReadOnly = false );
199 : bool Find( const SfxItemSet& rAttr, bool bNoColls,
200 : SwMoveFn fnMove,
201 : const SwPaM *pPam, bool bInReadOnly, bool bMoveFirst );
202 :
203 : bool DoSearch( const com::sun::star::util::SearchOptions& rSearchOpt, utl::TextSearch& rSTxt,
204 : SwMoveFn fnMove, bool bSrchForward, bool bRegSearch, bool bChkEmptyPara, bool bChkParaEnd,
205 : sal_Int32 &nStart, sal_Int32 &nEnd, sal_Int32 nTxtLen, SwNode* pNode, SwPaM* pPam);
206 :
207 434239 : inline bool IsInFrontOfLabel() const { return m_bIsInFrontOfLabel; }
208 0 : inline void _SetInFrontOfLabel( bool bNew ) { m_bIsInFrontOfLabel = bNew; }
209 :
210 : /// Unless this is called, the getter method of Mark will return Point.
211 : virtual void SetMark();
212 :
213 290963 : void DeleteMark()
214 : {
215 290963 : if (m_pMark != m_pPoint)
216 : {
217 : /** clear the mark position; this helps if mark's SwIndex is
218 : registered at some node, and that node is then deleted */
219 59475 : *m_pMark = SwPosition( SwNodeIndex( GetNode().GetNodes() ) );
220 59475 : m_pMark = m_pPoint;
221 : }
222 290963 : }
223 : #ifdef DBG_UTIL
224 : void Exchange();
225 :
226 : #else
227 13324 : void Exchange()
228 : {
229 13324 : if (m_pPoint != m_pMark)
230 : {
231 10002 : SwPosition *pTmp = m_pPoint;
232 10002 : m_pPoint = m_pMark;
233 10002 : m_pMark = pTmp;
234 : }
235 13324 : }
236 : #endif
237 :
238 : /** A PaM marks a selection if Point and Mark are distinct positions.
239 : @return true if the PaM spans a selection
240 : */
241 2615241 : bool HasMark() const { return m_pPoint != m_pMark; }
242 :
243 3122832 : const SwPosition *GetPoint() const { return m_pPoint; }
244 8401638 : SwPosition *GetPoint() { return m_pPoint; }
245 574923 : const SwPosition *GetMark() const { return m_pMark; }
246 1440139 : SwPosition *GetMark() { return m_pMark; }
247 :
248 631521 : const SwPosition *Start() const
249 631521 : { return (*m_pPoint) <= (*m_pMark) ? m_pPoint : m_pMark; }
250 1466933 : SwPosition *Start()
251 1466933 : { return (*m_pPoint) <= (*m_pMark) ? m_pPoint : m_pMark; }
252 :
253 483438 : const SwPosition *End() const
254 483438 : { return (*m_pPoint) > (*m_pMark) ? m_pPoint : m_pMark; }
255 1013315 : SwPosition *End()
256 1013315 : { return (*m_pPoint) > (*m_pMark) ? m_pPoint : m_pMark; }
257 :
258 : /// @return current Node at Point/Mark
259 1819808 : SwNode & GetNode ( bool bPoint = true ) const
260 : {
261 1819808 : return ( bPoint ? m_pPoint->nNode : m_pMark->nNode ).GetNode();
262 : }
263 :
264 : /// @return current ContentNode at Point/Mark
265 624339 : SwCntntNode* GetCntntNode( bool bPoint = true ) const
266 : {
267 624339 : return GetNode(bPoint).GetCntntNode();
268 : }
269 :
270 : /**
271 : Normalizes PaM, i.e. sort point and mark.
272 :
273 : @param bPointFirst true: If the point is behind the mark then swap.
274 : false: If the mark is behind the point then swap.
275 : */
276 : SwPaM & Normalize(bool bPointFirst = true);
277 :
278 : /// @return the document (SwDoc) at which the PaM is registered
279 2616753 : SwDoc* GetDoc() const { return m_pPoint->nNode.GetNode().GetDoc(); }
280 :
281 3091197 : SwPosition& GetBound( bool bOne = true )
282 3091197 : { return bOne ? m_Bound1 : m_Bound2; }
283 : const SwPosition& GetBound( bool bOne = true ) const
284 : { return bOne ? m_Bound1 : m_Bound2; }
285 :
286 : /// Get number of page which contains cursor.
287 : sal_uInt16 GetPageNum( bool bAtPoint = true, const Point* pLayPos = 0 );
288 :
289 : /** Is in something protected (readonly) or selection contains
290 : something protected. */
291 : bool HasReadonlySel( bool bFormView, bool bAnnotationMode = false ) const;
292 :
293 36866 : bool ContainsPosition(const SwPosition & rPos) const
294 : {
295 36866 : return *Start() <= rPos && rPos <= *End();
296 : }
297 :
298 591817 : DECL_FIXEDMEMPOOL_NEWDEL(SwPaM);
299 :
300 : OUString GetTxt() const;
301 : void InvalidatePaM();
302 : };
303 :
304 : bool CheckNodesRange( const SwNodeIndex&, const SwNodeIndex&, bool bChkSection );
305 :
306 : #endif // INCLUDED_SW_INC_PAM_HXX
307 :
308 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|