xref: /aoo41x/main/cui/source/options/optasian.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_cui.hxx"
30 
31 #include <optasian.hxx>
32 #include <editeng/langitem.hxx>
33 #include <editeng/unolingu.hxx>
34 #include <optasian.hrc>
35 #include <dialmgr.hxx>
36 #include <cuires.hrc>
37 #include <tools/table.hxx>
38 #include <tools/shl.hxx>
39 #include <svl/asiancfg.hxx>
40 #include <com/sun/star/lang/Locale.hpp>
41 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
42 #include <com/sun/star/i18n/XForbiddenCharacters.hpp>
43 #include <com/sun/star/beans/XPropertySet.hpp>
44 #include <sfx2/viewfrm.hxx>
45 #include <sfx2/objsh.hxx>
46 #include <vcl/svapp.hxx>
47 #include <comphelper/processfactory.hxx>
48 #include <unotools/localedatawrapper.hxx>
49 
50 using namespace com::sun::star::uno;
51 using namespace com::sun::star::lang;
52 using namespace com::sun::star::i18n;
53 using namespace com::sun::star::frame;
54 using namespace com::sun::star::beans;
55 using rtl::OUString;
56 
57 #define C2U(cChar) rtl::OUString::createFromAscii(cChar)
58 
59 const sal_Char cIsKernAsianPunctuation[] = "IsKernAsianPunctuation";
60 const sal_Char cCharacterCompressionType[] = "CharacterCompressionType";
61 
62 struct SvxForbiddenChars_Impl
63 {
64 	sal_Bool 				bRemoved;
65 	ForbiddenCharacters*	pCharacters;
66 };
67 
68 DECLARE_TABLE( _SvxForbiddenCharacterTable_Impl, SvxForbiddenChars_Impl* )
69 
70 class SvxForbiddenCharacterTable_Impl : public _SvxForbiddenCharacterTable_Impl
71 {
72 public:
73 	SvxForbiddenCharacterTable_Impl()
74 		: _SvxForbiddenCharacterTable_Impl( 4, 4 )
75 	{}
76 	~SvxForbiddenCharacterTable_Impl();
77 };
78 
79 struct SvxAsianLayoutPage_Impl
80 {
81 	SvxAsianConfig 	aConfig;
82 		SvxAsianLayoutPage_Impl() :
83 			aConfig(sal_False){}
84 
85 	Reference< XForbiddenCharacters > 	xForbidden;
86     Reference< XPropertySet >           xPrSet;
87     Reference< XPropertySetInfo >       xPrSetInfo;
88     SvxForbiddenCharacterTable_Impl     aChangedLanguagesTbl;
89 
90 	sal_Bool				hasForbiddenCharacters(LanguageType eLang);
91 	SvxForbiddenChars_Impl* getForbiddenCharacters(LanguageType eLang);
92 	void					addForbiddenCharacters(LanguageType eLang, ForbiddenCharacters* pForbidden);
93 };
94 /* -----------------------------24.01.01 14:50--------------------------------
95 
96  ---------------------------------------------------------------------------*/
97 SvxForbiddenCharacterTable_Impl::~SvxForbiddenCharacterTable_Impl()
98 {
99 	for( SvxForbiddenChars_Impl*  pDel = First(); pDel; pDel = Next() )
100 	{
101 		delete pDel->pCharacters;
102 		delete pDel;
103 	}
104 }
105 /* -----------------------------24.01.01 14:50--------------------------------
106 
107  ---------------------------------------------------------------------------*/
108 sal_Bool	SvxAsianLayoutPage_Impl::hasForbiddenCharacters(LanguageType eLang)
109 {
110 	return 0 != aChangedLanguagesTbl.Get(eLang);
111 }
112 /* -----------------------------24.01.01 14:50--------------------------------
113 
114  ---------------------------------------------------------------------------*/
115 SvxForbiddenChars_Impl* SvxAsianLayoutPage_Impl::getForbiddenCharacters(LanguageType eLang)
116 {
117 	SvxForbiddenChars_Impl* pImp = aChangedLanguagesTbl.Get(eLang);
118 	DBG_ASSERT(pImp, "language not available");
119 	if(pImp)
120 		return pImp;
121 	return 0;
122 }
123 /* -----------------------------24.01.01 14:50--------------------------------
124 
125  ---------------------------------------------------------------------------*/
126 void SvxAsianLayoutPage_Impl::addForbiddenCharacters(
127 	LanguageType eLang, ForbiddenCharacters* pForbidden)
128 {
129 	SvxForbiddenChars_Impl* pOld = aChangedLanguagesTbl.Get(eLang);
130 	if( !pOld )
131 	{
132 		pOld = new SvxForbiddenChars_Impl;
133 		pOld->bRemoved = 0 == pForbidden;
134 		pOld->pCharacters = pForbidden ? new ForbiddenCharacters(*pForbidden) : 0;
135 		aChangedLanguagesTbl.Insert( eLang, pOld );
136 	}
137 	else
138 	{
139 		pOld->bRemoved = 0 == pForbidden;
140 		delete pOld->pCharacters;
141 		pOld->pCharacters = pForbidden ? new ForbiddenCharacters(*pForbidden) : 0;
142 	}
143 
144 }
145 /*-- 09.01.01 13:29:02---------------------------------------------------
146 
147   -----------------------------------------------------------------------*/
148 static LanguageType eLastUsedLanguageTypeForForbiddenCharacters = USHRT_MAX;
149 
150 SvxAsianLayoutPage::SvxAsianLayoutPage( Window* pParent, const SfxItemSet& rSet ) :
151 	SfxTabPage(pParent, CUI_RES( RID_SVXPAGE_ASIAN_LAYOUT ), rSet),
152 	aKerningGB(				this, CUI_RES(GB_KERNING			)),
153 	aCharKerningRB(			this, CUI_RES(RB_CHAR_KERNING		)),
154 	aCharPunctKerningRB(	this, CUI_RES(RB_CHAR_PUNCT		)),
155 	aCharDistGB(			this, CUI_RES(GB_CHAR_DIST		)),
156 	aNoCompressionRB(		this, CUI_RES(RB_NO_COMP			)),
157 	aPunctCompressionRB(	this, CUI_RES(RB_PUNCT_COMP		)),
158  	aPunctKanaCompressionRB(this, CUI_RES(RB_PUNCT_KANA_COMP	)),
159 	aStartEndGB(			this, CUI_RES(GB_START_END		)),
160 	aLanguageFT(			this, CUI_RES(FT_LANGUAGE			)),
161 	aLanguageLB(			this, CUI_RES(LB_LANGUAGE			)),
162 	aStandardCB(			this, CUI_RES(CB_STANDARD			)),
163 	aStartFT(				this, CUI_RES(FT_START			)),
164 	aStartED(				this, CUI_RES(ED_START			)),
165 	aEndFT(					this, CUI_RES(FT_END				)),
166 	aEndED(					this, CUI_RES(ED_END				)),
167 	aHintFT(				this, CUI_RES(FT_HINT				)),
168 	pImpl(new SvxAsianLayoutPage_Impl)
169 {
170 	FreeResource();
171 	LanguageHdl(&aLanguageLB);
172 	aLanguageLB.SetSelectHdl(LINK(this, SvxAsianLayoutPage, LanguageHdl));
173 	aStandardCB.SetClickHdl(LINK(this, SvxAsianLayoutPage, ChangeStandardHdl));
174 	Link aLk(LINK(this, SvxAsianLayoutPage, ModifyHdl));
175 	aStartED.SetModifyHdl(aLk);
176 	aEndED.SetModifyHdl(aLk);
177 
178 	aLanguageLB.SetLanguageList( LANG_LIST_FBD_CHARS, sal_False, sal_False );
179 }
180 /*-- 09.01.01 13:29:02---------------------------------------------------
181 
182   -----------------------------------------------------------------------*/
183 SvxAsianLayoutPage::~SvxAsianLayoutPage()
184 {
185 	delete pImpl;
186 }
187 /*-- 09.01.01 13:29:02---------------------------------------------------
188 
189   -----------------------------------------------------------------------*/
190 SfxTabPage*	SvxAsianLayoutPage::Create( Window* pParent, const SfxItemSet& rAttrSet )
191 {
192 	return new SvxAsianLayoutPage(pParent, rAttrSet);
193 }
194 /*-- 09.01.01 13:29:03---------------------------------------------------
195 
196   -----------------------------------------------------------------------*/
197 sal_Bool SvxAsianLayoutPage::FillItemSet( SfxItemSet& )
198 {
199 	if(aCharKerningRB.IsChecked() != aCharKerningRB.GetSavedValue())
200     {
201 		pImpl->aConfig.SetKerningWesternTextOnly(aCharKerningRB.IsChecked());
202         OUString sPunct(C2U(cIsKernAsianPunctuation));
203         if(pImpl->xPrSetInfo.is() && pImpl->xPrSetInfo->hasPropertyByName(sPunct))
204         {
205             Any aVal;
206             sal_Bool bVal = !aCharKerningRB.IsChecked();
207             aVal.setValue(&bVal, ::getBooleanCppuType());
208             pImpl->xPrSet->setPropertyValue(sPunct, aVal);
209         }
210     }
211 
212 	if(aNoCompressionRB.IsChecked() != aNoCompressionRB.GetSavedValue() ||
213 			aPunctCompressionRB.IsChecked() != aPunctCompressionRB.GetSavedValue())
214 	{
215 		sal_Int16 nSet = aNoCompressionRB.IsChecked() ? 0 :
216 							aPunctCompressionRB.IsChecked() ? 1 : 2;
217 		pImpl->aConfig.SetCharDistanceCompression(nSet);
218         OUString sCompress(C2U(cCharacterCompressionType));
219         if(pImpl->xPrSetInfo.is() && pImpl->xPrSetInfo->hasPropertyByName(sCompress))
220         {
221             Any aVal;
222             aVal <<= nSet;
223             pImpl->xPrSet->setPropertyValue(sCompress, aVal);
224         }
225     }
226 	if(pImpl->aConfig.IsModified())
227 		pImpl->aConfig.Commit();
228 	if(pImpl->xForbidden.is())
229 	{
230 		try
231 		{
232 			for( SvxForbiddenChars_Impl*  pElem = pImpl->aChangedLanguagesTbl.First();
233 				pElem; pElem = pImpl->aChangedLanguagesTbl.Next() )
234 			{
235 				sal_uLong nLang = pImpl->aChangedLanguagesTbl.GetKey( pElem );
236 				Locale aLocale;
237 				SvxLanguageToLocale(aLocale, (sal_uInt16)nLang );
238 				if(pElem->bRemoved)
239 					pImpl->xForbidden->removeForbiddenCharacters( aLocale );
240 				else if(pElem->pCharacters)
241 					pImpl->xForbidden->setForbiddenCharacters( aLocale, *pElem->pCharacters );
242 			}
243 		}
244 		catch(Exception&)
245 		{
246 			DBG_ERROR("exception in XForbiddenCharacters");
247 		}
248 	}
249     eLastUsedLanguageTypeForForbiddenCharacters = aLanguageLB.GetSelectLanguage();
250 
251 	return sal_False;
252 }
253 /*-- 09.01.01 13:29:03---------------------------------------------------
254 
255   -----------------------------------------------------------------------*/
256 void SvxAsianLayoutPage::Reset( const SfxItemSet& )
257 {
258 	SfxViewFrame* pCurFrm = SfxViewFrame::Current();
259 	SfxObjectShell* pDocSh = pCurFrm ? pCurFrm->GetObjectShell() : 0;
260 	Reference< XModel >	xModel;
261 	if(pDocSh)
262 		xModel = pDocSh->GetModel();
263     Reference<XMultiServiceFactory> xFact(xModel, UNO_QUERY);
264     if(xFact.is())
265 	{
266 		pImpl->xPrSet = Reference<XPropertySet>(
267 			xFact->createInstance(C2U("com.sun.star.document.Settings")), UNO_QUERY);
268 	}
269     if( pImpl->xPrSet.is() )
270         pImpl->xPrSetInfo = pImpl->xPrSet->getPropertySetInfo();
271 	OUString sForbidden(C2U("ForbiddenCharacters"));
272     sal_Bool bKernWesternText = pImpl->aConfig.IsKerningWesternTextOnly();
273     sal_Int16 nCompress = pImpl->aConfig.GetCharDistanceCompression();
274     if(pImpl->xPrSetInfo.is())
275     {
276         if(pImpl->xPrSetInfo->hasPropertyByName(sForbidden))
277         {
278             Any aForbidden = pImpl->xPrSet->getPropertyValue(sForbidden);
279             aForbidden >>= pImpl->xForbidden;
280         }
281         OUString sCompress(C2U(cCharacterCompressionType));
282         if(pImpl->xPrSetInfo->hasPropertyByName(sCompress))
283         {
284             Any aVal = pImpl->xPrSet->getPropertyValue(sCompress);
285             aVal >>= nCompress;
286         }
287         OUString sPunct(C2U(cIsKernAsianPunctuation));
288         if(pImpl->xPrSetInfo->hasPropertyByName(sPunct))
289         {
290             Any aVal = pImpl->xPrSet->getPropertyValue(sPunct);
291             bKernWesternText = !*(sal_Bool*)aVal.getValue();
292         }
293     }
294     else
295     {
296         aStartEndGB.Enable(sal_False);
297         aLanguageFT.Enable(sal_False);
298         aLanguageLB.Enable(sal_False);
299         aStandardCB.Enable(sal_False);
300         aStartFT.Enable(sal_False);
301         aStartED.Enable(sal_False);
302         aEndFT.Enable(sal_False);
303         aEndED.Enable(sal_False);
304         aHintFT.Enable(sal_False);
305     }
306     if(bKernWesternText)
307 		aCharKerningRB.Check(sal_True);
308 	else
309 		aCharPunctKerningRB.Check(sal_True);
310     switch(nCompress)
311 	{
312 		case 0 : aNoCompressionRB.Check();        break;
313 		case 1 : aPunctCompressionRB.Check();     break;
314 		default: aPunctKanaCompressionRB.Check();
315 	}
316 	aCharKerningRB.SaveValue();
317 	aNoCompressionRB.SaveValue();
318 	aPunctCompressionRB.SaveValue();
319 	aPunctKanaCompressionRB.SaveValue();
320 
321 	aLanguageLB.SelectEntryPos(0);
322     //preselect the system language in the box - if available
323     if(USHRT_MAX == eLastUsedLanguageTypeForForbiddenCharacters)
324     {
325         eLastUsedLanguageTypeForForbiddenCharacters = SvxLocaleToLanguage(
326             Application::GetSettings().GetLocale() );
327         switch(eLastUsedLanguageTypeForForbiddenCharacters)
328         {
329             case  LANGUAGE_CHINESE            :
330             case  LANGUAGE_CHINESE_SINGAPORE  :
331                 eLastUsedLanguageTypeForForbiddenCharacters = LANGUAGE_CHINESE_SIMPLIFIED;
332             break;
333             case  LANGUAGE_CHINESE_HONGKONG   :
334             case  LANGUAGE_CHINESE_MACAU      :
335                 eLastUsedLanguageTypeForForbiddenCharacters = LANGUAGE_CHINESE_TRADITIONAL;
336             break;
337         }
338     }
339     aLanguageLB.SelectLanguage( eLastUsedLanguageTypeForForbiddenCharacters );
340     LanguageHdl(&aLanguageLB);
341 }
342 /* -----------------------------17.01.01 11:02--------------------------------
343 
344  ---------------------------------------------------------------------------*/
345 IMPL_LINK(SvxAsianLayoutPage, LanguageHdl, SvxLanguageBox*, EMPTYARG )
346 {
347 	//set current value
348 	Locale aLocale;
349 	LanguageType eSelectLanguage = aLanguageLB.GetSelectLanguage();
350 	SvxLanguageToLocale(aLocale, eSelectLanguage );
351 
352 	OUString sStart, sEnd;
353 	sal_Bool bAvail;
354 	if(pImpl->xForbidden.is())
355 	{
356 		bAvail = pImpl->hasForbiddenCharacters(eSelectLanguage);
357 		if(bAvail)
358 		{
359 			SvxForbiddenChars_Impl* pElement = pImpl->getForbiddenCharacters(eSelectLanguage);
360 			if(pElement->bRemoved || !pElement->pCharacters)
361 			{
362 				bAvail = sal_False;
363 			}
364 			else
365 			{
366 				sStart = pElement->pCharacters->beginLine;
367 				sEnd = pElement->pCharacters->endLine;
368 			}
369 		}
370 		else
371 		{
372 			try
373 			{
374 				bAvail = pImpl->xForbidden->hasForbiddenCharacters(aLocale);
375 				if(bAvail)
376 				{
377 					ForbiddenCharacters aForbidden = pImpl->xForbidden->getForbiddenCharacters( aLocale );
378 					sStart = aForbidden.beginLine;
379 					sEnd = aForbidden.endLine;
380 				}
381 			}
382 			catch(Exception&)
383 			{
384 				DBG_ERROR("exception in XForbiddenCharacters");
385 			}
386 		}
387 	}
388 	else
389 	{
390 		bAvail = pImpl->aConfig.GetStartEndChars( aLocale, sStart, sEnd );
391 	}
392 	if(!bAvail)
393 	{
394 		Reference< XMultiServiceFactory > xMSF = ::comphelper::getProcessServiceFactory();
395 		LocaleDataWrapper aWrap( xMSF, aLocale );
396 		ForbiddenCharacters aForbidden = aWrap.getForbiddenCharacters();
397 		sStart = aForbidden.beginLine;
398 		sEnd = aForbidden.endLine;
399 	}
400 	aStandardCB.Check(!bAvail);
401 	aStartED.Enable(bAvail);
402 	aEndED.Enable(bAvail);
403 	aStartFT.Enable(bAvail);
404 	aEndFT.Enable(bAvail);
405 	aStartED.SetText(sStart);
406 	aEndED.SetText(sEnd);
407 
408 	return 0;
409 }
410 /* -----------------------------17.01.01 11:02--------------------------------
411 
412  ---------------------------------------------------------------------------*/
413 IMPL_LINK(SvxAsianLayoutPage, ChangeStandardHdl, CheckBox*, pBox)
414 {
415 	sal_Bool bCheck = pBox->IsChecked();
416 	aStartED.Enable(!bCheck);
417 	aEndED.Enable(!bCheck);
418 	aStartFT.Enable(!bCheck);
419 	aEndFT.Enable(!bCheck);
420 
421 	ModifyHdl(&aStartED);
422 	return 0;
423 }
424 /* -----------------------------17.01.01 12:26--------------------------------
425 
426  ---------------------------------------------------------------------------*/
427 IMPL_LINK(SvxAsianLayoutPage, ModifyHdl, Edit*, pEdit)
428 {
429 	Locale aLocale;
430 	LanguageType eSelectLanguage = aLanguageLB.GetSelectLanguage();
431 	SvxLanguageToLocale(aLocale, eSelectLanguage );
432 	OUString sStart = aStartED.GetText();
433 	OUString sEnd = aEndED.GetText();
434 	sal_Bool bEnable = pEdit->IsEnabled();
435 	if(pImpl->xForbidden.is())
436 	{
437 		try
438 		{
439 			if(bEnable)
440 			{
441 				ForbiddenCharacters aSet;
442 				aSet.beginLine = sStart;
443 				aSet.endLine = sEnd;
444 //				pImpl->xForbidden->setForbiddenCharacters( aLocale, aSet );
445 				pImpl->addForbiddenCharacters(eSelectLanguage, &aSet);
446 			}
447 			else
448 				pImpl->addForbiddenCharacters(eSelectLanguage, 0);
449 //				pImpl->xForbidden->removeForbiddenCharacters( aLocale );
450 		}
451 		catch(Exception&)
452 		{
453 			DBG_ERROR("exception in XForbiddenCharacters");
454 		}
455 	}
456 	pImpl->aConfig.SetStartEndChars( aLocale, bEnable ? &sStart : 0, bEnable ? &sEnd : 0);
457 	return 0;
458 }
459 /*-- 07.09.2007 12:05:09---------------------------------------------------
460 
461   -----------------------------------------------------------------------*/
462 sal_uInt16* SvxAsianLayoutPage::GetRanges()
463 {
464     //no items are used
465     static sal_uInt16 pAsianLayoutRanges[] = { 0 };
466     return pAsianLayoutRanges;
467 }
468