Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / svx / source / dialog / langbox.cxx
blobe552f68cc749d7caa4cfb634440ea0f4f2fd5037
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/XAvailableLocales.hpp>
21 #include <com/sun/star/linguistic2/XLinguServiceManager2.hpp>
22 #include <com/sun/star/linguistic2/XSpellChecker1.hpp>
23 #include <linguistic/misc.hxx>
24 #include <rtl/ustring.hxx>
25 #include <sal/log.hxx>
26 #include <unotools/localedatawrapper.hxx>
27 #include <tools/urlobj.hxx>
28 #include <svtools/langtab.hxx>
29 #include <i18nlangtag/mslangid.hxx>
30 #include <i18nlangtag/lang.h>
31 #include <editeng/unolingu.hxx>
32 #include <svl/languageoptions.hxx>
33 #include <svx/langbox.hxx>
34 #include <svx/dialmgr.hxx>
35 #include <svx/strings.hrc>
36 #include <bitmaps.hlst>
38 #include <comphelper/string.hxx>
39 #include <comphelper/processfactory.hxx>
40 #include <comphelper/scopeguard.hxx>
41 #include <vcl/svapp.hxx>
42 #include <vcl/settings.hxx>
44 using namespace ::com::sun::star::util;
45 using namespace ::com::sun::star::linguistic2;
46 using namespace ::com::sun::star::uno;
48 OUString GetDicInfoStr( std::u16string_view rName, const LanguageType nLang, bool bNeg )
50 INetURLObject aURLObj;
51 aURLObj.SetSmartProtocol( INetProtocol::File );
52 aURLObj.SetSmartURL( rName, INetURLObject::EncodeMechanism::All );
53 OUString aTmp( aURLObj.GetBase() + " " );
55 if ( bNeg )
57 aTmp += " (-) ";
60 if ( LANGUAGE_NONE == nLang )
61 aTmp += SvxResId(RID_SVXSTR_LANGUAGE_ALL);
62 else
64 aTmp += "[" + SvtLanguageTable::GetLanguageString( nLang ) + "]";
67 return aTmp;
70 // misc local helper functions
71 static void appendLocaleSeqToLangs(Sequence<css::lang::Locale> const& rSeq,
72 std::vector<LanguageType>& aLangs)
74 sal_Int32 nCount = rSeq.getLength();
76 aLangs.reserve(aLangs.size() + nCount);
78 std::transform(rSeq.begin(), rSeq.end(), std::back_inserter(aLangs),
79 [](const css::lang::Locale& rLocale) -> LanguageType {
80 return LanguageTag::convertToLanguageType(rLocale); });
83 static bool lcl_SeqHasLang( const Sequence< sal_Int16 > & rLangSeq, sal_Int16 nLang )
85 return rLangSeq.hasElements()
86 && std::find(rLangSeq.begin(), rLangSeq.end(), nLang) != rLangSeq.end();
89 namespace {
91 bool lcl_isPrerequisite(LanguageType nLangType, bool requireSublang)
93 return
94 nLangType != LANGUAGE_DONTKNOW &&
95 nLangType != LANGUAGE_SYSTEM &&
96 nLangType != LANGUAGE_NONE &&
97 nLangType != LANGUAGE_USER_KEYID &&
98 !MsLangId::isLegacy( nLangType) &&
99 (!requireSublang || MsLangId::getSubLanguage( nLangType));
102 bool lcl_isScriptTypeRequested( LanguageType nLangType, SvxLanguageListFlags nLangList )
104 return
105 bool(nLangList & SvxLanguageListFlags::ALL) ||
106 (bool(nLangList & SvxLanguageListFlags::WESTERN) &&
107 (SvtLanguageOptions::GetScriptTypeOfLanguage(nLangType) == SvtScriptType::LATIN)) ||
108 (bool(nLangList & SvxLanguageListFlags::CTL) &&
109 (SvtLanguageOptions::GetScriptTypeOfLanguage(nLangType) == SvtScriptType::COMPLEX)) ||
110 (bool(nLangList & SvxLanguageListFlags::CJK) &&
111 (SvtLanguageOptions::GetScriptTypeOfLanguage(nLangType) == SvtScriptType::ASIAN));
117 LanguageType SvxLanguageBox::get_active_id() const
119 OUString sLang = m_xControl->get_active_id();
120 if (!sLang.isEmpty())
121 return LanguageType(sLang.toInt32());
122 else
123 return LANGUAGE_DONTKNOW;
126 int SvxLanguageBox::find_id(const LanguageType eLangType) const
128 return m_xControl->find_id(OUString::number(static_cast<sal_uInt16>(eLangType)));
131 void SvxLanguageBox::set_id(int pos, const LanguageType eLangType)
133 m_xControl->set_id(pos, OUString::number(static_cast<sal_uInt16>(eLangType)));
136 LanguageType SvxLanguageBox::get_id(int pos) const
138 return LanguageType(m_xControl->get_id(pos).toInt32());
141 void SvxLanguageBox::remove_id(const LanguageType eLangType)
143 m_xControl->remove_id(OUString::number(static_cast<sal_uInt16>(eLangType)));
146 void SvxLanguageBox::append(const LanguageType eLangType, const OUString& rStr)
148 m_xControl->append(OUString::number(static_cast<sal_uInt16>(eLangType)), rStr);
151 void SvxLanguageBox::set_active_id(const LanguageType eLangType)
153 // If the core uses a LangID of an imported MS document and wants to select
154 // a language that is replaced, we need to select the replacement instead.
155 LanguageType nLang = MsLangId::getReplacementForObsoleteLanguage( eLangType);
157 sal_Int32 nAt = find_id( nLang );
159 if (nAt == -1)
161 InsertLanguage( nLang ); // on-the-fly-ID
162 nAt = find_id( nLang );
165 if (nAt != -1)
166 m_xControl->set_active(nAt);
169 void SvxLanguageBox::AddLanguages(const std::vector< LanguageType >& rLanguageTypes,
170 SvxLanguageListFlags nLangList, std::vector<weld::ComboBoxEntry>& rEntries, bool requireSublang)
172 for ( auto const & nLangType : rLanguageTypes )
174 if (lcl_isPrerequisite(nLangType, requireSublang))
176 LanguageType nLang = MsLangId::getReplacementForObsoleteLanguage( nLangType );
177 if (lcl_isScriptTypeRequested( nLang, nLangList))
179 int nAt = find_id(nLang);
180 if (nAt != -1)
181 continue;
182 weld::ComboBoxEntry aNewEntry(BuildEntry(nLang));
183 if (aNewEntry.sString.isEmpty())
184 continue;
185 rEntries.push_back(aNewEntry);
191 static void SortLanguages(std::vector<weld::ComboBoxEntry>& rEntries)
193 auto langLess = [](const weld::ComboBoxEntry& e1, const weld::ComboBoxEntry& e2)
195 if (e1.sId == e2.sId)
196 return false; // shortcut
197 // Make sure that e.g. generic 'Spanish {es}' goes before 'Spanish (Argentina)'.
198 // We can't depend on MsLangId::getPrimaryLanguage/getSubLanguage, because e.g.
199 // for generic Bosnian {bs}, the MS-LCID is 0x781A, and getSubLanguage is not 0.
200 // So we have to do the expensive LanguageTag construction.
201 LanguageTag lt1(LanguageType(e1.sId.toInt32())), lt2(LanguageType(e2.sId.toInt32()));
202 if (lt1.getLanguage() == lt2.getLanguage())
204 const bool isLangOnly1 = lt1.isIsoLocale() && lt1.getCountry().isEmpty();
205 const bool isLangOnly2 = lt2.isIsoLocale() && lt2.getCountry().isEmpty();
207 if (isLangOnly1)
209 // lt1 is a generic language-only tag
210 if (!isLangOnly2)
211 return true; // lt2 is not
213 else if (isLangOnly2)
215 // lt2 is a generic language-only tag, lt1 is not
216 return false;
219 // Do a normal string comparison for other cases
220 static const auto aSorter = comphelper::string::NaturalStringSorter(
221 comphelper::getProcessComponentContext(),
222 Application::GetSettings().GetUILanguageTag().getLocale());
223 return aSorter.compare(e1.sString, e2.sString) < 0;
226 std::sort(rEntries.begin(), rEntries.end(), langLess);
227 rEntries.erase(std::unique(rEntries.begin(), rEntries.end(),
228 [](const weld::ComboBoxEntry& e1, const weld::ComboBoxEntry& e2)
229 { return e1.sId == e2.sId; }),
230 rEntries.end());
233 void SvxLanguageBox::SetLanguageList(SvxLanguageListFlags nLangList, bool bHasLangNone,
234 bool bLangNoneIsLangAll, bool bCheckSpellAvail,
235 bool bDefaultLangExist, LanguageType eDefaultLangType,
236 sal_Int16 nDefaultType)
238 m_bHasLangNone = bHasLangNone;
239 m_bLangNoneIsLangAll = bLangNoneIsLangAll;
240 m_bWithCheckmark = bCheckSpellAvail;
242 m_xControl->freeze();
243 comphelper::ScopeGuard aThawGuard([this]() { m_xControl->thaw(); });
244 m_xControl->clear();
246 if (SvxLanguageListFlags::EMPTY == nLangList)
247 return;
249 bool bAddSeparator = false;
251 if (bHasLangNone)
253 m_xControl->append(BuildEntry(LANGUAGE_NONE));
254 bAddSeparator = true;
257 if (bDefaultLangExist)
259 m_xControl->append(BuildEntry(eDefaultLangType, nDefaultType));
260 bAddSeparator = true;
263 if (bAddSeparator)
264 m_xControl->append_separator("");
266 bool bAddAvailable = (!(nLangList & SvxLanguageListFlags::ONLY_KNOWN) &&
267 ((nLangList & SvxLanguageListFlags::ALL) ||
268 (nLangList & SvxLanguageListFlags::WESTERN) ||
269 (nLangList & SvxLanguageListFlags::CTL) ||
270 (nLangList & SvxLanguageListFlags::CJK)));
271 std::vector< LanguageType > aAvailLang;
272 Sequence< sal_Int16 > aSpellUsedLang;
273 if (bAddAvailable)
275 if (auto xAvail = LinguMgr::GetLngSvcMgr())
277 appendLocaleSeqToLangs(xAvail->getAvailableLocales(SN_SPELLCHECKER), aAvailLang);
278 appendLocaleSeqToLangs(xAvail->getAvailableLocales(SN_HYPHENATOR), aAvailLang);
279 appendLocaleSeqToLangs(xAvail->getAvailableLocales(SN_THESAURUS), aAvailLang);
282 if (SvxLanguageListFlags::SPELL_USED & nLangList)
284 Reference< XSpellChecker1 > xTmp1 = LinguMgr::GetSpellChecker();
285 if (xTmp1.is())
286 aSpellUsedLang = xTmp1->getLanguages();
289 std::vector<LanguageType> aKnown;
290 sal_uInt32 nCount;
291 if ( nLangList & SvxLanguageListFlags::ONLY_KNOWN )
293 aKnown = LocaleDataWrapper::getInstalledLanguageTypes();
294 nCount = aKnown.size();
296 else
298 nCount = SvtLanguageTable::GetLanguageEntryCount();
301 std::vector<weld::ComboBoxEntry> aEntries;
302 for ( sal_uInt32 i = 0; i < nCount; i++ )
304 LanguageType nLangType;
305 if ( nLangList & SvxLanguageListFlags::ONLY_KNOWN )
306 nLangType = aKnown[i];
307 else
308 nLangType = SvtLanguageTable::GetLanguageTypeAtIndex( i );
309 if ( lcl_isPrerequisite( nLangType, true ) &&
310 (lcl_isScriptTypeRequested( nLangType, nLangList) ||
311 (bool(nLangList & SvxLanguageListFlags::FBD_CHARS) &&
312 MsLangId::hasForbiddenCharacters(nLangType)) ||
313 (bool(nLangList & SvxLanguageListFlags::SPELL_USED) &&
314 lcl_SeqHasLang(aSpellUsedLang, static_cast<sal_uInt16>(nLangType)))
317 aEntries.push_back(BuildEntry(nLangType));
318 if (aEntries.back().sString.isEmpty())
319 aEntries.pop_back();
323 if (bAddAvailable)
325 // Spell checkers, hyphenators and thesauri may add language tags
326 // unknown so far.
327 AddLanguages(aAvailLang, nLangList, aEntries, true);
330 SortLanguages(aEntries);
331 m_xControl->insert_vector(aEntries, true);
334 void SvxLanguageBox::InsertLanguage(const LanguageType nLangType)
336 if (find_id(nLangType) != -1)
337 return;
338 weld::ComboBoxEntry aEntry = BuildEntry(nLangType);
339 if (aEntry.sString.isEmpty())
340 return;
341 m_xControl->append(aEntry);
344 void SvxLanguageBox::InsertLanguages(const std::vector<LanguageType>& rLanguageTypes)
346 std::vector<weld::ComboBoxEntry> entries;
347 AddLanguages(rLanguageTypes, SvxLanguageListFlags::ALL, entries, false);
348 SortLanguages(entries);
349 m_xControl->insert_vector(entries, true);
352 weld::ComboBoxEntry SvxLanguageBox::BuildEntry(const LanguageType nLangType, sal_Int16 nType)
354 LanguageType nLang = MsLangId::getReplacementForObsoleteLanguage(nLangType);
355 // For obsolete and to be replaced languages check whether an entry of the
356 // replacement already exists and if so don't add an entry with identical
357 // string as would be returned by SvtLanguageTable::GetString().
358 if (nLang != nLangType)
360 int nAt = find_id( nLang );
361 if (nAt != -1)
362 return weld::ComboBoxEntry("");
365 OUString aStrEntry = (LANGUAGE_NONE == nLang && m_bHasLangNone && m_bLangNoneIsLangAll)
366 ? SvxResId(RID_SVXSTR_LANGUAGE_ALL)
367 : SvtLanguageTable::GetLanguageString(nLang);
369 LanguageType nRealLang = nLang;
370 if (nRealLang == LANGUAGE_SYSTEM)
372 nRealLang = MsLangId::resolveSystemLanguageByScriptType(nRealLang, nType);
373 aStrEntry += " - " + SvtLanguageTable::GetLanguageString( nRealLang );
375 else if (nRealLang == LANGUAGE_USER_SYSTEM_CONFIG)
377 nRealLang = MsLangId::getSystemLanguage();
378 // Whatever we obtained, ensure a known supported locale.
379 nRealLang = LanguageTag(nRealLang).makeFallback().getLanguageType();
380 aStrEntry += " - " + SvtLanguageTable::GetLanguageString( nRealLang );
383 if (m_bWithCheckmark)
385 if (!m_xSpellUsedLang)
387 Reference<XSpellChecker1> xSpell = LinguMgr::GetSpellChecker();
388 if (xSpell.is())
389 m_xSpellUsedLang.reset(new Sequence<sal_Int16>(xSpell->getLanguages()));
392 bool bFound = m_xSpellUsedLang && lcl_SeqHasLang(*m_xSpellUsedLang, static_cast<sal_uInt16>(nRealLang));
394 return weld::ComboBoxEntry(aStrEntry, OUString::number(static_cast<sal_uInt16>(nLang)), bFound ? OUString(RID_SVXBMP_CHECKED) : OUString(RID_SVXBMP_NOTCHECKED));
396 else
397 return weld::ComboBoxEntry(aStrEntry, OUString::number(static_cast<sal_uInt16>(nLang)));
400 IMPL_LINK(SvxLanguageBox, ChangeHdl, weld::ComboBox&, rControl, void)
402 if (rControl.has_entry())
404 EditedAndValid eOldState = m_eEditedAndValid;
405 OUString aStr(rControl.get_active_text());
406 if (aStr.isEmpty())
407 m_eEditedAndValid = EditedAndValid::Invalid;
408 else
410 const int nPos = rControl.find_text(aStr);
411 if (nPos != -1)
413 int nStartSelectPos, nEndSelectPos;
414 rControl.get_entry_selection_bounds(nStartSelectPos, nEndSelectPos);
416 // Select the corresponding listbox entry if not current. This
417 // invalidates the Edit Selection thus has to happen between
418 // obtaining the Selection and setting the new Selection.
419 int nSelPos = m_xControl->get_active();
420 bool bSetEditSelection;
421 if (nSelPos == nPos)
422 bSetEditSelection = false;
423 else
425 m_xControl->set_active(nPos);
426 bSetEditSelection = true;
429 // If typing into the Edit control led us here, advance start of a
430 // full selection by one so the next character will already
431 // continue the string instead of having to type the same character
432 // again to start a new string. The selection is in reverse
433 // when obtained from the Edit control.
434 if (nEndSelectPos == 0)
436 OUString aText(m_xControl->get_active_text());
437 if (nStartSelectPos == aText.getLength())
439 ++nEndSelectPos;
440 bSetEditSelection = true;
444 if (bSetEditSelection)
445 rControl.select_entry_region(nStartSelectPos, nEndSelectPos);
447 m_eEditedAndValid = EditedAndValid::No;
449 else
451 OUString aCanonicalized;
452 bool bValid = LanguageTag::isValidBcp47( aStr, &aCanonicalized, LanguageTag::PrivateUse::ALLOW_ART_X);
453 m_eEditedAndValid = (bValid ? EditedAndValid::Valid : EditedAndValid::Invalid);
454 if (bValid && aCanonicalized != aStr)
456 m_xControl->set_entry_text(aCanonicalized);
457 const auto nCursorPos = aCanonicalized.getLength();
458 m_xControl->select_entry_region(nCursorPos, nCursorPos);
462 if (eOldState != m_eEditedAndValid)
464 if (m_eEditedAndValid == EditedAndValid::Invalid)
465 rControl.set_entry_message_type(weld::EntryMessageType::Error);
466 else
467 rControl.set_entry_message_type(weld::EntryMessageType::Normal);
470 m_aChangeHdl.Call(rControl);
473 SvxLanguageBox::SvxLanguageBox(std::unique_ptr<weld::ComboBox> pControl)
474 : m_xControl(std::move(pControl))
475 , m_eSavedLanguage(LANGUAGE_DONTKNOW)
476 , m_eEditedAndValid(EditedAndValid::No)
477 , m_bHasLangNone(false)
478 , m_bLangNoneIsLangAll(false)
479 , m_bWithCheckmark(false)
481 m_xControl->connect_changed(LINK(this, SvxLanguageBox, ChangeHdl));
484 SvxLanguageBox* SvxLanguageBox::SaveEditedAsEntry(SvxLanguageBox* ppBoxes[3])
486 if (m_eEditedAndValid != EditedAndValid::Valid)
487 return this;
489 LanguageTag aLanguageTag(m_xControl->get_active_text());
490 LanguageType nLang = aLanguageTag.getLanguageType();
491 if (nLang == LANGUAGE_DONTKNOW)
493 SAL_WARN( "svx.dialog", "SvxLanguageBox::SaveEditedAsEntry: unknown tag");
494 return this;
497 for (size_t i = 0; i < 3; ++i)
499 SvxLanguageBox* pBox = ppBoxes[i];
500 if (!pBox)
501 continue;
503 const int nPos = pBox->find_id( nLang);
504 if (nPos != -1)
506 // Already present but with a different string or in another list.
507 pBox->m_xControl->set_active(nPos);
508 return pBox;
512 if (SvtLanguageTable::HasLanguageType( nLang))
514 // In SvtLanguageTable but not in SvxLanguageBox. On purpose? This
515 // may be an entry with different settings.
516 SAL_WARN( "svx.dialog", "SvxLanguageBox::SaveEditedAsEntry: already in SvtLanguageTable: " <<
517 SvtLanguageTable::GetLanguageString( nLang) << ", " << nLang);
519 else
521 // Add to SvtLanguageTable first. This at an on-the-fly LanguageTag
522 // also sets the ScriptType needed below.
523 SvtLanguageTable::AddLanguageTag( aLanguageTag );
526 // Add to the proper list.
527 SvxLanguageBox* pBox = nullptr;
528 switch (MsLangId::getScriptType(nLang))
530 default:
531 case css::i18n::ScriptType::LATIN:
532 pBox = ppBoxes[0];
533 break;
534 case css::i18n::ScriptType::ASIAN:
535 pBox = ppBoxes[1];
536 break;
537 case css::i18n::ScriptType::COMPLEX:
538 pBox = ppBoxes[2];
539 break;
541 if (!pBox)
542 pBox = this;
543 pBox->InsertLanguage(nLang);
545 // Select it.
546 const int nPos = pBox->find_id(nLang);
547 if (nPos != -1)
548 pBox->m_xControl->set_active(nPos);
550 return pBox;
553 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */