xref: /AOO41X/main/svtools/source/edit/textundo.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_svtools.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <svtools/texteng.hxx>
32*cdf0e10cSrcweir #include <svtools/textview.hxx>
33*cdf0e10cSrcweir #include <textundo.hxx>
34*cdf0e10cSrcweir #include <textund2.hxx>
35*cdf0e10cSrcweir #include <svtools/textdata.hxx>
36*cdf0e10cSrcweir #include <textdoc.hxx>
37*cdf0e10cSrcweir #include <textdat2.hxx>
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir TYPEINIT1( TextUndo, SfxUndoAction );
40*cdf0e10cSrcweir TYPEINIT1( TextUndoDelPara, TextUndo );
41*cdf0e10cSrcweir TYPEINIT1( TextUndoConnectParas, TextUndo );
42*cdf0e10cSrcweir TYPEINIT1( TextUndoSplitPara, TextUndo );
43*cdf0e10cSrcweir TYPEINIT1( TextUndoInsertChars, TextUndo );
44*cdf0e10cSrcweir TYPEINIT1( TextUndoRemoveChars, TextUndo );
45*cdf0e10cSrcweir TYPEINIT1( TextUndoSetAttribs, TextUndo );
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir TextUndoManager::TextUndoManager( TextEngine* p )
49*cdf0e10cSrcweir {
50*cdf0e10cSrcweir 	mpTextEngine = p;
51*cdf0e10cSrcweir }
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir TextUndoManager::~TextUndoManager()
54*cdf0e10cSrcweir {
55*cdf0e10cSrcweir }
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir sal_Bool __EXPORT TextUndoManager::Undo()
58*cdf0e10cSrcweir {
59*cdf0e10cSrcweir 	if ( GetUndoActionCount() == 0 )
60*cdf0e10cSrcweir 		return sal_False;
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir 	UndoRedoStart();
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir 	mpTextEngine->SetIsInUndo( sal_True );
65*cdf0e10cSrcweir     sal_Bool bDone = SfxUndoManager::Undo();
66*cdf0e10cSrcweir 	mpTextEngine->SetIsInUndo( sal_False );
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir 	UndoRedoEnd();
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir 	return bDone;
71*cdf0e10cSrcweir }
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir sal_Bool __EXPORT TextUndoManager::Redo()
74*cdf0e10cSrcweir {
75*cdf0e10cSrcweir 	if ( GetRedoActionCount() == 0 )
76*cdf0e10cSrcweir 		return sal_False;
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir 	UndoRedoStart();
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir 	mpTextEngine->SetIsInUndo( sal_True );
82*cdf0e10cSrcweir     sal_Bool bDone = SfxUndoManager::Redo();
83*cdf0e10cSrcweir 	mpTextEngine->SetIsInUndo( sal_False );
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir 	UndoRedoEnd();
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir 	return bDone;
88*cdf0e10cSrcweir }
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir void TextUndoManager::UndoRedoStart()
91*cdf0e10cSrcweir {
92*cdf0e10cSrcweir 	DBG_ASSERT( GetView(), "Undo/Redo: Active View?" );
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir //	if ( GetView() )
95*cdf0e10cSrcweir //		GetView()->HideSelection();
96*cdf0e10cSrcweir }
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir void TextUndoManager::UndoRedoEnd()
99*cdf0e10cSrcweir {
100*cdf0e10cSrcweir 	if ( GetView() )
101*cdf0e10cSrcweir 	{
102*cdf0e10cSrcweir 		TextSelection aNewSel( GetView()->GetSelection() );
103*cdf0e10cSrcweir 		aNewSel.GetStart() = aNewSel.GetEnd();
104*cdf0e10cSrcweir 		GetView()->ImpSetSelection( aNewSel );
105*cdf0e10cSrcweir 	}
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir 	mpTextEngine->UpdateSelections();
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir 	mpTextEngine->FormatAndUpdate( GetView() );
110*cdf0e10cSrcweir }
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir TextUndo::TextUndo( TextEngine* p )
114*cdf0e10cSrcweir {
115*cdf0e10cSrcweir 	mpTextEngine = p;
116*cdf0e10cSrcweir }
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir TextUndo::~TextUndo()
119*cdf0e10cSrcweir {
120*cdf0e10cSrcweir }
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir XubString __EXPORT TextUndo::GetComment() const
123*cdf0e10cSrcweir {
124*cdf0e10cSrcweir //	return mpTextEngine->GetUndoComment( this );
125*cdf0e10cSrcweir 	return String();
126*cdf0e10cSrcweir }
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir void TextUndo::SetSelection( const TextSelection& rSel )
129*cdf0e10cSrcweir {
130*cdf0e10cSrcweir 	if ( GetView() )
131*cdf0e10cSrcweir 		GetView()->ImpSetSelection( rSel );
132*cdf0e10cSrcweir }
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir TextUndoDelPara::TextUndoDelPara( TextEngine* pTextEngine, TextNode* pNode, sal_uLong nPara )
136*cdf0e10cSrcweir 					: TextUndo( pTextEngine )
137*cdf0e10cSrcweir {
138*cdf0e10cSrcweir 	mpNode = pNode;
139*cdf0e10cSrcweir 	mnPara = nPara;
140*cdf0e10cSrcweir 	mbDelObject = sal_True;
141*cdf0e10cSrcweir }
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir TextUndoDelPara::~TextUndoDelPara()
144*cdf0e10cSrcweir {
145*cdf0e10cSrcweir 	if ( mbDelObject )
146*cdf0e10cSrcweir 		delete mpNode;
147*cdf0e10cSrcweir }
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir void __EXPORT TextUndoDelPara::Undo()
150*cdf0e10cSrcweir {
151*cdf0e10cSrcweir 	GetTextEngine()->InsertContent( mpNode, mnPara );
152*cdf0e10cSrcweir 	mbDelObject = sal_False;	// gehoert wieder der Engine
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir 	if ( GetView() )
155*cdf0e10cSrcweir 	{
156*cdf0e10cSrcweir 		TextSelection aSel( TextPaM( mnPara, 0 ), TextPaM( mnPara, mpNode->GetText().Len() ) );
157*cdf0e10cSrcweir 		SetSelection( aSel );
158*cdf0e10cSrcweir 	}
159*cdf0e10cSrcweir }
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir void __EXPORT TextUndoDelPara::Redo()
162*cdf0e10cSrcweir {
163*cdf0e10cSrcweir 	// pNode stimmt nicht mehr, falls zwischendurch Undos, in denen
164*cdf0e10cSrcweir 	// Absaetze verschmolzen sind.
165*cdf0e10cSrcweir 	mpNode = GetDoc()->GetNodes().GetObject( mnPara );
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir 	delete GetTEParaPortions()->GetObject( mnPara );
168*cdf0e10cSrcweir 	GetTEParaPortions()->Remove( mnPara );
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir 	// Node nicht loeschen, haengt im Undo!
171*cdf0e10cSrcweir 	GetDoc()->GetNodes().Remove( mnPara );
172*cdf0e10cSrcweir 	GetTextEngine()->ImpParagraphRemoved( mnPara );
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir 	mbDelObject = sal_True;	// gehoert wieder dem Undo
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir 	sal_uLong nParas = GetDoc()->GetNodes().Count();
177*cdf0e10cSrcweir 	sal_uLong n = mnPara < nParas ? mnPara : (nParas-1);
178*cdf0e10cSrcweir 	TextNode* pN = GetDoc()->GetNodes().GetObject( n );
179*cdf0e10cSrcweir 	TextPaM aPaM( n, pN->GetText().Len() );
180*cdf0e10cSrcweir 	SetSelection( aPaM );
181*cdf0e10cSrcweir }
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir // -----------------------------------------------------------------------
184*cdf0e10cSrcweir // TextUndoConnectParas
185*cdf0e10cSrcweir // ------------------------------------------------------------------------
186*cdf0e10cSrcweir TextUndoConnectParas::TextUndoConnectParas( TextEngine* pTextEngine, sal_uLong nPara, sal_uInt16 nPos )
187*cdf0e10cSrcweir 					: 	TextUndo( pTextEngine )
188*cdf0e10cSrcweir {
189*cdf0e10cSrcweir 	mnPara = nPara;
190*cdf0e10cSrcweir 	mnSepPos = nPos;
191*cdf0e10cSrcweir }
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir TextUndoConnectParas::~TextUndoConnectParas()
194*cdf0e10cSrcweir {
195*cdf0e10cSrcweir }
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir void __EXPORT TextUndoConnectParas::Undo()
198*cdf0e10cSrcweir {
199*cdf0e10cSrcweir 	TextPaM aPaM = GetTextEngine()->SplitContent( mnPara, mnSepPos );
200*cdf0e10cSrcweir 	SetSelection( aPaM );
201*cdf0e10cSrcweir }
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir void __EXPORT TextUndoConnectParas::Redo()
204*cdf0e10cSrcweir {
205*cdf0e10cSrcweir 	TextPaM aPaM = GetTextEngine()->ConnectContents( mnPara );
206*cdf0e10cSrcweir 	SetSelection( aPaM );
207*cdf0e10cSrcweir }
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir TextUndoSplitPara::TextUndoSplitPara( TextEngine* pTextEngine, sal_uLong nPara, sal_uInt16 nPos )
211*cdf0e10cSrcweir 					: TextUndo( pTextEngine )
212*cdf0e10cSrcweir {
213*cdf0e10cSrcweir 	mnPara = nPara;
214*cdf0e10cSrcweir 	mnSepPos = nPos;
215*cdf0e10cSrcweir }
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir TextUndoSplitPara::~TextUndoSplitPara()
218*cdf0e10cSrcweir {
219*cdf0e10cSrcweir }
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir void __EXPORT TextUndoSplitPara::Undo()
222*cdf0e10cSrcweir {
223*cdf0e10cSrcweir 	TextPaM aPaM = GetTextEngine()->ConnectContents( mnPara );
224*cdf0e10cSrcweir 	SetSelection( aPaM );
225*cdf0e10cSrcweir }
226*cdf0e10cSrcweir 
227*cdf0e10cSrcweir void __EXPORT TextUndoSplitPara::Redo()
228*cdf0e10cSrcweir {
229*cdf0e10cSrcweir 	TextPaM aPaM = GetTextEngine()->SplitContent( mnPara, mnSepPos );
230*cdf0e10cSrcweir 	SetSelection( aPaM );
231*cdf0e10cSrcweir }
232*cdf0e10cSrcweir 
233*cdf0e10cSrcweir 
234*cdf0e10cSrcweir TextUndoInsertChars::TextUndoInsertChars( TextEngine* pTextEngine, const TextPaM& rTextPaM, const XubString& rStr )
235*cdf0e10cSrcweir 					: TextUndo( pTextEngine ),
236*cdf0e10cSrcweir 						maTextPaM( rTextPaM ), maText( rStr )
237*cdf0e10cSrcweir {
238*cdf0e10cSrcweir }
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir void __EXPORT TextUndoInsertChars::Undo()
241*cdf0e10cSrcweir {
242*cdf0e10cSrcweir 	TextSelection aSel( maTextPaM, maTextPaM );
243*cdf0e10cSrcweir 	aSel.GetEnd().GetIndex() = aSel.GetEnd().GetIndex() + maText.Len();
244*cdf0e10cSrcweir 	TextPaM aPaM = GetTextEngine()->ImpDeleteText( aSel );
245*cdf0e10cSrcweir 	SetSelection( aPaM );
246*cdf0e10cSrcweir }
247*cdf0e10cSrcweir 
248*cdf0e10cSrcweir void __EXPORT TextUndoInsertChars::Redo()
249*cdf0e10cSrcweir {
250*cdf0e10cSrcweir 	TextSelection aSel( maTextPaM, maTextPaM );
251*cdf0e10cSrcweir 	GetTextEngine()->ImpInsertText( aSel, maText );
252*cdf0e10cSrcweir 	TextPaM aNewPaM( maTextPaM );
253*cdf0e10cSrcweir 	aNewPaM.GetIndex() = aNewPaM.GetIndex() + maText.Len();
254*cdf0e10cSrcweir 	SetSelection( TextSelection( aSel.GetStart(), aNewPaM ) );
255*cdf0e10cSrcweir }
256*cdf0e10cSrcweir 
257*cdf0e10cSrcweir sal_Bool __EXPORT TextUndoInsertChars::Merge( SfxUndoAction* pNextAction )
258*cdf0e10cSrcweir {
259*cdf0e10cSrcweir 	if ( !pNextAction->ISA( TextUndoInsertChars ) )
260*cdf0e10cSrcweir 		return sal_False;
261*cdf0e10cSrcweir 
262*cdf0e10cSrcweir 	TextUndoInsertChars* pNext = (TextUndoInsertChars*)pNextAction;
263*cdf0e10cSrcweir 
264*cdf0e10cSrcweir 	if ( maTextPaM.GetPara() != pNext->maTextPaM.GetPara() )
265*cdf0e10cSrcweir 		return sal_False;
266*cdf0e10cSrcweir 
267*cdf0e10cSrcweir 	if ( ( maTextPaM.GetIndex() + maText.Len() ) == pNext->maTextPaM.GetIndex() )
268*cdf0e10cSrcweir 	{
269*cdf0e10cSrcweir 		maText += pNext->maText;
270*cdf0e10cSrcweir 		return sal_True;
271*cdf0e10cSrcweir 	}
272*cdf0e10cSrcweir 	return sal_False;
273*cdf0e10cSrcweir }
274*cdf0e10cSrcweir 
275*cdf0e10cSrcweir 
276*cdf0e10cSrcweir TextUndoRemoveChars::TextUndoRemoveChars( TextEngine* pTextEngine, const TextPaM& rTextPaM, const XubString& rStr )
277*cdf0e10cSrcweir 					: TextUndo( pTextEngine ),
278*cdf0e10cSrcweir 						maTextPaM( rTextPaM ), maText( rStr )
279*cdf0e10cSrcweir {
280*cdf0e10cSrcweir }
281*cdf0e10cSrcweir 
282*cdf0e10cSrcweir void __EXPORT TextUndoRemoveChars::Undo()
283*cdf0e10cSrcweir {
284*cdf0e10cSrcweir 	TextSelection aSel( maTextPaM, maTextPaM );
285*cdf0e10cSrcweir 	GetTextEngine()->ImpInsertText( aSel, maText );
286*cdf0e10cSrcweir 	aSel.GetEnd().GetIndex() = aSel.GetEnd().GetIndex() + maText.Len();
287*cdf0e10cSrcweir 	SetSelection( aSel );
288*cdf0e10cSrcweir }
289*cdf0e10cSrcweir 
290*cdf0e10cSrcweir void __EXPORT TextUndoRemoveChars::Redo()
291*cdf0e10cSrcweir {
292*cdf0e10cSrcweir 	TextSelection aSel( maTextPaM, maTextPaM );
293*cdf0e10cSrcweir 	aSel.GetEnd().GetIndex() = aSel.GetEnd().GetIndex() + maText.Len();
294*cdf0e10cSrcweir 	TextPaM aPaM = GetTextEngine()->ImpDeleteText( aSel );
295*cdf0e10cSrcweir 	SetSelection( aPaM );
296*cdf0e10cSrcweir }
297*cdf0e10cSrcweir 
298*cdf0e10cSrcweir 
299*cdf0e10cSrcweir TextUndoSetAttribs::TextUndoSetAttribs( TextEngine* pTextEngine, const TextSelection& rSel )
300*cdf0e10cSrcweir 	: TextUndo( pTextEngine ), maSelection( rSel )
301*cdf0e10cSrcweir {
302*cdf0e10cSrcweir 	maSelection.Justify();
303*cdf0e10cSrcweir // 	aNewAttribs.Set( rNewItems );
304*cdf0e10cSrcweir //	mbSetIsRemove = sal_False;
305*cdf0e10cSrcweir //	mnRemoveWhich = 0;
306*cdf0e10cSrcweir //	mnSpecial = 0;
307*cdf0e10cSrcweir }
308*cdf0e10cSrcweir 
309*cdf0e10cSrcweir TextUndoSetAttribs::~TextUndoSetAttribs()
310*cdf0e10cSrcweir {
311*cdf0e10cSrcweir 	// ...............
312*cdf0e10cSrcweir }
313*cdf0e10cSrcweir 
314*cdf0e10cSrcweir void __EXPORT TextUndoSetAttribs::Undo()
315*cdf0e10cSrcweir {
316*cdf0e10cSrcweir 	for ( sal_uLong nPara = maSelection.GetStart().GetPara(); nPara <= maSelection.GetEnd().GetPara(); nPara++ )
317*cdf0e10cSrcweir 	{
318*cdf0e10cSrcweir //		ContentAttribsInfo* pInf = aPrevAttribs[ (sal_uInt16)(nPara-aESel.nStartPara) ];
319*cdf0e10cSrcweir //		GetTextEngine()->RemoveCharAttribs( nPara );
320*cdf0e10cSrcweir //		TextNode* pNode = GetTextEngine()->GetTextDoc().GetObject( nPara );
321*cdf0e10cSrcweir //		for ( sal_uInt16 nAttr = 0; nAttr < pInf->GetPrevCharAttribs().Count(); nAttr++ )
322*cdf0e10cSrcweir //		{
323*cdf0e10cSrcweir //			GetTextEngine()->GetTextDoc().InsertAttrib( pNode, pX->GetStart(), pX->GetEnd(), *pX->GetItem() );
324*cdf0e10cSrcweir //		}
325*cdf0e10cSrcweir 	}
326*cdf0e10cSrcweir 	SetSelection( maSelection );
327*cdf0e10cSrcweir }
328*cdf0e10cSrcweir 
329*cdf0e10cSrcweir void __EXPORT TextUndoSetAttribs::Redo()
330*cdf0e10cSrcweir {
331*cdf0e10cSrcweir //	if ( !bSetIsRemove )
332*cdf0e10cSrcweir //		GetTextEngine()->SetAttribs( aSel, aNewAttribs, nSpecial );
333*cdf0e10cSrcweir //	else
334*cdf0e10cSrcweir //		GetTextEngine()->RemoveCharAttribs( aSel, bRemoveParaAttribs, nRemoveWhich );
335*cdf0e10cSrcweir 	SetSelection( maSelection );
336*cdf0e10cSrcweir }
337