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 : String GetDicInfoStr( const String& 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 : 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 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 : bool bInsert = true;
170 0 : if ((LANGUAGE_DONTKNOW == nLangType) ||
171 : (LANGUAGE_SYSTEM == nLangType))
172 : {
173 0 : bInsert = false;
174 : }
175 :
176 0 : if ( bInsert )
177 0 : InsertLanguage( nLangType );
178 : }
179 0 : m_nLangList = LANG_LIST_ALL;
180 : }
181 0 : }
182 : //------------------------------------------------------------------------
183 :
184 0 : SvxLanguageBox::~SvxLanguageBox()
185 : {
186 0 : delete m_pSpellUsedLang;
187 0 : delete m_pLangTable;
188 0 : }
189 :
190 : //------------------------------------------------------------------------
191 :
192 0 : sal_uInt16 SvxLanguageBox::ImplInsertImgEntry( const String& rEntry, sal_uInt16 nPos, bool bChecked )
193 : {
194 0 : sal_uInt16 nRet = 0;
195 0 : if( !bChecked )
196 0 : nRet = InsertEntry( rEntry, m_aNotCheckedImage, nPos );
197 : else
198 0 : nRet = InsertEntry( rEntry, m_aCheckedImage, nPos );
199 0 : return nRet;
200 : }
201 :
202 : //------------------------------------------------------------------------
203 :
204 0 : void SvxLanguageBox::SetLanguageList( sal_Int16 nLangList,
205 : sal_Bool bHasLangNone, sal_Bool bLangNoneIsLangAll, sal_Bool bCheckSpellAvail )
206 : {
207 0 : Clear();
208 :
209 0 : m_nLangList = nLangList;
210 0 : m_bHasLangNone = bHasLangNone;
211 0 : m_bLangNoneIsLangAll = bLangNoneIsLangAll;
212 0 : m_bWithCheckmark = bCheckSpellAvail;
213 :
214 0 : if ( LANG_LIST_EMPTY != nLangList )
215 : {
216 0 : Sequence< sal_Int16 > aSpellAvailLang;
217 0 : Sequence< sal_Int16 > aHyphAvailLang;
218 0 : Sequence< sal_Int16 > aThesAvailLang;
219 0 : Sequence< sal_Int16 > aSpellUsedLang;
220 0 : Sequence< sal_Int16 > aHyphUsedLang;
221 0 : Sequence< sal_Int16 > aThesUsedLang;
222 0 : Reference< XAvailableLocales > xAvail( LinguMgr::GetLngSvcMgr(), UNO_QUERY );
223 0 : if (xAvail.is())
224 : {
225 0 : Sequence< Locale > aTmp;
226 :
227 0 : if (LANG_LIST_SPELL_AVAIL & nLangList)
228 : {
229 0 : aTmp = xAvail->getAvailableLocales( SN_SPELLCHECKER );
230 0 : aSpellAvailLang = lcl_LocaleSeqToLangSeq( aTmp );
231 : }
232 0 : if (LANG_LIST_HYPH_AVAIL & nLangList)
233 : {
234 0 : aTmp = xAvail->getAvailableLocales( SN_HYPHENATOR );
235 0 : aHyphAvailLang = lcl_LocaleSeqToLangSeq( aTmp );
236 : }
237 0 : if (LANG_LIST_THES_AVAIL & nLangList)
238 : {
239 0 : aTmp = xAvail->getAvailableLocales( SN_THESAURUS );
240 0 : aThesAvailLang = lcl_LocaleSeqToLangSeq( aTmp );
241 0 : }
242 : }
243 0 : if (LANG_LIST_SPELL_USED & nLangList)
244 : {
245 0 : Reference< XSpellChecker1 > xTmp1( SvxGetSpellChecker(), UNO_QUERY );
246 0 : if (xTmp1.is())
247 0 : aSpellUsedLang = xTmp1->getLanguages();
248 : }
249 0 : if (LANG_LIST_HYPH_USED & nLangList)
250 : {
251 0 : Reference< XHyphenator > xTmp( SvxGetHyphenator() );
252 0 : if (xTmp.is()) {
253 0 : Sequence < Locale > aLocaleSequence( xTmp->getLocales() );
254 0 : aHyphUsedLang = lcl_LocaleSeqToLangSeq( aLocaleSequence );
255 0 : }
256 : }
257 0 : if (LANG_LIST_THES_USED & nLangList)
258 : {
259 0 : Reference< XThesaurus > xTmp( SvxGetThesaurus() );
260 0 : if (xTmp.is()) {
261 0 : Sequence < Locale > aLocaleSequence( xTmp->getLocales() );
262 0 : aThesUsedLang = lcl_LocaleSeqToLangSeq( aLocaleSequence );
263 0 : }
264 : }
265 :
266 0 : SvtLanguageTable aLangTable;
267 0 : ::com::sun::star::uno::Sequence< sal_uInt16 > xKnown;
268 : const sal_uInt16* pKnown;
269 : sal_uInt32 nCount;
270 0 : if ( nLangList & LANG_LIST_ONLY_KNOWN )
271 : {
272 0 : xKnown = LocaleDataWrapper::getInstalledLanguageTypes();
273 0 : pKnown = xKnown.getConstArray();
274 0 : nCount = xKnown.getLength();
275 : }
276 : else
277 : {
278 0 : nCount = aLangTable.GetEntryCount();
279 0 : pKnown = NULL;
280 : }
281 0 : for ( sal_uInt32 i = 0; i < nCount; i++ )
282 : {
283 : LanguageType nLangType;
284 0 : if ( nLangList & LANG_LIST_ONLY_KNOWN )
285 0 : nLangType = pKnown[i];
286 : else
287 0 : nLangType = aLangTable.GetTypeAtIndex( i );
288 0 : if ( nLangType != LANGUAGE_DONTKNOW &&
289 0 : nLangType != LANGUAGE_SYSTEM &&
290 0 : nLangType != LANGUAGE_NONE &&
291 0 : (MsLangId::getSubLanguage( nLangType) != 0 ||
292 0 : (nLangList & LANG_LIST_ALSO_PRIMARY_ONLY)) &&
293 0 : ((nLangList & LANG_LIST_ALL) != 0 ||
294 0 : ((nLangList & LANG_LIST_WESTERN) != 0 &&
295 0 : (SvtLanguageOptions::GetScriptTypeOfLanguage(nLangType) ==
296 0 : SCRIPTTYPE_LATIN)) ||
297 0 : ((nLangList & LANG_LIST_CTL) != 0 &&
298 0 : (SvtLanguageOptions::GetScriptTypeOfLanguage(nLangType) ==
299 0 : SCRIPTTYPE_COMPLEX)) ||
300 0 : ((nLangList & LANG_LIST_CJK) != 0 &&
301 0 : (SvtLanguageOptions::GetScriptTypeOfLanguage(nLangType) ==
302 0 : SCRIPTTYPE_ASIAN)) ||
303 0 : ((nLangList & LANG_LIST_FBD_CHARS) != 0 &&
304 0 : MsLangId::hasForbiddenCharacters(nLangType)) ||
305 0 : ((nLangList & LANG_LIST_SPELL_AVAIL) != 0 &&
306 0 : lcl_SeqHasLang(aSpellAvailLang, nLangType)) ||
307 0 : ((nLangList & LANG_LIST_HYPH_AVAIL) != 0 &&
308 0 : lcl_SeqHasLang(aHyphAvailLang, nLangType)) ||
309 0 : ((nLangList & LANG_LIST_THES_AVAIL) != 0 &&
310 0 : lcl_SeqHasLang(aThesAvailLang, nLangType)) ||
311 0 : ((nLangList & LANG_LIST_SPELL_USED) != 0 &&
312 0 : lcl_SeqHasLang(aSpellUsedLang, nLangType)) ||
313 0 : ((nLangList & LANG_LIST_HYPH_USED) != 0 &&
314 0 : lcl_SeqHasLang(aHyphUsedLang, nLangType)) ||
315 0 : ((nLangList & LANG_LIST_THES_USED) != 0 &&
316 0 : lcl_SeqHasLang(aThesUsedLang, nLangType))) )
317 0 : InsertLanguage( nLangType );
318 : }
319 :
320 0 : if (bHasLangNone)
321 0 : InsertLanguage( LANGUAGE_NONE );
322 : }
323 0 : }
324 :
325 : //------------------------------------------------------------------------
326 :
327 0 : sal_uInt16 SvxLanguageBox::InsertLanguage( const LanguageType nLangType, sal_uInt16 nPos )
328 : {
329 0 : return ImplInsertLanguage( nLangType, nPos, ::com::sun::star::i18n::ScriptType::WEAK );
330 : }
331 :
332 : //------------------------------------------------------------------------
333 :
334 0 : sal_uInt16 SvxLanguageBox::ImplInsertLanguage( const LanguageType nLangType, sal_uInt16 nPos, sal_Int16 nType )
335 : {
336 0 : LanguageType nLang = MsLangId::getReplacementForObsoleteLanguage( nLangType);
337 : // For obsolete and to be replaced languages check whether an entry of the
338 : // replacement already exists and if so don't add an entry with identical
339 : // string as would be returned by SvtLanguageTable::GetString().
340 0 : if (nLang != nLangType)
341 : {
342 0 : sal_uInt16 nAt = TypeToPos_Impl( nLang, *this );
343 0 : if ( nAt != LISTBOX_ENTRY_NOTFOUND )
344 0 : return nAt;
345 : }
346 :
347 0 : String aStrEntry = m_pLangTable->GetString( nLang );
348 0 : if (LANGUAGE_NONE == nLang && m_bHasLangNone && m_bLangNoneIsLangAll)
349 0 : aStrEntry = m_aAllString;
350 :
351 0 : LanguageType nRealLang = nLang;
352 0 : if (nRealLang == LANGUAGE_SYSTEM)
353 : {
354 0 : nRealLang = MsLangId::resolveSystemLanguageByScriptType(nRealLang, nType);
355 0 : aStrEntry.AppendAscii(" - ");
356 0 : aStrEntry.Append(m_pLangTable->GetString( nRealLang ));
357 0 : } else if (nRealLang == LANGUAGE_USER_SYSTEM_CONFIG) {
358 0 : nRealLang = MsLangId::getSystemLanguage();
359 0 : aStrEntry.AppendAscii(" - ");
360 0 : aStrEntry.Append(m_pLangTable->GetString( nRealLang ));
361 : }
362 :
363 0 : aStrEntry = ApplyLreOrRleEmbedding( aStrEntry );
364 :
365 0 : sal_uInt16 nAt = 0;
366 0 : if ( m_bWithCheckmark )
367 : {
368 0 : bool bFound = false;
369 :
370 0 : if (!m_pSpellUsedLang)
371 : {
372 0 : Reference< XSpellChecker1 > xSpell( SvxGetSpellChecker(), UNO_QUERY );
373 0 : if ( xSpell.is() )
374 0 : m_pSpellUsedLang = new Sequence< sal_Int16 >( xSpell->getLanguages() );
375 : }
376 : bFound = m_pSpellUsedLang ?
377 0 : lcl_SeqHasLang( *m_pSpellUsedLang, nRealLang ) : false;
378 :
379 0 : nAt = ImplInsertImgEntry( aStrEntry, nPos, bFound );
380 : }
381 : else
382 0 : nAt = InsertEntry( aStrEntry, nPos );
383 :
384 0 : SetEntryData( nAt, (void*)(sal_uIntPtr)nLangType );
385 0 : return nAt;
386 : }
387 :
388 : //------------------------------------------------------------------------
389 :
390 0 : sal_uInt16 SvxLanguageBox::InsertDefaultLanguage( sal_Int16 nType, sal_uInt16 nPos )
391 : {
392 0 : return ImplInsertLanguage( LANGUAGE_SYSTEM, nPos, nType );
393 : }
394 :
395 : //------------------------------------------------------------------------
396 :
397 0 : sal_uInt16 SvxLanguageBox::InsertSystemLanguage( sal_uInt16 nPos )
398 : {
399 0 : return ImplInsertLanguage( LANGUAGE_USER_SYSTEM_CONFIG, nPos, ::com::sun::star::i18n::ScriptType::WEAK );
400 : }
401 :
402 : //------------------------------------------------------------------------
403 :
404 0 : sal_uInt16 SvxLanguageBox::InsertLanguage( const LanguageType nLangType,
405 : sal_Bool bCheckEntry, sal_uInt16 nPos )
406 : {
407 0 : LanguageType nLang = MsLangId::getReplacementForObsoleteLanguage( nLangType);
408 : // For obsolete and to be replaced languages check whether an entry of the
409 : // replacement already exists and if so don't add an entry with identical
410 : // string as would be returned by SvtLanguageTable::GetString().
411 0 : if (nLang != nLangType)
412 : {
413 0 : sal_uInt16 nAt = TypeToPos_Impl( nLang, *this );
414 0 : if ( nAt != LISTBOX_ENTRY_NOTFOUND )
415 0 : return nAt;
416 : }
417 :
418 0 : String aStrEntry = m_pLangTable->GetString( nLang );
419 0 : if (LANGUAGE_NONE == nLang && m_bHasLangNone && m_bLangNoneIsLangAll)
420 0 : aStrEntry = m_aAllString;
421 :
422 0 : sal_uInt16 nAt = ImplInsertImgEntry( aStrEntry, nPos, bCheckEntry );
423 0 : SetEntryData( nAt, (void*)(sal_uIntPtr)nLang );
424 :
425 0 : return nAt;
426 : }
427 :
428 : //------------------------------------------------------------------------
429 :
430 0 : void SvxLanguageBox::RemoveLanguage( const LanguageType eLangType )
431 : {
432 0 : sal_uInt16 nAt = TypeToPos_Impl( eLangType, *this );
433 :
434 0 : if ( nAt != LISTBOX_ENTRY_NOTFOUND )
435 0 : RemoveEntry( nAt );
436 0 : }
437 :
438 : //------------------------------------------------------------------------
439 :
440 0 : LanguageType SvxLanguageBox::GetSelectLanguage() const
441 : {
442 0 : sal_uInt16 nPos = GetSelectEntryPos();
443 :
444 0 : if ( nPos != LISTBOX_ENTRY_NOTFOUND )
445 0 : return LanguageType( (sal_uIntPtr)GetEntryData(nPos) );
446 : else
447 0 : return LanguageType( LANGUAGE_DONTKNOW );
448 : }
449 :
450 : //------------------------------------------------------------------------
451 :
452 0 : void SvxLanguageBox::SelectLanguage( const LanguageType eLangType, sal_Bool bSelect )
453 : {
454 : // If the core uses a LangID of an imported MS document and wants to select
455 : // a language that is replaced, we need to select the replacement instead.
456 0 : LanguageType nLang = MsLangId::getReplacementForObsoleteLanguage( eLangType);
457 :
458 0 : sal_uInt16 nAt = TypeToPos_Impl( nLang, *this );
459 :
460 0 : if ( nAt != LISTBOX_ENTRY_NOTFOUND )
461 0 : SelectEntryPos( nAt, bSelect );
462 0 : }
463 :
464 : //------------------------------------------------------------------------
465 :
466 0 : sal_Bool SvxLanguageBox::IsLanguageSelected( const LanguageType eLangType ) const
467 : {
468 : // Same here, work on the replacement if applicable.
469 0 : LanguageType nLang = MsLangId::getReplacementForObsoleteLanguage( eLangType);
470 :
471 0 : sal_uInt16 nAt = TypeToPos_Impl( nLang, *this );
472 :
473 0 : if ( nAt != LISTBOX_ENTRY_NOTFOUND )
474 0 : return IsEntryPosSelected( nAt );
475 : else
476 0 : return sal_False;
477 258 : }
478 :
479 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|