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 .
21 #include <strings.hrc>
22 #include <frm_resource.hxx>
23 #include <InterfaceContainer.hxx>
24 #include <componenttools.hxx>
25 #include <services.hxx>
27 #include <com/sun/star/beans/XPropertySet.hpp>
28 #include <com/sun/star/container/XNamed.hpp>
29 #include <com/sun/star/io/WrongFormatException.hpp>
30 #include <com/sun/star/io/XMarkableStream.hpp>
31 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
32 #include <com/sun/star/lang/ServiceNotRegisteredException.hpp>
33 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
34 #include <com/sun/star/lang/XComponent.hpp>
35 #include <com/sun/star/util/XCloneable.hpp>
36 #include <com/sun/star/form/XForm.hpp>
38 #include <comphelper/enumhelper.hxx>
39 #include <comphelper/eventattachermgr.hxx>
40 #include <comphelper/property.hxx>
41 #include <comphelper/sequence.hxx>
42 #include <cppuhelper/exc_hlp.hxx>
43 #include <cppuhelper/queryinterface.hxx>
44 #include <tools/debug.hxx>
45 #include <tools/diagnose_ex.h>
51 #include <com/sun/star/frame/XModel.hpp>
52 #include <com/sun/star/document/XCodeNameQuery.hpp>
53 #include <ooo/vba/XVBAToOOEventDescGen.hpp>
59 using namespace ::com::sun::star::frame
;
60 using namespace ::com::sun::star::lang
;
61 using namespace ::com::sun::star::uno
;
62 using namespace ::com::sun::star::beans
;
63 using namespace ::com::sun::star::document
;
64 using namespace ::com::sun::star::container
;
65 using namespace ::com::sun::star::script
;
66 using namespace ::com::sun::star::io
;
67 using namespace ::com::sun::star::form
;
68 using namespace ::com::sun::star::util
;
73 void lcl_throwIllegalArgumentException()
75 throw IllegalArgumentException();
80 lcl_hasVbaEvents( const Sequence
< ScriptEventDescriptor
>& sEvents
)
82 for ( auto const& rDesc
: sEvents
)
84 if ( rDesc
.ScriptType
== "VBAInterop" )
90 Sequence
< ScriptEventDescriptor
>
91 lcl_stripVbaEvents( const Sequence
< ScriptEventDescriptor
>& sEvents
)
93 Sequence
< ScriptEventDescriptor
> sStripped( sEvents
.getLength() );
94 ScriptEventDescriptor
* pStripped
= sStripped
.getArray();
96 sal_Int32 nCopied
= 0;
97 for ( auto const& rDesc
: sEvents
)
99 if ( rDesc
.ScriptType
!= "VBAInterop" )
101 pStripped
[ nCopied
++ ] = rDesc
;
104 sStripped
.realloc( nCopied
);
108 void OInterfaceContainer::impl_addVbEvents_nolck_nothrow( const sal_Int32 i_nIndex
)
110 // we are dealing with form controls
115 Reference
< XModel
> xDoc( getXModel( static_cast< XContainer
*> ( this ) ) );
119 Reference
< XMultiServiceFactory
> xDocFac( xDoc
, UNO_QUERY_THROW
);
120 Reference
< XCodeNameQuery
> xNameQuery( xDocFac
->createInstance("ooo.vba.VBACodeNameProvider"), UNO_QUERY
);
121 if ( !xNameQuery
.is() )
124 ::osl::MutexGuard
aGuard( m_rMutex
);
125 bool hasVBABindings
= lcl_hasVbaEvents( m_xEventAttacher
->getScriptEvents( i_nIndex
) );
126 if ( hasVBABindings
)
129 Reference
< XInterface
> xElement( getByIndex( i_nIndex
) , UNO_QUERY_THROW
);
130 Reference
< XForm
> xElementAsForm( xElement
, UNO_QUERY
);
131 if ( xElementAsForm
.is() )
134 // Try getting the code name from the container first (faster),
135 // then from the element if that fails (slower).
136 Reference
<XInterface
> xThis
= static_cast<XContainer
*>(this);
137 OUString sCodeName
= xNameQuery
->getCodeNameForContainer(xThis
);
138 if (sCodeName
.isEmpty())
139 sCodeName
= xNameQuery
->getCodeNameForObject(xElement
);
141 Reference
< XPropertySet
> xProps( xElement
, UNO_QUERY_THROW
);
142 OUString sServiceName
;
143 xProps
->getPropertyValue("DefaultControl") >>= sServiceName
;
145 Reference
< ooo::vba::XVBAToOOEventDescGen
> xDescSupplier( m_xContext
->getServiceManager()->createInstanceWithContext("ooo.vba.VBAToOOEventDesc", m_xContext
), UNO_QUERY_THROW
);
146 Sequence
< ScriptEventDescriptor
> vbaEvents
= xDescSupplier
->getEventDescriptions( sServiceName
, sCodeName
);
148 // register the vba script events
149 m_xEventAttacher
->registerScriptEvents( i_nIndex
, vbaEvents
);
153 catch ( const ServiceNotRegisteredException
& )
155 // silence this, not all document types support the ooo.vba.VBACodeNameProvider service
157 catch( const Exception
& )
159 DBG_UNHANDLED_EXCEPTION("forms.misc");
164 ElementDescription::ElementDescription( )
169 OInterfaceContainer::OInterfaceContainer(
170 const Reference
<XComponentContext
>& _rxContext
,
171 ::osl::Mutex
& _rMutex
,
172 const Type
& _rElementType
)
173 :OInterfaceContainer_BASE()
175 ,m_aContainerListeners(_rMutex
)
176 ,m_aElementType(_rElementType
)
177 ,m_xContext(_rxContext
)
179 impl_createEventAttacher_nothrow();
183 OInterfaceContainer::OInterfaceContainer( ::osl::Mutex
& _rMutex
, const OInterfaceContainer
& _cloneSource
)
184 :OInterfaceContainer_BASE()
186 ,m_aContainerListeners( _rMutex
)
187 ,m_aElementType( _cloneSource
.m_aElementType
)
188 ,m_xContext( _cloneSource
.m_xContext
)
190 impl_createEventAttacher_nothrow();
193 void OInterfaceContainer::clonedFrom(const OInterfaceContainer
& _cloneSource
)
197 const Reference
< XIndexAccess
> xSourceHierarchy( const_cast< OInterfaceContainer
* >( &_cloneSource
) );
198 const sal_Int32 nCount
= xSourceHierarchy
->getCount();
199 for ( sal_Int32 i
=0; i
<nCount
; ++i
)
201 Reference
< XCloneable
> xCloneable( xSourceHierarchy
->getByIndex( i
), UNO_QUERY_THROW
);
202 Reference
< XInterface
> xClone( xCloneable
->createClone() );
203 insertByIndex( i
, makeAny( xClone
) );
206 catch (const RuntimeException
&)
210 catch (const Exception
&)
212 throw WrappedTargetRuntimeException(
213 "Could not clone the given interface hierarchy.",
214 static_cast< XIndexContainer
* >( const_cast< OInterfaceContainer
* >( &_cloneSource
) ),
215 ::cppu::getCaughtException()
220 void OInterfaceContainer::impl_createEventAttacher_nothrow()
224 m_xEventAttacher
.set( ::comphelper::createEventAttacherManager( m_xContext
), UNO_SET_THROW
);
226 catch( const Exception
& )
228 DBG_UNHANDLED_EXCEPTION("forms.misc");
233 OInterfaceContainer::~OInterfaceContainer()
238 void OInterfaceContainer::disposing()
240 // dispose all elements
241 for (sal_Int32 i
= m_aItems
.size(); i
> 0; --i
)
243 Reference
<XPropertySet
> xSet(m_aItems
[i
- 1], UNO_QUERY
);
245 xSet
->removePropertyChangeListener(PROPERTY_NAME
, this);
247 // revoke event knittings
248 if ( m_xEventAttacher
.is() )
250 m_xEventAttacher
->detach( i
- 1, Reference
<XInterface
>(xSet
, UNO_QUERY
) );
251 m_xEventAttacher
->removeEntry( i
- 1 );
254 Reference
<XComponent
> xComponent(xSet
, UNO_QUERY
);
256 xComponent
->dispose();
261 css::lang::EventObject
aEvt(static_cast<XContainer
*>(this));
262 m_aContainerListeners
.disposeAndClear(aEvt
);
270 void lcl_saveEvents( ::std::vector
< Sequence
< ScriptEventDescriptor
> >& _rSave
,
271 const Reference
< XEventAttacherManager
>& _rxManager
, const sal_Int32 _nItemCount
)
273 OSL_ENSURE( _rxManager
.is(), "lcl_saveEvents: invalid event attacher manager!" );
274 if ( !_rxManager
.is() )
277 // reserve the space needed
278 _rSave
.reserve( _nItemCount
);
281 for (sal_Int32 i
=0; i
<_nItemCount
; ++i
)
282 _rSave
.push_back(_rxManager
->getScriptEvents( i
));
286 void lcl_restoreEvents( const ::std::vector
< Sequence
< ScriptEventDescriptor
> >& _rSave
,
287 const Reference
< XEventAttacherManager
>& _rxManager
)
289 OSL_ENSURE( _rxManager
.is(), "lcl_restoreEvents: invalid event attacher manager!" );
290 if ( !_rxManager
.is() )
294 for (auto const& elem
: _rSave
)
296 _rxManager
->revokeScriptEvents( i
);
297 _rxManager
->registerScriptEvents(i
, elem
);
304 void SAL_CALL
OInterfaceContainer::writeEvents(const Reference
<XObjectOutputStream
>& _rxOutStream
)
306 // We're writing a document in SO 5.2 format (or even from earlier versions)
307 // -> convert the events from the new runtime format to the format of the 5.2 files
308 // but before, remember the current script events set for our children
309 ::std::vector
< Sequence
< ScriptEventDescriptor
> > aSave
;
310 if ( m_xEventAttacher
.is() )
311 lcl_saveEvents( aSave
, m_xEventAttacher
, m_aItems
.size() );
317 Reference
<XMarkableStream
> xMark(_rxOutStream
, UNO_QUERY
);
318 sal_Int32 nMark
= xMark
->createMark();
320 sal_Int32 nObjLen
= 0;
321 _rxOutStream
->writeLong(nObjLen
);
323 Reference
<XPersistObject
> xScripts(m_xEventAttacher
, UNO_QUERY
);
325 xScripts
->write(_rxOutStream
);
328 nObjLen
= xMark
->offsetToMark(nMark
) - 4;
329 xMark
->jumpToMark(nMark
);
330 _rxOutStream
->writeLong(nObjLen
);
331 xMark
->jumpToFurthest();
332 xMark
->deleteMark(nMark
);
334 catch( const Exception
& )
336 // restore the events
337 if ( m_xEventAttacher
.is() )
338 lcl_restoreEvents( aSave
, m_xEventAttacher
);
342 // restore the events
343 if ( m_xEventAttacher
.is() )
344 lcl_restoreEvents( aSave
, m_xEventAttacher
);
348 struct TransformEventTo52Format
350 void operator()( ScriptEventDescriptor
& _rDescriptor
)
352 if ( _rDescriptor
.ScriptType
== "StarBasic" )
353 { // it's a starbasic macro
354 sal_Int32 nPrefixLength
= _rDescriptor
.ScriptCode
.indexOf( ':' );
355 if ( 0 <= nPrefixLength
)
356 { // the macro name does not already contain a :
358 const OUString sPrefix
= _rDescriptor
.ScriptCode
.copy( 0, nPrefixLength
);
359 DBG_ASSERT( sPrefix
== "document"
360 || sPrefix
== "application",
361 "TransformEventTo52Format: invalid (unknown) prefix!" );
364 _rDescriptor
.ScriptCode
= _rDescriptor
.ScriptCode
.copy( nPrefixLength
+ 1 );
371 void OInterfaceContainer::transformEvents()
373 OSL_ENSURE( m_xEventAttacher
.is(), "OInterfaceContainer::transformEvents: no event attacher manager!" );
374 if ( !m_xEventAttacher
.is() )
379 // loop through all our children
380 sal_Int32 nItems
= m_aItems
.size();
381 Sequence
< ScriptEventDescriptor
> aChildEvents
;
383 for (sal_Int32 i
=0; i
<nItems
; ++i
)
385 // get the script events for this object
386 aChildEvents
= m_xEventAttacher
->getScriptEvents( i
);
388 if ( aChildEvents
.getLength() )
390 // do the transformation
391 ::std::for_each( aChildEvents
.begin(), aChildEvents
.end(), TransformEventTo52Format() );
393 // revoke the script events
394 m_xEventAttacher
->revokeScriptEvents( i
);
395 // and re-register them
396 m_xEventAttacher
->registerScriptEvents( i
, aChildEvents
);
400 catch( const Exception
& )
402 DBG_UNHANDLED_EXCEPTION("forms.misc");
407 void SAL_CALL
OInterfaceContainer::readEvents(const Reference
<XObjectInputStream
>& _rxInStream
)
409 ::osl::MutexGuard
aGuard( m_rMutex
);
411 // Read scripting info
412 Reference
<XMarkableStream
> xMark(_rxInStream
, UNO_QUERY
);
413 sal_Int32 nObjLen
= _rxInStream
->readLong();
416 sal_Int32 nMark
= xMark
->createMark();
417 Reference
<XPersistObject
> xObj(m_xEventAttacher
, UNO_QUERY
);
419 xObj
->read(_rxInStream
);
420 xMark
->jumpToMark(nMark
);
421 _rxInStream
->skipBytes(nObjLen
);
422 xMark
->deleteMark(nMark
);
426 if ( m_xEventAttacher
.is() )
429 for (auto const& item
: m_aItems
)
431 Reference
< XInterface
> xAsIFace( item
, UNO_QUERY
); // important to normalize this ....
432 Reference
< XPropertySet
> xAsSet( xAsIFace
, UNO_QUERY
);
433 m_xEventAttacher
->attach( i
++, xAsIFace
, makeAny( xAsSet
) );
439 void SAL_CALL
OInterfaceContainer::write( const Reference
< XObjectOutputStream
>& _rxOutStream
)
441 ::osl::MutexGuard
aGuard( m_rMutex
);
442 sal_Int32 nLen
= m_aItems
.size();
445 _rxOutStream
->writeLong(nLen
);
450 _rxOutStream
->writeShort(0x0001);
453 for (sal_Int32 i
= 0; i
< nLen
; i
++)
455 Reference
<XPersistObject
> xObj(m_aItems
[i
], UNO_QUERY
);
457 _rxOutStream
->writeObject(xObj
);
465 writeEvents(_rxOutStream
);
472 Reference
< XPersistObject
> lcl_createPlaceHolder( const Reference
< XComponentContext
>& _rxORB
)
474 Reference
< XPersistObject
> xObject( _rxORB
->getServiceManager()->createInstanceWithContext(FRM_COMPONENT_HIDDENCONTROL
, _rxORB
), UNO_QUERY
);
475 DBG_ASSERT( xObject
.is(), "lcl_createPlaceHolder: could not create a substitute for the unknown object!" );
478 // set some properties describing what we did
479 Reference
< XPropertySet
> xObjProps( xObject
, UNO_QUERY
);
484 xObjProps
->setPropertyValue( PROPERTY_NAME
, makeAny( FRM_RES_STRING( RID_STR_CONTROL_SUBSTITUTED_NAME
) ) );
485 xObjProps
->setPropertyValue( PROPERTY_TAG
, makeAny( FRM_RES_STRING( RID_STR_CONTROL_SUBSTITUTED_EPXPLAIN
) ) );
487 catch(const Exception
&)
497 void SAL_CALL
OInterfaceContainer::read( const Reference
< XObjectInputStream
>& _rxInStream
)
499 ::osl::MutexGuard
aGuard( m_rMutex
);
501 // after ::read the object is expected to be in the state it was when ::write was called, so we have
502 // to empty ourself here
506 // Only writes depending on the length
507 sal_Int32 nLen
= _rxInStream
->readLong();
512 _rxInStream
->readShort();
515 for (sal_Int32 i
= 0; i
< nLen
; i
++)
517 Reference
<XPersistObject
> xObj
;
520 xObj
= _rxInStream
->readObject();
522 catch(const WrongFormatException
&)
524 // the object could not be read
525 // create a object (so the readEvents below will assign the events to the right controls)
526 xObj
= lcl_createPlaceHolder( m_xContext
);
528 // couldn't handle it
531 catch(const Exception
&)
534 while (!m_aItems
.empty())
535 removeElementsNoEvents();
537 // Rethrow the exception
543 Reference
< XPropertySet
> xElement( xObj
, UNO_QUERY
);
547 m_aItems
.size(), // position
548 xElement
, // element to insert
549 false, // no event attacher manager handling
550 nullptr, // not yet approved - let implInsert do it
551 true // fire the event
554 catch( const Exception
& )
556 SAL_WARN("forms.misc", "OInterfaceContainerHelper2::read: reading succeeded, but not inserting!" );
557 // create a placeholder
558 xElement
.set(lcl_createPlaceHolder( m_xContext
), css::uno::UNO_QUERY
);
559 if ( !xElement
.is() )
560 // couldn't handle it
562 // insert the placeholder
563 implInsert( m_aItems
.size(), xElement
, false, nullptr, true );
568 readEvents(_rxInStream
);
574 m_xEventAttacher
= ::comphelper::createEventAttacherManager( m_xContext
);
575 OSL_ENSURE( m_xEventAttacher
.is(), "OInterfaceContainer::read: could not create an event attacher manager!" );
577 catch( const Exception
& )
579 DBG_UNHANDLED_EXCEPTION("forms.misc");
586 void SAL_CALL
OInterfaceContainer::addContainerListener(const Reference
<XContainerListener
>& _rxListener
)
588 m_aContainerListeners
.addInterface(_rxListener
);
592 void SAL_CALL
OInterfaceContainer::removeContainerListener(const Reference
<XContainerListener
>& _rxListener
)
594 m_aContainerListeners
.removeInterface(_rxListener
);
599 void SAL_CALL
OInterfaceContainer::disposing(const css::lang::EventObject
& _rSource
)
601 ::osl::MutexGuard
aGuard( m_rMutex
);
603 Reference
< XInterface
> xSource( _rSource
.Source
, UNO_QUERY
);
606 OInterfaceArray::iterator j
;
607 for ( j
= m_aItems
.begin(); j
!= m_aItems
.end(); ++j
)
609 DBG_ASSERT( j
->get() == Reference
< XInterface
>( *j
, UNO_QUERY
).get(),
610 "OInterfaceContainer::disposing: vector element not normalized!" );
612 if ( xSource
.get() == j
->get() )
617 if ( m_aItems
.end() != j
)
621 // look up in, and erase from, m_aMap, too
622 OInterfaceMap::iterator i
= m_aMap
.begin();
623 while ( i
!= m_aMap
.end() )
625 DBG_ASSERT( i
->second
.get() == Reference
< XInterface
>( i
->second
, UNO_QUERY
).get(),
626 "OInterfaceContainer::disposing: map element not normalized!" );
628 if ( i
->second
.get() == xSource
.get() )
637 DBG_ASSERT( i
!= m_aMap
.end(), "OInterfaceContainer::disposing: inconsistency: the element was in m_aItems, but not in m_aMap!" );
642 // XPropertyChangeListener
644 void OInterfaceContainer::propertyChange(const PropertyChangeEvent
& evt
) {
645 if (evt
.PropertyName
== PROPERTY_NAME
)
647 ::osl::MutexGuard
aGuard( m_rMutex
);
648 auto range
= m_aMap
.equal_range(::comphelper::getString(evt
.OldValue
));
649 for (auto it
= range
.first
; it
!= range
.second
; ++it
)
650 if (it
->second
== evt
.Source
)
652 css::uno::Reference
<css::uno::XInterface
> xCorrectType(it
->second
);
654 m_aMap
.insert(::std::pair
<const OUString
, css::uno::Reference
<css::uno::XInterface
> >(::comphelper::getString(evt
.NewValue
),xCorrectType
));
662 sal_Bool SAL_CALL
OInterfaceContainer::hasElements()
664 return !m_aMap
.empty();
668 Type SAL_CALL
OInterfaceContainer::getElementType()
670 return m_aElementType
;
673 // XEnumerationAccess
675 Reference
<XEnumeration
> SAL_CALL
OInterfaceContainer::createEnumeration()
677 ::osl::MutexGuard
aGuard( m_rMutex
);
678 return new ::comphelper::OEnumerationByIndex(static_cast<XIndexAccess
*>(this));
683 Any SAL_CALL
OInterfaceContainer::getByName( const OUString
& _rName
)
685 ::std::pair
<OInterfaceMap::iterator
,
686 OInterfaceMap::iterator
> aPair
= m_aMap
.equal_range(_rName
);
688 if (aPair
.first
== aPair
.second
)
689 throw NoSuchElementException();
691 return (*aPair
.first
).second
->queryInterface( m_aElementType
);
695 css::uno::Sequence
<OUString
> SAL_CALL
OInterfaceContainer::getElementNames()
697 return comphelper::mapKeysToSequence(m_aMap
);
701 sal_Bool SAL_CALL
OInterfaceContainer::hasByName( const OUString
& _rName
)
703 ::std::pair
<OInterfaceMap::iterator
,
704 OInterfaceMap::iterator
> aPair
= m_aMap
.equal_range(_rName
);
705 return aPair
.first
!= aPair
.second
;
710 sal_Int32
OInterfaceContainer::getCount()
712 return m_aItems
.size();
716 Any
OInterfaceContainer::getByIndex(sal_Int32 _nIndex
)
718 if (_nIndex
< 0 || (_nIndex
>= static_cast<sal_Int32
>(m_aItems
.size())))
719 throw IndexOutOfBoundsException();
721 return m_aItems
[_nIndex
]->queryInterface( m_aElementType
);
725 void OInterfaceContainer::approveNewElement( const Reference
< XPropertySet
>& _rxObject
, ElementDescription
* _pElement
)
727 // it has to be non-NULL
728 if ( !_rxObject
.is() )
729 throw IllegalArgumentException(FRM_RES_STRING(RID_STR_NEED_NON_NULL_OBJECT
), static_cast<XContainer
*>(this), 1);
731 // it has to support our element type interface
732 Any aCorrectType
= _rxObject
->queryInterface( m_aElementType
);
733 if ( !aCorrectType
.hasValue() )
734 lcl_throwIllegalArgumentException();
736 // it has to have a "Name" property
737 if ( !hasProperty( PROPERTY_NAME
, _rxObject
) )
738 lcl_throwIllegalArgumentException();
740 // it has to be a child, and it must not have a parent already
741 Reference
< XChild
> xChild( _rxObject
, UNO_QUERY
);
742 if ( !xChild
.is() || xChild
->getParent().is() )
744 lcl_throwIllegalArgumentException();
747 // passed all tests. cache the information we have so far
748 DBG_ASSERT( _pElement
, "OInterfaceContainer::approveNewElement: invalid event descriptor!" );
751 _pElement
->xPropertySet
= _rxObject
;
752 _pElement
->xChild
= xChild
;
753 _pElement
->aElementTypeInterface
= aCorrectType
;
754 _pElement
->xInterface
= Reference
< XInterface
>( _rxObject
, UNO_QUERY
); // normalized XInterface
759 void OInterfaceContainer::implInsert(sal_Int32 _nIndex
, const Reference
< XPropertySet
>& _rxElement
,
760 bool _bEvents
, ElementDescription
* _pApprovalResult
, bool _bFire
)
762 const bool bHandleEvents
= _bEvents
&& m_xEventAttacher
.is();
764 // SYNCHRONIZED ----->
765 ::osl::ClearableMutexGuard
aGuard( m_rMutex
);
767 std::unique_ptr
< ElementDescription
> aAutoDeleteMetaData
;
768 ElementDescription
* pElementMetaData
= _pApprovalResult
;
769 if ( !pElementMetaData
)
770 { // not yet approved by the caller -> do ourself
771 pElementMetaData
= createElementMetaData();
772 DBG_ASSERT( pElementMetaData
, "OInterfaceContainer::implInsert: createElementMetaData returned nonsense!" );
774 // ensure that the meta data structure will be deleted later on
775 aAutoDeleteMetaData
.reset( pElementMetaData
);
777 // will throw an exception if necessary
778 approveNewElement( _rxElement
, pElementMetaData
);
782 // approveNewElement (no matter if called here or outside) has ensure that all relevant interfaces
785 // set the name, and add as change listener for the name
787 _rxElement
->getPropertyValue(PROPERTY_NAME
) >>= sName
;
788 _rxElement
->addPropertyChangeListener(PROPERTY_NAME
, this);
790 // insert the object into our internal structures
791 if (_nIndex
> static_cast<sal_Int32
>(m_aItems
.size())) // Calculate the actual index
793 _nIndex
= m_aItems
.size();
794 m_aItems
.push_back( pElementMetaData
->xInterface
);
797 m_aItems
.insert( m_aItems
.begin() + _nIndex
, pElementMetaData
->xInterface
);
799 m_aMap
.insert( ::std::pair
< const OUString
, css::uno::Reference
<css::uno::XInterface
> >( sName
, pElementMetaData
->xInterface
) );
801 // announce ourself as parent to the new element
802 pElementMetaData
->xChild
->setParent(static_cast<XContainer
*>(this));
807 m_xEventAttacher
->insertEntry(_nIndex
);
808 m_xEventAttacher
->attach( _nIndex
, pElementMetaData
->xInterface
, makeAny( _rxElement
) );
811 // notify derived classes
812 implInserted( pElementMetaData
);
815 // <----- SYNCHRONIZED
817 // insert faked VBA events?
818 bool bHandleVbaEvents
= false;
821 _rxElement
->getPropertyValue("GenerateVbaEvents") >>= bHandleVbaEvents
;
823 catch( const Exception
& )
826 if ( bHandleVbaEvents
)
828 Reference
< XEventAttacherManager
> xMgr ( pElementMetaData
->xInterface
, UNO_QUERY
);
829 OInterfaceContainer
* pIfcMgr
= xMgr
.is() ? dynamic_cast<OInterfaceContainer
*>(xMgr
.get()) : nullptr;
832 sal_Int32 nLen
= pIfcMgr
->getCount();
833 for (sal_Int32 i
= 0; i
< nLen
; ++i
)
835 // add fake events to the control at index i
836 pIfcMgr
->impl_addVbEvents_nolck_nothrow( i
);
841 // add fake events to the control at index i
842 impl_addVbEvents_nolck_nothrow( _nIndex
);
846 // fire the notification about the change
851 aEvt
.Source
= static_cast<XContainer
*>(this);
852 aEvt
.Accessor
<<= _nIndex
;
853 aEvt
.Element
= pElementMetaData
->aElementTypeInterface
;
856 m_aContainerListeners
.notifyEach( &XContainerListener::elementInserted
, aEvt
);
861 void OInterfaceContainer::removeElementsNoEvents()
863 OInterfaceArray::iterator i
= m_aItems
.begin();
864 css::uno::Reference
<css::uno::XInterface
> xElement(*i
);
866 OInterfaceMap::iterator j
= m_aMap
.begin();
867 while (j
!= m_aMap
.end() && (*j
).second
!= xElement
) ++j
;
872 Reference
<XPropertySet
> xSet(xElement
, UNO_QUERY
);
874 xSet
->removePropertyChangeListener(PROPERTY_NAME
, this);
876 Reference
<XChild
> xChild(xElement
, UNO_QUERY
);
878 xChild
->setParent(css::uno::Reference
<css::uno::XInterface
> ());
882 void OInterfaceContainer::implInserted( const ElementDescription
* /*_pElement*/ )
888 void OInterfaceContainer::implRemoved( const css::uno::Reference
<css::uno::XInterface
>& /*_rxObject*/ )
894 void OInterfaceContainer::impl_replacedElement( const ContainerEvent
& _rEvent
, ::osl::ClearableMutexGuard
& _rInstanceLock
)
896 _rInstanceLock
.clear();
897 m_aContainerListeners
.notifyEach( &XContainerListener::elementReplaced
, _rEvent
);
902 void SAL_CALL
OInterfaceContainer::insertByIndex( sal_Int32 _nIndex
, const Any
& _rElement
)
904 Reference
< XPropertySet
> xElement
;
905 _rElement
>>= xElement
;
906 implInsert( _nIndex
, xElement
, true /* event handling */ , nullptr /* not yet approved */ , true /* notification */ );
910 void OInterfaceContainer::implReplaceByIndex( const sal_Int32 _nIndex
, const Any
& _rNewElement
, ::osl::ClearableMutexGuard
& _rClearBeforeNotify
)
912 OSL_PRECOND( ( _nIndex
>= 0 ) && ( _nIndex
< static_cast<sal_Int32
>(m_aItems
.size()) ), "OInterfaceContainer::implReplaceByIndex: precondition not met (index)!" );
914 // approve the new object
915 std::unique_ptr
< ElementDescription
> aElementMetaData( createElementMetaData() );
916 DBG_ASSERT( aElementMetaData
.get(), "OInterfaceContainer::implReplaceByIndex: createElementMetaData returned nonsense!" );
918 Reference
< XPropertySet
> xElementProps
;
919 _rNewElement
>>= xElementProps
;
920 approveNewElement( xElementProps
, aElementMetaData
.get() );
923 // get the old element
924 css::uno::Reference
<css::uno::XInterface
> xOldElement( m_aItems
[ _nIndex
] );
925 DBG_ASSERT( xOldElement
.get() == Reference
< XInterface
>( xOldElement
, UNO_QUERY
).get(),
926 "OInterfaceContainer::implReplaceByIndex: elements should be held normalized!" );
928 // locate the old element in the map
929 OInterfaceMap::iterator j
= m_aMap
.begin();
930 while ( ( j
!= m_aMap
.end() ) && ( j
->second
.get() != xOldElement
.get() ) )
933 // remove event knittings
934 if ( m_xEventAttacher
.is() )
936 css::uno::Reference
<css::uno::XInterface
> xNormalized( xOldElement
, UNO_QUERY
);
937 m_xEventAttacher
->detach( _nIndex
, xNormalized
);
938 m_xEventAttacher
->removeEntry( _nIndex
);
941 // don't listen for property changes anymore
942 Reference
<XPropertySet
> xSet( xOldElement
, UNO_QUERY
);
944 xSet
->removePropertyChangeListener(PROPERTY_NAME
, this);
946 // give the old element a new (void) parent
947 Reference
<XChild
> xChild(xOldElement
, UNO_QUERY
);
949 xChild
->setParent(css::uno::Reference
<css::uno::XInterface
> ());
951 // remove the old one
954 // examine the new element
956 DBG_ASSERT( aElementMetaData
.get()->xPropertySet
.is(), "OInterfaceContainer::implReplaceByIndex: what did approveNewElement do?" );
958 aElementMetaData
.get()->xPropertySet
->getPropertyValue(PROPERTY_NAME
) >>= sName
;
959 aElementMetaData
.get()->xPropertySet
->addPropertyChangeListener(PROPERTY_NAME
, this);
961 // insert the new one
962 m_aMap
.insert( ::std::pair
<const OUString
, css::uno::Reference
<css::uno::XInterface
> >( sName
, aElementMetaData
.get()->xInterface
) );
963 m_aItems
[ _nIndex
] = aElementMetaData
.get()->xInterface
;
965 aElementMetaData
.get()->xChild
->setParent(static_cast<XContainer
*>(this));
967 if ( m_xEventAttacher
.is() )
969 m_xEventAttacher
->insertEntry( _nIndex
);
970 m_xEventAttacher
->attach( _nIndex
, aElementMetaData
.get()->xInterface
, makeAny( aElementMetaData
.get()->xPropertySet
) );
973 ContainerEvent aReplaceEvent
;
974 aReplaceEvent
.Source
= static_cast< XContainer
* >( this );
975 aReplaceEvent
.Accessor
<<= _nIndex
;
976 aReplaceEvent
.Element
= aElementMetaData
.get()->xInterface
->queryInterface( m_aElementType
);
977 aReplaceEvent
.ReplacedElement
= xOldElement
->queryInterface( m_aElementType
);
979 impl_replacedElement( aReplaceEvent
, _rClearBeforeNotify
);
983 void OInterfaceContainer::implCheckIndex( const sal_Int32 _nIndex
)
985 if (_nIndex
< 0 || _nIndex
>= static_cast<sal_Int32
>(m_aItems
.size()))
986 throw IndexOutOfBoundsException();
990 void SAL_CALL
OInterfaceContainer::replaceByIndex(sal_Int32 _nIndex
, const Any
& Element
)
992 ::osl::ClearableMutexGuard
aGuard( m_rMutex
);
994 implCheckIndex( _nIndex
);
996 implReplaceByIndex( _nIndex
, Element
, aGuard
);
1000 void OInterfaceContainer::implRemoveByIndex( const sal_Int32 _nIndex
, ::osl::ClearableMutexGuard
& _rClearBeforeNotify
)
1002 OSL_PRECOND( ( _nIndex
>= 0 ) && ( _nIndex
< static_cast<sal_Int32
>(m_aItems
.size()) ), "OInterfaceContainer::implRemoveByIndex: precondition not met (index)!" );
1004 OInterfaceArray::iterator i
= m_aItems
.begin() + _nIndex
;
1005 css::uno::Reference
<css::uno::XInterface
> xElement(*i
);
1007 OInterfaceMap::iterator j
= m_aMap
.begin();
1008 while (j
!= m_aMap
.end() && (*j
).second
!= xElement
) ++j
;
1013 // remove event knittings
1014 if ( m_xEventAttacher
.is() )
1016 css::uno::Reference
<css::uno::XInterface
> xNormalized( xElement
, UNO_QUERY
);
1017 m_xEventAttacher
->detach( _nIndex
, xNormalized
);
1018 m_xEventAttacher
->removeEntry( _nIndex
);
1021 Reference
<XPropertySet
> xSet(xElement
, UNO_QUERY
);
1023 xSet
->removePropertyChangeListener(PROPERTY_NAME
, this);
1025 Reference
<XChild
> xChild(xElement
, UNO_QUERY
);
1027 xChild
->setParent(css::uno::Reference
<css::uno::XInterface
> ());
1029 // notify derived classes
1030 implRemoved(xElement
);
1033 ContainerEvent aEvt
;
1034 aEvt
.Source
= static_cast<XContainer
*>(this);
1035 aEvt
.Element
= xElement
->queryInterface( m_aElementType
);
1036 aEvt
.Accessor
<<= _nIndex
;
1038 _rClearBeforeNotify
.clear();
1039 m_aContainerListeners
.notifyEach( &XContainerListener::elementRemoved
, aEvt
);
1043 void SAL_CALL
OInterfaceContainer::removeByIndex(sal_Int32 _nIndex
)
1045 ::osl::ClearableMutexGuard
aGuard( m_rMutex
);
1047 implCheckIndex( _nIndex
);
1049 implRemoveByIndex( _nIndex
, aGuard
);
1053 ElementDescription
* OInterfaceContainer::createElementMetaData( )
1055 return new ElementDescription
;
1059 void SAL_CALL
OInterfaceContainer::insertByName(const OUString
& _rName
, const Any
& _rElement
)
1061 Reference
< XPropertySet
> xElementProps
;
1063 std::unique_ptr
< ElementDescription
> aElementMetaData( createElementMetaData() );
1064 DBG_ASSERT( aElementMetaData
.get(), "OInterfaceContainer::insertByName: createElementMetaData returned nonsense!" );
1066 // ensure the correct name of the element
1069 _rElement
>>= xElementProps
;
1070 approveNewElement( xElementProps
, aElementMetaData
.get() );
1072 xElementProps
->setPropertyValue( PROPERTY_NAME
, makeAny( _rName
) );
1074 catch( const IllegalArgumentException
& )
1076 throw; // allowed to leave
1078 catch( const ElementExistException
& )
1080 throw; // allowed to leave
1082 catch( const Exception
& )
1084 SAL_WARN("forms.misc", "OInterfaceContainer::insertByName: caught an exception!" );
1086 implInsert( m_aItems
.size(), xElementProps
, true, aElementMetaData
.get(), true );
1090 void SAL_CALL
OInterfaceContainer::replaceByName(const OUString
& Name
, const Any
& Element
)
1092 ::osl::ClearableMutexGuard
aGuard( m_rMutex
);
1093 ::std::pair
<OInterfaceMap::iterator
,
1094 OInterfaceMap::iterator
> aPair
= m_aMap
.equal_range(Name
);
1095 if (aPair
.first
== aPair
.second
)
1096 throw NoSuchElementException();
1098 if (Element
.getValueType().getTypeClass() != TypeClass_INTERFACE
)
1099 lcl_throwIllegalArgumentException();
1101 Reference
<XPropertySet
> xSet
;
1105 if (!hasProperty(PROPERTY_NAME
, xSet
))
1106 lcl_throwIllegalArgumentException();
1108 xSet
->setPropertyValue(PROPERTY_NAME
, makeAny(Name
));
1111 // determine the element pos
1112 sal_Int32 nPos
= ::std::find(m_aItems
.begin(), m_aItems
.end(), (*aPair
.first
).second
) - m_aItems
.begin();
1114 implReplaceByIndex( nPos
, Element
, aGuard
);
1118 void SAL_CALL
OInterfaceContainer::removeByName(const OUString
& Name
)
1120 ::osl::MutexGuard
aGuard( m_rMutex
);
1121 ::std::pair
<OInterfaceMap::iterator
,
1122 OInterfaceMap::iterator
> aPair
= m_aMap
.equal_range(Name
);
1123 if (aPair
.first
== aPair
.second
)
1124 throw NoSuchElementException();
1126 sal_Int32 nPos
= ::std::find(m_aItems
.begin(), m_aItems
.end(), (*aPair
.first
).second
) - m_aItems
.begin();
1127 removeByIndex(nPos
);
1131 // XEventAttacherManager
1133 void SAL_CALL
OInterfaceContainer::registerScriptEvent( sal_Int32 nIndex
, const ScriptEventDescriptor
& aScriptEvent
)
1135 ::osl::ClearableMutexGuard
aGuard( m_rMutex
);
1136 if ( m_xEventAttacher
.is() )
1138 m_xEventAttacher
->registerScriptEvent( nIndex
, aScriptEvent
);
1140 impl_addVbEvents_nolck_nothrow( nIndex
); // add fake vba events
1145 void SAL_CALL
OInterfaceContainer::registerScriptEvents( sal_Int32 nIndex
, const Sequence
< ScriptEventDescriptor
>& aScriptEvents
)
1147 ::osl::ClearableMutexGuard
aGuard( m_rMutex
);
1148 if ( m_xEventAttacher
.is() )
1150 m_xEventAttacher
->registerScriptEvents( nIndex
, aScriptEvents
);
1152 impl_addVbEvents_nolck_nothrow( nIndex
); // add fake vba events
1157 void SAL_CALL
OInterfaceContainer::revokeScriptEvent( sal_Int32 nIndex
, const OUString
& aListenerType
, const OUString
& aEventMethod
, const OUString
& aRemoveListenerParam
)
1159 if ( m_xEventAttacher
.is() )
1160 m_xEventAttacher
->revokeScriptEvent( nIndex
, aListenerType
, aEventMethod
, aRemoveListenerParam
);
1164 void SAL_CALL
OInterfaceContainer::revokeScriptEvents( sal_Int32 nIndex
)
1166 if ( m_xEventAttacher
.is() )
1167 m_xEventAttacher
->revokeScriptEvents( nIndex
);
1171 void SAL_CALL
OInterfaceContainer::insertEntry( sal_Int32 nIndex
)
1173 if ( m_xEventAttacher
.is() )
1174 m_xEventAttacher
->insertEntry( nIndex
);
1178 void SAL_CALL
OInterfaceContainer::removeEntry( sal_Int32 nIndex
)
1180 if ( m_xEventAttacher
.is() )
1181 m_xEventAttacher
->removeEntry( nIndex
);
1185 Sequence
< ScriptEventDescriptor
> SAL_CALL
OInterfaceContainer::getScriptEvents( sal_Int32 nIndex
)
1187 Sequence
< ScriptEventDescriptor
> aReturn
;
1188 if ( m_xEventAttacher
.is() )
1190 aReturn
= m_xEventAttacher
->getScriptEvents( nIndex
);
1191 if ( lcl_hasVbaEvents( aReturn
) )
1193 aReturn
= lcl_stripVbaEvents( aReturn
);
1200 void SAL_CALL
OInterfaceContainer::attach( sal_Int32 nIndex
, const Reference
< XInterface
>& xObject
, const Any
& aHelper
)
1202 if ( m_xEventAttacher
.is() )
1203 m_xEventAttacher
->attach( nIndex
, xObject
, aHelper
);
1207 void SAL_CALL
OInterfaceContainer::detach( sal_Int32 nIndex
, const Reference
< XInterface
>& xObject
)
1209 if ( m_xEventAttacher
.is() )
1210 m_xEventAttacher
->detach( nIndex
, xObject
);
1214 void SAL_CALL
OInterfaceContainer::addScriptListener( const Reference
< XScriptListener
>& xListener
)
1216 if ( m_xEventAttacher
.is() )
1217 m_xEventAttacher
->addScriptListener( xListener
);
1221 void SAL_CALL
OInterfaceContainer::removeScriptListener( const Reference
< XScriptListener
>& xListener
)
1223 if ( m_xEventAttacher
.is() )
1224 m_xEventAttacher
->removeScriptListener( xListener
);
1231 Any SAL_CALL
OFormComponents::queryAggregation(const Type
& _rType
)
1233 Any aReturn
= OFormComponents_BASE::queryInterface(_rType
);
1234 if (!aReturn
.hasValue())
1236 aReturn
= OInterfaceContainer::queryInterface(_rType
);
1238 if (!aReturn
.hasValue())
1239 aReturn
= ::cppu::OComponentHelper::queryAggregation(_rType
);
1246 Sequence
<Type
> SAL_CALL
OFormComponents::getTypes()
1248 return ::comphelper::concatSequences(OInterfaceContainer::getTypes(), ::cppu::OComponentHelper::getTypes(), OFormComponents_BASE::getTypes());
1252 OFormComponents::OFormComponents(const Reference
<XComponentContext
>& _rxFactory
)
1253 : ::cppu::OComponentHelper( m_aMutex
)
1254 ,OInterfaceContainer( _rxFactory
, m_aMutex
, cppu::UnoType
<XFormComponent
>::get() )
1255 ,OFormComponents_BASE()
1260 OFormComponents::OFormComponents( const OFormComponents
& _cloneSource
)
1261 : ::cppu::OComponentHelper( m_aMutex
)
1262 ,OInterfaceContainer( m_aMutex
, _cloneSource
)
1263 ,OFormComponents_BASE()
1268 OFormComponents::~OFormComponents()
1270 if (! ::cppu::OComponentHelper::rBHelper
.bDisposed
)
1279 void OFormComponents::disposing()
1281 OInterfaceContainer::disposing();
1282 ::cppu::OComponentHelper::disposing();
1283 m_xParent
= nullptr;
1288 void OFormComponents::setParent(const css::uno::Reference
<css::uno::XInterface
>& Parent
)
1290 ::osl::MutexGuard
aGuard( m_aMutex
);
1295 css::uno::Reference
<css::uno::XInterface
> OFormComponents::getParent()
1304 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */