Line data Source code
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 <hintids.hxx>
21 :
22 : #include <svx/svxids.hrc>
23 : #include <editeng/langitem.hxx>
24 : #include <fmtinfmt.hxx>
25 : #include <txtatr.hxx>
26 : #include <txtinet.hxx>
27 : #include <editsh.hxx>
28 : #include <doc.hxx>
29 : #include <pam.hxx>
30 : #include <ndtxt.hxx>
31 : #include <acorrect.hxx>
32 : #include <shellio.hxx>
33 : #include <swundo.hxx>
34 : #include <viscrs.hxx>
35 :
36 : #include <editeng/acorrcfg.hxx>
37 :
38 : using namespace ::com::sun::star;
39 :
40 :
41 : class _PaMIntoCrsrShellRing
42 : {
43 : SwCrsrShell& rSh;
44 : SwPaM &rDelPam, &rCrsr;
45 : Ring *pPrevDelPam, *pPrevCrsr;
46 :
47 : void RemoveFromRing( SwPaM& rPam, Ring* pPrev );
48 : public:
49 : _PaMIntoCrsrShellRing( SwCrsrShell& rSh, SwPaM& rCrsr, SwPaM& rPam );
50 : ~_PaMIntoCrsrShellRing();
51 : };
52 :
53 0 : _PaMIntoCrsrShellRing::_PaMIntoCrsrShellRing( SwCrsrShell& rCSh,
54 : SwPaM& rShCrsr, SwPaM& rPam )
55 0 : : rSh( rCSh ), rDelPam( rPam ), rCrsr( rShCrsr )
56 : {
57 0 : SwPaM* pShCrsr = rSh._GetCrsr();
58 :
59 0 : pPrevDelPam = rDelPam.GetPrev();
60 0 : pPrevCrsr = rCrsr.GetPrev();
61 :
62 0 : rDelPam.MoveRingTo( pShCrsr );
63 0 : rCrsr.MoveRingTo( pShCrsr );
64 0 : }
65 0 : _PaMIntoCrsrShellRing::~_PaMIntoCrsrShellRing()
66 : {
67 : // und den Pam wieder herausnehmen:
68 0 : RemoveFromRing( rDelPam, pPrevDelPam );
69 0 : RemoveFromRing( rCrsr, pPrevCrsr );
70 0 : }
71 0 : void _PaMIntoCrsrShellRing::RemoveFromRing( SwPaM& rPam, Ring* pPrev )
72 : {
73 0 : Ring *p, *pNext = (Ring*)&rPam;
74 0 : do {
75 0 : p = pNext;
76 0 : pNext = p->GetNext();
77 0 : p->MoveTo( &rPam );
78 : } while( p != pPrev );
79 0 : }
80 :
81 :
82 0 : SwAutoCorrDoc::SwAutoCorrDoc( SwEditShell& rEditShell, SwPaM& rPam,
83 : sal_Unicode cIns )
84 : : rEditSh( rEditShell ), rCrsr( rPam ), pIdx( 0 )
85 : , m_nEndUndoCounter(0)
86 0 : , bUndoIdInitialized( cIns ? false : true )
87 : {
88 0 : }
89 :
90 :
91 0 : SwAutoCorrDoc::~SwAutoCorrDoc()
92 : {
93 0 : for (int i = 0; i < m_nEndUndoCounter; ++i)
94 : {
95 0 : rEditSh.EndUndo();
96 : }
97 0 : delete pIdx;
98 0 : }
99 :
100 0 : void SwAutoCorrDoc::DeleteSel( SwPaM& rDelPam )
101 : {
102 0 : SwDoc* pDoc = rEditSh.GetDoc();
103 0 : if( pDoc->IsAutoFmtRedline() )
104 : {
105 : // damit der DelPam auch verschoben wird, in den Shell-Cursr-Ring
106 : // mit aufnehmen !!
107 0 : _PaMIntoCrsrShellRing aTmp( rEditSh, rCrsr, rDelPam );
108 0 : pDoc->DeleteAndJoin( rDelPam );
109 : }
110 : else
111 : {
112 0 : pDoc->DeleteRange( rDelPam );
113 : }
114 0 : }
115 :
116 0 : sal_Bool SwAutoCorrDoc::Delete( xub_StrLen nStt, xub_StrLen nEnd )
117 : {
118 0 : const SwNodeIndex& rNd = rCrsr.GetPoint()->nNode;
119 0 : SwPaM aSel( rNd, nStt, rNd, nEnd );
120 0 : DeleteSel( aSel );
121 :
122 0 : if( bUndoIdInitialized )
123 0 : bUndoIdInitialized = true;
124 0 : return sal_True;
125 : }
126 :
127 :
128 0 : sal_Bool SwAutoCorrDoc::Insert( xub_StrLen nPos, const String& rTxt )
129 : {
130 0 : SwPaM aPam( rCrsr.GetPoint()->nNode.GetNode(), nPos );
131 0 : rEditSh.GetDoc()->InsertString( aPam, rTxt );
132 0 : if( !bUndoIdInitialized )
133 : {
134 0 : bUndoIdInitialized = true;
135 0 : if( 1 == rTxt.Len() )
136 : {
137 0 : rEditSh.StartUndo( UNDO_AUTOCORRECT );
138 0 : ++m_nEndUndoCounter;
139 : }
140 : }
141 0 : return sal_True;
142 : }
143 :
144 :
145 0 : sal_Bool SwAutoCorrDoc::Replace( xub_StrLen nPos, const String& rTxt )
146 : {
147 0 : return ReplaceRange( nPos, rTxt.Len(), rTxt );
148 : }
149 0 : sal_Bool SwAutoCorrDoc::ReplaceRange( xub_StrLen nPos, xub_StrLen nSourceLength, const String& rTxt )
150 : {
151 0 : SwPaM* pPam = &rCrsr;
152 0 : if( pPam->GetPoint()->nContent.GetIndex() != nPos )
153 : {
154 0 : pPam = new SwPaM( *rCrsr.GetPoint() );
155 0 : pPam->GetPoint()->nContent = nPos;
156 : }
157 :
158 0 : SwTxtNode * const pNd = pPam->GetNode()->GetTxtNode();
159 0 : if ( !pNd )
160 : {
161 0 : return sal_False;
162 : }
163 :
164 : // text attributes with dummy characters must not be replaced!
165 0 : bool bDoReplace = true;
166 0 : xub_StrLen const nLen = rTxt.Len();
167 0 : for ( xub_StrLen n = 0; n < nLen; ++n )
168 : {
169 0 : sal_Unicode const Char = pNd->GetTxt().GetChar( n + nPos );
170 0 : if ( ( CH_TXTATR_BREAKWORD == Char || CH_TXTATR_INWORD == Char )
171 0 : && pNd->GetTxtAttrForCharAt( n + nPos ) )
172 : {
173 0 : bDoReplace = false;
174 0 : break;
175 : }
176 : }
177 :
178 0 : if ( bDoReplace )
179 : {
180 0 : SwDoc* pDoc = rEditSh.GetDoc();
181 :
182 0 : if( pDoc->IsAutoFmtRedline() )
183 : {
184 0 : if( nPos == pNd->GetTxt().Len() ) // am Ende erfolgt ein Insert
185 : {
186 0 : pDoc->InsertString( *pPam, rTxt );
187 : }
188 : else
189 : {
190 0 : _PaMIntoCrsrShellRing aTmp( rEditSh, rCrsr, *pPam );
191 :
192 0 : pPam->SetMark();
193 0 : pPam->GetPoint()->nContent = Min( pNd->GetTxt().Len(),
194 0 : xub_StrLen( nPos + nSourceLength ));
195 0 : pDoc->ReplaceRange( *pPam, rTxt, false );
196 0 : pPam->Exchange();
197 0 : pPam->DeleteMark();
198 : }
199 : }
200 : else
201 : {
202 0 : if( nSourceLength != rTxt.Len() )
203 : {
204 0 : pPam->SetMark();
205 0 : pPam->GetPoint()->nContent = Min( pNd->GetTxt().Len(),
206 0 : xub_StrLen( nPos + nSourceLength ));
207 0 : pDoc->ReplaceRange( *pPam, rTxt, false );
208 0 : pPam->Exchange();
209 0 : pPam->DeleteMark();
210 : }
211 : else
212 0 : pDoc->Overwrite( *pPam, rTxt );
213 : }
214 :
215 : // pDoc->SetRedlineMode_intern( eOld );
216 0 : if( bUndoIdInitialized )
217 : {
218 0 : bUndoIdInitialized = true;
219 0 : if( 1 == rTxt.Len() )
220 : {
221 0 : rEditSh.StartUndo( UNDO_AUTOCORRECT );
222 0 : ++m_nEndUndoCounter;
223 : }
224 : }
225 : }
226 :
227 0 : if( pPam != &rCrsr )
228 0 : delete pPam;
229 :
230 0 : return sal_True;
231 : }
232 :
233 :
234 :
235 0 : sal_Bool SwAutoCorrDoc::SetAttr( xub_StrLen nStt, xub_StrLen nEnd, sal_uInt16 nSlotId,
236 : SfxPoolItem& rItem )
237 : {
238 0 : const SwNodeIndex& rNd = rCrsr.GetPoint()->nNode;
239 0 : SwPaM aPam( rNd, nStt, rNd, nEnd );
240 :
241 0 : SfxItemPool& rPool = rEditSh.GetDoc()->GetAttrPool();
242 0 : sal_uInt16 nWhich = rPool.GetWhich( nSlotId, sal_False );
243 0 : if( nWhich )
244 : {
245 0 : rItem.SetWhich( nWhich );
246 :
247 0 : SfxItemSet aSet( rPool, aCharFmtSetRange );
248 0 : SetAllScriptItem( aSet, rItem );
249 :
250 0 : rEditSh.GetDoc()->SetFmtItemByAutoFmt( aPam, aSet );
251 :
252 0 : if( bUndoIdInitialized )
253 0 : bUndoIdInitialized = true;
254 : }
255 0 : return 0 != nWhich;
256 : }
257 :
258 :
259 :
260 0 : sal_Bool SwAutoCorrDoc::SetINetAttr( xub_StrLen nStt, xub_StrLen nEnd, const String& rURL )
261 : {
262 0 : const SwNodeIndex& rNd = rCrsr.GetPoint()->nNode;
263 0 : SwPaM aPam( rNd, nStt, rNd, nEnd );
264 :
265 0 : SfxItemSet aSet( rEditSh.GetDoc()->GetAttrPool(),
266 0 : RES_TXTATR_INETFMT, RES_TXTATR_INETFMT );
267 0 : aSet.Put( SwFmtINetFmt( rURL, aEmptyStr ));
268 0 : rEditSh.GetDoc()->SetFmtItemByAutoFmt( aPam, aSet );
269 0 : if( bUndoIdInitialized )
270 0 : bUndoIdInitialized = true;
271 0 : return sal_True;
272 : }
273 :
274 : // returne den Text eines vorherigen Absatzes.
275 : // Dieser darf nicht leer sein!
276 : // Gibt es diesen nicht oder gibt es davor nur Leere, dann returne 0
277 : // Das Flag gibt an:
278 : // sal_True: den, vor der normalen Einfuegeposition (sal_True)
279 : // sal_False: den, in den das korrigierte Wort eingfuegt wurde.
280 : // (Muss nicht der gleiche Absatz sein!!!!)
281 0 : const String* SwAutoCorrDoc::GetPrevPara( sal_Bool bAtNormalPos )
282 : {
283 0 : const String* pStr = 0;
284 :
285 0 : if( bAtNormalPos || !pIdx )
286 0 : pIdx = new SwNodeIndex( rCrsr.GetPoint()->nNode, -1 );
287 : else
288 0 : (*pIdx)--;
289 :
290 0 : SwTxtNode* pTNd = pIdx->GetNode().GetTxtNode();
291 0 : while( pTNd && !pTNd->GetTxt().Len() )
292 : {
293 0 : (*pIdx)--;
294 0 : pTNd = pIdx->GetNode().GetTxtNode();
295 : }
296 0 : if( pTNd && 0 == pTNd->GetAttrOutlineLevel() )//#outline level,zhaojianwei
297 0 : pStr = &pTNd->GetTxt();
298 :
299 0 : if( bUndoIdInitialized )
300 0 : bUndoIdInitialized = true;
301 0 : return pStr;
302 : }
303 :
304 :
305 0 : sal_Bool SwAutoCorrDoc::ChgAutoCorrWord( xub_StrLen & rSttPos, xub_StrLen nEndPos,
306 : SvxAutoCorrect& rACorrect,
307 : const String** ppPara )
308 : {
309 0 : if( bUndoIdInitialized )
310 0 : bUndoIdInitialized = true;
311 :
312 : // Absatz-Anfang oder ein Blank gefunden, suche nach dem Wort
313 : // Kuerzel im Auto
314 0 : SwTxtNode* pTxtNd = rCrsr.GetNode()->GetTxtNode();
315 : OSL_ENSURE( pTxtNd, "wo ist denn der TextNode?" );
316 :
317 0 : sal_Bool bRet = sal_False;
318 0 : if( nEndPos == rSttPos )
319 0 : return bRet;
320 :
321 0 : LanguageType eLang = GetLanguage(nEndPos, sal_False);
322 0 : if(LANGUAGE_SYSTEM == eLang)
323 0 : eLang = GetAppLanguage();
324 :
325 : //JP 22.04.99: Bug 63883 - Sonderbehandlung fuer Punkte.
326 0 : bool bLastCharIsPoint = nEndPos < pTxtNd->GetTxt().Len() &&
327 0 : '.' == pTxtNd->GetTxt().GetChar( nEndPos );
328 :
329 : const SvxAutocorrWord* pFnd = rACorrect.SearchWordsInList(
330 0 : pTxtNd->GetTxt(), rSttPos, nEndPos, *this, eLang );
331 0 : SwDoc* pDoc = rEditSh.GetDoc();
332 0 : if( pFnd )
333 : {
334 0 : const SwNodeIndex& rNd = rCrsr.GetPoint()->nNode;
335 0 : SwPaM aPam( rNd, rSttPos, rNd, nEndPos );
336 :
337 0 : if( pFnd->IsTextOnly() )
338 : {
339 : //JP 22.04.99: Bug 63883 - Sonderbehandlung fuer Punkte.
340 0 : if( !bLastCharIsPoint || !pFnd->GetLong().Len() ||
341 0 : '.' != pFnd->GetLong().GetChar( pFnd->GetLong().Len() - 1 ) )
342 : {
343 : // replace the selection
344 0 : pDoc->ReplaceRange( aPam, pFnd->GetLong(), false);
345 0 : bRet = sal_True;
346 : }
347 : }
348 : else
349 : {
350 0 : SwTextBlocks aTBlks( rACorrect.GetAutoCorrFileName( eLang, sal_False, sal_True ));
351 0 : sal_uInt16 nPos = aTBlks.GetIndex( pFnd->GetShort() );
352 0 : if( USHRT_MAX != nPos && aTBlks.BeginGetDoc( nPos ) )
353 : {
354 0 : DeleteSel( aPam );
355 0 : pDoc->DontExpandFmt( *aPam.GetPoint() );
356 :
357 0 : if( ppPara )
358 : {
359 : OSL_ENSURE( !pIdx, "wer hat seinen Index nicht geloescht?" );
360 0 : pIdx = new SwNodeIndex( rCrsr.GetPoint()->nNode, -1 );
361 : }
362 :
363 : //
364 0 : SwDoc* pAutoDoc = aTBlks.GetDoc();
365 0 : SwNodeIndex aSttIdx( pAutoDoc->GetNodes().GetEndOfExtras(), 1 );
366 0 : SwCntntNode* pCntntNd = pAutoDoc->GetNodes().GoNext( &aSttIdx );
367 0 : SwPaM aCpyPam( aSttIdx );
368 :
369 0 : const SwTableNode* pTblNd = pCntntNd->FindTableNode();
370 0 : if( pTblNd )
371 : {
372 0 : aCpyPam.GetPoint()->nContent.Assign( 0, 0 );
373 0 : aCpyPam.GetPoint()->nNode = *pTblNd;
374 : }
375 0 : aCpyPam.SetMark();
376 :
377 : // dann bis zum Ende vom Nodes Array
378 0 : aCpyPam.GetPoint()->nNode.Assign( pAutoDoc->GetNodes().GetEndOfContent(), -1 );
379 0 : pCntntNd = aCpyPam.GetCntntNode();
380 0 : aCpyPam.GetPoint()->nContent.Assign( pCntntNd, pCntntNd->Len() );
381 :
382 0 : SwDontExpandItem aExpItem;
383 0 : aExpItem.SaveDontExpandItems( *aPam.GetPoint() );
384 :
385 0 : pAutoDoc->CopyRange( aCpyPam, *aPam.GetPoint(), false );
386 :
387 0 : aExpItem.RestoreDontExpandItems( *aPam.GetPoint() );
388 :
389 0 : if( ppPara )
390 : {
391 0 : ++(*pIdx);
392 0 : pTxtNd = pIdx->GetNode().GetTxtNode();
393 : }
394 0 : bRet = sal_True;
395 : }
396 0 : aTBlks.EndGetDoc();
397 0 : }
398 : }
399 :
400 0 : if( bRet && ppPara && pTxtNd )
401 0 : *ppPara = &pTxtNd->GetTxt();
402 :
403 0 : return bRet;
404 : }
405 :
406 :
407 : // wird nach dem austauschen der Zeichen von den Funktionen
408 : // - FnCptlSttWrd
409 : // - FnCptlSttSntnc
410 : // gerufen. Dann koennen die Worte ggfs. in die Ausnahmelisten
411 : // aufgenommen werden.
412 0 : void SwAutoCorrDoc::SaveCpltSttWord( sal_uLong nFlag, xub_StrLen nPos,
413 : const String& rExceptWord,
414 : sal_Unicode cChar )
415 : {
416 0 : sal_uLong nNode = pIdx ? pIdx->GetIndex() : rCrsr.GetPoint()->nNode.GetIndex();
417 0 : LanguageType eLang = GetLanguage(nPos, sal_False);
418 : rEditSh.GetDoc()->SetAutoCorrExceptWord( new SwAutoCorrExceptWord( nFlag,
419 0 : nNode, nPos, rExceptWord, cChar, eLang ));
420 0 : }
421 :
422 0 : LanguageType SwAutoCorrDoc::GetLanguage( xub_StrLen nPos, sal_Bool bPrevPara ) const
423 : {
424 0 : LanguageType eRet = LANGUAGE_SYSTEM;
425 :
426 : SwTxtNode* pNd = (( bPrevPara && pIdx )
427 : ? *pIdx
428 0 : : rCrsr.GetPoint()->nNode ).GetNode().GetTxtNode();
429 :
430 0 : if( pNd )
431 0 : eRet = pNd->GetLang( nPos, 0 );
432 0 : if(LANGUAGE_SYSTEM == eRet)
433 0 : eRet = GetAppLanguage();
434 0 : return eRet;
435 : }
436 :
437 0 : void SwAutoCorrExceptWord::CheckChar( const SwPosition& rPos, sal_Unicode cChr )
438 : {
439 : // nur testen ob es eine Verbesserung ist. Wenn ja, dann das Wort
440 : // in die Ausnahmeliste aufnehmen.
441 0 : if( cChar == cChr && rPos.nNode.GetIndex() == nNode &&
442 0 : rPos.nContent.GetIndex() == nCntnt )
443 : {
444 : // die akt. Autokorrektur besorgen:
445 0 : SvxAutoCorrect* pACorr = SvxAutoCorrCfg::Get().GetAutoCorrect();
446 :
447 : // dann in die Liste aufnehmen:
448 0 : if( CptlSttWrd & nFlags )
449 0 : pACorr->AddWrtSttException( sWord, eLanguage );
450 0 : else if( CptlSttSntnc & nFlags )
451 0 : pACorr->AddCplSttException( sWord, eLanguage );
452 : }
453 0 : }
454 :
455 :
456 0 : sal_Bool SwAutoCorrExceptWord::CheckDelChar( const SwPosition& rPos )
457 : {
458 0 : sal_Bool bRet = sal_False;
459 0 : if( !bDeleted && rPos.nNode.GetIndex() == nNode &&
460 0 : rPos.nContent.GetIndex() == nCntnt )
461 0 : bDeleted = bRet = sal_True;
462 0 : return bRet;
463 : }
464 :
465 0 : SwDontExpandItem::~SwDontExpandItem()
466 : {
467 0 : delete pDontExpItems;
468 0 : }
469 :
470 0 : void SwDontExpandItem::SaveDontExpandItems( const SwPosition& rPos )
471 : {
472 0 : const SwTxtNode* pTxtNd = rPos.nNode.GetNode().GetTxtNode();
473 0 : if( pTxtNd )
474 : {
475 0 : pDontExpItems = new SfxItemSet( ((SwDoc*)pTxtNd->GetDoc())->GetAttrPool(),
476 0 : aCharFmtSetRange );
477 0 : xub_StrLen n = rPos.nContent.GetIndex();
478 0 : if( !pTxtNd->GetAttr( *pDontExpItems, n, n,
479 0 : n != pTxtNd->GetTxt().Len() ))
480 0 : delete pDontExpItems, pDontExpItems = 0;
481 : }
482 0 : }
483 :
484 0 : void SwDontExpandItem::RestoreDontExpandItems( const SwPosition& rPos )
485 : {
486 0 : SwTxtNode* pTxtNd = rPos.nNode.GetNode().GetTxtNode();
487 0 : if( pTxtNd )
488 : {
489 0 : xub_StrLen nStart = rPos.nContent.GetIndex();
490 0 : if( nStart == pTxtNd->GetTxt().Len() )
491 0 : pTxtNd->FmtToTxtAttr( pTxtNd );
492 :
493 0 : if( pTxtNd->GetpSwpHints() && pTxtNd->GetpSwpHints()->Count() )
494 : {
495 0 : const sal_uInt16 nSize = pTxtNd->GetpSwpHints()->Count();
496 : sal_uInt16 n;
497 : xub_StrLen nAttrStart;
498 : const xub_StrLen* pAttrEnd;
499 :
500 0 : for( n = 0; n < nSize; ++n )
501 : {
502 0 : SwTxtAttr* pHt = pTxtNd->GetpSwpHints()->GetTextHint( n );
503 0 : nAttrStart = *pHt->GetStart();
504 0 : if( nAttrStart > nStart ) // ueber den Bereich hinaus
505 0 : break;
506 :
507 0 : if( 0 != ( pAttrEnd = pHt->GetEnd() ) &&
508 : ( ( nAttrStart < nStart &&
509 0 : ( pHt->DontExpand() ? nStart < *pAttrEnd
510 : : nStart <= *pAttrEnd )) ||
511 : ( nStart == nAttrStart &&
512 : ( nAttrStart == *pAttrEnd || !nStart ))) )
513 : {
514 : const SfxPoolItem* pItem;
515 0 : if( !pDontExpItems || SFX_ITEM_SET != pDontExpItems->
516 0 : GetItemState( pHt->Which(), sal_False, &pItem ) ||
517 0 : *pItem != pHt->GetAttr() )
518 : {
519 : // das Attribut war vorher nicht in dieser Form im Absatz
520 : // gesetzt, also kann es nur durchs einfuegen/kopieren erzeugt
521 : // worden sein. Damit ist es ein Kandiadat fuers DontExpand
522 0 : pHt->SetDontExpand( sal_True );
523 : }
524 : }
525 : }
526 : }
527 : }
528 0 : }
529 :
530 :
531 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|