merged tag LIBREOFFICE_3_2_99_3
[LibreOffice.git] / extensions / source / propctrlr / cellbindinghelper.cxx
blob24fdc7fc28c3667ad5017c1f2c00034de275ce21
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 // MARKER(update_precomp.py): autogen include statement, do not remove
30 #include "precompiled_extensions.hxx"
31 #include "cellbindinghelper.hxx"
32 #include <com/sun/star/form/binding/XBindableValue.hpp>
33 #include <com/sun/star/form/binding/XListEntrySink.hpp>
34 #include <com/sun/star/form/FormComponentType.hpp>
35 #include <com/sun/star/form/XGridColumnFactory.hpp>
36 #include <com/sun/star/container/XChild.hpp>
37 #include <com/sun/star/container/XNamed.hpp>
38 #include <com/sun/star/drawing/XDrawPageSupplier.hpp>
39 #include <com/sun/star/table/XCellRange.hpp>
40 #include <com/sun/star/form/XFormsSupplier.hpp>
41 #include <com/sun/star/form/XForm.hpp>
42 #include <com/sun/star/lang/XServiceInfo.hpp>
43 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
44 #include <com/sun/star/beans/NamedValue.hpp>
45 #include <com/sun/star/sheet/XSpreadsheet.hpp>
46 #include <unotools/transliterationwrapper.hxx>
47 #include <osl/diagnose.h>
48 #include <tools/diagnose_ex.h>
49 #include "formstrings.hxx"
51 #include <functional>
52 #include <algorithm>
54 //............................................................................
55 namespace pcr
57 //............................................................................
59 using namespace ::com::sun::star::uno;
60 using namespace ::com::sun::star::beans;
61 using namespace ::com::sun::star::frame;
62 using namespace ::com::sun::star::sheet;
63 using namespace ::com::sun::star::container;
64 using namespace ::com::sun::star::drawing;
65 using namespace ::com::sun::star::table;
66 using namespace ::com::sun::star::form;
67 using namespace ::com::sun::star::lang;
68 using namespace ::com::sun::star::i18n;
69 using namespace ::com::sun::star::form::binding;
71 namespace
73 //....................................................................
74 struct StringCompare : public ::std::unary_function< ::rtl::OUString, bool >
76 private:
77 ::rtl::OUString m_sReference;
79 public:
80 StringCompare( const ::rtl::OUString& _rReference ) : m_sReference( _rReference ) { }
82 inline bool operator()( const ::rtl::OUString& _rCompare )
84 return ( _rCompare == m_sReference ) ? true : false;
89 //========================================================================
90 //= CellBindingHelper
91 //========================================================================
92 //------------------------------------------------------------------------
93 CellBindingHelper::CellBindingHelper( const Reference< XPropertySet >& _rxControlModel, const Reference< XModel >& _rxContextDocument )
94 :m_xControlModel( _rxControlModel )
96 OSL_ENSURE( m_xControlModel.is(), "CellBindingHelper::CellBindingHelper: invalid control model!" );
98 m_xDocument = m_xDocument.query( _rxContextDocument );
99 OSL_ENSURE( m_xDocument.is(), "CellBindingHelper::CellBindingHelper: This is no spreadsheet document!" );
101 OSL_ENSURE( isSpreadsheetDocumentWhichSupplies( SERVICE_ADDRESS_CONVERSION ),
102 "CellBindingHelper::CellBindingHelper: the document cannot convert address representations!" );
105 //------------------------------------------------------------------------
106 sal_Bool CellBindingHelper::isSpreadsheetDocument( const Reference< XModel >& _rxContextDocument )
108 return Reference< XSpreadsheetDocument >::query( _rxContextDocument ).is();
111 //------------------------------------------------------------------------
112 sal_Int16 CellBindingHelper::getControlSheetIndex( Reference< XSpreadsheet >& _out_rxSheet ) const
114 sal_Int16 nSheetIndex = -1;
115 // every sheet has a draw page, and every draw page has a forms collection.
116 // Our control, OTOH, belongs to a forms collection. Match these ...
119 // for determining the draw page, we need the forms collection which
120 // the object belongs to. This is the first object up the hierarchy which is
121 // *no* XForm (and, well, no XGridColumnFactory)
122 Reference< XChild > xCheck( m_xControlModel, UNO_QUERY );
123 Reference< XForm > xParentAsForm; if ( xCheck.is() ) xParentAsForm = xParentAsForm.query( xCheck->getParent() );
124 Reference< XGridColumnFactory > xParentAsGrid; if ( xCheck.is() ) xParentAsGrid = xParentAsGrid.query( xCheck->getParent() );
126 while ( ( xParentAsForm.is() || xParentAsGrid.is() ) && xCheck.is() )
128 xCheck = xCheck.query( xCheck->getParent() );
129 xParentAsForm = xParentAsForm.query( xCheck.is() ? xCheck->getParent() : (Reference< XInterface >) Reference< XForm >() );
130 xParentAsGrid = xParentAsGrid.query( xCheck.is() ? xCheck->getParent() : (Reference< XInterface >) Reference< XGridColumnFactory >() );
132 Reference< XInterface > xFormsCollection( xCheck.is() ? xCheck->getParent() : Reference< XInterface >() );
134 // now iterate through the sheets
135 Reference< XIndexAccess > xSheets( m_xDocument->getSheets(), UNO_QUERY );
136 if ( xSheets.is() && xFormsCollection.is() )
138 for ( sal_Int32 i = 0; i < xSheets->getCount(); ++i )
140 Reference< XDrawPageSupplier > xSuppPage( xSheets->getByIndex( i ), UNO_QUERY_THROW );
141 Reference< XFormsSupplier > xSuppForms( xSuppPage->getDrawPage(), UNO_QUERY_THROW );
143 if ( xSuppForms->getForms() == xFormsCollection )
144 { // found it
145 nSheetIndex = (sal_Int16)i;
146 _out_rxSheet.set( xSuppPage, UNO_QUERY_THROW );
147 break;
152 catch( const Exception& )
154 DBG_UNHANDLED_EXCEPTION();
157 return nSheetIndex;
160 //------------------------------------------------------------------------
161 bool CellBindingHelper::convertStringAddress( const ::rtl::OUString& _rAddressDescription, CellAddress& /* [out] */ _rAddress ) const
163 Any aAddress;
164 return doConvertAddressRepresentations(
165 PROPERTY_UI_REPRESENTATION,
166 makeAny( _rAddressDescription ),
167 PROPERTY_ADDRESS,
168 aAddress,
169 false
171 && ( aAddress >>= _rAddress );
174 //------------------------------------------------------------------------
175 bool CellBindingHelper::doConvertAddressRepresentations( const ::rtl::OUString& _rInputProperty, const Any& _rInputValue,
176 const ::rtl::OUString& _rOutputProperty, Any& _rOutputValue, bool _bIsRange ) const SAL_THROW(())
178 bool bSuccess = false;
180 Reference< XPropertySet > xConverter(
181 createDocumentDependentInstance(
182 _bIsRange ? SERVICE_RANGEADDRESS_CONVERSION : SERVICE_ADDRESS_CONVERSION,
183 ::rtl::OUString(),
184 Any()
186 UNO_QUERY
188 OSL_ENSURE( xConverter.is(), "CellBindingHelper::doConvertAddressRepresentations: could not get a converter service!" );
189 if ( xConverter.is() )
193 Reference< XSpreadsheet > xSheet;
194 xConverter->setPropertyValue( PROPERTY_REFERENCE_SHEET, makeAny( (sal_Int32)getControlSheetIndex( xSheet ) ) );
195 xConverter->setPropertyValue( _rInputProperty, _rInputValue );
196 _rOutputValue = xConverter->getPropertyValue( _rOutputProperty );
197 bSuccess = true;
199 catch( const Exception& )
201 OSL_ENSURE( sal_False, "CellBindingHelper::doConvertAddressRepresentations: caught an exception!" );
205 return bSuccess;
208 //------------------------------------------------------------------------
209 bool CellBindingHelper::convertStringAddress( const ::rtl::OUString& _rAddressDescription,
210 CellRangeAddress& /* [out] */ _rAddress ) const
212 Any aAddress;
213 return doConvertAddressRepresentations(
214 PROPERTY_UI_REPRESENTATION,
215 makeAny( _rAddressDescription ),
216 PROPERTY_ADDRESS,
217 aAddress,
218 true
220 && ( aAddress >>= _rAddress );
223 //------------------------------------------------------------------------
224 Reference< XValueBinding > CellBindingHelper::createCellBindingFromAddress( const CellAddress& _rAddress, bool _bSupportIntegerExchange ) const
226 Reference< XValueBinding > xBinding( createDocumentDependentInstance(
227 _bSupportIntegerExchange ? SERVICE_SHEET_CELL_INT_BINDING : SERVICE_SHEET_CELL_BINDING,
228 PROPERTY_BOUND_CELL,
229 makeAny( _rAddress )
230 ), UNO_QUERY );
232 return xBinding;
235 //------------------------------------------------------------------------
236 Reference< XValueBinding > CellBindingHelper::createCellBindingFromStringAddress( const ::rtl::OUString& _rAddress, bool _bSupportIntegerExchange ) const
238 Reference< XValueBinding > xBinding;
239 if ( !m_xDocument.is() )
240 // very bad ...
241 return xBinding;
243 // get the UNO representation of the address
244 CellAddress aAddress;
245 if ( !_rAddress.getLength() || !convertStringAddress( _rAddress, aAddress ) )
246 return xBinding;
248 return createCellBindingFromAddress( aAddress, _bSupportIntegerExchange );
251 //------------------------------------------------------------------------
252 Reference< XListEntrySource > CellBindingHelper::createCellListSourceFromStringAddress( const ::rtl::OUString& _rAddress ) const
254 Reference< XListEntrySource > xSource;
256 CellRangeAddress aRangeAddress;
257 if ( !_rAddress.getLength() || !convertStringAddress( _rAddress, aRangeAddress ) )
258 return xSource;
260 // create a range object for this address
261 xSource = xSource.query( createDocumentDependentInstance(
262 SERVICE_SHEET_CELLRANGE_LISTSOURCE,
263 PROPERTY_LIST_CELL_RANGE,
264 makeAny( aRangeAddress )
265 ) );
267 return xSource;
270 //------------------------------------------------------------------------
271 Reference< XInterface > CellBindingHelper::createDocumentDependentInstance( const ::rtl::OUString& _rService, const ::rtl::OUString& _rArgumentName,
272 const Any& _rArgumentValue ) const
274 Reference< XInterface > xReturn;
276 Reference< XMultiServiceFactory > xDocumentFactory( m_xDocument, UNO_QUERY );
277 OSL_ENSURE( xDocumentFactory.is(), "CellBindingHelper::createDocumentDependentInstance: no document service factory!" );
278 if ( xDocumentFactory.is() )
282 if ( _rArgumentName.getLength() )
284 NamedValue aArg;
285 aArg.Name = _rArgumentName;
286 aArg.Value = _rArgumentValue;
288 Sequence< Any > aArgs( 1 );
289 aArgs[ 0 ] <<= aArg;
291 xReturn = xDocumentFactory->createInstanceWithArguments( _rService, aArgs );
293 else
295 xReturn = xDocumentFactory->createInstance( _rService );
298 catch ( const Exception& )
300 OSL_ENSURE( sal_False, "CellBindingHelper::createDocumentDependentInstance: could not create the binding at the document!" );
303 return xReturn;
306 //------------------------------------------------------------------------
307 bool CellBindingHelper::getAddressFromCellBinding(
308 const Reference< XValueBinding >& _rxBinding, CellAddress& _rAddress ) const
310 OSL_PRECOND( !_rxBinding.is() || isCellBinding( _rxBinding ), "CellBindingHelper::getAddressFromCellBinding: this is no cell binding!" );
312 bool bReturn = false;
313 if ( !m_xDocument.is() )
314 // very bad ...
315 return bReturn;
319 Reference< XPropertySet > xBindingProps( _rxBinding, UNO_QUERY );
320 OSL_ENSURE( xBindingProps.is() || !_rxBinding.is(), "CellBindingHelper::getAddressFromCellBinding: no property set for the binding!" );
321 if ( xBindingProps.is() )
323 CellAddress aAddress;
324 bReturn = (bool)( xBindingProps->getPropertyValue( PROPERTY_BOUND_CELL ) >>= _rAddress );
327 catch( const Exception& )
329 OSL_ENSURE( sal_False, "CellBindingHelper::getAddressFromCellBinding: caught an exception!" );
332 return bReturn;
335 //------------------------------------------------------------------------
336 ::rtl::OUString CellBindingHelper::getStringAddressFromCellBinding( const Reference< XValueBinding >& _rxBinding ) const
338 CellAddress aAddress;
339 ::rtl::OUString sAddress;
340 if ( getAddressFromCellBinding( _rxBinding, aAddress ) )
342 Any aStringAddress;
343 doConvertAddressRepresentations( PROPERTY_ADDRESS, makeAny( aAddress ),
344 PROPERTY_UI_REPRESENTATION, aStringAddress, false );
346 aStringAddress >>= sAddress;
349 return sAddress;
352 //------------------------------------------------------------------------
353 ::rtl::OUString CellBindingHelper::getStringAddressFromCellListSource( const Reference< XListEntrySource >& _rxSource ) const
355 OSL_PRECOND( !_rxSource.is() || isCellRangeListSource( _rxSource ), "CellBindingHelper::getStringAddressFromCellListSource: this is no cell list source!" );
357 ::rtl::OUString sAddress;
358 if ( !m_xDocument.is() )
359 // very bad ...
360 return sAddress;
364 Reference< XPropertySet > xSourceProps( _rxSource, UNO_QUERY );
365 OSL_ENSURE( xSourceProps.is() || !_rxSource.is(), "CellBindingHelper::getStringAddressFromCellListSource: no property set for the list source!" );
366 if ( xSourceProps.is() )
368 CellRangeAddress aRangeAddress;
369 xSourceProps->getPropertyValue( PROPERTY_LIST_CELL_RANGE ) >>= aRangeAddress;
371 Any aStringAddress;
372 doConvertAddressRepresentations( PROPERTY_ADDRESS, makeAny( aRangeAddress ),
373 PROPERTY_UI_REPRESENTATION, aStringAddress, true );
374 aStringAddress >>= sAddress;
377 catch( const Exception& )
379 OSL_ENSURE( sal_False, "CellBindingHelper::getStringAddressFromCellListSource: caught an exception!" );
382 return sAddress;
385 //------------------------------------------------------------------------
386 bool CellBindingHelper::isSpreadsheetDocumentWhichSupplies( const ::rtl::OUString& _rService ) const
388 bool bYesItIs = false;
390 Reference< XServiceInfo > xSI( m_xDocument, UNO_QUERY );
391 if ( xSI.is() && xSI->supportsService( SERVICE_SPREADSHEET_DOCUMENT ) )
393 Reference< XMultiServiceFactory > xDocumentFactory( m_xDocument, UNO_QUERY );
394 OSL_ENSURE( xDocumentFactory.is(), "CellBindingHelper::isSpreadsheetDocumentWhichSupplies: spreadsheet document, but no factory?" );
396 Sequence< ::rtl::OUString > aAvailableServices;
397 if ( xDocumentFactory.is() )
398 aAvailableServices = xDocumentFactory->getAvailableServiceNames( );
400 const ::rtl::OUString* pFound = ::std::find_if(
401 aAvailableServices.getConstArray(),
402 aAvailableServices.getConstArray() + aAvailableServices.getLength(),
403 StringCompare( _rService )
405 if ( pFound - aAvailableServices.getConstArray() < aAvailableServices.getLength() )
407 bYesItIs = true;
411 return bYesItIs;
414 //------------------------------------------------------------------------
415 bool CellBindingHelper::isListCellRangeAllowed( ) const
417 bool bAllow( false );
419 Reference< XListEntrySink > xSink( m_xControlModel, UNO_QUERY );
420 if ( xSink.is() )
422 bAllow = isSpreadsheetDocumentWhichSupplies( SERVICE_SHEET_CELLRANGE_LISTSOURCE );
425 return bAllow;
428 //------------------------------------------------------------------------
429 bool CellBindingHelper::isCellIntegerBindingAllowed( ) const
431 bool bAllow( true );
433 // first, we only offer this for controls which allow bindings in general
434 Reference< XBindableValue > xBindable( m_xControlModel, UNO_QUERY );
435 if ( !xBindable.is() )
436 bAllow = false;
438 // then, we must live in a spreadsheet document which can provide the special
439 // service needed for exchanging integer values
440 if ( bAllow )
441 bAllow = isSpreadsheetDocumentWhichSupplies( SERVICE_SHEET_CELL_INT_BINDING );
443 // then, we only offer this for list boxes
444 if ( bAllow )
448 sal_Int16 nClassId = FormComponentType::CONTROL;
449 m_xControlModel->getPropertyValue( PROPERTY_CLASSID ) >>= nClassId;
450 if ( FormComponentType::LISTBOX != nClassId )
451 bAllow = false;
453 catch( const Exception& )
455 OSL_ENSURE( sal_False, "CellBindingHelper::isCellIntegerBindingAllowed: caught an exception!" );
456 // are there really control models which survive isCellBindingAllowed, but don't have a ClassId
457 // property?
458 bAllow = false;
462 return bAllow;
465 //------------------------------------------------------------------------
466 bool CellBindingHelper::isCellBindingAllowed( ) const
468 bool bAllow( false );
470 Reference< XBindableValue > xBindable( m_xControlModel, UNO_QUERY );
471 if ( xBindable.is() )
473 // the control can potentially be bound to an external value
474 // Does it live within a Calc document, and is able to supply CellBindings?
475 bAllow = isSpreadsheetDocumentWhichSupplies( SERVICE_SHEET_CELL_BINDING );
478 // disallow for some types
479 // TODO: shouldn't the XBindableValue supply a list of supported types, and we can distingusih
480 // using this list? The current behavior below is somewhat hackish ...
481 if ( bAllow )
485 sal_Int16 nClassId = FormComponentType::CONTROL;
486 m_xControlModel->getPropertyValue( PROPERTY_CLASSID ) >>= nClassId;
487 if ( ( FormComponentType::DATEFIELD == nClassId ) || ( FormComponentType::TIMEFIELD == nClassId ) )
488 bAllow = false;
490 catch( const Exception& )
492 OSL_ENSURE( sal_False, "CellBindingHelper::isCellBindingAllowed: caught an exception!" );
493 bAllow = false;
496 return bAllow;
499 //------------------------------------------------------------------------
500 bool CellBindingHelper::isCellBinding( const Reference< XValueBinding >& _rxBinding ) const
502 return doesComponentSupport( _rxBinding.get(), SERVICE_SHEET_CELL_BINDING );
505 //------------------------------------------------------------------------
506 bool CellBindingHelper::isCellIntegerBinding( const Reference< XValueBinding >& _rxBinding ) const
508 return doesComponentSupport( _rxBinding.get(), SERVICE_SHEET_CELL_INT_BINDING );
511 //------------------------------------------------------------------------
512 bool CellBindingHelper::isCellRangeListSource( const Reference< XListEntrySource >& _rxSource ) const
514 return doesComponentSupport( _rxSource.get(), SERVICE_SHEET_CELLRANGE_LISTSOURCE );
517 //------------------------------------------------------------------------
518 bool CellBindingHelper::doesComponentSupport( const Reference< XInterface >& _rxComponent, const ::rtl::OUString& _rService ) const
520 bool bDoes = false;
521 Reference< XServiceInfo > xSI( _rxComponent, UNO_QUERY );
522 bDoes = xSI.is() && xSI->supportsService( _rService );
523 return bDoes;
526 //------------------------------------------------------------------------
527 Reference< XValueBinding > CellBindingHelper::getCurrentBinding( ) const
529 Reference< XValueBinding > xBinding;
530 Reference< XBindableValue > xBindable( m_xControlModel, UNO_QUERY );
531 if ( xBindable.is() )
532 xBinding = xBindable->getValueBinding();
533 return xBinding;
536 //------------------------------------------------------------------------
537 Reference< XListEntrySource > CellBindingHelper::getCurrentListSource( ) const
539 Reference< XListEntrySource > xSource;
540 Reference< XListEntrySink > xSink( m_xControlModel, UNO_QUERY );
541 if ( xSink.is() )
542 xSource = xSink->getListEntrySource();
543 return xSource;
546 //------------------------------------------------------------------------
547 void CellBindingHelper::setBinding( const Reference< XValueBinding >& _rxBinding )
549 Reference< XBindableValue > xBindable( m_xControlModel, UNO_QUERY );
550 OSL_PRECOND( xBindable.is(), "CellBindingHelper::setBinding: the object is not bindable!" );
551 if ( xBindable.is() )
552 xBindable->setValueBinding( _rxBinding );
555 //------------------------------------------------------------------------
556 void CellBindingHelper::setListSource( const Reference< XListEntrySource >& _rxSource )
558 Reference< XListEntrySink > xSink( m_xControlModel, UNO_QUERY );
559 OSL_PRECOND( xSink.is(), "CellBindingHelper::setListSource: the object is no list entry sink!" );
560 if ( xSink.is() )
561 xSink->setListEntrySource( _rxSource );
564 //............................................................................
565 } // namespace pcr
566 //............................................................................
568 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */