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 : #include <hintids.hxx>
21 : #include <fmtanchr.hxx>
22 : #include <frmfmt.hxx>
23 : #include <doc.hxx>
24 : #include <IDocumentUndoRedo.hxx>
25 : #include <IShellCursorSupplier.hxx>
26 : #include <docary.hxx>
27 : #include <swundo.hxx>
28 : #include <pam.hxx>
29 : #include <ndtxt.hxx>
30 : #include <UndoCore.hxx>
31 : #include <rolbck.hxx>
32 : #include <redline.hxx>
33 :
34 0 : SwUndoInserts::SwUndoInserts( SwUndoId nUndoId, const SwPaM& rPam )
35 : : SwUndo( nUndoId ), SwUndRng( rPam ),
36 : pTxtFmtColl( 0 ), pLastNdColl(0), pFrmFmts( 0 ), pRedlData( 0 ),
37 0 : bSttWasTxtNd( sal_True ), nNdDiff( 0 ), pPos( 0 ), nSetPos( 0 )
38 : {
39 0 : pHistory = new SwHistory;
40 0 : SwDoc* pDoc = (SwDoc*)rPam.GetDoc();
41 :
42 0 : SwTxtNode* pTxtNd = rPam.GetPoint()->nNode.GetNode().GetTxtNode();
43 0 : if( pTxtNd )
44 : {
45 0 : pTxtFmtColl = pTxtNd->GetTxtColl();
46 : pHistory->CopyAttr( pTxtNd->GetpSwpHints(), nSttNode,
47 0 : 0, pTxtNd->GetTxt().Len(), false );
48 0 : if( pTxtNd->HasSwAttrSet() )
49 0 : pHistory->CopyFmtAttr( *pTxtNd->GetpSwAttrSet(), nSttNode );
50 :
51 0 : if( !nSttCntnt ) // than take the Flys along
52 : {
53 0 : sal_uInt16 nArrLen = pDoc->GetSpzFrmFmts()->size();
54 0 : for( sal_uInt16 n = 0; n < nArrLen; ++n )
55 : {
56 0 : SwFrmFmt* pFmt = (*pDoc->GetSpzFrmFmts())[n];
57 0 : SwFmtAnchor const*const pAnchor = &pFmt->GetAnchor();
58 0 : const SwPosition* pAPos = pAnchor->GetCntntAnchor();
59 0 : if (pAPos &&
60 0 : (pAnchor->GetAnchorId() == FLY_AT_PARA) &&
61 0 : nSttNode == pAPos->nNode.GetIndex() )
62 : {
63 0 : if( !pFrmFmts )
64 0 : pFrmFmts = new std::vector<SwFrmFmt*>;
65 0 : pFrmFmts->push_back( pFmt );
66 : }
67 : }
68 : }
69 : }
70 : // consider Redline
71 0 : if( pDoc->IsRedlineOn() )
72 : {
73 0 : pRedlData = new SwRedlineData( nsRedlineType_t::REDLINE_INSERT, pDoc->GetRedlineAuthor() );
74 0 : SetRedlineMode( pDoc->GetRedlineMode() );
75 : }
76 0 : }
77 :
78 : // set destination after reading input
79 0 : void SwUndoInserts::SetInsertRange( const SwPaM& rPam, sal_Bool bScanFlys,
80 : sal_Bool bSttIsTxtNd )
81 : {
82 0 : const SwPosition* pTmpPos = rPam.End();
83 0 : nEndNode = pTmpPos->nNode.GetIndex();
84 0 : nEndCntnt = pTmpPos->nContent.GetIndex();
85 0 : if( rPam.HasMark() )
86 : {
87 0 : if( pTmpPos == rPam.GetPoint() )
88 0 : pTmpPos = rPam.GetMark();
89 : else
90 0 : pTmpPos = rPam.GetPoint();
91 :
92 0 : nSttNode = pTmpPos->nNode.GetIndex();
93 0 : nSttCntnt = pTmpPos->nContent.GetIndex();
94 :
95 0 : if( !bSttIsTxtNd ) // if a table selection is added ...
96 : {
97 0 : ++nSttNode; // ... than the CopyPam is not fully correct
98 0 : bSttWasTxtNd = sal_False;
99 : }
100 : }
101 :
102 0 : if( bScanFlys && !nSttCntnt )
103 : {
104 : // than collect all new Flys
105 0 : SwDoc* pDoc = (SwDoc*)rPam.GetDoc();
106 0 : sal_uInt16 nArrLen = pDoc->GetSpzFrmFmts()->size();
107 0 : for( sal_uInt16 n = 0; n < nArrLen; ++n )
108 : {
109 0 : SwFrmFmt* pFmt = (*pDoc->GetSpzFrmFmts())[n];
110 0 : SwFmtAnchor const*const pAnchor = &pFmt->GetAnchor();
111 0 : SwPosition const*const pAPos = pAnchor->GetCntntAnchor();
112 0 : if (pAPos &&
113 0 : (pAnchor->GetAnchorId() == FLY_AT_PARA) &&
114 0 : nSttNode == pAPos->nNode.GetIndex() )
115 : {
116 0 : std::vector<SwFrmFmt*>::iterator it;
117 0 : if( !pFrmFmts ||
118 0 : pFrmFmts->end() == ( it = std::find( pFrmFmts->begin(), pFrmFmts->end(), pFmt ) ) )
119 : {
120 : ::boost::shared_ptr<SwUndoInsLayFmt> const pFlyUndo(
121 0 : new SwUndoInsLayFmt(pFmt, 0, 0));
122 0 : m_FlyUndos.push_back(pFlyUndo);
123 : }
124 : else
125 0 : pFrmFmts->erase( it );
126 : }
127 : }
128 0 : delete pFrmFmts, pFrmFmts = 0;
129 : }
130 0 : }
131 :
132 0 : SwUndoInserts::~SwUndoInserts()
133 : {
134 0 : if( pPos ) // delete also the section from UndoNodes array
135 : {
136 : // Insert saves content in IconSection
137 0 : SwNodes& rUNds = pPos->nNode.GetNodes();
138 0 : if( pPos->nContent.GetIndex() ) // do not delete complete Node
139 : {
140 0 : SwTxtNode* pTxtNd = pPos->nNode.GetNode().GetTxtNode();
141 : OSL_ENSURE( pTxtNd, "no TextNode to delete from" );
142 0 : if( pTxtNd ) // robust
143 : {
144 0 : pTxtNd->EraseText( pPos->nContent );
145 : }
146 0 : pPos->nNode++;
147 : }
148 0 : pPos->nContent.Assign( 0, 0 );
149 0 : rUNds.Delete( pPos->nNode, rUNds.GetEndOfExtras().GetIndex() -
150 0 : pPos->nNode.GetIndex() );
151 0 : delete pPos;
152 : }
153 0 : delete pFrmFmts;
154 0 : delete pRedlData;
155 0 : }
156 :
157 0 : void SwUndoInserts::UndoImpl(::sw::UndoRedoContext & rContext)
158 : {
159 0 : SwDoc *const pDoc = & rContext.GetDoc();
160 0 : SwPaM *const pPam = & AddUndoRedoPaM(rContext);
161 :
162 0 : if( IDocumentRedlineAccess::IsRedlineOn( GetRedlineMode() ))
163 0 : pDoc->DeleteRedline( *pPam, true, USHRT_MAX );
164 :
165 : // if Point and Mark are different text nodes so a JoinNext has to be done
166 : bool bJoinNext = nSttNode != nEndNode &&
167 0 : pPam->GetMark()->nNode.GetNode().GetTxtNode() &&
168 0 : pPam->GetPoint()->nNode.GetNode().GetTxtNode();
169 :
170 :
171 : // Is there any content? (loading from template does not have content)
172 0 : if( nSttNode != nEndNode || nSttCntnt != nEndCntnt )
173 : {
174 0 : if( nSttNode != nEndNode )
175 : {
176 0 : SwTxtNode* pTxtNd = pDoc->GetNodes()[ nEndNode ]->GetTxtNode();
177 0 : if( pTxtNd && pTxtNd->GetTxt().Len() == nEndCntnt )
178 0 : pLastNdColl = pTxtNd->GetTxtColl();
179 : }
180 :
181 0 : RemoveIdxFromRange( *pPam, sal_False );
182 0 : SetPaM(*pPam);
183 :
184 : // are there Footnotes or CntntFlyFrames in text?
185 0 : nSetPos = pHistory->Count();
186 0 : nNdDiff = pPam->GetMark()->nNode.GetIndex();
187 0 : DelCntntIndex( *pPam->GetMark(), *pPam->GetPoint() );
188 0 : nNdDiff -= pPam->GetMark()->nNode.GetIndex();
189 :
190 0 : if( *pPam->GetPoint() != *pPam->GetMark() )
191 : {
192 0 : pPos = new SwPosition( *pPam->GetPoint() );
193 0 : MoveToUndoNds( *pPam, &pPos->nNode, &pPos->nContent );
194 :
195 0 : if( !bSttWasTxtNd )
196 0 : pPam->Move( fnMoveBackward, fnGoCntnt );
197 : }
198 : }
199 :
200 0 : if (m_FlyUndos.size())
201 : {
202 0 : sal_uLong nTmp = pPam->GetPoint()->nNode.GetIndex();
203 0 : for (size_t n = m_FlyUndos.size(); 0 < n; --n)
204 : {
205 0 : m_FlyUndos[ n-1 ]->UndoImpl(rContext);
206 : }
207 0 : nNdDiff += nTmp - pPam->GetPoint()->nNode.GetIndex();
208 : }
209 :
210 0 : SwNodeIndex& rIdx = pPam->GetPoint()->nNode;
211 0 : SwTxtNode* pTxtNode = rIdx.GetNode().GetTxtNode();
212 0 : if( pTxtNode )
213 : {
214 0 : if( !pTxtFmtColl ) // if 0 than it's no TextNode -> delete
215 : {
216 0 : SwNodeIndex aDelIdx( rIdx );
217 0 : ++rIdx;
218 0 : SwCntntNode* pCNd = rIdx.GetNode().GetCntntNode();
219 0 : xub_StrLen nCnt = 0;
220 0 : if( pCNd )
221 0 : nCnt = pCNd->Len();
222 0 : pPam->GetPoint()->nContent.Assign( pCNd, nCnt );
223 0 : pPam->SetMark();
224 0 : pPam->DeleteMark();
225 :
226 0 : RemoveIdxRel( aDelIdx.GetIndex(), *pPam->GetPoint() );
227 :
228 0 : pDoc->GetNodes().Delete( aDelIdx, 1 );
229 : }
230 : else
231 : {
232 0 : if( bJoinNext && pTxtNode->CanJoinNext())
233 : {
234 : {
235 0 : RemoveIdxRel( rIdx.GetIndex()+1, SwPosition( rIdx,
236 0 : SwIndex( pTxtNode, pTxtNode->GetTxt().Len() )));
237 : }
238 0 : pTxtNode->JoinNext();
239 : }
240 : // reset all text attributes in the paragraph!
241 0 : pTxtNode->RstAttr( SwIndex(pTxtNode, 0), pTxtNode->Len(),
242 0 : 0, 0, true );
243 :
244 0 : pTxtNode->ResetAllAttr();
245 :
246 0 : if( USHRT_MAX != pDoc->GetTxtFmtColls()->GetPos( pTxtFmtColl ))
247 0 : pTxtFmtColl = (SwTxtFmtColl*)pTxtNode->ChgFmtColl( pTxtFmtColl );
248 :
249 0 : pHistory->SetTmpEnd( nSetPos );
250 0 : pHistory->TmpRollback( pDoc, 0, false );
251 : }
252 : }
253 0 : }
254 :
255 0 : void SwUndoInserts::RedoImpl(::sw::UndoRedoContext & rContext)
256 : {
257 : // position cursor onto REDO section
258 0 : SwPaM *const pPam(& rContext.GetCursorSupplier().CreateNewShellCursor());
259 0 : SwDoc* pDoc = pPam->GetDoc();
260 0 : pPam->DeleteMark();
261 0 : pPam->GetPoint()->nNode = nSttNode - nNdDiff;
262 0 : SwCntntNode* pCNd = pPam->GetCntntNode();
263 0 : pPam->GetPoint()->nContent.Assign( pCNd, nSttCntnt );
264 :
265 0 : SwTxtFmtColl* pSavTxtFmtColl = pTxtFmtColl;
266 0 : if( pTxtFmtColl && pCNd && pCNd->IsTxtNode() )
267 0 : pSavTxtFmtColl = ((SwTxtNode*)pCNd)->GetTxtColl();
268 :
269 0 : pHistory->SetTmpEnd( nSetPos );
270 :
271 : // retrieve start position for rollback
272 0 : if( ( nSttNode != nEndNode || nSttCntnt != nEndCntnt ) && pPos )
273 : {
274 0 : sal_Bool bMvBkwrd = MovePtBackward( *pPam );
275 :
276 : // re-insert content again (first detach pPos!)
277 0 : sal_uLong nMvNd = pPos->nNode.GetIndex();
278 0 : xub_StrLen nMvCnt = pPos->nContent.GetIndex();
279 0 : DELETEZ( pPos );
280 0 : MoveFromUndoNds( *pDoc, nMvNd, nMvCnt, *pPam->GetMark() );
281 0 : if( bSttWasTxtNd )
282 0 : MovePtForward( *pPam, bMvBkwrd );
283 0 : pPam->Exchange();
284 : }
285 :
286 0 : if( USHRT_MAX != pDoc->GetTxtFmtColls()->GetPos( pTxtFmtColl ))
287 : {
288 0 : SwTxtNode* pTxtNd = pPam->GetMark()->nNode.GetNode().GetTxtNode();
289 0 : if( pTxtNd )
290 0 : pTxtNd->ChgFmtColl( pTxtFmtColl );
291 : }
292 0 : pTxtFmtColl = pSavTxtFmtColl;
293 :
294 0 : if( pLastNdColl && USHRT_MAX != pDoc->GetTxtFmtColls()->GetPos( pLastNdColl ) &&
295 0 : pPam->GetPoint()->nNode != pPam->GetMark()->nNode )
296 : {
297 0 : SwTxtNode* pTxtNd = pPam->GetPoint()->nNode.GetNode().GetTxtNode();
298 0 : if( pTxtNd )
299 0 : pTxtNd->ChgFmtColl( pLastNdColl );
300 : }
301 :
302 0 : for (size_t n = m_FlyUndos.size(); 0 < n; --n)
303 : {
304 0 : m_FlyUndos[ n-1 ]->RedoImpl(rContext);
305 : }
306 :
307 0 : pHistory->Rollback( pDoc, nSetPos );
308 :
309 0 : if( pRedlData && IDocumentRedlineAccess::IsRedlineOn( GetRedlineMode() ))
310 : {
311 0 : RedlineMode_t eOld = pDoc->GetRedlineMode();
312 0 : pDoc->SetRedlineMode_intern((RedlineMode_t)( eOld & ~nsRedlineMode_t::REDLINE_IGNORE ));
313 0 : pDoc->AppendRedline( new SwRedline( *pRedlData, *pPam ), true);
314 0 : pDoc->SetRedlineMode_intern( eOld );
315 : }
316 0 : else if( !( nsRedlineMode_t::REDLINE_IGNORE & GetRedlineMode() ) &&
317 0 : !pDoc->GetRedlineTbl().empty() )
318 0 : pDoc->SplitRedline( *pPam );
319 0 : }
320 :
321 0 : void SwUndoInserts::RepeatImpl(::sw::RepeatContext & rContext)
322 : {
323 0 : SwPaM aPam( rContext.GetDoc().GetNodes().GetEndOfContent() );
324 0 : SetPaM( aPam );
325 0 : SwPaM & rRepeatPaM( rContext.GetRepeatPaM() );
326 0 : aPam.GetDoc()->CopyRange( aPam, *rRepeatPaM.GetPoint(), false );
327 0 : }
328 :
329 0 : SwUndoInsDoc::SwUndoInsDoc( const SwPaM& rPam )
330 0 : : SwUndoInserts( UNDO_INSDOKUMENT, rPam )
331 : {
332 0 : }
333 :
334 0 : SwUndoCpyDoc::SwUndoCpyDoc( const SwPaM& rPam )
335 0 : : SwUndoInserts( UNDO_COPY, rPam )
336 : {
337 0 : }
338 :
339 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|