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 : class _PaMIntoCrsrShellRing
41 : {
42 : SwCrsrShell& rSh;
43 : SwPaM &rDelPam, &rCrsr;
44 : SwPaM* pPrevDelPam;
45 : SwPaM* pPrevCrsr;
46 :
47 : static void RemoveFromRing( SwPaM& rPam, SwPaM* 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.GetRingContainer().merge( pShCrsr->GetRingContainer() );
63 0 : rCrsr.GetRingContainer().merge( pShCrsr->GetRingContainer() );
64 0 : }
65 :
66 0 : _PaMIntoCrsrShellRing::~_PaMIntoCrsrShellRing()
67 : {
68 : // and take out the Pam again:
69 0 : RemoveFromRing( rDelPam, pPrevDelPam );
70 0 : RemoveFromRing( rCrsr, pPrevCrsr );
71 0 : }
72 :
73 0 : void _PaMIntoCrsrShellRing::RemoveFromRing( SwPaM& rPam, SwPaM* pPrev )
74 : {
75 : SwPaM* p;
76 0 : SwPaM* pNext = &rPam;
77 0 : do {
78 0 : p = pNext;
79 0 : pNext = p->GetNext();
80 0 : p->MoveTo( &rPam );
81 : } while( p != pPrev );
82 0 : }
83 :
84 3 : SwAutoCorrDoc::SwAutoCorrDoc( SwEditShell& rEditShell, SwPaM& rPam,
85 : sal_Unicode cIns )
86 : : rEditSh( rEditShell ), rCrsr( rPam ), pIdx( 0 )
87 : , m_nEndUndoCounter(0)
88 3 : , bUndoIdInitialized( cIns == 0 )
89 : {
90 3 : }
91 :
92 6 : SwAutoCorrDoc::~SwAutoCorrDoc()
93 : {
94 7 : for (int i = 0; i < m_nEndUndoCounter; ++i)
95 : {
96 4 : rEditSh.EndUndo();
97 : }
98 3 : delete pIdx;
99 3 : }
100 :
101 2 : void SwAutoCorrDoc::DeleteSel( SwPaM& rDelPam )
102 : {
103 2 : SwDoc* pDoc = rEditSh.GetDoc();
104 2 : if( pDoc->IsAutoFormatRedline() )
105 : {
106 : // so that also the DelPam be moved, include it in the
107 : // Shell-Cursr-Ring !!
108 0 : _PaMIntoCrsrShellRing aTmp( rEditSh, rCrsr, rDelPam );
109 0 : pDoc->getIDocumentContentOperations().DeleteAndJoin( rDelPam );
110 : }
111 : else
112 : {
113 2 : pDoc->getIDocumentContentOperations().DeleteRange( rDelPam );
114 : }
115 2 : }
116 :
117 0 : bool SwAutoCorrDoc::Delete( sal_Int32 nStt, sal_Int32 nEnd )
118 : {
119 0 : const SwNodeIndex& rNd = rCrsr.GetPoint()->nNode;
120 0 : SwPaM aSel( rNd, nStt, rNd, nEnd );
121 0 : DeleteSel( aSel );
122 :
123 0 : if( bUndoIdInitialized )
124 0 : bUndoIdInitialized = true;
125 0 : return true;
126 : }
127 :
128 3 : bool SwAutoCorrDoc::Insert( sal_Int32 nPos, const OUString& rText )
129 : {
130 3 : SwPaM aPam( rCrsr.GetPoint()->nNode.GetNode(), nPos );
131 3 : rEditSh.GetDoc()->getIDocumentContentOperations().InsertString( aPam, rText );
132 3 : if( !bUndoIdInitialized )
133 : {
134 3 : bUndoIdInitialized = true;
135 3 : if( 1 == rText.getLength() )
136 : {
137 3 : rEditSh.StartUndo( UNDO_AUTOCORRECT );
138 3 : ++m_nEndUndoCounter;
139 : }
140 : }
141 3 : return true;
142 : }
143 :
144 0 : bool SwAutoCorrDoc::Replace( sal_Int32 nPos, const OUString& rText )
145 : {
146 0 : return ReplaceRange( nPos, rText.getLength(), rText );
147 : }
148 :
149 1 : bool SwAutoCorrDoc::ReplaceRange( sal_Int32 nPos, sal_Int32 nSourceLength, const OUString& rText )
150 : {
151 1 : SwPaM* pPam = &rCrsr;
152 1 : if( pPam->GetPoint()->nContent.GetIndex() != nPos )
153 : {
154 1 : pPam = new SwPaM( *rCrsr.GetPoint() );
155 1 : pPam->GetPoint()->nContent = nPos;
156 : }
157 :
158 1 : SwTextNode * const pNd = pPam->GetNode().GetTextNode();
159 1 : if ( !pNd )
160 : {
161 0 : return false;
162 : }
163 :
164 : // text attributes with dummy characters must not be replaced!
165 1 : bool bDoReplace = true;
166 1 : sal_Int32 const nLen = rText.getLength();
167 2 : for ( sal_Int32 n = 0; n < nLen; ++n )
168 : {
169 1 : sal_Unicode const Char = pNd->GetText()[n + nPos];
170 2 : if ( ( CH_TXTATR_BREAKWORD == Char || CH_TXTATR_INWORD == Char )
171 1 : && pNd->GetTextAttrForCharAt( n + nPos ) )
172 : {
173 0 : bDoReplace = false;
174 0 : break;
175 : }
176 : }
177 :
178 1 : if ( bDoReplace )
179 : {
180 1 : SwDoc* pDoc = rEditSh.GetDoc();
181 :
182 1 : if( pDoc->IsAutoFormatRedline() )
183 : {
184 0 : if (nPos == pNd->GetText().getLength()) // at the End do an Insert
185 : {
186 0 : pDoc->getIDocumentContentOperations().InsertString( *pPam, rText );
187 : }
188 : else
189 : {
190 0 : _PaMIntoCrsrShellRing aTmp( rEditSh, rCrsr, *pPam );
191 :
192 0 : pPam->SetMark();
193 0 : pPam->GetPoint()->nContent = std::min<sal_Int32>(
194 0 : pNd->GetText().getLength(), nPos + nSourceLength);
195 0 : pDoc->getIDocumentContentOperations().ReplaceRange( *pPam, rText, false );
196 0 : pPam->Exchange();
197 0 : pPam->DeleteMark();
198 : }
199 : }
200 : else
201 : {
202 1 : if( nSourceLength != rText.getLength() )
203 : {
204 0 : pPam->SetMark();
205 0 : pPam->GetPoint()->nContent = std::min<sal_Int32>(
206 0 : pNd->GetText().getLength(), nPos + nSourceLength);
207 0 : pDoc->getIDocumentContentOperations().ReplaceRange( *pPam, rText, false );
208 0 : pPam->Exchange();
209 0 : pPam->DeleteMark();
210 : }
211 : else
212 1 : pDoc->getIDocumentContentOperations().Overwrite( *pPam, rText );
213 : }
214 :
215 1 : if( bUndoIdInitialized )
216 : {
217 1 : bUndoIdInitialized = true;
218 1 : if( 1 == rText.getLength() )
219 : {
220 1 : rEditSh.StartUndo( UNDO_AUTOCORRECT );
221 1 : ++m_nEndUndoCounter;
222 : }
223 : }
224 : }
225 :
226 1 : if( pPam != &rCrsr )
227 1 : delete pPam;
228 :
229 1 : return true;
230 : }
231 :
232 0 : bool SwAutoCorrDoc::SetAttr( sal_Int32 nStt, sal_Int32 nEnd, sal_uInt16 nSlotId,
233 : SfxPoolItem& rItem )
234 : {
235 0 : const SwNodeIndex& rNd = rCrsr.GetPoint()->nNode;
236 0 : SwPaM aPam( rNd, nStt, rNd, nEnd );
237 :
238 0 : SfxItemPool& rPool = rEditSh.GetDoc()->GetAttrPool();
239 0 : sal_uInt16 nWhich = rPool.GetWhich( nSlotId, false );
240 0 : if( nWhich )
241 : {
242 0 : rItem.SetWhich( nWhich );
243 :
244 0 : SfxItemSet aSet( rPool, aCharFormatSetRange );
245 0 : SetAllScriptItem( aSet, rItem );
246 :
247 0 : rEditSh.GetDoc()->SetFormatItemByAutoFormat( aPam, aSet );
248 :
249 0 : if( bUndoIdInitialized )
250 0 : bUndoIdInitialized = true;
251 : }
252 0 : return 0 != nWhich;
253 : }
254 :
255 0 : bool SwAutoCorrDoc::SetINetAttr( sal_Int32 nStt, sal_Int32 nEnd, const OUString& rURL )
256 : {
257 0 : const SwNodeIndex& rNd = rCrsr.GetPoint()->nNode;
258 0 : SwPaM aPam( rNd, nStt, rNd, nEnd );
259 :
260 0 : SfxItemSet aSet( rEditSh.GetDoc()->GetAttrPool(),
261 0 : RES_TXTATR_INETFMT, RES_TXTATR_INETFMT );
262 0 : aSet.Put( SwFormatINetFormat( rURL, OUString() ));
263 0 : rEditSh.GetDoc()->SetFormatItemByAutoFormat( aPam, aSet );
264 0 : if( bUndoIdInitialized )
265 0 : bUndoIdInitialized = true;
266 0 : return true;
267 : }
268 :
269 : /** Return the text of a previous paragraph
270 : *
271 : * @param bAtNormalPos If <true> before the normal insert position; if <false> in which the
272 : * corrected word was inserted. (Doesn't need to be the same paragraph!)
273 : * @return text or 0, if previous paragraph does not exists or there are only blankness
274 : */
275 1 : OUString const* SwAutoCorrDoc::GetPrevPara(bool const bAtNormalPos)
276 : {
277 1 : OUString const* pStr(0);
278 :
279 1 : if( bAtNormalPos || !pIdx )
280 1 : pIdx = new SwNodeIndex( rCrsr.GetPoint()->nNode, -1 );
281 : else
282 0 : --(*pIdx);
283 :
284 1 : SwTextNode* pTNd = pIdx->GetNode().GetTextNode();
285 2 : while (pTNd && !pTNd->GetText().getLength())
286 : {
287 0 : --(*pIdx);
288 0 : pTNd = pIdx->GetNode().GetTextNode();
289 : }
290 1 : if( pTNd && 0 == pTNd->GetAttrOutlineLevel() )
291 0 : pStr = & pTNd->GetText();
292 :
293 1 : if( bUndoIdInitialized )
294 1 : bUndoIdInitialized = true;
295 :
296 1 : return pStr;
297 : }
298 :
299 3 : bool SwAutoCorrDoc::ChgAutoCorrWord( sal_Int32& rSttPos, sal_Int32 nEndPos,
300 : SvxAutoCorrect& rACorrect,
301 : OUString* pPara )
302 : {
303 3 : if( bUndoIdInitialized )
304 3 : bUndoIdInitialized = true;
305 :
306 : // Found a beginning of a paragraph or a Blank,
307 : // search for the word Kuerzel (Shortcut) in the Auto
308 3 : SwTextNode* pTextNd = rCrsr.GetNode().GetTextNode();
309 : OSL_ENSURE( pTextNd, "where is the TextNode?" );
310 :
311 3 : bool bRet = false;
312 3 : if( nEndPos == rSttPos )
313 0 : return bRet;
314 :
315 3 : LanguageType eLang = GetLanguage(nEndPos, false);
316 3 : if(LANGUAGE_SYSTEM == eLang)
317 0 : eLang = GetAppLanguage();
318 3 : LanguageTag aLanguageTag( eLang);
319 :
320 : //JP 22.04.99: Bug 63883 - Special treatment for dots.
321 6 : bool bLastCharIsPoint = nEndPos < pTextNd->GetText().getLength() &&
322 6 : ('.' == pTextNd->GetText()[nEndPos]);
323 :
324 : const SvxAutocorrWord* pFnd = rACorrect.SearchWordsInList(
325 3 : pTextNd->GetText(), rSttPos, nEndPos, *this, aLanguageTag );
326 3 : SwDoc* pDoc = rEditSh.GetDoc();
327 3 : if( pFnd )
328 : {
329 : // replace also last colon of keywords surrounded by colons (for example, ":name:")
330 3 : bool replaceLastChar = pFnd->GetShort()[0] == ':' && pFnd->GetShort().endsWith(":");
331 :
332 3 : const SwNodeIndex& rNd = rCrsr.GetPoint()->nNode;
333 3 : SwPaM aPam( rNd, rSttPos, rNd, nEndPos + (replaceLastChar ? 1 : 0) );
334 :
335 3 : if( pFnd->IsTextOnly() )
336 : {
337 : //JP 22.04.99: Bug 63883 - Special treatment for dots.
338 1 : if( !bLastCharIsPoint || pFnd->GetLong().isEmpty() ||
339 0 : '.' != pFnd->GetLong()[ pFnd->GetLong().getLength() - 1 ] )
340 : {
341 : // replace the selection
342 1 : pDoc->getIDocumentContentOperations().ReplaceRange( aPam, pFnd->GetLong(), false);
343 1 : bRet = true;
344 : }
345 : }
346 : else
347 : {
348 2 : SwTextBlocks aTBlks( rACorrect.GetAutoCorrFileName( aLanguageTag, false, true, false ));
349 2 : sal_uInt16 nPos = aTBlks.GetIndex( pFnd->GetShort() );
350 2 : if( USHRT_MAX != nPos && aTBlks.BeginGetDoc( nPos ) )
351 : {
352 2 : DeleteSel( aPam );
353 2 : pDoc->DontExpandFormat( *aPam.GetPoint() );
354 :
355 2 : if( pPara )
356 : {
357 : OSL_ENSURE( !pIdx, "who has not deleted his Index?" );
358 2 : pIdx = new SwNodeIndex( rCrsr.GetPoint()->nNode, -1 );
359 : }
360 :
361 2 : SwDoc* pAutoDoc = aTBlks.GetDoc();
362 2 : SwNodeIndex aSttIdx( pAutoDoc->GetNodes().GetEndOfExtras(), 1 );
363 2 : SwContentNode* pContentNd = pAutoDoc->GetNodes().GoNext( &aSttIdx );
364 4 : SwPaM aCpyPam( aSttIdx );
365 :
366 2 : const SwTableNode* pTableNd = pContentNd->FindTableNode();
367 2 : if( pTableNd )
368 : {
369 1 : aCpyPam.GetPoint()->nContent.Assign( 0, 0 );
370 1 : aCpyPam.GetPoint()->nNode = *pTableNd;
371 : }
372 2 : aCpyPam.SetMark();
373 :
374 : // then until the end of the Nodes Array
375 2 : aCpyPam.GetPoint()->nNode.Assign( pAutoDoc->GetNodes().GetEndOfContent(), -1 );
376 2 : pContentNd = aCpyPam.GetContentNode();
377 2 : aCpyPam.GetPoint()->nContent.Assign(
378 4 : pContentNd, (pContentNd) ? pContentNd->Len() : 0);
379 :
380 4 : SwDontExpandItem aExpItem;
381 2 : aExpItem.SaveDontExpandItems( *aPam.GetPoint() );
382 :
383 2 : pAutoDoc->getIDocumentContentOperations().CopyRange( aCpyPam, *aPam.GetPoint(), /*bCopyAll=*/false, /*bCheckPos=*/true );
384 :
385 2 : aExpItem.RestoreDontExpandItems( *aPam.GetPoint() );
386 :
387 2 : if( pPara )
388 : {
389 2 : ++(*pIdx);
390 2 : pTextNd = pIdx->GetNode().GetTextNode();
391 : }
392 4 : bRet = true;
393 : }
394 2 : aTBlks.EndGetDoc();
395 3 : }
396 : }
397 :
398 3 : if( bRet && pPara && pTextNd )
399 3 : *pPara = pTextNd->GetText();
400 :
401 3 : return bRet;
402 : }
403 :
404 : // Called by the functions:
405 : // - FnCapitalStartWord
406 : // - FnCapitalStartSentence
407 : // after the exchange of characters. Then the words, if necessary, can be inserted
408 : // into the exception list.
409 0 : void SwAutoCorrDoc::SaveCpltSttWord( sal_uLong nFlag, sal_Int32 nPos,
410 : const OUString& rExceptWord,
411 : sal_Unicode cChar )
412 : {
413 0 : sal_uLong nNode = pIdx ? pIdx->GetIndex() : rCrsr.GetPoint()->nNode.GetIndex();
414 0 : LanguageType eLang = GetLanguage(nPos, false);
415 : rEditSh.GetDoc()->SetAutoCorrExceptWord( new SwAutoCorrExceptWord( nFlag,
416 0 : nNode, nPos, rExceptWord, cChar, eLang ));
417 0 : }
418 :
419 6 : LanguageType SwAutoCorrDoc::GetLanguage( sal_Int32 nPos, bool bPrevPara ) const
420 : {
421 6 : LanguageType eRet = LANGUAGE_SYSTEM;
422 :
423 0 : SwTextNode* pNd = (( bPrevPara && pIdx )
424 : ? *pIdx
425 6 : : rCrsr.GetPoint()->nNode ).GetNode().GetTextNode();
426 :
427 6 : if( pNd )
428 6 : eRet = pNd->GetLang( nPos, 0 );
429 6 : if(LANGUAGE_SYSTEM == eRet)
430 0 : eRet = GetAppLanguage();
431 6 : return eRet;
432 : }
433 :
434 0 : void SwAutoCorrExceptWord::CheckChar( const SwPosition& rPos, sal_Unicode cChr )
435 : {
436 : // test only if this is a improvement.
437 : // If yes, then add the word to the list.
438 0 : if (m_cChar == cChr && rPos.nNode.GetIndex() == m_nNode && rPos.nContent.GetIndex() == m_nContent)
439 : {
440 : // get the current autocorrection:
441 0 : SvxAutoCorrect* pACorr = SvxAutoCorrCfg::Get().GetAutoCorrect();
442 :
443 : // then add to the list:
444 0 : if (CapitalStartWord & m_nFlags)
445 0 : pACorr->AddWrtSttException(m_sWord, m_eLanguage);
446 0 : else if (CapitalStartSentence & m_nFlags)
447 0 : pACorr->AddCplSttException(m_sWord, m_eLanguage);
448 : }
449 0 : }
450 :
451 0 : bool SwAutoCorrExceptWord::CheckDelChar( const SwPosition& rPos )
452 : {
453 0 : bool bRet = false;
454 0 : if (!m_bDeleted && rPos.nNode.GetIndex() == m_nNode && rPos.nContent.GetIndex() == m_nContent)
455 0 : m_bDeleted = bRet = true;
456 0 : return bRet;
457 : }
458 :
459 6 : SwDontExpandItem::~SwDontExpandItem()
460 : {
461 6 : delete pDontExpItems;
462 6 : }
463 :
464 6 : void SwDontExpandItem::SaveDontExpandItems( const SwPosition& rPos )
465 : {
466 6 : const SwTextNode* pTextNd = rPos.nNode.GetNode().GetTextNode();
467 6 : if( pTextNd )
468 : {
469 6 : pDontExpItems = new SfxItemSet( const_cast<SwDoc*>(pTextNd->GetDoc())->GetAttrPool(),
470 6 : aCharFormatSetRange );
471 6 : const sal_Int32 n = rPos.nContent.GetIndex();
472 6 : if( !pTextNd->GetAttr( *pDontExpItems, n, n,
473 6 : n != pTextNd->GetText().getLength() ))
474 6 : delete pDontExpItems, pDontExpItems = 0;
475 : }
476 6 : }
477 :
478 6 : void SwDontExpandItem::RestoreDontExpandItems( const SwPosition& rPos )
479 : {
480 6 : SwTextNode* pTextNd = rPos.nNode.GetNode().GetTextNode();
481 6 : if( pTextNd )
482 : {
483 6 : const sal_Int32 nStart = rPos.nContent.GetIndex();
484 6 : if( nStart == pTextNd->GetText().getLength() )
485 1 : pTextNd->FormatToTextAttr( pTextNd );
486 :
487 6 : if( pTextNd->GetpSwpHints() && pTextNd->GetpSwpHints()->Count() )
488 : {
489 1 : const size_t nSize = pTextNd->GetpSwpHints()->Count();
490 : sal_Int32 nAttrStart;
491 :
492 2 : for( size_t n = 0; n < nSize; ++n )
493 : {
494 1 : SwTextAttr* pHt = pTextNd->GetpSwpHints()->GetTextHint( n );
495 1 : nAttrStart = pHt->GetStart();
496 1 : if( nAttrStart > nStart ) // beyond the area
497 0 : break;
498 :
499 : const sal_Int32* pAttrEnd;
500 3 : if( 0 != ( pAttrEnd = pHt->End() ) &&
501 1 : ( ( nAttrStart < nStart &&
502 1 : ( pHt->DontExpand() ? nStart < *pAttrEnd
503 1 : : nStart <= *pAttrEnd )) ||
504 0 : ( nStart == nAttrStart &&
505 0 : ( nAttrStart == *pAttrEnd || !nStart ))) )
506 : {
507 : const SfxPoolItem* pItem;
508 2 : if( !pDontExpItems || SfxItemState::SET != pDontExpItems->
509 1 : GetItemState( pHt->Which(), false, &pItem ) ||
510 0 : *pItem != pHt->GetAttr() )
511 : {
512 : // The attribute was not previously set in this form in the
513 : // paragraph, so it can only be created through insert/copy
514 : // Because of that it is a candidate for DontExpand
515 1 : pHt->SetDontExpand( true );
516 : }
517 : }
518 : }
519 : }
520 : }
521 183 : }
522 :
523 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|