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>
27 #include <UndoManager.hxx>
28 #include <docary.hxx>
29 #include <textboxhelper.hxx>
30 #include <dcontact.hxx>
31 #include <grfatr.hxx>
32 #include <numrule.hxx>
33 #include <charfmt.hxx>
34 #include <ndgrf.hxx>
35 #include <ndnotxt.hxx>
36 #include <ndole.hxx>
37 #include <breakit.hxx>
38 #include <frmfmt.hxx>
39 #include <fmtanchr.hxx>
40 #include <fmtcntnt.hxx>
41 #include <fmtinfmt.hxx>
42 #include <fmtpdsc.hxx>
43 #include <fmtcnct.hxx>
44 #include <SwStyleNameMapper.hxx>
45 #include <redline.hxx>
46 #include <txtfrm.hxx>
47 #include <rootfrm.hxx>
48 #include <frmtool.hxx>
49 #include <unocrsr.hxx>
50 #include <mvsave.hxx>
51 #include <ndtxt.hxx>
52 #include <poolfmt.hxx>
53 #include <paratr.hxx>
54 #include <txatbase.hxx>
55 #include <UndoRedline.hxx>
56 #include <undobj.hxx>
57 #include <UndoBookmark.hxx>
58 #include <UndoDelete.hxx>
59 #include <UndoSplitMove.hxx>
60 #include <UndoOverwrite.hxx>
61 #include <UndoInsert.hxx>
62 #include <UndoAttribute.hxx>
63 #include <rolbck.hxx>
64 #include <acorrect.hxx>
65 #include <bookmrk.hxx>
66 #include <ftnidx.hxx>
67 #include <txtftn.hxx>
68 #include <hints.hxx>
69 #include <fmtflcnt.hxx>
70 #include <docedt.hxx>
71 #include <frameformats.hxx>
72 #include <o3tl/safeint.hxx>
73 #include <sal/log.hxx>
74 #include <unotools/charclass.hxx>
75 #include <unotools/configmgr.hxx>
76 #include <sfx2/Metadatable.hxx>
77 #include <sot/exchange.hxx>
78 #include <svl/stritem.hxx>
79 #include <svl/itemiter.hxx>
80 #include <svx/svdobj.hxx>
81 #include <svx/svdouno.hxx>
82 #include <tools/globname.hxx>
84 #include <com/sun/star/i18n/Boundary.hpp>
85 #include <com/sun/star/i18n/WordType.hpp>
86 #include <com/sun/star/i18n/XBreakIterator.hpp>
87 #include <com/sun/star/embed/XEmbeddedObject.hpp>
88 
89 #include <tuple>
90 #include <memory>
91 
92 #include <editsh.hxx>
93 #include <viewopt.hxx>
94 #include <wrtsh.hxx>
95 
96 using namespace ::com::sun::star::i18n;
97 
98 namespace
99 {
100  // Copy method from SwDoc
101  // Prevent copying into Flys that are anchored in the range
102  bool lcl_ChkFlyFly( SwDoc& rDoc, sal_uLong nSttNd, sal_uLong nEndNd,
103  sal_uLong nInsNd )
104  {
105  const SwFrameFormats& rFrameFormatTable = *rDoc.GetSpzFrameFormats();
106 
107  for( size_t n = 0; n < rFrameFormatTable.size(); ++n )
108  {
109  SwFrameFormat const*const pFormat = rFrameFormatTable[n];
110  SwFormatAnchor const*const pAnchor = &pFormat->GetAnchor();
111  SwPosition const*const pAPos = pAnchor->GetContentAnchor();
112  if (pAPos &&
113  ((RndStdIds::FLY_AS_CHAR == pAnchor->GetAnchorId()) ||
114  (RndStdIds::FLY_AT_CHAR == pAnchor->GetAnchorId()) ||
115  (RndStdIds::FLY_AT_FLY == pAnchor->GetAnchorId()) ||
116  (RndStdIds::FLY_AT_PARA == pAnchor->GetAnchorId())) &&
117  nSttNd <= pAPos->nNode.GetIndex() &&
118  pAPos->nNode.GetIndex() < nEndNd )
119  {
120  const SwFormatContent& rContent = pFormat->GetContent();
121  SwStartNode* pSNd;
122  if( !rContent.GetContentIdx() ||
123  nullptr == ( pSNd = rContent.GetContentIdx()->GetNode().GetStartNode() ))
124  continue;
125 
126  if( pSNd->GetIndex() < nInsNd &&
127  nInsNd < pSNd->EndOfSectionIndex() )
128  // Do not copy !
129  return true;
130 
131  if( lcl_ChkFlyFly( rDoc, pSNd->GetIndex(),
132  pSNd->EndOfSectionIndex(), nInsNd ) )
133  // Do not copy !
134  return true;
135  }
136  }
137 
138  return false;
139  }
140 
141  SwNodeIndex InitDelCount(SwPaM const& rSourcePaM, sal_uLong & rDelCount)
142  {
143  SwNodeIndex const& rStart(rSourcePaM.Start()->nNode);
144  // Special handling for SwDoc::AppendDoc
145  if (rSourcePaM.GetDoc().GetNodes().GetEndOfExtras().GetIndex() + 1
146  == rStart.GetIndex())
147  {
148  rDelCount = 1;
149  return SwNodeIndex(rStart, +1);
150  }
151  else
152  {
153  rDelCount = 0;
154  return rStart;
155  }
156  }
157 
158  /*
159  The CopyBookmarks function has to copy bookmarks from the source to the destination nodes
160  array. It is called after a call of the CopyNodes(..) function. But this function does not copy
161  every node (at least at the moment: 2/08/2006 ), section start and end nodes will not be copied
162  if the corresponding end/start node is outside the copied pam.
163  The lcl_NonCopyCount function counts the number of these nodes, given the copied pam and a node
164  index inside the pam.
165  rPam is the original source pam, rLastIdx is the last calculated position, rDelCount the number
166  of "non-copy" nodes between rPam.Start() and rLastIdx.
167  nNewIdx is the new position of interest.
168  */
169  void lcl_NonCopyCount( const SwPaM& rPam, SwNodeIndex& rLastIdx, const sal_uLong nNewIdx, sal_uLong& rDelCount )
170  {
171  sal_uLong nStart = rPam.Start()->nNode.GetIndex();
172  sal_uLong nEnd = rPam.End()->nNode.GetIndex();
173  if( rLastIdx.GetIndex() < nNewIdx ) // Moving forward?
174  {
175  // We never copy the StartOfContent node
176  do // count "non-copy" nodes
177  {
178  SwNode& rNode = rLastIdx.GetNode();
179  if( ( rNode.IsSectionNode() && rNode.EndOfSectionIndex() >= nEnd )
180  || ( rNode.IsEndNode() && rNode.StartOfSectionNode()->GetIndex() < nStart ) )
181  {
182  ++rDelCount;
183  }
184  ++rLastIdx;
185  }
186  while( rLastIdx.GetIndex() < nNewIdx );
187  }
188  else if( rDelCount ) // optimization: if there are no "non-copy" nodes until now,
189  // no move backward needed
190  {
191  while( rLastIdx.GetIndex() > nNewIdx )
192  {
193  SwNode& rNode = rLastIdx.GetNode();
194  if( ( rNode.IsSectionNode() && rNode.EndOfSectionIndex() >= nEnd )
195  || ( rNode.IsEndNode() && rNode.StartOfSectionNode()->GetIndex() < nStart ) )
196  {
197  --rDelCount;
198  }
199  rLastIdx--;
200  }
201  }
202  }
203 
204  void lcl_SetCpyPos( const SwPosition& rOrigPos,
205  const SwPosition& rOrigStt,
206  const SwPosition& rCpyStt,
207  SwPosition& rChgPos,
208  sal_uLong nDelCount )
209  {
210  sal_uLong nNdOff = rOrigPos.nNode.GetIndex();
211  nNdOff -= rOrigStt.nNode.GetIndex();
212  nNdOff -= nDelCount;
213  sal_Int32 nContentPos = rOrigPos.nContent.GetIndex();
214 
215  // Always adjust <nNode> at to be changed <SwPosition> instance <rChgPos>
216  rChgPos.nNode = nNdOff + rCpyStt.nNode.GetIndex();
217  if( !nNdOff )
218  {
219  // just adapt the content index
220  if( nContentPos > rOrigStt.nContent.GetIndex() )
221  nContentPos -= rOrigStt.nContent.GetIndex();
222  else
223  nContentPos = 0;
224  nContentPos += rCpyStt.nContent.GetIndex();
225  }
226  rChgPos.nContent.Assign( rChgPos.nNode.GetNode().GetContentNode(), nContentPos );
227  }
228 
229 }
230 
231 namespace sw
232 {
233  // TODO: use SaveBookmark (from DelBookmarks)
234  void CopyBookmarks(const SwPaM& rPam, SwPosition& rCpyPam)
235  {
236  const SwDoc& rSrcDoc = rPam.GetDoc();
237  SwDoc& rDestDoc = rCpyPam.GetDoc();
238  const IDocumentMarkAccess* const pSrcMarkAccess = rSrcDoc.getIDocumentMarkAccess();
239  ::sw::UndoGuard const undoGuard(rDestDoc.GetIDocumentUndoRedo());
240 
241  const SwPosition &rStt = *rPam.Start(), &rEnd = *rPam.End();
242  SwPosition const*const pCpyStt = &rCpyPam;
243 
244  std::vector< const ::sw::mark::IMark* > vMarksToCopy;
245  for ( IDocumentMarkAccess::const_iterator_t ppMark = pSrcMarkAccess->getAllMarksBegin();
246  ppMark != pSrcMarkAccess->getAllMarksEnd();
247  ++ppMark )
248  {
249  const ::sw::mark::IMark* const pMark = *ppMark;
250 
251  const SwPosition& rMarkStart = pMark->GetMarkStart();
252  const SwPosition& rMarkEnd = pMark->GetMarkEnd();
253  // only include marks that are in the range and not touching both start and end
254  // - not for annotation or checkbox marks.
255  const bool bIsNotOnBoundary =
256  pMark->IsExpanded()
257  ? (rMarkStart != rStt || rMarkEnd != rEnd) // rMarkStart != rMarkEnd
258  : (rMarkStart != rStt && rMarkEnd != rEnd); // rMarkStart == rMarkEnd
260  if ( rMarkStart >= rStt && rMarkEnd <= rEnd
261  && ( bIsNotOnBoundary
267  {
268  vMarksToCopy.push_back(pMark);
269  }
270  }
271  // We have to count the "non-copied" nodes...
272  sal_uLong nDelCount;
273  SwNodeIndex aCorrIdx(InitDelCount(rPam, nDelCount));
274  for(const sw::mark::IMark* const pMark : vMarksToCopy)
275  {
276  SwPaM aTmpPam(*pCpyStt);
277  lcl_NonCopyCount(rPam, aCorrIdx, pMark->GetMarkPos().nNode.GetIndex(), nDelCount);
278  lcl_SetCpyPos( pMark->GetMarkPos(), rStt, *pCpyStt, *aTmpPam.GetPoint(), nDelCount);
279  if(pMark->IsExpanded())
280  {
281  aTmpPam.SetMark();
282  lcl_NonCopyCount(rPam, aCorrIdx, pMark->GetOtherMarkPos().nNode.GetIndex(), nDelCount);
283  lcl_SetCpyPos(pMark->GetOtherMarkPos(), rStt, *pCpyStt, *aTmpPam.GetMark(), nDelCount);
284  }
285 
286  ::sw::mark::IMark* const pNewMark = rDestDoc.getIDocumentMarkAccess()->makeMark(
287  aTmpPam,
288  pMark->GetName(),
291  // Explicitly try to get exactly the same name as in the source
292  // because NavigatorReminders, DdeBookmarks etc. ignore the proposed name
293  rDestDoc.getIDocumentMarkAccess()->renameMark(pNewMark, pMark->GetName());
294 
295  // copying additional attributes for bookmarks or fieldmarks
296  ::sw::mark::IBookmark* const pNewBookmark =
297  dynamic_cast< ::sw::mark::IBookmark* const >(pNewMark);
298  const ::sw::mark::IBookmark* const pOldBookmark =
299  dynamic_cast< const ::sw::mark::IBookmark* >(pMark);
300  if (pNewBookmark && pOldBookmark)
301  {
302  pNewBookmark->SetKeyCode(pOldBookmark->GetKeyCode());
303  pNewBookmark->SetShortName(pOldBookmark->GetShortName());
304  pNewBookmark->Hide(pOldBookmark->IsHidden());
305  pNewBookmark->SetHideCondition(pOldBookmark->GetHideCondition());
306  }
307  ::sw::mark::IFieldmark* const pNewFieldmark =
308  dynamic_cast< ::sw::mark::IFieldmark* const >(pNewMark);
309  const ::sw::mark::IFieldmark* const pOldFieldmark =
310  dynamic_cast< const ::sw::mark::IFieldmark* >(pMark);
311  if (pNewFieldmark && pOldFieldmark)
312  {
313  pNewFieldmark->SetFieldname(pOldFieldmark->GetFieldname());
314  pNewFieldmark->SetFieldHelptext(pOldFieldmark->GetFieldHelptext());
315  ::sw::mark::IFieldmark::parameter_map_t* pNewParams = pNewFieldmark->GetParameters();
316  const ::sw::mark::IFieldmark::parameter_map_t* pOldParams = pOldFieldmark->GetParameters();
317  for (const auto& rEntry : *pOldParams )
318  {
319  pNewParams->insert( rEntry );
320  }
321  }
322 
323  ::sfx2::Metadatable const*const pMetadatable(
324  dynamic_cast< ::sfx2::Metadatable const* >(pMark));
325  ::sfx2::Metadatable *const pNewMetadatable(
326  dynamic_cast< ::sfx2::Metadatable * >(pNewMark));
327  if (pMetadatable && pNewMetadatable)
328  {
329  pNewMetadatable->RegisterAsCopyOf(*pMetadatable);
330  }
331  }
332  }
333 } // namespace sw
334 
335 namespace
336 {
337  void lcl_DeleteRedlines( const SwPaM& rPam, SwPaM& rCpyPam )
338  {
339  const SwDoc& rSrcDoc = rPam.GetDoc();
340  const SwRedlineTable& rTable = rSrcDoc.getIDocumentRedlineAccess().GetRedlineTable();
341  if( rTable.empty() )
342  return;
343 
344  SwDoc& rDestDoc = rCpyPam.GetDoc();
345  SwPosition* pCpyStt = rCpyPam.Start(), *pCpyEnd = rCpyPam.End();
346  std::unique_ptr<SwPaM> pDelPam;
347  const SwPosition *pStt = rPam.Start(), *pEnd = rPam.End();
348  // We have to count the "non-copied" nodes
349  sal_uLong nDelCount;
350  SwNodeIndex aCorrIdx(InitDelCount(rPam, nDelCount));
351 
353  rSrcDoc.getIDocumentRedlineAccess().GetRedline( *pStt, &n );
354  for( ; n < rTable.size(); ++n )
355  {
356  const SwRangeRedline* pRedl = rTable[ n ];
357  if( RedlineType::Delete == pRedl->GetType() && pRedl->IsVisible() )
358  {
359  const SwPosition *pRStt = pRedl->Start(), *pREnd = pRedl->End();
360 
361  SwComparePosition eCmpPos = ComparePosition( *pStt, *pEnd, *pRStt, *pREnd );
362  switch( eCmpPos )
363  {
366  // Pos1 is before Pos2
367  break;
368 
371  // Pos1 is after Pos2
372  n = rTable.size();
373  break;
374 
375  default:
376  {
377  pDelPam.reset(new SwPaM( *pCpyStt, pDelPam.release() ));
378  if( *pStt < *pRStt )
379  {
380  lcl_NonCopyCount( rPam, aCorrIdx, pRStt->nNode.GetIndex(), nDelCount );
381  lcl_SetCpyPos( *pRStt, *pStt, *pCpyStt,
382  *pDelPam->GetPoint(), nDelCount );
383  }
384  pDelPam->SetMark();
385 
386  if( *pEnd < *pREnd )
387  *pDelPam->GetPoint() = *pCpyEnd;
388  else
389  {
390  lcl_NonCopyCount( rPam, aCorrIdx, pREnd->nNode.GetIndex(), nDelCount );
391  lcl_SetCpyPos( *pREnd, *pStt, *pCpyStt,
392  *pDelPam->GetPoint(), nDelCount );
393  }
394 
395  if (pDelPam->GetNext() && *pDelPam->GetNext()->End() == *pDelPam->Start())
396  {
397  *pDelPam->GetNext()->End() = *pDelPam->End();
398  pDelPam.reset(pDelPam->GetNext());
399  }
400  }
401  }
402  }
403  }
404 
405  if( !pDelPam )
406  return;
407 
410 
411  ::sw::UndoGuard const undoGuard(rDestDoc.GetIDocumentUndoRedo());
412 
413  do {
414  rDestDoc.getIDocumentContentOperations().DeleteAndJoin( *pDelPam->GetNext() );
415  if( !pDelPam->IsMultiSelection() )
416  break;
417  delete pDelPam->GetNext();
418  } while( true );
419 
421  }
422 
423  void lcl_DeleteRedlines( const SwNodeRange& rRg, SwNodeRange const & rCpyRg )
424  {
425  SwDoc& rSrcDoc = rRg.aStart.GetNode().GetDoc();
426  if( !rSrcDoc.getIDocumentRedlineAccess().GetRedlineTable().empty() )
427  {
428  SwPaM aRgTmp( rRg.aStart, rRg.aEnd );
429  SwPaM aCpyTmp( rCpyRg.aStart, rCpyRg.aEnd );
430  lcl_DeleteRedlines( aRgTmp, aCpyTmp );
431  }
432  }
433 
434  void lcl_ChainFormats( SwFlyFrameFormat *pSrc, SwFlyFrameFormat *pDest )
435  {
436  SwFormatChain aSrc( pSrc->GetChain() );
437  if ( !aSrc.GetNext() )
438  {
439  aSrc.SetNext( pDest );
440  pSrc->SetFormatAttr( aSrc );
441  }
442  SwFormatChain aDest( pDest->GetChain() );
443  if ( !aDest.GetPrev() )
444  {
445  aDest.SetPrev( pSrc );
446  pDest->SetFormatAttr( aDest );
447  }
448  }
449 
450  // #i86492#
451  bool lcl_ContainsOnlyParagraphsInList( const SwPaM& rPam )
452  {
453  bool bRet = false;
454 
455  const SwTextNode* pTextNd = rPam.Start()->nNode.GetNode().GetTextNode();
456  const SwTextNode* pEndTextNd = rPam.End()->nNode.GetNode().GetTextNode();
457  if ( pTextNd && pTextNd->IsInList() &&
458  pEndTextNd && pEndTextNd->IsInList() )
459  {
460  bRet = true;
461  SwNodeIndex aIdx(rPam.Start()->nNode);
462 
463  do
464  {
465  ++aIdx;
466  pTextNd = aIdx.GetNode().GetTextNode();
467 
468  if ( !pTextNd || !pTextNd->IsInList() )
469  {
470  bRet = false;
471  break;
472  }
473  } while (pTextNd != pEndTextNd);
474  }
475 
476  return bRet;
477  }
478 
479  bool lcl_MarksWholeNode(const SwPaM & rPam)
480  {
481  bool bResult = false;
482  const SwPosition* pStt = rPam.Start();
483  const SwPosition* pEnd = rPam.End();
484 
485  if (nullptr != pStt && nullptr != pEnd)
486  {
487  const SwTextNode* pSttNd = pStt->nNode.GetNode().GetTextNode();
488  const SwTextNode* pEndNd = pEnd->nNode.GetNode().GetTextNode();
489 
490  if (nullptr != pSttNd && nullptr != pEndNd &&
491  pStt->nContent.GetIndex() == 0 &&
492  pEnd->nContent.GetIndex() == pEndNd->Len())
493  {
494  bResult = true;
495  }
496  }
497 
498  return bResult;
499  }
500 }
501 
502 //local functions originally from sw/source/core/doc/docedt.cxx
503 namespace sw
504 {
505  void CalcBreaks(std::vector<std::pair<sal_uLong, sal_Int32>> & rBreaks,
506  SwPaM const & rPam, bool const isOnlyFieldmarks)
507  {
508  sal_uLong const nStartNode(rPam.Start()->nNode.GetIndex());
509  sal_uLong const nEndNode(rPam.End()->nNode.GetIndex());
510  SwNodes const& rNodes(rPam.GetPoint()->nNode.GetNodes());
511  IDocumentMarkAccess const& rIDMA(*rPam.GetDoc().getIDocumentMarkAccess());
512 
513  std::stack<std::tuple<sw::mark::IFieldmark const*, bool, sal_uLong, sal_Int32>> startedFields;
514 
515  for (sal_uLong n = nStartNode; n <= nEndNode; ++n)
516  {
517  SwNode *const pNode(rNodes[n]);
518  if (pNode->IsTextNode())
519  {
520  SwTextNode & rTextNode(*pNode->GetTextNode());
521  sal_Int32 const nStart(n == nStartNode
522  ? rPam.Start()->nContent.GetIndex()
523  : 0);
524  sal_Int32 const nEnd(n == nEndNode
525  ? rPam.End()->nContent.GetIndex()
526  : rTextNode.Len());
527  for (sal_Int32 i = nStart; i < nEnd; ++i)
528  {
529  const sal_Unicode c(rTextNode.GetText()[i]);
530  switch (c)
531  {
532  // note: CH_TXT_ATR_FORMELEMENT does not need handling
533  // not sure how CH_TXT_ATR_INPUTFIELDSTART/END are currently handled
534  case CH_TXTATR_INWORD:
535  case CH_TXTATR_BREAKWORD:
536  {
537  // META hints only have dummy char at the start, not
538  // at the end, so no need to check in nStartNode
539  if (n == nEndNode && !isOnlyFieldmarks)
540  {
541  SwTextAttr const*const pAttr(rTextNode.GetTextAttrForCharAt(i));
542  if (pAttr && pAttr->End() && (nEnd < *pAttr->End()))
543  {
544  assert(pAttr->HasDummyChar());
545  rBreaks.emplace_back(n, i);
546  }
547  }
548  break;
549  }
551  {
552  auto const pFieldMark(rIDMA.getFieldmarkAt(SwPosition(rTextNode, i)));
553  startedFields.emplace(pFieldMark, false, 0, 0);
554  break;
555  }
556  case CH_TXT_ATR_FIELDSEP:
557  {
558  if (startedFields.empty())
559  {
560  rBreaks.emplace_back(n, i);
561  }
562  else
563  { // no way to find the field via MarkManager...
564  assert(std::get<0>(startedFields.top())->IsCoveringPosition(SwPosition(rTextNode, i)));
565  std::get<1>(startedFields.top()) = true;
566  std::get<2>(startedFields.top()) = n;
567  std::get<3>(startedFields.top()) = i;
568  }
569  break;
570  }
571  case CH_TXT_ATR_FIELDEND:
572  {
573  if (startedFields.empty())
574  {
575  rBreaks.emplace_back(n, i);
576  }
577  else
578  { // fieldmarks must not overlap => stack
579  assert(std::get<0>(startedFields.top()) == rIDMA.getFieldmarkAt(SwPosition(rTextNode, i)));
580  startedFields.pop();
581  }
582  break;
583  }
584  }
585  }
586  }
587  else if (pNode->IsStartNode())
588  {
589  if (pNode->EndOfSectionIndex() <= nEndNode)
590  { // fieldmark cannot overlap node section
591  n = pNode->EndOfSectionIndex();
592  }
593  }
594  else
595  { // EndNode can actually happen with sections :(
596  assert(pNode->IsEndNode() || pNode->IsNoTextNode());
597  }
598  }
599  while (!startedFields.empty())
600  {
601  SwPosition const& rStart(std::get<0>(startedFields.top())->GetMarkStart());
602  std::pair<sal_uLong, sal_Int32> const pos(
603  rStart.nNode.GetIndex(), rStart.nContent.GetIndex());
604  auto it = std::lower_bound(rBreaks.begin(), rBreaks.end(), pos);
605  assert(it == rBreaks.end() || *it != pos);
606  rBreaks.insert(it, pos);
607  if (std::get<1>(startedFields.top()))
608  {
609  std::pair<sal_uLong, sal_Int32> const posSep(
610  std::get<2>(startedFields.top()),
611  std::get<3>(startedFields.top()));
612  it = std::lower_bound(rBreaks.begin(), rBreaks.end(), posSep);
613  assert(it == rBreaks.end() || *it != posSep);
614  rBreaks.insert(it, posSep);
615  }
616  startedFields.pop();
617  }
618  }
619 }
620 
621 namespace
622 {
623 
624  bool lcl_DoWithBreaks(::sw::DocumentContentOperationsManager & rDocumentContentOperations, SwPaM & rPam,
625  bool (::sw::DocumentContentOperationsManager::*pFunc)(SwPaM&, bool), const bool bForceJoinNext = false)
626  {
627  std::vector<std::pair<sal_uLong, sal_Int32>> Breaks;
628 
629  sw::CalcBreaks(Breaks, rPam);
630 
631  if (Breaks.empty())
632  {
633  return (rDocumentContentOperations.*pFunc)(rPam, bForceJoinNext);
634  }
635 
636  // Deletion must be split into several parts if the text node
637  // contains a text attribute with end and with dummy character
638  // and the selection does not contain the text attribute completely,
639  // but overlaps its start (left), where the dummy character is.
640 
641  SwPosition const & rSelectionEnd( *rPam.End() );
642 
643  bool bRet( true );
644  // iterate from end to start, to avoid invalidating the offsets!
645  auto iter( Breaks.rbegin() );
646  sal_uLong nOffset(0);
647  SwNodes const& rNodes(rPam.GetPoint()->nNode.GetNodes());
648  SwPaM aPam( rSelectionEnd, rSelectionEnd ); // end node!
649  SwPosition & rEnd( *aPam.End() );
650  SwPosition & rStart( *aPam.Start() );
651 
652  while (iter != Breaks.rend())
653  {
654  rStart = SwPosition(*rNodes[iter->first - nOffset]->GetTextNode(), iter->second + 1);
655  if (rStart < rEnd) // check if part is empty
656  {
657  bRet &= (rDocumentContentOperations.*pFunc)(aPam, bForceJoinNext);
658  nOffset = iter->first - rStart.nNode.GetIndex(); // deleted fly nodes...
659  }
660  rEnd = SwPosition(*rNodes[iter->first - nOffset]->GetTextNode(), iter->second);
661  ++iter;
662  }
663 
664  rStart = *rPam.Start(); // set to original start
665  if (rStart < rEnd) // check if part is empty
666  {
667  bRet &= (rDocumentContentOperations.*pFunc)(aPam, bForceJoinNext);
668  }
669 
670  return bRet;
671  }
672 
673  bool lcl_StrLenOverflow( const SwPaM& rPam )
674  {
675  // If we try to merge two paragraphs we have to test if afterwards
676  // the string doesn't exceed the allowed string length
677  if( rPam.GetPoint()->nNode != rPam.GetMark()->nNode )
678  {
679  const SwPosition* pStt = rPam.Start(), *pEnd = rPam.End();
680  const SwTextNode* pEndNd = pEnd->nNode.GetNode().GetTextNode();
681  if( (nullptr != pEndNd) && pStt->nNode.GetNode().IsTextNode() )
682  {
683  const sal_uInt64 nSum = pStt->nContent.GetIndex() +
684  pEndNd->GetText().getLength() - pEnd->nContent.GetIndex();
685  return nSum > o3tl::make_unsigned(SAL_MAX_INT32);
686  }
687  }
688  return false;
689  }
690 
691  struct SaveRedline
692  {
693  SwRangeRedline* pRedl;
694  sal_uInt32 nStt, nEnd;
695  sal_Int32 nSttCnt;
696  sal_Int32 nEndCnt;
697 
698  SaveRedline( SwRangeRedline* pR, const SwNodeIndex& rSttIdx )
699  : pRedl(pR)
700  , nEnd(0)
701  , nEndCnt(0)
702  {
703  const SwPosition* pStt = pR->Start(),
704  * pEnd = pR->GetMark() == pStt ? pR->GetPoint() : pR->GetMark();
705  sal_uInt32 nSttIdx = rSttIdx.GetIndex();
706  nStt = pStt->nNode.GetIndex() - nSttIdx;
707  nSttCnt = pStt->nContent.GetIndex();
708  if( pR->HasMark() )
709  {
710  nEnd = pEnd->nNode.GetIndex() - nSttIdx;
711  nEndCnt = pEnd->nContent.GetIndex();
712  }
713 
714  pRedl->GetPoint()->nNode = 0;
715  pRedl->GetPoint()->nContent.Assign( nullptr, 0 );
716  pRedl->GetMark()->nNode = 0;
717  pRedl->GetMark()->nContent.Assign( nullptr, 0 );
718  }
719 
720  SaveRedline( SwRangeRedline* pR, const SwPosition& rPos )
721  : pRedl(pR)
722  , nEnd(0)
723  , nEndCnt(0)
724  {
725  const SwPosition* pStt = pR->Start(),
726  * pEnd = pR->GetMark() == pStt ? pR->GetPoint() : pR->GetMark();
727  sal_uInt32 nSttIdx = rPos.nNode.GetIndex();
728  nStt = pStt->nNode.GetIndex() - nSttIdx;
729  nSttCnt = pStt->nContent.GetIndex();
730  if( nStt == 0 )
731  nSttCnt = nSttCnt - rPos.nContent.GetIndex();
732  if( pR->HasMark() )
733  {
734  nEnd = pEnd->nNode.GetIndex() - nSttIdx;
735  nEndCnt = pEnd->nContent.GetIndex();
736  if( nEnd == 0 )
737  nEndCnt = nEndCnt - rPos.nContent.GetIndex();
738  }
739 
740  pRedl->GetPoint()->nNode = 0;
741  pRedl->GetPoint()->nContent.Assign( nullptr, 0 );
742  pRedl->GetMark()->nNode = 0;
743  pRedl->GetMark()->nContent.Assign( nullptr, 0 );
744  }
745 
746  void SetPos( sal_uInt32 nInsPos )
747  {
748  pRedl->GetPoint()->nNode = nInsPos + nStt;
749  pRedl->GetPoint()->nContent.Assign( pRedl->GetContentNode(), nSttCnt );
750  if( pRedl->HasMark() )
751  {
752  pRedl->GetMark()->nNode = nInsPos + nEnd;
753  pRedl->GetMark()->nContent.Assign( pRedl->GetContentNode(false), nEndCnt );
754  }
755  }
756 
757  void SetPos( const SwPosition& aPos )
758  {
759  pRedl->GetPoint()->nNode = aPos.nNode.GetIndex() + nStt;
760  pRedl->GetPoint()->nContent.Assign( pRedl->GetContentNode(), nSttCnt + ( nStt == 0 ? aPos.nContent.GetIndex() : 0 ) );
761  if( pRedl->HasMark() )
762  {
763  pRedl->GetMark()->nNode = aPos.nNode.GetIndex() + nEnd;
764  pRedl->GetMark()->nContent.Assign( pRedl->GetContentNode(false), nEndCnt + ( nEnd == 0 ? aPos.nContent.GetIndex() : 0 ) );
765  }
766  }
767  };
768 
769  typedef std::vector< SaveRedline > SaveRedlines_t;
770 
771  void lcl_SaveRedlines(const SwPaM& aPam, SaveRedlines_t& rArr)
772  {
773  SwDoc& rDoc = aPam.GetNode().GetDoc();
774 
775  const SwPosition* pStart = aPam.Start();
776  const SwPosition* pEnd = aPam.End();
777 
778  // get first relevant redline
779  SwRedlineTable::size_type nCurrentRedline;
780  rDoc.getIDocumentRedlineAccess().GetRedline( *pStart, &nCurrentRedline );
781  if( nCurrentRedline > 0)
782  nCurrentRedline--;
783 
784  // redline mode RedlineFlags::Ignore|RedlineFlags::On; save old mode
787 
788  // iterate over relevant redlines and decide for each whether it should
789  // be saved, or split + saved
790  SwRedlineTable& rRedlineTable = rDoc.getIDocumentRedlineAccess().GetRedlineTable();
791  for( ; nCurrentRedline < rRedlineTable.size(); nCurrentRedline++ )
792  {
793  SwRangeRedline* pCurrent = rRedlineTable[ nCurrentRedline ];
794  SwComparePosition eCompare =
795  ComparePosition( *pCurrent->Start(), *pCurrent->End(),
796  *pStart, *pEnd);
797 
798  // we must save this redline if it overlaps aPam
799  // (we may have to split it, too)
800  if( eCompare == SwComparePosition::OverlapBehind ||
801  eCompare == SwComparePosition::OverlapBefore ||
802  eCompare == SwComparePosition::Outside ||
803  eCompare == SwComparePosition::Inside ||
804  eCompare == SwComparePosition::Equal )
805  {
806  rRedlineTable.Remove( nCurrentRedline-- );
807 
808  // split beginning, if necessary
809  if( eCompare == SwComparePosition::OverlapBefore ||
810  eCompare == SwComparePosition::Outside )
811  {
812  SwRangeRedline* pNewRedline = new SwRangeRedline( *pCurrent );
813  *pNewRedline->End() = *pStart;
814  *pCurrent->Start() = *pStart;
815  rDoc.getIDocumentRedlineAccess().AppendRedline( pNewRedline, true );
816  }
817 
818  // split end, if necessary
819  if( eCompare == SwComparePosition::OverlapBehind ||
820  eCompare == SwComparePosition::Outside )
821  {
822  SwRangeRedline* pNewRedline = new SwRangeRedline( *pCurrent );
823  *pNewRedline->Start() = *pEnd;
824  *pCurrent->End() = *pEnd;
825  rDoc.getIDocumentRedlineAccess().AppendRedline( pNewRedline, true );
826  }
827 
828  // save the current redline
829  rArr.emplace_back( pCurrent, *pStart );
830  }
831  }
832 
833  // restore old redline mode
835  }
836 
837  void lcl_RestoreRedlines(SwDoc& rDoc, const SwPosition& rPos, SaveRedlines_t& rArr)
838  {
841 
842  for(SaveRedline & rSvRedLine : rArr)
843  {
844  rSvRedLine.SetPos( rPos );
845  rDoc.getIDocumentRedlineAccess().AppendRedline( rSvRedLine.pRedl, true );
846  }
847 
849  }
850 
851  void lcl_SaveRedlines(const SwNodeRange& rRg, SaveRedlines_t& rArr)
852  {
853  SwDoc& rDoc = rRg.aStart.GetNode().GetDoc();
854  SwRedlineTable::size_type nRedlPos;
855  SwPosition aSrchPos( rRg.aStart ); aSrchPos.nNode--;
856  aSrchPos.nContent.Assign( aSrchPos.nNode.GetNode().GetContentNode(), 0 );
857  if( rDoc.getIDocumentRedlineAccess().GetRedline( aSrchPos, &nRedlPos ) && nRedlPos )
858  --nRedlPos;
859  else if( nRedlPos >= rDoc.getIDocumentRedlineAccess().GetRedlineTable().size() )
860  return ;
861 
865 
866  do {
867  SwRangeRedline* pTmp = rRedlTable[ nRedlPos ];
868 
869  const SwPosition* pRStt = pTmp->Start(),
870  * pREnd = pTmp->GetMark() == pRStt
871  ? pTmp->GetPoint() : pTmp->GetMark();
872 
873  if( pRStt->nNode < rRg.aStart )
874  {
875  if( pREnd->nNode > rRg.aStart && pREnd->nNode < rRg.aEnd )
876  {
877  // Create a copy and set the end of the original to the end of the MoveArea.
878  // The copy is moved too.
879  SwRangeRedline* pNewRedl = new SwRangeRedline( *pTmp );
880  SwPosition* pTmpPos = pNewRedl->Start();
881  pTmpPos->nNode = rRg.aStart;
882  pTmpPos->nContent.Assign(
883  pTmpPos->nNode.GetNode().GetContentNode(), 0 );
884 
885  rArr.emplace_back(pNewRedl, rRg.aStart);
886 
887  pTmpPos = pTmp->End();
888  pTmpPos->nNode = rRg.aEnd;
889  pTmpPos->nContent.Assign(
890  pTmpPos->nNode.GetNode().GetContentNode(), 0 );
891  }
892  else if( pREnd->nNode == rRg.aStart )
893  {
894  SwPosition* pTmpPos = pTmp->End();
895  pTmpPos->nNode = rRg.aEnd;
896  pTmpPos->nContent.Assign(
897  pTmpPos->nNode.GetNode().GetContentNode(), 0 );
898  }
899  }
900  else if( pRStt->nNode < rRg.aEnd )
901  {
902  rRedlTable.Remove( nRedlPos-- );
903  if( pREnd->nNode < rRg.aEnd ||
904  ( pREnd->nNode == rRg.aEnd && !pREnd->nContent.GetIndex()) )
905  {
906  // move everything
907  rArr.emplace_back( pTmp, rRg.aStart );
908  }
909  else
910  {
911  // split
912  SwRangeRedline* pNewRedl = new SwRangeRedline( *pTmp );
913  SwPosition* pTmpPos = pNewRedl->End();
914  pTmpPos->nNode = rRg.aEnd;
915  pTmpPos->nContent.Assign(
916  pTmpPos->nNode.GetNode().GetContentNode(), 0 );
917 
918  rArr.emplace_back( pNewRedl, rRg.aStart );
919 
920  pTmpPos = pTmp->Start();
921  pTmpPos->nNode = rRg.aEnd;
922  pTmpPos->nContent.Assign(
923  pTmpPos->nNode.GetNode().GetContentNode(), 0 );
924  rDoc.getIDocumentRedlineAccess().AppendRedline( pTmp, true );
925  }
926  }
927  else
928  break;
929 
930  } while( ++nRedlPos < rDoc.getIDocumentRedlineAccess().GetRedlineTable().size() );
932  }
933 
934  void lcl_RestoreRedlines(SwDoc& rDoc, sal_uInt32 const nInsPos, SaveRedlines_t& rArr)
935  {
938 
939  for(SaveRedline & rSvRedLine : rArr)
940  {
941  rSvRedLine.SetPos( nInsPos );
942  rDoc.getIDocumentRedlineAccess().AppendRedline( rSvRedLine.pRedl, true );
943  if (rSvRedLine.pRedl->GetType() == RedlineType::Delete)
944  {
945  UpdateFramesForAddDeleteRedline(rDoc, *rSvRedLine.pRedl);
946  }
947  }
948 
950  }
951 
952  bool lcl_SaveFootnote( const SwNodeIndex& rSttNd, const SwNodeIndex& rEndNd,
953  const SwNodeIndex& rInsPos,
954  SwFootnoteIdxs& rFootnoteArr, SwFootnoteIdxs& rSaveArr,
955  const SwIndex* pSttCnt = nullptr, const SwIndex* pEndCnt = nullptr )
956  {
957  bool bUpdateFootnote = false;
958  const SwNodes& rNds = rInsPos.GetNodes();
959  const bool bDelFootnote = rInsPos.GetIndex() < rNds.GetEndOfAutotext().GetIndex() &&
960  rSttNd.GetIndex() >= rNds.GetEndOfAutotext().GetIndex();
961  const bool bSaveFootnote = !bDelFootnote &&
962  rInsPos.GetIndex() >= rNds.GetEndOfExtras().GetIndex();
963  if( !rFootnoteArr.empty() )
964  {
965 
966  size_t nPos = 0;
967  rFootnoteArr.SeekEntry( rSttNd, &nPos );
968  SwTextFootnote* pSrch;
969  const SwNode* pFootnoteNd;
970 
971  // Delete/save all that come after it
972  while( nPos < rFootnoteArr.size() && ( pFootnoteNd =
973  &( pSrch = rFootnoteArr[ nPos ] )->GetTextNode())->GetIndex()
974  <= rEndNd.GetIndex() )
975  {
976  const sal_Int32 nFootnoteSttIdx = pSrch->GetStart();
977  if( ( pEndCnt && pSttCnt )
978  ? (( &rSttNd.GetNode() == pFootnoteNd &&
979  pSttCnt->GetIndex() > nFootnoteSttIdx) ||
980  ( &rEndNd.GetNode() == pFootnoteNd &&
981  nFootnoteSttIdx >= pEndCnt->GetIndex() ))
982  : ( &rEndNd.GetNode() == pFootnoteNd ))
983  {
984  ++nPos; // continue searching
985  }
986  else
987  {
988  // delete it
989  if( bDelFootnote )
990  {
991  SwTextNode& rTextNd = const_cast<SwTextNode&>(pSrch->GetTextNode());
992  SwIndex aIdx( &rTextNd, nFootnoteSttIdx );
993  rTextNd.EraseText( aIdx, 1 );
994  }
995  else
996  {
997  pSrch->DelFrames(nullptr);
998  rFootnoteArr.erase( rFootnoteArr.begin() + nPos );
999  if( bSaveFootnote )
1000  rSaveArr.insert( pSrch );
1001  }
1002  bUpdateFootnote = true;
1003  }
1004  }
1005 
1006  while( nPos-- && ( pFootnoteNd = &( pSrch = rFootnoteArr[ nPos ] )->
1007  GetTextNode())->GetIndex() >= rSttNd.GetIndex() )
1008  {
1009  const sal_Int32 nFootnoteSttIdx = pSrch->GetStart();
1010  if( !pEndCnt || !pSttCnt ||
1011  ! (( &rSttNd.GetNode() == pFootnoteNd &&
1012  pSttCnt->GetIndex() > nFootnoteSttIdx ) ||
1013  ( &rEndNd.GetNode() == pFootnoteNd &&
1014  nFootnoteSttIdx >= pEndCnt->GetIndex() )) )
1015  {
1016  if( bDelFootnote )
1017  {
1018  // delete it
1019  SwTextNode& rTextNd = const_cast<SwTextNode&>(pSrch->GetTextNode());
1020  SwIndex aIdx( &rTextNd, nFootnoteSttIdx );
1021  rTextNd.EraseText( aIdx, 1 );
1022  }
1023  else
1024  {
1025  pSrch->DelFrames(nullptr);
1026  rFootnoteArr.erase( rFootnoteArr.begin() + nPos );
1027  if( bSaveFootnote )
1028  rSaveArr.insert( pSrch );
1029  }
1030  bUpdateFootnote = true;
1031  }
1032  }
1033  }
1034  // When moving from redline section into document content section, e.g.
1035  // after loading a document with (delete-)redlines, the footnote array
1036  // has to be adjusted... (#i70572)
1037  if( bSaveFootnote )
1038  {
1039  SwNodeIndex aIdx( rSttNd );
1040  while( aIdx < rEndNd ) // Check the moved section
1041  {
1042  SwNode* pNode = &aIdx.GetNode();
1043  if( pNode->IsTextNode() ) // Looking for text nodes...
1044  {
1045  SwpHints *pHints = pNode->GetTextNode()->GetpSwpHints();
1046  if( pHints && pHints->HasFootnote() ) //...with footnotes
1047  {
1048  bUpdateFootnote = true; // Heureka
1049  const size_t nCount = pHints->Count();
1050  for( size_t i = 0; i < nCount; ++i )
1051  {
1052  SwTextAttr *pAttr = pHints->Get( i );
1053  if ( pAttr->Which() == RES_TXTATR_FTN )
1054  {
1055  rSaveArr.insert( static_cast<SwTextFootnote*>(pAttr) );
1056  }
1057  }
1058  }
1059  }
1060  ++aIdx;
1061  }
1062  }
1063  return bUpdateFootnote;
1064  }
1065 
1066  bool lcl_MayOverwrite( const SwTextNode *pNode, const sal_Int32 nPos )
1067  {
1068  sal_Unicode const cChr = pNode->GetText()[nPos];
1069  switch (cChr)
1070  {
1071  case CH_TXTATR_BREAKWORD:
1072  case CH_TXTATR_INWORD:
1073  return !pNode->GetTextAttrForCharAt(nPos);// how could there be none?
1076  case CH_TXT_ATR_FIELDSTART:
1077  case CH_TXT_ATR_FIELDSEP:
1078  case CH_TXT_ATR_FIELDEND:
1080  return false;
1081  default:
1082  return true;
1083  }
1084  }
1085 
1086  void lcl_SkipAttr( const SwTextNode *pNode, SwIndex &rIdx, sal_Int32 &rStart )
1087  {
1088  if( !lcl_MayOverwrite( pNode, rStart ) )
1089  {
1090  // skip all special attributes
1091  do {
1092  ++rIdx;
1093  rStart = rIdx.GetIndex();
1094  } while (rStart < pNode->GetText().getLength()
1095  && !lcl_MayOverwrite(pNode, rStart) );
1096  }
1097  }
1098 
1099  bool lcl_GetTokenToParaBreak( OUString& rStr, OUString& rRet, bool bRegExpRplc )
1100  {
1101  if( bRegExpRplc )
1102  {
1103  sal_Int32 nPos = 0;
1104  const OUString sPara("\\n");
1105  for (;;)
1106  {
1107  nPos = rStr.indexOf( sPara, nPos );
1108  if (nPos<0)
1109  {
1110  break;
1111  }
1112  // Has this been escaped?
1113  if( nPos && '\\' == rStr[nPos-1])
1114  {
1115  ++nPos;
1116  if( nPos >= rStr.getLength() )
1117  {
1118  break;
1119  }
1120  }
1121  else
1122  {
1123  rRet = rStr.copy( 0, nPos );
1124  rStr = rStr.copy( nPos + sPara.getLength() );
1125  return true;
1126  }
1127  }
1128  }
1129  rRet = rStr;
1130  rStr.clear();
1131  return false;
1132  }
1133 }
1134 
1135 namespace //local functions originally from docfmt.cxx
1136 {
1137 
1138  bool lcl_ApplyOtherSet(
1139  SwContentNode & rNode,
1140  SwHistory *const pHistory,
1141  SfxItemSet const& rOtherSet,
1142  SfxItemSet const& rFirstSet,
1143  SfxItemSet const& rPropsSet,
1144  SwRootFrame const*const pLayout,
1145  SwNodeIndex *const o_pIndex = nullptr)
1146  {
1147  assert(rOtherSet.Count());
1148 
1149  bool ret(false);
1150  SwTextNode *const pTNd = rNode.GetTextNode();
1151  sw::MergedPara const* pMerged(nullptr);
1152  if (pLayout && pLayout->IsHideRedlines() && pTNd)
1153  {
1154  SwTextFrame const*const pTextFrame(static_cast<SwTextFrame const*>(
1155  pTNd->getLayoutFrame(pLayout)));
1156  if (pTextFrame)
1157  {
1158  pMerged = pTextFrame->GetMergedPara();
1159  }
1160  if (pMerged)
1161  {
1162  if (rFirstSet.Count())
1163  {
1164  if (pHistory)
1165  {
1166  SwRegHistory aRegH(pMerged->pFirstNode, *pMerged->pFirstNode, pHistory);
1167  ret = pMerged->pFirstNode->SetAttr(rFirstSet);
1168  }
1169  else
1170  {
1171  ret = pMerged->pFirstNode->SetAttr(rFirstSet);
1172  }
1173  }
1174  if (rPropsSet.Count())
1175  {
1176  if (pHistory)
1177  {
1178  SwRegHistory aRegH(pMerged->pParaPropsNode, *pMerged->pParaPropsNode, pHistory);
1179  ret = pMerged->pParaPropsNode->SetAttr(rPropsSet) || ret;
1180  }
1181  else
1182  {
1183  ret = pMerged->pParaPropsNode->SetAttr(rPropsSet) || ret;
1184  }
1185  }
1186  if (o_pIndex)
1187  {
1188  *o_pIndex = *pMerged->pLastNode; // skip hidden
1189  }
1190  }
1191  }
1192 
1193  // input cursor can't be on hidden node, and iteration skips them
1194  assert(!pLayout || !pLayout->IsHideRedlines()
1196 
1197  if (!pMerged)
1198  {
1199  if (pHistory)
1200  {
1201  SwRegHistory aRegH(&rNode, rNode, pHistory);
1202  ret = rNode.SetAttr( rOtherSet );
1203  }
1204  else
1205  {
1206  ret = rNode.SetAttr( rOtherSet );
1207  }
1208  }
1209  return ret;
1210  }
1211 
1212  #define DELETECHARSETS if ( bDelete ) { delete pCharSet; delete pOtherSet; }
1213 
1215  // Is used in SwDoc::Insert(..., SwFormatHint &rHt)
1216 
1217  bool lcl_InsAttr(
1218  SwDoc& rDoc,
1219  const SwPaM &rRg,
1220  const SfxItemSet& rChgSet,
1221  const SetAttrMode nFlags,
1222  SwUndoAttr *const pUndo,
1223  SwRootFrame const*const pLayout,
1224  const bool bExpandCharToPara,
1225  SwTextAttr **ppNewTextAttr)
1226  {
1227  // Divide the Sets (for selections in Nodes)
1228  const SfxItemSet* pCharSet = nullptr;
1229  const SfxItemSet* pOtherSet = nullptr;
1230  bool bDelete = false;
1231  bool bCharAttr = false;
1232  bool bOtherAttr = false;
1233 
1234  // Check, if we can work with rChgSet or if we have to create additional SfxItemSets
1235  if ( 1 == rChgSet.Count() )
1236  {
1237  SfxItemIter aIter( rChgSet );
1238  const SfxPoolItem* pItem = aIter.GetCurItem();
1239  if (pItem && !IsInvalidItem(pItem))
1240  {
1241  const sal_uInt16 nWhich = pItem->Which();
1242 
1243  if ( isCHRATR(nWhich) ||
1244  (RES_TXTATR_CHARFMT == nWhich) ||
1245  (RES_TXTATR_INETFMT == nWhich) ||
1246  (RES_TXTATR_AUTOFMT == nWhich) ||
1247  (RES_TXTATR_UNKNOWN_CONTAINER == nWhich) )
1248  {
1249  pCharSet = &rChgSet;
1250  bCharAttr = true;
1251  }
1252 
1253  if ( isPARATR(nWhich)
1254  || isPARATR_LIST(nWhich)
1255  || isFRMATR(nWhich)
1256  || isGRFATR(nWhich)
1257  || isUNKNOWNATR(nWhich)
1258  || isDrawingLayerAttribute(nWhich) )
1259  {
1260  pOtherSet = &rChgSet;
1261  bOtherAttr = true;
1262  }
1263  }
1264  }
1265 
1266  // Build new itemset if either
1267  // - rChgSet.Count() > 1 or
1268  // - The attribute in rChgSet does not belong to one of the above categories
1269  if ( !bCharAttr && !bOtherAttr )
1270  {
1271  SfxItemSet* pTmpCharItemSet = new SfxItemSet(
1272  rDoc.GetAttrPool(),
1273  svl::Items<
1277  RES_TXTATR_UNKNOWN_CONTAINER>{});
1278 
1279  SfxItemSet* pTmpOtherItemSet = new SfxItemSet(
1280  rDoc.GetAttrPool(),
1281  svl::Items<
1284  // FillAttribute support:
1286 
1287  pTmpCharItemSet->Put( rChgSet );
1288  pTmpOtherItemSet->Put( rChgSet );
1289 
1290  pCharSet = pTmpCharItemSet;
1291  pOtherSet = pTmpOtherItemSet;
1292 
1293  bDelete = true;
1294  }
1295 
1296  SwHistory* pHistory = pUndo ? &pUndo->GetHistory() : nullptr;
1297  bool bRet = false;
1298  const SwPosition *pStt = rRg.Start(), *pEnd = rRg.End();
1299  SwContentNode* pNode = pStt->nNode.GetNode().GetContentNode();
1300 
1301  if( pNode && pNode->IsTextNode() )
1302  {
1303  // tdf#127606 at editing, remove different formatting of DOCX-like numbering symbol
1304  if (pLayout && pNode->GetTextNode()->getIDocumentSettingAccess()->
1306  {
1307  SwContentNode* pEndNode = pEnd->nNode.GetNode().GetContentNode();
1308  SwContentNode* pCurrentNode = pEndNode;
1309  auto nStartIndex = pNode->GetIndex();
1310  auto nEndIndex = pEndNode->GetIndex();
1311  SwNodeIndex aIdx( pEnd->nNode.GetNode() );
1312  while ( pCurrentNode != nullptr && nStartIndex <= pCurrentNode->GetIndex() )
1313  {
1314  if (pCurrentNode->GetSwAttrSet().HasItem(RES_PARATR_LIST_AUTOFMT) &&
1315  // remove character formatting only on wholly selected paragraphs
1316  (nStartIndex < pCurrentNode->GetIndex() || pStt->nContent.GetIndex() == 0) &&
1317  (pCurrentNode->GetIndex() < nEndIndex || pEnd->nContent.GetIndex() == pEndNode->Len()))
1318  {
1319  pCurrentNode->ResetAttr(RES_PARATR_LIST_AUTOFMT);
1320  // reset also paragraph marker
1321  SwIndex nIdx( pCurrentNode, pCurrentNode->Len() );
1322  pCurrentNode->GetTextNode()->RstTextAttr(nIdx, 1);
1323  }
1324  pCurrentNode = SwNodes::GoPrevious( &aIdx );
1325  }
1326  }
1327  // #i27615#
1328  if (rRg.IsInFrontOfLabel())
1329  {
1330  SwTextNode * pTextNd = pNode->GetTextNode();
1331  if (pLayout)
1332  {
1333  pTextNd = sw::GetParaPropsNode(*pLayout, *pTextNd);
1334  }
1335  SwNumRule * pNumRule = pTextNd->GetNumRule();
1336 
1337  if ( !pNumRule )
1338  {
1339  OSL_FAIL( "<InsAttr(..)> - PaM in front of label, but text node has no numbering rule set. This is a serious defect." );
1341  return false;
1342  }
1343 
1344  int nLevel = pTextNd->GetActualListLevel();
1345 
1346  if (nLevel < 0)
1347  nLevel = 0;
1348 
1349  if (nLevel >= MAXLEVEL)
1350  nLevel = MAXLEVEL - 1;
1351 
1352  SwNumFormat aNumFormat = pNumRule->Get(static_cast<sal_uInt16>(nLevel));
1353  SwCharFormat * pCharFormat =
1354  rDoc.FindCharFormatByName(aNumFormat.GetCharFormatName());
1355 
1356  if (pCharFormat)
1357  {
1358  if (pHistory)
1359  pHistory->Add(pCharFormat->GetAttrSet(), *pCharFormat);
1360 
1361  if ( pCharSet )
1362  pCharFormat->SetFormatAttr(*pCharSet);
1363  }
1364 
1366  return true;
1367  }
1368 
1369  const SwIndex& rSt = pStt->nContent;
1370 
1371  // Attributes without an end do not have a range
1372  if ( !bCharAttr && !bOtherAttr )
1373  {
1374  SfxItemSet aTextSet( rDoc.GetAttrPool(),
1376  aTextSet.Put( rChgSet );
1377  if( aTextSet.Count() )
1378  {
1379  SwRegHistory history( pNode, *pNode, pHistory );
1380  bRet = history.InsertItems(
1381  aTextSet, rSt.GetIndex(), rSt.GetIndex(), nFlags, /*ppNewTextAttr*/nullptr ) || bRet;
1382 
1385  {
1386  SwPaM aPam( pStt->nNode, pStt->nContent.GetIndex()-1,
1387  pStt->nNode, pStt->nContent.GetIndex() );
1388 
1389  if( pUndo )
1390  pUndo->SaveRedlineData( aPam, true );
1391 
1392  if( rDoc.getIDocumentRedlineAccess().IsRedlineOn() )
1393  rDoc.getIDocumentRedlineAccess().AppendRedline( new SwRangeRedline( RedlineType::Insert, aPam ), true);
1394  else
1395  rDoc.getIDocumentRedlineAccess().SplitRedline( aPam );
1396  }
1397  }
1398  }
1399 
1400  // TextAttributes with an end never expand their range
1401  if ( !bCharAttr && !bOtherAttr )
1402  {
1403  // CharFormat and URL attributes are treated separately!
1404  // TEST_TEMP ToDo: AutoFormat!
1405  SfxItemSet aTextSet(
1406  rDoc.GetAttrPool(),
1407  svl::Items<
1410  RES_TXTATR_INPUTFIELD, RES_TXTATR_INPUTFIELD>{});
1411 
1412  aTextSet.Put( rChgSet );
1413  if( aTextSet.Count() )
1414  {
1415  const sal_Int32 nInsCnt = rSt.GetIndex();
1416  const sal_Int32 nEnd = pStt->nNode == pEnd->nNode
1417  ? pEnd->nContent.GetIndex()
1418  : pNode->Len();
1419  SwRegHistory history( pNode, *pNode, pHistory );
1420  bRet = history.InsertItems( aTextSet, nInsCnt, nEnd, nFlags, ppNewTextAttr )
1421  || bRet;
1422 
1425  {
1426  // Was text content inserted? (RefMark/TOXMarks without an end)
1427  bool bTextIns = nInsCnt != rSt.GetIndex();
1428  // Was content inserted or set over the selection?
1429  SwPaM aPam( pStt->nNode, bTextIns ? nInsCnt + 1 : nEnd,
1430  pStt->nNode, nInsCnt );
1431  if( pUndo )
1432  pUndo->SaveRedlineData( aPam, bTextIns );
1433 
1434  if( rDoc.getIDocumentRedlineAccess().IsRedlineOn() )
1436  new SwRangeRedline(
1437  bTextIns ? RedlineType::Insert : RedlineType::Format, aPam ),
1438  true);
1439  else if( bTextIns )
1440  rDoc.getIDocumentRedlineAccess().SplitRedline( aPam );
1441  }
1442  }
1443  }
1444  }
1445 
1446  // We always have to set the auto flag for PageDescs that are set at the Node!
1447  if( pOtherSet && pOtherSet->Count() )
1448  {
1449  SwTableNode* pTableNd;
1450  const SwFormatPageDesc* pDesc;
1451  if( SfxItemState::SET == pOtherSet->GetItemState( RES_PAGEDESC,
1452  false, reinterpret_cast<const SfxPoolItem**>(&pDesc) ))
1453  {
1454  if( pNode )
1455  {
1456  // Set auto flag. Only in the template it's without auto!
1457  SwFormatPageDesc aNew( *pDesc );
1458 
1459  // Tables now also know line breaks
1460  if( !(nFlags & SetAttrMode::APICALL) &&
1461  nullptr != ( pTableNd = pNode->FindTableNode() ) )
1462  {
1463  SwTableNode* pCurTableNd = pTableNd;
1464  while ( nullptr != ( pCurTableNd = pCurTableNd->StartOfSectionNode()->FindTableNode() ) )
1465  pTableNd = pCurTableNd;
1466 
1467  // set the table format
1468  SwFrameFormat* pFormat = pTableNd->GetTable().GetFrameFormat();
1469  SwRegHistory aRegH( pFormat, *pTableNd, pHistory );
1470  pFormat->SetFormatAttr( aNew );
1471  bRet = true;
1472  }
1473  else
1474  {
1475  SwContentNode * pFirstNode(pNode);
1476  if (pLayout && pLayout->IsHideRedlines())
1477  {
1478  pFirstNode = sw::GetFirstAndLastNode(*pLayout, pStt->nNode).first;
1479  }
1480  SwRegHistory aRegH( pFirstNode, *pFirstNode, pHistory );
1481  bRet = pFirstNode->SetAttr( aNew ) || bRet;
1482  }
1483  }
1484 
1485  // bOtherAttr = true means that pOtherSet == rChgSet. In this case
1486  // we know, that there is only one attribute in pOtherSet. We cannot
1487  // perform the following operations, instead we return:
1488  if ( bOtherAttr )
1489  return bRet;
1490 
1491  const_cast<SfxItemSet*>(pOtherSet)->ClearItem( RES_PAGEDESC );
1492  if( !pOtherSet->Count() )
1493  {
1495  return bRet;
1496  }
1497  }
1498 
1499  // Tables now also know line breaks
1500  const SvxFormatBreakItem* pBreak;
1501  if( pNode && !(nFlags & SetAttrMode::APICALL) &&
1502  nullptr != (pTableNd = pNode->FindTableNode() ) &&
1503  SfxItemState::SET == pOtherSet->GetItemState( RES_BREAK,
1504  false, reinterpret_cast<const SfxPoolItem**>(&pBreak) ) )
1505  {
1506  SwTableNode* pCurTableNd = pTableNd;
1507  while ( nullptr != ( pCurTableNd = pCurTableNd->StartOfSectionNode()->FindTableNode() ) )
1508  pTableNd = pCurTableNd;
1509 
1510  // set the table format
1511  SwFrameFormat* pFormat = pTableNd->GetTable().GetFrameFormat();
1512  SwRegHistory aRegH( pFormat, *pTableNd, pHistory );
1513  pFormat->SetFormatAttr( *pBreak );
1514  bRet = true;
1515 
1516  // bOtherAttr = true means that pOtherSet == rChgSet. In this case
1517  // we know, that there is only one attribute in pOtherSet. We cannot
1518  // perform the following operations, instead we return:
1519  if ( bOtherAttr )
1520  return bRet;
1521 
1522  const_cast<SfxItemSet*>(pOtherSet)->ClearItem( RES_BREAK );
1523  if( !pOtherSet->Count() )
1524  {
1526  return bRet;
1527  }
1528  }
1529 
1530  {
1531  // If we have a PoolNumRule, create it if needed
1532  const SwNumRuleItem* pRule;
1533  sal_uInt16 nPoolId=0;
1534  if( SfxItemState::SET == pOtherSet->GetItemState( RES_PARATR_NUMRULE,
1535  false, reinterpret_cast<const SfxPoolItem**>(&pRule) ) &&
1536  !rDoc.FindNumRulePtr( pRule->GetValue() ) &&
1540  }
1541  }
1542 
1543  SfxItemSet firstSet(rDoc.GetAttrPool(),
1545  if (pOtherSet && pOtherSet->Count())
1546  { // actually only RES_BREAK is possible here...
1547  firstSet.Put(*pOtherSet);
1548  }
1549  SfxItemSet propsSet(rDoc.GetAttrPool(),
1553  if (pOtherSet && pOtherSet->Count())
1554  {
1555  propsSet.Put(*pOtherSet);
1556  }
1557 
1558  if( !rRg.HasMark() ) // no range
1559  {
1560  if( !pNode )
1561  {
1563  return bRet;
1564  }
1565 
1566  if( pNode->IsTextNode() && pCharSet && pCharSet->Count() )
1567  {
1568  SwTextNode* pTextNd = pNode->GetTextNode();
1569  const SwIndex& rSt = pStt->nContent;
1570  sal_Int32 nMkPos, nPtPos = rSt.GetIndex();
1571  const OUString& rStr = pTextNd->GetText();
1572 
1573  // Special case: if the Cursor is located within a URL attribute, we take over it's area
1574  SwTextAttr const*const pURLAttr(
1575  pTextNd->GetTextAttrAt(rSt.GetIndex(), RES_TXTATR_INETFMT));
1576  if (pURLAttr && !pURLAttr->GetINetFormat().GetValue().isEmpty())
1577  {
1578  nMkPos = pURLAttr->GetStart();
1579  nPtPos = *pURLAttr->End();
1580  }
1581  else
1582  {
1584  Boundary aBndry = g_pBreakIt->GetBreakIter()->getWordBoundary(
1585  pTextNd->GetText(), nPtPos,
1586  g_pBreakIt->GetLocale( pTextNd->GetLang( nPtPos ) ),
1587  WordType::ANY_WORD /*ANYWORD_IGNOREWHITESPACES*/,
1588  true);
1589 
1590  if( aBndry.startPos < nPtPos && nPtPos < aBndry.endPos )
1591  {
1592  nMkPos = aBndry.startPos;
1593  nPtPos = aBndry.endPos;
1594  }
1595  else
1596  nPtPos = nMkPos = rSt.GetIndex();
1597  }
1598 
1599  // Remove the overriding attributes from the SwpHintsArray,
1600  // if the selection spans across the whole paragraph.
1601  // These attributes are inserted as FormatAttributes and
1602  // never override the TextAttributes!
1603  if( !(nFlags & SetAttrMode::DONTREPLACE ) &&
1604  pTextNd->HasHints() && !nMkPos && nPtPos == rStr.getLength())
1605  {
1606  SwIndex aSt( pTextNd );
1607  if( pHistory )
1608  {
1609  // Save all attributes for the Undo.
1610  SwRegHistory aRHst( *pTextNd, pHistory );
1611  pTextNd->GetpSwpHints()->Register( &aRHst );
1612  pTextNd->RstTextAttr( aSt, nPtPos, 0, pCharSet );
1613  if( pTextNd->GetpSwpHints() )
1614  pTextNd->GetpSwpHints()->DeRegister();
1615  }
1616  else
1617  pTextNd->RstTextAttr( aSt, nPtPos, 0, pCharSet );
1618  }
1619 
1620  // the SwRegHistory inserts the attribute into the TextNode!
1621  SwRegHistory history( pNode, *pNode, pHistory );
1622  bRet = history.InsertItems( *pCharSet, nMkPos, nPtPos, nFlags, /*ppNewTextAttr*/nullptr )
1623  || bRet;
1624 
1625  if( rDoc.getIDocumentRedlineAccess().IsRedlineOn() )
1626  {
1627  SwPaM aPam( *pNode, nMkPos, *pNode, nPtPos );
1628 
1629  if( pUndo )
1630  pUndo->SaveRedlineData( aPam, false );
1631  rDoc.getIDocumentRedlineAccess().AppendRedline( new SwRangeRedline( RedlineType::Format, aPam ), true);
1632  }
1633  }
1634  if( pOtherSet && pOtherSet->Count() )
1635  {
1636  // Need to check for unique item for DrawingLayer items of type NameOrIndex
1637  // and evtl. correct that item to ensure unique names for that type. This call may
1638  // modify/correct entries inside of the given SfxItemSet
1639  SfxItemSet aTempLocalCopy(*pOtherSet);
1640 
1641  rDoc.CheckForUniqueItemForLineFillNameOrIndex(aTempLocalCopy);
1642  bRet = lcl_ApplyOtherSet(*pNode, pHistory, aTempLocalCopy, firstSet, propsSet, pLayout) || bRet;
1643  }
1644 
1646  return bRet;
1647  }
1648 
1649  if( rDoc.getIDocumentRedlineAccess().IsRedlineOn() && pCharSet && pCharSet->Count() )
1650  {
1651  if( pUndo )
1652  pUndo->SaveRedlineData( rRg, false );
1653  rDoc.getIDocumentRedlineAccess().AppendRedline( new SwRangeRedline( RedlineType::Format, rRg ), true);
1654  }
1655 
1656  /* now if range */
1657  sal_uLong nNodes = 0;
1658 
1659  SwNodeIndex aSt( rDoc.GetNodes() );
1660  SwNodeIndex aEnd( rDoc.GetNodes() );
1661  SwIndex aCntEnd( pEnd->nContent );
1662 
1663  if( pNode )
1664  {
1665  const sal_Int32 nLen = pNode->Len();
1666  if( pStt->nNode != pEnd->nNode )
1667  aCntEnd.Assign( pNode, nLen );
1668 
1669  if( pStt->nContent.GetIndex() != 0 || aCntEnd.GetIndex() != nLen )
1670  {
1671  // the SwRegHistory inserts the attribute into the TextNode!
1672  if( pNode->IsTextNode() && pCharSet && pCharSet->Count() )
1673  {
1674  SwRegHistory history( pNode, *pNode, pHistory );
1675  bRet = history.InsertItems(*pCharSet,
1676  pStt->nContent.GetIndex(), aCntEnd.GetIndex(), nFlags, /*ppNewTextAttr*/nullptr)
1677  || bRet;
1678  }
1679 
1680  if( pOtherSet && pOtherSet->Count() )
1681  {
1682  bRet = lcl_ApplyOtherSet(*pNode, pHistory, *pOtherSet, firstSet, propsSet, pLayout) || bRet;
1683  }
1684 
1685  // Only selection in a Node.
1686  if( pStt->nNode == pEnd->nNode )
1687  {
1688  //The data parameter flag: bExpandCharToPara, comes from the data member of SwDoc,
1689  //which is set in SW MS Word Binary filter WW8ImplRreader. With this flag on, means that
1690  //current setting attribute set is a character range properties set and comes from a MS Word
1691  //binary file, and the setting range include a paragraph end position (0X0D);
1692  //more specifications, as such property inside the character range properties set recorded in
1693  //MS Word binary file are dealt and inserted into data model (SwDoc) one by one, so we
1694  //only dealing the scenario that the char properties set with 1 item inside;
1695 
1696  if (bExpandCharToPara && pCharSet && pCharSet->Count() ==1 )
1697  {
1698  SwTextNode* pCurrentNd = pStt->nNode.GetNode().GetTextNode();
1699 
1700  if (pCurrentNd)
1701  {
1702  pCurrentNd->TryCharSetExpandToNum(*pCharSet);
1703 
1704  }
1705  }
1707  return bRet;
1708  }
1709  ++nNodes;
1710  aSt.Assign( pStt->nNode.GetNode(), +1 );
1711  }
1712  else
1713  aSt = pStt->nNode;
1714  aCntEnd = pEnd->nContent; // aEnd was changed!
1715  }
1716  else
1717  aSt.Assign( pStt->nNode.GetNode(), +1 );
1718 
1719  // aSt points to the first full Node now
1720 
1721  /*
1722  * The selection spans more than one Node.
1723  */
1724  if( pStt->nNode < pEnd->nNode )
1725  {
1726  pNode = pEnd->nNode.GetNode().GetContentNode();
1727  if(pNode)
1728  {
1729  if( aCntEnd.GetIndex() != pNode->Len() )
1730  {
1731  // the SwRegHistory inserts the attribute into the TextNode!
1732  if( pNode->IsTextNode() && pCharSet && pCharSet->Count() )
1733  {
1734  SwRegHistory history( pNode, *pNode, pHistory );
1735  (void)history.InsertItems(*pCharSet,
1736  0, aCntEnd.GetIndex(), nFlags, /*ppNewTextAttr*/nullptr);
1737  }
1738 
1739  if( pOtherSet && pOtherSet->Count() )
1740  {
1741  lcl_ApplyOtherSet(*pNode, pHistory, *pOtherSet, firstSet, propsSet, pLayout);
1742  }
1743 
1744  ++nNodes;
1745  aEnd = pEnd->nNode;
1746  }
1747  else
1748  aEnd.Assign( pEnd->nNode.GetNode(), +1 );
1749  }
1750  else
1751  aEnd = pEnd->nNode;
1752  }
1753  else
1754  aEnd.Assign( pEnd->nNode.GetNode(), +1 );
1755 
1756  // aEnd points BEHIND the last full node now
1757 
1758  /* Edit the fully selected Nodes. */
1759  // Reset all attributes from the set!
1760  if( pCharSet && pCharSet->Count() && !( SetAttrMode::DONTREPLACE & nFlags ) )
1761  {
1763  pStt, pEnd, pHistory, pCharSet, pLayout);
1765  }
1766 
1767  bool bCreateSwpHints = pCharSet && (
1768  SfxItemState::SET == pCharSet->GetItemState( RES_TXTATR_CHARFMT, false ) ||
1769  SfxItemState::SET == pCharSet->GetItemState( RES_TXTATR_INETFMT, false ) );
1770 
1771  for (SwNodeIndex current = aSt; current < aEnd; ++current)
1772  {
1773  SwTextNode *const pTNd = current.GetNode().GetTextNode();
1774  if (!pTNd)
1775  continue;
1776 
1777  if (pLayout && pLayout->IsHideRedlines()
1779  { // not really sure what to do here, but applying to hidden
1780  continue; // nodes doesn't make sense...
1781  }
1782 
1783  if( pHistory )
1784  {
1785  SwRegHistory aRegH( pTNd, *pTNd, pHistory );
1786 
1787  if (pCharSet && pCharSet->Count())
1788  {
1789  SwpHints *pSwpHints = bCreateSwpHints ? &pTNd->GetOrCreateSwpHints()
1790  : pTNd->GetpSwpHints();
1791  if( pSwpHints )
1792  pSwpHints->Register( &aRegH );
1793 
1794  pTNd->SetAttr(*pCharSet, 0, pTNd->GetText().getLength(), nFlags);
1795  if( pSwpHints )
1796  pSwpHints->DeRegister();
1797  }
1798  }
1799  else
1800  {
1801  if (pCharSet && pCharSet->Count())
1802  pTNd->SetAttr(*pCharSet, 0, pTNd->GetText().getLength(), nFlags);
1803  }
1804  ++nNodes;
1805  }
1806 
1807  if (pOtherSet && pOtherSet->Count())
1808  {
1809  for (; aSt < aEnd; ++aSt)
1810  {
1811  pNode = aSt.GetNode().GetContentNode();
1812  if (!pNode)
1813  continue;
1814 
1815  lcl_ApplyOtherSet(*pNode, pHistory, *pOtherSet, firstSet, propsSet, pLayout, &aSt);
1816  ++nNodes;
1817  }
1818  }
1819 
1820  //The data parameter flag: bExpandCharToPara, comes from the data member of SwDoc,
1821  //which is set in SW MS Word Binary filter WW8ImplRreader. With this flag on, means that
1822  //current setting attribute set is a character range properties set and comes from a MS Word
1823  //binary file, and the setting range include a paragraph end position (0X0D);
1824  //more specifications, as such property inside the character range properties set recorded in
1825  //MS Word binary file are dealt and inserted into data model (SwDoc) one by one, so we
1826  //only dealing the scenario that the char properties set with 1 item inside;
1827  if (bExpandCharToPara && pCharSet && pCharSet->Count() ==1)
1828  {
1829  SwPosition aStartPos (*rRg.Start());
1830  SwPosition aEndPos (*rRg.End());
1831 
1832  if (aEndPos.nNode.GetNode().GetTextNode() && aEndPos.nContent != aEndPos.nNode.GetNode().GetTextNode()->Len())
1833  aEndPos.nNode--;
1834 
1835  sal_uLong nStart = aStartPos.nNode.GetIndex();
1836  sal_uLong nEnd = aEndPos.nNode.GetIndex();
1837  for(; nStart <= nEnd; ++nStart)
1838  {
1839  SwNode* pNd = rDoc.GetNodes()[ nStart ];
1840  if (!pNd || !pNd->IsTextNode())
1841  continue;
1842  SwTextNode *pCurrentNd = pNd->GetTextNode();
1843  pCurrentNd->TryCharSetExpandToNum(*pCharSet);
1844  }
1845  }
1846 
1848  return (nNodes != 0) || bRet;
1849  }
1850 }
1851 
1852 namespace sw
1853 {
1854 
1855 namespace mark
1856 {
1857  bool IsFieldmarkOverlap(SwPaM const& rPaM)
1858  {
1859  std::vector<std::pair<sal_uLong, sal_Int32>> Breaks;
1860  sw::CalcBreaks(Breaks, rPaM);
1861  return !Breaks.empty();
1862  }
1863 }
1864 
1866 {
1867 }
1868 
1875 static bool IsEmptyRange(const SwPosition& rStart, const SwPosition& rEnd,
1876  SwCopyFlags const flags)
1877 {
1878  if (rStart == rEnd)
1879  { // check if a fly anchored there would be copied - then copy...
1880  return !IsDestroyFrameAnchoredAtChar(rStart, rStart, rEnd,
1881  (flags & SwCopyFlags::IsMoveToFly)
1884  }
1885  else
1886  {
1887  return rEnd < rStart;
1888  }
1889 }
1890 
1891 // Copy an area into this document or into another document
1892 bool
1894  SwCopyFlags const flags) const
1895 {
1896  const SwPosition *pStt = rPam.Start(), *pEnd = rPam.End();
1897 
1898  SwDoc& rDoc = rPos.nNode.GetNode().GetDoc();
1899  bool bColumnSel = rDoc.IsClipBoard() && rDoc.IsColumnSelection();
1900 
1901  // Catch if there's no copy to do
1902  if (!rPam.HasMark() || (IsEmptyRange(*pStt, *pEnd, flags) && !bColumnSel))
1903  return false;
1904 
1905  // Prevent copying into Flys that are anchored in the source range
1906  if (&rDoc == &m_rDoc && (flags & SwCopyFlags::CheckPosInFly))
1907  {
1908  // Correct the Start-/EndNode
1909  sal_uLong nStt = pStt->nNode.GetIndex(),
1910  nEnd = pEnd->nNode.GetIndex(),
1911  nDiff = nEnd - nStt +1;
1912  SwNode* pNd = m_rDoc.GetNodes()[ nStt ];
1913  if( pNd->IsContentNode() && pStt->nContent.GetIndex() )
1914  {
1915  ++nStt;
1916  --nDiff;
1917  }
1918  if( (pNd = m_rDoc.GetNodes()[ nEnd ])->IsContentNode() &&
1919  static_cast<SwContentNode*>(pNd)->Len() != pEnd->nContent.GetIndex() )
1920  {
1921  --nEnd;
1922  --nDiff;
1923  }
1924  if( nDiff &&
1925  lcl_ChkFlyFly( rDoc, nStt, nEnd, rPos.nNode.GetIndex() ) )
1926  {
1927  return false;
1928  }
1929  }
1930 
1931  SwPaM* pRedlineRange = nullptr;
1932  if( rDoc.getIDocumentRedlineAccess().IsRedlineOn() ||
1934  pRedlineRange = new SwPaM( rPos );
1935 
1937 
1938  bool bRet = false;
1939 
1940  if( &rDoc != &m_rDoc )
1941  { // ordinary copy
1942  bRet = CopyImpl(rPam, rPos, flags & ~SwCopyFlags::CheckPosInFly, pRedlineRange);
1943  }
1944  else if( ! ( *pStt <= rPos && rPos < *pEnd &&
1945  ( pStt->nNode != pEnd->nNode ||
1946  !pStt->nNode.GetNode().IsTextNode() )) )
1947  {
1948  // Copy to a position outside of the area, or copy a single TextNode
1949  // Do an ordinary copy
1950  bRet = CopyImpl(rPam, rPos, flags & ~SwCopyFlags::CheckPosInFly, pRedlineRange);
1951  }
1952  else
1953  {
1954  // Copy the range in itself
1955  assert(!"mst: this is assumed to be dead code");
1956  }
1957 
1959  if( pRedlineRange )
1960  {
1961  if( rDoc.getIDocumentRedlineAccess().IsRedlineOn() )
1962  rDoc.getIDocumentRedlineAccess().AppendRedline( new SwRangeRedline( RedlineType::Insert, *pRedlineRange ), true);
1963  else
1964  rDoc.getIDocumentRedlineAccess().SplitRedline( *pRedlineRange );
1965  delete pRedlineRange;
1966  }
1967 
1968  return bRet;
1969 }
1970 
1974 {
1975  assert(pNode && "Didn't pass a Node.");
1976 
1977  SwStartNode* pSttNd = pNode->IsStartNode() ? static_cast<SwStartNode*>(pNode)
1978  : pNode->StartOfSectionNode();
1979  SwNodeIndex aSttIdx( *pSttNd ), aEndIdx( *pNode->EndOfSectionNode() );
1980 
1981  // delete all Flys, Bookmarks, ...
1982  DelFlyInRange( aSttIdx, aEndIdx );
1983  m_rDoc.getIDocumentRedlineAccess().DeleteRedline( *pSttNd, true, RedlineType::Any );
1984  DelBookmarks(aSttIdx, aEndIdx);
1985 
1986  {
1987  // move all Cursor/StackCursor/UnoCursor out of the to-be-deleted area
1988  SwNodeIndex aMvStt( aSttIdx, 1 );
1989  SwDoc::CorrAbs( aMvStt, aEndIdx, SwPosition( aSttIdx ), true );
1990  }
1991 
1992  m_rDoc.GetNodes().DelNodes( aSttIdx, aEndIdx.GetIndex() - aSttIdx.GetIndex() + 1 );
1993 }
1994 
1996  SwPosition const& rPos, sal_Unicode const cDummy)
1997 {
1998  SwPaM aPam(rPos, rPos);
1999  ++aPam.GetPoint()->nContent;
2000  assert(aPam.GetText().getLength() == 1 && aPam.GetText()[0] == cDummy);
2001  (void) cDummy;
2002 
2003  DeleteRangeImpl(aPam);
2004 
2007  {
2009  }
2010 }
2011 
2013 {
2014  lcl_DoWithBreaks( *this, rPam, &DocumentContentOperationsManager::DeleteRangeImpl );
2015 
2018  {
2020  }
2021 }
2022 
2024 {
2025  const SwPosition &rStt = *rPam.Start(), &rEnd = *rPam.End();
2026  const SwNode* pNd = &rStt.nNode.GetNode();
2027  sal_uInt32 nSectDiff = pNd->StartOfSectionNode()->EndOfSectionIndex() -
2028  pNd->StartOfSectionIndex();
2029  sal_uInt32 nNodeDiff = rEnd.nNode.GetIndex() - rStt.nNode.GetIndex();
2030 
2031  if ( nSectDiff-2 <= nNodeDiff || m_rDoc.getIDocumentRedlineAccess().IsRedlineOn() ||
2032  /* #i9185# Prevent getting the node after the end node (see below) */
2033  rEnd.nNode.GetIndex() + 1 == m_rDoc.GetNodes().Count() )
2034  {
2035  return false;
2036  }
2037 
2038  {
2039  SwPaM temp(rPam, nullptr);
2040  if (!temp.HasMark())
2041  {
2042  temp.SetMark();
2043  }
2044  if (SwTextNode *const pNode = temp.Start()->nNode.GetNode().GetTextNode())
2045  { // rPam may not have nContent set but IsFieldmarkOverlap requires it
2046  pNode->MakeStartIndex(&temp.Start()->nContent);
2047  }
2048  if (SwTextNode *const pNode = temp.End()->nNode.GetNode().GetTextNode())
2049  {
2050  pNode->MakeEndIndex(&temp.End()->nContent);
2051  }
2052  if (sw::mark::IsFieldmarkOverlap(temp))
2053  { // a bit of a problem: we want to completely remove the nodes
2054  // but then how can the CH_TXT_ATR survive?
2055  return false;
2056  }
2057  }
2058 
2059  // Move hard page brakes to the following Node.
2060  bool bSavePageBreak = false, bSavePageDesc = false;
2061 
2062  /* #i9185# This would lead to a segmentation fault if not caught above. */
2063  sal_uLong nNextNd = rEnd.nNode.GetIndex() + 1;
2064  SwTableNode *const pTableNd = m_rDoc.GetNodes()[ nNextNd ]->GetTableNode();
2065 
2066  if( pTableNd && pNd->IsContentNode() )
2067  {
2068  SwFrameFormat* pTableFormat = pTableNd->GetTable().GetFrameFormat();
2069 
2070  {
2071  const SfxPoolItem *pItem;
2072  const SfxItemSet* pSet = static_cast<const SwContentNode*>(pNd)->GetpSwAttrSet();
2073  if( pSet && SfxItemState::SET == pSet->GetItemState( RES_PAGEDESC,
2074  false, &pItem ) )
2075  {
2076  pTableFormat->SetFormatAttr( *pItem );
2077  bSavePageDesc = true;
2078  }
2079 
2080  if( pSet && SfxItemState::SET == pSet->GetItemState( RES_BREAK,
2081  false, &pItem ) )
2082  {
2083  pTableFormat->SetFormatAttr( *pItem );
2084  bSavePageBreak = true;
2085  }
2086  }
2087  }
2088 
2089  bool const bDoesUndo = m_rDoc.GetIDocumentUndoRedo().DoesUndo();
2090  if( bDoesUndo )
2091  {
2092  if( !rPam.HasMark() )
2093  rPam.SetMark();
2094  else if( rPam.GetPoint() == &rStt )
2095  rPam.Exchange();
2096  rPam.GetPoint()->nNode++;
2097 
2098  SwContentNode *pTmpNode = rPam.GetPoint()->nNode.GetNode().GetContentNode();
2099  rPam.GetPoint()->nContent.Assign( pTmpNode, 0 );
2100  bool bGoNext = (nullptr == pTmpNode);
2101  pTmpNode = rPam.GetMark()->nNode.GetNode().GetContentNode();
2102  rPam.GetMark()->nContent.Assign( pTmpNode, 0 );
2103 
2105 
2106  SwPaM aDelPam( *rPam.GetMark(), *rPam.GetPoint() );
2107  {
2108  SwPosition aTmpPos( *aDelPam.GetPoint() );
2109  if( bGoNext )
2110  {
2111  pTmpNode = m_rDoc.GetNodes().GoNext( &aTmpPos.nNode );
2112  aTmpPos.nContent.Assign( pTmpNode, 0 );
2113  }
2114  ::PaMCorrAbs( aDelPam, aTmpPos );
2115  }
2116 
2117  std::unique_ptr<SwUndoDelete> pUndo(new SwUndoDelete( aDelPam, true ));
2118 
2119  *rPam.GetPoint() = *aDelPam.GetPoint();
2120  pUndo->SetPgBrkFlags( bSavePageBreak, bSavePageDesc );
2121  m_rDoc.GetIDocumentUndoRedo().AppendUndo(std::move(pUndo));
2122  rPam.DeleteMark();
2123  }
2124  else
2125  {
2126  SwNodeRange aRg( rStt.nNode, rEnd.nNode );
2127  rPam.Normalize(false);
2128 
2129  // Try to move past the End
2130  if( !rPam.Move( fnMoveForward, GoInNode ) )
2131  {
2132  // Fair enough, at the Beginning then
2133  rPam.Exchange();
2134  if( !rPam.Move( fnMoveBackward, GoInNode ))
2135  {
2136  SAL_WARN("sw.core", "DelFullPara: no more Nodes");
2137  return false;
2138  }
2139  }
2140  // move bookmarks, redlines etc.
2141  if (aRg.aStart == aRg.aEnd) // only first CorrAbs variant handles this
2142  {
2143  m_rDoc.CorrAbs( aRg.aStart, *rPam.GetPoint(), 0, true );
2144  }
2145  else
2146  {
2147  SwDoc::CorrAbs( aRg.aStart, aRg.aEnd, *rPam.GetPoint(), true );
2148  }
2149 
2150  // What's with Flys?
2151  {
2152  // If there are FlyFrames left, delete these too
2153  for( size_t n = 0; n < m_rDoc.GetSpzFrameFormats()->size(); ++n )
2154  {
2155  SwFrameFormat* pFly = (*m_rDoc.GetSpzFrameFormats())[n];
2156  const SwFormatAnchor* pAnchor = &pFly->GetAnchor();
2157  SwPosition const*const pAPos = pAnchor->GetContentAnchor();
2158  if (pAPos &&
2159  ((RndStdIds::FLY_AT_PARA == pAnchor->GetAnchorId()) ||
2160  (RndStdIds::FLY_AT_CHAR == pAnchor->GetAnchorId())) &&
2161  // note: here use <= not < like in
2162  // IsDestroyFrameAnchoredAtChar() because of the increment
2163  // of rPam in the bDoesUndo path above!
2164  aRg.aStart <= pAPos->nNode && pAPos->nNode <= aRg.aEnd )
2165  {
2167  --n;
2168  }
2169  }
2170  }
2171 
2172  rPam.DeleteMark();
2173  m_rDoc.GetNodes().Delete( aRg.aStart, nNodeDiff+1 );
2174  }
2176 
2177  return true;
2178 }
2179 
2180 // #i100466# Add handling of new optional parameter <bForceJoinNext>
2182  const bool bForceJoinNext )
2183 {
2184  if ( lcl_StrLenOverflow( rPam ) )
2185  return false;
2186 
2187  bool const ret = lcl_DoWithBreaks( *this, rPam, (m_rDoc.getIDocumentRedlineAccess().IsRedlineOn())
2190  bForceJoinNext );
2191 
2192  return ret;
2193 }
2194 
2195 // It seems that this is mostly used by SwDoc internals; the only
2196 // way to call this from the outside seems to be the special case in
2197 // SwDoc::CopyRange (but I have not managed to actually hit that case).
2199 {
2200  // nothing moved: return
2201  const SwPosition *pStt = rPaM.Start(), *pEnd = rPaM.End();
2202  if( !rPaM.HasMark() || *pStt >= *pEnd || (*pStt <= rPos && rPos < *pEnd))
2203  return false;
2204 
2205  assert(!sw::mark::IsFieldmarkOverlap(rPaM)); // probably an invalid redline was created?
2206 
2207  // Save the paragraph anchored Flys, so that they can be moved.
2208  SaveFlyArr aSaveFlyArr;
2209  SaveFlyInRange( rPaM, rPos, aSaveFlyArr, bool( SwMoveFlags::ALLFLYS & eMvFlags ) );
2210 
2211  // save redlines (if DOC_MOVEREDLINES is used)
2212  SaveRedlines_t aSaveRedl;
2214  {
2215  lcl_SaveRedlines( rPaM, aSaveRedl );
2216 
2217  // #i17764# unfortunately, code below relies on undos being
2218  // in a particular order, and presence of bookmarks
2219  // will change this order. Hence, we delete bookmarks
2220  // here without undo.
2221  ::sw::UndoGuard const undoGuard(m_rDoc.GetIDocumentUndoRedo());
2222  DelBookmarks(
2223  pStt->nNode,
2224  pEnd->nNode,
2225  nullptr,
2226  &pStt->nContent,
2227  &pEnd->nContent);
2228  }
2229 
2230  bool bUpdateFootnote = false;
2231  SwFootnoteIdxs aTmpFntIdx;
2232 
2233  std::unique_ptr<SwUndoMove> pUndoMove;
2235  {
2237  pUndoMove.reset(new SwUndoMove( rPaM, rPos ));
2238  pUndoMove->SetMoveRedlines( eMvFlags == SwMoveFlags::REDLINES );
2239  }
2240  else
2241  {
2242  bUpdateFootnote = lcl_SaveFootnote( pStt->nNode, pEnd->nNode, rPos.nNode,
2243  m_rDoc.GetFootnoteIdxs(), aTmpFntIdx,
2244  &pStt->nContent, &pEnd->nContent );
2245  }
2246 
2247  bool bSplit = false;
2248  SwPaM aSavePam( rPos, rPos );
2249 
2250  // Move the SPoint to the beginning of the range
2251  if( rPaM.GetPoint() == pEnd )
2252  rPaM.Exchange();
2253 
2254  // If there is a TextNode before and after the Move, create a JoinNext in the EditShell.
2255  SwTextNode* pSrcNd = rPaM.GetPoint()->nNode.GetNode().GetTextNode();
2256  bool bCorrSavePam = pSrcNd && pStt->nNode != pEnd->nNode;
2257 
2258  // If one ore more TextNodes are moved, SwNodes::Move will do a SplitNode.
2259  // However, this does not update the cursor. So we create a TextNode to keep
2260  // updating the indices. After the Move the Node is optionally deleted.
2261  SwTextNode * pTNd = rPos.nNode.GetNode().GetTextNode();
2262  if( pTNd && rPaM.GetPoint()->nNode != rPaM.GetMark()->nNode &&
2263  ( rPos.nContent.GetIndex() || ( pTNd->Len() && bCorrSavePam )) )
2264  {
2265  bSplit = true;
2266  const sal_Int32 nMkContent = rPaM.GetMark()->nContent.GetIndex();
2267 
2268  const std::shared_ptr<sw::mark::ContentIdxStore> pContentStore(sw::mark::ContentIdxStore::Create());
2269  pContentStore->Save( m_rDoc, rPos.nNode.GetIndex(), rPos.nContent.GetIndex(), true );
2270 
2271  SwTextNode * pOrigNode = pTNd;
2272  assert(*aSavePam.GetPoint() == *aSavePam.GetMark() &&
2273  *aSavePam.GetPoint() == rPos);
2274  assert(aSavePam.GetPoint()->nContent.GetIdxReg() == pOrigNode);
2275  assert(aSavePam.GetPoint()->nNode == rPos.nNode.GetIndex());
2276  assert(rPos.nNode.GetIndex() == pOrigNode->GetIndex());
2277 
2278  std::function<void (SwTextNode *, sw::mark::RestoreMode)> restoreFunc(
2279  [&](SwTextNode *const, sw::mark::RestoreMode const eMode)
2280  {
2281  if (!pContentStore->Empty())
2282  {
2283  pContentStore->Restore(m_rDoc, pOrigNode->GetIndex()-1, 0, true, eMode);
2284  }
2285  });
2286  pTNd = pTNd->SplitContentNode(rPos, &restoreFunc)->GetTextNode();
2287 
2288  //A new node was inserted before the orig pTNd and the content up to
2289  //rPos moved into it. The old node is returned with the remainder
2290  //of the content in it.
2291  //
2292  //aSavePam was created with rPos, it continues to point to the
2293  //old node, but with the *original* content index into the node.
2294  //Seeing as all the orignode content before that index has
2295  //been removed, the new index into the original node should now be set
2296  //to 0 and the content index of rPos should also be adapted to the
2297  //truncated node
2298  assert(*aSavePam.GetPoint() == *aSavePam.GetMark() &&
2299  *aSavePam.GetPoint() == rPos);
2300  assert(aSavePam.GetPoint()->nContent.GetIdxReg() == pOrigNode);
2301  assert(aSavePam.GetPoint()->nNode == rPos.nNode.GetIndex());
2302  assert(rPos.nNode.GetIndex() == pOrigNode->GetIndex());
2303  aSavePam.GetPoint()->nContent.Assign(pOrigNode, 0);
2304  rPos = *aSavePam.GetMark() = *aSavePam.GetPoint();
2305 
2306  // correct the PaM!
2307  if( rPos.nNode == rPaM.GetMark()->nNode )
2308  {
2309  rPaM.GetMark()->nNode = rPos.nNode.GetIndex()-1;
2310  rPaM.GetMark()->nContent.Assign( pTNd, nMkContent );
2311  }
2312  }
2313 
2314  // Put back the Pam by one "content"; so that it's always outside of
2315  // the manipulated range.
2316  // tdf#99692 don't Move() back if that would end up in another node
2317  // because moving backward is not necessarily the inverse of forward then.
2318  // (but do Move() back if we have split the node)
2319  const bool bNullContent = !bSplit && aSavePam.GetPoint()->nContent == 0;
2320  if( bNullContent )
2321  {
2322  aSavePam.GetPoint()->nNode--;
2323  aSavePam.GetPoint()->nContent.Assign(aSavePam.GetContentNode(), 0);
2324  }
2325  else
2326  {
2327  bool const success(aSavePam.Move(fnMoveBackward, GoInContent));
2328  assert(success);
2329  (void) success;
2330  }
2331 
2332  // Copy all Bookmarks that are within the Move range into an array,
2333  // that saves the position as an offset.
2334  std::vector< ::sw::mark::SaveBookmark> aSaveBkmks;
2335  DelBookmarks(
2336  pStt->nNode,
2337  pEnd->nNode,
2338  &aSaveBkmks,
2339  &pStt->nContent,
2340  &pEnd->nContent);
2341 
2342  // If there is no range anymore due to the above deletions (e.g. the
2343  // footnotes got deleted), it's still a valid Move!
2344  if( *rPaM.GetPoint() != *rPaM.GetMark() )
2345  {
2346  // now do the actual move
2347  m_rDoc.GetNodes().MoveRange( rPaM, rPos, m_rDoc.GetNodes() );
2348 
2349  // after a MoveRange() the Mark is deleted
2350  if ( rPaM.HasMark() ) // => no Move occurred!
2351  {
2352  return false;
2353  }
2354  }
2355  else
2356  rPaM.DeleteMark();
2357 
2358  OSL_ENSURE( *aSavePam.GetMark() == rPos ||
2359  ( aSavePam.GetMark()->nNode.GetNode().GetContentNode() == nullptr ),
2360  "PaM was not moved. Aren't there ContentNodes at the beginning/end?" );
2361  *aSavePam.GetMark() = rPos;
2362 
2363  rPaM.SetMark(); // create a Sel. around the new range
2364  pTNd = aSavePam.GetNode().GetTextNode();
2366  bool bRemove = true;
2367  // Do two Nodes have to be joined at the SavePam?
2368  if (bSplit && pTNd)
2369  {
2370  if (pTNd->CanJoinNext())
2371  {
2372  // Always join next, because <pTNd> has to stay as it is.
2373  // A join previous from its next would more or less delete <pTNd>
2374  pTNd->JoinNext();
2375  bRemove = false;
2376  }
2377  }
2378  if (bNullContent)
2379  {
2380  aSavePam.GetPoint()->nNode++;
2381  aSavePam.GetPoint()->nContent.Assign( aSavePam.GetContentNode(), 0 );
2382  }
2383  else if (bRemove) // No move forward after joining with next paragraph
2384  {
2385  aSavePam.Move( fnMoveForward, GoInContent );
2386  }
2387 
2388  // Insert the Bookmarks back into the Document.
2389  *rPaM.GetMark() = *aSavePam.Start();
2390  for(auto& rBkmk : aSaveBkmks)
2391  rBkmk.SetInDoc(
2392  &m_rDoc,
2393  rPaM.GetMark()->nNode,
2394  &rPaM.GetMark()->nContent);
2395  *rPaM.GetPoint() = *aSavePam.End();
2396 
2397  // Move the Flys to the new position.
2398  // note: rPos is at the end here; can't really tell flys that used to be
2399  // at the start of rPam from flys that used to be at the end of rPam
2400  // unfortunately, so some of them are going to end up with wrong anchor...
2401  RestFlyInRange( aSaveFlyArr, *rPaM.Start(), &(rPos.nNode) );
2402 
2403  // restore redlines (if DOC_MOVEREDLINES is used)
2404  if( !aSaveRedl.empty() )
2405  {
2406  lcl_RestoreRedlines( m_rDoc, *aSavePam.Start(), aSaveRedl );
2407  }
2408 
2409  if( bUpdateFootnote )
2410  {
2411  if( !aTmpFntIdx.empty() )
2412  {
2413  m_rDoc.GetFootnoteIdxs().insert( aTmpFntIdx );
2414  aTmpFntIdx.clear();
2415  }
2416 
2418  }
2419 
2421  return true;
2422 }
2423 
2425  SwMoveFlags eMvFlags )
2426 {
2427  std::vector<SwNode*> aFoldedOutlineNdsArray;
2428  SwWrtShell* pWrtShell = dynamic_cast<SwWrtShell*>(m_rDoc.GetEditShell());
2429  if (pWrtShell && pWrtShell->GetViewOptions() && pWrtShell->GetViewOptions()->IsShowOutlineContentVisibilityButton())
2430  {
2431  // unfold all folded outline content
2432  SwOutlineNodes rOutlineNds = m_rDoc.GetNodes().GetOutLineNds();
2433  for (SwOutlineNodes::size_type nPos = 0; nPos < rOutlineNds.size(); ++nPos)
2434  {
2435  SwNode* pNd = rOutlineNds[nPos];
2436  if (pNd->IsTextNode()) // should always be true
2437  {
2438  bool bOutlineContentVisibleAttr = true;
2439  pNd->GetTextNode()->GetAttrOutlineContentVisible(bOutlineContentVisibleAttr);
2440  if (!bOutlineContentVisibleAttr)
2441  {
2442  aFoldedOutlineNdsArray.push_back(pNd);
2443  pWrtShell->ToggleOutlineContentVisibility(nPos);
2444  }
2445  }
2446  }
2447  }
2448  // Moves all Nodes to the new position.
2449  // Bookmarks are moved too (currently without Undo support).
2450 
2451  // If footnotes are being moved to the special section, remove them now.
2452 
2453  // Or else delete the Frames for all footnotes that are being moved
2454  // and have it rebuild after the Move (footnotes can change pages).
2455  // Additionally we have to correct the FootnoteIdx array's sorting.
2456  bool bUpdateFootnote = false;
2457  SwFootnoteIdxs aTmpFntIdx;
2458 
2459  std::unique_ptr<SwUndoMove> pUndo;
2461  {
2462  pUndo.reset(new SwUndoMove( m_rDoc, rRange, rPos ));
2463  }
2464  else
2465  {
2466  bUpdateFootnote = lcl_SaveFootnote( rRange.aStart, rRange.aEnd, rPos,
2467  m_rDoc.GetFootnoteIdxs(), aTmpFntIdx );
2468  }
2469 
2470  SaveRedlines_t aSaveRedl;
2471  std::vector<SwRangeRedline*> aSavRedlInsPosArr;
2473  {
2474  lcl_SaveRedlines( rRange, aSaveRedl );
2475 
2476  // Find all RedLines that end at the InsPos.
2477  // These have to be moved back to the "old" position after the Move.
2478  SwRedlineTable::size_type nRedlPos = m_rDoc.getIDocumentRedlineAccess().GetRedlinePos( rPos.GetNode(), RedlineType::Any );
2479  if( SwRedlineTable::npos != nRedlPos )
2480  {
2481  const SwPosition *pRStt, *pREnd;
2482  do {
2484  pRStt = pTmp->Start();
2485  pREnd = pTmp->End();
2486  if( pREnd->nNode == rPos && pRStt->nNode < rPos )
2487  {
2488  aSavRedlInsPosArr.push_back( pTmp );
2489  }
2490  } while( pRStt->nNode < rPos && ++nRedlPos < m_rDoc.getIDocumentRedlineAccess().GetRedlineTable().size());
2491  }
2492  }
2493 
2494  // Copy all Bookmarks that are within the Move range into an array
2495  // that stores all references to positions as an offset.
2496  // The final mapping happens after the Move.
2497  std::vector< ::sw::mark::SaveBookmark> aSaveBkmks;
2498  DelBookmarks(rRange.aStart, rRange.aEnd, &aSaveBkmks);
2499 
2500  // Save the paragraph-bound Flys, so that they can be moved.
2501  SaveFlyArr aSaveFlyArr;
2502  if( !m_rDoc.GetSpzFrameFormats()->empty() )
2503  SaveFlyInRange( rRange, aSaveFlyArr );
2504 
2505  // Set it to before the Position, so that it cannot be moved further.
2506  SwNodeIndex aIdx( rPos, -1 );
2507 
2508  std::unique_ptr<SwNodeIndex> pSaveInsPos;
2509  if( pUndo )
2510  pSaveInsPos.reset(new SwNodeIndex( rRange.aStart, -1 ));
2511 
2512  // move the Nodes
2513  bool bNoDelFrames = bool(SwMoveFlags::NO_DELFRMS & eMvFlags);
2514  if( m_rDoc.GetNodes().MoveNodes( rRange, m_rDoc.GetNodes(), rPos, !bNoDelFrames ) )
2515  {
2516  ++aIdx; // again back to old position
2517  if( pSaveInsPos )
2518  ++(*pSaveInsPos);
2519  }
2520  else
2521  {
2522  aIdx = rRange.aStart;
2523  pUndo.reset();
2524  }
2525 
2526  // move the Flys to the new position
2527  if( !aSaveFlyArr.empty() )
2528  {
2529  SwPosition const tmp(aIdx);
2530  RestFlyInRange(aSaveFlyArr, tmp, nullptr);
2531  }
2532 
2533  // Add the Bookmarks back to the Document
2534  for(auto& rBkmk : aSaveBkmks)
2535  rBkmk.SetInDoc(&m_rDoc, aIdx);
2536 
2537  if( !aSavRedlInsPosArr.empty() )
2538  {
2539  SwNode* pNewNd = &aIdx.GetNode();
2540  for(SwRangeRedline* pTmp : aSavRedlInsPosArr)
2541  {
2543  {
2544  SwPosition* pEnd = pTmp->End();
2545  pEnd->nNode = aIdx;
2546  pEnd->nContent.Assign( pNewNd->GetContentNode(), 0 );
2547  }
2548  }
2549  }
2550 
2551  if( !aSaveRedl.empty() )
2552  lcl_RestoreRedlines( m_rDoc, aIdx.GetIndex(), aSaveRedl );
2553 
2554  if( pUndo )
2555  {
2556  pUndo->SetDestRange( aIdx, rPos, *pSaveInsPos );
2557  m_rDoc.GetIDocumentUndoRedo().AppendUndo(std::move(pUndo));
2558  }
2559 
2560  pSaveInsPos.reset();
2561 
2562  if( bUpdateFootnote )
2563  {
2564  if( !aTmpFntIdx.empty() )
2565  {
2566  m_rDoc.GetFootnoteIdxs().insert( aTmpFntIdx );
2567  aTmpFntIdx.clear();
2568  }
2569 
2571  }
2572 
2573  if (pWrtShell && pWrtShell->GetViewOptions() && pWrtShell->GetViewOptions()->IsShowOutlineContentVisibilityButton())
2574  {
2575  // fold all outlines that were folded before move
2576  for (SwNode* pNd : aFoldedOutlineNdsArray)
2577  pWrtShell->ToggleOutlineContentVisibility(pNd, true);
2578  }
2579 
2581  return true;
2582 }
2583 
2585 {
2586  SwNodeIndex aIdx( rPaM.Start()->nNode );
2587  bool bJoinText = aIdx.GetNode().IsTextNode();
2588  bool bOneNode = rPaM.GetPoint()->nNode == rPaM.GetMark()->nNode;
2589  aIdx--; // in front of the move area!
2590 
2591  bool bRet = MoveRange( rPaM, rPos, SwMoveFlags::DEFAULT );
2592  if( bRet && !bOneNode )
2593  {
2594  if( bJoinText )
2595  ++aIdx;
2596  SwTextNode * pTextNd = aIdx.GetNode().GetTextNode();
2597  SwNodeIndex aNxtIdx( aIdx );
2598  if( pTextNd && pTextNd->CanJoinNext( &aNxtIdx ) )
2599  {
2600  { // Block so SwIndex into node is deleted before Join
2601  m_rDoc.CorrRel( aNxtIdx, SwPosition( aIdx, SwIndex(pTextNd,
2602  pTextNd->GetText().getLength()) ), 0, true );
2603  }
2604  pTextNd->JoinNext();
2605  }
2606  }
2607  return bRet;
2608 }
2609 
2610 // Overwrite only uses the point of the PaM, the mark is ignored; characters
2611 // are replaced from point until the end of the node; at the end of the node,
2612 // characters are inserted.
2613 bool DocumentContentOperationsManager::Overwrite( const SwPaM &rRg, const OUString &rStr )
2614 {
2615  assert(rStr.getLength());
2616  SwPosition& rPt = *const_cast<SwPosition*>(rRg.GetPoint());
2617  if( m_rDoc.GetAutoCorrExceptWord() ) // Add to AutoCorrect
2618  {
2619  if( 1 == rStr.getLength() )
2620  m_rDoc.GetAutoCorrExceptWord()->CheckChar( rPt, rStr[ 0 ] );
2622  }
2623 
2624  SwTextNode *pNode = rPt.nNode.GetNode().GetTextNode();
2625  if (!pNode || rStr.getLength() > pNode->GetSpaceLeft()) // worst case: no erase
2626  {
2627  return false;
2628  }
2629 
2631  {
2632  m_rDoc.GetIDocumentUndoRedo().ClearRedo(); // AppendUndo not always called
2633  }
2634 
2635  const size_t nOldAttrCnt = pNode->GetpSwpHints()
2636  ? pNode->GetpSwpHints()->Count() : 0;
2637  SwDataChanged aTmp( rRg );
2638  SwIndex& rIdx = rPt.nContent;
2639  sal_Int32 const nActualStart(rIdx.GetIndex());
2640  sal_Int32 nStart = 0;
2641 
2642  bool bOldExpFlg = pNode->IsIgnoreDontExpand();
2643  pNode->SetIgnoreDontExpand( true );
2644 
2645  for( sal_Int32 nCnt = 0; nCnt < rStr.getLength(); ++nCnt )
2646  {
2647  // start behind the characters (to fix the attributes!)
2648  nStart = rIdx.GetIndex();
2649  if (nStart < pNode->GetText().getLength())
2650  {
2651  lcl_SkipAttr( pNode, rIdx, nStart );
2652  }
2653  sal_Unicode c = rStr[ nCnt ];
2655  {
2656  bool bMerged(false);
2658  {
2659  SwUndo *const pUndo = m_rDoc.GetUndoManager().GetLastUndo();
2660  SwUndoOverwrite *const pUndoOW(
2661  dynamic_cast<SwUndoOverwrite *>(pUndo) );
2662  if (pUndoOW)
2663  {
2664  // if CanGrouping() returns true it's already merged
2665  bMerged = pUndoOW->CanGrouping(m_rDoc, rPt, c);
2666  }
2667  }
2668  if (!bMerged)
2669  {
2671  std::make_unique<SwUndoOverwrite>(m_rDoc, rPt, c) );
2672  }
2673  }
2674  else
2675  {
2676  // start behind the characters (to fix the attributes!)
2677  if (nStart < pNode->GetText().getLength())
2678  ++rIdx;
2679  pNode->InsertText( OUString(c), rIdx, SwInsertFlags::EMPTYEXPAND );
2680  if( nStart+1 < rIdx.GetIndex() )
2681  {
2682  rIdx = nStart;
2683  pNode->EraseText( rIdx, 1 );
2684  ++rIdx;
2685  }
2686  }
2687  }
2688  pNode->SetIgnoreDontExpand( bOldExpFlg );
2689 
2690  const size_t nNewAttrCnt = pNode->GetpSwpHints()
2691  ? pNode->GetpSwpHints()->Count() : 0;
2692  if( nOldAttrCnt != nNewAttrCnt )
2693  {
2694  const SwUpdateAttr aHint(0,0,0);
2695  pNode->TriggerNodeUpdate(sw::LegacyModifyHint(&aHint, &aHint));
2696  }
2697 
2700  {
2701  SwPaM aPam(rPt.nNode, nActualStart, rPt.nNode, rPt.nContent.GetIndex());
2702  m_rDoc.getIDocumentRedlineAccess().DeleteRedline( aPam, true, RedlineType::Any );
2703  }
2705  {
2706  // FIXME: this redline is WRONG: there is no DELETE, and the skipped
2707  // characters are also included in aPam
2708  SwPaM aPam(rPt.nNode, nActualStart, rPt.nNode, rPt.nContent.GetIndex());
2709  m_rDoc.getIDocumentRedlineAccess().AppendRedline( new SwRangeRedline( RedlineType::Insert, aPam ), true);
2710  }
2711 
2713  return true;
2714 }
2715 
2716 bool DocumentContentOperationsManager::InsertString( const SwPaM &rRg, const OUString &rStr,
2717  const SwInsertFlags nInsertMode )
2718 {
2719  // tdf#119019 accept tracked paragraph formatting to do not hide new insertions
2721  {
2726  }
2727 
2728  // fetching DoesUndo is surprisingly expensive
2729  bool bDoesUndo = m_rDoc.GetIDocumentUndoRedo().DoesUndo();
2730  if (bDoesUndo)
2731  m_rDoc.GetIDocumentUndoRedo().ClearRedo(); // AppendUndo not always called!
2732 
2733  const SwPosition& rPos = *rRg.GetPoint();
2734 
2735  if( m_rDoc.GetAutoCorrExceptWord() ) // add to auto correction
2736  {
2737  if( 1 == rStr.getLength() && m_rDoc.GetAutoCorrExceptWord()->IsDeleted() )
2738  {
2739  m_rDoc.GetAutoCorrExceptWord()->CheckChar( rPos, rStr[ 0 ] );
2740  }
2742  }
2743 
2744  SwTextNode *const pNode = rPos.nNode.GetNode().GetTextNode();
2745  if(!pNode)
2746  return false;
2747 
2748  SwDataChanged aTmp( rRg );
2749 
2750  if (!bDoesUndo || !m_rDoc.GetIDocumentUndoRedo().DoesGroupUndo())
2751  {
2752  OUString const ins(pNode->InsertText(rStr, rPos.nContent, nInsertMode));
2753  if (bDoesUndo)
2754  {
2756  std::make_unique<SwUndoInsert>(rPos.nNode,
2757  rPos.nContent.GetIndex(), ins.getLength(), nInsertMode));
2758  }
2759  }
2760  else
2761  { // if Undo and grouping is enabled, everything changes!
2762  SwUndoInsert * pUndo = nullptr;
2763 
2764  // don't group the start if hints at the start should be expanded
2765  if (!(nInsertMode & SwInsertFlags::FORCEHINTEXPAND))
2766  {
2767  SwUndo *const pLastUndo = m_rDoc.GetUndoManager().GetLastUndo();
2768  SwUndoInsert *const pUndoInsert(
2769  dynamic_cast<SwUndoInsert *>(pLastUndo) );
2770  if (pUndoInsert && pUndoInsert->CanGrouping(rPos))
2771  {
2772  pUndo = pUndoInsert;
2773  }
2774  }
2775 
2776  CharClass const& rCC = GetAppCharClass();
2777  sal_Int32 nInsPos = rPos.nContent.GetIndex();
2778 
2779  if (!pUndo)
2780  {
2781  pUndo = new SwUndoInsert( rPos.nNode, nInsPos, 0, nInsertMode,
2782  !rCC.isLetterNumeric( rStr, 0 ) );
2783  m_rDoc.GetIDocumentUndoRedo().AppendUndo( std::unique_ptr<SwUndo>(pUndo) );
2784  }
2785 
2786  OUString const ins(pNode->InsertText(rStr, rPos.nContent, nInsertMode));
2787 
2788  for (sal_Int32 i = 0; i < ins.getLength(); ++i)
2789  {
2790  nInsPos++;
2791  // if CanGrouping() returns true, everything has already been done
2792  if (!pUndo->CanGrouping(ins[i]))
2793  {
2794  pUndo = new SwUndoInsert(rPos.nNode, nInsPos, 1, nInsertMode,
2795  !rCC.isLetterNumeric(ins, i));
2796  m_rDoc.GetIDocumentUndoRedo().AppendUndo( std::unique_ptr<SwUndo>(pUndo) );
2797  }
2798  }
2799  }
2800 
2801  // To-Do - add 'SwExtraRedlineTable' also ?
2803  {
2804  SwPaM aPam( rPos.nNode, aTmp.GetContent(),
2805  rPos.nNode, rPos.nContent.GetIndex());
2807  {
2809  new SwRangeRedline( RedlineType::Insert, aPam ), true);
2810  }
2811  else
2812  {
2814  }
2815  }
2816 
2818  return true;
2819 }
2820 
2822  const SwPaM& rPaM,
2823  utl::TransliterationWrapper& rTrans )
2824 {
2825  std::unique_ptr<SwUndoTransliterate> pUndo;
2827  pUndo.reset(new SwUndoTransliterate( rPaM, rTrans ));
2828 
2829  const SwPosition* pStt = rPaM.Start(),
2830  * pEnd = rPaM.End();
2831  sal_uLong nSttNd = pStt->nNode.GetIndex(),
2832  nEndNd = pEnd->nNode.GetIndex();
2833  sal_Int32 nSttCnt = pStt->nContent.GetIndex();
2834  sal_Int32 nEndCnt = pEnd->nContent.GetIndex();
2835 
2836  SwTextNode* pTNd = pStt->nNode.GetNode().GetTextNode();
2837  if( pStt == pEnd && pTNd ) // no selection?
2838  {
2839  // set current word as 'area of effect'
2840 
2842  Boundary aBndry = g_pBreakIt->GetBreakIter()->getWordBoundary(
2843  pTNd->GetText(), nSttCnt,
2844  g_pBreakIt->GetLocale( pTNd->GetLang( nSttCnt ) ),
2845  WordType::ANY_WORD /*ANYWORD_IGNOREWHITESPACES*/,
2846  true);
2847 
2848  if( aBndry.startPos < nSttCnt && nSttCnt < aBndry.endPos )
2849  {
2850  nSttCnt = aBndry.startPos;
2851  nEndCnt = aBndry.endPos;
2852  }
2853  }
2854 
2855  if( nSttNd != nEndNd ) // is more than one text node involved?
2856  {
2857  // iterate over all effected text nodes, the first and the last one
2858  // may be incomplete because the selection starts and/or ends there
2859 
2860  SwNodeIndex aIdx( pStt->nNode );
2861  if( nSttCnt )
2862  {
2863  ++aIdx;
2864  if( pTNd )
2865  pTNd->TransliterateText(
2866  rTrans, nSttCnt, pTNd->GetText().getLength(), pUndo.get());
2867  }
2868 
2869  for( ; aIdx.GetIndex() < nEndNd; ++aIdx )
2870  {
2871  pTNd = aIdx.GetNode().GetTextNode();
2872  if (pTNd)
2873  {
2874  pTNd->TransliterateText(
2875  rTrans, 0, pTNd->GetText().getLength(), pUndo.get());
2876  }
2877  }
2878 
2879  if( nEndCnt && nullptr != ( pTNd = pEnd->nNode.GetNode().GetTextNode() ))
2880  pTNd->TransliterateText( rTrans, 0, nEndCnt, pUndo.get() );
2881  }
2882  else if( pTNd && nSttCnt < nEndCnt )
2883  pTNd->TransliterateText( rTrans, nSttCnt, nEndCnt, pUndo.get() );
2884 
2885  if( pUndo && pUndo->HasData() )
2886  {
2887  m_rDoc.GetIDocumentUndoRedo().AppendUndo(std::move(pUndo));
2888  }
2890 }
2891 
2893  const SwPaM &rRg,
2894  const OUString& rGrfName,
2895  const OUString& rFltName,
2896  const Graphic* pGraphic,
2897  const SfxItemSet* pFlyAttrSet,
2898  const SfxItemSet* pGrfAttrSet,
2899  SwFrameFormat* pFrameFormat )
2900 {
2901  if( !pFrameFormat )
2903  SwGrfNode* pSwGrfNode = SwNodes::MakeGrfNode(
2905  rGrfName, rFltName, pGraphic,
2907  SwFlyFrameFormat* pSwFlyFrameFormat = InsNoTextNode( *rRg.GetPoint(), pSwGrfNode,
2908  pFlyAttrSet, pGrfAttrSet, pFrameFormat );
2909  return pSwFlyFrameFormat;
2910 }
2911 
2913  const SwPaM &rRg, const GraphicObject& rGrfObj,
2914  const SfxItemSet* pFlyAttrSet,
2915  const SfxItemSet* pGrfAttrSet )
2916 {
2918  SwGrfNode* pSwGrfNode = SwNodes::MakeGrfNode(
2920  rGrfObj, m_rDoc.GetDfltGrfFormatColl() );
2921  SwFlyFrameFormat* pSwFlyFrameFormat = InsNoTextNode( *rRg.GetPoint(), pSwGrfNode,
2922  pFlyAttrSet, pGrfAttrSet, pFrameFormat );
2923  return pSwFlyFrameFormat;
2924 }
2925 
2927  const SwPaM &rRg, const svt::EmbeddedObjectRef& xObj,
2928  SfxItemSet* pFlyAttrSet)
2929 {
2930  sal_uInt16 nId = RES_POOLFRM_OLE;
2931  if (xObj.is())
2932  {
2933  SvGlobalName aClassName( xObj->getClassID() );
2934  if (SotExchange::IsMath(aClassName))
2935  {
2936  nId = RES_POOLFRM_FORMEL;
2937  }
2938  }
2939 
2941 
2942  return InsNoTextNode( *rRg.GetPoint(), m_rDoc.GetNodes().MakeOLENode(
2944  xObj,
2946  pFlyAttrSet, nullptr,
2947  pFrameFormat );
2948 }
2949 
2951  sal_Int64 nAspect,
2952  const SfxItemSet* pFlyAttrSet,
2953  const SfxItemSet* pGrfAttrSet)
2954 {
2956 
2957  return InsNoTextNode( *rRg.GetPoint(),
2960  rObjName,
2961  nAspect,
2963  nullptr ),
2964  pFlyAttrSet, pGrfAttrSet,
2965  pFrameFormat );
2966 }
2967 
2968 void DocumentContentOperationsManager::ReRead( SwPaM& rPam, const OUString& rGrfName,
2969  const OUString& rFltName, const Graphic* pGraphic )
2970 {
2971  SwGrfNode *pGrfNd;
2972  if( !(( !rPam.HasMark()
2973  || rPam.GetPoint()->nNode.GetIndex() == rPam.GetMark()->nNode.GetIndex() )
2974  && nullptr != ( pGrfNd = rPam.GetPoint()->nNode.GetNode().GetGrfNode() )) )
2975  return;
2976 
2978  {
2979  m_rDoc.GetIDocumentUndoRedo().AppendUndo(std::make_unique<SwUndoReRead>(rPam, *pGrfNd));
2980  }
2981 
2982  // Because we don't know if we can mirror the graphic, the mirror attribute is always reset
2983  if( MirrorGraph::Dont != pGrfNd->GetSwAttrSet().
2984  GetMirrorGrf().GetValue() )
2985  pGrfNd->SetAttr( SwMirrorGrf() );
2986 
2987  pGrfNd->ReRead( rGrfName, rFltName, pGraphic );
2989 }
2990 
2991 // Insert drawing object, which has to be already inserted in the DrawModel
2993  const SwPaM &rRg,
2994  SdrObject& rDrawObj,
2995  const SfxItemSet& rFlyAttrSet )
2996 {
2998 
2999  const SwFormatAnchor* pAnchor = nullptr;
3000  rFlyAttrSet.GetItemState( RES_ANCHOR, false, reinterpret_cast<const SfxPoolItem**>(&pAnchor) );
3001  pFormat->SetFormatAttr( rFlyAttrSet );
3002 
3003  // Didn't set the Anchor yet?
3004  // DrawObjecte must never end up in the Header/Footer!
3005  RndStdIds eAnchorId = pAnchor != nullptr ? pAnchor->GetAnchorId() : pFormat->GetAnchor().GetAnchorId();
3006  const bool bIsAtContent = (RndStdIds::FLY_AT_PAGE != eAnchorId);
3007 
3008  const SwNodeIndex* pChkIdx = nullptr;
3009  if ( pAnchor == nullptr )
3010  {
3011  pChkIdx = &rRg.GetPoint()->nNode;
3012  }
3013  else if ( bIsAtContent )
3014  {
3015  pChkIdx =
3016  pAnchor->GetContentAnchor() ? &pAnchor->GetContentAnchor()->nNode : &rRg.GetPoint()->nNode;
3017  }
3018 
3019  // allow drawing objects in header/footer, but control objects aren't allowed in header/footer.
3020  if( pChkIdx != nullptr
3021  && ::CheckControlLayer( &rDrawObj )
3022  && m_rDoc.IsInHeaderFooter( *pChkIdx ) )
3023  {
3024  // apply at-page anchor format
3025  eAnchorId = RndStdIds::FLY_AT_PAGE;
3026  pFormat->SetFormatAttr( SwFormatAnchor( eAnchorId ) );
3027  }
3028  else if( pAnchor == nullptr
3029  || ( bIsAtContent
3030  && pAnchor->GetContentAnchor() == nullptr ) )
3031  {
3032  // apply anchor format
3033  SwFormatAnchor aAnch( pAnchor != nullptr ? *pAnchor : pFormat->GetAnchor() );
3034  eAnchorId = aAnch.GetAnchorId();
3035  if ( eAnchorId == RndStdIds::FLY_AT_FLY )
3036  {
3037  const SwStartNode* pStartNode = rRg.GetNode().FindFlyStartNode();
3038  assert(pStartNode);
3039  SwPosition aPos(*pStartNode);
3040  aAnch.SetAnchor( &aPos );
3041  }
3042  else
3043  {
3044  aAnch.SetAnchor( rRg.GetPoint() );
3045  if ( eAnchorId == RndStdIds::FLY_AT_PAGE )
3046  {
3047  eAnchorId = dynamic_cast<const SdrUnoObj*>( &rDrawObj) != nullptr ? RndStdIds::FLY_AS_CHAR : RndStdIds::FLY_AT_PARA;
3048  aAnch.SetType( eAnchorId );
3049  }
3050  }
3051  pFormat->SetFormatAttr( aAnch );
3052  }
3053 
3054  // insert text attribute for as-character anchored drawing object
3055  if ( eAnchorId == RndStdIds::FLY_AS_CHAR )
3056  {
3057  bool bAnchorAtPageAsFallback = true;
3058  const SwFormatAnchor& rDrawObjAnchorFormat = pFormat->GetAnchor();
3059  if ( rDrawObjAnchorFormat.GetContentAnchor() != nullptr )
3060  {
3061  SwTextNode* pAnchorTextNode =
3062  rDrawObjAnchorFormat.GetContentAnchor()->nNode.GetNode().GetTextNode();
3063  if ( pAnchorTextNode != nullptr )
3064  {
3065  const sal_Int32 nStt = rDrawObjAnchorFormat.GetContentAnchor()->nContent.GetIndex();
3066  SwFormatFlyCnt aFormat( pFormat );
3067  pAnchorTextNode->InsertItem( aFormat, nStt, nStt );
3068  bAnchorAtPageAsFallback = false;
3069  }
3070  }
3071 
3072  if ( bAnchorAtPageAsFallback )
3073  {
3074  OSL_ENSURE( false, "DocumentContentOperationsManager::InsertDrawObj(..) - missing content anchor for as-character anchored drawing object --> anchor at-page" );
3075  pFormat->SetFormatAttr( SwFormatAnchor( RndStdIds::FLY_AT_PAGE ) );
3076  }
3077  }
3078 
3079  SwDrawContact* pContact = new SwDrawContact( pFormat, &rDrawObj );
3080 
3081  // Create Frames if necessary
3083  {
3084  // create layout representation
3085  pFormat->MakeFrames();
3086  // #i42319# - follow-up of #i35635#
3087  // move object to visible layer
3088  // #i79391#
3089  if ( pContact->GetAnchorFrame() )
3090  {
3091  pContact->MoveObjToVisibleLayer( &rDrawObj );
3092  }
3093  }
3094 
3096  {
3097  m_rDoc.GetIDocumentUndoRedo().AppendUndo( std::make_unique<SwUndoInsLayFormat>(pFormat, 0, 0) );
3098  }
3099 
3101  return pFormat;
3102 }
3103 
3104 bool DocumentContentOperationsManager::SplitNode( const SwPosition &rPos, bool bChkTableStart )
3105 {
3106  SwContentNode *pNode = rPos.nNode.GetNode().GetContentNode();
3107  if(nullptr == pNode)
3108  return false;
3109 
3110  {
3111  // BUG 26675: Send DataChanged before deleting, so that we notice which objects are in scope.
3112  // After that they can be before/after the position.
3113  SwDataChanged aTmp( m_rDoc, rPos );
3114  }
3115 
3116  SwUndoSplitNode* pUndo = nullptr;
3118  {
3120  // insert the Undo object (currently only for TextNode)
3121  if( pNode->IsTextNode() )
3122  {
3123  pUndo = new SwUndoSplitNode( m_rDoc, rPos, bChkTableStart );
3124  m_rDoc.GetIDocumentUndoRedo().AppendUndo(std::unique_ptr<SwUndo>(pUndo));
3125  }
3126  }
3127 
3128  // Update the rsid of the old and the new node unless
3129  // the old node is split at the beginning or at the end
3130  SwTextNode *pTextNode = rPos.nNode.GetNode().GetTextNode();
3131  const sal_Int32 nPos = rPos.nContent.GetIndex();
3132  if( pTextNode && nPos && nPos != pTextNode->Len() )
3133  {
3134  m_rDoc.UpdateParRsid( pTextNode );
3135  }
3136 
3137  //JP 28.01.97: Special case for SplitNode at table start:
3138  // If it is at the beginning of a Doc/Fly/Footer/... or right at after a table
3139  // then insert a paragraph before it.
3140  if( bChkTableStart && !rPos.nContent.GetIndex() && pNode->IsTextNode() )
3141  {
3142  sal_uLong nPrevPos = rPos.nNode.GetIndex() - 1;
3143  const SwTableNode* pTableNd;
3144  const SwNode* pNd = m_rDoc.GetNodes()[ nPrevPos ];
3145  if( pNd->IsStartNode() &&
3146  SwTableBoxStartNode == static_cast<const SwStartNode*>(pNd)->GetStartNodeType() &&
3147  nullptr != ( pTableNd = m_rDoc.GetNodes()[ --nPrevPos ]->GetTableNode() ) &&
3148  ((( pNd = m_rDoc.GetNodes()[ --nPrevPos ])->IsStartNode() &&
3149  SwTableBoxStartNode != static_cast<const SwStartNode*>(pNd)->GetStartNodeType() )
3150  || ( pNd->IsEndNode() && pNd->StartOfSectionNode()->IsTableNode() )
3151  || pNd->IsContentNode() ))
3152  {
3153  if( pNd->IsContentNode() )
3154  {
3155  //JP 30.04.99 Bug 65660:
3156  // There are no page breaks outside of the normal body area,
3157  // so this is not a valid condition to insert a paragraph.
3158  if( nPrevPos < m_rDoc.GetNodes().GetEndOfExtras().GetIndex() )
3159  pNd = nullptr;
3160  else
3161  {
3162  // Only if the table has page breaks!
3163  const SwFrameFormat* pFrameFormat = pTableNd->GetTable().GetFrameFormat();
3164  if( SfxItemState::SET != pFrameFormat->GetItemState(RES_PAGEDESC, false) &&
3165  SfxItemState::SET != pFrameFormat->GetItemState( RES_BREAK, false ) )
3166  pNd = nullptr;
3167  }
3168  }
3169 
3170  if( pNd )
3171  {
3172  SwTextNode* pTextNd = m_rDoc.GetNodes().MakeTextNode(
3173  SwNodeIndex( *pTableNd ),
3175  if( pTextNd )
3176  {
3177  const_cast<SwPosition&>(rPos).nNode = pTableNd->GetIndex()-1;
3178  const_cast<SwPosition&>(rPos).nContent.Assign( pTextNd, 0 );
3179 
3180  // only add page breaks/styles to the body area
3181  if( nPrevPos > m_rDoc.GetNodes().GetEndOfExtras().GetIndex() )
3182  {
3183  SwFrameFormat* pFrameFormat = pTableNd->GetTable().GetFrameFormat();
3184  const SfxPoolItem *pItem;
3185  if( SfxItemState::SET == pFrameFormat->GetItemState( RES_PAGEDESC,
3186  false, &pItem ) )
3187  {
3188  pTextNd->SetAttr( *pItem );
3189  pFrameFormat->ResetFormatAttr( RES_PAGEDESC );
3190  }
3191  if( SfxItemState::SET == pFrameFormat->GetItemState( RES_BREAK,
3192  false, &pItem ) )
3193  {
3194  pTextNd->SetAttr( *pItem );
3195  pFrameFormat->ResetFormatAttr( RES_BREAK );
3196  }
3197  }
3198 
3199  if( pUndo )
3200  pUndo->SetTableFlag();
3202  return true;
3203  }
3204  }
3205  }
3206  }
3207 
3208  const std::shared_ptr<sw::mark::ContentIdxStore> pContentStore(sw::mark::ContentIdxStore::Create());
3209  pContentStore->Save( m_rDoc, rPos.nNode.GetIndex(), rPos.nContent.GetIndex(), true );
3210  assert(pNode->IsTextNode());
3211  std::function<void (SwTextNode *, sw::mark::RestoreMode)> restoreFunc(
3212  [&](SwTextNode *const, sw::mark::RestoreMode const eMode)
3213  {
3214  if (!pContentStore->Empty())
3215  { // move all bookmarks, TOXMarks, FlyAtCnt
3216  pContentStore->Restore(m_rDoc, rPos.nNode.GetIndex()-1, 0, true, eMode);
3217  }
3218  if (eMode & sw::mark::RestoreMode::NonFlys)
3219  {
3220  // To-Do - add 'SwExtraRedlineTable' also ?
3224  {
3225  SwPaM aPam( rPos );
3226  aPam.SetMark();
3227  aPam.Move( fnMoveBackward );
3229  {
3231  new SwRangeRedline(RedlineType::Insert, aPam), true);
3232  }
3233  else
3234  {
3236  }
3237  }
3238  }
3239  });
3240  pNode->GetTextNode()->SplitContentNode(rPos, &restoreFunc);
3241 
3243  return true;
3244 }
3245 
3247 {
3248  // create new node before EndOfContent
3249  SwTextNode * pCurNode = rPos.nNode.GetNode().GetTextNode();
3250  if( !pCurNode )
3251  {
3252  // so then one can be created!
3253  SwNodeIndex aIdx( rPos.nNode, 1 );
3254  pCurNode = m_rDoc.GetNodes().MakeTextNode( aIdx,
3256  }
3257  else
3258  pCurNode = pCurNode->AppendNode( rPos )->GetTextNode();
3259 
3260  rPos.nNode++;
3261  rPos.nContent.Assign( pCurNode, 0 );
3262 
3264  {
3265  m_rDoc.GetIDocumentUndoRedo().AppendUndo( std::make_unique<SwUndoInsert>( rPos.nNode ) );
3266  }
3267 
3268  // To-Do - add 'SwExtraRedlineTable' also ?
3270  {
3271  SwPaM aPam( rPos );
3272  aPam.SetMark();
3273  aPam.Move( fnMoveBackward );
3275  m_rDoc.getIDocumentRedlineAccess().AppendRedline( new SwRangeRedline( RedlineType::Insert, aPam ), true);
3276  else
3278  }
3279 
3281  return true;
3282 }
3283 
3284 bool DocumentContentOperationsManager::ReplaceRange( SwPaM& rPam, const OUString& rStr,
3285  const bool bRegExReplace )
3286 {
3287  // unfortunately replace works slightly differently from delete,
3288  // so we cannot use lcl_DoWithBreaks here...
3289 
3290  std::vector<std::pair<sal_uLong, sal_Int32>> Breaks;
3291 
3292  SwPaM aPam( *rPam.GetMark(), *rPam.GetPoint() );
3293  aPam.Normalize(false);
3294  if (aPam.GetPoint()->nNode != aPam.GetMark()->nNode)
3295  {
3296  aPam.Move(fnMoveBackward);
3297  }
3298  OSL_ENSURE((aPam.GetPoint()->nNode == aPam.GetMark()->nNode), "invalid pam?");
3299 
3300  sw::CalcBreaks(Breaks, aPam);
3301 
3302  while (!Breaks.empty() // skip over prefix of dummy chars
3303  && (aPam.GetMark()->nNode.GetIndex() == Breaks.begin()->first)
3304  && (aPam.GetMark()->nContent.GetIndex() == Breaks.begin()->second))
3305  {
3306  // skip!
3307  ++aPam.GetMark()->nContent; // always in bounds if Breaks valid
3308  Breaks.erase(Breaks.begin());
3309  }
3310  *rPam.Start() = *aPam.GetMark(); // update start of original pam w/ prefix
3311 
3312  if (Breaks.empty())
3313  {
3314  // park aPam somewhere so it does not point to node that is deleted
3315  aPam.DeleteMark();
3317  return ReplaceRangeImpl(rPam, rStr, bRegExReplace); // original pam!
3318  }
3319 
3320  // Deletion must be split into several parts if the text node
3321  // contains a text attribute with end and with dummy character
3322  // and the selection does not contain the text attribute completely,
3323  // but overlaps its start (left), where the dummy character is.
3324 
3325  bool bRet( true );
3326  // iterate from end to start, to avoid invalidating the offsets!
3327  auto iter( Breaks.rbegin() );
3328  sal_uLong nOffset(0);
3329  SwNodes const& rNodes(rPam.GetPoint()->nNode.GetNodes());
3330  OSL_ENSURE(aPam.GetPoint() == aPam.End(), "wrong!");
3331  SwPosition & rEnd( *aPam.End() );
3332  SwPosition & rStart( *aPam.Start() );
3333 
3334  // set end of temp pam to original end (undo Move backward above)
3335  rEnd = *rPam.End();
3336  // after first deletion, rEnd will point into the original text node again!
3337 
3338  while (iter != Breaks.rend())
3339  {
3340  rStart = SwPosition(*rNodes[iter->first - nOffset]->GetTextNode(), iter->second + 1);
3341  if (rStart < rEnd) // check if part is empty
3342  {
3345  : DeleteAndJoinImpl(aPam, false);
3346  nOffset = iter->first - rStart.nNode.GetIndex(); // deleted fly nodes...
3347  }
3348  rEnd = SwPosition(*rNodes[iter->first - nOffset]->GetTextNode(), iter->second);
3349  ++iter;
3350  }
3351 
3352  rStart = *rPam.Start(); // set to original start
3353  assert(rStart < rEnd && "replace part empty!");
3354  if (rStart < rEnd) // check if part is empty
3355  {
3356  bRet &= ReplaceRangeImpl(aPam, rStr, bRegExReplace);
3357  }
3358 
3359  rPam = aPam; // update original pam (is this required?)
3360 
3361  return bRet;
3362 }
3363 
3366  const SwPaM &rRg,
3367  const SfxPoolItem &rHt,
3368  const SetAttrMode nFlags,
3369  SwRootFrame const*const pLayout,
3370  const bool bExpandCharToPara,
3371  SwTextAttr **ppNewTextAttr)
3372 {
3374  return false;
3375 
3376  SwDataChanged aTmp( rRg );
3377  std::unique_ptr<SwUndoAttr> pUndoAttr;
3379  {
3381  pUndoAttr.reset(new SwUndoAttr( rRg, rHt, nFlags ));
3382  }
3383 
3384  SfxItemSet aSet( m_rDoc.GetAttrPool(), {{rHt.Which(), rHt.Which()}} );
3385  aSet.Put( rHt );
3386  const bool bRet = lcl_InsAttr(m_rDoc, rRg, aSet, nFlags, pUndoAttr.get(), pLayout, bExpandCharToPara, ppNewTextAttr);
3387 
3389  {
3390  m_rDoc.GetIDocumentUndoRedo().AppendUndo( std::move(pUndoAttr) );
3391  }
3392 
3393  if( bRet )
3394  {
3396  }
3397  return bRet;
3398 }
3399 
3401  const SetAttrMode nFlags, SwRootFrame const*const pLayout)
3402 {
3403  SwDataChanged aTmp( rRg );
3404  std::unique_ptr<SwUndoAttr> pUndoAttr;
3406  {
3408  pUndoAttr.reset(new SwUndoAttr( rRg, rSet, nFlags ));
3409  }
3410 
3411  bool bRet = lcl_InsAttr(m_rDoc, rRg, rSet, nFlags, pUndoAttr.get(), pLayout, /*bExpandCharToPara*/false, /*ppNewTextAttr*/nullptr );
3412 
3414  {
3415  m_rDoc.GetIDocumentUndoRedo().AppendUndo( std::move(pUndoAttr) );
3416  }
3417 
3418  if( bRet )
3420 }
3421 
3423 {
3424  const SwTextNode* pTNd = rPos.nNode.GetNode().GetTextNode();
3425  if ( !pTNd )
3426  return;
3427 
3428  const OUString& rText = pTNd->GetText();
3429  sal_Int32 nIdx = 0;
3430  while (nIdx < rText.getLength())
3431  {
3432  sal_Unicode const cCh = rText[nIdx];
3433  if (('\t' != cCh) && (' ' != cCh))
3434  {
3435  break;
3436  }
3437  ++nIdx;
3438  }
3439 
3440  if ( nIdx > 0 )
3441  {
3442  SwPaM aPam(rPos);
3443  aPam.GetPoint()->nContent = 0;
3444  aPam.SetMark();
3445  aPam.GetMark()->nContent = nIdx;
3446  DeleteRange( aPam );
3447  }
3448 }
3449 
3450 // Copy method from SwDoc - "copy Flys in Flys"
3454  const SwNodeRange& rRg,
3455  const SwNodeIndex& rInsPos,
3456  const std::pair<const SwPaM&, const SwPosition&>* pCopiedPaM /*and real insert pos*/,
3457  const bool bMakeNewFrames,
3458  const bool bDelRedlines,
3459  const bool bCopyFlyAtFly,
3460  SwCopyFlags const flags) const
3461 {
3462  assert(!pCopiedPaM || pCopiedPaM->first.End()->nNode == rRg.aEnd);
3463  assert(!pCopiedPaM || pCopiedPaM->second.nNode <= rInsPos);
3464 
3465  SwDoc& rDest = rInsPos.GetNode().GetDoc();
3466  SwNodeIndex aSavePos( rInsPos );
3467 
3468  if (rRg.aStart != rRg.aEnd)
3469  {
3470  bool bEndIsEqualEndPos = rInsPos == rRg.aEnd;
3471  bool isRecreateEndNode(false);
3472  --aSavePos;
3473  SaveRedlEndPosForRestore aRedlRest( rInsPos, 0 );
3474 
3475  // insert behind the already copied start node
3476  m_rDoc.GetNodes().CopyNodes( rRg, rInsPos, false, true );
3477  aRedlRest.Restore();
3478  if (bMakeNewFrames) // tdf#130685 only after aRedlRest
3479  { // recreate from previous node (could be merged now)
3480  if (SwTextNode *const pNode = aSavePos.GetNode().GetTextNode())
3481  {
3483  SwTextNode *const pEndNode = rInsPos.GetNode().GetTextNode();
3484  if (pEndNode)
3485  {
3487  for (SwTextFrame* pFrame = aIter.First(); pFrame; pFrame = aIter.Next())
3488  {
3489  if (pFrame->getRootFrame()->IsHideRedlines())
3490  {
3491  frames.insert(pFrame);
3492  }
3493  }
3494  }
3496  if (!frames.empty())
3497  { // tdf#132187 check if the end node needs new frames
3499  for (SwTextFrame* pFrame = aIter.First(); pFrame; pFrame = aIter.Next())
3500  {
3501  if (pFrame->getRootFrame()->IsHideRedlines())
3502  {
3503  auto const it = frames.find(pFrame);
3504  if (it != frames.end())
3505  {
3506  frames.erase(it);
3507  }
3508  }
3509  }
3510  if (!frames.empty()) // existing frame was deleted
3511  { // all layouts because MakeFrames recreates all layouts
3512  pEndNode->DelFrames(nullptr);
3513  isRecreateEndNode = true;
3514  }
3515  }
3516  }
3517  }
3518  bool const isAtStartOfSection(aSavePos.GetNode().IsStartNode());
3519  ++aSavePos;
3520  if (bMakeNewFrames)
3521  {
3522  // it's possible that CheckParaRedlineMerge() deleted frames
3523  // on rInsPos so have to include it, but it must not be included
3524  // if it was the first node in the document so that MakeFrames()
3525  // will find the existing (wasn't deleted) frame on it
3526  SwNodeIndex const end(rInsPos,
3527  (!isRecreateEndNode || isAtStartOfSection)
3528  ? 0 : +1);
3529  ::MakeFrames(&rDest, aSavePos, end);
3530  }
3531  if (bEndIsEqualEndPos)
3532  {
3533  const_cast<SwNodeIndex&>(rRg.aEnd) = aSavePos;
3534  }
3535  }
3536 
3537 #if OSL_DEBUG_LEVEL > 0
3538  {
3539  //JP 17.06.99: Bug 66973 - check count only if the selection is in
3540  // the same section or there's no section, because sections that are
3541  // not fully selected are not copied.
3542  const SwSectionNode* pSSectNd = rRg.aStart.GetNode().FindSectionNode();
3543  SwNodeIndex aTmpI( rRg.aEnd, -1 );
3544  const SwSectionNode* pESectNd = aTmpI.GetNode().FindSectionNode();
3545  if( pSSectNd == pESectNd &&
3546  !rRg.aStart.GetNode().IsSectionNode() &&
3547  !aTmpI.GetNode().IsEndNode() )
3548  {
3549  // If the range starts with a SwStartNode, it isn't copied
3550  sal_uInt16 offset = (rRg.aStart.GetNode().GetNodeType() != SwNodeType::Start) ? 1 : 0;
3551  OSL_ENSURE( rInsPos.GetIndex() - aSavePos.GetIndex() ==
3552  rRg.aEnd.GetIndex() - rRg.aStart.GetIndex() - 1 + offset,
3553  "An insufficient number of nodes were copied!" );
3554  }
3555  }
3556 #endif
3557 
3558  {
3559  ::sw::UndoGuard const undoGuard(rDest.GetIDocumentUndoRedo());
3560  CopyFlyInFlyImpl(rRg, pCopiedPaM ? &pCopiedPaM->first : nullptr,
3561  // see comment below regarding use of pCopiedPaM->second
3562  (pCopiedPaM && rRg.aStart != pCopiedPaM->first.Start()->nNode)
3563  ? pCopiedPaM->second.nNode
3564  : aSavePos,
3565  bCopyFlyAtFly,
3566  flags);
3567  }
3568 
3569  SwNodeRange aCpyRange( aSavePos, rInsPos );
3570 
3571  // Also copy all bookmarks
3572  // guess this must be done before the DelDummyNodes below as that
3573  // deletes nodes so would mess up the index arithmetic
3575  {
3576  SwPaM aRgTmp( rRg.aStart, rRg.aEnd );
3577  SwPaM aCpyPaM(aCpyRange.aStart, aCpyRange.aEnd);
3578  if (pCopiedPaM && rRg.aStart != pCopiedPaM->first.Start()->nNode)
3579  {
3580  // there is 1 (partially selected, maybe) paragraph before
3581  assert(SwNodeIndex(rRg.aStart, -1) == pCopiedPaM->first.Start()->nNode);
3582  // only use the passed in target SwPosition if the source PaM point
3583  // is on a different node; if it was the same node then the target
3584  // position was likely moved along by the copy operation and now
3585  // points to the end of the range!
3586  *aCpyPaM.GetPoint() = pCopiedPaM->second;
3587  }
3588 
3589  sw::CopyBookmarks(pCopiedPaM ? pCopiedPaM->first : aRgTmp, *aCpyPaM.Start());
3590  }
3591 
3592  if( bDelRedlines && ( RedlineFlags::DeleteRedlines & rDest.getIDocumentRedlineAccess().GetRedlineFlags() ))
3593  lcl_DeleteRedlines( rRg, aCpyRange );
3594 
3595  rDest.GetNodes().DelDummyNodes( aCpyRange );
3596 }
3597 
3598 // note: for the redline Show/Hide this must be in sync with
3599 // SwRangeRedline::CopyToSection()/DelCopyOfSection()/MoveFromSection()
3601  const SwNodeRange& rRg,
3602  SwPaM const*const pCopiedPaM,
3603  const SwNodeIndex& rStartIdx,
3604  const bool bCopyFlyAtFly,
3605  SwCopyFlags const flags) const
3606 {
3607  assert(!pCopiedPaM || pCopiedPaM->End()->nNode == rRg.aEnd);
3608 
3609  // First collect all Flys, sort them according to their ordering number,
3610  // and then only copy them. This maintains the ordering numbers (which are only
3611  // managed in the DrawModel).
3612  SwDoc& rDest = rStartIdx.GetNode().GetDoc();
3613  std::set< ZSortFly > aSet;
3614  const size_t nArrLen = m_rDoc.GetSpzFrameFormats()->size();
3615 
3616  SwTextBoxHelper::SavedLink aOldTextBoxes;
3618 
3619  for ( size_t n = 0; n < nArrLen; ++n )
3620  {
3621  SwFrameFormat* pFormat = (*m_rDoc.GetSpzFrameFormats())[n];
3622  SwFormatAnchor const*const pAnchor = &pFormat->GetAnchor();
3623  SwPosition const*const pAPos = pAnchor->GetContentAnchor();
3624  if ( !pAPos )
3625  continue;
3626  bool bAdd = false;
3627  sal_uLong nSkipAfter = pAPos->nNode.GetIndex();
3628  sal_uLong nStart = rRg.aStart.GetIndex();
3629  switch ( pAnchor->GetAnchorId() )
3630  {
3631  case RndStdIds::FLY_AT_FLY:
3632  if(bCopyFlyAtFly)
3633  ++nSkipAfter;
3635  ++nStart;
3636  break;
3637  case RndStdIds::FLY_AT_PARA:
3638  {
3639  bAdd = IsSelectFrameAnchoredAtPara(*pAPos,
3640  pCopiedPaM ? *pCopiedPaM->Start() : SwPosition(rRg.aStart),
3641  pCopiedPaM ? *pCopiedPaM->End() : SwPosition(rRg.aEnd),
3642  (flags & SwCopyFlags::IsMoveToFly)
3645  }
3646  break;
3647  case RndStdIds::FLY_AT_CHAR:
3648  {
3649  bAdd = IsDestroyFrameAnchoredAtChar(*pAPos,
3650  pCopiedPaM ? *pCopiedPaM->Start() : SwPosition(rRg.aStart),
3651  pCopiedPaM ? *pCopiedPaM->End() : SwPosition(rRg.aEnd),
3652  (flags & SwCopyFlags::IsMoveToFly)
3655  }
3656  break;
3657  default:
3658  continue;
3659  }
3660  if (RndStdIds::FLY_AT_FLY == pAnchor->GetAnchorId())
3661  {
3662  if (nStart > nSkipAfter)
3663  continue;
3664  if (pAPos->nNode > rRg.aEnd)
3665  continue;
3666  //frames at the last source node are not always copied:
3667  //- if the node is empty and is the last node of the document or a table cell
3668  // or a text frame then they have to be copied
3669  //- if the content index in this node is > 0 then paragraph and frame bound objects are copied
3670  //- to-character bound objects are copied if their index is <= nEndContentIndex
3671  if (pAPos->nNode < rRg.aEnd)
3672  bAdd = true;
3673  if (!bAdd && !m_rDoc.getIDocumentRedlineAccess().IsRedlineMove()) // fdo#40599: not for redline move
3674  {
3675  if (!bAdd)
3676  {
3677  // technically old code checked nContent of AT_FLY which is pointless
3678  bAdd = pCopiedPaM && 0 < pCopiedPaM->End()->nContent.GetIndex();
3679  }
3680  }
3681  }
3682  if( bAdd )
3683  {
3684  aSet.insert( ZSortFly( pFormat, pAnchor, nArrLen + aSet.size() ));
3685  }
3686  }
3687 
3688  // Store all copied (and also the newly created) frames in another array.
3689  // They are stored as matching the originals, so that we will be later
3690  // able to build the chains accordingly.
3691  std::vector< SwFrameFormat* > aVecSwFrameFormat;
3692  std::set< ZSortFly >::const_iterator it=aSet.begin();
3693 
3694  while (it != aSet.end())
3695  {
3696  // #i59964#
3697  // correct determination of new anchor position
3698  SwFormatAnchor aAnchor( *(*it).GetAnchor() );
3699  assert( aAnchor.GetContentAnchor() != nullptr );
3700  SwPosition newPos = *aAnchor.GetContentAnchor();
3701  // for at-paragraph and at-character anchored objects the new anchor
3702  // position can *not* be determined by the difference of the current
3703  // anchor position to the start of the copied range, because not
3704  // complete selected sections in the copied range aren't copied - see
3705  // method <SwNodes::CopyNodes(..)>.
3706  // Thus, the new anchor position in the destination document is found
3707  // by counting the text nodes.
3708  if ((aAnchor.GetAnchorId() == RndStdIds::FLY_AT_PARA) ||
3709  (aAnchor.GetAnchorId() == RndStdIds::FLY_AT_CHAR) )
3710  {
3711  // First, determine number of anchor text node in the copied range.
3712  // Note: The anchor text node *have* to be inside the copied range.
3713  sal_uLong nAnchorTextNdNumInRange( 0 );
3714  bool bAnchorTextNdFound( false );
3715  // start at the first node for which flys are copied
3716  SwNodeIndex aIdx(pCopiedPaM ? pCopiedPaM->Start()->nNode : rRg.aStart);
3717  while ( !bAnchorTextNdFound && aIdx <= rRg.aEnd )
3718  {
3719  if ( aIdx.GetNode().IsTextNode() )
3720  {
3721  ++nAnchorTextNdNumInRange;
3722  bAnchorTextNdFound = aAnchor.GetContentAnchor()->nNode == aIdx;
3723  }
3724 
3725  ++aIdx;
3726  }
3727 
3728  if ( !bAnchorTextNdFound )
3729  {
3730  // This case can *not* happen, but to be robust take the first
3731  // text node in the destination document.
3732  OSL_FAIL( "<SwDoc::_CopyFlyInFly(..)> - anchor text node in copied range not found" );
3733  nAnchorTextNdNumInRange = 1;
3734  }
3735  // Second, search corresponding text node in destination document
3736  // by counting forward from start insert position <rStartIdx> the
3737  // determined number of text nodes.
3738  aIdx = rStartIdx;
3739  SwNodeIndex aAnchorNdIdx( rStartIdx );
3740  const SwNode& aEndOfContentNd =
3741  aIdx.GetNode().GetNodes().GetEndOfContent();
3742  while ( nAnchorTextNdNumInRange > 0 &&
3743  &(aIdx.GetNode()) != &aEndOfContentNd )
3744  {
3745  if ( aIdx.GetNode().IsTextNode() )
3746  {
3747  --nAnchorTextNdNumInRange;
3748  aAnchorNdIdx = aIdx;
3749  }
3750 
3751  ++aIdx;
3752  }
3753  if ( !aAnchorNdIdx.GetNode().IsTextNode() )
3754  {
3755  // This case can *not* happen, but to be robust take the first
3756  // text node in the destination document.
3757  OSL_FAIL( "<SwDoc::_CopyFlyInFly(..)> - found anchor node index isn't a text node" );
3758  aAnchorNdIdx = rStartIdx;
3759  while ( !aAnchorNdIdx.GetNode().IsTextNode() )
3760  {
3761  ++aAnchorNdIdx;
3762  }
3763  }
3764  // apply found anchor text node as new anchor position
3765  newPos.nNode = aAnchorNdIdx;
3766  }
3767  else
3768  {
3769  tools::Long nOffset = newPos.nNode.GetIndex() - rRg.aStart.GetIndex();
3770  SwNodeIndex aIdx( rStartIdx, nOffset );
3771  newPos.nNode = aIdx;
3772  }
3773  // Set the character bound Flys back at the original character
3774  if ((RndStdIds::FLY_AT_CHAR == aAnchor.GetAnchorId()) &&
3775  newPos.nNode.GetNode().IsTextNode() )
3776  {
3777  // only if pCopiedPaM: care about partially selected start node
3778  sal_Int32 const nContent = pCopiedPaM && pCopiedPaM->Start()->nNode == aAnchor.GetContentAnchor()->nNode
3779  ? newPos.nContent.GetIndex() - pCopiedPaM->Start()->nContent.GetIndex()
3780  : newPos.nContent.GetIndex();
3781  newPos.nContent.Assign(newPos.nNode.GetNode().GetTextNode(), nContent);
3782  }
3783  else
3784  {
3785  newPos.nContent.Assign( nullptr, 0 );
3786  }
3787  aAnchor.SetAnchor( &newPos );
3788 
3789  // Check recursion: if copying content inside the same frame, then don't copy the format.
3790  if( &rDest == &m_rDoc )
3791  {
3792  const SwFormatContent& rContent = (*it).GetFormat()->GetContent();
3793  const SwStartNode* pSNd;
3794  if( rContent.GetContentIdx() &&
3795  nullptr != ( pSNd = rContent.GetContentIdx()->GetNode().GetStartNode() ) &&
3796  pSNd->GetIndex() < rStartIdx.GetIndex() &&
3797  rStartIdx.GetIndex() < pSNd->EndOfSectionIndex() )
3798  {
3799  it = aSet.erase(it);
3800  continue;
3801  }
3802  }
3803 
3804  // Ignore TextBoxes, they are already handled in
3805  // sw::DocumentLayoutManager::CopyLayoutFormat().
3806  if (SwTextBoxHelper::isTextBox(it->GetFormat(), RES_FLYFRMFMT))
3807  {
3808  it = aSet.erase(it);
3809  continue;
3810  }
3811 
3812  // Copy the format and set the new anchor
3813  aVecSwFrameFormat.push_back( rDest.getIDocumentLayoutAccess().CopyLayoutFormat( *(*it).GetFormat(),
3814  aAnchor, false, true ) );
3815  ++it;
3816  }
3817 
3818  // Rebuild as much as possible of all chains that are available in the original,
3819  OSL_ENSURE( aSet.size() == aVecSwFrameFormat.size(), "Missing new Flys" );
3820  if ( aSet.size() != aVecSwFrameFormat.size() )
3821  return;
3822 
3823  size_t n = 0;
3824  for (const auto& rFlyN : aSet)
3825  {
3826  const SwFrameFormat *pFormatN = rFlyN.GetFormat();
3827  const SwFormatChain &rChain = pFormatN->GetChain();
3828  int nCnt = int(nullptr != rChain.GetPrev());
3829  nCnt += rChain.GetNext() ? 1: 0;
3830  size_t k = 0;
3831  for (const auto& rFlyK : aSet)
3832  {
3833  const SwFrameFormat *pFormatK = rFlyK.GetFormat();
3834  if ( rChain.GetPrev() == pFormatK )
3835  {
3836  ::lcl_ChainFormats( static_cast< SwFlyFrameFormat* >(aVecSwFrameFormat[k]),
3837  static_cast< SwFlyFrameFormat* >(aVecSwFrameFormat[n]) );
3838  --nCnt;
3839  }
3840  else if ( rChain.GetNext() == pFormatK )
3841  {
3842  ::lcl_ChainFormats( static_cast< SwFlyFrameFormat* >(aVecSwFrameFormat[n]),
3843  static_cast< SwFlyFrameFormat* >(aVecSwFrameFormat[k]) );
3844  --nCnt;
3845  }
3846  ++k;
3847  }
3848  ++n;
3849  }
3850 
3851  // Re-create content property of draw formats, knowing how old shapes
3852  // were paired with old fly formats (aOldTextBoxes) and that aSet is
3853  // parallel with aVecSwFrameFormat.
3854  SwTextBoxHelper::restoreLinks(aSet, aVecSwFrameFormat, aOldTextBoxes);
3855 }
3856 
3857 /*
3858  * Reset the text's hard formatting
3859  */
3864 {
3865  ParaRstFormat* pPara = static_cast<ParaRstFormat*>(pArgs);
3866  if (pPara->pLayout && pPara->pLayout->IsHideRedlines()
3868  {
3869  return true; // skip hidden, since new items aren't applied
3870  }
3871  SwTextNode * pTextNode = rpNd->GetTextNode();
3872  if( pTextNode && pTextNode->GetpSwpHints() )
3873  {
3874  SwIndex aSt( pTextNode, 0 );
3875  sal_Int32 nEnd = pTextNode->Len();
3876 
3877  if( &pPara->pSttNd->nNode.GetNode() == pTextNode &&
3878  pPara->pSttNd->nContent.GetIndex() )
3879  aSt = pPara->pSttNd->nContent.GetIndex();
3880 
3881  if( &pPara->pEndNd->nNode.GetNode() == rpNd )
3882  nEnd = pPara->pEndNd->nContent.GetIndex();
3883 
3884  if( pPara->pHistory )
3885  {
3886  // Save all attributes for the Undo.
3887  SwRegHistory aRHst( *pTextNode, pPara->pHistory );
3888  pTextNode->GetpSwpHints()->Register( &aRHst );
3889  pTextNode->RstTextAttr( aSt, nEnd - aSt.GetIndex(), pPara->nWhich,
3890  pPara->pDelSet, pPara->bInclRefToxMark, pPara->bExactRange );
3891  if( pTextNode->GetpSwpHints() )
3892  pTextNode->GetpSwpHints()->DeRegister();
3893  }
3894  else
3895  pTextNode->RstTextAttr( aSt, nEnd - aSt.GetIndex(), pPara->nWhich,
3896  pPara->pDelSet, pPara->bInclRefToxMark, pPara->bExactRange );
3897  }
3898  return true;
3899 }
3900 
3902 {
3903 }
3904 //Private methods
3905 
3907 {
3909 
3911 
3912  if (*rPam.GetPoint() == *rPam.GetMark())
3913  {
3914  return false; // do not add empty redlines
3915  }
3916 
3917  std::vector<SwRangeRedline*> redlines;
3918  {
3919  auto pRedline(std::make_unique<SwRangeRedline>(RedlineType::Delete, rPam));
3920  if (pRedline->HasValidRange())
3921  {
3922  redlines.push_back(pRedline.release());
3923  }
3924  else // sigh ... why is such a selection even possible...
3925  { // split it up so we get one SwUndoRedlineDelete per inserted RL
3926  redlines = GetAllValidRanges(std::move(pRedline));
3927  }
3928  }
3929 
3930  if (redlines.empty())
3931  {
3932  return false;
3933  }
3934 
3935  // tdf#54819 current redlining needs also modification of paragraph style and
3936  // attributes added to the same grouped Undo
3939 
3940  auto & rDMA(*m_rDoc.getIDocumentMarkAccess());
3941  std::vector<std::unique_ptr<SwUndo>> MarkUndos;
3942  for (auto iter = rDMA.getAnnotationMarksBegin();
3943  iter != rDMA.getAnnotationMarksEnd(); )
3944  {
3945  // tdf#111524 remove annotation marks that have their field
3946  // characters deleted
3947  SwPosition const& rEndPos((**iter).GetMarkEnd());
3948  if (*rPam.Start() < rEndPos && rEndPos <= *rPam.End())
3949  {
3951  {
3952  MarkUndos.emplace_back(std::make_unique<SwUndoDeleteBookmark>(**iter));
3953  }
3954  // iter is into annotation mark vector so must be dereferenced!
3955  rDMA.deleteMark(&**iter);
3956  // this invalidates iter, have to start over...
3957  iter = rDMA.getAnnotationMarksBegin();
3958  }
3959  else
3960  { // marks are sorted by start
3961  if (*rPam.End() < (**iter).GetMarkStart())
3962  {
3963  break;
3964  }
3965  ++iter;
3966  }
3967  }
3968 
3969  // tdf#119019 accept tracked paragraph formatting to do not hide new deletions
3970  if (*rPam.GetPoint() != *rPam.GetMark())
3972 
3973  std::vector<std::unique_ptr<SwUndoRedlineDelete>> undos;
3975  {
3976  // this should no longer happen in calls from the UI but maybe via API
3977  // (randomTest and testTdf54819 triggers it)
3978  SAL_WARN_IF((eOld & RedlineFlags::ShowMask) != RedlineFlags::ShowMask,
3979  "sw.core", "redlines will be moved in DeleteAndJoin");
3982 
3984  bool bShowChangesInMargin = pSh && pSh->GetViewOptions()->IsShowChangesInMargin();
3986  for (SwRangeRedline * pRedline : redlines)
3987  {
3988  assert(pRedline->HasValidRange());
3989 
3990  // deletions shown in margin
3991  if (bShowChangesInMargin &&
3992  // within a paragraph TODO: fix also for paragraph join
3993  pRedline->GetPoint()->nNode == pRedline->GetMark()->nNode)
3994  {
3995  // show hidden previous deletion for joining
3997  const SwRangeRedline* pPrevRedline = rTable.FindAtPosition(
3998  *pRedline->End(), index, /*bNext=*/false, /*bGetVisible=*/false );
3999  if ( pPrevRedline && RedlineType::Delete == pPrevRedline->GetType() )
4000  {
4001  SwRangeRedline* pPrevRed = rTable[ index ];
4002  pPrevRed->Show(1, index, /*bForced=*/true);
4003  }
4004  pRedline->Show(0, rTable.GetPos(pRedline), /*bForced=*/false);
4005  pRedline->Show(1, rTable.GetPos(pRedline), /*bForced=*/false);
4006  }
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(static_cast<SwNumRuleItem*>(pItem->Clone()));
4602  }
4603 
4604  aListIdState = pAttrSet->GetItemState(RES_PARATR_LIST_ID, false, &pItem);
4605  if (SfxItemState::SET == aListIdState)
4606  {
4607  aListIdItem.reset(static_cast<SfxStringItem*>(pItem->Clone()));
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  rDoc.GetIDocumentUndoRedo().AppendUndo( std::unique_ptr<SwUndo>(pUndo) );
4742  pFlysAtInsPos = pUndo->GetFlysAnchoredAt();
4743  }
4744  else
4745  {
4746  pFlys = sw::GetFlysAnchoredAt(rDoc, rPos.nNode.GetIndex());
4747  pFlysAtInsPos = pFlys.get();
4748  }
4749 
4752 
4753  // Move the PaM one node back from the insert position, so that
4754  // the position doesn't get moved
4755  pCopyPam->SetMark();
4756  bool bCanMoveBack = pCopyPam->Move(fnMoveBackward, GoInContent);
4757  // If the position was shifted from more than one node, an end node has been skipped
4758  bool bAfterTable = false;
4759  if ((rPos.nNode.GetIndex() - pCopyPam->GetPoint()->nNode.GetIndex()) > 1)
4760  {
4761  // First go back to the original place
4762  pCopyPam->GetPoint()->nNode = rPos.nNode;
4763  pCopyPam->GetPoint()->nContent = rPos.nContent;
4764 
4765  bCanMoveBack = false;
4766  bAfterTable = true;
4767  }
4768  if( !bCanMoveBack )
4769  {
4770  pCopyPam->GetPoint()->nNode--;
4771  assert(pCopyPam->GetPoint()->nContent.GetIndex() == 0);
4772  }
4773 
4774  SwNodeRange aRg( pStt->nNode, pEnd->nNode );
4775  SwNodeIndex aInsPos( rPos.nNode );
4776  const bool bOneNode = pStt->nNode == pEnd->nNode;
4777  SwTextNode* pSttTextNd = pStt->nNode.GetNode().GetTextNode();
4778  SwTextNode* pEndTextNd = pEnd->nNode.GetNode().GetTextNode();
4779  SwTextNode* pDestTextNd = aInsPos.GetNode().GetTextNode();
4780  bool bCopyCollFormat = !rDoc.IsInsOnlyTextGlossary() &&
4781  ( (pDestTextNd && !pDestTextNd->GetText().getLength()) ||
4782  ( !bOneNode && !rPos.nContent.GetIndex() ) );
4783  bool bCopyBookmarks = true;
4784  bool bCopyPageSource = false;
4785  int nDeleteTextNodes = 0;
4786 
4787  // #i104585# copy outline num rule to clipboard (for ASCII filter)
4788  if (rDoc.IsClipBoard() && m_rDoc.GetOutlineNumRule())
4789  {
4791  }
4792 
4793  // #i86492#
4794  // Correct the search for a previous list:
4795  // First search for non-outline numbering list. Then search for non-outline
4796  // bullet list.
4797  // Keep also the <ListId> value for possible propagation.
4798  OUString aListIdToPropagate;
4799  const SwNumRule* pNumRuleToPropagate =
4800  rDoc.SearchNumRule( rPos, false, true, false, 0, aListIdToPropagate, nullptr, true );
4801  if ( !pNumRuleToPropagate )
4802  {
4803  pNumRuleToPropagate =
4804  rDoc.SearchNumRule( rPos, false, false, false, 0, aListIdToPropagate, nullptr, true );
4805  }
4806  // #i86492#
4807  // Do not propagate previous found list, if
4808  // - destination is an empty paragraph which is not in a list and
4809  // - source contains at least one paragraph which is not in a list
4810  if ( pNumRuleToPropagate &&
4811  pDestTextNd && !pDestTextNd->GetText().getLength() &&
4812  !pDestTextNd->IsInList() &&
4813  !lcl_ContainsOnlyParagraphsInList( rPam ) )
4814  {
4815  pNumRuleToPropagate = nullptr;
4816  }
4817 
4818  // This do/while block is only there so that we can break out of it!
4819  do {
4820  if( pSttTextNd )
4821  {
4822  ++nDeleteTextNodes; // must be joined in Undo
4823  // Don't copy the beginning completely?
4824  if( !bCopyCollFormat || bColumnSel || pStt->nContent.GetIndex() )
4825  {
4826  SwIndex aDestIdx( rPos.nContent );
4827  bool bCopyOk = false;
4828  if( !pDestTextNd )
4829  {
4830  if( pStt->nContent.GetIndex() || bOneNode )
4831  pDestTextNd = rDoc.GetNodes().MakeTextNode( aInsPos,
4833  else
4834  {
4835  pDestTextNd = pSttTextNd->MakeCopy(rDoc, aInsPos, true)->GetTextNode();
4836  bCopyOk = true;
4837  }
4838  aDestIdx.Assign( pDestTextNd, 0 );
4839  bCopyCollFormat = true;
4840  }
4841  else if( !bOneNode || bColumnSel )
4842  {
4843  const sal_Int32 nContentEnd = pEnd->nContent.GetIndex();
4844  {
4845  ::sw::UndoGuard const ug(rDoc.GetIDocumentUndoRedo());
4846  rDoc.getIDocumentContentOperations().SplitNode( rPos, false );
4847  }
4848 
4849  if (bCanMoveBack && rPos == *pCopyPam->GetPoint())
4850  {
4851  // after the SplitNode, span the CpyPam correctly again
4852  pCopyPam->Move( fnMoveBackward, GoInContent );
4853  pCopyPam->Move( fnMoveBackward, GoInContent );
4854  }
4855 
4856  pDestTextNd = rDoc.GetNodes()[ aInsPos.GetIndex()-1 ]->GetTextNode();
4857  aDestIdx.Assign(
4858  pDestTextNd, pDestTextNd->GetText().getLength());
4859 
4860  // Correct the area again
4861  if( bEndEqualIns )
4862  {
4863  bool bChg = pEnd != rPam.GetPoint();
4864  if( bChg )
4865  rPam.Exchange();
4866  rPam.Move( fnMoveBackward, GoInContent );
4867  if( bChg )
4868  rPam.Exchange();
4869  }
4870  else if( rPos == *pEnd )
4871  {
4872  // The end was also moved
4873  pEnd->nNode--;
4874  pEnd->nContent.Assign( pDestTextNd, nContentEnd );
4875  }
4876  // tdf#63022 always reset pEndTextNd after SplitNode
4877  aRg.aEnd = pEnd->nNode;
4878  pEndTextNd = pEnd->nNode.GetNode().GetTextNode();
4879  }
4880 
4882  if( bCopyCollFormat && bOneNode )
4883  {
4885  }
4886 
4887  if( !bCopyOk )
4888  {
4889  const sal_Int32 nCpyLen = ( bOneNode
4890  ? pEnd->nContent.GetIndex()
4891  : pSttTextNd->GetText().getLength())
4892  - pStt->nContent.GetIndex();
4893  pSttTextNd->CopyText( pDestTextNd, aDestIdx,
4894  pStt->nContent, nCpyLen );
4895  if( bEndEqualIns )
4896  pEnd->nContent -= nCpyLen;
4897  }
4898 
4899  aRg.aStart++;
4900 
4901  if( bOneNode )
4902  {
4903  if (bCopyCollFormat)
4904  {
4905  pSttTextNd->CopyCollFormat( *pDestTextNd );
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  pEndTextNd->CopyCollFormat( *pDestTextNd );
5005  if ( bOneNode )
5006  {
5008  }
5009  }
5010  }
5011 
5012  SfxItemSet aBrkSet( rDoc.GetAttrPool(), aBreakSetRange );
5013  if ((flags & SwCopyFlags::CopyAll) || aRg.aStart != aRg.aEnd)
5014  {
5015  if (pSttTextNd && bCopyCollFormat && pDestTextNd->HasSwAttrSet())
5016  {
5017  aBrkSet.Put( *pDestTextNd->GetpSwAttrSet() );
5018  if( SfxItemState::SET == aBrkSet.GetItemState( RES_BREAK, false ) )
5019  pDestTextNd->ResetAttr( RES_BREAK );
5020  if( SfxItemState::SET == aBrkSet.GetItemState( RES_PAGEDESC, false ) )
5021  pDestTextNd->ResetAttr( RES_PAGEDESC );
5022  }
5023  }
5024 
5025  {
5026  SwPosition startPos(SwNodeIndex(pCopyPam->GetPoint()->nNode, +1),
5027  SwIndex(SwNodeIndex(pCopyPam->GetPoint()->nNode, +1).GetNode().GetContentNode()));
5028  if (bCanMoveBack)
5029  { // pCopyPam is actually 1 before the copy range so move it fwd
5030  SwPaM temp(*pCopyPam->GetPoint());
5032  startPos = *temp.GetPoint();
5033  }
5034  assert(startPos.nNode.GetNode().IsContentNode());
5035  std::pair<SwPaM const&, SwPosition const&> tmp(rPam, startPos);
5036  if( aInsPos == pEnd->nNode )
5037  {
5038  SwNodeIndex aSaveIdx( aInsPos, -1 );
5039  assert(pStt->nNode != pEnd->nNode);
5040  pEnd->nContent = 0; // TODO why this?
5041  CopyWithFlyInFly(aRg, aInsPos, &tmp, /*bMakeNewFrames*/true, false, /*bCopyFlyAtFly=*/false, flags);
5042  ++aSaveIdx;
5043  pEnd->nNode = aSaveIdx;
5044  pEnd->nContent.Assign( aSaveIdx.GetNode().GetTextNode(), 0 );
5045  }
5046  else
5047  CopyWithFlyInFly(aRg, aInsPos, &tmp, /*bMakeNewFrames*/true, false, /*bCopyFlyAtFly=*/false, flags);
5048 
5049  bCopyBookmarks = false;
5050  }
5051 
5052  // at-char anchors post SplitNode are on index 0 of 2nd node and will
5053  // remain there - move them back to the start (end would also work?)
5054  // ... also for at-para anchors; here start is preferable because
5055  // it's consistent with SplitNode from SwUndoInserts::RedoImpl()
5056  if (pFlysAtInsPos)
5057  {
5058  // init *again* - because CopyWithFlyInFly moved startPos
5059  SwPosition startPos(SwNodeIndex(pCopyPam->GetPoint()->nNode, +1),
5060  SwIndex(SwNodeIndex(pCopyPam->GetPoint()->nNode, +1).GetNode().GetContentNode()));
5061  if (bCanMoveBack)
5062  { // pCopyPam is actually 1 before the copy range so move it fwd
5063  SwPaM temp(*pCopyPam->GetPoint());
5065  startPos = *temp.GetPoint();
5066  }
5067  assert(startPos.nNode.GetNode().IsContentNode());
5068  SwPosition startPosAtPara(startPos);
5069  startPosAtPara.nContent.Assign(nullptr, 0);
5070 
5071  for (SwFrameFormat * pFly : *pFlysAtInsPos)
5072  {
5073  SwFormatAnchor const*const pAnchor = &pFly->GetAnchor();
5074  if (pAnchor->GetAnchorId() == RndStdIds::FLY_AT_CHAR)
5075  {
5076  SwFormatAnchor anchor(*pAnchor);
5077  anchor.SetAnchor( &startPos );
5078  pFly->SetFormatAttr(anchor);
5079  }
5080  else if (pAnchor->GetAnchorId() == RndStdIds::FLY_AT_PARA)
5081  {
5082  SwFormatAnchor anchor(*pAnchor);
5083  anchor.SetAnchor( &startPosAtPara );
5084  pFly->SetFormatAttr(anchor);
5085  }
5086  }
5087  }
5088 
5089  if ((flags & SwCopyFlags::CopyAll) || aRg.aStart != aRg.aEnd)
5090  {
5091  // Put the breaks back into the first node
5092  if( aBrkSet.Count() && nullptr != ( pDestTextNd = rDoc.GetNodes()[
5093  pCopyPam->GetPoint()->nNode.GetIndex()+1 ]->GetTextNode()))
5094  {
5095  pDestTextNd->SetAttr( aBrkSet );
5096  bCopyPageSource = true;
5097  }
5098  }
5099  } while( false );
5100 
5101 
5102  // it is not possible to make this test when copy from the clipBoard to document
5103  // in this case the PageNum not exist anymore
5104  // tdf#39400 and tdf#97526
5105  // when copy from document to ClipBoard, and it is from the first page
5106  // and not the source has the page break
5107  if (rDoc.IsClipBoard() && (rPam.GetPageNum(pStt == rPam.GetPoint()) == 1) && !bCopyPageSource)
5108  {
5109  pDestTextNd->ResetAttr(RES_BREAK); // remove the page-break
5110  pDestTextNd->ResetAttr(RES_PAGEDESC);
5111  }
5112 
5113 
5114  // Adjust position (in case it was moved / in another node)
5115  rPos.nContent.Assign( rPos.nNode.GetNode().GetContentNode(),
5116  rPos.nContent.GetIndex() );
5117 
5118  if( rPos.nNode != aInsPos )
5119  {
5120  pCopyPam->GetMark()->nNode = aInsPos;
5121  if (aInsPos < rPos.nNode)
5122  { // tdf#134250 decremented in (pEndTextNd && !pDestTextNd) above
5123  pCopyPam->GetContentNode(false)->MakeEndIndex(&pCopyPam->GetMark()->nContent);
5124  }
5125  else // incremented in (!pSttTextNd && pDestTextNd) above
5126  {
5127  pCopyPam->GetMark()->nContent.Assign(pCopyPam->GetContentNode(false), 0);
5128  }
5129  rPos = *pCopyPam->GetMark();
5130  }
5131  else
5132  *pCopyPam->GetMark() = rPos;
5133 
5134  if ( !bAfterTable )
5135  pCopyPam->Move(