1*efeef26fSAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*efeef26fSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*efeef26fSAndrew Rist * or more contributor license agreements. See the NOTICE file
5*efeef26fSAndrew Rist * distributed with this work for additional information
6*efeef26fSAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*efeef26fSAndrew Rist * to you under the Apache License, Version 2.0 (the
8*efeef26fSAndrew Rist * "License"); you may not use this file except in compliance
9*efeef26fSAndrew Rist * with the License. You may obtain a copy of the License at
10cdf0e10cSrcweir *
11*efeef26fSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir *
13*efeef26fSAndrew Rist * Unless required by applicable law or agreed to in writing,
14*efeef26fSAndrew Rist * software distributed under the License is distributed on an
15*efeef26fSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*efeef26fSAndrew Rist * KIND, either express or implied. See the License for the
17*efeef26fSAndrew Rist * specific language governing permissions and limitations
18*efeef26fSAndrew Rist * under the License.
19cdf0e10cSrcweir *
20*efeef26fSAndrew Rist *************************************************************/
21*efeef26fSAndrew Rist
22*efeef26fSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sw.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir #include <UndoOverwrite.hxx>
28cdf0e10cSrcweir
29cdf0e10cSrcweir #include <tools/resid.hxx>
30cdf0e10cSrcweir
31cdf0e10cSrcweir #include <unotools/charclass.hxx>
32cdf0e10cSrcweir #include <unotools/transliterationwrapper.hxx>
33cdf0e10cSrcweir
34cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
35cdf0e10cSrcweir
36cdf0e10cSrcweir #include <doc.hxx>
37cdf0e10cSrcweir #include <IDocumentUndoRedo.hxx>
38cdf0e10cSrcweir #include <IShellCursorSupplier.hxx>
39cdf0e10cSrcweir #include <swundo.hxx> // fuer die UndoIds
40cdf0e10cSrcweir #include <pam.hxx>
41cdf0e10cSrcweir #include <ndtxt.hxx>
42cdf0e10cSrcweir #include <UndoCore.hxx>
43cdf0e10cSrcweir #include <rolbck.hxx>
44cdf0e10cSrcweir #include <acorrect.hxx>
45cdf0e10cSrcweir #include <docary.hxx>
46cdf0e10cSrcweir
47cdf0e10cSrcweir #include <comcore.hrc> // #111827#
48cdf0e10cSrcweir #include <undo.hrc>
49cdf0e10cSrcweir
50cdf0e10cSrcweir using namespace ::com::sun::star;
51cdf0e10cSrcweir using namespace ::com::sun::star::i18n;
52cdf0e10cSrcweir using namespace ::com::sun::star::uno;
53cdf0e10cSrcweir
54cdf0e10cSrcweir
55cdf0e10cSrcweir //------------------------------------------------------------
56cdf0e10cSrcweir
57cdf0e10cSrcweir // OVERWRITE
58cdf0e10cSrcweir
59cdf0e10cSrcweir
SwUndoOverwrite(SwDoc * pDoc,SwPosition & rPos,sal_Unicode cIns)60cdf0e10cSrcweir SwUndoOverwrite::SwUndoOverwrite( SwDoc* pDoc, SwPosition& rPos,
61cdf0e10cSrcweir sal_Unicode cIns )
62cdf0e10cSrcweir : SwUndo(UNDO_OVERWRITE),
63cdf0e10cSrcweir pRedlSaveData( 0 ), bGroup( sal_False )
64cdf0e10cSrcweir {
65cdf0e10cSrcweir if( !pDoc->IsIgnoreRedline() && pDoc->GetRedlineTbl().Count() )
66cdf0e10cSrcweir {
67cdf0e10cSrcweir SwPaM aPam( rPos.nNode, rPos.nContent.GetIndex(),
68cdf0e10cSrcweir rPos.nNode, rPos.nContent.GetIndex()+1 );
69cdf0e10cSrcweir pRedlSaveData = new SwRedlineSaveDatas;
70cdf0e10cSrcweir if( !FillSaveData( aPam, *pRedlSaveData, sal_False ))
71cdf0e10cSrcweir delete pRedlSaveData, pRedlSaveData = 0;
72cdf0e10cSrcweir }
73cdf0e10cSrcweir
74cdf0e10cSrcweir nSttNode = rPos.nNode.GetIndex();
75cdf0e10cSrcweir nSttCntnt = rPos.nContent.GetIndex();
76cdf0e10cSrcweir
77cdf0e10cSrcweir SwTxtNode* pTxtNd = rPos.nNode.GetNode().GetTxtNode();
78cdf0e10cSrcweir ASSERT( pTxtNd, "Overwrite nicht im TextNode?" );
79cdf0e10cSrcweir
80cdf0e10cSrcweir bInsChar = sal_True;
81cdf0e10cSrcweir xub_StrLen nTxtNdLen = pTxtNd->GetTxt().Len();
82cdf0e10cSrcweir if( nSttCntnt < nTxtNdLen ) // kein reines Einfuegen ?
83cdf0e10cSrcweir {
84cdf0e10cSrcweir aDelStr.Insert( pTxtNd->GetTxt().GetChar( nSttCntnt ) );
85cdf0e10cSrcweir if( !pHistory )
86cdf0e10cSrcweir pHistory = new SwHistory;
87cdf0e10cSrcweir SwRegHistory aRHst( *pTxtNd, pHistory );
88cdf0e10cSrcweir pHistory->CopyAttr( pTxtNd->GetpSwpHints(), nSttNode, 0,
89cdf0e10cSrcweir nTxtNdLen, false );
90cdf0e10cSrcweir rPos.nContent++;
91cdf0e10cSrcweir bInsChar = sal_False;
92cdf0e10cSrcweir }
93cdf0e10cSrcweir
94cdf0e10cSrcweir sal_Bool bOldExpFlg = pTxtNd->IsIgnoreDontExpand();
95cdf0e10cSrcweir pTxtNd->SetIgnoreDontExpand( sal_True );
96cdf0e10cSrcweir
97cdf0e10cSrcweir pTxtNd->InsertText( cIns, rPos.nContent,
98cdf0e10cSrcweir IDocumentContentOperations::INS_EMPTYEXPAND );
99cdf0e10cSrcweir aInsStr.Insert( cIns );
100cdf0e10cSrcweir
101cdf0e10cSrcweir if( !bInsChar )
102cdf0e10cSrcweir {
103cdf0e10cSrcweir const SwIndex aTmpIndex( rPos.nContent, -2 );
104cdf0e10cSrcweir pTxtNd->EraseText( aTmpIndex, 1 );
105cdf0e10cSrcweir }
106cdf0e10cSrcweir pTxtNd->SetIgnoreDontExpand( bOldExpFlg );
107cdf0e10cSrcweir
108cdf0e10cSrcweir bCacheComment = false;
109cdf0e10cSrcweir }
110cdf0e10cSrcweir
~SwUndoOverwrite()111cdf0e10cSrcweir SwUndoOverwrite::~SwUndoOverwrite()
112cdf0e10cSrcweir {
113cdf0e10cSrcweir delete pRedlSaveData;
114cdf0e10cSrcweir }
115cdf0e10cSrcweir
CanGrouping(SwDoc * pDoc,SwPosition & rPos,sal_Unicode cIns)116cdf0e10cSrcweir sal_Bool SwUndoOverwrite::CanGrouping( SwDoc* pDoc, SwPosition& rPos,
117cdf0e10cSrcweir sal_Unicode cIns )
118cdf0e10cSrcweir {
119cdf0e10cSrcweir /// ?? was ist mit nur eingefuegten Charaktern ???
120cdf0e10cSrcweir
121cdf0e10cSrcweir // es kann nur das Loeschen von einzelnen char's zusammengefasst werden
122cdf0e10cSrcweir if( rPos.nNode != nSttNode || !aInsStr.Len() ||
123cdf0e10cSrcweir ( !bGroup && aInsStr.Len() != 1 ))
124cdf0e10cSrcweir return sal_False;
125cdf0e10cSrcweir
126cdf0e10cSrcweir // ist der Node ueberhaupt ein TextNode?
127cdf0e10cSrcweir SwTxtNode * pDelTxtNd = rPos.nNode.GetNode().GetTxtNode();
128cdf0e10cSrcweir if( !pDelTxtNd ||
129cdf0e10cSrcweir ( pDelTxtNd->GetTxt().Len() != rPos.nContent.GetIndex() &&
130cdf0e10cSrcweir rPos.nContent.GetIndex() != ( nSttCntnt + aInsStr.Len() )))
131cdf0e10cSrcweir return sal_False;
132cdf0e10cSrcweir
133cdf0e10cSrcweir CharClass& rCC = GetAppCharClass();
134cdf0e10cSrcweir
135cdf0e10cSrcweir // befrage das einzufuegende Charakter
136cdf0e10cSrcweir if (( CH_TXTATR_BREAKWORD == cIns || CH_TXTATR_INWORD == cIns ) ||
137cdf0e10cSrcweir rCC.isLetterNumeric( String( cIns ), 0 ) !=
138cdf0e10cSrcweir rCC.isLetterNumeric( aInsStr, aInsStr.Len()-1 ) )
139cdf0e10cSrcweir return sal_False;
140cdf0e10cSrcweir
141cdf0e10cSrcweir {
142cdf0e10cSrcweir SwRedlineSaveDatas* pTmpSav = new SwRedlineSaveDatas;
143cdf0e10cSrcweir SwPaM aPam( rPos.nNode, rPos.nContent.GetIndex(),
144cdf0e10cSrcweir rPos.nNode, rPos.nContent.GetIndex()+1 );
145cdf0e10cSrcweir
146cdf0e10cSrcweir if( !FillSaveData( aPam, *pTmpSav, sal_False ))
147cdf0e10cSrcweir delete pTmpSav, pTmpSav = 0;
148cdf0e10cSrcweir
149cdf0e10cSrcweir sal_Bool bOk = ( !pRedlSaveData && !pTmpSav ) ||
150cdf0e10cSrcweir ( pRedlSaveData && pTmpSav &&
151cdf0e10cSrcweir SwUndo::CanRedlineGroup( *pRedlSaveData, *pTmpSav,
152cdf0e10cSrcweir nSttCntnt > rPos.nContent.GetIndex() ));
153cdf0e10cSrcweir delete pTmpSav;
154cdf0e10cSrcweir if( !bOk )
155cdf0e10cSrcweir return sal_False;
156cdf0e10cSrcweir
157cdf0e10cSrcweir pDoc->DeleteRedline( aPam, false, USHRT_MAX );
158cdf0e10cSrcweir }
159cdf0e10cSrcweir
160cdf0e10cSrcweir // Ok, die beiden 'Overwrites' koennen zusammen gefasst werden, also
161cdf0e10cSrcweir // 'verschiebe' das enstprechende Zeichen
162cdf0e10cSrcweir if( !bInsChar )
163cdf0e10cSrcweir {
164cdf0e10cSrcweir if( rPos.nContent.GetIndex() < pDelTxtNd->GetTxt().Len() )
165cdf0e10cSrcweir {
166cdf0e10cSrcweir aDelStr.Insert( pDelTxtNd->GetTxt().GetChar(rPos.nContent.GetIndex()) );
167cdf0e10cSrcweir rPos.nContent++;
168cdf0e10cSrcweir }
169cdf0e10cSrcweir else
170cdf0e10cSrcweir bInsChar = sal_True;
171cdf0e10cSrcweir }
172cdf0e10cSrcweir
173cdf0e10cSrcweir sal_Bool bOldExpFlg = pDelTxtNd->IsIgnoreDontExpand();
174cdf0e10cSrcweir pDelTxtNd->SetIgnoreDontExpand( sal_True );
175cdf0e10cSrcweir
176cdf0e10cSrcweir pDelTxtNd->InsertText( cIns, rPos.nContent,
177cdf0e10cSrcweir IDocumentContentOperations::INS_EMPTYEXPAND );
178cdf0e10cSrcweir aInsStr.Insert( cIns );
179cdf0e10cSrcweir
180cdf0e10cSrcweir if( !bInsChar )
181cdf0e10cSrcweir {
182cdf0e10cSrcweir const SwIndex aTmpIndex( rPos.nContent, -2 );
183cdf0e10cSrcweir pDelTxtNd->EraseText( aTmpIndex, 1 );
184cdf0e10cSrcweir }
185cdf0e10cSrcweir pDelTxtNd->SetIgnoreDontExpand( bOldExpFlg );
186cdf0e10cSrcweir
187cdf0e10cSrcweir bGroup = sal_True;
188cdf0e10cSrcweir return sal_True;
189cdf0e10cSrcweir }
190cdf0e10cSrcweir
191cdf0e10cSrcweir
192cdf0e10cSrcweir
193cdf0e10cSrcweir
194cdf0e10cSrcweir
UndoImpl(::sw::UndoRedoContext & rContext)195cdf0e10cSrcweir void SwUndoOverwrite::UndoImpl(::sw::UndoRedoContext & rContext)
196cdf0e10cSrcweir {
197cdf0e10cSrcweir SwDoc *const pDoc = & rContext.GetDoc();
198cdf0e10cSrcweir SwPaM *const pAktPam(& rContext.GetCursorSupplier().CreateNewShellCursor());
199cdf0e10cSrcweir
200cdf0e10cSrcweir pAktPam->DeleteMark();
201cdf0e10cSrcweir pAktPam->GetPoint()->nNode = nSttNode;
202cdf0e10cSrcweir SwTxtNode* pTxtNd = pAktPam->GetNode()->GetTxtNode();
203cdf0e10cSrcweir ASSERT( pTxtNd, "Overwrite nicht im TextNode?" );
204cdf0e10cSrcweir SwIndex& rIdx = pAktPam->GetPoint()->nContent;
205cdf0e10cSrcweir rIdx.Assign( pTxtNd, nSttCntnt );
206cdf0e10cSrcweir
207cdf0e10cSrcweir SwAutoCorrExceptWord* pACEWord = pDoc->GetAutoCorrExceptWord();
208cdf0e10cSrcweir if( pACEWord )
209cdf0e10cSrcweir {
210cdf0e10cSrcweir if( 1 == aInsStr.Len() && 1 == aDelStr.Len() )
211cdf0e10cSrcweir pACEWord->CheckChar( *pAktPam->GetPoint(), aDelStr.GetChar( 0 ) );
212cdf0e10cSrcweir pDoc->SetAutoCorrExceptWord( 0 );
213cdf0e10cSrcweir }
214cdf0e10cSrcweir
215cdf0e10cSrcweir // wurde nicht nur ueberschieben sondern auch geinsertet, so loesche
216cdf0e10cSrcweir // den Ueberhang
217cdf0e10cSrcweir if( aInsStr.Len() > aDelStr.Len() )
218cdf0e10cSrcweir {
219cdf0e10cSrcweir rIdx += aDelStr.Len();
220cdf0e10cSrcweir pTxtNd->EraseText( rIdx, aInsStr.Len() - aDelStr.Len() );
221cdf0e10cSrcweir rIdx = nSttCntnt;
222cdf0e10cSrcweir }
223cdf0e10cSrcweir
224cdf0e10cSrcweir if( aDelStr.Len() )
225cdf0e10cSrcweir {
226cdf0e10cSrcweir String aTmpStr( '1' );
227cdf0e10cSrcweir sal_Unicode* pTmpStr = aTmpStr.GetBufferAccess();
228cdf0e10cSrcweir
229cdf0e10cSrcweir sal_Bool bOldExpFlg = pTxtNd->IsIgnoreDontExpand();
230cdf0e10cSrcweir pTxtNd->SetIgnoreDontExpand( sal_True );
231cdf0e10cSrcweir
232cdf0e10cSrcweir rIdx++;
233cdf0e10cSrcweir for( xub_StrLen n = 0; n < aDelStr.Len(); n++ )
234cdf0e10cSrcweir {
235cdf0e10cSrcweir // einzeln, damit die Attribute stehen bleiben !!!
236cdf0e10cSrcweir *pTmpStr = aDelStr.GetChar( n );
237cdf0e10cSrcweir pTxtNd->InsertText( aTmpStr, rIdx /*???, SETATTR_NOTXTATRCHR*/ );
238cdf0e10cSrcweir rIdx -= 2;
239cdf0e10cSrcweir pTxtNd->EraseText( rIdx, 1 );
240cdf0e10cSrcweir rIdx += 2;
241cdf0e10cSrcweir }
242cdf0e10cSrcweir pTxtNd->SetIgnoreDontExpand( bOldExpFlg );
243cdf0e10cSrcweir rIdx--;
244cdf0e10cSrcweir }
245cdf0e10cSrcweir if( pHistory )
246cdf0e10cSrcweir {
247cdf0e10cSrcweir if( pTxtNd->GetpSwpHints() )
248cdf0e10cSrcweir pTxtNd->ClearSwpHintsArr( false );
249cdf0e10cSrcweir pHistory->TmpRollback( pDoc, 0, false );
250cdf0e10cSrcweir }
251cdf0e10cSrcweir
252cdf0e10cSrcweir if( pAktPam->GetMark()->nContent.GetIndex() != nSttCntnt )
253cdf0e10cSrcweir {
254cdf0e10cSrcweir pAktPam->SetMark();
255cdf0e10cSrcweir pAktPam->GetMark()->nContent = nSttCntnt;
256cdf0e10cSrcweir }
257cdf0e10cSrcweir
258cdf0e10cSrcweir if( pRedlSaveData )
259cdf0e10cSrcweir SetSaveData( *pDoc, *pRedlSaveData );
260cdf0e10cSrcweir }
261cdf0e10cSrcweir
RepeatImpl(::sw::RepeatContext & rContext)262cdf0e10cSrcweir void SwUndoOverwrite::RepeatImpl(::sw::RepeatContext & rContext)
263cdf0e10cSrcweir {
264cdf0e10cSrcweir SwPaM *const pAktPam = & rContext.GetRepeatPaM();
265cdf0e10cSrcweir if( !aInsStr.Len() || pAktPam->HasMark() )
266cdf0e10cSrcweir return;
267cdf0e10cSrcweir
268cdf0e10cSrcweir SwDoc & rDoc = rContext.GetDoc();
269cdf0e10cSrcweir
270cdf0e10cSrcweir {
271cdf0e10cSrcweir ::sw::GroupUndoGuard const undoGuard(rDoc.GetIDocumentUndoRedo());
272cdf0e10cSrcweir rDoc.Overwrite(*pAktPam, aInsStr.GetChar(0));
273cdf0e10cSrcweir }
274cdf0e10cSrcweir for( xub_StrLen n = 1; n < aInsStr.Len(); ++n )
275cdf0e10cSrcweir rDoc.Overwrite( *pAktPam, aInsStr.GetChar( n ) );
276cdf0e10cSrcweir }
277cdf0e10cSrcweir
RedoImpl(::sw::UndoRedoContext & rContext)278cdf0e10cSrcweir void SwUndoOverwrite::RedoImpl(::sw::UndoRedoContext & rContext)
279cdf0e10cSrcweir {
280cdf0e10cSrcweir SwDoc *const pDoc = & rContext.GetDoc();
281cdf0e10cSrcweir SwPaM *const pAktPam(& rContext.GetCursorSupplier().CreateNewShellCursor());
282cdf0e10cSrcweir
283cdf0e10cSrcweir pAktPam->DeleteMark();
284cdf0e10cSrcweir pAktPam->GetPoint()->nNode = nSttNode;
285cdf0e10cSrcweir SwTxtNode* pTxtNd = pAktPam->GetNode()->GetTxtNode();
286cdf0e10cSrcweir ASSERT( pTxtNd, "Overwrite nicht im TextNode?" );
287cdf0e10cSrcweir SwIndex& rIdx = pAktPam->GetPoint()->nContent;
288cdf0e10cSrcweir
289cdf0e10cSrcweir if( pRedlSaveData )
290cdf0e10cSrcweir {
291cdf0e10cSrcweir rIdx.Assign( pTxtNd, nSttCntnt );
292cdf0e10cSrcweir pAktPam->SetMark();
293cdf0e10cSrcweir pAktPam->GetMark()->nContent += aInsStr.Len();
294cdf0e10cSrcweir pDoc->DeleteRedline( *pAktPam, false, USHRT_MAX );
295cdf0e10cSrcweir pAktPam->DeleteMark();
296cdf0e10cSrcweir }
297cdf0e10cSrcweir rIdx.Assign( pTxtNd, aDelStr.Len() ? nSttCntnt+1 : nSttCntnt );
298cdf0e10cSrcweir
299cdf0e10cSrcweir sal_Bool bOldExpFlg = pTxtNd->IsIgnoreDontExpand();
300cdf0e10cSrcweir pTxtNd->SetIgnoreDontExpand( sal_True );
301cdf0e10cSrcweir
302cdf0e10cSrcweir for( xub_StrLen n = 0; n < aInsStr.Len(); n++ )
303cdf0e10cSrcweir {
304cdf0e10cSrcweir // einzeln, damit die Attribute stehen bleiben !!!
305cdf0e10cSrcweir pTxtNd->InsertText( aInsStr.GetChar( n ), rIdx,
306cdf0e10cSrcweir IDocumentContentOperations::INS_EMPTYEXPAND );
307cdf0e10cSrcweir if( n < aDelStr.Len() )
308cdf0e10cSrcweir {
309cdf0e10cSrcweir rIdx -= 2;
310cdf0e10cSrcweir pTxtNd->EraseText( rIdx, 1 );
311cdf0e10cSrcweir rIdx += n+1 < aDelStr.Len() ? 2 : 1;
312cdf0e10cSrcweir }
313cdf0e10cSrcweir }
314cdf0e10cSrcweir pTxtNd->SetIgnoreDontExpand( bOldExpFlg );
315cdf0e10cSrcweir
316cdf0e10cSrcweir // alte Anfangs-Position vom UndoNodes-Array zurueckholen
317cdf0e10cSrcweir if( pHistory )
318cdf0e10cSrcweir pHistory->SetTmpEnd( pHistory->Count() );
319cdf0e10cSrcweir if( pAktPam->GetMark()->nContent.GetIndex() != nSttCntnt )
320cdf0e10cSrcweir {
321cdf0e10cSrcweir pAktPam->SetMark();
322cdf0e10cSrcweir pAktPam->GetMark()->nContent = nSttCntnt;
323cdf0e10cSrcweir }
324cdf0e10cSrcweir }
325cdf0e10cSrcweir
GetRewriter() const326cdf0e10cSrcweir SwRewriter SwUndoOverwrite::GetRewriter() const
327cdf0e10cSrcweir {
328cdf0e10cSrcweir SwRewriter aResult;
329cdf0e10cSrcweir
330cdf0e10cSrcweir String aString;
331cdf0e10cSrcweir
332cdf0e10cSrcweir aString += String(SW_RES(STR_START_QUOTE));
333cdf0e10cSrcweir aString += ShortenString(aInsStr, nUndoStringLength,
334cdf0e10cSrcweir String(SW_RES(STR_LDOTS)));
335cdf0e10cSrcweir aString += String(SW_RES(STR_END_QUOTE));
336cdf0e10cSrcweir
337cdf0e10cSrcweir aResult.AddRule(UNDO_ARG1, aString);
338cdf0e10cSrcweir
339cdf0e10cSrcweir return aResult;
340cdf0e10cSrcweir }
341cdf0e10cSrcweir
342cdf0e10cSrcweir //------------------------------------------------------------
343cdf0e10cSrcweir
344cdf0e10cSrcweir struct _UndoTransliterate_Data
345cdf0e10cSrcweir {
346cdf0e10cSrcweir String sText;
347cdf0e10cSrcweir SwHistory* pHistory;
348cdf0e10cSrcweir Sequence< sal_Int32 >* pOffsets;
349cdf0e10cSrcweir sal_uLong nNdIdx;
350cdf0e10cSrcweir xub_StrLen nStart, nLen;
351cdf0e10cSrcweir
_UndoTransliterate_Data_UndoTransliterate_Data352cdf0e10cSrcweir _UndoTransliterate_Data( sal_uLong nNd, xub_StrLen nStt, xub_StrLen nStrLen, const String& rTxt )
353cdf0e10cSrcweir : sText( rTxt ), pHistory( 0 ), pOffsets( 0 ),
354cdf0e10cSrcweir nNdIdx( nNd ), nStart( nStt ), nLen( nStrLen )
355cdf0e10cSrcweir {}
~_UndoTransliterate_Data_UndoTransliterate_Data356cdf0e10cSrcweir ~_UndoTransliterate_Data() { delete pOffsets; delete pHistory; }
357cdf0e10cSrcweir
358cdf0e10cSrcweir void SetChangeAtNode( SwDoc& rDoc );
359cdf0e10cSrcweir };
360cdf0e10cSrcweir
SwUndoTransliterate(const SwPaM & rPam,const utl::TransliterationWrapper & rTrans)361cdf0e10cSrcweir SwUndoTransliterate::SwUndoTransliterate(
362cdf0e10cSrcweir const SwPaM& rPam,
363cdf0e10cSrcweir const utl::TransliterationWrapper& rTrans )
364cdf0e10cSrcweir : SwUndo( UNDO_TRANSLITERATE ), SwUndRng( rPam ), nType( rTrans.getType() )
365cdf0e10cSrcweir {
366cdf0e10cSrcweir }
367cdf0e10cSrcweir
~SwUndoTransliterate()368cdf0e10cSrcweir SwUndoTransliterate::~SwUndoTransliterate()
369cdf0e10cSrcweir {
370cdf0e10cSrcweir for (size_t i = 0; i < aChanges.size(); ++i)
371cdf0e10cSrcweir delete aChanges[i];
372cdf0e10cSrcweir }
373cdf0e10cSrcweir
UndoImpl(::sw::UndoRedoContext & rContext)374cdf0e10cSrcweir void SwUndoTransliterate::UndoImpl(::sw::UndoRedoContext & rContext)
375cdf0e10cSrcweir {
376cdf0e10cSrcweir SwDoc & rDoc = rContext.GetDoc();
377cdf0e10cSrcweir
378cdf0e10cSrcweir // since the changes were added to the vector from the end of the string/node towards
379cdf0e10cSrcweir // the start, we need to revert them from the start towards the end now to keep the
380cdf0e10cSrcweir // offset information of the undo data in sync with the changing text.
381cdf0e10cSrcweir // Thus we need to iterate from the end of the vector to the start
382cdf0e10cSrcweir for (sal_Int32 i = aChanges.size() - 1; i >= 0; --i)
383cdf0e10cSrcweir aChanges[i]->SetChangeAtNode( rDoc );
384cdf0e10cSrcweir
385cdf0e10cSrcweir AddUndoRedoPaM(rContext, true);
386cdf0e10cSrcweir }
387cdf0e10cSrcweir
RedoImpl(::sw::UndoRedoContext & rContext)388cdf0e10cSrcweir void SwUndoTransliterate::RedoImpl(::sw::UndoRedoContext & rContext)
389cdf0e10cSrcweir {
390cdf0e10cSrcweir SwPaM & rPam( AddUndoRedoPaM(rContext) );
391cdf0e10cSrcweir DoTransliterate(rContext.GetDoc(), rPam);
392cdf0e10cSrcweir }
393cdf0e10cSrcweir
RepeatImpl(::sw::RepeatContext & rContext)394cdf0e10cSrcweir void SwUndoTransliterate::RepeatImpl(::sw::RepeatContext & rContext)
395cdf0e10cSrcweir {
396cdf0e10cSrcweir DoTransliterate(rContext.GetDoc(), rContext.GetRepeatPaM());
397cdf0e10cSrcweir }
398cdf0e10cSrcweir
DoTransliterate(SwDoc & rDoc,SwPaM & rPam)399cdf0e10cSrcweir void SwUndoTransliterate::DoTransliterate(SwDoc & rDoc, SwPaM & rPam)
400cdf0e10cSrcweir {
401cdf0e10cSrcweir utl::TransliterationWrapper aTrans( ::comphelper::getProcessServiceFactory(), nType );
402cdf0e10cSrcweir rDoc.TransliterateText( rPam, aTrans );
403cdf0e10cSrcweir }
404cdf0e10cSrcweir
AddChanges(SwTxtNode & rTNd,xub_StrLen nStart,xub_StrLen nLen,uno::Sequence<sal_Int32> & rOffsets)405cdf0e10cSrcweir void SwUndoTransliterate::AddChanges( SwTxtNode& rTNd,
406cdf0e10cSrcweir xub_StrLen nStart, xub_StrLen nLen,
407cdf0e10cSrcweir uno::Sequence <sal_Int32>& rOffsets )
408cdf0e10cSrcweir {
409cdf0e10cSrcweir long nOffsLen = rOffsets.getLength();
410cdf0e10cSrcweir _UndoTransliterate_Data* pNew = new _UndoTransliterate_Data(
411cdf0e10cSrcweir rTNd.GetIndex(), nStart, (xub_StrLen)nOffsLen,
412cdf0e10cSrcweir rTNd.GetTxt().Copy( nStart, nLen ));
413cdf0e10cSrcweir
414cdf0e10cSrcweir aChanges.push_back( pNew );
415cdf0e10cSrcweir
416cdf0e10cSrcweir const sal_Int32* pOffsets = rOffsets.getConstArray();
417cdf0e10cSrcweir // where did we need less memory ?
418cdf0e10cSrcweir const sal_Int32* p = pOffsets;
419cdf0e10cSrcweir for( long n = 0; n < nOffsLen; ++n, ++p )
420cdf0e10cSrcweir if( *p != ( nStart + n ))
421cdf0e10cSrcweir {
422cdf0e10cSrcweir // create the Offset array
423cdf0e10cSrcweir pNew->pOffsets = new Sequence <sal_Int32> ( nLen );
424cdf0e10cSrcweir sal_Int32* pIdx = pNew->pOffsets->getArray();
425cdf0e10cSrcweir p = pOffsets;
426cdf0e10cSrcweir long nMyOff, nNewVal = nStart;
427cdf0e10cSrcweir for( n = 0, nMyOff = nStart; n < nOffsLen; ++p, ++n, ++nMyOff )
428cdf0e10cSrcweir {
429cdf0e10cSrcweir if( *p < nMyOff )
430cdf0e10cSrcweir {
431cdf0e10cSrcweir // something is deleted
432cdf0e10cSrcweir nMyOff = *p;
433cdf0e10cSrcweir *(pIdx-1) = nNewVal++;
434cdf0e10cSrcweir }
435cdf0e10cSrcweir else if( *p > nMyOff )
436cdf0e10cSrcweir {
437cdf0e10cSrcweir for( ; *p > nMyOff; ++nMyOff )
438cdf0e10cSrcweir *pIdx++ = nNewVal;
439cdf0e10cSrcweir --nMyOff;
440cdf0e10cSrcweir --n;
441cdf0e10cSrcweir --p;
442cdf0e10cSrcweir }
443cdf0e10cSrcweir else
444cdf0e10cSrcweir *pIdx++ = nNewVal++;
445cdf0e10cSrcweir }
446cdf0e10cSrcweir
447cdf0e10cSrcweir // and then we need to save the attributes/bookmarks
448cdf0e10cSrcweir // but this data must moved every time to the last in the chain!
449cdf0e10cSrcweir for (size_t i = 0; i + 1 < aChanges.size(); ++i) // check all changes but not the current one
450cdf0e10cSrcweir {
451cdf0e10cSrcweir _UndoTransliterate_Data* pD = aChanges[i];
452cdf0e10cSrcweir if( pD->nNdIdx == pNew->nNdIdx && pD->pHistory )
453cdf0e10cSrcweir {
454cdf0e10cSrcweir // same node and have a history?
455cdf0e10cSrcweir pNew->pHistory = pD->pHistory;
456cdf0e10cSrcweir pD->pHistory = 0;
457cdf0e10cSrcweir break; // more can't exist
458cdf0e10cSrcweir }
459cdf0e10cSrcweir }
460cdf0e10cSrcweir
461cdf0e10cSrcweir if( !pNew->pHistory )
462cdf0e10cSrcweir {
463cdf0e10cSrcweir pNew->pHistory = new SwHistory;
464cdf0e10cSrcweir SwRegHistory aRHst( rTNd, pNew->pHistory );
465cdf0e10cSrcweir pNew->pHistory->CopyAttr( rTNd.GetpSwpHints(),
466cdf0e10cSrcweir pNew->nNdIdx, 0, rTNd.GetTxt().Len(), false );
467cdf0e10cSrcweir }
468cdf0e10cSrcweir break;
469cdf0e10cSrcweir }
470cdf0e10cSrcweir }
471cdf0e10cSrcweir
SetChangeAtNode(SwDoc & rDoc)472cdf0e10cSrcweir void _UndoTransliterate_Data::SetChangeAtNode( SwDoc& rDoc )
473cdf0e10cSrcweir {
474cdf0e10cSrcweir SwTxtNode* pTNd = rDoc.GetNodes()[ nNdIdx ]->GetTxtNode();
475cdf0e10cSrcweir if( pTNd )
476cdf0e10cSrcweir {
477cdf0e10cSrcweir Sequence <sal_Int32> aOffsets( pOffsets ? pOffsets->getLength() : nLen );
478cdf0e10cSrcweir if( pOffsets )
479cdf0e10cSrcweir aOffsets = *pOffsets;
480cdf0e10cSrcweir else
481cdf0e10cSrcweir {
482cdf0e10cSrcweir sal_Int32* p = aOffsets.getArray();
483cdf0e10cSrcweir for( xub_StrLen n = 0; n < nLen; ++n, ++p )
484cdf0e10cSrcweir *p = n + nStart;
485cdf0e10cSrcweir }
486cdf0e10cSrcweir pTNd->ReplaceTextOnly( nStart, nLen, sText, aOffsets );
487cdf0e10cSrcweir
488cdf0e10cSrcweir if( pHistory )
489cdf0e10cSrcweir {
490cdf0e10cSrcweir if( pTNd->GetpSwpHints() )
491cdf0e10cSrcweir pTNd->ClearSwpHintsArr( false );
492cdf0e10cSrcweir pHistory->TmpRollback( &rDoc, 0, false );
493cdf0e10cSrcweir pHistory->SetTmpEnd( pHistory->Count() );
494cdf0e10cSrcweir }
495cdf0e10cSrcweir }
496cdf0e10cSrcweir }
497