Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / extensions / source / propctrlr / cellbindinghelper.cxx
blob7942a4200563f8a5c7157bbee84513f7f96fb752
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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"
40 #include <algorithm>
43 namespace pcr
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;
59 namespace
62 struct StringCompare
64 private:
65 OUString m_sReference;
67 public:
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 )
129 { // found it
130 nSheetIndex = static_cast<sal_Int16>(i);
131 _out_rxSheet.set( xSuppPage, UNO_QUERY_THROW );
132 break;
137 catch( const Exception& )
139 DBG_UNHANDLED_EXCEPTION("extensions.propctrlr");
142 return nSheetIndex;
146 bool CellBindingHelper::convertStringAddress( const OUString& _rAddressDescription, CellAddress& /* [out] */ _rAddress ) const
148 Any aAddress;
149 return doConvertAddressRepresentations(
150 PROPERTY_UI_REPRESENTATION,
151 makeAny( _rAddressDescription ),
152 PROPERTY_ADDRESS,
153 aAddress,
154 false
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),
168 OUString(),
169 Any()
171 UNO_QUERY
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 );
182 bSuccess = true;
184 catch( const Exception& )
186 OSL_FAIL( "CellBindingHelper::doConvertAddressRepresentations: caught an exception!" );
190 return bSuccess;
194 bool CellBindingHelper::convertStringAddress( const OUString& _rAddressDescription,
195 CellRangeAddress& /* [out] */ _rAddress ) const
197 Any aAddress;
198 return doConvertAddressRepresentations(
199 PROPERTY_UI_REPRESENTATION,
200 makeAny( _rAddressDescription ),
201 PROPERTY_ADDRESS,
202 aAddress,
203 true
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),
213 PROPERTY_BOUND_CELL,
214 makeAny( _rAddress )
215 ), UNO_QUERY );
217 return xBinding;
221 Reference< XValueBinding > CellBindingHelper::createCellBindingFromStringAddress( const OUString& _rAddress, bool _bSupportIntegerExchange ) const
223 Reference< XValueBinding > xBinding;
224 if ( !m_xDocument.is() )
225 // very bad ...
226 return xBinding;
228 // get the UNO representation of the address
229 CellAddress aAddress;
230 if ( _rAddress.isEmpty() || !convertStringAddress( _rAddress, aAddress ) )
231 return xBinding;
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 ) )
243 return xSource;
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);
252 return xSource;
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() )
269 NamedValue aArg;
270 aArg.Name = _rArgumentName;
271 aArg.Value = _rArgumentValue;
273 Sequence< Any > aArgs( 1 );
274 aArgs[ 0 ] <<= aArg;
276 xReturn = xDocumentFactory->createInstanceWithArguments( _rService, aArgs );
278 else
280 xReturn = xDocumentFactory->createInstance( _rService );
283 catch ( const Exception& )
285 OSL_FAIL( "CellBindingHelper::createDocumentDependentInstance: could not create the binding at the document!" );
288 return xReturn;
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() )
299 // very bad ...
300 return bReturn;
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!" );
316 return bReturn;
320 OUString CellBindingHelper::getStringAddressFromCellBinding( const Reference< XValueBinding >& _rxBinding ) const
322 CellAddress aAddress;
323 OUString sAddress;
324 if ( getAddressFromCellBinding( _rxBinding, aAddress ) )
326 Any aStringAddress;
327 doConvertAddressRepresentations( PROPERTY_ADDRESS, makeAny( aAddress ),
328 PROPERTY_UI_REPRESENTATION, aStringAddress, false );
330 aStringAddress >>= sAddress;
333 return 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!" );
341 OUString sAddress;
342 if ( !m_xDocument.is() )
343 // very bad ...
344 return sAddress;
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;
355 Any aStringAddress;
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!" );
366 return sAddress;
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 )
391 return bYesItIs;
395 bool CellBindingHelper::isListCellRangeAllowed( ) const
397 bool bAllow( false );
399 Reference< XListEntrySink > xSink( m_xControlModel, UNO_QUERY );
400 if ( xSink.is() )
402 bAllow = isSpreadsheetDocumentWhichSupplies( SERVICE_SHEET_CELLRANGE_LISTSOURCE );
405 return bAllow;
409 bool CellBindingHelper::isCellIntegerBindingAllowed( ) const
411 bool bAllow( true );
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() )
416 bAllow = false;
418 // then, we must live in a spreadsheet document which can provide the special
419 // service needed for exchanging integer values
420 if ( bAllow )
421 bAllow = isSpreadsheetDocumentWhichSupplies( SERVICE_SHEET_CELL_INT_BINDING );
423 // then, we only offer this for list boxes
424 if ( bAllow )
428 sal_Int16 nClassId = FormComponentType::CONTROL;
429 m_xControlModel->getPropertyValue( PROPERTY_CLASSID ) >>= nClassId;
430 if ( FormComponentType::LISTBOX != nClassId )
431 bAllow = false;
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
437 // property?
438 bAllow = false;
442 return bAllow;
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...
461 if ( bAllow )
465 sal_Int16 nClassId = FormComponentType::CONTROL;
466 m_xControlModel->getPropertyValue( PROPERTY_CLASSID ) >>= nClassId;
467 if ( ( FormComponentType::DATEFIELD == nClassId ) || ( FormComponentType::TIMEFIELD == nClassId ) )
468 bAllow = false;
470 catch( const Exception& )
472 OSL_FAIL( "CellBindingHelper::isCellBindingAllowed: caught an exception!" );
473 bAllow = false;
476 return bAllow;
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 );
502 return bDoes;
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();
512 return xBinding;
516 Reference< XListEntrySource > CellBindingHelper::getCurrentListSource( ) const
518 Reference< XListEntrySource > xSource;
519 Reference< XListEntrySink > xSink( m_xControlModel, UNO_QUERY );
520 if ( xSink.is() )
521 xSource = xSink->getListEntrySource();
522 return xSource;
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!" );
539 if ( xSink.is() )
540 xSink->setListEntrySource( _rxSource );
544 } // namespace pcr
547 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */