LibreOffice Module sw (master)  1
pam.hxx
Go to the documentation of this file.
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 <sal/types.h>
23 #include "ring.hxx"
24 #include "index.hxx"
25 #include "ndindex.hxx"
26 #include "swdllapi.h"
27 #include "nodeoffset.hxx"
28 
29 #include <iostream>
30 
31 class SwDoc;
32 class SwPaM;
33 class Point;
34 
37 {
40 
41  SwPosition( const SwNodeIndex &rNode, const SwIndex &rContent );
42  explicit SwPosition( const SwNodeIndex &rNode );
43  explicit SwPosition( const SwNode& rNode );
44  explicit SwPosition( SwContentNode& rNode, const sal_Int32 nOffset = 0 );
45 
51  SwDoc& GetDoc() const;
52 
53  bool operator < (const SwPosition &) const;
54  bool operator > (const SwPosition &) const;
55  bool operator <=(const SwPosition &) const;
56  bool operator >=(const SwPosition &) const;
57  bool operator ==(const SwPosition &) const;
58  bool operator !=(const SwPosition &) const;
59  void dumpAsXml(xmlTextWriterPtr pWriter) const;
60 };
61 
62 SW_DLLPUBLIC std::ostream &operator <<(std::ostream& s, const SwPosition& position);
63 
64 // Result of comparing positions.
65 enum class SwComparePosition {
66  Before,
67  Behind,
68  Inside,
69  Outside,
70  Equal,
73  CollideStart,
74  CollideEnd
75 };
76 
77 template<typename T>
79  const T& rStt1, const T& rEnd1,
80  const T& rStt2, const T& rEnd2 )
81 {
82  SwComparePosition nRet;
83  if( rStt1 < rStt2 )
84  {
85  if( rEnd1 > rStt2 )
86  {
87  if( rEnd1 >= rEnd2 )
89  else
91 
92  }
93  else if( rEnd1 == rStt2 )
95  else
97  }
98  else if( rEnd2 > rStt1 )
99  {
100  if( rEnd2 >= rEnd1 )
101  {
102  if( rEnd2 == rEnd1 && rStt2 == rStt1 )
104  else
106  }
107  else
108  {
109  if (rStt1 == rStt2)
111  else
113  }
114  }
115  else if( rEnd2 == rStt1 )
117  else
119  return nRet;
120 }
121 
123 struct SwMoveFnCollection;
126 
127 using SwGoInDoc = auto (*)(SwPaM& rPam, SwMoveFnCollection const & fnMove) -> bool;
129 bool GoInSection( SwPaM&, SwMoveFnCollection const &);
132 bool GoInContentCells( SwPaM&, SwMoveFnCollection const &);
135 
138 {
144 
145  SwPaM(SwPaM const& rPaM) = delete;
146 
147 public:
148  explicit SwPaM( const SwPosition& rPos, SwPaM* pRing = nullptr );
149  SwPaM( const SwPosition& rMk, const SwPosition& rPt, SwPaM* pRing = nullptr );
150  SwPaM( const SwNodeIndex& rMk, const SwNodeIndex& rPt,
151  SwNodeOffset nMkOffset = SwNodeOffset(0), SwNodeOffset nPtOffset = SwNodeOffset(0), SwPaM* pRing = nullptr );
152  SwPaM( const SwNode& rMk, const SwNode& rPt,
153  SwNodeOffset nMkOffset = SwNodeOffset(0), SwNodeOffset nPtOffset = SwNodeOffset(0), SwPaM* pRing = nullptr );
154  SwPaM( const SwNodeIndex& rMk, sal_Int32 nMkContent,
155  const SwNodeIndex& rPt, sal_Int32 nPtContent, SwPaM* pRing = nullptr );
156  SwPaM( const SwNode& rMk, sal_Int32 nMkContent,
157  const SwNode& rPt, sal_Int32 nPtContent, SwPaM* pRing = nullptr );
158  SwPaM( const SwNode& rNd, sal_Int32 nContent = 0, SwPaM* pRing = nullptr );
159  SwPaM( const SwNodeIndex& rNd, sal_Int32 nContent = 0, SwPaM* pRing = nullptr );
160  virtual ~SwPaM() override;
161 
164  SwPaM(SwPaM const& rPaM, SwPaM * pRing);
166  SwPaM& operator=( const SwPaM & );
167 
169  bool Move( SwMoveFnCollection const & fnMove = fnMoveForward,
170  SwGoInDoc fnGo = GoInContent );
171 
172  bool IsInFrontOfLabel() const { return m_bIsInFrontOfLabel; }
173  void SetInFrontOfLabel_( bool bNew ) { m_bIsInFrontOfLabel = bNew; }
174 
176  virtual void SetMark();
177 
178  void DeleteMark()
179  {
180  if (m_pMark != m_pPoint)
181  {
184  *m_pMark = SwPosition( SwNodeIndex( GetNode().GetNodes() ) );
185  m_pMark = m_pPoint;
186  }
187  }
188 #ifdef DBG_UTIL
189  void Exchange();
190 
191 #else
192  void Exchange()
193  {
194  if (m_pPoint != m_pMark)
195  {
196  SwPosition *pTmp = m_pPoint;
197  m_pPoint = m_pMark;
198  m_pMark = pTmp;
199  }
200  }
201 #endif
202 
206  bool HasMark() const { return m_pPoint != m_pMark; }
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; }
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; }
221  { return (*m_pPoint) > (*m_pMark) ? m_pPoint : m_pMark; }
222 
224  SwNode & GetNode ( bool bPoint = true ) const
225  {
226  return ( bPoint ? m_pPoint->nNode : m_pMark->nNode ).GetNode();
227  }
228 
230  SwContentNode* GetContentNode( bool bPoint = true ) const
231  {
232  return GetNode(bPoint).GetContentNode();
233  }
234 
241  void Normalize(bool bPointFirst = true);
242 
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 
252  sal_uInt16 GetPageNum( bool bAtPoint = true, const Point* pLayPos = nullptr );
253 
256  bool HasReadonlySel( bool bFormView ) const;
257 
258  bool ContainsPosition(const SwPosition & rPos) const
259  {
260  return *Start() <= rPos && rPos <= *End();
261  }
262 
263  OUString GetText() const;
264  void InvalidatePaM();
266  { return GetNextInRing(); }
267  const SwPaM* GetNext() const
268  { return GetNextInRing(); }
270  { return GetPrevInRing(); }
271  const SwPaM* GetPrev() const
272  { return GetPrevInRing(); }
273  bool IsMultiSelection() const
274  { return !unique(); }
275 
276  void dumpAsXml(xmlTextWriterPtr pWriter) const;
277 };
278 
279 SW_DLLPUBLIC std::ostream &operator <<(std::ostream& s, const SwPaM& pam);
280 
281 bool CheckNodesRange(const SwNodeIndex&, const SwNodeIndex&, bool bChkSection);
282 
283 #endif // INCLUDED_SW_INC_PAM_HXX
284 
285 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SwPosition m_Bound1
Definition: pam.hxx:139
SwPosition * GetPoint()
Definition: pam.hxx:209
void DeleteMark()
Definition: pam.hxx:178
SwNode & GetNode(bool bPoint=true) const
Definition: pam.hxx:224
Marks a position in the document model.
Definition: pam.hxx:36
bool IsMultiSelection() const
Definition: pam.hxx:273
SwContentNode * GetNode(SwPaM &rPam, bool &rbFirst, SwMoveFnCollection const &fnMove, bool const bInReadOnly, SwRootFrame const *const i_pLayout)
This function returns the next node in direction of search.
Definition: pam.cxx:823
SW_DLLPUBLIC bool GoInNode(SwPaM &, SwMoveFnCollection const &)
Definition: pam.cxx:961
SwComparePosition ComparePosition(const T &rStt1, const T &rEnd1, const T &rStt2, const T &rEnd2)
Definition: pam.hxx:78
SwNodeIndex nNode
Definition: pam.hxx:38
Pos1 is as large as Pos2.
const SwPosition * GetMark() const
Definition: pam.hxx:210
const SwPaM * GetPrev() const
Definition: pam.hxx:271
Pos1 completely contained in Pos2.
SwPosition * GetMark()
Definition: pam.hxx:211
Definition: doc.hxx:188
SW_DLLPUBLIC SwMoveFnCollection const & fnMoveForward
SwPam::Move()/Find() default argument.
Definition: paminit.cxx:59
SwPosition * m_pPoint
points at either m_Bound1 or m_Bound2
Definition: pam.hxx:141
SwNode & GetNode() const
Definition: ndindex.hxx:121
SW_DLLPUBLIC bool GoInDoc(SwPaM &, SwMoveFnCollection const &)
Definition: pam.cxx:949
SwPosition & GetBound(bool bOne=true)
Definition: pam.hxx:246
SW_DLLPUBLIC bool GoInContent(SwPaM &, SwMoveFnCollection const &)
Definition: pam.cxx:970
SwPosition * m_pMark
points at either m_Bound1 or m_Bound2
Definition: pam.hxx:142
SwContentNode * GetContentNode(bool bPoint=true) const
Definition: pam.hxx:230
Pos1 end touches at Pos2 start.
SwIndex nContent
Definition: pam.hxx:39
auto(*)(SwPaM &rPam, SwMoveFnCollection const &fnMove) -> bool SwGoInDoc
Definition: pam.hxx:127
bool GoInContentCells(SwPaM &, SwMoveFnCollection const &)
Definition: pam.cxx:978
bool m_bIsInFrontOfLabel
Definition: pam.hxx:143
Pos2 completely contained in Pos1.
Pos1 before Pos2.
value_type * GetPrevInRing()
Definition: ring.hxx:87
SwPaM * GetNext()
Definition: pam.hxx:265
bool GoInContentSkipHidden(SwPaM &, SwMoveFnCollection const &)
Definition: pam.cxx:986
const SwPaM * GetNext() const
Definition: pam.hxx:267
bool ContainsPosition(const SwPosition &rPos) const
Definition: pam.hxx:258
bool CheckNodesRange(const SwNodeIndex &, const SwNodeIndex &, bool bChkSection)
Check if the given range is inside one of the defined top-level sections.
Definition: pam.cxx:259
PaM is Point and Mark: a selection of the document model.
Definition: pam.hxx:137
SwDoc & GetDoc()
Definition: node.hxx:213
const SwPosition * GetPoint() const
Definition: pam.hxx:208
Pos1 start touches at Pos2 end.
bool unique() const
Definition: ring.hxx:96
const SwPosition & GetBound(bool bOne=true) const
Definition: pam.hxx:248
SwContentNode * GetContentNode()
Definition: node.hxx:619
SwPosition m_Bound2
Definition: pam.hxx:140
bool HasMark() const
A PaM marks a selection if Point and Mark are distinct positions.
Definition: pam.hxx:206
value_type * GetNextInRing()
Definition: ring.hxx:84
Marks a character position inside a document model node.
Definition: index.hxx:33
bool operator<(const wwFont &r1, const wwFont &r2)
Definition: wrtw8sty.cxx:849
Marks a node in the document model.
Definition: ndindex.hxx:32
#define SW_DLLPUBLIC
Definition: swdllapi.h:28
const SwPosition * Start() const
Definition: pam.hxx:213
bool GoInSection(SwPaM &, SwMoveFnCollection const &)
Definition: pam.cxx:955
SwPaM * GetPrev()
Definition: pam.hxx:269
#define SAL_WARN_UNUSED
SwPosition * End()
Definition: pam.hxx:220
SW_DLLPUBLIC std::ostream & operator<<(std::ostream &s, const SwPosition &position)
Definition: pam.cxx:194
Pos1 overlaps Pos2 at the end.
const SwPosition * End() const
Definition: pam.hxx:218
o3tl::strong_int< sal_Int32, struct Tag_SwNodeOffset > SwNodeOffset
Definition: nodeoffset.hxx:16
SW_DLLPUBLIC SwMoveFnCollection const & fnMoveBackward
Definition: paminit.cxx:58
struct _xmlTextWriter * xmlTextWriterPtr
SwDoc & GetDoc() const
Definition: pam.hxx:244
void SetInFrontOfLabel_(bool bNew)
Definition: pam.hxx:173
Pos1 behind Pos2.
bool GoInContentCellsSkipHidden(SwPaM &, SwMoveFnCollection const &)
Definition: pam.cxx:994
Pos1 overlaps Pos2 at the beginning.
SwComparePosition
Definition: pam.hxx:65
SwPosition * Start()
Definition: pam.hxx:215
bool IsInFrontOfLabel() const
Definition: pam.hxx:172
Base class of the Writer document model elements.
Definition: node.hxx:81