bump product version to 5.0.4.1
[LibreOffice.git] / svl / source / config / cjkoptions.cxx
blob3f61dcaaa2b9fa914d3547aace9a0c06029bd1dc
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 <svl/languageoptions.hxx>
23 #include <i18nlangtag/lang.h>
24 #include <unotools/configitem.hxx>
25 #include <tools/solar.h>
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() SAL_OVERRIDE;
62 public:
63 SvtCJKOptions_Impl();
64 virtual ~SvtCJKOptions_Impl();
66 virtual void Notify( const com::sun::star::uno::Sequence< OUString >& rPropertyNames ) SAL_OVERRIDE;
67 void Load();
69 bool IsLoaded() { return bIsLoaded; }
71 bool IsCJKFontEnabled() const { return bCJKFont; }
72 bool IsVerticalTextEnabled() const { return bVerticalText; }
73 bool IsAsianTypographyEnabled() const { return bAsianTypography; }
74 bool IsJapaneseFindEnabled() const { return bJapaneseFind; }
75 bool IsRubyEnabled() const { return bRuby; }
76 bool IsChangeCaseMapEnabled() const { return bChangeCaseMap; }
77 bool IsDoubleLinesEnabled() const { return bDoubleLines; }
79 bool IsAnyEnabled() const {
80 return bCJKFont||bVerticalText||bAsianTypography||bJapaneseFind||
81 bRuby||bChangeCaseMap||bDoubleLines||bEmphasisMarks||bVerticalCallOut; }
82 void SetAll(bool bSet);
83 bool IsReadOnly(SvtCJKOptions::EOption eOption) const;
86 namespace
88 struct PropertyNames
89 : public rtl::Static< Sequence<OUString>, PropertyNames > {};
92 SvtCJKOptions_Impl::SvtCJKOptions_Impl() :
93 utl::ConfigItem("Office.Common/I18N/CJK"),
94 bIsLoaded(false),
95 bCJKFont(true),
96 bVerticalText(true),
97 bAsianTypography(true),
98 bJapaneseFind(true),
99 bRuby(true),
100 bChangeCaseMap(true),
101 bDoubleLines(true),
102 bEmphasisMarks(true),
103 bVerticalCallOut(true),
104 bROCJKFont(CFG_READONLY_DEFAULT),
105 bROVerticalText(CFG_READONLY_DEFAULT),
106 bROAsianTypography(CFG_READONLY_DEFAULT),
107 bROJapaneseFind(CFG_READONLY_DEFAULT),
108 bRORuby(CFG_READONLY_DEFAULT),
109 bROChangeCaseMap(CFG_READONLY_DEFAULT),
110 bRODoubleLines(CFG_READONLY_DEFAULT),
111 bROEmphasisMarks(CFG_READONLY_DEFAULT),
112 bROVerticalCallOut(CFG_READONLY_DEFAULT)
116 SvtCJKOptions_Impl::~SvtCJKOptions_Impl()
120 void SvtCJKOptions_Impl::SetAll(bool bSet)
122 if (
123 !bROCJKFont &&
124 !bROVerticalText &&
125 !bROAsianTypography &&
126 !bROJapaneseFind &&
127 !bRORuby &&
128 !bROChangeCaseMap &&
129 !bRODoubleLines &&
130 !bROEmphasisMarks &&
131 !bROVerticalCallOut
134 bCJKFont=bSet;
135 bVerticalText=bSet;
136 bAsianTypography=bSet;
137 bJapaneseFind=bSet;
138 bRuby=bSet;
139 bChangeCaseMap=bSet;
140 bDoubleLines=bSet;
141 bEmphasisMarks=bSet;
142 bVerticalCallOut=bSet;
144 SetModified();
145 Commit();
146 NotifyListeners(0);
150 void SvtCJKOptions_Impl::Load()
152 Sequence<OUString> &rPropertyNames = PropertyNames::get();
153 if(!rPropertyNames.getLength())
155 rPropertyNames.realloc(9);
156 OUString* pNames = rPropertyNames.getArray();
158 pNames[0] = "CJKFont";
159 pNames[1] = "VerticalText";
160 pNames[2] = "AsianTypography";
161 pNames[3] = "JapaneseFind";
162 pNames[4] = "Ruby";
163 pNames[5] = "ChangeCaseMap";
164 pNames[6] = "DoubleLines";
165 pNames[7] = "EmphasisMarks";
166 pNames[8] = "VerticalCallOut";
168 EnableNotification( rPropertyNames );
170 Sequence< Any > aValues = GetProperties(rPropertyNames);
171 Sequence< sal_Bool > aROStates = GetReadOnlyStates(rPropertyNames);
172 const Any* pValues = aValues.getConstArray();
173 const sal_Bool* pROStates = aROStates.getConstArray();
174 assert(aValues.getLength() == rPropertyNames.getLength() && "GetProperties failed");
175 assert(aROStates.getLength() == rPropertyNames.getLength() && "GetReadOnlyStates failed");
176 if ( aValues.getLength() == rPropertyNames.getLength() && aROStates.getLength() == rPropertyNames.getLength() )
178 for ( int nProp = 0; nProp < rPropertyNames.getLength(); nProp++ )
180 if( pValues[nProp].hasValue() )
182 bool bValue = *static_cast<sal_Bool const *>(pValues[nProp].getValue());
183 switch ( nProp )
185 case 0: { bCJKFont = bValue; bROCJKFont = pROStates[nProp]; } break;
186 case 1: { bVerticalText = bValue; bROVerticalText = pROStates[nProp]; } break;
187 case 2: { bAsianTypography = bValue; bROAsianTypography = pROStates[nProp]; } break;
188 case 3: { bJapaneseFind = bValue; bROJapaneseFind = pROStates[nProp]; } break;
189 case 4: { bRuby = bValue; bRORuby = pROStates[nProp]; } break;
190 case 5: { bChangeCaseMap = bValue; bROChangeCaseMap = pROStates[nProp]; } break;
191 case 6: { bDoubleLines = bValue; bRODoubleLines = pROStates[nProp]; } break;
192 case 7: { bEmphasisMarks = bValue; bROEmphasisMarks = pROStates[nProp]; } break;
193 case 8: { bVerticalCallOut = bValue; bROVerticalCallOut = pROStates[nProp]; } break;
199 if (!bCJKFont)
201 SvtScriptType nScriptType = SvtLanguageOptions::GetScriptTypeOfLanguage(LANGUAGE_SYSTEM);
202 //system locale is CJK
203 bool bAutoEnableCJK = bool(nScriptType & SvtScriptType::ASIAN);
205 if (!bAutoEnableCJK)
207 SvtSystemLanguageOptions aSystemLocaleSettings;
209 //windows secondary system locale is CJK
210 LanguageType eSystemLanguage = aSystemLocaleSettings.GetWin16SystemLanguage();
211 if (eSystemLanguage != LANGUAGE_SYSTEM)
213 SvtScriptType nWinScript = SvtLanguageOptions::GetScriptTypeOfLanguage( eSystemLanguage );
214 bAutoEnableCJK = bool(nWinScript & SvtScriptType::ASIAN);
217 //CJK keyboard is installed
218 if (!bAutoEnableCJK)
219 bAutoEnableCJK = aSystemLocaleSettings.isCJKKeyboardLayoutInstalled();
222 if (bAutoEnableCJK)
224 SetAll(true);
227 bIsLoaded = true;
230 void SvtCJKOptions_Impl::Notify( const Sequence< OUString >& )
232 Load();
233 NotifyListeners(0);
236 void SvtCJKOptions_Impl::ImplCommit()
238 Sequence<OUString> &rPropertyNames = PropertyNames::get();
239 OUString* pOrgNames = rPropertyNames.getArray();
240 sal_Int32 nOrgCount = rPropertyNames.getLength();
242 Sequence< OUString > aNames(nOrgCount);
243 Sequence< Any > aValues(nOrgCount);
245 OUString* pNames = aNames.getArray();
246 Any* pValues = aValues.getArray();
247 sal_Int32 nRealCount = 0;
249 const Type& rType = cppu::UnoType<bool>::get();
250 for(int nProp = 0; nProp < nOrgCount; nProp++)
252 switch(nProp)
254 case 0:
256 if (!bROCJKFont)
258 pNames[nRealCount] = pOrgNames[nProp];
259 pValues[nRealCount].setValue(&bCJKFont, rType);
260 ++nRealCount;
263 break;
265 case 1:
267 if (!bROVerticalText)
269 pNames[nRealCount] = pOrgNames[nProp];
270 pValues[nRealCount].setValue(&bVerticalText, rType);
271 ++nRealCount;
274 break;
276 case 2:
278 if (!bROAsianTypography)
280 pNames[nRealCount] = pOrgNames[nProp];
281 pValues[nRealCount].setValue(&bAsianTypography, rType);
282 ++nRealCount;
285 break;
287 case 3:
289 if (!bROJapaneseFind)
291 pNames[nRealCount] = pOrgNames[nProp];
292 pValues[nRealCount].setValue(&bJapaneseFind, rType);
293 ++nRealCount;
296 break;
298 case 4:
300 if (!bRORuby)
302 pNames[nRealCount] = pOrgNames[nProp];
303 pValues[nRealCount].setValue(&bRuby, rType);
304 ++nRealCount;
307 break;
309 case 5:
311 if (!bROChangeCaseMap)
313 pNames[nRealCount] = pOrgNames[nProp];
314 pValues[nRealCount].setValue(&bChangeCaseMap, rType);
315 ++nRealCount;
318 break;
320 case 6:
322 if (!bRODoubleLines)
324 pNames[nRealCount] = pOrgNames[nProp];
325 pValues[nRealCount].setValue(&bDoubleLines, rType);
326 ++nRealCount;
329 break;
331 case 7:
333 if (!bROEmphasisMarks)
335 pNames[nRealCount] = pOrgNames[nProp];
336 pValues[nRealCount].setValue(&bEmphasisMarks, rType);
337 ++nRealCount;
340 break;
342 case 8:
344 if (!bROVerticalCallOut)
346 pNames[nRealCount] = pOrgNames[nProp];
347 pValues[nRealCount].setValue(&bVerticalCallOut, rType);
348 ++nRealCount;
351 break;
354 aNames.realloc(nRealCount);
355 aValues.realloc(nRealCount);
356 PutProperties(aNames, aValues);
359 bool SvtCJKOptions_Impl::IsReadOnly(SvtCJKOptions::EOption eOption) const
361 bool bReadOnly = CFG_READONLY_DEFAULT;
362 switch(eOption)
364 case SvtCJKOptions::E_CJKFONT : bReadOnly = bROCJKFont; break;
365 case SvtCJKOptions::E_VERTICALTEXT : bReadOnly = bROVerticalText; break;
366 case SvtCJKOptions::E_ASIANTYPOGRAPHY : bReadOnly = bROAsianTypography; break;
367 case SvtCJKOptions::E_JAPANESEFIND : bReadOnly = bROJapaneseFind; break;
368 case SvtCJKOptions::E_RUBY : bReadOnly = bRORuby; break;
369 case SvtCJKOptions::E_CHANGECASEMAP : bReadOnly = bROChangeCaseMap; break;
370 case SvtCJKOptions::E_DOUBLELINES : bReadOnly = bRODoubleLines; break;
371 case SvtCJKOptions::E_EMPHASISMARKS : bReadOnly = bROEmphasisMarks; break;
372 case SvtCJKOptions::E_VERTICALCALLOUT : bReadOnly = bROVerticalCallOut; break;
373 case SvtCJKOptions::E_ALL : if (bROCJKFont || bROVerticalText || bROAsianTypography || bROJapaneseFind || bRORuby || bROChangeCaseMap || bRODoubleLines || bROEmphasisMarks || bROVerticalCallOut)
374 bReadOnly = true;
375 break;
377 return bReadOnly;
380 // global
382 static SvtCJKOptions_Impl* pCJKOptions = NULL;
383 static sal_Int32 nCJKRefCount = 0;
384 namespace { struct theCJKOptionsMutex : public rtl::Static< ::osl::Mutex , theCJKOptionsMutex >{}; }
386 SvtCJKOptions::SvtCJKOptions(bool bDontLoad)
388 // Global access, must be guarded (multithreading)
389 ::osl::MutexGuard aGuard( theCJKOptionsMutex::get() );
390 if ( !pCJKOptions )
392 pCJKOptions = new SvtCJKOptions_Impl;
393 ItemHolder2::holdConfigItem(E_CJKOPTIONS);
395 if( !bDontLoad && !pCJKOptions->IsLoaded())
396 pCJKOptions->Load();
398 ++nCJKRefCount;
399 pImp = pCJKOptions;
404 SvtCJKOptions::~SvtCJKOptions()
406 // Global access, must be guarded (multithreading)
407 ::osl::MutexGuard aGuard( theCJKOptionsMutex::get() );
408 if ( !--nCJKRefCount )
409 DELETEZ( pCJKOptions );
412 bool SvtCJKOptions::IsCJKFontEnabled() const
414 assert(pCJKOptions->IsLoaded());
415 return pCJKOptions->IsCJKFontEnabled();
418 bool SvtCJKOptions::IsVerticalTextEnabled() const
420 assert(pCJKOptions->IsLoaded());
421 return pCJKOptions->IsVerticalTextEnabled();
424 bool SvtCJKOptions::IsAsianTypographyEnabled() const
426 assert(pCJKOptions->IsLoaded());
427 return pCJKOptions->IsAsianTypographyEnabled();
430 bool SvtCJKOptions::IsJapaneseFindEnabled() const
432 assert(pCJKOptions->IsLoaded());
433 return pCJKOptions->IsJapaneseFindEnabled();
436 bool SvtCJKOptions::IsRubyEnabled() const
438 assert(pCJKOptions->IsLoaded());
439 return pCJKOptions->IsRubyEnabled();
442 bool SvtCJKOptions::IsChangeCaseMapEnabled() const
444 assert(pCJKOptions->IsLoaded());
445 return pCJKOptions->IsChangeCaseMapEnabled();
448 bool SvtCJKOptions::IsDoubleLinesEnabled() const
450 assert(pCJKOptions->IsLoaded());
451 return pCJKOptions->IsDoubleLinesEnabled();
454 void SvtCJKOptions::SetAll(bool bSet)
456 assert(pCJKOptions->IsLoaded());
457 pCJKOptions->SetAll(bSet);
460 bool SvtCJKOptions::IsAnyEnabled() const
462 assert(pCJKOptions->IsLoaded());
463 return pCJKOptions->IsAnyEnabled();
466 bool SvtCJKOptions::IsReadOnly(EOption eOption) const
468 assert(pCJKOptions->IsLoaded());
469 return pCJKOptions->IsReadOnly(eOption);
472 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */