LibreOffice Module sw (master)  1
trvlreg.cxx
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 
20 #include <crsrsh.hxx>
21 #include <doc.hxx>
22 #include <swcrsr.hxx>
23 #include <docary.hxx>
24 #include <fmtcntnt.hxx>
25 #include <viscrs.hxx>
26 #include "callnk.hxx"
27 #include <pamtyp.hxx>
28 #include <section.hxx>
29 #include <svx/srchdlg.hxx>
30 
31 bool GotoPrevRegion( SwPaM& rCurrentCursor, SwMoveFnCollection const & fnPosRegion,
32  bool bInReadOnly )
33 {
34  SvxSearchDialogWrapper::SetSearchLabel( SearchLabel::Empty );
35  SwNodeIndex aIdx( rCurrentCursor.GetPoint()->nNode );
36  SwSectionNode* pNd = aIdx.GetNode().FindSectionNode();
37  if( pNd )
38  aIdx.Assign( *pNd, - 1 );
39 
40  SwNodeIndex aOldIdx = aIdx;
41  sal_uLong nLastNd = rCurrentCursor.GetDoc()->GetNodes().Count() - 1;
42  do {
43  while( aIdx.GetIndex() )
44  {
45  pNd = aIdx.GetNode().StartOfSectionNode()->GetSectionNode();
46  if (pNd)
47  break;
48  --aIdx;
49  if ( aIdx == aOldIdx )
50  {
51  SvxSearchDialogWrapper::SetSearchLabel( SearchLabel::NavElementNotFound );
52  return false;
53  }
54  }
55 
56  if ( !aIdx.GetIndex() )
57  {
58  SvxSearchDialogWrapper::SetSearchLabel( SearchLabel::StartWrapped );
59  aIdx = nLastNd;
60  continue;
61  }
62 
63  assert( pNd ); // coverity, should never be nullptr
64  {
65  if( pNd->GetSection().IsHiddenFlag() ||
66  ( !bInReadOnly &&
67  pNd->GetSection().IsProtectFlag() ))
68  {
69  // skip protected or hidden ones
70  aIdx.Assign( *pNd, - 1 );
71  continue;
72  }
73  else if( &fnPosRegion == &fnMoveForward )
74  {
75  aIdx = *pNd;
76  SwContentNode* pCNd = pNd->GetNodes().GoNextSection( &aIdx,
77  true, !bInReadOnly );
78  if( !pCNd )
79  {
80  --aIdx;
81  continue;
82  }
83  rCurrentCursor.GetPoint()->nContent.Assign( pCNd, 0 );
84  }
85  else
86  {
87  aIdx = *pNd->EndOfSectionNode();
89  true, !bInReadOnly );
90  if( !pCNd )
91  {
92  aIdx.Assign( *pNd, - 1 );
93  continue;
94  }
95  rCurrentCursor.GetPoint()->nContent.Assign( pCNd, pCNd->Len() );
96  }
97  rCurrentCursor.GetPoint()->nNode = aIdx;
98  return true;
99  }
100  } while( true );
101 
102  // the flow is such that it is not possible to get here
103  return false;
104 }
105 
106 bool GotoNextRegion( SwPaM& rCurrentCursor, SwMoveFnCollection const & fnPosRegion,
107  bool bInReadOnly )
108 {
109  SvxSearchDialogWrapper::SetSearchLabel( SearchLabel::Empty );
110  SwNodeIndex aIdx( rCurrentCursor.GetPoint()->nNode );
111  SwSectionNode* pNd = aIdx.GetNode().FindSectionNode();
112  if( pNd )
113  aIdx.Assign( *pNd->EndOfSectionNode(), - 1 );
114 
115  SwNodeIndex aOldIdx = aIdx;
116  sal_uLong nEndCount = aIdx.GetNode().GetNodes().Count()-1;
117  do {
118  while( aIdx.GetIndex() < nEndCount )
119  {
120  pNd = aIdx.GetNode().GetSectionNode();
121  if (pNd)
122  break;
123  ++aIdx;
124  if ( aIdx == aOldIdx )
125  {
126  SvxSearchDialogWrapper::SetSearchLabel( SearchLabel::NavElementNotFound );
127  return false;
128  }
129  }
130 
131  if ( aIdx.GetIndex() == nEndCount )
132  {
133  SvxSearchDialogWrapper::SetSearchLabel( SearchLabel::EndWrapped );
134  aIdx = 0;
135  continue;
136  }
137 
138  assert( pNd ); // coverity, should never be nullptr
139  {
140  if( pNd->GetSection().IsHiddenFlag() ||
141  ( !bInReadOnly &&
142  pNd->GetSection().IsProtectFlag() ))
143  {
144  // skip protected or hidden ones
145  aIdx.Assign( *pNd->EndOfSectionNode(), +1 );
146  continue;
147  }
148  else if( &fnPosRegion == &fnMoveForward )
149  {
150  aIdx = *pNd;
151  SwContentNode* pCNd = pNd->GetNodes().GoNextSection( &aIdx,
152  true, !bInReadOnly );
153  if( !pCNd )
154  {
155  aIdx.Assign( *pNd->EndOfSectionNode(), +1 );
156  continue;
157  }
158  rCurrentCursor.GetPoint()->nContent.Assign( pCNd, 0 );
159  }
160  else
161  {
162  aIdx = *pNd->EndOfSectionNode();
163  SwContentNode* pCNd = SwNodes::GoPrevSection( &aIdx,
164  true, !bInReadOnly );
165  if( !pCNd )
166  {
167  ++aIdx;
168  continue;
169  }
170  rCurrentCursor.GetPoint()->nContent.Assign( pCNd, pCNd->Len() );
171  }
172  rCurrentCursor.GetPoint()->nNode = aIdx;
173  return true;
174  }
175  } while( true );
176 
177  // the flow is such that it is not possible to get here
178  return false;
179 }
180 
181 bool GotoCurrRegionAndSkip( SwPaM& rCurrentCursor, SwMoveFnCollection const & fnPosRegion,
182  bool bInReadOnly )
183 {
184  SwNode& rCurrNd = rCurrentCursor.GetNode();
185  SwSectionNode* pNd = rCurrNd.FindSectionNode();
186  if( !pNd )
187  return false;
188 
189  SwPosition* pPos = rCurrentCursor.GetPoint();
190  const sal_Int32 nCurrCnt = pPos->nContent.GetIndex();
191  bool bMoveBackward = &fnPosRegion == &fnMoveBackward;
192 
193  do {
194  SwContentNode* pCNd;
195  if( bMoveBackward ) // to the end of the section
196  {
197  SwNodeIndex aIdx( *pNd->EndOfSectionNode() );
198  pCNd = SwNodes::GoPrevSection( &aIdx, true, !bInReadOnly );
199  if( !pCNd )
200  return false;
201  pPos->nNode = aIdx;
202  }
203  else
204  {
205  SwNodeIndex aIdx( *pNd );
206  pCNd = pNd->GetNodes().GoNextSection( &aIdx, true, !bInReadOnly );
207  if( !pCNd )
208  return false;
209  pPos->nNode = aIdx;
210  }
211 
212  pPos->nContent.Assign( pCNd, bMoveBackward ? pCNd->Len() : 0 );
213 
214  if( &pPos->nNode.GetNode() != &rCurrNd ||
215  pPos->nContent.GetIndex() != nCurrCnt )
216  // there was a change
217  return true;
218 
219  // try also the parent of this section
220  SwSection* pParent = pNd->GetSection().GetParent();
221  pNd = pParent ? pParent->GetFormat()->GetSectionNode() : nullptr;
222  } while( pNd );
223  return false;
224 }
225 
226 bool SwCursor::MoveRegion( SwWhichRegion fnWhichRegion, SwMoveFnCollection const & fnPosRegion )
227 {
228  SwCursorSaveState aSaveState( *this );
229  return !dynamic_cast<SwTableCursor*>(this) &&
230  (*fnWhichRegion)( *this, fnPosRegion, IsReadOnlyAvailable() ) &&
231  !IsSelOvr() &&
232  (GetPoint()->nNode.GetIndex() != m_vSavePos.back().nNode ||
233  GetPoint()->nContent.GetIndex() != m_vSavePos.back().nContent);
234 }
235 
236 bool SwCursorShell::MoveRegion( SwWhichRegion fnWhichRegion, SwMoveFnCollection const & fnPosRegion )
237 {
238  SwCallLink aLk( *this ); // watch Cursor-Moves;call Link if needed
239  bool bRet = !m_pTableCursor && m_pCurrentCursor->MoveRegion( fnWhichRegion, fnPosRegion );
240  if( bRet )
241  UpdateCursor();
242  return bRet;
243 }
244 
245 bool SwCursor::GotoRegion( const OUString& rName )
246 {
247  bool bRet = false;
248  const SwSectionFormats& rFormats = GetDoc()->GetSections();
249  for( SwSectionFormats::size_type n = rFormats.size(); n; )
250  {
251  const SwSectionFormat* pFormat = rFormats[ --n ];
252  const SwSection* pSect = pFormat->GetSection();
253  if( pSect && pSect->GetSectionName() == rName )
254  {
255  const SwNodeIndex* pIdx = pFormat->GetContent().GetContentIdx();
256  if( pIdx && pIdx->GetNode().GetNodes().IsDocNodes() )
257  {
258  // area in normal nodes array
259  SwCursorSaveState aSaveState( *this );
260 
261  GetPoint()->nNode = *pIdx;
263  bRet = !IsSelOvr();
264  }
265  }
266  }
267  return bRet;
268 }
269 
270 bool SwCursorShell::GotoRegion( const OUString& rName )
271 {
272  SwCallLink aLk( *this ); // watch Cursor-Moves;call Link if needed
273  bool bRet = !m_pTableCursor && m_pCurrentCursor->GotoRegion( rName );
274  if( bRet )
277  return bRet;
278 }
279 
280 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SwSectionNode * FindSectionNode()
Search section node, in which it is.
Definition: ndsect.cxx:958
const SwEndNode * EndOfSectionNode() const
Definition: node.hxx:682
virtual sal_Int32 Len() const
Definition: node.cxx:1181
SAL_DLLPRIVATE void UpdateCursor(sal_uInt16 eFlags=SwCursorShell::SCROLLWIN|SwCursorShell::CHKRANGE, bool bIdleEnd=false)
Definition: crsrsh.cxx:1565
SwNode & GetNode(bool bPoint=true) const
Definition: pam.hxx:223
SwShellCursor * m_pCurrentCursor
current cursor
Definition: crsrsh.hxx:179
bool GoInContent(SwPaM &rPam, SwMoveFnCollection const &fnMove)
Definition: pam.cxx:963
sal_uLong Count() const
Definition: ndarr.hxx:142
Marks a position in the document model.
Definition: pam.hxx:35
bool GotoRegion(const OUString &rName)
Definition: trvlreg.cxx:270
check overlapping PaMs
Definition: crsrsh.hxx:158
SwNodeIndex nNode
Definition: pam.hxx:37
sal_uIntPtr sal_uLong
sal_Int64 n
virtual bool IsSelOvr(SwCursorSelOverFlags eFlags=SwCursorSelOverFlags::CheckNodeSection|SwCursorSelOverFlags::Toggle|SwCursorSelOverFlags::ChangePos)
Definition: swcrsr.cxx:221
static SwContentNode * GoPrevSection(SwNodeIndex *, bool bSkipHidden=true, bool bSkipProtect=true)
Definition: nodes.cxx:1952
SwNode & GetNode() const
Definition: ndindex.hxx:119
SwSectionFormat * GetFormat()
Definition: section.hxx:337
SwSection * GetParent() const
Definition: section.hxx:347
bool MoveRegion(SwWhichRegion, SwMoveFnCollection const &)
Definition: trvlreg.cxx:226
Array of Undo-history.
Definition: docary.hxx:197
const SwSection & GetSection() const
Definition: node.hxx:541
SwIndex nContent
Definition: pam.hxx:38
SwSectionNode * GetSectionNode()
Definition: section.cxx:1003
sal_uLong GetIndex() const
Definition: ndindex.hxx:152
std::vector< SwCursor_SavePos > m_vSavePos
Definition: swcrsr.hxx:72
SwShellTableCursor * m_pTableCursor
table Cursor; only in tables when the selection lays over 2 columns
Definition: crsrsh.hxx:185
bool(* SwWhichRegion)(SwPaM &, SwMoveFnCollection const &, bool bInReadOnly)
Definition: cshtyp.hxx:75
PaM is Point and Mark: a selection of the document model.
Definition: pam.hxx:136
bool Move(SwMoveFnCollection const &fnMove=fnMoveForward, SwGoInDoc fnGo=GoInContent)
Movement of cursor.
Definition: pam.cxx:501
A helper class to save cursor state (position).
Definition: swcrsr.hxx:230
const OUString & GetSectionName() const
Definition: section.hxx:169
const SwPosition * GetPoint() const
Definition: pam.hxx:207
std::vector< SwSectionFormat * >::size_type size_type
Definition: docary.hxx:63
SwIndex & Assign(SwIndexReg *, sal_Int32)
Definition: index.cxx:206
SwDoc * GetDoc() const
Definition: pam.hxx:243
bool GotoRegion(const OUString &rName)
Definition: trvlreg.cxx:245
size_t size() const
Definition: docary.hxx:84
Marks a node in the document model.
Definition: ndindex.hxx:31
SwNodes & GetNodes()
Node is in which nodes-array/doc?
Definition: node.hxx:693
static void SetSearchLabel(const SearchLabel &rSL)
const SwNodeIndex * GetContentIdx() const
Definition: fmtcntnt.hxx:46
virtual bool IsReadOnlyAvailable() const
Definition: swcrsr.cxx:149
bool GotoNextRegion(SwPaM &rCurrentCursor, SwMoveFnCollection const &fnPosRegion, bool bInReadOnly)
Definition: trvlreg.cxx:106
bool MoveRegion(SwWhichRegion, SwMoveFnCollection const &)
Definition: trvlreg.cxx:236
SwMoveFnCollection const & fnMoveForward
SwPam::Move()/Find() default argument.
Definition: paminit.cxx:59
sal_Int32 GetIndex() const
Definition: index.hxx:95
SwNodes & GetNodes()
Definition: doc.hxx:405
scroll window
Definition: crsrsh.hxx:157
const SwFormatContent & GetContent(bool=true) const
Definition: fmtcntnt.hxx:55
SwMoveFnCollection const & fnMoveBackward
Definition: paminit.cxx:58
bool GotoPrevRegion(SwPaM &rCurrentCursor, SwMoveFnCollection const &fnPosRegion, bool bInReadOnly)
Definition: trvlreg.cxx:31
SwContentNode * GoNextSection(SwNodeIndex *, bool bSkipHidden=true, bool bSkipProtect=true) const
Go to next content-node that is not protected or hidden (Both set FALSE ==> GoNext/GoPrevious!!!).
Definition: nodes.cxx:1900
bool IsDocNodes() const
Is the NodesArray the regular one of Doc? (and not the UndoNds, ...) Implementation in doc...
Definition: nodes.cxx:2313
make visible in spite of Readonly
Definition: crsrsh.hxx:159
bool GotoCurrRegionAndSkip(SwPaM &rCurrentCursor, SwMoveFnCollection const &fnPosRegion, bool bInReadOnly)
Definition: trvlreg.cxx:181
SwSection * GetSection() const
Definition: section.cxx:671
SwSectionFormats & GetSections()
Definition: doc.hxx:1336
Base class of the Writer document model elements.
Definition: node.hxx:79