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