xref: /trunk/main/sw/inc/pam.hxx (revision 1d2dbeb0)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 #ifndef _PAM_HXX
24 #define _PAM_HXX
25 
26 #include <stddef.h>			// fuer MemPool
27 #include <tools/gen.hxx>
28 #include <tools/mempool.hxx>
29 #include <cshtyp.hxx>		// fuer die Funktions-Definitionen
30 #include <ring.hxx>			// Superklasse
31 #include <index.hxx>		// fuer SwIndex
32 #include <ndindex.hxx>		// fuer SwNodeIndex
33 #include "swdllapi.h"
34 
35 class SwFmt;
36 class SfxPoolItem;
37 class SfxItemSet;
38 class SwDoc;
39 class SwNode;
40 class SwCntntNode;
41 class SwPaM;
42 
43 namespace com { namespace sun { namespace star { namespace util {
44 	struct SearchOptions;
45 } } } }
46 
47 namespace utl {
48 	class TextSearch;
49 }
50 
51 struct SW_DLLPUBLIC SwPosition
52 {
53 	SwNodeIndex nNode;
54 	SwIndex nContent;
55 
56     SwPosition( const SwNodeIndex &rNode, const SwIndex &rCntnt );
57     explicit SwPosition( const SwNodeIndex &rNode );
58     explicit SwPosition( const SwNode& rNode );
59     explicit SwPosition( SwCntntNode& rNode, const xub_StrLen nOffset = 0 );
60 
61 	SwPosition( const SwPosition & );
62 	SwPosition &operator=(const SwPosition &);
63 
64     // #111827#
65     /**
66        Returns the document this position is in.
67 
68        @return the document this position is in.
69     */
70     SwDoc * GetDoc() const;
71 
72 	sal_Bool operator < (const SwPosition &) const;
73 	sal_Bool operator >	(const SwPosition &) const;
74 	sal_Bool operator <=(const SwPosition &) const;
75 	sal_Bool operator >=(const SwPosition &) const;
76 	sal_Bool operator ==(const SwPosition &) const;
77 	sal_Bool operator !=(const SwPosition &) const;
78 };
79 
80 
81 // das Ergebnis eines Positions Vergleiches
82 enum SwComparePosition {
83 	POS_BEFORE,				// Pos1 liegt vor Pos2
84 	POS_BEHIND,				// Pos1 liegt hinter Pos2
85 	POS_INSIDE,				// Pos1 liegt vollstaendig in Pos2
86 	POS_OUTSIDE,			// Pos2 liegt vollstaendig in Pos1
87 	POS_EQUAL,				// Pos1 ist genauso gross wie Pos2
88 	POS_OVERLAP_BEFORE,		// Pos1 ueberlappt Pos2 am Anfang
89 	POS_OVERLAP_BEHIND,		// Pos1 ueberlappt Pos2 am Ende
90 	POS_COLLIDE_START,		// Pos1 Start stoesst an Pos2 Ende
91 	POS_COLLIDE_END			// Pos1 End stoesst an Pos2 Start
92 };
93 SwComparePosition ComparePosition(
94 			const SwPosition& rStt1, const SwPosition& rEnd1,
95 			const SwPosition& rStt2, const SwPosition& rEnd2 );
96 
97 SwComparePosition ComparePosition(
98 			const unsigned long nStt1, const unsigned long nEnd1,
99 			const unsigned long nStt2, const unsigned long nEnd2 );
100 
101 
102 // SwPointAndMark / SwPaM
103 struct SwMoveFnCollection;
104 typedef SwMoveFnCollection* SwMoveFn;
105 SW_DLLPUBLIC extern SwMoveFn fnMoveForward; // SwPam::Move()/Find() default argument.
106 SW_DLLPUBLIC extern SwMoveFn fnMoveBackward;
107 
108 typedef sal_Bool (*SwGoInDoc)( SwPaM& rPam, SwMoveFn fnMove );
109 SW_DLLPUBLIC extern SwGoInDoc fnGoDoc;
110 extern SwGoInDoc fnGoSection;
111 SW_DLLPUBLIC extern SwGoInDoc fnGoNode;
112 SW_DLLPUBLIC extern SwGoInDoc fnGoCntnt; // SwPam::Move() default argument.
113 extern SwGoInDoc fnGoCntntCells;
114 extern SwGoInDoc fnGoCntntSkipHidden;
115 extern SwGoInDoc fnGoCntntCellsSkipHidden;
116 
117 void _InitPam();
118 
119 class SW_DLLPUBLIC SwPaM : public Ring
120 {
121     SwPosition   m_Bound1;
122     SwPosition   m_Bound2;
123     SwPosition * m_pPoint; // points at either m_Bound1 or m_Bound2
124     SwPosition * m_pMark;  // points at either m_Bound1 or m_Bound2
125     bool m_bIsInFrontOfLabel;
126 
127 	SwPaM* MakeRegion( SwMoveFn fnMove, const SwPaM * pOrigRg = 0 );
128 
129 public:
130 	SwPaM( const SwPosition& rPos, SwPaM* pRing = 0 );
131 	SwPaM( const SwPosition& rMk, const SwPosition& rPt, SwPaM* pRing = 0 );
132 	SwPaM( const SwNodeIndex& rMk, const SwNodeIndex& rPt,
133 		   long nMkOffset = 0, long nPtOffset = 0, SwPaM* pRing = 0 );
134 	SwPaM( const SwNode& rMk, const SwNode& rPt,
135 		   long nMkOffset = 0, long nPtOffset = 0, SwPaM* pRing = 0 );
136 	SwPaM(	const SwNodeIndex& rMk, xub_StrLen nMkCntnt,
137 			const SwNodeIndex& rPt, xub_StrLen nPtCntnt, SwPaM* pRing = 0 );
138 	SwPaM(	const SwNode& rMk, xub_StrLen nMkCntnt,
139 			const SwNode& rPt, xub_StrLen nPtCntnt, SwPaM* pRing = 0 );
140 	SwPaM( const SwNode& rNd, xub_StrLen nCntnt = 0, SwPaM* pRing = 0 );
141 	SwPaM( const SwNodeIndex& rNd, xub_StrLen nCntnt = 0, SwPaM* pRing = 0 );
142 	virtual ~SwPaM();
143 
144 	// @@@ semantic: no copy ctor.
145 	SwPaM( SwPaM & );
146 	// @@@ semantic: no copy assignment for super class Ring.
147 	SwPaM& operator=( const SwPaM & );
148 
149 	// Bewegen des Cursors
150 	sal_Bool Move( SwMoveFn fnMove = fnMoveForward,
151 					SwGoInDoc fnGo = fnGoCntnt );
152 
153 	// Suchen
154 	sal_uInt8 Find(	const com::sun::star::util::SearchOptions& rSearchOpt,
155 				sal_Bool bSearchInNotes,
156 				utl::TextSearch& rSTxt,
157 				SwMoveFn fnMove = fnMoveForward,
158 				const SwPaM *pPam =0, sal_Bool bInReadOnly = sal_False);
159 	sal_Bool Find(	const SwFmt& rFmt,
160 				SwMoveFn fnMove = fnMoveForward,
161 				const SwPaM *pPam =0, sal_Bool bInReadOnly = sal_False);
162 	sal_Bool Find(	const SfxPoolItem& rAttr, sal_Bool bValue = sal_True,
163 				SwMoveFn fnMove = fnMoveForward,
164 				const SwPaM *pPam =0, sal_Bool bInReadOnly = sal_False );
165 	sal_Bool Find(	const SfxItemSet& rAttr, sal_Bool bNoColls,
166 				SwMoveFn fnMove,
167 				const SwPaM *pPam, sal_Bool bInReadOnly, sal_Bool bMoveFirst );
168 
169 	bool DoSearch( const com::sun::star::util::SearchOptions& rSearchOpt, utl::TextSearch& rSTxt,
170 					SwMoveFn fnMove, sal_Bool bSrchForward, sal_Bool bRegSearch, sal_Bool bChkEmptyPara, sal_Bool bChkParaEnd,
171 					xub_StrLen &nStart, xub_StrLen &nEnde,xub_StrLen nTxtLen,SwNode* pNode, SwPaM* pPam);
172 
173     inline bool IsInFrontOfLabel() const        { return m_bIsInFrontOfLabel; }
174     inline void _SetInFrontOfLabel( bool bNew ) { m_bIsInFrontOfLabel = bNew; }
175 
176     virtual void SetMark();
177 
178     void DeleteMark()
179     {
180         if (m_pMark != m_pPoint)
181         {
182             // clear the mark position; this helps if mark's SwIndex is
183             // registered at some node, and that node is then deleted
184             *m_pMark = SwPosition( SwNodeIndex( GetNode()->GetNodes() ) );
185             m_pMark = m_pPoint;
186         }
187     }
188 #ifndef DBG_UTIL
189 
190     void Exchange()
191     {
192         if (m_pPoint != m_pMark)
193         {
194             SwPosition *pTmp = m_pPoint;
195             m_pPoint = m_pMark;
196             m_pMark = pTmp;
197         }
198     }
199 #else
200     void Exchange();
201 #endif
202 
203     /** A PaM marks a selection if Point and Mark are distinct positions.
204         @return     true iff the PaM spans a selection
205      */
206     bool HasMark() const { return m_pPoint == m_pMark ? false : true; }
207 
208     const SwPosition *GetPoint() const { return m_pPoint; }
209           SwPosition *GetPoint()       { return m_pPoint; }
210     const SwPosition *GetMark()  const { return m_pMark; }
211           SwPosition *GetMark()        { return m_pMark; }
212 
213     const SwPosition *Start() const
214                 { return (*m_pPoint) <= (*m_pMark) ? m_pPoint : m_pMark; }
215           SwPosition *Start()
216                 { return (*m_pPoint) <= (*m_pMark) ? m_pPoint : m_pMark; }
217 
218     const SwPosition *End()   const
219                 { return (*m_pPoint) >  (*m_pMark) ? m_pPoint : m_pMark; }
220           SwPosition *End()
221                 { return (*m_pPoint) >  (*m_pMark) ? m_pPoint : m_pMark; }
222 
223     /// @return current Node at Point/Mark
224     SwNode    * GetNode      ( bool bPoint = true ) const
225     {
226         return &( bPoint ? m_pPoint->nNode : m_pMark->nNode ).GetNode();
227     }
228 
229     /// @return current ContentNode at Point/Mark
230     SwCntntNode* GetCntntNode( bool bPoint = true ) const
231     {
232         return GetNode(bPoint)->GetCntntNode();
233     }
234 
235     /**
236        Normalizes PaM, i.e. sort point and mark.
237 
238        @param bPointFirst sal_True: If the point is behind the mark then swap.
239                           sal_False: If the mark is behind the point then swap.
240     */
241     SwPaM & Normalize(sal_Bool bPointFirst = sal_True);
242 
243     /// @return the document (SwDoc) at which the PaM is registered
244     SwDoc* GetDoc() const   { return m_pPoint->nNode.GetNode().GetDoc(); }
245 
246           SwPosition& GetBound( bool bOne = true )
247                             { return bOne ? m_Bound1 : m_Bound2; }
248     const SwPosition& GetBound( bool bOne = true ) const
249                             { return bOne ? m_Bound1 : m_Bound2; }
250 
251 	// erfrage die Seitennummer auf der der Cursor steht
252 	sal_uInt16 GetPageNum( sal_Bool bAtPoint = sal_True, const Point* pLayPos = 0 );
253 
254 	// steht in etwas geschuetztem oder in die Selektion umspannt
255 	// etwas geschuetztes.
256     sal_Bool HasReadonlySel( bool bFormView ) const;
257 
258     sal_Bool ContainsPosition(const SwPosition & rPos)
259     { return *Start() <= rPos && rPos <= *End(); }
260 
261     static sal_Bool Overlap(const SwPaM & a, const SwPaM & b);
262 
263     static sal_Bool LessThan(const SwPaM & a, const SwPaM & b);
264 
265 	DECL_FIXEDMEMPOOL_NEWDEL(SwPaM);
266 
267     String GetTxt() const;
268     void InvalidatePaM();
269 };
270 
271 
272 sal_Bool CheckNodesRange( const SwNodeIndex&, const SwNodeIndex&, sal_Bool );
273 sal_Bool GoInCntnt( SwPaM & rPam, SwMoveFn fnMove );
274 
275 
276 #endif	// _PAM_HXX
277