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