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