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