merge the formfield patch from ooo-build
[ooovba.git] / svtools / source / edit / textundo.cxx
blob3f298aeaba5f22720f86a028c5bf7008b2127c0c
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: textundo.cxx,v $
10 * $Revision: 1.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svtools.hxx"
34 #include <svtools/texteng.hxx>
35 #include <svtools/textview.hxx>
36 #include <textundo.hxx>
37 #include <textund2.hxx>
38 #include <svtools/textdata.hxx>
39 #include <textdoc.hxx>
40 #include <textdat2.hxx>
42 TYPEINIT1( TextUndo, SfxUndoAction );
43 TYPEINIT1( TextUndoDelPara, TextUndo );
44 TYPEINIT1( TextUndoConnectParas, TextUndo );
45 TYPEINIT1( TextUndoSplitPara, TextUndo );
46 TYPEINIT1( TextUndoInsertChars, TextUndo );
47 TYPEINIT1( TextUndoRemoveChars, TextUndo );
48 TYPEINIT1( TextUndoSetAttribs, TextUndo );
51 TextUndoManager::TextUndoManager( TextEngine* p )
53 mpTextEngine = p;
56 TextUndoManager::~TextUndoManager()
60 BOOL __EXPORT TextUndoManager::Undo( USHORT nCount )
62 if ( GetUndoActionCount() == 0 )
63 return FALSE;
65 UndoRedoStart();
67 mpTextEngine->SetIsInUndo( TRUE );
68 BOOL bDone = SfxUndoManager::Undo( nCount );
69 mpTextEngine->SetIsInUndo( FALSE );
71 UndoRedoEnd();
73 return bDone;
76 BOOL __EXPORT TextUndoManager::Redo( USHORT nCount )
78 if ( GetRedoActionCount() == 0 )
79 return FALSE;
82 UndoRedoStart();
84 mpTextEngine->SetIsInUndo( TRUE );
85 BOOL bDone = SfxUndoManager::Redo( nCount );
86 mpTextEngine->SetIsInUndo( FALSE );
88 UndoRedoEnd();
90 return bDone;
93 void TextUndoManager::UndoRedoStart()
95 DBG_ASSERT( GetView(), "Undo/Redo: Active View?" );
97 // if ( GetView() )
98 // GetView()->HideSelection();
101 void TextUndoManager::UndoRedoEnd()
103 if ( GetView() )
105 TextSelection aNewSel( GetView()->GetSelection() );
106 aNewSel.GetStart() = aNewSel.GetEnd();
107 GetView()->ImpSetSelection( aNewSel );
110 mpTextEngine->UpdateSelections();
112 mpTextEngine->FormatAndUpdate( GetView() );
116 TextUndo::TextUndo( USHORT nI, TextEngine* p )
118 mnId = nI;
119 mpTextEngine = p;
122 TextUndo::~TextUndo()
126 USHORT __EXPORT TextUndo::GetId() const
128 //nId sollte mal entfallen => GetId ueberall ueberladen...
129 return mnId;
132 XubString __EXPORT TextUndo::GetComment() const
134 // return mpTextEngine->GetUndoComment( this );
135 return String();
138 void TextUndo::SetSelection( const TextSelection& rSel )
140 if ( GetView() )
141 GetView()->ImpSetSelection( rSel );
145 TextUndoDelPara::TextUndoDelPara( TextEngine* pTextEngine, TextNode* pNode, ULONG nPara )
146 : TextUndo( TEXTUNDO_DELCONTENT, pTextEngine )
148 mpNode = pNode;
149 mnPara = nPara;
150 mbDelObject = TRUE;
153 TextUndoDelPara::~TextUndoDelPara()
155 if ( mbDelObject )
156 delete mpNode;
159 void __EXPORT TextUndoDelPara::Undo()
161 GetTextEngine()->InsertContent( mpNode, mnPara );
162 mbDelObject = FALSE; // gehoert wieder der Engine
164 if ( GetView() )
166 TextSelection aSel( TextPaM( mnPara, 0 ), TextPaM( mnPara, mpNode->GetText().Len() ) );
167 SetSelection( aSel );
171 void __EXPORT TextUndoDelPara::Redo()
173 // pNode stimmt nicht mehr, falls zwischendurch Undos, in denen
174 // Absaetze verschmolzen sind.
175 mpNode = GetDoc()->GetNodes().GetObject( mnPara );
177 delete GetTEParaPortions()->GetObject( mnPara );
178 GetTEParaPortions()->Remove( mnPara );
180 // Node nicht loeschen, haengt im Undo!
181 GetDoc()->GetNodes().Remove( mnPara );
182 GetTextEngine()->ImpParagraphRemoved( mnPara );
184 mbDelObject = TRUE; // gehoert wieder dem Undo
186 ULONG nParas = GetDoc()->GetNodes().Count();
187 ULONG n = mnPara < nParas ? mnPara : (nParas-1);
188 TextNode* pN = GetDoc()->GetNodes().GetObject( n );
189 TextPaM aPaM( n, pN->GetText().Len() );
190 SetSelection( aPaM );
193 \f// -----------------------------------------------------------------------
194 // TextUndoConnectParas
195 // ------------------------------------------------------------------------
196 TextUndoConnectParas::TextUndoConnectParas( TextEngine* pTextEngine, ULONG nPara, USHORT nPos )
197 : TextUndo( TEXTUNDO_CONNECTPARAS, pTextEngine )
199 mnPara = nPara;
200 mnSepPos = nPos;
203 TextUndoConnectParas::~TextUndoConnectParas()
207 void __EXPORT TextUndoConnectParas::Undo()
209 TextPaM aPaM = GetTextEngine()->SplitContent( mnPara, mnSepPos );
210 SetSelection( aPaM );
213 void __EXPORT TextUndoConnectParas::Redo()
215 TextPaM aPaM = GetTextEngine()->ConnectContents( mnPara );
216 SetSelection( aPaM );
220 TextUndoSplitPara::TextUndoSplitPara( TextEngine* pTextEngine, ULONG nPara, USHORT nPos )
221 : TextUndo( TEXTUNDO_SPLITPARA, pTextEngine )
223 mnPara = nPara;
224 mnSepPos = nPos;
227 TextUndoSplitPara::~TextUndoSplitPara()
231 void __EXPORT TextUndoSplitPara::Undo()
233 TextPaM aPaM = GetTextEngine()->ConnectContents( mnPara );
234 SetSelection( aPaM );
237 void __EXPORT TextUndoSplitPara::Redo()
239 TextPaM aPaM = GetTextEngine()->SplitContent( mnPara, mnSepPos );
240 SetSelection( aPaM );
244 TextUndoInsertChars::TextUndoInsertChars( TextEngine* pTextEngine, const TextPaM& rTextPaM, const XubString& rStr )
245 : TextUndo( TEXTUNDO_INSERTCHARS, pTextEngine ),
246 maTextPaM( rTextPaM ), maText( rStr )
250 void __EXPORT TextUndoInsertChars::Undo()
252 TextSelection aSel( maTextPaM, maTextPaM );
253 aSel.GetEnd().GetIndex() = aSel.GetEnd().GetIndex() + maText.Len();
254 TextPaM aPaM = GetTextEngine()->ImpDeleteText( aSel );
255 SetSelection( aPaM );
258 void __EXPORT TextUndoInsertChars::Redo()
260 TextSelection aSel( maTextPaM, maTextPaM );
261 GetTextEngine()->ImpInsertText( aSel, maText );
262 TextPaM aNewPaM( maTextPaM );
263 aNewPaM.GetIndex() = aNewPaM.GetIndex() + maText.Len();
264 SetSelection( TextSelection( aSel.GetStart(), aNewPaM ) );
267 BOOL __EXPORT TextUndoInsertChars::Merge( SfxUndoAction* pNextAction )
269 if ( !pNextAction->ISA( TextUndoInsertChars ) )
270 return FALSE;
272 TextUndoInsertChars* pNext = (TextUndoInsertChars*)pNextAction;
274 if ( maTextPaM.GetPara() != pNext->maTextPaM.GetPara() )
275 return FALSE;
277 if ( ( maTextPaM.GetIndex() + maText.Len() ) == pNext->maTextPaM.GetIndex() )
279 maText += pNext->maText;
280 return TRUE;
282 return FALSE;
286 TextUndoRemoveChars::TextUndoRemoveChars( TextEngine* pTextEngine, const TextPaM& rTextPaM, const XubString& rStr )
287 : TextUndo( TEXTUNDO_REMOVECHARS, pTextEngine ),
288 maTextPaM( rTextPaM ), maText( rStr )
292 void __EXPORT TextUndoRemoveChars::Undo()
294 TextSelection aSel( maTextPaM, maTextPaM );
295 GetTextEngine()->ImpInsertText( aSel, maText );
296 aSel.GetEnd().GetIndex() = aSel.GetEnd().GetIndex() + maText.Len();
297 SetSelection( aSel );
300 void __EXPORT TextUndoRemoveChars::Redo()
302 TextSelection aSel( maTextPaM, maTextPaM );
303 aSel.GetEnd().GetIndex() = aSel.GetEnd().GetIndex() + maText.Len();
304 TextPaM aPaM = GetTextEngine()->ImpDeleteText( aSel );
305 SetSelection( aPaM );
309 TextUndoSetAttribs::TextUndoSetAttribs( TextEngine* pTextEngine, const TextSelection& rSel )
310 : TextUndo( TEXTUNDO_ATTRIBS, pTextEngine ), maSelection( rSel )
312 maSelection.Justify();
313 // aNewAttribs.Set( rNewItems );
314 // mbSetIsRemove = FALSE;
315 // mnRemoveWhich = 0;
316 // mnSpecial = 0;
319 TextUndoSetAttribs::~TextUndoSetAttribs()
321 // ...............
324 void __EXPORT TextUndoSetAttribs::Undo()
326 for ( ULONG nPara = maSelection.GetStart().GetPara(); nPara <= maSelection.GetEnd().GetPara(); nPara++ )
328 // ContentAttribsInfo* pInf = aPrevAttribs[ (USHORT)(nPara-aESel.nStartPara) ];
329 // GetTextEngine()->RemoveCharAttribs( nPara );
330 // TextNode* pNode = GetTextEngine()->GetTextDoc().GetObject( nPara );
331 // for ( USHORT nAttr = 0; nAttr < pInf->GetPrevCharAttribs().Count(); nAttr++ )
332 // {
333 // GetTextEngine()->GetTextDoc().InsertAttrib( pNode, pX->GetStart(), pX->GetEnd(), *pX->GetItem() );
334 // }
336 SetSelection( maSelection );
339 void __EXPORT TextUndoSetAttribs::Redo()
341 // if ( !bSetIsRemove )
342 // GetTextEngine()->SetAttribs( aSel, aNewAttribs, nSpecial );
343 // else
344 // GetTextEngine()->RemoveCharAttribs( aSel, bRemoveParaAttribs, nRemoveWhich );
345 SetSelection( maSelection );