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 ), 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 (m_pUndoNodeIndex) // delete also the section from UndoNodes array
135 : {
136 : // Insert saves content in IconSection
137 0 : SwNodes& rUNds = m_pUndoNodeIndex->GetNodes();
138 0 : rUNds.Delete(*m_pUndoNodeIndex,
139 0 : rUNds.GetEndOfExtras().GetIndex() - m_pUndoNodeIndex->GetIndex());
140 0 : m_pUndoNodeIndex.reset();
141 : }
142 0 : delete pFrmFmts;
143 0 : delete pRedlData;
144 0 : }
145 :
146 0 : void SwUndoInserts::UndoImpl(::sw::UndoRedoContext & rContext)
147 : {
148 0 : SwDoc *const pDoc = & rContext.GetDoc();
149 0 : SwPaM *const pPam = & AddUndoRedoPaM(rContext);
150 :
151 0 : if( IDocumentRedlineAccess::IsRedlineOn( GetRedlineMode() ))
152 0 : pDoc->DeleteRedline( *pPam, true, USHRT_MAX );
153 :
154 : // if Point and Mark are different text nodes so a JoinNext has to be done
155 : bool bJoinNext = nSttNode != nEndNode &&
156 0 : pPam->GetMark()->nNode.GetNode().GetTxtNode() &&
157 0 : pPam->GetPoint()->nNode.GetNode().GetTxtNode();
158 :
159 :
160 : // Is there any content? (loading from template does not have content)
161 0 : if( nSttNode != nEndNode || nSttCntnt != nEndCntnt )
162 : {
163 0 : if( nSttNode != nEndNode )
164 : {
165 0 : SwTxtNode* pTxtNd = pDoc->GetNodes()[ nEndNode ]->GetTxtNode();
166 0 : if( pTxtNd && pTxtNd->GetTxt().Len() == nEndCntnt )
167 0 : pLastNdColl = pTxtNd->GetTxtColl();
168 : }
169 :
170 0 : RemoveIdxFromRange( *pPam, sal_False );
171 0 : SetPaM(*pPam);
172 :
173 : // are there Footnotes or CntntFlyFrames in text?
174 0 : nSetPos = pHistory->Count();
175 0 : nNdDiff = pPam->GetMark()->nNode.GetIndex();
176 0 : DelCntntIndex( *pPam->GetMark(), *pPam->GetPoint() );
177 0 : nNdDiff -= pPam->GetMark()->nNode.GetIndex();
178 :
179 0 : if( *pPam->GetPoint() != *pPam->GetMark() )
180 : {
181 : m_pUndoNodeIndex.reset(
182 0 : new SwNodeIndex(pDoc->GetNodes().GetEndOfContent()));
183 0 : MoveToUndoNds(*pPam, m_pUndoNodeIndex.get());
184 :
185 0 : if( !bSttWasTxtNd )
186 0 : pPam->Move( fnMoveBackward, fnGoCntnt );
187 : }
188 : }
189 :
190 0 : if (m_FlyUndos.size())
191 : {
192 0 : sal_uLong nTmp = pPam->GetPoint()->nNode.GetIndex();
193 0 : for (size_t n = m_FlyUndos.size(); 0 < n; --n)
194 : {
195 0 : m_FlyUndos[ n-1 ]->UndoImpl(rContext);
196 : }
197 0 : nNdDiff += nTmp - pPam->GetPoint()->nNode.GetIndex();
198 : }
199 :
200 0 : SwNodeIndex& rIdx = pPam->GetPoint()->nNode;
201 0 : SwTxtNode* pTxtNode = rIdx.GetNode().GetTxtNode();
202 0 : if( pTxtNode )
203 : {
204 0 : if( !pTxtFmtColl ) // if 0 than it's no TextNode -> delete
205 : {
206 0 : SwNodeIndex aDelIdx( rIdx );
207 0 : ++rIdx;
208 0 : SwCntntNode* pCNd = rIdx.GetNode().GetCntntNode();
209 0 : xub_StrLen nCnt = 0;
210 0 : if( pCNd )
211 0 : nCnt = pCNd->Len();
212 0 : pPam->GetPoint()->nContent.Assign( pCNd, nCnt );
213 0 : pPam->SetMark();
214 0 : pPam->DeleteMark();
215 :
216 0 : RemoveIdxRel( aDelIdx.GetIndex(), *pPam->GetPoint() );
217 :
218 0 : pDoc->GetNodes().Delete( aDelIdx, 1 );
219 : }
220 : else
221 : {
222 0 : if( bJoinNext && pTxtNode->CanJoinNext())
223 : {
224 : {
225 0 : RemoveIdxRel( rIdx.GetIndex()+1, SwPosition( rIdx,
226 0 : SwIndex( pTxtNode, pTxtNode->GetTxt().Len() )));
227 : }
228 0 : pTxtNode->JoinNext();
229 : }
230 : // reset all text attributes in the paragraph!
231 0 : pTxtNode->RstAttr( SwIndex(pTxtNode, 0), pTxtNode->Len(),
232 0 : 0, 0, true );
233 :
234 0 : pTxtNode->ResetAllAttr();
235 :
236 0 : if( USHRT_MAX != pDoc->GetTxtFmtColls()->GetPos( pTxtFmtColl ))
237 0 : pTxtFmtColl = (SwTxtFmtColl*)pTxtNode->ChgFmtColl( pTxtFmtColl );
238 :
239 0 : pHistory->SetTmpEnd( nSetPos );
240 0 : pHistory->TmpRollback( pDoc, 0, false );
241 : }
242 : }
243 0 : }
244 :
245 0 : void SwUndoInserts::RedoImpl(::sw::UndoRedoContext & rContext)
246 : {
247 : // position cursor onto REDO section
248 0 : SwPaM *const pPam(& rContext.GetCursorSupplier().CreateNewShellCursor());
249 0 : SwDoc* pDoc = pPam->GetDoc();
250 0 : pPam->DeleteMark();
251 0 : pPam->GetPoint()->nNode = nSttNode - nNdDiff;
252 0 : SwCntntNode* pCNd = pPam->GetCntntNode();
253 0 : pPam->GetPoint()->nContent.Assign( pCNd, nSttCntnt );
254 :
255 0 : SwTxtFmtColl* pSavTxtFmtColl = pTxtFmtColl;
256 0 : if( pTxtFmtColl && pCNd && pCNd->IsTxtNode() )
257 0 : pSavTxtFmtColl = ((SwTxtNode*)pCNd)->GetTxtColl();
258 :
259 0 : pHistory->SetTmpEnd( nSetPos );
260 :
261 : // retrieve start position for rollback
262 0 : if( ( nSttNode != nEndNode || nSttCntnt != nEndCntnt ) && m_pUndoNodeIndex)
263 : {
264 0 : sal_Bool bMvBkwrd = MovePtBackward( *pPam );
265 :
266 : // re-insert content again (first detach m_pUndoNodeIndex!)
267 0 : sal_uLong const nMvNd = m_pUndoNodeIndex->GetIndex();
268 0 : m_pUndoNodeIndex.reset();
269 0 : MoveFromUndoNds(*pDoc, nMvNd, *pPam->GetMark());
270 0 : if( bSttWasTxtNd )
271 0 : MovePtForward( *pPam, bMvBkwrd );
272 0 : pPam->Exchange();
273 : }
274 :
275 0 : if( USHRT_MAX != pDoc->GetTxtFmtColls()->GetPos( pTxtFmtColl ))
276 : {
277 0 : SwTxtNode* pTxtNd = pPam->GetMark()->nNode.GetNode().GetTxtNode();
278 0 : if( pTxtNd )
279 0 : pTxtNd->ChgFmtColl( pTxtFmtColl );
280 : }
281 0 : pTxtFmtColl = pSavTxtFmtColl;
282 :
283 0 : if( pLastNdColl && USHRT_MAX != pDoc->GetTxtFmtColls()->GetPos( pLastNdColl ) &&
284 0 : pPam->GetPoint()->nNode != pPam->GetMark()->nNode )
285 : {
286 0 : SwTxtNode* pTxtNd = pPam->GetPoint()->nNode.GetNode().GetTxtNode();
287 0 : if( pTxtNd )
288 0 : pTxtNd->ChgFmtColl( pLastNdColl );
289 : }
290 :
291 0 : for (size_t n = m_FlyUndos.size(); 0 < n; --n)
292 : {
293 0 : m_FlyUndos[ n-1 ]->RedoImpl(rContext);
294 : }
295 :
296 0 : pHistory->Rollback( pDoc, nSetPos );
297 :
298 0 : if( pRedlData && IDocumentRedlineAccess::IsRedlineOn( GetRedlineMode() ))
299 : {
300 0 : RedlineMode_t eOld = pDoc->GetRedlineMode();
301 0 : pDoc->SetRedlineMode_intern((RedlineMode_t)( eOld & ~nsRedlineMode_t::REDLINE_IGNORE ));
302 0 : pDoc->AppendRedline( new SwRedline( *pRedlData, *pPam ), true);
303 0 : pDoc->SetRedlineMode_intern( eOld );
304 : }
305 0 : else if( !( nsRedlineMode_t::REDLINE_IGNORE & GetRedlineMode() ) &&
306 0 : !pDoc->GetRedlineTbl().empty() )
307 0 : pDoc->SplitRedline( *pPam );
308 0 : }
309 :
310 0 : void SwUndoInserts::RepeatImpl(::sw::RepeatContext & rContext)
311 : {
312 0 : SwPaM aPam( rContext.GetDoc().GetNodes().GetEndOfContent() );
313 0 : SetPaM( aPam );
314 0 : SwPaM & rRepeatPaM( rContext.GetRepeatPaM() );
315 0 : aPam.GetDoc()->CopyRange( aPam, *rRepeatPaM.GetPoint(), false );
316 0 : }
317 :
318 0 : SwUndoInsDoc::SwUndoInsDoc( const SwPaM& rPam )
319 0 : : SwUndoInserts( UNDO_INSDOKUMENT, rPam )
320 : {
321 0 : }
322 :
323 0 : SwUndoCpyDoc::SwUndoCpyDoc( const SwPaM& rPam )
324 0 : : SwUndoInserts( UNDO_COPY, rPam )
325 : {
326 0 : }
327 :
328 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|