Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 :
21 : #include <UndoInsert.hxx>
22 :
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 :
29 : #include <docsh.hxx>
30 : #include <fmtcntnt.hxx>
31 : #include <fmtanchr.hxx>
32 : #include <frmfmt.hxx>
33 : #include <doc.hxx>
34 : #include <IDocumentUndoRedo.hxx>
35 : #include <swundo.hxx>
36 : #include <pam.hxx>
37 : #include <ndtxt.hxx>
38 : #include <UndoCore.hxx>
39 : #include <UndoDelete.hxx>
40 : #include <UndoAttribute.hxx>
41 : #include <rolbck.hxx>
42 : #include <ndgrf.hxx>
43 : #include <ndole.hxx>
44 : #include <grfatr.hxx>
45 : #include <cntfrm.hxx>
46 : #include <flyfrm.hxx>
47 : #include <fesh.hxx>
48 : #include <swtable.hxx>
49 : #include <redline.hxx>
50 : #include <docary.hxx>
51 : #include <acorrect.hxx>
52 : #include <dcontact.hxx>
53 :
54 : #include <comcore.hrc> // #111827#
55 : #include <undo.hrc>
56 :
57 : using namespace ::com::sun::star;
58 :
59 :
60 : //------------------------------------------------------------
61 :
62 : // INSERT
63 :
64 1 : String * SwUndoInsert::GetTxtFromDoc() const
65 : {
66 1 : String * pResult = NULL;
67 :
68 1 : SwNodeIndex aNd( pDoc->GetNodes(), nNode);
69 1 : SwCntntNode* pCNd = aNd.GetNode().GetCntntNode();
70 1 : SwPaM aPaM( *pCNd, nCntnt );
71 :
72 1 : aPaM.SetMark();
73 :
74 1 : if( pCNd->IsTxtNode() )
75 : {
76 : pResult = new String( ((SwTxtNode*)pCNd)->GetTxt().Copy(nCntnt-nLen,
77 1 : nLen ) );
78 :
79 : }
80 :
81 1 : return pResult;
82 : }
83 :
84 1 : void SwUndoInsert::Init(const SwNodeIndex & rNd)
85 : {
86 : // consider Redline
87 1 : pDoc = rNd.GetNode().GetDoc();
88 1 : if( pDoc->IsRedlineOn() )
89 : {
90 : pRedlData = new SwRedlineData( nsRedlineType_t::REDLINE_INSERT,
91 0 : pDoc->GetRedlineAuthor() );
92 0 : SetRedlineMode( pDoc->GetRedlineMode() );
93 : }
94 :
95 1 : pUndoTxt = GetTxtFromDoc();
96 :
97 1 : bCacheComment = false;
98 1 : }
99 :
100 : // #111827#
101 1 : SwUndoInsert::SwUndoInsert( const SwNodeIndex& rNd, xub_StrLen nCnt,
102 : xub_StrLen nL,
103 : const IDocumentContentOperations::InsertFlags nInsertFlags,
104 : sal_Bool bWDelim )
105 : : SwUndo(UNDO_TYPING), pPos( 0 ), pTxt( 0 ), pRedlData( 0 ),
106 1 : nNode( rNd.GetIndex() ), nCntnt(nCnt), nLen(nL),
107 : bIsWordDelim( bWDelim ), bIsAppend( sal_False )
108 2 : , m_nInsertFlags(nInsertFlags)
109 : {
110 1 : Init(rNd);
111 1 : }
112 :
113 : // #111827#
114 0 : SwUndoInsert::SwUndoInsert( const SwNodeIndex& rNd )
115 : : SwUndo(UNDO_SPLITNODE), pPos( 0 ), pTxt( 0 ),
116 0 : pRedlData( 0 ), nNode( rNd.GetIndex() ), nCntnt(0), nLen(1),
117 : bIsWordDelim( sal_False ), bIsAppend( sal_True )
118 0 : , m_nInsertFlags(IDocumentContentOperations::INS_EMPTYEXPAND)
119 : {
120 0 : Init(rNd);
121 0 : }
122 :
123 : // Check if the next Insert can be combined with the current one. If so
124 : // change the length and InsPos. As a result, SwDoc::Inser will not add a
125 : // new object into the Undo list.
126 :
127 0 : sal_Bool SwUndoInsert::CanGrouping( sal_Unicode cIns )
128 : {
129 0 : if( !bIsAppend && bIsWordDelim ==
130 0 : !GetAppCharClass().isLetterNumeric( rtl::OUString( cIns )) )
131 : {
132 0 : nLen++;
133 0 : nCntnt++;
134 :
135 0 : if (pUndoTxt)
136 0 : pUndoTxt->Insert(cIns);
137 :
138 0 : return sal_True;
139 : }
140 0 : return sal_False;
141 : }
142 :
143 0 : sal_Bool SwUndoInsert::CanGrouping( const SwPosition& rPos )
144 : {
145 0 : sal_Bool bRet = sal_False;
146 0 : if( nNode == rPos.nNode.GetIndex() &&
147 0 : nCntnt == rPos.nContent.GetIndex() )
148 : {
149 : // consider Redline
150 0 : SwDoc& rDoc = *rPos.nNode.GetNode().GetDoc();
151 0 : if( ( ~nsRedlineMode_t::REDLINE_SHOW_MASK & rDoc.GetRedlineMode() ) ==
152 0 : ( ~nsRedlineMode_t::REDLINE_SHOW_MASK & GetRedlineMode() ) )
153 : {
154 0 : bRet = sal_True;
155 :
156 : // than there is or was still an active Redline:
157 : // Check if there is another Redline at the InsPosition. If the
158 : // same exists only once, it can be combined.
159 0 : const SwRedlineTbl& rTbl = rDoc.GetRedlineTbl();
160 0 : if( !rTbl.empty() )
161 : {
162 0 : SwRedlineData aRData( nsRedlineType_t::REDLINE_INSERT, rDoc.GetRedlineAuthor() );
163 0 : const SwIndexReg* pIReg = rPos.nContent.GetIdxReg();
164 : SwIndex* pIdx;
165 0 : for( sal_uInt16 i = 0; i < rTbl.size(); ++i )
166 : {
167 0 : SwRedline* pRedl = rTbl[ i ];
168 0 : if( pIReg == (pIdx = &pRedl->End()->nContent)->GetIdxReg() &&
169 0 : nCntnt == pIdx->GetIndex() )
170 : {
171 0 : if( !pRedl->HasMark() || !pRedlData ||
172 0 : *pRedl != *pRedlData || *pRedl != aRData )
173 : {
174 0 : bRet = sal_False;
175 0 : break;
176 : }
177 : }
178 0 : }
179 : }
180 : }
181 : }
182 0 : return bRet;
183 : }
184 :
185 3 : SwUndoInsert::~SwUndoInsert()
186 : {
187 1 : if( pPos ) // delete the section from UndoNodes array
188 : {
189 : // Insert saves the content in IconSection
190 0 : SwNodes& rUNds = pPos->nNode.GetNode().GetNodes();
191 0 : if( pPos->nContent.GetIndex() ) // do not delete the whole node
192 : {
193 0 : SwTxtNode* pTxtNd = pPos->nNode.GetNode().GetTxtNode();
194 : OSL_ENSURE( pTxtNd, "no TextNode to delete from" );
195 0 : pTxtNd->EraseText( pPos->nContent );
196 0 : pPos->nNode++;
197 : }
198 0 : pPos->nContent.Assign( 0, 0 );
199 0 : rUNds.Delete( pPos->nNode, rUNds.GetEndOfExtras().GetIndex() -
200 0 : pPos->nNode.GetIndex() );
201 0 : delete pPos;
202 : }
203 1 : else if( pTxt ) // the inserted text
204 0 : delete pTxt;
205 1 : delete pRedlData;
206 1 : delete pUndoTxt;
207 2 : }
208 :
209 :
210 :
211 0 : void SwUndoInsert::UndoImpl(::sw::UndoRedoContext & rContext)
212 : {
213 0 : SwDoc *const pTmpDoc = & rContext.GetDoc();
214 0 : SwPaM *const pPam(& rContext.GetCursorSupplier().CreateNewShellCursor());
215 :
216 0 : if( bIsAppend )
217 : {
218 0 : pPam->GetPoint()->nNode = nNode;
219 :
220 0 : if( IDocumentRedlineAccess::IsRedlineOn( GetRedlineMode() ))
221 : {
222 0 : pPam->GetPoint()->nContent.Assign( pPam->GetCntntNode(), 0 );
223 0 : pPam->SetMark();
224 0 : pPam->Move( fnMoveBackward );
225 0 : pPam->Exchange();
226 0 : pTmpDoc->DeleteRedline( *pPam, true, USHRT_MAX );
227 : }
228 0 : pPam->DeleteMark();
229 0 : pTmpDoc->DelFullPara( *pPam );
230 0 : pPam->GetPoint()->nContent.Assign( pPam->GetCntntNode(), 0 );
231 : }
232 : else
233 : {
234 0 : sal_uLong nNd = nNode;
235 0 : xub_StrLen nCnt = nCntnt;
236 0 : if( nLen )
237 : {
238 0 : SwNodeIndex aNd( pTmpDoc->GetNodes(), nNode);
239 0 : SwCntntNode* pCNd = aNd.GetNode().GetCntntNode();
240 0 : SwPaM aPaM( *pCNd, nCntnt );
241 :
242 0 : aPaM.SetMark();
243 :
244 0 : SwTxtNode * const pTxtNode( pCNd->GetTxtNode() );
245 0 : if ( pTxtNode )
246 : {
247 0 : aPaM.GetPoint()->nContent -= nLen;
248 0 : if( IDocumentRedlineAccess::IsRedlineOn( GetRedlineMode() ))
249 0 : pTmpDoc->DeleteRedline( aPaM, true, USHRT_MAX );
250 0 : RemoveIdxFromRange( aPaM, sal_False );
251 0 : pTxt = new String( pTxtNode->GetTxt().Copy(nCntnt-nLen, nLen) );
252 0 : pTxtNode->EraseText( aPaM.GetPoint()->nContent, nLen );
253 : }
254 : else // otherwise Graphics/OLE/Text/...
255 : {
256 0 : aPaM.Move(fnMoveBackward);
257 0 : if( IDocumentRedlineAccess::IsRedlineOn( GetRedlineMode() ))
258 0 : pTmpDoc->DeleteRedline( aPaM, true, USHRT_MAX );
259 0 : RemoveIdxFromRange( aPaM, sal_False );
260 : }
261 :
262 0 : nNd = aPaM.GetPoint()->nNode.GetIndex();
263 0 : nCnt = aPaM.GetPoint()->nContent.GetIndex();
264 :
265 0 : if( !pTxt )
266 : {
267 0 : pPos = new SwPosition( *aPaM.GetPoint() );
268 0 : MoveToUndoNds( aPaM, &pPos->nNode, &pPos->nContent );
269 : }
270 0 : nNode = aPaM.GetPoint()->nNode.GetIndex();
271 0 : nCntnt = aPaM.GetPoint()->nContent.GetIndex();
272 : }
273 :
274 : // set cursor to Undo range
275 0 : pPam->DeleteMark();
276 :
277 0 : pPam->GetPoint()->nNode = nNd;
278 0 : pPam->GetPoint()->nContent.Assign(
279 0 : pPam->GetPoint()->nNode.GetNode().GetCntntNode(), nCnt );
280 : }
281 :
282 0 : DELETEZ(pUndoTxt);
283 0 : }
284 :
285 :
286 0 : void SwUndoInsert::RedoImpl(::sw::UndoRedoContext & rContext)
287 : {
288 0 : SwDoc *const pTmpDoc = & rContext.GetDoc();
289 0 : SwPaM *const pPam(& rContext.GetCursorSupplier().CreateNewShellCursor());
290 0 : pPam->DeleteMark();
291 :
292 0 : if( bIsAppend )
293 : {
294 0 : pPam->GetPoint()->nNode = nNode - 1;
295 0 : pTmpDoc->AppendTxtNode( *pPam->GetPoint() );
296 :
297 0 : pPam->SetMark();
298 0 : pPam->Move( fnMoveBackward );
299 0 : pPam->Exchange();
300 :
301 0 : if( pRedlData && IDocumentRedlineAccess::IsRedlineOn( GetRedlineMode() ))
302 : {
303 0 : RedlineMode_t eOld = pTmpDoc->GetRedlineMode();
304 0 : pTmpDoc->SetRedlineMode_intern((RedlineMode_t)(eOld & ~nsRedlineMode_t::REDLINE_IGNORE));
305 0 : pTmpDoc->AppendRedline( new SwRedline( *pRedlData, *pPam ), true);
306 0 : pTmpDoc->SetRedlineMode_intern( eOld );
307 : }
308 0 : else if( !( nsRedlineMode_t::REDLINE_IGNORE & GetRedlineMode() ) &&
309 0 : !pTmpDoc->GetRedlineTbl().empty() )
310 0 : pTmpDoc->SplitRedline( *pPam );
311 :
312 0 : pPam->DeleteMark();
313 : }
314 : else
315 : {
316 0 : pPam->GetPoint()->nNode = nNode;
317 : SwCntntNode *const pCNd =
318 0 : pPam->GetPoint()->nNode.GetNode().GetCntntNode();
319 0 : pPam->GetPoint()->nContent.Assign( pCNd, nCntnt );
320 :
321 0 : if( nLen )
322 : {
323 0 : sal_Bool bMvBkwrd = MovePtBackward( *pPam );
324 :
325 0 : if( pTxt )
326 : {
327 0 : SwTxtNode *const pTxtNode = pCNd->GetTxtNode();
328 : OSL_ENSURE( pTxtNode, "where is my textnode ?" );
329 0 : pTxtNode->InsertText( *pTxt, pPam->GetMark()->nContent,
330 0 : m_nInsertFlags );
331 0 : DELETEZ( pTxt );
332 : }
333 : else
334 : {
335 : // re-insert content (log out pPos before!)
336 0 : sal_uLong nMvNd = pPos->nNode.GetIndex();
337 0 : xub_StrLen nMvCnt = pPos->nContent.GetIndex();
338 0 : DELETEZ( pPos );
339 0 : MoveFromUndoNds( *pTmpDoc, nMvNd, nMvCnt, *pPam->GetMark() );
340 : }
341 0 : nNode = pPam->GetMark()->nNode.GetIndex();
342 0 : nCntnt = pPam->GetMark()->nContent.GetIndex();
343 :
344 0 : MovePtForward( *pPam, bMvBkwrd );
345 0 : pPam->Exchange();
346 0 : if( pRedlData && IDocumentRedlineAccess::IsRedlineOn( GetRedlineMode() ))
347 : {
348 0 : RedlineMode_t eOld = pTmpDoc->GetRedlineMode();
349 0 : pTmpDoc->SetRedlineMode_intern((RedlineMode_t)(eOld & ~nsRedlineMode_t::REDLINE_IGNORE));
350 : pTmpDoc->AppendRedline( new SwRedline( *pRedlData,
351 0 : *pPam ), true);
352 0 : pTmpDoc->SetRedlineMode_intern( eOld );
353 : }
354 0 : else if( !( nsRedlineMode_t::REDLINE_IGNORE & GetRedlineMode() ) &&
355 0 : !pTmpDoc->GetRedlineTbl().empty() )
356 0 : pTmpDoc->SplitRedline(*pPam);
357 : }
358 : }
359 :
360 0 : pUndoTxt = GetTxtFromDoc();
361 0 : }
362 :
363 :
364 0 : void SwUndoInsert::RepeatImpl(::sw::RepeatContext & rContext)
365 : {
366 0 : if( !nLen )
367 0 : return;
368 :
369 0 : SwDoc & rDoc = rContext.GetDoc();
370 0 : SwNodeIndex aNd( rDoc.GetNodes(), nNode );
371 0 : SwCntntNode* pCNd = aNd.GetNode().GetCntntNode();
372 :
373 0 : if( !bIsAppend && 1 == nLen ) // >1 than always Text, otherwise Graphics/OLE/Text/...
374 : {
375 0 : SwPaM aPaM( *pCNd, nCntnt );
376 0 : aPaM.SetMark();
377 0 : aPaM.Move(fnMoveBackward);
378 0 : pCNd = aPaM.GetCntntNode();
379 : }
380 :
381 : // What happens with the possible selected range ???
382 :
383 0 : switch( pCNd->GetNodeType() )
384 : {
385 : case ND_TEXTNODE:
386 0 : if( bIsAppend )
387 : {
388 0 : rDoc.AppendTxtNode( *rContext.GetRepeatPaM().GetPoint() );
389 : }
390 : else
391 : {
392 0 : String aTxt( ((SwTxtNode*)pCNd)->GetTxt() );
393 0 : ::sw::GroupUndoGuard const undoGuard(rDoc.GetIDocumentUndoRedo());
394 0 : rDoc.InsertString( rContext.GetRepeatPaM(),
395 0 : aTxt.Copy( nCntnt - nLen, nLen ) );
396 : }
397 0 : break;
398 : case ND_GRFNODE:
399 : {
400 0 : SwGrfNode* pGrfNd = (SwGrfNode*)pCNd;
401 0 : String sFile, sFilter;
402 0 : if( pGrfNd->IsGrfLink() )
403 0 : pGrfNd->GetFileFilterNms( &sFile, &sFilter );
404 :
405 0 : rDoc.Insert( rContext.GetRepeatPaM(), sFile, sFilter,
406 0 : &pGrfNd->GetGrf(),
407 0 : 0/* Graphics collection*/, NULL, NULL );
408 : }
409 0 : break;
410 :
411 : case ND_OLENODE:
412 : {
413 : // StarView does not yet provide an option to copy a StarOBJ
414 0 : SvStorageRef aRef = new SvStorage( aEmptyStr );
415 0 : SwOLEObj& rSwOLE = (SwOLEObj&)((SwOLENode*)pCNd)->GetOLEObj();
416 :
417 : // temporary storage until object is inserted
418 : // TODO/MBA: seems that here a physical copy is done - not as in drawing layer! Testing!
419 : // TODO/LATER: Copying through the container would copy the replacement image as well
420 0 : comphelper::EmbeddedObjectContainer aCnt;
421 0 : ::rtl::OUString aName = aCnt.CreateUniqueObjectName();
422 0 : if ( aCnt.StoreEmbeddedObject( rSwOLE.GetOleRef(), aName, sal_True ) )
423 : {
424 0 : uno::Reference < embed::XEmbeddedObject > aNew = aCnt.GetEmbeddedObject( aName );
425 0 : rDoc.Insert( rContext.GetRepeatPaM(),
426 : svt::EmbeddedObjectRef( aNew,
427 : static_cast<SwOLENode*>(pCNd)->GetAspect() ),
428 0 : NULL, NULL, NULL );
429 : }
430 :
431 0 : break;
432 : }
433 0 : }
434 : }
435 :
436 : // #111827#
437 1 : SwRewriter SwUndoInsert::GetRewriter() const
438 : {
439 1 : SwRewriter aResult;
440 1 : String * pStr = NULL;
441 1 : bool bDone = false;
442 :
443 1 : if (pTxt)
444 0 : pStr = pTxt;
445 1 : else if (pUndoTxt)
446 1 : pStr = pUndoTxt;
447 :
448 1 : if (pStr)
449 : {
450 : String aString = ShortenString(DenoteSpecialCharacters(*pStr),
451 : nUndoStringLength,
452 1 : String(SW_RES(STR_LDOTS)));
453 :
454 1 : aResult.AddRule(UndoArg1, aString);
455 :
456 1 : bDone = true;
457 : }
458 :
459 1 : if ( ! bDone )
460 : {
461 0 : aResult.AddRule(UndoArg1, String("??", RTL_TEXTENCODING_ASCII_US));
462 : }
463 :
464 1 : return aResult;
465 : }
466 :
467 :
468 : // SwUndoReplace /////////////////////////////////////////////////////////
469 :
470 :
471 : class SwUndoReplace::Impl
472 : : private SwUndoSaveCntnt
473 : {
474 : ::rtl::OUString m_sOld;
475 : ::rtl::OUString m_sIns;
476 : sal_uLong m_nSttNd, m_nEndNd, m_nOffset;
477 : xub_StrLen m_nSttCnt, m_nEndCnt, m_nSetPos, m_nSelEnd;
478 : bool m_bSplitNext : 1;
479 : bool m_bRegExp : 1;
480 : // metadata references for paragraph and following para (if m_bSplitNext)
481 : ::boost::shared_ptr< ::sfx2::MetadatableUndo > m_pMetadataUndoStart;
482 : ::boost::shared_ptr< ::sfx2::MetadatableUndo > m_pMetadataUndoEnd;
483 :
484 : public:
485 : Impl(SwPaM const& rPam, ::rtl::OUString const& rIns, bool const bRegExp);
486 0 : virtual ~Impl()
487 0 : {
488 0 : }
489 :
490 : virtual void UndoImpl( ::sw::UndoRedoContext & );
491 : virtual void RedoImpl( ::sw::UndoRedoContext & );
492 :
493 : void SetEnd(SwPaM const& rPam);
494 :
495 0 : ::rtl::OUString const& GetOld() const { return m_sOld; }
496 0 : ::rtl::OUString const& GetIns() const { return m_sIns; }
497 : };
498 :
499 :
500 0 : SwUndoReplace::SwUndoReplace(SwPaM const& rPam,
501 : ::rtl::OUString const& rIns, bool const bRegExp)
502 : : SwUndo( UNDO_REPLACE )
503 0 : , m_pImpl(new Impl(rPam, rIns, bRegExp))
504 : {
505 0 : }
506 :
507 0 : SwUndoReplace::~SwUndoReplace()
508 : {
509 0 : }
510 :
511 0 : void SwUndoReplace::UndoImpl(::sw::UndoRedoContext & rContext)
512 : {
513 0 : m_pImpl->UndoImpl(rContext);
514 0 : }
515 :
516 0 : void SwUndoReplace::RedoImpl(::sw::UndoRedoContext & rContext)
517 : {
518 0 : m_pImpl->RedoImpl(rContext);
519 0 : }
520 :
521 : SwRewriter
522 0 : MakeUndoReplaceRewriter(sal_uLong const occurrences,
523 : ::rtl::OUString const& sOld, ::rtl::OUString const& sNew)
524 : {
525 0 : SwRewriter aResult;
526 :
527 0 : if (1 < occurrences)
528 : {
529 0 : aResult.AddRule(UndoArg1, String::CreateFromInt32(occurrences));
530 0 : aResult.AddRule(UndoArg2, String(SW_RES(STR_OCCURRENCES_OF)));
531 :
532 0 : String aTmpStr;
533 0 : aTmpStr += String(SW_RES(STR_START_QUOTE));
534 : aTmpStr += ShortenString(sOld, nUndoStringLength,
535 0 : SW_RES(STR_LDOTS));
536 0 : aTmpStr += String(SW_RES(STR_END_QUOTE));
537 0 : aResult.AddRule(UndoArg3, aTmpStr);
538 : }
539 0 : else if (1 == occurrences)
540 : {
541 : {
542 0 : String aTmpStr;
543 :
544 0 : aTmpStr += String(SW_RES(STR_START_QUOTE));
545 : // #i33488 #
546 : aTmpStr += ShortenString(sOld, nUndoStringLength,
547 0 : SW_RES(STR_LDOTS));
548 0 : aTmpStr += String(SW_RES(STR_END_QUOTE));
549 0 : aResult.AddRule(UndoArg1, aTmpStr);
550 : }
551 :
552 0 : aResult.AddRule(UndoArg2, String(SW_RES(STR_YIELDS)));
553 :
554 : {
555 0 : String aTmpStr;
556 :
557 0 : aTmpStr += String(SW_RES(STR_START_QUOTE));
558 : // #i33488 #
559 : aTmpStr += ShortenString(sNew, nUndoStringLength,
560 0 : SW_RES(STR_LDOTS));
561 0 : aTmpStr += String(SW_RES(STR_END_QUOTE));
562 0 : aResult.AddRule(UndoArg3, aTmpStr);
563 : }
564 : }
565 :
566 0 : return aResult;
567 : }
568 :
569 : // #111827#
570 0 : SwRewriter SwUndoReplace::GetRewriter() const
571 : {
572 0 : return MakeUndoReplaceRewriter(1, m_pImpl->GetOld(), m_pImpl->GetIns());
573 : }
574 :
575 0 : void SwUndoReplace::SetEnd(SwPaM const& rPam)
576 : {
577 0 : m_pImpl->SetEnd(rPam);
578 0 : }
579 :
580 0 : SwUndoReplace::Impl::Impl(
581 : SwPaM const& rPam, ::rtl::OUString const& rIns, bool const bRegExp)
582 : : m_sIns( rIns )
583 : , m_nOffset( 0 )
584 0 : , m_bRegExp(bRegExp)
585 : {
586 :
587 0 : const SwPosition * pStt( rPam.Start() );
588 0 : const SwPosition * pEnd( rPam.End() );
589 :
590 0 : m_nSttNd = m_nEndNd = pStt->nNode.GetIndex();
591 0 : m_nSttCnt = pStt->nContent.GetIndex();
592 0 : m_nSelEnd = m_nEndCnt = pEnd->nContent.GetIndex();
593 :
594 0 : m_bSplitNext = m_nSttNd != pEnd->nNode.GetIndex();
595 :
596 0 : SwTxtNode* pNd = pStt->nNode.GetNode().GetTxtNode();
597 : OSL_ENSURE( pNd, "Dude, where's my TextNode?" );
598 :
599 0 : pHistory = new SwHistory;
600 0 : DelCntntIndex( *rPam.GetMark(), *rPam.GetPoint() );
601 :
602 0 : m_nSetPos = pHistory->Count();
603 :
604 0 : sal_uLong nNewPos = pStt->nNode.GetIndex();
605 0 : m_nOffset = m_nSttNd - nNewPos;
606 :
607 0 : if ( pNd->GetpSwpHints() )
608 : {
609 : pHistory->CopyAttr( pNd->GetpSwpHints(), nNewPos, 0,
610 0 : pNd->GetTxt().Len(), true );
611 : }
612 :
613 0 : if ( m_bSplitNext )
614 : {
615 0 : if( pNd->HasSwAttrSet() )
616 0 : pHistory->CopyFmtAttr( *pNd->GetpSwAttrSet(), nNewPos );
617 0 : pHistory->Add( pNd->GetTxtColl(), nNewPos, ND_TEXTNODE );
618 :
619 0 : SwTxtNode* pNext = pEnd->nNode.GetNode().GetTxtNode();
620 0 : sal_uLong nTmp = pNext->GetIndex();
621 : pHistory->CopyAttr( pNext->GetpSwpHints(), nTmp, 0,
622 0 : pNext->GetTxt().Len(), true );
623 0 : if( pNext->HasSwAttrSet() )
624 0 : pHistory->CopyFmtAttr( *pNext->GetpSwAttrSet(), nTmp );
625 0 : pHistory->Add( pNext->GetTxtColl(),nTmp, ND_TEXTNODE );
626 : // METADATA: store
627 0 : m_pMetadataUndoStart = pNd ->CreateUndo();
628 0 : m_pMetadataUndoEnd = pNext->CreateUndo();
629 : }
630 :
631 0 : if( !pHistory->Count() )
632 0 : delete pHistory, pHistory = 0;
633 :
634 0 : xub_StrLen nECnt = m_bSplitNext ? pNd->GetTxt().Len()
635 0 : : pEnd->nContent.GetIndex();
636 0 : m_sOld = pNd->GetTxt().Copy( m_nSttCnt, nECnt - m_nSttCnt );
637 0 : }
638 :
639 0 : void SwUndoReplace::Impl::UndoImpl(::sw::UndoRedoContext & rContext)
640 : {
641 0 : SwDoc *const pDoc = & rContext.GetDoc();
642 0 : SwPaM & rPam(rContext.GetCursorSupplier().CreateNewShellCursor());
643 0 : rPam.DeleteMark();
644 :
645 0 : SwTxtNode* pNd = pDoc->GetNodes()[ m_nSttNd - m_nOffset ]->GetTxtNode();
646 : OSL_ENSURE( pNd, "Dude, where's my TextNode?" );
647 :
648 0 : SwAutoCorrExceptWord* pACEWord = pDoc->GetAutoCorrExceptWord();
649 0 : if( pACEWord )
650 : {
651 0 : if ((1 == m_sIns.getLength()) && (1 == m_sOld.getLength()))
652 : {
653 0 : SwPosition aPos( *pNd ); aPos.nContent.Assign( pNd, m_nSttCnt );
654 0 : pACEWord->CheckChar( aPos, m_sOld[ 0 ] );
655 : }
656 0 : pDoc->SetAutoCorrExceptWord( 0 );
657 : }
658 :
659 0 : SwIndex aIdx( pNd, m_nSttCnt );
660 0 : if( m_nSttNd == m_nEndNd )
661 : {
662 0 : pNd->EraseText( aIdx, sal_uInt16( m_sIns.getLength() ) );
663 : }
664 : else
665 : {
666 0 : rPam.GetPoint()->nNode = *pNd;
667 0 : rPam.GetPoint()->nContent.Assign( pNd, m_nSttCnt );
668 0 : rPam.SetMark();
669 0 : rPam.GetPoint()->nNode = m_nEndNd - m_nOffset;
670 0 : rPam.GetPoint()->nContent.Assign( rPam.GetCntntNode(), m_nEndCnt );
671 : // move it out of the way so it is not registered at deleted node
672 0 : aIdx.Assign(0, 0);
673 :
674 0 : pDoc->DeleteAndJoin( rPam );
675 0 : rPam.DeleteMark();
676 0 : pNd = rPam.GetNode()->GetTxtNode();
677 : OSL_ENSURE( pNd, "Dude, where's my TextNode?" );
678 0 : aIdx.Assign( pNd, m_nSttCnt );
679 : }
680 :
681 0 : if( m_bSplitNext )
682 : {
683 0 : SwPosition aPos( *pNd, aIdx );
684 0 : pDoc->SplitNode( aPos, false );
685 0 : pNd->RestoreMetadata(m_pMetadataUndoEnd);
686 0 : pNd = pDoc->GetNodes()[ m_nSttNd - m_nOffset ]->GetTxtNode();
687 0 : aIdx.Assign( pNd, m_nSttCnt );
688 : // METADATA: restore
689 0 : pNd->RestoreMetadata(m_pMetadataUndoStart);
690 : }
691 :
692 0 : if (!m_sOld.isEmpty())
693 : {
694 0 : pNd->InsertText( m_sOld, aIdx );
695 : }
696 :
697 0 : if( pHistory )
698 : {
699 0 : if( pNd->GetpSwpHints() )
700 0 : pNd->ClearSwpHintsArr( true );
701 :
702 0 : pHistory->TmpRollback( pDoc, m_nSetPos, false );
703 0 : if ( m_nSetPos ) // there were footnotes/FlyFrames
704 : {
705 : // are there others than these?
706 0 : if( m_nSetPos < pHistory->Count() )
707 : {
708 : // than save those attributes as well
709 0 : SwHistory aHstr;
710 0 : aHstr.Move( 0, pHistory, m_nSetPos );
711 0 : pHistory->Rollback( pDoc );
712 0 : pHistory->Move( 0, &aHstr );
713 : }
714 : else
715 : {
716 0 : pHistory->Rollback( pDoc );
717 0 : DELETEZ( pHistory );
718 : }
719 : }
720 : }
721 :
722 0 : rPam.GetPoint()->nNode = m_nSttNd;
723 0 : rPam.GetPoint()->nContent = aIdx;
724 0 : }
725 :
726 0 : void SwUndoReplace::Impl::RedoImpl(::sw::UndoRedoContext & rContext)
727 : {
728 0 : SwDoc & rDoc = rContext.GetDoc();
729 0 : SwPaM & rPam(rContext.GetCursorSupplier().CreateNewShellCursor());
730 0 : rPam.DeleteMark();
731 0 : rPam.GetPoint()->nNode = m_nSttNd;
732 :
733 0 : SwTxtNode* pNd = rPam.GetPoint()->nNode.GetNode().GetTxtNode();
734 : OSL_ENSURE( pNd, "Dude, where's my TextNode?" );
735 0 : rPam.GetPoint()->nContent.Assign( pNd, m_nSttCnt );
736 0 : rPam.SetMark();
737 0 : if( m_bSplitNext )
738 : {
739 0 : rPam.GetPoint()->nNode = m_nSttNd + 1;
740 0 : pNd = rPam.GetPoint()->nNode.GetNode().GetTxtNode();
741 : }
742 0 : rPam.GetPoint()->nContent.Assign( pNd, m_nSelEnd );
743 :
744 0 : if( pHistory )
745 : {
746 0 : SwHistory* pSave = pHistory;
747 0 : SwHistory aHst;
748 0 : pHistory = &aHst;
749 0 : DelCntntIndex( *rPam.GetMark(), *rPam.GetPoint() );
750 0 : m_nSetPos = pHistory->Count();
751 :
752 0 : pHistory = pSave;
753 0 : pHistory->Move( 0, &aHst );
754 : }
755 : else
756 : {
757 0 : pHistory = new SwHistory;
758 0 : DelCntntIndex( *rPam.GetMark(), *rPam.GetPoint() );
759 0 : m_nSetPos = pHistory->Count();
760 0 : if( !m_nSetPos )
761 0 : delete pHistory, pHistory = 0;
762 : }
763 :
764 0 : rDoc.ReplaceRange( rPam, m_sIns, m_bRegExp );
765 0 : rPam.DeleteMark();
766 0 : }
767 :
768 0 : void SwUndoReplace::Impl::SetEnd(SwPaM const& rPam)
769 : {
770 0 : if( rPam.GetPoint()->nNode != rPam.GetMark()->nNode )
771 : {
772 : // multiple paragraphs were inserted
773 0 : const SwPosition* pEnd = rPam.End();
774 0 : m_nEndNd = m_nOffset + pEnd->nNode.GetIndex();
775 0 : m_nEndCnt = pEnd->nContent.GetIndex();
776 : }
777 0 : }
778 :
779 :
780 : // SwUndoReRead //////////////////////////////////////////////////////////
781 :
782 :
783 0 : SwUndoReRead::SwUndoReRead( const SwPaM& rPam, const SwGrfNode& rGrfNd )
784 0 : : SwUndo( UNDO_REREAD ), nPos( rPam.GetPoint()->nNode.GetIndex() )
785 : {
786 0 : SaveGraphicData( rGrfNd );
787 0 : }
788 :
789 :
790 0 : SwUndoReRead::~SwUndoReRead()
791 : {
792 0 : delete pGrf;
793 0 : delete pNm;
794 0 : delete pFltr;
795 0 : }
796 :
797 :
798 0 : void SwUndoReRead::SetAndSave(::sw::UndoRedoContext & rContext)
799 : {
800 0 : SwDoc & rDoc = rContext.GetDoc();
801 0 : SwGrfNode* pGrfNd = rDoc.GetNodes()[ nPos ]->GetGrfNode();
802 :
803 0 : if( !pGrfNd )
804 0 : return ;
805 :
806 : // cache the old values
807 0 : Graphic* pOldGrf = pGrf;
808 0 : String* pOldNm = pNm;
809 0 : String* pOldFltr = pFltr;
810 0 : sal_uInt16 nOldMirr = nMirr;
811 :
812 0 : SaveGraphicData( *pGrfNd );
813 0 : if( pOldNm )
814 : {
815 0 : pGrfNd->ReRead( *pOldNm, pFltr ? *pFltr : aEmptyStr, 0, 0, sal_True );
816 0 : delete pOldNm;
817 0 : delete pOldFltr;
818 : }
819 : else
820 : {
821 0 : pGrfNd->ReRead( aEmptyStr, aEmptyStr, pOldGrf, 0, sal_True );
822 0 : delete pOldGrf;
823 : }
824 :
825 0 : if( RES_MIRROR_GRAPH_DONT != nOldMirr )
826 0 : pGrfNd->SetAttr( SwMirrorGrf() );
827 :
828 0 : rContext.SetSelections(pGrfNd->GetFlyFmt(), 0);
829 : }
830 :
831 :
832 0 : void SwUndoReRead::UndoImpl(::sw::UndoRedoContext & rContext)
833 : {
834 0 : SetAndSave(rContext);
835 0 : }
836 :
837 :
838 0 : void SwUndoReRead::RedoImpl(::sw::UndoRedoContext & rContext)
839 : {
840 0 : SetAndSave(rContext);
841 0 : }
842 :
843 :
844 0 : void SwUndoReRead::SaveGraphicData( const SwGrfNode& rGrfNd )
845 : {
846 0 : if( rGrfNd.IsGrfLink() )
847 : {
848 0 : pNm = new String;
849 0 : pFltr = new String;
850 0 : rGrfNd.GetFileFilterNms( pNm, pFltr );
851 0 : pGrf = 0;
852 : }
853 : else
854 : {
855 0 : ((SwGrfNode&)rGrfNd).SwapIn( sal_True );
856 0 : pGrf = new Graphic( rGrfNd.GetGrf() );
857 0 : pNm = pFltr = 0;
858 : }
859 0 : nMirr = rGrfNd.GetSwAttrSet().GetMirrorGrf().GetValue();
860 0 : }
861 :
862 :
863 0 : SwUndoInsertLabel::SwUndoInsertLabel( const SwLabelType eTyp,
864 : const String &rTxt,
865 : const String& rSeparator,
866 : const String& rNumberSeparator,
867 : const sal_Bool bBef,
868 : const sal_uInt16 nInitId,
869 : const String& rCharacterStyle,
870 : const sal_Bool bCpyBorder )
871 : : SwUndo( UNDO_INSERTLABEL ),
872 : sText( rTxt ),
873 : sSeparator( rSeparator ),
874 : sNumberSeparator( rNumberSeparator ),//#i61007# order of captions
875 : sCharacterStyle( rCharacterStyle ),
876 : nFldId( nInitId ),
877 : eType( eTyp ),
878 : nLayerId( 0 ),
879 : bBefore( bBef ),
880 0 : bCpyBrd( bCpyBorder )
881 : {
882 0 : bUndoKeep = sal_False;
883 0 : OBJECT.pUndoFly = 0;
884 0 : OBJECT.pUndoAttr = 0;
885 0 : }
886 :
887 0 : SwUndoInsertLabel::~SwUndoInsertLabel()
888 : {
889 0 : if( LTYPE_OBJECT == eType || LTYPE_DRAW == eType )
890 : {
891 0 : delete OBJECT.pUndoFly;
892 0 : delete OBJECT.pUndoAttr;
893 : }
894 : else
895 0 : delete NODE.pUndoInsNd;
896 0 : }
897 :
898 0 : void SwUndoInsertLabel::UndoImpl(::sw::UndoRedoContext & rContext)
899 : {
900 0 : SwDoc & rDoc = rContext.GetDoc();
901 :
902 0 : if( LTYPE_OBJECT == eType || LTYPE_DRAW == eType )
903 : {
904 : OSL_ENSURE( OBJECT.pUndoAttr && OBJECT.pUndoFly, "Pointer not initialized" );
905 : SwFrmFmt* pFmt;
906 0 : SdrObject *pSdrObj = 0;
907 0 : if( OBJECT.pUndoAttr &&
908 0 : 0 != (pFmt = (SwFrmFmt*)OBJECT.pUndoAttr->GetFmt( rDoc )) &&
909 : ( LTYPE_DRAW != eType ||
910 : 0 != (pSdrObj = pFmt->FindSdrObject()) ) )
911 : {
912 0 : OBJECT.pUndoAttr->UndoImpl(rContext);
913 0 : OBJECT.pUndoFly->UndoImpl(rContext);
914 0 : if( LTYPE_DRAW == eType )
915 : {
916 0 : pSdrObj->SetLayer( nLayerId );
917 : }
918 0 : }
919 : }
920 0 : else if( NODE.nNode )
921 : {
922 0 : if ( eType == LTYPE_TABLE && bUndoKeep )
923 : {
924 0 : SwTableNode *pNd = rDoc.GetNodes()[
925 0 : rDoc.GetNodes()[NODE.nNode-1]->StartOfSectionIndex()]->GetTableNode();
926 0 : if ( pNd )
927 0 : pNd->GetTable().GetFrmFmt()->ResetFmtAttr( RES_KEEP );
928 : }
929 0 : SwPaM aPam( rDoc.GetNodes().GetEndOfContent() );
930 0 : aPam.GetPoint()->nNode = NODE.nNode;
931 0 : aPam.SetMark();
932 0 : aPam.GetPoint()->nNode = NODE.nNode + 1;
933 0 : NODE.pUndoInsNd = new SwUndoDelete( aPam, sal_True );
934 : }
935 0 : }
936 :
937 :
938 0 : void SwUndoInsertLabel::RedoImpl(::sw::UndoRedoContext & rContext)
939 : {
940 0 : SwDoc & rDoc = rContext.GetDoc();
941 :
942 0 : if( LTYPE_OBJECT == eType || LTYPE_DRAW == eType )
943 : {
944 : OSL_ENSURE( OBJECT.pUndoAttr && OBJECT.pUndoFly, "Pointer not initialized" );
945 : SwFrmFmt* pFmt;
946 0 : SdrObject *pSdrObj = 0;
947 0 : if( OBJECT.pUndoAttr &&
948 0 : 0 != (pFmt = (SwFrmFmt*)OBJECT.pUndoAttr->GetFmt( rDoc )) &&
949 : ( LTYPE_DRAW != eType ||
950 : 0 != (pSdrObj = pFmt->FindSdrObject()) ) )
951 : {
952 0 : OBJECT.pUndoFly->RedoImpl(rContext);
953 0 : OBJECT.pUndoAttr->RedoImpl(rContext);
954 0 : if( LTYPE_DRAW == eType )
955 : {
956 0 : pSdrObj->SetLayer( nLayerId );
957 0 : if( pSdrObj->GetLayer() == rDoc.GetHellId() )
958 0 : pSdrObj->SetLayer( rDoc.GetHeavenId() );
959 : // OD 02.07.2003 #108784#
960 0 : else if( pSdrObj->GetLayer() == rDoc.GetInvisibleHellId() )
961 0 : pSdrObj->SetLayer( rDoc.GetInvisibleHeavenId() );
962 : }
963 0 : }
964 : }
965 0 : else if( NODE.pUndoInsNd )
966 : {
967 0 : if ( eType == LTYPE_TABLE && bUndoKeep )
968 : {
969 0 : SwTableNode *pNd = rDoc.GetNodes()[
970 0 : rDoc.GetNodes()[NODE.nNode-1]->StartOfSectionIndex()]->GetTableNode();
971 0 : if ( pNd )
972 0 : pNd->GetTable().GetFrmFmt()->SetFmtAttr( SvxFmtKeepItem(sal_True, RES_KEEP) );
973 : }
974 0 : NODE.pUndoInsNd->UndoImpl(rContext);
975 0 : delete NODE.pUndoInsNd, NODE.pUndoInsNd = 0;
976 : }
977 0 : }
978 :
979 0 : void SwUndoInsertLabel::RepeatImpl(::sw::RepeatContext & rContext)
980 : {
981 0 : SwDoc & rDoc = rContext.GetDoc();
982 0 : const SwPosition& rPos = *rContext.GetRepeatPaM().GetPoint();
983 :
984 0 : sal_uLong nIdx = 0;
985 :
986 0 : SwCntntNode* pCNd = rPos.nNode.GetNode().GetCntntNode();
987 0 : if( pCNd )
988 0 : switch( eType )
989 : {
990 : case LTYPE_TABLE:
991 : {
992 0 : const SwTableNode* pTNd = pCNd->FindTableNode();
993 0 : if( pTNd )
994 0 : nIdx = pTNd->GetIndex();
995 : }
996 0 : break;
997 :
998 : case LTYPE_FLY:
999 : case LTYPE_OBJECT:
1000 : {
1001 : SwFlyFrm* pFly;
1002 0 : SwCntntFrm *pCnt = pCNd->getLayoutFrm( rDoc.GetCurrentLayout() );
1003 0 : if( pCnt && 0 != ( pFly = pCnt->FindFlyFrm() ) )
1004 0 : nIdx = pFly->GetFmt()->GetCntnt().GetCntntIdx()->GetIndex();
1005 : }
1006 0 : break;
1007 : case LTYPE_DRAW:
1008 0 : break;
1009 : }
1010 :
1011 0 : if( nIdx )
1012 : {
1013 : rDoc.InsertLabel( eType, sText, sSeparator, sNumberSeparator, bBefore,
1014 0 : nFldId, nIdx, sCharacterStyle, bCpyBrd );
1015 : }
1016 0 : }
1017 :
1018 : // #111827#
1019 0 : SwRewriter SwUndoInsertLabel::GetRewriter() const
1020 : {
1021 0 : SwRewriter aRewriter;
1022 :
1023 0 : String aTmpStr;
1024 :
1025 0 : aTmpStr += String(SW_RES(STR_START_QUOTE));
1026 : aTmpStr += ShortenString(sText, nUndoStringLength,
1027 0 : String(SW_RES(STR_LDOTS)));
1028 0 : aTmpStr += String(SW_RES(STR_END_QUOTE));
1029 :
1030 0 : aRewriter.AddRule(UndoArg1, aTmpStr);
1031 :
1032 0 : return aRewriter;
1033 : }
1034 :
1035 0 : void SwUndoInsertLabel::SetFlys( SwFrmFmt& rOldFly, SfxItemSet& rChgSet,
1036 : SwFrmFmt& rNewFly )
1037 : {
1038 0 : if( LTYPE_OBJECT == eType || LTYPE_DRAW == eType )
1039 : {
1040 0 : SwUndoFmtAttrHelper aTmp( rOldFly, false );
1041 0 : rOldFly.SetFmtAttr( rChgSet );
1042 0 : if ( aTmp.GetUndo() )
1043 : {
1044 0 : OBJECT.pUndoAttr = aTmp.ReleaseUndo();
1045 : }
1046 0 : OBJECT.pUndoFly = new SwUndoInsLayFmt( &rNewFly,0,0 );
1047 : }
1048 0 : }
1049 :
1050 0 : void SwUndoInsertLabel::SetDrawObj( sal_uInt8 nLId )
1051 : {
1052 0 : if( LTYPE_DRAW == eType )
1053 : {
1054 0 : nLayerId = nLId;
1055 : }
1056 0 : }
1057 :
1058 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|