Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / comphelper / source / misc / numbers.cxx
blob8ea7e264f010b0be1a044f65fd9e024629dd5624
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 <comphelper/numbers.hxx>
21 #include <osl/diagnose.h>
22 #include <com/sun/star/util/NumberFormat.hpp>
23 #include <com/sun/star/util/XNumberFormatTypes.hpp>
24 #include <com/sun/star/beans/XPropertySet.hpp>
25 #include <com/sun/star/lang/Locale.hpp>
27 //.........................................................................
28 namespace comphelper
30 //.........................................................................
32 namespace starbeans = ::com::sun::star::beans;
33 namespace starlang = ::com::sun::star::lang;
35 //------------------------------------------------------------------------------
36 sal_Int16 getNumberFormatType(const staruno::Reference<starutil::XNumberFormats>& xFormats, sal_Int32 nKey)
38 sal_Int16 nReturn(starutil::NumberFormat::UNDEFINED);
39 if (xFormats.is())
41 try
43 staruno::Reference<starbeans::XPropertySet> xFormat(xFormats->getByKey(nKey));
44 if (xFormat.is())
45 xFormat->getPropertyValue(rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Type" ))) >>= nReturn;
47 catch(...)
49 OSL_TRACE("getNumberFormatType : invalid key! (maybe created with another formatter ?)");
52 return nReturn;
55 //------------------------------------------------------------------------------
56 sal_Int16 getNumberFormatType(const staruno::Reference<starutil::XNumberFormatter>& xFormatter, sal_Int32 nKey)
58 OSL_ENSURE(xFormatter.is(), "getNumberFormatType : the formatter isn't valid !");
59 staruno::Reference<starutil::XNumberFormatsSupplier> xSupplier( xFormatter->getNumberFormatsSupplier());
60 OSL_ENSURE(xSupplier.is(), "getNumberFormatType : the formatter doesn't implement a supplier !");
61 staruno::Reference<starutil::XNumberFormats> xFormats( xSupplier->getNumberFormats());
62 return getNumberFormatType(xFormats, nKey);
65 //------------------------------------------------------------------------------
66 staruno::Any getNumberFormatDecimals(const staruno::Reference<starutil::XNumberFormats>& xFormats, sal_Int32 nKey)
68 if (xFormats.is())
70 try
72 staruno::Reference<starbeans::XPropertySet> xFormat( xFormats->getByKey(nKey));
73 if (xFormat.is())
75 static ::rtl::OUString PROPERTY_DECIMALS( RTL_CONSTASCII_USTRINGPARAM( "Decimals" ));
76 return xFormat->getPropertyValue(PROPERTY_DECIMALS);
79 catch(...)
81 OSL_TRACE("getNumberFormatDecimals : invalid key! (may be created with another formatter ?)");
84 return staruno::makeAny((sal_Int16)0);
88 //------------------------------------------------------------------------------
89 sal_Int32 getStandardFormat(
90 const staruno::Reference<starutil::XNumberFormatter>& xFormatter,
91 sal_Int16 nType,
92 const starlang::Locale& _rLocale)
94 staruno::Reference<starutil::XNumberFormatsSupplier> xSupplier( xFormatter.is() ? xFormatter->getNumberFormatsSupplier() : staruno::Reference<starutil::XNumberFormatsSupplier>(NULL));
95 staruno::Reference<starutil::XNumberFormats> xFormats( xSupplier.is() ? xSupplier->getNumberFormats() : staruno::Reference<starutil::XNumberFormats>(NULL));
96 staruno::Reference<starutil::XNumberFormatTypes> xTypes(xFormats, staruno::UNO_QUERY);
97 OSL_ENSURE(xTypes.is(), "getStandardFormat : no format types !");
99 return xTypes.is() ? xTypes->getStandardFormat(nType, _rLocale) : 0;
102 //------------------------------------------------------------------------------
103 using namespace ::com::sun::star::uno;
104 using namespace ::com::sun::star::util;
105 using namespace ::com::sun::star::beans;
107 //------------------------------------------------------------------------------
108 Any getNumberFormatProperty( const Reference< XNumberFormatter >& _rxFormatter, sal_Int32 _nKey, const rtl::OUString& _rPropertyName )
110 Any aReturn;
112 OSL_ENSURE( _rxFormatter.is() && !_rPropertyName.isEmpty(), "getNumberFormatProperty: invalid arguments!" );
115 Reference< XNumberFormatsSupplier > xSupplier;
116 Reference< XNumberFormats > xFormats;
117 Reference< XPropertySet > xFormatProperties;
119 if ( _rxFormatter.is() )
120 xSupplier = _rxFormatter->getNumberFormatsSupplier();
121 if ( xSupplier.is() )
122 xFormats = xSupplier->getNumberFormats();
123 if ( xFormats.is() )
124 xFormatProperties = xFormats->getByKey( _nKey );
126 if ( xFormatProperties.is() )
127 aReturn = xFormatProperties->getPropertyValue( _rPropertyName );
129 catch( const Exception& )
131 OSL_FAIL( "::getNumberFormatProperty: caught an exception (did you create the key with another formatter?)!" );
134 return aReturn;
137 //.........................................................................
138 } // namespace comphelper
139 //.........................................................................
141 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */