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>
26 #include <vcl/texteng.hxx>
27 #include <vcl/textview.hxx>
28 #include <vcl/textdata.hxx>
29 #include "textdoc.hxx"
30 #include "textdat2.hxx"
36 // Shorten() -- inserts ellipsis (...) in the middle of a long text
37 void Shorten (OUString
& rString
)
39 auto const nLen
= rString
.getLength();
43 // If possible, we don't break a word, hence first we look for a space.
44 // Space before the ellipsis:
45 auto iFirst
= rString
.lastIndexOf(' ', 32);
46 if (iFirst
== -1 || iFirst
< 16)
47 iFirst
= 24; // not possible
48 // Space after the ellipsis:
49 auto iLast
= rString
.indexOf(' ', nLen
- 16);
50 if (iLast
== -1 || iLast
> nLen
- 4)
51 iLast
= nLen
- 8; // not possible
54 OUString::Concat(rString
.subView(0, iFirst
+ 1)) +
56 rString
.subView(iLast
);
61 TextUndoManager::TextUndoManager( TextEngine
* p
)
66 TextUndoManager::~TextUndoManager()
70 bool TextUndoManager::Undo()
72 if ( GetUndoActionCount() == 0 )
77 mpTextEngine
->SetIsInUndo( true );
78 bool bDone
= SfxUndoManager::Undo();
79 mpTextEngine
->SetIsInUndo( false );
86 bool TextUndoManager::Redo()
88 if ( GetRedoActionCount() == 0 )
93 mpTextEngine
->SetIsInUndo( true );
94 bool bDone
= SfxUndoManager::Redo();
95 mpTextEngine
->SetIsInUndo( false );
102 void TextUndoManager::UndoRedoStart()
104 SAL_WARN_IF( !GetView(), "vcl", "Undo/Redo: Active View?" );
107 void TextUndoManager::UndoRedoEnd()
111 TextSelection
aNewSel( GetView()->GetSelection() );
112 aNewSel
.GetStart() = aNewSel
.GetEnd();
113 GetView()->ImpSetSelection( aNewSel
);
116 mpTextEngine
->FormatAndUpdate( GetView() );
119 TextUndo::TextUndo( TextEngine
* p
)
124 TextUndo::~TextUndo()
128 OUString
TextUndo::GetComment() const
133 void TextUndo::SetSelection( const TextSelection
& rSel
)
136 GetView()->ImpSetSelection( rSel
);
139 TextUndoDelPara::TextUndoDelPara( TextEngine
* pTextEngine
, TextNode
* pNode
, sal_uInt32 nPara
)
140 : TextUndo( pTextEngine
)
147 TextUndoDelPara::~TextUndoDelPara()
153 void TextUndoDelPara::Undo()
155 GetTextEngine()->InsertContent( std::unique_ptr
<TextNode
>(mpNode
), mnPara
);
156 mbDelObject
= false; // belongs again to the engine
160 TextSelection
aSel( TextPaM( mnPara
, 0 ), TextPaM( mnPara
, mpNode
->GetText().getLength() ) );
161 SetSelection( aSel
);
165 void TextUndoDelPara::Redo()
167 auto & rDocNodes
= GetDoc()->GetNodes();
168 // pNode is not valid anymore in case an Undo joined paragraphs
169 mpNode
= rDocNodes
[ mnPara
].get();
171 GetTEParaPortions()->Remove( mnPara
);
173 // do not delete Node because of Undo!
174 auto it
= ::std::find_if( rDocNodes
.begin(), rDocNodes
.end(),
175 [&] (std::unique_ptr
<TextNode
> const & p
) { return p
.get() == mpNode
; } );
176 assert(it
!= rDocNodes
.end());
177 // coverity[leaked_storage : FALSE] - ownership transferred to this with mbDelObject
179 GetDoc()->GetNodes().erase( it
);
181 GetTextEngine()->ImpParagraphRemoved( mnPara
);
183 mbDelObject
= true; // belongs again to the Undo
185 const sal_uInt32 nParas
= static_cast<sal_uInt32
>(GetDoc()->GetNodes().size());
186 const sal_uInt32 n
= mnPara
< nParas
? mnPara
: nParas
-1;
187 TextNode
* pN
= GetDoc()->GetNodes()[ n
].get();
188 TextPaM
aPaM( n
, pN
->GetText().getLength() );
189 SetSelection( aPaM
);
192 OUString
TextUndoDelPara::GetComment () const
194 return VclResId(STR_TEXTUNDO_DELPARA
);
197 TextUndoConnectParas::TextUndoConnectParas( TextEngine
* pTextEngine
, sal_uInt32 nPara
, sal_Int32 nPos
)
198 : TextUndo( pTextEngine
)
204 TextUndoConnectParas::~TextUndoConnectParas()
208 void TextUndoConnectParas::Undo()
210 TextPaM aPaM
= GetTextEngine()->SplitContent( mnPara
, mnSepPos
);
211 SetSelection( aPaM
);
214 void TextUndoConnectParas::Redo()
216 TextPaM aPaM
= GetTextEngine()->ConnectContents( mnPara
);
217 SetSelection( aPaM
);
220 OUString
TextUndoConnectParas::GetComment () const
222 return VclResId(STR_TEXTUNDO_CONNECTPARAS
);
225 TextUndoSplitPara::TextUndoSplitPara( TextEngine
* pTextEngine
, sal_uInt32 nPara
, sal_Int32 nPos
)
226 : TextUndo( pTextEngine
)
232 TextUndoSplitPara::~TextUndoSplitPara()
236 void TextUndoSplitPara::Undo()
238 TextPaM aPaM
= GetTextEngine()->ConnectContents( mnPara
);
239 SetSelection( aPaM
);
242 void TextUndoSplitPara::Redo()
244 TextPaM aPaM
= GetTextEngine()->SplitContent( mnPara
, mnSepPos
);
245 SetSelection( aPaM
);
248 OUString
TextUndoSplitPara::GetComment () const
250 return VclResId(STR_TEXTUNDO_SPLITPARA
);
253 TextUndoInsertChars::TextUndoInsertChars( TextEngine
* pTextEngine
, const TextPaM
& rTextPaM
, OUString aStr
)
254 : TextUndo( pTextEngine
),
255 maTextPaM( rTextPaM
), maText(std::move( aStr
))
259 void TextUndoInsertChars::Undo()
261 TextSelection
aSel( maTextPaM
, maTextPaM
);
262 aSel
.GetEnd().GetIndex() += maText
.getLength();
263 TextPaM aPaM
= GetTextEngine()->ImpDeleteText( aSel
);
264 SetSelection( aPaM
);
267 void TextUndoInsertChars::Redo()
269 TextSelection
aSel( maTextPaM
, maTextPaM
);
270 GetTextEngine()->ImpInsertText( aSel
, maText
);
271 TextPaM
aNewPaM( maTextPaM
);
272 aNewPaM
.GetIndex() += maText
.getLength();
273 SetSelection( TextSelection( aSel
.GetStart(), aNewPaM
) );
276 bool TextUndoInsertChars::Merge( SfxUndoAction
* pNextAction
)
278 TextUndoInsertChars
* pNext
= dynamic_cast<TextUndoInsertChars
*>(pNextAction
);
282 if ( maTextPaM
.GetPara() != pNext
->maTextPaM
.GetPara() )
285 if ( ( maTextPaM
.GetIndex() + maText
.getLength() ) == pNext
->maTextPaM
.GetIndex() )
287 maText
+= pNext
->maText
;
293 OUString
TextUndoInsertChars::GetComment () const
296 OUString
sText(maText
);
298 return VclResId(STR_TEXTUNDO_INSERTCHARS
).replaceAll("$1", sText
);
301 TextUndoRemoveChars::TextUndoRemoveChars( TextEngine
* pTextEngine
, const TextPaM
& rTextPaM
, OUString aStr
)
302 : TextUndo( pTextEngine
),
303 maTextPaM( rTextPaM
), maText(std::move( aStr
))
307 void TextUndoRemoveChars::Undo()
309 TextSelection
aSel( maTextPaM
, maTextPaM
);
310 GetTextEngine()->ImpInsertText( aSel
, maText
);
311 aSel
.GetEnd().GetIndex() += maText
.getLength();
312 SetSelection( aSel
);
315 void TextUndoRemoveChars::Redo()
317 TextSelection
aSel( maTextPaM
, maTextPaM
);
318 aSel
.GetEnd().GetIndex() += maText
.getLength();
319 TextPaM aPaM
= GetTextEngine()->ImpDeleteText( aSel
);
320 SetSelection( aPaM
);
323 OUString
TextUndoRemoveChars::GetComment () const
326 OUString
sText(maText
);
328 return VclResId(STR_TEXTUNDO_REMOVECHARS
).replaceAll("$1", sText
);
331 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */