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/drawing/XDrawPageSupplier.hpp>
27 #include <com/sun/star/form/XFormsSupplier.hpp>
28 #include <com/sun/star/form/XForm.hpp>
29 #include <com/sun/star/lang/XServiceInfo.hpp>
30 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
31 #include <com/sun/star/beans/NamedValue.hpp>
32 #include <com/sun/star/sheet/XSpreadsheet.hpp>
33 #include <unotools/transliterationwrapper.hxx>
34 #include <osl/diagnose.h>
35 #include <comphelper/diagnose_ex.hxx>
36 #include "formstrings.hxx"
46 using namespace ::com::sun::star::uno
;
47 using namespace ::com::sun::star::beans
;
48 using namespace ::com::sun::star::frame
;
49 using namespace ::com::sun::star::sheet
;
50 using namespace ::com::sun::star::container
;
51 using namespace ::com::sun::star::drawing
;
52 using namespace ::com::sun::star::table
;
53 using namespace ::com::sun::star::form
;
54 using namespace ::com::sun::star::lang
;
55 using namespace ::com::sun::star::form::binding
;
63 OUString m_sReference
;
66 explicit StringCompare( OUString _aReference
) : m_sReference(std::move( _aReference
)) { }
68 bool operator()( std::u16string_view _rCompare
)
70 return ( _rCompare
== m_sReference
);
76 CellBindingHelper::CellBindingHelper( const Reference
< XPropertySet
>& _rxControlModel
, const Reference
< XModel
>& _rxContextDocument
)
77 :m_xControlModel( _rxControlModel
)
79 OSL_ENSURE( m_xControlModel
.is(), "CellBindingHelper::CellBindingHelper: invalid control model!" );
81 m_xDocument
.set(_rxContextDocument
, css::uno::UNO_QUERY
);
82 OSL_ENSURE( m_xDocument
.is(), "CellBindingHelper::CellBindingHelper: This is no spreadsheet document!" );
84 OSL_ENSURE( isSpreadsheetDocumentWhichSupplies( SERVICE_ADDRESS_CONVERSION
),
85 "CellBindingHelper::CellBindingHelper: the document cannot convert address representations!" );
89 bool CellBindingHelper::isSpreadsheetDocument( const Reference
< XModel
>& _rxContextDocument
)
91 return Reference
< XSpreadsheetDocument
>::query( _rxContextDocument
).is();
95 sal_Int16
CellBindingHelper::getControlSheetIndex( Reference
< XSpreadsheet
>& _out_rxSheet
) const
97 sal_Int16 nSheetIndex
= -1;
98 // every sheet has a draw page, and every draw page has a forms collection.
99 // Our control, OTOH, belongs to a forms collection. Match these...
102 // for determining the draw page, we need the forms collection which
103 // the object belongs to. This is the first object up the hierarchy which is
104 // *no* XForm (and, well, no XGridColumnFactory)
105 Reference
< XChild
> xCheck( m_xControlModel
, UNO_QUERY
);
106 Reference
< XForm
> xParentAsForm
; if ( xCheck
.is() ) xParentAsForm
.set(xCheck
->getParent(), css::uno::UNO_QUERY
);
107 Reference
< XGridColumnFactory
> xParentAsGrid
; if ( xCheck
.is() ) xParentAsGrid
.set(xCheck
->getParent(), css::uno::UNO_QUERY
);
109 while ( ( xParentAsForm
.is() || xParentAsGrid
.is() ) && xCheck
.is() )
111 xCheck
.set(xCheck
->getParent(), css::uno::UNO_QUERY
);
112 xParentAsForm
.set(xCheck
.is() ? xCheck
->getParent() : Reference
< XForm
>(), css::uno::UNO_QUERY
);
113 xParentAsGrid
.set(xCheck
.is() ? xCheck
->getParent() : Reference
< XGridColumnFactory
>(), css::uno::UNO_QUERY
);
115 Reference
< XInterface
> xFormsCollection( xCheck
.is() ? xCheck
->getParent() : Reference
< XInterface
>() );
117 // now iterate through the sheets
118 Reference
< XIndexAccess
> xSheets( m_xDocument
->getSheets(), UNO_QUERY
);
119 if ( xSheets
.is() && xFormsCollection
.is() )
121 for ( sal_Int32 i
= 0; i
< xSheets
->getCount(); ++i
)
123 Reference
< XDrawPageSupplier
> xSuppPage( xSheets
->getByIndex( i
), UNO_QUERY_THROW
);
124 Reference
< XFormsSupplier
> xSuppForms( xSuppPage
->getDrawPage(), UNO_QUERY_THROW
);
126 if ( xSuppForms
->getForms() == xFormsCollection
)
128 nSheetIndex
= static_cast<sal_Int16
>(i
);
129 _out_rxSheet
.set( xSuppPage
, UNO_QUERY_THROW
);
135 catch( const Exception
& )
137 DBG_UNHANDLED_EXCEPTION("extensions.propctrlr");
144 bool CellBindingHelper::convertStringAddress( const OUString
& _rAddressDescription
, CellAddress
& /* [out] */ _rAddress
) const
147 return doConvertAddressRepresentations(
148 PROPERTY_UI_REPRESENTATION
,
149 Any( _rAddressDescription
),
154 && ( aAddress
>>= _rAddress
);
158 bool CellBindingHelper::doConvertAddressRepresentations( const OUString
& _rInputProperty
, const Any
& _rInputValue
,
159 const OUString
& _rOutputProperty
, Any
& _rOutputValue
, bool _bIsRange
) const
161 bool bSuccess
= false;
163 Reference
< XPropertySet
> xConverter(
164 createDocumentDependentInstance(
165 _bIsRange
? SERVICE_RANGEADDRESS_CONVERSION
: SERVICE_ADDRESS_CONVERSION
,
171 OSL_ENSURE( xConverter
.is(), "CellBindingHelper::doConvertAddressRepresentations: could not get a converter service!" );
172 if ( xConverter
.is() )
176 Reference
< XSpreadsheet
> xSheet
;
177 xConverter
->setPropertyValue( PROPERTY_REFERENCE_SHEET
, Any( static_cast<sal_Int32
>(getControlSheetIndex( xSheet
)) ) );
178 xConverter
->setPropertyValue( _rInputProperty
, _rInputValue
);
179 _rOutputValue
= xConverter
->getPropertyValue( _rOutputProperty
);
182 catch( const Exception
& )
184 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "CellBindingHelper::doConvertAddressRepresentations" );
192 bool CellBindingHelper::convertStringAddress( const OUString
& _rAddressDescription
,
193 CellRangeAddress
& /* [out] */ _rAddress
) const
196 return doConvertAddressRepresentations(
197 PROPERTY_UI_REPRESENTATION
,
198 Any( _rAddressDescription
),
203 && ( aAddress
>>= _rAddress
);
207 Reference
< XValueBinding
> CellBindingHelper::createCellBindingFromAddress( const CellAddress
& _rAddress
, bool _bSupportIntegerExchange
) const
209 Reference
< XValueBinding
> xBinding( createDocumentDependentInstance(
210 _bSupportIntegerExchange
? SERVICE_SHEET_CELL_INT_BINDING
: SERVICE_SHEET_CELL_BINDING
,
219 Reference
< XValueBinding
> CellBindingHelper::createCellBindingFromStringAddress( const OUString
& _rAddress
, bool _bSupportIntegerExchange
) const
221 Reference
< XValueBinding
> xBinding
;
222 if ( !m_xDocument
.is() )
226 // get the UNO representation of the address
227 CellAddress aAddress
;
228 if ( _rAddress
.isEmpty() || !convertStringAddress( _rAddress
, aAddress
) )
231 return createCellBindingFromAddress( aAddress
, _bSupportIntegerExchange
);
235 Reference
< XListEntrySource
> CellBindingHelper::createCellListSourceFromStringAddress( const OUString
& _rAddress
) const
237 Reference
< XListEntrySource
> xSource
;
239 CellRangeAddress aRangeAddress
;
240 if ( _rAddress
.isEmpty() || !convertStringAddress( _rAddress
, aRangeAddress
) )
243 // create a range object for this address
244 xSource
.set(createDocumentDependentInstance(
245 SERVICE_SHEET_CELLRANGE_LISTSOURCE
,
246 PROPERTY_LIST_CELL_RANGE
,
248 ), css::uno::UNO_QUERY
);
254 Reference
< XInterface
> CellBindingHelper::createDocumentDependentInstance( const OUString
& _rService
, const OUString
& _rArgumentName
,
255 const Any
& _rArgumentValue
) const
257 Reference
< XInterface
> xReturn
;
259 Reference
< XMultiServiceFactory
> xDocumentFactory( m_xDocument
, UNO_QUERY
);
260 OSL_ENSURE( xDocumentFactory
.is(), "CellBindingHelper::createDocumentDependentInstance: no document service factory!" );
261 if ( xDocumentFactory
.is() )
265 if ( !_rArgumentName
.isEmpty() )
267 Sequence aArgs
{ Any(NamedValue(_rArgumentName
, _rArgumentValue
)) };
268 xReturn
= xDocumentFactory
->createInstanceWithArguments( _rService
, aArgs
);
272 xReturn
= xDocumentFactory
->createInstance( _rService
);
275 catch ( const Exception
& )
277 OSL_FAIL( "CellBindingHelper::createDocumentDependentInstance: could not create the binding at the document!" );
284 bool CellBindingHelper::getAddressFromCellBinding(
285 const Reference
< XValueBinding
>& _rxBinding
, CellAddress
& _rAddress
) const
287 OSL_PRECOND( !_rxBinding
.is() || isCellBinding( _rxBinding
), "CellBindingHelper::getAddressFromCellBinding: this is no cell binding!" );
289 bool bReturn
= false;
290 if ( !m_xDocument
.is() )
296 Reference
< XPropertySet
> xBindingProps( _rxBinding
, UNO_QUERY
);
297 OSL_ENSURE( xBindingProps
.is() || !_rxBinding
.is(), "CellBindingHelper::getAddressFromCellBinding: no property set for the binding!" );
298 if ( xBindingProps
.is() )
300 bReturn
= ( xBindingProps
->getPropertyValue( PROPERTY_BOUND_CELL
) >>= _rAddress
);
303 catch( const Exception
& )
305 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "CellBindingHelper::getAddressFromCellBinding" );
312 OUString
CellBindingHelper::getStringAddressFromCellBinding( const Reference
< XValueBinding
>& _rxBinding
) const
314 CellAddress aAddress
;
316 if ( getAddressFromCellBinding( _rxBinding
, aAddress
) )
319 doConvertAddressRepresentations( PROPERTY_ADDRESS
, Any( aAddress
),
320 PROPERTY_UI_REPRESENTATION
, aStringAddress
, false );
322 aStringAddress
>>= sAddress
;
329 OUString
CellBindingHelper::getStringAddressFromCellListSource( const Reference
< XListEntrySource
>& _rxSource
) const
331 OSL_PRECOND( !_rxSource
.is() || isCellRangeListSource( _rxSource
), "CellBindingHelper::getStringAddressFromCellListSource: this is no cell list source!" );
334 if ( !m_xDocument
.is() )
340 Reference
< XPropertySet
> xSourceProps( _rxSource
, UNO_QUERY
);
341 OSL_ENSURE( xSourceProps
.is() || !_rxSource
.is(), "CellBindingHelper::getStringAddressFromCellListSource: no property set for the list source!" );
342 if ( xSourceProps
.is() )
344 CellRangeAddress aRangeAddress
;
345 xSourceProps
->getPropertyValue( PROPERTY_LIST_CELL_RANGE
) >>= aRangeAddress
;
348 doConvertAddressRepresentations( PROPERTY_ADDRESS
, Any( aRangeAddress
),
349 PROPERTY_UI_REPRESENTATION
, aStringAddress
, true );
350 aStringAddress
>>= sAddress
;
353 catch( const Exception
& )
355 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "CellBindingHelper::getStringAddressFromCellListSource" );
362 bool CellBindingHelper::isSpreadsheetDocumentWhichSupplies( const OUString
& _rService
) const
364 bool bYesItIs
= false;
366 Reference
< XServiceInfo
> xSI( m_xDocument
, UNO_QUERY
);
367 if ( xSI
.is() && xSI
->supportsService( SERVICE_SPREADSHEET_DOCUMENT
) )
369 Reference
< XMultiServiceFactory
> xDocumentFactory( m_xDocument
, UNO_QUERY
);
370 OSL_ENSURE( xDocumentFactory
.is(), "CellBindingHelper::isSpreadsheetDocumentWhichSupplies: spreadsheet document, but no factory?" );
372 if ( xDocumentFactory
.is() )
374 const Sequence
<OUString
> aAvailableServices
= xDocumentFactory
->getAvailableServiceNames( );
376 bYesItIs
= std::any_of(
377 aAvailableServices
.begin(),
378 aAvailableServices
.end(),
379 StringCompare( _rService
)
388 bool CellBindingHelper::isListCellRangeAllowed( ) const
390 bool bAllow( false );
392 Reference
< XListEntrySink
> xSink( m_xControlModel
, UNO_QUERY
);
395 bAllow
= isSpreadsheetDocumentWhichSupplies( SERVICE_SHEET_CELLRANGE_LISTSOURCE
);
402 bool CellBindingHelper::isCellIntegerBindingAllowed( ) const
406 // first, we only offer this for controls which allow bindings in general
407 Reference
< XBindableValue
> xBindable( m_xControlModel
, UNO_QUERY
);
408 if ( !xBindable
.is() )
411 // then, we must live in a spreadsheet document which can provide the special
412 // service needed for exchanging integer values
414 bAllow
= isSpreadsheetDocumentWhichSupplies( SERVICE_SHEET_CELL_INT_BINDING
);
416 // then, we only offer this for list boxes
421 sal_Int16 nClassId
= FormComponentType::CONTROL
;
422 m_xControlModel
->getPropertyValue( PROPERTY_CLASSID
) >>= nClassId
;
423 if ( FormComponentType::LISTBOX
!= nClassId
)
426 catch( const Exception
& )
428 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "CellBindingHelper::isCellIntegerBindingAllowed" );
429 // are there really control models which survive isCellBindingAllowed, but don't have a ClassId
439 bool CellBindingHelper::isCellBindingAllowed( ) const
441 bool bAllow( false );
443 Reference
< XBindableValue
> xBindable( m_xControlModel
, UNO_QUERY
);
444 if ( xBindable
.is() )
446 // the control can potentially be bound to an external value
447 // Does it live within a Calc document, and is able to supply CellBindings?
448 bAllow
= isSpreadsheetDocumentWhichSupplies( SERVICE_SHEET_CELL_BINDING
);
451 // disallow for some types
452 // TODO: shouldn't the XBindableValue supply a list of supported types, and we can distinguish
453 // using this list? The current behavior below is somewhat hackish...
458 sal_Int16 nClassId
= FormComponentType::CONTROL
;
459 m_xControlModel
->getPropertyValue( PROPERTY_CLASSID
) >>= nClassId
;
460 if ( ( FormComponentType::DATEFIELD
== nClassId
) || ( FormComponentType::TIMEFIELD
== nClassId
) )
463 catch( const Exception
& )
465 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "CellBindingHelper::isCellBindingAllowed" );
473 bool CellBindingHelper::isCellBinding( const Reference
< XValueBinding
>& _rxBinding
)
475 return doesComponentSupport( _rxBinding
, SERVICE_SHEET_CELL_BINDING
);
479 bool CellBindingHelper::isCellIntegerBinding( const Reference
< XValueBinding
>& _rxBinding
)
481 return doesComponentSupport( _rxBinding
, SERVICE_SHEET_CELL_INT_BINDING
);
485 bool CellBindingHelper::isCellRangeListSource( const Reference
< XListEntrySource
>& _rxSource
)
487 return doesComponentSupport( _rxSource
, SERVICE_SHEET_CELLRANGE_LISTSOURCE
);
491 bool CellBindingHelper::doesComponentSupport( const Reference
< XInterface
>& _rxComponent
, const OUString
& _rService
)
493 Reference
< XServiceInfo
> xSI( _rxComponent
, UNO_QUERY
);
494 bool bDoes
= xSI
.is() && xSI
->supportsService( _rService
);
498 Reference
< XValueBinding
> CellBindingHelper::getCurrentBinding( ) const
500 Reference
< XValueBinding
> xBinding
;
501 Reference
< XBindableValue
> xBindable( m_xControlModel
, UNO_QUERY
);
502 if ( xBindable
.is() )
503 xBinding
= xBindable
->getValueBinding();
507 Reference
< XListEntrySource
> CellBindingHelper::getCurrentListSource( ) const
509 Reference
< XListEntrySink
> xSink( m_xControlModel
, UNO_QUERY
);
511 return xSink
->getListEntrySource();
512 return Reference
<XListEntrySource
>();
515 void CellBindingHelper::setBinding( const Reference
< XValueBinding
>& _rxBinding
)
517 Reference
< XBindableValue
> xBindable( m_xControlModel
, UNO_QUERY
);
518 OSL_PRECOND( xBindable
.is(), "CellBindingHelper::setBinding: the object is not bindable!" );
519 if ( xBindable
.is() )
520 xBindable
->setValueBinding( _rxBinding
);
523 void CellBindingHelper::setListSource( const Reference
< XListEntrySource
>& _rxSource
)
525 Reference
< XListEntrySink
> xSink( m_xControlModel
, UNO_QUERY
);
526 OSL_PRECOND( xSink
.is(), "CellBindingHelper::setListSource: the object is no list entry sink!" );
528 xSink
->setListEntrySource( _rxSource
);
534 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */