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 <comphelper/types.hxx>
43 #include <cppuhelper/exc_hlp.hxx>
44 #include <cppuhelper/queryinterface.hxx>
45 #include <tools/debug.hxx>
46 #include <tools/diagnose_ex.h>
49 #include <boost/scoped_ptr.hpp>
52 #include <com/sun/star/frame/XModel.hpp>
53 #include <com/sun/star/document/XCodeNameQuery.hpp>
54 #include <ooo/vba/XVBAToOOEventDescGen.hpp>
55 #include <comphelper/processfactory.hxx>
61 using namespace ::com::sun::star::frame
;
62 using namespace ::com::sun::star::lang
;
63 using namespace ::com::sun::star::uno
;
64 using namespace ::com::sun::star::beans
;
65 using namespace ::com::sun::star::document
;
66 using namespace ::com::sun::star::container
;
67 using namespace ::com::sun::star::script
;
68 using namespace ::com::sun::star::io
;
69 using namespace ::com::sun::star::form
;
70 using namespace ::com::sun::star::util
;
75 static void lcl_throwIllegalArgumentException()
77 throw IllegalArgumentException();
82 lcl_hasVbaEvents( const Sequence
< ScriptEventDescriptor
>& sEvents
)
84 const ScriptEventDescriptor
* pDesc
= sEvents
.getConstArray();
85 const ScriptEventDescriptor
* pEnd
= ( pDesc
+ sEvents
.getLength() );
86 for ( ; pDesc
!= pEnd
; ++pDesc
)
88 if ( pDesc
->ScriptType
== "VBAInterop" )
94 Sequence
< ScriptEventDescriptor
>
95 lcl_stripVbaEvents( const Sequence
< ScriptEventDescriptor
>& sEvents
)
97 Sequence
< ScriptEventDescriptor
> sStripped( sEvents
.getLength() );
99 const ScriptEventDescriptor
* pDesc
= sEvents
.getConstArray();
100 const ScriptEventDescriptor
* pEnd
= ( pDesc
+ sEvents
.getLength() );
101 sal_Int32 nCopied
= 0;
102 for ( ; pDesc
!= pEnd
; ++pDesc
)
104 if ( pDesc
->ScriptType
!= "VBAInterop" )
106 sStripped
[ nCopied
++ ] = *pDesc
;
110 sStripped
.realloc( nCopied
);
114 void OInterfaceContainer::impl_addVbEvents_nolck_nothrow( const sal_Int32 i_nIndex
)
116 // we are dealing with form controls
121 Reference
< XModel
> xDoc( getXModel( static_cast< XContainer
*> ( this ) ) );
125 Reference
< XMultiServiceFactory
> xDocFac( xDoc
, UNO_QUERY_THROW
);
126 Reference
< XCodeNameQuery
> xNameQuery( xDocFac
->createInstance("ooo.vba.VBACodeNameProvider"), UNO_QUERY
);
127 if ( !xNameQuery
.is() )
130 ::osl::MutexGuard
aGuard( m_rMutex
);
131 bool hasVBABindings
= lcl_hasVbaEvents( m_xEventAttacher
->getScriptEvents( i_nIndex
) );
132 if ( hasVBABindings
)
135 Reference
< XInterface
> xElement( getByIndex( i_nIndex
) , UNO_QUERY_THROW
);
136 Reference
< XForm
> xElementAsForm( xElement
, UNO_QUERY
);
137 if ( xElementAsForm
.is() )
140 // Try getting the code name from the container first (faster),
141 // then from the element if that fails (slower).
142 Reference
<XInterface
> xThis
= static_cast<XContainer
*>(this);
143 OUString sCodeName
= xNameQuery
->getCodeNameForContainer(xThis
);
144 if (sCodeName
.isEmpty())
145 sCodeName
= xNameQuery
->getCodeNameForObject(xElement
);
147 Reference
< XPropertySet
> xProps( xElement
, UNO_QUERY_THROW
);
148 OUString sServiceName
;
149 xProps
->getPropertyValue("DefaultControl") >>= sServiceName
;
151 Reference
< ooo::vba::XVBAToOOEventDescGen
> xDescSupplier( m_xContext
->getServiceManager()->createInstanceWithContext("ooo.vba.VBAToOOEventDesc", m_xContext
), UNO_QUERY_THROW
);
152 Sequence
< ScriptEventDescriptor
> vbaEvents
= xDescSupplier
->getEventDescriptions( sServiceName
, sCodeName
);
154 // register the vba script events
155 m_xEventAttacher
->registerScriptEvents( i_nIndex
, vbaEvents
);
159 catch ( const ServiceNotRegisteredException
& )
161 // silence this, not all document types support the ooo.vba.VBACodeNameProvider service
163 catch( const Exception
& )
165 DBG_UNHANDLED_EXCEPTION();
170 ElementDescription::ElementDescription( )
175 ElementDescription::~ElementDescription()
179 OInterfaceContainer::OInterfaceContainer(
180 const Reference
<XComponentContext
>& _rxContext
,
181 ::osl::Mutex
& _rMutex
,
182 const Type
& _rElementType
)
183 :OInterfaceContainer_BASE()
185 ,m_aContainerListeners(_rMutex
)
186 ,m_aElementType(_rElementType
)
187 ,m_xContext(_rxContext
)
189 impl_createEventAttacher_nothrow();
193 OInterfaceContainer::OInterfaceContainer( ::osl::Mutex
& _rMutex
, const OInterfaceContainer
& _cloneSource
)
194 :OInterfaceContainer_BASE()
196 ,m_aContainerListeners( _rMutex
)
197 ,m_aElementType( _cloneSource
.m_aElementType
)
198 ,m_xContext( _cloneSource
.m_xContext
)
200 impl_createEventAttacher_nothrow();
203 void OInterfaceContainer::clonedFrom(const OInterfaceContainer
& _cloneSource
) throw(RuntimeException
, std::exception
)
207 const Reference
< XIndexAccess
> xSourceHierarchy( const_cast< OInterfaceContainer
* >( &_cloneSource
) );
208 const sal_Int32 nCount
= xSourceHierarchy
->getCount();
209 for ( sal_Int32 i
=0; i
<nCount
; ++i
)
211 Reference
< XCloneable
> xCloneable( xSourceHierarchy
->getByIndex( i
), UNO_QUERY_THROW
);
212 Reference
< XInterface
> xClone( xCloneable
->createClone() );
213 insertByIndex( i
, makeAny( xClone
) );
216 catch (const RuntimeException
&)
220 catch (const Exception
&)
222 throw WrappedTargetRuntimeException(
223 "Could not clone the given interface hierarchy.",
224 static_cast< XIndexContainer
* >( const_cast< OInterfaceContainer
* >( &_cloneSource
) ),
225 ::cppu::getCaughtException()
230 void OInterfaceContainer::impl_createEventAttacher_nothrow()
234 m_xEventAttacher
.set( ::comphelper::createEventAttacherManager( m_xContext
), UNO_SET_THROW
);
236 catch( const Exception
& )
238 DBG_UNHANDLED_EXCEPTION();
243 OInterfaceContainer::~OInterfaceContainer()
248 void OInterfaceContainer::disposing()
250 // dispose all elements
251 for (sal_Int32 i
= m_aItems
.size(); i
> 0; --i
)
253 Reference
<XPropertySet
> xSet(m_aItems
[i
- 1], UNO_QUERY
);
255 xSet
->removePropertyChangeListener(PROPERTY_NAME
, this);
257 // revoke event knittings
258 if ( m_xEventAttacher
.is() )
260 m_xEventAttacher
->detach( i
- 1, Reference
<XInterface
>(xSet
, UNO_QUERY
) );
261 m_xEventAttacher
->removeEntry( i
- 1 );
264 Reference
<XComponent
> xComponent(xSet
, UNO_QUERY
);
266 xComponent
->dispose();
271 css::lang::EventObject
aEvt(static_cast<XContainer
*>(this));
272 m_aContainerListeners
.disposeAndClear(aEvt
);
280 void lcl_saveEvents( ::std::vector
< Sequence
< ScriptEventDescriptor
> >& _rSave
,
281 const Reference
< XEventAttacherManager
>& _rxManager
, const sal_Int32 _nItemCount
)
283 OSL_ENSURE( _rxManager
.is(), "lcl_saveEvents: invalid event attacher manager!" );
284 if ( !_rxManager
.is() )
287 // reserve the space needed
288 _rSave
.reserve( _nItemCount
);
291 for (sal_Int32 i
=0; i
<_nItemCount
; ++i
)
292 _rSave
.push_back(_rxManager
->getScriptEvents( i
));
296 void lcl_restoreEvents( const ::std::vector
< Sequence
< ScriptEventDescriptor
> >& _rSave
,
297 const Reference
< XEventAttacherManager
>& _rxManager
)
299 OSL_ENSURE( _rxManager
.is(), "lcl_restoreEvents: invalid event attacher manager!" );
300 if ( !_rxManager
.is() )
303 ::std::vector
< Sequence
< ScriptEventDescriptor
> >::const_iterator aLoop
= _rSave
.begin();
304 ::std::vector
< Sequence
< ScriptEventDescriptor
> >::const_iterator aEnd
= _rSave
.end();
305 for ( sal_Int32 i
=0; aLoop
!= aEnd
; ++aLoop
, ++i
)
307 _rxManager
->revokeScriptEvents( i
);
308 _rxManager
->registerScriptEvents( i
, *aLoop
);
314 void SAL_CALL
OInterfaceContainer::writeEvents(const Reference
<XObjectOutputStream
>& _rxOutStream
)
316 // We're writing a document in SO 5.2 format (or even from earlier versions)
317 // -> convert the events from the new runtime format to the format of the 5.2 files
318 // but before, remember the current script events set for our children
319 ::std::vector
< Sequence
< ScriptEventDescriptor
> > aSave
;
320 if ( m_xEventAttacher
.is() )
321 lcl_saveEvents( aSave
, m_xEventAttacher
, m_aItems
.size() );
323 transformEvents( efVersionSO5x
);
327 Reference
<XMarkableStream
> xMark(_rxOutStream
, UNO_QUERY
);
328 sal_Int32 nMark
= xMark
->createMark();
330 sal_Int32 nObjLen
= 0;
331 _rxOutStream
->writeLong(nObjLen
);
333 Reference
<XPersistObject
> xScripts(m_xEventAttacher
, UNO_QUERY
);
335 xScripts
->write(_rxOutStream
);
338 nObjLen
= xMark
->offsetToMark(nMark
) - 4;
339 xMark
->jumpToMark(nMark
);
340 _rxOutStream
->writeLong(nObjLen
);
341 xMark
->jumpToFurthest();
342 xMark
->deleteMark(nMark
);
344 catch( const Exception
& )
346 // restore the events
347 if ( m_xEventAttacher
.is() )
348 lcl_restoreEvents( aSave
, m_xEventAttacher
);
352 // restore the events
353 if ( m_xEventAttacher
.is() )
354 lcl_restoreEvents( aSave
, m_xEventAttacher
);
358 struct TransformEventTo52Format
: public ::std::unary_function
< ScriptEventDescriptor
, void >
360 void operator()( ScriptEventDescriptor
& _rDescriptor
)
362 if ( _rDescriptor
.ScriptType
== "StarBasic" )
363 { // it's a starbasic macro
364 sal_Int32 nPrefixLength
= _rDescriptor
.ScriptCode
.indexOf( ':' );
365 if ( 0 <= nPrefixLength
)
366 { // the macro name does not already contain a :
368 const OUString sPrefix
= _rDescriptor
.ScriptCode
.copy( 0, nPrefixLength
);
369 DBG_ASSERT( sPrefix
== "document"
370 || sPrefix
== "application",
371 "TransformEventTo52Format: invalid (unknown) prefix!" );
374 _rDescriptor
.ScriptCode
= _rDescriptor
.ScriptCode
.copy( nPrefixLength
+ 1 );
381 struct TransformEventTo60Format
: public ::std::unary_function
< ScriptEventDescriptor
, void >
383 void operator()( ScriptEventDescriptor
& _rDescriptor
)
385 if ( _rDescriptor
.ScriptType
== "StarBasic" )
386 { // it's a starbasic macro
387 if ( _rDescriptor
.ScriptCode
.indexOf( ':' ) < 0 )
388 { // the macro name does not already contain a :
389 // -> default the type to "document"
390 _rDescriptor
.ScriptCode
= "document:" + _rDescriptor
.ScriptCode
;
397 void OInterfaceContainer::transformEvents( const EventFormat _eTargetFormat
)
399 OSL_ENSURE( m_xEventAttacher
.is(), "OInterfaceContainer::transformEvents: no event attacher manager!" );
400 if ( !m_xEventAttacher
.is() )
405 // loop through all our children
406 sal_Int32 nItems
= m_aItems
.size();
407 Sequence
< ScriptEventDescriptor
> aChildEvents
;
409 for (sal_Int32 i
=0; i
<nItems
; ++i
)
411 // get the script events for this object
412 aChildEvents
= m_xEventAttacher
->getScriptEvents( i
);
414 if ( aChildEvents
.getLength() )
416 // the "iterators" for the events for this child
417 ScriptEventDescriptor
* pChildEvents
= aChildEvents
.getArray();
418 ScriptEventDescriptor
* pChildEventsEnd
= pChildEvents
+ aChildEvents
.getLength();
420 // do the transformation
421 if ( efVersionSO6x
== _eTargetFormat
)
422 ::std::for_each( pChildEvents
, pChildEventsEnd
, TransformEventTo60Format() );
424 ::std::for_each( pChildEvents
, pChildEventsEnd
, TransformEventTo52Format() );
426 // revoke the script events
427 m_xEventAttacher
->revokeScriptEvents( i
);
428 // and re-register them
429 m_xEventAttacher
->registerScriptEvents( i
, aChildEvents
);
433 catch( const Exception
& )
435 DBG_UNHANDLED_EXCEPTION();
440 void SAL_CALL
OInterfaceContainer::readEvents(const Reference
<XObjectInputStream
>& _rxInStream
)
442 ::osl::MutexGuard
aGuard( m_rMutex
);
444 // Read scripting info
445 Reference
<XMarkableStream
> xMark(_rxInStream
, UNO_QUERY
);
446 sal_Int32 nObjLen
= _rxInStream
->readLong();
449 sal_Int32 nMark
= xMark
->createMark();
450 Reference
<XPersistObject
> xObj(m_xEventAttacher
, UNO_QUERY
);
452 xObj
->read(_rxInStream
);
453 xMark
->jumpToMark(nMark
);
454 _rxInStream
->skipBytes(nObjLen
);
455 xMark
->deleteMark(nMark
);
459 if ( m_xEventAttacher
.is() )
461 OInterfaceArray::const_iterator aAttach
= m_aItems
.begin();
462 OInterfaceArray::const_iterator aAttachEnd
= m_aItems
.end();
463 for ( sal_Int32 i
=0; aAttach
!= aAttachEnd
; ++aAttach
, ++i
)
465 Reference
< XInterface
> xAsIFace( *aAttach
, UNO_QUERY
); // important to normalize this ....
466 Reference
< XPropertySet
> xAsSet( xAsIFace
, UNO_QUERY
);
467 m_xEventAttacher
->attach( i
, xAsIFace
, makeAny( xAsSet
) );
473 void SAL_CALL
OInterfaceContainer::write( const Reference
< XObjectOutputStream
>& _rxOutStream
) throw(IOException
, RuntimeException
, std::exception
)
475 ::osl::MutexGuard
aGuard( m_rMutex
);
476 sal_Int32 nLen
= m_aItems
.size();
479 _rxOutStream
->writeLong(nLen
);
484 _rxOutStream
->writeShort(0x0001);
487 for (sal_Int32 i
= 0; i
< nLen
; i
++)
489 Reference
<XPersistObject
> xObj(m_aItems
[i
], UNO_QUERY
);
491 _rxOutStream
->writeObject(xObj
);
499 writeEvents(_rxOutStream
);
506 Reference
< XPersistObject
> lcl_createPlaceHolder( const Reference
< XComponentContext
>& _rxORB
)
508 Reference
< XPersistObject
> xObject( _rxORB
->getServiceManager()->createInstanceWithContext(FRM_COMPONENT_HIDDENCONTROL
, _rxORB
), UNO_QUERY
);
509 DBG_ASSERT( xObject
.is(), "lcl_createPlaceHolder: could not create a substitute for the unknown object!" );
512 // set some properties describing what we did
513 Reference
< XPropertySet
> xObjProps( xObject
, UNO_QUERY
);
518 xObjProps
->setPropertyValue( PROPERTY_NAME
, makeAny( FRM_RES_STRING( RID_STR_CONTROL_SUBSTITUTED_NAME
) ) );
519 xObjProps
->setPropertyValue( PROPERTY_TAG
, makeAny( FRM_RES_STRING( RID_STR_CONTROL_SUBSTITUTED_EPXPLAIN
) ) );
521 catch(const Exception
&)
531 void SAL_CALL
OInterfaceContainer::read( const Reference
< XObjectInputStream
>& _rxInStream
) throw(IOException
, RuntimeException
, std::exception
)
533 ::osl::MutexGuard
aGuard( m_rMutex
);
535 // after ::read the object is expected to be in the state it was when ::write was called, so we have
536 // to empty ourself here
540 // Only writes depending on the length
541 sal_Int32 nLen
= _rxInStream
->readLong();
546 sal_uInt16 nVersion
= _rxInStream
->readShort(); (void)nVersion
;
549 for (sal_Int32 i
= 0; i
< nLen
; i
++)
551 Reference
<XPersistObject
> xObj
;
554 xObj
= _rxInStream
->readObject();
556 catch(const WrongFormatException
&)
558 // the object could not be read
559 // create a object (so the readEvents below will assign the events to the right controls)
560 xObj
= lcl_createPlaceHolder( m_xContext
);
562 // couldn't handle it
565 catch(const Exception
&)
568 while (!m_aItems
.empty())
569 removeElementsNoEvents(0);
571 // Rethrow the exception
577 Reference
< XPropertySet
> xElement( xObj
, UNO_QUERY
);
581 m_aItems
.size(), // position
582 xElement
, // element to insert
583 false, // no event attacher manager handling
584 NULL
, // not yet approved - let implInsert do it
585 true // fire the event
588 catch( const Exception
& )
590 SAL_WARN("forms.misc", "OInterfaceContainerHelper::read: reading succeeded, but not inserting!" );
591 // create a placeholder
592 xElement
.set(lcl_createPlaceHolder( m_xContext
), css::uno::UNO_QUERY
);
593 if ( !xElement
.is() )
594 // couldn't handle it
596 // insert the placeholder
597 implInsert( m_aItems
.size(), xElement
, false, NULL
, true );
602 readEvents(_rxInStream
);
608 m_xEventAttacher
= ::comphelper::createEventAttacherManager( m_xContext
);
609 OSL_ENSURE( m_xEventAttacher
.is(), "OInterfaceContainer::read: could not create an event attacher manager!" );
611 catch( const Exception
& )
613 DBG_UNHANDLED_EXCEPTION();
620 void SAL_CALL
OInterfaceContainer::addContainerListener(const Reference
<XContainerListener
>& _rxListener
) throw( RuntimeException
, std::exception
)
622 m_aContainerListeners
.addInterface(_rxListener
);
626 void SAL_CALL
OInterfaceContainer::removeContainerListener(const Reference
<XContainerListener
>& _rxListener
) throw( RuntimeException
, std::exception
)
628 m_aContainerListeners
.removeInterface(_rxListener
);
633 void SAL_CALL
OInterfaceContainer::disposing(const css::lang::EventObject
& _rSource
) throw( RuntimeException
, std::exception
)
635 ::osl::MutexGuard
aGuard( m_rMutex
);
637 Reference
< XInterface
> xSource( _rSource
.Source
, UNO_QUERY
);
640 OInterfaceArray::iterator j
;
641 for ( j
= m_aItems
.begin(); j
!= m_aItems
.end(); ++j
)
643 DBG_ASSERT( j
->get() == Reference
< XInterface
>( *j
, UNO_QUERY
).get(),
644 "OInterfaceContainer::disposing: vector element not normalized!" );
646 if ( xSource
.get() == j
->get() )
651 if ( m_aItems
.end() != j
)
655 // look up in, and erase from, m_aMap, too
656 OInterfaceMap::iterator i
= m_aMap
.begin();
657 while ( i
!= m_aMap
.end() )
659 DBG_ASSERT( i
->second
.get() == Reference
< XInterface
>( i
->second
, UNO_QUERY
).get(),
660 "OInterfaceContainer::disposing: map element not normalized!" );
662 if ( i
->second
.get() == xSource
.get() )
671 DBG_ASSERT( i
!= m_aMap
.end(), "OInterfaceContainer::disposing: inconsistency: the element was in m_aItems, but not in m_aMap!" );
676 // XPropertyChangeListener
678 void OInterfaceContainer::propertyChange(const PropertyChangeEvent
& evt
)
679 throw (::com::sun::star::uno::RuntimeException
, std::exception
) {
680 if (evt
.PropertyName
== PROPERTY_NAME
)
682 ::osl::MutexGuard
aGuard( m_rMutex
);
683 OInterfaceMap::iterator i
= m_aMap
.find(::comphelper::getString(evt
.OldValue
));
684 if (i
!= m_aMap
.end() && (*i
).second
!= evt
.Source
)
686 InterfaceRef
xCorrectType((*i
).second
);
688 m_aMap
.insert(::std::pair
<const OUString
, InterfaceRef
>(::comphelper::getString(evt
.NewValue
),xCorrectType
));
695 sal_Bool SAL_CALL
OInterfaceContainer::hasElements() throw( RuntimeException
, std::exception
)
697 return !m_aMap
.empty();
701 Type SAL_CALL
OInterfaceContainer::getElementType() throw(RuntimeException
, std::exception
)
703 return m_aElementType
;
706 // XEnumerationAccess
708 Reference
<XEnumeration
> SAL_CALL
OInterfaceContainer::createEnumeration() throw( RuntimeException
, std::exception
)
710 ::osl::MutexGuard
aGuard( m_rMutex
);
711 return new ::comphelper::OEnumerationByIndex(static_cast<XIndexAccess
*>(this));
716 Any SAL_CALL
OInterfaceContainer::getByName( const OUString
& _rName
) throw(NoSuchElementException
, WrappedTargetException
, RuntimeException
, std::exception
)
718 ::std::pair
<OInterfaceMap::iterator
,
719 OInterfaceMap::iterator
> aPair
= m_aMap
.equal_range(_rName
);
721 if (aPair
.first
== aPair
.second
)
722 throw NoSuchElementException();
724 return (*aPair
.first
).second
->queryInterface( m_aElementType
);
728 StringSequence SAL_CALL
OInterfaceContainer::getElementNames() throw(RuntimeException
, std::exception
)
730 StringSequence
aNameList(m_aItems
.size());
731 OUString
* pStringArray
= aNameList
.getArray();
733 for (OInterfaceMap::const_iterator i
= m_aMap
.begin(); i
!= m_aMap
.end(); ++i
, ++pStringArray
)
735 *pStringArray
= (*i
).first
;
741 sal_Bool SAL_CALL
OInterfaceContainer::hasByName( const OUString
& _rName
) throw(RuntimeException
, std::exception
)
743 ::std::pair
<OInterfaceMap::iterator
,
744 OInterfaceMap::iterator
> aPair
= m_aMap
.equal_range(_rName
);
745 return aPair
.first
!= aPair
.second
;
750 sal_Int32
OInterfaceContainer::getCount() throw( RuntimeException
, std::exception
)
752 return m_aItems
.size();
756 Any
OInterfaceContainer::getByIndex(sal_Int32 _nIndex
) throw( IndexOutOfBoundsException
, WrappedTargetException
, RuntimeException
, std::exception
)
758 if (_nIndex
< 0 || (_nIndex
>= (sal_Int32
)m_aItems
.size()))
759 throw IndexOutOfBoundsException();
761 return m_aItems
[_nIndex
]->queryInterface( m_aElementType
);
765 void OInterfaceContainer::approveNewElement( const Reference
< XPropertySet
>& _rxObject
, ElementDescription
* _pElement
)
767 // it has to be non-NULL
768 if ( !_rxObject
.is() )
769 throw IllegalArgumentException(FRM_RES_STRING(RID_STR_NEED_NON_NULL_OBJECT
), static_cast<XContainer
*>(this), 1);
771 // it has to support our element type interface
772 Any aCorrectType
= _rxObject
->queryInterface( m_aElementType
);
773 if ( !aCorrectType
.hasValue() )
774 lcl_throwIllegalArgumentException();
776 // it has to have a "Name" property
777 if ( !hasProperty( PROPERTY_NAME
, _rxObject
) )
778 lcl_throwIllegalArgumentException();
780 // it has to be a child, and it must not have a parent already
781 Reference
< XChild
> xChild( _rxObject
, UNO_QUERY
);
782 if ( !xChild
.is() || xChild
->getParent().is() )
784 lcl_throwIllegalArgumentException();
787 // passed all tests. cache the information we have so far
788 DBG_ASSERT( _pElement
, "OInterfaceContainer::approveNewElement: invalid event descriptor!" );
791 _pElement
->xPropertySet
= _rxObject
;
792 _pElement
->xChild
= xChild
;
793 _pElement
->aElementTypeInterface
= aCorrectType
;
794 _pElement
->xInterface
= Reference
< XInterface
>( _rxObject
, UNO_QUERY
); // normalized XInterface
799 void OInterfaceContainer::implInsert(sal_Int32 _nIndex
, const Reference
< XPropertySet
>& _rxElement
,
800 bool _bEvents
, ElementDescription
* _pApprovalResult
, bool _bFire
) throw( IllegalArgumentException
)
802 const bool bHandleEvents
= _bEvents
&& m_xEventAttacher
.is();
804 // SYNCHRONIZED ----->
805 ::osl::ClearableMutexGuard
aGuard( m_rMutex
);
807 boost::scoped_ptr
< ElementDescription
> aAutoDeleteMetaData
;
808 ElementDescription
* pElementMetaData
= _pApprovalResult
;
809 if ( !pElementMetaData
)
810 { // not yet approved by the caller -> do ourself
811 pElementMetaData
= createElementMetaData();
812 DBG_ASSERT( pElementMetaData
, "OInterfaceContainer::implInsert: createElementMetaData returned nonsense!" );
814 // ensure that the meta data structure will be deleted later on
815 aAutoDeleteMetaData
.reset( pElementMetaData
);
817 // will throw an exception if necessary
818 approveNewElement( _rxElement
, pElementMetaData
);
822 // approveNewElement (no matter if called here or outside) has ensure that all relevant interfaces
825 // set the name, and add as change listener for the name
827 _rxElement
->getPropertyValue(PROPERTY_NAME
) >>= sName
;
828 _rxElement
->addPropertyChangeListener(PROPERTY_NAME
, this);
830 // insert the object into our internal structures
831 if (_nIndex
> (sal_Int32
)m_aItems
.size()) // Calculate the actual index
833 _nIndex
= m_aItems
.size();
834 m_aItems
.push_back( pElementMetaData
->xInterface
);
837 m_aItems
.insert( m_aItems
.begin() + _nIndex
, pElementMetaData
->xInterface
);
839 m_aMap
.insert( ::std::pair
< const OUString
, InterfaceRef
>( sName
, pElementMetaData
->xInterface
) );
841 // announce ourself as parent to the new element
842 pElementMetaData
->xChild
->setParent(static_cast<XContainer
*>(this));
847 m_xEventAttacher
->insertEntry(_nIndex
);
848 m_xEventAttacher
->attach( _nIndex
, pElementMetaData
->xInterface
, makeAny( _rxElement
) );
851 // notify derived classes
852 implInserted( pElementMetaData
);
855 // <----- SYNCHRONIZED
857 // insert faked VBA events?
858 bool bHandleVbaEvents
= false;
861 _rxElement
->getPropertyValue("GenerateVbaEvents") >>= bHandleVbaEvents
;
863 catch( const Exception
& )
866 if ( bHandleVbaEvents
)
868 Reference
< XEventAttacherManager
> xMgr ( pElementMetaData
->xInterface
, UNO_QUERY
);
869 OInterfaceContainer
* pIfcMgr
= xMgr
.is() ? dynamic_cast<OInterfaceContainer
*>(xMgr
.get()) : NULL
;
872 sal_Int32 nLen
= pIfcMgr
->getCount();
873 for (sal_Int32 i
= 0; i
< nLen
; ++i
)
875 // add fake events to the control at index i
876 pIfcMgr
->impl_addVbEvents_nolck_nothrow( i
);
881 // add fake events to the control at index i
882 impl_addVbEvents_nolck_nothrow( _nIndex
);
886 // fire the notification about the change
891 aEvt
.Source
= static_cast<XContainer
*>(this);
892 aEvt
.Accessor
<<= _nIndex
;
893 aEvt
.Element
= pElementMetaData
->aElementTypeInterface
;
896 m_aContainerListeners
.notifyEach( &XContainerListener::elementInserted
, aEvt
);
901 void OInterfaceContainer::removeElementsNoEvents(sal_Int32 nIndex
)
903 OInterfaceArray::iterator i
= m_aItems
.begin() + nIndex
;
904 InterfaceRef
xElement(*i
);
906 OInterfaceMap::iterator j
= m_aMap
.begin();
907 while (j
!= m_aMap
.end() && (*j
).second
!= xElement
) ++j
;
912 Reference
<XPropertySet
> xSet(xElement
, UNO_QUERY
);
914 xSet
->removePropertyChangeListener(PROPERTY_NAME
, this);
916 Reference
<XChild
> xChild(xElement
, UNO_QUERY
);
918 xChild
->setParent(InterfaceRef ());
922 void OInterfaceContainer::implInserted( const ElementDescription
* /*_pElement*/ )
928 void OInterfaceContainer::implRemoved( const InterfaceRef
& /*_rxObject*/ )
934 void OInterfaceContainer::impl_replacedElement( const ContainerEvent
& _rEvent
, ::osl::ClearableMutexGuard
& _rInstanceLock
)
936 _rInstanceLock
.clear();
937 m_aContainerListeners
.notifyEach( &XContainerListener::elementReplaced
, _rEvent
);
942 void SAL_CALL
OInterfaceContainer::insertByIndex( sal_Int32 _nIndex
, const Any
& _rElement
) throw(IllegalArgumentException
, IndexOutOfBoundsException
, WrappedTargetException
, RuntimeException
, std::exception
)
944 Reference
< XPropertySet
> xElement
;
945 _rElement
>>= xElement
;
946 implInsert( _nIndex
, xElement
, true /* event handling */ , NULL
/* not yet approved */ , true /* notification */ );
950 void OInterfaceContainer::implReplaceByIndex( const sal_Int32 _nIndex
, const Any
& _rNewElement
, ::osl::ClearableMutexGuard
& _rClearBeforeNotify
)
952 OSL_PRECOND( ( _nIndex
>= 0 ) && ( _nIndex
< (sal_Int32
)m_aItems
.size() ), "OInterfaceContainer::implReplaceByIndex: precondition not met (index)!" );
954 // approve the new object
955 boost::scoped_ptr
< ElementDescription
> aElementMetaData( createElementMetaData() );
956 DBG_ASSERT( aElementMetaData
.get(), "OInterfaceContainer::implReplaceByIndex: createElementMetaData returned nonsense!" );
958 Reference
< XPropertySet
> xElementProps
;
959 _rNewElement
>>= xElementProps
;
960 approveNewElement( xElementProps
, aElementMetaData
.get() );
963 // get the old element
964 InterfaceRef
xOldElement( m_aItems
[ _nIndex
] );
965 DBG_ASSERT( xOldElement
.get() == Reference
< XInterface
>( xOldElement
, UNO_QUERY
).get(),
966 "OInterfaceContainer::implReplaceByIndex: elements should be held normalized!" );
968 // locate the old element in the map
969 OInterfaceMap::iterator j
= m_aMap
.begin();
970 while ( ( j
!= m_aMap
.end() ) && ( j
->second
.get() != xOldElement
.get() ) )
973 // remove event knittings
974 if ( m_xEventAttacher
.is() )
976 InterfaceRef
xNormalized( xOldElement
, UNO_QUERY
);
977 m_xEventAttacher
->detach( _nIndex
, xNormalized
);
978 m_xEventAttacher
->removeEntry( _nIndex
);
981 // don't listen for property changes anymore
982 Reference
<XPropertySet
> xSet( xOldElement
, UNO_QUERY
);
984 xSet
->removePropertyChangeListener(PROPERTY_NAME
, this);
986 // give the old element a new (void) parent
987 Reference
<XChild
> xChild(xOldElement
, UNO_QUERY
);
989 xChild
->setParent(InterfaceRef ());
991 // remove the old one
994 // examine the new element
996 DBG_ASSERT( aElementMetaData
.get()->xPropertySet
.is(), "OInterfaceContainer::implReplaceByIndex: what did approveNewElement do?" );
998 aElementMetaData
.get()->xPropertySet
->getPropertyValue(PROPERTY_NAME
) >>= sName
;
999 aElementMetaData
.get()->xPropertySet
->addPropertyChangeListener(PROPERTY_NAME
, this);
1001 // insert the new one
1002 m_aMap
.insert( ::std::pair
<const OUString
, InterfaceRef
>( sName
, aElementMetaData
.get()->xInterface
) );
1003 m_aItems
[ _nIndex
] = aElementMetaData
.get()->xInterface
;
1005 aElementMetaData
.get()->xChild
->setParent(static_cast<XContainer
*>(this));
1007 if ( m_xEventAttacher
.is() )
1009 m_xEventAttacher
->insertEntry( _nIndex
);
1010 m_xEventAttacher
->attach( _nIndex
, aElementMetaData
.get()->xInterface
, makeAny( aElementMetaData
.get()->xPropertySet
) );
1013 ContainerEvent aReplaceEvent
;
1014 aReplaceEvent
.Source
= static_cast< XContainer
* >( this );
1015 aReplaceEvent
.Accessor
<<= _nIndex
;
1016 aReplaceEvent
.Element
= aElementMetaData
.get()->xInterface
->queryInterface( m_aElementType
);
1017 aReplaceEvent
.ReplacedElement
= xOldElement
->queryInterface( m_aElementType
);
1019 impl_replacedElement( aReplaceEvent
, _rClearBeforeNotify
);
1023 void OInterfaceContainer::implCheckIndex( const sal_Int32 _nIndex
)
1025 if (_nIndex
< 0 || _nIndex
>= (sal_Int32
)m_aItems
.size())
1026 throw IndexOutOfBoundsException();
1030 void SAL_CALL
OInterfaceContainer::replaceByIndex(sal_Int32 _nIndex
, const Any
& Element
) throw( IllegalArgumentException
, IndexOutOfBoundsException
, WrappedTargetException
, RuntimeException
, std::exception
)
1032 ::osl::ClearableMutexGuard
aGuard( m_rMutex
);
1034 implCheckIndex( _nIndex
);
1036 implReplaceByIndex( _nIndex
, Element
, aGuard
);
1040 void OInterfaceContainer::implRemoveByIndex( const sal_Int32 _nIndex
, ::osl::ClearableMutexGuard
& _rClearBeforeNotify
)
1042 OSL_PRECOND( ( _nIndex
>= 0 ) && ( _nIndex
< (sal_Int32
)m_aItems
.size() ), "OInterfaceContainer::implRemoveByIndex: precondition not met (index)!" );
1044 OInterfaceArray::iterator i
= m_aItems
.begin() + _nIndex
;
1045 InterfaceRef
xElement(*i
);
1047 OInterfaceMap::iterator j
= m_aMap
.begin();
1048 while (j
!= m_aMap
.end() && (*j
).second
!= xElement
) ++j
;
1053 // remove event knittings
1054 if ( m_xEventAttacher
.is() )
1056 InterfaceRef
xNormalized( xElement
, UNO_QUERY
);
1057 m_xEventAttacher
->detach( _nIndex
, xNormalized
);
1058 m_xEventAttacher
->removeEntry( _nIndex
);
1061 Reference
<XPropertySet
> xSet(xElement
, UNO_QUERY
);
1063 xSet
->removePropertyChangeListener(PROPERTY_NAME
, this);
1065 Reference
<XChild
> xChild(xElement
, UNO_QUERY
);
1067 xChild
->setParent(InterfaceRef ());
1069 // notify derived classes
1070 implRemoved(xElement
);
1073 ContainerEvent aEvt
;
1074 aEvt
.Source
= static_cast<XContainer
*>(this);
1075 aEvt
.Element
= xElement
->queryInterface( m_aElementType
);
1076 aEvt
.Accessor
<<= _nIndex
;
1078 _rClearBeforeNotify
.clear();
1079 m_aContainerListeners
.notifyEach( &XContainerListener::elementRemoved
, aEvt
);
1083 void SAL_CALL
OInterfaceContainer::removeByIndex(sal_Int32 _nIndex
) throw( IndexOutOfBoundsException
, WrappedTargetException
, RuntimeException
, std::exception
)
1085 ::osl::ClearableMutexGuard
aGuard( m_rMutex
);
1087 implCheckIndex( _nIndex
);
1089 implRemoveByIndex( _nIndex
, aGuard
);
1093 ElementDescription
* OInterfaceContainer::createElementMetaData( )
1095 return new ElementDescription
;
1099 void SAL_CALL
OInterfaceContainer::insertByName(const OUString
& _rName
, const Any
& _rElement
) throw( IllegalArgumentException
, ElementExistException
, WrappedTargetException
, RuntimeException
, std::exception
)
1101 Reference
< XPropertySet
> xElementProps
;
1103 boost::scoped_ptr
< ElementDescription
> aElementMetaData( createElementMetaData() );
1104 DBG_ASSERT( aElementMetaData
.get(), "OInterfaceContainer::insertByName: createElementMetaData returned nonsense!" );
1106 // ensure the correct name of the element
1109 _rElement
>>= xElementProps
;
1110 approveNewElement( xElementProps
, aElementMetaData
.get() );
1112 xElementProps
->setPropertyValue( PROPERTY_NAME
, makeAny( _rName
) );
1114 catch( const IllegalArgumentException
& )
1116 throw; // allowed to leave
1118 catch( const ElementExistException
& )
1120 throw; // allowed to leave
1122 catch( const Exception
& )
1124 SAL_WARN("forms.misc", "OInterfaceContainer::insertByName: caught an exception!" );
1126 implInsert( m_aItems
.size(), xElementProps
, true, aElementMetaData
.get(), true );
1130 void SAL_CALL
OInterfaceContainer::replaceByName(const OUString
& Name
, const Any
& Element
) throw( IllegalArgumentException
, NoSuchElementException
, WrappedTargetException
, RuntimeException
, std::exception
)
1132 ::osl::ClearableMutexGuard
aGuard( m_rMutex
);
1133 ::std::pair
<OInterfaceMap::iterator
,
1134 OInterfaceMap::iterator
> aPair
= m_aMap
.equal_range(Name
);
1135 if (aPair
.first
== aPair
.second
)
1136 throw NoSuchElementException();
1138 if (Element
.getValueType().getTypeClass() != TypeClass_INTERFACE
)
1139 lcl_throwIllegalArgumentException();
1141 Reference
<XPropertySet
> xSet
;
1145 if (!hasProperty(PROPERTY_NAME
, xSet
))
1146 lcl_throwIllegalArgumentException();
1148 xSet
->setPropertyValue(PROPERTY_NAME
, makeAny(Name
));
1151 // determine the element pos
1152 sal_Int32 nPos
= ::std::find(m_aItems
.begin(), m_aItems
.end(), (*aPair
.first
).second
) - m_aItems
.begin();
1154 implReplaceByIndex( nPos
, Element
, aGuard
);
1158 void SAL_CALL
OInterfaceContainer::removeByName(const OUString
& Name
) throw( NoSuchElementException
, WrappedTargetException
, RuntimeException
, std::exception
)
1160 ::osl::MutexGuard
aGuard( m_rMutex
);
1161 ::std::pair
<OInterfaceMap::iterator
,
1162 OInterfaceMap::iterator
> aPair
= m_aMap
.equal_range(Name
);
1163 if (aPair
.first
== aPair
.second
)
1164 throw NoSuchElementException();
1166 sal_Int32 nPos
= ::std::find(m_aItems
.begin(), m_aItems
.end(), (*aPair
.first
).second
) - m_aItems
.begin();
1167 removeByIndex(nPos
);
1171 // XEventAttacherManager
1173 void SAL_CALL
OInterfaceContainer::registerScriptEvent( sal_Int32 nIndex
, const ScriptEventDescriptor
& aScriptEvent
) throw(IllegalArgumentException
, RuntimeException
, std::exception
)
1175 ::osl::ClearableMutexGuard
aGuard( m_rMutex
);
1176 if ( m_xEventAttacher
.is() )
1178 m_xEventAttacher
->registerScriptEvent( nIndex
, aScriptEvent
);
1180 impl_addVbEvents_nolck_nothrow( nIndex
); // add fake vba events
1185 void SAL_CALL
OInterfaceContainer::registerScriptEvents( sal_Int32 nIndex
, const Sequence
< ScriptEventDescriptor
>& aScriptEvents
) throw(IllegalArgumentException
, RuntimeException
, std::exception
)
1187 ::osl::ClearableMutexGuard
aGuard( m_rMutex
);
1188 if ( m_xEventAttacher
.is() )
1190 m_xEventAttacher
->registerScriptEvents( nIndex
, aScriptEvents
);
1192 impl_addVbEvents_nolck_nothrow( nIndex
); // add fake vba events
1197 void SAL_CALL
OInterfaceContainer::revokeScriptEvent( sal_Int32 nIndex
, const OUString
& aListenerType
, const OUString
& aEventMethod
, const OUString
& aRemoveListenerParam
) throw(IllegalArgumentException
, RuntimeException
, std::exception
)
1199 if ( m_xEventAttacher
.is() )
1200 m_xEventAttacher
->revokeScriptEvent( nIndex
, aListenerType
, aEventMethod
, aRemoveListenerParam
);
1204 void SAL_CALL
OInterfaceContainer::revokeScriptEvents( sal_Int32 nIndex
) throw(IllegalArgumentException
, RuntimeException
, std::exception
)
1206 if ( m_xEventAttacher
.is() )
1207 m_xEventAttacher
->revokeScriptEvents( nIndex
);
1211 void SAL_CALL
OInterfaceContainer::insertEntry( sal_Int32 nIndex
) throw(IllegalArgumentException
, RuntimeException
, std::exception
)
1213 if ( m_xEventAttacher
.is() )
1214 m_xEventAttacher
->insertEntry( nIndex
);
1218 void SAL_CALL
OInterfaceContainer::removeEntry( sal_Int32 nIndex
) throw(IllegalArgumentException
, RuntimeException
, std::exception
)
1220 if ( m_xEventAttacher
.is() )
1221 m_xEventAttacher
->removeEntry( nIndex
);
1225 Sequence
< ScriptEventDescriptor
> SAL_CALL
OInterfaceContainer::getScriptEvents( sal_Int32 nIndex
) throw(IllegalArgumentException
, RuntimeException
, std::exception
)
1227 Sequence
< ScriptEventDescriptor
> aReturn
;
1228 if ( m_xEventAttacher
.is() )
1230 aReturn
= m_xEventAttacher
->getScriptEvents( nIndex
);
1231 if ( lcl_hasVbaEvents( aReturn
) )
1233 aReturn
= lcl_stripVbaEvents( aReturn
);
1240 void SAL_CALL
OInterfaceContainer::attach( sal_Int32 nIndex
, const Reference
< XInterface
>& xObject
, const Any
& aHelper
) throw(IllegalArgumentException
, ServiceNotRegisteredException
, RuntimeException
, std::exception
)
1242 if ( m_xEventAttacher
.is() )
1243 m_xEventAttacher
->attach( nIndex
, xObject
, aHelper
);
1247 void SAL_CALL
OInterfaceContainer::detach( sal_Int32 nIndex
, const Reference
< XInterface
>& xObject
) throw(IllegalArgumentException
, RuntimeException
, std::exception
)
1249 if ( m_xEventAttacher
.is() )
1250 m_xEventAttacher
->detach( nIndex
, xObject
);
1254 void SAL_CALL
OInterfaceContainer::addScriptListener( const Reference
< XScriptListener
>& xListener
) throw(IllegalArgumentException
, RuntimeException
, std::exception
)
1256 if ( m_xEventAttacher
.is() )
1257 m_xEventAttacher
->addScriptListener( xListener
);
1261 void SAL_CALL
OInterfaceContainer::removeScriptListener( const Reference
< XScriptListener
>& xListener
) throw(IllegalArgumentException
, RuntimeException
, std::exception
)
1263 if ( m_xEventAttacher
.is() )
1264 m_xEventAttacher
->removeScriptListener( xListener
);
1271 Any SAL_CALL
OFormComponents::queryAggregation(const Type
& _rType
) throw(RuntimeException
, std::exception
)
1273 Any aReturn
= OFormComponents_BASE::queryInterface(_rType
);
1274 if (!aReturn
.hasValue())
1276 aReturn
= OInterfaceContainer::queryInterface(_rType
);
1278 if (!aReturn
.hasValue())
1279 aReturn
= FormComponentsBase::queryAggregation(_rType
);
1286 Sequence
<Type
> SAL_CALL
OFormComponents::getTypes() throw(RuntimeException
, std::exception
)
1288 return ::comphelper::concatSequences(OInterfaceContainer::getTypes(), FormComponentsBase::getTypes(), OFormComponents_BASE::getTypes());
1292 OFormComponents::OFormComponents(const Reference
<XComponentContext
>& _rxFactory
)
1293 :FormComponentsBase( m_aMutex
)
1294 ,OInterfaceContainer( _rxFactory
, m_aMutex
, cppu::UnoType
<XFormComponent
>::get() )
1295 ,OFormComponents_BASE()
1300 OFormComponents::OFormComponents( const OFormComponents
& _cloneSource
)
1301 :FormComponentsBase( m_aMutex
)
1302 ,OInterfaceContainer( m_aMutex
, _cloneSource
)
1303 ,OFormComponents_BASE()
1308 OFormComponents::~OFormComponents()
1310 if (!FormComponentsBase::rBHelper
.bDisposed
)
1319 void OFormComponents::disposing()
1321 OInterfaceContainer::disposing();
1322 FormComponentsBase::disposing();
1328 void OFormComponents::setParent(const InterfaceRef
& Parent
) throw( NoSupportException
, RuntimeException
, std::exception
)
1330 ::osl::MutexGuard
aGuard( m_aMutex
);
1335 InterfaceRef
OFormComponents::getParent() throw( RuntimeException
, std::exception
)
1344 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */