LibreOffice Module sw (master)  1
doccorr.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 <doc.hxx>
22 #include <node.hxx>
23 #include <editsh.hxx>
24 #include <viscrs.hxx>
25 #include <redline.hxx>
26 #include <mvsave.hxx>
27 #include <docary.hxx>
28 #include <unocrsr.hxx>
29 
30 namespace
31 {
34  const SwStartNode* lcl_FindUnoCursorSection( const SwNode& rNode )
35  {
36  const SwStartNode* pStartNode = rNode.StartOfSectionNode();
37  while( ( pStartNode != nullptr ) &&
38  ( pStartNode->StartOfSectionNode() != pStartNode ) &&
39  ( pStartNode->GetStartNodeType() == SwNormalStartNode ) )
40  pStartNode = pStartNode->StartOfSectionNode();
41 
42  return pStartNode;
43  }
44 
45  bool lcl_PosCorrAbs(SwPosition & rPos,
46  const SwPosition& rStart,
47  const SwPosition& rEnd,
48  const SwPosition& rNewPos)
49  {
50  if ((rStart <= rPos) && (rPos <= rEnd))
51  {
52  rPos = rNewPos;
53  return true;
54  }
55  return false;
56  };
57 
58  bool lcl_PaMCorrAbs(SwPaM & rPam,
59  const SwPosition& rStart,
60  const SwPosition& rEnd,
61  const SwPosition& rNewPos)
62  {
63  bool bRet = false;
64  bRet |= lcl_PosCorrAbs(rPam.GetBound(), rStart, rEnd, rNewPos);
65  bRet |= lcl_PosCorrAbs(rPam.GetBound(false), rStart, rEnd, rNewPos);
66  return bRet;
67  };
68 
69  void lcl_PaMCorrRel1(SwPaM * pPam,
70  SwNode const * const pOldNode,
71  const SwPosition& rNewPos,
72  const sal_Int32 nCntIdx)
73  {
74  for(int nb = 0; nb < 2; ++nb)
75  if(&(pPam->GetBound(bool(nb)).nNode.GetNode()) == pOldNode)
76  {
77  pPam->GetBound(bool(nb)).nNode = rNewPos.nNode;
78  pPam->GetBound(bool(nb)).nContent.Assign(
79  const_cast<SwIndexReg*>(rNewPos.nContent.GetIdxReg()),
80  nCntIdx + pPam->GetBound(bool(nb)).nContent.GetIndex());
81  }
82  }
83 }
84 
85 void PaMCorrAbs( const SwPaM& rRange,
86  const SwPosition& rNewPos )
87 {
88  SwPosition const aStart( *rRange.Start() );
89  SwPosition const aEnd( *rRange.End() );
90  SwPosition const aNewPos( rNewPos );
91  SwDoc& rDoc = aStart.nNode.GetNode().GetDoc();
92  SwCursorShell *const pShell = rDoc.GetEditShell();
93 
94  if( pShell )
95  {
96  for(const SwViewShell& rShell : pShell->GetRingContainer())
97  {
98  const SwCursorShell* pCursorShell = dynamic_cast<const SwCursorShell*>(&rShell);
99  if(!pCursorShell)
100  continue;
101  SwPaM *_pStackCursor = pCursorShell->GetStackCursor();
102  if( _pStackCursor )
103  for (;;)
104  {
105  lcl_PaMCorrAbs( *_pStackCursor, aStart, aEnd, aNewPos );
106  if( !_pStackCursor )
107  break;
108  _pStackCursor = _pStackCursor->GetNext();
109  if( _pStackCursor == pCursorShell->GetStackCursor() )
110  break;
111  }
112 
113  for(SwPaM& rPaM : const_cast<SwShellCursor*>(pCursorShell->GetCursor_())->GetRingContainer())
114  {
115  lcl_PaMCorrAbs( rPaM, aStart, aEnd, aNewPos );
116  }
117 
118  if( pCursorShell->IsTableMode() )
119  lcl_PaMCorrAbs( const_cast<SwPaM &>(*pCursorShell->GetTableCrs()), aStart, aEnd, aNewPos );
120  }
121  }
122 
123  rDoc.cleanupUnoCursorTable();
124  for(const auto& pWeakUnoCursor : rDoc.mvUnoCursorTable)
125  {
126  auto pUnoCursor(pWeakUnoCursor.lock());
127  if(!pUnoCursor)
128  continue;
129 
130  bool bChange = false; // has the UNO cursor been corrected?
131 
132  // determine whether the UNO cursor will leave it's designated
133  // section
134  bool const bLeaveSection =
135  pUnoCursor->IsRemainInSection() &&
136  ( lcl_FindUnoCursorSection( aNewPos.nNode.GetNode() ) !=
137  lcl_FindUnoCursorSection(
138  pUnoCursor->GetPoint()->nNode.GetNode() ) );
139 
140  for(SwPaM& rPaM : pUnoCursor->GetRingContainer())
141  {
142  bChange |= lcl_PaMCorrAbs( rPaM, aStart, aEnd, aNewPos );
143  }
144 
145  SwUnoTableCursor *const pUnoTableCursor =
146  dynamic_cast<SwUnoTableCursor *>(pUnoCursor.get());
147  if( pUnoTableCursor )
148  {
149  for(SwPaM& rPaM : pUnoTableCursor->GetSelRing().GetRingContainer())
150  {
151  bChange |=
152  lcl_PaMCorrAbs( rPaM, aStart, aEnd, aNewPos );
153  }
154  }
155 
156  // if a UNO cursor leaves its designated section, we must inform
157  // (and invalidate) said cursor
158  if (bChange && bLeaveSection)
159  {
160  // the UNO cursor has left its section. We need to notify it!
161  sw::UnoCursorHint aHint;
162  pUnoCursor->m_aNotifier.Broadcast(aHint);
163  }
164  }
165 }
166 
167 void SwDoc::CorrAbs(const SwNodeIndex& rOldNode,
168  const SwPosition& rNewPos,
169  const sal_Int32 nOffset,
170  bool bMoveCursor)
171 {
172  SwContentNode *const pContentNode( rOldNode.GetNode().GetContentNode() );
173  SwPaM const aPam(rOldNode, 0,
174  rOldNode, pContentNode ? pContentNode->Len() : 0);
175  SwPosition aNewPos(rNewPos);
176  aNewPos.nContent += nOffset;
177 
178  getIDocumentMarkAccess()->correctMarksAbsolute(rOldNode, rNewPos, nOffset);
179  // fix redlines
180  {
182  for (SwRedlineTable::size_type n = 0; n < rTable.size(); )
183  {
184  // is on position ??
185  SwRangeRedline *const pRedline( rTable[ n ] );
186  bool const bChanged =
187  lcl_PaMCorrAbs(*pRedline, *aPam.Start(), *aPam.End(), aNewPos);
188  // clean up empty redlines: docredln.cxx asserts these as invalid
189  if (bChanged && (*pRedline->GetPoint() == *pRedline->GetMark())
190  && (pRedline->GetContentIdx() == nullptr))
191  {
192  rTable.DeleteAndDestroy(n);
193  }
194  else
195  {
196  ++n;
197  }
198  }
199 
200  // To-Do - need to add here 'SwExtraRedlineTable' also ?
201  }
202 
203  if(bMoveCursor)
204  {
205  ::PaMCorrAbs(aPam, aNewPos);
206  }
207 }
208 
210  const SwPaM& rRange,
211  const SwPosition& rNewPos,
212  bool bMoveCursor )
213 {
214  SwPosition aStart(*rRange.Start());
215  SwPosition aEnd(*rRange.End());
216 
217  DelBookmarks( aStart.nNode, aEnd.nNode, nullptr, &aStart.nContent, &aEnd.nContent );
218 
219  if(bMoveCursor)
220  ::PaMCorrAbs(rRange, rNewPos);
221 }
222 
224  const SwNodeIndex& rStartNode,
225  const SwNodeIndex& rEndNode,
226  const SwPosition& rNewPos,
227  bool bMoveCursor )
228 {
229  DelBookmarks( rStartNode, rEndNode );
230 
231  if(bMoveCursor)
232  {
233  SwContentNode *const pContentNode( rEndNode.GetNode().GetContentNode() );
234  SwPaM const aPam(rStartNode, 0,
235  rEndNode, pContentNode ? pContentNode->Len() : 0);
236  ::PaMCorrAbs(aPam, rNewPos);
237  }
238 }
239 
240 void PaMCorrRel( const SwNodeIndex &rOldNode,
241  const SwPosition &rNewPos,
242  const sal_Int32 nOffset )
243 {
244  const SwNode* pOldNode = &rOldNode.GetNode();
245  SwPosition aNewPos( rNewPos );
246  const SwDoc& rDoc = pOldNode->GetDoc();
247 
248  const sal_Int32 nCntIdx = rNewPos.nContent.GetIndex() + nOffset;
249 
250  SwCursorShell const* pShell = rDoc.GetEditShell();
251  if( pShell )
252  {
253  for(const SwViewShell& rShell : pShell->GetRingContainer())
254  {
255  SwCursorShell* pCursorShell = const_cast<SwCursorShell*>(dynamic_cast<const SwCursorShell*>(&rShell));
256  if(!pCursorShell)
257  continue;
258  SwPaM *_pStackCursor = pCursorShell->GetStackCursor();
259  if( _pStackCursor )
260  for (;;)
261  {
262  lcl_PaMCorrRel1( _pStackCursor, pOldNode, aNewPos, nCntIdx );
263  if( !_pStackCursor )
264  break;
265  _pStackCursor = _pStackCursor->GetNext();
266  if( _pStackCursor == pCursorShell->GetStackCursor() )
267  break;
268  }
269 
270  SwPaM* pStartPaM = pCursorShell->GetCursor_();
271  for(SwPaM& rPaM : pStartPaM->GetRingContainer())
272  {
273  lcl_PaMCorrRel1( &rPaM, pOldNode, aNewPos, nCntIdx);
274  }
275 
276  if( pCursorShell->IsTableMode() )
277  lcl_PaMCorrRel1( pCursorShell->GetTableCrs(), pOldNode, aNewPos, nCntIdx );
278  }
279  }
280 
281  rDoc.cleanupUnoCursorTable();
282  for(const auto& pWeakUnoCursor : rDoc.mvUnoCursorTable)
283  {
284  auto pUnoCursor(pWeakUnoCursor.lock());
285  if(!pUnoCursor)
286  continue;
287  for(SwPaM& rPaM : pUnoCursor->GetRingContainer())
288  {
289  lcl_PaMCorrRel1( &rPaM, pOldNode, aNewPos, nCntIdx );
290  }
291 
292  SwUnoTableCursor* pUnoTableCursor =
293  dynamic_cast<SwUnoTableCursor*>(pUnoCursor.get());
294  if( pUnoTableCursor )
295  {
296  for(SwPaM& rPaM : pUnoTableCursor->GetSelRing().GetRingContainer())
297  {
298  lcl_PaMCorrRel1( &rPaM, pOldNode, aNewPos, nCntIdx );
299  }
300  }
301  }
302 }
303 
304 void SwDoc::CorrRel(const SwNodeIndex& rOldNode,
305  const SwPosition& rNewPos,
306  const sal_Int32 nOffset,
307  bool bMoveCursor)
308 {
309  getIDocumentMarkAccess()->correctMarksRelative(rOldNode, rNewPos, nOffset);
310 
311  { // fix the Redlines
313  SwPosition aNewPos(rNewPos);
314  for(SwRangeRedline* p : rTable)
315  {
316  // lies on the position ??
317  lcl_PaMCorrRel1( p, &rOldNode.GetNode(), aNewPos, aNewPos.nContent.GetIndex() + nOffset );
318  }
319 
320  // To-Do - need to add here 'SwExtraRedlineTable' also ?
321  }
322 
323  if(bMoveCursor)
324  ::PaMCorrRel(rOldNode, rNewPos, nOffset);
325 }
326 
328 {
329  SwViewShell const *pCurrentView = getIDocumentLayoutAccess().GetCurrentViewShell();
330  // Layout and OLE shells should be available
331  if( pCurrentView )
332  {
333  for(const SwViewShell& rCurrentSh : pCurrentView->GetRingContainer())
334  {
335  // look for an EditShell (if it exists)
336  if( auto pEditShell = dynamic_cast<const SwEditShell *>(&rCurrentSh) )
337  {
338  return pEditShell;
339  }
340  }
341  }
342  return nullptr;
343 }
344 
346 {
347  return const_cast<SwEditShell*>( const_cast<SwDoc const *>( this )->GetEditShell() );
348 }
349 
351 {
352  return GetEditShell();
353 }
354 
355 //bool foo()
356 //{
357 // bool b1 = true ? true : false;
358 // bool b2 = (true ? true : false) ? true : false;
359 // bool b3 = true ? (true ? true : false) : false;
360 // bool b4 = true ? true : (true ? true : false);
361 // return false;
362 //}
363 
364 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Starts a section of nodes in the document model.
Definition: node.hxx:311
Marks a position in the document model.
Definition: pam.hxx:35
void DelBookmarks(const SwNodeIndex &rStt, const SwNodeIndex &rEnd, std::vector< SaveBookmark > *pSaveBkmk, const SwIndex *pSttIdx, const SwIndex *pEndIdx)
Definition: docbm.cxx:1813
SwNodeIndex nNode
Definition: pam.hxx:37
bool IsTableMode() const
Definition: crsrsh.hxx:643
const SwPosition * GetMark() const
Definition: pam.hxx:209
virtual void correctMarksRelative(const SwNodeIndex &rOldNode, const SwPosition &rNewPos, const sal_Int32 nOffset)=0
Corrects marks (relative) This method uses the previous position of the mark in the paragraph as offs...
sal_Int64 n
Definition: doc.hxx:186
SwNode & GetNode() const
Definition: ndindex.hxx:119
SwPosition & GetBound(bool bOne=true)
Definition: pam.hxx:245
IDocumentMarkAccess * getIDocumentMarkAccess()
Definition: docbm.cxx:1704
SwPaM * GetStackCursor() const
Definition: crsrsh.hxx:873
size_type size() const
Definition: docary.hxx:269
void cleanupUnoCursorTable() const
Definition: doc.hxx:1637
SwIndex nContent
Definition: pam.hxx:38
SwPaM * GetNext()
Definition: pam.hxx:264
SwShellCursor * GetCursor_()
Definition: crsrsh.hxx:328
PaM is Point and Mark: a selection of the document model.
Definition: pam.hxx:136
const SwPaM * GetTableCrs() const
Definition: crsrsh.hxx:904
const SwStartNode * StartOfSectionNode() const
Definition: node.hxx:131
SwDoc & GetDoc()
Definition: node.hxx:211
void DeleteAndDestroy(size_type nPos)
Definition: docredln.cxx:636
const SwPosition * GetPoint() const
Definition: pam.hxx:207
SwIndex & Assign(SwIndexReg *, sal_Int32)
Definition: index.cxx:206
SwEditShell const * GetEditShell() const
Definition: doccorr.cxx:327
std::vector< std::weak_ptr< SwUnoCursor > > mvUnoCursorTable
Definition: doc.hxx:1634
SwContentNode * GetContentNode()
Definition: node.hxx:618
vector_type::size_type size_type
Definition: docary.hxx:228
void PaMCorrRel(const SwNodeIndex &rOldNode, const SwPosition &rNewPos, const sal_Int32 nOffset)
Sets all PaMs in OldNode to relative Pos.
Definition: doccorr.cxx:240
Marks a node in the document model.
Definition: ndindex.hxx:31
ring_container GetRingContainer()
Definition: ring.hxx:240
SwStartNodeType GetStartNodeType() const
Definition: node.hxx:328
IDocumentLayoutAccess const & getIDocumentLayoutAccess() const
Definition: doc.cxx:405
const SwPosition * Start() const
Definition: pam.hxx:212
The Undo actions need to create new Shell cursors.
void CorrRel(const SwNodeIndex &rOldNode, const SwPosition &rNewPos, const sal_Int32 nOffset=0, bool bMoveCursor=false)
Definition: doccorr.cxx:304
IDocumentRedlineAccess const & getIDocumentRedlineAccess() const
Definition: doc.cxx:335
void CorrAbs(const SwNodeIndex &rOldNode, const SwPosition &rNewPos, const sal_Int32 nOffset=0, bool bMoveCursor=false)
Definition: doccorr.cxx:167
virtual const SwViewShell * GetCurrentViewShell() const =0
Returns the layout set at the document.
sal_Int32 GetIndex() const
Definition: index.hxx:91
const SwPosition * End() const
Definition: pam.hxx:217
void PaMCorrAbs(const SwPaM &rRange, const SwPosition &rNewPos)
Function declarations so that everything below the CursorShell can move the Cursor once in a while...
Definition: doccorr.cxx:85
void * p
SwCursor & GetSelRing()
Definition: unocrsr.hxx:100
virtual void correctMarksAbsolute(const SwNodeIndex &rOldNode, const SwPosition &rNewPos, const sal_Int32 nOffset)=0
Corrects marks (absolute) This method ignores the previous position of the mark in the paragraph...
virtual const SwRedlineTable & GetRedlineTable() const =0
const SwIndexReg * GetIdxReg() const
Definition: index.hxx:97
::sw::IShellCursorSupplier * GetIShellCursorSupplier()
Definition: doccorr.cxx:350
SwNodeIndex * GetContentIdx() const
Definition: redline.hxx:184
Base class of the Writer document model elements.
Definition: node.hxx:79