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