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