1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include "cellbindinghelper.hxx"
30 #include <com/sun/star/form/binding/XBindableValue.hpp>
31 #include <com/sun/star/form/binding/XListEntrySink.hpp>
32 #include <com/sun/star/form/FormComponentType.hpp>
33 #include <com/sun/star/form/XGridColumnFactory.hpp>
34 #include <com/sun/star/container/XChild.hpp>
35 #include <com/sun/star/container/XNamed.hpp>
36 #include <com/sun/star/drawing/XDrawPageSupplier.hpp>
37 #include <com/sun/star/table/XCellRange.hpp>
38 #include <com/sun/star/form/XFormsSupplier.hpp>
39 #include <com/sun/star/form/XForm.hpp>
40 #include <com/sun/star/lang/XServiceInfo.hpp>
41 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
42 #include <com/sun/star/beans/NamedValue.hpp>
43 #include <com/sun/star/sheet/XSpreadsheet.hpp>
44 #include <unotools/transliterationwrapper.hxx>
45 #include <osl/diagnose.h>
46 #include <tools/diagnose_ex.h>
47 #include "formstrings.hxx"
52 //............................................................................
55 //............................................................................
57 using namespace ::com::sun::star::uno
;
58 using namespace ::com::sun::star::beans
;
59 using namespace ::com::sun::star::frame
;
60 using namespace ::com::sun::star::sheet
;
61 using namespace ::com::sun::star::container
;
62 using namespace ::com::sun::star::drawing
;
63 using namespace ::com::sun::star::table
;
64 using namespace ::com::sun::star::form
;
65 using namespace ::com::sun::star::lang
;
66 using namespace ::com::sun::star::i18n
;
67 using namespace ::com::sun::star::form::binding
;
71 //....................................................................
72 struct StringCompare
: public ::std::unary_function
< ::rtl::OUString
, bool >
75 ::rtl::OUString m_sReference
;
78 StringCompare( const ::rtl::OUString
& _rReference
) : m_sReference( _rReference
) { }
80 inline bool operator()( const ::rtl::OUString
& _rCompare
)
82 return ( _rCompare
== m_sReference
) ? true : false;
87 //========================================================================
89 //========================================================================
90 //------------------------------------------------------------------------
91 CellBindingHelper::CellBindingHelper( const Reference
< XPropertySet
>& _rxControlModel
, const Reference
< XModel
>& _rxContextDocument
)
92 :m_xControlModel( _rxControlModel
)
94 OSL_ENSURE( m_xControlModel
.is(), "CellBindingHelper::CellBindingHelper: invalid control model!" );
96 m_xDocument
= m_xDocument
.query( _rxContextDocument
);
97 OSL_ENSURE( m_xDocument
.is(), "CellBindingHelper::CellBindingHelper: This is no spreadsheet document!" );
99 OSL_ENSURE( isSpreadsheetDocumentWhichSupplies( SERVICE_ADDRESS_CONVERSION
),
100 "CellBindingHelper::CellBindingHelper: the document cannot convert address representations!" );
103 //------------------------------------------------------------------------
104 sal_Bool
CellBindingHelper::isSpreadsheetDocument( const Reference
< XModel
>& _rxContextDocument
)
106 return Reference
< XSpreadsheetDocument
>::query( _rxContextDocument
).is();
109 //------------------------------------------------------------------------
110 sal_Int16
CellBindingHelper::getControlSheetIndex( Reference
< XSpreadsheet
>& _out_rxSheet
) const
112 sal_Int16 nSheetIndex
= -1;
113 // every sheet has a draw page, and every draw page has a forms collection.
114 // Our control, OTOH, belongs to a forms collection. Match these ...
117 // for determining the draw page, we need the forms collection which
118 // the object belongs to. This is the first object up the hierarchy which is
119 // *no* XForm (and, well, no XGridColumnFactory)
120 Reference
< XChild
> xCheck( m_xControlModel
, UNO_QUERY
);
121 Reference
< XForm
> xParentAsForm
; if ( xCheck
.is() ) xParentAsForm
= xParentAsForm
.query( xCheck
->getParent() );
122 Reference
< XGridColumnFactory
> xParentAsGrid
; if ( xCheck
.is() ) xParentAsGrid
= xParentAsGrid
.query( xCheck
->getParent() );
124 while ( ( xParentAsForm
.is() || xParentAsGrid
.is() ) && xCheck
.is() )
126 xCheck
= xCheck
.query( xCheck
->getParent() );
127 xParentAsForm
= xParentAsForm
.query( xCheck
.is() ? xCheck
->getParent() : (Reference
< XInterface
>) Reference
< XForm
>() );
128 xParentAsGrid
= xParentAsGrid
.query( xCheck
.is() ? xCheck
->getParent() : (Reference
< XInterface
>) Reference
< XGridColumnFactory
>() );
130 Reference
< XInterface
> xFormsCollection( xCheck
.is() ? xCheck
->getParent() : Reference
< XInterface
>() );
132 // now iterate through the sheets
133 Reference
< XIndexAccess
> xSheets( m_xDocument
->getSheets(), UNO_QUERY
);
134 if ( xSheets
.is() && xFormsCollection
.is() )
136 for ( sal_Int32 i
= 0; i
< xSheets
->getCount(); ++i
)
138 Reference
< XDrawPageSupplier
> xSuppPage( xSheets
->getByIndex( i
), UNO_QUERY_THROW
);
139 Reference
< XFormsSupplier
> xSuppForms( xSuppPage
->getDrawPage(), UNO_QUERY_THROW
);
141 if ( xSuppForms
->getForms() == xFormsCollection
)
143 nSheetIndex
= (sal_Int16
)i
;
144 _out_rxSheet
.set( xSuppPage
, UNO_QUERY_THROW
);
150 catch( const Exception
& )
152 DBG_UNHANDLED_EXCEPTION();
158 //------------------------------------------------------------------------
159 bool CellBindingHelper::convertStringAddress( const ::rtl::OUString
& _rAddressDescription
, CellAddress
& /* [out] */ _rAddress
) const
162 return doConvertAddressRepresentations(
163 PROPERTY_UI_REPRESENTATION
,
164 makeAny( _rAddressDescription
),
169 && ( aAddress
>>= _rAddress
);
172 //------------------------------------------------------------------------
173 bool CellBindingHelper::doConvertAddressRepresentations( const ::rtl::OUString
& _rInputProperty
, const Any
& _rInputValue
,
174 const ::rtl::OUString
& _rOutputProperty
, Any
& _rOutputValue
, bool _bIsRange
) const SAL_THROW(())
176 bool bSuccess
= false;
178 Reference
< XPropertySet
> xConverter(
179 createDocumentDependentInstance(
180 _bIsRange
? SERVICE_RANGEADDRESS_CONVERSION
: SERVICE_ADDRESS_CONVERSION
,
186 OSL_ENSURE( xConverter
.is(), "CellBindingHelper::doConvertAddressRepresentations: could not get a converter service!" );
187 if ( xConverter
.is() )
191 Reference
< XSpreadsheet
> xSheet
;
192 xConverter
->setPropertyValue( PROPERTY_REFERENCE_SHEET
, makeAny( (sal_Int32
)getControlSheetIndex( xSheet
) ) );
193 xConverter
->setPropertyValue( _rInputProperty
, _rInputValue
);
194 _rOutputValue
= xConverter
->getPropertyValue( _rOutputProperty
);
197 catch( const Exception
& )
199 OSL_FAIL( "CellBindingHelper::doConvertAddressRepresentations: caught an exception!" );
206 //------------------------------------------------------------------------
207 bool CellBindingHelper::convertStringAddress( const ::rtl::OUString
& _rAddressDescription
,
208 CellRangeAddress
& /* [out] */ _rAddress
) const
211 return doConvertAddressRepresentations(
212 PROPERTY_UI_REPRESENTATION
,
213 makeAny( _rAddressDescription
),
218 && ( aAddress
>>= _rAddress
);
221 //------------------------------------------------------------------------
222 Reference
< XValueBinding
> CellBindingHelper::createCellBindingFromAddress( const CellAddress
& _rAddress
, bool _bSupportIntegerExchange
) const
224 Reference
< XValueBinding
> xBinding( createDocumentDependentInstance(
225 _bSupportIntegerExchange
? SERVICE_SHEET_CELL_INT_BINDING
: SERVICE_SHEET_CELL_BINDING
,
233 //------------------------------------------------------------------------
234 Reference
< XValueBinding
> CellBindingHelper::createCellBindingFromStringAddress( const ::rtl::OUString
& _rAddress
, bool _bSupportIntegerExchange
) const
236 Reference
< XValueBinding
> xBinding
;
237 if ( !m_xDocument
.is() )
241 // get the UNO representation of the address
242 CellAddress aAddress
;
243 if ( _rAddress
.isEmpty() || !convertStringAddress( _rAddress
, aAddress
) )
246 return createCellBindingFromAddress( aAddress
, _bSupportIntegerExchange
);
249 //------------------------------------------------------------------------
250 Reference
< XListEntrySource
> CellBindingHelper::createCellListSourceFromStringAddress( const ::rtl::OUString
& _rAddress
) const
252 Reference
< XListEntrySource
> xSource
;
254 CellRangeAddress aRangeAddress
;
255 if ( _rAddress
.isEmpty() || !convertStringAddress( _rAddress
, aRangeAddress
) )
258 // create a range object for this address
259 xSource
= xSource
.query( createDocumentDependentInstance(
260 SERVICE_SHEET_CELLRANGE_LISTSOURCE
,
261 PROPERTY_LIST_CELL_RANGE
,
262 makeAny( aRangeAddress
)
268 //------------------------------------------------------------------------
269 Reference
< XInterface
> CellBindingHelper::createDocumentDependentInstance( const ::rtl::OUString
& _rService
, const ::rtl::OUString
& _rArgumentName
,
270 const Any
& _rArgumentValue
) const
272 Reference
< XInterface
> xReturn
;
274 Reference
< XMultiServiceFactory
> xDocumentFactory( m_xDocument
, UNO_QUERY
);
275 OSL_ENSURE( xDocumentFactory
.is(), "CellBindingHelper::createDocumentDependentInstance: no document service factory!" );
276 if ( xDocumentFactory
.is() )
280 if ( !_rArgumentName
.isEmpty() )
283 aArg
.Name
= _rArgumentName
;
284 aArg
.Value
= _rArgumentValue
;
286 Sequence
< Any
> aArgs( 1 );
289 xReturn
= xDocumentFactory
->createInstanceWithArguments( _rService
, aArgs
);
293 xReturn
= xDocumentFactory
->createInstance( _rService
);
296 catch ( const Exception
& )
298 OSL_FAIL( "CellBindingHelper::createDocumentDependentInstance: could not create the binding at the document!" );
304 //------------------------------------------------------------------------
305 bool CellBindingHelper::getAddressFromCellBinding(
306 const Reference
< XValueBinding
>& _rxBinding
, CellAddress
& _rAddress
) const
308 OSL_PRECOND( !_rxBinding
.is() || isCellBinding( _rxBinding
), "CellBindingHelper::getAddressFromCellBinding: this is no cell binding!" );
310 bool bReturn
= false;
311 if ( !m_xDocument
.is() )
317 Reference
< XPropertySet
> xBindingProps( _rxBinding
, UNO_QUERY
);
318 OSL_ENSURE( xBindingProps
.is() || !_rxBinding
.is(), "CellBindingHelper::getAddressFromCellBinding: no property set for the binding!" );
319 if ( xBindingProps
.is() )
321 CellAddress aAddress
;
322 bReturn
= (bool)( xBindingProps
->getPropertyValue( PROPERTY_BOUND_CELL
) >>= _rAddress
);
325 catch( const Exception
& )
327 OSL_FAIL( "CellBindingHelper::getAddressFromCellBinding: caught an exception!" );
333 //------------------------------------------------------------------------
334 ::rtl::OUString
CellBindingHelper::getStringAddressFromCellBinding( const Reference
< XValueBinding
>& _rxBinding
) const
336 CellAddress aAddress
;
337 ::rtl::OUString sAddress
;
338 if ( getAddressFromCellBinding( _rxBinding
, aAddress
) )
341 doConvertAddressRepresentations( PROPERTY_ADDRESS
, makeAny( aAddress
),
342 PROPERTY_UI_REPRESENTATION
, aStringAddress
, false );
344 aStringAddress
>>= sAddress
;
350 //------------------------------------------------------------------------
351 ::rtl::OUString
CellBindingHelper::getStringAddressFromCellListSource( const Reference
< XListEntrySource
>& _rxSource
) const
353 OSL_PRECOND( !_rxSource
.is() || isCellRangeListSource( _rxSource
), "CellBindingHelper::getStringAddressFromCellListSource: this is no cell list source!" );
355 ::rtl::OUString sAddress
;
356 if ( !m_xDocument
.is() )
362 Reference
< XPropertySet
> xSourceProps( _rxSource
, UNO_QUERY
);
363 OSL_ENSURE( xSourceProps
.is() || !_rxSource
.is(), "CellBindingHelper::getStringAddressFromCellListSource: no property set for the list source!" );
364 if ( xSourceProps
.is() )
366 CellRangeAddress aRangeAddress
;
367 xSourceProps
->getPropertyValue( PROPERTY_LIST_CELL_RANGE
) >>= aRangeAddress
;
370 doConvertAddressRepresentations( PROPERTY_ADDRESS
, makeAny( aRangeAddress
),
371 PROPERTY_UI_REPRESENTATION
, aStringAddress
, true );
372 aStringAddress
>>= sAddress
;
375 catch( const Exception
& )
377 OSL_FAIL( "CellBindingHelper::getStringAddressFromCellListSource: caught an exception!" );
383 //------------------------------------------------------------------------
384 bool CellBindingHelper::isSpreadsheetDocumentWhichSupplies( const ::rtl::OUString
& _rService
) const
386 bool bYesItIs
= false;
388 Reference
< XServiceInfo
> xSI( m_xDocument
, UNO_QUERY
);
389 if ( xSI
.is() && xSI
->supportsService( SERVICE_SPREADSHEET_DOCUMENT
) )
391 Reference
< XMultiServiceFactory
> xDocumentFactory( m_xDocument
, UNO_QUERY
);
392 OSL_ENSURE( xDocumentFactory
.is(), "CellBindingHelper::isSpreadsheetDocumentWhichSupplies: spreadsheet document, but no factory?" );
394 Sequence
< ::rtl::OUString
> aAvailableServices
;
395 if ( xDocumentFactory
.is() )
396 aAvailableServices
= xDocumentFactory
->getAvailableServiceNames( );
398 const ::rtl::OUString
* pFound
= ::std::find_if(
399 aAvailableServices
.getConstArray(),
400 aAvailableServices
.getConstArray() + aAvailableServices
.getLength(),
401 StringCompare( _rService
)
403 if ( pFound
- aAvailableServices
.getConstArray() < aAvailableServices
.getLength() )
412 //------------------------------------------------------------------------
413 bool CellBindingHelper::isListCellRangeAllowed( ) const
415 bool bAllow( false );
417 Reference
< XListEntrySink
> xSink( m_xControlModel
, UNO_QUERY
);
420 bAllow
= isSpreadsheetDocumentWhichSupplies( SERVICE_SHEET_CELLRANGE_LISTSOURCE
);
426 //------------------------------------------------------------------------
427 bool CellBindingHelper::isCellIntegerBindingAllowed( ) const
431 // first, we only offer this for controls which allow bindings in general
432 Reference
< XBindableValue
> xBindable( m_xControlModel
, UNO_QUERY
);
433 if ( !xBindable
.is() )
436 // then, we must live in a spreadsheet document which can provide the special
437 // service needed for exchanging integer values
439 bAllow
= isSpreadsheetDocumentWhichSupplies( SERVICE_SHEET_CELL_INT_BINDING
);
441 // then, we only offer this for list boxes
446 sal_Int16 nClassId
= FormComponentType::CONTROL
;
447 m_xControlModel
->getPropertyValue( PROPERTY_CLASSID
) >>= nClassId
;
448 if ( FormComponentType::LISTBOX
!= nClassId
)
451 catch( const Exception
& )
453 OSL_FAIL( "CellBindingHelper::isCellIntegerBindingAllowed: caught an exception!" );
454 // are there really control models which survive isCellBindingAllowed, but don't have a ClassId
463 //------------------------------------------------------------------------
464 bool CellBindingHelper::isCellBindingAllowed( ) const
466 bool bAllow( false );
468 Reference
< XBindableValue
> xBindable( m_xControlModel
, UNO_QUERY
);
469 if ( xBindable
.is() )
471 // the control can potentially be bound to an external value
472 // Does it live within a Calc document, and is able to supply CellBindings?
473 bAllow
= isSpreadsheetDocumentWhichSupplies( SERVICE_SHEET_CELL_BINDING
);
476 // disallow for some types
477 // TODO: shouldn't the XBindableValue supply a list of supported types, and we can distingusih
478 // using this list? The current behavior below is somewhat hackish ...
483 sal_Int16 nClassId
= FormComponentType::CONTROL
;
484 m_xControlModel
->getPropertyValue( PROPERTY_CLASSID
) >>= nClassId
;
485 if ( ( FormComponentType::DATEFIELD
== nClassId
) || ( FormComponentType::TIMEFIELD
== nClassId
) )
488 catch( const Exception
& )
490 OSL_FAIL( "CellBindingHelper::isCellBindingAllowed: caught an exception!" );
497 //------------------------------------------------------------------------
498 bool CellBindingHelper::isCellBinding( const Reference
< XValueBinding
>& _rxBinding
) const
500 return doesComponentSupport( _rxBinding
.get(), SERVICE_SHEET_CELL_BINDING
);
503 //------------------------------------------------------------------------
504 bool CellBindingHelper::isCellIntegerBinding( const Reference
< XValueBinding
>& _rxBinding
) const
506 return doesComponentSupport( _rxBinding
.get(), SERVICE_SHEET_CELL_INT_BINDING
);
509 //------------------------------------------------------------------------
510 bool CellBindingHelper::isCellRangeListSource( const Reference
< XListEntrySource
>& _rxSource
) const
512 return doesComponentSupport( _rxSource
.get(), SERVICE_SHEET_CELLRANGE_LISTSOURCE
);
515 //------------------------------------------------------------------------
516 bool CellBindingHelper::doesComponentSupport( const Reference
< XInterface
>& _rxComponent
, const ::rtl::OUString
& _rService
) const
519 Reference
< XServiceInfo
> xSI( _rxComponent
, UNO_QUERY
);
520 bDoes
= xSI
.is() && xSI
->supportsService( _rService
);
524 //------------------------------------------------------------------------
525 Reference
< XValueBinding
> CellBindingHelper::getCurrentBinding( ) const
527 Reference
< XValueBinding
> xBinding
;
528 Reference
< XBindableValue
> xBindable( m_xControlModel
, UNO_QUERY
);
529 if ( xBindable
.is() )
530 xBinding
= xBindable
->getValueBinding();
534 //------------------------------------------------------------------------
535 Reference
< XListEntrySource
> CellBindingHelper::getCurrentListSource( ) const
537 Reference
< XListEntrySource
> xSource
;
538 Reference
< XListEntrySink
> xSink( m_xControlModel
, UNO_QUERY
);
540 xSource
= xSink
->getListEntrySource();
544 //------------------------------------------------------------------------
545 void CellBindingHelper::setBinding( const Reference
< XValueBinding
>& _rxBinding
)
547 Reference
< XBindableValue
> xBindable( m_xControlModel
, UNO_QUERY
);
548 OSL_PRECOND( xBindable
.is(), "CellBindingHelper::setBinding: the object is not bindable!" );
549 if ( xBindable
.is() )
550 xBindable
->setValueBinding( _rxBinding
);
553 //------------------------------------------------------------------------
554 void CellBindingHelper::setListSource( const Reference
< XListEntrySource
>& _rxSource
)
556 Reference
< XListEntrySink
> xSink( m_xControlModel
, UNO_QUERY
);
557 OSL_PRECOND( xSink
.is(), "CellBindingHelper::setListSource: the object is no list entry sink!" );
559 xSink
->setListEntrySource( _rxSource
);
562 //............................................................................
564 //............................................................................
566 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */