LibreOffice Module cui (master)  1
optasian.cxx
Go to the documentation of this file.
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 <memory>
21 #include <map>
22 #include <optasian.hxx>
23 #include <osl/diagnose.h>
24 #include <tools/debug.hxx>
25 #include <o3tl/any.hxx>
26 #include <i18nlangtag/mslangid.hxx>
27 #include <svl/asiancfg.hxx>
28 #include <com/sun/star/lang/Locale.hpp>
29 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
30 #include <com/sun/star/i18n/XForbiddenCharacters.hpp>
31 #include <com/sun/star/beans/XPropertySet.hpp>
32 #include <sfx2/viewfrm.hxx>
33 #include <sfx2/objsh.hxx>
34 #include <vcl/svapp.hxx>
35 #include <vcl/settings.hxx>
37 
38 using namespace com::sun::star::uno;
39 using namespace com::sun::star::lang;
40 using namespace com::sun::star::i18n;
41 using namespace com::sun::star::frame;
42 using namespace com::sun::star::beans;
43 
44 const char cIsKernAsianPunctuation[] = "IsKernAsianPunctuation";
45 const char cCharacterCompressionType[] = "CharacterCompressionType";
46 
47 namespace {
48 
49 struct SvxForbiddenChars_Impl
50 {
51  bool bRemoved;
52  std::unique_ptr<ForbiddenCharacters> pCharacters;
53 };
54 
55 }
56 
58 {
61 
65  std::map< LanguageType, std::unique_ptr<SvxForbiddenChars_Impl> >
67 
68  bool hasForbiddenCharacters(LanguageType eLang);
69  SvxForbiddenChars_Impl* getForbiddenCharacters(LanguageType eLang);
70  void addForbiddenCharacters(LanguageType eLang, std::unique_ptr<ForbiddenCharacters> pForbidden);
71 };
72 
74 {
75  return aChangedLanguagesMap.count( eLang );
76 }
77 
79 {
80  auto it = aChangedLanguagesMap.find( eLang );
81  DBG_ASSERT( ( it != aChangedLanguagesMap.end() ), "language not available");
82  if( it != aChangedLanguagesMap.end() )
83  return it->second.get();
84  return nullptr;
85 }
86 
88  LanguageType eLang, std::unique_ptr<ForbiddenCharacters> pForbidden)
89 {
90  auto itOld = aChangedLanguagesMap.find( eLang );
91  if( itOld == aChangedLanguagesMap.end() )
92  {
93  std::unique_ptr<SvxForbiddenChars_Impl> pChar(new SvxForbiddenChars_Impl);
94  pChar->bRemoved = nullptr == pForbidden;
95  pChar->pCharacters = std::move(pForbidden);
96  aChangedLanguagesMap.emplace( eLang, std::move(pChar) );
97  }
98  else
99  {
100  itOld->second->bRemoved = nullptr == pForbidden;
101  itOld->second->pCharacters = std::move(pForbidden);
102  }
103 }
104 
106 
108  : SfxTabPage(pPage, pController, "cui/ui/optasianpage.ui", "OptAsianPage", &rSet)
109  , pImpl(new SvxAsianLayoutPage_Impl)
110  , m_xCharKerningRB(m_xBuilder->weld_radio_button("charkerning"))
111  , m_xCharPunctKerningRB(m_xBuilder->weld_radio_button("charpunctkerning"))
112  , m_xNoCompressionRB(m_xBuilder->weld_radio_button("nocompression"))
113  , m_xPunctCompressionRB(m_xBuilder->weld_radio_button("punctcompression"))
114  , m_xPunctKanaCompressionRB(m_xBuilder->weld_radio_button("punctkanacompression"))
115  , m_xLanguageFT(m_xBuilder->weld_label("languageft"))
116  , m_xLanguageLB(new SvxLanguageBox(m_xBuilder->weld_combo_box("language")))
117  , m_xStandardCB(m_xBuilder->weld_check_button("standard"))
118  , m_xStartFT(m_xBuilder->weld_label("startft"))
119  , m_xStartED(m_xBuilder->weld_entry("start"))
120  , m_xEndFT(m_xBuilder->weld_label("endft"))
121  , m_xEndED(m_xBuilder->weld_entry("end"))
122  , m_xHintFT(m_xBuilder->weld_label("hintft"))
123 {
124  LanguageHdl(*m_xLanguageLB->get_widget());
125  m_xLanguageLB->connect_changed(LINK(this, SvxAsianLayoutPage, LanguageHdl));
126  m_xStandardCB->connect_toggled(LINK(this, SvxAsianLayoutPage, ChangeStandardHdl));
127  Link<weld::Entry&,void> aLk(LINK(this, SvxAsianLayoutPage, ModifyHdl));
128  m_xStartED->connect_changed(aLk);
129  m_xEndED->connect_changed(aLk);
130 
131  m_xLanguageLB->SetLanguageList( SvxLanguageListFlags::FBD_CHARS, false, false );
132 }
133 
135 {
136 }
137 
138 std::unique_ptr<SfxTabPage> SvxAsianLayoutPage::Create(weld::Container* pPage, weld::DialogController* pController, const SfxItemSet* rAttrSet)
139 {
140  return std::make_unique<SvxAsianLayoutPage>(pPage, pController, *rAttrSet);
141 }
142 
144 {
145  if(m_xCharKerningRB->get_state_changed_from_saved())
146  {
147  pImpl->aConfig.SetKerningWesternTextOnly(m_xCharKerningRB->get_active());
148  OUString sPunct(cIsKernAsianPunctuation);
149  if(pImpl->xPrSetInfo.is() && pImpl->xPrSetInfo->hasPropertyByName(sPunct))
150  {
151  bool bVal = !m_xCharKerningRB->get_active();
152  pImpl->xPrSet->setPropertyValue(sPunct, Any(bVal));
153  }
154  }
155 
156  if(m_xNoCompressionRB->get_state_changed_from_saved() ||
157  m_xPunctCompressionRB->get_state_changed_from_saved())
158  {
159  CharCompressType nSet = m_xNoCompressionRB->get_active() ? CharCompressType::NONE :
160  m_xPunctCompressionRB->get_active() ? CharCompressType::PunctuationOnly :
161  CharCompressType::PunctuationAndKana;
162  pImpl->aConfig.SetCharDistanceCompression(nSet);
163  OUString sCompress(cCharacterCompressionType);
164  if(pImpl->xPrSetInfo.is() && pImpl->xPrSetInfo->hasPropertyByName(sCompress))
165  {
166  pImpl->xPrSet->setPropertyValue(sCompress, Any(static_cast<sal_uInt16>(nSet)));
167  }
168  }
169  pImpl->aConfig.Commit();
170  if(pImpl->xForbidden.is())
171  {
172  try
173  {
174  for (auto const& changedLanguage : pImpl->aChangedLanguagesMap)
175  {
176  Locale aLocale( LanguageTag::convertToLocale(changedLanguage.first));
177  if(changedLanguage.second->bRemoved)
178  pImpl->xForbidden->removeForbiddenCharacters( aLocale );
179  else if(changedLanguage.second->pCharacters)
180  pImpl->xForbidden->setForbiddenCharacters( aLocale, *( changedLanguage.second->pCharacters ) );
181  }
182  }
183  catch (const Exception&)
184  {
185  OSL_FAIL("exception in XForbiddenCharacters");
186  }
187  }
189 
190  return false;
191 }
192 
194 {
195  SfxViewFrame* pCurFrm = SfxViewFrame::Current();
196  SfxObjectShell* pDocSh = pCurFrm ? pCurFrm->GetObjectShell() : nullptr;
198  if(pDocSh)
199  xModel = pDocSh->GetModel();
200  Reference<XMultiServiceFactory> xFact(xModel, UNO_QUERY);
201  if(xFact.is())
202  {
203  pImpl->xPrSet.set(xFact->createInstance("com.sun.star.document.Settings"), UNO_QUERY);
204  }
205  if( pImpl->xPrSet.is() )
206  pImpl->xPrSetInfo = pImpl->xPrSet->getPropertySetInfo();
207  OUString sForbidden("ForbiddenCharacters");
208  bool bKernWesternText = pImpl->aConfig.IsKerningWesternTextOnly();
209  CharCompressType nCompress = pImpl->aConfig.GetCharDistanceCompression();
210  if(pImpl->xPrSetInfo.is())
211  {
212  if(pImpl->xPrSetInfo->hasPropertyByName(sForbidden))
213  {
214  Any aForbidden = pImpl->xPrSet->getPropertyValue(sForbidden);
215  aForbidden >>= pImpl->xForbidden;
216  }
217  OUString sCompress(cCharacterCompressionType);
218  if(pImpl->xPrSetInfo->hasPropertyByName(sCompress))
219  {
220  Any aVal = pImpl->xPrSet->getPropertyValue(sCompress);
221  sal_uInt16 nTmp;
222  if (aVal >>= nTmp)
223  nCompress = static_cast<CharCompressType>(nTmp);
224  }
225  OUString sPunct(cIsKernAsianPunctuation);
226  if(pImpl->xPrSetInfo->hasPropertyByName(sPunct))
227  {
228  Any aVal = pImpl->xPrSet->getPropertyValue(sPunct);
229  bKernWesternText = !*o3tl::doAccess<bool>(aVal);
230  }
231  }
232  else
233  {
234  m_xLanguageFT->set_sensitive(false);
235  m_xLanguageLB->set_sensitive(false);
236  m_xStandardCB->set_sensitive(false);
237  m_xStartFT->set_sensitive(false);
238  m_xStartED->set_sensitive(false);
239  m_xEndFT->set_sensitive(false);
240  m_xEndED->set_sensitive(false);
241  m_xHintFT->set_sensitive(false);
242  }
243  if(bKernWesternText)
244  m_xCharKerningRB->set_active(true);
245  else
246  m_xCharPunctKerningRB->set_active(true);
247  switch(nCompress)
248  {
249  case CharCompressType::NONE : m_xNoCompressionRB->set_active(true); break;
250  case CharCompressType::PunctuationOnly : m_xPunctCompressionRB->set_active(true); break;
251  default: m_xPunctKanaCompressionRB->set_active(true);
252  }
253  m_xCharKerningRB->save_state();
254  m_xNoCompressionRB->save_state();
255  m_xPunctCompressionRB->save_state();
256  m_xPunctKanaCompressionRB->save_state();
257 
258  m_xLanguageLB->set_active(0);
259  //preselect the system language in the box - if available
261  {
268  }
270  LanguageHdl(*m_xLanguageLB->get_widget());
271 }
272 
274 {
275  //set current value
276  LanguageType eSelectLanguage = m_xLanguageLB->get_active_id();
277  LanguageTag aLanguageTag( eSelectLanguage);
278  const Locale& aLocale( aLanguageTag.getLocale());
279 
280  OUString sStart, sEnd;
281  bool bAvail;
282  if(pImpl->xForbidden.is())
283  {
284  bAvail = pImpl->hasForbiddenCharacters(eSelectLanguage);
285  if(bAvail)
286  {
287  SvxForbiddenChars_Impl* pElement = pImpl->getForbiddenCharacters(eSelectLanguage);
288  if(pElement->bRemoved || !pElement->pCharacters)
289  {
290  bAvail = false;
291  }
292  else
293  {
294  sStart = pElement->pCharacters->beginLine;
295  sEnd = pElement->pCharacters->endLine;
296  }
297  }
298  else
299  {
300  try
301  {
302  bAvail = pImpl->xForbidden->hasForbiddenCharacters(aLocale);
303  if(bAvail)
304  {
305  ForbiddenCharacters aForbidden = pImpl->xForbidden->getForbiddenCharacters( aLocale );
306  sStart = aForbidden.beginLine;
307  sEnd = aForbidden.endLine;
308  }
309  }
310  catch (const Exception&)
311  {
312  OSL_FAIL("exception in XForbiddenCharacters");
313  }
314  }
315  }
316  else
317  {
318  bAvail = pImpl->aConfig.GetStartEndChars( aLocale, sStart, sEnd );
319  }
320  if(!bAvail)
321  {
322  LocaleDataWrapper aWrap( aLanguageTag );
323  ForbiddenCharacters aForbidden = aWrap.getForbiddenCharacters();
324  sStart = aForbidden.beginLine;
325  sEnd = aForbidden.endLine;
326  }
327  m_xStandardCB->set_active(!bAvail);
328  m_xStartED->set_sensitive(bAvail);
329  m_xEndED->set_sensitive(bAvail);
330  m_xStartFT->set_sensitive(bAvail);
331  m_xEndFT->set_sensitive(bAvail);
332  m_xStartED->set_text(sStart);
333  m_xEndED->set_text(sEnd);
334 }
335 
336 IMPL_LINK(SvxAsianLayoutPage, ChangeStandardHdl, weld::ToggleButton&, rBox, void)
337 {
338  bool bCheck = rBox.get_active();
339  m_xStartED->set_sensitive(!bCheck);
340  m_xEndED->set_sensitive(!bCheck);
341  m_xStartFT->set_sensitive(!bCheck);
342  m_xEndFT->set_sensitive(!bCheck);
343 
344  ModifyHdl(*m_xStartED);
345 }
346 
347 IMPL_LINK(SvxAsianLayoutPage, ModifyHdl, weld::Entry&, rEdit, void)
348 {
349  LanguageType eSelectLanguage = m_xLanguageLB->get_active_id();
350  Locale aLocale( LanguageTag::convertToLocale( eSelectLanguage ));
351  OUString sStart = m_xStartED->get_text();
352  OUString sEnd = m_xEndED->get_text();
353  bool bEnable = rEdit.get_sensitive();
354  if(pImpl->xForbidden.is())
355  {
356  try
357  {
358  if(bEnable)
359  {
360  std::unique_ptr<ForbiddenCharacters> pFCSet(new ForbiddenCharacters);
361  pFCSet->beginLine = sStart;
362  pFCSet->endLine = sEnd;
363  pImpl->addForbiddenCharacters(eSelectLanguage, std::move(pFCSet));
364  }
365  else
366  pImpl->addForbiddenCharacters(eSelectLanguage, nullptr);
367  }
368  catch (const Exception&)
369  {
370  OSL_FAIL("exception in XForbiddenCharacters");
371  }
372  }
373  pImpl->aConfig.SetStartEndChars( aLocale, bEnable ? &sStart : nullptr, bEnable ? &sEnd : nullptr);
374 }
375 
376 const sal_uInt16* SvxAsianLayoutPage::GetRanges()
377 {
378  //no items are used
379  static const sal_uInt16 pAsianLayoutRanges[] = { 0 };
380  return pAsianLayoutRanges;
381 }
382 
383 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::unique_ptr< weld::Label > m_xEndFT
Definition: optasian.hxx:41
SvxForbiddenChars_Impl * getForbiddenCharacters(LanguageType eLang)
Definition: optasian.cxx:78
virtual bool FillItemSet(SfxItemSet *rSet) override
Definition: optasian.cxx:143
static std::unique_ptr< SfxTabPage > Create(weld::Container *pPage, weld::DialogController *pController, const SfxItemSet *rAttrSet)
Definition: optasian.cxx:138
const char cIsKernAsianPunctuation[]
Definition: optasian.cxx:44
LanguageType getLanguageType(bool bResolveSystem=true) const
void addForbiddenCharacters(LanguageType eLang, std::unique_ptr< ForbiddenCharacters > pForbidden)
Definition: optasian.cxx:87
std::map< LanguageType, std::unique_ptr< SvxForbiddenChars_Impl > > aChangedLanguagesMap
Definition: optasian.cxx:66
std::unique_ptr< weld::RadioButton > m_xPunctCompressionRB
Definition: optasian.hxx:34
static const AllSettings & GetSettings()
css::i18n::ForbiddenCharacters getForbiddenCharacters() const
css::uno::Reference< css::frame::XModel > GetModel() const
Reference< XForbiddenCharacters > xForbidden
Definition: optasian.cxx:62
virtual SfxObjectShell * GetObjectShell() override
const css::lang::Locale & getLocale(bool bResolveSystem=true) const
static LanguageType eLastUsedLanguageTypeForForbiddenCharacters(USHRT_MAX)
bool hasForbiddenCharacters(LanguageType eLang)
Definition: optasian.cxx:73
static const sal_uInt16 * GetRanges()
Definition: optasian.cxx:376
const char cCharacterCompressionType[]
Definition: optasian.cxx:45
std::unique_ptr< SvxAsianLayoutPage_Impl > pImpl
Definition: optasian.hxx:29
std::unique_ptr< weld::Label > m_xHintFT
Definition: optasian.hxx:43
IMPL_LINK_NOARG(SvxAsianLayoutPage, LanguageHdl, weld::ComboBox &, void)
Definition: optasian.cxx:273
Reference< XPropertySet > xPrSet
Definition: optasian.cxx:63
const LanguageTag & GetLanguageTag() const
Reference< XPropertySetInfo > xPrSetInfo
Definition: optasian.cxx:64
#define DBG_ASSERT(sCon, aError)
CharCompressType
std::unique_ptr< weld::RadioButton > m_xNoCompressionRB
Definition: optasian.hxx:33
SvxAsianConfig aConfig
Definition: optasian.cxx:59
std::unique_ptr< weld::RadioButton > m_xPunctKanaCompressionRB
Definition: optasian.hxx:35
#define LANGUAGE_CHINESE_SIMPLIFIED
std::unique_ptr< weld::Label > m_xStartFT
Definition: optasian.hxx:39
static bool isSimplifiedChinese(LanguageType nLang)
std::unique_ptr< weld::RadioButton > m_xCharKerningRB
Definition: optasian.hxx:31
virtual bool get_sensitive() const =0
virtual ~SvxAsianLayoutPage() override
Definition: optasian.cxx:134
std::unique_ptr< weld::Label > m_xLanguageFT
Definition: optasian.hxx:36
virtual void Reset(const SfxItemSet *rSet) override
Definition: optasian.cxx:193
static SfxViewFrame * Current()
weld::Entry & rEdit
SvxAsianLayoutPage(weld::Container *pPage, weld::DialogController *pController, const SfxItemSet &rSet)
Definition: optasian.cxx:107
IMPL_LINK(SvxAsianLayoutPage, ChangeStandardHdl, weld::ToggleButton &, rBox, void)
Definition: optasian.cxx:336
const o3tl::enumarray< SvxAdjust, unsigned short > aSvxToUnoAdjust USHRT_MAX
std::unique_ptr< SvxLanguageBox > m_xLanguageLB
Definition: optasian.hxx:37
const char * pChar
#define LANGUAGE_CHINESE_TRADITIONAL
Reference< XModel > xModel
std::unique_ptr< weld::Entry > m_xStartED
Definition: optasian.hxx:40
std::unique_ptr< weld::Entry > m_xEndED
Definition: optasian.hxx:42
static css::lang::Locale convertToLocale(LanguageType nLangID, bool bResolveSystem=true)
std::unique_ptr< weld::RadioButton > m_xCharPunctKerningRB
Definition: optasian.hxx:32
std::unique_ptr< weld::CheckButton > m_xStandardCB
Definition: optasian.hxx:38
static bool isTraditionalChinese(LanguageType nLang)