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