xref: /AOO41X/main/forms/source/component/CheckBox.cxx (revision 24acc54625a85f778a4f966495e8f4cd9d7b247c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_forms.hxx"
26 #include "CheckBox.hxx"
27 #include "property.hxx"
28 #ifndef _FRM_PROPERTY_HRC_
29 #include "property.hrc"
30 #endif
31 #include "services.hxx"
32 #include <tools/debug.hxx>
33 #include <comphelper/basicio.hxx>
34 
35 //.........................................................................
36 namespace frm
37 {
38 using namespace ::com::sun::star::uno;
39 using namespace ::com::sun::star::sdb;
40 using namespace ::com::sun::star::sdbc;
41 using namespace ::com::sun::star::sdbcx;
42 using namespace ::com::sun::star::beans;
43 using namespace ::com::sun::star::container;
44 using namespace ::com::sun::star::form;
45 using namespace ::com::sun::star::awt;
46 using namespace ::com::sun::star::io;
47 using namespace ::com::sun::star::lang;
48 using namespace ::com::sun::star::util;
49 using namespace ::com::sun::star::form::binding;
50 
51 //==================================================================
52 //= OCheckBoxControl
53 //==================================================================
54 
55 //------------------------------------------------------------------
OCheckBoxControl(const Reference<XMultiServiceFactory> & _rxFactory)56 OCheckBoxControl::OCheckBoxControl(const Reference<XMultiServiceFactory>& _rxFactory)
57     :OBoundControl(_rxFactory, VCL_CONTROL_CHECKBOX)
58 {
59 }
60 
61 //------------------------------------------------------------------
OCheckBoxControl_CreateInstance(const Reference<XMultiServiceFactory> & _rxFactory)62 InterfaceRef SAL_CALL OCheckBoxControl_CreateInstance(const Reference<XMultiServiceFactory>& _rxFactory) throw (RuntimeException)
63 {
64     return *(new OCheckBoxControl(_rxFactory));
65 }
66 
67 //------------------------------------------------------------------------------
getSupportedServiceNames()68 StringSequence SAL_CALL OCheckBoxControl::getSupportedServiceNames() throw(::com::sun::star::uno::RuntimeException)
69 {
70     StringSequence aSupported = OBoundControl::getSupportedServiceNames();
71     aSupported.realloc(aSupported.getLength() + 1);
72 
73     ::rtl::OUString* pArray = aSupported.getArray();
74     pArray[aSupported.getLength()-1] = FRM_SUN_CONTROL_CHECKBOX;
75     return aSupported;
76 }
77 
78 //==================================================================
79 //= OCheckBoxModel
80 //==================================================================
81 
82 //==================================================================
OCheckBoxModel_CreateInstance(const Reference<XMultiServiceFactory> & _rxFactory)83 InterfaceRef SAL_CALL OCheckBoxModel_CreateInstance(const Reference<XMultiServiceFactory>& _rxFactory) throw (RuntimeException)
84 {
85     return *(new OCheckBoxModel(_rxFactory));
86 }
87 
88 //------------------------------------------------------------------
DBG_NAME(OCheckBoxModel)89 DBG_NAME( OCheckBoxModel )
90 //------------------------------------------------------------------
91 OCheckBoxModel::OCheckBoxModel(const Reference<XMultiServiceFactory>& _rxFactory)
92     :OReferenceValueComponent( _rxFactory, VCL_CONTROLMODEL_CHECKBOX, FRM_SUN_CONTROL_CHECKBOX, sal_True )
93                     // use the old control name for compytibility reasons
94 {
95     DBG_CTOR( OCheckBoxModel, NULL );
96 
97     m_nClassId = FormComponentType::CHECKBOX;
98     initValueProperty( PROPERTY_STATE, PROPERTY_ID_STATE );
99 }
100 
101 //------------------------------------------------------------------
OCheckBoxModel(const OCheckBoxModel * _pOriginal,const Reference<XMultiServiceFactory> & _rxFactory)102 OCheckBoxModel::OCheckBoxModel( const OCheckBoxModel* _pOriginal, const Reference<XMultiServiceFactory>& _rxFactory )
103     :OReferenceValueComponent( _pOriginal, _rxFactory )
104 {
105     DBG_CTOR( OCheckBoxModel, NULL );
106 }
107 
108 //------------------------------------------------------------------------------
~OCheckBoxModel()109 OCheckBoxModel::~OCheckBoxModel()
110 {
111     DBG_DTOR( OCheckBoxModel, NULL );
112 }
113 
114 //------------------------------------------------------------------------------
IMPLEMENT_DEFAULT_CLONING(OCheckBoxModel)115 IMPLEMENT_DEFAULT_CLONING( OCheckBoxModel )
116 
117 // XServiceInfo
118 //------------------------------------------------------------------------------
119 StringSequence SAL_CALL OCheckBoxModel::getSupportedServiceNames() throw(::com::sun::star::uno::RuntimeException)
120 {
121     StringSequence aSupported = OReferenceValueComponent::getSupportedServiceNames();
122 
123     sal_Int32 nOldLen = aSupported.getLength();
124     aSupported.realloc( nOldLen + 8 );
125     ::rtl::OUString* pStoreTo = aSupported.getArray() + nOldLen;
126 
127     *pStoreTo++ = BINDABLE_CONTROL_MODEL;
128     *pStoreTo++ = DATA_AWARE_CONTROL_MODEL;
129     *pStoreTo++ = VALIDATABLE_CONTROL_MODEL;
130 
131     *pStoreTo++ = BINDABLE_DATA_AWARE_CONTROL_MODEL;
132     *pStoreTo++ = VALIDATABLE_BINDABLE_CONTROL_MODEL;
133 
134     *pStoreTo++ = FRM_SUN_COMPONENT_CHECKBOX;
135     *pStoreTo++ = FRM_SUN_COMPONENT_DATABASE_CHECKBOX;
136     *pStoreTo++ = BINDABLE_DATABASE_CHECK_BOX;
137 
138     return aSupported;
139 }
140 
141 //------------------------------------------------------------------------------
describeFixedProperties(Sequence<Property> & _rProps) const142 void OCheckBoxModel::describeFixedProperties( Sequence< Property >& _rProps ) const
143 {
144     BEGIN_DESCRIBE_PROPERTIES( 1, OReferenceValueComponent )
145         DECL_PROP1(TABINDEX,        sal_Int16,          BOUND);
146     END_DESCRIBE_PROPERTIES();
147 }
148 
149 //------------------------------------------------------------------------------
getServiceName()150 ::rtl::OUString SAL_CALL OCheckBoxModel::getServiceName() throw(RuntimeException)
151 {
152     return FRM_COMPONENT_CHECKBOX;  // old (non-sun) name for compatibility !
153 }
154 
155 //------------------------------------------------------------------------------
write(const Reference<stario::XObjectOutputStream> & _rxOutStream)156 void SAL_CALL OCheckBoxModel::write(const Reference<stario::XObjectOutputStream>& _rxOutStream)
157     throw(stario::IOException, RuntimeException)
158 {
159     OReferenceValueComponent::write(_rxOutStream);
160 
161     // Version
162     _rxOutStream->writeShort(0x0003);
163     // Properties
164     _rxOutStream << getReferenceValue();
165     _rxOutStream << (sal_Int16)getDefaultChecked();
166     writeHelpTextCompatibly(_rxOutStream);
167     // from version 0x0003 : common properties
168     writeCommonProperties(_rxOutStream);
169 }
170 
171 //------------------------------------------------------------------------------
read(const Reference<stario::XObjectInputStream> & _rxInStream)172 void SAL_CALL OCheckBoxModel::read(const Reference<stario::XObjectInputStream>& _rxInStream) throw(stario::IOException, RuntimeException)
173 {
174     OReferenceValueComponent::read(_rxInStream);
175     osl::MutexGuard aGuard(m_aMutex);
176 
177     // Version
178     sal_uInt16 nVersion = _rxInStream->readShort();
179 
180     ::rtl::OUString sReferenceValue;
181     sal_Int16       nDefaultChecked( 0 );
182     switch ( nVersion )
183     {
184         case 0x0001:
185             _rxInStream >> sReferenceValue;
186             nDefaultChecked = _rxInStream->readShort();
187             break;
188         case 0x0002:
189             _rxInStream >> sReferenceValue;
190             _rxInStream >> nDefaultChecked;
191             readHelpTextCompatibly( _rxInStream );
192             break;
193         case 0x0003:
194             _rxInStream >> sReferenceValue;
195             _rxInStream >> nDefaultChecked;
196             readHelpTextCompatibly(_rxInStream);
197             readCommonProperties(_rxInStream);
198             break;
199         default:
200             DBG_ERROR("OCheckBoxModel::read : unknown version !");
201             defaultCommonProperties();
202             break;
203     }
204     setReferenceValue( sReferenceValue );
205     setDefaultChecked( static_cast< ToggleState >( nDefaultChecked ) );
206 
207     // Nach dem Lesen die Defaultwerte anzeigen
208     if ( getControlSource().getLength() )
209         // (not if we don't have a control source - the "State" property acts like it is persistent, then
210         resetNoBroadcast();
211 }
212 
213 //------------------------------------------------------------------------------
translateDbColumnToControlValue()214 Any OCheckBoxModel::translateDbColumnToControlValue()
215 {
216     Any aValue;
217 
218     //////////////////////////////////////////////////////////////////
219     // Wert an ControlModel setzen
220     sal_Bool bValue = m_xColumn->getBoolean();
221     if ( m_xColumn->wasNull() )
222     {
223         sal_Bool bTriState = sal_True;
224         if ( m_xAggregateSet.is() )
225             m_xAggregateSet->getPropertyValue( PROPERTY_TRISTATE ) >>= bTriState;
226         aValue <<= (sal_Int16)( bTriState ? STATE_DONTKNOW : getDefaultChecked() );
227     }
228     else
229         aValue <<= (sal_Int16)( bValue ? STATE_CHECK : STATE_NOCHECK );
230 
231     return aValue;
232 }
233 
234 //-----------------------------------------------------------------------------
commitControlValueToDbColumn(bool)235 sal_Bool OCheckBoxModel::commitControlValueToDbColumn( bool /*_bPostReset*/ )
236 {
237     OSL_PRECOND( m_xColumnUpdate.is(), "OCheckBoxModel::commitControlValueToDbColumn: not bound!" );
238     if ( m_xColumnUpdate.is() )
239     {
240         Any aControlValue( m_xAggregateSet->getPropertyValue( PROPERTY_STATE ) );
241         try
242         {
243             sal_Int16 nValue = STATE_DONTKNOW;
244             aControlValue >>= nValue;
245             switch (nValue)
246             {
247                 case STATE_DONTKNOW:
248                     m_xColumnUpdate->updateNull();
249                     break;
250                 case STATE_CHECK:
251                     m_xColumnUpdate->updateBoolean( sal_True );
252                     break;
253                 case STATE_NOCHECK:
254                     m_xColumnUpdate->updateBoolean( sal_False );
255                     break;
256                 default:
257                     DBG_ERROR("OCheckBoxModel::commitControlValueToDbColumn: invalid value !");
258             }
259         }
260         catch(Exception&)
261         {
262             DBG_ERROR("OCheckBoxModel::commitControlValueToDbColumn: could not commit !");
263         }
264     }
265     return sal_True;
266 }
267 
268 //.........................................................................
269 }
270 //.........................................................................
271 
272