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