LibreOffice Module vcl (master) 1
textundo.cxx
Go to the documentation of this file.
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 <strings.hrc>
23
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>
32
33namespace
34{
35
36// Shorten() -- inserts ellipsis (...) in the middle of a long text
37void Shorten (OUString& rString)
38{
39 auto const nLen = rString.getLength();
40 if (nLen <= 48)
41 return;
42
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);
57}
58
59} // namespace
60
62{
64}
65
67{
68}
69
71{
72 if ( GetUndoActionCount() == 0 )
73 return false;
74
76
78 bool bDone = SfxUndoManager::Undo();
79 mpTextEngine->SetIsInUndo( false );
80
82
83 return bDone;
84}
85
87{
88 if ( GetRedoActionCount() == 0 )
89 return false;
90
92
94 bool bDone = SfxUndoManager::Redo();
95 mpTextEngine->SetIsInUndo( false );
96
98
99 return bDone;
100}
101
103{
104 SAL_WARN_IF( !GetView(), "vcl", "Undo/Redo: Active View?" );
105}
106
108{
109 if ( GetView() )
110 {
111 TextSelection aNewSel( GetView()->GetSelection() );
112 aNewSel.GetStart() = aNewSel.GetEnd();
113 GetView()->ImpSetSelection( aNewSel );
114 }
115
117}
118
120{
121 mpTextEngine = p;
122}
123
125{
126}
127
128OUString TextUndo::GetComment() const
129{
130 return OUString();
131}
132
134{
135 if ( GetView() )
136 GetView()->ImpSetSelection( rSel );
137}
138
139TextUndoDelPara::TextUndoDelPara( TextEngine* pTextEngine, TextNode* pNode, sal_uInt32 nPara )
140 : TextUndo( pTextEngine )
141 , mbDelObject( true)
142 , mnPara( nPara )
143 , mpNode( pNode )
144{
145}
146
148{
149 if ( mbDelObject )
150 delete mpNode;
151}
152
154{
155 GetTextEngine()->InsertContent( std::unique_ptr<TextNode>(mpNode), mnPara );
156 mbDelObject = false; // belongs again to the engine
157
158 if ( GetView() )
159 {
160 TextSelection aSel( TextPaM( mnPara, 0 ), TextPaM( mnPara, mpNode->GetText().getLength() ) );
161 SetSelection( aSel );
162 }
163}
164
166{
167 auto & rDocNodes = GetDoc()->GetNodes();
168 // pNode is not valid anymore in case an Undo joined paragraphs
169 mpNode = rDocNodes[ mnPara ].get();
170
172
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 );
180
182
183 mbDelObject = true; // belongs again to the Undo
184
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 );
190}
191
193{
194 return VclResId(STR_TEXTUNDO_DELPARA);
195}
196
197TextUndoConnectParas::TextUndoConnectParas( TextEngine* pTextEngine, sal_uInt32 nPara, sal_Int32 nPos )
198 : TextUndo( pTextEngine )
199 , mnPara( nPara )
200 , mnSepPos( nPos )
201{
202}
203
205{
206}
207
209{
211 SetSelection( aPaM );
212}
213
215{
217 SetSelection( aPaM );
218}
219
221{
222 return VclResId(STR_TEXTUNDO_CONNECTPARAS);
223}
224
225TextUndoSplitPara::TextUndoSplitPara( TextEngine* pTextEngine, sal_uInt32 nPara, sal_Int32 nPos )
226 : TextUndo( pTextEngine )
227 , mnPara( nPara )
228 , mnSepPos ( nPos )
229{
230}
231
233{
234}
235
237{
239 SetSelection( aPaM );
240}
241
243{
245 SetSelection( aPaM );
246}
247
249{
250 return VclResId(STR_TEXTUNDO_SPLITPARA);
251}
252
253TextUndoInsertChars::TextUndoInsertChars( TextEngine* pTextEngine, const TextPaM& rTextPaM, OUString aStr )
254 : TextUndo( pTextEngine ),
255 maTextPaM( rTextPaM ), maText(std::move( aStr ))
256{
257}
258
260{
262 aSel.GetEnd().GetIndex() += maText.getLength();
263 TextPaM aPaM = GetTextEngine()->ImpDeleteText( aSel );
264 SetSelection( aPaM );
265}
266
268{
271 TextPaM aNewPaM( maTextPaM );
272 aNewPaM.GetIndex() += maText.getLength();
273 SetSelection( TextSelection( aSel.GetStart(), aNewPaM ) );
274}
275
277{
278 TextUndoInsertChars* pNext = dynamic_cast<TextUndoInsertChars*>(pNextAction);
279 if ( !pNext )
280 return false;
281
282 if ( maTextPaM.GetPara() != pNext->maTextPaM.GetPara() )
283 return false;
284
285 if ( ( maTextPaM.GetIndex() + maText.getLength() ) == pNext->maTextPaM.GetIndex() )
286 {
287 maText += pNext->maText;
288 return true;
289 }
290 return false;
291}
292
294{
295 // multiple lines?
296 OUString sText(maText);
297 Shorten(sText);
298 return VclResId(STR_TEXTUNDO_INSERTCHARS).replaceAll("$1", sText);
299}
300
301TextUndoRemoveChars::TextUndoRemoveChars( TextEngine* pTextEngine, const TextPaM& rTextPaM, OUString aStr )
302 : TextUndo( pTextEngine ),
303 maTextPaM( rTextPaM ), maText(std::move( aStr ))
304{
305}
306
308{
311 aSel.GetEnd().GetIndex() += maText.getLength();
312 SetSelection( aSel );
313}
314
316{
318 aSel.GetEnd().GetIndex() += maText.getLength();
319 TextPaM aPaM = GetTextEngine()->ImpDeleteText( aSel );
320 SetSelection( aPaM );
321}
322
324{
325 // multiple lines?
326 OUString sText(maText);
327 Shorten(sText);
328 return VclResId(STR_TEXTUNDO_REMOVECHARS).replaceAll("$1", sText);
329}
330
331/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Text maText
virtual bool Undo()
virtual bool Redo()
virtual size_t GetRedoActionCount(bool const i_currentLevel=CurrentLevel) const
virtual size_t GetUndoActionCount(bool const i_currentLevel=CurrentLevel) const
void Remove(sal_uInt32 nPos)
Definition: textdat2.hxx:224
std::vector< std::unique_ptr< TextNode > > & GetNodes()
Definition: textdoc.hxx:106
TextPaM SplitContent(sal_uInt32 nNode, sal_Int32 nSepPos)
Definition: texteng.cxx:1324
void SetIsInUndo(bool bInUndo)
Definition: texteng.hxx:262
void ImpParagraphRemoved(sal_uInt32 nPara)
Definition: texteng.cxx:2562
TextPaM ConnectContents(sal_uInt32 nLeftNode)
Definition: texteng.cxx:1336
void InsertContent(std::unique_ptr< TextNode > pNode, sal_uInt32 nPara)
Definition: texteng.cxx:1314
TextPaM ImpInsertText(const TextSelection &rSel, sal_Unicode c, bool bOverwrite=false)
Definition: texteng.cxx:632
void FormatAndUpdate(TextView *pCurView=nullptr)
Definition: texteng.cxx:1409
TextPaM ImpDeleteText(const TextSelection &rSel)
Definition: texteng.cxx:521
const OUString & GetText() const
Definition: textdoc.hxx:79
sal_uInt32 GetPara() const
Definition: textdata.hxx:45
sal_Int32 GetIndex() const
Definition: textdata.hxx:48
const TextPaM & GetStart() const
Definition: textdata.hxx:90
const TextPaM & GetEnd() const
Definition: textdata.hxx:93
virtual void Undo() override
Definition: textundo.cxx:208
TextUndoConnectParas(TextEngine *pTextEngine, sal_uInt32 nPara, sal_Int32 nSepPos)
Definition: textundo.cxx:197
sal_uInt32 mnPara
Definition: textund2.hxx:44
virtual ~TextUndoConnectParas() override
Definition: textundo.cxx:204
virtual OUString GetComment() const override
Definition: textundo.cxx:220
sal_Int32 mnSepPos
Definition: textund2.hxx:45
virtual void Redo() override
Definition: textundo.cxx:214
TextNode * mpNode
Definition: textund2.hxx:29
sal_uInt32 mnPara
Definition: textund2.hxx:28
virtual void Undo() override
Definition: textundo.cxx:153
virtual OUString GetComment() const override
Definition: textundo.cxx:192
virtual void Redo() override
Definition: textundo.cxx:165
virtual ~TextUndoDelPara() override
Definition: textundo.cxx:147
TextUndoDelPara(TextEngine *pTextEngine, TextNode *pNode, sal_uInt32 nPara)
Definition: textundo.cxx:139
TextUndoInsertChars(TextEngine *pTextEngine, const TextPaM &rTextPaM, OUString aStr)
Definition: textundo.cxx:253
virtual OUString GetComment() const override
Definition: textundo.cxx:293
virtual void Undo() override
Definition: textundo.cxx:259
virtual bool Merge(SfxUndoAction *pNextAction) override
Definition: textundo.cxx:276
virtual void Redo() override
Definition: textundo.cxx:267
virtual bool Undo() override
Definition: textundo.cxx:70
TextView * GetView() const
Definition: textundo.hxx:39
TextUndoManager(TextEngine *pTextEngine)
Definition: textundo.cxx:61
virtual bool Redo() override
Definition: textundo.cxx:86
void UndoRedoStart()
Definition: textundo.cxx:102
TextEngine * mpTextEngine
Definition: textundo.hxx:32
void UndoRedoEnd()
Definition: textundo.cxx:107
virtual ~TextUndoManager() override
Definition: textundo.cxx:66
virtual OUString GetComment() const override
Definition: textundo.cxx:323
virtual void Undo() override
Definition: textundo.cxx:307
TextUndoRemoveChars(TextEngine *pTextEngine, const TextPaM &rTextPaM, OUString aStr)
Definition: textundo.cxx:301
virtual void Redo() override
Definition: textundo.cxx:315
virtual void Undo() override
Definition: textundo.cxx:236
sal_uInt32 mnPara
Definition: textund2.hxx:60
virtual OUString GetComment() const override
Definition: textundo.cxx:248
virtual void Redo() override
Definition: textundo.cxx:242
sal_Int32 mnSepPos
Definition: textund2.hxx:61
virtual ~TextUndoSplitPara() override
Definition: textundo.cxx:232
TextUndoSplitPara(TextEngine *pTextEngine, sal_uInt32 nPara, sal_Int32 nSepPos)
Definition: textundo.cxx:225
TextEngine * mpTextEngine
Definition: textundo.hxx:55
virtual ~TextUndo() override
Definition: textundo.cxx:124
TextView * GetView() const
Definition: textundo.hxx:59
TextEngine * GetTextEngine() const
Definition: textundo.hxx:69
TextDoc * GetDoc() const
Definition: textundo.hxx:62
TEParaPortions * GetTEParaPortions() const
Definition: textundo.hxx:63
virtual OUString GetComment() const override
Definition: textundo.cxx:128
void SetSelection(const TextSelection &rSel)
Definition: textundo.cxx:133
TextUndo(TextEngine *pTextEngine)
Definition: textundo.cxx:119
void ImpSetSelection(const TextSelection &rSelection)
Definition: textview.cxx:297
void * p
sal_Int64 n
AlgAtomPtr mpNode
sal_uInt16 nPos
#define SAL_WARN_IF(condition, area, stream)
aStr
OUString VclResId(TranslateId aId)
Definition: svdata.cxx:261
void GetSelection(struct ESelection &rSel, SvxTextForwarder const *pForwarder) noexcept