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 "CheckBox.hxx"
31 #include "property.hxx"
32 #ifndef _FRM_PROPERTY_HRC_
33 #include "property.hrc"
34 #endif
35 #include "services.hxx"
36 #include <tools/debug.hxx>
37 #include <comphelper/basicio.hxx>
38 
39 //.........................................................................
40 namespace frm
41 {
42 using namespace ::com::sun::star::uno;
43 using namespace ::com::sun::star::sdb;
44 using namespace ::com::sun::star::sdbc;
45 using namespace ::com::sun::star::sdbcx;
46 using namespace ::com::sun::star::beans;
47 using namespace ::com::sun::star::container;
48 using namespace ::com::sun::star::form;
49 using namespace ::com::sun::star::awt;
50 using namespace ::com::sun::star::io;
51 using namespace ::com::sun::star::lang;
52 using namespace ::com::sun::star::util;
53 using namespace ::com::sun::star::form::binding;
54 
55 //==================================================================
56 //= OCheckBoxControl
57 //==================================================================
58 
59 //------------------------------------------------------------------
60 OCheckBoxControl::OCheckBoxControl(const Reference<XMultiServiceFactory>& _rxFactory)
61 	:OBoundControl(_rxFactory, VCL_CONTROL_CHECKBOX)
62 {
63 }
64 
65 //------------------------------------------------------------------
66 InterfaceRef SAL_CALL OCheckBoxControl_CreateInstance(const Reference<XMultiServiceFactory>& _rxFactory) throw (RuntimeException)
67 {
68 	return *(new OCheckBoxControl(_rxFactory));
69 }
70 
71 //------------------------------------------------------------------------------
72 StringSequence SAL_CALL	OCheckBoxControl::getSupportedServiceNames() throw(::com::sun::star::uno::RuntimeException)
73 {
74 	StringSequence aSupported = OBoundControl::getSupportedServiceNames();
75 	aSupported.realloc(aSupported.getLength() + 1);
76 
77 	::rtl::OUString* pArray = aSupported.getArray();
78 	pArray[aSupported.getLength()-1] = FRM_SUN_CONTROL_CHECKBOX;
79 	return aSupported;
80 }
81 
82 //==================================================================
83 //= OCheckBoxModel
84 //==================================================================
85 
86 //==================================================================
87 InterfaceRef SAL_CALL OCheckBoxModel_CreateInstance(const Reference<XMultiServiceFactory>& _rxFactory) throw (RuntimeException)
88 {
89 	return *(new OCheckBoxModel(_rxFactory));
90 }
91 
92 //------------------------------------------------------------------
93 DBG_NAME( OCheckBoxModel )
94 //------------------------------------------------------------------
95 OCheckBoxModel::OCheckBoxModel(const Reference<XMultiServiceFactory>& _rxFactory)
96     :OReferenceValueComponent( _rxFactory, VCL_CONTROLMODEL_CHECKBOX, FRM_SUN_CONTROL_CHECKBOX, sal_True )
97 					// use the old control name for compytibility reasons
98 {
99 	DBG_CTOR( OCheckBoxModel, NULL );
100 
101 	m_nClassId = FormComponentType::CHECKBOX;
102     initValueProperty( PROPERTY_STATE, PROPERTY_ID_STATE );
103 }
104 
105 //------------------------------------------------------------------
106 OCheckBoxModel::OCheckBoxModel( const OCheckBoxModel* _pOriginal, const Reference<XMultiServiceFactory>& _rxFactory )
107 	:OReferenceValueComponent( _pOriginal, _rxFactory )
108 {
109 	DBG_CTOR( OCheckBoxModel, NULL );
110 }
111 
112 //------------------------------------------------------------------------------
113 OCheckBoxModel::~OCheckBoxModel()
114 {
115 	DBG_DTOR( OCheckBoxModel, NULL );
116 }
117 
118 //------------------------------------------------------------------------------
119 IMPLEMENT_DEFAULT_CLONING( OCheckBoxModel )
120 
121 // XServiceInfo
122 //------------------------------------------------------------------------------
123 StringSequence SAL_CALL	OCheckBoxModel::getSupportedServiceNames() throw(::com::sun::star::uno::RuntimeException)
124 {
125 	StringSequence aSupported = OReferenceValueComponent::getSupportedServiceNames();
126 
127     sal_Int32 nOldLen = aSupported.getLength();
128 	aSupported.realloc( nOldLen + 8 );
129 	::rtl::OUString* pStoreTo = aSupported.getArray() + nOldLen;
130 
131     *pStoreTo++ = BINDABLE_CONTROL_MODEL;
132     *pStoreTo++ = DATA_AWARE_CONTROL_MODEL;
133     *pStoreTo++ = VALIDATABLE_CONTROL_MODEL;
134 
135     *pStoreTo++ = BINDABLE_DATA_AWARE_CONTROL_MODEL;
136     *pStoreTo++ = VALIDATABLE_BINDABLE_CONTROL_MODEL;
137 
138     *pStoreTo++ = FRM_SUN_COMPONENT_CHECKBOX;
139     *pStoreTo++ = FRM_SUN_COMPONENT_DATABASE_CHECKBOX;
140     *pStoreTo++ = BINDABLE_DATABASE_CHECK_BOX;
141 
142 	return aSupported;
143 }
144 
145 //------------------------------------------------------------------------------
146 void OCheckBoxModel::describeFixedProperties( Sequence< Property >& _rProps ) const
147 {
148 	BEGIN_DESCRIBE_PROPERTIES( 1, OReferenceValueComponent )
149 		DECL_PROP1(TABINDEX,		sal_Int16,			BOUND);
150 	END_DESCRIBE_PROPERTIES();
151 }
152 
153 //------------------------------------------------------------------------------
154 ::rtl::OUString SAL_CALL OCheckBoxModel::getServiceName() throw(RuntimeException)
155 {
156 	return FRM_COMPONENT_CHECKBOX;	// old (non-sun) name for compatibility !
157 }
158 
159 //------------------------------------------------------------------------------
160 void SAL_CALL OCheckBoxModel::write(const Reference<stario::XObjectOutputStream>& _rxOutStream)
161 	throw(stario::IOException, RuntimeException)
162 {
163 	OReferenceValueComponent::write(_rxOutStream);
164 
165 	// Version
166 	_rxOutStream->writeShort(0x0003);
167 	// Properties
168 	_rxOutStream << getReferenceValue();
169 	_rxOutStream << (sal_Int16)getDefaultChecked();
170 	writeHelpTextCompatibly(_rxOutStream);
171 	// from version 0x0003 : common properties
172 	writeCommonProperties(_rxOutStream);
173 }
174 
175 //------------------------------------------------------------------------------
176 void SAL_CALL OCheckBoxModel::read(const Reference<stario::XObjectInputStream>& _rxInStream) throw(stario::IOException, RuntimeException)
177 {
178 	OReferenceValueComponent::read(_rxInStream);
179 	osl::MutexGuard aGuard(m_aMutex);
180 
181 	// Version
182 	sal_uInt16 nVersion = _rxInStream->readShort();
183 
184     ::rtl::OUString sReferenceValue;
185     sal_Int16       nDefaultChecked( 0 );
186 	switch ( nVersion )
187 	{
188 		case 0x0001:
189             _rxInStream >> sReferenceValue;
190             nDefaultChecked = _rxInStream->readShort();
191             break;
192 		case 0x0002:
193 			_rxInStream >> sReferenceValue;
194 			_rxInStream >> nDefaultChecked;
195 			readHelpTextCompatibly( _rxInStream );
196 			break;
197 		case 0x0003:
198 			_rxInStream >> sReferenceValue;
199 			_rxInStream >> nDefaultChecked;
200 			readHelpTextCompatibly(_rxInStream);
201 			readCommonProperties(_rxInStream);
202 			break;
203 		default:
204 			DBG_ERROR("OCheckBoxModel::read : unknown version !");
205 			defaultCommonProperties();
206 			break;
207 	}
208     setReferenceValue( sReferenceValue );
209     setDefaultChecked( static_cast< ToggleState >( nDefaultChecked ) );
210 
211 	// Nach dem Lesen die Defaultwerte anzeigen
212 	if ( getControlSource().getLength() )
213 		// (not if we don't have a control source - the "State" property acts like it is persistent, then
214 		resetNoBroadcast();
215 }
216 
217 //------------------------------------------------------------------------------
218 Any OCheckBoxModel::translateDbColumnToControlValue()
219 {
220     Any aValue;
221 
222 	//////////////////////////////////////////////////////////////////
223 	// Wert an ControlModel setzen
224 	sal_Bool bValue = m_xColumn->getBoolean();
225 	if ( m_xColumn->wasNull() )
226 	{
227         sal_Bool bTriState = sal_True;
228 	    if ( m_xAggregateSet.is() )
229 		    m_xAggregateSet->getPropertyValue( PROPERTY_TRISTATE ) >>= bTriState;
230         aValue <<= (sal_Int16)( bTriState ? STATE_DONTKNOW : getDefaultChecked() );
231 	}
232 	else
233 		aValue <<= (sal_Int16)( bValue ? STATE_CHECK : STATE_NOCHECK );
234 
235     return aValue;
236 }
237 
238 //-----------------------------------------------------------------------------
239 sal_Bool OCheckBoxModel::commitControlValueToDbColumn( bool /*_bPostReset*/ )
240 {
241 	OSL_PRECOND( m_xColumnUpdate.is(), "OCheckBoxModel::commitControlValueToDbColumn: not bound!" );
242 	if ( m_xColumnUpdate.is() )
243 	{
244         Any aControlValue( m_xAggregateSet->getPropertyValue( PROPERTY_STATE ) );
245 		try
246 		{
247 			sal_Int16 nValue = STATE_DONTKNOW;
248 			aControlValue >>= nValue;
249 			switch (nValue)
250 			{
251 				case STATE_DONTKNOW:
252 					m_xColumnUpdate->updateNull();
253 					break;
254 				case STATE_CHECK:
255 					m_xColumnUpdate->updateBoolean( sal_True );
256 					break;
257 				case STATE_NOCHECK:
258 					m_xColumnUpdate->updateBoolean( sal_False );
259 					break;
260 				default:
261 					DBG_ERROR("OCheckBoxModel::commitControlValueToDbColumn: invalid value !");
262 			}
263 		}
264 		catch(Exception&)
265 		{
266 			DBG_ERROR("OCheckBoxModel::commitControlValueToDbColumn: could not commit !");
267 		}
268 	}
269 	return sal_True;
270 }
271 
272 //.........................................................................
273 }
274 //.........................................................................
275 
276