xref: /trunk/main/forms/source/component/Currency.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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_forms.hxx"
30*cdf0e10cSrcweir #include "Currency.hxx"
31*cdf0e10cSrcweir #include <tools/debug.hxx>
32*cdf0e10cSrcweir #include <unotools/localedatawrapper.hxx>
33*cdf0e10cSrcweir #include <vcl/svapp.hxx>
34*cdf0e10cSrcweir #include <unotools/syslocale.hxx>
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir //.........................................................................
37*cdf0e10cSrcweir namespace frm
38*cdf0e10cSrcweir {
39*cdf0e10cSrcweir //.........................................................................
40*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
41*cdf0e10cSrcweir using namespace ::com::sun::star::sdb;
42*cdf0e10cSrcweir using namespace ::com::sun::star::sdbc;
43*cdf0e10cSrcweir using namespace ::com::sun::star::sdbcx;
44*cdf0e10cSrcweir using namespace ::com::sun::star::beans;
45*cdf0e10cSrcweir using namespace ::com::sun::star::container;
46*cdf0e10cSrcweir using namespace ::com::sun::star::form;
47*cdf0e10cSrcweir using namespace ::com::sun::star::awt;
48*cdf0e10cSrcweir using namespace ::com::sun::star::io;
49*cdf0e10cSrcweir using namespace ::com::sun::star::lang;
50*cdf0e10cSrcweir using namespace ::com::sun::star::util;
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir //==================================================================
53*cdf0e10cSrcweir // OCurrencyControl
54*cdf0e10cSrcweir //==================================================================
55*cdf0e10cSrcweir //------------------------------------------------------------------
56*cdf0e10cSrcweir OCurrencyControl::OCurrencyControl(const Reference<XMultiServiceFactory>& _rxFactory)
57*cdf0e10cSrcweir     :OBoundControl(_rxFactory, VCL_CONTROL_CURRENCYFIELD)
58*cdf0e10cSrcweir {
59*cdf0e10cSrcweir }
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir //------------------------------------------------------------------
62*cdf0e10cSrcweir InterfaceRef SAL_CALL OCurrencyControl_CreateInstance(const Reference<XMultiServiceFactory>& _rxFactory)
63*cdf0e10cSrcweir {
64*cdf0e10cSrcweir     return *(new OCurrencyControl(_rxFactory));
65*cdf0e10cSrcweir }
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir //------------------------------------------------------------------------------
68*cdf0e10cSrcweir Sequence<Type> OCurrencyControl::_getTypes()
69*cdf0e10cSrcweir {
70*cdf0e10cSrcweir     return OBoundControl::_getTypes();
71*cdf0e10cSrcweir }
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir //------------------------------------------------------------------------------
74*cdf0e10cSrcweir StringSequence SAL_CALL OCurrencyControl::getSupportedServiceNames() throw()
75*cdf0e10cSrcweir {
76*cdf0e10cSrcweir     StringSequence aSupported = OBoundControl::getSupportedServiceNames();
77*cdf0e10cSrcweir     aSupported.realloc(aSupported.getLength() + 1);
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir     ::rtl::OUString*pArray = aSupported.getArray();
80*cdf0e10cSrcweir     pArray[aSupported.getLength()-1] = FRM_SUN_CONTROL_CURRENCYFIELD;
81*cdf0e10cSrcweir     return aSupported;
82*cdf0e10cSrcweir }
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir //==================================================================
85*cdf0e10cSrcweir // OCurrencyModel
86*cdf0e10cSrcweir //==================================================================
87*cdf0e10cSrcweir //------------------------------------------------------------------
88*cdf0e10cSrcweir InterfaceRef SAL_CALL OCurrencyModel_CreateInstance(const Reference<XMultiServiceFactory>& _rxFactory)
89*cdf0e10cSrcweir {
90*cdf0e10cSrcweir     return *(new OCurrencyModel(_rxFactory));
91*cdf0e10cSrcweir }
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir //------------------------------------------------------------------------------
94*cdf0e10cSrcweir Sequence<Type> OCurrencyModel::_getTypes()
95*cdf0e10cSrcweir {
96*cdf0e10cSrcweir     return OEditBaseModel::_getTypes();
97*cdf0e10cSrcweir }
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir //------------------------------------------------------------------
100*cdf0e10cSrcweir void OCurrencyModel::implConstruct()
101*cdf0e10cSrcweir {
102*cdf0e10cSrcweir     if (m_xAggregateSet.is())
103*cdf0e10cSrcweir     {
104*cdf0e10cSrcweir         try
105*cdf0e10cSrcweir         {
106*cdf0e10cSrcweir             // get the system international informations
107*cdf0e10cSrcweir             const SvtSysLocale aSysLocale;
108*cdf0e10cSrcweir             const LocaleDataWrapper& aLocaleInfo = aSysLocale.GetLocaleData();
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir             ::rtl::OUString sCurrencySymbol;
111*cdf0e10cSrcweir             sal_Bool bPrependCurrencySymbol;
112*cdf0e10cSrcweir             switch ( aLocaleInfo.getCurrPositiveFormat() )
113*cdf0e10cSrcweir             {
114*cdf0e10cSrcweir                 case 0: // $1
115*cdf0e10cSrcweir                     sCurrencySymbol = String(aLocaleInfo.getCurrSymbol());
116*cdf0e10cSrcweir                     bPrependCurrencySymbol = sal_True;
117*cdf0e10cSrcweir                     break;
118*cdf0e10cSrcweir                 case 1: // 1$
119*cdf0e10cSrcweir                     sCurrencySymbol = String(aLocaleInfo.getCurrSymbol());
120*cdf0e10cSrcweir                     bPrependCurrencySymbol = sal_False;
121*cdf0e10cSrcweir                     break;
122*cdf0e10cSrcweir                 case 2: // $ 1
123*cdf0e10cSrcweir                     sCurrencySymbol = ::rtl::OUString(String(aLocaleInfo.getCurrSymbol())) + ::rtl::OUString::createFromAscii(" ");
124*cdf0e10cSrcweir                     bPrependCurrencySymbol = sal_True;
125*cdf0e10cSrcweir                     break;
126*cdf0e10cSrcweir                 case 3: // 1 $
127*cdf0e10cSrcweir                     sCurrencySymbol = ::rtl::OUString::createFromAscii(" ") + ::rtl::OUString(String(aLocaleInfo.getCurrSymbol()));
128*cdf0e10cSrcweir                     bPrependCurrencySymbol = sal_False;
129*cdf0e10cSrcweir                     break;
130*cdf0e10cSrcweir             }
131*cdf0e10cSrcweir             if (sCurrencySymbol.getLength())
132*cdf0e10cSrcweir             {
133*cdf0e10cSrcweir                 m_xAggregateSet->setPropertyValue(PROPERTY_CURRENCYSYMBOL, makeAny(sCurrencySymbol));
134*cdf0e10cSrcweir                 m_xAggregateSet->setPropertyValue(PROPERTY_CURRSYM_POSITION, makeAny(bPrependCurrencySymbol));
135*cdf0e10cSrcweir             }
136*cdf0e10cSrcweir         }
137*cdf0e10cSrcweir         catch(Exception&)
138*cdf0e10cSrcweir         {
139*cdf0e10cSrcweir             DBG_ERROR( "OCurrencyModel::implConstruct: caught an exception while initializing the aggregate!" );
140*cdf0e10cSrcweir         }
141*cdf0e10cSrcweir     }
142*cdf0e10cSrcweir }
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir //------------------------------------------------------------------
145*cdf0e10cSrcweir DBG_NAME( OCurrencyModel )
146*cdf0e10cSrcweir //------------------------------------------------------------------
147*cdf0e10cSrcweir OCurrencyModel::OCurrencyModel(const Reference<XMultiServiceFactory>& _rxFactory)
148*cdf0e10cSrcweir     :OEditBaseModel( _rxFactory, VCL_CONTROLMODEL_CURRENCYFIELD, FRM_SUN_CONTROL_CURRENCYFIELD, sal_False, sal_True )
149*cdf0e10cSrcweir                                     // use the old control name for compytibility reasons
150*cdf0e10cSrcweir {
151*cdf0e10cSrcweir     DBG_CTOR( OCurrencyModel, NULL );
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir     m_nClassId = FormComponentType::CURRENCYFIELD;
154*cdf0e10cSrcweir     initValueProperty( PROPERTY_VALUE, PROPERTY_ID_VALUE );
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir     implConstruct();
157*cdf0e10cSrcweir }
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir //------------------------------------------------------------------
160*cdf0e10cSrcweir OCurrencyModel::OCurrencyModel( const OCurrencyModel* _pOriginal, const Reference<XMultiServiceFactory>& _rxFactory )
161*cdf0e10cSrcweir     :OEditBaseModel( _pOriginal, _rxFactory )
162*cdf0e10cSrcweir {
163*cdf0e10cSrcweir     DBG_CTOR( OCurrencyModel, NULL );
164*cdf0e10cSrcweir     implConstruct();
165*cdf0e10cSrcweir }
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir //------------------------------------------------------------------
168*cdf0e10cSrcweir OCurrencyModel::~OCurrencyModel()
169*cdf0e10cSrcweir {
170*cdf0e10cSrcweir     DBG_DTOR( OCurrencyModel, NULL );
171*cdf0e10cSrcweir }
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir // XCloneable
174*cdf0e10cSrcweir //------------------------------------------------------------------------------
175*cdf0e10cSrcweir IMPLEMENT_DEFAULT_CLONING( OCurrencyModel )
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir // XServiceInfo
178*cdf0e10cSrcweir //------------------------------------------------------------------------------
179*cdf0e10cSrcweir StringSequence SAL_CALL OCurrencyModel::getSupportedServiceNames() throw()
180*cdf0e10cSrcweir {
181*cdf0e10cSrcweir     StringSequence aSupported = OBoundControlModel::getSupportedServiceNames();
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir     sal_Int32 nOldLen = aSupported.getLength();
184*cdf0e10cSrcweir     aSupported.realloc( nOldLen + 4 );
185*cdf0e10cSrcweir     ::rtl::OUString* pStoreTo = aSupported.getArray() + nOldLen;
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir     *pStoreTo++ = DATA_AWARE_CONTROL_MODEL;
188*cdf0e10cSrcweir     *pStoreTo++ = VALIDATABLE_CONTROL_MODEL;
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir     *pStoreTo++ = FRM_SUN_COMPONENT_CURRENCYFIELD;
191*cdf0e10cSrcweir     *pStoreTo++ = FRM_SUN_COMPONENT_DATABASE_CURRENCYFIELD;
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir     return aSupported;
194*cdf0e10cSrcweir }
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir //------------------------------------------------------------------------------
197*cdf0e10cSrcweir void OCurrencyModel::describeFixedProperties( Sequence< Property >& _rProps ) const
198*cdf0e10cSrcweir {
199*cdf0e10cSrcweir     BEGIN_DESCRIBE_PROPERTIES( 2, OEditBaseModel )
200*cdf0e10cSrcweir         // Value auf transient setzen
201*cdf0e10cSrcweir //      ModifyPropertyAttributes(_rAggregateProps, PROPERTY_VALUE, PropertyAttribute::TRANSIENT, 0);
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir         DECL_PROP3(DEFAULT_VALUE,       double,             BOUND, MAYBEDEFAULT, MAYBEVOID);
204*cdf0e10cSrcweir         DECL_PROP1(TABINDEX,        sal_Int16,              BOUND);
205*cdf0e10cSrcweir     END_DESCRIBE_PROPERTIES();
206*cdf0e10cSrcweir }
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir //------------------------------------------------------------------------------
209*cdf0e10cSrcweir ::rtl::OUString SAL_CALL OCurrencyModel::getServiceName() throw ( ::com::sun::star::uno::RuntimeException)
210*cdf0e10cSrcweir {
211*cdf0e10cSrcweir     return FRM_COMPONENT_CURRENCYFIELD; // old (non-sun) name for compatibility !
212*cdf0e10cSrcweir }
213*cdf0e10cSrcweir 
214*cdf0e10cSrcweir //------------------------------------------------------------------------------
215*cdf0e10cSrcweir sal_Bool OCurrencyModel::commitControlValueToDbColumn( bool /*_bPostReset*/ )
216*cdf0e10cSrcweir {
217*cdf0e10cSrcweir     Any aControlValue( m_xAggregateFastSet->getFastPropertyValue( getValuePropertyAggHandle() ) );
218*cdf0e10cSrcweir     if ( !compare( aControlValue, m_aSaveValue ) )
219*cdf0e10cSrcweir     {
220*cdf0e10cSrcweir         if ( aControlValue.getValueType().getTypeClass() == TypeClass_VOID )
221*cdf0e10cSrcweir             m_xColumnUpdate->updateNull();
222*cdf0e10cSrcweir         else
223*cdf0e10cSrcweir         {
224*cdf0e10cSrcweir             try
225*cdf0e10cSrcweir             {
226*cdf0e10cSrcweir                 m_xColumnUpdate->updateDouble( getDouble( aControlValue ) );
227*cdf0e10cSrcweir             }
228*cdf0e10cSrcweir             catch(Exception&)
229*cdf0e10cSrcweir             {
230*cdf0e10cSrcweir                 return sal_False;
231*cdf0e10cSrcweir             }
232*cdf0e10cSrcweir         }
233*cdf0e10cSrcweir         m_aSaveValue = aControlValue;
234*cdf0e10cSrcweir     }
235*cdf0e10cSrcweir     return sal_True;
236*cdf0e10cSrcweir }
237*cdf0e10cSrcweir 
238*cdf0e10cSrcweir //------------------------------------------------------------------------------
239*cdf0e10cSrcweir Any OCurrencyModel::translateDbColumnToControlValue()
240*cdf0e10cSrcweir {
241*cdf0e10cSrcweir     m_aSaveValue <<= m_xColumn->getDouble();
242*cdf0e10cSrcweir     if ( m_xColumn->wasNull() )
243*cdf0e10cSrcweir         m_aSaveValue.clear();
244*cdf0e10cSrcweir     return m_aSaveValue;
245*cdf0e10cSrcweir }
246*cdf0e10cSrcweir 
247*cdf0e10cSrcweir // XReset
248*cdf0e10cSrcweir //------------------------------------------------------------------------------
249*cdf0e10cSrcweir Any OCurrencyModel::getDefaultForReset() const
250*cdf0e10cSrcweir {
251*cdf0e10cSrcweir     Any aValue;
252*cdf0e10cSrcweir     if ( m_aDefault.getValueType().getTypeClass() == TypeClass_DOUBLE )
253*cdf0e10cSrcweir         aValue = m_aDefault;
254*cdf0e10cSrcweir 
255*cdf0e10cSrcweir     return aValue;
256*cdf0e10cSrcweir }
257*cdf0e10cSrcweir 
258*cdf0e10cSrcweir //------------------------------------------------------------------------------
259*cdf0e10cSrcweir void OCurrencyModel::resetNoBroadcast()
260*cdf0e10cSrcweir {
261*cdf0e10cSrcweir     OEditBaseModel::resetNoBroadcast();
262*cdf0e10cSrcweir     m_aSaveValue.clear();
263*cdf0e10cSrcweir }
264*cdf0e10cSrcweir 
265*cdf0e10cSrcweir //.........................................................................
266*cdf0e10cSrcweir }   // namespace frm
267*cdf0e10cSrcweir //.........................................................................
268*cdf0e10cSrcweir 
269