update credits
[LibreOffice.git] / sw / source / core / undo / unins.cxx
blobcbacd830e7aa6966623ca5aa5c6e5c578b8fb151
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <UndoInsert.hxx>
23 #include <hintids.hxx>
24 #include <unotools/charclass.hxx>
25 #include <sot/storage.hxx>
26 #include <editeng/keepitem.hxx>
27 #include <svx/svdobj.hxx>
28 #include <xmloff/odffields.hxx>
30 #include <docsh.hxx>
31 #include <fmtcntnt.hxx>
32 #include <fmtanchr.hxx>
33 #include <frmfmt.hxx>
34 #include <doc.hxx>
35 #include <IDocumentUndoRedo.hxx>
36 #include <swundo.hxx>
37 #include <pam.hxx>
38 #include <ndtxt.hxx>
39 #include <UndoCore.hxx>
40 #include <UndoDelete.hxx>
41 #include <UndoAttribute.hxx>
42 #include <rolbck.hxx>
43 #include <ndgrf.hxx>
44 #include <ndole.hxx>
45 #include <grfatr.hxx>
46 #include <cntfrm.hxx>
47 #include <flyfrm.hxx>
48 #include <fesh.hxx>
49 #include <swtable.hxx>
50 #include <redline.hxx>
51 #include <docary.hxx>
52 #include <acorrect.hxx>
53 #include <dcontact.hxx>
55 #include <comcore.hrc> // #111827#
56 #include <undo.hrc>
58 using namespace ::com::sun::star;
61 //------------------------------------------------------------
63 // INSERT
65 String * SwUndoInsert::GetTxtFromDoc() const
67 String * pResult = NULL;
69 SwNodeIndex aNd( pDoc->GetNodes(), nNode);
70 SwCntntNode* pCNd = aNd.GetNode().GetCntntNode();
71 SwPaM aPaM( *pCNd, nCntnt );
73 aPaM.SetMark();
75 if( pCNd->IsTxtNode() )
77 OUString sTxt = static_cast<SwTxtNode*>(pCNd)->GetTxt();
79 sal_Int32 nStart = nCntnt-nLen;
80 sal_Int32 nLength = nLen;
82 if (nStart < 0)
84 nLength += nStart;
85 nStart = 0;
88 pResult = new String(sTxt.copy(nStart, nLength));
91 return pResult;
94 void SwUndoInsert::Init(const SwNodeIndex & rNd)
96 // consider Redline
97 pDoc = rNd.GetNode().GetDoc();
98 if( pDoc->IsRedlineOn() )
100 pRedlData = new SwRedlineData( nsRedlineType_t::REDLINE_INSERT,
101 pDoc->GetRedlineAuthor() );
102 SetRedlineMode( pDoc->GetRedlineMode() );
105 pUndoTxt = GetTxtFromDoc();
107 bCacheComment = false;
110 // #111827#
111 SwUndoInsert::SwUndoInsert( const SwNodeIndex& rNd, xub_StrLen nCnt,
112 xub_StrLen nL,
113 const IDocumentContentOperations::InsertFlags nInsertFlags,
114 sal_Bool bWDelim )
115 : SwUndo(UNDO_TYPING), pTxt( 0 ), pRedlData( 0 ),
116 nNode( rNd.GetIndex() ), nCntnt(nCnt), nLen(nL),
117 bIsWordDelim( bWDelim ), bIsAppend( sal_False )
118 , m_bWithRsid(false)
119 , m_nInsertFlags(nInsertFlags)
121 Init(rNd);
124 // #111827#
125 SwUndoInsert::SwUndoInsert( const SwNodeIndex& rNd )
126 : SwUndo(UNDO_SPLITNODE), pTxt( 0 ),
127 pRedlData( 0 ), nNode( rNd.GetIndex() ), nCntnt(0), nLen(1),
128 bIsWordDelim( sal_False ), bIsAppend( sal_True )
129 , m_bWithRsid(false)
130 , m_nInsertFlags(IDocumentContentOperations::INS_EMPTYEXPAND)
132 Init(rNd);
135 // Check if the next Insert can be combined with the current one. If so
136 // change the length and InsPos. As a result, SwDoc::Inser will not add a
137 // new object into the Undo list.
139 sal_Bool SwUndoInsert::CanGrouping( sal_Unicode cIns )
141 if( !bIsAppend && bIsWordDelim ==
142 !GetAppCharClass().isLetterNumeric( OUString( cIns )) )
144 nLen++;
145 nCntnt++;
147 if (pUndoTxt)
148 pUndoTxt->Insert(cIns);
150 return sal_True;
152 return sal_False;
155 sal_Bool SwUndoInsert::CanGrouping( const SwPosition& rPos )
157 sal_Bool bRet = sal_False;
158 if( nNode == rPos.nNode.GetIndex() &&
159 nCntnt == rPos.nContent.GetIndex() )
161 // consider Redline
162 SwDoc& rDoc = *rPos.nNode.GetNode().GetDoc();
163 if( ( ~nsRedlineMode_t::REDLINE_SHOW_MASK & rDoc.GetRedlineMode() ) ==
164 ( ~nsRedlineMode_t::REDLINE_SHOW_MASK & GetRedlineMode() ) )
166 bRet = sal_True;
168 // than there is or was still an active Redline:
169 // Check if there is another Redline at the InsPosition. If the
170 // same exists only once, it can be combined.
171 const SwRedlineTbl& rTbl = rDoc.GetRedlineTbl();
172 if( !rTbl.empty() )
174 SwRedlineData aRData( nsRedlineType_t::REDLINE_INSERT, rDoc.GetRedlineAuthor() );
175 const SwIndexReg* pIReg = rPos.nContent.GetIdxReg();
176 SwIndex* pIdx;
177 for( sal_uInt16 i = 0; i < rTbl.size(); ++i )
179 SwRedline* pRedl = rTbl[ i ];
180 if( pIReg == (pIdx = &pRedl->End()->nContent)->GetIdxReg() &&
181 nCntnt == pIdx->GetIndex() )
183 if( !pRedl->HasMark() || !pRedlData ||
184 *pRedl != *pRedlData || *pRedl != aRData )
186 bRet = sal_False;
187 break;
194 return bRet;
197 SwUndoInsert::~SwUndoInsert()
199 if (m_pUndoNodeIndex) // delete the section from UndoNodes array
201 // Insert saves the content in IconSection
202 SwNodes& rUNds = m_pUndoNodeIndex->GetNodes();
203 rUNds.Delete(*m_pUndoNodeIndex,
204 rUNds.GetEndOfExtras().GetIndex() - m_pUndoNodeIndex->GetIndex());
205 m_pUndoNodeIndex.reset();
207 else if( pTxt ) // the inserted text
208 delete pTxt;
209 delete pRedlData;
210 delete pUndoTxt;
213 void SwUndoInsert::UndoImpl(::sw::UndoRedoContext & rContext)
215 SwDoc *const pTmpDoc = & rContext.GetDoc();
216 SwPaM *const pPam(& rContext.GetCursorSupplier().CreateNewShellCursor());
218 if( bIsAppend )
220 pPam->GetPoint()->nNode = nNode;
222 if( IDocumentRedlineAccess::IsRedlineOn( GetRedlineMode() ))
224 pPam->GetPoint()->nContent.Assign( pPam->GetCntntNode(), 0 );
225 pPam->SetMark();
226 pPam->Move( fnMoveBackward );
227 pPam->Exchange();
228 pTmpDoc->DeleteRedline( *pPam, true, USHRT_MAX );
230 pPam->DeleteMark();
231 pTmpDoc->DelFullPara( *pPam );
232 pPam->GetPoint()->nContent.Assign( pPam->GetCntntNode(), 0 );
234 else
236 sal_uLong nNd = nNode;
237 xub_StrLen nCnt = nCntnt;
238 if( nLen )
240 SwNodeIndex aNd( pTmpDoc->GetNodes(), nNode);
241 SwCntntNode* pCNd = aNd.GetNode().GetCntntNode();
242 SwPaM aPaM( *pCNd, nCntnt );
244 aPaM.SetMark();
246 SwTxtNode * const pTxtNode( pCNd->GetTxtNode() );
247 if ( pTxtNode )
249 aPaM.GetPoint()->nContent -= nLen;
250 if( IDocumentRedlineAccess::IsRedlineOn( GetRedlineMode() ))
251 pTmpDoc->DeleteRedline( aPaM, true, USHRT_MAX );
252 if (m_bWithRsid)
254 // RSID was added: remove any CHARFMT/AUTOFMT that may be
255 // set on the deleted text; EraseText will leave empty
256 // ones behind otherwise
257 pTxtNode->DeleteAttributes(RES_TXTATR_AUTOFMT,
258 aPaM.GetPoint()->nContent.GetIndex(),
259 aPaM.GetMark()->nContent.GetIndex());
260 pTxtNode->DeleteAttributes(RES_TXTATR_CHARFMT,
261 aPaM.GetPoint()->nContent.GetIndex(),
262 aPaM.GetMark()->nContent.GetIndex());
264 RemoveIdxFromRange( aPaM, sal_False );
265 pTxt = new String( pTxtNode->GetTxt().copy(nCntnt-nLen, nLen) );
266 pTxtNode->EraseText( aPaM.GetPoint()->nContent, nLen );
268 // Undo deletes fieldmarks in two step: first the end then the start position.
269 // Once the start position is deleted, make sure the fieldmark itself is deleted as well.
270 if (nLen == 1)
272 IDocumentMarkAccess* const pMarkAccess = pTmpDoc->getIDocumentMarkAccess();
273 for ( IDocumentMarkAccess::const_iterator_t i = pMarkAccess->getMarksBegin(); i != pMarkAccess->getMarksEnd(); ++i)
275 sw::mark::IMark* pMark = i->get();
276 if (pMark->GetMarkStart() == *aPaM.GetPoint() && pMark->GetMarkStart().nContent == aPaM.GetPoint()->nContent)
278 sw::mark::IFieldmark* pFieldmark = dynamic_cast<sw::mark::IFieldmark*>(pMark);
279 if (pFieldmark && pFieldmark->GetFieldname() == ODF_COMMENTRANGE)
281 pTmpDoc->getIDocumentMarkAccess()->deleteMark(pMark);
282 break;
288 else // otherwise Graphics/OLE/Text/...
290 aPaM.Move(fnMoveBackward);
291 if( IDocumentRedlineAccess::IsRedlineOn( GetRedlineMode() ))
292 pTmpDoc->DeleteRedline( aPaM, true, USHRT_MAX );
293 RemoveIdxFromRange( aPaM, sal_False );
296 nNd = aPaM.GetPoint()->nNode.GetIndex();
297 nCnt = aPaM.GetPoint()->nContent.GetIndex();
299 if( !pTxt )
301 m_pUndoNodeIndex.reset(
302 new SwNodeIndex(pDoc->GetNodes().GetEndOfContent()));
303 MoveToUndoNds(aPaM, m_pUndoNodeIndex.get());
305 nNode = aPaM.GetPoint()->nNode.GetIndex();
306 nCntnt = aPaM.GetPoint()->nContent.GetIndex();
309 // set cursor to Undo range
310 pPam->DeleteMark();
312 pPam->GetPoint()->nNode = nNd;
313 pPam->GetPoint()->nContent.Assign(
314 pPam->GetPoint()->nNode.GetNode().GetCntntNode(), nCnt );
317 DELETEZ(pUndoTxt);
321 void SwUndoInsert::RedoImpl(::sw::UndoRedoContext & rContext)
323 SwDoc *const pTmpDoc = & rContext.GetDoc();
324 SwPaM *const pPam(& rContext.GetCursorSupplier().CreateNewShellCursor());
325 pPam->DeleteMark();
327 if( bIsAppend )
329 pPam->GetPoint()->nNode = nNode - 1;
330 pTmpDoc->AppendTxtNode( *pPam->GetPoint() );
332 pPam->SetMark();
333 pPam->Move( fnMoveBackward );
334 pPam->Exchange();
336 if( pRedlData && IDocumentRedlineAccess::IsRedlineOn( GetRedlineMode() ))
338 RedlineMode_t eOld = pTmpDoc->GetRedlineMode();
339 pTmpDoc->SetRedlineMode_intern((RedlineMode_t)(eOld & ~nsRedlineMode_t::REDLINE_IGNORE));
340 pTmpDoc->AppendRedline( new SwRedline( *pRedlData, *pPam ), true);
341 pTmpDoc->SetRedlineMode_intern( eOld );
343 else if( !( nsRedlineMode_t::REDLINE_IGNORE & GetRedlineMode() ) &&
344 !pTmpDoc->GetRedlineTbl().empty() )
345 pTmpDoc->SplitRedline( *pPam );
347 pPam->DeleteMark();
349 else
351 pPam->GetPoint()->nNode = nNode;
352 SwCntntNode *const pCNd =
353 pPam->GetPoint()->nNode.GetNode().GetCntntNode();
354 pPam->GetPoint()->nContent.Assign( pCNd, nCntnt );
356 if( nLen )
358 sal_Bool bMvBkwrd = MovePtBackward( *pPam );
360 if( pTxt )
362 SwTxtNode *const pTxtNode = pCNd->GetTxtNode();
363 OSL_ENSURE( pTxtNode, "where is my textnode ?" );
364 OUString const ins(
365 pTxtNode->InsertText( *pTxt, pPam->GetMark()->nContent,
366 m_nInsertFlags) );
367 assert(ins.getLength() == pTxt->Len()); // must succeed
368 DELETEZ( pTxt );
369 if (m_bWithRsid) // re-insert RSID
371 SwPaM pam(*pPam->GetMark(), 0); // mark -> point
372 pTmpDoc->UpdateRsid(pam, ins.getLength());
375 else
377 // re-insert content again (first detach m_pUndoNodeIndex!)
378 sal_uLong const nMvNd = m_pUndoNodeIndex->GetIndex();
379 m_pUndoNodeIndex.reset();
380 MoveFromUndoNds(*pTmpDoc, nMvNd, *pPam->GetMark());
382 nNode = pPam->GetMark()->nNode.GetIndex();
383 nCntnt = pPam->GetMark()->nContent.GetIndex();
385 MovePtForward( *pPam, bMvBkwrd );
386 pPam->Exchange();
387 if( pRedlData && IDocumentRedlineAccess::IsRedlineOn( GetRedlineMode() ))
389 RedlineMode_t eOld = pTmpDoc->GetRedlineMode();
390 pTmpDoc->SetRedlineMode_intern((RedlineMode_t)(eOld & ~nsRedlineMode_t::REDLINE_IGNORE));
391 pTmpDoc->AppendRedline( new SwRedline( *pRedlData,
392 *pPam ), true);
393 pTmpDoc->SetRedlineMode_intern( eOld );
395 else if( !( nsRedlineMode_t::REDLINE_IGNORE & GetRedlineMode() ) &&
396 !pTmpDoc->GetRedlineTbl().empty() )
397 pTmpDoc->SplitRedline(*pPam);
401 pUndoTxt = GetTxtFromDoc();
404 void SwUndoInsert::RepeatImpl(::sw::RepeatContext & rContext)
406 if( !nLen )
407 return;
409 SwDoc & rDoc = rContext.GetDoc();
410 SwNodeIndex aNd( rDoc.GetNodes(), nNode );
411 SwCntntNode* pCNd = aNd.GetNode().GetCntntNode();
413 if( !bIsAppend && 1 == nLen ) // >1 than always Text, otherwise Graphics/OLE/Text/...
415 SwPaM aPaM( *pCNd, nCntnt );
416 aPaM.SetMark();
417 aPaM.Move(fnMoveBackward);
418 pCNd = aPaM.GetCntntNode();
421 // What happens with the possible selected range ???
423 switch( pCNd->GetNodeType() )
425 case ND_TEXTNODE:
426 if( bIsAppend )
428 rDoc.AppendTxtNode( *rContext.GetRepeatPaM().GetPoint() );
430 else
432 OUString const aTxt( static_cast<SwTxtNode*>(pCNd)->GetTxt() );
433 ::sw::GroupUndoGuard const undoGuard(rDoc.GetIDocumentUndoRedo());
434 rDoc.InsertString( rContext.GetRepeatPaM(),
435 aTxt.copy(nCntnt - nLen, nLen) );
437 break;
438 case ND_GRFNODE:
440 SwGrfNode* pGrfNd = (SwGrfNode*)pCNd;
441 String sFile, sFilter;
442 if( pGrfNd->IsGrfLink() )
443 pGrfNd->GetFileFilterNms( &sFile, &sFilter );
445 rDoc.Insert( rContext.GetRepeatPaM(), sFile, sFilter,
446 &pGrfNd->GetGrf(),
447 0/* Graphics collection*/, NULL, NULL );
449 break;
451 case ND_OLENODE:
453 // StarView does not yet provide an option to copy a StarOBJ
454 SvStorageRef aRef = new SvStorage( aEmptyStr );
455 SwOLEObj& rSwOLE = (SwOLEObj&)((SwOLENode*)pCNd)->GetOLEObj();
457 // temporary storage until object is inserted
458 // TODO/MBA: seems that here a physical copy is done - not as in drawing layer! Testing!
459 // TODO/LATER: Copying through the container would copy the replacement image as well
460 comphelper::EmbeddedObjectContainer aCnt;
461 OUString aName = aCnt.CreateUniqueObjectName();
462 if ( aCnt.StoreEmbeddedObject( rSwOLE.GetOleRef(), aName, sal_True ) )
464 uno::Reference < embed::XEmbeddedObject > aNew = aCnt.GetEmbeddedObject( aName );
465 rDoc.Insert( rContext.GetRepeatPaM(),
466 svt::EmbeddedObjectRef( aNew,
467 static_cast<SwOLENode*>(pCNd)->GetAspect() ),
468 NULL, NULL, NULL );
471 break;
476 // #111827#
477 SwRewriter SwUndoInsert::GetRewriter() const
479 SwRewriter aResult;
480 String * pStr = NULL;
481 bool bDone = false;
483 if (pTxt)
484 pStr = pTxt;
485 else if (pUndoTxt)
486 pStr = pUndoTxt;
488 if (pStr)
490 String aString = ShortenString(DenoteSpecialCharacters(*pStr),
491 nUndoStringLength,
492 String(SW_RES(STR_LDOTS)));
494 aResult.AddRule(UndoArg1, aString);
496 bDone = true;
499 if ( ! bDone )
501 aResult.AddRule(UndoArg1, String("??", RTL_TEXTENCODING_ASCII_US));
504 return aResult;
508 // SwUndoReplace /////////////////////////////////////////////////////////
511 class SwUndoReplace::Impl
512 : private SwUndoSaveCntnt
514 OUString m_sOld;
515 OUString m_sIns;
516 sal_uLong m_nSttNd, m_nEndNd, m_nOffset;
517 xub_StrLen m_nSttCnt, m_nEndCnt, m_nSetPos, m_nSelEnd;
518 bool m_bSplitNext : 1;
519 bool m_bRegExp : 1;
520 // metadata references for paragraph and following para (if m_bSplitNext)
521 ::boost::shared_ptr< ::sfx2::MetadatableUndo > m_pMetadataUndoStart;
522 ::boost::shared_ptr< ::sfx2::MetadatableUndo > m_pMetadataUndoEnd;
524 public:
525 Impl(SwPaM const& rPam, OUString const& rIns, bool const bRegExp);
526 virtual ~Impl()
530 virtual void UndoImpl( ::sw::UndoRedoContext & );
531 virtual void RedoImpl( ::sw::UndoRedoContext & );
533 void SetEnd(SwPaM const& rPam);
535 OUString const& GetOld() const { return m_sOld; }
536 OUString const& GetIns() const { return m_sIns; }
540 SwUndoReplace::SwUndoReplace(SwPaM const& rPam,
541 OUString const& rIns, bool const bRegExp)
542 : SwUndo( UNDO_REPLACE )
543 , m_pImpl(new Impl(rPam, rIns, bRegExp))
547 SwUndoReplace::~SwUndoReplace()
551 void SwUndoReplace::UndoImpl(::sw::UndoRedoContext & rContext)
553 m_pImpl->UndoImpl(rContext);
556 void SwUndoReplace::RedoImpl(::sw::UndoRedoContext & rContext)
558 m_pImpl->RedoImpl(rContext);
561 SwRewriter
562 MakeUndoReplaceRewriter(sal_uLong const occurrences,
563 OUString const& sOld, OUString const& sNew)
565 SwRewriter aResult;
567 if (1 < occurrences)
569 aResult.AddRule(UndoArg1, OUString::number(occurrences));
570 aResult.AddRule(UndoArg2, String(SW_RES(STR_OCCURRENCES_OF)));
572 String aTmpStr;
573 aTmpStr += String(SW_RES(STR_START_QUOTE));
574 aTmpStr += ShortenString(sOld, nUndoStringLength,
575 SW_RES(STR_LDOTS));
576 aTmpStr += String(SW_RES(STR_END_QUOTE));
577 aResult.AddRule(UndoArg3, aTmpStr);
579 else if (1 == occurrences)
582 String aTmpStr;
584 aTmpStr += String(SW_RES(STR_START_QUOTE));
585 // #i33488 #
586 aTmpStr += ShortenString(sOld, nUndoStringLength,
587 SW_RES(STR_LDOTS));
588 aTmpStr += String(SW_RES(STR_END_QUOTE));
589 aResult.AddRule(UndoArg1, aTmpStr);
592 aResult.AddRule(UndoArg2, String(SW_RES(STR_YIELDS)));
595 String aTmpStr;
597 aTmpStr += String(SW_RES(STR_START_QUOTE));
598 // #i33488 #
599 aTmpStr += ShortenString(sNew, nUndoStringLength,
600 SW_RES(STR_LDOTS));
601 aTmpStr += String(SW_RES(STR_END_QUOTE));
602 aResult.AddRule(UndoArg3, aTmpStr);
606 return aResult;
609 // #111827#
610 SwRewriter SwUndoReplace::GetRewriter() const
612 return MakeUndoReplaceRewriter(1, m_pImpl->GetOld(), m_pImpl->GetIns());
615 void SwUndoReplace::SetEnd(SwPaM const& rPam)
617 m_pImpl->SetEnd(rPam);
620 SwUndoReplace::Impl::Impl(
621 SwPaM const& rPam, OUString const& rIns, bool const bRegExp)
622 : m_sIns( rIns )
623 , m_nOffset( 0 )
624 , m_bRegExp(bRegExp)
627 const SwPosition * pStt( rPam.Start() );
628 const SwPosition * pEnd( rPam.End() );
630 m_nSttNd = m_nEndNd = pStt->nNode.GetIndex();
631 m_nSttCnt = pStt->nContent.GetIndex();
632 m_nSelEnd = m_nEndCnt = pEnd->nContent.GetIndex();
634 m_bSplitNext = m_nSttNd != pEnd->nNode.GetIndex();
636 SwTxtNode* pNd = pStt->nNode.GetNode().GetTxtNode();
637 OSL_ENSURE( pNd, "Dude, where's my TextNode?" );
639 pHistory = new SwHistory;
640 DelCntntIndex( *rPam.GetMark(), *rPam.GetPoint() );
642 m_nSetPos = pHistory->Count();
644 sal_uLong nNewPos = pStt->nNode.GetIndex();
645 m_nOffset = m_nSttNd - nNewPos;
647 if ( pNd->GetpSwpHints() )
649 pHistory->CopyAttr( pNd->GetpSwpHints(), nNewPos, 0,
650 pNd->GetTxt().getLength(), true );
653 if ( m_bSplitNext )
655 if( pNd->HasSwAttrSet() )
656 pHistory->CopyFmtAttr( *pNd->GetpSwAttrSet(), nNewPos );
657 pHistory->Add( pNd->GetTxtColl(), nNewPos, ND_TEXTNODE );
659 SwTxtNode* pNext = pEnd->nNode.GetNode().GetTxtNode();
660 sal_uLong nTmp = pNext->GetIndex();
661 pHistory->CopyAttr( pNext->GetpSwpHints(), nTmp, 0,
662 pNext->GetTxt().getLength(), true );
663 if( pNext->HasSwAttrSet() )
664 pHistory->CopyFmtAttr( *pNext->GetpSwAttrSet(), nTmp );
665 pHistory->Add( pNext->GetTxtColl(),nTmp, ND_TEXTNODE );
666 // METADATA: store
667 m_pMetadataUndoStart = pNd ->CreateUndo();
668 m_pMetadataUndoEnd = pNext->CreateUndo();
671 if( !pHistory->Count() )
672 delete pHistory, pHistory = 0;
674 xub_StrLen nECnt = m_bSplitNext ? pNd->GetTxt().getLength()
675 : pEnd->nContent.GetIndex();
676 m_sOld = pNd->GetTxt().copy( m_nSttCnt, nECnt - m_nSttCnt );
679 void SwUndoReplace::Impl::UndoImpl(::sw::UndoRedoContext & rContext)
681 SwDoc *const pDoc = & rContext.GetDoc();
682 SwPaM & rPam(rContext.GetCursorSupplier().CreateNewShellCursor());
683 rPam.DeleteMark();
685 SwTxtNode* pNd = pDoc->GetNodes()[ m_nSttNd - m_nOffset ]->GetTxtNode();
686 OSL_ENSURE( pNd, "Dude, where's my TextNode?" );
688 SwAutoCorrExceptWord* pACEWord = pDoc->GetAutoCorrExceptWord();
689 if( pACEWord )
691 if ((1 == m_sIns.getLength()) && (1 == m_sOld.getLength()))
693 SwPosition aPos( *pNd ); aPos.nContent.Assign( pNd, m_nSttCnt );
694 pACEWord->CheckChar( aPos, m_sOld[ 0 ] );
696 pDoc->SetAutoCorrExceptWord( 0 );
699 SwIndex aIdx( pNd, m_nSttCnt );
700 // don't look at m_sIns for deletion, maybe it was not completely inserted
702 rPam.GetPoint()->nNode = *pNd;
703 rPam.GetPoint()->nContent.Assign( pNd, m_nSttCnt );
704 rPam.SetMark();
705 rPam.GetPoint()->nNode = m_nEndNd - m_nOffset;
706 rPam.GetPoint()->nContent.Assign( rPam.GetCntntNode(), m_nEndCnt );
707 // move it out of the way so it is not registered at deleted node
708 aIdx.Assign(0, 0);
710 pDoc->DeleteAndJoin( rPam );
711 rPam.DeleteMark();
712 pNd = rPam.GetNode()->GetTxtNode();
713 OSL_ENSURE( pNd, "Dude, where's my TextNode?" );
714 aIdx.Assign( pNd, m_nSttCnt );
717 if( m_bSplitNext )
719 SwPosition aPos( *pNd, aIdx );
720 pDoc->SplitNode( aPos, false );
721 pNd->RestoreMetadata(m_pMetadataUndoEnd);
722 pNd = pDoc->GetNodes()[ m_nSttNd - m_nOffset ]->GetTxtNode();
723 aIdx.Assign( pNd, m_nSttCnt );
724 // METADATA: restore
725 pNd->RestoreMetadata(m_pMetadataUndoStart);
728 if (!m_sOld.isEmpty())
730 OUString const ins( pNd->InsertText( m_sOld, aIdx ) );
731 assert(ins.getLength() == m_sOld.getLength()); // must succeed
734 if( pHistory )
736 if( pNd->GetpSwpHints() )
737 pNd->ClearSwpHintsArr( true );
739 pHistory->TmpRollback( pDoc, m_nSetPos, false );
740 if ( m_nSetPos ) // there were footnotes/FlyFrames
742 // are there others than these?
743 if( m_nSetPos < pHistory->Count() )
745 // than save those attributes as well
746 SwHistory aHstr;
747 aHstr.Move( 0, pHistory, m_nSetPos );
748 pHistory->Rollback( pDoc );
749 pHistory->Move( 0, &aHstr );
751 else
753 pHistory->Rollback( pDoc );
754 DELETEZ( pHistory );
759 rPam.GetPoint()->nNode = m_nSttNd;
760 rPam.GetPoint()->nContent = aIdx;
763 void SwUndoReplace::Impl::RedoImpl(::sw::UndoRedoContext & rContext)
765 SwDoc & rDoc = rContext.GetDoc();
766 SwPaM & rPam(rContext.GetCursorSupplier().CreateNewShellCursor());
767 rPam.DeleteMark();
768 rPam.GetPoint()->nNode = m_nSttNd;
770 SwTxtNode* pNd = rPam.GetPoint()->nNode.GetNode().GetTxtNode();
771 OSL_ENSURE( pNd, "Dude, where's my TextNode?" );
772 rPam.GetPoint()->nContent.Assign( pNd, m_nSttCnt );
773 rPam.SetMark();
774 if( m_bSplitNext )
776 rPam.GetPoint()->nNode = m_nSttNd + 1;
777 pNd = rPam.GetPoint()->nNode.GetNode().GetTxtNode();
779 rPam.GetPoint()->nContent.Assign( pNd, m_nSelEnd );
781 if( pHistory )
783 SwHistory* pSave = pHistory;
784 SwHistory aHst;
785 pHistory = &aHst;
786 DelCntntIndex( *rPam.GetMark(), *rPam.GetPoint() );
787 m_nSetPos = pHistory->Count();
789 pHistory = pSave;
790 pHistory->Move( 0, &aHst );
792 else
794 pHistory = new SwHistory;
795 DelCntntIndex( *rPam.GetMark(), *rPam.GetPoint() );
796 m_nSetPos = pHistory->Count();
797 if( !m_nSetPos )
798 delete pHistory, pHistory = 0;
801 rDoc.ReplaceRange( rPam, m_sIns, m_bRegExp );
802 rPam.DeleteMark();
805 void SwUndoReplace::Impl::SetEnd(SwPaM const& rPam)
807 const SwPosition* pEnd = rPam.End();
808 m_nEndNd = m_nOffset + pEnd->nNode.GetIndex();
809 m_nEndCnt = pEnd->nContent.GetIndex();
813 // SwUndoReRead //////////////////////////////////////////////////////////
816 SwUndoReRead::SwUndoReRead( const SwPaM& rPam, const SwGrfNode& rGrfNd )
817 : SwUndo( UNDO_REREAD ), nPos( rPam.GetPoint()->nNode.GetIndex() )
819 SaveGraphicData( rGrfNd );
823 SwUndoReRead::~SwUndoReRead()
825 delete pGrf;
826 delete pNm;
827 delete pFltr;
831 void SwUndoReRead::SetAndSave(::sw::UndoRedoContext & rContext)
833 SwDoc & rDoc = rContext.GetDoc();
834 SwGrfNode* pGrfNd = rDoc.GetNodes()[ nPos ]->GetGrfNode();
836 if( !pGrfNd )
837 return ;
839 // cache the old values
840 Graphic* pOldGrf = pGrf;
841 String* pOldNm = pNm;
842 String* pOldFltr = pFltr;
843 sal_uInt16 nOldMirr = nMirr;
845 SaveGraphicData( *pGrfNd );
846 if( pOldNm )
848 pGrfNd->ReRead( *pOldNm, pFltr ? *pFltr : aEmptyStr, 0, 0, sal_True );
849 delete pOldNm;
850 delete pOldFltr;
852 else
854 pGrfNd->ReRead( aEmptyStr, aEmptyStr, pOldGrf, 0, sal_True );
855 delete pOldGrf;
858 if( RES_MIRROR_GRAPH_DONT != nOldMirr )
859 pGrfNd->SetAttr( SwMirrorGrf() );
861 rContext.SetSelections(pGrfNd->GetFlyFmt(), 0);
865 void SwUndoReRead::UndoImpl(::sw::UndoRedoContext & rContext)
867 SetAndSave(rContext);
871 void SwUndoReRead::RedoImpl(::sw::UndoRedoContext & rContext)
873 SetAndSave(rContext);
877 void SwUndoReRead::SaveGraphicData( const SwGrfNode& rGrfNd )
879 if( rGrfNd.IsGrfLink() )
881 pNm = new String;
882 pFltr = new String;
883 rGrfNd.GetFileFilterNms( pNm, pFltr );
884 pGrf = 0;
886 else
888 ((SwGrfNode&)rGrfNd).SwapIn( sal_True );
889 pGrf = new Graphic( rGrfNd.GetGrf() );
890 pNm = pFltr = 0;
892 nMirr = rGrfNd.GetSwAttrSet().GetMirrorGrf().GetValue();
896 SwUndoInsertLabel::SwUndoInsertLabel( const SwLabelType eTyp,
897 const String &rTxt,
898 const String& rSeparator,
899 const String& rNumberSeparator,
900 const sal_Bool bBef,
901 const sal_uInt16 nInitId,
902 const String& rCharacterStyle,
903 const sal_Bool bCpyBorder )
904 : SwUndo( UNDO_INSERTLABEL ),
905 sText( rTxt ),
906 sSeparator( rSeparator ),
907 sNumberSeparator( rNumberSeparator ),//#i61007# order of captions
908 sCharacterStyle( rCharacterStyle ),
909 nFldId( nInitId ),
910 eType( eTyp ),
911 nLayerId( 0 ),
912 bBefore( bBef ),
913 bCpyBrd( bCpyBorder )
915 bUndoKeep = sal_False;
916 OBJECT.pUndoFly = 0;
917 OBJECT.pUndoAttr = 0;
920 SwUndoInsertLabel::~SwUndoInsertLabel()
922 if( LTYPE_OBJECT == eType || LTYPE_DRAW == eType )
924 delete OBJECT.pUndoFly;
925 delete OBJECT.pUndoAttr;
927 else
928 delete NODE.pUndoInsNd;
931 void SwUndoInsertLabel::UndoImpl(::sw::UndoRedoContext & rContext)
933 SwDoc & rDoc = rContext.GetDoc();
935 if( LTYPE_OBJECT == eType || LTYPE_DRAW == eType )
937 OSL_ENSURE( OBJECT.pUndoAttr && OBJECT.pUndoFly, "Pointer not initialized" );
938 SwFrmFmt* pFmt;
939 SdrObject *pSdrObj = 0;
940 if( OBJECT.pUndoAttr &&
941 0 != (pFmt = (SwFrmFmt*)OBJECT.pUndoAttr->GetFmt( rDoc )) &&
942 ( LTYPE_DRAW != eType ||
943 0 != (pSdrObj = pFmt->FindSdrObject()) ) )
945 OBJECT.pUndoAttr->UndoImpl(rContext);
946 OBJECT.pUndoFly->UndoImpl(rContext);
947 if( LTYPE_DRAW == eType )
949 pSdrObj->SetLayer( nLayerId );
953 else if( NODE.nNode )
955 if ( eType == LTYPE_TABLE && bUndoKeep )
957 SwTableNode *pNd = rDoc.GetNodes()[
958 rDoc.GetNodes()[NODE.nNode-1]->StartOfSectionIndex()]->GetTableNode();
959 if ( pNd )
960 pNd->GetTable().GetFrmFmt()->ResetFmtAttr( RES_KEEP );
962 SwPaM aPam( rDoc.GetNodes().GetEndOfContent() );
963 aPam.GetPoint()->nNode = NODE.nNode;
964 aPam.SetMark();
965 aPam.GetPoint()->nNode = NODE.nNode + 1;
966 NODE.pUndoInsNd = new SwUndoDelete( aPam, sal_True );
971 void SwUndoInsertLabel::RedoImpl(::sw::UndoRedoContext & rContext)
973 SwDoc & rDoc = rContext.GetDoc();
975 if( LTYPE_OBJECT == eType || LTYPE_DRAW == eType )
977 OSL_ENSURE( OBJECT.pUndoAttr && OBJECT.pUndoFly, "Pointer not initialized" );
978 SwFrmFmt* pFmt;
979 SdrObject *pSdrObj = 0;
980 if( OBJECT.pUndoAttr &&
981 0 != (pFmt = (SwFrmFmt*)OBJECT.pUndoAttr->GetFmt( rDoc )) &&
982 ( LTYPE_DRAW != eType ||
983 0 != (pSdrObj = pFmt->FindSdrObject()) ) )
985 OBJECT.pUndoFly->RedoImpl(rContext);
986 OBJECT.pUndoAttr->RedoImpl(rContext);
987 if( LTYPE_DRAW == eType )
989 pSdrObj->SetLayer( nLayerId );
990 if( pSdrObj->GetLayer() == rDoc.GetHellId() )
991 pSdrObj->SetLayer( rDoc.GetHeavenId() );
992 // OD 02.07.2003 #108784#
993 else if( pSdrObj->GetLayer() == rDoc.GetInvisibleHellId() )
994 pSdrObj->SetLayer( rDoc.GetInvisibleHeavenId() );
998 else if( NODE.pUndoInsNd )
1000 if ( eType == LTYPE_TABLE && bUndoKeep )
1002 SwTableNode *pNd = rDoc.GetNodes()[
1003 rDoc.GetNodes()[NODE.nNode-1]->StartOfSectionIndex()]->GetTableNode();
1004 if ( pNd )
1005 pNd->GetTable().GetFrmFmt()->SetFmtAttr( SvxFmtKeepItem(sal_True, RES_KEEP) );
1007 NODE.pUndoInsNd->UndoImpl(rContext);
1008 delete NODE.pUndoInsNd, NODE.pUndoInsNd = 0;
1012 void SwUndoInsertLabel::RepeatImpl(::sw::RepeatContext & rContext)
1014 SwDoc & rDoc = rContext.GetDoc();
1015 const SwPosition& rPos = *rContext.GetRepeatPaM().GetPoint();
1017 sal_uLong nIdx = 0;
1019 SwCntntNode* pCNd = rPos.nNode.GetNode().GetCntntNode();
1020 if( pCNd )
1021 switch( eType )
1023 case LTYPE_TABLE:
1025 const SwTableNode* pTNd = pCNd->FindTableNode();
1026 if( pTNd )
1027 nIdx = pTNd->GetIndex();
1029 break;
1031 case LTYPE_FLY:
1032 case LTYPE_OBJECT:
1034 SwFlyFrm* pFly;
1035 SwCntntFrm *pCnt = pCNd->getLayoutFrm( rDoc.GetCurrentLayout() );
1036 if( pCnt && 0 != ( pFly = pCnt->FindFlyFrm() ) )
1037 nIdx = pFly->GetFmt()->GetCntnt().GetCntntIdx()->GetIndex();
1039 break;
1040 case LTYPE_DRAW:
1041 break;
1044 if( nIdx )
1046 rDoc.InsertLabel( eType, sText, sSeparator, sNumberSeparator, bBefore,
1047 nFldId, nIdx, sCharacterStyle, bCpyBrd );
1051 // #111827#
1052 SwRewriter SwUndoInsertLabel::GetRewriter() const
1054 SwRewriter aRewriter;
1056 String aTmpStr;
1058 aTmpStr += String(SW_RES(STR_START_QUOTE));
1059 aTmpStr += ShortenString(sText, nUndoStringLength,
1060 String(SW_RES(STR_LDOTS)));
1061 aTmpStr += String(SW_RES(STR_END_QUOTE));
1063 aRewriter.AddRule(UndoArg1, aTmpStr);
1065 return aRewriter;
1068 void SwUndoInsertLabel::SetFlys( SwFrmFmt& rOldFly, SfxItemSet& rChgSet,
1069 SwFrmFmt& rNewFly )
1071 if( LTYPE_OBJECT == eType || LTYPE_DRAW == eType )
1073 SwUndoFmtAttrHelper aTmp( rOldFly, false );
1074 rOldFly.SetFmtAttr( rChgSet );
1075 if ( aTmp.GetUndo() )
1077 OBJECT.pUndoAttr = aTmp.ReleaseUndo();
1079 OBJECT.pUndoFly = new SwUndoInsLayFmt( &rNewFly,0,0 );
1083 void SwUndoInsertLabel::SetDrawObj( sal_uInt8 nLId )
1085 if( LTYPE_DRAW == eType )
1087 nLayerId = nLId;
1091 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */