Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "CheckBox.hxx"
21 : #include "property.hxx"
22 : #include "property.hrc"
23 : #include "services.hxx"
24 : #include <comphelper/basicio.hxx>
25 : #include <comphelper/processfactory.hxx>
26 :
27 :
28 : namespace frm
29 : {
30 : using namespace ::com::sun::star::uno;
31 : using namespace ::com::sun::star::sdb;
32 : using namespace ::com::sun::star::sdbc;
33 : using namespace ::com::sun::star::sdbcx;
34 : using namespace ::com::sun::star::beans;
35 : using namespace ::com::sun::star::container;
36 : using namespace ::com::sun::star::form;
37 : using namespace ::com::sun::star::awt;
38 : using namespace ::com::sun::star::io;
39 : using namespace ::com::sun::star::lang;
40 : using namespace ::com::sun::star::util;
41 : using namespace ::com::sun::star::form::binding;
42 :
43 36 : OCheckBoxControl::OCheckBoxControl(const Reference<XComponentContext>& _rxFactory)
44 36 : :OBoundControl(_rxFactory, VCL_CONTROL_CHECKBOX)
45 : {
46 36 : }
47 :
48 :
49 36 : InterfaceRef SAL_CALL OCheckBoxControl_CreateInstance(const Reference<XMultiServiceFactory>& _rxFactory)
50 : {
51 36 : return *(new OCheckBoxControl( comphelper::getComponentContext(_rxFactory) ));
52 : }
53 :
54 :
55 0 : StringSequence SAL_CALL OCheckBoxControl::getSupportedServiceNames() throw(::com::sun::star::uno::RuntimeException, std::exception)
56 : {
57 0 : StringSequence aSupported = OBoundControl::getSupportedServiceNames();
58 0 : aSupported.realloc(aSupported.getLength() + 1);
59 :
60 0 : OUString* pArray = aSupported.getArray();
61 0 : pArray[aSupported.getLength()-1] = FRM_SUN_CONTROL_CHECKBOX;
62 0 : return aSupported;
63 : }
64 :
65 :
66 : //= OCheckBoxModel
67 :
68 :
69 :
70 38 : InterfaceRef SAL_CALL OCheckBoxModel_CreateInstance(const Reference<XMultiServiceFactory>& _rxFactory)
71 : {
72 38 : return *(new OCheckBoxModel( comphelper::getComponentContext(_rxFactory) ));
73 : }
74 :
75 :
76 :
77 38 : OCheckBoxModel::OCheckBoxModel(const Reference<XComponentContext>& _rxFactory)
78 38 : :OReferenceValueComponent( _rxFactory, VCL_CONTROLMODEL_CHECKBOX, FRM_SUN_CONTROL_CHECKBOX, true )
79 : // use the old control name for compytibility reasons
80 : {
81 :
82 38 : m_nClassId = FormComponentType::CHECKBOX;
83 38 : initValueProperty( PROPERTY_STATE, PROPERTY_ID_STATE );
84 38 : }
85 :
86 :
87 2 : OCheckBoxModel::OCheckBoxModel( const OCheckBoxModel* _pOriginal, const Reference<XComponentContext>& _rxFactory )
88 2 : :OReferenceValueComponent( _pOriginal, _rxFactory )
89 : {
90 2 : }
91 :
92 :
93 80 : OCheckBoxModel::~OCheckBoxModel()
94 : {
95 80 : }
96 :
97 :
98 2 : IMPLEMENT_DEFAULT_CLONING( OCheckBoxModel )
99 :
100 : // XServiceInfo
101 :
102 14 : StringSequence SAL_CALL OCheckBoxModel::getSupportedServiceNames() throw(::com::sun::star::uno::RuntimeException, std::exception)
103 : {
104 14 : StringSequence aSupported = OReferenceValueComponent::getSupportedServiceNames();
105 :
106 14 : sal_Int32 nOldLen = aSupported.getLength();
107 14 : aSupported.realloc( nOldLen + 8 );
108 14 : OUString* pStoreTo = aSupported.getArray() + nOldLen;
109 :
110 14 : *pStoreTo++ = BINDABLE_CONTROL_MODEL;
111 14 : *pStoreTo++ = DATA_AWARE_CONTROL_MODEL;
112 14 : *pStoreTo++ = VALIDATABLE_CONTROL_MODEL;
113 :
114 14 : *pStoreTo++ = BINDABLE_DATA_AWARE_CONTROL_MODEL;
115 14 : *pStoreTo++ = VALIDATABLE_BINDABLE_CONTROL_MODEL;
116 :
117 14 : *pStoreTo++ = FRM_SUN_COMPONENT_CHECKBOX;
118 14 : *pStoreTo++ = FRM_SUN_COMPONENT_DATABASE_CHECKBOX;
119 14 : *pStoreTo++ = BINDABLE_DATABASE_CHECK_BOX;
120 :
121 14 : return aSupported;
122 : }
123 :
124 :
125 42 : void OCheckBoxModel::describeFixedProperties( Sequence< Property >& _rProps ) const
126 : {
127 42 : BEGIN_DESCRIBE_PROPERTIES( 1, OReferenceValueComponent )
128 42 : DECL_PROP1(TABINDEX, sal_Int16, BOUND);
129 : END_DESCRIBE_PROPERTIES();
130 42 : }
131 :
132 :
133 2 : OUString SAL_CALL OCheckBoxModel::getServiceName() throw(RuntimeException, std::exception)
134 : {
135 2 : return OUString(FRM_COMPONENT_CHECKBOX); // old (non-sun) name for compatibility !
136 : }
137 :
138 :
139 2 : void SAL_CALL OCheckBoxModel::write(const Reference<css::io::XObjectOutputStream>& _rxOutStream)
140 : throw(css::io::IOException, RuntimeException, std::exception)
141 : {
142 2 : OReferenceValueComponent::write(_rxOutStream);
143 :
144 : // Version
145 2 : _rxOutStream->writeShort(0x0003);
146 : // Properties
147 2 : _rxOutStream << getReferenceValue();
148 2 : _rxOutStream << (sal_Int16)getDefaultChecked();
149 2 : writeHelpTextCompatibly(_rxOutStream);
150 : // from version 0x0003 : common properties
151 2 : writeCommonProperties(_rxOutStream);
152 2 : }
153 :
154 :
155 2 : void SAL_CALL OCheckBoxModel::read(const Reference<css::io::XObjectInputStream>& _rxInStream) throw(css::io::IOException, RuntimeException, std::exception)
156 : {
157 2 : OReferenceValueComponent::read(_rxInStream);
158 2 : osl::MutexGuard aGuard(m_aMutex);
159 :
160 : // Version
161 2 : sal_uInt16 nVersion = _rxInStream->readShort();
162 :
163 4 : OUString sReferenceValue;
164 2 : sal_Int16 nDefaultChecked( 0 );
165 2 : switch ( nVersion )
166 : {
167 : case 0x0001:
168 0 : _rxInStream >> sReferenceValue;
169 0 : nDefaultChecked = _rxInStream->readShort();
170 0 : break;
171 : case 0x0002:
172 0 : _rxInStream >> sReferenceValue;
173 0 : _rxInStream >> nDefaultChecked;
174 0 : readHelpTextCompatibly( _rxInStream );
175 0 : break;
176 : case 0x0003:
177 2 : _rxInStream >> sReferenceValue;
178 2 : _rxInStream >> nDefaultChecked;
179 2 : readHelpTextCompatibly(_rxInStream);
180 2 : readCommonProperties(_rxInStream);
181 2 : break;
182 : default:
183 : OSL_FAIL("OCheckBoxModel::read : unknown version !");
184 0 : defaultCommonProperties();
185 0 : break;
186 : }
187 2 : setReferenceValue( sReferenceValue );
188 2 : setDefaultChecked( static_cast< ToggleState >( nDefaultChecked ) );
189 :
190 : // After reading in, display the default values
191 2 : if ( !getControlSource().isEmpty() )
192 : // (not if we don't have a control source - the "State" property acts like it is persistent, then
193 2 : resetNoBroadcast();
194 2 : }
195 :
196 0 : bool OCheckBoxModel::DbUseBool()
197 : {
198 0 : if ( ! (getReferenceValue().isEmpty() && getNoCheckReferenceValue().isEmpty()) )
199 0 : return false;
200 0 : return true;
201 : }
202 :
203 :
204 0 : Any OCheckBoxModel::translateDbColumnToControlValue()
205 : {
206 0 : Any aValue;
207 :
208 :
209 : // Set value in ControlModel
210 0 : bool bValue = bool(); // avoid warning
211 0 : if(DbUseBool())
212 : {
213 0 : bValue = m_xColumn->getBoolean();
214 : }
215 : else
216 : {
217 0 : const OUString sVal(m_xColumn->getString());
218 0 : if (sVal == getReferenceValue())
219 0 : bValue = true;
220 0 : else if (sVal == getNoCheckReferenceValue())
221 0 : bValue = false;
222 : else
223 0 : aValue <<= static_cast<sal_Int16>(getDefaultChecked());
224 : }
225 0 : if ( m_xColumn->wasNull() )
226 : {
227 0 : bool bTriState = true;
228 0 : if ( m_xAggregateSet.is() )
229 0 : m_xAggregateSet->getPropertyValue( PROPERTY_TRISTATE ) >>= bTriState;
230 0 : aValue <<= (sal_Int16)( bTriState ? TRISTATE_INDET : getDefaultChecked() );
231 : }
232 0 : else if ( !aValue.hasValue() )
233 : {
234 : // Since above either bValue is initialised, either aValue.hasValue(),
235 : // bValue cannot be used uninitialised here.
236 : // But GCC does not see/understand that, which breaks -Werror builds,
237 : // so we explicitly default-initialise it.
238 0 : aValue <<= (sal_Int16)( bValue ? TRISTATE_TRUE : TRISTATE_FALSE );
239 : }
240 :
241 0 : return aValue;
242 : }
243 :
244 :
245 0 : bool OCheckBoxModel::commitControlValueToDbColumn( bool /*_bPostReset*/ )
246 : {
247 : OSL_PRECOND( m_xColumnUpdate.is(), "OCheckBoxModel::commitControlValueToDbColumn: not bound!" );
248 0 : if ( m_xColumnUpdate.is() )
249 : {
250 0 : Any aControlValue( m_xAggregateSet->getPropertyValue( PROPERTY_STATE ) );
251 : try
252 : {
253 0 : sal_Int16 nValue = TRISTATE_INDET;
254 0 : aControlValue >>= nValue;
255 0 : switch (nValue)
256 : {
257 : case TRISTATE_INDET:
258 0 : m_xColumnUpdate->updateNull();
259 0 : break;
260 : case TRISTATE_TRUE:
261 0 : if (DbUseBool())
262 0 : m_xColumnUpdate->updateBoolean( sal_True );
263 : else
264 0 : m_xColumnUpdate->updateString( getReferenceValue() );
265 0 : break;
266 : case TRISTATE_FALSE:
267 0 : if (DbUseBool())
268 0 : m_xColumnUpdate->updateBoolean( sal_False );
269 : else
270 0 : m_xColumnUpdate->updateString( getNoCheckReferenceValue() );
271 0 : break;
272 : default:
273 : OSL_FAIL("OCheckBoxModel::commitControlValueToDbColumn: invalid value !");
274 : }
275 : }
276 0 : catch(const Exception&)
277 : {
278 : OSL_FAIL("OCheckBoxModel::commitControlValueToDbColumn: could not commit !");
279 0 : }
280 : }
281 0 : return true;
282 : }
283 :
284 :
285 : }
286 :
287 :
288 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|