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 "frm_resource.hrc"
22 #include "frm_resource.hxx"
23 #include "InterfaceContainer.hxx"
24 #include "componenttools.hxx"
25 #include "property.hrc"
26 #include "services.hxx"
28 #include <com/sun/star/beans/XPropertySet.hpp>
29 #include <com/sun/star/container/XNamed.hpp>
30 #include <com/sun/star/io/WrongFormatException.hpp>
31 #include <com/sun/star/io/XMarkableStream.hpp>
32 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
33 #include <com/sun/star/lang/XComponent.hpp>
34 #include <com/sun/star/util/XCloneable.hpp>
35 #include <com/sun/star/form/XForm.hpp>
37 #include <comphelper/container.hxx>
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>
54 #include <comphelper/processfactory.hxx>
60 using namespace ::com::sun::star::frame
;
61 using namespace ::com::sun::star::lang
;
62 using namespace ::com::sun::star::uno
;
63 using namespace ::com::sun::star::beans
;
64 using namespace ::com::sun::star::document
;
65 using namespace ::com::sun::star::container
;
66 using namespace ::com::sun::star::script
;
67 using namespace ::com::sun::star::io
;
68 using namespace ::com::sun::star::form
;
69 using namespace ::com::sun::star::util
;
74 void lcl_throwIllegalArgumentException()
76 throw IllegalArgumentException();
81 lcl_hasVbaEvents( const Sequence
< ScriptEventDescriptor
>& sEvents
)
83 const ScriptEventDescriptor
* pDesc
= sEvents
.getConstArray();
84 const ScriptEventDescriptor
* pEnd
= ( pDesc
+ sEvents
.getLength() );
85 for ( ; pDesc
!= pEnd
; ++pDesc
)
87 if ( pDesc
->ScriptType
== "VBAInterop" )
93 Sequence
< ScriptEventDescriptor
>
94 lcl_stripVbaEvents( const Sequence
< ScriptEventDescriptor
>& sEvents
)
96 Sequence
< ScriptEventDescriptor
> sStripped( sEvents
.getLength() );
98 const ScriptEventDescriptor
* pDesc
= sEvents
.getConstArray();
99 const ScriptEventDescriptor
* pEnd
= ( pDesc
+ sEvents
.getLength() );
100 sal_Int32 nCopied
= 0;
101 for ( ; pDesc
!= pEnd
; ++pDesc
)
103 if ( pDesc
->ScriptType
!= "VBAInterop" )
105 sStripped
[ nCopied
++ ] = *pDesc
;
109 sStripped
.realloc( nCopied
);
113 void OInterfaceContainer::impl_addVbEvents_nolck_nothrow( const sal_Int32 i_nIndex
)
115 // we are dealing with form controls
120 Reference
< XModel
> xDoc( getXModel( static_cast< XContainer
*> ( this ) ) );
124 Reference
< XMultiServiceFactory
> xDocFac( xDoc
, UNO_QUERY_THROW
);
125 Reference
< XCodeNameQuery
> xNameQuery( xDocFac
->createInstance("ooo.vba.VBACodeNameProvider"), UNO_QUERY
);
126 if ( !xNameQuery
.is() )
129 ::osl::MutexGuard
aGuard( m_rMutex
);
130 bool hasVBABindings
= lcl_hasVbaEvents( m_xEventAttacher
->getScriptEvents( i_nIndex
) );
131 if ( hasVBABindings
)
134 Reference
< XInterface
> xElement( getByIndex( i_nIndex
) , UNO_QUERY_THROW
);
135 Reference
< XForm
> xElementAsForm( xElement
, UNO_QUERY
);
136 if ( xElementAsForm
.is() )
139 // Try getting the code name from the container first (faster),
140 // then from the element if that fails (slower).
141 Reference
<XInterface
> xThis
= static_cast<XContainer
*>(this);
142 OUString sCodeName
= xNameQuery
->getCodeNameForContainer(xThis
);
143 if (sCodeName
.isEmpty())
144 sCodeName
= xNameQuery
->getCodeNameForObject(xElement
);
146 Reference
< XPropertySet
> xProps( xElement
, UNO_QUERY_THROW
);
147 OUString sServiceName
;
148 xProps
->getPropertyValue("DefaultControl") >>= sServiceName
;
150 Reference
< ooo::vba::XVBAToOOEventDescGen
> xDescSupplier( m_xContext
->getServiceManager()->createInstanceWithContext("ooo.vba.VBAToOOEventDesc", m_xContext
), UNO_QUERY_THROW
);
151 Sequence
< ScriptEventDescriptor
> vbaEvents
= xDescSupplier
->getEventDescriptions( sServiceName
, sCodeName
);
153 // register the vba script events
154 m_xEventAttacher
->registerScriptEvents( i_nIndex
, vbaEvents
);
158 catch ( const ServiceNotRegisteredException
& )
160 // silence this, not all document types support the ooo.vba.VBACodeNameProvider service
162 catch( const Exception
& )
164 DBG_UNHANDLED_EXCEPTION();
169 ElementDescription::ElementDescription( )
174 ElementDescription::~ElementDescription()
178 OInterfaceContainer::OInterfaceContainer(
179 const Reference
<XComponentContext
>& _rxContext
,
180 ::osl::Mutex
& _rMutex
,
181 const Type
& _rElementType
)
182 :OInterfaceContainer_BASE()
184 ,m_aContainerListeners(_rMutex
)
185 ,m_aElementType(_rElementType
)
186 ,m_xContext(_rxContext
)
188 impl_createEventAttacher_nothrow();
192 OInterfaceContainer::OInterfaceContainer( ::osl::Mutex
& _rMutex
, const OInterfaceContainer
& _cloneSource
)
193 :OInterfaceContainer_BASE()
195 ,m_aContainerListeners( _rMutex
)
196 ,m_aElementType( _cloneSource
.m_aElementType
)
197 ,m_xContext( _cloneSource
.m_xContext
)
199 impl_createEventAttacher_nothrow();
202 void OInterfaceContainer::clonedFrom(const OInterfaceContainer
& _cloneSource
) throw(RuntimeException
, std::exception
)
206 const Reference
< XIndexAccess
> xSourceHierarchy( const_cast< OInterfaceContainer
* >( &_cloneSource
) );
207 const sal_Int32 nCount
= xSourceHierarchy
->getCount();
208 for ( sal_Int32 i
=0; i
<nCount
; ++i
)
210 Reference
< XCloneable
> xCloneable( xSourceHierarchy
->getByIndex( i
), UNO_QUERY_THROW
);
211 Reference
< XInterface
> xClone( xCloneable
->createClone() );
212 insertByIndex( i
, makeAny( xClone
) );
215 catch (const RuntimeException
&)
219 catch (const Exception
&)
221 throw WrappedTargetRuntimeException(
222 "Could not clone the given interface hierarchy.",
223 static_cast< XIndexContainer
* >( const_cast< OInterfaceContainer
* >( &_cloneSource
) ),
224 ::cppu::getCaughtException()
229 void OInterfaceContainer::impl_createEventAttacher_nothrow()
233 m_xEventAttacher
.set( ::comphelper::createEventAttacherManager( m_xContext
), UNO_SET_THROW
);
235 catch( const Exception
& )
237 DBG_UNHANDLED_EXCEPTION();
242 OInterfaceContainer::~OInterfaceContainer()
247 void OInterfaceContainer::disposing()
249 // dispose all elements
250 for (sal_Int32 i
= m_aItems
.size(); i
> 0; --i
)
252 Reference
<XPropertySet
> xSet(m_aItems
[i
- 1], UNO_QUERY
);
254 xSet
->removePropertyChangeListener(PROPERTY_NAME
, this);
256 // revoke event knittings
257 if ( m_xEventAttacher
.is() )
259 m_xEventAttacher
->detach( i
- 1, Reference
<XInterface
>(xSet
, UNO_QUERY
) );
260 m_xEventAttacher
->removeEntry( i
- 1 );
263 Reference
<XComponent
> xComponent(xSet
, UNO_QUERY
);
265 xComponent
->dispose();
270 css::lang::EventObject
aEvt(static_cast<XContainer
*>(this));
271 m_aContainerListeners
.disposeAndClear(aEvt
);
279 void lcl_saveEvents( ::std::vector
< Sequence
< ScriptEventDescriptor
> >& _rSave
,
280 const Reference
< XEventAttacherManager
>& _rxManager
, const sal_Int32 _nItemCount
)
282 OSL_ENSURE( _rxManager
.is(), "lcl_saveEvents: invalid event attacher manager!" );
283 if ( !_rxManager
.is() )
286 // reserve the space needed
287 _rSave
.reserve( _nItemCount
);
290 for (sal_Int32 i
=0; i
<_nItemCount
; ++i
)
291 _rSave
.push_back(_rxManager
->getScriptEvents( i
));
295 void lcl_restoreEvents( const ::std::vector
< Sequence
< ScriptEventDescriptor
> >& _rSave
,
296 const Reference
< XEventAttacherManager
>& _rxManager
)
298 OSL_ENSURE( _rxManager
.is(), "lcl_restoreEvents: invalid event attacher manager!" );
299 if ( !_rxManager
.is() )
302 ::std::vector
< Sequence
< ScriptEventDescriptor
> >::const_iterator aLoop
= _rSave
.begin();
303 ::std::vector
< Sequence
< ScriptEventDescriptor
> >::const_iterator aEnd
= _rSave
.end();
304 for ( sal_Int32 i
=0; aLoop
!= aEnd
; ++aLoop
, ++i
)
306 _rxManager
->revokeScriptEvents( i
);
307 _rxManager
->registerScriptEvents( i
, *aLoop
);
313 void SAL_CALL
OInterfaceContainer::writeEvents(const Reference
<XObjectOutputStream
>& _rxOutStream
)
315 // We're writing a document in SO 5.2 format (or even from earlier versions)
316 // -> convert the events from the new runtime format to the format of the 5.2 files
317 // but before, remember the current script events set for our children
318 ::std::vector
< Sequence
< ScriptEventDescriptor
> > aSave
;
319 if ( m_xEventAttacher
.is() )
320 lcl_saveEvents( aSave
, m_xEventAttacher
, m_aItems
.size() );
322 transformEvents( efVersionSO5x
);
326 Reference
<XMarkableStream
> xMark(_rxOutStream
, UNO_QUERY
);
327 sal_Int32 nMark
= xMark
->createMark();
329 sal_Int32 nObjLen
= 0;
330 _rxOutStream
->writeLong(nObjLen
);
332 Reference
<XPersistObject
> xScripts(m_xEventAttacher
, UNO_QUERY
);
334 xScripts
->write(_rxOutStream
);
337 nObjLen
= xMark
->offsetToMark(nMark
) - 4;
338 xMark
->jumpToMark(nMark
);
339 _rxOutStream
->writeLong(nObjLen
);
340 xMark
->jumpToFurthest();
341 xMark
->deleteMark(nMark
);
343 catch( const Exception
& )
345 // restore the events
346 if ( m_xEventAttacher
.is() )
347 lcl_restoreEvents( aSave
, m_xEventAttacher
);
351 // restore the events
352 if ( m_xEventAttacher
.is() )
353 lcl_restoreEvents( aSave
, m_xEventAttacher
);
357 struct TransformEventTo52Format
: public ::std::unary_function
< ScriptEventDescriptor
, void >
359 void operator()( ScriptEventDescriptor
& _rDescriptor
)
361 if ( _rDescriptor
.ScriptType
== "StarBasic" )
362 { // it's a starbasic macro
363 sal_Int32 nPrefixLength
= _rDescriptor
.ScriptCode
.indexOf( ':' );
364 if ( 0 <= nPrefixLength
)
365 { // the macro name does not already contain a :
367 const OUString sPrefix
= _rDescriptor
.ScriptCode
.copy( 0, nPrefixLength
);
368 DBG_ASSERT( sPrefix
== "document"
369 || sPrefix
== "application",
370 "TransformEventTo52Format: invalid (unknown) prefix!" );
373 _rDescriptor
.ScriptCode
= _rDescriptor
.ScriptCode
.copy( nPrefixLength
+ 1 );
380 struct TransformEventTo60Format
: public ::std::unary_function
< ScriptEventDescriptor
, void >
382 void operator()( ScriptEventDescriptor
& _rDescriptor
)
384 if ( _rDescriptor
.ScriptType
== "StarBasic" )
385 { // it's a starbasic macro
386 if ( _rDescriptor
.ScriptCode
.indexOf( ':' ) < 0 )
387 { // the macro name does not already contain a :
388 // -> default the type to "document"
389 _rDescriptor
.ScriptCode
= "document:" + _rDescriptor
.ScriptCode
;
396 void OInterfaceContainer::transformEvents( const EventFormat _eTargetFormat
)
398 OSL_ENSURE( m_xEventAttacher
.is(), "OInterfaceContainer::transformEvents: no event attacher manager!" );
399 if ( !m_xEventAttacher
.is() )
404 // loop through all our children
405 sal_Int32 nItems
= m_aItems
.size();
406 Sequence
< ScriptEventDescriptor
> aChildEvents
;
408 for (sal_Int32 i
=0; i
<nItems
; ++i
)
410 // get the script events for this object
411 aChildEvents
= m_xEventAttacher
->getScriptEvents( i
);
413 if ( aChildEvents
.getLength() )
415 // the "iterators" for the events for this child
416 ScriptEventDescriptor
* pChildEvents
= aChildEvents
.getArray();
417 ScriptEventDescriptor
* pChildEventsEnd
= pChildEvents
+ aChildEvents
.getLength();
419 // do the transformation
420 if ( efVersionSO6x
== _eTargetFormat
)
421 ::std::for_each( pChildEvents
, pChildEventsEnd
, TransformEventTo60Format() );
423 ::std::for_each( pChildEvents
, pChildEventsEnd
, TransformEventTo52Format() );
425 // revoke the script events
426 m_xEventAttacher
->revokeScriptEvents( i
);
427 // and re-register them
428 m_xEventAttacher
->registerScriptEvents( i
, aChildEvents
);
432 catch( const Exception
& )
434 DBG_UNHANDLED_EXCEPTION();
439 void SAL_CALL
OInterfaceContainer::readEvents(const Reference
<XObjectInputStream
>& _rxInStream
)
441 ::osl::MutexGuard
aGuard( m_rMutex
);
443 // Read scripting info
444 Reference
<XMarkableStream
> xMark(_rxInStream
, UNO_QUERY
);
445 sal_Int32 nObjLen
= _rxInStream
->readLong();
448 sal_Int32 nMark
= xMark
->createMark();
449 Reference
<XPersistObject
> xObj(m_xEventAttacher
, UNO_QUERY
);
451 xObj
->read(_rxInStream
);
452 xMark
->jumpToMark(nMark
);
453 _rxInStream
->skipBytes(nObjLen
);
454 xMark
->deleteMark(nMark
);
458 if ( m_xEventAttacher
.is() )
460 OInterfaceArray::const_iterator aAttach
= m_aItems
.begin();
461 OInterfaceArray::const_iterator aAttachEnd
= m_aItems
.end();
462 for ( sal_Int32 i
=0; aAttach
!= aAttachEnd
; ++aAttach
, ++i
)
464 Reference
< XInterface
> xAsIFace( *aAttach
, UNO_QUERY
); // important to normalize this ....
465 Reference
< XPropertySet
> xAsSet( xAsIFace
, UNO_QUERY
);
466 m_xEventAttacher
->attach( i
, xAsIFace
, makeAny( xAsSet
) );
472 void SAL_CALL
OInterfaceContainer::write( const Reference
< XObjectOutputStream
>& _rxOutStream
) throw(IOException
, RuntimeException
, std::exception
)
474 ::osl::MutexGuard
aGuard( m_rMutex
);
475 sal_Int32 nLen
= m_aItems
.size();
478 _rxOutStream
->writeLong(nLen
);
483 _rxOutStream
->writeShort(0x0001);
486 for (sal_Int32 i
= 0; i
< nLen
; i
++)
488 Reference
<XPersistObject
> xObj(m_aItems
[i
], UNO_QUERY
);
490 _rxOutStream
->writeObject(xObj
);
498 writeEvents(_rxOutStream
);
505 Reference
< XPersistObject
> lcl_createPlaceHolder( const Reference
< XComponentContext
>& _rxORB
)
507 Reference
< XPersistObject
> xObject( _rxORB
->getServiceManager()->createInstanceWithContext(FRM_COMPONENT_HIDDENCONTROL
, _rxORB
), UNO_QUERY
);
508 DBG_ASSERT( xObject
.is(), "lcl_createPlaceHolder: could not create a substitute for the unknown object!" );
511 // set some properties describing what we did
512 Reference
< XPropertySet
> xObjProps( xObject
, UNO_QUERY
);
517 xObjProps
->setPropertyValue( PROPERTY_NAME
, makeAny( FRM_RES_STRING( RID_STR_CONTROL_SUBSTITUTED_NAME
) ) );
518 xObjProps
->setPropertyValue( PROPERTY_TAG
, makeAny( FRM_RES_STRING( RID_STR_CONTROL_SUBSTITUTED_EPXPLAIN
) ) );
520 catch(const Exception
&)
530 void SAL_CALL
OInterfaceContainer::read( const Reference
< XObjectInputStream
>& _rxInStream
) throw(IOException
, RuntimeException
, std::exception
)
532 ::osl::MutexGuard
aGuard( m_rMutex
);
534 // after ::read the object is expected to be in the state it was when ::write was called, so we have
535 // to empty ourself here
539 // Only writes depending on the length
540 sal_Int32 nLen
= _rxInStream
->readLong();
545 sal_uInt16 nVersion
= _rxInStream
->readShort(); (void)nVersion
;
548 for (sal_Int32 i
= 0; i
< nLen
; i
++)
550 Reference
<XPersistObject
> xObj
;
553 xObj
= _rxInStream
->readObject();
555 catch(const WrongFormatException
&)
557 // the object could not be read
558 // create a object (so the readEvents below will assign the events to the right controls)
559 xObj
= lcl_createPlaceHolder( m_xContext
);
561 // couldn't handle it
564 catch(const Exception
&)
567 while (!m_aItems
.empty())
568 removeElementsNoEvents();
570 // Rethrow the exception
576 Reference
< XPropertySet
> xElement( xObj
, UNO_QUERY
);
580 m_aItems
.size(), // position
581 xElement
, // element to insert
582 false, // no event attacher manager handling
583 nullptr, // not yet approved - let implInsert do it
584 true // fire the event
587 catch( const Exception
& )
589 SAL_WARN("forms.misc", "OInterfaceContainerHelper2::read: reading succeeded, but not inserting!" );
590 // create a placeholder
591 xElement
.set(lcl_createPlaceHolder( m_xContext
), css::uno::UNO_QUERY
);
592 if ( !xElement
.is() )
593 // couldn't handle it
595 // insert the placeholder
596 implInsert( m_aItems
.size(), xElement
, false, nullptr, true );
601 readEvents(_rxInStream
);
607 m_xEventAttacher
= ::comphelper::createEventAttacherManager( m_xContext
);
608 OSL_ENSURE( m_xEventAttacher
.is(), "OInterfaceContainer::read: could not create an event attacher manager!" );
610 catch( const Exception
& )
612 DBG_UNHANDLED_EXCEPTION();
619 void SAL_CALL
OInterfaceContainer::addContainerListener(const Reference
<XContainerListener
>& _rxListener
) throw( RuntimeException
, std::exception
)
621 m_aContainerListeners
.addInterface(_rxListener
);
625 void SAL_CALL
OInterfaceContainer::removeContainerListener(const Reference
<XContainerListener
>& _rxListener
) throw( RuntimeException
, std::exception
)
627 m_aContainerListeners
.removeInterface(_rxListener
);
632 void SAL_CALL
OInterfaceContainer::disposing(const css::lang::EventObject
& _rSource
) throw( RuntimeException
, std::exception
)
634 ::osl::MutexGuard
aGuard( m_rMutex
);
636 Reference
< XInterface
> xSource( _rSource
.Source
, UNO_QUERY
);
639 OInterfaceArray::iterator j
;
640 for ( j
= m_aItems
.begin(); j
!= m_aItems
.end(); ++j
)
642 DBG_ASSERT( j
->get() == Reference
< XInterface
>( *j
, UNO_QUERY
).get(),
643 "OInterfaceContainer::disposing: vector element not normalized!" );
645 if ( xSource
.get() == j
->get() )
650 if ( m_aItems
.end() != j
)
654 // look up in, and erase from, m_aMap, too
655 OInterfaceMap::iterator i
= m_aMap
.begin();
656 while ( i
!= m_aMap
.end() )
658 DBG_ASSERT( i
->second
.get() == Reference
< XInterface
>( i
->second
, UNO_QUERY
).get(),
659 "OInterfaceContainer::disposing: map element not normalized!" );
661 if ( i
->second
.get() == xSource
.get() )
670 DBG_ASSERT( i
!= m_aMap
.end(), "OInterfaceContainer::disposing: inconsistency: the element was in m_aItems, but not in m_aMap!" );
675 // XPropertyChangeListener
677 void OInterfaceContainer::propertyChange(const PropertyChangeEvent
& evt
)
678 throw (css::uno::RuntimeException
, std::exception
) {
679 if (evt
.PropertyName
== PROPERTY_NAME
)
681 ::osl::MutexGuard
aGuard( m_rMutex
);
682 OInterfaceMap::iterator i
= m_aMap
.find(::comphelper::getString(evt
.OldValue
));
683 if (i
!= m_aMap
.end() && (*i
).second
!= evt
.Source
)
685 css::uno::Reference
<css::uno::XInterface
> xCorrectType((*i
).second
);
687 m_aMap
.insert(::std::pair
<const OUString
, css::uno::Reference
<css::uno::XInterface
> >(::comphelper::getString(evt
.NewValue
),xCorrectType
));
694 sal_Bool SAL_CALL
OInterfaceContainer::hasElements() throw( RuntimeException
, std::exception
)
696 return !m_aMap
.empty();
700 Type SAL_CALL
OInterfaceContainer::getElementType() throw(RuntimeException
, std::exception
)
702 return m_aElementType
;
705 // XEnumerationAccess
707 Reference
<XEnumeration
> SAL_CALL
OInterfaceContainer::createEnumeration() throw( RuntimeException
, std::exception
)
709 ::osl::MutexGuard
aGuard( m_rMutex
);
710 return new ::comphelper::OEnumerationByIndex(static_cast<XIndexAccess
*>(this));
715 Any SAL_CALL
OInterfaceContainer::getByName( const OUString
& _rName
) throw(NoSuchElementException
, WrappedTargetException
, RuntimeException
, std::exception
)
717 ::std::pair
<OInterfaceMap::iterator
,
718 OInterfaceMap::iterator
> aPair
= m_aMap
.equal_range(_rName
);
720 if (aPair
.first
== aPair
.second
)
721 throw NoSuchElementException();
723 return (*aPair
.first
).second
->queryInterface( m_aElementType
);
727 css::uno::Sequence
<OUString
> SAL_CALL
OInterfaceContainer::getElementNames() throw(RuntimeException
, std::exception
)
729 return comphelper::mapKeysToSequence(m_aMap
);
733 sal_Bool SAL_CALL
OInterfaceContainer::hasByName( const OUString
& _rName
) throw(RuntimeException
, std::exception
)
735 ::std::pair
<OInterfaceMap::iterator
,
736 OInterfaceMap::iterator
> aPair
= m_aMap
.equal_range(_rName
);
737 return aPair
.first
!= aPair
.second
;
742 sal_Int32
OInterfaceContainer::getCount() throw( RuntimeException
, std::exception
)
744 return m_aItems
.size();
748 Any
OInterfaceContainer::getByIndex(sal_Int32 _nIndex
) throw( IndexOutOfBoundsException
, WrappedTargetException
, RuntimeException
, std::exception
)
750 if (_nIndex
< 0 || (_nIndex
>= (sal_Int32
)m_aItems
.size()))
751 throw IndexOutOfBoundsException();
753 return m_aItems
[_nIndex
]->queryInterface( m_aElementType
);
757 void OInterfaceContainer::approveNewElement( const Reference
< XPropertySet
>& _rxObject
, ElementDescription
* _pElement
)
759 // it has to be non-NULL
760 if ( !_rxObject
.is() )
761 throw IllegalArgumentException(FRM_RES_STRING(RID_STR_NEED_NON_NULL_OBJECT
), static_cast<XContainer
*>(this), 1);
763 // it has to support our element type interface
764 Any aCorrectType
= _rxObject
->queryInterface( m_aElementType
);
765 if ( !aCorrectType
.hasValue() )
766 lcl_throwIllegalArgumentException();
768 // it has to have a "Name" property
769 if ( !hasProperty( PROPERTY_NAME
, _rxObject
) )
770 lcl_throwIllegalArgumentException();
772 // it has to be a child, and it must not have a parent already
773 Reference
< XChild
> xChild( _rxObject
, UNO_QUERY
);
774 if ( !xChild
.is() || xChild
->getParent().is() )
776 lcl_throwIllegalArgumentException();
779 // passed all tests. cache the information we have so far
780 DBG_ASSERT( _pElement
, "OInterfaceContainer::approveNewElement: invalid event descriptor!" );
783 _pElement
->xPropertySet
= _rxObject
;
784 _pElement
->xChild
= xChild
;
785 _pElement
->aElementTypeInterface
= aCorrectType
;
786 _pElement
->xInterface
= Reference
< XInterface
>( _rxObject
, UNO_QUERY
); // normalized XInterface
791 void OInterfaceContainer::implInsert(sal_Int32 _nIndex
, const Reference
< XPropertySet
>& _rxElement
,
792 bool _bEvents
, ElementDescription
* _pApprovalResult
, bool _bFire
) throw( IllegalArgumentException
)
794 const bool bHandleEvents
= _bEvents
&& m_xEventAttacher
.is();
796 // SYNCHRONIZED ----->
797 ::osl::ClearableMutexGuard
aGuard( m_rMutex
);
799 std::unique_ptr
< ElementDescription
> aAutoDeleteMetaData
;
800 ElementDescription
* pElementMetaData
= _pApprovalResult
;
801 if ( !pElementMetaData
)
802 { // not yet approved by the caller -> do ourself
803 pElementMetaData
= createElementMetaData();
804 DBG_ASSERT( pElementMetaData
, "OInterfaceContainer::implInsert: createElementMetaData returned nonsense!" );
806 // ensure that the meta data structure will be deleted later on
807 aAutoDeleteMetaData
.reset( pElementMetaData
);
809 // will throw an exception if necessary
810 approveNewElement( _rxElement
, pElementMetaData
);
814 // approveNewElement (no matter if called here or outside) has ensure that all relevant interfaces
817 // set the name, and add as change listener for the name
819 _rxElement
->getPropertyValue(PROPERTY_NAME
) >>= sName
;
820 _rxElement
->addPropertyChangeListener(PROPERTY_NAME
, this);
822 // insert the object into our internal structures
823 if (_nIndex
> (sal_Int32
)m_aItems
.size()) // Calculate the actual index
825 _nIndex
= m_aItems
.size();
826 m_aItems
.push_back( pElementMetaData
->xInterface
);
829 m_aItems
.insert( m_aItems
.begin() + _nIndex
, pElementMetaData
->xInterface
);
831 m_aMap
.insert( ::std::pair
< const OUString
, css::uno::Reference
<css::uno::XInterface
> >( sName
, pElementMetaData
->xInterface
) );
833 // announce ourself as parent to the new element
834 pElementMetaData
->xChild
->setParent(static_cast<XContainer
*>(this));
839 m_xEventAttacher
->insertEntry(_nIndex
);
840 m_xEventAttacher
->attach( _nIndex
, pElementMetaData
->xInterface
, makeAny( _rxElement
) );
843 // notify derived classes
844 implInserted( pElementMetaData
);
847 // <----- SYNCHRONIZED
849 // insert faked VBA events?
850 bool bHandleVbaEvents
= false;
853 _rxElement
->getPropertyValue("GenerateVbaEvents") >>= bHandleVbaEvents
;
855 catch( const Exception
& )
858 if ( bHandleVbaEvents
)
860 Reference
< XEventAttacherManager
> xMgr ( pElementMetaData
->xInterface
, UNO_QUERY
);
861 OInterfaceContainer
* pIfcMgr
= xMgr
.is() ? dynamic_cast<OInterfaceContainer
*>(xMgr
.get()) : nullptr;
864 sal_Int32 nLen
= pIfcMgr
->getCount();
865 for (sal_Int32 i
= 0; i
< nLen
; ++i
)
867 // add fake events to the control at index i
868 pIfcMgr
->impl_addVbEvents_nolck_nothrow( i
);
873 // add fake events to the control at index i
874 impl_addVbEvents_nolck_nothrow( _nIndex
);
878 // fire the notification about the change
883 aEvt
.Source
= static_cast<XContainer
*>(this);
884 aEvt
.Accessor
<<= _nIndex
;
885 aEvt
.Element
= pElementMetaData
->aElementTypeInterface
;
888 m_aContainerListeners
.notifyEach( &XContainerListener::elementInserted
, aEvt
);
893 void OInterfaceContainer::removeElementsNoEvents()
895 OInterfaceArray::iterator i
= m_aItems
.begin();
896 css::uno::Reference
<css::uno::XInterface
> xElement(*i
);
898 OInterfaceMap::iterator j
= m_aMap
.begin();
899 while (j
!= m_aMap
.end() && (*j
).second
!= xElement
) ++j
;
904 Reference
<XPropertySet
> xSet(xElement
, UNO_QUERY
);
906 xSet
->removePropertyChangeListener(PROPERTY_NAME
, this);
908 Reference
<XChild
> xChild(xElement
, UNO_QUERY
);
910 xChild
->setParent(css::uno::Reference
<css::uno::XInterface
> ());
914 void OInterfaceContainer::implInserted( const ElementDescription
* /*_pElement*/ )
920 void OInterfaceContainer::implRemoved( const css::uno::Reference
<css::uno::XInterface
>& /*_rxObject*/ )
926 void OInterfaceContainer::impl_replacedElement( const ContainerEvent
& _rEvent
, ::osl::ClearableMutexGuard
& _rInstanceLock
)
928 _rInstanceLock
.clear();
929 m_aContainerListeners
.notifyEach( &XContainerListener::elementReplaced
, _rEvent
);
934 void SAL_CALL
OInterfaceContainer::insertByIndex( sal_Int32 _nIndex
, const Any
& _rElement
) throw(IllegalArgumentException
, IndexOutOfBoundsException
, WrappedTargetException
, RuntimeException
, std::exception
)
936 Reference
< XPropertySet
> xElement
;
937 _rElement
>>= xElement
;
938 implInsert( _nIndex
, xElement
, true /* event handling */ , nullptr /* not yet approved */ , true /* notification */ );
942 void OInterfaceContainer::implReplaceByIndex( const sal_Int32 _nIndex
, const Any
& _rNewElement
, ::osl::ClearableMutexGuard
& _rClearBeforeNotify
)
944 OSL_PRECOND( ( _nIndex
>= 0 ) && ( _nIndex
< (sal_Int32
)m_aItems
.size() ), "OInterfaceContainer::implReplaceByIndex: precondition not met (index)!" );
946 // approve the new object
947 std::unique_ptr
< ElementDescription
> aElementMetaData( createElementMetaData() );
948 DBG_ASSERT( aElementMetaData
.get(), "OInterfaceContainer::implReplaceByIndex: createElementMetaData returned nonsense!" );
950 Reference
< XPropertySet
> xElementProps
;
951 _rNewElement
>>= xElementProps
;
952 approveNewElement( xElementProps
, aElementMetaData
.get() );
955 // get the old element
956 css::uno::Reference
<css::uno::XInterface
> xOldElement( m_aItems
[ _nIndex
] );
957 DBG_ASSERT( xOldElement
.get() == Reference
< XInterface
>( xOldElement
, UNO_QUERY
).get(),
958 "OInterfaceContainer::implReplaceByIndex: elements should be held normalized!" );
960 // locate the old element in the map
961 OInterfaceMap::iterator j
= m_aMap
.begin();
962 while ( ( j
!= m_aMap
.end() ) && ( j
->second
.get() != xOldElement
.get() ) )
965 // remove event knittings
966 if ( m_xEventAttacher
.is() )
968 css::uno::Reference
<css::uno::XInterface
> xNormalized( xOldElement
, UNO_QUERY
);
969 m_xEventAttacher
->detach( _nIndex
, xNormalized
);
970 m_xEventAttacher
->removeEntry( _nIndex
);
973 // don't listen for property changes anymore
974 Reference
<XPropertySet
> xSet( xOldElement
, UNO_QUERY
);
976 xSet
->removePropertyChangeListener(PROPERTY_NAME
, this);
978 // give the old element a new (void) parent
979 Reference
<XChild
> xChild(xOldElement
, UNO_QUERY
);
981 xChild
->setParent(css::uno::Reference
<css::uno::XInterface
> ());
983 // remove the old one
986 // examine the new element
988 DBG_ASSERT( aElementMetaData
.get()->xPropertySet
.is(), "OInterfaceContainer::implReplaceByIndex: what did approveNewElement do?" );
990 aElementMetaData
.get()->xPropertySet
->getPropertyValue(PROPERTY_NAME
) >>= sName
;
991 aElementMetaData
.get()->xPropertySet
->addPropertyChangeListener(PROPERTY_NAME
, this);
993 // insert the new one
994 m_aMap
.insert( ::std::pair
<const OUString
, css::uno::Reference
<css::uno::XInterface
> >( sName
, aElementMetaData
.get()->xInterface
) );
995 m_aItems
[ _nIndex
] = aElementMetaData
.get()->xInterface
;
997 aElementMetaData
.get()->xChild
->setParent(static_cast<XContainer
*>(this));
999 if ( m_xEventAttacher
.is() )
1001 m_xEventAttacher
->insertEntry( _nIndex
);
1002 m_xEventAttacher
->attach( _nIndex
, aElementMetaData
.get()->xInterface
, makeAny( aElementMetaData
.get()->xPropertySet
) );
1005 ContainerEvent aReplaceEvent
;
1006 aReplaceEvent
.Source
= static_cast< XContainer
* >( this );
1007 aReplaceEvent
.Accessor
<<= _nIndex
;
1008 aReplaceEvent
.Element
= aElementMetaData
.get()->xInterface
->queryInterface( m_aElementType
);
1009 aReplaceEvent
.ReplacedElement
= xOldElement
->queryInterface( m_aElementType
);
1011 impl_replacedElement( aReplaceEvent
, _rClearBeforeNotify
);
1015 void OInterfaceContainer::implCheckIndex( const sal_Int32 _nIndex
)
1017 if (_nIndex
< 0 || _nIndex
>= (sal_Int32
)m_aItems
.size())
1018 throw IndexOutOfBoundsException();
1022 void SAL_CALL
OInterfaceContainer::replaceByIndex(sal_Int32 _nIndex
, const Any
& Element
) throw( IllegalArgumentException
, IndexOutOfBoundsException
, WrappedTargetException
, RuntimeException
, std::exception
)
1024 ::osl::ClearableMutexGuard
aGuard( m_rMutex
);
1026 implCheckIndex( _nIndex
);
1028 implReplaceByIndex( _nIndex
, Element
, aGuard
);
1032 void OInterfaceContainer::implRemoveByIndex( const sal_Int32 _nIndex
, ::osl::ClearableMutexGuard
& _rClearBeforeNotify
)
1034 OSL_PRECOND( ( _nIndex
>= 0 ) && ( _nIndex
< (sal_Int32
)m_aItems
.size() ), "OInterfaceContainer::implRemoveByIndex: precondition not met (index)!" );
1036 OInterfaceArray::iterator i
= m_aItems
.begin() + _nIndex
;
1037 css::uno::Reference
<css::uno::XInterface
> xElement(*i
);
1039 OInterfaceMap::iterator j
= m_aMap
.begin();
1040 while (j
!= m_aMap
.end() && (*j
).second
!= xElement
) ++j
;
1045 // remove event knittings
1046 if ( m_xEventAttacher
.is() )
1048 css::uno::Reference
<css::uno::XInterface
> xNormalized( xElement
, UNO_QUERY
);
1049 m_xEventAttacher
->detach( _nIndex
, xNormalized
);
1050 m_xEventAttacher
->removeEntry( _nIndex
);
1053 Reference
<XPropertySet
> xSet(xElement
, UNO_QUERY
);
1055 xSet
->removePropertyChangeListener(PROPERTY_NAME
, this);
1057 Reference
<XChild
> xChild(xElement
, UNO_QUERY
);
1059 xChild
->setParent(css::uno::Reference
<css::uno::XInterface
> ());
1061 // notify derived classes
1062 implRemoved(xElement
);
1065 ContainerEvent aEvt
;
1066 aEvt
.Source
= static_cast<XContainer
*>(this);
1067 aEvt
.Element
= xElement
->queryInterface( m_aElementType
);
1068 aEvt
.Accessor
<<= _nIndex
;
1070 _rClearBeforeNotify
.clear();
1071 m_aContainerListeners
.notifyEach( &XContainerListener::elementRemoved
, aEvt
);
1075 void SAL_CALL
OInterfaceContainer::removeByIndex(sal_Int32 _nIndex
) throw( IndexOutOfBoundsException
, WrappedTargetException
, RuntimeException
, std::exception
)
1077 ::osl::ClearableMutexGuard
aGuard( m_rMutex
);
1079 implCheckIndex( _nIndex
);
1081 implRemoveByIndex( _nIndex
, aGuard
);
1085 ElementDescription
* OInterfaceContainer::createElementMetaData( )
1087 return new ElementDescription
;
1091 void SAL_CALL
OInterfaceContainer::insertByName(const OUString
& _rName
, const Any
& _rElement
) throw( IllegalArgumentException
, ElementExistException
, WrappedTargetException
, RuntimeException
, std::exception
)
1093 Reference
< XPropertySet
> xElementProps
;
1095 std::unique_ptr
< ElementDescription
> aElementMetaData( createElementMetaData() );
1096 DBG_ASSERT( aElementMetaData
.get(), "OInterfaceContainer::insertByName: createElementMetaData returned nonsense!" );
1098 // ensure the correct name of the element
1101 _rElement
>>= xElementProps
;
1102 approveNewElement( xElementProps
, aElementMetaData
.get() );
1104 xElementProps
->setPropertyValue( PROPERTY_NAME
, makeAny( _rName
) );
1106 catch( const IllegalArgumentException
& )
1108 throw; // allowed to leave
1110 catch( const ElementExistException
& )
1112 throw; // allowed to leave
1114 catch( const Exception
& )
1116 SAL_WARN("forms.misc", "OInterfaceContainer::insertByName: caught an exception!" );
1118 implInsert( m_aItems
.size(), xElementProps
, true, aElementMetaData
.get(), true );
1122 void SAL_CALL
OInterfaceContainer::replaceByName(const OUString
& Name
, const Any
& Element
) throw( IllegalArgumentException
, NoSuchElementException
, WrappedTargetException
, RuntimeException
, std::exception
)
1124 ::osl::ClearableMutexGuard
aGuard( m_rMutex
);
1125 ::std::pair
<OInterfaceMap::iterator
,
1126 OInterfaceMap::iterator
> aPair
= m_aMap
.equal_range(Name
);
1127 if (aPair
.first
== aPair
.second
)
1128 throw NoSuchElementException();
1130 if (Element
.getValueType().getTypeClass() != TypeClass_INTERFACE
)
1131 lcl_throwIllegalArgumentException();
1133 Reference
<XPropertySet
> xSet
;
1137 if (!hasProperty(PROPERTY_NAME
, xSet
))
1138 lcl_throwIllegalArgumentException();
1140 xSet
->setPropertyValue(PROPERTY_NAME
, makeAny(Name
));
1143 // determine the element pos
1144 sal_Int32 nPos
= ::std::find(m_aItems
.begin(), m_aItems
.end(), (*aPair
.first
).second
) - m_aItems
.begin();
1146 implReplaceByIndex( nPos
, Element
, aGuard
);
1150 void SAL_CALL
OInterfaceContainer::removeByName(const OUString
& Name
) throw( NoSuchElementException
, WrappedTargetException
, RuntimeException
, std::exception
)
1152 ::osl::MutexGuard
aGuard( m_rMutex
);
1153 ::std::pair
<OInterfaceMap::iterator
,
1154 OInterfaceMap::iterator
> aPair
= m_aMap
.equal_range(Name
);
1155 if (aPair
.first
== aPair
.second
)
1156 throw NoSuchElementException();
1158 sal_Int32 nPos
= ::std::find(m_aItems
.begin(), m_aItems
.end(), (*aPair
.first
).second
) - m_aItems
.begin();
1159 removeByIndex(nPos
);
1163 // XEventAttacherManager
1165 void SAL_CALL
OInterfaceContainer::registerScriptEvent( sal_Int32 nIndex
, const ScriptEventDescriptor
& aScriptEvent
) throw(IllegalArgumentException
, RuntimeException
, std::exception
)
1167 ::osl::ClearableMutexGuard
aGuard( m_rMutex
);
1168 if ( m_xEventAttacher
.is() )
1170 m_xEventAttacher
->registerScriptEvent( nIndex
, aScriptEvent
);
1172 impl_addVbEvents_nolck_nothrow( nIndex
); // add fake vba events
1177 void SAL_CALL
OInterfaceContainer::registerScriptEvents( sal_Int32 nIndex
, const Sequence
< ScriptEventDescriptor
>& aScriptEvents
) throw(IllegalArgumentException
, RuntimeException
, std::exception
)
1179 ::osl::ClearableMutexGuard
aGuard( m_rMutex
);
1180 if ( m_xEventAttacher
.is() )
1182 m_xEventAttacher
->registerScriptEvents( nIndex
, aScriptEvents
);
1184 impl_addVbEvents_nolck_nothrow( nIndex
); // add fake vba events
1189 void SAL_CALL
OInterfaceContainer::revokeScriptEvent( sal_Int32 nIndex
, const OUString
& aListenerType
, const OUString
& aEventMethod
, const OUString
& aRemoveListenerParam
) throw(IllegalArgumentException
, RuntimeException
, std::exception
)
1191 if ( m_xEventAttacher
.is() )
1192 m_xEventAttacher
->revokeScriptEvent( nIndex
, aListenerType
, aEventMethod
, aRemoveListenerParam
);
1196 void SAL_CALL
OInterfaceContainer::revokeScriptEvents( sal_Int32 nIndex
) throw(IllegalArgumentException
, RuntimeException
, std::exception
)
1198 if ( m_xEventAttacher
.is() )
1199 m_xEventAttacher
->revokeScriptEvents( nIndex
);
1203 void SAL_CALL
OInterfaceContainer::insertEntry( sal_Int32 nIndex
) throw(IllegalArgumentException
, RuntimeException
, std::exception
)
1205 if ( m_xEventAttacher
.is() )
1206 m_xEventAttacher
->insertEntry( nIndex
);
1210 void SAL_CALL
OInterfaceContainer::removeEntry( sal_Int32 nIndex
) throw(IllegalArgumentException
, RuntimeException
, std::exception
)
1212 if ( m_xEventAttacher
.is() )
1213 m_xEventAttacher
->removeEntry( nIndex
);
1217 Sequence
< ScriptEventDescriptor
> SAL_CALL
OInterfaceContainer::getScriptEvents( sal_Int32 nIndex
) throw(IllegalArgumentException
, RuntimeException
, std::exception
)
1219 Sequence
< ScriptEventDescriptor
> aReturn
;
1220 if ( m_xEventAttacher
.is() )
1222 aReturn
= m_xEventAttacher
->getScriptEvents( nIndex
);
1223 if ( lcl_hasVbaEvents( aReturn
) )
1225 aReturn
= lcl_stripVbaEvents( aReturn
);
1232 void SAL_CALL
OInterfaceContainer::attach( sal_Int32 nIndex
, const Reference
< XInterface
>& xObject
, const Any
& aHelper
) throw(IllegalArgumentException
, ServiceNotRegisteredException
, RuntimeException
, std::exception
)
1234 if ( m_xEventAttacher
.is() )
1235 m_xEventAttacher
->attach( nIndex
, xObject
, aHelper
);
1239 void SAL_CALL
OInterfaceContainer::detach( sal_Int32 nIndex
, const Reference
< XInterface
>& xObject
) throw(IllegalArgumentException
, RuntimeException
, std::exception
)
1241 if ( m_xEventAttacher
.is() )
1242 m_xEventAttacher
->detach( nIndex
, xObject
);
1246 void SAL_CALL
OInterfaceContainer::addScriptListener( const Reference
< XScriptListener
>& xListener
) throw(IllegalArgumentException
, RuntimeException
, std::exception
)
1248 if ( m_xEventAttacher
.is() )
1249 m_xEventAttacher
->addScriptListener( xListener
);
1253 void SAL_CALL
OInterfaceContainer::removeScriptListener( const Reference
< XScriptListener
>& xListener
) throw(IllegalArgumentException
, RuntimeException
, std::exception
)
1255 if ( m_xEventAttacher
.is() )
1256 m_xEventAttacher
->removeScriptListener( xListener
);
1263 Any SAL_CALL
OFormComponents::queryAggregation(const Type
& _rType
) throw(RuntimeException
, std::exception
)
1265 Any aReturn
= OFormComponents_BASE::queryInterface(_rType
);
1266 if (!aReturn
.hasValue())
1268 aReturn
= OInterfaceContainer::queryInterface(_rType
);
1270 if (!aReturn
.hasValue())
1271 aReturn
= FormComponentsBase::queryAggregation(_rType
);
1278 Sequence
<Type
> SAL_CALL
OFormComponents::getTypes() throw(RuntimeException
, std::exception
)
1280 return ::comphelper::concatSequences(OInterfaceContainer::getTypes(), FormComponentsBase::getTypes(), OFormComponents_BASE::getTypes());
1284 OFormComponents::OFormComponents(const Reference
<XComponentContext
>& _rxFactory
)
1285 :FormComponentsBase( m_aMutex
)
1286 ,OInterfaceContainer( _rxFactory
, m_aMutex
, cppu::UnoType
<XFormComponent
>::get() )
1287 ,OFormComponents_BASE()
1292 OFormComponents::OFormComponents( const OFormComponents
& _cloneSource
)
1293 :FormComponentsBase( m_aMutex
)
1294 ,OInterfaceContainer( m_aMutex
, _cloneSource
)
1295 ,OFormComponents_BASE()
1300 OFormComponents::~OFormComponents()
1302 if (!FormComponentsBase::rBHelper
.bDisposed
)
1311 void OFormComponents::disposing()
1313 OInterfaceContainer::disposing();
1314 FormComponentsBase::disposing();
1315 m_xParent
= nullptr;
1320 void OFormComponents::setParent(const css::uno::Reference
<css::uno::XInterface
>& Parent
) throw( NoSupportException
, RuntimeException
, std::exception
)
1322 ::osl::MutexGuard
aGuard( m_aMutex
);
1327 css::uno::Reference
<css::uno::XInterface
> OFormComponents::getParent() throw( RuntimeException
, std::exception
)
1336 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */