1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: textundo.cxx,v $
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
)
56 TextUndoManager::~TextUndoManager()
60 BOOL __EXPORT
TextUndoManager::Undo( USHORT nCount
)
62 if ( GetUndoActionCount() == 0 )
67 mpTextEngine
->SetIsInUndo( TRUE
);
68 BOOL bDone
= SfxUndoManager::Undo( nCount
);
69 mpTextEngine
->SetIsInUndo( FALSE
);
76 BOOL __EXPORT
TextUndoManager::Redo( USHORT nCount
)
78 if ( GetRedoActionCount() == 0 )
84 mpTextEngine
->SetIsInUndo( TRUE
);
85 BOOL bDone
= SfxUndoManager::Redo( nCount
);
86 mpTextEngine
->SetIsInUndo( FALSE
);
93 void TextUndoManager::UndoRedoStart()
95 DBG_ASSERT( GetView(), "Undo/Redo: Active View?" );
98 // GetView()->HideSelection();
101 void TextUndoManager::UndoRedoEnd()
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
)
122 TextUndo::~TextUndo()
126 USHORT __EXPORT
TextUndo::GetId() const
128 //nId sollte mal entfallen => GetId ueberall ueberladen...
132 XubString __EXPORT
TextUndo::GetComment() const
134 // return mpTextEngine->GetUndoComment( this );
138 void TextUndo::SetSelection( const TextSelection
& rSel
)
141 GetView()->ImpSetSelection( rSel
);
145 TextUndoDelPara::TextUndoDelPara( TextEngine
* pTextEngine
, TextNode
* pNode
, ULONG nPara
)
146 : TextUndo( TEXTUNDO_DELCONTENT
, pTextEngine
)
153 TextUndoDelPara::~TextUndoDelPara()
159 void __EXPORT
TextUndoDelPara::Undo()
161 GetTextEngine()->InsertContent( mpNode
, mnPara
);
162 mbDelObject
= FALSE
; // gehoert wieder der Engine
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
)
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
)
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
) )
272 TextUndoInsertChars
* pNext
= (TextUndoInsertChars
*)pNextAction
;
274 if ( maTextPaM
.GetPara() != pNext
->maTextPaM
.GetPara() )
277 if ( ( maTextPaM
.GetIndex() + maText
.Len() ) == pNext
->maTextPaM
.GetIndex() )
279 maText
+= pNext
->maText
;
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;
319 TextUndoSetAttribs::~TextUndoSetAttribs()
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++ )
333 // GetTextEngine()->GetTextDoc().InsertAttrib( pNode, pX->GetStart(), pX->GetEnd(), *pX->GetItem() );
336 SetSelection( maSelection
);
339 void __EXPORT
TextUndoSetAttribs::Redo()
341 // if ( !bSetIsRemove )
342 // GetTextEngine()->SetAttribs( aSel, aNewAttribs, nSpecial );
344 // GetTextEngine()->RemoveCharAttribs( aSel, bRemoveParaAttribs, nRemoveWhich );
345 SetSelection( maSelection
);