1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "cellbindinghelper.hxx"
21 #include <com/sun/star/form/binding/XBindableValue.hpp>
22 #include <com/sun/star/form/binding/XListEntrySink.hpp>
23 #include <com/sun/star/form/FormComponentType.hpp>
24 #include <com/sun/star/form/XGridColumnFactory.hpp>
25 #include <com/sun/star/container/XChild.hpp>
26 #include <com/sun/star/container/XNamed.hpp>
27 #include <com/sun/star/drawing/XDrawPageSupplier.hpp>
28 #include <com/sun/star/table/XCellRange.hpp>
29 #include <com/sun/star/form/XFormsSupplier.hpp>
30 #include <com/sun/star/form/XForm.hpp>
31 #include <com/sun/star/lang/XServiceInfo.hpp>
32 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
33 #include <com/sun/star/beans/NamedValue.hpp>
34 #include <com/sun/star/sheet/XSpreadsheet.hpp>
35 #include <unotools/transliterationwrapper.hxx>
36 #include <osl/diagnose.h>
37 #include <tools/diagnose_ex.h>
38 #include "formstrings.hxx"
48 using namespace ::com::sun::star::uno
;
49 using namespace ::com::sun::star::beans
;
50 using namespace ::com::sun::star::frame
;
51 using namespace ::com::sun::star::sheet
;
52 using namespace ::com::sun::star::container
;
53 using namespace ::com::sun::star::drawing
;
54 using namespace ::com::sun::star::table
;
55 using namespace ::com::sun::star::form
;
56 using namespace ::com::sun::star::lang
;
57 using namespace ::com::sun::star::i18n
;
58 using namespace ::com::sun::star::form::binding
;
63 struct StringCompare
: public std::unary_function
< OUString
, bool >
66 OUString m_sReference
;
69 explicit StringCompare( const OUString
& _rReference
) : m_sReference( _rReference
) { }
71 bool operator()( const OUString
& _rCompare
)
73 return ( _rCompare
== m_sReference
);
79 CellBindingHelper::CellBindingHelper( const Reference
< XPropertySet
>& _rxControlModel
, const Reference
< XModel
>& _rxContextDocument
)
80 :m_xControlModel( _rxControlModel
)
82 OSL_ENSURE( m_xControlModel
.is(), "CellBindingHelper::CellBindingHelper: invalid control model!" );
84 m_xDocument
.set(_rxContextDocument
, css::uno::UNO_QUERY
);
85 OSL_ENSURE( m_xDocument
.is(), "CellBindingHelper::CellBindingHelper: This is no spreadsheet document!" );
87 OSL_ENSURE( isSpreadsheetDocumentWhichSupplies( SERVICE_ADDRESS_CONVERSION
),
88 "CellBindingHelper::CellBindingHelper: the document cannot convert address representations!" );
92 bool CellBindingHelper::isSpreadsheetDocument( const Reference
< XModel
>& _rxContextDocument
)
94 return Reference
< XSpreadsheetDocument
>::query( _rxContextDocument
).is();
98 sal_Int16
CellBindingHelper::getControlSheetIndex( Reference
< XSpreadsheet
>& _out_rxSheet
) const
100 sal_Int16 nSheetIndex
= -1;
101 // every sheet has a draw page, and every draw page has a forms collection.
102 // Our control, OTOH, belongs to a forms collection. Match these...
105 // for determining the draw page, we need the forms collection which
106 // the object belongs to. This is the first object up the hierarchy which is
107 // *no* XForm (and, well, no XGridColumnFactory)
108 Reference
< XChild
> xCheck( m_xControlModel
, UNO_QUERY
);
109 Reference
< XForm
> xParentAsForm
; if ( xCheck
.is() ) xParentAsForm
.set(xCheck
->getParent(), css::uno::UNO_QUERY
);
110 Reference
< XGridColumnFactory
> xParentAsGrid
; if ( xCheck
.is() ) xParentAsGrid
.set(xCheck
->getParent(), css::uno::UNO_QUERY
);
112 while ( ( xParentAsForm
.is() || xParentAsGrid
.is() ) && xCheck
.is() )
114 xCheck
.set(xCheck
->getParent(), css::uno::UNO_QUERY
);
115 xParentAsForm
.set(xCheck
.is() ? xCheck
->getParent() : Reference
< XForm
>(), css::uno::UNO_QUERY
);
116 xParentAsGrid
.set(xCheck
.is() ? xCheck
->getParent() : Reference
< XGridColumnFactory
>(), css::uno::UNO_QUERY
);
118 Reference
< XInterface
> xFormsCollection( xCheck
.is() ? xCheck
->getParent() : Reference
< XInterface
>() );
120 // now iterate through the sheets
121 Reference
< XIndexAccess
> xSheets( m_xDocument
->getSheets(), UNO_QUERY
);
122 if ( xSheets
.is() && xFormsCollection
.is() )
124 for ( sal_Int32 i
= 0; i
< xSheets
->getCount(); ++i
)
126 Reference
< XDrawPageSupplier
> xSuppPage( xSheets
->getByIndex( i
), UNO_QUERY_THROW
);
127 Reference
< XFormsSupplier
> xSuppForms( xSuppPage
->getDrawPage(), UNO_QUERY_THROW
);
129 if ( xSuppForms
->getForms() == xFormsCollection
)
131 nSheetIndex
= (sal_Int16
)i
;
132 _out_rxSheet
.set( xSuppPage
, UNO_QUERY_THROW
);
138 catch( const Exception
& )
140 DBG_UNHANDLED_EXCEPTION();
147 bool CellBindingHelper::convertStringAddress( const OUString
& _rAddressDescription
, CellAddress
& /* [out] */ _rAddress
) const
150 return doConvertAddressRepresentations(
151 PROPERTY_UI_REPRESENTATION
,
152 makeAny( _rAddressDescription
),
157 && ( aAddress
>>= _rAddress
);
161 bool CellBindingHelper::doConvertAddressRepresentations( const OUString
& _rInputProperty
, const Any
& _rInputValue
,
162 const OUString
& _rOutputProperty
, Any
& _rOutputValue
, bool _bIsRange
) const
164 bool bSuccess
= false;
166 Reference
< XPropertySet
> xConverter(
167 createDocumentDependentInstance(
168 _bIsRange
? OUString(SERVICE_RANGEADDRESS_CONVERSION
) : OUString(SERVICE_ADDRESS_CONVERSION
),
174 OSL_ENSURE( xConverter
.is(), "CellBindingHelper::doConvertAddressRepresentations: could not get a converter service!" );
175 if ( xConverter
.is() )
179 Reference
< XSpreadsheet
> xSheet
;
180 xConverter
->setPropertyValue( PROPERTY_REFERENCE_SHEET
, makeAny( (sal_Int32
)getControlSheetIndex( xSheet
) ) );
181 xConverter
->setPropertyValue( _rInputProperty
, _rInputValue
);
182 _rOutputValue
= xConverter
->getPropertyValue( _rOutputProperty
);
185 catch( const Exception
& )
187 OSL_FAIL( "CellBindingHelper::doConvertAddressRepresentations: caught an exception!" );
195 bool CellBindingHelper::convertStringAddress( const OUString
& _rAddressDescription
,
196 CellRangeAddress
& /* [out] */ _rAddress
) const
199 return doConvertAddressRepresentations(
200 PROPERTY_UI_REPRESENTATION
,
201 makeAny( _rAddressDescription
),
206 && ( aAddress
>>= _rAddress
);
210 Reference
< XValueBinding
> CellBindingHelper::createCellBindingFromAddress( const CellAddress
& _rAddress
, bool _bSupportIntegerExchange
) const
212 Reference
< XValueBinding
> xBinding( createDocumentDependentInstance(
213 _bSupportIntegerExchange
? OUString(SERVICE_SHEET_CELL_INT_BINDING
) : OUString(SERVICE_SHEET_CELL_BINDING
),
222 Reference
< XValueBinding
> CellBindingHelper::createCellBindingFromStringAddress( const OUString
& _rAddress
, bool _bSupportIntegerExchange
) const
224 Reference
< XValueBinding
> xBinding
;
225 if ( !m_xDocument
.is() )
229 // get the UNO representation of the address
230 CellAddress aAddress
;
231 if ( _rAddress
.isEmpty() || !convertStringAddress( _rAddress
, aAddress
) )
234 return createCellBindingFromAddress( aAddress
, _bSupportIntegerExchange
);
238 Reference
< XListEntrySource
> CellBindingHelper::createCellListSourceFromStringAddress( const OUString
& _rAddress
) const
240 Reference
< XListEntrySource
> xSource
;
242 CellRangeAddress aRangeAddress
;
243 if ( _rAddress
.isEmpty() || !convertStringAddress( _rAddress
, aRangeAddress
) )
246 // create a range object for this address
247 xSource
.set(createDocumentDependentInstance(
248 SERVICE_SHEET_CELLRANGE_LISTSOURCE
,
249 PROPERTY_LIST_CELL_RANGE
,
250 makeAny( aRangeAddress
)
251 ), css::uno::UNO_QUERY
);
257 Reference
< XInterface
> CellBindingHelper::createDocumentDependentInstance( const OUString
& _rService
, const OUString
& _rArgumentName
,
258 const Any
& _rArgumentValue
) const
260 Reference
< XInterface
> xReturn
;
262 Reference
< XMultiServiceFactory
> xDocumentFactory( m_xDocument
, UNO_QUERY
);
263 OSL_ENSURE( xDocumentFactory
.is(), "CellBindingHelper::createDocumentDependentInstance: no document service factory!" );
264 if ( xDocumentFactory
.is() )
268 if ( !_rArgumentName
.isEmpty() )
271 aArg
.Name
= _rArgumentName
;
272 aArg
.Value
= _rArgumentValue
;
274 Sequence
< Any
> aArgs( 1 );
277 xReturn
= xDocumentFactory
->createInstanceWithArguments( _rService
, aArgs
);
281 xReturn
= xDocumentFactory
->createInstance( _rService
);
284 catch ( const Exception
& )
286 OSL_FAIL( "CellBindingHelper::createDocumentDependentInstance: could not create the binding at the document!" );
293 bool CellBindingHelper::getAddressFromCellBinding(
294 const Reference
< XValueBinding
>& _rxBinding
, CellAddress
& _rAddress
) const
296 OSL_PRECOND( !_rxBinding
.is() || isCellBinding( _rxBinding
), "CellBindingHelper::getAddressFromCellBinding: this is no cell binding!" );
298 bool bReturn
= false;
299 if ( !m_xDocument
.is() )
305 Reference
< XPropertySet
> xBindingProps( _rxBinding
, UNO_QUERY
);
306 OSL_ENSURE( xBindingProps
.is() || !_rxBinding
.is(), "CellBindingHelper::getAddressFromCellBinding: no property set for the binding!" );
307 if ( xBindingProps
.is() )
309 bReturn
= ( xBindingProps
->getPropertyValue( PROPERTY_BOUND_CELL
) >>= _rAddress
);
312 catch( const Exception
& )
314 OSL_FAIL( "CellBindingHelper::getAddressFromCellBinding: caught an exception!" );
321 OUString
CellBindingHelper::getStringAddressFromCellBinding( const Reference
< XValueBinding
>& _rxBinding
) const
323 CellAddress aAddress
;
325 if ( getAddressFromCellBinding( _rxBinding
, aAddress
) )
328 doConvertAddressRepresentations( PROPERTY_ADDRESS
, makeAny( aAddress
),
329 PROPERTY_UI_REPRESENTATION
, aStringAddress
, false );
331 aStringAddress
>>= sAddress
;
338 OUString
CellBindingHelper::getStringAddressFromCellListSource( const Reference
< XListEntrySource
>& _rxSource
) const
340 OSL_PRECOND( !_rxSource
.is() || isCellRangeListSource( _rxSource
), "CellBindingHelper::getStringAddressFromCellListSource: this is no cell list source!" );
343 if ( !m_xDocument
.is() )
349 Reference
< XPropertySet
> xSourceProps( _rxSource
, UNO_QUERY
);
350 OSL_ENSURE( xSourceProps
.is() || !_rxSource
.is(), "CellBindingHelper::getStringAddressFromCellListSource: no property set for the list source!" );
351 if ( xSourceProps
.is() )
353 CellRangeAddress aRangeAddress
;
354 xSourceProps
->getPropertyValue( PROPERTY_LIST_CELL_RANGE
) >>= aRangeAddress
;
357 doConvertAddressRepresentations( PROPERTY_ADDRESS
, makeAny( aRangeAddress
),
358 PROPERTY_UI_REPRESENTATION
, aStringAddress
, true );
359 aStringAddress
>>= sAddress
;
362 catch( const Exception
& )
364 OSL_FAIL( "CellBindingHelper::getStringAddressFromCellListSource: caught an exception!" );
371 bool CellBindingHelper::isSpreadsheetDocumentWhichSupplies( const OUString
& _rService
) const
373 bool bYesItIs
= false;
375 Reference
< XServiceInfo
> xSI( m_xDocument
, UNO_QUERY
);
376 if ( xSI
.is() && xSI
->supportsService( SERVICE_SPREADSHEET_DOCUMENT
) )
378 Reference
< XMultiServiceFactory
> xDocumentFactory( m_xDocument
, UNO_QUERY
);
379 OSL_ENSURE( xDocumentFactory
.is(), "CellBindingHelper::isSpreadsheetDocumentWhichSupplies: spreadsheet document, but no factory?" );
381 Sequence
< OUString
> aAvailableServices
;
382 if ( xDocumentFactory
.is() )
383 aAvailableServices
= xDocumentFactory
->getAvailableServiceNames( );
385 const OUString
* pFound
= std::find_if(
386 aAvailableServices
.getConstArray(),
387 aAvailableServices
.getConstArray() + aAvailableServices
.getLength(),
388 StringCompare( _rService
)
390 if ( pFound
- aAvailableServices
.getConstArray() < aAvailableServices
.getLength() )
400 bool CellBindingHelper::isListCellRangeAllowed( ) const
402 bool bAllow( false );
404 Reference
< XListEntrySink
> xSink( m_xControlModel
, UNO_QUERY
);
407 bAllow
= isSpreadsheetDocumentWhichSupplies( SERVICE_SHEET_CELLRANGE_LISTSOURCE
);
414 bool CellBindingHelper::isCellIntegerBindingAllowed( ) const
418 // first, we only offer this for controls which allow bindings in general
419 Reference
< XBindableValue
> xBindable( m_xControlModel
, UNO_QUERY
);
420 if ( !xBindable
.is() )
423 // then, we must live in a spreadsheet document which can provide the special
424 // service needed for exchanging integer values
426 bAllow
= isSpreadsheetDocumentWhichSupplies( SERVICE_SHEET_CELL_INT_BINDING
);
428 // then, we only offer this for list boxes
433 sal_Int16 nClassId
= FormComponentType::CONTROL
;
434 m_xControlModel
->getPropertyValue( PROPERTY_CLASSID
) >>= nClassId
;
435 if ( FormComponentType::LISTBOX
!= nClassId
)
438 catch( const Exception
& )
440 OSL_FAIL( "CellBindingHelper::isCellIntegerBindingAllowed: caught an exception!" );
441 // are there really control models which survive isCellBindingAllowed, but don't have a ClassId
451 bool CellBindingHelper::isCellBindingAllowed( ) const
453 bool bAllow( false );
455 Reference
< XBindableValue
> xBindable( m_xControlModel
, UNO_QUERY
);
456 if ( xBindable
.is() )
458 // the control can potentially be bound to an external value
459 // Does it live within a Calc document, and is able to supply CellBindings?
460 bAllow
= isSpreadsheetDocumentWhichSupplies( SERVICE_SHEET_CELL_BINDING
);
463 // disallow for some types
464 // TODO: shouldn't the XBindableValue supply a list of supported types, and we can distinguish
465 // using this list? The current behavior below is somewhat hackish...
470 sal_Int16 nClassId
= FormComponentType::CONTROL
;
471 m_xControlModel
->getPropertyValue( PROPERTY_CLASSID
) >>= nClassId
;
472 if ( ( FormComponentType::DATEFIELD
== nClassId
) || ( FormComponentType::TIMEFIELD
== nClassId
) )
475 catch( const Exception
& )
477 OSL_FAIL( "CellBindingHelper::isCellBindingAllowed: caught an exception!" );
485 bool CellBindingHelper::isCellBinding( const Reference
< XValueBinding
>& _rxBinding
)
487 return doesComponentSupport( _rxBinding
.get(), SERVICE_SHEET_CELL_BINDING
);
491 bool CellBindingHelper::isCellIntegerBinding( const Reference
< XValueBinding
>& _rxBinding
)
493 return doesComponentSupport( _rxBinding
.get(), SERVICE_SHEET_CELL_INT_BINDING
);
497 bool CellBindingHelper::isCellRangeListSource( const Reference
< XListEntrySource
>& _rxSource
)
499 return doesComponentSupport( _rxSource
.get(), SERVICE_SHEET_CELLRANGE_LISTSOURCE
);
503 bool CellBindingHelper::doesComponentSupport( const Reference
< XInterface
>& _rxComponent
, const OUString
& _rService
)
505 Reference
< XServiceInfo
> xSI( _rxComponent
, UNO_QUERY
);
506 bool bDoes
= xSI
.is() && xSI
->supportsService( _rService
);
511 Reference
< XValueBinding
> CellBindingHelper::getCurrentBinding( ) const
513 Reference
< XValueBinding
> xBinding
;
514 Reference
< XBindableValue
> xBindable( m_xControlModel
, UNO_QUERY
);
515 if ( xBindable
.is() )
516 xBinding
= xBindable
->getValueBinding();
521 Reference
< XListEntrySource
> CellBindingHelper::getCurrentListSource( ) const
523 Reference
< XListEntrySource
> xSource
;
524 Reference
< XListEntrySink
> xSink( m_xControlModel
, UNO_QUERY
);
526 xSource
= xSink
->getListEntrySource();
531 void CellBindingHelper::setBinding( const Reference
< XValueBinding
>& _rxBinding
)
533 Reference
< XBindableValue
> xBindable( m_xControlModel
, UNO_QUERY
);
534 OSL_PRECOND( xBindable
.is(), "CellBindingHelper::setBinding: the object is not bindable!" );
535 if ( xBindable
.is() )
536 xBindable
->setValueBinding( _rxBinding
);
540 void CellBindingHelper::setListSource( const Reference
< XListEntrySource
>& _rxSource
)
542 Reference
< XListEntrySink
> xSink( m_xControlModel
, UNO_QUERY
);
543 OSL_PRECOND( xSink
.is(), "CellBindingHelper::setListSource: the object is no list entry sink!" );
545 xSink
->setListEntrySource( _rxSource
);
552 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */