xref: /trunk/main/sc/source/core/tool/docoptio.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_sc.hxx"
30 
31 
32 
33 #include <vcl/svapp.hxx>
34 #include <svl/zforlist.hxx>
35 
36 #include <com/sun/star/uno/Any.hxx>
37 #include <com/sun/star/uno/Sequence.hxx>
38 
39 #include "cfgids.hxx"
40 #include "docoptio.hxx"
41 #include "rechead.hxx"
42 #include "scresid.hxx"
43 #include "sc.hrc"
44 #include "miscuno.hxx"
45 
46 using namespace utl;
47 using namespace rtl;
48 using namespace com::sun::star::uno;
49 
50 //------------------------------------------------------------------------
51 
52 #define SC_VERSION ((sal_uInt16)251)
53 
54 TYPEINIT1(ScTpCalcItem, SfxPoolItem);
55 
56 //------------------------------------------------------------------------
57 
58 //!	these functions should be moved to some header file
59 inline long TwipsToHMM(long nTwips)	{ return (nTwips * 127 + 36) / 72; }
60 inline long HMMToTwips(long nHMM)	{ return (nHMM * 72 + 63) / 127; }
61 
62 inline long TwipsToEvenHMM(long nTwips)	{ return ( (nTwips * 127 + 72) / 144 ) * 2; }
63 
64 //------------------------------------------------------------------------
65 
66 sal_uInt16 lcl_GetDefaultTabDist()
67 {
68 	if ( ScOptionsUtil::IsMetricSystem() )
69 		return 709;					// 1,25 cm
70 	else
71 		return 720;					// 1/2"
72 }
73 
74 //========================================================================
75 //      ScDocOptions - Dokument-Optionen
76 //========================================================================
77 
78 ScDocOptions::ScDocOptions()
79 {
80 	ResetDocOptions();
81 }
82 
83 //------------------------------------------------------------------------
84 
85 ScDocOptions::ScDocOptions( const ScDocOptions& rCpy )
86 		:   fIterEps( rCpy.fIterEps ),
87 			nIterCount( rCpy.nIterCount ),
88 			nPrecStandardFormat( rCpy.nPrecStandardFormat ),
89 			nDay( rCpy.nDay ),
90 			nMonth( rCpy.nMonth ),
91 			nYear( rCpy.nYear ),
92 			nYear2000( rCpy.nYear2000 ),
93 			nTabDistance( rCpy.nTabDistance ),
94             bIsIgnoreCase( rCpy.bIsIgnoreCase ),
95 			bIsIter( rCpy.bIsIter ),
96 			bCalcAsShown( rCpy.bCalcAsShown ),
97 			bMatchWholeCell( rCpy.bMatchWholeCell ),
98 			bDoAutoSpell( rCpy.bDoAutoSpell ),
99             bLookUpColRowNames( rCpy.bLookUpColRowNames ),
100             bFormulaRegexEnabled( rCpy.bFormulaRegexEnabled )
101 {
102 }
103 
104 //------------------------------------------------------------------------
105 
106 ScDocOptions::~ScDocOptions()
107 {
108 }
109 
110 //------------------------------------------------------------------------
111 
112 void ScDocOptions::ResetDocOptions()
113 {
114 	bIsIgnoreCase		= sal_False;
115 	bIsIter				= sal_False;
116 	nIterCount			= 100;
117 	fIterEps			= 1.0E-3;
118     nPrecStandardFormat = SvNumberFormatter::UNLIMITED_PRECISION;
119 	nDay				= 30;
120 	nMonth				= 12;
121 	nYear				= 1899;
122 	nYear2000			= SvNumberFormatter::GetYear2000Default();
123 	nTabDistance 		= lcl_GetDefaultTabDist();
124 	bCalcAsShown		= sal_False;
125 	bMatchWholeCell		= sal_True;
126 	bDoAutoSpell		= sal_False;
127 	bLookUpColRowNames	= sal_True;
128     bFormulaRegexEnabled= sal_True;
129 }
130 
131 //========================================================================
132 //      ScTpCalcItem - Daten fuer die CalcOptions-TabPage
133 //========================================================================
134 
135 //UNUSED2008-05  ScTpCalcItem::ScTpCalcItem( sal_uInt16 nWhichP ) : SfxPoolItem( nWhichP )
136 //UNUSED2008-05  {
137 //UNUSED2008-05  }
138 
139 //------------------------------------------------------------------------
140 
141 ScTpCalcItem::ScTpCalcItem( sal_uInt16 nWhichP, const ScDocOptions& rOpt )
142     :   SfxPoolItem ( nWhichP ),
143 		theOptions	( rOpt )
144 {
145 }
146 
147 //------------------------------------------------------------------------
148 
149 ScTpCalcItem::ScTpCalcItem( const ScTpCalcItem& rItem )
150 	:   SfxPoolItem	( rItem ),
151 		theOptions	( rItem.theOptions )
152 {
153 }
154 
155 //------------------------------------------------------------------------
156 
157 __EXPORT ScTpCalcItem::~ScTpCalcItem()
158 {
159 }
160 
161 //------------------------------------------------------------------------
162 
163 String __EXPORT ScTpCalcItem::GetValueText() const
164 {
165 	return String::CreateFromAscii( RTL_CONSTASCII_STRINGPARAM("ScTpCalcItem") );
166 }
167 
168 //------------------------------------------------------------------------
169 
170 int __EXPORT ScTpCalcItem::operator==( const SfxPoolItem& rItem ) const
171 {
172 	DBG_ASSERT( SfxPoolItem::operator==( rItem ), "unequal Which or Type" );
173 
174 	const ScTpCalcItem& rPItem = (const ScTpCalcItem&)rItem;
175 
176 	return ( theOptions == rPItem.theOptions );
177 }
178 
179 //------------------------------------------------------------------------
180 
181 SfxPoolItem* __EXPORT ScTpCalcItem::Clone( SfxItemPool * ) const
182 {
183 	return new ScTpCalcItem( *this );
184 }
185 
186 //==================================================================
187 //	Config Item containing document options
188 //==================================================================
189 
190 #define CFGPATH_CALC		"Office.Calc/Calculate"
191 
192 #define SCCALCOPT_ITER_ITER			0
193 #define SCCALCOPT_ITER_STEPS		1
194 #define SCCALCOPT_ITER_MINCHG		2
195 #define SCCALCOPT_DATE_DAY			3
196 #define SCCALCOPT_DATE_MONTH		4
197 #define SCCALCOPT_DATE_YEAR			5
198 #define SCCALCOPT_DECIMALS			6
199 #define SCCALCOPT_CASESENSITIVE		7
200 #define SCCALCOPT_PRECISION			8
201 #define SCCALCOPT_SEARCHCRIT		9
202 #define SCCALCOPT_FINDLABEL			10
203 #define SCCALCOPT_REGEX             11
204 #define SCCALCOPT_COUNT             12
205 
206 #define CFGPATH_DOCLAYOUT	"Office.Calc/Layout/Other"
207 
208 #define SCDOCLAYOUTOPT_TABSTOP		0
209 #define SCDOCLAYOUTOPT_COUNT		1
210 
211 
212 Sequence<OUString> ScDocCfg::GetCalcPropertyNames()
213 {
214 	static const char* aPropNames[] =
215 	{
216 		"IterativeReference/Iteration",		// SCCALCOPT_ITER_ITER
217 		"IterativeReference/Steps",			// SCCALCOPT_ITER_STEPS
218 		"IterativeReference/MinimumChange",	// SCCALCOPT_ITER_MINCHG
219 		"Other/Date/DD",					// SCCALCOPT_DATE_DAY
220 		"Other/Date/MM",					// SCCALCOPT_DATE_MONTH
221 		"Other/Date/YY",					// SCCALCOPT_DATE_YEAR
222 		"Other/DecimalPlaces",				// SCCALCOPT_DECIMALS
223 		"Other/CaseSensitive",				// SCCALCOPT_CASESENSITIVE
224 		"Other/Precision",					// SCCALCOPT_PRECISION
225 		"Other/SearchCriteria",				// SCCALCOPT_SEARCHCRIT
226         "Other/FindLabel",                  // SCCALCOPT_FINDLABEL
227         "Other/RegularExpressions"          // SCCALCOPT_REGEX
228 	};
229 	Sequence<OUString> aNames(SCCALCOPT_COUNT);
230 	OUString* pNames = aNames.getArray();
231 	for(int i = 0; i < SCCALCOPT_COUNT; i++)
232 		pNames[i] = OUString::createFromAscii(aPropNames[i]);
233 
234 	return aNames;
235 }
236 
237 Sequence<OUString> ScDocCfg::GetLayoutPropertyNames()
238 {
239 	static const char* aPropNames[] =
240 	{
241 		"TabStop/NonMetric"			// SCDOCLAYOUTOPT_TABSTOP
242 	};
243 	Sequence<OUString> aNames(SCDOCLAYOUTOPT_COUNT);
244 	OUString* pNames = aNames.getArray();
245 	for(int i = 0; i < SCDOCLAYOUTOPT_COUNT; i++)
246 		pNames[i] = OUString::createFromAscii(aPropNames[i]);
247 
248 	//	adjust for metric system
249 	if (ScOptionsUtil::IsMetricSystem())
250 		pNames[SCDOCLAYOUTOPT_TABSTOP] = OUString::createFromAscii( "TabStop/Metric" );
251 
252 	return aNames;
253 }
254 
255 ScDocCfg::ScDocCfg() :
256 	aCalcItem( OUString::createFromAscii( CFGPATH_CALC ) ),
257 	aLayoutItem( OUString::createFromAscii( CFGPATH_DOCLAYOUT ) )
258 {
259 	sal_Int32 nIntVal = 0;
260 	double fDoubleVal = 0;
261 
262 	Sequence<OUString> aNames;
263 	Sequence<Any> aValues;
264 	const Any* pValues = NULL;
265 
266 	sal_uInt16 nDateDay, nDateMonth, nDateYear;
267 	GetDate( nDateDay, nDateMonth, nDateYear );
268 
269 	aNames = GetCalcPropertyNames();
270 	aValues = aCalcItem.GetProperties(aNames);
271 	aCalcItem.EnableNotification(aNames);
272 	pValues = aValues.getConstArray();
273 	DBG_ASSERT(aValues.getLength() == aNames.getLength(), "GetProperties failed");
274 	if(aValues.getLength() == aNames.getLength())
275 	{
276 		for(int nProp = 0; nProp < aNames.getLength(); nProp++)
277 		{
278 			DBG_ASSERT(pValues[nProp].hasValue(), "property value missing");
279 			if(pValues[nProp].hasValue())
280 			{
281 				switch(nProp)
282 				{
283 					case SCCALCOPT_ITER_ITER:
284 						SetIter( ScUnoHelpFunctions::GetBoolFromAny( pValues[nProp] ) );
285 						break;
286 					case SCCALCOPT_ITER_STEPS:
287 						if (pValues[nProp] >>= nIntVal) SetIterCount( (sal_uInt16) nIntVal );
288 						break;
289 					case SCCALCOPT_ITER_MINCHG:
290 						if (pValues[nProp] >>= fDoubleVal) SetIterEps( fDoubleVal );
291 						break;
292 					case SCCALCOPT_DATE_DAY:
293 						if (pValues[nProp] >>= nIntVal) nDateDay = (sal_uInt16) nIntVal;
294 						break;
295 					case SCCALCOPT_DATE_MONTH:
296 						if (pValues[nProp] >>= nIntVal) nDateMonth = (sal_uInt16) nIntVal;
297 						break;
298 					case SCCALCOPT_DATE_YEAR:
299 						if (pValues[nProp] >>= nIntVal) nDateYear = (sal_uInt16) nIntVal;
300 						break;
301 					case SCCALCOPT_DECIMALS:
302 						if (pValues[nProp] >>= nIntVal) SetStdPrecision( (sal_uInt16) nIntVal );
303 						break;
304 					case SCCALCOPT_CASESENSITIVE:
305 						// content is reversed
306 						SetIgnoreCase( !ScUnoHelpFunctions::GetBoolFromAny( pValues[nProp] ) );
307 						break;
308 					case SCCALCOPT_PRECISION:
309 						SetCalcAsShown( ScUnoHelpFunctions::GetBoolFromAny( pValues[nProp] ) );
310 						break;
311 					case SCCALCOPT_SEARCHCRIT:
312 						SetMatchWholeCell( ScUnoHelpFunctions::GetBoolFromAny( pValues[nProp] ) );
313 						break;
314 					case SCCALCOPT_FINDLABEL:
315 						SetLookUpColRowNames( ScUnoHelpFunctions::GetBoolFromAny( pValues[nProp] ) );
316 						break;
317                     case SCCALCOPT_REGEX :
318                         SetFormulaRegexEnabled( ScUnoHelpFunctions::GetBoolFromAny( pValues[nProp] ) );
319                         break;
320 				}
321 			}
322 		}
323 	}
324 	aCalcItem.SetCommitLink( LINK( this, ScDocCfg, CalcCommitHdl ) );
325 
326 	SetDate( nDateDay, nDateMonth, nDateYear );
327 
328 	aNames = GetLayoutPropertyNames();
329 	aValues = aLayoutItem.GetProperties(aNames);
330 	aLayoutItem.EnableNotification(aNames);
331 	pValues = aValues.getConstArray();
332 	DBG_ASSERT(aValues.getLength() == aNames.getLength(), "GetProperties failed");
333 	if(aValues.getLength() == aNames.getLength())
334 	{
335 		for(int nProp = 0; nProp < aNames.getLength(); nProp++)
336 		{
337 			DBG_ASSERT(pValues[nProp].hasValue(), "property value missing");
338 			if(pValues[nProp].hasValue())
339 			{
340 				switch(nProp)
341 				{
342 					case SCDOCLAYOUTOPT_TABSTOP:
343 						// TabDistance in ScDocOptions is in twips
344 						if (pValues[nProp] >>= nIntVal)
345 							SetTabDistance( (sal_uInt16) HMMToTwips( nIntVal ) );
346 						break;
347 				}
348 			}
349 		}
350 	}
351 	aLayoutItem.SetCommitLink( LINK( this, ScDocCfg, LayoutCommitHdl ) );
352 }
353 
354 IMPL_LINK( ScDocCfg, CalcCommitHdl, void *, EMPTYARG )
355 {
356 	Sequence<OUString> aNames = GetCalcPropertyNames();
357 	Sequence<Any> aValues(aNames.getLength());
358 	Any* pValues = aValues.getArray();
359 
360 	sal_uInt16 nDateDay, nDateMonth, nDateYear;
361 	GetDate( nDateDay, nDateMonth, nDateYear );
362 
363 	for(int nProp = 0; nProp < aNames.getLength(); nProp++)
364 	{
365 		switch(nProp)
366 		{
367 			case SCCALCOPT_ITER_ITER:
368 				ScUnoHelpFunctions::SetBoolInAny( pValues[nProp], IsIter() );
369 				break;
370 			case SCCALCOPT_ITER_STEPS:
371 				pValues[nProp] <<= (sal_Int32) GetIterCount();
372 				break;
373 			case SCCALCOPT_ITER_MINCHG:
374 				pValues[nProp] <<= (double) GetIterEps();
375 				break;
376 			case SCCALCOPT_DATE_DAY:
377 				pValues[nProp] <<= (sal_Int32) nDateDay;
378 				break;
379 			case SCCALCOPT_DATE_MONTH:
380 				pValues[nProp] <<= (sal_Int32) nDateMonth;
381 				break;
382 			case SCCALCOPT_DATE_YEAR:
383 				pValues[nProp] <<= (sal_Int32) nDateYear;
384 				break;
385 			case SCCALCOPT_DECIMALS:
386 				pValues[nProp] <<= (sal_Int32) GetStdPrecision();
387 				break;
388 			case SCCALCOPT_CASESENSITIVE:
389 				// content is reversed
390 				ScUnoHelpFunctions::SetBoolInAny( pValues[nProp], !IsIgnoreCase() );
391 				break;
392 			case SCCALCOPT_PRECISION:
393 				ScUnoHelpFunctions::SetBoolInAny( pValues[nProp], IsCalcAsShown() );
394 				break;
395 			case SCCALCOPT_SEARCHCRIT:
396 				ScUnoHelpFunctions::SetBoolInAny( pValues[nProp], IsMatchWholeCell() );
397 				break;
398 			case SCCALCOPT_FINDLABEL:
399 				ScUnoHelpFunctions::SetBoolInAny( pValues[nProp], IsLookUpColRowNames() );
400 				break;
401             case SCCALCOPT_REGEX :
402                 ScUnoHelpFunctions::SetBoolInAny( pValues[nProp], IsFormulaRegexEnabled() );
403 		}
404 	}
405 	aCalcItem.PutProperties(aNames, aValues);
406 
407 	return 0;
408 }
409 
410 IMPL_LINK( ScDocCfg, LayoutCommitHdl, void *, EMPTYARG )
411 {
412 	Sequence<OUString> aNames = GetLayoutPropertyNames();
413 	Sequence<Any> aValues(aNames.getLength());
414 	Any* pValues = aValues.getArray();
415 
416 	for(int nProp = 0; nProp < aNames.getLength(); nProp++)
417 	{
418 		switch(nProp)
419 		{
420 			case SCDOCLAYOUTOPT_TABSTOP:
421 				//	TabDistance in ScDocOptions is in twips
422 				//	use only even numbers, so defaults don't get changed
423 				//	by modifying other settings in the same config item
424 				pValues[nProp] <<= (sal_Int32) TwipsToEvenHMM( GetTabDistance() );
425 				break;
426 		}
427 	}
428 	aLayoutItem.PutProperties(aNames, aValues);
429 
430 	return 0;
431 }
432 
433 
434 void ScDocCfg::SetOptions( const ScDocOptions& rNew )
435 {
436 	*(ScDocOptions*)this = rNew;
437 
438 	aCalcItem.SetModified();
439 	aLayoutItem.SetModified();
440 }
441 
442 
443