Fix GNU C++ version check
[LibreOffice.git] / vcl / source / edit / textundo.cxx
blobe53e60e5d5af4ed8696e1ff6a7deadcbe78544a1
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <utility>
26 #include <vcl/texteng.hxx>
27 #include <vcl/textview.hxx>
28 #include <vcl/textdata.hxx>
29 #include "textdoc.hxx"
30 #include "textdat2.hxx"
31 #include <svdata.hxx>
33 namespace
36 // Shorten() -- inserts ellipsis (...) in the middle of a long text
37 void Shorten (OUString& rString)
39 auto const nLen = rString.getLength();
40 if (nLen <= 48)
41 return;
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
52 // finally:
53 rString =
54 OUString::Concat(rString.subView(0, iFirst + 1)) +
55 "..." +
56 rString.subView(iLast);
59 } // namespace
61 TextUndoManager::TextUndoManager( TextEngine* p )
63 mpTextEngine = p;
66 TextUndoManager::~TextUndoManager()
70 bool TextUndoManager::Undo()
72 if ( GetUndoActionCount() == 0 )
73 return false;
75 UndoRedoStart();
77 mpTextEngine->SetIsInUndo( true );
78 bool bDone = SfxUndoManager::Undo();
79 mpTextEngine->SetIsInUndo( false );
81 UndoRedoEnd();
83 return bDone;
86 bool TextUndoManager::Redo()
88 if ( GetRedoActionCount() == 0 )
89 return false;
91 UndoRedoStart();
93 mpTextEngine->SetIsInUndo( true );
94 bool bDone = SfxUndoManager::Redo();
95 mpTextEngine->SetIsInUndo( false );
97 UndoRedoEnd();
99 return bDone;
102 void TextUndoManager::UndoRedoStart()
104 SAL_WARN_IF( !GetView(), "vcl", "Undo/Redo: Active View?" );
107 void TextUndoManager::UndoRedoEnd()
109 if ( GetView() )
111 TextSelection aNewSel( GetView()->GetSelection() );
112 aNewSel.GetStart() = aNewSel.GetEnd();
113 GetView()->ImpSetSelection( aNewSel );
116 mpTextEngine->FormatAndUpdate( GetView() );
119 TextUndo::TextUndo( TextEngine* p )
121 mpTextEngine = p;
124 TextUndo::~TextUndo()
128 OUString TextUndo::GetComment() const
130 return OUString();
133 void TextUndo::SetSelection( const TextSelection& rSel )
135 if ( GetView() )
136 GetView()->ImpSetSelection( rSel );
139 TextUndoDelPara::TextUndoDelPara( TextEngine* pTextEngine, TextNode* pNode, sal_uInt32 nPara )
140 : TextUndo( pTextEngine )
141 , mbDelObject( true)
142 , mnPara( nPara )
143 , mpNode( pNode )
147 TextUndoDelPara::~TextUndoDelPara()
149 if ( mbDelObject )
150 delete mpNode;
153 void TextUndoDelPara::Undo()
155 GetTextEngine()->InsertContent( std::unique_ptr<TextNode>(mpNode), mnPara );
156 mbDelObject = false; // belongs again to the engine
158 if ( GetView() )
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
178 it->release();
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 )
199 , mnPara( nPara )
200 , mnSepPos( nPos )
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 )
227 , mnPara( nPara )
228 , mnSepPos ( nPos )
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);
279 if ( !pNext )
280 return false;
282 if ( maTextPaM.GetPara() != pNext->maTextPaM.GetPara() )
283 return false;
285 if ( ( maTextPaM.GetIndex() + maText.getLength() ) == pNext->maTextPaM.GetIndex() )
287 maText += pNext->maText;
288 return true;
290 return false;
293 OUString TextUndoInsertChars::GetComment () const
295 // multiple lines?
296 OUString sText(maText);
297 Shorten(sText);
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
325 // multiple lines?
326 OUString sText(maText);
327 Shorten(sText);
328 return VclResId(STR_TEXTUNDO_REMOVECHARS).replaceAll("$1", sText);
331 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */