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::i18n
;
56 using namespace ::com::sun::star::form::binding
;
64 OUString m_sReference
;
67 explicit StringCompare( OUString _aReference
) : m_sReference(std::move( _aReference
)) { }
69 bool operator()( std::u16string_view _rCompare
)
71 return ( _rCompare
== m_sReference
);
77 CellBindingHelper::CellBindingHelper( const Reference
< XPropertySet
>& _rxControlModel
, const Reference
< XModel
>& _rxContextDocument
)
78 :m_xControlModel( _rxControlModel
)
80 OSL_ENSURE( m_xControlModel
.is(), "CellBindingHelper::CellBindingHelper: invalid control model!" );
82 m_xDocument
.set(_rxContextDocument
, css::uno::UNO_QUERY
);
83 OSL_ENSURE( m_xDocument
.is(), "CellBindingHelper::CellBindingHelper: This is no spreadsheet document!" );
85 OSL_ENSURE( isSpreadsheetDocumentWhichSupplies( SERVICE_ADDRESS_CONVERSION
),
86 "CellBindingHelper::CellBindingHelper: the document cannot convert address representations!" );
90 bool CellBindingHelper::isSpreadsheetDocument( const Reference
< XModel
>& _rxContextDocument
)
92 return Reference
< XSpreadsheetDocument
>::query( _rxContextDocument
).is();
96 sal_Int16
CellBindingHelper::getControlSheetIndex( Reference
< XSpreadsheet
>& _out_rxSheet
) const
98 sal_Int16 nSheetIndex
= -1;
99 // every sheet has a draw page, and every draw page has a forms collection.
100 // Our control, OTOH, belongs to a forms collection. Match these...
103 // for determining the draw page, we need the forms collection which
104 // the object belongs to. This is the first object up the hierarchy which is
105 // *no* XForm (and, well, no XGridColumnFactory)
106 Reference
< XChild
> xCheck( m_xControlModel
, UNO_QUERY
);
107 Reference
< XForm
> xParentAsForm
; if ( xCheck
.is() ) xParentAsForm
.set(xCheck
->getParent(), css::uno::UNO_QUERY
);
108 Reference
< XGridColumnFactory
> xParentAsGrid
; if ( xCheck
.is() ) xParentAsGrid
.set(xCheck
->getParent(), css::uno::UNO_QUERY
);
110 while ( ( xParentAsForm
.is() || xParentAsGrid
.is() ) && xCheck
.is() )
112 xCheck
.set(xCheck
->getParent(), css::uno::UNO_QUERY
);
113 xParentAsForm
.set(xCheck
.is() ? xCheck
->getParent() : Reference
< XForm
>(), css::uno::UNO_QUERY
);
114 xParentAsGrid
.set(xCheck
.is() ? xCheck
->getParent() : Reference
< XGridColumnFactory
>(), css::uno::UNO_QUERY
);
116 Reference
< XInterface
> xFormsCollection( xCheck
.is() ? xCheck
->getParent() : Reference
< XInterface
>() );
118 // now iterate through the sheets
119 Reference
< XIndexAccess
> xSheets( m_xDocument
->getSheets(), UNO_QUERY
);
120 if ( xSheets
.is() && xFormsCollection
.is() )
122 for ( sal_Int32 i
= 0; i
< xSheets
->getCount(); ++i
)
124 Reference
< XDrawPageSupplier
> xSuppPage( xSheets
->getByIndex( i
), UNO_QUERY_THROW
);
125 Reference
< XFormsSupplier
> xSuppForms( xSuppPage
->getDrawPage(), UNO_QUERY_THROW
);
127 if ( xSuppForms
->getForms() == xFormsCollection
)
129 nSheetIndex
= static_cast<sal_Int16
>(i
);
130 _out_rxSheet
.set( xSuppPage
, UNO_QUERY_THROW
);
136 catch( const Exception
& )
138 DBG_UNHANDLED_EXCEPTION("extensions.propctrlr");
145 bool CellBindingHelper::convertStringAddress( const OUString
& _rAddressDescription
, CellAddress
& /* [out] */ _rAddress
) const
148 return doConvertAddressRepresentations(
149 PROPERTY_UI_REPRESENTATION
,
150 Any( _rAddressDescription
),
155 && ( aAddress
>>= _rAddress
);
159 bool CellBindingHelper::doConvertAddressRepresentations( const OUString
& _rInputProperty
, const Any
& _rInputValue
,
160 const OUString
& _rOutputProperty
, Any
& _rOutputValue
, bool _bIsRange
) const
162 bool bSuccess
= false;
164 Reference
< XPropertySet
> xConverter(
165 createDocumentDependentInstance(
166 _bIsRange
? SERVICE_RANGEADDRESS_CONVERSION
: SERVICE_ADDRESS_CONVERSION
,
172 OSL_ENSURE( xConverter
.is(), "CellBindingHelper::doConvertAddressRepresentations: could not get a converter service!" );
173 if ( xConverter
.is() )
177 Reference
< XSpreadsheet
> xSheet
;
178 xConverter
->setPropertyValue( PROPERTY_REFERENCE_SHEET
, Any( static_cast<sal_Int32
>(getControlSheetIndex( xSheet
)) ) );
179 xConverter
->setPropertyValue( _rInputProperty
, _rInputValue
);
180 _rOutputValue
= xConverter
->getPropertyValue( _rOutputProperty
);
183 catch( const Exception
& )
185 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "CellBindingHelper::doConvertAddressRepresentations" );
193 bool CellBindingHelper::convertStringAddress( const OUString
& _rAddressDescription
,
194 CellRangeAddress
& /* [out] */ _rAddress
) const
197 return doConvertAddressRepresentations(
198 PROPERTY_UI_REPRESENTATION
,
199 Any( _rAddressDescription
),
204 && ( aAddress
>>= _rAddress
);
208 Reference
< XValueBinding
> CellBindingHelper::createCellBindingFromAddress( const CellAddress
& _rAddress
, bool _bSupportIntegerExchange
) const
210 Reference
< XValueBinding
> xBinding( createDocumentDependentInstance(
211 _bSupportIntegerExchange
? SERVICE_SHEET_CELL_INT_BINDING
: SERVICE_SHEET_CELL_BINDING
,
220 Reference
< XValueBinding
> CellBindingHelper::createCellBindingFromStringAddress( const OUString
& _rAddress
, bool _bSupportIntegerExchange
) const
222 Reference
< XValueBinding
> xBinding
;
223 if ( !m_xDocument
.is() )
227 // get the UNO representation of the address
228 CellAddress aAddress
;
229 if ( _rAddress
.isEmpty() || !convertStringAddress( _rAddress
, aAddress
) )
232 return createCellBindingFromAddress( aAddress
, _bSupportIntegerExchange
);
236 Reference
< XListEntrySource
> CellBindingHelper::createCellListSourceFromStringAddress( const OUString
& _rAddress
) const
238 Reference
< XListEntrySource
> xSource
;
240 CellRangeAddress aRangeAddress
;
241 if ( _rAddress
.isEmpty() || !convertStringAddress( _rAddress
, aRangeAddress
) )
244 // create a range object for this address
245 xSource
.set(createDocumentDependentInstance(
246 SERVICE_SHEET_CELLRANGE_LISTSOURCE
,
247 PROPERTY_LIST_CELL_RANGE
,
249 ), css::uno::UNO_QUERY
);
255 Reference
< XInterface
> CellBindingHelper::createDocumentDependentInstance( const OUString
& _rService
, const OUString
& _rArgumentName
,
256 const Any
& _rArgumentValue
) const
258 Reference
< XInterface
> xReturn
;
260 Reference
< XMultiServiceFactory
> xDocumentFactory( m_xDocument
, UNO_QUERY
);
261 OSL_ENSURE( xDocumentFactory
.is(), "CellBindingHelper::createDocumentDependentInstance: no document service factory!" );
262 if ( xDocumentFactory
.is() )
266 if ( !_rArgumentName
.isEmpty() )
268 Sequence aArgs
{ Any(NamedValue(_rArgumentName
, _rArgumentValue
)) };
269 xReturn
= xDocumentFactory
->createInstanceWithArguments( _rService
, aArgs
);
273 xReturn
= xDocumentFactory
->createInstance( _rService
);
276 catch ( const Exception
& )
278 OSL_FAIL( "CellBindingHelper::createDocumentDependentInstance: could not create the binding at the document!" );
285 bool CellBindingHelper::getAddressFromCellBinding(
286 const Reference
< XValueBinding
>& _rxBinding
, CellAddress
& _rAddress
) const
288 OSL_PRECOND( !_rxBinding
.is() || isCellBinding( _rxBinding
), "CellBindingHelper::getAddressFromCellBinding: this is no cell binding!" );
290 bool bReturn
= false;
291 if ( !m_xDocument
.is() )
297 Reference
< XPropertySet
> xBindingProps( _rxBinding
, UNO_QUERY
);
298 OSL_ENSURE( xBindingProps
.is() || !_rxBinding
.is(), "CellBindingHelper::getAddressFromCellBinding: no property set for the binding!" );
299 if ( xBindingProps
.is() )
301 bReturn
= ( xBindingProps
->getPropertyValue( PROPERTY_BOUND_CELL
) >>= _rAddress
);
304 catch( const Exception
& )
306 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "CellBindingHelper::getAddressFromCellBinding" );
313 OUString
CellBindingHelper::getStringAddressFromCellBinding( const Reference
< XValueBinding
>& _rxBinding
) const
315 CellAddress aAddress
;
317 if ( getAddressFromCellBinding( _rxBinding
, aAddress
) )
320 doConvertAddressRepresentations( PROPERTY_ADDRESS
, Any( aAddress
),
321 PROPERTY_UI_REPRESENTATION
, aStringAddress
, false );
323 aStringAddress
>>= sAddress
;
330 OUString
CellBindingHelper::getStringAddressFromCellListSource( const Reference
< XListEntrySource
>& _rxSource
) const
332 OSL_PRECOND( !_rxSource
.is() || isCellRangeListSource( _rxSource
), "CellBindingHelper::getStringAddressFromCellListSource: this is no cell list source!" );
335 if ( !m_xDocument
.is() )
341 Reference
< XPropertySet
> xSourceProps( _rxSource
, UNO_QUERY
);
342 OSL_ENSURE( xSourceProps
.is() || !_rxSource
.is(), "CellBindingHelper::getStringAddressFromCellListSource: no property set for the list source!" );
343 if ( xSourceProps
.is() )
345 CellRangeAddress aRangeAddress
;
346 xSourceProps
->getPropertyValue( PROPERTY_LIST_CELL_RANGE
) >>= aRangeAddress
;
349 doConvertAddressRepresentations( PROPERTY_ADDRESS
, Any( aRangeAddress
),
350 PROPERTY_UI_REPRESENTATION
, aStringAddress
, true );
351 aStringAddress
>>= sAddress
;
354 catch( const Exception
& )
356 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "CellBindingHelper::getStringAddressFromCellListSource" );
363 bool CellBindingHelper::isSpreadsheetDocumentWhichSupplies( const OUString
& _rService
) const
365 bool bYesItIs
= false;
367 Reference
< XServiceInfo
> xSI( m_xDocument
, UNO_QUERY
);
368 if ( xSI
.is() && xSI
->supportsService( SERVICE_SPREADSHEET_DOCUMENT
) )
370 Reference
< XMultiServiceFactory
> xDocumentFactory( m_xDocument
, UNO_QUERY
);
371 OSL_ENSURE( xDocumentFactory
.is(), "CellBindingHelper::isSpreadsheetDocumentWhichSupplies: spreadsheet document, but no factory?" );
373 if ( xDocumentFactory
.is() )
375 const Sequence
<OUString
> aAvailableServices
= xDocumentFactory
->getAvailableServiceNames( );
377 bYesItIs
= std::any_of(
378 aAvailableServices
.begin(),
379 aAvailableServices
.end(),
380 StringCompare( _rService
)
389 bool CellBindingHelper::isListCellRangeAllowed( ) const
391 bool bAllow( false );
393 Reference
< XListEntrySink
> xSink( m_xControlModel
, UNO_QUERY
);
396 bAllow
= isSpreadsheetDocumentWhichSupplies( SERVICE_SHEET_CELLRANGE_LISTSOURCE
);
403 bool CellBindingHelper::isCellIntegerBindingAllowed( ) const
407 // first, we only offer this for controls which allow bindings in general
408 Reference
< XBindableValue
> xBindable( m_xControlModel
, UNO_QUERY
);
409 if ( !xBindable
.is() )
412 // then, we must live in a spreadsheet document which can provide the special
413 // service needed for exchanging integer values
415 bAllow
= isSpreadsheetDocumentWhichSupplies( SERVICE_SHEET_CELL_INT_BINDING
);
417 // then, we only offer this for list boxes
422 sal_Int16 nClassId
= FormComponentType::CONTROL
;
423 m_xControlModel
->getPropertyValue( PROPERTY_CLASSID
) >>= nClassId
;
424 if ( FormComponentType::LISTBOX
!= nClassId
)
427 catch( const Exception
& )
429 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "CellBindingHelper::isCellIntegerBindingAllowed" );
430 // are there really control models which survive isCellBindingAllowed, but don't have a ClassId
440 bool CellBindingHelper::isCellBindingAllowed( ) const
442 bool bAllow( false );
444 Reference
< XBindableValue
> xBindable( m_xControlModel
, UNO_QUERY
);
445 if ( xBindable
.is() )
447 // the control can potentially be bound to an external value
448 // Does it live within a Calc document, and is able to supply CellBindings?
449 bAllow
= isSpreadsheetDocumentWhichSupplies( SERVICE_SHEET_CELL_BINDING
);
452 // disallow for some types
453 // TODO: shouldn't the XBindableValue supply a list of supported types, and we can distinguish
454 // using this list? The current behavior below is somewhat hackish...
459 sal_Int16 nClassId
= FormComponentType::CONTROL
;
460 m_xControlModel
->getPropertyValue( PROPERTY_CLASSID
) >>= nClassId
;
461 if ( ( FormComponentType::DATEFIELD
== nClassId
) || ( FormComponentType::TIMEFIELD
== nClassId
) )
464 catch( const Exception
& )
466 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "CellBindingHelper::isCellBindingAllowed" );
474 bool CellBindingHelper::isCellBinding( const Reference
< XValueBinding
>& _rxBinding
)
476 return doesComponentSupport( _rxBinding
, SERVICE_SHEET_CELL_BINDING
);
480 bool CellBindingHelper::isCellIntegerBinding( const Reference
< XValueBinding
>& _rxBinding
)
482 return doesComponentSupport( _rxBinding
, SERVICE_SHEET_CELL_INT_BINDING
);
486 bool CellBindingHelper::isCellRangeListSource( const Reference
< XListEntrySource
>& _rxSource
)
488 return doesComponentSupport( _rxSource
, SERVICE_SHEET_CELLRANGE_LISTSOURCE
);
492 bool CellBindingHelper::doesComponentSupport( const Reference
< XInterface
>& _rxComponent
, const OUString
& _rService
)
494 Reference
< XServiceInfo
> xSI( _rxComponent
, UNO_QUERY
);
495 bool bDoes
= xSI
.is() && xSI
->supportsService( _rService
);
500 Reference
< XValueBinding
> CellBindingHelper::getCurrentBinding( ) const
502 Reference
< XValueBinding
> xBinding
;
503 Reference
< XBindableValue
> xBindable( m_xControlModel
, UNO_QUERY
);
504 if ( xBindable
.is() )
505 xBinding
= xBindable
->getValueBinding();
510 Reference
< XListEntrySource
> CellBindingHelper::getCurrentListSource( ) const
512 Reference
< XListEntrySource
> xSource
;
513 Reference
< XListEntrySink
> xSink( m_xControlModel
, UNO_QUERY
);
515 xSource
= xSink
->getListEntrySource();
520 void CellBindingHelper::setBinding( const Reference
< XValueBinding
>& _rxBinding
)
522 Reference
< XBindableValue
> xBindable( m_xControlModel
, UNO_QUERY
);
523 OSL_PRECOND( xBindable
.is(), "CellBindingHelper::setBinding: the object is not bindable!" );
524 if ( xBindable
.is() )
525 xBindable
->setValueBinding( _rxBinding
);
529 void CellBindingHelper::setListSource( const Reference
< XListEntrySource
>& _rxSource
)
531 Reference
< XListEntrySink
> xSink( m_xControlModel
, UNO_QUERY
);
532 OSL_PRECOND( xSink
.is(), "CellBindingHelper::setListSource: the object is no list entry sink!" );
534 xSink
->setListEntrySource( _rxSource
);
541 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */