1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "textundo.hxx"
21 #include "textund2.hxx"
22 #include <strings.hrc>
24 #include <sal/log.hxx>
25 #include <vcl/texteng.hxx>
26 #include <vcl/textview.hxx>
27 #include <vcl/textdata.hxx>
28 #include "textdoc.hxx"
29 #include "textdat2.hxx"
35 // Shorten() -- inserts ellipsis (...) in the middle of a long text
36 void Shorten (OUString
& rString
)
38 unsigned nLen
= rString
.getLength();
41 // If possible, we don't break a word, hence first we look for a space.
42 // Space before the ellipsis:
43 int iFirst
= rString
.lastIndexOf(' ', 32);
44 if (iFirst
== -1 || unsigned(iFirst
) < 16)
45 iFirst
= 24; // not possible
46 // Space after the ellipsis:
47 int iLast
= rString
.indexOf(' ', nLen
- 16);
48 if (iLast
== -1 || unsigned(iLast
) > nLen
- 4)
49 iLast
= nLen
- 8; // not possible
52 rString
.copy(0, iFirst
+ 1) +
60 TextUndoManager::TextUndoManager( TextEngine
* p
)
65 TextUndoManager::~TextUndoManager()
69 bool TextUndoManager::Undo()
71 if ( GetUndoActionCount() == 0 )
76 mpTextEngine
->SetIsInUndo( true );
77 bool bDone
= SfxUndoManager::Undo();
78 mpTextEngine
->SetIsInUndo( false );
85 bool TextUndoManager::Redo()
87 if ( GetRedoActionCount() == 0 )
92 mpTextEngine
->SetIsInUndo( true );
93 bool bDone
= SfxUndoManager::Redo();
94 mpTextEngine
->SetIsInUndo( false );
101 void TextUndoManager::UndoRedoStart()
103 SAL_WARN_IF( !GetView(), "vcl", "Undo/Redo: Active View?" );
106 void TextUndoManager::UndoRedoEnd()
110 TextSelection
aNewSel( GetView()->GetSelection() );
111 aNewSel
.GetStart() = aNewSel
.GetEnd();
112 GetView()->ImpSetSelection( aNewSel
);
115 mpTextEngine
->FormatAndUpdate( GetView() );
118 TextUndo::TextUndo( TextEngine
* p
)
123 TextUndo::~TextUndo()
127 OUString
TextUndo::GetComment() const
132 void TextUndo::SetSelection( const TextSelection
& rSel
)
135 GetView()->ImpSetSelection( rSel
);
138 TextUndoDelPara::TextUndoDelPara( TextEngine
* pTextEngine
, TextNode
* pNode
, sal_uInt32 nPara
)
139 : TextUndo( pTextEngine
)
146 TextUndoDelPara::~TextUndoDelPara()
152 void TextUndoDelPara::Undo()
154 GetTextEngine()->InsertContent( std::unique_ptr
<TextNode
>(mpNode
), mnPara
);
155 mbDelObject
= false; // belongs again to the engine
159 TextSelection
aSel( TextPaM( mnPara
, 0 ), TextPaM( mnPara
, mpNode
->GetText().getLength() ) );
160 SetSelection( aSel
);
164 void TextUndoDelPara::Redo()
166 auto & rDocNodes
= GetDoc()->GetNodes();
167 // pNode is not valid anymore in case an Undo joined paragraphs
168 mpNode
= rDocNodes
[ mnPara
].get();
170 GetTEParaPortions()->Remove( mnPara
);
172 // do not delete Node because of Undo!
173 auto it
= ::std::find_if( rDocNodes
.begin(), rDocNodes
.end(),
174 [&] (std::unique_ptr
<TextNode
> const & p
) { return p
.get() == mpNode
; } );
175 assert(it
!= rDocNodes
.end());
177 GetDoc()->GetNodes().erase( it
);
179 GetTextEngine()->ImpParagraphRemoved( mnPara
);
181 mbDelObject
= true; // belongs again to the Undo
183 const sal_uInt32 nParas
= static_cast<sal_uInt32
>(GetDoc()->GetNodes().size());
184 const sal_uInt32 n
= mnPara
< nParas
? mnPara
: nParas
-1;
185 TextNode
* pN
= GetDoc()->GetNodes()[ n
].get();
186 TextPaM
aPaM( n
, pN
->GetText().getLength() );
187 SetSelection( aPaM
);
190 OUString
TextUndoDelPara::GetComment () const
192 return VclResId(STR_TEXTUNDO_DELPARA
);
195 TextUndoConnectParas::TextUndoConnectParas( TextEngine
* pTextEngine
, sal_uInt32 nPara
, sal_Int32 nPos
)
196 : TextUndo( pTextEngine
)
202 TextUndoConnectParas::~TextUndoConnectParas()
206 void TextUndoConnectParas::Undo()
208 TextPaM aPaM
= GetTextEngine()->SplitContent( mnPara
, mnSepPos
);
209 SetSelection( aPaM
);
212 void TextUndoConnectParas::Redo()
214 TextPaM aPaM
= GetTextEngine()->ConnectContents( mnPara
);
215 SetSelection( aPaM
);
218 OUString
TextUndoConnectParas::GetComment () const
220 return VclResId(STR_TEXTUNDO_CONNECTPARAS
);
223 TextUndoSplitPara::TextUndoSplitPara( TextEngine
* pTextEngine
, sal_uInt32 nPara
, sal_Int32 nPos
)
224 : TextUndo( pTextEngine
)
230 TextUndoSplitPara::~TextUndoSplitPara()
234 void TextUndoSplitPara::Undo()
236 TextPaM aPaM
= GetTextEngine()->ConnectContents( mnPara
);
237 SetSelection( aPaM
);
240 void TextUndoSplitPara::Redo()
242 TextPaM aPaM
= GetTextEngine()->SplitContent( mnPara
, mnSepPos
);
243 SetSelection( aPaM
);
246 OUString
TextUndoSplitPara::GetComment () const
248 return VclResId(STR_TEXTUNDO_SPLITPARA
);
251 TextUndoInsertChars::TextUndoInsertChars( TextEngine
* pTextEngine
, const TextPaM
& rTextPaM
, const OUString
& rStr
)
252 : TextUndo( pTextEngine
),
253 maTextPaM( rTextPaM
), maText( rStr
)
257 void TextUndoInsertChars::Undo()
259 TextSelection
aSel( maTextPaM
, maTextPaM
);
260 aSel
.GetEnd().GetIndex() += maText
.getLength();
261 TextPaM aPaM
= GetTextEngine()->ImpDeleteText( aSel
);
262 SetSelection( aPaM
);
265 void TextUndoInsertChars::Redo()
267 TextSelection
aSel( maTextPaM
, maTextPaM
);
268 GetTextEngine()->ImpInsertText( aSel
, maText
);
269 TextPaM
aNewPaM( maTextPaM
);
270 aNewPaM
.GetIndex() += maText
.getLength();
271 SetSelection( TextSelection( aSel
.GetStart(), aNewPaM
) );
274 bool TextUndoInsertChars::Merge( SfxUndoAction
* pNextAction
)
276 if ( nullptr == dynamic_cast< const TextUndoInsertChars
*>( pNextAction
) )
279 TextUndoInsertChars
* pNext
= static_cast<TextUndoInsertChars
*>(pNextAction
);
281 if ( maTextPaM
.GetPara() != pNext
->maTextPaM
.GetPara() )
284 if ( ( maTextPaM
.GetIndex() + maText
.getLength() ) == pNext
->maTextPaM
.GetIndex() )
286 maText
+= pNext
->maText
;
292 OUString
TextUndoInsertChars::GetComment () const
295 OUString
sText(maText
);
297 return VclResId(STR_TEXTUNDO_INSERTCHARS
).replaceAll("$1", sText
);
300 TextUndoRemoveChars::TextUndoRemoveChars( TextEngine
* pTextEngine
, const TextPaM
& rTextPaM
, const OUString
& rStr
)
301 : TextUndo( pTextEngine
),
302 maTextPaM( rTextPaM
), maText( rStr
)
306 void TextUndoRemoveChars::Undo()
308 TextSelection
aSel( maTextPaM
, maTextPaM
);
309 GetTextEngine()->ImpInsertText( aSel
, maText
);
310 aSel
.GetEnd().GetIndex() += maText
.getLength();
311 SetSelection( aSel
);
314 void TextUndoRemoveChars::Redo()
316 TextSelection
aSel( maTextPaM
, maTextPaM
);
317 aSel
.GetEnd().GetIndex() += maText
.getLength();
318 TextPaM aPaM
= GetTextEngine()->ImpDeleteText( aSel
);
319 SetSelection( aPaM
);
322 OUString
TextUndoRemoveChars::GetComment () const
325 OUString
sText(maText
);
327 return VclResId(STR_TEXTUNDO_REMOVECHARS
).replaceAll("$1", sText
);
330 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */