Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / cui / source / options / optasian.cxx
blob50bfe4da92a581d65e66e1b6323d4a166b0d2bbb
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <map>
21 #include <optasian.hxx>
22 #include <editeng/langitem.hxx>
23 #include <editeng/unolingu.hxx>
24 #include <optasian.hrc>
25 #include <dialmgr.hxx>
26 #include <cuires.hrc>
27 #include <i18npool/mslangid.hxx>
28 #include <tools/shl.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 <comphelper/processfactory.hxx>
38 #include <unotools/localedatawrapper.hxx>
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 using rtl::OUString;
47 const sal_Char cIsKernAsianPunctuation[] = "IsKernAsianPunctuation";
48 const sal_Char cCharacterCompressionType[] = "CharacterCompressionType";
50 struct SvxForbiddenChars_Impl
52 ~SvxForbiddenChars_Impl();
54 sal_Bool bRemoved;
55 ForbiddenCharacters* pCharacters;
58 SvxForbiddenChars_Impl::~SvxForbiddenChars_Impl()
60 delete pCharacters;
63 typedef ::std::map< LanguageType, SvxForbiddenChars_Impl* > SvxForbiddenCharacterMap_Impl;
65 struct SvxAsianLayoutPage_Impl
67 SvxAsianConfig aConfig;
68 SvxAsianLayoutPage_Impl() {}
70 ~SvxAsianLayoutPage_Impl();
72 Reference< XForbiddenCharacters > xForbidden;
73 Reference< XPropertySet > xPrSet;
74 Reference< XPropertySetInfo > xPrSetInfo;
75 SvxForbiddenCharacterMap_Impl aChangedLanguagesMap;
77 sal_Bool hasForbiddenCharacters(LanguageType eLang);
78 SvxForbiddenChars_Impl* getForbiddenCharacters(LanguageType eLang);
79 void addForbiddenCharacters(LanguageType eLang, ForbiddenCharacters* pForbidden);
82 SvxAsianLayoutPage_Impl::~SvxAsianLayoutPage_Impl()
84 SvxForbiddenCharacterMap_Impl::iterator it;
85 for( it = aChangedLanguagesMap.begin(); it != aChangedLanguagesMap.end(); ++it )
87 delete it->second;
91 sal_Bool SvxAsianLayoutPage_Impl::hasForbiddenCharacters(LanguageType eLang)
93 return aChangedLanguagesMap.count( eLang );
96 SvxForbiddenChars_Impl* SvxAsianLayoutPage_Impl::getForbiddenCharacters(LanguageType eLang)
98 SvxForbiddenCharacterMap_Impl::iterator it = aChangedLanguagesMap.find( eLang );
99 DBG_ASSERT( ( it != aChangedLanguagesMap.end() ), "language not available");
100 if( it != aChangedLanguagesMap.end() )
101 return it->second;
102 return 0;
105 void SvxAsianLayoutPage_Impl::addForbiddenCharacters(
106 LanguageType eLang, ForbiddenCharacters* pForbidden)
108 SvxForbiddenCharacterMap_Impl::iterator itOld = aChangedLanguagesMap.find( eLang );
109 if( itOld == aChangedLanguagesMap.end() )
111 SvxForbiddenChars_Impl* pChar = new SvxForbiddenChars_Impl;
112 pChar->bRemoved = 0 == pForbidden;
113 pChar->pCharacters = pForbidden ? new ForbiddenCharacters(*pForbidden) : 0;
114 aChangedLanguagesMap.insert( ::std::make_pair( eLang, pChar ) );
116 else
118 itOld->second->bRemoved = 0 == pForbidden;
119 delete itOld->second->pCharacters;
120 itOld->second->pCharacters = pForbidden ? new ForbiddenCharacters(*pForbidden) : 0;
124 static LanguageType eLastUsedLanguageTypeForForbiddenCharacters = USHRT_MAX;
126 SvxAsianLayoutPage::SvxAsianLayoutPage( Window* pParent, const SfxItemSet& rSet ) :
127 SfxTabPage(pParent, CUI_RES( RID_SVXPAGE_ASIAN_LAYOUT ), rSet),
128 aKerningGB( this, CUI_RES(GB_KERNING )),
129 aCharKerningRB( this, CUI_RES(RB_CHAR_KERNING )),
130 aCharPunctKerningRB( this, CUI_RES(RB_CHAR_PUNCT )),
131 aCharDistGB( this, CUI_RES(GB_CHAR_DIST )),
132 aNoCompressionRB( this, CUI_RES(RB_NO_COMP )),
133 aPunctCompressionRB( this, CUI_RES(RB_PUNCT_COMP )),
134 aPunctKanaCompressionRB(this, CUI_RES(RB_PUNCT_KANA_COMP )),
135 aStartEndGB( this, CUI_RES(GB_START_END )),
136 aLanguageFT( this, CUI_RES(FT_LANGUAGE )),
137 aLanguageLB( this, CUI_RES(LB_LANGUAGE )),
138 aStandardCB( this, CUI_RES(CB_STANDARD )),
139 aStartFT( this, CUI_RES(FT_START )),
140 aStartED( this, CUI_RES(ED_START )),
141 aEndFT( this, CUI_RES(FT_END )),
142 aEndED( this, CUI_RES(ED_END )),
143 aHintFT( this, CUI_RES(FT_HINT )),
144 pImpl(new SvxAsianLayoutPage_Impl)
146 FreeResource();
147 LanguageHdl(&aLanguageLB);
148 aLanguageLB.SetSelectHdl(LINK(this, SvxAsianLayoutPage, LanguageHdl));
149 aStandardCB.SetClickHdl(LINK(this, SvxAsianLayoutPage, ChangeStandardHdl));
150 Link aLk(LINK(this, SvxAsianLayoutPage, ModifyHdl));
151 aStartED.SetModifyHdl(aLk);
152 aEndED.SetModifyHdl(aLk);
154 aLanguageLB.SetLanguageList( LANG_LIST_FBD_CHARS, sal_False, sal_False );
157 SvxAsianLayoutPage::~SvxAsianLayoutPage()
159 delete pImpl;
162 SfxTabPage* SvxAsianLayoutPage::Create( Window* pParent, const SfxItemSet& rAttrSet )
164 return new SvxAsianLayoutPage(pParent, rAttrSet);
167 sal_Bool SvxAsianLayoutPage::FillItemSet( SfxItemSet& )
169 if(aCharKerningRB.IsChecked() != aCharKerningRB.GetSavedValue())
171 pImpl->aConfig.SetKerningWesternTextOnly(aCharKerningRB.IsChecked());
172 OUString sPunct(cIsKernAsianPunctuation);
173 if(pImpl->xPrSetInfo.is() && pImpl->xPrSetInfo->hasPropertyByName(sPunct))
175 Any aVal;
176 sal_Bool bVal = !aCharKerningRB.IsChecked();
177 aVal.setValue(&bVal, ::getBooleanCppuType());
178 pImpl->xPrSet->setPropertyValue(sPunct, aVal);
182 if(aNoCompressionRB.IsChecked() != aNoCompressionRB.GetSavedValue() ||
183 aPunctCompressionRB.IsChecked() != aPunctCompressionRB.GetSavedValue())
185 sal_Int16 nSet = aNoCompressionRB.IsChecked() ? 0 :
186 aPunctCompressionRB.IsChecked() ? 1 : 2;
187 pImpl->aConfig.SetCharDistanceCompression(nSet);
188 OUString sCompress(cCharacterCompressionType);
189 if(pImpl->xPrSetInfo.is() && pImpl->xPrSetInfo->hasPropertyByName(sCompress))
191 Any aVal;
192 aVal <<= nSet;
193 pImpl->xPrSet->setPropertyValue(sCompress, aVal);
196 pImpl->aConfig.Commit();
197 if(pImpl->xForbidden.is())
201 SvxForbiddenCharacterMap_Impl::iterator itElem;
202 for( itElem = pImpl->aChangedLanguagesMap.begin();
203 itElem != pImpl->aChangedLanguagesMap.end(); ++itElem )
205 Locale aLocale( LanguageTag( itElem->first ).getLocale());
206 if(itElem->second->bRemoved)
207 pImpl->xForbidden->removeForbiddenCharacters( aLocale );
208 else if(itElem->second->pCharacters)
209 pImpl->xForbidden->setForbiddenCharacters( aLocale, *( itElem->second->pCharacters ) );
212 catch (const Exception&)
214 OSL_FAIL("exception in XForbiddenCharacters");
217 eLastUsedLanguageTypeForForbiddenCharacters = aLanguageLB.GetSelectLanguage();
219 return sal_False;
222 void SvxAsianLayoutPage::Reset( const SfxItemSet& )
224 SfxViewFrame* pCurFrm = SfxViewFrame::Current();
225 SfxObjectShell* pDocSh = pCurFrm ? pCurFrm->GetObjectShell() : 0;
226 Reference< XModel > xModel;
227 if(pDocSh)
228 xModel = pDocSh->GetModel();
229 Reference<XMultiServiceFactory> xFact(xModel, UNO_QUERY);
230 if(xFact.is())
232 pImpl->xPrSet = Reference<XPropertySet>(
233 xFact->createInstance("com.sun.star.document.Settings"), UNO_QUERY);
235 if( pImpl->xPrSet.is() )
236 pImpl->xPrSetInfo = pImpl->xPrSet->getPropertySetInfo();
237 OUString sForbidden("ForbiddenCharacters");
238 sal_Bool bKernWesternText = pImpl->aConfig.IsKerningWesternTextOnly();
239 sal_Int16 nCompress = pImpl->aConfig.GetCharDistanceCompression();
240 if(pImpl->xPrSetInfo.is())
242 if(pImpl->xPrSetInfo->hasPropertyByName(sForbidden))
244 Any aForbidden = pImpl->xPrSet->getPropertyValue(sForbidden);
245 aForbidden >>= pImpl->xForbidden;
247 OUString sCompress(cCharacterCompressionType);
248 if(pImpl->xPrSetInfo->hasPropertyByName(sCompress))
250 Any aVal = pImpl->xPrSet->getPropertyValue(sCompress);
251 aVal >>= nCompress;
253 OUString sPunct(cIsKernAsianPunctuation);
254 if(pImpl->xPrSetInfo->hasPropertyByName(sPunct))
256 Any aVal = pImpl->xPrSet->getPropertyValue(sPunct);
257 bKernWesternText = !*(sal_Bool*)aVal.getValue();
260 else
262 aStartEndGB.Enable(sal_False);
263 aLanguageFT.Enable(sal_False);
264 aLanguageLB.Enable(sal_False);
265 aStandardCB.Enable(sal_False);
266 aStartFT.Enable(sal_False);
267 aStartED.Enable(sal_False);
268 aEndFT.Enable(sal_False);
269 aEndED.Enable(sal_False);
270 aHintFT.Enable(sal_False);
272 if(bKernWesternText)
273 aCharKerningRB.Check(sal_True);
274 else
275 aCharPunctKerningRB.Check(sal_True);
276 switch(nCompress)
278 case 0 : aNoCompressionRB.Check(); break;
279 case 1 : aPunctCompressionRB.Check(); break;
280 default: aPunctKanaCompressionRB.Check();
282 aCharKerningRB.SaveValue();
283 aNoCompressionRB.SaveValue();
284 aPunctCompressionRB.SaveValue();
285 aPunctKanaCompressionRB.SaveValue();
287 aLanguageLB.SelectEntryPos(0);
288 //preselect the system language in the box - if available
289 if(USHRT_MAX == eLastUsedLanguageTypeForForbiddenCharacters)
291 eLastUsedLanguageTypeForForbiddenCharacters =
292 Application::GetSettings().GetLanguageTag().getLanguageType();
293 if (MsLangId::isSimplifiedChinese(eLastUsedLanguageTypeForForbiddenCharacters))
294 eLastUsedLanguageTypeForForbiddenCharacters = LANGUAGE_CHINESE_SIMPLIFIED;
295 else if (MsLangId::isTraditionalChinese(eLastUsedLanguageTypeForForbiddenCharacters))
296 eLastUsedLanguageTypeForForbiddenCharacters = LANGUAGE_CHINESE_TRADITIONAL;
298 aLanguageLB.SelectLanguage( eLastUsedLanguageTypeForForbiddenCharacters );
299 LanguageHdl(&aLanguageLB);
302 IMPL_LINK_NOARG(SvxAsianLayoutPage, LanguageHdl)
304 //set current value
305 LanguageType eSelectLanguage = aLanguageLB.GetSelectLanguage();
306 LanguageTag aLanguageTag( eSelectLanguage);
307 Locale aLocale( aLanguageTag.getLocale());
309 OUString sStart, sEnd;
310 sal_Bool bAvail;
311 if(pImpl->xForbidden.is())
313 bAvail = pImpl->hasForbiddenCharacters(eSelectLanguage);
314 if(bAvail)
316 SvxForbiddenChars_Impl* pElement = pImpl->getForbiddenCharacters(eSelectLanguage);
317 if(pElement->bRemoved || !pElement->pCharacters)
319 bAvail = sal_False;
321 else
323 sStart = pElement->pCharacters->beginLine;
324 sEnd = pElement->pCharacters->endLine;
327 else
331 bAvail = pImpl->xForbidden->hasForbiddenCharacters(aLocale);
332 if(bAvail)
334 ForbiddenCharacters aForbidden = pImpl->xForbidden->getForbiddenCharacters( aLocale );
335 sStart = aForbidden.beginLine;
336 sEnd = aForbidden.endLine;
339 catch (const Exception&)
341 OSL_FAIL("exception in XForbiddenCharacters");
345 else
347 bAvail = pImpl->aConfig.GetStartEndChars( aLocale, sStart, sEnd );
349 if(!bAvail)
351 LocaleDataWrapper aWrap( aLanguageTag );
352 ForbiddenCharacters aForbidden = aWrap.getForbiddenCharacters();
353 sStart = aForbidden.beginLine;
354 sEnd = aForbidden.endLine;
356 aStandardCB.Check(!bAvail);
357 aStartED.Enable(bAvail);
358 aEndED.Enable(bAvail);
359 aStartFT.Enable(bAvail);
360 aEndFT.Enable(bAvail);
361 aStartED.SetText(sStart);
362 aEndED.SetText(sEnd);
364 return 0;
367 IMPL_LINK(SvxAsianLayoutPage, ChangeStandardHdl, CheckBox*, pBox)
369 sal_Bool bCheck = pBox->IsChecked();
370 aStartED.Enable(!bCheck);
371 aEndED.Enable(!bCheck);
372 aStartFT.Enable(!bCheck);
373 aEndFT.Enable(!bCheck);
375 ModifyHdl(&aStartED);
376 return 0;
379 IMPL_LINK(SvxAsianLayoutPage, ModifyHdl, Edit*, pEdit)
381 LanguageType eSelectLanguage = aLanguageLB.GetSelectLanguage();
382 Locale aLocale( LanguageTag( eSelectLanguage ).getLocale());
383 OUString sStart = aStartED.GetText();
384 OUString sEnd = aEndED.GetText();
385 sal_Bool bEnable = pEdit->IsEnabled();
386 if(pImpl->xForbidden.is())
390 if(bEnable)
392 ForbiddenCharacters aSet;
393 aSet.beginLine = sStart;
394 aSet.endLine = sEnd;
395 pImpl->addForbiddenCharacters(eSelectLanguage, &aSet);
397 else
398 pImpl->addForbiddenCharacters(eSelectLanguage, 0);
400 catch (const Exception&)
402 OSL_FAIL("exception in XForbiddenCharacters");
405 pImpl->aConfig.SetStartEndChars( aLocale, bEnable ? &sStart : 0, bEnable ? &sEnd : 0);
406 return 0;
409 sal_uInt16* SvxAsianLayoutPage::GetRanges()
411 //no items are used
412 static sal_uInt16 pAsianLayoutRanges[] = { 0 };
413 return pAsianLayoutRanges;
416 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */