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 <tools/debug.hxx>
25#include <o3tl/any.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
39using namespace com::sun::star::uno;
40using namespace com::sun::star::lang;
41using namespace com::sun::star::i18n;
42using namespace com::sun::star::frame;
43using namespace com::sun::star::beans;
44
45constexpr OUStringLiteral cIsKernAsianPunctuation = u"IsKernAsianPunctuation";
46constexpr OUStringLiteral cCharacterCompressionType = u"CharacterCompressionType";
47
48namespace {
49
50struct SvxForbiddenChars_Impl
51{
52 bool bRemoved;
53 std::optional<ForbiddenCharacters> oCharacters;
54};
55
56}
57
59{
62
66 std::map< LanguageType, SvxForbiddenChars_Impl >
68
70 SvxForbiddenChars_Impl* getForbiddenCharacters(LanguageType eLang);
71 void addForbiddenCharacters(LanguageType eLang, std::optional<ForbiddenCharacters> oForbidden);
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;
85 return nullptr;
86}
87
89 LanguageType eLang, std::optional<ForbiddenCharacters> oForbidden)
90{
91 auto itOld = aChangedLanguagesMap.find( eLang );
92 if( itOld == aChangedLanguagesMap.end() )
93 {
94 SvxForbiddenChars_Impl aChar;
95 aChar.bRemoved = !oForbidden.has_value();
96 aChar.oCharacters = std::move(oForbidden);
97 aChangedLanguagesMap.emplace( eLang, std::move(aChar) );
98 }
99 else
100 {
101 itOld->second.bRemoved = !oForbidden.has_value();
102 itOld->second.oCharacters = std::move(oForbidden);
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
139std::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.oCharacters)
181 pImpl->xForbidden->setForbiddenCharacters( aLocale, *( changedLanguage.second.oCharacters ) );
182 }
183 }
184 catch (const Exception&)
185 {
186 TOOLS_WARN_EXCEPTION( "cui.options", "in XForbiddenCharacters");
187 }
188 }
190
191 return false;
192}
193
195{
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 bool bKernWesternText = pImpl->aConfig.IsKerningWesternTextOnly();
209 CharCompressType nCompress = pImpl->aConfig.GetCharDistanceCompression();
210 if(pImpl->xPrSetInfo.is())
211 {
212 OUString sForbidden("ForbiddenCharacters");
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->oCharacters)
290 {
291 bAvail = false;
292 }
293 else
294 {
295 sStart = pElement->oCharacters->beginLine;
296 sEnd = pElement->oCharacters->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 TOOLS_WARN_EXCEPTION( "cui.options", "in XForbiddenCharacters");
314 }
315 }
316 }
317 else
318 {
319 bAvail = pImpl->aConfig.GetStartEndChars( aLocale, sStart, sEnd );
320 }
321 if(!bAvail)
322 {
323 LocaleDataWrapper aWrap( std::move(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
337IMPL_LINK(SvxAsianLayoutPage, ChangeStandardHdl, weld::Toggleable&, 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
348IMPL_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 ForbiddenCharacters aFCSet;
362 aFCSet.beginLine = sStart;
363 aFCSet.endLine = sEnd;
364 pImpl->addForbiddenCharacters(eSelectLanguage, std::move(aFCSet));
365 }
366 else
367 pImpl->addForbiddenCharacters(eSelectLanguage, std::nullopt);
368 }
369 catch (const Exception&)
370 {
371 TOOLS_WARN_EXCEPTION( "cui.options", "in XForbiddenCharacters");
372 }
373 }
374 pImpl->aConfig.SetStartEndChars( aLocale, bEnable ? &sStart : nullptr, bEnable ? &sEnd : nullptr);
375}
376
378{
379 //no items are used
380 return WhichRangesContainer();
381}
382
383/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
CharCompressType
const LanguageTag & GetLanguageTag() const
static const AllSettings & GetSettings()
LanguageType getLanguageType(bool bResolveSystem=true) const
const css::lang::Locale & getLocale(bool bResolveSystem=true) const
static css::lang::Locale convertToLocale(LanguageType nLangID, bool bResolveSystem=true)
css::i18n::ForbiddenCharacters getForbiddenCharacters() const
static bool isTraditionalChinese(LanguageType nLang)
static bool isSimplifiedChinese(LanguageType nLang)
css::uno::Reference< css::frame::XModel3 > GetModel() const
static SAL_WARN_UNUSED_RESULT SfxViewFrame * Current()
virtual SfxObjectShell * GetObjectShell() override
std::unique_ptr< weld::RadioButton > m_xCharKerningRB
Definition: optasian.hxx:30
std::unique_ptr< weld::Label > m_xLanguageFT
Definition: optasian.hxx:35
std::unique_ptr< weld::Label > m_xStartFT
Definition: optasian.hxx:38
std::unique_ptr< weld::Entry > m_xEndED
Definition: optasian.hxx:41
virtual ~SvxAsianLayoutPage() override
Definition: optasian.cxx:135
virtual void Reset(const SfxItemSet *rSet) override
Definition: optasian.cxx:194
std::unique_ptr< weld::CheckButton > m_xStandardCB
Definition: optasian.hxx:37
std::unique_ptr< weld::Entry > m_xStartED
Definition: optasian.hxx:39
std::unique_ptr< SvxLanguageBox > m_xLanguageLB
Definition: optasian.hxx:36
std::unique_ptr< weld::Label > m_xEndFT
Definition: optasian.hxx:40
static WhichRangesContainer GetRanges()
Definition: optasian.cxx:377
std::unique_ptr< weld::RadioButton > m_xNoCompressionRB
Definition: optasian.hxx:32
SvxAsianLayoutPage(weld::Container *pPage, weld::DialogController *pController, const SfxItemSet &rSet)
Definition: optasian.cxx:108
std::unique_ptr< weld::RadioButton > m_xPunctKanaCompressionRB
Definition: optasian.hxx:34
virtual bool FillItemSet(SfxItemSet *rSet) override
Definition: optasian.cxx:144
std::unique_ptr< weld::RadioButton > m_xPunctCompressionRB
Definition: optasian.hxx:33
std::unique_ptr< SvxAsianLayoutPage_Impl > pImpl
Definition: optasian.hxx:28
static std::unique_ptr< SfxTabPage > Create(weld::Container *pPage, weld::DialogController *pController, const SfxItemSet *rAttrSet)
Definition: optasian.cxx:139
std::unique_ptr< weld::Label > m_xHintFT
Definition: optasian.hxx:42
std::unique_ptr< weld::RadioButton > m_xCharPunctKerningRB
Definition: optasian.hxx:31
#define DBG_ASSERT(sCon, aError)
#define TOOLS_WARN_EXCEPTION(area, stream)
float u
#define LANGUAGE_CHINESE_TRADITIONAL
#define LANGUAGE_CHINESE_SIMPLIFIED
@ Exception
IMPL_LINK_NOARG(SvxAsianLayoutPage, LanguageHdl, weld::ComboBox &, void)
Definition: optasian.cxx:274
IMPL_LINK(SvxAsianLayoutPage, ChangeStandardHdl, weld::Toggleable &, rBox, void)
Definition: optasian.cxx:337
constexpr OUStringLiteral cCharacterCompressionType
Definition: optasian.cxx:46
static LanguageType eLastUsedLanguageTypeForForbiddenCharacters(USHRT_MAX)
constexpr OUStringLiteral cIsKernAsianPunctuation
Definition: optasian.cxx:45
static SfxItemSet & rSet
std::map< LanguageType, SvxForbiddenChars_Impl > aChangedLanguagesMap
Definition: optasian.cxx:67
Reference< XPropertySet > xPrSet
Definition: optasian.cxx:64
Reference< XForbiddenCharacters > xForbidden
Definition: optasian.cxx:63
SvxForbiddenChars_Impl * getForbiddenCharacters(LanguageType eLang)
Definition: optasian.cxx:79
void addForbiddenCharacters(LanguageType eLang, std::optional< ForbiddenCharacters > oForbidden)
Definition: optasian.cxx:88
bool hasForbiddenCharacters(LanguageType eLang)
Definition: optasian.cxx:74
SvxAsianConfig aConfig
Definition: optasian.cxx:60
Reference< XPropertySetInfo > xPrSetInfo
Definition: optasian.cxx:65
Reference< XModel > xModel