Branch libreoffice-5-0-4
[LibreOffice.git] / sw / inc / pam.hxx
blob451895460a67a88e9eaec23187240cb3d292ed81
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
19 #ifndef INCLUDED_SW_INC_PAM_HXX
20 #define INCLUDED_SW_INC_PAM_HXX
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"
31 #include <iostream>
33 class SwFormat;
34 class SfxPoolItem;
35 class SfxItemSet;
36 class SwDoc;
37 class SwNode;
38 class SwContentNode;
39 class SwPaM;
40 class Point;
42 namespace com { namespace sun { namespace star { namespace util {
43 struct SearchOptions;
44 } } } }
46 namespace utl {
47 class TextSearch;
50 /// Marks a position in the document model.
51 struct SW_DLLPUBLIC SwPosition
53 SwNodeIndex nNode;
54 SwIndex nContent;
56 SwPosition( const SwNodeIndex &rNode, const SwIndex &rContent );
57 explicit SwPosition( const SwNodeIndex &rNode );
58 explicit SwPosition( const SwNode& rNode );
59 explicit SwPosition( SwContentNode& rNode, const sal_Int32 nOffset = 0 );
61 SwPosition( const SwPosition & );
62 SwPosition &operator=(const SwPosition &);
64 /**
65 Returns the document this position is in.
67 @return the document this position is in.
69 SwDoc * GetDoc() 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 bool operator ==(const SwPosition &) const;
76 bool operator !=(const SwPosition &) const;
77 void dumpAsXml(struct _xmlTextWriter* pWriter) const;
80 std::ostream &operator <<(std::ostream& s, const SwPosition& position);
82 // Result of comparing positions.
83 enum SwComparePosition {
84 POS_BEFORE, ///< Pos1 before Pos2.
85 POS_BEHIND, ///< Pos1 behind Pos2.
86 POS_INSIDE, ///< Pos1 completely contained in Pos2.
87 POS_OUTSIDE, ///< Pos2 completely contained in Pos1.
88 POS_EQUAL, ///< Pos1 is as large as Pos2.
89 POS_OVERLAP_BEFORE, ///< Pos1 overlaps Pos2 at the beginning.
90 POS_OVERLAP_BEHIND, ///< Pos1 overlaps Pos2 at the end.
91 POS_COLLIDE_START, ///< Pos1 start touches at Pos2 end.
92 POS_COLLIDE_END ///< Pos1 end touches at Pos2 start.
95 template<typename T>
96 SwComparePosition ComparePosition(
97 const T& rStt1, const T& rEnd1,
98 const T& rStt2, const T& rEnd2 )
100 SwComparePosition nRet;
101 if( rStt1 < rStt2 )
103 if( rEnd1 > rStt2 )
105 if( rEnd1 >= rEnd2 )
106 nRet = POS_OUTSIDE;
107 else
108 nRet = POS_OVERLAP_BEFORE;
111 else if( rEnd1 == rStt2 )
112 nRet = POS_COLLIDE_END;
113 else
114 nRet = POS_BEFORE;
116 else if( rEnd2 > rStt1 )
118 if( rEnd2 >= rEnd1 )
120 if( rEnd2 == rEnd1 && rStt2 == rStt1 )
121 nRet = POS_EQUAL;
122 else
123 nRet = POS_INSIDE;
125 else
127 if (rStt1 == rStt2)
128 nRet = POS_OUTSIDE;
129 else
130 nRet = POS_OVERLAP_BEHIND;
133 else if( rEnd2 == rStt1 )
134 nRet = POS_COLLIDE_START;
135 else
136 nRet = POS_BEHIND;
137 return nRet;
140 /// SwPointAndMark / SwPaM
141 struct SwMoveFnCollection;
142 typedef SwMoveFnCollection* SwMoveFn;
143 SW_DLLPUBLIC extern SwMoveFn fnMoveForward; ///< SwPam::Move()/Find() default argument.
144 SW_DLLPUBLIC extern SwMoveFn fnMoveBackward;
146 typedef bool (*SwGoInDoc)( SwPaM& rPam, SwMoveFn fnMove );
147 SW_DLLPUBLIC extern SwGoInDoc fnGoDoc;
148 extern SwGoInDoc fnGoSection;
149 SW_DLLPUBLIC extern SwGoInDoc fnGoNode;
150 SW_DLLPUBLIC extern SwGoInDoc fnGoContent; ///< SwPam::Move() default argument.
151 extern SwGoInDoc fnGoContentCells;
152 extern SwGoInDoc fnGoContentSkipHidden;
153 extern SwGoInDoc fnGoContentCellsSkipHidden;
155 void _InitPam();
157 class SwPaM;
158 /// PaM is Point and Mark: a selection of the document model.
159 class SW_DLLPUBLIC SwPaM : public sw::Ring<SwPaM>
161 SwPosition m_Bound1;
162 SwPosition m_Bound2;
163 SwPosition * m_pPoint; ///< points at either m_Bound1 or m_Bound2
164 SwPosition * m_pMark; ///< points at either m_Bound1 or m_Bound2
165 bool m_bIsInFrontOfLabel;
167 SwPaM* MakeRegion( SwMoveFn fnMove, const SwPaM * pOrigRg = 0 );
169 SwPaM(SwPaM const& rPaM) SAL_DELETED_FUNCTION;
171 public:
172 explicit SwPaM( const SwPosition& rPos, SwPaM* pRing = 0 );
173 SwPaM( const SwPosition& rMk, const SwPosition& rPt, SwPaM* pRing = 0 );
174 SwPaM( const SwNodeIndex& rMk, const SwNodeIndex& rPt,
175 long nMkOffset = 0, long nPtOffset = 0, SwPaM* pRing = 0 );
176 SwPaM( const SwNode& rMk, const SwNode& rPt,
177 long nMkOffset = 0, long nPtOffset = 0, SwPaM* pRing = 0 );
178 SwPaM( const SwNodeIndex& rMk, sal_Int32 nMkContent,
179 const SwNodeIndex& rPt, sal_Int32 nPtContent, SwPaM* pRing = 0 );
180 SwPaM( const SwNode& rMk, sal_Int32 nMkContent,
181 const SwNode& rPt, sal_Int32 nPtContent, SwPaM* pRing = 0 );
182 SwPaM( const SwNode& rNd, sal_Int32 nContent = 0, SwPaM* pRing = 0 );
183 SwPaM( const SwNodeIndex& rNd, sal_Int32 nContent = 0, SwPaM* pRing = 0 );
184 virtual ~SwPaM();
186 /// this takes a second parameter, which indicates the Ring that
187 /// the new PaM should be part of (may be null)
188 SwPaM(SwPaM const& rPaM, SwPaM * pRing);
189 /// @@@ semantic: no copy assignment for super class Ring.
190 SwPaM& operator=( const SwPaM & );
192 /// Movement of cursor.
193 bool Move( SwMoveFn fnMove = fnMoveForward,
194 SwGoInDoc fnGo = fnGoContent );
196 /// Search.
197 bool Find( const com::sun::star::util::SearchOptions& rSearchOpt,
198 bool bSearchInNotes,
199 utl::TextSearch& rSText,
200 SwMoveFn fnMove = fnMoveForward,
201 const SwPaM *pPam =0, bool bInReadOnly = false);
202 bool Find( const SwFormat& rFormat,
203 SwMoveFn fnMove = fnMoveForward,
204 const SwPaM *pPam =0, bool bInReadOnly = false);
205 bool Find( const SfxPoolItem& rAttr, bool bValue = true,
206 SwMoveFn fnMove = fnMoveForward,
207 const SwPaM *pPam =0, bool bInReadOnly = false );
208 bool Find( const SfxItemSet& rAttr, bool bNoColls,
209 SwMoveFn fnMove,
210 const SwPaM *pPam, bool bInReadOnly, bool bMoveFirst );
212 bool DoSearch( const com::sun::star::util::SearchOptions& rSearchOpt, utl::TextSearch& rSText,
213 SwMoveFn fnMove, bool bSrchForward, bool bRegSearch, bool bChkEmptyPara, bool bChkParaEnd,
214 sal_Int32 &nStart, sal_Int32 &nEnd, sal_Int32 nTextLen, SwNode* pNode, SwPaM* pPam);
216 inline bool IsInFrontOfLabel() const { return m_bIsInFrontOfLabel; }
217 inline void _SetInFrontOfLabel( bool bNew ) { m_bIsInFrontOfLabel = bNew; }
219 /// Unless this is called, the getter method of Mark will return Point.
220 virtual void SetMark();
222 void DeleteMark()
224 if (m_pMark != m_pPoint)
226 /** clear the mark position; this helps if mark's SwIndex is
227 registered at some node, and that node is then deleted */
228 *m_pMark = SwPosition( SwNodeIndex( GetNode().GetNodes() ) );
229 m_pMark = m_pPoint;
232 #ifdef DBG_UTIL
233 void Exchange();
235 #else
236 void Exchange()
238 if (m_pPoint != m_pMark)
240 SwPosition *pTmp = m_pPoint;
241 m_pPoint = m_pMark;
242 m_pMark = pTmp;
245 #endif
247 /** A PaM marks a selection if Point and Mark are distinct positions.
248 @return true if the PaM spans a selection
250 bool HasMark() const { return m_pPoint != m_pMark; }
252 const SwPosition *GetPoint() const { return m_pPoint; }
253 SwPosition *GetPoint() { return m_pPoint; }
254 const SwPosition *GetMark() const { return m_pMark; }
255 SwPosition *GetMark() { return m_pMark; }
257 const SwPosition *Start() const
258 { return (*m_pPoint) <= (*m_pMark) ? m_pPoint : m_pMark; }
259 SwPosition *Start()
260 { return (*m_pPoint) <= (*m_pMark) ? m_pPoint : m_pMark; }
262 const SwPosition *End() const
263 { return (*m_pPoint) > (*m_pMark) ? m_pPoint : m_pMark; }
264 SwPosition *End()
265 { return (*m_pPoint) > (*m_pMark) ? m_pPoint : m_pMark; }
267 /// @return current Node at Point/Mark
268 SwNode & GetNode ( bool bPoint = true ) const
270 return ( bPoint ? m_pPoint->nNode : m_pMark->nNode ).GetNode();
273 /// @return current ContentNode at Point/Mark
274 SwContentNode* GetContentNode( bool bPoint = true ) const
276 return GetNode(bPoint).GetContentNode();
280 Normalizes PaM, i.e. sort point and mark.
282 @param bPointFirst true: If the point is behind the mark then swap.
283 false: If the mark is behind the point then swap.
285 SwPaM & Normalize(bool bPointFirst = true);
287 /// @return the document (SwDoc) at which the PaM is registered
288 SwDoc* GetDoc() const { return m_pPoint->nNode.GetNode().GetDoc(); }
290 SwPosition& GetBound( bool bOne = true )
291 { return bOne ? m_Bound1 : m_Bound2; }
292 const SwPosition& GetBound( bool bOne = true ) const
293 { return bOne ? m_Bound1 : m_Bound2; }
295 /// Get number of page which contains cursor.
296 sal_uInt16 GetPageNum( bool bAtPoint = true, const Point* pLayPos = 0 );
298 /** Is in something protected (readonly) or selection contains
299 something protected. */
300 bool HasReadonlySel( bool bFormView, bool bAnnotationMode = false ) const;
302 bool ContainsPosition(const SwPosition & rPos) const
304 return *Start() <= rPos && rPos <= *End();
307 DECL_FIXEDMEMPOOL_NEWDEL(SwPaM);
309 OUString GetText() const;
310 void InvalidatePaM();
311 SwPaM* GetNext()
312 { return GetNextInRing(); }
313 const SwPaM* GetNext() const
314 { return GetNextInRing(); }
315 SwPaM* GetPrev()
316 { return GetPrevInRing(); }
317 const SwPaM* GetPrev() const
318 { return GetPrevInRing(); }
319 bool IsMultiSelection() const
320 { return !unique(); }
322 void dumpAsXml(struct _xmlTextWriter* pWriter) const;
325 std::ostream &operator <<(std::ostream& s, const SwPaM& pam);
327 bool CheckNodesRange( const SwNodeIndex&, const SwNodeIndex&, bool bChkSection );
329 #endif // INCLUDED_SW_INC_PAM_HXX
331 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */