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 <UndoOverwrite.hxx>
21 #include <unotools/charclass.hxx>
22 #include <unotools/transliterationwrapper.hxx>
23 #include <comphelper/processfactory.hxx>
24 #include <doc.hxx>
25 #include <IDocumentUndoRedo.hxx>
26 #include <IDocumentRedlineAccess.hxx>
27 #include <IShellCursorSupplier.hxx>
28 #include <swundo.hxx>
29 #include <pam.hxx>
30 #include <ndtxt.hxx>
31 #include <UndoCore.hxx>
32 #include <rolbck.hxx>
33 #include <acorrect.hxx>
34 #include <docary.hxx>
35 #include <strings.hrc>
36 
37 using namespace ::com::sun::star;
38 using namespace ::com::sun::star::i18n;
39 using namespace ::com::sun::star::uno;
40 
SwUndoOverwrite(SwDoc & rDoc,SwPosition & rPos,sal_Unicode cIns)41 SwUndoOverwrite::SwUndoOverwrite( SwDoc& rDoc, SwPosition& rPos,
42                                     sal_Unicode cIns )
43     : SwUndo(SwUndoId::OVERWRITE, &rDoc),
44       m_bGroup( false )
45 {
46     SwTextNode *const pTextNd = rPos.nNode.GetNode().GetTextNode();
47     assert(pTextNd);
48     sal_Int32 const nTextNdLen = pTextNd->GetText().getLength();
49 
50     m_nStartNode = rPos.nNode.GetIndex();
51     m_nStartContent = rPos.nContent.GetIndex();
52 
53     if( !rDoc.getIDocumentRedlineAccess().IsIgnoreRedline() && !rDoc.getIDocumentRedlineAccess().GetRedlineTable().empty() )
54     {
55         SwPaM aPam( rPos.nNode, rPos.nContent.GetIndex(),
56                     rPos.nNode, rPos.nContent.GetIndex()+1 );
57         m_pRedlSaveData.reset( new SwRedlineSaveDatas );
58         if( !FillSaveData( aPam, *m_pRedlSaveData, false ))
59         {
60             m_pRedlSaveData.reset();
61         }
62         if (m_nStartContent < nTextNdLen)
63         {
64             rDoc.getIDocumentRedlineAccess().DeleteRedline(aPam, false, RedlineType::Any);
65         }
66     }
67 
68     m_bInsChar = true;
69     if( m_nStartContent < nTextNdLen )     // no pure insert?
70     {
71         m_aDelStr += OUStringChar( pTextNd->GetText()[m_nStartContent] );
72         if( !m_pHistory )
73             m_pHistory.reset( new SwHistory );
74         SwRegHistory aRHst( *pTextNd, m_pHistory.get() );
75         m_pHistory->CopyAttr( pTextNd->GetpSwpHints(), m_nStartNode, 0,
76                             nTextNdLen, false );
77         ++rPos.nContent;
78         m_bInsChar = false;
79     }
80 
81     bool bOldExpFlg = pTextNd->IsIgnoreDontExpand();
82     pTextNd->SetIgnoreDontExpand( true );
83 
84     pTextNd->InsertText( OUString(cIns), rPos.nContent,
85             SwInsertFlags::EMPTYEXPAND );
86     m_aInsStr += OUStringChar( cIns );
87 
88     if( !m_bInsChar )
89     {
90         const SwIndex aTmpIndex( rPos.nContent, -2 );
91         pTextNd->EraseText( aTmpIndex, 1 );
92     }
93     pTextNd->SetIgnoreDontExpand( bOldExpFlg );
94 
95     m_bCacheComment = false;
96 }
97 
~SwUndoOverwrite()98 SwUndoOverwrite::~SwUndoOverwrite()
99 {
100 }
101 
CanGrouping(SwDoc & rDoc,SwPosition & rPos,sal_Unicode cIns)102 bool SwUndoOverwrite::CanGrouping( SwDoc& rDoc, SwPosition& rPos,
103                                     sal_Unicode cIns )
104 {
105 // What is with only inserted characters?
106 
107     // Only deletion of single chars can be combined.
108     if( rPos.nNode != m_nStartNode || m_aInsStr.isEmpty()  ||
109         ( !m_bGroup && m_aInsStr.getLength() != 1 ))
110         return false;
111 
112     // Is the node a TextNode at all?
113     SwTextNode * pDelTextNd = rPos.nNode.GetNode().GetTextNode();
114     if( !pDelTextNd ||
115         (pDelTextNd->GetText().getLength() != rPos.nContent.GetIndex() &&
116             rPos.nContent.GetIndex() != ( m_nStartContent + m_aInsStr.getLength() )))
117         return false;
118 
119     CharClass& rCC = GetAppCharClass();
120 
121     // ask the char that should be inserted
122     if (( CH_TXTATR_BREAKWORD == cIns || CH_TXTATR_INWORD == cIns ) ||
123         rCC.isLetterNumeric( OUString( cIns ), 0 ) !=
124         rCC.isLetterNumeric( m_aInsStr, m_aInsStr.getLength()-1 ) )
125         return false;
126 
127     if (!m_bInsChar && rPos.nContent.GetIndex() < pDelTextNd->GetText().getLength())
128     {
129         SwRedlineSaveDatas aTmpSav;
130         SwPaM aPam( rPos.nNode, rPos.nContent.GetIndex(),
131                     rPos.nNode, rPos.nContent.GetIndex()+1 );
132 
133         const bool bSaved = FillSaveData( aPam, aTmpSav, false );
134 
135         bool bOk = ( !m_pRedlSaveData && !bSaved ) ||
136                    ( m_pRedlSaveData && bSaved &&
137                         SwUndo::CanRedlineGroup( *m_pRedlSaveData, aTmpSav,
138                             m_nStartContent > rPos.nContent.GetIndex() ));
139         // aTmpSav.DeleteAndDestroyAll();
140         if( !bOk )
141             return false;
142 
143         rDoc.getIDocumentRedlineAccess().DeleteRedline( aPam, false, RedlineType::Any );
144     }
145 
146     // both 'overwrites' can be combined so 'move' the corresponding character
147     if( !m_bInsChar )
148     {
149         if (rPos.nContent.GetIndex() < pDelTextNd->GetText().getLength())
150         {
151             m_aDelStr += OUStringChar( pDelTextNd->GetText()[rPos.nContent.GetIndex()] );
152             ++rPos.nContent;
153         }
154         else
155             m_bInsChar = true;
156     }
157 
158     bool bOldExpFlg = pDelTextNd->IsIgnoreDontExpand();
159     pDelTextNd->SetIgnoreDontExpand( true );
160 
161     OUString const ins( pDelTextNd->InsertText(OUString(cIns), rPos.nContent,
162             SwInsertFlags::EMPTYEXPAND) );
163     assert(ins.getLength() == 1); // check in SwDoc::Overwrite => cannot fail
164     (void) ins;
165     m_aInsStr += OUStringChar( cIns );
166 
167     if( !m_bInsChar )
168     {
169         const SwIndex aTmpIndex( rPos.nContent, -2 );
170         pDelTextNd->EraseText( aTmpIndex, 1 );
171     }
172     pDelTextNd->SetIgnoreDontExpand( bOldExpFlg );
173 
174     m_bGroup = true;
175     return true;
176 }
177 
UndoImpl(::sw::UndoRedoContext & rContext)178 void SwUndoOverwrite::UndoImpl(::sw::UndoRedoContext & rContext)
179 {
180     SwDoc& rDoc = rContext.GetDoc();
181     SwPaM& rCurrentPam(rContext.GetCursorSupplier().CreateNewShellCursor());
182 
183     rCurrentPam.DeleteMark();
184     rCurrentPam.GetPoint()->nNode = m_nStartNode;
185     SwTextNode* pTextNd = rCurrentPam.GetNode().GetTextNode();
186     assert(pTextNd);
187     SwIndex& rIdx = rCurrentPam.GetPoint()->nContent;
188     rIdx.Assign( pTextNd, m_nStartContent );
189 
190     SwAutoCorrExceptWord* pACEWord = rDoc.GetAutoCorrExceptWord();
191     if( pACEWord )
192     {
193         if( 1 == m_aInsStr.getLength() && 1 == m_aDelStr.getLength() )
194             pACEWord->CheckChar( *rCurrentPam.GetPoint(), m_aDelStr[0] );
195         rDoc.SetAutoCorrExceptWord( nullptr );
196     }
197 
198     // If there was not only an overwrite but also an insert, delete the surplus
199     if( m_aInsStr.getLength() > m_aDelStr.getLength() )
200     {
201         rIdx += m_aDelStr.getLength();
202         pTextNd->EraseText( rIdx, m_aInsStr.getLength() - m_aDelStr.getLength() );
203         rIdx = m_nStartContent;
204     }
205 
206     if( !m_aDelStr.isEmpty() )
207     {
208         bool bOldExpFlg = pTextNd->IsIgnoreDontExpand();
209         pTextNd->SetIgnoreDontExpand( true );
210 
211         ++rIdx;
212         for( sal_Int32 n = 0; n < m_aDelStr.getLength(); n++  )
213         {
214             // do it individually, to keep the attributes!
215             OUString aTmpStr(m_aDelStr[n]);
216             OUString const ins( pTextNd->InsertText(aTmpStr, rIdx) );
217             assert(ins.getLength() == 1); // cannot fail
218             (void) ins;
219             rIdx -= 2;
220             pTextNd->EraseText( rIdx, 1 );
221             rIdx += 2;
222         }
223         pTextNd->SetIgnoreDontExpand( bOldExpFlg );
224         --rIdx;
225     }
226 
227     if( m_pHistory )
228     {
229         if( pTextNd->GetpSwpHints() )
230             pTextNd->ClearSwpHintsArr( false );
231         m_pHistory->TmpRollback( &rDoc, 0, false );
232     }
233 
234     if( rCurrentPam.GetMark()->nContent.GetIndex() != m_nStartContent )
235     {
236         rCurrentPam.SetMark();
237         rCurrentPam.GetMark()->nContent = m_nStartContent;
238     }
239 
240     if( m_pRedlSaveData )
241         SetSaveData( rDoc, *m_pRedlSaveData );
242 }
243 
RepeatImpl(::sw::RepeatContext & rContext)244 void SwUndoOverwrite::RepeatImpl(::sw::RepeatContext & rContext)
245 {
246     SwPaM& rCurrentPam = rContext.GetRepeatPaM();
247     if( m_aInsStr.isEmpty() || rCurrentPam.HasMark() )
248         return;
249 
250     SwDoc & rDoc = rContext.GetDoc();
251 
252     {
253         ::sw::GroupUndoGuard const undoGuard(rDoc.GetIDocumentUndoRedo());
254         rDoc.getIDocumentContentOperations().Overwrite(rCurrentPam, OUString(m_aInsStr[0]));
255     }
256     for( sal_Int32 n = 1; n < m_aInsStr.getLength(); ++n )
257         rDoc.getIDocumentContentOperations().Overwrite(rCurrentPam, OUString(m_aInsStr[n]));
258 }
259 
RedoImpl(::sw::UndoRedoContext & rContext)260 void SwUndoOverwrite::RedoImpl(::sw::UndoRedoContext & rContext)
261 {
262     SwDoc& rDoc = rContext.GetDoc();
263     SwPaM& rCurrentPam(rContext.GetCursorSupplier().CreateNewShellCursor());
264 
265     rCurrentPam.DeleteMark();
266     rCurrentPam.GetPoint()->nNode = m_nStartNode;
267     SwTextNode* pTextNd = rCurrentPam.GetNode().GetTextNode();
268     assert(pTextNd);
269     SwIndex& rIdx = rCurrentPam.GetPoint()->nContent;
270 
271     if( m_pRedlSaveData )
272     {
273         rIdx.Assign( pTextNd, m_nStartContent );
274         rCurrentPam.SetMark();
275         rCurrentPam.GetMark()->nContent += m_aDelStr.getLength();
276         rDoc.getIDocumentRedlineAccess().DeleteRedline( rCurrentPam, false, RedlineType::Any );
277         rCurrentPam.DeleteMark();
278     }
279     rIdx.Assign( pTextNd, !m_aDelStr.isEmpty() ? m_nStartContent+1 : m_nStartContent );
280 
281     bool bOldExpFlg = pTextNd->IsIgnoreDontExpand();
282     pTextNd->SetIgnoreDontExpand( true );
283 
284     for( sal_Int32 n = 0; n < m_aInsStr.getLength(); n++  )
285     {
286         // do it individually, to keep the attributes!
287         OUString const ins(
288                 pTextNd->InsertText( OUString(m_aInsStr[n]), rIdx,
289                 SwInsertFlags::EMPTYEXPAND) );
290         assert(ins.getLength() == 1); // cannot fail
291         (void) ins;
292         if( n < m_aDelStr.getLength() )
293         {
294             rIdx -= 2;
295             pTextNd->EraseText( rIdx, 1 );
296             rIdx += n+1 < m_aDelStr.getLength() ? 2 : 1;
297         }
298     }
299     pTextNd->SetIgnoreDontExpand( bOldExpFlg );
300 
301     // get back old start position from UndoNodes array
302     if( m_pHistory )
303         m_pHistory->SetTmpEnd( m_pHistory->Count() );
304     if( rCurrentPam.GetMark()->nContent.GetIndex() != m_nStartContent )
305     {
306         rCurrentPam.SetMark();
307         rCurrentPam.GetMark()->nContent = m_nStartContent;
308     }
309 }
310 
GetRewriter() const311 SwRewriter SwUndoOverwrite::GetRewriter() const
312 {
313     SwRewriter aResult;
314 
315     OUString aString = SwResId(STR_START_QUOTE) +
316         ShortenString(m_aInsStr, nUndoStringLength, SwResId(STR_LDOTS)) +
317         SwResId(STR_END_QUOTE);
318 
319     aResult.AddRule(UndoArg1, aString);
320 
321     return aResult;
322 }
323 
324 struct UndoTransliterate_Data
325 {
326     OUString        sText;
327     std::unique_ptr<SwHistory> pHistory;
328     std::unique_ptr<Sequence< sal_Int32 >> pOffsets;
329     sal_uLong           nNdIdx;
330     sal_Int32      nStart, nLen;
331 
UndoTransliterate_DataUndoTransliterate_Data332     UndoTransliterate_Data( sal_uLong nNd, sal_Int32 nStt, sal_Int32 nStrLen, const OUString& rText )
333         : sText( rText ),
334         nNdIdx( nNd ), nStart( nStt ), nLen( nStrLen )
335     {}
336 
337     void SetChangeAtNode( SwDoc& rDoc );
338 };
339 
SwUndoTransliterate(const SwPaM & rPam,const utl::TransliterationWrapper & rTrans)340 SwUndoTransliterate::SwUndoTransliterate(
341     const SwPaM& rPam,
342     const utl::TransliterationWrapper& rTrans )
343     : SwUndo( SwUndoId::TRANSLITERATE, &rPam.GetDoc() ), SwUndRng( rPam ), m_nType( rTrans.getType() )
344 {
345 }
346 
~SwUndoTransliterate()347 SwUndoTransliterate::~SwUndoTransliterate()
348 {
349 }
350 
UndoImpl(::sw::UndoRedoContext & rContext)351 void SwUndoTransliterate::UndoImpl(::sw::UndoRedoContext & rContext)
352 {
353     SwDoc & rDoc = rContext.GetDoc();
354 
355     // since the changes were added to the vector from the end of the string/node towards
356     // the start, we need to revert them from the start towards the end now to keep the
357     // offset information of the undo data in sync with the changing text.
358     // Thus we need to iterate from the end of the vector to the start
359     for (sal_Int32 i = m_aChanges.size() - 1; i >= 0;  --i)
360         m_aChanges[i]->SetChangeAtNode( rDoc );
361 
362     AddUndoRedoPaM(rContext, true);
363 }
364 
RedoImpl(::sw::UndoRedoContext & rContext)365 void SwUndoTransliterate::RedoImpl(::sw::UndoRedoContext & rContext)
366 {
367     SwPaM & rPam( AddUndoRedoPaM(rContext) );
368     DoTransliterate(rContext.GetDoc(), rPam);
369 }
370 
RepeatImpl(::sw::RepeatContext & rContext)371 void SwUndoTransliterate::RepeatImpl(::sw::RepeatContext & rContext)
372 {
373     DoTransliterate(rContext.GetDoc(), rContext.GetRepeatPaM());
374 }
375 
DoTransliterate(SwDoc & rDoc,SwPaM const & rPam)376 void SwUndoTransliterate::DoTransliterate(SwDoc & rDoc, SwPaM const & rPam)
377 {
378     utl::TransliterationWrapper aTrans( ::comphelper::getProcessComponentContext(), m_nType );
379     rDoc.getIDocumentContentOperations().TransliterateText( rPam, aTrans );
380 }
381 
AddChanges(SwTextNode & rTNd,sal_Int32 nStart,sal_Int32 nLen,uno::Sequence<sal_Int32> const & rOffsets)382 void SwUndoTransliterate::AddChanges( SwTextNode& rTNd,
383                     sal_Int32 nStart, sal_Int32 nLen,
384                     uno::Sequence <sal_Int32> const & rOffsets )
385 {
386     tools::Long nOffsLen = rOffsets.getLength();
387     UndoTransliterate_Data* pNew = new UndoTransliterate_Data(
388                         rTNd.GetIndex(), nStart, static_cast<sal_Int32>(nOffsLen),
389                         rTNd.GetText().copy(nStart, nLen));
390 
391     m_aChanges.push_back( std::unique_ptr<UndoTransliterate_Data>(pNew) );
392 
393     const sal_Int32* pOffsets = rOffsets.getConstArray();
394     // where did we need less memory ?
395     const sal_Int32* p = pOffsets;
396     for( tools::Long n = 0; n < nOffsLen; ++n, ++p )
397     if( *p != ( nStart + n ))
398     {
399         // create the Offset array
400         pNew->pOffsets.reset( new Sequence <sal_Int32> ( nLen ) );
401         sal_Int32* pIdx = pNew->pOffsets->getArray();
402         p = pOffsets;
403         tools::Long nMyOff, nNewVal = nStart;
404         for( n = 0, nMyOff = nStart; n < nOffsLen; ++p, ++n, ++nMyOff )
405         {
406             if( *p < nMyOff )
407             {
408                 // something is deleted
409                 nMyOff = *p;
410                 *(pIdx-1) = nNewVal++;
411             }
412             else if( *p > nMyOff )
413             {
414                 for( ; *p > nMyOff; ++nMyOff )
415                     *pIdx++ = nNewVal;
416                 --nMyOff;
417                 --n;
418                 --p;
419             }
420             else
421                 *pIdx++ = nNewVal++;
422         }
423 
424         // and then we need to save the attributes/bookmarks
425         // but this data must moved every time to the last in the chain!
426         for (size_t i = 0; i + 1 < m_aChanges.size(); ++i)    // check all changes but not the current one
427         {
428             UndoTransliterate_Data* pD = m_aChanges[i].get();
429             if( pD->nNdIdx == pNew->nNdIdx && pD->pHistory )
430             {
431                 // same node and have a history?
432                 pNew->pHistory = std::move(pD->pHistory);
433                 break;          // more can't exist
434             }
435         }
436 
437         if( !pNew->pHistory )
438         {
439             pNew->pHistory.reset( new SwHistory );
440             SwRegHistory aRHst( rTNd, pNew->pHistory.get() );
441             pNew->pHistory->CopyAttr( rTNd.GetpSwpHints(),
442                     pNew->nNdIdx, 0, rTNd.GetText().getLength(), false );
443         }
444         break;
445     }
446 }
447 
SetChangeAtNode(SwDoc & rDoc)448 void UndoTransliterate_Data::SetChangeAtNode( SwDoc& rDoc )
449 {
450     SwTextNode* pTNd = rDoc.GetNodes()[ nNdIdx ]->GetTextNode();
451     if( !pTNd )
452         return;
453 
454     Sequence <sal_Int32> aOffsets( pOffsets ? pOffsets->getLength() : nLen );
455     if( pOffsets )
456         aOffsets = *pOffsets;
457     else
458     {
459         sal_Int32* p = aOffsets.getArray();
460         for( sal_Int32 n = 0; n < nLen; ++n, ++p )
461             *p = n + nStart;
462     }
463     pTNd->ReplaceTextOnly( nStart, nLen, sText, aOffsets );
464 
465     if( pHistory )
466     {
467         if( pTNd->GetpSwpHints() )
468             pTNd->ClearSwpHintsArr( false );
469         pHistory->TmpRollback( &rDoc, 0, false );
470         pHistory->SetTmpEnd( pHistory->Count() );
471     }
472 }
473 
474 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
475