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"
47 using namespace ::com::sun::star::uno
;
48 using namespace ::com::sun::star::beans
;
49 using namespace ::com::sun::star::frame
;
50 using namespace ::com::sun::star::sheet
;
51 using namespace ::com::sun::star::container
;
52 using namespace ::com::sun::star::drawing
;
53 using namespace ::com::sun::star::table
;
54 using namespace ::com::sun::star::form
;
55 using namespace ::com::sun::star::lang
;
56 using namespace ::com::sun::star::i18n
;
57 using namespace ::com::sun::star::form::binding
;
65 OUString m_sReference
;
68 explicit StringCompare( const OUString
& _rReference
) : m_sReference( _rReference
) { }
70 bool operator()( const OUString
& _rCompare
)
72 return ( _rCompare
== m_sReference
);
78 CellBindingHelper::CellBindingHelper( const Reference
< XPropertySet
>& _rxControlModel
, const Reference
< XModel
>& _rxContextDocument
)
79 :m_xControlModel( _rxControlModel
)
81 OSL_ENSURE( m_xControlModel
.is(), "CellBindingHelper::CellBindingHelper: invalid control model!" );
83 m_xDocument
.set(_rxContextDocument
, css::uno::UNO_QUERY
);
84 OSL_ENSURE( m_xDocument
.is(), "CellBindingHelper::CellBindingHelper: This is no spreadsheet document!" );
86 OSL_ENSURE( isSpreadsheetDocumentWhichSupplies( SERVICE_ADDRESS_CONVERSION
),
87 "CellBindingHelper::CellBindingHelper: the document cannot convert address representations!" );
91 bool CellBindingHelper::isSpreadsheetDocument( const Reference
< XModel
>& _rxContextDocument
)
93 return Reference
< XSpreadsheetDocument
>::query( _rxContextDocument
).is();
97 sal_Int16
CellBindingHelper::getControlSheetIndex( Reference
< XSpreadsheet
>& _out_rxSheet
) const
99 sal_Int16 nSheetIndex
= -1;
100 // every sheet has a draw page, and every draw page has a forms collection.
101 // Our control, OTOH, belongs to a forms collection. Match these...
104 // for determining the draw page, we need the forms collection which
105 // the object belongs to. This is the first object up the hierarchy which is
106 // *no* XForm (and, well, no XGridColumnFactory)
107 Reference
< XChild
> xCheck( m_xControlModel
, UNO_QUERY
);
108 Reference
< XForm
> xParentAsForm
; if ( xCheck
.is() ) xParentAsForm
.set(xCheck
->getParent(), css::uno::UNO_QUERY
);
109 Reference
< XGridColumnFactory
> xParentAsGrid
; if ( xCheck
.is() ) xParentAsGrid
.set(xCheck
->getParent(), css::uno::UNO_QUERY
);
111 while ( ( xParentAsForm
.is() || xParentAsGrid
.is() ) && xCheck
.is() )
113 xCheck
.set(xCheck
->getParent(), css::uno::UNO_QUERY
);
114 xParentAsForm
.set(xCheck
.is() ? xCheck
->getParent() : Reference
< XForm
>(), css::uno::UNO_QUERY
);
115 xParentAsGrid
.set(xCheck
.is() ? xCheck
->getParent() : Reference
< XGridColumnFactory
>(), css::uno::UNO_QUERY
);
117 Reference
< XInterface
> xFormsCollection( xCheck
.is() ? xCheck
->getParent() : Reference
< XInterface
>() );
119 // now iterate through the sheets
120 Reference
< XIndexAccess
> xSheets( m_xDocument
->getSheets(), UNO_QUERY
);
121 if ( xSheets
.is() && xFormsCollection
.is() )
123 for ( sal_Int32 i
= 0; i
< xSheets
->getCount(); ++i
)
125 Reference
< XDrawPageSupplier
> xSuppPage( xSheets
->getByIndex( i
), UNO_QUERY_THROW
);
126 Reference
< XFormsSupplier
> xSuppForms( xSuppPage
->getDrawPage(), UNO_QUERY_THROW
);
128 if ( xSuppForms
->getForms() == xFormsCollection
)
130 nSheetIndex
= static_cast<sal_Int16
>(i
);
131 _out_rxSheet
.set( xSuppPage
, UNO_QUERY_THROW
);
137 catch( const Exception
& )
139 DBG_UNHANDLED_EXCEPTION("extensions.propctrlr");
146 bool CellBindingHelper::convertStringAddress( const OUString
& _rAddressDescription
, CellAddress
& /* [out] */ _rAddress
) const
149 return doConvertAddressRepresentations(
150 PROPERTY_UI_REPRESENTATION
,
151 makeAny( _rAddressDescription
),
156 && ( aAddress
>>= _rAddress
);
160 bool CellBindingHelper::doConvertAddressRepresentations( const OUString
& _rInputProperty
, const Any
& _rInputValue
,
161 const OUString
& _rOutputProperty
, Any
& _rOutputValue
, bool _bIsRange
) const
163 bool bSuccess
= false;
165 Reference
< XPropertySet
> xConverter(
166 createDocumentDependentInstance(
167 _bIsRange
? OUString(SERVICE_RANGEADDRESS_CONVERSION
) : OUString(SERVICE_ADDRESS_CONVERSION
),
173 OSL_ENSURE( xConverter
.is(), "CellBindingHelper::doConvertAddressRepresentations: could not get a converter service!" );
174 if ( xConverter
.is() )
178 Reference
< XSpreadsheet
> xSheet
;
179 xConverter
->setPropertyValue( PROPERTY_REFERENCE_SHEET
, makeAny( static_cast<sal_Int32
>(getControlSheetIndex( xSheet
)) ) );
180 xConverter
->setPropertyValue( _rInputProperty
, _rInputValue
);
181 _rOutputValue
= xConverter
->getPropertyValue( _rOutputProperty
);
184 catch( const Exception
& )
186 OSL_FAIL( "CellBindingHelper::doConvertAddressRepresentations: caught an exception!" );
194 bool CellBindingHelper::convertStringAddress( const OUString
& _rAddressDescription
,
195 CellRangeAddress
& /* [out] */ _rAddress
) const
198 return doConvertAddressRepresentations(
199 PROPERTY_UI_REPRESENTATION
,
200 makeAny( _rAddressDescription
),
205 && ( aAddress
>>= _rAddress
);
209 Reference
< XValueBinding
> CellBindingHelper::createCellBindingFromAddress( const CellAddress
& _rAddress
, bool _bSupportIntegerExchange
) const
211 Reference
< XValueBinding
> xBinding( createDocumentDependentInstance(
212 _bSupportIntegerExchange
? OUString(SERVICE_SHEET_CELL_INT_BINDING
) : OUString(SERVICE_SHEET_CELL_BINDING
),
221 Reference
< XValueBinding
> CellBindingHelper::createCellBindingFromStringAddress( const OUString
& _rAddress
, bool _bSupportIntegerExchange
) const
223 Reference
< XValueBinding
> xBinding
;
224 if ( !m_xDocument
.is() )
228 // get the UNO representation of the address
229 CellAddress aAddress
;
230 if ( _rAddress
.isEmpty() || !convertStringAddress( _rAddress
, aAddress
) )
233 return createCellBindingFromAddress( aAddress
, _bSupportIntegerExchange
);
237 Reference
< XListEntrySource
> CellBindingHelper::createCellListSourceFromStringAddress( const OUString
& _rAddress
) const
239 Reference
< XListEntrySource
> xSource
;
241 CellRangeAddress aRangeAddress
;
242 if ( _rAddress
.isEmpty() || !convertStringAddress( _rAddress
, aRangeAddress
) )
245 // create a range object for this address
246 xSource
.set(createDocumentDependentInstance(
247 SERVICE_SHEET_CELLRANGE_LISTSOURCE
,
248 PROPERTY_LIST_CELL_RANGE
,
249 makeAny( aRangeAddress
)
250 ), css::uno::UNO_QUERY
);
256 Reference
< XInterface
> CellBindingHelper::createDocumentDependentInstance( const OUString
& _rService
, const OUString
& _rArgumentName
,
257 const Any
& _rArgumentValue
) const
259 Reference
< XInterface
> xReturn
;
261 Reference
< XMultiServiceFactory
> xDocumentFactory( m_xDocument
, UNO_QUERY
);
262 OSL_ENSURE( xDocumentFactory
.is(), "CellBindingHelper::createDocumentDependentInstance: no document service factory!" );
263 if ( xDocumentFactory
.is() )
267 if ( !_rArgumentName
.isEmpty() )
270 aArg
.Name
= _rArgumentName
;
271 aArg
.Value
= _rArgumentValue
;
273 Sequence
< Any
> aArgs( 1 );
276 xReturn
= xDocumentFactory
->createInstanceWithArguments( _rService
, aArgs
);
280 xReturn
= xDocumentFactory
->createInstance( _rService
);
283 catch ( const Exception
& )
285 OSL_FAIL( "CellBindingHelper::createDocumentDependentInstance: could not create the binding at the document!" );
292 bool CellBindingHelper::getAddressFromCellBinding(
293 const Reference
< XValueBinding
>& _rxBinding
, CellAddress
& _rAddress
) const
295 OSL_PRECOND( !_rxBinding
.is() || isCellBinding( _rxBinding
), "CellBindingHelper::getAddressFromCellBinding: this is no cell binding!" );
297 bool bReturn
= false;
298 if ( !m_xDocument
.is() )
304 Reference
< XPropertySet
> xBindingProps( _rxBinding
, UNO_QUERY
);
305 OSL_ENSURE( xBindingProps
.is() || !_rxBinding
.is(), "CellBindingHelper::getAddressFromCellBinding: no property set for the binding!" );
306 if ( xBindingProps
.is() )
308 bReturn
= ( xBindingProps
->getPropertyValue( PROPERTY_BOUND_CELL
) >>= _rAddress
);
311 catch( const Exception
& )
313 OSL_FAIL( "CellBindingHelper::getAddressFromCellBinding: caught an exception!" );
320 OUString
CellBindingHelper::getStringAddressFromCellBinding( const Reference
< XValueBinding
>& _rxBinding
) const
322 CellAddress aAddress
;
324 if ( getAddressFromCellBinding( _rxBinding
, aAddress
) )
327 doConvertAddressRepresentations( PROPERTY_ADDRESS
, makeAny( aAddress
),
328 PROPERTY_UI_REPRESENTATION
, aStringAddress
, false );
330 aStringAddress
>>= sAddress
;
337 OUString
CellBindingHelper::getStringAddressFromCellListSource( const Reference
< XListEntrySource
>& _rxSource
) const
339 OSL_PRECOND( !_rxSource
.is() || isCellRangeListSource( _rxSource
), "CellBindingHelper::getStringAddressFromCellListSource: this is no cell list source!" );
342 if ( !m_xDocument
.is() )
348 Reference
< XPropertySet
> xSourceProps( _rxSource
, UNO_QUERY
);
349 OSL_ENSURE( xSourceProps
.is() || !_rxSource
.is(), "CellBindingHelper::getStringAddressFromCellListSource: no property set for the list source!" );
350 if ( xSourceProps
.is() )
352 CellRangeAddress aRangeAddress
;
353 xSourceProps
->getPropertyValue( PROPERTY_LIST_CELL_RANGE
) >>= aRangeAddress
;
356 doConvertAddressRepresentations( PROPERTY_ADDRESS
, makeAny( aRangeAddress
),
357 PROPERTY_UI_REPRESENTATION
, aStringAddress
, true );
358 aStringAddress
>>= sAddress
;
361 catch( const Exception
& )
363 OSL_FAIL( "CellBindingHelper::getStringAddressFromCellListSource: caught an exception!" );
370 bool CellBindingHelper::isSpreadsheetDocumentWhichSupplies( const OUString
& _rService
) const
372 bool bYesItIs
= false;
374 Reference
< XServiceInfo
> xSI( m_xDocument
, UNO_QUERY
);
375 if ( xSI
.is() && xSI
->supportsService( SERVICE_SPREADSHEET_DOCUMENT
) )
377 Reference
< XMultiServiceFactory
> xDocumentFactory( m_xDocument
, UNO_QUERY
);
378 OSL_ENSURE( xDocumentFactory
.is(), "CellBindingHelper::isSpreadsheetDocumentWhichSupplies: spreadsheet document, but no factory?" );
380 Sequence
< OUString
> aAvailableServices
;
381 if ( xDocumentFactory
.is() )
382 aAvailableServices
= xDocumentFactory
->getAvailableServiceNames( );
384 bYesItIs
= std::any_of(
385 aAvailableServices
.begin(),
386 aAvailableServices
.end(),
387 StringCompare( _rService
)
395 bool CellBindingHelper::isListCellRangeAllowed( ) const
397 bool bAllow( false );
399 Reference
< XListEntrySink
> xSink( m_xControlModel
, UNO_QUERY
);
402 bAllow
= isSpreadsheetDocumentWhichSupplies( SERVICE_SHEET_CELLRANGE_LISTSOURCE
);
409 bool CellBindingHelper::isCellIntegerBindingAllowed( ) const
413 // first, we only offer this for controls which allow bindings in general
414 Reference
< XBindableValue
> xBindable( m_xControlModel
, UNO_QUERY
);
415 if ( !xBindable
.is() )
418 // then, we must live in a spreadsheet document which can provide the special
419 // service needed for exchanging integer values
421 bAllow
= isSpreadsheetDocumentWhichSupplies( SERVICE_SHEET_CELL_INT_BINDING
);
423 // then, we only offer this for list boxes
428 sal_Int16 nClassId
= FormComponentType::CONTROL
;
429 m_xControlModel
->getPropertyValue( PROPERTY_CLASSID
) >>= nClassId
;
430 if ( FormComponentType::LISTBOX
!= nClassId
)
433 catch( const Exception
& )
435 OSL_FAIL( "CellBindingHelper::isCellIntegerBindingAllowed: caught an exception!" );
436 // are there really control models which survive isCellBindingAllowed, but don't have a ClassId
446 bool CellBindingHelper::isCellBindingAllowed( ) const
448 bool bAllow( false );
450 Reference
< XBindableValue
> xBindable( m_xControlModel
, UNO_QUERY
);
451 if ( xBindable
.is() )
453 // the control can potentially be bound to an external value
454 // Does it live within a Calc document, and is able to supply CellBindings?
455 bAllow
= isSpreadsheetDocumentWhichSupplies( SERVICE_SHEET_CELL_BINDING
);
458 // disallow for some types
459 // TODO: shouldn't the XBindableValue supply a list of supported types, and we can distinguish
460 // using this list? The current behavior below is somewhat hackish...
465 sal_Int16 nClassId
= FormComponentType::CONTROL
;
466 m_xControlModel
->getPropertyValue( PROPERTY_CLASSID
) >>= nClassId
;
467 if ( ( FormComponentType::DATEFIELD
== nClassId
) || ( FormComponentType::TIMEFIELD
== nClassId
) )
470 catch( const Exception
& )
472 OSL_FAIL( "CellBindingHelper::isCellBindingAllowed: caught an exception!" );
480 bool CellBindingHelper::isCellBinding( const Reference
< XValueBinding
>& _rxBinding
)
482 return doesComponentSupport( _rxBinding
.get(), SERVICE_SHEET_CELL_BINDING
);
486 bool CellBindingHelper::isCellIntegerBinding( const Reference
< XValueBinding
>& _rxBinding
)
488 return doesComponentSupport( _rxBinding
.get(), SERVICE_SHEET_CELL_INT_BINDING
);
492 bool CellBindingHelper::isCellRangeListSource( const Reference
< XListEntrySource
>& _rxSource
)
494 return doesComponentSupport( _rxSource
.get(), SERVICE_SHEET_CELLRANGE_LISTSOURCE
);
498 bool CellBindingHelper::doesComponentSupport( const Reference
< XInterface
>& _rxComponent
, const OUString
& _rService
)
500 Reference
< XServiceInfo
> xSI( _rxComponent
, UNO_QUERY
);
501 bool bDoes
= xSI
.is() && xSI
->supportsService( _rService
);
506 Reference
< XValueBinding
> CellBindingHelper::getCurrentBinding( ) const
508 Reference
< XValueBinding
> xBinding
;
509 Reference
< XBindableValue
> xBindable( m_xControlModel
, UNO_QUERY
);
510 if ( xBindable
.is() )
511 xBinding
= xBindable
->getValueBinding();
516 Reference
< XListEntrySource
> CellBindingHelper::getCurrentListSource( ) const
518 Reference
< XListEntrySource
> xSource
;
519 Reference
< XListEntrySink
> xSink( m_xControlModel
, UNO_QUERY
);
521 xSource
= xSink
->getListEntrySource();
526 void CellBindingHelper::setBinding( const Reference
< XValueBinding
>& _rxBinding
)
528 Reference
< XBindableValue
> xBindable( m_xControlModel
, UNO_QUERY
);
529 OSL_PRECOND( xBindable
.is(), "CellBindingHelper::setBinding: the object is not bindable!" );
530 if ( xBindable
.is() )
531 xBindable
->setValueBinding( _rxBinding
);
535 void CellBindingHelper::setListSource( const Reference
< XListEntrySource
>& _rxSource
)
537 Reference
< XListEntrySink
> xSink( m_xControlModel
, UNO_QUERY
);
538 OSL_PRECOND( xSink
.is(), "CellBindingHelper::setListSource: the object is no list entry sink!" );
540 xSink
->setListEntrySource( _rxSource
);
547 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */