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