1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
42 #include <IDocumentUndoRedo.hxx>
43 #include <IDocumentRedlineAccess.hxx>
44 #include <rootfrm.hxx>
48 #include <viewopt.hxx>
50 #include <SwGrammarMarkUp.hxx>
52 #include <statstr.hrc>
54 #include <splargs.hxx>
55 #include <redline.hxx>
58 #include <txatbase.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
;
74 sal_uInt16 nCursorCnt
;
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
; }
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
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
);
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
;
146 explicit SwConvIter(SwConversionArgs
&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
);
164 static void DelSoftHyph( SwPaM
&rPam
);
167 SwHyphIter() : m_pLastNode(nullptr), m_pLastFrame(nullptr), bOldIdle(false) {}
169 void Start( SwEditShell
*pSh
, SwDocPositions eStart
, SwDocPositions eEnd
);
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()
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
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();
222 for( n
= 0; n
< nCursorCnt
; ++n
)
225 pSh
->DestroyCursor();
234 pSh
->SetLinguRange( eStart
, eEnd
);
237 pCursor
= pSh
->GetCursor();
238 if ( *pCursor
->GetPoint() > *pCursor
->GetMark() )
241 pStart
= new SwPosition( *pCursor
->GetPoint() );
242 pEnd
= new SwPosition( *pCursor
->GetMark() );
245 SwPosition
* pNew
= new SwPosition( *GetStart() );
247 pNew
= new SwPosition( *pNew
);
254 void SwLinguIter::End_(bool bRestoreSelection
)
259 OSL_ENSURE( pEnd
, "SwLinguIter::End_ without end?");
260 if(bRestoreSelection
)
262 while( nCursorCnt
-- )
276 void SwSpellIter::Start( SwEditShell
*pShell
, SwDocPositions eStart
,
277 SwDocPositions eEnd
)
282 xSpeller
= ::GetSpellChecker();
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
)
293 //!! Please check SwConvIter also when modifying this
297 SwEditShell
*pMySh
= GetSh();
301 OSL_ENSURE( GetEnd(), "SwSpellIter::Continue without start?");
303 uno::Reference
< uno::XInterface
> xSpellRet
;
306 SwPaM
*pCursor
= pMySh
->GetCursor();
307 if ( !pCursor
->HasMark() )
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;
319 SwPosition
* pNewPoint
= new SwPosition( *pCursor
->GetPoint() );
320 SwPosition
* pNewMark
= new SwPosition( *pCursor
->GetMark() );
321 SetCurr( pNewPoint
);
322 SetCurrX( pNewMark
);
327 pCursor
= pMySh
->GetCursor();
328 if ( *pCursor
->GetPoint() > *pCursor
->GetMark() )
330 SwPosition
* pNew
= new SwPosition( *pCursor
->GetPoint() );
332 pNew
= new SwPosition( *pCursor
->GetMark() );
334 pNew
= new SwPosition( *GetStart() );
336 pNew
= new SwPosition( *pNew
);
342 aSpellRet
<<= xSpellRet
;
346 void SwConvIter::Start( SwEditShell
*pShell
, SwDocPositions eStart
,
347 SwDocPositions eEnd
)
351 Start_( pShell
, eStart
, eEnd
);
354 uno::Any
SwConvIter::Continue( sal_uInt16
* pPageCnt
, sal_uInt16
* pPageSt
)
357 //!! Please check SwSpellIter also when modifying this
360 uno::Any
aConvRet( makeAny( OUString() ) );
361 SwEditShell
*pMySh
= GetSh();
365 OSL_ENSURE( GetEnd(), "SwConvIter::Continue() without Start?");
370 SwPaM
*pCursor
= pMySh
->GetCursor();
371 if ( !pCursor
->HasMark() )
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() )
386 SwPosition
* pNewPoint
= new SwPosition( *pCursor
->GetPoint() );
387 SwPosition
* pNewMark
= new SwPosition( *pCursor
->GetMark() );
389 SetCurr( pNewPoint
);
390 SetCurrX( pNewMark
);
395 pCursor
= pMySh
->GetCursor();
396 if ( *pCursor
->GetPoint() > *pCursor
->GetMark() )
398 SwPosition
* pNew
= new SwPosition( *pCursor
->GetPoint() );
400 pNew
= new SwPosition( *pCursor
->GetMark() );
402 pNew
= new SwPosition( *GetStart() );
404 pNew
= new SwPosition( *pNew
);
410 return makeAny( aConvText
);
413 bool SwHyphIter::IsAuto()
415 uno::Reference
< beans::XPropertySet
> xProp( ::GetLinguPropertySet() );
416 return xProp
.is() && *o3tl::doAccess
<bool>(xProp
->getPropertyValue(
420 void SwHyphIter::ShowSelection()
422 SwEditShell
*pMySh
= GetSh();
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!
432 void SwHyphIter::Start( SwEditShell
*pShell
, SwDocPositions eStart
, SwDocPositions eEnd
)
435 if( GetSh() || GetEnd() )
437 OSL_ENSURE( !GetSh(), "SwHyphIter::Start: missing HyphEnd()" );
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()
452 GetSh()->GetViewOptions()->SetIdle( bOldIdle
);
456 uno::Any
SwHyphIter::Continue( sal_uInt16
* pPageCnt
, sal_uInt16
* pPageSt
)
459 SwEditShell
*pMySh
= GetSh();
463 const bool bAuto
= IsAuto();
464 uno::Reference
< XHyphenatedWord
> xHyphWord
;
469 OSL_ENSURE( GetEnd(), "SwHyphIter::Continue without Start?" );
470 pCursor
= pMySh
->GetCursor();
471 if ( !pCursor
->HasMark() )
473 if ( *pCursor
->GetPoint() < *pCursor
->GetMark() )
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
,
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;
499 pCursor
= pMySh
->GetCursor();
500 if ( *pCursor
->GetPoint() > *pCursor
->GetMark() )
502 SwPosition
* pNew
= new SwPosition(*pCursor
->End());
508 aHyphRet
<<= xHyphWord
;
512 /// ignore hyphenation
513 void SwHyphIter::Ignore()
515 SwEditShell
*pMySh
= GetSh();
516 SwPaM
*pCursor
= pMySh
->GetCursor();
518 // delete old SoftHyphen
519 DelSoftHyph( *pCursor
);
522 pCursor
->Start()->nContent
= pCursor
->End()->nContent
;
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()");
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()");
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
) );
567 pCursor
->DeleteMark();
575 SwHyphIterCacheLastTextFrame(SwTextNode
*const pNode
,
576 const sw::Creator
& create
)
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;
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() )
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
;
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
);
671 g_pSpellIter
= nullptr;
673 if (pConvArgs
&& g_pConvIter
&& g_pConvIter
->GetSh() == this)
675 OSL_ENSURE( g_pConvIter
, "where is my Iterator?" );
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
)
689 if ((!pConvArgs
&& g_pSpellIter
->GetSh() != this) ||
690 ( pConvArgs
&& g_pConvIter
->GetSh() != this))
693 if( pPageCnt
&& !*pPageCnt
)
695 sal_uInt16 nEndPage
= GetLayout()->GetPageNum();
696 nEndPage
+= nEndPage
* 10 / 100;
697 *pPageCnt
= 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.
708 uno::Reference
< uno::XInterface
> xRet
;
711 g_pConvIter
->Continue( pPageCnt
, pPageSt
) >>= aRet
;
716 g_pSpellIter
->Continue( pPageCnt
, pPageSt
) >>= xRet
;
721 if( !aRet
.isEmpty() || xRet
.is() )
723 // then make awt::Selection again visible
730 /* Interactive Hyphenation (BP 10.03.93)
733 * - Revoke all Selections
734 * - Save current Cursor
735 * - if no selections existent:
736 * - create new selection reaching until document end
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.
751 * - Restore old cursor, EndAction
753 void SwEditShell::HyphStart( SwDocPositions eStart
, SwDocPositions eEnd
)
755 // do not hyphenate if interactive hyphenation is active elsewhere
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" );
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)
783 if( pPageCnt
&& !*pPageCnt
&& !*pPageSt
)
785 sal_uInt16 nEndPage
= GetLayout()->GetPageNum();
786 nEndPage
+= nEndPage
* 10 / 100;
789 *pPageCnt
= nEndPage
;
790 ::StartProgress( STR_STATSTR_HYPHEN
, 0, nEndPage
, GetDoc()->GetDocShell());
792 else // here we once and for all suppress StatLineStartPercent
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.
800 uno::Reference
< uno::XInterface
> xRet
;
801 g_pHyphIter
->Continue( pPageCnt
, pPageSt
) >>= xRet
;
805 g_pHyphIter
->ShowSelection();
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.
827 g_pHyphIter
->Ignore();
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
;
848 SwPaM
* pCursor
= GetCursor();
849 SwPosition
aPos( *pCursor
->GetPoint() );
851 SwCursorMoveState
eTmpState( MV_SETONLYTEXT
);
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();
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() );
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
898 const sal_Int32 nLineStart
= GetCursor()->GetPoint()->nContent
.GetIndex();
900 const sal_Int32 nLineEnd
= GetCursor()->GetPoint()->nContent
.GetIndex();
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();
909 while (pChar
&& *pChar
++ == CH_TXTATR_INWORD
)
911 pChar
= aText
.getLength() ? aText
.getStr() + aText
.getLength() - 1 : nullptr;
912 sal_Int32 nRight
= 0;
913 while (pChar
&& *pChar
-- == CH_TXTATR_INWORD
)
916 aPos
.nContent
= nBegin
+ nLeft
;
917 pCursor
= GetCursor();
918 *pCursor
->GetPoint() = aPos
;
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
);
927 pCursor
->DeleteMark();
928 SwIndex
& rContent
= GetCursor()->GetPoint()->nContent
;
929 rContent
= nWordStart
;
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;
939 pContentFrame
->GetCharRect( aEndRect
, *pCursor
->GetPoint(),&aState
);
940 rSelectRect
= aStartRect
.Union( aEndRect
);
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
)
960 SwPaM
* pCursor
= GetCursor();
961 SwPosition
aPos( *pCursor
->GetPoint() );
963 SwCursorMoveState
eTmpState( MV_SETONLYTEXT
);
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();
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
,
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
;
1017 if (rResult
.aErrors
.getLength() > 0) // error found?
1019 // save the start and end positions of the line and the starting point
1022 const sal_Int32 nLineStart
= GetCursor()->GetPoint()->nContent
.GetIndex();
1024 const sal_Int32 nLineEnd
= GetCursor()->GetPoint()->nContent
.GetIndex();
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
)
1035 pChar
= aText
.getLength() ? aText
.getStr() + aText
.getLength() - 1 : nullptr;
1036 sal_Int32 nRight
= 0;
1037 while (pChar
&& *pChar
-- == CH_TXTATR_INWORD
)
1040 aPos
.nContent
= nBegin
+ nLeft
;
1041 pCursor
= GetCursor();
1042 *pCursor
->GetPoint() = aPos
;
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
);
1051 pCursor
->DeleteMark();
1052 SwIndex
& rContent
= GetCursor()->GetPoint()->nContent
;
1053 rContent
= nWordStart
;
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;
1063 pContentFrame
->GetCharRect( aEndRect
, *pCursor
->GetPoint(),&aState
);
1064 rSelectRect
= aStartRect
.Union( aEndRect
);
1073 bool SwEditShell::SpellSentence(svx::SpellPortions
& rPortions
, bool bIsGrammarCheck
)
1075 OSL_ENSURE( g_pSpellIter
, "SpellIter missing" );
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
1087 ///make SpellIter start with the current sentence when called next time
1088 void SwEditShell::PutSpellingToSentenceStart()
1090 OSL_ENSURE( g_pSpellIter
, "SpellIter missing" );
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
)
1108 void SwEditShell::MoveContinuationPosToEndOfCheckedSentence()
1110 // give hint that continuation position for spell/grammar checking is
1111 // at the end of this sentence
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" );
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 );
1140 SwPaM
*pCursor
= GetCursor();
1141 // save cursor position (which should be at the end of the current sentence)
1142 // for later restoration
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
;
1173 OSL_FAIL("ApplyChangedSentence: iterator positions broken" );
1177 if ( !pCursor
->HasMark() )
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
;
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
)
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
)
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())
1211 while(aCurrentNewPortion
!= rNewPortions
.begin());
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
;
1234 case SvtScriptType::ASIAN
: nLangWhichId
= RES_CHRATR_CJK_LANGUAGE
; break;
1235 case SvtScriptType::COMPLEX
: nLangWhichId
= RES_CHRATR_CTL_LANGUAGE
; break;
1238 SfxItemSet
aSet(GetAttrPool(), nLangWhichId
, nLangWhichId
, 0);
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!)
1257 // collapse cursor to the end of the modified sentence
1258 *pCursor
->Start() = *pCursor
->End();
1261 // in grammar check the current sentence has to be checked again
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 );
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() );
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() )
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
;
1305 aRedlines
.push_back(aAdd
);
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
)
1349 bool SwSpellIter::SpellSentence(svx::SpellPortions
& rPortions
, bool bIsGrammarCheck
)
1352 aLastPortions
.clear();
1353 aLastPositions
.clear();
1355 SwEditShell
*pMySh
= GetSh();
1359 OSL_ENSURE( GetEnd(), "SwSpellIter::SpellSentence without Start?");
1361 uno::Reference
< XSpellAlternatives
> xSpellRet
;
1362 linguistic2::ProofreadingResult aGrammarResult
;
1364 bool bGrammarErrorFound
= false;
1366 SwPaM
*pCursor
= pMySh
->GetCursor();
1367 if ( !pCursor
->HasMark() )
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
)
1388 SwPosition
* pNewPoint
= new SwPosition( *pCursor
->GetPoint() );
1389 SwPosition
* pNewMark
= new SwPosition( *pCursor
->GetMark() );
1391 SetCurr( pNewPoint
);
1392 SetCurrX( pNewMark
);
1396 pMySh
->Pop( false );
1397 pCursor
= pMySh
->GetCursor();
1398 if ( *pCursor
->GetPoint() > *pCursor
->GetMark() )
1399 pCursor
->Exchange();
1400 SwPosition
* pNew
= new SwPosition( *pCursor
->GetPoint() );
1402 pNew
= new SwPosition( *pCursor
->GetMark() );
1404 pNew
= new SwPosition( *GetStart() );
1406 pNew
= new SwPosition( *pNew
);
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();
1424 bool bStartSent
= pMySh
->GoStartSentence();
1425 SpellContentPositions aDeletedRedlines
= lcl_CollectDeletedRedlines(pMySh
);
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();
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] == ' ' )
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
1489 *pCursor
->GetPoint() = aSaveStartPos
;
1490 *pCursor
->GetMark() = *GetCurr();
1493 AddPortion(nullptr, nullptr, aDeletedRedlines
);
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())
1511 while(xSpellRet
.is());
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
;
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();
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
;
1562 case SvtScriptType::ASIAN
: nLangWhichId
= RES_CHRATR_CJK_LANGUAGE
; break;
1563 case SvtScriptType::COMPLEX
: nLangWhichId
= RES_CHRATR_CTL_LANGUAGE
; 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
;
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
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
;
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();
1625 pMySh
->GetSelectedText( sText
);
1626 if(!sText
.isEmpty())
1628 if(xAlt
.is() || pGrammarResult
!= nullptr)
1630 CreatePortion(xAlt
, pGrammarResult
, false, false);
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
))
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
);
1678 case RES_TXTATR_FIELD
:
1679 case RES_TXTATR_ANNOTATION
:
1680 case RES_TXTATR_FTN
:
1681 case RES_TXTATR_FLYCNT
:
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
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
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
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();
1739 *pCursor
->GetMark() = aStart
;
1740 CreatePortion(xAlt
, pGrammarResult
, false, false);
1745 void SwEditShell::IgnoreGrammarErrorAt( SwPaM
& rErrorPosition
)
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();
1757 if( aIdx
== aEndIdx
)
1758 nEnd
= rErrorPosition
.End()->nContent
.GetIndex();
1759 pWrong
= pNode
->GetGrammarCheck();
1761 pWrong
->RemoveEntry( nStart
, nEnd
);
1762 pWrong
= pNode
->GetWrong();
1764 pWrong
->RemoveEntry( nStart
, nEnd
);
1765 SwTextFrame::repaintTextFrames( *pNode
);
1772 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */