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  nullptr == ( pNd = aIdx.GetNode().StartOfSectionNode()->GetSectionNode()) )
45  {
46  --aIdx;
47  if ( aIdx == aOldIdx )
48  {
49  SvxSearchDialogWrapper::SetSearchLabel( SearchLabel::NavElementNotFound );
50  return false;
51  }
52  }
53 
54  if ( !aIdx.GetIndex() )
55  {
56  SvxSearchDialogWrapper::SetSearchLabel( SearchLabel::StartWrapped );
57  aIdx = nLastNd;
58  continue;
59  }
60 
61  assert( pNd ); // coverity, should never be nullptr
62  {
63  if( pNd->GetSection().IsHiddenFlag() ||
64  ( !bInReadOnly &&
65  pNd->GetSection().IsProtectFlag() ))
66  {
67  // skip protected or hidden ones
68  aIdx.Assign( *pNd, - 1 );
69  continue;
70  }
71  else if( &fnPosRegion == &fnMoveForward )
72  {
73  aIdx = *pNd;
74  SwContentNode* pCNd = pNd->GetNodes().GoNextSection( &aIdx,
75  true, !bInReadOnly );
76  if( !pCNd )
77  {
78  --aIdx;
79  continue;
80  }
81  rCurrentCursor.GetPoint()->nContent.Assign( pCNd, 0 );
82  }
83  else
84  {
85  aIdx = *pNd->EndOfSectionNode();
87  true, !bInReadOnly );
88  if( !pCNd )
89  {
90  aIdx.Assign( *pNd, - 1 );
91  continue;
92  }
93  rCurrentCursor.GetPoint()->nContent.Assign( pCNd, pCNd->Len() );
94  }
95  rCurrentCursor.GetPoint()->nNode = aIdx;
96  return true;
97  }
98  } while( true );
99 
100  // the flow is such that it is not possible to get here
101  return false;
102 }
103 
104 bool GotoNextRegion( SwPaM& rCurrentCursor, SwMoveFnCollection const & fnPosRegion,
105  bool bInReadOnly )
106 {
107  SvxSearchDialogWrapper::SetSearchLabel( SearchLabel::Empty );
108  SwNodeIndex aIdx( rCurrentCursor.GetPoint()->nNode );
109  SwSectionNode* pNd = aIdx.GetNode().FindSectionNode();
110  if( pNd )
111  aIdx.Assign( *pNd->EndOfSectionNode(), - 1 );
112 
113  SwNodeIndex aOldIdx = aIdx;
114  sal_uLong nEndCount = aIdx.GetNode().GetNodes().Count()-1;
115  do {
116  while( aIdx.GetIndex() < nEndCount &&
117  nullptr == ( pNd = aIdx.GetNode().GetSectionNode()) )
118  {
119  ++aIdx;
120  if ( aIdx == aOldIdx )
121  {
122  SvxSearchDialogWrapper::SetSearchLabel( SearchLabel::NavElementNotFound );
123  return false;
124  }
125  }
126 
127  if ( aIdx.GetIndex() == nEndCount )
128  {
129  SvxSearchDialogWrapper::SetSearchLabel( SearchLabel::EndWrapped );
130  aIdx = 0;
131  continue;
132  }
133 
134  assert( pNd ); // coverity, should never be nullptr
135  {
136  if( pNd->GetSection().IsHiddenFlag() ||
137  ( !bInReadOnly &&
138  pNd->GetSection().IsProtectFlag() ))
139  {
140  // skip protected or hidden ones
141  aIdx.Assign( *pNd->EndOfSectionNode(), +1 );
142  continue;
143  }
144  else if( &fnPosRegion == &fnMoveForward )
145  {
146  aIdx = *pNd;
147  SwContentNode* pCNd = pNd->GetNodes().GoNextSection( &aIdx,
148  true, !bInReadOnly );
149  if( !pCNd )
150  {
151  aIdx.Assign( *pNd->EndOfSectionNode(), +1 );
152  continue;
153  }
154  rCurrentCursor.GetPoint()->nContent.Assign( pCNd, 0 );
155  }
156  else
157  {
158  aIdx = *pNd->EndOfSectionNode();
159  SwContentNode* pCNd = SwNodes::GoPrevSection( &aIdx,
160  true, !bInReadOnly );
161  if( !pCNd )
162  {
163  ++aIdx;
164  continue;
165  }
166  rCurrentCursor.GetPoint()->nContent.Assign( pCNd, pCNd->Len() );
167  }
168  rCurrentCursor.GetPoint()->nNode = aIdx;
169  return true;
170  }
171  } while( true );
172 
173  // the flow is such that it is not possible to get here
174  return false;
175 }
176 
177 bool GotoCurrRegionAndSkip( SwPaM& rCurrentCursor, SwMoveFnCollection const & fnPosRegion,
178  bool bInReadOnly )
179 {
180  SwNode& rCurrNd = rCurrentCursor.GetNode();
181  SwSectionNode* pNd = rCurrNd.FindSectionNode();
182  if( !pNd )
183  return false;
184 
185  SwPosition* pPos = rCurrentCursor.GetPoint();
186  const sal_Int32 nCurrCnt = pPos->nContent.GetIndex();
187  bool bMoveBackward = &fnPosRegion == &fnMoveBackward;
188 
189  do {
190  SwContentNode* pCNd;
191  if( bMoveBackward ) // to the end of the section
192  {
193  SwNodeIndex aIdx( *pNd->EndOfSectionNode() );
194  pCNd = SwNodes::GoPrevSection( &aIdx, true, !bInReadOnly );
195  if( !pCNd )
196  return false;
197  pPos->nNode = aIdx;
198  }
199  else
200  {
201  SwNodeIndex aIdx( *pNd );
202  pCNd = pNd->GetNodes().GoNextSection( &aIdx, true, !bInReadOnly );
203  if( !pCNd )
204  return false;
205  pPos->nNode = aIdx;
206  }
207 
208  pPos->nContent.Assign( pCNd, bMoveBackward ? pCNd->Len() : 0 );
209 
210  if( &pPos->nNode.GetNode() != &rCurrNd ||
211  pPos->nContent.GetIndex() != nCurrCnt )
212  // there was a change
213  return true;
214 
215  // try also the parent of this section
216  SwSection* pParent = pNd->GetSection().GetParent();
217  pNd = pParent ? pParent->GetFormat()->GetSectionNode() : nullptr;
218  } while( pNd );
219  return false;
220 }
221 
222 bool SwCursor::MoveRegion( SwWhichRegion fnWhichRegion, SwMoveFnCollection const & fnPosRegion )
223 {
224  SwCursorSaveState aSaveState( *this );
225  return !dynamic_cast<SwTableCursor*>(this) &&
226  (*fnWhichRegion)( *this, fnPosRegion, IsReadOnlyAvailable() ) &&
227  !IsSelOvr() &&
228  (GetPoint()->nNode.GetIndex() != m_vSavePos.back().nNode ||
229  GetPoint()->nContent.GetIndex() != m_vSavePos.back().nContent);
230 }
231 
232 bool SwCursorShell::MoveRegion( SwWhichRegion fnWhichRegion, SwMoveFnCollection const & fnPosRegion )
233 {
234  SwCallLink aLk( *this ); // watch Cursor-Moves;call Link if needed
235  bool bRet = !m_pTableCursor && m_pCurrentCursor->MoveRegion( fnWhichRegion, fnPosRegion );
236  if( bRet )
237  UpdateCursor();
238  return bRet;
239 }
240 
241 bool SwCursor::GotoRegion( const OUString& rName )
242 {
243  bool bRet = false;
244  const SwSectionFormats& rFormats = GetDoc()->GetSections();
245  for( SwSectionFormats::size_type n = rFormats.size(); n; )
246  {
247  const SwSectionFormat* pFormat = rFormats[ --n ];
248  const SwNodeIndex* pIdx = nullptr;
249  const SwSection* pSect;
250  if( nullptr != ( pSect = pFormat->GetSection() ) &&
251  pSect->GetSectionName() == rName &&
252  nullptr != ( pIdx = pFormat->GetContent().GetContentIdx() ) &&
253  pIdx->GetNode().GetNodes().IsDocNodes() )
254  {
255  // area in normal nodes array
256  SwCursorSaveState aSaveState( *this );
257 
258  GetPoint()->nNode = *pIdx;
260  bRet = !IsSelOvr();
261  }
262  }
263  return bRet;
264 }
265 
266 bool SwCursorShell::GotoRegion( const OUString& rName )
267 {
268  SwCallLink aLk( *this ); // watch Cursor-Moves;call Link if needed
269  bool bRet = !m_pTableCursor && m_pCurrentCursor->GotoRegion( rName );
270  if( bRet )
273  return bRet;
274 }
275 
276 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SwSectionNode * FindSectionNode()
Search section node, in which it is.
Definition: ndsect.cxx:955
const SwEndNode * EndOfSectionNode() const
Definition: node.hxx:682
virtual sal_Int32 Len() const
Definition: node.cxx:1180
SAL_DLLPRIVATE void UpdateCursor(sal_uInt16 eFlags=SwCursorShell::SCROLLWIN|SwCursorShell::CHKRANGE, bool bIdleEnd=false)
Definition: crsrsh.cxx:1551
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:901
sal_uLong Count() const
Definition: ndarr.hxx:143
Marks a position in the document model.
Definition: pam.hxx:35
bool GotoRegion(const OUString &rName)
Definition: trvlreg.cxx:266
check overlapping PaMs
Definition: crsrsh.hxx:158
SwNodeIndex nNode
Definition: pam.hxx:37
sal_uIntPtr sal_uLong
virtual bool IsSelOvr(SwCursorSelOverFlags eFlags=SwCursorSelOverFlags::CheckNodeSection|SwCursorSelOverFlags::Toggle|SwCursorSelOverFlags::ChangePos)
Definition: swcrsr.cxx:216
static SwContentNode * GoPrevSection(SwNodeIndex *, bool bSkipHidden=true, bool bSkipProtect=true)
Definition: nodes.cxx:1973
SwNode & GetNode() const
Definition: ndindex.hxx:119
SwSectionFormat * GetFormat()
Definition: section.hxx:336
SwSection * GetParent() const
Definition: section.hxx:346
bool MoveRegion(SwWhichRegion, SwMoveFnCollection const &)
Definition: trvlreg.cxx:222
Array of Undo-history.
Definition: docary.hxx:299
const SwSection & GetSection() const
Definition: node.hxx:541
SwIndex nContent
Definition: pam.hxx:38
SwSectionNode * GetSectionNode()
Definition: section.cxx:1000
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:481
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:70
SwIndex & Assign(SwIndexReg *, sal_Int32)
Definition: index.cxx:198
SwDoc * GetDoc() const
Definition: pam.hxx:243
bool GotoRegion(const OUString &rName)
Definition: trvlreg.cxx:241
size_t size() const
Definition: docary.hxx:91
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:144
bool GotoNextRegion(SwPaM &rCurrentCursor, SwMoveFnCollection const &fnPosRegion, bool bInReadOnly)
Definition: trvlreg.cxx:104
bool MoveRegion(SwWhichRegion, SwMoveFnCollection const &)
Definition: trvlreg.cxx:232
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:402
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:1921
bool IsDocNodes() const
Is the NodesArray the regular one of Doc? (and not the UndoNds, ...) Implementation in doc...
Definition: nodes.cxx:2334
make visible in spite of Readonly
Definition: crsrsh.hxx:159
bool GotoCurrRegionAndSkip(SwPaM &rCurrentCursor, SwMoveFnCollection const &fnPosRegion, bool bInReadOnly)
Definition: trvlreg.cxx:177
SwSection * GetSection() const
Definition: section.cxx:668
SwSectionFormats & GetSections()
Definition: doc.hxx:1328
Base class of the Writer document model elements.
Definition: node.hxx:79