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 <config_wasm_strip.h>
22 #include <rtl/ustring.hxx>
23 #include <i18nlangtag/languagetag.hxx>
24 #include <vcl/svapp.hxx>
25 #include <vcl/weld.hxx>
26 #include <svtools/langtab.hxx>
28 #include <vcl/errinf.hxx>
29 #include <editeng/unolingu.hxx>
30 #include <com/sun/star/frame/XStorable.hpp>
31 #include <com/sun/star/linguistic2/XLinguProperties.hpp>
32 #include <com/sun/star/linguistic2/XSpellChecker1.hpp>
33 #include <com/sun/star/linguistic2/XHyphenator.hpp>
34 #include <com/sun/star/linguistic2/XSearchableDictionaryList.hpp>
35 #include <com/sun/star/linguistic2/XDictionary.hpp>
37 #include <editeng/svxenum.hxx>
38 #include <editeng/splwrap.hxx>
39 #include <editeng/edtdlg.hxx>
40 #include <editeng/eerdll.hxx>
41 #include <editeng/editrids.hrc>
42 #include <editeng/editerr.hxx>
47 using namespace ::com::sun::star
;
48 using namespace ::com::sun::star::uno
;
49 using namespace ::com::sun::star::beans
;
50 using namespace ::com::sun::star::linguistic2
;
53 // misc functions ---------------------------------------------
55 void SvxPrepareAutoCorrect( OUString
&rOldText
, std::u16string_view rNewText
)
57 // This function should be used to strip (or add) trailing '.' from
58 // the strings before passing them on to the autocorrect function in
59 // order that the autocorrect function will hopefully
60 // works properly with normal words and abbreviations (with trailing '.')
61 // independent of if they are at the end of the sentence or not.
63 // rOldText: text to be replaced
64 // rNewText: replacement text
66 sal_Int32 nOldLen
= rOldText
.getLength();
67 sal_Int32 nNewLen
= rNewText
.size();
68 if (nOldLen
&& nNewLen
)
70 bool bOldHasDot
= '.' == rOldText
[ nOldLen
- 1 ],
71 bNewHasDot
= '.' == rNewText
[ nNewLen
- 1 ];
72 if (bOldHasDot
&& !bNewHasDot
73 /*this is: !(bOldHasDot && bNewHasDot) && bOldHasDot*/)
74 rOldText
= rOldText
.copy( 0, nOldLen
- 1 );
78 #define SVX_LANG_NEED_CHECK 0
80 #define SVX_LANG_MISSING 2
81 #define SVX_LANG_MISSING_DO_WARN 3
83 typedef std::map
< LanguageType
, sal_uInt16
> LangCheckState_map_t
;
85 static LangCheckState_map_t
& GetLangCheckState()
87 static LangCheckState_map_t aLangCheckState
;
88 return aLangCheckState
;
91 void SvxSpellWrapper::ShowLanguageErrors()
93 // display message boxes for languages not available for
94 // spellchecking or hyphenation
95 LangCheckState_map_t
&rLCS
= GetLangCheckState();
96 for (auto const& elem
: rLCS
)
98 LanguageType nLang
= elem
.first
;
99 sal_uInt16 nVal
= elem
.second
;
100 sal_uInt16 nTmpSpell
= nVal
& 0x00FF;
101 sal_uInt16 nTmpHyph
= (nVal
>> 8) & 0x00FF;
103 if (SVX_LANG_MISSING_DO_WARN
== nTmpSpell
)
105 OUString
aErr( SvtLanguageTable::GetLanguageString( nLang
) );
106 ErrorHandler::HandleError(
107 ErrCodeMsg( ERRCODE_SVX_LINGU_LANGUAGENOTEXISTS
, aErr
) );
108 nTmpSpell
= SVX_LANG_MISSING
;
110 if (SVX_LANG_MISSING_DO_WARN
== nTmpHyph
)
112 OUString
aErr( SvtLanguageTable::GetLanguageString( nLang
) );
113 ErrorHandler::HandleError(
114 ErrCodeMsg( ERRCODE_SVX_LINGU_LANGUAGENOTEXISTS
, aErr
) );
115 nTmpHyph
= SVX_LANG_MISSING
;
118 rLCS
[ nLang
] = (nTmpHyph
<< 8) | nTmpSpell
;
123 SvxSpellWrapper::~SvxSpellWrapper()
127 /*--------------------------------------------------------------------
128 * Description: Constructor, the test sequence is determined
130 * !bStart && !bOtherCntnt: BODY_END, BODY_START, OTHER
131 * !bStart && bOtherCntnt: OTHER, BODY
132 * bStart && !bOtherCntnt: BODY_END, OTHER
133 * bStart && bOtherCntnt: OTHER
135 --------------------------------------------------------------------*/
137 SvxSpellWrapper::SvxSpellWrapper( weld::Widget
* pWn
,
138 const bool bStart
, const bool bIsAllRight
) :
141 bOtherCntnt ( false ),
143 bRevAllowed ( true ),
144 bAllRight ( bIsAllRight
)
146 Reference
< linguistic2::XLinguProperties
> xProp( LinguMgr::GetLinguPropertySet() );
147 bool bWrapReverse
= xProp
.is() && xProp
->getIsWrapReverse();
148 bReverse
= bWrapReverse
;
149 bStartDone
= !bReverse
&& bStart
;
150 bEndDone
= bReverse
&& bStart
;
154 SvxSpellWrapper::SvxSpellWrapper( weld::Widget
* pWn
,
155 Reference
< XHyphenator
> const &xHyphenator
,
156 const bool bStart
, const bool bOther
) :
158 xHyph ( xHyphenator
),
159 bOtherCntnt ( bOther
),
161 bStartDone ( bOther
|| bStart
),
163 bStartChk ( bOther
),
164 bRevAllowed ( false ),
170 sal_Int16
SvxSpellWrapper::CheckSpellLang(
171 Reference
< XSpellChecker1
> const & xSpell
, LanguageType nLang
)
173 LangCheckState_map_t
&rLCS
= GetLangCheckState();
175 LangCheckState_map_t::iterator
aIt( rLCS
.find( nLang
) );
176 sal_uInt16 nVal
= aIt
== rLCS
.end() ? SVX_LANG_NEED_CHECK
: aIt
->second
;
178 if (aIt
== rLCS
.end())
179 rLCS
[ nLang
] = nVal
;
181 if (SVX_LANG_NEED_CHECK
== (nVal
& 0x00FF))
183 sal_uInt16 nTmpVal
= SVX_LANG_MISSING_DO_WARN
;
184 if (xSpell
.is() && xSpell
->hasLanguage( static_cast<sal_uInt16
>(nLang
) ))
185 nTmpVal
= SVX_LANG_OK
;
189 rLCS
[ nLang
] = nVal
;
192 return static_cast<sal_Int16
>(nVal
);
195 sal_Int16
SvxSpellWrapper::CheckHyphLang(
196 Reference
< XHyphenator
> const & xHyph
, LanguageType nLang
)
198 LangCheckState_map_t
&rLCS
= GetLangCheckState();
200 LangCheckState_map_t::iterator
aIt( rLCS
.find( nLang
) );
201 sal_uInt16 nVal
= aIt
== rLCS
.end() ? 0 : aIt
->second
;
203 if (aIt
== rLCS
.end())
204 rLCS
[ nLang
] = nVal
;
206 if (SVX_LANG_NEED_CHECK
== ((nVal
>> 8) & 0x00FF))
208 sal_uInt16 nTmpVal
= SVX_LANG_MISSING_DO_WARN
;
209 if (xHyph
.is() && xHyph
->hasLocale( LanguageTag::convertToLocale( nLang
) ))
210 nTmpVal
= SVX_LANG_OK
;
212 nVal
|= nTmpVal
<< 8;
214 rLCS
[ nLang
] = nVal
;
217 return static_cast<sal_Int16
>(nVal
);
221 void SvxSpellWrapper::SpellStart( SvxSpellArea
/*eSpell*/ )
222 { // Here, the necessary preparations be made for SpellContinue in the
226 bool SvxSpellWrapper::SpellMore()
228 return false; // Should additional documents be examined?
232 void SvxSpellWrapper::SpellEnd()
233 { // Area is complete, tidy up if necessary
235 // display error for last language not found
236 ShowLanguageErrors();
239 void SvxSpellWrapper::SpellContinue()
243 void SvxSpellWrapper::ReplaceAll( const OUString
& )
244 { // Replace Word from the Replace list
247 void SvxSpellWrapper::InsertHyphen( const sal_Int32
)
248 { // inserting and deleting Hyphen
251 // Testing of the document areas in the order specified by the flags
252 void SvxSpellWrapper::SpellDocument( )
254 #if ENABLE_WASM_STRIP_HUNSPELL
260 SpellStart( SvxSpellArea::Other
);
264 bStartChk
= bReverse
;
265 SpellStart( bReverse
? SvxSpellArea::BodyStart
: SvxSpellArea::BodyEnd
);
268 if ( !FindSpellError() )
271 Reference
< XHyphenatedWord
> xHyphWord( GetLast(), UNO_QUERY
);
275 EditAbstractDialogFactory
* pFact
= EditAbstractDialogFactory::Create();
276 ScopedVclPtr
<AbstractHyphenWordDialog
> pDlg(pFact
->CreateHyphenWordDialog(
278 xHyphWord
->getWord(),
279 LanguageTag( xHyphWord
->getLocale() ).getLanguageType(),
287 // Select the next area
290 bool SvxSpellWrapper::SpellNext( )
292 Reference
< linguistic2::XLinguProperties
> xProp( LinguMgr::GetLinguPropertySet() );
293 bool bWrapReverse
= xProp
.is() && xProp
->getIsWrapReverse();
294 bool bActRev
= bRevAllowed
&& bWrapReverse
;
296 // bActRev is the direction after Spell checking, bReverse is the one
298 if( bActRev
== bReverse
)
299 { // No change of direction, thus is the
300 if( bStartChk
) // desired area ( bStartChk )
301 bStartDone
= true; // completely processed.
305 else if( bReverse
== bStartChk
) //For a change of direction, an area can
306 { // be processed during certain circumstances
307 if( bStartChk
) // If the first part is spell checked in backwards
308 bEndDone
= true; // and this is reversed in the process, then
309 else // then the end part is processed (and vice-versa).
314 if( bOtherCntnt
&& bStartDone
&& bEndDone
) // Document has been fully checked?
316 if ( SpellMore() ) // spell check another document?
319 bStartDone
= !bReverse
;
321 SpellStart( SvxSpellArea::Body
);
332 SpellStart( SvxSpellArea::Body
);
335 else if ( bStartDone
&& bEndDone
)
337 if ( SpellMore() ) // check another document?
340 bStartDone
= !bReverse
;
342 SpellStart( SvxSpellArea::Body
);
348 // a BODY_area done, ask for the other BODY_area
351 TranslateId pResId
= bReverse
? RID_SVXSTR_QUERY_BW_CONTINUE
: RID_SVXSTR_QUERY_CONTINUE
;
352 std::unique_ptr
<weld::MessageDialog
> xBox(Application::CreateMessageDialog(pWin
,
353 VclMessageType::Question
, VclButtonsType::YesNo
,
355 if (xBox
->run() != RET_YES
)
357 // sacrifice the other area if necessary ask for special area
358 xWait
.reset(new weld::WaitObject(pWin
));
359 bStartDone
= bEndDone
= true;
364 bStartChk
= !bStartDone
;
365 SpellStart( bStartChk
? SvxSpellArea::BodyStart
: SvxSpellArea::BodyEnd
);
368 xWait
.reset(new weld::WaitObject(pWin
));
374 Reference
< XDictionary
> SvxSpellWrapper::GetAllRightDic()
376 Reference
< XDictionary
> xDic
;
378 Reference
< XSearchableDictionaryList
> xDicList( LinguMgr::GetDictionaryList() );
381 Sequence
< Reference
< XDictionary
> > aDics( xDicList
->getDictionaries() );
382 const Reference
< XDictionary
> *pDic
= aDics
.getConstArray();
383 sal_Int32 nCount
= aDics
.getLength();
386 while (!xDic
.is() && i
< nCount
)
388 Reference
< XDictionary
> xTmp
= pDic
[i
];
391 if ( xTmp
->isActive() &&
392 xTmp
->getDictionaryType() != DictionaryType_NEGATIVE
&&
393 LanguageTag( xTmp
->getLocale() ).getLanguageType() == LANGUAGE_NONE
)
395 Reference
< frame::XStorable
> xStor( xTmp
, UNO_QUERY
);
396 if (xStor
.is() && xStor
->hasLocation() && !xStor
->isReadonly())
407 xDic
= LinguMgr::GetStandardDic();
409 xDic
->setActive( true );
417 bool SvxSpellWrapper::FindSpellError()
419 ShowLanguageErrors();
421 xWait
.reset(new weld::WaitObject(pWin
));
424 Reference
< XDictionary
> xAllRightDic
;
426 xAllRightDic
= GetAllRightDic();
432 Reference
< XSpellAlternatives
> xAlt( GetLast(), UNO_QUERY
);
433 Reference
< XHyphenatedWord
> xHyphWord( GetLast(), UNO_QUERY
);
437 if (IsAllRight() && xAllRightDic
.is())
439 xAllRightDic
->add( xAlt
->getWord(), false, OUString() );
443 // look up in ChangeAllList for misspelled word
444 Reference
< XDictionary
> xChangeAllList
=
445 LinguMgr::GetChangeAllList();
446 Reference
< XDictionaryEntry
> xEntry
;
447 if (xChangeAllList
.is())
448 xEntry
= xChangeAllList
->getEntry( xAlt
->getWord() );
452 // replace word without asking
453 ReplaceAll( xEntry
->getReplacementText() );
459 else if (xHyphWord
.is())
464 bSpell
= SpellNext();
468 return GetLast().is();
472 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */