summaryrefslogtreecommitdiffstats
path: root/vcl/source/edit/textundo.cxx
blob: e53e60e5d5af4ed8696e1ff6a7deadcbe78544a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include "textundo.hxx"
#include "textund2.hxx"
#include <strings.hrc>

#include <sal/log.hxx>
#include <utility>
#include <vcl/texteng.hxx>
#include <vcl/textview.hxx>
#include <vcl/textdata.hxx>
#include "textdoc.hxx"
#include "textdat2.hxx"
#include <svdata.hxx>

namespace
{

// Shorten() -- inserts ellipsis (...) in the middle of a long text
void Shorten (OUString& rString)
{
    auto const nLen = rString.getLength();
    if (nLen <= 48)
        return;

    // If possible, we don't break a word, hence first we look for a space.
    // Space before the ellipsis:
    auto iFirst = rString.lastIndexOf(' ', 32);
    if (iFirst == -1 || iFirst < 16)
        iFirst = 24; // not possible
    // Space after the ellipsis:
    auto iLast = rString.indexOf(' ', nLen - 16);
    if (iLast == -1 || iLast > nLen - 4)
        iLast = nLen - 8; // not possible
    // finally:
    rString =
        OUString::Concat(rString.subView(0, iFirst + 1)) +
        "..." +
        rString.subView(iLast);
}

} // namespace

TextUndoManager::TextUndoManager( TextEngine* p )
{
    mpTextEngine = p;
}

TextUndoManager::~TextUndoManager()
{
}

bool TextUndoManager::Undo()
{
    if ( GetUndoActionCount() == 0 )
        return false;

    UndoRedoStart();

    mpTextEngine->SetIsInUndo( true );
    bool bDone = SfxUndoManager::Undo();
    mpTextEngine->SetIsInUndo( false );

    UndoRedoEnd();

    return bDone;
}

bool TextUndoManager::Redo()
{
    if ( GetRedoActionCount() == 0 )
        return false;

    UndoRedoStart();

    mpTextEngine->SetIsInUndo( true );
    bool bDone = SfxUndoManager::Redo();
    mpTextEngine->SetIsInUndo( false );

    UndoRedoEnd();

    return bDone;
}

void TextUndoManager::UndoRedoStart()
{
    SAL_WARN_IF( !GetView(), "vcl", "Undo/Redo: Active View?" );
}

void TextUndoManager::UndoRedoEnd()
{
    if ( GetView() )
    {
        TextSelection aNewSel( GetView()->GetSelection() );
        aNewSel.GetStart() = aNewSel.GetEnd();
        GetView()->ImpSetSelection( aNewSel );
    }

    mpTextEngine->FormatAndUpdate( GetView() );
}

TextUndo::TextUndo( TextEngine* p )
{
    mpTextEngine = p;
}

TextUndo::~TextUndo()
{
}

OUString TextUndo::GetComment() const
{
    return OUString();
}

void TextUndo::SetSelection( const TextSelection& rSel )
{
    if ( GetView() )
        GetView()->ImpSetSelection( rSel );
}

TextUndoDelPara::TextUndoDelPara( TextEngine* pTextEngine, TextNode* pNode, sal_uInt32 nPara )
    : TextUndo( pTextEngine )
    , mbDelObject( true)
    , mnPara( nPara )
    , mpNode( pNode )
{
}

TextUndoDelPara::~TextUndoDelPara()
{
    if ( mbDelObject )
        delete mpNode;
}

void TextUndoDelPara::Undo()
{
    GetTextEngine()->InsertContent( std::unique_ptr<TextNode>(mpNode), mnPara );
    mbDelObject = false;    // belongs again to the engine

    if ( GetView() )
    {
        TextSelection aSel( TextPaM( mnPara, 0 ), TextPaM( mnPara, mpNode->GetText().getLength() ) );
        SetSelection( aSel );
    }
}

void TextUndoDelPara::Redo()
{
    auto & rDocNodes = GetDoc()->GetNodes();
    // pNode is not valid anymore in case an Undo joined paragraphs
    mpNode = rDocNodes[ mnPara ].get();

    GetTEParaPortions()->Remove( mnPara );

    // do not delete Node because of Undo!
    auto it = ::std::find_if( rDocNodes.begin(), rDocNodes.end(),
                              [&] (std::unique_ptr<TextNode> const & p) { return p.get() == mpNode; } );
    assert(it != rDocNodes.end());
    // coverity[leaked_storage : FALSE] - ownership transferred to this with mbDelObject
    it->release();
    GetDoc()->GetNodes().erase( it );

    GetTextEngine()->ImpParagraphRemoved( mnPara );

    mbDelObject = true; // belongs again to the Undo

    const sal_uInt32 nParas = static_cast<sal_uInt32>(GetDoc()->GetNodes().size());
    const sal_uInt32 n = mnPara < nParas ? mnPara : nParas-1;
    TextNode* pN = GetDoc()->GetNodes()[ n ].get();
    TextPaM aPaM( n, pN->GetText().getLength() );
    SetSelection( aPaM );
}

OUString TextUndoDelPara::GetComment () const
{
    return VclResId(STR_TEXTUNDO_DELPARA);
}

TextUndoConnectParas::TextUndoConnectParas( TextEngine* pTextEngine, sal_uInt32 nPara, sal_Int32 nPos )
    : TextUndo( pTextEngine )
    , mnPara( nPara )
    , mnSepPos( nPos )
{
}

TextUndoConnectParas::~TextUndoConnectParas()
{
}

void TextUndoConnectParas::Undo()
{
    TextPaM aPaM = GetTextEngine()->SplitContent( mnPara, mnSepPos );
    SetSelection( aPaM );
}

void TextUndoConnectParas::Redo()
{
    TextPaM aPaM = GetTextEngine()->ConnectContents( mnPara );
    SetSelection( aPaM );
}

OUString TextUndoConnectParas::GetComment () const
{
    return VclResId(STR_TEXTUNDO_CONNECTPARAS);
}

TextUndoSplitPara::TextUndoSplitPara( TextEngine* pTextEngine, sal_uInt32 nPara, sal_Int32 nPos )
    : TextUndo( pTextEngine )
    , mnPara( nPara )
    , mnSepPos ( nPos )
{
}

TextUndoSplitPara::~TextUndoSplitPara()
{
}

void TextUndoSplitPara::Undo()
{
    TextPaM aPaM = GetTextEngine()->ConnectContents( mnPara );
    SetSelection( aPaM );
}

void TextUndoSplitPara::Redo()
{
    TextPaM aPaM = GetTextEngine()->SplitContent( mnPara, mnSepPos );
    SetSelection( aPaM );
}

OUString TextUndoSplitPara::GetComment () const
{
    return VclResId(STR_TEXTUNDO_SPLITPARA);
}

TextUndoInsertChars::TextUndoInsertChars( TextEngine* pTextEngine, const TextPaM& rTextPaM, OUString aStr )
                    : TextUndo( pTextEngine ),
                        maTextPaM( rTextPaM ), maText(std::move( aStr ))
{
}

void TextUndoInsertChars::Undo()
{
    TextSelection aSel( maTextPaM, maTextPaM );
    aSel.GetEnd().GetIndex() += maText.getLength();
    TextPaM aPaM = GetTextEngine()->ImpDeleteText( aSel );
    SetSelection( aPaM );
}

void TextUndoInsertChars::Redo()
{
    TextSelection aSel( maTextPaM, maTextPaM );
    GetTextEngine()->ImpInsertText( aSel, maText );
    TextPaM aNewPaM( maTextPaM );
    aNewPaM.GetIndex() += maText.getLength();
    SetSelection( TextSelection( aSel.GetStart(), aNewPaM ) );
}

bool TextUndoInsertChars::Merge( SfxUndoAction* pNextAction )
{
    TextUndoInsertChars* pNext = dynamic_cast<TextUndoInsertChars*>(pNextAction);
    if ( !pNext )
        return false;

    if ( maTextPaM.GetPara() != pNext->maTextPaM.GetPara() )
        return false;

    if ( ( maTextPaM.GetIndex() + maText.getLength() ) == pNext->maTextPaM.GetIndex() )
    {
        maText += pNext->maText;
        return true;
    }
    return false;
}

OUString TextUndoInsertChars::GetComment () const
{
    // multiple lines?
    OUString sText(maText);
    Shorten(sText);
    return VclResId(STR_TEXTUNDO_INSERTCHARS).replaceAll("$1", sText);
}

TextUndoRemoveChars::TextUndoRemoveChars( TextEngine* pTextEngine, const TextPaM& rTextPaM, OUString aStr )
                    : TextUndo( pTextEngine ),
                        maTextPaM( rTextPaM ), maText(std::move( aStr ))
{
}

void TextUndoRemoveChars::Undo()
{
    TextSelection aSel( maTextPaM, maTextPaM );
    GetTextEngine()->ImpInsertText( aSel, maText );
    aSel.GetEnd().GetIndex() += maText.getLength();
    SetSelection( aSel );
}

void TextUndoRemoveChars::Redo()
{
    TextSelection aSel( maTextPaM, maTextPaM );
    aSel.GetEnd().GetIndex() += maText.getLength();
    TextPaM aPaM = GetTextEngine()->ImpDeleteText( aSel );
    SetSelection( aPaM );
}

OUString TextUndoRemoveChars::GetComment () const
{
    // multiple lines?
    OUString sText(maText);
    Shorten(sText);
    return VclResId(STR_TEXTUNDO_REMOVECHARS).replaceAll("$1", sText);
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */