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 <frm_strings.hxx>
24 #include <InterfaceContainer.hxx>
25 #include <componenttools.hxx>
26 #include <services.hxx>
28 #include <com/sun/star/beans/XPropertySet.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/XMultiServiceFactory.hpp>
33 #include <com/sun/star/lang/ServiceNotRegisteredException.hpp>
34 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
35 #include <com/sun/star/lang/XComponent.hpp>
36 #include <com/sun/star/uno/XComponentContext.hpp>
37 #include <com/sun/star/util/XCloneable.hpp>
38 #include <com/sun/star/form/XForm.hpp>
40 #include <comphelper/enumhelper.hxx>
41 #include <comphelper/eventattachermgr.hxx>
42 #include <comphelper/property.hxx>
43 #include <comphelper/sequence.hxx>
44 #include <comphelper/types.hxx>
45 #include <cppuhelper/exc_hlp.hxx>
46 #include <tools/debug.hxx>
47 #include <tools/diagnose_ex.h>
48 #include <sal/log.hxx>
54 #include <com/sun/star/frame/XModel.hpp>
55 #include <com/sun/star/document/XCodeNameQuery.hpp>
56 #include <ooo/vba/XVBAToOOEventDescGen.hpp>
62 using namespace ::com::sun::star::frame
;
63 using namespace ::com::sun::star::lang
;
64 using namespace ::com::sun::star::uno
;
65 using namespace ::com::sun::star::beans
;
66 using namespace ::com::sun::star::document
;
67 using namespace ::com::sun::star::container
;
68 using namespace ::com::sun::star::script
;
69 using namespace ::com::sun::star::io
;
70 using namespace ::com::sun::star::form
;
71 using namespace ::com::sun::star::util
;
76 void lcl_throwIllegalArgumentException()
78 throw IllegalArgumentException();
83 lcl_hasVbaEvents( const Sequence
< ScriptEventDescriptor
>& sEvents
)
85 for ( auto const& rDesc
: sEvents
)
87 if ( rDesc
.ScriptType
== "VBAInterop" )
93 static Sequence
< ScriptEventDescriptor
>
94 lcl_stripVbaEvents( const Sequence
< ScriptEventDescriptor
>& sEvents
)
96 Sequence
< ScriptEventDescriptor
> sStripped( sEvents
.getLength() );
97 ScriptEventDescriptor
* pStripped
= sStripped
.getArray();
99 sal_Int32 nCopied
= 0;
100 for ( auto const& rDesc
: sEvents
)
102 if ( rDesc
.ScriptType
!= "VBAInterop" )
104 pStripped
[ nCopied
++ ] = rDesc
;
107 sStripped
.realloc( nCopied
);
111 void OInterfaceContainer::impl_addVbEvents_nolck_nothrow( const sal_Int32 i_nIndex
)
113 // we are dealing with form controls
118 Reference
< XModel
> xDoc( getXModel( static_cast< XContainer
*> ( this ) ) );
122 Reference
< XMultiServiceFactory
> xDocFac( xDoc
, UNO_QUERY_THROW
);
123 Reference
< XCodeNameQuery
> xNameQuery( xDocFac
->createInstance("ooo.vba.VBACodeNameProvider"), UNO_QUERY
);
124 if ( !xNameQuery
.is() )
127 ::osl::MutexGuard
aGuard( m_rMutex
);
128 bool hasVBABindings
= lcl_hasVbaEvents( m_xEventAttacher
->getScriptEvents( i_nIndex
) );
129 if ( hasVBABindings
)
132 Reference
< XInterface
> xElement( getByIndex( i_nIndex
) , UNO_QUERY_THROW
);
133 Reference
< XForm
> xElementAsForm( xElement
, UNO_QUERY
);
134 if ( xElementAsForm
.is() )
137 // Try getting the code name from the container first (faster),
138 // then from the element if that fails (slower).
139 Reference
<XInterface
> xThis
= static_cast<XContainer
*>(this);
140 OUString sCodeName
= xNameQuery
->getCodeNameForContainer(xThis
);
141 if (sCodeName
.isEmpty())
142 sCodeName
= xNameQuery
->getCodeNameForObject(xElement
);
144 Reference
< XPropertySet
> xProps( xElement
, UNO_QUERY_THROW
);
145 OUString sServiceName
;
146 xProps
->getPropertyValue("DefaultControl") >>= sServiceName
;
148 Reference
< ooo::vba::XVBAToOOEventDescGen
> xDescSupplier( m_xContext
->getServiceManager()->createInstanceWithContext("ooo.vba.VBAToOOEventDesc", m_xContext
), UNO_QUERY_THROW
);
149 Sequence
< ScriptEventDescriptor
> vbaEvents
= xDescSupplier
->getEventDescriptions( sServiceName
, sCodeName
);
151 // register the vba script events
152 m_xEventAttacher
->registerScriptEvents( i_nIndex
, vbaEvents
);
156 catch ( const ServiceNotRegisteredException
& )
158 // silence this, not all document types support the ooo.vba.VBACodeNameProvider service
160 catch( const Exception
& )
162 DBG_UNHANDLED_EXCEPTION("forms.misc");
167 ElementDescription::ElementDescription( )
172 OInterfaceContainer::OInterfaceContainer(
173 const Reference
<XComponentContext
>& _rxContext
,
174 ::osl::Mutex
& _rMutex
,
175 const Type
& _rElementType
)
176 :OInterfaceContainer_BASE()
178 ,m_aContainerListeners(_rMutex
)
179 ,m_aElementType(_rElementType
)
180 ,m_xContext(_rxContext
)
182 impl_createEventAttacher_nothrow();
186 OInterfaceContainer::OInterfaceContainer( ::osl::Mutex
& _rMutex
, const OInterfaceContainer
& _cloneSource
)
187 :OInterfaceContainer_BASE()
189 ,m_aContainerListeners( _rMutex
)
190 ,m_aElementType( _cloneSource
.m_aElementType
)
191 ,m_xContext( _cloneSource
.m_xContext
)
193 impl_createEventAttacher_nothrow();
196 void OInterfaceContainer::clonedFrom(const OInterfaceContainer
& _cloneSource
)
200 const Reference
< XIndexAccess
> xSourceHierarchy( const_cast< OInterfaceContainer
* >( &_cloneSource
) );
201 const sal_Int32 nCount
= xSourceHierarchy
->getCount();
202 for ( sal_Int32 i
=0; i
<nCount
; ++i
)
204 Reference
< XCloneable
> xCloneable( xSourceHierarchy
->getByIndex( i
), UNO_QUERY_THROW
);
205 Reference
< XInterface
> xClone( xCloneable
->createClone() );
206 insertByIndex( i
, makeAny( xClone
) );
209 catch (const RuntimeException
&)
213 catch (const Exception
&)
215 throw WrappedTargetRuntimeException(
216 "Could not clone the given interface hierarchy.",
217 static_cast< XIndexContainer
* >( const_cast< OInterfaceContainer
* >( &_cloneSource
) ),
218 ::cppu::getCaughtException()
223 void OInterfaceContainer::impl_createEventAttacher_nothrow()
227 m_xEventAttacher
.set( ::comphelper::createEventAttacherManager( m_xContext
), UNO_SET_THROW
);
229 catch( const Exception
& )
231 DBG_UNHANDLED_EXCEPTION("forms.misc");
236 OInterfaceContainer::~OInterfaceContainer()
241 void OInterfaceContainer::disposing()
243 // dispose all elements
244 for (sal_Int32 i
= m_aItems
.size(); i
> 0; --i
)
246 Reference
<XPropertySet
> xSet(m_aItems
[i
- 1], UNO_QUERY
);
248 xSet
->removePropertyChangeListener(PROPERTY_NAME
, this);
250 // revoke event knittings
251 if ( m_xEventAttacher
.is() )
253 m_xEventAttacher
->detach( i
- 1, Reference
<XInterface
>(xSet
, UNO_QUERY
) );
254 m_xEventAttacher
->removeEntry( i
- 1 );
257 Reference
<XComponent
> xComponent(xSet
, UNO_QUERY
);
259 xComponent
->dispose();
264 css::lang::EventObject
aEvt(static_cast<XContainer
*>(this));
265 m_aContainerListeners
.disposeAndClear(aEvt
);
273 void lcl_saveEvents( ::std::vector
< Sequence
< ScriptEventDescriptor
> >& _rSave
,
274 const Reference
< XEventAttacherManager
>& _rxManager
, const sal_Int32 _nItemCount
)
276 OSL_ENSURE( _rxManager
.is(), "lcl_saveEvents: invalid event attacher manager!" );
277 if ( !_rxManager
.is() )
280 // reserve the space needed
281 _rSave
.reserve( _nItemCount
);
284 for (sal_Int32 i
=0; i
<_nItemCount
; ++i
)
285 _rSave
.push_back(_rxManager
->getScriptEvents( i
));
289 void lcl_restoreEvents( const ::std::vector
< Sequence
< ScriptEventDescriptor
> >& _rSave
,
290 const Reference
< XEventAttacherManager
>& _rxManager
)
292 OSL_ENSURE( _rxManager
.is(), "lcl_restoreEvents: invalid event attacher manager!" );
293 if ( !_rxManager
.is() )
297 for (auto const& elem
: _rSave
)
299 _rxManager
->revokeScriptEvents( i
);
300 _rxManager
->registerScriptEvents(i
, elem
);
307 void SAL_CALL
OInterfaceContainer::writeEvents(const Reference
<XObjectOutputStream
>& _rxOutStream
)
309 // We're writing a document in SO 5.2 format (or even from earlier versions)
310 // -> convert the events from the new runtime format to the format of the 5.2 files
311 // but before, remember the current script events set for our children
312 ::std::vector
< Sequence
< ScriptEventDescriptor
> > aSave
;
313 if ( m_xEventAttacher
.is() )
314 lcl_saveEvents( aSave
, m_xEventAttacher
, m_aItems
.size() );
320 Reference
<XMarkableStream
> xMark(_rxOutStream
, UNO_QUERY
);
321 sal_Int32 nMark
= xMark
->createMark();
323 sal_Int32 nObjLen
= 0;
324 _rxOutStream
->writeLong(nObjLen
);
326 Reference
<XPersistObject
> xScripts(m_xEventAttacher
, UNO_QUERY
);
328 xScripts
->write(_rxOutStream
);
331 nObjLen
= xMark
->offsetToMark(nMark
) - 4;
332 xMark
->jumpToMark(nMark
);
333 _rxOutStream
->writeLong(nObjLen
);
334 xMark
->jumpToFurthest();
335 xMark
->deleteMark(nMark
);
337 catch( const Exception
& )
339 // restore the events
340 if ( m_xEventAttacher
.is() )
341 lcl_restoreEvents( aSave
, m_xEventAttacher
);
345 // restore the events
346 if ( m_xEventAttacher
.is() )
347 lcl_restoreEvents( aSave
, m_xEventAttacher
);
352 struct TransformEventTo52Format
354 void operator()( ScriptEventDescriptor
& _rDescriptor
)
356 if ( _rDescriptor
.ScriptType
!= "StarBasic" )
359 // it's a starbasic macro
360 sal_Int32 nPrefixLength
= _rDescriptor
.ScriptCode
.indexOf( ':' );
361 if ( 0 <= nPrefixLength
)
362 { // the macro name does not already contain a :
364 const OUString sPrefix
= _rDescriptor
.ScriptCode
.copy( 0, nPrefixLength
);
365 DBG_ASSERT( sPrefix
== "document"
366 || sPrefix
== "application",
367 "TransformEventTo52Format: invalid (unknown) prefix!" );
370 _rDescriptor
.ScriptCode
= _rDescriptor
.ScriptCode
.copy( nPrefixLength
+ 1 );
377 void OInterfaceContainer::transformEvents()
379 OSL_ENSURE( m_xEventAttacher
.is(), "OInterfaceContainer::transformEvents: no event attacher manager!" );
380 if ( !m_xEventAttacher
.is() )
385 // loop through all our children
386 sal_Int32 nItems
= m_aItems
.size();
387 Sequence
< ScriptEventDescriptor
> aChildEvents
;
389 for (sal_Int32 i
=0; i
<nItems
; ++i
)
391 // get the script events for this object
392 aChildEvents
= m_xEventAttacher
->getScriptEvents( i
);
394 if ( aChildEvents
.hasElements() )
396 // do the transformation
397 ::std::for_each( aChildEvents
.begin(), aChildEvents
.end(), TransformEventTo52Format() );
399 // revoke the script events
400 m_xEventAttacher
->revokeScriptEvents( i
);
401 // and re-register them
402 m_xEventAttacher
->registerScriptEvents( i
, aChildEvents
);
406 catch( const Exception
& )
408 DBG_UNHANDLED_EXCEPTION("forms.misc");
413 void SAL_CALL
OInterfaceContainer::readEvents(const Reference
<XObjectInputStream
>& _rxInStream
)
415 ::osl::MutexGuard
aGuard( m_rMutex
);
417 // Read scripting info
418 Reference
<XMarkableStream
> xMark(_rxInStream
, UNO_QUERY
);
419 sal_Int32 nObjLen
= _rxInStream
->readLong();
422 sal_Int32 nMark
= xMark
->createMark();
423 Reference
<XPersistObject
> xObj(m_xEventAttacher
, UNO_QUERY
);
425 xObj
->read(_rxInStream
);
426 xMark
->jumpToMark(nMark
);
427 _rxInStream
->skipBytes(nObjLen
);
428 xMark
->deleteMark(nMark
);
432 if ( m_xEventAttacher
.is() )
435 for (auto const& item
: m_aItems
)
437 Reference
< XInterface
> xAsIFace( item
, UNO_QUERY
); // important to normalize this...
438 Reference
< XPropertySet
> xAsSet( xAsIFace
, UNO_QUERY
);
439 m_xEventAttacher
->attach( i
++, xAsIFace
, makeAny( xAsSet
) );
445 void SAL_CALL
OInterfaceContainer::write( const Reference
< XObjectOutputStream
>& _rxOutStream
)
447 ::osl::MutexGuard
aGuard( m_rMutex
);
448 sal_Int32 nLen
= m_aItems
.size();
451 _rxOutStream
->writeLong(nLen
);
457 _rxOutStream
->writeShort(0x0001);
460 for (sal_Int32 i
= 0; i
< nLen
; i
++)
462 Reference
<XPersistObject
> xObj(m_aItems
[i
], UNO_QUERY
);
464 _rxOutStream
->writeObject(xObj
);
472 writeEvents(_rxOutStream
);
478 Reference
< XPersistObject
> lcl_createPlaceHolder( const Reference
< XComponentContext
>& _rxORB
)
480 Reference
< XPersistObject
> xObject( _rxORB
->getServiceManager()->createInstanceWithContext(FRM_COMPONENT_HIDDENCONTROL
, _rxORB
), UNO_QUERY
);
481 DBG_ASSERT( xObject
.is(), "lcl_createPlaceHolder: could not create a substitute for the unknown object!" );
484 // set some properties describing what we did
485 Reference
< XPropertySet
> xObjProps( xObject
, UNO_QUERY
);
490 xObjProps
->setPropertyValue( PROPERTY_NAME
, makeAny( FRM_RES_STRING( RID_STR_CONTROL_SUBSTITUTED_NAME
) ) );
491 xObjProps
->setPropertyValue( PROPERTY_TAG
, makeAny( FRM_RES_STRING( RID_STR_CONTROL_SUBSTITUTED_EPXPLAIN
) ) );
493 catch(const Exception
&)
503 void SAL_CALL
OInterfaceContainer::read( const Reference
< XObjectInputStream
>& _rxInStream
)
505 ::osl::MutexGuard
aGuard( m_rMutex
);
507 // after ::read the object is expected to be in the state it was when ::write was called, so we have
508 // to empty ourself here
512 // Only writes depending on the length
513 sal_Int32 nLen
= _rxInStream
->readLong();
518 _rxInStream
->readShort();
521 for (sal_Int32 i
= 0; i
< nLen
; i
++)
523 Reference
<XPersistObject
> xObj
;
526 xObj
= _rxInStream
->readObject();
528 catch(const WrongFormatException
&)
530 // the object could not be read
531 // create an object (so the readEvents below will assign the events to the right controls)
532 xObj
= lcl_createPlaceHolder( m_xContext
);
534 // couldn't handle it
537 catch(const Exception
&)
540 while (!m_aItems
.empty())
541 removeElementsNoEvents();
543 // Rethrow the exception
549 Reference
< XPropertySet
> xElement( xObj
, UNO_QUERY
);
553 m_aItems
.size(), // position
554 xElement
, // element to insert
555 false, // no event attacher manager handling
556 nullptr, // not yet approved - let implInsert do it
557 true // fire the event
560 catch( const Exception
& )
562 SAL_WARN("forms.misc", "OInterfaceContainerHelper2::read: reading succeeded, but not inserting!" );
563 // create a placeholder
564 xElement
.set(lcl_createPlaceHolder( m_xContext
), css::uno::UNO_QUERY
);
565 if ( !xElement
.is() )
566 // couldn't handle it
568 // insert the placeholder
569 implInsert( m_aItems
.size(), xElement
, false, nullptr, true );
574 readEvents(_rxInStream
);
580 m_xEventAttacher
= ::comphelper::createEventAttacherManager( m_xContext
);
581 OSL_ENSURE( m_xEventAttacher
.is(), "OInterfaceContainer::read: could not create an event attacher manager!" );
583 catch( const Exception
& )
585 DBG_UNHANDLED_EXCEPTION("forms.misc");
592 void SAL_CALL
OInterfaceContainer::addContainerListener(const Reference
<XContainerListener
>& _rxListener
)
594 m_aContainerListeners
.addInterface(_rxListener
);
598 void SAL_CALL
OInterfaceContainer::removeContainerListener(const Reference
<XContainerListener
>& _rxListener
)
600 m_aContainerListeners
.removeInterface(_rxListener
);
605 void SAL_CALL
OInterfaceContainer::disposing(const css::lang::EventObject
& _rSource
)
607 ::osl::MutexGuard
aGuard( m_rMutex
);
609 Reference
< XInterface
> xSource( _rSource
.Source
, UNO_QUERY
);
612 OInterfaceArray::iterator j
;
613 for ( j
= m_aItems
.begin(); j
!= m_aItems
.end(); ++j
)
615 DBG_ASSERT( j
->get() == Reference
< XInterface
>( *j
, UNO_QUERY
).get(),
616 "OInterfaceContainer::disposing: vector element not normalized!" );
618 if ( xSource
.get() == j
->get() )
623 if ( m_aItems
.end() == j
)
628 // look up in, and erase from, m_aMap, too
629 OInterfaceMap::iterator i
= m_aMap
.begin();
630 while ( i
!= m_aMap
.end() )
632 DBG_ASSERT( i
->second
.get() == Reference
< XInterface
>( i
->second
, UNO_QUERY
).get(),
633 "OInterfaceContainer::disposing: map element not normalized!" );
635 if ( i
->second
.get() == xSource
.get() )
644 DBG_ASSERT( i
!= m_aMap
.end(), "OInterfaceContainer::disposing: inconsistency: the element was in m_aItems, but not in m_aMap!" );
648 // XPropertyChangeListener
650 void OInterfaceContainer::propertyChange(const PropertyChangeEvent
& evt
) {
651 if (evt
.PropertyName
!= PROPERTY_NAME
)
654 ::osl::MutexGuard
aGuard( m_rMutex
);
655 auto range
= m_aMap
.equal_range(::comphelper::getString(evt
.OldValue
));
656 for (auto it
= range
.first
; it
!= range
.second
; ++it
)
657 if (it
->second
== evt
.Source
)
659 css::uno::Reference
<css::uno::XInterface
> xCorrectType(it
->second
);
661 m_aMap
.insert(::std::pair
<const OUString
, css::uno::Reference
<css::uno::XInterface
> >(::comphelper::getString(evt
.NewValue
),xCorrectType
));
668 sal_Bool SAL_CALL
OInterfaceContainer::hasElements()
670 return !m_aMap
.empty();
674 Type SAL_CALL
OInterfaceContainer::getElementType()
676 return m_aElementType
;
679 // XEnumerationAccess
681 Reference
<XEnumeration
> SAL_CALL
OInterfaceContainer::createEnumeration()
683 ::osl::MutexGuard
aGuard( m_rMutex
);
684 return new ::comphelper::OEnumerationByIndex(static_cast<XIndexAccess
*>(this));
689 Any SAL_CALL
OInterfaceContainer::getByName( const OUString
& _rName
)
691 ::std::pair
<OInterfaceMap::iterator
,
692 OInterfaceMap::iterator
> aPair
= m_aMap
.equal_range(_rName
);
694 if (aPair
.first
== aPair
.second
)
695 throw NoSuchElementException();
697 return (*aPair
.first
).second
->queryInterface( m_aElementType
);
701 css::uno::Sequence
<OUString
> SAL_CALL
OInterfaceContainer::getElementNames()
703 return comphelper::mapKeysToSequence(m_aMap
);
707 sal_Bool SAL_CALL
OInterfaceContainer::hasByName( const OUString
& _rName
)
709 ::std::pair
<OInterfaceMap::iterator
,
710 OInterfaceMap::iterator
> aPair
= m_aMap
.equal_range(_rName
);
711 return aPair
.first
!= aPair
.second
;
716 sal_Int32
OInterfaceContainer::getCount()
718 return m_aItems
.size();
722 Any
OInterfaceContainer::getByIndex(sal_Int32 _nIndex
)
724 if (_nIndex
< 0 || (_nIndex
>= static_cast<sal_Int32
>(m_aItems
.size())))
725 throw IndexOutOfBoundsException();
727 return m_aItems
[_nIndex
]->queryInterface( m_aElementType
);
731 void OInterfaceContainer::approveNewElement( const Reference
< XPropertySet
>& _rxObject
, ElementDescription
* _pElement
)
733 // it has to be non-NULL
734 if ( !_rxObject
.is() )
735 throw IllegalArgumentException(FRM_RES_STRING(RID_STR_NEED_NON_NULL_OBJECT
), static_cast<XContainer
*>(this), 1);
737 // it has to support our element type interface
738 Any aCorrectType
= _rxObject
->queryInterface( m_aElementType
);
739 if ( !aCorrectType
.hasValue() )
740 lcl_throwIllegalArgumentException();
742 // it has to have a "Name" property
743 if ( !hasProperty( PROPERTY_NAME
, _rxObject
) )
744 lcl_throwIllegalArgumentException();
746 // it has to be a child, and it must not have a parent already
747 Reference
< XChild
> xChild( _rxObject
, UNO_QUERY
);
748 if ( !xChild
.is() || xChild
->getParent().is() )
750 lcl_throwIllegalArgumentException();
753 // passed all tests. cache the information we have so far
754 DBG_ASSERT( _pElement
, "OInterfaceContainer::approveNewElement: invalid event descriptor!" );
757 _pElement
->xPropertySet
= _rxObject
;
758 _pElement
->xChild
= xChild
;
759 _pElement
->aElementTypeInterface
= aCorrectType
;
760 _pElement
->xInterface
= Reference
< XInterface
>( _rxObject
, UNO_QUERY
); // normalized XInterface
765 void OInterfaceContainer::implInsert(sal_Int32 _nIndex
, const Reference
< XPropertySet
>& _rxElement
,
766 bool _bEvents
, ElementDescription
* _pApprovalResult
, bool _bFire
)
768 const bool bHandleEvents
= _bEvents
&& m_xEventAttacher
.is();
770 // SYNCHRONIZED ----->
771 ::osl::ClearableMutexGuard
aGuard( m_rMutex
);
773 std::unique_ptr
< ElementDescription
> aAutoDeleteMetaData
;
774 ElementDescription
* pElementMetaData
= _pApprovalResult
;
775 if ( !pElementMetaData
)
776 { // not yet approved by the caller -> do ourself
777 pElementMetaData
= createElementMetaData();
778 DBG_ASSERT( pElementMetaData
, "OInterfaceContainer::implInsert: createElementMetaData returned nonsense!" );
780 // ensure that the meta data structure will be deleted later on
781 aAutoDeleteMetaData
.reset( pElementMetaData
);
783 // will throw an exception if necessary
784 approveNewElement( _rxElement
, pElementMetaData
);
788 // approveNewElement (no matter if called here or outside) has ensure that all relevant interfaces
791 // set the name, and add as change listener for the name
793 _rxElement
->getPropertyValue(PROPERTY_NAME
) >>= sName
;
794 _rxElement
->addPropertyChangeListener(PROPERTY_NAME
, this);
796 // insert the object into our internal structures
797 if (_nIndex
> static_cast<sal_Int32
>(m_aItems
.size())) // Calculate the actual index
799 _nIndex
= m_aItems
.size();
800 m_aItems
.push_back( pElementMetaData
->xInterface
);
803 m_aItems
.insert( m_aItems
.begin() + _nIndex
, pElementMetaData
->xInterface
);
805 m_aMap
.insert( ::std::pair
< const OUString
, css::uno::Reference
<css::uno::XInterface
> >( sName
, pElementMetaData
->xInterface
) );
807 // announce ourself as parent to the new element
808 pElementMetaData
->xChild
->setParent(static_cast<XContainer
*>(this));
813 m_xEventAttacher
->insertEntry(_nIndex
);
814 m_xEventAttacher
->attach( _nIndex
, pElementMetaData
->xInterface
, makeAny( _rxElement
) );
817 // notify derived classes
818 implInserted( pElementMetaData
);
821 // <----- SYNCHRONIZED
823 // insert faked VBA events?
824 bool bHandleVbaEvents
= false;
827 _rxElement
->getPropertyValue("GenerateVbaEvents") >>= bHandleVbaEvents
;
829 catch( const Exception
& )
832 if ( bHandleVbaEvents
)
834 Reference
< XEventAttacherManager
> xMgr ( pElementMetaData
->xInterface
, UNO_QUERY
);
835 OInterfaceContainer
* pIfcMgr
= xMgr
.is() ? dynamic_cast<OInterfaceContainer
*>(xMgr
.get()) : nullptr;
838 sal_Int32 nLen
= pIfcMgr
->getCount();
839 for (sal_Int32 i
= 0; i
< nLen
; ++i
)
841 // add fake events to the control at index i
842 pIfcMgr
->impl_addVbEvents_nolck_nothrow( i
);
847 // add fake events to the control at index i
848 impl_addVbEvents_nolck_nothrow( _nIndex
);
852 // fire the notification about the change
857 aEvt
.Source
= static_cast<XContainer
*>(this);
858 aEvt
.Accessor
<<= _nIndex
;
859 aEvt
.Element
= pElementMetaData
->aElementTypeInterface
;
861 m_aContainerListeners
.notifyEach( &XContainerListener::elementInserted
, aEvt
);
866 void OInterfaceContainer::removeElementsNoEvents()
868 OInterfaceArray::iterator i
= m_aItems
.begin();
869 css::uno::Reference
<css::uno::XInterface
> xElement(*i
);
871 OInterfaceMap::iterator j
= std::find_if(m_aMap
.begin(), m_aMap
.end(),
872 [&xElement
](const OInterfaceMap::value_type
& rEntry
) { return rEntry
.second
== xElement
; });
877 Reference
<XPropertySet
> xSet(xElement
, UNO_QUERY
);
879 xSet
->removePropertyChangeListener(PROPERTY_NAME
, this);
881 Reference
<XChild
> xChild(xElement
, UNO_QUERY
);
883 xChild
->setParent(css::uno::Reference
<css::uno::XInterface
> ());
887 void OInterfaceContainer::implInserted( const ElementDescription
* /*_pElement*/ )
893 void OInterfaceContainer::implRemoved( const css::uno::Reference
<css::uno::XInterface
>& /*_rxObject*/ )
899 void OInterfaceContainer::impl_replacedElement( const ContainerEvent
& _rEvent
, ::osl::ClearableMutexGuard
& _rInstanceLock
)
901 _rInstanceLock
.clear();
902 m_aContainerListeners
.notifyEach( &XContainerListener::elementReplaced
, _rEvent
);
907 void SAL_CALL
OInterfaceContainer::insertByIndex( sal_Int32 _nIndex
, const Any
& _rElement
)
909 Reference
< XPropertySet
> xElement
;
910 _rElement
>>= xElement
;
911 implInsert( _nIndex
, xElement
, true /* event handling */ , nullptr /* not yet approved */ , true /* notification */ );
915 void OInterfaceContainer::implReplaceByIndex( const sal_Int32 _nIndex
, const Any
& _rNewElement
, ::osl::ClearableMutexGuard
& _rClearBeforeNotify
)
917 OSL_PRECOND( ( _nIndex
>= 0 ) && ( _nIndex
< static_cast<sal_Int32
>(m_aItems
.size()) ), "OInterfaceContainer::implReplaceByIndex: precondition not met (index)!" );
919 // approve the new object
920 std::unique_ptr
< ElementDescription
> aElementMetaData( createElementMetaData() );
921 DBG_ASSERT(aElementMetaData
,
922 "OInterfaceContainer::implReplaceByIndex: createElementMetaData returned nonsense!");
924 Reference
< XPropertySet
> xElementProps
;
925 _rNewElement
>>= xElementProps
;
926 approveNewElement( xElementProps
, aElementMetaData
.get() );
929 // get the old element
930 css::uno::Reference
<css::uno::XInterface
> xOldElement( m_aItems
[ _nIndex
] );
931 DBG_ASSERT( xOldElement
.get() == Reference
< XInterface
>( xOldElement
, UNO_QUERY
).get(),
932 "OInterfaceContainer::implReplaceByIndex: elements should be held normalized!" );
934 // locate the old element in the map
935 OInterfaceMap::iterator j
= std::find_if(m_aMap
.begin(), m_aMap
.end(),
936 [&xOldElement
](const OInterfaceMap::value_type
& rEntry
) { return rEntry
.second
.get() == xOldElement
.get(); });
938 // remove event knittings
939 if ( m_xEventAttacher
.is() )
941 css::uno::Reference
<css::uno::XInterface
> xNormalized( xOldElement
, UNO_QUERY
);
942 m_xEventAttacher
->detach( _nIndex
, xNormalized
);
943 m_xEventAttacher
->removeEntry( _nIndex
);
946 // don't listen for property changes anymore
947 Reference
<XPropertySet
> xSet( xOldElement
, UNO_QUERY
);
949 xSet
->removePropertyChangeListener(PROPERTY_NAME
, this);
951 // give the old element a new (void) parent
952 Reference
<XChild
> xChild(xOldElement
, UNO_QUERY
);
954 xChild
->setParent(css::uno::Reference
<css::uno::XInterface
> ());
956 // remove the old one
959 // examine the new element
961 DBG_ASSERT(aElementMetaData
->xPropertySet
.is(),
962 "OInterfaceContainer::implReplaceByIndex: what did approveNewElement do?");
964 aElementMetaData
->xPropertySet
->getPropertyValue(PROPERTY_NAME
) >>= sName
;
965 aElementMetaData
->xPropertySet
->addPropertyChangeListener(PROPERTY_NAME
, this);
967 // insert the new one
968 m_aMap
.insert(::std::pair
<const OUString
, css::uno::Reference
<css::uno::XInterface
>>(
969 sName
, aElementMetaData
->xInterface
));
970 m_aItems
[_nIndex
] = aElementMetaData
->xInterface
;
972 aElementMetaData
->xChild
->setParent(static_cast<XContainer
*>(this));
974 if ( m_xEventAttacher
.is() )
976 m_xEventAttacher
->insertEntry( _nIndex
);
977 m_xEventAttacher
->attach(_nIndex
, aElementMetaData
->xInterface
,
978 makeAny(aElementMetaData
->xPropertySet
));
981 ContainerEvent aReplaceEvent
;
982 aReplaceEvent
.Source
= static_cast< XContainer
* >( this );
983 aReplaceEvent
.Accessor
<<= _nIndex
;
984 aReplaceEvent
.Element
= aElementMetaData
->xInterface
->queryInterface(m_aElementType
);
985 aReplaceEvent
.ReplacedElement
= xOldElement
->queryInterface( m_aElementType
);
987 impl_replacedElement( aReplaceEvent
, _rClearBeforeNotify
);
991 void OInterfaceContainer::implCheckIndex( const sal_Int32 _nIndex
)
993 if (_nIndex
< 0 || _nIndex
>= static_cast<sal_Int32
>(m_aItems
.size()))
994 throw IndexOutOfBoundsException();
998 void SAL_CALL
OInterfaceContainer::replaceByIndex(sal_Int32 _nIndex
, const Any
& Element
)
1000 ::osl::ClearableMutexGuard
aGuard( m_rMutex
);
1002 implCheckIndex( _nIndex
);
1004 implReplaceByIndex( _nIndex
, Element
, aGuard
);
1008 void OInterfaceContainer::implRemoveByIndex( const sal_Int32 _nIndex
, ::osl::ClearableMutexGuard
& _rClearBeforeNotify
)
1010 OSL_PRECOND( ( _nIndex
>= 0 ) && ( _nIndex
< static_cast<sal_Int32
>(m_aItems
.size()) ), "OInterfaceContainer::implRemoveByIndex: precondition not met (index)!" );
1012 OInterfaceArray::iterator i
= m_aItems
.begin() + _nIndex
;
1013 css::uno::Reference
<css::uno::XInterface
> xElement(*i
);
1015 OInterfaceMap::iterator j
= std::find_if(m_aMap
.begin(), m_aMap
.end(),
1016 [&xElement
](const OInterfaceMap::value_type
& rEntry
) { return rEntry
.second
== xElement
; });
1021 // remove event knittings
1022 if ( m_xEventAttacher
.is() )
1024 css::uno::Reference
<css::uno::XInterface
> xNormalized( xElement
, UNO_QUERY
);
1025 m_xEventAttacher
->detach( _nIndex
, xNormalized
);
1026 m_xEventAttacher
->removeEntry( _nIndex
);
1029 Reference
<XPropertySet
> xSet(xElement
, UNO_QUERY
);
1031 xSet
->removePropertyChangeListener(PROPERTY_NAME
, this);
1033 Reference
<XChild
> xChild(xElement
, UNO_QUERY
);
1035 xChild
->setParent(css::uno::Reference
<css::uno::XInterface
> ());
1037 // notify derived classes
1038 implRemoved(xElement
);
1041 ContainerEvent aEvt
;
1042 aEvt
.Source
= static_cast<XContainer
*>(this);
1043 aEvt
.Element
= xElement
->queryInterface( m_aElementType
);
1044 aEvt
.Accessor
<<= _nIndex
;
1046 _rClearBeforeNotify
.clear();
1047 m_aContainerListeners
.notifyEach( &XContainerListener::elementRemoved
, aEvt
);
1051 void SAL_CALL
OInterfaceContainer::removeByIndex(sal_Int32 _nIndex
)
1053 ::osl::ClearableMutexGuard
aGuard( m_rMutex
);
1055 implCheckIndex( _nIndex
);
1057 implRemoveByIndex( _nIndex
, aGuard
);
1061 ElementDescription
* OInterfaceContainer::createElementMetaData( )
1063 return new ElementDescription
;
1067 void SAL_CALL
OInterfaceContainer::insertByName(const OUString
& _rName
, const Any
& _rElement
)
1069 Reference
< XPropertySet
> xElementProps
;
1071 std::unique_ptr
< ElementDescription
> aElementMetaData( createElementMetaData() );
1072 DBG_ASSERT(aElementMetaData
,
1073 "OInterfaceContainer::insertByName: createElementMetaData returned nonsense!");
1075 // ensure the correct name of the element
1078 _rElement
>>= xElementProps
;
1079 approveNewElement( xElementProps
, aElementMetaData
.get() );
1081 xElementProps
->setPropertyValue( PROPERTY_NAME
, makeAny( _rName
) );
1083 catch( const IllegalArgumentException
& )
1085 throw; // allowed to leave
1087 catch( const ElementExistException
& )
1089 throw; // allowed to leave
1091 catch( const Exception
& )
1093 TOOLS_WARN_EXCEPTION("forms.misc", "OInterfaceContainer::insertByName" );
1095 implInsert( m_aItems
.size(), xElementProps
, true, aElementMetaData
.get(), true );
1099 void SAL_CALL
OInterfaceContainer::replaceByName(const OUString
& Name
, const Any
& Element
)
1101 ::osl::ClearableMutexGuard
aGuard( m_rMutex
);
1102 ::std::pair
<OInterfaceMap::iterator
,
1103 OInterfaceMap::iterator
> aPair
= m_aMap
.equal_range(Name
);
1104 if (aPair
.first
== aPair
.second
)
1105 throw NoSuchElementException();
1107 if (Element
.getValueType().getTypeClass() != TypeClass_INTERFACE
)
1108 lcl_throwIllegalArgumentException();
1110 Reference
<XPropertySet
> xSet
;
1114 if (!hasProperty(PROPERTY_NAME
, xSet
))
1115 lcl_throwIllegalArgumentException();
1117 xSet
->setPropertyValue(PROPERTY_NAME
, makeAny(Name
));
1120 // determine the element pos
1121 sal_Int32 nPos
= ::std::find(m_aItems
.begin(), m_aItems
.end(), (*aPair
.first
).second
) - m_aItems
.begin();
1123 implReplaceByIndex( nPos
, Element
, aGuard
);
1127 void SAL_CALL
OInterfaceContainer::removeByName(const OUString
& Name
)
1129 ::osl::MutexGuard
aGuard( m_rMutex
);
1130 ::std::pair
<OInterfaceMap::iterator
,
1131 OInterfaceMap::iterator
> aPair
= m_aMap
.equal_range(Name
);
1132 if (aPair
.first
== aPair
.second
)
1133 throw NoSuchElementException();
1135 sal_Int32 nPos
= ::std::find(m_aItems
.begin(), m_aItems
.end(), (*aPair
.first
).second
) - m_aItems
.begin();
1136 removeByIndex(nPos
);
1140 // XEventAttacherManager
1142 void SAL_CALL
OInterfaceContainer::registerScriptEvent( sal_Int32 nIndex
, const ScriptEventDescriptor
& aScriptEvent
)
1144 ::osl::ClearableMutexGuard
aGuard( m_rMutex
);
1145 if ( m_xEventAttacher
.is() )
1147 m_xEventAttacher
->registerScriptEvent( nIndex
, aScriptEvent
);
1149 impl_addVbEvents_nolck_nothrow( nIndex
); // add fake vba events
1154 void SAL_CALL
OInterfaceContainer::registerScriptEvents( sal_Int32 nIndex
, const Sequence
< ScriptEventDescriptor
>& aScriptEvents
)
1156 ::osl::ClearableMutexGuard
aGuard( m_rMutex
);
1157 if ( m_xEventAttacher
.is() )
1159 m_xEventAttacher
->registerScriptEvents( nIndex
, aScriptEvents
);
1161 impl_addVbEvents_nolck_nothrow( nIndex
); // add fake vba events
1166 void SAL_CALL
OInterfaceContainer::revokeScriptEvent( sal_Int32 nIndex
, const OUString
& aListenerType
, const OUString
& aEventMethod
, const OUString
& aRemoveListenerParam
)
1168 if ( m_xEventAttacher
.is() )
1169 m_xEventAttacher
->revokeScriptEvent( nIndex
, aListenerType
, aEventMethod
, aRemoveListenerParam
);
1173 void SAL_CALL
OInterfaceContainer::revokeScriptEvents( sal_Int32 nIndex
)
1175 if ( m_xEventAttacher
.is() )
1176 m_xEventAttacher
->revokeScriptEvents( nIndex
);
1180 void SAL_CALL
OInterfaceContainer::insertEntry( sal_Int32 nIndex
)
1182 if ( m_xEventAttacher
.is() )
1183 m_xEventAttacher
->insertEntry( nIndex
);
1187 void SAL_CALL
OInterfaceContainer::removeEntry( sal_Int32 nIndex
)
1189 if ( m_xEventAttacher
.is() )
1190 m_xEventAttacher
->removeEntry( nIndex
);
1194 Sequence
< ScriptEventDescriptor
> SAL_CALL
OInterfaceContainer::getScriptEvents( sal_Int32 nIndex
)
1196 Sequence
< ScriptEventDescriptor
> aReturn
;
1197 if ( m_xEventAttacher
.is() )
1199 aReturn
= m_xEventAttacher
->getScriptEvents( nIndex
);
1200 if ( lcl_hasVbaEvents( aReturn
) )
1202 aReturn
= lcl_stripVbaEvents( aReturn
);
1209 void SAL_CALL
OInterfaceContainer::attach( sal_Int32 nIndex
, const Reference
< XInterface
>& xObject
, const Any
& aHelper
)
1211 if ( m_xEventAttacher
.is() )
1212 m_xEventAttacher
->attach( nIndex
, xObject
, aHelper
);
1216 void SAL_CALL
OInterfaceContainer::detach( sal_Int32 nIndex
, const Reference
< XInterface
>& xObject
)
1218 if ( m_xEventAttacher
.is() )
1219 m_xEventAttacher
->detach( nIndex
, xObject
);
1223 void SAL_CALL
OInterfaceContainer::addScriptListener( const Reference
< XScriptListener
>& xListener
)
1225 if ( m_xEventAttacher
.is() )
1226 m_xEventAttacher
->addScriptListener( xListener
);
1230 void SAL_CALL
OInterfaceContainer::removeScriptListener( const Reference
< XScriptListener
>& xListener
)
1232 if ( m_xEventAttacher
.is() )
1233 m_xEventAttacher
->removeScriptListener( xListener
);
1240 Any SAL_CALL
OFormComponents::queryAggregation(const Type
& _rType
)
1242 Any aReturn
= OFormComponents_BASE::queryInterface(_rType
);
1243 if (!aReturn
.hasValue())
1245 aReturn
= OInterfaceContainer::queryInterface(_rType
);
1247 if (!aReturn
.hasValue())
1248 aReturn
= ::cppu::OComponentHelper::queryAggregation(_rType
);
1255 Sequence
<Type
> SAL_CALL
OFormComponents::getTypes()
1257 return ::comphelper::concatSequences(OInterfaceContainer::getTypes(), ::cppu::OComponentHelper::getTypes(), OFormComponents_BASE::getTypes());
1261 OFormComponents::OFormComponents(const Reference
<XComponentContext
>& _rxFactory
)
1262 : ::cppu::OComponentHelper( m_aMutex
)
1263 ,OInterfaceContainer( _rxFactory
, m_aMutex
, cppu::UnoType
<XFormComponent
>::get() )
1264 ,OFormComponents_BASE()
1269 OFormComponents::OFormComponents( const OFormComponents
& _cloneSource
)
1270 : ::cppu::OComponentHelper( m_aMutex
)
1271 ,OInterfaceContainer( m_aMutex
, _cloneSource
)
1272 ,OFormComponents_BASE()
1277 OFormComponents::~OFormComponents()
1279 if (! ::cppu::OComponentHelper::rBHelper
.bDisposed
)
1288 void OFormComponents::disposing()
1290 OInterfaceContainer::disposing();
1291 ::cppu::OComponentHelper::disposing();
1292 m_xParent
= nullptr;
1297 void OFormComponents::setParent(const css::uno::Reference
<css::uno::XInterface
>& Parent
)
1299 ::osl::MutexGuard
aGuard( m_aMutex
);
1304 css::uno::Reference
<css::uno::XInterface
> OFormComponents::getParent()
1313 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */