1*efeef26fSAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*efeef26fSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*efeef26fSAndrew Rist * or more contributor license agreements. See the NOTICE file
5*efeef26fSAndrew Rist * distributed with this work for additional information
6*efeef26fSAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*efeef26fSAndrew Rist * to you under the Apache License, Version 2.0 (the
8*efeef26fSAndrew Rist * "License"); you may not use this file except in compliance
9*efeef26fSAndrew Rist * with the License. You may obtain a copy of the License at
10*efeef26fSAndrew Rist *
11*efeef26fSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*efeef26fSAndrew Rist *
13*efeef26fSAndrew Rist * Unless required by applicable law or agreed to in writing,
14*efeef26fSAndrew Rist * software distributed under the License is distributed on an
15*efeef26fSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*efeef26fSAndrew Rist * KIND, either express or implied. See the License for the
17*efeef26fSAndrew Rist * specific language governing permissions and limitations
18*efeef26fSAndrew Rist * under the License.
19*efeef26fSAndrew Rist *
20*efeef26fSAndrew Rist *************************************************************/
21*efeef26fSAndrew Rist
22*efeef26fSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sw.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir #include <UndoSplitMove.hxx>
28cdf0e10cSrcweir
29cdf0e10cSrcweir #include <doc.hxx>
30cdf0e10cSrcweir #include <IDocumentUndoRedo.hxx>
31cdf0e10cSrcweir #include <pam.hxx>
32cdf0e10cSrcweir #include <swundo.hxx> // fuer die UndoIds
33cdf0e10cSrcweir #include <ndtxt.hxx>
34cdf0e10cSrcweir #include <UndoCore.hxx>
35cdf0e10cSrcweir #include <rolbck.hxx>
36cdf0e10cSrcweir
37cdf0e10cSrcweir
38cdf0e10cSrcweir // MOVE
39cdf0e10cSrcweir
SwUndoMove(const SwPaM & rRange,const SwPosition & rMvPos)40cdf0e10cSrcweir SwUndoMove::SwUndoMove( const SwPaM& rRange, const SwPosition& rMvPos )
41cdf0e10cSrcweir : SwUndo( UNDO_MOVE ), SwUndRng( rRange ),
42cdf0e10cSrcweir nMvDestNode( rMvPos.nNode.GetIndex() ),
43cdf0e10cSrcweir nMvDestCntnt( rMvPos.nContent.GetIndex() ),
44cdf0e10cSrcweir bMoveRedlines( false )
45cdf0e10cSrcweir {
46cdf0e10cSrcweir bMoveRange = bJoinNext = bJoinPrev = sal_False;
47cdf0e10cSrcweir
48cdf0e10cSrcweir // StartNode vorm loeschen von Fussnoten besorgen!
49cdf0e10cSrcweir SwDoc* pDoc = rRange.GetDoc();
50cdf0e10cSrcweir SwTxtNode* pTxtNd = pDoc->GetNodes()[ nSttNode ]->GetTxtNode();
51cdf0e10cSrcweir SwTxtNode* pEndTxtNd = pDoc->GetNodes()[ nEndNode ]->GetTxtNode();
52cdf0e10cSrcweir
53cdf0e10cSrcweir pHistory = new SwHistory;
54cdf0e10cSrcweir
55cdf0e10cSrcweir if( pTxtNd )
56cdf0e10cSrcweir {
57cdf0e10cSrcweir pHistory->Add( pTxtNd->GetTxtColl(), nSttNode, ND_TEXTNODE );
58cdf0e10cSrcweir if ( pTxtNd->GetpSwpHints() )
59cdf0e10cSrcweir {
60cdf0e10cSrcweir pHistory->CopyAttr( pTxtNd->GetpSwpHints(), nSttNode,
61cdf0e10cSrcweir 0, pTxtNd->GetTxt().Len(), false );
62cdf0e10cSrcweir }
63cdf0e10cSrcweir if( pTxtNd->HasSwAttrSet() )
64cdf0e10cSrcweir pHistory->CopyFmtAttr( *pTxtNd->GetpSwAttrSet(), nSttNode );
65cdf0e10cSrcweir }
66cdf0e10cSrcweir if( pEndTxtNd && pEndTxtNd != pTxtNd )
67cdf0e10cSrcweir {
68cdf0e10cSrcweir pHistory->Add( pEndTxtNd->GetTxtColl(), nEndNode, ND_TEXTNODE );
69cdf0e10cSrcweir if ( pEndTxtNd->GetpSwpHints() )
70cdf0e10cSrcweir {
71cdf0e10cSrcweir pHistory->CopyAttr( pEndTxtNd->GetpSwpHints(), nEndNode,
72cdf0e10cSrcweir 0, pEndTxtNd->GetTxt().Len(), false );
73cdf0e10cSrcweir }
74cdf0e10cSrcweir if( pEndTxtNd->HasSwAttrSet() )
75cdf0e10cSrcweir pHistory->CopyFmtAttr( *pEndTxtNd->GetpSwAttrSet(), nEndNode );
76cdf0e10cSrcweir }
77cdf0e10cSrcweir
78cdf0e10cSrcweir pTxtNd = rMvPos.nNode.GetNode().GetTxtNode();
79cdf0e10cSrcweir if (0 != pTxtNd)
80cdf0e10cSrcweir {
81cdf0e10cSrcweir pHistory->Add( pTxtNd->GetTxtColl(), nMvDestNode, ND_TEXTNODE );
82cdf0e10cSrcweir if ( pTxtNd->GetpSwpHints() )
83cdf0e10cSrcweir {
84cdf0e10cSrcweir pHistory->CopyAttr( pTxtNd->GetpSwpHints(), nMvDestNode,
85cdf0e10cSrcweir 0, pTxtNd->GetTxt().Len(), false );
86cdf0e10cSrcweir }
87cdf0e10cSrcweir if( pTxtNd->HasSwAttrSet() )
88cdf0e10cSrcweir pHistory->CopyFmtAttr( *pTxtNd->GetpSwAttrSet(), nMvDestNode );
89cdf0e10cSrcweir }
90cdf0e10cSrcweir
91cdf0e10cSrcweir
92cdf0e10cSrcweir nFtnStt = pHistory->Count();
93cdf0e10cSrcweir DelFtn( rRange );
94cdf0e10cSrcweir
95cdf0e10cSrcweir if( pHistory && !pHistory->Count() )
96cdf0e10cSrcweir DELETEZ( pHistory );
97cdf0e10cSrcweir }
98cdf0e10cSrcweir
99cdf0e10cSrcweir
SwUndoMove(SwDoc * pDoc,const SwNodeRange & rRg,const SwNodeIndex & rMvPos)100cdf0e10cSrcweir SwUndoMove::SwUndoMove( SwDoc* pDoc, const SwNodeRange& rRg,
101cdf0e10cSrcweir const SwNodeIndex& rMvPos )
102cdf0e10cSrcweir : SwUndo( UNDO_MOVE ),
103cdf0e10cSrcweir nMvDestNode( rMvPos.GetIndex() ),
104cdf0e10cSrcweir bMoveRedlines( false )
105cdf0e10cSrcweir {
106cdf0e10cSrcweir bMoveRange = sal_True;
107cdf0e10cSrcweir bJoinNext = bJoinPrev = sal_False;
108cdf0e10cSrcweir
109cdf0e10cSrcweir nSttCntnt = nEndCntnt = nMvDestCntnt = STRING_MAXLEN;
110cdf0e10cSrcweir
111cdf0e10cSrcweir nSttNode = rRg.aStart.GetIndex();
112cdf0e10cSrcweir nEndNode = rRg.aEnd.GetIndex();
113cdf0e10cSrcweir
114cdf0e10cSrcweir // DelFtn( rRange );
115cdf0e10cSrcweir
116cdf0e10cSrcweir // wird aus dem CntntBereich in den Sonderbereich verschoben ?
117cdf0e10cSrcweir sal_uLong nCntntStt = pDoc->GetNodes().GetEndOfAutotext().GetIndex();
118cdf0e10cSrcweir if( nMvDestNode < nCntntStt && rRg.aStart.GetIndex() > nCntntStt )
119cdf0e10cSrcweir {
120cdf0e10cSrcweir // loesche alle Fussnoten. Diese sind dort nicht erwuenscht.
121cdf0e10cSrcweir SwPosition aPtPos( rRg.aEnd );
122cdf0e10cSrcweir SwCntntNode* pCNd = rRg.aEnd.GetNode().GetCntntNode();
123cdf0e10cSrcweir if( pCNd )
124cdf0e10cSrcweir aPtPos.nContent.Assign( pCNd, pCNd->Len() );
125cdf0e10cSrcweir SwPosition aMkPos( rRg.aStart );
126cdf0e10cSrcweir if( 0 != ( pCNd = aMkPos.nNode.GetNode().GetCntntNode() ))
127cdf0e10cSrcweir aMkPos.nContent.Assign( pCNd, 0 );
128cdf0e10cSrcweir
129cdf0e10cSrcweir DelCntntIndex( aMkPos, aPtPos, nsDelCntntType::DELCNT_FTN );
130cdf0e10cSrcweir
131cdf0e10cSrcweir if( pHistory && !pHistory->Count() )
132cdf0e10cSrcweir DELETEZ( pHistory );
133cdf0e10cSrcweir }
134cdf0e10cSrcweir
135cdf0e10cSrcweir nFtnStt = 0;
136cdf0e10cSrcweir }
137cdf0e10cSrcweir
138cdf0e10cSrcweir
139cdf0e10cSrcweir
SetDestRange(const SwPaM & rRange,const SwPosition & rInsPos,sal_Bool bJoin,sal_Bool bCorrPam)140cdf0e10cSrcweir void SwUndoMove::SetDestRange( const SwPaM& rRange,
141cdf0e10cSrcweir const SwPosition& rInsPos,
142cdf0e10cSrcweir sal_Bool bJoin, sal_Bool bCorrPam )
143cdf0e10cSrcweir {
144cdf0e10cSrcweir const SwPosition *pStt = rRange.Start(),
145cdf0e10cSrcweir *pEnd = rRange.GetPoint() == pStt
146cdf0e10cSrcweir ? rRange.GetMark()
147cdf0e10cSrcweir : rRange.GetPoint();
148cdf0e10cSrcweir
149cdf0e10cSrcweir nDestSttNode = pStt->nNode.GetIndex();
150cdf0e10cSrcweir nDestSttCntnt = pStt->nContent.GetIndex();
151cdf0e10cSrcweir nDestEndNode = pEnd->nNode.GetIndex();
152cdf0e10cSrcweir nDestEndCntnt = pEnd->nContent.GetIndex();
153cdf0e10cSrcweir
154cdf0e10cSrcweir nInsPosNode = rInsPos.nNode.GetIndex();
155cdf0e10cSrcweir nInsPosCntnt = rInsPos.nContent.GetIndex();
156cdf0e10cSrcweir
157cdf0e10cSrcweir if( bCorrPam )
158cdf0e10cSrcweir {
159cdf0e10cSrcweir nDestSttNode--;
160cdf0e10cSrcweir nDestEndNode--;
161cdf0e10cSrcweir }
162cdf0e10cSrcweir
163cdf0e10cSrcweir bJoinNext = nDestSttNode != nDestEndNode &&
164cdf0e10cSrcweir pStt->nNode.GetNode().GetTxtNode() &&
165cdf0e10cSrcweir pEnd->nNode.GetNode().GetTxtNode();
166cdf0e10cSrcweir bJoinPrev = bJoin;
167cdf0e10cSrcweir }
168cdf0e10cSrcweir
169cdf0e10cSrcweir
SetDestRange(const SwNodeIndex & rStt,const SwNodeIndex & rEnd,const SwNodeIndex & rInsPos)170cdf0e10cSrcweir void SwUndoMove::SetDestRange( const SwNodeIndex& rStt,
171cdf0e10cSrcweir const SwNodeIndex& rEnd,
172cdf0e10cSrcweir const SwNodeIndex& rInsPos )
173cdf0e10cSrcweir {
174cdf0e10cSrcweir nDestSttNode = rStt.GetIndex();
175cdf0e10cSrcweir nDestEndNode = rEnd.GetIndex();
176cdf0e10cSrcweir if( nDestSttNode > nDestEndNode )
177cdf0e10cSrcweir {
178cdf0e10cSrcweir nDestSttNode = nDestEndNode;
179cdf0e10cSrcweir nDestEndNode = rStt.GetIndex();
180cdf0e10cSrcweir }
181cdf0e10cSrcweir nInsPosNode = rInsPos.GetIndex();
182cdf0e10cSrcweir
183cdf0e10cSrcweir nDestSttCntnt = nDestEndCntnt = nInsPosCntnt = STRING_MAXLEN;
184cdf0e10cSrcweir }
185cdf0e10cSrcweir
186cdf0e10cSrcweir
UndoImpl(::sw::UndoRedoContext & rContext)187cdf0e10cSrcweir void SwUndoMove::UndoImpl(::sw::UndoRedoContext & rContext)
188cdf0e10cSrcweir {
189cdf0e10cSrcweir SwDoc *const pDoc = & rContext.GetDoc();
190cdf0e10cSrcweir
191cdf0e10cSrcweir // Block, damit aus diesem gesprungen werden kann
192cdf0e10cSrcweir do {
193cdf0e10cSrcweir // erzeuge aus den Werten die Insert-Position und den Bereich
194cdf0e10cSrcweir SwNodeIndex aIdx( pDoc->GetNodes(), nDestSttNode );
195cdf0e10cSrcweir
196cdf0e10cSrcweir if( bMoveRange )
197cdf0e10cSrcweir {
198cdf0e10cSrcweir // nur ein Move mit SwRange
199cdf0e10cSrcweir SwNodeRange aRg( aIdx, aIdx );
200cdf0e10cSrcweir aRg.aEnd = nDestEndNode;
201cdf0e10cSrcweir aIdx = nInsPosNode;
202cdf0e10cSrcweir bool bSuccess = pDoc->MoveNodeRange( aRg, aIdx,
203cdf0e10cSrcweir IDocumentContentOperations::DOC_MOVEDEFAULT );
204cdf0e10cSrcweir if (!bSuccess)
205cdf0e10cSrcweir break;
206cdf0e10cSrcweir }
207cdf0e10cSrcweir else
208cdf0e10cSrcweir {
209cdf0e10cSrcweir SwPaM aPam( aIdx.GetNode(), nDestSttCntnt,
210cdf0e10cSrcweir *pDoc->GetNodes()[ nDestEndNode ], nDestEndCntnt );
211cdf0e10cSrcweir
212cdf0e10cSrcweir // #i17764# if redlines are to be moved, we may not remove them before
213cdf0e10cSrcweir // pDoc->Move gets a chance to handle them
214cdf0e10cSrcweir if( ! bMoveRedlines )
215cdf0e10cSrcweir RemoveIdxFromRange( aPam, sal_False );
216cdf0e10cSrcweir
217cdf0e10cSrcweir SwPosition aPos( *pDoc->GetNodes()[ nInsPosNode] );
218cdf0e10cSrcweir SwCntntNode* pCNd = aPos.nNode.GetNode().GetCntntNode();
219cdf0e10cSrcweir aPos.nContent.Assign( pCNd, nInsPosCntnt );
220cdf0e10cSrcweir
221cdf0e10cSrcweir if( pCNd->HasSwAttrSet() )
222cdf0e10cSrcweir pCNd->ResetAllAttr();
223cdf0e10cSrcweir
224cdf0e10cSrcweir if( pCNd->IsTxtNode() && ((SwTxtNode*)pCNd)->GetpSwpHints() )
225cdf0e10cSrcweir ((SwTxtNode*)pCNd)->ClearSwpHintsArr( false );
226cdf0e10cSrcweir
227cdf0e10cSrcweir // an der InsertPos erstmal alle Attribute entfernen,
228cdf0e10cSrcweir const bool bSuccess = pDoc->MoveRange( aPam, aPos, (bMoveRedlines)
229cdf0e10cSrcweir ? IDocumentContentOperations::DOC_MOVEREDLINES
230cdf0e10cSrcweir : IDocumentContentOperations::DOC_MOVEDEFAULT );
231cdf0e10cSrcweir if (!bSuccess)
232cdf0e10cSrcweir break;
233cdf0e10cSrcweir
234cdf0e10cSrcweir aPam.Exchange();
235cdf0e10cSrcweir aPam.DeleteMark();
236cdf0e10cSrcweir // pDoc->ResetAttr( aPam, sal_False );
237cdf0e10cSrcweir if( aPam.GetNode()->IsCntntNode() )
238cdf0e10cSrcweir aPam.GetNode()->GetCntntNode()->ResetAllAttr();
239cdf0e10cSrcweir // der Pam wird jetzt aufgegeben.
240cdf0e10cSrcweir }
241cdf0e10cSrcweir
242cdf0e10cSrcweir SwTxtNode* pTxtNd = aIdx.GetNode().GetTxtNode();
243cdf0e10cSrcweir if( bJoinNext )
244cdf0e10cSrcweir {
245cdf0e10cSrcweir {
246cdf0e10cSrcweir RemoveIdxRel( aIdx.GetIndex() + 1, SwPosition( aIdx,
247cdf0e10cSrcweir SwIndex( pTxtNd, pTxtNd->GetTxt().Len() ) ) );
248cdf0e10cSrcweir }
249cdf0e10cSrcweir // sind keine Pams mehr im naechsten TextNode
250cdf0e10cSrcweir pTxtNd->JoinNext();
251cdf0e10cSrcweir }
252cdf0e10cSrcweir
253cdf0e10cSrcweir if( bJoinPrev && pTxtNd->CanJoinPrev( &aIdx ) )
254cdf0e10cSrcweir {
255cdf0e10cSrcweir // ?? sind keine Pams mehr im naechsten TextNode ??
256cdf0e10cSrcweir pTxtNd = aIdx.GetNode().GetTxtNode();
257cdf0e10cSrcweir {
258cdf0e10cSrcweir RemoveIdxRel( aIdx.GetIndex() + 1, SwPosition( aIdx,
259cdf0e10cSrcweir SwIndex( pTxtNd, pTxtNd->GetTxt().Len() ) ) );
260cdf0e10cSrcweir }
261cdf0e10cSrcweir pTxtNd->JoinNext();
262cdf0e10cSrcweir }
263cdf0e10cSrcweir
264cdf0e10cSrcweir } while( sal_False );
265cdf0e10cSrcweir
266cdf0e10cSrcweir if( pHistory )
267cdf0e10cSrcweir {
268cdf0e10cSrcweir if( nFtnStt != pHistory->Count() )
269cdf0e10cSrcweir pHistory->Rollback( pDoc, nFtnStt );
270cdf0e10cSrcweir pHistory->TmpRollback( pDoc, 0 );
271cdf0e10cSrcweir pHistory->SetTmpEnd( pHistory->Count() );
272cdf0e10cSrcweir }
273cdf0e10cSrcweir
274cdf0e10cSrcweir // setze noch den Cursor auf den Undo-Bereich
275cdf0e10cSrcweir if( !bMoveRange )
276cdf0e10cSrcweir {
277cdf0e10cSrcweir AddUndoRedoPaM(rContext);
278cdf0e10cSrcweir }
279cdf0e10cSrcweir }
280cdf0e10cSrcweir
281cdf0e10cSrcweir
RedoImpl(::sw::UndoRedoContext & rContext)282cdf0e10cSrcweir void SwUndoMove::RedoImpl(::sw::UndoRedoContext & rContext)
283cdf0e10cSrcweir {
284cdf0e10cSrcweir SwPaM *const pPam = & AddUndoRedoPaM(rContext);
285cdf0e10cSrcweir SwDoc & rDoc = rContext.GetDoc();
286cdf0e10cSrcweir
287cdf0e10cSrcweir SwNodes& rNds = rDoc.GetNodes();
288cdf0e10cSrcweir SwNodeIndex aIdx( rNds, nMvDestNode );
289cdf0e10cSrcweir
290cdf0e10cSrcweir if( bMoveRange )
291cdf0e10cSrcweir {
292cdf0e10cSrcweir // nur ein Move mit SwRange
293cdf0e10cSrcweir SwNodeRange aRg( rNds, nSttNode, rNds, nEndNode );
294cdf0e10cSrcweir rDoc.MoveNodeRange( aRg, aIdx, (bMoveRedlines)
295cdf0e10cSrcweir ? IDocumentContentOperations::DOC_MOVEREDLINES
296cdf0e10cSrcweir : IDocumentContentOperations::DOC_MOVEDEFAULT );
297cdf0e10cSrcweir }
298cdf0e10cSrcweir else
299cdf0e10cSrcweir {
300cdf0e10cSrcweir SwPaM aPam( *pPam->GetPoint() );
301cdf0e10cSrcweir SetPaM( aPam );
302cdf0e10cSrcweir SwPosition aMvPos( aIdx, SwIndex( aIdx.GetNode().GetCntntNode(),
303cdf0e10cSrcweir nMvDestCntnt ));
304cdf0e10cSrcweir
305cdf0e10cSrcweir DelFtn( aPam );
306cdf0e10cSrcweir RemoveIdxFromRange( aPam, sal_False );
307cdf0e10cSrcweir
308cdf0e10cSrcweir aIdx = aPam.Start()->nNode;
309cdf0e10cSrcweir sal_Bool bJoinTxt = aIdx.GetNode().IsTxtNode();
310cdf0e10cSrcweir
311cdf0e10cSrcweir aIdx--;
312cdf0e10cSrcweir rDoc.MoveRange( aPam, aMvPos,
313cdf0e10cSrcweir IDocumentContentOperations::DOC_MOVEDEFAULT );
314cdf0e10cSrcweir
315cdf0e10cSrcweir if( nSttNode != nEndNode && bJoinTxt )
316cdf0e10cSrcweir {
317cdf0e10cSrcweir aIdx++;
318cdf0e10cSrcweir SwTxtNode * pTxtNd = aIdx.GetNode().GetTxtNode();
319cdf0e10cSrcweir if( pTxtNd && pTxtNd->CanJoinNext() )
320cdf0e10cSrcweir {
321cdf0e10cSrcweir {
322cdf0e10cSrcweir RemoveIdxRel( aIdx.GetIndex() + 1, SwPosition( aIdx,
323cdf0e10cSrcweir SwIndex( pTxtNd, pTxtNd->GetTxt().Len() ) ) );
324cdf0e10cSrcweir }
325cdf0e10cSrcweir pTxtNd->JoinNext();
326cdf0e10cSrcweir }
327cdf0e10cSrcweir }
328cdf0e10cSrcweir *pPam->GetPoint() = *aPam.GetPoint();
329cdf0e10cSrcweir pPam->SetMark();
330cdf0e10cSrcweir *pPam->GetMark() = *aPam.GetMark();
331cdf0e10cSrcweir }
332cdf0e10cSrcweir }
333cdf0e10cSrcweir
334cdf0e10cSrcweir
DelFtn(const SwPaM & rRange)335cdf0e10cSrcweir void SwUndoMove::DelFtn( const SwPaM& rRange )
336cdf0e10cSrcweir {
337cdf0e10cSrcweir // wird aus dem CntntBereich in den Sonderbereich verschoben ?
338cdf0e10cSrcweir SwDoc* pDoc = rRange.GetDoc();
339cdf0e10cSrcweir sal_uLong nCntntStt = pDoc->GetNodes().GetEndOfAutotext().GetIndex();
340cdf0e10cSrcweir if( nMvDestNode < nCntntStt &&
341cdf0e10cSrcweir rRange.GetPoint()->nNode.GetIndex() >= nCntntStt )
342cdf0e10cSrcweir {
343cdf0e10cSrcweir // loesche alle Fussnoten. Diese sind dort nicht erwuenscht.
344cdf0e10cSrcweir DelCntntIndex( *rRange.GetMark(), *rRange.GetPoint(),
345cdf0e10cSrcweir nsDelCntntType::DELCNT_FTN );
346cdf0e10cSrcweir
347cdf0e10cSrcweir if( pHistory && !pHistory->Count() )
348cdf0e10cSrcweir delete pHistory, pHistory = 0;
349cdf0e10cSrcweir }
350cdf0e10cSrcweir }
351cdf0e10cSrcweir
352