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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
|
/* -*- 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 <UndoOverwrite.hxx>
#include <unotools/charclass.hxx>
#include <unotools/transliterationwrapper.hxx>
#include <comphelper/processfactory.hxx>
#include <doc.hxx>
#include <IDocumentUndoRedo.hxx>
#include <IDocumentRedlineAccess.hxx>
#include <IShellCursorSupplier.hxx>
#include <swundo.hxx>
#include <pam.hxx>
#include <ndtxt.hxx>
#include <UndoCore.hxx>
#include <rolbck.hxx>
#include <acorrect.hxx>
#include <docary.hxx>
#include <strings.hrc>
using namespace ::com::sun::star;
using namespace ::com::sun::star::i18n;
using namespace ::com::sun::star::uno;
SwUndoOverwrite::SwUndoOverwrite( SwDoc* pDoc, SwPosition& rPos,
sal_Unicode cIns )
: SwUndo(SwUndoId::OVERWRITE, pDoc),
bGroup( false )
{
SwTextNode *const pTextNd = rPos.nNode.GetNode().GetTextNode();
assert(pTextNd);
sal_Int32 const nTextNdLen = pTextNd->GetText().getLength();
nSttNode = rPos.nNode.GetIndex();
nSttContent = rPos.nContent.GetIndex();
if( !pDoc->getIDocumentRedlineAccess().IsIgnoreRedline() && !pDoc->getIDocumentRedlineAccess().GetRedlineTable().empty() )
{
SwPaM aPam( rPos.nNode, rPos.nContent.GetIndex(),
rPos.nNode, rPos.nContent.GetIndex()+1 );
pRedlSaveData.reset( new SwRedlineSaveDatas );
if( !FillSaveData( aPam, *pRedlSaveData, false ))
{
pRedlSaveData.reset();
}
if (nSttContent < nTextNdLen)
{
pDoc->getIDocumentRedlineAccess().DeleteRedline(aPam, false, RedlineType::Any);
}
}
bInsChar = true;
if( nSttContent < nTextNdLen ) // no pure insert?
{
aDelStr += OUStringChar( pTextNd->GetText()[nSttContent] );
if( !m_pHistory )
m_pHistory.reset( new SwHistory );
SwRegHistory aRHst( *pTextNd, m_pHistory.get() );
m_pHistory->CopyAttr( pTextNd->GetpSwpHints(), nSttNode, 0,
nTextNdLen, false );
++rPos.nContent;
bInsChar = false;
}
bool bOldExpFlg = pTextNd->IsIgnoreDontExpand();
pTextNd->SetIgnoreDontExpand( true );
pTextNd->InsertText( OUString(cIns), rPos.nContent,
SwInsertFlags::EMPTYEXPAND );
aInsStr += OUStringChar( cIns );
if( !bInsChar )
{
const SwIndex aTmpIndex( rPos.nContent, -2 );
pTextNd->EraseText( aTmpIndex, 1 );
}
pTextNd->SetIgnoreDontExpand( bOldExpFlg );
m_bCacheComment = false;
}
SwUndoOverwrite::~SwUndoOverwrite()
{
}
bool SwUndoOverwrite::CanGrouping( SwDoc* pDoc, SwPosition& rPos,
sal_Unicode cIns )
{
// What is with only inserted characters?
// Only deletion of single chars can be combined.
if( rPos.nNode != nSttNode || aInsStr.isEmpty() ||
( !bGroup && aInsStr.getLength() != 1 ))
return false;
// Is the node a TextNode at all?
SwTextNode * pDelTextNd = rPos.nNode.GetNode().GetTextNode();
if( !pDelTextNd ||
(pDelTextNd->GetText().getLength() != rPos.nContent.GetIndex() &&
rPos.nContent.GetIndex() != ( nSttContent + aInsStr.getLength() )))
return false;
CharClass& rCC = GetAppCharClass();
// ask the char that should be inserted
if (( CH_TXTATR_BREAKWORD == cIns || CH_TXTATR_INWORD == cIns ) ||
rCC.isLetterNumeric( OUString( cIns ), 0 ) !=
rCC.isLetterNumeric( aInsStr, aInsStr.getLength()-1 ) )
return false;
if (!bInsChar && rPos.nContent.GetIndex() < pDelTextNd->GetText().getLength())
{
SwRedlineSaveDatas aTmpSav;
SwPaM aPam( rPos.nNode, rPos.nContent.GetIndex(),
rPos.nNode, rPos.nContent.GetIndex()+1 );
const bool bSaved = FillSaveData( aPam, aTmpSav, false );
bool bOk = ( !pRedlSaveData && !bSaved ) ||
( pRedlSaveData && bSaved &&
SwUndo::CanRedlineGroup( *pRedlSaveData, aTmpSav,
nSttContent > rPos.nContent.GetIndex() ));
// aTmpSav.DeleteAndDestroyAll();
if( !bOk )
return false;
pDoc->getIDocumentRedlineAccess().DeleteRedline( aPam, false, RedlineType::Any );
}
// both 'overwrites' can be combined so 'move' the corresponding character
if( !bInsChar )
{
if (rPos.nContent.GetIndex() < pDelTextNd->GetText().getLength())
{
aDelStr += OUStringChar( pDelTextNd->GetText()[rPos.nContent.GetIndex()] );
++rPos.nContent;
}
else
bInsChar = true;
}
bool bOldExpFlg = pDelTextNd->IsIgnoreDontExpand();
pDelTextNd->SetIgnoreDontExpand( true );
OUString const ins( pDelTextNd->InsertText(OUString(cIns), rPos.nContent,
SwInsertFlags::EMPTYEXPAND) );
assert(ins.getLength() == 1); // check in SwDoc::Overwrite => cannot fail
(void) ins;
aInsStr += OUStringChar( cIns );
if( !bInsChar )
{
const SwIndex aTmpIndex( rPos.nContent, -2 );
pDelTextNd->EraseText( aTmpIndex, 1 );
}
pDelTextNd->SetIgnoreDontExpand( bOldExpFlg );
bGroup = true;
return true;
}
void SwUndoOverwrite::UndoImpl(::sw::UndoRedoContext & rContext)
{
SwDoc *const pDoc = & rContext.GetDoc();
SwPaM *const pCurrentPam(& rContext.GetCursorSupplier().CreateNewShellCursor());
pCurrentPam->DeleteMark();
pCurrentPam->GetPoint()->nNode = nSttNode;
SwTextNode* pTextNd = pCurrentPam->GetNode().GetTextNode();
assert(pTextNd);
SwIndex& rIdx = pCurrentPam->GetPoint()->nContent;
rIdx.Assign( pTextNd, nSttContent );
SwAutoCorrExceptWord* pACEWord = pDoc->GetAutoCorrExceptWord();
if( pACEWord )
{
if( 1 == aInsStr.getLength() && 1 == aDelStr.getLength() )
pACEWord->CheckChar( *pCurrentPam->GetPoint(), aDelStr[0] );
pDoc->SetAutoCorrExceptWord( nullptr );
}
// If there was not only an overwrite but also an insert, delete the surplus
if( aInsStr.getLength() > aDelStr.getLength() )
{
rIdx += aDelStr.getLength();
pTextNd->EraseText( rIdx, aInsStr.getLength() - aDelStr.getLength() );
rIdx = nSttContent;
}
if( !aDelStr.isEmpty() )
{
bool bOldExpFlg = pTextNd->IsIgnoreDontExpand();
pTextNd->SetIgnoreDontExpand( true );
++rIdx;
for( sal_Int32 n = 0; n < aDelStr.getLength(); n++ )
{
// do it individually, to keep the attributes!
OUString aTmpStr(aDelStr[n]);
OUString const ins( pTextNd->InsertText(aTmpStr, rIdx) );
assert(ins.getLength() == 1); // cannot fail
(void) ins;
rIdx -= 2;
pTextNd->EraseText( rIdx, 1 );
rIdx += 2;
}
pTextNd->SetIgnoreDontExpand( bOldExpFlg );
--rIdx;
}
if( m_pHistory )
{
if( pTextNd->GetpSwpHints() )
pTextNd->ClearSwpHintsArr( false );
m_pHistory->TmpRollback( pDoc, 0, false );
}
if( pCurrentPam->GetMark()->nContent.GetIndex() != nSttContent )
{
pCurrentPam->SetMark();
pCurrentPam->GetMark()->nContent = nSttContent;
}
if( pRedlSaveData )
SetSaveData( *pDoc, *pRedlSaveData );
}
void SwUndoOverwrite::RepeatImpl(::sw::RepeatContext & rContext)
{
SwPaM *const pCurrentPam = & rContext.GetRepeatPaM();
if( aInsStr.isEmpty() || pCurrentPam->HasMark() )
return;
SwDoc & rDoc = rContext.GetDoc();
{
::sw::GroupUndoGuard const undoGuard(rDoc.GetIDocumentUndoRedo());
rDoc.getIDocumentContentOperations().Overwrite(*pCurrentPam, OUString(aInsStr[0]));
}
for( sal_Int32 n = 1; n < aInsStr.getLength(); ++n )
rDoc.getIDocumentContentOperations().Overwrite( *pCurrentPam, OUString(aInsStr[n]) );
}
void SwUndoOverwrite::RedoImpl(::sw::UndoRedoContext & rContext)
{
SwDoc *const pDoc = & rContext.GetDoc();
SwPaM *const pCurrentPam(& rContext.GetCursorSupplier().CreateNewShellCursor());
pCurrentPam->DeleteMark();
pCurrentPam->GetPoint()->nNode = nSttNode;
SwTextNode* pTextNd = pCurrentPam->GetNode().GetTextNode();
assert(pTextNd);
SwIndex& rIdx = pCurrentPam->GetPoint()->nContent;
if( pRedlSaveData )
{
rIdx.Assign( pTextNd, nSttContent );
pCurrentPam->SetMark();
pCurrentPam->GetMark()->nContent += aDelStr.getLength();
pDoc->getIDocumentRedlineAccess().DeleteRedline( *pCurrentPam, false, RedlineType::Any );
pCurrentPam->DeleteMark();
}
rIdx.Assign( pTextNd, !aDelStr.isEmpty() ? nSttContent+1 : nSttContent );
bool bOldExpFlg = pTextNd->IsIgnoreDontExpand();
pTextNd->SetIgnoreDontExpand( true );
for( sal_Int32 n = 0; n < aInsStr.getLength(); n++ )
{
// do it individually, to keep the attributes!
OUString const ins(
pTextNd->InsertText( OUString(aInsStr[n]), rIdx,
SwInsertFlags::EMPTYEXPAND) );
assert(ins.getLength() == 1); // cannot fail
(void) ins;
if( n < aDelStr.getLength() )
{
rIdx -= 2;
pTextNd->EraseText( rIdx, 1 );
rIdx += n+1 < aDelStr.getLength() ? 2 : 1;
}
}
pTextNd->SetIgnoreDontExpand( bOldExpFlg );
// get back old start position from UndoNodes array
if( m_pHistory )
m_pHistory->SetTmpEnd( m_pHistory->Count() );
if( pCurrentPam->GetMark()->nContent.GetIndex() != nSttContent )
{
pCurrentPam->SetMark();
pCurrentPam->GetMark()->nContent = nSttContent;
}
}
SwRewriter SwUndoOverwrite::GetRewriter() const
{
SwRewriter aResult;
OUString aString = SwResId(STR_START_QUOTE) +
ShortenString(aInsStr, nUndoStringLength, SwResId(STR_LDOTS)) +
SwResId(STR_END_QUOTE);
aResult.AddRule(UndoArg1, aString);
return aResult;
}
struct UndoTransliterate_Data
{
OUString sText;
std::unique_ptr<SwHistory> pHistory;
std::unique_ptr<Sequence< sal_Int32 >> pOffsets;
sal_uLong nNdIdx;
sal_Int32 nStart, nLen;
UndoTransliterate_Data( sal_uLong nNd, sal_Int32 nStt, sal_Int32 nStrLen, const OUString& rText )
: sText( rText ),
nNdIdx( nNd ), nStart( nStt ), nLen( nStrLen )
{}
void SetChangeAtNode( SwDoc& rDoc );
};
SwUndoTransliterate::SwUndoTransliterate(
const SwPaM& rPam,
const utl::TransliterationWrapper& rTrans )
: SwUndo( SwUndoId::TRANSLITERATE, rPam.GetDoc() ), SwUndRng( rPam ), nType( rTrans.getType() )
{
}
SwUndoTransliterate::~SwUndoTransliterate()
{
}
void SwUndoTransliterate::UndoImpl(::sw::UndoRedoContext & rContext)
{
SwDoc & rDoc = rContext.GetDoc();
// since the changes were added to the vector from the end of the string/node towards
// the start, we need to revert them from the start towards the end now to keep the
// offset information of the undo data in sync with the changing text.
// Thus we need to iterate from the end of the vector to the start
for (sal_Int32 i = aChanges.size() - 1; i >= 0; --i)
aChanges[i]->SetChangeAtNode( rDoc );
AddUndoRedoPaM(rContext, true);
}
void SwUndoTransliterate::RedoImpl(::sw::UndoRedoContext & rContext)
{
SwPaM & rPam( AddUndoRedoPaM(rContext) );
DoTransliterate(rContext.GetDoc(), rPam);
}
void SwUndoTransliterate::RepeatImpl(::sw::RepeatContext & rContext)
{
DoTransliterate(rContext.GetDoc(), rContext.GetRepeatPaM());
}
void SwUndoTransliterate::DoTransliterate(SwDoc & rDoc, SwPaM const & rPam)
{
utl::TransliterationWrapper aTrans( ::comphelper::getProcessComponentContext(), nType );
rDoc.getIDocumentContentOperations().TransliterateText( rPam, aTrans );
}
void SwUndoTransliterate::AddChanges( SwTextNode& rTNd,
sal_Int32 nStart, sal_Int32 nLen,
uno::Sequence <sal_Int32> const & rOffsets )
{
long nOffsLen = rOffsets.getLength();
UndoTransliterate_Data* pNew = new UndoTransliterate_Data(
rTNd.GetIndex(), nStart, static_cast<sal_Int32>(nOffsLen),
rTNd.GetText().copy(nStart, nLen));
aChanges.push_back( std::unique_ptr<UndoTransliterate_Data>(pNew) );
const sal_Int32* pOffsets = rOffsets.getConstArray();
// where did we need less memory ?
const sal_Int32* p = pOffsets;
for( long n = 0; n < nOffsLen; ++n, ++p )
if( *p != ( nStart + n ))
{
// create the Offset array
pNew->pOffsets.reset( new Sequence <sal_Int32> ( nLen ) );
sal_Int32* pIdx = pNew->pOffsets->getArray();
p = pOffsets;
long nMyOff, nNewVal = nStart;
for( n = 0, nMyOff = nStart; n < nOffsLen; ++p, ++n, ++nMyOff )
{
if( *p < nMyOff )
{
// something is deleted
nMyOff = *p;
*(pIdx-1) = nNewVal++;
}
else if( *p > nMyOff )
{
for( ; *p > nMyOff; ++nMyOff )
*pIdx++ = nNewVal;
--nMyOff;
--n;
--p;
}
else
*pIdx++ = nNewVal++;
}
// and then we need to save the attributes/bookmarks
// but this data must moved every time to the last in the chain!
for (size_t i = 0; i + 1 < aChanges.size(); ++i) // check all changes but not the current one
{
UndoTransliterate_Data* pD = aChanges[i].get();
if( pD->nNdIdx == pNew->nNdIdx && pD->pHistory )
{
// same node and have a history?
pNew->pHistory = std::move(pD->pHistory);
break; // more can't exist
}
}
if( !pNew->pHistory )
{
pNew->pHistory.reset( new SwHistory );
SwRegHistory aRHst( rTNd, pNew->pHistory.get() );
pNew->pHistory->CopyAttr( rTNd.GetpSwpHints(),
pNew->nNdIdx, 0, rTNd.GetText().getLength(), false );
}
break;
}
}
void UndoTransliterate_Data::SetChangeAtNode( SwDoc& rDoc )
{
SwTextNode* pTNd = rDoc.GetNodes()[ nNdIdx ]->GetTextNode();
if( pTNd )
{
Sequence <sal_Int32> aOffsets( pOffsets ? pOffsets->getLength() : nLen );
if( pOffsets )
aOffsets = *pOffsets;
else
{
sal_Int32* p = aOffsets.getArray();
for( sal_Int32 n = 0; n < nLen; ++n, ++p )
*p = n + nStart;
}
pTNd->ReplaceTextOnly( nStart, nLen, sText, aOffsets );
if( pHistory )
{
if( pTNd->GetpSwpHints() )
pTNd->ClearSwpHintsArr( false );
pHistory->TmpRollback( &rDoc, 0, false );
pHistory->SetTmpEnd( pHistory->Count() );
}
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|