Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "textundo.hxx"
21 : #include "textund2.hxx"
22 : #include "textundo.hrc"
23 :
24 : #include <vcl/texteng.hxx>
25 : #include <vcl/textview.hxx>
26 : #include <vcl/textdata.hxx>
27 : #include <textdoc.hxx>
28 : #include <textdat2.hxx>
29 : #include <svdata.hxx> // ImplGetResMgr()
30 : #include <tools/resid.hxx>
31 :
32 0 : TYPEINIT1( TextUndo, SfxUndoAction );
33 0 : TYPEINIT1( TextUndoDelPara, TextUndo );
34 0 : TYPEINIT1( TextUndoConnectParas, TextUndo );
35 0 : TYPEINIT1( TextUndoSplitPara, TextUndo );
36 0 : TYPEINIT1( TextUndoInsertChars, TextUndo );
37 0 : TYPEINIT1( TextUndoRemoveChars, TextUndo );
38 :
39 :
40 : namespace
41 : {
42 :
43 : // Shorten() -- inserts ellipsis (...) in the middle of a long text
44 0 : void Shorten (OUString& rString)
45 : {
46 0 : unsigned nLen = rString.getLength();
47 0 : if (nLen > 48)
48 : {
49 : // If possible, we don't break a word, hence first we look for a space.
50 : // Space before the ellipsis:
51 0 : int iFirst = rString.lastIndexOf(' ', 32);
52 0 : if (iFirst == -1 || unsigned(iFirst) < 16)
53 0 : iFirst = 24; // not possible
54 : // Space after the ellipsis:
55 0 : int iLast = rString.indexOf(' ', nLen - 16);
56 0 : if (iLast == -1 || unsigned(iLast) > nLen - 4)
57 0 : iLast = nLen - 8; // not possible
58 : // finally:
59 0 : rString =
60 0 : rString.copy(0, iFirst + 1) +
61 0 : "..." +
62 0 : rString.copy(iLast);
63 : }
64 0 : }
65 :
66 : } // namespace
67 :
68 :
69 0 : TextUndoManager::TextUndoManager( TextEngine* p )
70 : {
71 0 : mpTextEngine = p;
72 0 : }
73 :
74 0 : TextUndoManager::~TextUndoManager()
75 : {
76 0 : }
77 :
78 0 : bool TextUndoManager::Undo()
79 : {
80 0 : if ( GetUndoActionCount() == 0 )
81 0 : return false;
82 :
83 0 : UndoRedoStart();
84 :
85 0 : mpTextEngine->SetIsInUndo( sal_True );
86 0 : bool bDone = SfxUndoManager::Undo();
87 0 : mpTextEngine->SetIsInUndo( sal_False );
88 :
89 0 : UndoRedoEnd();
90 :
91 0 : return bDone;
92 : }
93 :
94 0 : bool TextUndoManager::Redo()
95 : {
96 0 : if ( GetRedoActionCount() == 0 )
97 0 : return false;
98 :
99 :
100 0 : UndoRedoStart();
101 :
102 0 : mpTextEngine->SetIsInUndo( sal_True );
103 0 : bool bDone = SfxUndoManager::Redo();
104 0 : mpTextEngine->SetIsInUndo( sal_False );
105 :
106 0 : UndoRedoEnd();
107 :
108 0 : return bDone;
109 : }
110 :
111 0 : void TextUndoManager::UndoRedoStart()
112 : {
113 : DBG_ASSERT( GetView(), "Undo/Redo: Active View?" );
114 0 : }
115 :
116 0 : void TextUndoManager::UndoRedoEnd()
117 : {
118 0 : if ( GetView() )
119 : {
120 0 : TextSelection aNewSel( GetView()->GetSelection() );
121 0 : aNewSel.GetStart() = aNewSel.GetEnd();
122 0 : GetView()->ImpSetSelection( aNewSel );
123 : }
124 :
125 0 : mpTextEngine->UpdateSelections();
126 :
127 0 : mpTextEngine->FormatAndUpdate( GetView() );
128 0 : }
129 :
130 0 : TextUndo::TextUndo( TextEngine* p )
131 : {
132 0 : mpTextEngine = p;
133 0 : }
134 :
135 0 : TextUndo::~TextUndo()
136 : {
137 0 : }
138 :
139 0 : OUString TextUndo::GetComment() const
140 : {
141 0 : return OUString();
142 : }
143 :
144 0 : void TextUndo::SetSelection( const TextSelection& rSel )
145 : {
146 0 : if ( GetView() )
147 0 : GetView()->ImpSetSelection( rSel );
148 0 : }
149 :
150 0 : TextUndoDelPara::TextUndoDelPara( TextEngine* pTextEngine, TextNode* pNode, sal_uLong nPara )
151 0 : : TextUndo( pTextEngine )
152 : {
153 0 : mpNode = pNode;
154 0 : mnPara = nPara;
155 0 : mbDelObject = true;
156 0 : }
157 :
158 0 : TextUndoDelPara::~TextUndoDelPara()
159 : {
160 0 : if ( mbDelObject )
161 0 : delete mpNode;
162 0 : }
163 :
164 0 : void TextUndoDelPara::Undo()
165 : {
166 0 : GetTextEngine()->InsertContent( mpNode, mnPara );
167 0 : mbDelObject = false; // belongs again to the engine
168 :
169 0 : if ( GetView() )
170 : {
171 0 : TextSelection aSel( TextPaM( mnPara, 0 ), TextPaM( mnPara, mpNode->GetText().Len() ) );
172 0 : SetSelection( aSel );
173 : }
174 0 : }
175 :
176 0 : void TextUndoDelPara::Redo()
177 : {
178 : // pNode is not valid anymore in case an Undo joined paragraphs
179 0 : mpNode = GetDoc()->GetNodes().GetObject( mnPara );
180 :
181 0 : delete GetTEParaPortions()->GetObject( mnPara );
182 0 : GetTEParaPortions()->Remove( mnPara );
183 :
184 : // do not delete Node because of Undo!
185 0 : GetDoc()->GetNodes().Remove( mnPara );
186 0 : GetTextEngine()->ImpParagraphRemoved( mnPara );
187 :
188 0 : mbDelObject = true; // belongs again to the Undo
189 :
190 0 : sal_uLong nParas = GetDoc()->GetNodes().Count();
191 0 : sal_uLong n = mnPara < nParas ? mnPara : (nParas-1);
192 0 : TextNode* pN = GetDoc()->GetNodes().GetObject( n );
193 0 : TextPaM aPaM( n, pN->GetText().Len() );
194 0 : SetSelection( aPaM );
195 0 : }
196 :
197 0 : OUString TextUndoDelPara::GetComment () const
198 : {
199 0 : return ResId(STR_TEXTUNDO_DELPARA, *ImplGetResMgr());
200 : }
201 :
202 0 : TextUndoConnectParas::TextUndoConnectParas( TextEngine* pTextEngine, sal_uLong nPara, sal_uInt16 nPos )
203 0 : : TextUndo( pTextEngine )
204 : {
205 0 : mnPara = nPara;
206 0 : mnSepPos = nPos;
207 0 : }
208 :
209 0 : TextUndoConnectParas::~TextUndoConnectParas()
210 : {
211 0 : }
212 :
213 0 : void TextUndoConnectParas::Undo()
214 : {
215 0 : TextPaM aPaM = GetTextEngine()->SplitContent( mnPara, mnSepPos );
216 0 : SetSelection( aPaM );
217 0 : }
218 :
219 0 : void TextUndoConnectParas::Redo()
220 : {
221 0 : TextPaM aPaM = GetTextEngine()->ConnectContents( mnPara );
222 0 : SetSelection( aPaM );
223 0 : }
224 :
225 0 : OUString TextUndoConnectParas::GetComment () const
226 : {
227 0 : return ResId(STR_TEXTUNDO_CONNECTPARAS, *ImplGetResMgr());
228 : }
229 :
230 0 : TextUndoSplitPara::TextUndoSplitPara( TextEngine* pTextEngine, sal_uLong nPara, sal_uInt16 nPos )
231 0 : : TextUndo( pTextEngine )
232 : {
233 0 : mnPara = nPara;
234 0 : mnSepPos = nPos;
235 0 : }
236 :
237 0 : TextUndoSplitPara::~TextUndoSplitPara()
238 : {
239 0 : }
240 :
241 0 : void TextUndoSplitPara::Undo()
242 : {
243 0 : TextPaM aPaM = GetTextEngine()->ConnectContents( mnPara );
244 0 : SetSelection( aPaM );
245 0 : }
246 :
247 0 : void TextUndoSplitPara::Redo()
248 : {
249 0 : TextPaM aPaM = GetTextEngine()->SplitContent( mnPara, mnSepPos );
250 0 : SetSelection( aPaM );
251 0 : }
252 :
253 0 : OUString TextUndoSplitPara::GetComment () const
254 : {
255 0 : return ResId(STR_TEXTUNDO_SPLITPARA, *ImplGetResMgr());
256 : }
257 :
258 0 : TextUndoInsertChars::TextUndoInsertChars( TextEngine* pTextEngine, const TextPaM& rTextPaM, const XubString& rStr )
259 : : TextUndo( pTextEngine ),
260 0 : maTextPaM( rTextPaM ), maText( rStr )
261 : {
262 0 : }
263 :
264 0 : void TextUndoInsertChars::Undo()
265 : {
266 0 : TextSelection aSel( maTextPaM, maTextPaM );
267 0 : aSel.GetEnd().GetIndex() = aSel.GetEnd().GetIndex() + maText.Len();
268 0 : TextPaM aPaM = GetTextEngine()->ImpDeleteText( aSel );
269 0 : SetSelection( aPaM );
270 0 : }
271 :
272 0 : void TextUndoInsertChars::Redo()
273 : {
274 0 : TextSelection aSel( maTextPaM, maTextPaM );
275 0 : GetTextEngine()->ImpInsertText( aSel, maText );
276 0 : TextPaM aNewPaM( maTextPaM );
277 0 : aNewPaM.GetIndex() = aNewPaM.GetIndex() + maText.Len();
278 0 : SetSelection( TextSelection( aSel.GetStart(), aNewPaM ) );
279 0 : }
280 :
281 0 : bool TextUndoInsertChars::Merge( SfxUndoAction* pNextAction )
282 : {
283 0 : if ( !pNextAction->ISA( TextUndoInsertChars ) )
284 0 : return false;
285 :
286 0 : TextUndoInsertChars* pNext = (TextUndoInsertChars*)pNextAction;
287 :
288 0 : if ( maTextPaM.GetPara() != pNext->maTextPaM.GetPara() )
289 0 : return false;
290 :
291 0 : if ( ( maTextPaM.GetIndex() + maText.Len() ) == pNext->maTextPaM.GetIndex() )
292 : {
293 0 : maText += pNext->maText;
294 0 : return true;
295 : }
296 0 : return false;
297 : }
298 :
299 0 : OUString TextUndoInsertChars::GetComment () const
300 : {
301 : // multiple lines?
302 0 : OUString sText(maText);
303 0 : Shorten(sText);
304 0 : return OUString(ResId(STR_TEXTUNDO_INSERTCHARS, *ImplGetResMgr())).replaceAll("$1", sText);
305 : }
306 :
307 0 : TextUndoRemoveChars::TextUndoRemoveChars( TextEngine* pTextEngine, const TextPaM& rTextPaM, const XubString& rStr )
308 : : TextUndo( pTextEngine ),
309 0 : maTextPaM( rTextPaM ), maText( rStr )
310 : {
311 0 : }
312 :
313 0 : void TextUndoRemoveChars::Undo()
314 : {
315 0 : TextSelection aSel( maTextPaM, maTextPaM );
316 0 : GetTextEngine()->ImpInsertText( aSel, maText );
317 0 : aSel.GetEnd().GetIndex() = aSel.GetEnd().GetIndex() + maText.Len();
318 0 : SetSelection( aSel );
319 0 : }
320 :
321 0 : void TextUndoRemoveChars::Redo()
322 : {
323 0 : TextSelection aSel( maTextPaM, maTextPaM );
324 0 : aSel.GetEnd().GetIndex() = aSel.GetEnd().GetIndex() + maText.Len();
325 0 : TextPaM aPaM = GetTextEngine()->ImpDeleteText( aSel );
326 0 : SetSelection( aPaM );
327 0 : }
328 :
329 0 : OUString TextUndoRemoveChars::GetComment () const
330 : {
331 : // multiple lines?
332 0 : OUString sText(maText);
333 0 : Shorten(sText);
334 0 : return OUString(ResId(STR_TEXTUNDO_REMOVECHARS, *ImplGetResMgr())).replaceAll("$1", sText);
335 465 : }
336 :
337 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|