LibreOffice Module sw (master) 1
DocumentContentOperationsManager.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 */
20#include <doc.hxx>
21#include <IDocumentUndoRedo.hxx>
23#include <IDocumentState.hxx>
28#include <UndoManager.hxx>
29#include <docary.hxx>
30#include <textboxhelper.hxx>
31#include <dcontact.hxx>
32#include <grfatr.hxx>
33#include <numrule.hxx>
34#include <charfmt.hxx>
35#include <ndgrf.hxx>
36#include <ndnotxt.hxx>
37#include <ndole.hxx>
38#include <breakit.hxx>
39#include <frmfmt.hxx>
40#include <fmtanchr.hxx>
41#include <fmtcntnt.hxx>
42#include <fmtinfmt.hxx>
43#include <fmtpdsc.hxx>
44#include <fmtcnct.hxx>
45#include <SwStyleNameMapper.hxx>
46#include <redline.hxx>
47#include <txtfrm.hxx>
48#include <rootfrm.hxx>
49#include <frmtool.hxx>
50#include <unocrsr.hxx>
51#include <mvsave.hxx>
52#include <ndtxt.hxx>
53#include <poolfmt.hxx>
54#include <paratr.hxx>
55#include <txatbase.hxx>
56#include <UndoRedline.hxx>
57#include <undobj.hxx>
58#include <UndoBookmark.hxx>
59#include <UndoDelete.hxx>
60#include <UndoSplitMove.hxx>
61#include <UndoOverwrite.hxx>
62#include <UndoInsert.hxx>
63#include <UndoAttribute.hxx>
64#include <rolbck.hxx>
65#include <acorrect.hxx>
66#include <bookmark.hxx>
67#include <ftnidx.hxx>
68#include <txtftn.hxx>
69#include <hints.hxx>
70#include <fmtflcnt.hxx>
71#include <docedt.hxx>
72#include <frameformats.hxx>
73#include <o3tl/safeint.hxx>
74#include <sal/log.hxx>
79#include <sfx2/Metadatable.hxx>
80#include <sot/exchange.hxx>
81#include <svl/stritem.hxx>
82#include <svl/itemiter.hxx>
83#include <svx/svdobj.hxx>
84#include <svx/svdouno.hxx>
85#include <tools/globname.hxx>
87#include <com/sun/star/i18n/Boundary.hpp>
88#include <com/sun/star/i18n/WordType.hpp>
89#include <com/sun/star/i18n/XBreakIterator.hpp>
90#include <com/sun/star/embed/XEmbeddedObject.hpp>
91
92#include <tuple>
93#include <memory>
94
95using namespace ::com::sun::star::i18n;
96
97namespace
98{
99 // Copy method from SwDoc
100 // Prevent copying into Flys that are anchored in the range
101 bool lcl_ChkFlyFly( SwDoc& rDoc, SwNodeOffset nSttNd, SwNodeOffset nEndNd,
102 SwNodeOffset nInsNd )
103 {
104 const SwFrameFormats& rFrameFormatTable = *rDoc.GetSpzFrameFormats();
105
106 for( size_t n = 0; n < rFrameFormatTable.size(); ++n )
107 {
108 SwFrameFormat const*const pFormat = rFrameFormatTable[n];
109 SwFormatAnchor const*const pAnchor = &pFormat->GetAnchor();
110 SwPosition const*const pAPos = pAnchor->GetContentAnchor();
111 if (pAPos &&
112 ((RndStdIds::FLY_AS_CHAR == pAnchor->GetAnchorId()) ||
113 (RndStdIds::FLY_AT_CHAR == pAnchor->GetAnchorId()) ||
114 (RndStdIds::FLY_AT_FLY == pAnchor->GetAnchorId()) ||
115 (RndStdIds::FLY_AT_PARA == pAnchor->GetAnchorId())) &&
116 nSttNd <= pAPos->GetNodeIndex() &&
117 pAPos->GetNodeIndex() < nEndNd )
118 {
119 const SwFormatContent& rContent = pFormat->GetContent();
120 SwStartNode* pSNd;
121 if( !rContent.GetContentIdx() ||
122 nullptr == ( pSNd = rContent.GetContentIdx()->GetNode().GetStartNode() ))
123 continue;
124
125 if( pSNd->GetIndex() < nInsNd &&
126 nInsNd < pSNd->EndOfSectionIndex() )
127 // Do not copy !
128 return true;
129
130 if( lcl_ChkFlyFly( rDoc, pSNd->GetIndex(),
131 pSNd->EndOfSectionIndex(), nInsNd ) )
132 // Do not copy !
133 return true;
134 }
135 }
136
137 return false;
138 }
139
140 SwNodeIndex InitDelCount(SwPaM const& rSourcePaM, SwNodeOffset & rDelCount)
141 {
142 SwPosition const& rStart(*rSourcePaM.Start());
143 // Special handling for SwDoc::AppendDoc
144 if (rSourcePaM.GetDoc().GetNodes().GetEndOfExtras().GetIndex() + 1
145 == rStart.GetNodeIndex())
146 {
147 rDelCount = SwNodeOffset(1);
148 return SwNodeIndex(rStart.GetNode(), +1);
149 }
150 else
151 {
152 rDelCount = SwNodeOffset(0);
153 return SwNodeIndex(rStart.GetNode());
154 }
155 }
156
157 /*
158 The CopyBookmarks function has to copy bookmarks from the source to the destination nodes
159 array. It is called after a call of the CopyNodes(..) function. But this function does not copy
160 every node (at least at the moment: 2/08/2006 ), section start and end nodes will not be copied
161 if the corresponding end/start node is outside the copied pam.
162 The lcl_NonCopyCount function counts the number of these nodes, given the copied pam and a node
163 index inside the pam.
164 rPam is the original source pam, rLastIdx is the last calculated position, rDelCount the number
165 of "non-copy" nodes between rPam.Start() and rLastIdx.
166 nNewIdx is the new position of interest.
167 */
168 void lcl_NonCopyCount( const SwPaM& rPam, SwNodeIndex& rLastIdx, const SwNodeOffset nNewIdx, SwNodeOffset& rDelCount )
169 {
170 SwNodeOffset nStart = rPam.Start()->GetNodeIndex();
171 SwNodeOffset nEnd = rPam.End()->GetNodeIndex();
172 if( rLastIdx.GetIndex() < nNewIdx ) // Moving forward?
173 {
174 // We never copy the StartOfContent node
175 do // count "non-copy" nodes
176 {
177 SwNode& rNode = rLastIdx.GetNode();
178 if( ( rNode.IsSectionNode() && rNode.EndOfSectionIndex() >= nEnd )
179 || ( rNode.IsEndNode() && rNode.StartOfSectionNode()->GetIndex() < nStart ) )
180 {
181 ++rDelCount;
182 }
183 ++rLastIdx;
184 }
185 while( rLastIdx.GetIndex() < nNewIdx );
186 }
187 else if( rDelCount ) // optimization: if there are no "non-copy" nodes until now,
188 // no move backward needed
189 {
190 while( rLastIdx.GetIndex() > nNewIdx )
191 {
192 SwNode& rNode = rLastIdx.GetNode();
193 if( ( rNode.IsSectionNode() && rNode.EndOfSectionIndex() >= nEnd )
194 || ( rNode.IsEndNode() && rNode.StartOfSectionNode()->GetIndex() < nStart ) )
195 {
196 --rDelCount;
197 }
198 rLastIdx--;
199 }
200 }
201 }
202
203 void lcl_SetCpyPos( const SwPosition& rOrigPos,
204 const SwPosition& rOrigStt,
205 const SwPosition& rCpyStt,
206 SwPosition& rChgPos,
207 SwNodeOffset nDelCount )
208 {
209 SwNodeOffset nNdOff = rOrigPos.GetNodeIndex();
210 nNdOff -= rOrigStt.GetNodeIndex();
211 nNdOff -= nDelCount;
212 sal_Int32 nContentPos = rOrigPos.GetContentIndex();
213
214 // Always adjust <nNode> at to be changed <SwPosition> instance <rChgPos>
215 rChgPos.Assign( nNdOff + rCpyStt.GetNodeIndex() );
216 if (!rChgPos.GetNode().GetContentNode())
217 return;
218 if( !nNdOff )
219 {
220 // just adapt the content index
221 if( nContentPos > rOrigStt.GetContentIndex() )
222 nContentPos -= rOrigStt.GetContentIndex();
223 else
224 nContentPos = 0;
225 nContentPos += rCpyStt.GetContentIndex();
226 }
227 rChgPos.SetContent( nContentPos );
228 }
229
230}
231
232namespace sw
233{
234 // TODO: use SaveBookmark (from DelBookmarks)
235 void CopyBookmarks(const SwPaM& rPam, const SwPosition& rCpyPam)
236 {
237 const SwDoc& rSrcDoc = rPam.GetDoc();
238 SwDoc& rDestDoc = rCpyPam.GetDoc();
239 const IDocumentMarkAccess* const pSrcMarkAccess = rSrcDoc.getIDocumentMarkAccess();
240 ::sw::UndoGuard const undoGuard(rDestDoc.GetIDocumentUndoRedo());
241
242 const SwPosition &rStt = *rPam.Start(), &rEnd = *rPam.End();
243 SwPosition const*const pCpyStt = &rCpyPam;
244
245 std::vector< const ::sw::mark::IMark* > vMarksToCopy;
246 for ( IDocumentMarkAccess::const_iterator_t ppMark = pSrcMarkAccess->getAllMarksBegin();
247 ppMark != pSrcMarkAccess->getAllMarksEnd();
248 ++ppMark )
249 {
250 const ::sw::mark::IMark* const pMark = *ppMark;
251
252 const SwPosition& rMarkStart = pMark->GetMarkStart();
253 const SwPosition& rMarkEnd = pMark->GetMarkEnd();
254 // only include marks that are in the range and not touching both start and end
255 // - not for annotation or checkbox marks.
256 bool const isIncludeStart(
257 (rStt.GetContentIndex() == 0 // paragraph start selected?
258 // also: only if inserting at the start - cross reference
259 // marks require index to be 0, and there could be one
260 // on the target node already
261 && rCpyPam.GetContentIndex() == 0)
262 || rMarkStart != rStt);
263 bool const isIncludeEnd(
264 (rEnd.GetNode().IsTextNode() // paragraph end selected?
265 && rEnd.GetContentIndex() == rEnd.GetNode().GetTextNode()->Len())
266 || rMarkEnd != rEnd);
267 const bool bIsNotOnBoundary =
268 pMark->IsExpanded()
269 ? (isIncludeStart || isIncludeEnd) // rMarkStart != rMarkEnd
270 : (isIncludeStart && isIncludeEnd); // rMarkStart == rMarkEnd
272 if ( rMarkStart >= rStt && rMarkEnd <= rEnd
273 && ( bIsNotOnBoundary
279 {
280 vMarksToCopy.push_back(pMark);
281 }
282 }
283 // We have to count the "non-copied" nodes...
284 SwNodeOffset nDelCount;
285 SwNodeIndex aCorrIdx(InitDelCount(rPam, nDelCount));
286 for(const sw::mark::IMark* const pMark : vMarksToCopy)
287 {
288 SwPaM aTmpPam(*pCpyStt);
289 lcl_NonCopyCount(rPam, aCorrIdx, pMark->GetMarkPos().GetNodeIndex(), nDelCount);
290 lcl_SetCpyPos( pMark->GetMarkPos(), rStt, *pCpyStt, *aTmpPam.GetPoint(), nDelCount);
291 if(pMark->IsExpanded())
292 {
293 aTmpPam.SetMark();
294 lcl_NonCopyCount(rPam, aCorrIdx, pMark->GetOtherMarkPos().GetNodeIndex(), nDelCount);
295 lcl_SetCpyPos(pMark->GetOtherMarkPos(), rStt, *pCpyStt, *aTmpPam.GetMark(), nDelCount);
296 }
297
298 ::sw::mark::IMark* const pNewMark = rDestDoc.getIDocumentMarkAccess()->makeMark(
299 aTmpPam,
300 pMark->GetName(),
303 // Explicitly try to get exactly the same name as in the source
304 // because NavigatorReminders, DdeBookmarks etc. ignore the proposed name
305 if (pNewMark == nullptr)
306 {
309 continue; // can't insert duplicate cross reference mark
310 }
311 rDestDoc.getIDocumentMarkAccess()->renameMark(pNewMark, pMark->GetName());
312
313 // copying additional attributes for bookmarks or fieldmarks
314 ::sw::mark::IBookmark* const pNewBookmark =
315 dynamic_cast< ::sw::mark::IBookmark* const >(pNewMark);
316 const ::sw::mark::IBookmark* const pOldBookmark =
317 dynamic_cast< const ::sw::mark::IBookmark* >(pMark);
318 if (pNewBookmark && pOldBookmark)
319 {
320 pNewBookmark->SetKeyCode(pOldBookmark->GetKeyCode());
321 pNewBookmark->SetShortName(pOldBookmark->GetShortName());
322 pNewBookmark->Hide(pOldBookmark->IsHidden());
323 pNewBookmark->SetHideCondition(pOldBookmark->GetHideCondition());
324 }
325 ::sw::mark::IFieldmark* const pNewFieldmark =
326 dynamic_cast< ::sw::mark::IFieldmark* const >(pNewMark);
327 const ::sw::mark::IFieldmark* const pOldFieldmark =
328 dynamic_cast< const ::sw::mark::IFieldmark* >(pMark);
329 if (pNewFieldmark && pOldFieldmark)
330 {
331 pNewFieldmark->SetFieldname(pOldFieldmark->GetFieldname());
332 pNewFieldmark->SetFieldHelptext(pOldFieldmark->GetFieldHelptext());
333 ::sw::mark::IFieldmark::parameter_map_t* pNewParams = pNewFieldmark->GetParameters();
334 const ::sw::mark::IFieldmark::parameter_map_t* pOldParams = pOldFieldmark->GetParameters();
335 for (const auto& rEntry : *pOldParams )
336 {
337 pNewParams->insert( rEntry );
338 }
339 }
340
341 ::sfx2::Metadatable const*const pMetadatable(
342 dynamic_cast< ::sfx2::Metadatable const* >(pMark));
343 ::sfx2::Metadatable *const pNewMetadatable(
344 dynamic_cast< ::sfx2::Metadatable * >(pNewMark));
345 if (pMetadatable && pNewMetadatable)
346 {
347 pNewMetadatable->RegisterAsCopyOf(*pMetadatable);
348 }
349 }
350 }
351} // namespace sw
352
353namespace
354{
355 void lcl_DeleteRedlines( const SwPaM& rPam, SwPaM& rCpyPam )
356 {
357 const SwDoc& rSrcDoc = rPam.GetDoc();
358 const SwRedlineTable& rTable = rSrcDoc.getIDocumentRedlineAccess().GetRedlineTable();
359 if( rTable.empty() )
360 return;
361
362 SwDoc& rDestDoc = rCpyPam.GetDoc();
363 SwPosition* pCpyStt = rCpyPam.Start(), *pCpyEnd = rCpyPam.End();
364 std::unique_ptr<SwPaM> pDelPam;
365 auto [pStt, pEnd] = rPam.StartEnd(); // SwPosition*
366 // We have to count the "non-copied" nodes
367 SwNodeOffset nDelCount;
368 SwNodeIndex aCorrIdx(InitDelCount(rPam, nDelCount));
369
371 rSrcDoc.getIDocumentRedlineAccess().GetRedline( *pStt, &n );
372 for( ; n < rTable.size(); ++n )
373 {
374 const SwRangeRedline* pRedl = rTable[ n ];
375 if( RedlineType::Delete == pRedl->GetType() && pRedl->IsVisible() )
376 {
377 auto [pRStt, pREnd] = pRedl->StartEnd(); // SwPosition*
378
379 SwComparePosition eCmpPos = ComparePosition( *pStt, *pEnd, *pRStt, *pREnd );
380 switch( eCmpPos )
381 {
384 // Pos1 is before Pos2
385 break;
386
389 // Pos1 is after Pos2
390 n = rTable.size();
391 break;
392
393 default:
394 {
395 pDelPam.reset(new SwPaM( *pCpyStt, pDelPam.release() ));
396 if( *pStt < *pRStt )
397 {
398 lcl_NonCopyCount( rPam, aCorrIdx, pRStt->GetNodeIndex(), nDelCount );
399 lcl_SetCpyPos( *pRStt, *pStt, *pCpyStt,
400 *pDelPam->GetPoint(), nDelCount );
401 }
402 pDelPam->SetMark();
403
404 if( *pEnd < *pREnd )
405 *pDelPam->GetPoint() = *pCpyEnd;
406 else
407 {
408 lcl_NonCopyCount( rPam, aCorrIdx, pREnd->GetNodeIndex(), nDelCount );
409 lcl_SetCpyPos( *pREnd, *pStt, *pCpyStt,
410 *pDelPam->GetPoint(), nDelCount );
411 }
412
413 if (pDelPam->GetNext() != pDelPam.get()
414 && *pDelPam->GetNext()->End() == *pDelPam->Start())
415 {
416 *pDelPam->GetNext()->End() = *pDelPam->End();
417 pDelPam.reset(pDelPam->GetNext());
418 }
419 }
420 }
421 }
422 }
423
424 if( !pDelPam )
425 return;
426
429
430 ::sw::UndoGuard const undoGuard(rDestDoc.GetIDocumentUndoRedo());
431
432 do {
433 rDestDoc.getIDocumentContentOperations().DeleteAndJoin( *pDelPam->GetNext() );
434 if( !pDelPam->IsMultiSelection() )
435 break;
436 delete pDelPam->GetNext();
437 } while( true );
438
440 }
441
442 void lcl_DeleteRedlines( const SwNodeRange& rRg, SwNodeRange const & rCpyRg )
443 {
444 SwDoc& rSrcDoc = rRg.aStart.GetNode().GetDoc();
446 {
447 SwPaM aRgTmp( rRg.aStart, rRg.aEnd );
448 SwPaM aCpyTmp( rCpyRg.aStart, rCpyRg.aEnd );
449 lcl_DeleteRedlines( aRgTmp, aCpyTmp );
450 }
451 }
452
453 void lcl_ChainFormats( SwFlyFrameFormat *pSrc, SwFlyFrameFormat *pDest )
454 {
455 SwFormatChain aSrc( pSrc->GetChain() );
456 if ( !aSrc.GetNext() )
457 {
458 aSrc.SetNext( pDest );
459 pSrc->SetFormatAttr( aSrc );
460 }
461 SwFormatChain aDest( pDest->GetChain() );
462 if ( !aDest.GetPrev() )
463 {
464 aDest.SetPrev( pSrc );
465 pDest->SetFormatAttr( aDest );
466 }
467 }
468
469 // #i86492#
470 bool lcl_ContainsOnlyParagraphsInList( const SwPaM& rPam )
471 {
472 bool bRet = false;
473
474 const SwTextNode* pTextNd = rPam.Start()->GetNode().GetTextNode();
475 const SwTextNode* pEndTextNd = rPam.End()->GetNode().GetTextNode();
476 if ( pTextNd && pTextNd->IsInList() &&
477 pEndTextNd && pEndTextNd->IsInList() )
478 {
479 bRet = true;
480 SwNodeIndex aIdx(rPam.Start()->GetNode());
481
482 do
483 {
484 ++aIdx;
485 pTextNd = aIdx.GetNode().GetTextNode();
486
487 if ( !pTextNd || !pTextNd->IsInList() )
488 {
489 bRet = false;
490 break;
491 }
492 } while (pTextNd != pEndTextNd);
493 }
494
495 return bRet;
496 }
497
498 bool lcl_MarksWholeNode(const SwPaM & rPam)
499 {
500 bool bResult = false;
501 auto [pStt, pEnd] = rPam.StartEnd(); // SwPosition*
502
503 if (nullptr != pStt && nullptr != pEnd)
504 {
505 const SwTextNode* pSttNd = pStt->GetNode().GetTextNode();
506 const SwTextNode* pEndNd = pEnd->GetNode().GetTextNode();
507
508 if (nullptr != pSttNd && nullptr != pEndNd &&
509 pStt->GetContentIndex() == 0 &&
510 pEnd->GetContentIndex() == pEndNd->Len())
511 {
512 bResult = true;
513 }
514 }
515
516 return bResult;
517 }
518}
519
520//local functions originally from sw/source/core/doc/docedt.cxx
521namespace sw
522{
523 void CalcBreaks(std::vector<std::pair<SwNodeOffset, sal_Int32>> & rBreaks,
524 SwPaM const & rPam, bool const isOnlyFieldmarks)
525 {
526 SwNodeOffset const nStartNode(rPam.Start()->GetNodeIndex());
527 SwNodeOffset const nEndNode(rPam.End()->GetNodeIndex());
528 SwNodes const& rNodes(rPam.GetPoint()->GetNodes());
529 IDocumentMarkAccess const& rIDMA(*rPam.GetDoc().getIDocumentMarkAccess());
530
531 std::stack<std::tuple<sw::mark::IFieldmark const*, bool, SwNodeOffset, sal_Int32>> startedFields;
532
533 for (SwNodeOffset n = nStartNode; n <= nEndNode; ++n)
534 {
535 SwNode *const pNode(rNodes[n]);
536 if (pNode->IsTextNode())
537 {
538 SwTextNode & rTextNode(*pNode->GetTextNode());
539 sal_Int32 const nStart(n == nStartNode
540 ? rPam.Start()->GetContentIndex()
541 : 0);
542 sal_Int32 const nEnd(n == nEndNode
543 ? rPam.End()->GetContentIndex()
544 : rTextNode.Len());
545 for (sal_Int32 i = nStart; i < nEnd; ++i)
546 {
547 const sal_Unicode c(rTextNode.GetText()[i]);
548 switch (c)
549 {
550 // note: CH_TXT_ATR_FORMELEMENT does not need handling
551 // not sure how CH_TXT_ATR_INPUTFIELDSTART/END are currently handled
552 case CH_TXTATR_INWORD:
554 {
555 // META hints only have dummy char at the start, not
556 // at the end, so no need to check in nStartNode
557 if (n == nEndNode && !isOnlyFieldmarks)
558 {
559 SwTextAttr const* pAttr(rTextNode.GetTextAttrForCharAt(i));
560 if (pAttr && pAttr->End() && (nEnd < *pAttr->End()))
561 {
562 assert(pAttr->HasDummyChar());
563 rBreaks.emplace_back(n, i);
564 }
565
566 if (!pAttr)
567 {
568 // See if this is an end dummy character for a content control.
570 if (pAttr && (nStart > pAttr->GetStart()))
571 {
572 rBreaks.emplace_back(n, i);
573 }
574 }
575 }
576 break;
577 }
579 {
580 auto const pFieldMark(rIDMA.getFieldmarkAt(SwPosition(rTextNode, i)));
581 startedFields.emplace(pFieldMark, false, 0, 0);
582 break;
583 }
585 {
586 if (startedFields.empty())
587 {
588 rBreaks.emplace_back(n, i);
589 }
590 else
591 { // no way to find the field via MarkManager...
592 assert(std::get<0>(startedFields.top())->IsCoveringPosition(SwPosition(rTextNode, i)));
593 std::get<1>(startedFields.top()) = true;
594 std::get<2>(startedFields.top()) = n;
595 std::get<3>(startedFields.top()) = i;
596 }
597 break;
598 }
600 {
601 if (startedFields.empty())
602 {
603 rBreaks.emplace_back(n, i);
604 }
605 else
606 { // fieldmarks must not overlap => stack
607 assert(std::get<0>(startedFields.top()) == rIDMA.getFieldmarkAt(SwPosition(rTextNode, i)));
608 startedFields.pop();
609 }
610 break;
611 }
612 }
613 }
614 }
615 else if (pNode->IsStartNode())
616 {
617 if (pNode->EndOfSectionIndex() <= nEndNode)
618 { // fieldmark cannot overlap node section
619 n = pNode->EndOfSectionIndex();
620 }
621 }
622 else
623 { // EndNode can actually happen with sections :(
624 assert(pNode->IsEndNode() || pNode->IsNoTextNode());
625 }
626 }
627 while (!startedFields.empty())
628 {
629 SwPosition const& rStart(std::get<0>(startedFields.top())->GetMarkStart());
630 std::pair<SwNodeOffset, sal_Int32> const pos(
631 rStart.GetNodeIndex(), rStart.GetContentIndex());
632 auto it = std::lower_bound(rBreaks.begin(), rBreaks.end(), pos);
633 assert(it == rBreaks.end() || *it != pos);
634 rBreaks.insert(it, pos);
635 if (std::get<1>(startedFields.top()))
636 {
637 std::pair<SwNodeOffset, sal_Int32> const posSep(
638 std::get<2>(startedFields.top()),
639 std::get<3>(startedFields.top()));
640 it = std::lower_bound(rBreaks.begin(), rBreaks.end(), posSep);
641 assert(it == rBreaks.end() || *it != posSep);
642 rBreaks.insert(it, posSep);
643 }
644 startedFields.pop();
645 }
646 }
647}
648
649namespace
650{
651
652 bool lcl_DoWithBreaks(::sw::DocumentContentOperationsManager & rDocumentContentOperations,
653 SwPaM & rPam, SwDeleteFlags const flags,
655 {
656 std::vector<std::pair<SwNodeOffset, sal_Int32>> Breaks;
657
658 sw::CalcBreaks(Breaks, rPam);
659
660 if (Breaks.empty())
661 {
662 return (rDocumentContentOperations.*pFunc)(rPam, flags);
663 }
664
665 // Deletion must be split into several parts if the text node
666 // contains a text attribute with end and with dummy character
667 // and the selection does not contain the text attribute completely,
668 // but overlaps its start (left), where the dummy character is.
669
670 SwPosition const & rSelectionEnd( *rPam.End() );
671
672 bool bRet( true );
673 // iterate from end to start, to avoid invalidating the offsets!
674 auto iter( Breaks.rbegin() );
675 SwNodeOffset nOffset(0);
676 SwNodes const& rNodes(rPam.GetPoint()->GetNodes());
677 SwPaM aPam( rSelectionEnd, rSelectionEnd ); // end node!
678 SwPosition & rEnd( *aPam.End() );
679 SwPosition & rStart( *aPam.Start() );
680
681 while (iter != Breaks.rend())
682 {
683 rStart.Assign(*rNodes[iter->first - nOffset]->GetTextNode(), iter->second + 1);
684 if (rStart < rEnd) // check if part is empty
685 {
686 bRet &= (rDocumentContentOperations.*pFunc)(aPam, flags);
687 nOffset = iter->first - rStart.GetNodeIndex(); // deleted fly nodes...
688 }
689 rEnd.Assign(*rNodes[iter->first - nOffset]->GetTextNode(), iter->second);
690 ++iter;
691 }
692
693 rStart = *rPam.Start(); // set to original start
694 if (rStart < rEnd) // check if part is empty
695 {
696 bRet &= (rDocumentContentOperations.*pFunc)(aPam, flags);
697 }
698
699 return bRet;
700 }
701
702 bool lcl_StrLenOverflow( const SwPaM& rPam )
703 {
704 // If we try to merge two paragraphs we have to test if afterwards
705 // the string doesn't exceed the allowed string length
706 if( rPam.GetPoint()->GetNode() != rPam.GetMark()->GetNode() )
707 {
708 auto [pStt, pEnd] = rPam.StartEnd(); // SwPosition*
709 const SwTextNode* pEndNd = pEnd->GetNode().GetTextNode();
710 if( (nullptr != pEndNd) && pStt->GetNode().IsTextNode() )
711 {
712 const sal_uInt64 nSum = pStt->GetContentIndex() +
713 pEndNd->GetText().getLength() - pEnd->GetContentIndex();
714 return nSum > o3tl::make_unsigned(SAL_MAX_INT32);
715 }
716 }
717 return false;
718 }
719
720 struct SaveRedline
721 {
722 SwRangeRedline* pRedl;
723 SwNodeOffset nStt, nEnd;
724 sal_Int32 nSttCnt;
725 sal_Int32 nEndCnt;
726
727 SaveRedline( SwRangeRedline* pR, const SwNodeIndex& rSttIdx )
728 : pRedl(pR)
729 , nEnd(0)
730 , nEndCnt(0)
731 {
732 auto [pStt, pEnd] = pR->StartEnd(); // SwPosition*
733 SwNodeOffset nSttIdx = rSttIdx.GetIndex();
734 nStt = pStt->GetNodeIndex() - nSttIdx;
735 nSttCnt = pStt->GetContentIndex();
736 if( pR->HasMark() )
737 {
738 nEnd = pEnd->GetNodeIndex() - nSttIdx;
739 nEndCnt = pEnd->GetContentIndex();
740 }
741
742 pRedl->GetPoint()->Assign( SwNodeOffset(0) );
743 pRedl->GetMark()->Assign( SwNodeOffset(0) );
744 }
745
746 SaveRedline( SwRangeRedline* pR, const SwPosition& rPos )
747 : pRedl(pR)
748 , nEnd(0)
749 , nEndCnt(0)
750 {
751 auto [pStt, pEnd] = pR->StartEnd(); // SwPosition*
752 SwNodeOffset nSttIdx = rPos.GetNodeIndex();
753 nStt = pStt->GetNodeIndex() - nSttIdx;
754 nSttCnt = pStt->GetContentIndex();
755 if( nStt == SwNodeOffset(0) )
756 nSttCnt = nSttCnt - rPos.GetContentIndex();
757 if( pR->HasMark() )
758 {
759 nEnd = pEnd->GetNodeIndex() - nSttIdx;
760 nEndCnt = pEnd->GetContentIndex();
761 if( nEnd == SwNodeOffset(0) )
762 nEndCnt = nEndCnt - rPos.GetContentIndex();
763 }
764
765 pRedl->GetPoint()->Assign( SwNodeOffset(0) );
766 pRedl->GetMark()->Assign( SwNodeOffset(0) );
767 }
768
769 void SetPos( SwNodeOffset nInsPos )
770 {
771 pRedl->GetPoint()->Assign( nInsPos + nStt, nSttCnt );
772 if( pRedl->HasMark() )
773 {
774 pRedl->GetMark()->Assign( nInsPos + nEnd, nEndCnt );
775 }
776 }
777
778 void SetPos( const SwPosition& aPos )
779 {
780 pRedl->GetPoint()->Assign( aPos.GetNodeIndex() + nStt,
781 nSttCnt + ( nStt == SwNodeOffset(0) ? aPos.GetContentIndex() : 0 ) );
782 if( pRedl->HasMark() )
783 {
784 pRedl->GetMark()->Assign( aPos.GetNodeIndex() + nEnd,
785 nEndCnt + ( nEnd == SwNodeOffset(0) ? aPos.GetContentIndex() : 0 ) );
786 }
787 }
788 };
789
790 typedef std::vector< SaveRedline > SaveRedlines_t;
791
792 void lcl_SaveRedlines(const SwPaM& aPam, SaveRedlines_t& rArr)
793 {
794 SwDoc& rDoc = aPam.GetPointNode().GetDoc();
795
796 auto [pStart, pEnd] = aPam.StartEnd(); // SwPosition*
797
798 // get first relevant redline
799 SwRedlineTable::size_type nCurrentRedline;
800 rDoc.getIDocumentRedlineAccess().GetRedline( *pStart, &nCurrentRedline );
801 if( nCurrentRedline > 0)
802 nCurrentRedline--;
803
804 // redline mode RedlineFlags::Ignore|RedlineFlags::On; save old mode
807
808 // iterate over relevant redlines and decide for each whether it should
809 // be saved, or split + saved
811 for( ; nCurrentRedline < rRedlineTable.size(); nCurrentRedline++ )
812 {
813 SwRangeRedline* pCurrent = rRedlineTable[ nCurrentRedline ];
814 SwComparePosition eCompare =
815 ComparePosition( *pCurrent->Start(), *pCurrent->End(),
816 *pStart, *pEnd);
817
818 // we must save this redline if it overlaps aPam
819 // (we may have to split it, too)
820 if( eCompare == SwComparePosition::OverlapBehind ||
822 eCompare == SwComparePosition::Outside ||
823 eCompare == SwComparePosition::Inside ||
824 eCompare == SwComparePosition::Equal )
825 {
826 rRedlineTable.Remove( nCurrentRedline-- );
827
828 // split beginning, if necessary
829 if( eCompare == SwComparePosition::OverlapBefore ||
830 eCompare == SwComparePosition::Outside )
831 {
832 SwRangeRedline* pNewRedline = new SwRangeRedline( *pCurrent );
833 *pNewRedline->End() = *pStart;
834 *pCurrent->Start() = *pStart;
835 rDoc.getIDocumentRedlineAccess().AppendRedline( pNewRedline, true );
836 }
837
838 // split end, if necessary
839 if( eCompare == SwComparePosition::OverlapBehind ||
840 eCompare == SwComparePosition::Outside )
841 {
842 SwRangeRedline* pNewRedline = new SwRangeRedline( *pCurrent );
843 *pNewRedline->Start() = *pEnd;
844 *pCurrent->End() = *pEnd;
845 rDoc.getIDocumentRedlineAccess().AppendRedline( pNewRedline, true );
846 }
847
848 // save the current redline
849 rArr.emplace_back( pCurrent, *pStart );
850 }
851 }
852
853 // restore old redline mode
855 }
856
857 void lcl_RestoreRedlines(SwDoc& rDoc, const SwPosition& rPos, SaveRedlines_t& rArr)
858 {
861
862 for(SaveRedline & rSvRedLine : rArr)
863 {
864 rSvRedLine.SetPos( rPos );
865 rDoc.getIDocumentRedlineAccess().AppendRedline( rSvRedLine.pRedl, true );
866 }
867
869 }
870
871 void lcl_SaveRedlines(const SwNodeRange& rRg, SaveRedlines_t& rArr)
872 {
873 SwDoc& rDoc = rRg.aStart.GetNode().GetDoc();
875 SwPosition aSrchPos( rRg.aStart );
876 aSrchPos.Adjust(SwNodeOffset(-1));
877 if( rDoc.getIDocumentRedlineAccess().GetRedline( aSrchPos, &nRedlPos ) && nRedlPos )
878 --nRedlPos;
879 else if( nRedlPos >= rDoc.getIDocumentRedlineAccess().GetRedlineTable().size() )
880 return ;
881
885
886 do {
887 SwRangeRedline* pTmp = rRedlTable[ nRedlPos ];
888
889 auto [pRStt, pREnd] = pTmp->StartEnd(); // SwPosition*
890
891 if( pRStt->GetNode() < rRg.aStart.GetNode() )
892 {
893 if( pREnd->GetNode() > rRg.aStart.GetNode() && pREnd->GetNode() < rRg.aEnd.GetNode() )
894 {
895 // Create a copy and set the end of the original to the end of the MoveArea.
896 // The copy is moved too.
897 SwRangeRedline* pNewRedl = new SwRangeRedline( *pTmp );
898 SwPosition* pTmpPos = pNewRedl->Start();
899 pTmpPos->Assign(rRg.aStart);
900
901 rArr.emplace_back(pNewRedl, rRg.aStart);
902
903 pTmpPos = pTmp->End();
904 pTmpPos->Assign(rRg.aEnd);
905 }
906 else if( pREnd->GetNode() == rRg.aStart.GetNode() )
907 {
908 SwPosition* pTmpPos = pTmp->End();
909 pTmpPos->Assign(rRg.aEnd);
910 }
911 }
912 else if( pRStt->GetNode() < rRg.aEnd.GetNode() )
913 {
914 rRedlTable.Remove( nRedlPos-- );
915 if( pREnd->GetNode() < rRg.aEnd.GetNode() ||
916 ( pREnd->GetNode() == rRg.aEnd.GetNode() && !pREnd->GetContentIndex()) )
917 {
918 // move everything
919 rArr.emplace_back( pTmp, rRg.aStart );
920 }
921 else
922 {
923 // split
924 SwRangeRedline* pNewRedl = new SwRangeRedline( *pTmp );
925 SwPosition* pTmpPos = pNewRedl->End();
926 pTmpPos->Assign(rRg.aEnd);
927
928 rArr.emplace_back( pNewRedl, rRg.aStart );
929
930 pTmpPos = pTmp->Start();
931 pTmpPos->Assign(rRg.aEnd);
932 rDoc.getIDocumentRedlineAccess().AppendRedline( pTmp, true );
933 }
934 }
935 else
936 break;
937
938 } while( ++nRedlPos < rDoc.getIDocumentRedlineAccess().GetRedlineTable().size() );
940 }
941
942 void lcl_RestoreRedlines(SwDoc& rDoc, SwNodeOffset const nInsPos, SaveRedlines_t& rArr)
943 {
946
947 for(SaveRedline & rSvRedLine : rArr)
948 {
949 rSvRedLine.SetPos( nInsPos );
951 rDoc.getIDocumentRedlineAccess().AppendRedline( rSvRedLine.pRedl, true ));
953 rSvRedLine.pRedl->GetType() == RedlineType::Delete )
954 {
955 UpdateFramesForAddDeleteRedline(rDoc, *rSvRedLine.pRedl);
956 }
957 }
958
960 }
961
962 bool lcl_SaveFootnote( const SwNode& rSttNd, const SwNode& rEndNd,
963 const SwNode& rInsPos,
964 SwFootnoteIdxs& rFootnoteArr, SwFootnoteIdxs& rSaveArr,
965 std::optional<sal_Int32> oSttCnt = std::nullopt, std::optional<sal_Int32> oEndCnt = std::nullopt )
966 {
967 bool bUpdateFootnote = false;
968 const SwNodes& rNds = rInsPos.GetNodes();
969 const bool bDelFootnote = rInsPos.GetIndex() < rNds.GetEndOfAutotext().GetIndex() &&
970 rSttNd.GetIndex() >= rNds.GetEndOfAutotext().GetIndex();
971 const bool bSaveFootnote = !bDelFootnote &&
972 rInsPos.GetIndex() >= rNds.GetEndOfExtras().GetIndex();
973 if( !rFootnoteArr.empty() )
974 {
975
976 size_t nPos = 0;
977 rFootnoteArr.SeekEntry( rSttNd, &nPos );
978 SwTextFootnote* pSrch;
979 const SwNode* pFootnoteNd;
980
981 // Delete/save all that come after it
982 while( nPos < rFootnoteArr.size() && ( pFootnoteNd =
983 &( pSrch = rFootnoteArr[ nPos ] )->GetTextNode())->GetIndex()
984 <= rEndNd.GetIndex() )
985 {
986 const sal_Int32 nFootnoteSttIdx = pSrch->GetStart();
987 if( ( oEndCnt && oSttCnt )
988 ? (( &rSttNd == pFootnoteNd &&
989 *oSttCnt > nFootnoteSttIdx) ||
990 ( &rEndNd == pFootnoteNd &&
991 nFootnoteSttIdx >= *oEndCnt ))
992 : ( &rEndNd == pFootnoteNd ))
993 {
994 ++nPos; // continue searching
995 }
996 else
997 {
998 // delete it
999 if( bDelFootnote )
1000 {
1001 SwTextNode& rTextNd = const_cast<SwTextNode&>(pSrch->GetTextNode());
1002 SwContentIndex aIdx( &rTextNd, nFootnoteSttIdx );
1003 rTextNd.EraseText( aIdx, 1 );
1004 }
1005 else
1006 {
1007 pSrch->DelFrames(nullptr);
1008 rFootnoteArr.erase( rFootnoteArr.begin() + nPos );
1009 if( bSaveFootnote )
1010 rSaveArr.insert( pSrch );
1011 }
1012 bUpdateFootnote = true;
1013 }
1014 }
1015
1016 while( nPos-- && ( pFootnoteNd = &( pSrch = rFootnoteArr[ nPos ] )->
1017 GetTextNode())->GetIndex() >= rSttNd.GetIndex() )
1018 {
1019 const sal_Int32 nFootnoteSttIdx = pSrch->GetStart();
1020 if( !oEndCnt || !oSttCnt ||
1021 ! (( &rSttNd == pFootnoteNd &&
1022 *oSttCnt > nFootnoteSttIdx ) ||
1023 ( &rEndNd == pFootnoteNd &&
1024 nFootnoteSttIdx >= *oEndCnt )) )
1025 {
1026 if( bDelFootnote )
1027 {
1028 // delete it
1029 SwTextNode& rTextNd = const_cast<SwTextNode&>(pSrch->GetTextNode());
1030 SwContentIndex aIdx( &rTextNd, nFootnoteSttIdx );
1031 rTextNd.EraseText( aIdx, 1 );
1032 }
1033 else
1034 {
1035 pSrch->DelFrames(nullptr);
1036 rFootnoteArr.erase( rFootnoteArr.begin() + nPos );
1037 if( bSaveFootnote )
1038 rSaveArr.insert( pSrch );
1039 }
1040 bUpdateFootnote = true;
1041 }
1042 }
1043 }
1044 // When moving from redline section into document content section, e.g.
1045 // after loading a document with (delete-)redlines, the footnote array
1046 // has to be adjusted... (#i70572)
1047 if( bSaveFootnote )
1048 {
1049 SwNodeIndex aIdx( rSttNd );
1050 while( aIdx < rEndNd ) // Check the moved section
1051 {
1052 SwNode* pNode = &aIdx.GetNode();
1053 if( pNode->IsTextNode() ) // Looking for text nodes...
1054 {
1055 SwpHints *pHints = pNode->GetTextNode()->GetpSwpHints();
1056 if( pHints && pHints->HasFootnote() ) //...with footnotes
1057 {
1058 bUpdateFootnote = true; // Heureka
1059 const size_t nCount = pHints->Count();
1060 for( size_t i = 0; i < nCount; ++i )
1061 {
1062 SwTextAttr *pAttr = pHints->Get( i );
1063 if ( pAttr->Which() == RES_TXTATR_FTN )
1064 {
1065 rSaveArr.insert( static_cast<SwTextFootnote*>(pAttr) );
1066 }
1067 }
1068 }
1069 }
1070 ++aIdx;
1071 }
1072 }
1073 return bUpdateFootnote;
1074 }
1075
1076 bool lcl_MayOverwrite( const SwTextNode *pNode, const sal_Int32 nPos )
1077 {
1078 sal_Unicode const cChr = pNode->GetText()[nPos];
1079 switch (cChr)
1080 {
1082 case CH_TXTATR_INWORD:
1083 return !pNode->GetTextAttrForCharAt(nPos);// how could there be none?
1090 return false;
1091 default:
1092 return true;
1093 }
1094 }
1095
1096 void lcl_SkipAttr( const SwTextNode *pNode, SwPosition &rIdx, sal_Int32 &rStart )
1097 {
1098 if( !lcl_MayOverwrite( pNode, rStart ) )
1099 {
1100 // skip all special attributes
1101 do {
1102 rIdx.AdjustContent(+1);
1103 rStart = rIdx.GetContentIndex();
1104 } while (rStart < pNode->GetText().getLength()
1105 && !lcl_MayOverwrite(pNode, rStart) );
1106 }
1107 }
1108
1109 bool lcl_GetTokenToParaBreak( OUString& rStr, OUString& rRet, bool bRegExpRplc )
1110 {
1111 if( bRegExpRplc )
1112 {
1113 sal_Int32 nPos = 0;
1114 static const OUStringLiteral sPara(u"\\n");
1115 for (;;)
1116 {
1117 nPos = rStr.indexOf( sPara, nPos );
1118 if (nPos<0)
1119 {
1120 break;
1121 }
1122 // Has this been escaped?
1123 if( nPos && '\\' == rStr[nPos-1])
1124 {
1125 ++nPos;
1126 if( nPos >= rStr.getLength() )
1127 {
1128 break;
1129 }
1130 }
1131 else
1132 {
1133 rRet = rStr.copy( 0, nPos );
1134 rStr = rStr.copy( nPos + sPara.getLength() );
1135 return true;
1136 }
1137 }
1138 }
1139 rRet = rStr;
1140 rStr.clear();
1141 return false;
1142 }
1143}
1144
1145namespace //local functions originally from docfmt.cxx
1146{
1147
1148 bool lcl_ApplyOtherSet(
1149 SwContentNode & rNode,
1150 SwHistory *const pHistory,
1151 SfxItemSet const& rOtherSet,
1152 SfxItemSet const& rFirstSet,
1153 SfxItemSet const& rPropsSet,
1154 SwRootFrame const*const pLayout,
1155 SwNodeIndex *const o_pIndex = nullptr)
1156 {
1157 assert(rOtherSet.Count());
1158
1159 bool ret(false);
1160 SwTextNode *const pTNd = rNode.GetTextNode();
1161 sw::MergedPara const* pMerged(nullptr);
1162 if (pLayout && pLayout->HasMergedParas() && pTNd)
1163 {
1164 SwTextFrame const*const pTextFrame(static_cast<SwTextFrame const*>(
1165 pTNd->getLayoutFrame(pLayout)));
1166 if (pTextFrame)
1167 {
1168 pMerged = pTextFrame->GetMergedPara();
1169 }
1170 if (pMerged)
1171 {
1172 if (rFirstSet.Count())
1173 {
1174 if (pHistory)
1175 {
1176 SwRegHistory aRegH(pMerged->pFirstNode, *pMerged->pFirstNode, pHistory);
1177 ret = pMerged->pFirstNode->SetAttr(rFirstSet);
1178 }
1179 else
1180 {
1181 ret = pMerged->pFirstNode->SetAttr(rFirstSet);
1182 }
1183 }
1184 if (rPropsSet.Count())
1185 {
1186 if (pHistory)
1187 {
1188 SwRegHistory aRegH(pMerged->pParaPropsNode, *pMerged->pParaPropsNode, pHistory);
1189 ret = pMerged->pParaPropsNode->SetAttr(rPropsSet) || ret;
1190 }
1191 else
1192 {
1193 ret = pMerged->pParaPropsNode->SetAttr(rPropsSet) || ret;
1194 }
1195 }
1196 if (o_pIndex)
1197 {
1198 *o_pIndex = *pMerged->pLastNode; // skip hidden
1199 }
1200 }
1201 }
1202
1203 // input cursor can't be on hidden node, and iteration skips them
1204 assert(!pLayout || !pLayout->HasMergedParas()
1206
1207 if (!pMerged)
1208 {
1209 if (pHistory)
1210 {
1211 SwRegHistory aRegH(&rNode, rNode, pHistory);
1212 ret = rNode.SetAttr( rOtherSet );
1213 }
1214 else
1215 {
1216 ret = rNode.SetAttr( rOtherSet );
1217 }
1218 }
1219 return ret;
1220 }
1221
1222 #define DELETECHARSETS if ( bDelete ) { delete pCharSet; delete pOtherSet; }
1223
1224 // set format redline with extra data for lcl_InsAttr()
1225 void lcl_SetRedline(
1226 SwDoc& rDoc,
1227 const SwPaM &rRg)
1228 {
1229 std::unique_ptr<SwRedlineExtraData_FormatColl> xExtra;
1230
1231 // check existing redline on the same range, and use its extra data, if it exists
1233 rRg.Start()->GetNode(), RedlineType::Format );
1234 if( SwRedlineTable::npos != nRedlPos )
1235 {
1236 const SwPosition *pRStt, *pREnd;
1237 do {
1238 SwRangeRedline* pTmp = rDoc.getIDocumentRedlineAccess().GetRedlineTable()[ nRedlPos ];
1239 pRStt = pTmp->Start();
1240 pREnd = pTmp->End();
1241 SwComparePosition eCompare = ComparePosition( *rRg.Start(), *rRg.End(), *pRStt, *pREnd );
1242 if ( eCompare == SwComparePosition::Inside || eCompare == SwComparePosition::Equal )
1243 {
1244 if (pTmp->GetExtraData())
1245 {
1246 const SwRedlineExtraData* pExtraData = pTmp->GetExtraData();
1247 const SwRedlineExtraData_FormatColl* pFormattingChanges =
1248 dynamic_cast<const SwRedlineExtraData_FormatColl*>(pExtraData);
1249 // Check if the extra data is of type 'formatting changes'
1250 if (pFormattingChanges)
1251 {
1252 // Get the item set that holds all the changes properties
1253 const SfxItemSet *pChangesSet = pFormattingChanges->GetItemSet();
1254 xExtra.reset(new SwRedlineExtraData_FormatColl("", USHRT_MAX, pChangesSet));
1255 break;
1256 }
1257 }
1258 }
1259 } while( pRStt <= rRg.Start() && ++nRedlPos < rDoc.getIDocumentRedlineAccess().GetRedlineTable().size());
1260 }
1261
1262 SwRangeRedline * pRedline = new SwRangeRedline( RedlineType::Format, rRg );
1263 auto const result(rDoc.getIDocumentRedlineAccess().AppendRedline( pRedline, true));
1264 // store original text attributes to reject formatting change
1266 return;
1267
1268 // no existing format redline in the range
1269 if (!xExtra)
1270 {
1271 // Apply the first character's attributes to the ReplaceText
1274 SwTextNode * pNode = rRg.Start()->GetNode().GetTextNode();
1275 pNode->GetParaAttr( aSet, rRg.Start()->GetContentIndex() + 1, rRg.End()->GetContentIndex() );
1276
1277 aSet.ClearItem( RES_TXTATR_REFMARK );
1278 aSet.ClearItem( RES_TXTATR_TOXMARK );
1279 aSet.ClearItem( RES_TXTATR_CJK_RUBY );
1280 aSet.ClearItem( RES_TXTATR_INETFMT );
1281 aSet.ClearItem( RES_TXTATR_META );
1282 aSet.ClearItem( RES_TXTATR_METAFIELD );
1283
1284 // After GetParaAttr aSet can contain INVALID_POOL_ITEM items, e.g. RES_TXTATR_CHARFMT
1285 // and (a copy of) this SfxItemSet can be passed to MSWordExportBase::OutputItemSet
1286 // which doesn't handle INVALID_POOL_ITEM items so clear InvalidItems here
1287 aSet.ClearInvalidItems();
1288
1289 xExtra.reset(new SwRedlineExtraData_FormatColl("", USHRT_MAX, &aSet));
1290 }
1291
1292 if (xExtra)
1293 {
1294 pRedline->SetExtraData(xExtra.get() );
1295 }
1296 }
1297
1298 // create format redline(s) for the given range:
1299 // to track the original formatting stored in the
1300 // hints, create redlines for all parts of the
1301 // range partitioned by boundaries of the hints.
1302 void lcl_SetRedlines(
1303 SwDoc& rDoc,
1304 const SwPaM &rRg)
1305 {
1306 SwNodeIndex aIdx( rRg.Start()->GetNode() );
1307 const SwNodeIndex aEndNd( rRg.End()->GetNode() );
1308 while( aIdx <= aEndNd )
1309 {
1310 SwTextNode *pNode = aIdx.GetNode().GetTextNode();
1311 if( pNode )
1312 {
1313 const sal_Int32 nStart = aIdx == rRg.Start()->GetNode()
1314 ? rRg.Start()->GetContentIndex()
1315 : 0;
1316 const sal_Int32 nEnd = aIdx < aEndNd
1317 ? pNode->GetText().getLength()
1318 : rRg.End()->GetContentIndex();
1319
1320 if( SwpHints *pHints = pNode->GetpSwpHints() )
1321 {
1322 const size_t nCount = pHints->Count();
1323 sal_Int32 nRedEnd = nStart;
1324 for( size_t i = 0; i < nCount; ++i )
1325 {
1326 SwTextAttr *pAttr = pHints->Get( i );
1327
1328 if ( pAttr->GetStart() > nEnd )
1329 {
1330 break; // after the range
1331 }
1332
1333 if ( !pAttr->GetEnd() || *pAttr->GetEnd() < nStart )
1334 {
1335 continue; // before the range
1336 }
1337
1338 // range part before the hint
1339 if ( nRedEnd < pAttr->GetStart() )
1340 {
1341 SwPaM aPam( *pNode, nRedEnd, *pNode, pAttr->GetStart() );
1342 lcl_SetRedline(rDoc, aPam);
1343 }
1344
1345 // range part at the hint
1346 sal_Int32 nRedStart = std::max(pAttr->GetStart(), nStart);
1347 nRedEnd = std::min(*pAttr->GetEnd(), nEnd);
1348 SwPaM aPam2( *pNode, nRedStart, *pNode, nRedEnd );
1349 lcl_SetRedline(rDoc, aPam2);
1350 }
1351
1352 // range part after the last hint
1353 if ( nRedEnd < nEnd )
1354 {
1355 SwPaM aPam( *pNode, nRedEnd, *pNode, nEnd );
1356 lcl_SetRedline(rDoc, aPam);
1357 }
1358 }
1359 else
1360 {
1361 SwPaM aPam( *pNode, nStart, *pNode, nEnd );
1362 lcl_SetRedline(rDoc, aPam);
1363 }
1364 }
1365 ++aIdx;
1366 }
1367 }
1368
1370 // Is used in SwDoc::Insert(..., SwFormatHint &rHt)
1371
1372 bool lcl_InsAttr(
1373 SwDoc& rDoc,
1374 const SwPaM &rRg,
1375 const SfxItemSet& rChgSet,
1376 const SetAttrMode nFlags,
1377 SwUndoAttr *const pUndo,
1378 SwRootFrame const*const pLayout,
1379 SwTextAttr **ppNewTextAttr)
1380 {
1381 // Divide the Sets (for selections in Nodes)
1382 const SfxItemSet* pCharSet = nullptr;
1383 const SfxItemSet* pOtherSet = nullptr;
1384 bool bDelete = false;
1385 bool bCharAttr = false;
1386 bool bOtherAttr = false;
1387
1388 // Check, if we can work with rChgSet or if we have to create additional SfxItemSets
1389 if ( 1 == rChgSet.Count() )
1390 {
1391 SfxItemIter aIter( rChgSet );
1392 const SfxPoolItem* pItem = aIter.GetCurItem();
1393 if (pItem && !IsInvalidItem(pItem))
1394 {
1395 const sal_uInt16 nWhich = pItem->Which();
1396
1397 if ( isCHRATR(nWhich) ||
1398 (RES_TXTATR_CHARFMT == nWhich) ||
1399 (RES_TXTATR_INETFMT == nWhich) ||
1400 (RES_TXTATR_AUTOFMT == nWhich) ||
1401 (RES_TXTATR_UNKNOWN_CONTAINER == nWhich) )
1402 {
1403 pCharSet = &rChgSet;
1404 bCharAttr = true;
1405 }
1406
1407 if ( isPARATR(nWhich)
1408 || isPARATR_LIST(nWhich)
1409 || isFRMATR(nWhich)
1410 || isGRFATR(nWhich)
1411 || isUNKNOWNATR(nWhich)
1412 || isDrawingLayerAttribute(nWhich) )
1413 {
1414 pOtherSet = &rChgSet;
1415 bOtherAttr = true;
1416 }
1417 }
1418 }
1419
1420 // Build new itemset if either
1421 // - rChgSet.Count() > 1 or
1422 // - The attribute in rChgSet does not belong to one of the above categories
1423 if ( !bCharAttr && !bOtherAttr )
1424 {
1425 SfxItemSet* pTmpCharItemSet = new SfxItemSetFixed<
1430
1431 SfxItemSet* pTmpOtherItemSet = new SfxItemSetFixed<
1434 // FillAttribute support:
1436
1437 pTmpCharItemSet->Put( rChgSet );
1438 pTmpOtherItemSet->Put( rChgSet );
1439
1440 pCharSet = pTmpCharItemSet;
1441 pOtherSet = pTmpOtherItemSet;
1442
1443 bDelete = true;
1444 }
1445
1446 SwHistory* pHistory = pUndo ? &pUndo->GetHistory() : nullptr;
1447 bool bRet = false;
1448 const SwPosition *pStt = rRg.Start(), *pEnd = rRg.End();
1449 SwContentNode* pNode = pStt->GetNode().GetContentNode();
1450
1451 if( pNode && pNode->IsTextNode() )
1452 {
1453 // tdf#127606 at editing, remove different formatting of DOCX-like numbering symbol
1454 if (pLayout && pNode->GetTextNode()->getIDocumentSettingAccess()->
1456 {
1457 SwContentNode* pEndNode = pEnd->GetNode().GetContentNode();
1458 SwContentNode* pCurrentNode = pEndNode;
1459 auto nStartIndex = pNode->GetIndex();
1460 auto nEndIndex = pEndNode->GetIndex();
1461 SwNodeIndex aIdx( pEnd->GetNode() );
1462 while ( pCurrentNode != nullptr && nStartIndex <= pCurrentNode->GetIndex() )
1463 {
1464 if (pCurrentNode->GetSwAttrSet().HasItem(RES_PARATR_LIST_AUTOFMT) &&
1465 // remove character formatting only on wholly selected paragraphs
1466 (nStartIndex < pCurrentNode->GetIndex() || pStt->GetContentIndex() == 0) &&
1467 (pCurrentNode->GetIndex() < nEndIndex || pEnd->GetContentIndex() == pEndNode->Len()))
1468 {
1469 pCurrentNode->ResetAttr(RES_PARATR_LIST_AUTOFMT);
1470 // reset also paragraph marker
1471 pCurrentNode->GetTextNode()->RstTextAttr(pCurrentNode->Len(), 1);
1472 }
1473 pCurrentNode = SwNodes::GoPrevious( &aIdx );
1474 }
1475 }
1476 // #i27615#
1477 if (rRg.IsInFrontOfLabel())
1478 {
1479 SwTextNode * pTextNd = pNode->GetTextNode();
1480 if (pLayout)
1481 {
1482 pTextNd = sw::GetParaPropsNode(*pLayout, *pTextNd);
1483 }
1484 SwNumRule * pNumRule = pTextNd->GetNumRule();
1485
1486 if ( !pNumRule )
1487 {
1488 OSL_FAIL( "<InsAttr(..)> - PaM in front of label, but text node has no numbering rule set. This is a serious defect." );
1490 return false;
1491 }
1492
1493 int nLevel = pTextNd->GetActualListLevel();
1494
1495 if (nLevel < 0)
1496 nLevel = 0;
1497
1498 if (nLevel >= MAXLEVEL)
1499 nLevel = MAXLEVEL - 1;
1500
1501 SwNumFormat aNumFormat = pNumRule->Get(o3tl::narrowing<sal_uInt16>(nLevel));
1502 SwCharFormat * pCharFormat =
1503 rDoc.FindCharFormatByName(aNumFormat.GetCharFormatName());
1504
1505 if (pCharFormat)
1506 {
1507 if (pHistory)
1508 pHistory->Add(pCharFormat->GetAttrSet(), *pCharFormat);
1509
1510 if ( pCharSet )
1511 pCharFormat->SetFormatAttr(*pCharSet);
1512 }
1513
1515 return true;
1516 }
1517
1518 // Attributes without an end do not have a range
1519 if ( !bCharAttr && !bOtherAttr )
1520 {
1522 aTextSet( rDoc.GetAttrPool() );
1523 aTextSet.Put( rChgSet );
1524 if( aTextSet.Count() )
1525 {
1526 SwRegHistory history( pNode, *pNode, pHistory );
1527 bRet = history.InsertItems(
1528 aTextSet, pStt->GetContentIndex(), pStt->GetContentIndex(), nFlags, /*ppNewTextAttr*/nullptr ) || bRet;
1529
1532 {
1533 SwPaM aPam( pStt->GetNode(), pStt->GetContentIndex()-1,
1534 pStt->GetNode(), pStt->GetContentIndex() );
1535
1536 if( pUndo )
1537 pUndo->SaveRedlineData( aPam, true );
1538
1540 rDoc.getIDocumentRedlineAccess().AppendRedline( new SwRangeRedline( RedlineType::Insert, aPam ), true);
1541 else
1543 }
1544 }
1545 }
1546
1547 // TextAttributes with an end never expand their range
1548 if ( !bCharAttr && !bOtherAttr )
1549 {
1550 // CharFormat and URL attributes are treated separately!
1551 // TEST_TEMP ToDo: AutoFormat!
1556 aTextSet(rDoc.GetAttrPool());
1557
1558 aTextSet.Put( rChgSet );
1559 if( aTextSet.Count() )
1560 {
1561 const sal_Int32 nInsCnt = pStt->GetContentIndex();
1562 const sal_Int32 nEnd = pStt->GetNode() == pEnd->GetNode()
1563 ? pEnd->GetContentIndex()
1564 : pNode->Len();
1565 SwRegHistory history( pNode, *pNode, pHistory );
1566 bRet = history.InsertItems( aTextSet, nInsCnt, nEnd, nFlags, ppNewTextAttr )
1567 || bRet;
1568
1571 {
1572 // Was text content inserted? (RefMark/TOXMarks without an end)
1573 bool bTextIns = nInsCnt != pStt->GetContentIndex();
1574 // Was content inserted or set over the selection?
1575 SwPaM aPam( pStt->GetNode(), bTextIns ? nInsCnt + 1 : nEnd,
1576 pStt->GetNode(), nInsCnt );
1577 if( pUndo )
1578 pUndo->SaveRedlineData( aPam, bTextIns );
1579
1582 new SwRangeRedline(
1583 bTextIns ? RedlineType::Insert : RedlineType::Format, aPam ),
1584 true);
1585 else if( bTextIns )
1587 }
1588 }
1589 }
1590 }
1591
1592 // We always have to set the auto flag for PageDescs that are set at the Node!
1593 if( pOtherSet && pOtherSet->Count() )
1594 {
1595 SwTableNode* pTableNd;
1596 const SwFormatPageDesc* pDesc = pOtherSet->GetItemIfSet( RES_PAGEDESC, false );
1597 if( pDesc )
1598 {
1599 if( pNode )
1600 {
1601 // Set auto flag. Only in the template it's without auto!
1602 SwFormatPageDesc aNew( *pDesc );
1603
1604 // Tables now also know line breaks
1605 if( !(nFlags & SetAttrMode::APICALL) &&
1606 nullptr != ( pTableNd = pNode->FindTableNode() ) )
1607 {
1608 SwTableNode* pCurTableNd = pTableNd;
1609 while ( nullptr != ( pCurTableNd = pCurTableNd->StartOfSectionNode()->FindTableNode() ) )
1610 pTableNd = pCurTableNd;
1611
1612 // set the table format
1613 SwFrameFormat* pFormat = pTableNd->GetTable().GetFrameFormat();
1614 SwRegHistory aRegH( pFormat, *pTableNd, pHistory );
1615 pFormat->SetFormatAttr( aNew );
1616 bRet = true;
1617 }
1618 else
1619 {
1620 SwContentNode * pFirstNode(pNode);
1621 if (pLayout && pLayout->HasMergedParas())
1622 {
1623 pFirstNode = sw::GetFirstAndLastNode(*pLayout, pStt->GetNode()).first;
1624 }
1625 SwRegHistory aRegH( pFirstNode, *pFirstNode, pHistory );
1626 bRet = pFirstNode->SetAttr( aNew ) || bRet;
1627 }
1628 }
1629
1630 // bOtherAttr = true means that pOtherSet == rChgSet. In this case
1631 // we know, that there is only one attribute in pOtherSet. We cannot
1632 // perform the following operations, instead we return:
1633 if ( bOtherAttr )
1634 return bRet;
1635
1636 const_cast<SfxItemSet*>(pOtherSet)->ClearItem( RES_PAGEDESC );
1637 if( !pOtherSet->Count() )
1638 {
1640 return bRet;
1641 }
1642 }
1643
1644 // Tables now also know line breaks
1645 const SvxFormatBreakItem* pBreak;
1646 if( pNode && !(nFlags & SetAttrMode::APICALL) &&
1647 nullptr != (pTableNd = pNode->FindTableNode() ) &&
1648 (pBreak = pOtherSet->GetItemIfSet( RES_BREAK, false )) )
1649 {
1650 SwTableNode* pCurTableNd = pTableNd;
1651 while ( nullptr != ( pCurTableNd = pCurTableNd->StartOfSectionNode()->FindTableNode() ) )
1652 pTableNd = pCurTableNd;
1653
1654 // set the table format
1655 SwFrameFormat* pFormat = pTableNd->GetTable().GetFrameFormat();
1656 SwRegHistory aRegH( pFormat, *pTableNd, pHistory );
1657 pFormat->SetFormatAttr( *pBreak );
1658 bRet = true;
1659
1660 // bOtherAttr = true means that pOtherSet == rChgSet. In this case
1661 // we know, that there is only one attribute in pOtherSet. We cannot
1662 // perform the following operations, instead we return:
1663 if ( bOtherAttr )
1664 return bRet;
1665
1666 const_cast<SfxItemSet*>(pOtherSet)->ClearItem( RES_BREAK );
1667 if( !pOtherSet->Count() )
1668 {
1670 return bRet;
1671 }
1672 }
1673
1674 {
1675 // If we have a PoolNumRule, create it if needed
1676 sal_uInt16 nPoolId=0;
1677 const SwNumRuleItem* pRule = pOtherSet->GetItemIfSet( RES_PARATR_NUMRULE, false );
1678 if( pRule &&
1679 !rDoc.FindNumRulePtr( pRule->GetValue() ) &&
1680 USHRT_MAX != (nPoolId = SwStyleNameMapper::GetPoolIdFromUIName ( pRule->GetValue(),
1683 }
1684 }
1685
1687 if (pOtherSet && pOtherSet->Count())
1688 { // actually only RES_BREAK is possible here...
1689 firstSet.Put(*pOtherSet);
1690 }
1694 XATTR_FILL_FIRST, XATTR_FILL_LAST+1> propsSet(rDoc.GetAttrPool());
1695 if (pOtherSet && pOtherSet->Count())
1696 {
1697 propsSet.Put(*pOtherSet);
1698 }
1699
1700 if( !rRg.HasMark() ) // no range
1701 {
1702 if( !pNode )
1703 {
1705 return bRet;
1706 }
1707
1708 if( pNode->IsTextNode() && pCharSet && pCharSet->Count() )
1709 {
1710 SwTextNode* pTextNd = pNode->GetTextNode();
1711 sal_Int32 nMkPos, nPtPos = pStt->GetContentIndex();
1712 const OUString& rStr = pTextNd->GetText();
1713
1714 // Special case: if the Cursor is located within a URL attribute, we take over it's area
1715 SwTextAttr const*const pURLAttr(
1717 if (pURLAttr && !pURLAttr->GetINetFormat().GetValue().isEmpty())
1718 {
1719 nMkPos = pURLAttr->GetStart();
1720 nPtPos = *pURLAttr->End();
1721 }
1722 else
1723 {
1724 assert(g_pBreakIt && g_pBreakIt->GetBreakIter().is());
1725 Boundary aBndry = g_pBreakIt->GetBreakIter()->getWordBoundary(
1726 pTextNd->GetText(), nPtPos,
1727 g_pBreakIt->GetLocale( pTextNd->GetLang( nPtPos ) ),
1728 WordType::ANY_WORD /*ANYWORD_IGNOREWHITESPACES*/,
1729 true);
1730
1731 if( aBndry.startPos < nPtPos && nPtPos < aBndry.endPos )
1732 {
1733 nMkPos = aBndry.startPos;
1734 nPtPos = aBndry.endPos;
1735 }
1736 else
1737 nPtPos = nMkPos = pStt->GetContentIndex();
1738 }
1739
1740 // Remove the overriding attributes from the SwpHintsArray,
1741 // if the selection spans across the whole paragraph.
1742 // These attributes are inserted as FormatAttributes and
1743 // never override the TextAttributes!
1744 if( !(nFlags & SetAttrMode::DONTREPLACE ) &&
1745 pTextNd->HasHints() && !nMkPos && nPtPos == rStr.getLength())
1746 {
1747 if( pHistory )
1748 {
1749 // Save all attributes for the Undo.
1750 SwRegHistory aRHst( *pTextNd, pHistory );
1751 pTextNd->GetpSwpHints()->Register( &aRHst );
1752 pTextNd->RstTextAttr( 0, nPtPos, 0, pCharSet );
1753 if( pTextNd->GetpSwpHints() )
1754 pTextNd->GetpSwpHints()->DeRegister();
1755 }
1756 else
1757 pTextNd->RstTextAttr( 0, nPtPos, 0, pCharSet );
1758 }
1759
1761 {
1762 SwPaM aPam( *pNode, nMkPos, *pNode, nPtPos );
1763
1764 if( pUndo )
1765 pUndo->SaveRedlineData( aPam, false );
1766
1767 lcl_SetRedlines(rDoc, aPam);
1768 }
1769
1770 // the SwRegHistory inserts the attribute into the TextNode!
1771 SwRegHistory history( pNode, *pNode, pHistory );
1772
1773 bRet = history.InsertItems( *pCharSet, nMkPos, nPtPos, nFlags, /*ppNewTextAttr*/nullptr )
1774 || bRet;
1775
1776 }
1777 if( pOtherSet && pOtherSet->Count() )
1778 {
1779 // Need to check for unique item for DrawingLayer items of type NameOrIndex
1780 // and evtl. correct that item to ensure unique names for that type. This call may
1781 // modify/correct entries inside of the given SfxItemSet
1782 SfxItemSet aTempLocalCopy(*pOtherSet);
1783
1784 rDoc.CheckForUniqueItemForLineFillNameOrIndex(aTempLocalCopy);
1785 bRet = lcl_ApplyOtherSet(*pNode, pHistory, aTempLocalCopy, firstSet, propsSet, pLayout) || bRet;
1786 }
1787
1789 return bRet;
1790 }
1791
1792 if( rDoc.getIDocumentRedlineAccess().IsRedlineOn() && pCharSet && pCharSet->Count() )
1793 {
1794 if( pUndo )
1795 pUndo->SaveRedlineData( rRg, false );
1796
1797 lcl_SetRedlines(rDoc, rRg);
1798 }
1799
1800 /* now if range */
1801 sal_uLong nNodes = 0;
1802
1803 SwNodeIndex aSt( rDoc.GetNodes() );
1804 SwNodeIndex aEnd( rDoc.GetNodes() );
1805 SwContentIndex aCntEnd( pEnd->GetContentNode(), pEnd->GetContentIndex() );
1806
1807 if( pNode )
1808 {
1809 const sal_Int32 nLen = pNode->Len();
1810 if( pStt->GetNode() != pEnd->GetNode() )
1811 aCntEnd.Assign( pNode, nLen );
1812
1813 if( pStt->GetContentIndex() != 0 || aCntEnd.GetIndex() != nLen )
1814 {
1815 // the SwRegHistory inserts the attribute into the TextNode!
1816 if( pNode->IsTextNode() && pCharSet && pCharSet->Count() )
1817 {
1818 SwRegHistory history( pNode, *pNode, pHistory );
1819 bRet = history.InsertItems(*pCharSet,
1820 pStt->GetContentIndex(), aCntEnd.GetIndex(), nFlags, /*ppNewTextAttr*/nullptr)
1821 || bRet;
1822 }
1823
1824 if( pOtherSet && pOtherSet->Count() )
1825 {
1826 bRet = lcl_ApplyOtherSet(*pNode, pHistory, *pOtherSet, firstSet, propsSet, pLayout) || bRet;
1827 }
1828
1829 // Only selection in a Node.
1830 if( pStt->GetNode() == pEnd->GetNode() )
1831 {
1833 return bRet;
1834 }
1835 ++nNodes;
1836 aSt.Assign( pStt->GetNode(), +1 );
1837 }
1838 else
1839 aSt = pStt->GetNode();
1840 aCntEnd.Assign(pEnd->GetContentNode(), pEnd->GetContentIndex()); // aEnd was changed!
1841 }
1842 else
1843 aSt.Assign( pStt->GetNode(), +1 );
1844
1845 // aSt points to the first full Node now
1846
1847 /*
1848 * The selection spans more than one Node.
1849 */
1850 if( pStt->GetNode() < pEnd->GetNode() )
1851 {
1852 pNode = pEnd->GetNode().GetContentNode();
1853 if(pNode)
1854 {
1855 if( aCntEnd.GetIndex() != pNode->Len() )
1856 {
1857 // the SwRegHistory inserts the attribute into the TextNode!
1858 if( pNode->IsTextNode() && pCharSet && pCharSet->Count() )
1859 {
1860 SwRegHistory history( pNode, *pNode, pHistory );
1861 (void)history.InsertItems(*pCharSet,
1862 0, aCntEnd.GetIndex(), nFlags, /*ppNewTextAttr*/nullptr);
1863 }
1864
1865 if( pOtherSet && pOtherSet->Count() )
1866 {
1867 lcl_ApplyOtherSet(*pNode, pHistory, *pOtherSet, firstSet, propsSet, pLayout);
1868 }
1869
1870 ++nNodes;
1871 aEnd = pEnd->GetNode();
1872 }
1873 else
1874 aEnd.Assign( pEnd->GetNode(), +1 );
1875 }
1876 else
1877 aEnd = pEnd->GetNode();
1878 }
1879 else
1880 aEnd.Assign( pEnd->GetNode(), +1 );
1881
1882 // aEnd points BEHIND the last full node now
1883
1884 /* Edit the fully selected Nodes. */
1885 // Reset all attributes from the set!
1886 if( pCharSet && pCharSet->Count() && !( SetAttrMode::DONTREPLACE & nFlags ) )
1887 {
1889 pStt, pEnd, pHistory, pCharSet, pLayout);
1891 }
1892
1893 bool bCreateSwpHints = pCharSet && (
1894 SfxItemState::SET == pCharSet->GetItemState( RES_TXTATR_CHARFMT, false ) ||
1895 SfxItemState::SET == pCharSet->GetItemState( RES_TXTATR_INETFMT, false ) );
1896
1897 for (SwNodeIndex current = aSt; current < aEnd; ++current)
1898 {
1899 SwTextNode *const pTNd = current.GetNode().GetTextNode();
1900 if (!pTNd)
1901 continue;
1902
1903 if (pLayout && pLayout->HasMergedParas()
1905 { // not really sure what to do here, but applying to hidden
1906 continue; // nodes doesn't make sense...
1907 }
1908
1909 if( pHistory )
1910 {
1911 SwRegHistory aRegH( pTNd, *pTNd, pHistory );
1912
1913 if (pCharSet && pCharSet->Count())
1914 {
1915 if (SwpHints *pSwpHints = bCreateSwpHints ? &pTNd->GetOrCreateSwpHints()
1916 : pTNd->GetpSwpHints())
1917 {
1918 pSwpHints->Register( &aRegH );
1919 }
1920
1921 pTNd->SetAttr(*pCharSet, 0, pTNd->GetText().getLength(), nFlags);
1922
1923 // re-fetch as it may be deleted by SetAttr
1924 if (SwpHints *pSwpHints = pTNd->GetpSwpHints())
1925 pSwpHints->DeRegister();
1926 }
1927 }
1928 else
1929 {
1930 if (pCharSet && pCharSet->Count())
1931 pTNd->SetAttr(*pCharSet, 0, pTNd->GetText().getLength(), nFlags);
1932 }
1933 ++nNodes;
1934 }
1935
1936 if (pOtherSet && pOtherSet->Count())
1937 {
1938 for (; aSt < aEnd; ++aSt)
1939 {
1940 pNode = aSt.GetNode().GetContentNode();
1941 if (!pNode)
1942 continue;
1943
1944 lcl_ApplyOtherSet(*pNode, pHistory, *pOtherSet, firstSet, propsSet, pLayout, &aSt);
1945 ++nNodes;
1946 }
1947 }
1948
1950 return (nNodes != 0) || bRet;
1951 }
1952}
1953
1954namespace sw
1955{
1956
1957namespace mark
1958{
1959 bool IsFieldmarkOverlap(SwPaM const& rPaM)
1960 {
1961 std::vector<std::pair<SwNodeOffset, sal_Int32>> Breaks;
1962 sw::CalcBreaks(Breaks, rPaM);
1963 return !Breaks.empty();
1964 }
1965}
1966
1968{
1969}
1970
1977static bool IsEmptyRange(const SwPosition& rStart, const SwPosition& rEnd,
1978 SwCopyFlags const flags)
1979{
1980 if (rStart == rEnd)
1981 { // check if a fly anchored there would be copied - then copy...
1982 return !IsDestroyFrameAnchoredAtChar(rStart, rStart, rEnd,
1983 (flags & SwCopyFlags::IsMoveToFly)
1986 }
1987 else
1988 {
1989 return rEnd < rStart;
1990 }
1991}
1992
1993// Copy an area into this document or into another document
1994bool
1996 SwCopyFlags const flags) const
1997{
1998 const SwPosition *pStt = rPam.Start(), *pEnd = rPam.End();
1999
2000 SwDoc& rDoc = rPos.GetNode().GetDoc();
2001 bool bColumnSel = rDoc.IsClipBoard() && rDoc.IsColumnSelection();
2002
2003 // Catch if there's no copy to do
2004 if (!rPam.HasMark() || (IsEmptyRange(*pStt, *pEnd, flags) && !bColumnSel))
2005 return false;
2006
2007 // Prevent copying into Flys that are anchored in the source range
2008 if (&rDoc == &m_rDoc && (flags & SwCopyFlags::CheckPosInFly))
2009 {
2010 // Correct the Start-/EndNode
2011 SwNodeOffset nStt = pStt->GetNodeIndex(),
2012 nEnd = pEnd->GetNodeIndex(),
2013 nDiff = nEnd - nStt +1;
2014 SwNode* pNd = m_rDoc.GetNodes()[ nStt ];
2015 if( pNd->IsContentNode() && pStt->GetContentIndex() )
2016 {
2017 ++nStt;
2018 --nDiff;
2019 }
2020 if( (pNd = m_rDoc.GetNodes()[ nEnd ])->IsContentNode() &&
2021 static_cast<SwContentNode*>(pNd)->Len() != pEnd->GetContentIndex() )
2022 {
2023 --nEnd;
2024 --nDiff;
2025 }
2026 if( nDiff &&
2027 lcl_ChkFlyFly( rDoc, nStt, nEnd, rPos.GetNodeIndex() ) )
2028 {
2029 return false;
2030 }
2031 }
2032
2033 SwPaM* pRedlineRange = nullptr;
2036 pRedlineRange = new SwPaM( rPos );
2037
2039
2040 bool bRet = false;
2041
2042 if( &rDoc != &m_rDoc )
2043 { // ordinary copy
2044 bRet = CopyImpl(rPam, rPos, flags & ~SwCopyFlags::CheckPosInFly, pRedlineRange);
2045 }
2046 else if( ! ( *pStt <= rPos && rPos < *pEnd &&
2047 ( pStt->GetNode() != pEnd->GetNode() ||
2048 !pStt->GetNode().IsTextNode() )) )
2049 {
2050 // Copy to a position outside of the area, or copy a single TextNode
2051 // Do an ordinary copy
2052 bRet = CopyImpl(rPam, rPos, flags & ~SwCopyFlags::CheckPosInFly, pRedlineRange);
2053 }
2054 else
2055 {
2056 // Copy the range in itself
2057 assert(!"mst: this is assumed to be dead code");
2058 }
2059
2061 if( pRedlineRange )
2062 {
2064 rDoc.getIDocumentRedlineAccess().AppendRedline( new SwRangeRedline( RedlineType::Insert, *pRedlineRange ), true);
2065 else
2066 rDoc.getIDocumentRedlineAccess().SplitRedline( *pRedlineRange );
2067 delete pRedlineRange;
2068 }
2069
2070 return bRet;
2071}
2072
2076{
2077 assert(pNode && "Didn't pass a Node.");
2078
2079 SwStartNode* pSttNd = pNode->IsStartNode() ? static_cast<SwStartNode*>(pNode)
2080 : pNode->StartOfSectionNode();
2081 SwNodeIndex aSttIdx( *pSttNd ), aEndIdx( *pNode->EndOfSectionNode() );
2082
2083 // delete all Flys, Bookmarks, ...
2084 DelFlyInRange( aSttIdx.GetNode(), aEndIdx.GetNode() );
2085 m_rDoc.getIDocumentRedlineAccess().DeleteRedline( *pSttNd, true, RedlineType::Any );
2086 DelBookmarks(aSttIdx.GetNode(), aEndIdx.GetNode());
2087
2088 {
2089 // move all Cursor/StackCursor/UnoCursor out of the to-be-deleted area
2090 SwNodeIndex aMvStt( aSttIdx, 1 );
2091 SwDoc::CorrAbs( aMvStt, aEndIdx, SwPosition( aSttIdx ), true );
2092 }
2093
2094 m_rDoc.GetNodes().DelNodes( aSttIdx, aEndIdx.GetIndex() - aSttIdx.GetIndex() + 1 );
2095}
2096
2098 SwPosition const& rPos, sal_Unicode const cDummy)
2099{
2100 SwPaM aPam(rPos, rPos);
2101 aPam.GetPoint()->AdjustContent(+1);
2102 assert(aPam.GetText().getLength() == 1 && aPam.GetText()[0] == cDummy);
2103 (void) cDummy;
2104
2106
2109 {
2111 }
2112}
2113
2115{
2117
2120 {
2122 }
2123}
2124
2126{
2127 const SwPosition &rStt = *rPam.Start(), &rEnd = *rPam.End();
2128 const SwNode* pNd = &rStt.GetNode();
2129 SwNodeOffset nSectDiff = pNd->StartOfSectionNode()->EndOfSectionIndex() -
2130 pNd->StartOfSectionIndex();
2131 SwNodeOffset nNodeDiff = rEnd.GetNodeIndex() - rStt.GetNodeIndex();
2132
2133 if ( nSectDiff-SwNodeOffset(2) <= nNodeDiff || m_rDoc.getIDocumentRedlineAccess().IsRedlineOn() ||
2134 /* #i9185# Prevent getting the node after the end node (see below) */
2135 rEnd.GetNodeIndex() + 1 == m_rDoc.GetNodes().Count() )
2136 {
2137 return false;
2138 }
2139
2140 {
2141 SwPaM temp(rPam, nullptr);
2142 if (!temp.HasMark())
2143 {
2144 temp.SetMark();
2145 }
2146 if (SwTextNode *const pNode = temp.Start()->GetNode().GetTextNode())
2147 { // rPam may not have nContent set but IsFieldmarkOverlap requires it
2148 temp.Start()->AssignStartIndex(*pNode);
2149 }
2150 if (SwTextNode *const pNode = temp.End()->GetNode().GetTextNode())
2151 {
2152 temp.End()->AssignEndIndex(*pNode);
2153 }
2155 { // a bit of a problem: we want to completely remove the nodes
2156 // but then how can the CH_TXT_ATR survive?
2157 return false;
2158 }
2159 }
2160
2161 // Move hard page breaks to the following Node.
2162 bool bSavePageBreak = false, bSavePageDesc = false;
2163
2164 /* #i9185# This would lead to a segmentation fault if not caught above. */
2165 SwNodeOffset nNextNd = rEnd.GetNodeIndex() + 1;
2166 SwTableNode *const pTableNd = m_rDoc.GetNodes()[ nNextNd ]->GetTableNode();
2167
2168 if( pTableNd && pNd->IsContentNode() )
2169 {
2170 SwFrameFormat* pTableFormat = pTableNd->GetTable().GetFrameFormat();
2171
2172 {
2173 const SfxPoolItem *pItem;
2174 const SfxItemSet* pSet = static_cast<const SwContentNode*>(pNd)->GetpSwAttrSet();
2175 if( pSet && SfxItemState::SET == pSet->GetItemState( RES_PAGEDESC,
2176 false, &pItem ) )
2177 {
2178 pTableFormat->SetFormatAttr( *pItem );
2179 bSavePageDesc = true;
2180 }
2181
2182 if( pSet && SfxItemState::SET == pSet->GetItemState( RES_BREAK,
2183 false, &pItem ) )
2184 {
2185 pTableFormat->SetFormatAttr( *pItem );
2186 bSavePageBreak = true;
2187 }
2188 }
2189 }
2190
2191 bool const bDoesUndo = m_rDoc.GetIDocumentUndoRedo().DoesUndo();
2192 if( bDoesUndo )
2193 {
2194 if( !rPam.HasMark() )
2195 rPam.SetMark();
2196 else if( rPam.GetPoint() == &rStt )
2197 rPam.Exchange();
2198 rPam.GetPoint()->Adjust(SwNodeOffset(1));
2199
2200 SwContentNode *pTmpNode = rPam.GetPoint()->GetNode().GetContentNode();
2201 bool bGoNext = (nullptr == pTmpNode);
2202
2203 if (rPam.GetMark()->GetContentNode())
2204 rPam.GetMark()->SetContent( 0 );
2205
2207
2208 SwPaM aDelPam( *rPam.GetMark(), *rPam.GetPoint() );
2209 {
2210 SwPosition aTmpPos( *aDelPam.GetPoint() );
2211 if( bGoNext )
2212 {
2213 m_rDoc.GetNodes().GoNext( &aTmpPos );
2214 }
2215 ::PaMCorrAbs( aDelPam, aTmpPos );
2216 }
2217
2218 std::unique_ptr<SwUndoDelete> pUndo(new SwUndoDelete(aDelPam, SwDeleteFlags::Default, true));
2219
2220 *rPam.GetPoint() = *aDelPam.GetPoint();
2221 pUndo->SetPgBrkFlags( bSavePageBreak, bSavePageDesc );
2222 m_rDoc.GetIDocumentUndoRedo().AppendUndo(std::move(pUndo));
2223 rPam.DeleteMark();
2224 }
2225 else
2226 {
2227 SwNodeRange aRg( rStt.GetNode(), rEnd.GetNode() );
2228 rPam.Normalize(false);
2229
2230 // Try to move past the End
2231 if( !rPam.Move( fnMoveForward, GoInNode ) )
2232 {
2233 // Fair enough, at the Beginning then
2234 rPam.Exchange();
2235 if( !rPam.Move( fnMoveBackward, GoInNode ))
2236 {
2237 SAL_WARN("sw.core", "DelFullPara: no more Nodes");
2238 return false;
2239 }
2240 }
2241
2242 // must delete all fieldmarks before CorrAbs(), or they'll remain
2243 // moved to wrong node without their CH_TXT_ATR_FIELD*
2244 // (note: deleteMarks() doesn't help here, in case of partially
2245 // selected fieldmarks; let's delete these as re-inserting their chars
2246 // elsewhere looks difficult)
2247 ::std::set<::sw::mark::IFieldmark*> fieldmarks;
2248 for (SwNodeIndex i = aRg.aStart; i <= aRg.aEnd; ++i)
2249 {
2250 if (SwTextNode *const pTextNode = i.GetNode().GetTextNode())
2251 {
2252 for (sal_Int32 j = 0; j < pTextNode->GetText().getLength(); ++j)
2253 {
2254 switch (pTextNode->GetText()[j])
2255 {
2258 fieldmarks.insert(m_rDoc.getIDocumentMarkAccess()->getFieldmarkAt(SwPosition(*pTextNode, j)));
2259 break;
2261 fieldmarks.insert(m_rDoc.getIDocumentMarkAccess()->getFieldmarkFor(SwPosition(*pTextNode, j)));
2262 break;
2263 }
2264 }
2265 }
2266 }
2267 for (auto const pFieldMark : fieldmarks)
2268 {
2270 }
2271
2272 // move bookmarks, redlines etc.
2273 if (aRg.aStart == aRg.aEnd) // only first CorrAbs variant handles this
2274 {
2275 m_rDoc.CorrAbs( aRg.aStart.GetNode(), *rPam.GetPoint(), 0, true );
2276 }
2277 else
2278 {
2279 SwDoc::CorrAbs( aRg.aStart, aRg.aEnd, *rPam.GetPoint(), true );
2280 }
2281
2282 // What's with Flys?
2283 {
2284 // If there are FlyFrames left, delete these too
2285 for( size_t n = 0; n < m_rDoc.GetSpzFrameFormats()->size(); ++n )
2286 {
2288 const SwFormatAnchor* pAnchor = &pFly->GetAnchor();
2289 SwPosition const*const pAPos = pAnchor->GetContentAnchor();
2290 if (pAPos &&
2291 ((RndStdIds::FLY_AT_PARA == pAnchor->GetAnchorId()) ||
2292 (RndStdIds::FLY_AT_CHAR == pAnchor->GetAnchorId())) &&
2293 // note: here use <= not < like in
2294 // IsDestroyFrameAnchoredAtChar() because of the increment
2295 // of rPam in the bDoesUndo path above!
2296 aRg.aStart <= pAPos->GetNode() && pAPos->GetNode() <= aRg.aEnd.GetNode() )
2297 {
2299 --n;
2300 }
2301 }
2302 }
2303
2304 rPam.DeleteMark();
2305 m_rDoc.GetNodes().Delete( aRg.aStart, nNodeDiff+1 );
2306 }
2308
2309 return true;
2310}
2311
2313{
2314 if ( lcl_StrLenOverflow( rPam ) )
2315 return false;
2316
2317 bool const ret = lcl_DoWithBreaks( *this, rPam, flags, (m_rDoc.getIDocumentRedlineAccess().IsRedlineOn())
2320
2321 return ret;
2322}
2323
2324// It seems that this is mostly used by SwDoc internals; the only
2325// way to call this from the outside seems to be the special case in
2326// SwDoc::CopyRange (but I have not managed to actually hit that case).
2328{
2329 // nothing moved: return
2330 const SwPosition *pStt = rPaM.Start(), *pEnd = rPaM.End();
2331 if( !rPaM.HasMark() || *pStt >= *pEnd || (*pStt <= rPos && rPos < *pEnd))
2332 return false;
2333
2334 assert(!sw::mark::IsFieldmarkOverlap(rPaM)); // probably an invalid redline was created?
2335
2336 // Save the paragraph anchored Flys, so that they can be moved.
2337 SaveFlyArr aSaveFlyArr;
2338 SaveFlyInRange( rPaM, rPos, aSaveFlyArr, bool( SwMoveFlags::ALLFLYS & eMvFlags ) );
2339
2340 // save redlines (if DOC_MOVEREDLINES is used)
2341 SaveRedlines_t aSaveRedl;
2343 {
2344 lcl_SaveRedlines( rPaM, aSaveRedl );
2345
2346 // #i17764# unfortunately, code below relies on undos being
2347 // in a particular order, and presence of bookmarks
2348 // will change this order. Hence, we delete bookmarks
2349 // here without undo.
2350 ::sw::UndoGuard const undoGuard(m_rDoc.GetIDocumentUndoRedo());
2352 pStt->GetNode(),
2353 pEnd->GetNode(),
2354 nullptr,
2355 pStt->GetContentIndex(),
2356 pEnd->GetContentIndex());
2357 }
2358
2359 bool bUpdateFootnote = false;
2360 SwFootnoteIdxs aTmpFntIdx;
2361
2362 std::unique_ptr<SwUndoMove> pUndoMove;
2364 {
2366 pUndoMove.reset(new SwUndoMove( rPaM, rPos ));
2367 pUndoMove->SetMoveRedlines( eMvFlags == SwMoveFlags::REDLINES );
2368 }
2369 else
2370 {
2371 bUpdateFootnote = lcl_SaveFootnote( pStt->GetNode(), pEnd->GetNode(), rPos.GetNode(),
2372 m_rDoc.GetFootnoteIdxs(), aTmpFntIdx,
2373 pStt->GetContentIndex(), pEnd->GetContentIndex() );
2374 }
2375
2376 bool bSplit = false;
2377 SwPaM aSavePam( rPos, rPos );
2378
2379 // Move the SPoint to the beginning of the range
2380 if( rPaM.GetPoint() == pEnd )
2381 rPaM.Exchange();
2382
2383 // If there is a TextNode before and after the Move, create a JoinNext in the EditShell.
2384 SwTextNode* pSrcNd = rPaM.GetPoint()->GetNode().GetTextNode();
2385 bool bCorrSavePam = pSrcNd && pStt->GetNode() != pEnd->GetNode();
2386
2387 // If one or more TextNodes are moved, SwNodes::Move will do a SplitNode.
2388 // However, this does not update the cursor. So we create a TextNode to keep
2389 // updating the indices. After the Move the Node is optionally deleted.
2390 SwTextNode * pTNd = rPos.GetNode().GetTextNode();
2391 if( pTNd && rPaM.GetPoint()->GetNode() != rPaM.GetMark()->GetNode() &&
2392 ( rPos.GetContentIndex() || ( pTNd->Len() && bCorrSavePam )) )
2393 {
2394 bSplit = true;
2395 const sal_Int32 nMkContent = rPaM.GetMark()->GetContentIndex();
2396
2397 const std::shared_ptr<sw::mark::ContentIdxStore> pContentStore(sw::mark::ContentIdxStore::Create());
2398 pContentStore->Save( m_rDoc, rPos.GetNodeIndex(), rPos.GetContentIndex(), true );
2399
2400 SwTextNode * pOrigNode = pTNd;
2401 assert(*aSavePam.GetPoint() == *aSavePam.GetMark() &&
2402 *aSavePam.GetPoint() == rPos);
2403 assert(aSavePam.GetPoint()->GetContentNode() == pOrigNode);
2404 assert(aSavePam.GetPoint()->GetNode() == rPos.GetNode());
2405 assert(rPos.GetNodeIndex() == pOrigNode->GetIndex());
2406
2407 std::function<void (SwTextNode *, sw::mark::RestoreMode, bool)> restoreFunc(
2408 [&](SwTextNode *const, sw::mark::RestoreMode const eMode, bool)
2409 {
2410 if (!pContentStore->Empty())
2411 {
2412 pContentStore->Restore(m_rDoc, pOrigNode->GetIndex()-SwNodeOffset(1), 0, true, false, eMode);
2413 }
2414 });
2415 pTNd->SplitContentNode(rPos, &restoreFunc);
2416
2417 //A new node was inserted before the orig pTNd and the content up to
2418 //rPos moved into it. The old node is returned with the remainder
2419 //of the content in it.
2420 //
2421 //aSavePam was created with rPos, it continues to point to the
2422 //old node, but with the *original* content index into the node.
2423 //Seeing as all the orignode content before that index has
2424 //been removed, the new index into the original node should now be set
2425 //to 0 and the content index of rPos should also be adapted to the
2426 //truncated node
2427 assert(*aSavePam.GetPoint() == *aSavePam.GetMark() &&
2428 *aSavePam.GetPoint() == rPos);
2429 assert(aSavePam.GetPoint()->GetContentNode() == pOrigNode);
2430 assert(aSavePam.GetPoint()->GetNode() == rPos.GetNode());
2431 assert(rPos.GetNodeIndex() == pOrigNode->GetIndex());
2432 aSavePam.GetPoint()->SetContent(0);
2433 rPos = *aSavePam.GetMark() = *aSavePam.GetPoint();
2434
2435 // correct the PaM!
2436 if( rPos.GetNode() == rPaM.GetMark()->GetNode() )
2437 {
2438 rPaM.GetMark()->Assign( rPos.GetNodeIndex() - SwNodeOffset(1) );
2439 rPaM.GetMark()->SetContent( nMkContent );
2440 }
2441 }
2442
2443 // Put back the Pam by one "content"; so that it's always outside of
2444 // the manipulated range.
2445 // tdf#99692 don't Move() back if that would end up in another node
2446 // because moving backward is not necessarily the inverse of forward then.
2447 // (but do Move() back if we have split the node)
2448 const bool bNullContent = !bSplit && aSavePam.GetPoint()->GetContentIndex() == 0;
2449 if( bNullContent )
2450 {
2451 aSavePam.GetPoint()->Adjust(SwNodeOffset(-1));
2452 }
2453 else
2454 {
2455 bool const success(aSavePam.Move(fnMoveBackward, GoInContent));
2456 assert(success);
2457 (void) success;
2458 }
2459
2460 // Copy all Bookmarks that are within the Move range into an array,
2461 // that saves the position as an offset.
2462 std::vector< ::sw::mark::SaveBookmark> aSaveBkmks;
2464 pStt->GetNode(),
2465 pEnd->GetNode(),
2466 &aSaveBkmks,
2467 pStt->GetContentIndex(),
2468 pEnd->GetContentIndex());
2469
2470 // If there is no range anymore due to the above deletions (e.g. the
2471 // footnotes got deleted), it's still a valid Move!
2472 if( *rPaM.GetPoint() != *rPaM.GetMark() )
2473 {
2474 // now do the actual move
2475 m_rDoc.GetNodes().MoveRange( rPaM, rPos, m_rDoc.GetNodes() );
2476
2477 // after a MoveRange() the Mark is deleted
2478 if ( rPaM.HasMark() ) // => no Move occurred!
2479 {
2480 return false;
2481 }
2482 }
2483 else
2484 rPaM.DeleteMark();
2485
2486 OSL_ENSURE( *aSavePam.GetMark() == rPos ||
2487 ( aSavePam.GetMark()->GetNode().GetContentNode() == nullptr ),
2488 "PaM was not moved. Aren't there ContentNodes at the beginning/end?" );
2489 *aSavePam.GetMark() = rPos;
2490
2491 rPaM.SetMark(); // create a Sel. around the new range
2492 pTNd = aSavePam.GetPointNode().GetTextNode();
2494 bool bRemove = true;
2495 // Do two Nodes have to be joined at the SavePam?
2496 if (bSplit && pTNd)
2497 {
2498 if (pTNd->CanJoinNext())
2499 {
2500 // Always join next, because <pTNd> has to stay as it is.
2501 // A join previous from its next would more or less delete <pTNd>
2502 pTNd->JoinNext();
2503 bRemove = false;
2504 }
2505 }
2506 if (bNullContent)
2507 {
2508 aSavePam.GetPoint()->Adjust(SwNodeOffset(1));
2509 }
2510 else if (bRemove) // No move forward after joining with next paragraph
2511 {
2512 aSavePam.Move( fnMoveForward, GoInContent );
2513 }
2514
2515 // Insert the Bookmarks back into the Document.
2516 *rPaM.GetMark() = *aSavePam.Start();
2517 for(auto& rBkmk : aSaveBkmks)
2518 rBkmk.SetInDoc(
2519 &m_rDoc,
2520 rPaM.GetMark()->GetNode(),
2521 rPaM.GetMark()->GetContentIndex());
2522 *rPaM.GetPoint() = *aSavePam.End();
2523
2524 // Move the Flys to the new position.
2525 // note: rPos is at the end here; can't really tell flys that used to be
2526 // at the start of rPam from flys that used to be at the end of rPam
2527 // unfortunately, so some of them are going to end up with wrong anchor...
2528 RestFlyInRange( aSaveFlyArr, *rPaM.Start(), &rPos.GetNode() );
2529
2530 // restore redlines (if DOC_MOVEREDLINES is used)
2531 if( !aSaveRedl.empty() )
2532 {
2533 lcl_RestoreRedlines( m_rDoc, *aSavePam.Start(), aSaveRedl );
2534 }
2535
2536 if( bUpdateFootnote )
2537 {
2538 if( !aTmpFntIdx.empty() )
2539 {
2540 m_rDoc.GetFootnoteIdxs().insert( aTmpFntIdx );
2541 aTmpFntIdx.clear();
2542 }
2543
2545 }
2546
2548 return true;
2549}
2550
2552 SwMoveFlags eMvFlags )
2553{
2554 // Moves all Nodes to the new position.
2555 // Bookmarks are moved too (currently without Undo support).
2556
2557 // If footnotes are being moved to the special section, remove them now.
2558
2559 // Or else delete the Frames for all footnotes that are being moved
2560 // and have it rebuild after the Move (footnotes can change pages).
2561 // Additionally we have to correct the FootnoteIdx array's sorting.
2562 bool bUpdateFootnote = false;
2563 SwFootnoteIdxs aTmpFntIdx;
2564
2565 std::unique_ptr<SwUndoMove> pUndo;
2567 {
2568 pUndo.reset(new SwUndoMove( m_rDoc, rRange, rDestNd ));
2569 }
2570 else
2571 {
2572 bUpdateFootnote = lcl_SaveFootnote( rRange.aStart.GetNode(), rRange.aEnd.GetNode(), rDestNd,
2573 m_rDoc.GetFootnoteIdxs(), aTmpFntIdx );
2574 }
2575
2576 SaveRedlines_t aSaveRedl;
2577 std::vector<SwRangeRedline*> aSavRedlInsPosArr;
2579 {
2580 lcl_SaveRedlines( rRange, aSaveRedl );
2581
2582 // Find all RedLines that end at the InsPos.
2583 // These have to be moved back to the "old" position after the Move.
2584 SwRedlineTable::size_type nRedlPos = m_rDoc.getIDocumentRedlineAccess().GetRedlinePos( rDestNd, RedlineType::Any );
2585 if( SwRedlineTable::npos != nRedlPos )
2586 {
2587 const SwPosition *pRStt, *pREnd;
2588 do {
2590 pRStt = pTmp->Start();
2591 pREnd = pTmp->End();
2592 if( pREnd->GetNode() == rDestNd && pRStt->GetNode() < rDestNd )
2593 {
2594 aSavRedlInsPosArr.push_back( pTmp );
2595 }
2596 } while( pRStt->GetNode() < rDestNd && ++nRedlPos < m_rDoc.getIDocumentRedlineAccess().GetRedlineTable().size());
2597 }
2598 }
2599
2600 // Copy all Bookmarks that are within the Move range into an array
2601 // that stores all references to positions as an offset.
2602 // The final mapping happens after the Move.
2603 std::vector< ::sw::mark::SaveBookmark> aSaveBkmks;
2604 DelBookmarks(rRange.aStart.GetNode(), rRange.aEnd.GetNode(), &aSaveBkmks);
2605
2606 // Save the paragraph-bound Flys, so that they can be moved.
2607 SaveFlyArr aSaveFlyArr;
2608 if( !m_rDoc.GetSpzFrameFormats()->empty() )
2609 SaveFlyInRange( rRange, aSaveFlyArr );
2610
2611 // Set it to before the Position, so that it cannot be moved further.
2612 SwNodeIndex aIdx( rDestNd, -1 );
2613
2614 std::optional<SwNodeIndex> oSaveInsPos;
2615 if( pUndo )
2616 oSaveInsPos.emplace(rRange.aStart, -1 );
2617
2618 // move the Nodes
2619 bool bNoDelFrames = bool(SwMoveFlags::NO_DELFRMS & eMvFlags);
2620 if( m_rDoc.GetNodes().MoveNodes( rRange, m_rDoc.GetNodes(), rDestNd, !bNoDelFrames ) )
2621 {
2622 ++aIdx; // again back to old position
2623 if( oSaveInsPos )
2624 ++(*oSaveInsPos);
2625 }
2626 else
2627 {
2628 aIdx = rRange.aStart;
2629 pUndo.reset();
2630 }
2631
2632 // move the Flys to the new position
2633 if( !aSaveFlyArr.empty() )
2634 {
2635 SwPosition const tmp(aIdx);
2636 RestFlyInRange(aSaveFlyArr, tmp, nullptr);
2637 }
2638
2639 // Add the Bookmarks back to the Document
2640 for(auto& rBkmk : aSaveBkmks)
2641 rBkmk.SetInDoc(&m_rDoc, aIdx.GetNode());
2642
2643 if( !aSavRedlInsPosArr.empty() )
2644 {
2645 for(SwRangeRedline* pTmp : aSavRedlInsPosArr)
2646 {
2648 {
2649 SwPosition* pEnd = pTmp->End();
2650 pEnd->Assign(aIdx);
2651 }
2652 }
2653 }
2654
2655 if( !aSaveRedl.empty() )
2656 lcl_RestoreRedlines( m_rDoc, aIdx.GetIndex(), aSaveRedl );
2657
2658 if( pUndo )
2659 {
2660 pUndo->SetDestRange( aIdx.GetNode(), rDestNd, *oSaveInsPos );
2661 m_rDoc.GetIDocumentUndoRedo().AppendUndo(std::move(pUndo));
2662 }
2663
2664 oSaveInsPos.reset();
2665
2666 if( bUpdateFootnote )
2667 {
2668 if( !aTmpFntIdx.empty() )
2669 {
2670 m_rDoc.GetFootnoteIdxs().insert( aTmpFntIdx );
2671 aTmpFntIdx.clear();
2672 }
2673
2675 }
2676
2678 return true;
2679}
2680
2682{
2683 SwNodeIndex aIdx( rPaM.Start()->GetNode() );
2684 bool bJoinText = aIdx.GetNode().IsTextNode();
2685 bool bOneNode = rPaM.GetPoint()->GetNode() == rPaM.GetMark()->GetNode();
2686 aIdx--; // in front of the move area!
2687
2688 bool bRet = MoveRange( rPaM, rPos, SwMoveFlags::DEFAULT );
2689 if( !bRet || bOneNode )
2690 return;
2691
2692 if( bJoinText )
2693 ++aIdx;
2694 SwTextNode * pTextNd = aIdx.GetNode().GetTextNode();
2695 SwNodeIndex aNxtIdx( aIdx );
2696 if( pTextNd && pTextNd->CanJoinNext( &aNxtIdx ) )
2697 {
2698 { // Block so SwContentIndex into node is deleted before Join
2699 m_rDoc.CorrRel( aNxtIdx.GetNode(),
2700 SwPosition( *pTextNd, pTextNd->GetText().getLength() ),
2701 0, true );
2702 }
2703 pTextNd->JoinNext();
2704 }
2705}
2706
2707// Overwrite only uses the point of the PaM, the mark is ignored; characters
2708// are replaced from point until the end of the node; at the end of the node,
2709// characters are inserted.
2710bool DocumentContentOperationsManager::Overwrite( const SwPaM &rRg, const OUString &rStr )
2711{
2712 assert(rStr.getLength());
2713 SwPosition& rPt = *const_cast<SwPosition*>(rRg.GetPoint());
2714 if( m_rDoc.GetAutoCorrExceptWord() ) // Add to AutoCorrect
2715 {
2716 if( 1 == rStr.getLength() )
2717 m_rDoc.GetAutoCorrExceptWord()->CheckChar( rPt, rStr[ 0 ] );
2719 }
2720
2721 SwTextNode *pNode = rPt.GetNode().GetTextNode();
2722 if (!pNode || rStr.getLength() > pNode->GetSpaceLeft()) // worst case: no erase
2723 {
2724 return false;
2725 }
2726
2728 {
2729 m_rDoc.GetIDocumentUndoRedo().ClearRedo(); // AppendUndo not always called
2730 }
2731
2732 const size_t nOldAttrCnt = pNode->GetpSwpHints()
2733 ? pNode->GetpSwpHints()->Count() : 0;
2734 SwDataChanged aTmp( rRg );
2735 sal_Int32 const nActualStart(rPt.GetContentIndex());
2736 sal_Int32 nStart = 0;
2737
2738 bool bOldExpFlg = pNode->IsIgnoreDontExpand();
2739 pNode->SetIgnoreDontExpand( true );
2740
2741 for( sal_Int32 nCnt = 0; nCnt < rStr.getLength(); ++nCnt )
2742 {
2743 // start behind the characters (to fix the attributes!)
2744 nStart = rPt.GetContentIndex();
2745 if (nStart < pNode->GetText().getLength())
2746 {
2747 lcl_SkipAttr( pNode, rPt, nStart );
2748 }
2749 sal_Unicode c = rStr[ nCnt ];
2751 {
2752 bool bMerged(false);
2754 {
2755 SwUndo *const pUndo = m_rDoc.GetUndoManager().GetLastUndo();
2756 SwUndoOverwrite *const pUndoOW(
2757 dynamic_cast<SwUndoOverwrite *>(pUndo) );
2758 if (pUndoOW)
2759 {
2760 // if CanGrouping() returns true it's already merged
2761 bMerged = pUndoOW->CanGrouping(m_rDoc, rPt, c);
2762 }
2763 }
2764 if (!bMerged)
2765 {
2767 std::make_unique<SwUndoOverwrite>(m_rDoc, rPt, c) );
2768 }
2769 }
2770 else
2771 {
2772 // start behind the characters (to fix the attributes!)
2773 if (nStart < pNode->GetText().getLength())
2774 rPt.AdjustContent(+1);
2775 pNode->InsertText( OUString(c), rPt, SwInsertFlags::EMPTYEXPAND );
2776 if( nStart+1 < rPt.GetContentIndex() )
2777 {
2778 rPt.SetContent(nStart);
2779 pNode->EraseText( rPt, 1 );
2780 rPt.AdjustContent(+1);
2781 }
2782 }
2783 }
2784 pNode->SetIgnoreDontExpand( bOldExpFlg );
2785
2786 const size_t nNewAttrCnt = pNode->GetpSwpHints()
2787 ? pNode->GetpSwpHints()->Count() : 0;
2788 if( nOldAttrCnt != nNewAttrCnt )
2789 {
2790 const SwUpdateAttr aHint(0,0,0);
2791 pNode->TriggerNodeUpdate(sw::LegacyModifyHint(&aHint, &aHint));
2792 }
2793
2796 {
2797 SwPaM aPam(rPt.GetNode(), nActualStart, rPt.GetNode(), rPt.GetContentIndex());
2798 m_rDoc.getIDocumentRedlineAccess().DeleteRedline( aPam, true, RedlineType::Any );
2799 }
2801 {
2802 // FIXME: this redline is WRONG: there is no DELETE, and the skipped
2803 // characters are also included in aPam
2804 SwPaM aPam(rPt.GetNode(), nActualStart, rPt.GetNode(), rPt.GetContentIndex());
2805 m_rDoc.getIDocumentRedlineAccess().AppendRedline( new SwRangeRedline( RedlineType::Insert, aPam ), true);
2806 }
2807
2809 return true;
2810}
2811
2812bool DocumentContentOperationsManager::InsertString( const SwPaM &rRg, const OUString &rStr,
2813 const SwInsertFlags nInsertMode )
2814{
2815 // tdf#119019 accept tracked paragraph formatting to do not hide new insertions
2817 {
2822 }
2823
2824 // fetching DoesUndo is surprisingly expensive
2825 bool bDoesUndo = m_rDoc.GetIDocumentUndoRedo().DoesUndo();
2826 if (bDoesUndo)
2827 m_rDoc.GetIDocumentUndoRedo().ClearRedo(); // AppendUndo not always called!
2828
2829 const SwPosition& rPos = *rRg.GetPoint();
2830
2831 if( m_rDoc.GetAutoCorrExceptWord() ) // add to auto correction
2832 {
2833 if( 1 == rStr.getLength() && m_rDoc.GetAutoCorrExceptWord()->IsDeleted() )
2834 {
2835 m_rDoc.GetAutoCorrExceptWord()->CheckChar( rPos, rStr[ 0 ] );
2836 }
2838 }
2839
2840 SwTextNode *const pNode = rPos.GetNode().GetTextNode();
2841 if(!pNode)
2842 return false;
2843
2844 SwDataChanged aTmp( rRg );
2845
2846 if (!bDoesUndo || !m_rDoc.GetIDocumentUndoRedo().DoesGroupUndo())
2847 {
2848 OUString const ins(pNode->InsertText(rStr, rPos, nInsertMode));
2849 if (bDoesUndo)
2850 {
2852 std::make_unique<SwUndoInsert>(rPos.GetNode(),
2853 rPos.GetContentIndex(), ins.getLength(), nInsertMode));
2854 }
2855 }
2856 else
2857 { // if Undo and grouping is enabled, everything changes!
2858 SwUndoInsert * pUndo = nullptr;
2859
2860 // don't group the start if hints at the start should be expanded
2861 if (!(nInsertMode & SwInsertFlags::FORCEHINTEXPAND))
2862 {
2863 SwUndo *const pLastUndo = m_rDoc.GetUndoManager().GetLastUndo();
2864 SwUndoInsert *const pUndoInsert(
2865 dynamic_cast<SwUndoInsert *>(pLastUndo) );
2866 if (pUndoInsert && pUndoInsert->CanGrouping(rPos))
2867 {
2868 pUndo = pUndoInsert;
2869 }
2870 }
2871
2872 CharClass const& rCC = GetAppCharClass();
2873 sal_Int32 nInsPos = rPos.GetContentIndex();
2874
2875 if (!pUndo)
2876 {
2877 pUndo = new SwUndoInsert( rPos.GetNode(), nInsPos, 0, nInsertMode,
2878 !rCC.isLetterNumeric( rStr, 0 ) );
2879 m_rDoc.GetIDocumentUndoRedo().AppendUndo( std::unique_ptr<SwUndo>(pUndo) );
2880 }
2881
2882 OUString const ins(pNode->InsertText(rStr, rPos, nInsertMode));
2883
2884 for (sal_Int32 i = 0; i < ins.getLength(); ++i)
2885 {
2886 nInsPos++;
2887 // if CanGrouping() returns true, everything has already been done
2888 if (!pUndo->CanGrouping(ins[i]))
2889 {
2890 pUndo = new SwUndoInsert(rPos.GetNode(), nInsPos, 1, nInsertMode,
2891 !rCC.isLetterNumeric(ins, i));
2892 m_rDoc.GetIDocumentUndoRedo().AppendUndo( std::unique_ptr<SwUndo>(pUndo) );
2893 }
2894 }
2895 }
2896
2897 // To-Do - add 'SwExtraRedlineTable' also ?
2899 {
2900 SwPaM aPam( rPos.GetNode(), aTmp.GetContent(),
2901 rPos.GetNode(), rPos.GetContentIndex());
2903 {
2905 new SwRangeRedline( RedlineType::Insert, aPam ), true);
2906 }
2907 else
2908 {
2910 }
2911 }
2912
2914 return true;
2915}
2916
2918{
2919 m_bIME = bIME;
2920}
2921
2923{
2924 return m_bIME;
2925}
2926
2928 const SwPaM& rPaM,
2930{
2931 std::unique_ptr<SwUndoTransliterate> pUndo;
2933 pUndo.reset(new SwUndoTransliterate( rPaM, rTrans ));
2934
2935 auto [pStt, pEnd] = rPaM.StartEnd(); // SwPosition*
2936 SwNodeOffset nSttNd = pStt->GetNodeIndex(),
2937 nEndNd = pEnd->GetNodeIndex();
2938 sal_Int32 nSttCnt = pStt->GetContentIndex();
2939 sal_Int32 nEndCnt = pEnd->GetContentIndex();
2940
2941 SwTextNode* pTNd = pStt->GetNode().GetTextNode();
2942 if( (pStt == pEnd) && pTNd ) // no selection?
2943 {
2944 /* Check if cursor is inside of a word */
2945 assert(g_pBreakIt && g_pBreakIt->GetBreakIter().is());
2946 Boundary aBndry = g_pBreakIt->GetBreakIter()->getWordBoundary(
2947 pTNd->GetText(), nSttCnt,
2948 g_pBreakIt->GetLocale( pTNd->GetLang( nSttCnt ) ),
2949 WordType::ANY_WORD /*ANYWORD_IGNOREWHITESPACES*/,
2950 true);
2951
2952 if( aBndry.startPos < nSttCnt && nSttCnt < aBndry.endPos )
2953 {
2954 /* Cursor is inside of a word */
2955 if (rTrans.getType() == TransliterationFlags::SENTENCE_CASE) {
2956 /* set current sentence as 'area of effect' */
2957 nSttCnt = g_pBreakIt->GetBreakIter()->beginOfSentence(
2958 pTNd->GetText(), nSttCnt,
2959 g_pBreakIt->GetLocale( pTNd->GetLang( nSttCnt ) ) );
2960 nEndCnt = g_pBreakIt->GetBreakIter()->endOfSentence(
2961 pTNd->GetText(), nEndCnt,
2962 g_pBreakIt->GetLocale( pTNd->GetLang( nEndCnt ) ) );
2963 } else {
2964 /* Set current word as 'area of effect' */
2965 nSttCnt = aBndry.startPos;
2966 nEndCnt = aBndry.endPos;
2967 }
2968 } else {
2969 /* Cursor is not inside of a word. Nothing should happen. */
2970 return;
2971 }
2972 }
2973
2974 bool bUseRedlining = m_rDoc.getIDocumentRedlineAccess().IsRedlineOn();
2975 // as a workaround for a known performance problem, switch off redlining
2976 // to avoid freezing, if transliteration could result too many redlines
2977 if ( bUseRedlining )
2978 {
2979 const sal_uLong nMaxRedlines = 500;
2980 const bool bIsTitleCase = rTrans.getType() == TransliterationFlags::TITLE_CASE;
2981 sal_uLong nAffectedNodes = 0;
2982 sal_uLong nAffectedChars = nEndCnt;
2983 SwNodeIndex aIdx( pStt->GetNode() );
2984 for( ; aIdx.GetIndex() <= nEndNd; ++aIdx )
2985 {
2986 SwTextNode* pAffectedNode = aIdx.GetNode().GetTextNode();
2987
2988 // don't count not text nodes or empty text nodes
2989 if( !pAffectedNode || pAffectedNode->GetText().isEmpty() )
2990 continue;
2991
2992 nAffectedNodes++;
2993
2994 // count characters of the node (the last - maybe partially
2995 // selected - node was counted at initialization of nAffectedChars)
2996 if( aIdx.GetIndex() < nEndNd )
2997 nAffectedChars += pAffectedNode->GetText().getLength();
2998
2999 // transliteration creates n redlines for n nodes, except in the
3000 // case of title case, where it creates n redlines for n words
3001 if( nAffectedNodes > nMaxRedlines ||
3002 // estimate word count based on the character count, where
3003 // 6 = average English word length is ~5 letters + space
3004 ( bIsTitleCase && (nAffectedChars - nSttCnt)/6 > nMaxRedlines ) )
3005 {
3006 bUseRedlining = false;
3007 break;
3008 }
3009 }
3010 }
3011
3012 if( nSttNd != nEndNd ) // is more than one text node involved?
3013 {
3014 // iterate over all affected text nodes, the first and the last one
3015 // may be incomplete because the selection starts and/or ends there
3016
3017 SwNodeIndex aIdx( pStt->GetNode() );
3018 if( nSttCnt )
3019 {
3020 ++aIdx;
3021 if( pTNd )
3022 {
3023 pTNd->TransliterateText(
3024 rTrans, nSttCnt, pTNd->GetText().getLength(), pUndo.get(), bUseRedlining);
3025 }
3026 }
3027
3028 for( ; aIdx.GetIndex() < nEndNd; ++aIdx )
3029 {
3030 pTNd = aIdx.GetNode().GetTextNode();
3031 if (pTNd)
3032 {
3033 pTNd->TransliterateText(
3034 rTrans, 0, pTNd->GetText().getLength(), pUndo.get(), bUseRedlining);
3035 }
3036 }
3037
3038 if( nEndCnt && nullptr != ( pTNd = pEnd->GetNode().GetTextNode() ))
3039 {
3040 pTNd->TransliterateText( rTrans, 0, nEndCnt, pUndo.get(), bUseRedlining );
3041 }
3042 }
3043 else if( pTNd && nSttCnt < nEndCnt )
3044 {
3045 pTNd->TransliterateText( rTrans, nSttCnt, nEndCnt, pUndo.get(), bUseRedlining );
3046 }
3047 if( pUndo && pUndo->HasData() )
3048 {
3049 m_rDoc.GetIDocumentUndoRedo().AppendUndo(std::move(pUndo));
3050 }
3052}
3053
3055 const SwPaM &rRg,
3056 const OUString& rGrfName,
3057 const OUString& rFltName,
3058 const Graphic* pGraphic,
3059 const SfxItemSet* pFlyAttrSet,
3060 const SfxItemSet* pGrfAttrSet,
3061 SwFrameFormat* pFrameFormat )
3062{
3063 if( !pFrameFormat )
3065 SwGrfNode* pSwGrfNode = SwNodes::MakeGrfNode(
3067 rGrfName, rFltName, pGraphic,
3069 SwFlyFrameFormat* pSwFlyFrameFormat = InsNoTextNode( *rRg.GetPoint(), pSwGrfNode,
3070 pFlyAttrSet, pGrfAttrSet, pFrameFormat );
3071 return pSwFlyFrameFormat;
3072}
3073
3075 const SwPaM &rRg, const GraphicObject& rGrfObj,
3076 const SfxItemSet* pFlyAttrSet,
3077 const SfxItemSet* pGrfAttrSet )
3078{
3080 SwGrfNode* pSwGrfNode = SwNodes::MakeGrfNode(
3082 rGrfObj, m_rDoc.GetDfltGrfFormatColl() );
3083 SwFlyFrameFormat* pSwFlyFrameFormat = InsNoTextNode( *rRg.GetPoint(), pSwGrfNode,
3084 pFlyAttrSet, pGrfAttrSet, pFrameFormat );
3085 return pSwFlyFrameFormat;
3086}
3087
3089 const SwPaM &rRg, const svt::EmbeddedObjectRef& xObj,
3090 SfxItemSet* pFlyAttrSet)
3091{
3092 sal_uInt16 nId = RES_POOLFRM_OLE;
3093 if (xObj.is())
3094 {
3095 SvGlobalName aClassName( xObj->getClassID() );
3096 if (SotExchange::IsMath(aClassName))
3097 {
3099 }
3100 }
3101
3103
3106 xObj,
3108 pFlyAttrSet, nullptr,
3109 pFrameFormat );
3110}
3111
3113 sal_Int64 nAspect,
3114 const SfxItemSet* pFlyAttrSet,
3115 const SfxItemSet* pGrfAttrSet)
3116{
3118
3119 return InsNoTextNode( *rRg.GetPoint(),
3122 rObjName,
3123 nAspect,
3125 nullptr ),
3126 pFlyAttrSet, pGrfAttrSet,
3127 pFrameFormat );
3128}
3129
3130void DocumentContentOperationsManager::ReRead( SwPaM& rPam, const OUString& rGrfName,
3131 const OUString& rFltName, const Graphic* pGraphic )
3132{
3133 SwGrfNode *pGrfNd;
3134 if( !(( !rPam.HasMark()
3135 || rPam.GetPoint()->GetNodeIndex() == rPam.GetMark()->GetNodeIndex() )
3136 && nullptr != ( pGrfNd = rPam.GetPoint()->GetNode().GetGrfNode() )) )
3137 return;
3138
3140 {
3141 m_rDoc.GetIDocumentUndoRedo().AppendUndo(std::make_unique<SwUndoReRead>(rPam, *pGrfNd));
3142 }
3143
3144 // Because we don't know if we can mirror the graphic, the mirror attribute is always reset
3145 if( MirrorGraph::Dont != pGrfNd->GetSwAttrSet().
3146 GetMirrorGrf().GetValue() )
3147 pGrfNd->SetAttr( SwMirrorGrf() );
3148
3149 pGrfNd->ReRead( rGrfName, rFltName, pGraphic );
3151}
3152
3153// Insert drawing object, which has to be already inserted in the DrawModel
3155 const SwPaM &rRg,
3156 SdrObject& rDrawObj,
3157 const SfxItemSet& rFlyAttrSet )
3158{
3160
3161 const SwFormatAnchor* pAnchor = rFlyAttrSet.GetItemIfSet( RES_ANCHOR, false );
3162 pFormat->SetFormatAttr( rFlyAttrSet );
3163
3164 // Didn't set the Anchor yet?
3165 // DrawObjecte must never end up in the Header/Footer!
3166 RndStdIds eAnchorId = pAnchor != nullptr ? pAnchor->GetAnchorId() : pFormat->GetAnchor().GetAnchorId();
3167 const bool bIsAtContent = (RndStdIds::FLY_AT_PAGE != eAnchorId);
3168
3169 const SwPosition* pChkPos = nullptr;
3170 if ( pAnchor == nullptr )
3171 {
3172 pChkPos = rRg.GetPoint();
3173 }
3174 else if ( bIsAtContent )
3175 {
3176 pChkPos =
3177 pAnchor->GetContentAnchor() ? pAnchor->GetContentAnchor() : rRg.GetPoint();
3178 }
3179
3180 // allow drawing objects in header/footer, but control objects aren't allowed in header/footer.
3181 if( pChkPos != nullptr
3182 && ::CheckControlLayer( &rDrawObj )
3183 && m_rDoc.IsInHeaderFooter( pChkPos->GetNode() ) )
3184 {
3185 // apply at-page anchor format
3186 eAnchorId = RndStdIds::FLY_AT_PAGE;
3187 pFormat->SetFormatAttr( SwFormatAnchor( eAnchorId ) );
3188 }
3189 else if( pAnchor == nullptr
3190 || ( bIsAtContent
3191 && pAnchor->GetContentAnchor() == nullptr ) )
3192 {
3193 // apply anchor format
3194 SwFormatAnchor aAnch( pAnchor != nullptr ? *pAnchor : pFormat->GetAnchor() );
3195 eAnchorId = aAnch.GetAnchorId();
3196 if ( eAnchorId == RndStdIds::FLY_AT_FLY )
3197 {
3198 const SwStartNode* pStartNode = rRg.GetPointNode().FindFlyStartNode();
3199 assert(pStartNode);
3200 SwPosition aPos(*pStartNode);
3201 aAnch.SetAnchor( &aPos );
3202 }
3203 else
3204 {
3205 aAnch.SetAnchor( rRg.GetPoint() );
3206 if ( eAnchorId == RndStdIds::FLY_AT_PAGE )
3207 {
3208 eAnchorId = dynamic_cast<const SdrUnoObj*>( &rDrawObj) != nullptr ? RndStdIds::FLY_AS_CHAR : RndStdIds::FLY_AT_PARA;
3209 aAnch.SetType( eAnchorId );
3210 }
3211 }
3212 pFormat->SetFormatAttr( aAnch );
3213 }
3214
3215 // insert text attribute for as-character anchored drawing object
3216 if ( eAnchorId == RndStdIds::FLY_AS_CHAR )
3217 {
3218 bool bAnchorAtPageAsFallback = true;
3219 const SwFormatAnchor& rDrawObjAnchorFormat = pFormat->GetAnchor();
3220 if ( rDrawObjAnchorFormat.GetContentAnchor() != nullptr )
3221 {
3222 SwTextNode* pAnchorTextNode =
3223 rDrawObjAnchorFormat.GetContentAnchor()->GetNode().GetTextNode();
3224 if ( pAnchorTextNode != nullptr )
3225 {
3226 const sal_Int32 nStt = rDrawObjAnchorFormat.GetContentAnchor()->GetContentIndex();
3227 SwFormatFlyCnt aFormat( pFormat );
3228 pAnchorTextNode->InsertItem( aFormat, nStt, nStt );
3229 bAnchorAtPageAsFallback = false;
3230 }
3231 }
3232
3233 if ( bAnchorAtPageAsFallback )
3234 {
3235 OSL_ENSURE( false, "DocumentContentOperationsManager::InsertDrawObj(..) - missing content anchor for as-character anchored drawing object --> anchor at-page" );
3236 pFormat->SetFormatAttr( SwFormatAnchor( RndStdIds::FLY_AT_PAGE ) );
3237 }
3238 }
3239
3240 SwDrawContact* pContact = new SwDrawContact( pFormat, &rDrawObj );
3241
3242 // Create Frames if necessary
3244 {
3245 // create layout representation
3246 pFormat->MakeFrames();
3247 // #i42319# - follow-up of #i35635#
3248 // move object to visible layer
3249 // #i79391#
3250 if ( pContact->GetAnchorFrame() )
3251 {
3252 pContact->MoveObjToVisibleLayer( &rDrawObj );
3253 }
3254 }
3255
3257 {
3258 m_rDoc.GetIDocumentUndoRedo().AppendUndo( std::make_unique<SwUndoInsLayFormat>(pFormat, SwNodeOffset(0), 0) );
3259 }
3260
3262 return pFormat;
3263}
3264
3265bool DocumentContentOperationsManager::SplitNode( const SwPosition &rPos, bool bChkTableStart )
3266{
3267 SwContentNode *pNode = rPos.GetNode().GetContentNode();
3268 if(nullptr == pNode)
3269 return false;
3270
3271 {
3272 // BUG 26675: Send DataChanged before deleting, so that we notice which objects are in scope.
3273 // After that they can be before/after the position.
3274 SwDataChanged aTmp( m_rDoc, rPos );
3275 }
3276
3277 SwUndoSplitNode* pUndo = nullptr;
3279 {
3281 // insert the Undo object (currently only for TextNode)
3282 if( pNode->IsTextNode() )
3283 {
3284 pUndo = new SwUndoSplitNode( m_rDoc, rPos, bChkTableStart );
3285 m_rDoc.GetIDocumentUndoRedo().AppendUndo(std::unique_ptr<SwUndo>(pUndo));
3286 }
3287 }
3288
3289 // Update the rsid of the old and the new node unless
3290 // the old node is split at the beginning or at the end
3291 SwTextNode *pTextNode = rPos.GetNode().GetTextNode();
3292 const sal_Int32 nPos = rPos.GetContentIndex();
3293 if( pTextNode && nPos && nPos != pTextNode->Len() )
3294 {
3295 m_rDoc.UpdateParRsid( pTextNode );
3296 }
3297
3298 //JP 28.01.97: Special case for SplitNode at table start:
3299 // If it is at the beginning of a Doc/Fly/Footer/... or right at after a table
3300 // then insert a paragraph before it.
3301 if( bChkTableStart && !rPos.GetContentIndex() && pNode->IsTextNode() )
3302 {
3303 SwNodeOffset nPrevPos = rPos.GetNodeIndex() - 1;
3304 SwTableNode* pTableNd;
3305 const SwNode* pNd = m_rDoc.GetNodes()[ nPrevPos ];
3306 if( pNd->IsStartNode() &&
3307 SwTableBoxStartNode == static_cast<const SwStartNode*>(pNd)->GetStartNodeType() &&
3308 nullptr != ( pTableNd = m_rDoc.GetNodes()[ --nPrevPos ]->GetTableNode() ) &&
3309 ((( pNd = m_rDoc.GetNodes()[ --nPrevPos ])->IsStartNode() &&
3310 SwTableBoxStartNode != static_cast<const SwStartNode*>(pNd)->GetStartNodeType() )
3311 || ( pNd->IsEndNode() && pNd->StartOfSectionNode()->IsTableNode() )
3312 || pNd->IsContentNode() ))
3313 {
3314 if( pNd->IsContentNode() )
3315 {
3316 //JP 30.04.99 Bug 65660:
3317 // There are no page breaks outside of the normal body area,
3318 // so this is not a valid condition to insert a paragraph.
3319 if( nPrevPos < m_rDoc.GetNodes().GetEndOfExtras().GetIndex() )
3320 pNd = nullptr;
3321 else
3322 {
3323 // Only if the table has page breaks!
3324 const SwFrameFormat* pFrameFormat = pTableNd->GetTable().GetFrameFormat();
3325 if( SfxItemState::SET != pFrameFormat->GetItemState(RES_PAGEDESC, false) &&
3326 SfxItemState::SET != pFrameFormat->GetItemState( RES_BREAK, false ) )
3327 pNd = nullptr;
3328 }
3329 }
3330
3331 if( pNd )
3332 {
3334 *pTableNd,
3336 if( pTextNd )
3337 {
3338 const_cast<SwPosition&>(rPos).Assign( pTableNd->GetIndex() - SwNodeOffset(1) );
3339
3340 // only add page breaks/styles to the body area
3341 if( nPrevPos > m_rDoc.GetNodes().GetEndOfExtras().GetIndex() )
3342 {
3343 SwFrameFormat* pFrameFormat = pTableNd->GetTable().GetFrameFormat();
3344 const SfxPoolItem *pItem;
3345 if( SfxItemState::SET == pFrameFormat->GetItemState( RES_PAGEDESC,
3346 false, &pItem ) )
3347 {
3348 pTextNd->SetAttr( *pItem );
3349 pFrameFormat->ResetFormatAttr( RES_PAGEDESC );
3350 }
3351 if( SfxItemState::SET == pFrameFormat->GetItemState( RES_BREAK,
3352 false, &pItem ) )
3353 {
3354 pTextNd->SetAttr( *pItem );
3355 pFrameFormat->ResetFormatAttr( RES_BREAK );
3356 }
3357 }
3358
3359 if( pUndo )
3360 pUndo->SetTableFlag();
3362 return true;
3363 }
3364 }
3365 }
3366 }
3367
3368 const std::shared_ptr<sw::mark::ContentIdxStore> pContentStore(sw::mark::ContentIdxStore::Create());
3369 pContentStore->Save( m_rDoc, rPos.GetNodeIndex(), rPos.GetContentIndex(), true );
3370 assert(pNode->IsTextNode());
3371 std::function<void (SwTextNode *, sw::mark::RestoreMode, bool bAtStart)> restoreFunc(
3372 [&](SwTextNode *const, sw::mark::RestoreMode const eMode, bool const bAtStart)
3373 {
3374 if (!pContentStore->Empty())
3375 { // move all bookmarks, TOXMarks, FlyAtCnt
3376 pContentStore->Restore(m_rDoc, rPos.GetNodeIndex()-SwNodeOffset(1), 0, true, bAtStart && (eMode & sw::mark::RestoreMode::Flys), eMode);
3377 }
3379 {
3380 // To-Do - add 'SwExtraRedlineTable' also ?
3384 {
3385 SwPaM aPam( rPos );
3386 aPam.SetMark();
3387 aPam.Move( fnMoveBackward );
3388 if (m_rDoc.getIDocumentRedlineAccess().IsRedlineOn())
3389 {
3390 m_rDoc.getIDocumentRedlineAccess().AppendRedline(
3391 new SwRangeRedline(RedlineType::Insert, aPam), true);
3392 }
3393 else
3394 {
3395 m_rDoc.getIDocumentRedlineAccess().SplitRedline(aPam);
3396 }
3397 }
3398 }
3399 });
3400 pNode->GetTextNode()->SplitContentNode(rPos, &restoreFunc);
3401
3403 return true;
3404}
3405
3406bool DocumentContentOperationsManager::AppendTextNode( SwPosition& rPos )
3407{
3408 // create new node before EndOfContent
3409 SwTextNode * pCurNode = rPos.GetNode().GetTextNode();
3410 if( !pCurNode )
3411 {
3412 // so then one can be created!
3413 SwNodeIndex aIdx( rPos.GetNode(), 1 );
3414 pCurNode = m_rDoc.GetNodes().MakeTextNode( aIdx.GetNode(),
3416 }
3417 else
3418 pCurNode = pCurNode->AppendNode( rPos )->GetTextNode();
3419
3420 rPos.Adjust(SwNodeOffset(1));
3421
3423 {
3424 m_rDoc.GetIDocumentUndoRedo().AppendUndo( std::make_unique<SwUndoInsert>( rPos.GetNode() ) );
3425 }
3426
3427 // To-Do - add 'SwExtraRedlineTable' also ?
3429 {
3430 SwPaM aPam( rPos );
3431 aPam.SetMark();
3432 aPam.Move( fnMoveBackward );
3434 m_rDoc.getIDocumentRedlineAccess().AppendRedline( new SwRangeRedline( RedlineType::Insert, aPam ), true);
3435 else
3437 }
3438
3440 return true;
3441}
3442
3443bool DocumentContentOperationsManager::ReplaceRange( SwPaM& rPam, const OUString& rStr,
3444 const bool bRegExReplace )
3445{
3446 // unfortunately replace works slightly differently from delete,
3447 // so we cannot use lcl_DoWithBreaks here...
3448
3449 std::vector<std::pair<SwNodeOffset, sal_Int32>> Breaks;
3450
3451 SwPaM aPam( *rPam.GetMark(), *rPam.GetPoint() );
3452 aPam.Normalize(false);
3453 if (aPam.GetPoint()->GetNode() != aPam.GetMark()->GetNode())
3454 {
3455 aPam.Move(fnMoveBackward);
3456 }
3457 OSL_ENSURE((aPam.GetPoint()->GetNode() == aPam.GetMark()->GetNode()), "invalid pam?");
3458
3459 sw::CalcBreaks(Breaks, aPam);
3460
3461 while (!Breaks.empty() // skip over prefix of dummy chars
3462 && (aPam.GetMark()->GetNodeIndex() == Breaks.begin()->first)
3463 && (aPam.GetMark()->GetContentIndex() == Breaks.begin()->second))
3464 {
3465 // skip!
3466 aPam.GetMark()->AdjustContent(+1); // always in bounds if Breaks valid
3467 Breaks.erase(Breaks.begin());
3468 }
3469 *rPam.Start() = *aPam.GetMark(); // update start of original pam w/ prefix
3470
3471 if (Breaks.empty())
3472 {
3473 // park aPam somewhere so it does not point to node that is deleted
3474 aPam.DeleteMark();
3476 return ReplaceRangeImpl(rPam, rStr, bRegExReplace); // original pam!
3477 }
3478
3479 // Deletion must be split into several parts if the text node
3480 // contains a text attribute with end and with dummy character
3481 // and the selection does not contain the text attribute completely,
3482 // but overlaps its start (left), where the dummy character is.
3483
3484 bool bRet( true );
3485 // iterate from end to start, to avoid invalidating the offsets!
3486 auto iter( Breaks.rbegin() );
3487 SwNodeOffset nOffset(0);
3488 SwNodes const& rNodes(rPam.GetPoint()->GetNodes());
3489 OSL_ENSURE(aPam.GetPoint() == aPam.End(), "wrong!");
3490 SwPosition & rEnd( *aPam.End() );
3491 SwPosition & rStart( *aPam.Start() );
3492
3493 // set end of temp pam to original end (undo Move backward above)
3494 rEnd = *rPam.End();
3495 // after first deletion, rEnd will point into the original text node again!
3496
3497 while (iter != Breaks.rend())
3498 {
3499 rStart.Assign(*rNodes[iter->first - nOffset]->GetTextNode(), iter->second + 1);
3500 if (rStart < rEnd) // check if part is empty
3501 {
3503 ? DeleteAndJoinWithRedlineImpl(aPam, SwDeleteFlags::Default)
3504 : DeleteAndJoinImpl(aPam, SwDeleteFlags::Default);
3505 nOffset = iter->first - rStart.GetNodeIndex(); // deleted fly nodes...
3506 }
3507 rEnd.Assign(*rNodes[iter->first - nOffset]->GetTextNode(), iter->second);
3508 ++iter;
3509 }
3510
3511 rStart = *rPam.Start(); // set to original start
3512 assert(rStart < rEnd && "replace part empty!");
3513 if (rStart < rEnd) // check if part is empty
3514 {
3515 bRet &= ReplaceRangeImpl(aPam, rStr, bRegExReplace);
3516 }
3517
3518 rPam = aPam; // update original pam (is this required?)
3519
3520 return bRet;
3521}
3522
3523bool DocumentContentOperationsManager::InsertPoolItem(
3524 const SwPaM &rRg,
3525 const SfxPoolItem &rHt,
3526 const SetAttrMode nFlags,
3527 SwRootFrame const*const pLayout,
3528 SwTextAttr **ppNewTextAttr)
3529{
3530 SwDataChanged aTmp( rRg );
3531 std::unique_ptr<SwUndoAttr> pUndoAttr;
3533 {
3535 pUndoAttr.reset(new SwUndoAttr( rRg, rHt, nFlags ));
3536 }
3537
3538 SfxItemSet aSet( m_rDoc.GetAttrPool(), rHt.Which(), rHt.Which() );
3539 aSet.Put( rHt );
3540 const bool bRet = lcl_InsAttr(m_rDoc, rRg, aSet, nFlags, pUndoAttr.get(), pLayout, ppNewTextAttr);
3541
3543 {
3544 m_rDoc.GetIDocumentUndoRedo().AppendUndo( std::move(pUndoAttr) );
3545 }
3546
3547 if( bRet )
3548 {
3550 }
3551 return bRet;
3552}
3553
3554void DocumentContentOperationsManager::InsertItemSet ( const SwPaM &rRg, const SfxItemSet &rSet,
3555 const SetAttrMode nFlags, SwRootFrame const*const pLayout)
3556{
3557 SwDataChanged aTmp( rRg );
3558 std::unique_ptr<SwUndoAttr> pUndoAttr;
3560 {
3562 pUndoAttr.reset(new SwUndoAttr( rRg, rSet, nFlags ));
3563 }
3564
3565 bool bRet = lcl_InsAttr(m_rDoc, rRg, rSet, nFlags, pUndoAttr.get(), pLayout, /*ppNewTextAttr*/nullptr );
3566
3568 {
3569 m_rDoc.GetIDocumentUndoRedo().AppendUndo( std::move(pUndoAttr) );
3570 }
3571
3572 if( bRet )
3574}
3575
3576void DocumentContentOperationsManager::RemoveLeadingWhiteSpace(const SwPosition & rPos )
3577{
3578 const SwTextNode* pTNd = rPos.GetNode().GetTextNode();
3579 if ( !pTNd )
3580 return;
3581
3582 const OUString& rText = pTNd->GetText();
3583 sal_Int32 nIdx = 0;
3584 while (nIdx < rText.getLength())
3585 {
3586 sal_Unicode const cCh = rText[nIdx];
3587 if (('\t' != cCh) && (' ' != cCh))
3588 {
3589 break;
3590 }
3591 ++nIdx;
3592 }
3593
3594 if ( nIdx > 0 )
3595 {
3596 SwPaM aPam(rPos);
3597 aPam.GetPoint()->SetContent(0);
3598 aPam.SetMark();
3599 aPam.GetMark()->SetContent(nIdx);
3600 DeleteRange( aPam );
3601 }
3602}
3603
3604void DocumentContentOperationsManager::RemoveLeadingWhiteSpace(SwPaM& rPaM )
3605{
3606 for (SwPaM& rSel :rPaM.GetRingContainer())
3607 {
3608 SwNodeOffset nStt = rSel.Start()->GetNodeIndex();
3609 SwNodeOffset nEnd = rSel.End()->GetNodeIndex();
3610 for (SwNodeOffset nPos = nStt; nPos<=nEnd; nPos++)
3611 RemoveLeadingWhiteSpace(SwPosition(rSel.GetBound().GetNodes(), nPos));
3612 }
3613}
3614
3615// Copy method from SwDoc - "copy Flys in Flys"
3618void DocumentContentOperationsManager::CopyWithFlyInFly(
3619 const SwNodeRange& rRg,
3620 SwNode& rInsPos,
3621 const std::pair<const SwPaM&, const SwPosition&>* pCopiedPaM /*and real insert pos*/,
3622 const bool bMakeNewFrames,
3623 const bool bDelRedlines,
3624 const bool bCopyFlyAtFly,
3625 SwCopyFlags const flags) const
3626{
3627 assert(!pCopiedPaM || pCopiedPaM->first.End()->GetNode() == rRg.aEnd.GetNode());
3628 assert(!pCopiedPaM || pCopiedPaM->second.GetNode() <= rInsPos);
3629
3630 SwDoc& rDest = rInsPos.GetDoc();
3631 SwNodeIndex aSavePos( rInsPos );
3632
3633 if (rRg.aStart != rRg.aEnd)
3634 {
3635 bool bEndIsEqualEndPos = rInsPos == rRg.aEnd.GetNode();
3636 --aSavePos;
3637 SaveRedlEndPosForRestore aRedlRest( rInsPos, 0 );
3638
3639 // insert behind the already copied start node
3640 m_rDoc.GetNodes().CopyNodes( rRg, rInsPos, false, true );
3641 aRedlRest.Restore();
3642
3643 if (bEndIsEqualEndPos)
3644 {
3645 const_cast<SwNodeIndex&>(rRg.aEnd).Assign(aSavePos.GetNode(), +1);
3646 }
3647 }
3648
3649 // Also copy all bookmarks
3650 // guess this must be done before the DelDummyNodes below as that
3651 // deletes nodes so would mess up the index arithmetic
3652 // sw_fieldmarkhide: also needs to be done before making frames
3654 {
3655 SwPaM aRgTmp( rRg.aStart, rRg.aEnd );
3656 SwPosition targetPos(aSavePos, SwNodeOffset(rRg.aStart != rRg.aEnd ? +1 : 0));
3657 if (pCopiedPaM && rRg.aStart != pCopiedPaM->first.Start()->GetNode())
3658 {
3659 // there is 1 (partially selected, maybe) paragraph before
3660 assert(SwNodeIndex(rRg.aStart, -1) == pCopiedPaM->first.Start()->GetNode());
3661 // only use the passed in target SwPosition if the source PaM point
3662 // is on a different node; if it was the same node then the target
3663 // position was likely moved along by the copy operation and now
3664 // points to the end of the range!
3665 targetPos = pCopiedPaM->second;
3666 }
3667
3668 sw::CopyBookmarks(pCopiedPaM ? pCopiedPaM->first : aRgTmp, targetPos);
3669 }
3670
3671 if (rRg.aStart != rRg.aEnd)
3672 {
3673 bool isRecreateEndNode(false);
3674 if (bMakeNewFrames) // tdf#130685 only after aRedlRest
3675 { // recreate from previous node (could be merged now)
3677 SwTextNode * pNode = aSavePos.GetNode().GetTextNode();
3678 SwTextNode *const pEndNode = rInsPos.GetTextNode();
3679 if (pEndNode)
3680 {
3682 for (SwTextFrame* pFrame = aIter.First(); pFrame; pFrame = aIter.Next())
3683 {
3684 if (pFrame->getRootFrame()->HasMergedParas())
3685 {
3686 frames.insert(pFrame);
3687 // tdf#135061 check if end node is merged to a preceding node
3688 if (pNode == nullptr && pFrame->GetMergedPara()
3689 && pFrame->GetMergedPara()->pFirstNode->GetIndex() < aSavePos.GetIndex())
3690 {
3691 pNode = pFrame->GetMergedPara()->pFirstNode;
3692 }
3693 }
3694 }
3695 }
3696 if (pNode != nullptr)
3697 {
3699 if (!frames.empty())
3700 { // tdf#132187 check if the end node needs new frames
3702 for (SwTextFrame* pFrame = aIter.First(); pFrame; pFrame = aIter.Next())
3703 {
3704 if (pFrame->getRootFrame()->HasMergedParas())
3705 {
3706 auto const it = frames.find(pFrame);
3707 if (it != frames.end())
3708 {
3709 frames.erase(it);
3710 }
3711 }
3712 }
3713 if (!frames.empty()) // existing frame was deleted
3714 { // all layouts because MakeFrames recreates all layouts
3715 pEndNode->DelFrames(nullptr);
3716 isRecreateEndNode = true;
3717 }
3718 }
3719 }
3720 }
3721 bool const isAtStartOfSection(aSavePos.GetNode().IsStartNode());
3722 ++aSavePos;
3723 if (bMakeNewFrames)
3724 {
3725 // it's possible that CheckParaRedlineMerge() deleted frames
3726 // on rInsPos so have to include it, but it must not be included
3727 // if it was the first node in the document so that MakeFrames()
3728 // will find the existing (wasn't deleted) frame on it
3729 SwNodeIndex const end(rInsPos,
3730 SwNodeOffset((!isRecreateEndNode || isAtStartOfSection)
3731 ? 0 : +1));
3732 ::MakeFrames(&rDest, aSavePos.GetNode(), end.GetNode());
3733 }
3734 }
3735
3736#if OSL_DEBUG_LEVEL > 0
3737 {
3738 //JP 17.06.99: Bug 66973 - check count only if the selection is in
3739 // the same section or there's no section, because sections that are
3740 // not fully selected are not copied.
3741 const SwSectionNode* pSSectNd = rRg.aStart.GetNode().FindSectionNode();
3742 SwNodeIndex aTmpI( rRg.aEnd, -1 );
3743 const SwSectionNode* pESectNd = aTmpI.GetNode().FindSectionNode();
3744 if( pSSectNd == pESectNd &&
3745 !rRg.aStart.GetNode().IsSectionNode() &&
3746 !aTmpI.GetNode().IsEndNode() )
3747 {
3748 // If the range starts with a SwStartNode, it isn't copied
3749 SwNodeOffset offset( (rRg.aStart.GetNode().GetNodeType() != SwNodeType::Start) ? 1 : 0 );
3750 OSL_ENSURE( rInsPos.GetIndex() - aSavePos.GetIndex() ==
3751 rRg.aEnd.GetIndex() - rRg.aStart.GetIndex() - 1 + offset,
3752 "An insufficient number of nodes were copied!" );
3753 }
3754 }
3755#endif
3756
3757 {
3758 ::sw::UndoGuard const undoGuard(rDest.GetIDocumentUndoRedo());
3759 CopyFlyInFlyImpl(rRg, pCopiedPaM ? &pCopiedPaM->first : nullptr,
3760 // see comment below regarding use of pCopiedPaM->second
3761 (pCopiedPaM && rRg.aStart != pCopiedPaM->first.Start()->GetNode())
3762 ? pCopiedPaM->second.GetNode()
3763 : aSavePos.GetNode(),
3764 bCopyFlyAtFly,
3765 flags);
3766 }
3767
3768 SwNodeRange aCpyRange( aSavePos.GetNode(), rInsPos );
3769
3771 lcl_DeleteRedlines( rRg, aCpyRange );
3772
3773 rDest.GetNodes().DelDummyNodes( aCpyRange );
3774}
3775
3776// note: for the redline Show/Hide this must be in sync with
3777// SwRangeRedline::CopyToSection()/DelCopyOfSection()/MoveFromSection()
3778void DocumentContentOperationsManager::CopyFlyInFlyImpl(
3779 const SwNodeRange& rRg,
3780 SwPaM const*const pCopiedPaM,
3781 SwNode& rStartIdx,
3782 const bool bCopyFlyAtFly,
3783 SwCopyFlags const flags) const
3784{
3785 assert(!pCopiedPaM || pCopiedPaM->End()->GetNode() == rRg.aEnd.GetNode());
3786
3787 // First collect all Flys, sort them according to their ordering number,
3788 // and then only copy them. This maintains the ordering numbers (which are only
3789 // managed in the DrawModel).
3790 SwDoc& rDest = rStartIdx.GetDoc();
3791 std::set< ZSortFly > aSet;
3792 const size_t nArrLen = m_rDoc.GetSpzFrameFormats()->size();
3793
3794 SwTextBoxHelper::SavedLink aOldTextBoxes;
3796
3797 for ( size_t n = 0; n < nArrLen; ++n )
3798 {
3799 SwFrameFormat* pFormat = (*m_rDoc.GetSpzFrameFormats())[n];
3800 SwFormatAnchor const*const pAnchor = &pFormat->GetAnchor();
3801 SwPosition const*const pAPos = pAnchor->GetContentAnchor();
3802 if ( !pAPos )
3803 continue;
3804 bool bAdd = false;
3805 SwNodeOffset nSkipAfter = pAPos->GetNodeIndex();
3806 SwNodeOffset nStart = rRg.aStart.GetIndex();
3807 switch ( pAnchor->GetAnchorId() )
3808 {
3809 case RndStdIds::FLY_AT_FLY:
3810 if(bCopyFlyAtFly)
3811 ++nSkipAfter;
3813 ++nStart;
3814 break;
3815 case RndStdIds::FLY_AT_PARA:
3816 {
3817 bAdd = IsSelectFrameAnchoredAtPara(*pAPos,
3818 pCopiedPaM ? *pCopiedPaM->Start() : SwPosition(rRg.aStart),
3819 pCopiedPaM ? *pCopiedPaM->End() : SwPosition(rRg.aEnd),
3820 (flags & SwCopyFlags::IsMoveToFly)
3823 }
3824 break;
3825 case RndStdIds::FLY_AT_CHAR:
3826 {
3827 bAdd = IsDestroyFrameAnchoredAtChar(*pAPos,
3828 pCopiedPaM ? *pCopiedPaM->Start() : SwPosition(rRg.aStart),
3829 pCopiedPaM ? *pCopiedPaM->End() : SwPosition(rRg.aEnd),
3830 (flags & SwCopyFlags::IsMoveToFly)
3833 }
3834 break;
3835 default:
3836 continue;
3837 }
3838 if (RndStdIds::FLY_AT_FLY == pAnchor->GetAnchorId())
3839 {
3840 if (nStart > nSkipAfter)
3841 continue;
3842 if (pAPos->GetNode() > rRg.aEnd.GetNode())
3843 continue;
3844 //frames at the last source node are not always copied:
3845 //- if the node is empty and is the last node of the document or a table cell
3846 // or a text frame then they have to be copied
3847 //- if the content index in this node is > 0 then paragraph and frame bound objects are copied
3848 //- to-character bound objects are copied if their index is <= nEndContentIndex
3849 if (pAPos->GetNode() < rRg.aEnd.GetNode())
3850 bAdd = true;
3851 if (!bAdd && !m_rDoc.getIDocumentRedlineAccess().IsRedlineMove()) // fdo#40599: not for redline move
3852 {
3853 if (!bAdd)
3854 {
3855 // technically old code checked nContent of AT_FLY which is pointless
3856 bAdd = pCopiedPaM && 0 < pCopiedPaM->End()->GetContentIndex();
3857 }
3858 }
3859 }
3860 if( bAdd )
3861 {
3862 aSet.insert( ZSortFly( pFormat, pAnchor, nArrLen + aSet.size() ));
3863 }
3864 }
3865
3866 // Store all copied (and also the newly created) frames in another array.
3867 // They are stored as matching the originals, so that we will be later
3868 // able to build the chains accordingly.
3869 std::vector< SwFrameFormat* > aVecSwFrameFormat;
3870 std::set< ZSortFly >::const_iterator it=aSet.begin();
3871
3872 while (it != aSet.end())
3873 {
3874 // #i59964#
3875 // correct determination of new anchor position
3876 SwFormatAnchor aAnchor( *(*it).GetAnchor() );
3877 assert( aAnchor.GetContentAnchor() != nullptr );
3878 SwPosition newPos = *aAnchor.GetContentAnchor();
3879 // for at-paragraph and at-character anchored objects the new anchor
3880 // position can *not* be determined by the difference of the current
3881 // anchor position to the start of the copied range, because not
3882 // complete selected sections in the copied range aren't copied - see
3883 // method <SwNodes::CopyNodes(..)>.
3884 // Thus, the new anchor position in the destination document is found
3885 // by counting the text nodes.
3886 if ((aAnchor.GetAnchorId() == RndStdIds::FLY_AT_PARA) ||
3887 (aAnchor.GetAnchorId() == RndStdIds::FLY_AT_CHAR) )
3888 {
3889 // First, determine number of anchor text node in the copied range.
3890 // Note: The anchor text node *have* to be inside the copied range.
3891 sal_uLong nAnchorTextNdNumInRange( 0 );
3892 bool bAnchorTextNdFound( false );
3893 // start at the first node for which flys are copied
3894 SwNodeIndex aIdx(pCopiedPaM ? pCopiedPaM->Start()->GetNode() : rRg.aStart.GetNode());
3895 while ( !bAnchorTextNdFound && aIdx <= rRg.aEnd )
3896 {
3897 if ( aIdx.GetNode().IsTextNode() )
3898 {
3899 ++nAnchorTextNdNumInRange;
3900 bAnchorTextNdFound = aAnchor.GetContentAnchor()->GetNode() == aIdx.GetNode();
3901 }
3902
3903 ++aIdx;
3904 }
3905
3906 if ( !bAnchorTextNdFound )
3907 {
3908 // This case can *not* happen, but to be robust take the first
3909 // text node in the destination document.
3910 OSL_FAIL( "<SwDoc::_CopyFlyInFly(..)> - anchor text node in copied range not found" );
3911 nAnchorTextNdNumInRange = 1;
3912 }
3913 // Second, search corresponding text node in destination document
3914 // by counting forward from start insert position <rStartIdx> the
3915 // determined number of text nodes.
3916 aIdx = rStartIdx;
3917 SwNodeIndex aAnchorNdIdx( rStartIdx );
3918 const SwNode& aEndOfContentNd =
3919 aIdx.GetNode().GetNodes().GetEndOfContent();
3920 while ( nAnchorTextNdNumInRange > 0 &&
3921 aIdx.GetNode() != aEndOfContentNd )
3922 {
3923 if ( aIdx.GetNode().IsTextNode() )
3924 {
3925 --nAnchorTextNdNumInRange;
3926 aAnchorNdIdx = aIdx;
3927 }
3928
3929 ++aIdx;
3930 }
3931 if ( !aAnchorNdIdx.GetNode().IsTextNode() )
3932 {
3933 // This case can *not* happen, but to be robust take the first
3934 // text node in the destination document.
3935 OSL_FAIL( "<SwDoc::_CopyFlyInFly(..)> - found anchor node index isn't a text node" );
3936 aAnchorNdIdx = rStartIdx;
3937 while ( !aAnchorNdIdx.GetNode().IsTextNode() )
3938 {
3939 ++aAnchorNdIdx;
3940 }
3941 }
3942 // apply found anchor text node as new anchor position
3943 newPos.Assign( aAnchorNdIdx );
3944 }
3945 else
3946 {
3947 SwNodeOffset nOffset = newPos.GetNodeIndex() - rRg.aStart.GetIndex();
3948 newPos.Assign( rStartIdx, nOffset );
3949 }
3950 // Set the character bound Flys back at the original character
3951 if ((RndStdIds::FLY_AT_CHAR == aAnchor.GetAnchorId()) &&
3952 newPos.GetNode().IsTextNode() )
3953 {
3954 // only if pCopiedPaM: care about partially selected start node
3955 sal_Int32 const nContent = pCopiedPaM && pCopiedPaM->Start()->GetNode() == aAnchor.GetContentAnchor()->GetNode()
3956 ? newPos.GetContentIndex() - pCopiedPaM->Start()->GetContentIndex()
3957 : newPos.GetContentIndex();
3958 newPos.SetContent(nContent);
3959 }
3960 aAnchor.SetAnchor( &newPos );
3961
3962 // Check recursion: if copying content inside the same frame, then don't copy the format.
3963 if( &rDest == &m_rDoc )
3964 {
3965 const SwFormatContent& rContent = (*it).GetFormat()->GetContent();
3966 const SwStartNode* pSNd;
3967 if( rContent.GetContentIdx() &&
3968 nullptr != ( pSNd = rContent.GetContentIdx()->GetNode().GetStartNode() ) &&
3969 pSNd->GetIndex() < rStartIdx.GetIndex() &&
3970 rStartIdx.GetIndex() < pSNd->EndOfSectionIndex() )
3971 {
3972 it = aSet.erase(it);
3973 continue;
3974 }
3975 }
3976
3977 // Ignore TextBoxes, they are already handled in
3978 // sw::DocumentLayoutManager::CopyLayoutFormat().
3979 if (SwTextBoxHelper::isTextBox(it->GetFormat(), RES_FLYFRMFMT))
3980 {
3981 it = aSet.erase(it);
3982 continue;
3983 }
3984
3985 // Copy the format and set the new anchor
3986 aVecSwFrameFormat.push_back( rDest.getIDocumentLayoutAccess().CopyLayoutFormat( *(*it).GetFormat(),
3987 aAnchor, false, true ) );
3988 ++it;
3989 }
3990
3991 // Rebuild as much as possible of all chains that are available in the original,
3992 OSL_ENSURE( aSet.size() == aVecSwFrameFormat.size(), "Missing new Flys" );
3993 if ( aSet.size() != aVecSwFrameFormat.size() )
3994 return;
3995
3996 size_t n = 0;
3997 for (const auto& rFlyN : aSet)
3998 {
3999 const SwFrameFormat *pFormatN = rFlyN.GetFormat();
4000 const SwFormatChain &rChain = pFormatN->GetChain();
4001 size_t k = 0;
4002 for (const auto& rFlyK : aSet)
4003 {
4004 const SwFrameFormat *pFormatK = rFlyK.GetFormat();
4005 if ( rChain.GetPrev() == pFormatK )
4006 {
4007 ::lcl_ChainFormats( static_cast< SwFlyFrameFormat* >(aVecSwFrameFormat[k]),
4008 static_cast< SwFlyFrameFormat* >(aVecSwFrameFormat[n]) );
4009 }
4010 else if ( rChain.GetNext() == pFormatK )
4011 {
4012 ::lcl_ChainFormats( static_cast< SwFlyFrameFormat* >(aVecSwFrameFormat[n]),
4013 static_cast< SwFlyFrameFormat* >(aVecSwFrameFormat[k]) );
4014 }
4015 ++k;
4016 }
4017 ++n;
4018 }
4019
4020 // Re-create content property of draw formats, knowing how old shapes
4021 // were paired with old fly formats (aOldTextBoxes) and that aSet is
4022 // parallel with aVecSwFrameFormat.
4023 SwTextBoxHelper::restoreLinks(aSet, aVecSwFrameFormat, aOldTextBoxes);
4024}
4025
4026/*
4027 * Reset the text's hard formatting
4028 */
4032bool DocumentContentOperationsManager::lcl_RstTextAttr( SwNode* pNd, void* pArgs )
4033{
4034 ParaRstFormat* pPara = static_cast<ParaRstFormat*>(pArgs);
4035 if (pPara->pLayout && pPara->pLayout->HasMergedParas()
4037 {
4038 return true; // skip hidden, since new items aren't applied
4039 }
4040 SwTextNode * pTextNode = pNd->GetTextNode();
4041 if( pTextNode && pTextNode->GetpSwpHints() )
4042 {
4043 SwContentIndex aSt( pTextNode, 0 );
4044 sal_Int32 nEnd = pTextNode->Len();
4045
4046 if( &pPara->pSttNd->GetNode() == pTextNode &&
4047 pPara->pSttNd->GetContentIndex() )
4048 aSt = pPara->pSttNd->GetContentIndex();
4049
4050 if( &pPara->pEndNd->GetNode() == pNd )
4051 nEnd = pPara->pEndNd->GetContentIndex();
4052
4053 if( pPara->pHistory )
4054 {
4055 // Save all attributes for the Undo.
4056 SwRegHistory aRHst( *pTextNode, pPara->pHistory );
4057 pTextNode->GetpSwpHints()->Register( &aRHst );
4058 pTextNode->RstTextAttr( aSt.GetIndex(), nEnd - aSt.GetIndex(), pPara->nWhich,
4059 pPara->pDelSet, pPara->bInclRefToxMark, pPara->bExactRange );
4060 if( pTextNode->GetpSwpHints() )
4061 pTextNode->GetpSwpHints()->DeRegister();
4062 }
4063 else
4064 pTextNode->RstTextAttr( aSt.GetIndex(), nEnd - aSt.GetIndex(), pPara->nWhich,
4065 pPara->pDelSet, pPara->bInclRefToxMark, pPara->bExactRange );
4066 }
4067 return true;
4068}
4069
4070DocumentContentOperationsManager::~DocumentContentOperationsManager()
4071{
4072}
4073//Private methods
4074
4075bool DocumentContentOperationsManager::DeleteAndJoinWithRedlineImpl(SwPaM & rPam, SwDeleteFlags const flags)
4076{
4078
4080
4081 if (*rPam.GetPoint() == *rPam.GetMark())
4082 {
4083 return false; // do not add empty redlines
4084 }
4085
4086 std::vector<std::unique_ptr<SwRangeRedline>> redlines;
4087 {
4088 auto pRedline(std::make_unique<SwRangeRedline>(RedlineType::Delete, rPam));
4089 if (pRedline->HasValidRange())
4090 {
4091 redlines.push_back(std::move(pRedline));
4092 }
4093 else // sigh ... why is such a selection even possible...
4094 { // split it up so we get one SwUndoRedlineDelete per inserted RL
4095 redlines = GetAllValidRanges(std::move(pRedline));
4096 }
4097 }
4098
4099 if (redlines.empty())
4100 {
4101 return false;
4102 }
4103
4104 // tdf#54819 current redlining needs also modification of paragraph style and
4105 // attributes added to the same grouped Undo
4108
4109 auto & rDMA(*m_rDoc.getIDocumentMarkAccess());
4110 std::vector<std::unique_ptr<SwUndo>> MarkUndos;
4111 for (auto iter = rDMA.getAnnotationMarksBegin();
4112 iter != rDMA.getAnnotationMarksEnd(); )
4113 {
4114 // tdf#111524 remove annotation marks that have their field
4115 // characters deleted
4116 SwPosition const& rEndPos((**iter).GetMarkEnd());
4117 if (*rPam.Start() < rEndPos && rEndPos <= *rPam.End())
4118 {
4120 {
4121 MarkUndos.emplace_back(std::make_unique<SwUndoDeleteBookmark>(**iter));
4122 }
4123 // iter is into annotation mark vector so must be dereferenced!
4124 rDMA.deleteMark(&**iter);
4125 // this invalidates iter, have to start over...
4126 iter = rDMA.getAnnotationMarksBegin();
4127 }
4128 else
4129 { // marks are sorted by start
4130 if (*rPam.End() < (**iter).GetMarkStart())
4131 {
4132 break;
4133 }
4134 ++iter;
4135 }
4136 }
4137
4138 // tdf#119019 accept tracked paragraph formatting to do not hide new deletions
4139 if (*rPam.GetPoint() != *rPam.GetMark())
4141
4142 std::vector<std::unique_ptr<SwUndoRedlineDelete>> undos;
4144 {
4145 // this should no longer happen in calls from the UI but maybe via API
4146 // (randomTest and testTdf54819 triggers it)
4148 "sw.core", "redlines will be moved in DeleteAndJoin");
4151
4152 for (std::unique_ptr<SwRangeRedline> & pRedline : redlines)
4153 {
4154 assert(pRedline->HasValidRange());
4155 undos.emplace_back(std::make_unique<SwUndoRedlineDelete>(
4156 *pRedline, SwUndoId::DELETE, flags));
4157 }
4158 const SwRewriter aRewriter = undos.front()->GetRewriter();
4159 // can only group a single undo action
4160 if (MarkUndos.empty() && undos.size() == 1
4162 {
4163 SwUndo * const pLastUndo( m_rDoc.GetUndoManager().GetLastUndo() );
4164 SwUndoRedlineDelete *const pUndoRedlineDel(dynamic_cast<SwUndoRedlineDelete*>(pLastUndo));
4165 bool const bMerged = pUndoRedlineDel
4166 && pUndoRedlineDel->CanGrouping(*undos.front());
4167 if (!bMerged)
4168 {
4169 m_rDoc.GetIDocumentUndoRedo().AppendUndo(std::move(undos.front()));
4170 }
4171 undos.clear(); // prevent unmatched EndUndo
4172 }
4173 else
4174 {
4176 for (auto& it : MarkUndos)
4177 {
4178 m_rDoc.GetIDocumentUndoRedo().AppendUndo(std::move(it));
4179 }
4180 for (auto & it : undos)
4181 {
4182 m_rDoc.GetIDocumentUndoRedo().AppendUndo(std::move(it));
4183 }
4184 }
4185 }
4186
4187 for (std::unique_ptr<SwRangeRedline> & pRedline : redlines)
4188 {
4189 // note: 1. the pRedline can still be merged & deleted
4190 // 2. the impl. can even DeleteAndJoin the range => no plain PaM
4191 std::shared_ptr<SwUnoCursor> const pCursor(m_rDoc.CreateUnoCursor(*pRedline->GetMark()));
4192 pCursor->SetMark();
4193 *pCursor->GetPoint() = *pRedline->GetPoint();
4194 m_rDoc.getIDocumentRedlineAccess().AppendRedline(pRedline.release(), true);
4195 // sw_redlinehide: 2 reasons why this is needed:
4196 // 1. it's the first redline in node => RedlineDelText was sent but ignored
4197 // 2. redline spans multiple nodes => must merge text frames
4199 }
4201
4203 {
4204 if (!undos.empty())
4205 {
4207 }
4209 }
4210
4213
4214 return true;
4215}
4216
4217bool DocumentContentOperationsManager::DeleteAndJoinImpl(SwPaM & rPam, SwDeleteFlags const flags)
4218{
4219 bool bJoinText, bJoinPrev;
4220 ::sw_GetJoinFlags( rPam, bJoinText, bJoinPrev );
4221
4222 bool const bSuccess( DeleteRangeImpl(rPam, flags) );
4223 if (!bSuccess)
4224 return false;
4225
4226 if( bJoinText )
4227 {
4228 ::sw_JoinText( rPam, bJoinPrev );
4229 }
4230
4233 {
4235 }
4236
4237 return true;
4238}
4239
4240bool DocumentContentOperationsManager::DeleteRangeImpl(SwPaM & rPam, SwDeleteFlags const flags)
4241{
4242 // Move all cursors out of the deleted range, but first copy the
4243 // passed PaM, because it could be a cursor that would be moved!
4244 SwPaM aDelPam( *rPam.GetMark(), *rPam.GetPoint() );
4245 ::PaMCorrAbs( aDelPam, *aDelPam.GetPoint() );
4246
4247 bool const bSuccess( DeleteRangeImplImpl(aDelPam, flags) );
4248 if (bSuccess)
4249 { // now copy position from temp copy to given PaM
4250 *rPam.GetPoint() = *aDelPam.GetPoint();
4251 }
4252
4253 return bSuccess;
4254}
4255
4256bool DocumentContentOperationsManager::DeleteRangeImplImpl(SwPaM & rPam, SwDeleteFlags const flags)
4257{
4258 auto [pStt, pEnd] = rPam.StartEnd(); // SwPosition*
4259
4260 if (!rPam.HasMark()
4261 || (*pStt == *pEnd && !IsFlySelectedByCursor(m_rDoc, *pStt, *pEnd)))
4262 {
4263 return false;
4264 }
4265
4267 {
4268 // if necessary the saved Word for the exception
4269 if( m_rDoc.GetAutoCorrExceptWord()->IsDeleted() || pStt->GetNode() != pEnd->GetNode() ||
4270 pStt->GetContentIndex() + 1 != pEnd->GetContentIndex() ||
4273 }
4274
4275 {
4276 // Delete all empty TextHints at the Mark's position
4277 SwTextNode* pTextNd = rPam.GetMark()->GetNode().GetTextNode();
4278 SwpHints* pHts;
4279 if( pTextNd && nullptr != ( pHts = pTextNd->GetpSwpHints()) && pHts->Count() )
4280 {
4281 const sal_Int32 nMkCntPos = rPam.GetMark()->GetContentIndex();
4282 for( size_t n = pHts->Count(); n; )
4283 {
4284 const SwTextAttr* pAttr = pHts->Get( --n );
4285 if( nMkCntPos > pAttr->GetStart() )
4286 break;
4287
4288 const sal_Int32 *pEndIdx;
4289 if( nMkCntPos == pAttr->GetStart() &&
4290 nullptr != (pEndIdx = pAttr->End()) &&
4291 *pEndIdx == pAttr->GetStart() )
4292 pTextNd->DestroyAttr( pHts->Cut( n ) );
4293 }
4294 }
4295 }
4296
4297 {
4298 // Send DataChanged before deletion, so that we still know
4299 // which objects are in the range.
4300 // Afterwards they could be before/after the Position.
4301 SwDataChanged aTmp( rPam );
4302 }
4303
4305 {
4307 bool bMerged(false);
4309 {
4310 SwUndo *const pLastUndo( m_rDoc.GetUndoManager().GetLastUndo() );
4311 SwUndoDelete *const pUndoDelete(
4312 dynamic_cast<SwUndoDelete *>(pLastUndo) );
4313 if (pUndoDelete)
4314 {
4315 bMerged = pUndoDelete->CanGrouping(m_rDoc, rPam);
4316 // if CanGrouping() returns true it's already merged
4317 }
4318 }
4319 if (!bMerged)
4320 {
4321 m_rDoc.GetIDocumentUndoRedo().AppendUndo(std::make_unique<SwUndoDelete>(rPam, flags));
4322 }
4323
4325
4326 return true;
4327 }
4328
4330 m_rDoc.getIDocumentRedlineAccess().DeleteRedline( rPam, true, RedlineType::Any );
4331
4332 // Delete and move all "Flys at the paragraph", which are within the Selection
4334 {
4335 DelFlyInRange(rPam.GetMark()->GetNode(), rPam.GetPoint()->GetNode(),
4336 rPam.GetMark()->GetContentIndex(), rPam.GetPoint()->GetContentIndex());
4337 }
4339 pStt->GetNode(),
4340 pEnd->GetNode(),
4341 nullptr,
4342 pStt->GetContentIndex(),
4343 pEnd->GetContentIndex());
4344
4345 SwNodeIndex aSttIdx( pStt->GetNode() );
4346 SwContentNode * pCNd = aSttIdx.GetNode().GetContentNode();
4347
4348 do { // middle checked loop!
4349 if( pCNd )
4350 {
4351 SwTextNode * pStartTextNode( pCNd->GetTextNode() );
4352 if ( pStartTextNode )
4353 {
4354 // now move the Content to the new Node
4355 bool bOneNd = pStt->GetNode() == pEnd->GetNode();
4356 const sal_Int32 nLen = ( bOneNd ? pEnd->GetContentIndex()
4357 : pCNd->Len() )
4358 - pStt->GetContentIndex();
4359
4360 // Don't call again, if already empty
4361 if( nLen )
4362 {
4363 pStartTextNode->EraseText( *pStt, nLen );
4364
4365 if( !pStartTextNode->Len() )
4366 {
4367 // METADATA: remove reference if empty (consider node deleted)
4368 pStartTextNode->RemoveMetadataReference();
4369 }
4370 }
4371
4372 if( bOneNd ) // that's it
4373 break;
4374
4375 ++aSttIdx;
4376 }
4377 else
4378 {
4379 // So that there are no indices left registered when deleted,
4380 // we remove a SwPaM from the Content here.
4381 pStt->nContent.Assign( nullptr, 0 );
4382 }
4383 }
4384
4385 pCNd = pEnd->GetNode().GetContentNode();
4386 if( pCNd )
4387 {
4388 SwTextNode * pEndTextNode( pCNd->GetTextNode() );
4389 if( pEndTextNode )
4390 {
4391 // if already empty, don't call again
4392 if( pEnd->GetContentIndex() )
4393 {
4394 SwContentIndex aIdx( pCNd, 0 );
4395 pEndTextNode->EraseText( aIdx, pEnd->GetContentIndex() );
4396
4397 if( !pEndTextNode->Len() )
4398 {
4399 // METADATA: remove reference if empty (consider node deleted)
4400 pEndTextNode->RemoveMetadataReference();
4401 }
4402 }
4403 }
4404 else
4405 {
4406 // So that there are no indices left registered when deleted,
4407 // we remove a SwPaM from the Content here.
4408 pEnd->nContent.Assign( nullptr, 0 );
4409 }
4410 }
4411
4412 // if the end is not a content node, delete it as well
4413 SwNodeOffset nEnd = pEnd->GetNodeIndex();
4414 if( pCNd == nullptr )
4415 nEnd++;
4416
4417 if( aSttIdx != nEnd )
4418 {
4419 // tdf#134436 delete section nodes like SwUndoDelete::SwUndoDelete
4420 SwNode *pTmpNd;
4421 while (pEnd == rPam.GetPoint()
4422 && nEnd + SwNodeOffset(2) < m_rDoc.GetNodes().Count()
4423 && (pTmpNd = m_rDoc.GetNodes()[nEnd + 1])->IsEndNode()
4424 && pTmpNd->StartOfSectionNode()->IsSectionNode()
4425 && aSttIdx <= pTmpNd->StartOfSectionNode()->GetIndex())
4426 {
4427 SwNodeRange range(*pTmpNd->StartOfSectionNode(), *pTmpNd);
4428 m_rDoc.GetNodes().SectionUp(&range);
4429 --nEnd; // account for deleted start node
4430 }
4431
4432 // delete the Nodes from the NodesArray
4433 m_rDoc.GetNodes().Delete( aSttIdx, nEnd - aSttIdx.GetIndex() );
4434 }
4435
4436 // If the Node that contained the Cursor has been deleted,
4437 // the Content has to be assigned to the current Content.
4438 if (pStt->GetNode().GetContentNode())
4439 pStt->SetContent( pStt->GetContentIndex() );
4440
4441 // If we deleted across Node boundaries we have to correct the PaM,
4442 // because they are in different Nodes now.
4443 // Also, the Selection is revoked.
4444 *pEnd = *pStt;
4445 rPam.DeleteMark();
4446
4447 } while( false );
4448
4450
4451 return true;
4452}
4453
4454// It's possible to call Replace with a PaM that spans 2 paragraphs:
4455// search with regex for "$", then replace _all_
4456bool DocumentContentOperationsManager::ReplaceRangeImpl( SwPaM& rPam, const OUString& rStr,
4457 const bool bRegExReplace )
4458{
4459 if (!rPam.HasMark())
4460 return false;
4461
4462 bool bJoinText, bJoinPrev;
4463 ::sw_GetJoinFlags( rPam, bJoinText, bJoinPrev );
4464
4465 {
4466 // Create a copy of the Cursor in order to move all Pams from
4467 // the other views out of the deletion range.
4468 // Except for itself!
4469 SwPaM aDelPam( *rPam.GetMark(), *rPam.GetPoint() );
4470 ::PaMCorrAbs( aDelPam, *aDelPam.GetPoint() );
4471
4472 auto [pStt, pEnd] = aDelPam.StartEnd(); // SwPosition*
4473 bool bOneNode = pStt->GetNode() == pEnd->GetNode();
4474
4475 // Own Undo?
4476 OUString sRepl( rStr );
4477 SwTextNode* pTextNd = pStt->GetNode().GetTextNode();
4478 sal_Int32 nStt = pStt->GetContentIndex();
4479 sal_Int32 nEnd;
4480
4481 SwDataChanged aTmp( aDelPam );
4482
4484 {
4487 {
4488 // this should no longer happen in calls from the UI but maybe via API
4490 "sw.core", "redlines will be moved in ReplaceRange");
4491
4493
4494 // If any Redline will change (split!) the node
4495 const ::sw::mark::IMark* pBkmk =
4498 ::sw::mark::InsertMode::New);
4499
4502
4503 *aDelPam.GetPoint() = pBkmk->GetMarkPos();
4504 if(pBkmk->IsExpanded())
4505 *aDelPam.GetMark() = pBkmk->GetOtherMarkPos();
4507 pStt = aDelPam.Start();
4508 pTextNd = pStt->GetNode().GetTextNode();
4509 nStt = pStt->GetContentIndex();
4510 }
4511
4512 if( !sRepl.isEmpty() )
4513 {
4514 // Apply the first character's attributes to the ReplaceText
4518 pTextNd->GetParaAttr( aSet, nStt+1, nStt+1 );
4519
4520 aSet.ClearItem( RES_TXTATR_REFMARK );
4521 aSet.ClearItem( RES_TXTATR_TOXMARK );
4522 aSet.ClearItem( RES_TXTATR_CJK_RUBY );
4523 aSet.ClearItem( RES_TXTATR_INETFMT );
4524 aSet.ClearItem( RES_TXTATR_META );
4525 aSet.ClearItem( RES_TXTATR_METAFIELD );
4526
4527 if( aDelPam.GetPoint() != aDelPam.End() )
4528 aDelPam.Exchange();
4529
4530 // Remember the End
4531 SwNodeIndex aPtNd( aDelPam.GetPoint()->GetNode(), -1 );
4532 const sal_Int32 nPtCnt = aDelPam.GetPoint()->GetContentIndex();
4533
4534 bool bFirst = true;
4535 OUString sIns;
4536 while ( lcl_GetTokenToParaBreak( sRepl, sIns, bRegExReplace ) )
4537 {
4538 InsertString( aDelPam, sIns );
4539 if( bFirst )
4540 {
4541 SwNodeIndex aMkNd( aDelPam.GetMark()->GetNode(), -1 );
4542 const sal_Int32 nMkCnt = aDelPam.GetMark()->GetContentIndex();
4543
4544 SplitNode( *aDelPam.GetPoint(), false );
4545
4546 ++aMkNd;
4547 aDelPam.GetMark()->Assign( aMkNd, nMkCnt );
4548 bFirst = false;
4549 }
4550 else
4551 SplitNode( *aDelPam.GetPoint(), false );
4552 }
4553 if( !sIns.isEmpty() )
4554 {
4555 InsertString( aDelPam, sIns );
4556 }
4557
4558 SwPaM aTmpRange( *aDelPam.GetPoint() );
4559 aTmpRange.SetMark();
4560
4561 ++aPtNd;
4562 aDelPam.GetPoint()->Assign(aPtNd, nPtCnt);
4563 *aTmpRange.GetMark() = *aDelPam.GetPoint();
4564
4565 m_rDoc.RstTextAttrs( aTmpRange );
4566 InsertItemSet( aTmpRange, aSet );
4567 }
4568
4569 // tdf#139982: Appending the redline may immediately delete flys
4570 // anchored in the previous text if it's inside an insert redline.
4571 // Also flys will be deleted if the redline is accepted. Move them
4572 // to the position between the previous text and the new text,
4573 // there the chance of surviving both accept and reject is best.
4574 SaveFlyArr flys;
4575 SaveFlyInRange(aDelPam, *aDelPam.End(), flys, false);
4576
4578 {
4580 std::make_unique<SwUndoRedlineDelete>( aDelPam, SwUndoId::REPLACE ));
4581 }
4582 // add redline similar to DeleteAndJoinWithRedlineImpl()
4583 std::shared_ptr<SwUnoCursor> const pCursor(m_rDoc.CreateUnoCursor(*aDelPam.GetMark()));
4584 pCursor->SetMark();
4585 *pCursor->GetPoint() = *aDelPam.GetPoint();
4586 m_rDoc.getIDocumentRedlineAccess().AppendRedline( new SwRangeRedline( RedlineType::Delete, aDelPam ), true);
4587 RestFlyInRange(flys, *aDelPam.End(), &aDelPam.End()->GetNode(), true);
4589
4590 *rPam.GetMark() = *aDelPam.GetMark();
4592 {
4593 *aDelPam.GetPoint() = *rPam.GetPoint();
4595
4596 // If any Redline will change (split!) the node
4597 const ::sw::mark::IMark* pBkmk =
4600 ::sw::mark::InsertMode::New);
4601
4602 aDelPam.GetPoint()->Assign( SwNodeOffset(0) );
4603 aDelPam.GetMark()->Assign( SwNodeOffset(0) );
4604 rPam.GetPoint()->Assign( SwNodeOffset(0) );
4605 *rPam.GetMark() = *rPam.GetPoint();
4607
4608 *rPam.GetPoint() = pBkmk->GetMarkPos();
4609 *rPam.GetMark() = pBkmk->IsExpanded() ? pBkmk->GetOtherMarkPos() : pBkmk->GetMarkPos();
4610
4612 }
4613 bJoinText = false;
4614 }
4615 else
4616 {
4617 assert((pStt->GetNode() == pEnd->GetNode() ||
4618 ( pStt->GetNodeIndex() + 1 == pEnd->GetNodeIndex() &&
4619 !pEnd->GetContentIndex() )) &&
4620 "invalid range: Point and Mark on different nodes" );
4621
4623 m_rDoc.getIDocumentRedlineAccess().DeleteRedline( aDelPam, true, RedlineType::Any );
4624
4625 SwUndoReplace* pUndoRpl = nullptr;
4626 bool const bDoesUndo = m_rDoc.GetIDocumentUndoRedo().DoesUndo();
4627 if (bDoesUndo)
4628 {
4629 pUndoRpl = new SwUndoReplace(aDelPam, sRepl, bRegExReplace);
4630 m_rDoc.GetIDocumentUndoRedo().AppendUndo(std::unique_ptr<SwUndo>(pUndoRpl));
4631 }
4632 ::sw::UndoGuard const undoGuard(m_rDoc.GetIDocumentUndoRedo());
4633
4634 if( aDelPam.GetPoint() != pStt )
4635 aDelPam.Exchange();
4636
4637 SwNodeIndex aPtNd( pStt->GetNode(), -1 );
4638 const sal_Int32 nPtCnt = pStt->GetContentIndex();
4639
4640 // Set the values again, if Frames or footnotes on the Text have been removed.
4641 nStt = nPtCnt;
4642 nEnd = bOneNode ? pEnd->GetContentIndex()
4643 : pTextNd->GetText().getLength();
4644
4645 bool bFirst = true;
4646 OUString sIns;
4647 while ( lcl_GetTokenToParaBreak( sRepl, sIns, bRegExReplace ) )
4648 {
4649 if (!bFirst || nStt == pTextNd->GetText().getLength())
4650 {
4651 InsertString( aDelPam, sIns );
4652 }
4653 else if( nStt < nEnd || !sIns.isEmpty() )
4654 {
4655 pTextNd->ReplaceText( *pStt, nEnd - nStt, sIns );
4656 }
4657 SplitNode( *pStt, false);
4658 bFirst = false;
4659 }
4660
4661 if( bFirst || !sIns.isEmpty() )
4662 {
4663 if (!bFirst || nStt == pTextNd->GetText().getLength())
4664 {
4665 InsertString( aDelPam, sIns );
4666 }
4667 else if( nStt < nEnd || !sIns.isEmpty() )
4668 {
4669 pTextNd->ReplaceText( *pStt, nEnd - nStt, sIns );
4670 }
4671 }
4672
4673 *rPam.GetPoint() = *aDelPam.GetMark();
4674 ++aPtNd;
4675 rPam.GetMark()->Assign( aPtNd, nPtCnt );
4676
4677 if (bJoinText)
4678 {
4679 assert(rPam.GetPoint() == rPam.End());
4680 // move so that SetEnd remembers position after sw_JoinText
4681 rPam.Move(fnMoveBackward);
4682 }
4683 else if (aDelPam.GetPoint() == pStt) // backward selection?
4684 {
4685 assert(*rPam.GetMark() <= *rPam.GetPoint());
4686 rPam.Exchange(); // swap so that rPam is backwards
4687 }
4688
4689 if( pUndoRpl )
4690 {
4691 pUndoRpl->SetEnd(rPam);
4692 }
4693 }
4694 }
4695
4696 bool bRet(true);
4697 if (bJoinText)
4698 {
4699 bRet = ::sw_JoinText(rPam, bJoinPrev);
4700 }
4701
4703 return bRet;
4704}
4705
4706SwFlyFrameFormat* DocumentContentOperationsManager::InsNoTextNode( const SwPosition& rPos, SwNoTextNode* pNode,
4707 const SfxItemSet* pFlyAttrSet,
4708 const SfxItemSet* pGrfAttrSet,
4709 SwFrameFormat* pFrameFormat)
4710{
4711 SwFlyFrameFormat *pFormat = nullptr;
4712 if( pNode )
4713 {
4714 pFormat = m_rDoc.MakeFlySection_( rPos, *pNode, RndStdIds::FLY_AT_PARA,
4715 pFlyAttrSet, pFrameFormat );
4716 if( pGrfAttrSet )
4717 pNode->SetAttr( *pGrfAttrSet );
4718 }
4719 return pFormat;
4720}
4721
4722#define NUMRULE_STATE \
4723 std::shared_ptr<SwNumRuleItem> aNumRuleItemHolderIfSet; \
4724 std::shared_ptr<SfxStringItem> aListIdItemHolderIfSet; \
4725
4726#define PUSH_NUMRULE_STATE \
4727 lcl_PushNumruleState( aNumRuleItemHolderIfSet, aListIdItemHolderIfSet, pDestTextNd );
4728
4729#define POP_NUMRULE_STATE \
4730 lcl_PopNumruleState( aNumRuleItemHolderIfSet, aListIdItemHolderIfSet, pDestTextNd, rPam );
4731
4733 std::shared_ptr<SwNumRuleItem>& aNumRuleItemHolderIfSet,
4734 std::shared_ptr<SfxStringItem>& aListIdItemHolderIfSet,
4735 const SwTextNode *pDestTextNd )
4736{
4737 // Safe numrule item at destination.
4738 // #i86492# - Safe also <ListId> item of destination.
4739 const SfxItemSet * pAttrSet = pDestTextNd->GetpSwAttrSet();
4740 if (pAttrSet == nullptr)
4741 return;
4742
4743 if (const SwNumRuleItem* pItem = pAttrSet->GetItemIfSet(RES_PARATR_NUMRULE, false))
4744 {
4745 aNumRuleItemHolderIfSet.reset(pItem->Clone());
4746 }
4747
4748 if (const SfxStringItem* pItem = pAttrSet->GetItemIfSet(RES_PARATR_LIST_ID, false))
4749 {
4750 aListIdItemHolderIfSet.reset(pItem->Clone());
4751 }
4752}
4753
4755 const std::shared_ptr<SwNumRuleItem>& aNumRuleItemHolderIfSet,
4756 const std::shared_ptr<SfxStringItem>& aListIdItemHolderIfSet,
4757 SwTextNode *pDestTextNd, const SwPaM& rPam )
4758{
4759 /* If only a part of one paragraph is copied
4760 restore the numrule at the destination. */
4761 // #i86492# - restore also <ListId> item
4762 if ( lcl_MarksWholeNode(rPam) )
4763 return;
4764
4765 if (aNumRuleItemHolderIfSet)
4766 {
4767 pDestTextNd->SetAttr(*aNumRuleItemHolderIfSet);
4768 }
4769 else
4770 {
4771 pDestTextNd->ResetAttr(RES_PARATR_NUMRULE);
4772 }
4773
4774 if (aListIdItemHolderIfSet)
4775 {
4776 pDestTextNd->SetAttr(*aListIdItemHolderIfSet);
4777 }
4778 else
4779 {
4780 pDestTextNd->ResetAttr(RES_PARATR_LIST_ID);
4781 }
4782}
4783
4784bool DocumentContentOperationsManager::CopyImpl(SwPaM& rPam, SwPosition& rPos,
4785 SwCopyFlags const flags,
4786 SwPaM *const pCopyRange) const
4787{
4788 std::vector<std::pair<SwNodeOffset, sal_Int32>> Breaks;
4789
4790 sw::CalcBreaks(Breaks, rPam, true);
4791
4792 if (Breaks.empty())
4793 {
4794 return CopyImplImpl(rPam, rPos, flags, pCopyRange);
4795 }
4796
4797 SwPosition const & rSelectionEnd( *rPam.End() );
4798
4799 bool bRet(true);
4800 bool bFirst(true);
4801 // iterate from end to start, ... don't think it's necessary here?
4802 auto iter( Breaks.rbegin() );
4803 SwNodeOffset nOffset(0);
4804 SwNodes const& rNodes(rPam.GetPoint()->GetNodes());
4805 SwPaM aPam( rSelectionEnd, rSelectionEnd ); // end node!
4806 SwPosition & rEnd( *aPam.End() );
4807 SwPosition & rStart( *aPam.Start() );
4808 SwPaM copyRange(rPos, rPos);
4809
4810 while (iter != Breaks.rend())
4811 {
4812 rStart.Assign(*rNodes[iter->first - nOffset]->GetTextNode(), iter->second + 1);
4813 if (rStart < rEnd) // check if part is empty
4814 {
4815 // pass in copyRange member as rPos; should work ...
4816 bRet &= CopyImplImpl(aPam, *copyRange.Start(), flags & ~SwCopyFlags::IsMoveToFly, &copyRange);
4817 nOffset = iter->first - rStart.GetNodeIndex(); // fly nodes...
4818 if (pCopyRange)
4819 {
4820 if (bFirst)
4821 {
4822 pCopyRange->SetMark();
4823 *pCopyRange->GetMark() = *copyRange.End();
4824 }
4825 *pCopyRange->GetPoint() = *copyRange.Start();
4826 }
4827 bFirst = false;
4828 }
4829 rEnd.Assign(*rNodes[iter->first - nOffset]->GetTextNode(), iter->second);
4830 ++iter;
4831 }
4832
4833 rStart = *rPam.Start(); // set to original start
4834 if (rStart < rEnd) // check if part is empty
4835 {
4836 bRet &= CopyImplImpl(aPam, *copyRange.Start(), flags & ~SwCopyFlags::IsMoveToFly, &copyRange);
4837 if (pCopyRange)
4838 {
4839 if (bFirst)
4840 {
4841 pCopyRange->SetMark();
4842 *pCopyRange->GetMark() = *copyRange.End();
4843 }
4844 *pCopyRange->GetPoint() = *copyRange.Start();
4845 }
4846 }
4847
4848 return bRet;
4849}
4850
4851bool DocumentContentOperationsManager::CopyImplImpl(SwPaM& rPam, SwPosition& rPos,
4852 SwCopyFlags const flags,
4853 SwPaM *const pCpyRange) const
4854{
4855 SwDoc& rDoc = rPos.GetNode().GetDoc();
4856 const bool bColumnSel = rDoc.IsClipBoard() && rDoc.IsColumnSelection();
4857
4858 auto [pStt, pEnd] = rPam.StartEnd(); // SwPosition*
4859
4860 // Catch when there's no copy to do.
4861 if (!rPam.HasMark() || (IsEmptyRange(*pStt, *pEnd, flags) && !bColumnSel) ||
4862 //JP 29.6.2001: 88963 - don't copy if inspos is in region of start to end
4863 //JP 15.11.2001: don't test inclusive the end, ever exclusive
4864 ( &rDoc == &m_rDoc && *pStt <= rPos && rPos < *pEnd ))
4865 {
4866 return false;
4867 }
4868
4869 const bool bEndEqualIns = &rDoc == &m_rDoc && rPos == *pEnd;
4870
4871 // If Undo is enabled, create the UndoCopy object
4872 SwUndoCpyDoc* pUndo = nullptr;
4873 // lcl_DeleteRedlines may delete the start or end node of the cursor when
4874 // removing the redlines so use cursor that is corrected by PaMCorrAbs
4875 std::shared_ptr<SwUnoCursor> const pCopyPam(rDoc.CreateUnoCursor(rPos));
4876
4877 SwTableNumFormatMerge aTNFM( m_rDoc, rDoc );
4878 std::optional<std::vector<SwFrameFormat*>> pFlys;
4879 std::vector<SwFrameFormat*> const* pFlysAtInsPos;
4880
4881 if (rDoc.GetIDocumentUndoRedo().DoesUndo())
4882 {
4883 pUndo = new SwUndoCpyDoc(*pCopyPam);
4884 pFlysAtInsPos = pUndo->GetFlysAnchoredAt();
4885 }
4886 else
4887 {
4888 pFlys = sw::GetFlysAnchoredAt(rDoc, rPos.GetNodeIndex());
4889 pFlysAtInsPos = pFlys ? &*pFlys : nullptr;
4890 }
4891
4894
4895 // Move the PaM one node back from the insert position, so that
4896 // the position doesn't get moved
4897 pCopyPam->SetMark();
4898 bool bCanMoveBack = pCopyPam->Move(fnMoveBackward, GoInContent);
4899 // If the position was shifted from more than one node, an end node has been skipped
4900 bool bAfterTable = false;
4901 if ((rPos.GetNodeIndex() - pCopyPam->GetPoint()->GetNodeIndex()) > SwNodeOffset(1))
4902 {
4903 // First go back to the original place
4904 *(pCopyPam->GetPoint()) = rPos;
4905
4906 bCanMoveBack = false;
4907 bAfterTable = true;
4908 }
4909 if( !bCanMoveBack )
4910 {
4911 pCopyPam->GetPoint()->Adjust(SwNodeOffset(-1));
4912 assert(pCopyPam->GetPoint()->GetContentIndex() == 0);
4913 }
4914
4915 SwNodeRange aRg( pStt->GetNode(), pEnd->GetNode() );
4916 SwNodeIndex aInsPos( rPos.GetNode() );
4917 const bool bOneNode = pStt->GetNode() == pEnd->GetNode();
4918 SwTextNode* pSttTextNd = pStt->GetNode().GetTextNode();
4919 SwTextNode* pEndTextNd = pEnd->GetNode().GetTextNode();
4920 SwTextNode* pDestTextNd = aInsPos.GetNode().GetTextNode();
4921 bool bCopyCollFormat = !rDoc.IsInsOnlyTextGlossary() &&
4922 ( (pDestTextNd && !pDestTextNd->GetText().getLength()) ||
4923 ( !bOneNode && !rPos.GetContentIndex() ) );
4924 bool bCopyBookmarks = true;
4925 bool bCopyPageSource = false;
4926 SwNodeOffset nDeleteTextNodes(0);
4927
4928 // #i104585# copy outline num rule to clipboard (for ASCII filter)
4929 if (rDoc.IsClipBoard() && m_rDoc.GetOutlineNumRule())
4930 {
4932 }
4933
4934 // #i86492#
4935 // Correct the search for a previous list:
4936 // First search for non-outline numbering list. Then search for non-outline
4937 // bullet list.
4938 // Keep also the <ListId> value for possible propagation.
4939 OUString aListIdToPropagate;
4940 const SwNumRule* pNumRuleToPropagate =
4941 rDoc.SearchNumRule( rPos, false, true, false, 0, aListIdToPropagate, nullptr, true );
4942 if ( !pNumRuleToPropagate )
4943 {
4944 pNumRuleToPropagate =
4945 rDoc.SearchNumRule( rPos, false, false, false, 0, aListIdToPropagate, nullptr, true );
4946 }
4947 // #i86492#
4948 // Do not propagate previous found list, if
4949 // - destination is an empty paragraph which is not in a list and
4950 // - source contains at least one paragraph which is not in a list
4951 if ( pNumRuleToPropagate &&
4952 pDestTextNd && !pDestTextNd->GetText().getLength() &&
4953 !pDestTextNd->IsInList() &&
4954 !lcl_ContainsOnlyParagraphsInList( rPam ) )
4955 {
4956 pNumRuleToPropagate = nullptr;
4957 }
4958
4959 // This do/while block is only there so that we can break out of it!
4960 do {
4961 if( pSttTextNd )
4962 {
4963 ++nDeleteTextNodes; // must be joined in Undo
4964 // Don't copy the beginning completely?
4965 if( !bCopyCollFormat || bColumnSel || pStt->GetContentIndex() )
4966 {
4967 SwContentIndex aDestIdx( rPos.GetContentNode(), rPos.GetContentIndex() );
4968 bool bCopyOk = false;
4969 if( !pDestTextNd )
4970 {
4971 if( pStt->GetContentIndex() || bOneNode )
4972 pDestTextNd = rDoc.GetNodes().MakeTextNode( aInsPos.GetNode(),
4974 else
4975 {
4976 pDestTextNd = pSttTextNd->MakeCopy(rDoc, aInsPos.GetNode(), true)->GetTextNode();
4977 bCopyOk = true;
4978 }
4979 aDestIdx.Assign( pDestTextNd, 0 );
4980 bCopyCollFormat = true;
4981 }
4982 else if( !bOneNode || bColumnSel )
4983 {
4984 const sal_Int32 nContentEnd = pEnd->GetContentIndex();
4985 {
4986 ::sw::UndoGuard const ug(rDoc.GetIDocumentUndoRedo());
4987 rDoc.getIDocumentContentOperations().SplitNode( rPos, false );
4988 }
4989
4990 if (bCanMoveBack && rPos == *pCopyPam->GetPoint())
4991 {
4992 // after the SplitNode, span the CpyPam correctly again
4993 pCopyPam->Move( fnMoveBackward, GoInContent );
4994 pCopyPam->Move( fnMoveBackward, GoInContent );
4995 }
4996
4997 pDestTextNd = rDoc.GetNodes()[ aInsPos.GetIndex()-SwNodeOffset(1) ]->GetTextNode();
4998 aDestIdx.Assign(
4999 pDestTextNd, pDestTextNd->GetText().getLength());
5000
5001 // Correct the area again
5002 if( bEndEqualIns )
5003 {
5004 bool bChg = pEnd != rPam.GetPoint();
5005 if( bChg )
5006 rPam.Exchange();
5008 if( bChg )
5009 rPam.Exchange();