nss: upgrade to release 3.73
[LibreOffice.git] / svl / source / config / cjkoptions.cxx
blob71054f564de49c2b2d11358d7697c16f4bf922c3
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 <svl/cjkoptions.hxx>
22 #include <o3tl/any.hxx>
23 #include <svl/languageoptions.hxx>
24 #include <i18nlangtag/lang.h>
25 #include <unotools/configitem.hxx>
26 #include <com/sun/star/uno/Any.h>
27 #include <com/sun/star/uno/Sequence.hxx>
28 #include <osl/mutex.hxx>
29 #include <rtl/instance.hxx>
31 #include "itemholder2.hxx"
33 using namespace ::com::sun::star::uno;
35 #define CFG_READONLY_DEFAULT false
37 class SvtCJKOptions_Impl : public utl::ConfigItem
39 bool bIsLoaded;
40 bool bCJKFont;
41 bool bVerticalText;
42 bool bAsianTypography;
43 bool bJapaneseFind;
44 bool bRuby;
45 bool bChangeCaseMap;
46 bool bDoubleLines;
47 bool bEmphasisMarks;
48 bool bVerticalCallOut;
50 bool bROCJKFont;
51 bool bROVerticalText;
52 bool bROAsianTypography;
53 bool bROJapaneseFind;
54 bool bRORuby;
55 bool bROChangeCaseMap;
56 bool bRODoubleLines;
57 bool bROEmphasisMarks;
58 bool bROVerticalCallOut;
60 virtual void ImplCommit() override;
62 public:
63 SvtCJKOptions_Impl();
65 virtual void Notify( const css::uno::Sequence< OUString >& rPropertyNames ) override;
66 void Load();
68 bool IsLoaded() const { return bIsLoaded; }
70 bool IsCJKFontEnabled() const { return bCJKFont; }
71 bool IsVerticalTextEnabled() const { return bVerticalText; }
72 bool IsAsianTypographyEnabled() const { return bAsianTypography; }
73 bool IsJapaneseFindEnabled() const { return bJapaneseFind; }
74 bool IsRubyEnabled() const { return bRuby; }
75 bool IsChangeCaseMapEnabled() const { return bChangeCaseMap; }
76 bool IsDoubleLinesEnabled() const { return bDoubleLines; }
78 bool IsAnyEnabled() const {
79 return bCJKFont||bVerticalText||bAsianTypography||bJapaneseFind||
80 bRuby||bChangeCaseMap||bDoubleLines||bEmphasisMarks||bVerticalCallOut; }
81 void SetAll(bool bSet);
82 bool IsReadOnly(SvtCJKOptions::EOption eOption) const;
85 namespace
87 struct PropertyNames
88 : public rtl::Static< Sequence<OUString>, PropertyNames > {};
91 SvtCJKOptions_Impl::SvtCJKOptions_Impl() :
92 utl::ConfigItem("Office.Common/I18N/CJK"),
93 bIsLoaded(false),
94 bCJKFont(true),
95 bVerticalText(true),
96 bAsianTypography(true),
97 bJapaneseFind(true),
98 bRuby(true),
99 bChangeCaseMap(true),
100 bDoubleLines(true),
101 bEmphasisMarks(true),
102 bVerticalCallOut(true),
103 bROCJKFont(CFG_READONLY_DEFAULT),
104 bROVerticalText(CFG_READONLY_DEFAULT),
105 bROAsianTypography(CFG_READONLY_DEFAULT),
106 bROJapaneseFind(CFG_READONLY_DEFAULT),
107 bRORuby(CFG_READONLY_DEFAULT),
108 bROChangeCaseMap(CFG_READONLY_DEFAULT),
109 bRODoubleLines(CFG_READONLY_DEFAULT),
110 bROEmphasisMarks(CFG_READONLY_DEFAULT),
111 bROVerticalCallOut(CFG_READONLY_DEFAULT)
115 void SvtCJKOptions_Impl::SetAll(bool bSet)
117 if (
118 bROCJKFont ||
119 bROVerticalText ||
120 bROAsianTypography ||
121 bROJapaneseFind ||
122 bRORuby ||
123 bROChangeCaseMap ||
124 bRODoubleLines ||
125 bROEmphasisMarks ||
126 bROVerticalCallOut
128 return;
130 bCJKFont=bSet;
131 bVerticalText=bSet;
132 bAsianTypography=bSet;
133 bJapaneseFind=bSet;
134 bRuby=bSet;
135 bChangeCaseMap=bSet;
136 bDoubleLines=bSet;
137 bEmphasisMarks=bSet;
138 bVerticalCallOut=bSet;
140 SetModified();
141 Commit();
142 NotifyListeners(ConfigurationHints::NONE);
145 void SvtCJKOptions_Impl::Load()
147 Sequence<OUString> &rPropertyNames = PropertyNames::get();
148 if(!rPropertyNames.hasElements())
150 rPropertyNames.realloc(9);
151 OUString* pNames = rPropertyNames.getArray();
153 pNames[0] = "CJKFont";
154 pNames[1] = "VerticalText";
155 pNames[2] = "AsianTypography";
156 pNames[3] = "JapaneseFind";
157 pNames[4] = "Ruby";
158 pNames[5] = "ChangeCaseMap";
159 pNames[6] = "DoubleLines";
160 pNames[7] = "EmphasisMarks";
161 pNames[8] = "VerticalCallOut";
163 EnableNotification( rPropertyNames );
165 Sequence< Any > aValues = GetProperties(rPropertyNames);
166 Sequence< sal_Bool > aROStates = GetReadOnlyStates(rPropertyNames);
167 const Any* pValues = aValues.getConstArray();
168 const sal_Bool* pROStates = aROStates.getConstArray();
169 assert(aValues.getLength() == rPropertyNames.getLength() && "GetProperties failed");
170 assert(aROStates.getLength() == rPropertyNames.getLength() && "GetReadOnlyStates failed");
171 if ( aValues.getLength() == rPropertyNames.getLength() && aROStates.getLength() == rPropertyNames.getLength() )
173 for ( int nProp = 0; nProp < rPropertyNames.getLength(); nProp++ )
175 if( pValues[nProp].hasValue() )
177 bool bValue = *o3tl::doAccess<bool>(pValues[nProp]);
178 switch ( nProp )
180 case 0: { bCJKFont = bValue; bROCJKFont = pROStates[nProp]; } break;
181 case 1: { bVerticalText = bValue; bROVerticalText = pROStates[nProp]; } break;
182 case 2: { bAsianTypography = bValue; bROAsianTypography = pROStates[nProp]; } break;
183 case 3: { bJapaneseFind = bValue; bROJapaneseFind = pROStates[nProp]; } break;
184 case 4: { bRuby = bValue; bRORuby = pROStates[nProp]; } break;
185 case 5: { bChangeCaseMap = bValue; bROChangeCaseMap = pROStates[nProp]; } break;
186 case 6: { bDoubleLines = bValue; bRODoubleLines = pROStates[nProp]; } break;
187 case 7: { bEmphasisMarks = bValue; bROEmphasisMarks = pROStates[nProp]; } break;
188 case 8: { bVerticalCallOut = bValue; bROVerticalCallOut = pROStates[nProp]; } break;
194 if (!bCJKFont)
196 SvtScriptType nScriptType = SvtLanguageOptions::GetScriptTypeOfLanguage(LANGUAGE_SYSTEM);
197 //system locale is CJK
198 bool bAutoEnableCJK = bool(nScriptType & SvtScriptType::ASIAN);
200 if (!bAutoEnableCJK)
202 SvtSystemLanguageOptions aSystemLocaleSettings;
204 //windows secondary system locale is CJK
205 LanguageType eSystemLanguage = aSystemLocaleSettings.GetWin16SystemLanguage();
206 if (eSystemLanguage != LANGUAGE_SYSTEM)
208 SvtScriptType nWinScript = SvtLanguageOptions::GetScriptTypeOfLanguage( eSystemLanguage );
209 bAutoEnableCJK = bool(nWinScript & SvtScriptType::ASIAN);
212 //CJK keyboard is installed
213 if (!bAutoEnableCJK)
214 bAutoEnableCJK = aSystemLocaleSettings.isCJKKeyboardLayoutInstalled();
217 if (bAutoEnableCJK)
219 SetAll(true);
222 bIsLoaded = true;
225 void SvtCJKOptions_Impl::Notify( const Sequence< OUString >& )
227 Load();
228 NotifyListeners(ConfigurationHints::NONE);
231 void SvtCJKOptions_Impl::ImplCommit()
233 Sequence<OUString> &rPropertyNames = PropertyNames::get();
234 OUString* pOrgNames = rPropertyNames.getArray();
235 sal_Int32 nOrgCount = rPropertyNames.getLength();
237 Sequence< OUString > aNames(nOrgCount);
238 Sequence< Any > aValues(nOrgCount);
240 OUString* pNames = aNames.getArray();
241 Any* pValues = aValues.getArray();
242 sal_Int32 nRealCount = 0;
244 for(int nProp = 0; nProp < nOrgCount; nProp++)
246 switch(nProp)
248 case 0:
250 if (!bROCJKFont)
252 pNames[nRealCount] = pOrgNames[nProp];
253 pValues[nRealCount] <<= bCJKFont;
254 ++nRealCount;
257 break;
259 case 1:
261 if (!bROVerticalText)
263 pNames[nRealCount] = pOrgNames[nProp];
264 pValues[nRealCount] <<= bVerticalText;
265 ++nRealCount;
268 break;
270 case 2:
272 if (!bROAsianTypography)
274 pNames[nRealCount] = pOrgNames[nProp];
275 pValues[nRealCount] <<= bAsianTypography;
276 ++nRealCount;
279 break;
281 case 3:
283 if (!bROJapaneseFind)
285 pNames[nRealCount] = pOrgNames[nProp];
286 pValues[nRealCount] <<= bJapaneseFind;
287 ++nRealCount;
290 break;
292 case 4:
294 if (!bRORuby)
296 pNames[nRealCount] = pOrgNames[nProp];
297 pValues[nRealCount] <<= bRuby;
298 ++nRealCount;
301 break;
303 case 5:
305 if (!bROChangeCaseMap)
307 pNames[nRealCount] = pOrgNames[nProp];
308 pValues[nRealCount] <<= bChangeCaseMap;
309 ++nRealCount;
312 break;
314 case 6:
316 if (!bRODoubleLines)
318 pNames[nRealCount] = pOrgNames[nProp];
319 pValues[nRealCount] <<= bDoubleLines;
320 ++nRealCount;
323 break;
325 case 7:
327 if (!bROEmphasisMarks)
329 pNames[nRealCount] = pOrgNames[nProp];
330 pValues[nRealCount] <<= bEmphasisMarks;
331 ++nRealCount;
334 break;
336 case 8:
338 if (!bROVerticalCallOut)
340 pNames[nRealCount] = pOrgNames[nProp];
341 pValues[nRealCount] <<= bVerticalCallOut;
342 ++nRealCount;
345 break;
348 aNames.realloc(nRealCount);
349 aValues.realloc(nRealCount);
350 PutProperties(aNames, aValues);
353 bool SvtCJKOptions_Impl::IsReadOnly(SvtCJKOptions::EOption eOption) const
355 bool bReadOnly = CFG_READONLY_DEFAULT;
356 switch(eOption)
358 case SvtCJKOptions::E_CJKFONT : bReadOnly = bROCJKFont; break;
359 case SvtCJKOptions::E_VERTICALTEXT : bReadOnly = bROVerticalText; break;
360 case SvtCJKOptions::E_ASIANTYPOGRAPHY : bReadOnly = bROAsianTypography; break;
361 case SvtCJKOptions::E_JAPANESEFIND : bReadOnly = bROJapaneseFind; break;
362 case SvtCJKOptions::E_RUBY : bReadOnly = bRORuby; break;
363 case SvtCJKOptions::E_CHANGECASEMAP : bReadOnly = bROChangeCaseMap; break;
364 case SvtCJKOptions::E_DOUBLELINES : bReadOnly = bRODoubleLines; break;
365 case SvtCJKOptions::E_EMPHASISMARKS : bReadOnly = bROEmphasisMarks; break;
366 case SvtCJKOptions::E_VERTICALCALLOUT : bReadOnly = bROVerticalCallOut; break;
367 case SvtCJKOptions::E_ALL : if (bROCJKFont || bROVerticalText || bROAsianTypography || bROJapaneseFind || bRORuby || bROChangeCaseMap || bRODoubleLines || bROEmphasisMarks || bROVerticalCallOut)
368 bReadOnly = true;
369 break;
371 return bReadOnly;
374 namespace {
376 // global
377 std::weak_ptr<SvtCJKOptions_Impl> g_pCJKOptions;
379 struct theCJKOptionsMutex : public rtl::Static< ::osl::Mutex , theCJKOptionsMutex >{};
382 SvtCJKOptions::SvtCJKOptions(bool bDontLoad)
384 // Global access, must be guarded (multithreading)
385 ::osl::MutexGuard aGuard( theCJKOptionsMutex::get() );
386 pImpl = g_pCJKOptions.lock();
387 if ( !pImpl )
389 pImpl = std::make_shared<SvtCJKOptions_Impl>();
390 g_pCJKOptions = pImpl;
391 ItemHolder2::holdConfigItem(EItem::CJKOptions);
394 if( !bDontLoad && !pImpl->IsLoaded())
395 pImpl->Load();
399 SvtCJKOptions::~SvtCJKOptions()
401 // Global access, must be guarded (multithreading)
402 ::osl::MutexGuard aGuard( theCJKOptionsMutex::get() );
404 // pImpl needs to be cleared before the mutex is dropped
405 pImpl.reset();
408 bool SvtCJKOptions::IsCJKFontEnabled() const
410 assert(pImpl->IsLoaded());
411 return pImpl->IsCJKFontEnabled();
414 bool SvtCJKOptions::IsVerticalTextEnabled() const
416 assert(pImpl->IsLoaded());
417 return pImpl->IsVerticalTextEnabled();
420 bool SvtCJKOptions::IsAsianTypographyEnabled() const
422 assert(pImpl->IsLoaded());
423 return pImpl->IsAsianTypographyEnabled();
426 bool SvtCJKOptions::IsJapaneseFindEnabled() const
428 assert(pImpl->IsLoaded());
429 return pImpl->IsJapaneseFindEnabled();
432 bool SvtCJKOptions::IsRubyEnabled() const
434 assert(pImpl->IsLoaded());
435 return pImpl->IsRubyEnabled();
438 bool SvtCJKOptions::IsChangeCaseMapEnabled() const
440 assert(pImpl->IsLoaded());
441 return pImpl->IsChangeCaseMapEnabled();
444 bool SvtCJKOptions::IsDoubleLinesEnabled() const
446 assert(pImpl->IsLoaded());
447 return pImpl->IsDoubleLinesEnabled();
450 void SvtCJKOptions::SetAll(bool bSet)
452 assert(pImpl->IsLoaded());
453 pImpl->SetAll(bSet);
456 bool SvtCJKOptions::IsAnyEnabled() const
458 assert(pImpl->IsLoaded());
459 return pImpl->IsAnyEnabled();
462 bool SvtCJKOptions::IsReadOnly(EOption eOption) const
464 assert(pImpl->IsLoaded());
465 return pImpl->IsReadOnly(eOption);
468 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */