Clean up uses of Any::getValue() in sw
[LibreOffice.git] / sw / source / core / edit / edlingu.cxx
blobb517487a6c517f6520027fa44a26ae0665edb033
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <com/sun/star/linguistic2/ProofreadingResult.hpp>
21 #include <com/sun/star/linguistic2/XProofreader.hpp>
22 #include <com/sun/star/linguistic2/XProofreadingIterator.hpp>
23 #include <com/sun/star/text/XFlatParagraph.hpp>
24 #include <com/sun/star/i18n/ScriptType.hpp>
25 #include <comphelper/string.hxx>
26 #include <o3tl/any.hxx>
28 #include <unoflatpara.hxx>
30 #include <comcore.hrc>
31 #include <hintids.hxx>
32 #include <linguistic/lngprops.hxx>
33 #include <vcl/msgbox.hxx>
34 #include <editeng/unolingu.hxx>
35 #include <editeng/svxacorr.hxx>
36 #include <editeng/langitem.hxx>
37 #include <editeng/SpellPortions.hxx>
38 #include <editeng/scripttypeitem.hxx>
39 #include <charatr.hxx>
40 #include <editsh.hxx>
41 #include <doc.hxx>
42 #include <IDocumentUndoRedo.hxx>
43 #include <IDocumentRedlineAccess.hxx>
44 #include <rootfrm.hxx>
45 #include <pam.hxx>
46 #include <swundo.hxx>
47 #include <ndtxt.hxx>
48 #include <viewopt.hxx>
49 #include <viscrs.hxx>
50 #include <SwGrammarMarkUp.hxx>
51 #include <mdiexp.hxx>
52 #include <statstr.hrc>
53 #include <cntfrm.hxx>
54 #include <splargs.hxx>
55 #include <redline.hxx>
56 #include <docary.hxx>
57 #include <docsh.hxx>
58 #include <txatbase.hxx>
59 #include <txtfrm.hxx>
61 using namespace ::svx;
62 using namespace ::com::sun::star;
63 using namespace ::com::sun::star::uno;
64 using namespace ::com::sun::star::beans;
65 using namespace ::com::sun::star::linguistic2;
67 class SwLinguIter
69 SwEditShell *pSh;
70 SwPosition *pStart;
71 SwPosition *pEnd;
72 SwPosition *pCurr;
73 SwPosition *pCurrX;
74 sal_uInt16 nCursorCnt;
75 public:
76 SwLinguIter();
78 inline SwEditShell *GetSh() { return pSh; }
80 inline const SwPosition *GetEnd() const { return pEnd; }
81 inline void SetEnd( SwPosition* pNew ){ delete pEnd; pEnd = pNew; }
83 inline const SwPosition *GetStart() const { return pStart; }
84 inline void SetStart( SwPosition* pNew ){ delete pStart; pStart = pNew; }
86 inline const SwPosition *GetCurr() const { return pCurr; }
87 inline void SetCurr( SwPosition* pNew ){ delete pCurr; pCurr = pNew; }
89 inline const SwPosition *GetCurrX() const { return pCurrX; }
90 inline void SetCurrX( SwPosition* pNew ){ delete pCurrX; pCurrX = pNew; }
92 inline sal_uInt16& GetCursorCnt(){ return nCursorCnt; }
94 // for the UI:
95 void Start_( SwEditShell *pSh, SwDocPositions eStart,
96 SwDocPositions eEnd );
97 void End_(bool bRestoreSelection = true);
100 // #i18881# to be able to identify the positions of the changed words
101 // the content positions of each portion need to be saved
102 struct SpellContentPosition
104 sal_Int32 nLeft;
105 sal_Int32 nRight;
108 typedef std::vector<SpellContentPosition> SpellContentPositions;
110 class SwSpellIter : public SwLinguIter
112 uno::Reference< XSpellChecker1 > xSpeller;
113 svx::SpellPortions aLastPortions;
115 SpellContentPositions aLastPositions;
116 bool bBackToStartOfSentence;
117 bool bMoveToEndOfSentence;
119 void CreatePortion(uno::Reference< XSpellAlternatives > xAlt,
120 linguistic2::ProofreadingResult* pGrammarResult,
121 bool bIsField, bool bIsHidden);
123 void AddPortion(uno::Reference< XSpellAlternatives > xAlt,
124 linguistic2::ProofreadingResult* pGrammarResult,
125 const SpellContentPositions& rDeletedRedlines);
126 public:
127 SwSpellIter() :
128 bBackToStartOfSentence(false), bMoveToEndOfSentence(false) {}
130 void Start( SwEditShell *pSh, SwDocPositions eStart, SwDocPositions eEnd );
132 uno::Any Continue( sal_uInt16* pPageCnt, sal_uInt16* pPageSt );
134 bool SpellSentence(svx::SpellPortions& rPortions, bool bIsGrammarCheck);
135 void ToSentenceStart();
136 const svx::SpellPortions& GetLastPortions() const { return aLastPortions;}
137 const SpellContentPositions& GetLastPositions() const {return aLastPositions;}
138 void ContinueAfterThisSentence() { bMoveToEndOfSentence = true; }
141 /// used for text conversion
142 class SwConvIter : public SwLinguIter
144 SwConversionArgs &rArgs;
145 public:
146 explicit SwConvIter(SwConversionArgs &rConvArgs)
147 : rArgs(rConvArgs)
151 void Start( SwEditShell *pSh, SwDocPositions eStart, SwDocPositions eEnd );
153 uno::Any Continue( sal_uInt16* pPageCnt, sal_uInt16* pPageSt );
156 class SwHyphIter : public SwLinguIter
158 // With that we save a GetFrame() in Hyphenate //TODO: does it actually matter?
159 const SwTextNode *m_pLastNode;
160 SwTextFrame *m_pLastFrame;
161 friend SwTextFrame * sw::SwHyphIterCacheLastTextFrame(SwTextNode* pNode, const sw::Creator& rCreator);
163 bool bOldIdle;
164 static void DelSoftHyph( SwPaM &rPam );
166 public:
167 SwHyphIter() : m_pLastNode(nullptr), m_pLastFrame(nullptr), bOldIdle(false) {}
169 void Start( SwEditShell *pSh, SwDocPositions eStart, SwDocPositions eEnd );
170 void End();
172 void Ignore();
174 uno::Any Continue( sal_uInt16* pPageCnt, sal_uInt16* pPageSt );
176 static bool IsAuto();
177 void InsertSoftHyph( const sal_Int32 nHyphPos );
178 void ShowSelection();
181 static SwSpellIter* g_pSpellIter = nullptr;
182 static SwConvIter* g_pConvIter = nullptr;
183 static SwHyphIter* g_pHyphIter = nullptr;
185 SwLinguIter::SwLinguIter()
186 : pSh(nullptr)
187 , pStart(nullptr)
188 , pEnd(nullptr)
189 , pCurr(nullptr)
190 , pCurrX(nullptr)
191 , nCursorCnt(0)
193 // TODO missing: ensurance of re-entrance, OSL_ENSURE( etc.
196 void SwLinguIter::Start_( SwEditShell *pShell, SwDocPositions eStart,
197 SwDocPositions eEnd )
199 // TODO missing: ensurance of re-entrance, locking
200 if( pSh )
201 return;
203 bool bSetCurr;
205 pSh = pShell;
207 SET_CURR_SHELL( pSh );
209 OSL_ENSURE( !pEnd, "SwLinguIter::Start_ without End?");
211 SwPaM *pCursor = pSh->GetCursor();
213 if( pShell->HasSelection() || pCursor != pCursor->GetNext() )
215 bSetCurr = nullptr != GetCurr();
216 nCursorCnt = pSh->GetCursorCnt();
217 if( pSh->IsTableMode() )
218 pSh->TableCursorToCursor();
220 pSh->Push();
221 sal_uInt16 n;
222 for( n = 0; n < nCursorCnt; ++n )
224 pSh->Push();
225 pSh->DestroyCursor();
227 pSh->Pop( false );
229 else
231 bSetCurr = false;
232 nCursorCnt = 1;
233 pSh->Push();
234 pSh->SetLinguRange( eStart, eEnd );
237 pCursor = pSh->GetCursor();
238 if ( *pCursor->GetPoint() > *pCursor->GetMark() )
239 pCursor->Exchange();
241 pStart = new SwPosition( *pCursor->GetPoint() );
242 pEnd = new SwPosition( *pCursor->GetMark() );
243 if( bSetCurr )
245 SwPosition* pNew = new SwPosition( *GetStart() );
246 SetCurr( pNew );
247 pNew = new SwPosition( *pNew );
248 SetCurrX( pNew );
251 pCursor->SetMark();
254 void SwLinguIter::End_(bool bRestoreSelection)
256 if( !pSh )
257 return;
259 OSL_ENSURE( pEnd, "SwLinguIter::End_ without end?");
260 if(bRestoreSelection)
262 while( nCursorCnt-- )
263 pSh->Pop( false );
265 pSh->KillPams();
266 pSh->ClearMark();
268 DELETEZ(pStart);
269 DELETEZ(pEnd);
270 DELETEZ(pCurr);
271 DELETEZ(pCurrX);
273 pSh = nullptr;
276 void SwSpellIter::Start( SwEditShell *pShell, SwDocPositions eStart,
277 SwDocPositions eEnd )
279 if( GetSh() )
280 return;
282 xSpeller = ::GetSpellChecker();
283 if ( xSpeller.is() )
284 Start_( pShell, eStart, eEnd );
285 aLastPortions.clear();
286 aLastPositions.clear();
289 // This method is the origin of SwEditShell::SpellContinue()
290 uno::Any SwSpellIter::Continue( sal_uInt16* pPageCnt, sal_uInt16* pPageSt )
292 //!!
293 //!! Please check SwConvIter also when modifying this
294 //!!
296 uno::Any aSpellRet;
297 SwEditShell *pMySh = GetSh();
298 if( !pMySh )
299 return aSpellRet;
301 OSL_ENSURE( GetEnd(), "SwSpellIter::Continue without start?");
303 uno::Reference< uno::XInterface > xSpellRet;
304 bool bGoOn = true;
305 do {
306 SwPaM *pCursor = pMySh->GetCursor();
307 if ( !pCursor->HasMark() )
308 pCursor->SetMark();
310 uno::Reference< beans::XPropertySet > xProp( GetLinguPropertySet() );
311 *pMySh->GetCursor()->GetPoint() = *GetCurr();
312 *pMySh->GetCursor()->GetMark() = *GetEnd();
313 pMySh->GetDoc()->Spell(*pMySh->GetCursor(),
314 xSpeller, pPageCnt, pPageSt, false ) >>= xSpellRet;
315 bGoOn = GetCursorCnt() > 1;
316 if( xSpellRet.is() )
318 bGoOn = false;
319 SwPosition* pNewPoint = new SwPosition( *pCursor->GetPoint() );
320 SwPosition* pNewMark = new SwPosition( *pCursor->GetMark() );
321 SetCurr( pNewPoint );
322 SetCurrX( pNewMark );
324 if( bGoOn )
326 pMySh->Pop( false );
327 pCursor = pMySh->GetCursor();
328 if ( *pCursor->GetPoint() > *pCursor->GetMark() )
329 pCursor->Exchange();
330 SwPosition* pNew = new SwPosition( *pCursor->GetPoint() );
331 SetStart( pNew );
332 pNew = new SwPosition( *pCursor->GetMark() );
333 SetEnd( pNew );
334 pNew = new SwPosition( *GetStart() );
335 SetCurr( pNew );
336 pNew = new SwPosition( *pNew );
337 SetCurrX( pNew );
338 pCursor->SetMark();
339 --GetCursorCnt();
341 }while ( bGoOn );
342 aSpellRet <<= xSpellRet;
343 return aSpellRet;
346 void SwConvIter::Start( SwEditShell *pShell, SwDocPositions eStart,
347 SwDocPositions eEnd )
349 if( GetSh() )
350 return;
351 Start_( pShell, eStart, eEnd );
354 uno::Any SwConvIter::Continue( sal_uInt16* pPageCnt, sal_uInt16* pPageSt )
356 //!!
357 //!! Please check SwSpellIter also when modifying this
358 //!!
360 uno::Any aConvRet( makeAny( OUString() ) );
361 SwEditShell *pMySh = GetSh();
362 if( !pMySh )
363 return aConvRet;
365 OSL_ENSURE( GetEnd(), "SwConvIter::Continue() without Start?");
367 OUString aConvText;
368 bool bGoOn = true;
369 do {
370 SwPaM *pCursor = pMySh->GetCursor();
371 if ( !pCursor->HasMark() )
372 pCursor->SetMark();
374 *pMySh->GetCursor()->GetPoint() = *GetCurr();
375 *pMySh->GetCursor()->GetMark() = *GetEnd();
377 // call function to find next text portion to be converted
378 uno::Reference< linguistic2::XSpellChecker1 > xEmpty;
379 pMySh->GetDoc()->Spell( *pMySh->GetCursor(),
380 xEmpty, pPageCnt, pPageSt, false, &rArgs ) >>= aConvText;
382 bGoOn = GetCursorCnt() > 1;
383 if( !aConvText.isEmpty() )
385 bGoOn = false;
386 SwPosition* pNewPoint = new SwPosition( *pCursor->GetPoint() );
387 SwPosition* pNewMark = new SwPosition( *pCursor->GetMark() );
389 SetCurr( pNewPoint );
390 SetCurrX( pNewMark );
392 if( bGoOn )
394 pMySh->Pop( false );
395 pCursor = pMySh->GetCursor();
396 if ( *pCursor->GetPoint() > *pCursor->GetMark() )
397 pCursor->Exchange();
398 SwPosition* pNew = new SwPosition( *pCursor->GetPoint() );
399 SetStart( pNew );
400 pNew = new SwPosition( *pCursor->GetMark() );
401 SetEnd( pNew );
402 pNew = new SwPosition( *GetStart() );
403 SetCurr( pNew );
404 pNew = new SwPosition( *pNew );
405 SetCurrX( pNew );
406 pCursor->SetMark();
407 --GetCursorCnt();
409 }while ( bGoOn );
410 return makeAny( aConvText );
413 bool SwHyphIter::IsAuto()
415 uno::Reference< beans::XPropertySet > xProp( ::GetLinguPropertySet() );
416 return xProp.is() && *o3tl::doAccess<bool>(xProp->getPropertyValue(
417 UPN_IS_HYPH_AUTO ));
420 void SwHyphIter::ShowSelection()
422 SwEditShell *pMySh = GetSh();
423 if( pMySh )
425 pMySh->StartAction();
426 // Caution! Due to EndAction() formatting is started which can lead to the fact that new
427 // words are added to/set in the Hyphenator. Thus: save!
428 pMySh->EndAction();
432 void SwHyphIter::Start( SwEditShell *pShell, SwDocPositions eStart, SwDocPositions eEnd )
434 // robust
435 if( GetSh() || GetEnd() )
437 OSL_ENSURE( !GetSh(), "SwHyphIter::Start: missing HyphEnd()" );
438 return;
441 // nothing to do (at least not in the way as in the "else" part)
442 bOldIdle = pShell->GetViewOptions()->IsIdle();
443 pShell->GetViewOptions()->SetIdle( false );
444 Start_( pShell, eStart, eEnd );
447 // restore selections
448 void SwHyphIter::End()
450 if( !GetSh() )
451 return;
452 GetSh()->GetViewOptions()->SetIdle( bOldIdle );
453 End_();
456 uno::Any SwHyphIter::Continue( sal_uInt16* pPageCnt, sal_uInt16* pPageSt )
458 uno::Any aHyphRet;
459 SwEditShell *pMySh = GetSh();
460 if( !pMySh )
461 return aHyphRet;
463 const bool bAuto = IsAuto();
464 uno::Reference< XHyphenatedWord > xHyphWord;
465 bool bGoOn = false;
466 do {
467 SwPaM *pCursor;
468 do {
469 OSL_ENSURE( GetEnd(), "SwHyphIter::Continue without Start?" );
470 pCursor = pMySh->GetCursor();
471 if ( !pCursor->HasMark() )
472 pCursor->SetMark();
473 if ( *pCursor->GetPoint() < *pCursor->GetMark() )
475 pCursor->Exchange();
476 pCursor->SetMark();
479 if ( *pCursor->End() <= *GetEnd() )
481 *pCursor->GetMark() = *GetEnd();
483 // Do we need to break the word at the current cursor position?
484 const Point aCursorPos( pMySh->GetCharRect().Pos() );
485 xHyphWord = pMySh->GetDoc()->Hyphenate( pCursor, aCursorPos,
486 pPageCnt, pPageSt );
489 if( bAuto && xHyphWord.is() )
491 SwEditShell::InsertSoftHyph( xHyphWord->getHyphenationPos() + 1);
493 } while( bAuto && xHyphWord.is() ); //end of do-while
494 bGoOn = !xHyphWord.is() && GetCursorCnt() > 1;
496 if( bGoOn )
498 pMySh->Pop( false );
499 pCursor = pMySh->GetCursor();
500 if ( *pCursor->GetPoint() > *pCursor->GetMark() )
501 pCursor->Exchange();
502 SwPosition* pNew = new SwPosition(*pCursor->End());
503 SetEnd( pNew );
504 pCursor->SetMark();
505 --GetCursorCnt();
507 } while ( bGoOn );
508 aHyphRet <<= xHyphWord;
509 return aHyphRet;
512 /// ignore hyphenation
513 void SwHyphIter::Ignore()
515 SwEditShell *pMySh = GetSh();
516 SwPaM *pCursor = pMySh->GetCursor();
518 // delete old SoftHyphen
519 DelSoftHyph( *pCursor );
521 // and continue
522 pCursor->Start()->nContent = pCursor->End()->nContent;
523 pCursor->SetMark();
526 void SwHyphIter::DelSoftHyph( SwPaM &rPam )
528 const SwPosition* pStt = rPam.Start();
529 const sal_Int32 nStart = pStt->nContent.GetIndex();
530 const sal_Int32 nEnd = rPam.End()->nContent.GetIndex();
531 SwTextNode *pNode = pStt->nNode.GetNode().GetTextNode();
532 pNode->DelSoftHyph( nStart, nEnd );
535 void SwHyphIter::InsertSoftHyph( const sal_Int32 nHyphPos )
537 SwEditShell *pMySh = GetSh();
538 OSL_ENSURE( pMySh, "SwHyphIter::InsertSoftHyph: missing HyphStart()");
539 if( !pMySh )
540 return;
542 SwPaM *pCursor = pMySh->GetCursor();
543 SwPosition* pSttPos = pCursor->Start();
544 SwPosition* pEndPos = pCursor->End();
546 const sal_Int32 nLastHyphLen = GetEnd()->nContent.GetIndex() -
547 pSttPos->nContent.GetIndex();
549 if( pSttPos->nNode != pEndPos->nNode || !nLastHyphLen )
551 OSL_ENSURE( pSttPos->nNode == pEndPos->nNode,
552 "SwHyphIter::InsertSoftHyph: node warp during hyphenation" );
553 OSL_ENSURE(nLastHyphLen, "SwHyphIter::InsertSoftHyph: missing HyphContinue()");
554 *pSttPos = *pEndPos;
555 return;
558 pMySh->StartAction();
560 SwDoc *pDoc = pMySh->GetDoc();
561 DelSoftHyph( *pCursor );
562 pSttPos->nContent += nHyphPos;
563 SwPaM aRg( *pSttPos );
564 pDoc->getIDocumentContentOperations().InsertString( aRg, OUString(CHAR_SOFTHYPHEN) );
566 // revoke selection
567 pCursor->DeleteMark();
568 pMySh->EndAction();
569 pCursor->SetMark();
572 namespace sw {
574 SwTextFrame *
575 SwHyphIterCacheLastTextFrame(SwTextNode *const pNode,
576 const sw::Creator& create)
578 assert(g_pHyphIter);
579 if (pNode != g_pHyphIter->m_pLastNode || !g_pHyphIter->m_pLastFrame)
581 g_pHyphIter->m_pLastNode = pNode;
582 g_pHyphIter->m_pLastFrame = create();
584 return g_pHyphIter->m_pLastFrame;
589 bool SwEditShell::HasLastSentenceGotGrammarChecked()
591 bool bTextWasGrammarChecked = false;
592 if (g_pSpellIter)
594 svx::SpellPortions aLastPortions( g_pSpellIter->GetLastPortions() );
595 for (size_t i = 0; i < aLastPortions.size() && !bTextWasGrammarChecked; ++i)
597 // bIsGrammarError is also true if the text was only checked but no
598 // grammar error was found. (That is if a ProofreadingResult was obtained in
599 // SwDoc::Spell and in turn bIsGrammarError was set in SwSpellIter::CreatePortion)
600 if (aLastPortions[i].bIsGrammarError)
601 bTextWasGrammarChecked = true;
604 return bTextWasGrammarChecked;
607 bool SwEditShell::HasConvIter()
609 return nullptr != g_pConvIter;
612 bool SwEditShell::HasHyphIter()
614 return nullptr != g_pHyphIter;
617 void SwEditShell::SetLinguRange( SwDocPositions eStart, SwDocPositions eEnd )
619 SwPaM *pCursor = GetCursor();
620 MakeFindRange( static_cast<sal_uInt16>(eStart), static_cast<sal_uInt16>(eEnd), pCursor );
621 if( *pCursor->GetPoint() > *pCursor->GetMark() )
622 pCursor->Exchange();
625 void SwEditShell::SpellStart(
626 SwDocPositions eStart, SwDocPositions eEnd, SwDocPositions eCurr,
627 SwConversionArgs *pConvArgs )
629 SwLinguIter *pLinguIter = nullptr;
631 // do not spell if interactive spelling is active elsewhere
632 if (!pConvArgs && !g_pSpellIter)
634 OSL_ENSURE( !g_pSpellIter, "wer ist da schon am spellen?" );
635 g_pSpellIter = new SwSpellIter;
636 pLinguIter = g_pSpellIter;
638 // do not do text conversion if it is active elsewhere
639 if (pConvArgs && !g_pConvIter)
641 OSL_ENSURE( !g_pConvIter, "text conversion already active!" );
642 g_pConvIter = new SwConvIter( *pConvArgs );
643 pLinguIter = g_pConvIter;
646 if (pLinguIter)
648 SwCursor* pSwCursor = GetSwCursor();
650 SwPosition *pTmp = new SwPosition( *pSwCursor->GetPoint() );
651 pSwCursor->FillFindPos( eCurr, *pTmp );
652 pLinguIter->SetCurr( pTmp );
654 pTmp = new SwPosition( *pTmp );
655 pLinguIter->SetCurrX( pTmp );
658 if (!pConvArgs && g_pSpellIter)
659 g_pSpellIter->Start( this, eStart, eEnd );
660 if (pConvArgs && g_pConvIter)
661 g_pConvIter->Start( this, eStart, eEnd );
664 void SwEditShell::SpellEnd( SwConversionArgs *pConvArgs, bool bRestoreSelection )
666 if (!pConvArgs && g_pSpellIter && g_pSpellIter->GetSh() == this)
668 OSL_ENSURE( g_pSpellIter, "where is my Iterator?" );
669 g_pSpellIter->End_(bRestoreSelection);
670 delete g_pSpellIter;
671 g_pSpellIter = nullptr;
673 if (pConvArgs && g_pConvIter && g_pConvIter->GetSh() == this)
675 OSL_ENSURE( g_pConvIter, "where is my Iterator?" );
676 g_pConvIter->End_();
677 delete g_pConvIter;
678 g_pConvIter = nullptr;
682 /// @returns SPL_ return values as in splchk.hxx
683 uno::Any SwEditShell::SpellContinue(
684 sal_uInt16* pPageCnt, sal_uInt16* pPageSt,
685 SwConversionArgs *pConvArgs )
687 uno::Any aRes;
689 if ((!pConvArgs && g_pSpellIter->GetSh() != this) ||
690 ( pConvArgs && g_pConvIter->GetSh() != this))
691 return aRes;
693 if( pPageCnt && !*pPageCnt )
695 sal_uInt16 nEndPage = GetLayout()->GetPageNum();
696 nEndPage += nEndPage * 10 / 100;
697 *pPageCnt = nEndPage;
698 if( nEndPage )
699 ::StartProgress( STR_STATSTR_SPELL, 0, nEndPage, GetDoc()->GetDocShell() );
702 OSL_ENSURE( pConvArgs || g_pSpellIter, "SpellIter missing" );
703 OSL_ENSURE( !pConvArgs || g_pConvIter, "ConvIter missing" );
704 //JP 18.07.95: prevent displaying selection on error messages. NO StartAction so that all
705 // Paints are also disabled.
706 ++mnStartAction;
707 OUString aRet;
708 uno::Reference< uno::XInterface > xRet;
709 if (pConvArgs)
711 g_pConvIter->Continue( pPageCnt, pPageSt ) >>= aRet;
712 aRes <<= aRet;
714 else
716 g_pSpellIter->Continue( pPageCnt, pPageSt ) >>= xRet;
717 aRes <<= xRet;
719 --mnStartAction;
721 if( !aRet.isEmpty() || xRet.is() )
723 // then make awt::Selection again visible
724 StartAction();
725 EndAction();
727 return aRes;
730 /* Interactive Hyphenation (BP 10.03.93)
732 * 1) HyphStart
733 * - Revoke all Selections
734 * - Save current Cursor
735 * - if no selections existent:
736 * - create new selection reaching until document end
737 * 2) HyphContinue
738 * - add nLastHyphLen onto SelectionStart
739 * - iterate over all selected areas
740 * - pDoc->Hyphenate() iterates over all Nodes of a selection
741 * - pTextNode->Hyphenate() calls SwTextFrame::Hyphenate of the EditShell
742 * - SwTextFrame:Hyphenate() iterates over all rows of the Pam
743 * - LineIter::Hyphenate() sets the Hyphenator and the Pam based on
744 * the to be separated word.
745 * - Returns true if there is a hyphenation and false if the Pam is processed.
746 * - If true, show the selected word and set nLastHyphLen.
747 * - If false, delete current selection and select next one. Returns HYPH_OK if no more.
748 * 3) InsertSoftHyph (might be called by UI if needed)
749 * - Place current cursor and add attribute.
750 * 4) HyphEnd
751 * - Restore old cursor, EndAction
753 void SwEditShell::HyphStart( SwDocPositions eStart, SwDocPositions eEnd )
755 // do not hyphenate if interactive hyphenation is active elsewhere
756 if (!g_pHyphIter)
758 OSL_ENSURE( !g_pHyphIter, "who is already hyphenating?" );
759 g_pHyphIter = new SwHyphIter;
760 g_pHyphIter->Start( this, eStart, eEnd );
764 /// restore selections
765 void SwEditShell::HyphEnd()
767 if (g_pHyphIter->GetSh() == this)
769 OSL_ENSURE( g_pHyphIter, "No Iterator" );
770 g_pHyphIter->End();
771 delete g_pHyphIter;
772 g_pHyphIter = nullptr;
776 /// @returns HYPH_CONTINUE if hyphenation, HYPH_OK if selected area was processed.
777 uno::Reference< uno::XInterface >
778 SwEditShell::HyphContinue( sal_uInt16* pPageCnt, sal_uInt16* pPageSt )
780 if (g_pHyphIter->GetSh() != this)
781 return nullptr;
783 if( pPageCnt && !*pPageCnt && !*pPageSt )
785 sal_uInt16 nEndPage = GetLayout()->GetPageNum();
786 nEndPage += nEndPage * 10 / 100;
787 if( nEndPage > 14 )
789 *pPageCnt = nEndPage;
790 ::StartProgress( STR_STATSTR_HYPHEN, 0, nEndPage, GetDoc()->GetDocShell());
792 else // here we once and for all suppress StatLineStartPercent
793 *pPageSt = 1;
796 OSL_ENSURE( g_pHyphIter, "No Iterator" );
797 //JP 18.07.95: prevent displaying selection on error messages. NO StartAction so that all
798 // Paints are also disabled.
799 ++mnStartAction;
800 uno::Reference< uno::XInterface > xRet;
801 g_pHyphIter->Continue( pPageCnt, pPageSt ) >>= xRet;
802 --mnStartAction;
804 if( xRet.is() )
805 g_pHyphIter->ShowSelection();
807 return xRet;
810 /** Insert soft hyphen
812 * @param nHyphPos Offset in the to be separated word
814 void SwEditShell::InsertSoftHyph( const sal_Int32 nHyphPos )
816 OSL_ENSURE( g_pHyphIter, "where is my Iterator?" );
817 g_pHyphIter->InsertSoftHyph( nHyphPos );
820 /// ignore hyphenation
821 void SwEditShell::HyphIgnore()
823 OSL_ENSURE( g_pHyphIter, "No Iterator" );
824 //JP 18.07.95: prevent displaying selection on error messages. NO StartAction so that all
825 // Paints are also disabled.
826 ++mnStartAction;
827 g_pHyphIter->Ignore();
828 --mnStartAction;
830 g_pHyphIter->ShowSelection();
833 /** Get a list of potential corrections for misspelled word.
835 * If empty, word is unknown but there are no corrections available.
836 * If NULL then the word is not misspelled but correct.
838 * @brief SwEditShell::GetCorrection
839 * @return list or NULL pointer
841 uno::Reference< XSpellAlternatives >
842 SwEditShell::GetCorrection( const Point* pPt, SwRect& rSelectRect )
844 uno::Reference< XSpellAlternatives > xSpellAlt;
846 if( IsTableMode() )
847 return nullptr;
848 SwPaM* pCursor = GetCursor();
849 SwPosition aPos( *pCursor->GetPoint() );
850 Point aPt( *pPt );
851 SwCursorMoveState eTmpState( MV_SETONLYTEXT );
852 SwTextNode *pNode;
853 SwWrongList *pWrong;
854 if( GetLayout()->GetCursorOfst( &aPos, aPt, &eTmpState ) &&
855 nullptr != (pNode = aPos.nNode.GetNode().GetTextNode()) &&
856 nullptr != (pWrong = pNode->GetWrong()) &&
857 !pNode->IsInProtectSect() )
859 sal_Int32 nBegin = aPos.nContent.GetIndex();
860 sal_Int32 nLen = 1;
861 if( pWrong->InWrongWord(nBegin,nLen) && !pNode->IsSymbol(nBegin) )
863 const OUString aText(pNode->GetText().copy(nBegin, nLen));
864 OUString aWord( aText );
865 aWord = comphelper::string::remove(aWord, CH_TXTATR_BREAKWORD);
866 aWord = comphelper::string::remove(aWord, CH_TXTATR_INWORD);
868 uno::Reference< XSpellChecker1 > xSpell( ::GetSpellChecker() );
869 if( xSpell.is() )
871 LanguageType eActLang = (LanguageType)pNode->GetLang( nBegin, nLen );
872 if( xSpell->hasLanguage( eActLang ))
874 // restrict the maximal number of suggestions displayed
875 // in the context menu.
876 // Note: That could of course be done by clipping the
877 // resulting sequence but the current third party
878 // implementations result differs greatly if the number of
879 // suggestions to be retuned gets changed. Statistically
880 // it gets much better if told to return e.g. only 7 strings
881 // than returning e.g. 16 suggestions and using only the
882 // first 7. Thus we hand down the value to use to that
883 // implementation here by providing an additional parameter.
884 Sequence< PropertyValue > aPropVals(1);
885 PropertyValue &rVal = aPropVals.getArray()[0];
886 rVal.Name = UPN_MAX_NUMBER_OF_SUGGESTIONS;
887 rVal.Value <<= (sal_Int16) 7;
889 xSpellAlt = xSpell->spell( aWord, eActLang, aPropVals );
893 if ( xSpellAlt.is() ) // error found?
895 // save the start and end positions of the line and the starting point
896 Push();
897 LeftMargin();
898 const sal_Int32 nLineStart = GetCursor()->GetPoint()->nContent.GetIndex();
899 RightMargin();
900 const sal_Int32 nLineEnd = GetCursor()->GetPoint()->nContent.GetIndex();
901 Pop(false);
903 // make sure the selection build later from the data below does
904 // not "in word" character to the left and right in order to
905 // preserve those. Therefore count those "in words" in order to
906 // modify the selection accordingly.
907 const sal_Unicode* pChar = aText.getStr();
908 sal_Int32 nLeft = 0;
909 while (pChar && *pChar++ == CH_TXTATR_INWORD)
910 ++nLeft;
911 pChar = aText.getLength() ? aText.getStr() + aText.getLength() - 1 : nullptr;
912 sal_Int32 nRight = 0;
913 while (pChar && *pChar-- == CH_TXTATR_INWORD)
914 ++nRight;
916 aPos.nContent = nBegin + nLeft;
917 pCursor = GetCursor();
918 *pCursor->GetPoint() = aPos;
919 pCursor->SetMark();
920 ExtendSelection( true, nLen - nLeft - nRight );
921 // don't determine the rectangle in the current line
922 const sal_Int32 nWordStart = (nBegin + nLeft) < nLineStart ? nLineStart : nBegin + nLeft;
923 // take one less than the line end - otherwise the next line would be calculated
924 const sal_Int32 nWordEnd = (nBegin + nLen - nLeft - nRight) > nLineEnd
925 ? nLineEnd : (nBegin + nLen - nLeft - nRight);
926 Push();
927 pCursor->DeleteMark();
928 SwIndex& rContent = GetCursor()->GetPoint()->nContent;
929 rContent = nWordStart;
930 SwRect aStartRect;
931 SwCursorMoveState aState;
932 aState.m_bRealWidth = true;
933 SwContentNode* pContentNode = pCursor->GetContentNode();
934 SwContentFrame *pContentFrame = pContentNode->getLayoutFrame( GetLayout(), pPt, pCursor->GetPoint(), false);
936 pContentFrame->GetCharRect( aStartRect, *pCursor->GetPoint(), &aState );
937 rContent = nWordEnd - 1;
938 SwRect aEndRect;
939 pContentFrame->GetCharRect( aEndRect, *pCursor->GetPoint(),&aState );
940 rSelectRect = aStartRect.Union( aEndRect );
941 Pop(false);
945 return xSpellAlt;
948 bool SwEditShell::GetGrammarCorrection(
949 linguistic2::ProofreadingResult /*out*/ &rResult, // the complete result
950 sal_Int32 /*out*/ &rErrorPosInText, // offset of error position in string that was grammar checked...
951 sal_Int32 /*out*/ &rErrorIndexInResult, // index of error in rResult.aGrammarErrors
952 uno::Sequence< OUString > /*out*/ &rSuggestions, // suggestions to be used for the error found
953 const Point *pPt, SwRect &rSelectRect )
955 bool bRes = false;
957 if( IsTableMode() )
958 return bRes;
960 SwPaM* pCursor = GetCursor();
961 SwPosition aPos( *pCursor->GetPoint() );
962 Point aPt( *pPt );
963 SwCursorMoveState eTmpState( MV_SETONLYTEXT );
964 SwTextNode *pNode;
965 SwGrammarMarkUp *pWrong;
966 if( GetLayout()->GetCursorOfst( &aPos, aPt, &eTmpState ) &&
967 nullptr != (pNode = aPos.nNode.GetNode().GetTextNode()) &&
968 nullptr != (pWrong = pNode->GetGrammarCheck()) &&
969 !pNode->IsInProtectSect() )
971 sal_Int32 nBegin = aPos.nContent.GetIndex();
972 sal_Int32 nLen = 1;
973 if (pWrong->InWrongWord(nBegin, nLen))
975 const OUString aText(pNode->GetText().copy(nBegin, nLen));
977 uno::Reference< linguistic2::XProofreadingIterator > xGCIterator( mpDoc->GetGCIterator() );
978 if (xGCIterator.is())
980 uno::Reference< lang::XComponent > xDoc( mpDoc->GetDocShell()->GetBaseModel(), uno::UNO_QUERY );
982 // Expand the string:
983 const ModelToViewHelper aConversionMap(*pNode);
984 const OUString& aExpandText = aConversionMap.getViewText();
985 // get XFlatParagraph to use...
986 uno::Reference< text::XFlatParagraph > xFlatPara = new SwXFlatParagraph( *pNode, aExpandText, aConversionMap );
988 // get error position of cursor in XFlatParagraph
989 rErrorPosInText = aConversionMap.ConvertToViewPosition( nBegin );
991 const sal_Int32 nStartOfSentence = aConversionMap.ConvertToViewPosition( pWrong->getSentenceStart( nBegin ) );
992 const sal_Int32 nEndOfSentence = aConversionMap.ConvertToViewPosition( pWrong->getSentenceEnd( nBegin ) );
994 rResult = xGCIterator->checkSentenceAtPosition(
995 xDoc, xFlatPara, aExpandText, lang::Locale(), nStartOfSentence,
996 nEndOfSentence == COMPLETE_STRING ? aExpandText.getLength() : nEndOfSentence,
997 rErrorPosInText );
998 bRes = true;
1000 // get suggestions to use for the specific error position
1001 sal_Int32 nErrors = rResult.aErrors.getLength();
1002 rSuggestions.realloc( 0 );
1003 for (sal_Int32 i = 0; i < nErrors; ++i )
1005 // return suggestions for first error that includes the given error position
1006 const linguistic2::SingleProofreadingError &rError = rResult.aErrors[i];
1007 if (rError.nErrorStart <= rErrorPosInText &&
1008 rErrorPosInText + nLen <= rError.nErrorStart + rError.nErrorLength)
1010 rSuggestions = rError.aSuggestions;
1011 rErrorIndexInResult = i;
1012 break;
1017 if (rResult.aErrors.getLength() > 0) // error found?
1019 // save the start and end positions of the line and the starting point
1020 Push();
1021 LeftMargin();
1022 const sal_Int32 nLineStart = GetCursor()->GetPoint()->nContent.GetIndex();
1023 RightMargin();
1024 const sal_Int32 nLineEnd = GetCursor()->GetPoint()->nContent.GetIndex();
1025 Pop(false);
1027 // make sure the selection build later from the data below does
1028 // not include "in word" character to the left and right in
1029 // order to preserve those. Therefore count those "in words" in
1030 // order to modify the selection accordingly.
1031 const sal_Unicode* pChar = aText.getStr();
1032 sal_Int32 nLeft = 0;
1033 while (pChar && *pChar++ == CH_TXTATR_INWORD)
1034 ++nLeft;
1035 pChar = aText.getLength() ? aText.getStr() + aText.getLength() - 1 : nullptr;
1036 sal_Int32 nRight = 0;
1037 while (pChar && *pChar-- == CH_TXTATR_INWORD)
1038 ++nRight;
1040 aPos.nContent = nBegin + nLeft;
1041 pCursor = GetCursor();
1042 *pCursor->GetPoint() = aPos;
1043 pCursor->SetMark();
1044 ExtendSelection( true, nLen - nLeft - nRight );
1045 // don't determine the rectangle in the current line
1046 const sal_Int32 nWordStart = (nBegin + nLeft) < nLineStart ? nLineStart : nBegin + nLeft;
1047 // take one less than the line end - otherwise the next line would be calculated
1048 const sal_Int32 nWordEnd = (nBegin + nLen - nLeft - nRight) > nLineEnd
1049 ? nLineEnd : (nBegin + nLen - nLeft - nRight);
1050 Push();
1051 pCursor->DeleteMark();
1052 SwIndex& rContent = GetCursor()->GetPoint()->nContent;
1053 rContent = nWordStart;
1054 SwRect aStartRect;
1055 SwCursorMoveState aState;
1056 aState.m_bRealWidth = true;
1057 SwContentNode* pContentNode = pCursor->GetContentNode();
1058 SwContentFrame *pContentFrame = pContentNode->getLayoutFrame( GetLayout(), pPt, pCursor->GetPoint(), false);
1060 pContentFrame->GetCharRect( aStartRect, *pCursor->GetPoint(), &aState );
1061 rContent = nWordEnd - 1;
1062 SwRect aEndRect;
1063 pContentFrame->GetCharRect( aEndRect, *pCursor->GetPoint(),&aState );
1064 rSelectRect = aStartRect.Union( aEndRect );
1065 Pop(false);
1070 return bRes;
1073 bool SwEditShell::SpellSentence(svx::SpellPortions& rPortions, bool bIsGrammarCheck)
1075 OSL_ENSURE( g_pSpellIter, "SpellIter missing" );
1076 if (!g_pSpellIter)
1077 return false;
1078 bool bRet = g_pSpellIter->SpellSentence(rPortions, bIsGrammarCheck);
1080 // make Selection visible - this should simply move the
1081 // cursor to the end of the sentence
1082 StartAction();
1083 EndAction();
1084 return bRet;
1087 ///make SpellIter start with the current sentence when called next time
1088 void SwEditShell::PutSpellingToSentenceStart()
1090 OSL_ENSURE( g_pSpellIter, "SpellIter missing" );
1091 if (!g_pSpellIter)
1092 return;
1093 g_pSpellIter->ToSentenceStart();
1096 static sal_uInt32 lcl_CountRedlines(const svx::SpellPortions& rLastPortions)
1098 sal_uInt32 nRet = 0;
1099 SpellPortions::const_iterator aIter = rLastPortions.begin();
1100 for( ; aIter != rLastPortions.end(); ++aIter)
1102 if( aIter->bIsHidden )
1103 ++nRet;
1105 return nRet;
1108 void SwEditShell::MoveContinuationPosToEndOfCheckedSentence()
1110 // give hint that continuation position for spell/grammar checking is
1111 // at the end of this sentence
1112 if (g_pSpellIter)
1114 g_pSpellIter->SetCurr( new SwPosition( *g_pSpellIter->GetCurrX() ) );
1115 g_pSpellIter->ContinueAfterThisSentence();
1119 void SwEditShell::ApplyChangedSentence(const svx::SpellPortions& rNewPortions, bool bRecheck)
1121 // Note: rNewPortions.size() == 0 is valid and happens when the whole
1122 // sentence got removed in the dialog
1124 OSL_ENSURE( g_pSpellIter, "SpellIter missing" );
1125 if (g_pSpellIter &&
1126 g_pSpellIter->GetLastPortions().size() > 0) // no portions -> no text to be changed
1128 const SpellPortions& rLastPortions = g_pSpellIter->GetLastPortions();
1129 const SpellContentPositions rLastPositions = g_pSpellIter->GetLastPositions();
1130 OSL_ENSURE(rLastPortions.size() > 0 &&
1131 rLastPortions.size() == rLastPositions.size(),
1132 "last vectors of spelling results are not set or not equal");
1134 // iterate over the new portions, beginning at the end to take advantage of the previously
1135 // saved content positions
1137 mpDoc->GetIDocumentUndoRedo().StartUndo( UNDO_UI_TEXT_CORRECTION, nullptr );
1138 StartAction();
1140 SwPaM *pCursor = GetCursor();
1141 // save cursor position (which should be at the end of the current sentence)
1142 // for later restoration
1143 Push();
1145 sal_uInt32 nRedlinePortions = lcl_CountRedlines(rLastPortions);
1146 if((rLastPortions.size() - nRedlinePortions) == rNewPortions.size())
1148 OSL_ENSURE( !rNewPortions.empty(), "rNewPortions should not be empty here" );
1149 OSL_ENSURE( !rLastPortions.empty(), "rLastPortions should not be empty here" );
1150 OSL_ENSURE( !rLastPositions.empty(), "rLastPositions should not be empty here" );
1152 // the simple case: the same number of elements on both sides
1153 // each changed element has to be applied to the corresponding source element
1154 svx::SpellPortions::const_iterator aCurrentNewPortion = rNewPortions.end();
1155 SpellPortions::const_iterator aCurrentOldPortion = rLastPortions.end();
1156 SpellContentPositions::const_iterator aCurrentOldPosition = rLastPositions.end();
1159 --aCurrentNewPortion;
1160 --aCurrentOldPortion;
1161 --aCurrentOldPosition;
1162 //jump over redline portions
1163 while(aCurrentOldPortion->bIsHidden)
1165 if (aCurrentOldPortion != rLastPortions.begin() &&
1166 aCurrentOldPosition != rLastPositions.begin())
1168 --aCurrentOldPortion;
1169 --aCurrentOldPosition;
1171 else
1173 OSL_FAIL("ApplyChangedSentence: iterator positions broken" );
1174 break;
1177 if ( !pCursor->HasMark() )
1178 pCursor->SetMark();
1179 pCursor->GetPoint()->nContent = aCurrentOldPosition->nLeft;
1180 pCursor->GetMark()->nContent = aCurrentOldPosition->nRight;
1181 sal_uInt16 nScriptType = SvtLanguageOptions::GetI18NScriptTypeOfLanguage( aCurrentNewPortion->eLanguage );
1182 sal_uInt16 nLangWhichId = RES_CHRATR_LANGUAGE;
1183 switch(nScriptType)
1185 case css::i18n::ScriptType::ASIAN : nLangWhichId = RES_CHRATR_CJK_LANGUAGE; break;
1186 case css::i18n::ScriptType::COMPLEX : nLangWhichId = RES_CHRATR_CTL_LANGUAGE; break;
1188 if(aCurrentNewPortion->sText != aCurrentOldPortion->sText)
1190 // change text ...
1191 mpDoc->getIDocumentContentOperations().DeleteAndJoin(*pCursor);
1192 // ... and apply language if necessary
1193 if(aCurrentNewPortion->eLanguage != aCurrentOldPortion->eLanguage)
1194 SetAttrItem( SvxLanguageItem(aCurrentNewPortion->eLanguage, nLangWhichId) );
1195 mpDoc->getIDocumentContentOperations().InsertString(*pCursor, aCurrentNewPortion->sText);
1197 else if(aCurrentNewPortion->eLanguage != aCurrentOldPortion->eLanguage)
1199 // apply language
1200 SetAttrItem( SvxLanguageItem(aCurrentNewPortion->eLanguage, nLangWhichId) );
1202 else if( aCurrentNewPortion->bIgnoreThisError )
1204 // add the 'ignore' markup to the TextNode's grammar ignore markup list
1205 IgnoreGrammarErrorAt( *pCursor );
1206 OSL_FAIL("TODO: add ignore mark to text node");
1208 if(aCurrentNewPortion == rNewPortions.begin())
1209 break;
1211 while(aCurrentNewPortion != rNewPortions.begin());
1213 else
1215 OSL_ENSURE( !rLastPositions.empty(), "rLastPositions should not be empty here" );
1217 // select the complete sentence
1218 SpellContentPositions::const_iterator aCurrentEndPosition = rLastPositions.end();
1219 --aCurrentEndPosition;
1220 SpellContentPositions::const_iterator aCurrentStartPosition = rLastPositions.begin();
1221 pCursor->GetPoint()->nContent = aCurrentStartPosition->nLeft;
1222 pCursor->GetMark()->nContent = aCurrentEndPosition->nRight;
1224 // delete the sentence completely
1225 mpDoc->getIDocumentContentOperations().DeleteAndJoin(*pCursor);
1226 svx::SpellPortions::const_iterator aCurrentNewPortion = rNewPortions.begin();
1227 while(aCurrentNewPortion != rNewPortions.end())
1229 // set the language attribute
1230 SvtScriptType nScriptType = GetScriptType();
1231 sal_uInt16 nLangWhichId = RES_CHRATR_LANGUAGE;
1232 switch(nScriptType)
1234 case SvtScriptType::ASIAN : nLangWhichId = RES_CHRATR_CJK_LANGUAGE; break;
1235 case SvtScriptType::COMPLEX : nLangWhichId = RES_CHRATR_CTL_LANGUAGE; break;
1236 default: break;
1238 SfxItemSet aSet(GetAttrPool(), nLangWhichId, nLangWhichId, 0);
1239 GetCurAttr( aSet );
1240 const SvxLanguageItem& rLang = static_cast<const SvxLanguageItem& >(aSet.Get(nLangWhichId));
1241 if(rLang.GetLanguage() != aCurrentNewPortion->eLanguage)
1242 SetAttrItem( SvxLanguageItem(aCurrentNewPortion->eLanguage, nLangWhichId) );
1243 // insert the new string
1244 mpDoc->getIDocumentContentOperations().InsertString(*pCursor, aCurrentNewPortion->sText);
1246 // set the cursor to the end of the inserted string
1247 *pCursor->Start() = *pCursor->End();
1248 ++aCurrentNewPortion;
1252 // restore cursor to the end of the sentence
1253 // (will work also if the sentence length has changed,
1254 // since cursors get updated automatically!)
1255 Pop( false );
1257 // collapse cursor to the end of the modified sentence
1258 *pCursor->Start() = *pCursor->End();
1259 if (bRecheck)
1261 // in grammar check the current sentence has to be checked again
1262 GoStartSentence();
1264 // set continuation position for spell/grammar checking to the end of this sentence
1265 g_pSpellIter->SetCurr( new SwPosition(*pCursor->Start()) );
1267 mpDoc->GetIDocumentUndoRedo().EndUndo( UNDO_UI_TEXT_CORRECTION, nullptr );
1268 EndAction();
1271 /** Collect all deleted redlines of the current text node
1272 * beginning at the start of the cursor position
1274 static SpellContentPositions lcl_CollectDeletedRedlines(SwEditShell* pSh)
1276 SpellContentPositions aRedlines;
1277 SwDoc* pDoc = pSh->GetDoc();
1278 const bool bShowChg = IDocumentRedlineAccess::IsShowChanges( pDoc->getIDocumentRedlineAccess().GetRedlineMode() );
1279 if ( bShowChg )
1281 SwPaM *pCursor = pSh->GetCursor();
1282 const SwPosition* pStartPos = pCursor->Start();
1283 const SwTextNode* pTextNode = pCursor->GetNode().GetTextNode();
1285 sal_uInt16 nAct = pDoc->getIDocumentRedlineAccess().GetRedlinePos( *pTextNode, USHRT_MAX );
1286 const sal_Int32 nStartIndex = pStartPos->nContent.GetIndex();
1287 for ( ; nAct < pDoc->getIDocumentRedlineAccess().GetRedlineTable().size(); nAct++ )
1289 const SwRangeRedline* pRed = pDoc->getIDocumentRedlineAccess().GetRedlineTable()[ nAct ];
1291 if ( pRed->Start()->nNode > pTextNode->GetIndex() )
1292 break;
1294 if( nsRedlineType_t::REDLINE_DELETE == pRed->GetType() )
1296 sal_Int32 nStart_, nEnd_;
1297 pRed->CalcStartEnd( pTextNode->GetIndex(), nStart_, nEnd_ );
1298 sal_Int32 nStart = nStart_;
1299 sal_Int32 nEnd = nEnd_;
1300 if(nStart >= nStartIndex || nEnd >= nStartIndex)
1302 SpellContentPosition aAdd;
1303 aAdd.nLeft = nStart;
1304 aAdd.nRight = nEnd;
1305 aRedlines.push_back(aAdd);
1310 return aRedlines;
1313 /// remove the redline positions after the current selection
1314 static void lcl_CutRedlines( SpellContentPositions& aDeletedRedlines, SwEditShell* pSh )
1316 if(!aDeletedRedlines.empty())
1318 SwPaM *pCursor = pSh->GetCursor();
1319 const SwPosition* pEndPos = pCursor->End();
1320 const sal_Int32 nEnd = pEndPos->nContent.GetIndex();
1321 while(!aDeletedRedlines.empty() &&
1322 aDeletedRedlines.back().nLeft > nEnd)
1324 aDeletedRedlines.pop_back();
1329 static SpellContentPosition lcl_FindNextDeletedRedline(
1330 const SpellContentPositions& rDeletedRedlines,
1331 sal_Int32 nSearchFrom )
1333 SpellContentPosition aRet;
1334 aRet.nLeft = aRet.nRight = SAL_MAX_INT32;
1335 if(!rDeletedRedlines.empty())
1337 SpellContentPositions::const_iterator aIter = rDeletedRedlines.begin();
1338 for( ; aIter != rDeletedRedlines.end(); ++aIter)
1340 if(aIter->nLeft < nSearchFrom)
1341 continue;
1342 aRet = *aIter;
1343 break;
1346 return aRet;
1349 bool SwSpellIter::SpellSentence(svx::SpellPortions& rPortions, bool bIsGrammarCheck)
1351 bool bRet = false;
1352 aLastPortions.clear();
1353 aLastPositions.clear();
1355 SwEditShell *pMySh = GetSh();
1356 if( !pMySh )
1357 return false;
1359 OSL_ENSURE( GetEnd(), "SwSpellIter::SpellSentence without Start?");
1361 uno::Reference< XSpellAlternatives > xSpellRet;
1362 linguistic2::ProofreadingResult aGrammarResult;
1363 bool bGoOn = true;
1364 bool bGrammarErrorFound = false;
1365 do {
1366 SwPaM *pCursor = pMySh->GetCursor();
1367 if ( !pCursor->HasMark() )
1368 pCursor->SetMark();
1370 *pCursor->GetPoint() = *GetCurr();
1371 *pCursor->GetMark() = *GetEnd();
1373 if( bBackToStartOfSentence )
1375 pMySh->GoStartSentence();
1376 bBackToStartOfSentence = false;
1378 uno::Any aSpellRet =
1379 pMySh->GetDoc()->Spell(*pCursor,
1380 xSpeller, nullptr, nullptr, bIsGrammarCheck );
1381 aSpellRet >>= xSpellRet;
1382 aSpellRet >>= aGrammarResult;
1383 bGoOn = GetCursorCnt() > 1;
1384 bGrammarErrorFound = aGrammarResult.aErrors.getLength() > 0;
1385 if( xSpellRet.is() || bGrammarErrorFound )
1387 bGoOn = false;
1388 SwPosition* pNewPoint = new SwPosition( *pCursor->GetPoint() );
1389 SwPosition* pNewMark = new SwPosition( *pCursor->GetMark() );
1391 SetCurr( pNewPoint );
1392 SetCurrX( pNewMark );
1394 if( bGoOn )
1396 pMySh->Pop( false );
1397 pCursor = pMySh->GetCursor();
1398 if ( *pCursor->GetPoint() > *pCursor->GetMark() )
1399 pCursor->Exchange();
1400 SwPosition* pNew = new SwPosition( *pCursor->GetPoint() );
1401 SetStart( pNew );
1402 pNew = new SwPosition( *pCursor->GetMark() );
1403 SetEnd( pNew );
1404 pNew = new SwPosition( *GetStart() );
1405 SetCurr( pNew );
1406 pNew = new SwPosition( *pNew );
1407 SetCurrX( pNew );
1408 pCursor->SetMark();
1409 --GetCursorCnt();
1411 } while ( bGoOn );
1413 if(xSpellRet.is() || bGrammarErrorFound)
1415 // an error has been found
1416 // To fill the spell portions the beginning of the sentence has to be found
1417 SwPaM *pCursor = pMySh->GetCursor();
1418 // set the mark to the right if necessary
1419 if ( *pCursor->GetPoint() > *pCursor->GetMark() )
1420 pCursor->Exchange();
1421 // the cursor has to be collapsed on the left to go to the start of the sentence - if sentence ends inside of the error
1422 pCursor->DeleteMark();
1423 pCursor->SetMark();
1424 bool bStartSent = pMySh->GoStartSentence();
1425 SpellContentPositions aDeletedRedlines = lcl_CollectDeletedRedlines(pMySh);
1426 if(bStartSent)
1428 // create a portion from the start part
1429 AddPortion(nullptr, nullptr, aDeletedRedlines);
1431 // Set the cursor to the error already found
1432 *pCursor->GetPoint() = *GetCurrX();
1433 *pCursor->GetMark() = *GetCurr();
1434 AddPortion(xSpellRet, &aGrammarResult, aDeletedRedlines);
1436 // save the end position of the error to continue from here
1437 SwPosition aSaveStartPos = *pCursor->End();
1438 // determine the end of the current sentence
1439 if ( *pCursor->GetPoint() < *pCursor->GetMark() )
1440 pCursor->Exchange();
1441 // again collapse to start marking after the end of the error
1442 pCursor->DeleteMark();
1443 pCursor->SetMark();
1445 pMySh->GoEndSentence();
1446 if( bGrammarErrorFound )
1448 const ModelToViewHelper aConversionMap(static_cast<SwTextNode&>(pCursor->GetNode()));
1449 const OUString& aExpandText = aConversionMap.getViewText();
1450 sal_Int32 nSentenceEnd =
1451 aConversionMap.ConvertToViewPosition( aGrammarResult.nBehindEndOfSentencePosition );
1452 // remove trailing space
1453 if( aExpandText[nSentenceEnd - 1] == ' ' )
1454 --nSentenceEnd;
1455 if( pCursor->End()->nContent.GetIndex() < nSentenceEnd )
1457 pCursor->End()->nContent.Assign(
1458 pCursor->End()->nNode.GetNode().GetContentNode(), nSentenceEnd);
1462 lcl_CutRedlines( aDeletedRedlines, pMySh );
1463 // save the 'global' end of the spellchecking
1464 const SwPosition aSaveEndPos = *GetEnd();
1465 // set the sentence end as 'local' end
1466 SetEnd( new SwPosition( *pCursor->End() ));
1468 *pCursor->GetPoint() = aSaveStartPos;
1469 *pCursor->GetMark() = *GetEnd();
1470 // now the rest of the sentence has to be searched for errors
1471 // for each error the non-error text between the current and the last error has
1472 // to be added to the portions - if necessary broken into same-language-portions
1473 if( !bGrammarErrorFound ) //in grammar check there's only one error returned
1477 xSpellRet = nullptr;
1478 // don't search for grammar errors here anymore!
1479 pMySh->GetDoc()->Spell(*pCursor,
1480 xSpeller, nullptr, nullptr, false ) >>= xSpellRet;
1481 if ( *pCursor->GetPoint() > *pCursor->GetMark() )
1482 pCursor->Exchange();
1483 SetCurr( new SwPosition( *pCursor->GetPoint() ));
1484 SetCurrX( new SwPosition( *pCursor->GetMark() ));
1486 // if an error has been found go back to the text preceding the error
1487 if(xSpellRet.is())
1489 *pCursor->GetPoint() = aSaveStartPos;
1490 *pCursor->GetMark() = *GetCurr();
1492 // add the portion
1493 AddPortion(nullptr, nullptr, aDeletedRedlines);
1495 if(xSpellRet.is())
1497 *pCursor->GetPoint() = *GetCurr();
1498 *pCursor->GetMark() = *GetCurrX();
1499 AddPortion(xSpellRet, nullptr, aDeletedRedlines);
1500 // move the cursor to the end of the error string
1501 *pCursor->GetPoint() = *GetCurrX();
1502 // and save the end of the error as new start position
1503 aSaveStartPos = *GetCurrX();
1504 // and the end of the sentence
1505 *pCursor->GetMark() = *GetEnd();
1507 // if the end of the sentence has already been reached then break here
1508 if(*GetCurrX() >= *GetEnd())
1509 break;
1511 while(xSpellRet.is());
1513 else
1515 // go to the end of sentence as the grammar check returned it
1516 // at this time the Point is behind the grammar error
1517 // and the mark points to the sentence end as
1518 if ( *pCursor->GetPoint() < *pCursor->GetMark() )
1519 pCursor->Exchange();
1522 // the part between the last error and the end of the sentence has to be added
1523 *pMySh->GetCursor()->GetPoint() = *GetEnd();
1524 if(*GetCurrX() < *GetEnd())
1526 AddPortion(nullptr, nullptr, aDeletedRedlines);
1528 // set the shell cursor to the end of the sentence to prevent a visible selection
1529 *pCursor->GetMark() = *GetEnd();
1530 if( !bIsGrammarCheck )
1532 // set the current position to the end of the sentence
1533 SetCurr( new SwPosition(*GetEnd()) );
1535 // restore the 'global' end
1536 SetEnd( new SwPosition(aSaveEndPos) );
1537 rPortions = aLastPortions;
1538 bRet = true;
1540 else
1542 // if no error could be found the selection has to be corrected - at least if it's not in the body
1543 *pMySh->GetCursor()->GetPoint() = *GetEnd();
1544 pMySh->GetCursor()->DeleteMark();
1547 return bRet;
1550 void SwSpellIter::ToSentenceStart()
1552 bBackToStartOfSentence = true;
1555 static LanguageType lcl_GetLanguage(SwEditShell& rSh)
1557 SvtScriptType nScriptType = rSh.GetScriptType();
1558 sal_uInt16 nLangWhichId = RES_CHRATR_LANGUAGE;
1560 switch(nScriptType)
1562 case SvtScriptType::ASIAN : nLangWhichId = RES_CHRATR_CJK_LANGUAGE; break;
1563 case SvtScriptType::COMPLEX : nLangWhichId = RES_CHRATR_CTL_LANGUAGE; break;
1564 default: break;
1566 SfxItemSet aSet(rSh.GetAttrPool(), nLangWhichId, nLangWhichId, 0);
1567 rSh.GetCurAttr( aSet );
1568 const SvxLanguageItem& rLang = static_cast<const SvxLanguageItem& >(aSet.Get(nLangWhichId));
1569 return rLang.GetLanguage();
1572 /// create a text portion at the given position
1573 void SwSpellIter::CreatePortion(uno::Reference< XSpellAlternatives > xAlt,
1574 linguistic2::ProofreadingResult* pGrammarResult,
1575 bool bIsField, bool bIsHidden)
1577 svx::SpellPortion aPortion;
1578 OUString sText;
1579 GetSh()->GetSelectedText( sText );
1580 if(!sText.isEmpty())
1582 // in case of redlined deletions the selection of an error is not the same as the _real_ word
1583 if(xAlt.is())
1584 aPortion.sText = xAlt->getWord();
1585 else if(pGrammarResult)
1587 aPortion.bIsGrammarError = true;
1588 if(pGrammarResult->aErrors.getLength())
1590 aPortion.aGrammarError = pGrammarResult->aErrors[0];
1591 aPortion.sText = pGrammarResult->aText.copy( aPortion.aGrammarError.nErrorStart, aPortion.aGrammarError.nErrorLength );
1592 aPortion.xGrammarChecker = pGrammarResult->xProofreader;
1593 const beans::PropertyValue* pProperties = pGrammarResult->aProperties.getConstArray();
1594 for( sal_Int32 nProp = 0; nProp < pGrammarResult->aProperties.getLength(); ++nProp )
1596 if ( pProperties->Name == "DialogTitle" )
1598 pProperties->Value >>= aPortion.sDialogTitle;
1599 break;
1604 else
1605 aPortion.sText = sText;
1606 aPortion.eLanguage = lcl_GetLanguage(*GetSh());
1607 aPortion.bIsField = bIsField;
1608 aPortion.bIsHidden = bIsHidden;
1609 aPortion.xAlternatives = xAlt;
1610 SpellContentPosition aPosition;
1611 SwPaM *pCursor = GetSh()->GetCursor();
1612 aPosition.nLeft = pCursor->Start()->nContent.GetIndex();
1613 aPosition.nRight = pCursor->End()->nContent.GetIndex();
1614 aLastPortions.push_back(aPortion);
1615 aLastPositions.push_back(aPosition);
1619 void SwSpellIter::AddPortion(uno::Reference< XSpellAlternatives > xAlt,
1620 linguistic2::ProofreadingResult* pGrammarResult,
1621 const SpellContentPositions& rDeletedRedlines)
1623 SwEditShell *pMySh = GetSh();
1624 OUString sText;
1625 pMySh->GetSelectedText( sText );
1626 if(!sText.isEmpty())
1628 if(xAlt.is() || pGrammarResult != nullptr)
1630 CreatePortion(xAlt, pGrammarResult, false, false);
1632 else
1634 SwPaM *pCursor = GetSh()->GetCursor();
1635 if ( *pCursor->GetPoint() > *pCursor->GetMark() )
1636 pCursor->Exchange();
1637 // save the start and end positions
1638 SwPosition aStart(*pCursor->GetPoint());
1639 SwPosition aEnd(*pCursor->GetMark());
1640 // iterate over the text to find changes in language
1641 // set the mark equal to the point
1642 *pCursor->GetMark() = aStart;
1643 SwTextNode* pTextNode = pCursor->GetNode().GetTextNode();
1644 LanguageType eStartLanguage = lcl_GetLanguage(*GetSh());
1645 SpellContentPosition aNextRedline = lcl_FindNextDeletedRedline(
1646 rDeletedRedlines, aStart.nContent.GetIndex() );
1647 if( aNextRedline.nLeft == aStart.nContent.GetIndex() )
1649 // select until the end of the current redline
1650 const sal_Int32 nEnd = aEnd.nContent.GetIndex() < aNextRedline.nRight ?
1651 aEnd.nContent.GetIndex() : aNextRedline.nRight;
1652 pCursor->GetPoint()->nContent.Assign( pTextNode, nEnd );
1653 CreatePortion(xAlt, pGrammarResult, false, true);
1654 aStart = *pCursor->End();
1655 // search for next redline
1656 aNextRedline = lcl_FindNextDeletedRedline(
1657 rDeletedRedlines, aStart.nContent.GetIndex() );
1659 while(*pCursor->GetPoint() < aEnd)
1661 // #125786 in table cell with fixed row height the cursor might not move forward
1662 if(!GetSh()->Right(1, CRSR_SKIP_CELLS))
1663 break;
1665 bool bField = false;
1666 // read the character at the current position to check if it's a field
1667 sal_Unicode const cChar =
1668 pTextNode->GetText()[pCursor->GetMark()->nContent.GetIndex()];
1669 if( CH_TXTATR_BREAKWORD == cChar || CH_TXTATR_INWORD == cChar)
1671 const SwTextAttr* pTextAttr = pTextNode->GetTextAttrForCharAt(
1672 pCursor->GetMark()->nContent.GetIndex() );
1673 const sal_uInt16 nWhich = pTextAttr
1674 ? pTextAttr->Which()
1675 : static_cast<sal_uInt16>(RES_TXTATR_END);
1676 switch (nWhich)
1678 case RES_TXTATR_FIELD:
1679 case RES_TXTATR_ANNOTATION:
1680 case RES_TXTATR_FTN:
1681 case RES_TXTATR_FLYCNT:
1682 bField = true;
1683 break;
1686 else if (cChar == CH_TXT_ATR_FORMELEMENT)
1688 SwPosition aPos(*pCursor->GetMark());
1689 bField = pMySh->GetDoc()->getIDocumentMarkAccess()->getDropDownFor(aPos);
1692 LanguageType eCurLanguage = lcl_GetLanguage(*GetSh());
1693 bool bRedline = aNextRedline.nLeft == pCursor->GetPoint()->nContent.GetIndex();
1694 // create a portion if the next character
1695 // - is a field,
1696 // - is at the beginning of a deleted redline
1697 // - has a different language
1698 if(bField || bRedline || eCurLanguage != eStartLanguage)
1700 eStartLanguage = eCurLanguage;
1701 // go one step back - the cursor currently selects the first character
1702 // with a different language
1703 // in the case of redlining it's different
1704 if(eCurLanguage != eStartLanguage || bField)
1705 *pCursor->GetPoint() = *pCursor->GetMark();
1706 // set to the last start
1707 *pCursor->GetMark() = aStart;
1708 // create portion should only be called if a selection exists
1709 // there's no selection if there's a field at the beginning
1710 if(*pCursor->Start() != *pCursor->End())
1711 CreatePortion(xAlt, pGrammarResult, false, false);
1712 aStart = *pCursor->End();
1713 // now export the field - if there is any
1714 if(bField)
1716 *pCursor->GetMark() = *pCursor->GetPoint();
1717 GetSh()->Right(1, CRSR_SKIP_CELLS);
1718 CreatePortion(xAlt, pGrammarResult, true, false);
1719 aStart = *pCursor->End();
1722 // if a redline start then create a portion for it
1723 if(bRedline)
1725 *pCursor->GetMark() = *pCursor->GetPoint();
1726 // select until the end of the current redline
1727 const sal_Int32 nEnd = aEnd.nContent.GetIndex() < aNextRedline.nRight ?
1728 aEnd.nContent.GetIndex() : aNextRedline.nRight;
1729 pCursor->GetPoint()->nContent.Assign( pTextNode, nEnd );
1730 CreatePortion(xAlt, pGrammarResult, false, true);
1731 aStart = *pCursor->End();
1732 // search for next redline
1733 aNextRedline = lcl_FindNextDeletedRedline(
1734 rDeletedRedlines, aStart.nContent.GetIndex() );
1736 *pCursor->GetMark() = *pCursor->GetPoint();
1738 pCursor->SetMark();
1739 *pCursor->GetMark() = aStart;
1740 CreatePortion(xAlt, pGrammarResult, false, false);
1745 void SwEditShell::IgnoreGrammarErrorAt( SwPaM& rErrorPosition )
1747 SwTextNode *pNode;
1748 SwWrongList *pWrong;
1749 SwNodeIndex aIdx = rErrorPosition.Start()->nNode;
1750 SwNodeIndex aEndIdx = rErrorPosition.Start()->nNode;
1751 sal_Int32 nStart = rErrorPosition.Start()->nContent.GetIndex();
1752 sal_Int32 nEnd = COMPLETE_STRING;
1753 while( aIdx <= aEndIdx )
1755 pNode = aIdx.GetNode().GetTextNode();
1756 if( pNode ) {
1757 if( aIdx == aEndIdx )
1758 nEnd = rErrorPosition.End()->nContent.GetIndex();
1759 pWrong = pNode->GetGrammarCheck();
1760 if( pWrong )
1761 pWrong->RemoveEntry( nStart, nEnd );
1762 pWrong = pNode->GetWrong();
1763 if( pWrong )
1764 pWrong->RemoveEntry( nStart, nEnd );
1765 SwTextFrame::repaintTextFrames( *pNode );
1767 ++aIdx;
1768 nStart = 0;
1772 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */