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