xref: /aoo42x/main/comphelper/source/misc/numbers.cxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_comphelper.hxx"
30*cdf0e10cSrcweir #include <comphelper/numbers.hxx>
31*cdf0e10cSrcweir #include <osl/diagnose.h>
32*cdf0e10cSrcweir #include <com/sun/star/util/NumberFormat.hpp>
33*cdf0e10cSrcweir #include <com/sun/star/util/XNumberFormatTypes.hpp>
34*cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
35*cdf0e10cSrcweir #include <com/sun/star/lang/Locale.hpp>
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir //.........................................................................
38*cdf0e10cSrcweir namespace comphelper
39*cdf0e10cSrcweir {
40*cdf0e10cSrcweir //.........................................................................
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir namespace starbeans	= ::com::sun::star::beans;
43*cdf0e10cSrcweir namespace starlang	= ::com::sun::star::lang;
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir //------------------------------------------------------------------------------
46*cdf0e10cSrcweir sal_Int16 getNumberFormatType(const staruno::Reference<starutil::XNumberFormats>& xFormats, sal_Int32 nKey)
47*cdf0e10cSrcweir {
48*cdf0e10cSrcweir 	sal_Int16 nReturn(starutil::NumberFormat::UNDEFINED);
49*cdf0e10cSrcweir 	if (xFormats.is())
50*cdf0e10cSrcweir 	{
51*cdf0e10cSrcweir 		try
52*cdf0e10cSrcweir 		{
53*cdf0e10cSrcweir 			staruno::Reference<starbeans::XPropertySet> xFormat(xFormats->getByKey(nKey));
54*cdf0e10cSrcweir 			if (xFormat.is())
55*cdf0e10cSrcweir 				xFormat->getPropertyValue(rtl::OUString::createFromAscii("Type")) >>= nReturn;
56*cdf0e10cSrcweir 		}
57*cdf0e10cSrcweir 		catch(...)
58*cdf0e10cSrcweir 		{
59*cdf0e10cSrcweir 			OSL_TRACE("getNumberFormatType : invalid key! (maybe created with another formatter ?)");
60*cdf0e10cSrcweir 		}
61*cdf0e10cSrcweir 	}
62*cdf0e10cSrcweir 	return nReturn;
63*cdf0e10cSrcweir }
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir //------------------------------------------------------------------------------
66*cdf0e10cSrcweir sal_Int16 getNumberFormatType(const staruno::Reference<starutil::XNumberFormatter>& xFormatter, sal_Int32 nKey)
67*cdf0e10cSrcweir {
68*cdf0e10cSrcweir 	OSL_ENSURE(xFormatter.is(), "getNumberFormatType : the formatter isn't valid !");
69*cdf0e10cSrcweir 	staruno::Reference<starutil::XNumberFormatsSupplier> xSupplier( xFormatter->getNumberFormatsSupplier());
70*cdf0e10cSrcweir 	OSL_ENSURE(xSupplier.is(), "getNumberFormatType : the formatter doesn't implement a supplier !");
71*cdf0e10cSrcweir 	staruno::Reference<starutil::XNumberFormats> xFormats( xSupplier->getNumberFormats());
72*cdf0e10cSrcweir 	return getNumberFormatType(xFormats, nKey);
73*cdf0e10cSrcweir }
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir //------------------------------------------------------------------------------
76*cdf0e10cSrcweir staruno::Any getNumberFormatDecimals(const staruno::Reference<starutil::XNumberFormats>& xFormats, sal_Int32 nKey)
77*cdf0e10cSrcweir {
78*cdf0e10cSrcweir 	if (xFormats.is())
79*cdf0e10cSrcweir 	{
80*cdf0e10cSrcweir 		try
81*cdf0e10cSrcweir 		{
82*cdf0e10cSrcweir 			staruno::Reference<starbeans::XPropertySet> xFormat( xFormats->getByKey(nKey));
83*cdf0e10cSrcweir 			if (xFormat.is())
84*cdf0e10cSrcweir 			{
85*cdf0e10cSrcweir 				static ::rtl::OUString PROPERTY_DECIMALS = ::rtl::OUString::createFromAscii("Decimals");
86*cdf0e10cSrcweir 				return xFormat->getPropertyValue(PROPERTY_DECIMALS);
87*cdf0e10cSrcweir 			}
88*cdf0e10cSrcweir 		}
89*cdf0e10cSrcweir 		catch(...)
90*cdf0e10cSrcweir 		{
91*cdf0e10cSrcweir 			OSL_TRACE("getNumberFormatDecimals : invalid key! (may be created with another formatter ?)");
92*cdf0e10cSrcweir 		}
93*cdf0e10cSrcweir 	}
94*cdf0e10cSrcweir 	return staruno::makeAny((sal_Int16)0);
95*cdf0e10cSrcweir }
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir //------------------------------------------------------------------------------
99*cdf0e10cSrcweir sal_Int32 getStandardFormat(
100*cdf0e10cSrcweir 		const staruno::Reference<starutil::XNumberFormatter>& xFormatter,
101*cdf0e10cSrcweir 		sal_Int16 nType,
102*cdf0e10cSrcweir 		const starlang::Locale& _rLocale)
103*cdf0e10cSrcweir {
104*cdf0e10cSrcweir 	staruno::Reference<starutil::XNumberFormatsSupplier> xSupplier( xFormatter.is() ? xFormatter->getNumberFormatsSupplier() : staruno::Reference<starutil::XNumberFormatsSupplier>(NULL));
105*cdf0e10cSrcweir 	staruno::Reference<starutil::XNumberFormats> xFormats( xSupplier.is() ? xSupplier->getNumberFormats() : staruno::Reference<starutil::XNumberFormats>(NULL));
106*cdf0e10cSrcweir 	staruno::Reference<starutil::XNumberFormatTypes> xTypes(xFormats, staruno::UNO_QUERY);
107*cdf0e10cSrcweir 	OSL_ENSURE(xTypes.is(), "getStandardFormat : no format types !");
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir 	return xTypes.is() ? xTypes->getStandardFormat(nType, _rLocale) : 0;
110*cdf0e10cSrcweir }
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir //------------------------------------------------------------------------------
113*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
114*cdf0e10cSrcweir using namespace ::com::sun::star::util;
115*cdf0e10cSrcweir using namespace ::com::sun::star::beans;
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir //------------------------------------------------------------------------------
118*cdf0e10cSrcweir Any getNumberFormatProperty( const Reference< XNumberFormatter >& _rxFormatter, sal_Int32 _nKey, const rtl::OUString& _rPropertyName )
119*cdf0e10cSrcweir {
120*cdf0e10cSrcweir 	Any aReturn;
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir 	OSL_ENSURE( _rxFormatter.is() && _rPropertyName.getLength(), "getNumberFormatProperty: invalid arguments!" );
123*cdf0e10cSrcweir 	try
124*cdf0e10cSrcweir 	{
125*cdf0e10cSrcweir 		Reference< XNumberFormatsSupplier > xSupplier;
126*cdf0e10cSrcweir 		Reference< XNumberFormats > xFormats;
127*cdf0e10cSrcweir 		Reference< XPropertySet > xFormatProperties;
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir 		if ( _rxFormatter.is() )
130*cdf0e10cSrcweir 			xSupplier = _rxFormatter->getNumberFormatsSupplier();
131*cdf0e10cSrcweir 		if ( xSupplier.is() )
132*cdf0e10cSrcweir 			xFormats = xSupplier->getNumberFormats();
133*cdf0e10cSrcweir 		if ( xFormats.is() )
134*cdf0e10cSrcweir 			xFormatProperties = xFormats->getByKey( _nKey );
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir 		if ( xFormatProperties.is() )
137*cdf0e10cSrcweir 			aReturn = xFormatProperties->getPropertyValue( _rPropertyName );
138*cdf0e10cSrcweir 	}
139*cdf0e10cSrcweir 	catch( const Exception& )
140*cdf0e10cSrcweir 	{
141*cdf0e10cSrcweir 		OSL_ENSURE( sal_False, "::getNumberFormatProperty: caught an exception (did you create the key with another formatter?)!" );
142*cdf0e10cSrcweir 	}
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir 	return aReturn;
145*cdf0e10cSrcweir }
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir //.........................................................................
148*cdf0e10cSrcweir }	// namespace comphelper
149*cdf0e10cSrcweir //.........................................................................
150*cdf0e10cSrcweir 
151