Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <com/sun/star/linguistic2/XAvailableLocales.hpp>
21 : #include <com/sun/star/i18n/ScriptType.hpp>
22 : #include <linguistic/misc.hxx>
23 : #include <rtl/ustring.hxx>
24 : #include <unotools/localedatawrapper.hxx>
25 : #include <tools/urlobj.hxx>
26 : #include <svtools/langtab.hxx>
27 : #include <tools/shl.hxx>
28 : #include <i18nlangtag/mslangid.hxx>
29 : #include <i18nlangtag/lang.h>
30 : #include <editeng/scripttypeitem.hxx>
31 : #include <editeng/unolingu.hxx>
32 : #include <svx/langbox.hxx>
33 : #include <svx/dialmgr.hxx>
34 : #include <svx/dialogs.hrc>
35 : #include <vcl/builder.hxx>
36 :
37 : using namespace ::com::sun::star::util;
38 : using namespace ::com::sun::star::lang;
39 : using namespace ::com::sun::star::linguistic2;
40 : using namespace ::com::sun::star::uno;
41 :
42 :
43 :
44 0 : OUString GetDicInfoStr( const OUString& rName, const sal_uInt16 nLang, bool bNeg )
45 : {
46 0 : INetURLObject aURLObj;
47 0 : aURLObj.SetSmartProtocol( INET_PROT_FILE );
48 0 : aURLObj.SetSmartURL( rName, INetURLObject::ENCODE_ALL );
49 0 : OUString aTmp( aURLObj.GetBase() );
50 0 : aTmp += " ";
51 :
52 0 : if ( bNeg )
53 : {
54 0 : aTmp += " (-) ";
55 : }
56 :
57 0 : if ( LANGUAGE_NONE == nLang )
58 0 : aTmp += SVX_RESSTR(RID_SVXSTR_LANGUAGE_ALL);
59 : else
60 : {
61 0 : aTmp += "[";
62 0 : aTmp += SvtLanguageTable::GetLanguageString( (LanguageType)nLang );
63 0 : aTmp += "]";
64 : }
65 :
66 0 : return aTmp;
67 : }
68 :
69 :
70 : // misc local helper functions
71 :
72 :
73 0 : static Sequence< sal_Int16 > lcl_LocaleSeqToLangSeq( Sequence< Locale > &rSeq )
74 : {
75 0 : const Locale *pLocale = rSeq.getConstArray();
76 0 : sal_Int32 nCount = rSeq.getLength();
77 :
78 0 : Sequence< sal_Int16 > aLangs( nCount );
79 0 : sal_Int16 *pLang = aLangs.getArray();
80 0 : for (sal_Int32 i = 0; i < nCount; ++i)
81 : {
82 0 : pLang[i] = LanguageTag::convertToLanguageType( pLocale[i] );
83 :
84 : }
85 :
86 0 : return aLangs;
87 : }
88 :
89 :
90 0 : static bool lcl_SeqHasLang( const Sequence< sal_Int16 > & rLangSeq, sal_Int16 nLang )
91 : {
92 0 : sal_Int32 i = -1;
93 0 : sal_Int32 nLen = rLangSeq.getLength();
94 0 : if (nLen)
95 : {
96 0 : const sal_Int16 *pLang = rLangSeq.getConstArray();
97 0 : for (i = 0; i < nLen; ++i)
98 : {
99 0 : if (nLang == pLang[i])
100 0 : break;
101 : }
102 : }
103 0 : return i >= 0 && i < nLen;
104 : }
105 :
106 :
107 : // class SvxLanguageBox
108 :
109 :
110 0 : sal_Int32 TypeToPos_Impl( LanguageType eType, const ListBox& rLb )
111 : {
112 0 : sal_Int32 nPos = LISTBOX_ENTRY_NOTFOUND;
113 0 : sal_Int32 nCount = rLb.GetEntryCount();
114 :
115 0 : for ( sal_Int32 i=0; nPos == LISTBOX_ENTRY_NOTFOUND && i<nCount; i++ )
116 0 : if ( eType == LanguageType((sal_uIntPtr)rLb.GetEntryData(i)) )
117 0 : nPos = i;
118 :
119 0 : return nPos;
120 : }
121 :
122 0 : SvxLanguageBox::SvxLanguageBox( Window* pParent, WinBits nBits, bool bCheck )
123 : : ListBox( pParent, nBits )
124 : , m_pSpellUsedLang( NULL )
125 0 : , m_bWithCheckmark( bCheck )
126 : {
127 0 : Init();
128 0 : }
129 :
130 0 : extern "C" SAL_DLLPUBLIC_EXPORT Window* SAL_CALL makeSvxLanguageBox(Window *pParent, VclBuilder::stringmap &rMap)
131 : {
132 0 : WinBits nBits = WB_LEFT|WB_VCENTER|WB_3DLOOK|WB_TABSTOP;
133 0 : bool bDropdown = VclBuilder::extractDropdown(rMap);
134 0 : if (bDropdown)
135 0 : nBits |= WB_DROPDOWN;
136 : else
137 0 : nBits |= WB_BORDER;
138 0 : SvxLanguageBox *pListBox = new SvxLanguageBox(pParent, nBits);
139 0 : pListBox->EnableAutoSize(true);
140 0 : return pListBox;
141 : }
142 :
143 :
144 0 : void SvxLanguageBox::Init()
145 : {
146 0 : m_pLangTable = new SvtLanguageTable;
147 0 : m_aNotCheckedImage = Image( SVX_RES( RID_SVXIMG_NOTCHECKED ) );
148 0 : m_aCheckedImage = Image( SVX_RES( RID_SVXIMG_CHECKED ) );
149 0 : m_aAllString = SVX_RESSTR( RID_SVXSTR_LANGUAGE_ALL );
150 0 : m_nLangList = LANG_LIST_EMPTY;
151 0 : m_bHasLangNone = false;
152 0 : m_bLangNoneIsLangAll = false;
153 :
154 : // display entries sorted
155 0 : SetStyle( GetStyle() | WB_SORT );
156 :
157 0 : if ( m_bWithCheckmark )
158 : {
159 0 : SvtLanguageTable aLangTable;
160 0 : sal_uInt32 nCount = aLangTable.GetEntryCount();
161 0 : for ( sal_uInt32 i = 0; i < nCount; i++ )
162 : {
163 0 : LanguageType nLangType = aLangTable.GetTypeAtIndex( i );
164 :
165 0 : bool bInsert = true;
166 0 : if ((LANGUAGE_DONTKNOW == nLangType) ||
167 : (LANGUAGE_SYSTEM == nLangType))
168 : {
169 0 : bInsert = false;
170 : }
171 :
172 0 : if ( bInsert )
173 0 : InsertLanguage( nLangType );
174 : }
175 0 : m_nLangList = LANG_LIST_ALL;
176 : }
177 0 : }
178 :
179 :
180 0 : SvxLanguageBox::~SvxLanguageBox()
181 : {
182 0 : delete m_pSpellUsedLang;
183 0 : delete m_pLangTable;
184 0 : }
185 :
186 :
187 :
188 0 : sal_Int32 SvxLanguageBox::ImplInsertImgEntry( const OUString& rEntry, sal_Int32 nPos, bool bChecked )
189 : {
190 0 : sal_Int32 nRet = 0;
191 0 : if( !bChecked )
192 0 : nRet = InsertEntry( rEntry, m_aNotCheckedImage, nPos );
193 : else
194 0 : nRet = InsertEntry( rEntry, m_aCheckedImage, nPos );
195 0 : return nRet;
196 : }
197 :
198 :
199 :
200 0 : void SvxLanguageBox::SetLanguageList( sal_Int16 nLangList,
201 : bool bHasLangNone, bool bLangNoneIsLangAll, bool bCheckSpellAvail )
202 : {
203 0 : Clear();
204 :
205 0 : m_nLangList = nLangList;
206 0 : m_bHasLangNone = bHasLangNone;
207 0 : m_bLangNoneIsLangAll = bLangNoneIsLangAll;
208 0 : m_bWithCheckmark = bCheckSpellAvail;
209 :
210 0 : if ( LANG_LIST_EMPTY != nLangList )
211 : {
212 0 : Sequence< sal_Int16 > aSpellAvailLang;
213 0 : Sequence< sal_Int16 > aHyphAvailLang;
214 0 : Sequence< sal_Int16 > aThesAvailLang;
215 0 : Sequence< sal_Int16 > aSpellUsedLang;
216 0 : Sequence< sal_Int16 > aHyphUsedLang;
217 0 : Sequence< sal_Int16 > aThesUsedLang;
218 0 : Reference< XAvailableLocales > xAvail( LinguMgr::GetLngSvcMgr(), UNO_QUERY );
219 0 : if (xAvail.is())
220 : {
221 0 : Sequence< Locale > aTmp;
222 :
223 0 : if (LANG_LIST_SPELL_AVAIL & nLangList)
224 : {
225 0 : aTmp = xAvail->getAvailableLocales( SN_SPELLCHECKER );
226 0 : aSpellAvailLang = lcl_LocaleSeqToLangSeq( aTmp );
227 : }
228 0 : if (LANG_LIST_HYPH_AVAIL & nLangList)
229 : {
230 0 : aTmp = xAvail->getAvailableLocales( SN_HYPHENATOR );
231 0 : aHyphAvailLang = lcl_LocaleSeqToLangSeq( aTmp );
232 : }
233 0 : if (LANG_LIST_THES_AVAIL & nLangList)
234 : {
235 0 : aTmp = xAvail->getAvailableLocales( SN_THESAURUS );
236 0 : aThesAvailLang = lcl_LocaleSeqToLangSeq( aTmp );
237 0 : }
238 : }
239 0 : if (LANG_LIST_SPELL_USED & nLangList)
240 : {
241 0 : Reference< XSpellChecker1 > xTmp1( SvxGetSpellChecker(), UNO_QUERY );
242 0 : if (xTmp1.is())
243 0 : aSpellUsedLang = xTmp1->getLanguages();
244 : }
245 0 : if (LANG_LIST_HYPH_USED & nLangList)
246 : {
247 0 : Reference< XHyphenator > xTmp( SvxGetHyphenator() );
248 0 : if (xTmp.is()) {
249 0 : Sequence < Locale > aLocaleSequence( xTmp->getLocales() );
250 0 : aHyphUsedLang = lcl_LocaleSeqToLangSeq( aLocaleSequence );
251 0 : }
252 : }
253 0 : if (LANG_LIST_THES_USED & nLangList)
254 : {
255 0 : Reference< XThesaurus > xTmp( SvxGetThesaurus() );
256 0 : if (xTmp.is()) {
257 0 : Sequence < Locale > aLocaleSequence( xTmp->getLocales() );
258 0 : aThesUsedLang = lcl_LocaleSeqToLangSeq( aLocaleSequence );
259 0 : }
260 : }
261 :
262 0 : SvtLanguageTable aLangTable;
263 0 : ::com::sun::star::uno::Sequence< sal_uInt16 > xKnown;
264 : const sal_uInt16* pKnown;
265 : sal_uInt32 nCount;
266 0 : if ( nLangList & LANG_LIST_ONLY_KNOWN )
267 : {
268 0 : xKnown = LocaleDataWrapper::getInstalledLanguageTypes();
269 0 : pKnown = xKnown.getConstArray();
270 0 : nCount = xKnown.getLength();
271 : }
272 : else
273 : {
274 0 : nCount = aLangTable.GetEntryCount();
275 0 : pKnown = NULL;
276 : }
277 0 : for ( sal_uInt32 i = 0; i < nCount; i++ )
278 : {
279 : LanguageType nLangType;
280 0 : if ( nLangList & LANG_LIST_ONLY_KNOWN )
281 0 : nLangType = pKnown[i];
282 : else
283 0 : nLangType = aLangTable.GetTypeAtIndex( i );
284 0 : if ( nLangType != LANGUAGE_DONTKNOW &&
285 0 : nLangType != LANGUAGE_SYSTEM &&
286 0 : nLangType != LANGUAGE_NONE &&
287 0 : (MsLangId::getSubLanguage( nLangType) != 0 ||
288 0 : (nLangList & LANG_LIST_ALSO_PRIMARY_ONLY)) &&
289 0 : ((nLangList & LANG_LIST_ALL) != 0 ||
290 0 : ((nLangList & LANG_LIST_WESTERN) != 0 &&
291 0 : (SvtLanguageOptions::GetScriptTypeOfLanguage(nLangType) ==
292 0 : SCRIPTTYPE_LATIN)) ||
293 0 : ((nLangList & LANG_LIST_CTL) != 0 &&
294 0 : (SvtLanguageOptions::GetScriptTypeOfLanguage(nLangType) ==
295 0 : SCRIPTTYPE_COMPLEX)) ||
296 0 : ((nLangList & LANG_LIST_CJK) != 0 &&
297 0 : (SvtLanguageOptions::GetScriptTypeOfLanguage(nLangType) ==
298 0 : SCRIPTTYPE_ASIAN)) ||
299 0 : ((nLangList & LANG_LIST_FBD_CHARS) != 0 &&
300 0 : MsLangId::hasForbiddenCharacters(nLangType)) ||
301 0 : ((nLangList & LANG_LIST_SPELL_AVAIL) != 0 &&
302 0 : lcl_SeqHasLang(aSpellAvailLang, nLangType)) ||
303 0 : ((nLangList & LANG_LIST_HYPH_AVAIL) != 0 &&
304 0 : lcl_SeqHasLang(aHyphAvailLang, nLangType)) ||
305 0 : ((nLangList & LANG_LIST_THES_AVAIL) != 0 &&
306 0 : lcl_SeqHasLang(aThesAvailLang, nLangType)) ||
307 0 : ((nLangList & LANG_LIST_SPELL_USED) != 0 &&
308 0 : lcl_SeqHasLang(aSpellUsedLang, nLangType)) ||
309 0 : ((nLangList & LANG_LIST_HYPH_USED) != 0 &&
310 0 : lcl_SeqHasLang(aHyphUsedLang, nLangType)) ||
311 0 : ((nLangList & LANG_LIST_THES_USED) != 0 &&
312 0 : lcl_SeqHasLang(aThesUsedLang, nLangType))) )
313 0 : InsertLanguage( nLangType );
314 : }
315 :
316 0 : if (bHasLangNone)
317 0 : InsertLanguage( LANGUAGE_NONE );
318 : }
319 0 : }
320 :
321 :
322 :
323 0 : sal_Int32 SvxLanguageBox::InsertLanguage( const LanguageType nLangType, sal_Int32 nPos )
324 : {
325 0 : return ImplInsertLanguage( nLangType, nPos, ::com::sun::star::i18n::ScriptType::WEAK );
326 : }
327 :
328 :
329 :
330 0 : sal_Int32 SvxLanguageBox::ImplInsertLanguage( const LanguageType nLangType, sal_Int32 nPos, sal_Int16 nType )
331 : {
332 0 : LanguageType nLang = MsLangId::getReplacementForObsoleteLanguage( nLangType);
333 : // For obsolete and to be replaced languages check whether an entry of the
334 : // replacement already exists and if so don't add an entry with identical
335 : // string as would be returned by SvtLanguageTable::GetString().
336 0 : if (nLang != nLangType)
337 : {
338 0 : sal_Int32 nAt = TypeToPos_Impl( nLang, *this );
339 0 : if ( nAt != LISTBOX_ENTRY_NOTFOUND )
340 0 : return nAt;
341 : }
342 :
343 0 : OUString aStrEntry = m_pLangTable->GetString( nLang );
344 0 : if (LANGUAGE_NONE == nLang && m_bHasLangNone && m_bLangNoneIsLangAll)
345 0 : aStrEntry = m_aAllString;
346 :
347 0 : LanguageType nRealLang = nLang;
348 0 : if (nRealLang == LANGUAGE_SYSTEM)
349 : {
350 0 : nRealLang = MsLangId::resolveSystemLanguageByScriptType(nRealLang, nType);
351 0 : aStrEntry += " - ";
352 0 : aStrEntry += m_pLangTable->GetString( nRealLang );
353 0 : } else if (nRealLang == LANGUAGE_USER_SYSTEM_CONFIG) {
354 0 : nRealLang = MsLangId::getSystemLanguage();
355 0 : aStrEntry += " - ";
356 0 : aStrEntry += m_pLangTable->GetString( nRealLang );
357 : }
358 :
359 0 : aStrEntry = ApplyLreOrRleEmbedding( aStrEntry );
360 :
361 0 : sal_Int32 nAt = 0;
362 0 : if ( m_bWithCheckmark )
363 : {
364 0 : bool bFound = false;
365 :
366 0 : if (!m_pSpellUsedLang)
367 : {
368 0 : Reference< XSpellChecker1 > xSpell( SvxGetSpellChecker(), UNO_QUERY );
369 0 : if ( xSpell.is() )
370 0 : m_pSpellUsedLang = new Sequence< sal_Int16 >( xSpell->getLanguages() );
371 : }
372 : bFound = m_pSpellUsedLang ?
373 0 : lcl_SeqHasLang( *m_pSpellUsedLang, nRealLang ) : false;
374 :
375 0 : nAt = ImplInsertImgEntry( aStrEntry, nPos, bFound );
376 : }
377 : else
378 0 : nAt = InsertEntry( aStrEntry, nPos );
379 :
380 0 : SetEntryData( nAt, (void*)(sal_uIntPtr)nLangType );
381 0 : return nAt;
382 : }
383 :
384 :
385 :
386 0 : sal_Int32 SvxLanguageBox::InsertDefaultLanguage( sal_Int16 nType, sal_Int32 nPos )
387 : {
388 0 : return ImplInsertLanguage( LANGUAGE_SYSTEM, nPos, nType );
389 : }
390 :
391 :
392 :
393 0 : sal_Int32 SvxLanguageBox::InsertSystemLanguage( sal_Int32 nPos )
394 : {
395 0 : return ImplInsertLanguage( LANGUAGE_USER_SYSTEM_CONFIG, nPos, ::com::sun::star::i18n::ScriptType::WEAK );
396 : }
397 :
398 :
399 :
400 0 : sal_Int32 SvxLanguageBox::InsertLanguage( const LanguageType nLangType,
401 : bool bCheckEntry, sal_Int32 nPos )
402 : {
403 0 : LanguageType nLang = MsLangId::getReplacementForObsoleteLanguage( nLangType);
404 : // For obsolete and to be replaced languages check whether an entry of the
405 : // replacement already exists and if so don't add an entry with identical
406 : // string as would be returned by SvtLanguageTable::GetString().
407 0 : if (nLang != nLangType)
408 : {
409 0 : sal_Int32 nAt = TypeToPos_Impl( nLang, *this );
410 0 : if ( nAt != LISTBOX_ENTRY_NOTFOUND )
411 0 : return nAt;
412 : }
413 :
414 0 : OUString aStrEntry = m_pLangTable->GetString( nLang );
415 0 : if (LANGUAGE_NONE == nLang && m_bHasLangNone && m_bLangNoneIsLangAll)
416 0 : aStrEntry = m_aAllString;
417 :
418 0 : sal_Int32 nAt = ImplInsertImgEntry( aStrEntry, nPos, bCheckEntry );
419 0 : SetEntryData( nAt, (void*)(sal_uIntPtr)nLang );
420 :
421 0 : return nAt;
422 : }
423 :
424 :
425 :
426 0 : void SvxLanguageBox::RemoveLanguage( const LanguageType eLangType )
427 : {
428 0 : sal_Int32 nAt = TypeToPos_Impl( eLangType, *this );
429 :
430 0 : if ( nAt != LISTBOX_ENTRY_NOTFOUND )
431 0 : RemoveEntry( nAt );
432 0 : }
433 :
434 :
435 :
436 0 : LanguageType SvxLanguageBox::GetSelectLanguage() const
437 : {
438 0 : sal_Int32 nPos = GetSelectEntryPos();
439 :
440 0 : if ( nPos != LISTBOX_ENTRY_NOTFOUND )
441 0 : return LanguageType( (sal_uIntPtr)GetEntryData(nPos) );
442 : else
443 0 : return LanguageType( LANGUAGE_DONTKNOW );
444 : }
445 :
446 :
447 :
448 0 : void SvxLanguageBox::SelectLanguage( const LanguageType eLangType, bool bSelect )
449 : {
450 : // If the core uses a LangID of an imported MS document and wants to select
451 : // a language that is replaced, we need to select the replacement instead.
452 0 : LanguageType nLang = MsLangId::getReplacementForObsoleteLanguage( eLangType);
453 :
454 0 : sal_Int32 nAt = TypeToPos_Impl( nLang, *this );
455 :
456 0 : if ( nAt == LISTBOX_ENTRY_NOTFOUND )
457 0 : nAt = InsertLanguage( nLang ); // on-the-fly-ID
458 :
459 0 : if ( nAt != LISTBOX_ENTRY_NOTFOUND )
460 0 : SelectEntryPos( nAt, bSelect );
461 0 : }
462 :
463 :
464 :
465 0 : bool SvxLanguageBox::IsLanguageSelected( const LanguageType eLangType ) const
466 : {
467 : // Same here, work on the replacement if applicable.
468 0 : LanguageType nLang = MsLangId::getReplacementForObsoleteLanguage( eLangType);
469 :
470 0 : sal_Int32 nAt = TypeToPos_Impl( nLang, *this );
471 :
472 0 : if ( nAt != LISTBOX_ENTRY_NOTFOUND )
473 0 : return IsEntryPosSelected( nAt );
474 : else
475 0 : return false;
476 : }
477 :
478 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|