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