1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 29*cdf0e10cSrcweir #include "precompiled_forms.hxx" 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir #include "frm_resource.hrc" 32*cdf0e10cSrcweir #include "frm_resource.hxx" 33*cdf0e10cSrcweir #include "InterfaceContainer.hxx" 34*cdf0e10cSrcweir #include "componenttools.hxx" 35*cdf0e10cSrcweir #include "property.hrc" 36*cdf0e10cSrcweir #include "services.hxx" 37*cdf0e10cSrcweir 38*cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp> 39*cdf0e10cSrcweir #include <com/sun/star/container/XNamed.hpp> 40*cdf0e10cSrcweir #include <com/sun/star/io/WrongFormatException.hpp> 41*cdf0e10cSrcweir #include <com/sun/star/io/XMarkableStream.hpp> 42*cdf0e10cSrcweir #include <com/sun/star/lang/XComponent.hpp> 43*cdf0e10cSrcweir #include <com/sun/star/util/XCloneable.hpp> 44*cdf0e10cSrcweir #include <com/sun/star/form/XForm.hpp> 45*cdf0e10cSrcweir 46*cdf0e10cSrcweir #include <comphelper/container.hxx> 47*cdf0e10cSrcweir #include <comphelper/enumhelper.hxx> 48*cdf0e10cSrcweir #include <comphelper/eventattachermgr.hxx> 49*cdf0e10cSrcweir #include <comphelper/property.hxx> 50*cdf0e10cSrcweir #include <comphelper/sequence.hxx> 51*cdf0e10cSrcweir #include <comphelper/types.hxx> 52*cdf0e10cSrcweir #include <cppuhelper/exc_hlp.hxx> 53*cdf0e10cSrcweir #include <cppuhelper/queryinterface.hxx> 54*cdf0e10cSrcweir #include <rtl/logfile.hxx> 55*cdf0e10cSrcweir #include <tools/debug.hxx> 56*cdf0e10cSrcweir #include <tools/diagnose_ex.h> 57*cdf0e10cSrcweir 58*cdf0e10cSrcweir #include <algorithm> 59*cdf0e10cSrcweir #include <memory> 60*cdf0e10cSrcweir 61*cdf0e10cSrcweir //......................................................................... 62*cdf0e10cSrcweir #include <com/sun/star/frame/XModel.hpp> 63*cdf0e10cSrcweir #include <com/sun/star/document/XCodeNameQuery.hpp> 64*cdf0e10cSrcweir #include <ooo/vba/XVBAToOOEventDescGen.hpp> 65*cdf0e10cSrcweir #include <comphelper/processfactory.hxx> 66*cdf0e10cSrcweir 67*cdf0e10cSrcweir namespace frm 68*cdf0e10cSrcweir { 69*cdf0e10cSrcweir //......................................................................... 70*cdf0e10cSrcweir 71*cdf0e10cSrcweir using namespace ::com::sun::star::frame; 72*cdf0e10cSrcweir using namespace ::com::sun::star::lang; 73*cdf0e10cSrcweir using namespace ::com::sun::star::uno; 74*cdf0e10cSrcweir using namespace ::com::sun::star::beans; 75*cdf0e10cSrcweir using namespace ::com::sun::star::document; 76*cdf0e10cSrcweir using namespace ::com::sun::star::container; 77*cdf0e10cSrcweir using namespace ::com::sun::star::script; 78*cdf0e10cSrcweir using namespace ::com::sun::star::io; 79*cdf0e10cSrcweir using namespace ::com::sun::star::form; 80*cdf0e10cSrcweir using namespace ::com::sun::star::util; 81*cdf0e10cSrcweir 82*cdf0e10cSrcweir namespace 83*cdf0e10cSrcweir { 84*cdf0e10cSrcweir //--------------------------------------------------------------------- 85*cdf0e10cSrcweir static void lcl_throwIllegalArgumentException() 86*cdf0e10cSrcweir { 87*cdf0e10cSrcweir throw IllegalArgumentException(); 88*cdf0e10cSrcweir } 89*cdf0e10cSrcweir } 90*cdf0e10cSrcweir 91*cdf0e10cSrcweir bool 92*cdf0e10cSrcweir lcl_hasVbaEvents( const Sequence< ScriptEventDescriptor >& sEvents ) 93*cdf0e10cSrcweir { 94*cdf0e10cSrcweir const ScriptEventDescriptor* pDesc = sEvents.getConstArray(); 95*cdf0e10cSrcweir const ScriptEventDescriptor* pEnd = ( pDesc + sEvents.getLength() ); 96*cdf0e10cSrcweir for ( ; pDesc != pEnd; ++pDesc ) 97*cdf0e10cSrcweir { 98*cdf0e10cSrcweir if ( pDesc->ScriptType.equals( rtl::OUString::createFromAscii( "VBAInterop" ) ) ) 99*cdf0e10cSrcweir return true; 100*cdf0e10cSrcweir } 101*cdf0e10cSrcweir return false; 102*cdf0e10cSrcweir } 103*cdf0e10cSrcweir 104*cdf0e10cSrcweir Sequence< ScriptEventDescriptor > 105*cdf0e10cSrcweir lcl_stripVbaEvents( const Sequence< ScriptEventDescriptor >& sEvents ) 106*cdf0e10cSrcweir { 107*cdf0e10cSrcweir Sequence< ScriptEventDescriptor > sStripped( sEvents.getLength() ); 108*cdf0e10cSrcweir 109*cdf0e10cSrcweir const ScriptEventDescriptor* pDesc = sEvents.getConstArray(); 110*cdf0e10cSrcweir const ScriptEventDescriptor* pEnd = ( pDesc + sEvents.getLength() ); 111*cdf0e10cSrcweir sal_Int32 nCopied = 0; 112*cdf0e10cSrcweir for ( ; pDesc != pEnd; ++pDesc ) 113*cdf0e10cSrcweir { 114*cdf0e10cSrcweir if ( !pDesc->ScriptType.equals( rtl::OUString::createFromAscii( "VBAInterop" ) ) ) 115*cdf0e10cSrcweir { 116*cdf0e10cSrcweir sStripped[ nCopied++ ] = *pDesc; 117*cdf0e10cSrcweir } 118*cdf0e10cSrcweir } 119*cdf0e10cSrcweir if ( nCopied ) 120*cdf0e10cSrcweir sStripped.realloc( nCopied ); 121*cdf0e10cSrcweir return sStripped; 122*cdf0e10cSrcweir } 123*cdf0e10cSrcweir 124*cdf0e10cSrcweir void OInterfaceContainer::impl_addVbEvents_nolck_nothrow( const sal_Int32 i_nIndex ) 125*cdf0e10cSrcweir { 126*cdf0e10cSrcweir // we are dealing with form controls 127*cdf0e10cSrcweir try 128*cdf0e10cSrcweir { 129*cdf0e10cSrcweir do 130*cdf0e10cSrcweir { 131*cdf0e10cSrcweir Reference< XModel > xDoc( getXModel( static_cast< XContainer *> ( this ) ) ); 132*cdf0e10cSrcweir if ( !xDoc.is() ) 133*cdf0e10cSrcweir break; 134*cdf0e10cSrcweir 135*cdf0e10cSrcweir Reference< XMultiServiceFactory > xDocFac( xDoc, UNO_QUERY_THROW ); 136*cdf0e10cSrcweir Reference< XCodeNameQuery > xNameQuery( xDocFac->createInstance( rtl::OUString::createFromAscii( "ooo.vba.VBACodeNameProvider" ) ), UNO_QUERY ); 137*cdf0e10cSrcweir if ( !xNameQuery.is() ) 138*cdf0e10cSrcweir break; 139*cdf0e10cSrcweir 140*cdf0e10cSrcweir ::osl::MutexGuard aGuard( m_rMutex ); 141*cdf0e10cSrcweir bool hasVBABindings = lcl_hasVbaEvents( m_xEventAttacher->getScriptEvents( i_nIndex ) ); 142*cdf0e10cSrcweir if ( hasVBABindings ) 143*cdf0e10cSrcweir break; 144*cdf0e10cSrcweir 145*cdf0e10cSrcweir Reference< XInterface > xElement( getByIndex( i_nIndex ) , UNO_QUERY_THROW ); 146*cdf0e10cSrcweir Reference< XForm > xElementAsForm( xElement, UNO_QUERY ); 147*cdf0e10cSrcweir if ( xElementAsForm.is() ) 148*cdf0e10cSrcweir break; 149*cdf0e10cSrcweir 150*cdf0e10cSrcweir ::rtl::OUString sCodeName( xNameQuery->getCodeNameForObject( xElement ) ); 151*cdf0e10cSrcweir 152*cdf0e10cSrcweir Reference< XPropertySet > xProps( xElement, UNO_QUERY_THROW ); 153*cdf0e10cSrcweir ::rtl::OUString sServiceName; 154*cdf0e10cSrcweir xProps->getPropertyValue( rtl::OUString::createFromAscii("DefaultControl" ) ) >>= sServiceName; 155*cdf0e10cSrcweir 156*cdf0e10cSrcweir Reference< ooo::vba::XVBAToOOEventDescGen > xDescSupplier( m_xServiceFactory->createInstance( rtl::OUString::createFromAscii( "ooo.vba.VBAToOOEventDesc" ) ), UNO_QUERY_THROW ); 157*cdf0e10cSrcweir Sequence< ScriptEventDescriptor > vbaEvents = xDescSupplier->getEventDescriptions( m_xServiceFactory->createInstance( sServiceName ), sCodeName ); 158*cdf0e10cSrcweir // register the vba script events 159*cdf0e10cSrcweir m_xEventAttacher->registerScriptEvents( i_nIndex, vbaEvents ); 160*cdf0e10cSrcweir } 161*cdf0e10cSrcweir while ( false ); 162*cdf0e10cSrcweir } 163*cdf0e10cSrcweir catch ( const ServiceNotRegisteredException& ) 164*cdf0e10cSrcweir { 165*cdf0e10cSrcweir // silence this, not all document types support the ooo.vba.VBACodeNameProvider service 166*cdf0e10cSrcweir } 167*cdf0e10cSrcweir catch( const Exception& ) 168*cdf0e10cSrcweir { 169*cdf0e10cSrcweir DBG_UNHANDLED_EXCEPTION(); 170*cdf0e10cSrcweir } 171*cdf0e10cSrcweir 172*cdf0e10cSrcweir } 173*cdf0e10cSrcweir //================================================================== 174*cdf0e10cSrcweir //= ElementDescription 175*cdf0e10cSrcweir //================================================================== 176*cdf0e10cSrcweir //------------------------------------------------------------------ 177*cdf0e10cSrcweir ElementDescription::ElementDescription( ) 178*cdf0e10cSrcweir { 179*cdf0e10cSrcweir } 180*cdf0e10cSrcweir 181*cdf0e10cSrcweir //------------------------------------------------------------------ 182*cdf0e10cSrcweir ElementDescription::~ElementDescription() 183*cdf0e10cSrcweir { 184*cdf0e10cSrcweir } 185*cdf0e10cSrcweir 186*cdf0e10cSrcweir //================================================================== 187*cdf0e10cSrcweir //= OInterfaceContainer 188*cdf0e10cSrcweir //================================================================== 189*cdf0e10cSrcweir //------------------------------------------------------------------ 190*cdf0e10cSrcweir OInterfaceContainer::OInterfaceContainer( 191*cdf0e10cSrcweir const Reference<XMultiServiceFactory>& _rxFactory, 192*cdf0e10cSrcweir ::osl::Mutex& _rMutex, 193*cdf0e10cSrcweir const Type& _rElementType) 194*cdf0e10cSrcweir :OInterfaceContainer_BASE() 195*cdf0e10cSrcweir ,m_rMutex(_rMutex) 196*cdf0e10cSrcweir ,m_aContainerListeners(_rMutex) 197*cdf0e10cSrcweir ,m_aElementType(_rElementType) 198*cdf0e10cSrcweir ,m_xServiceFactory(_rxFactory) 199*cdf0e10cSrcweir { 200*cdf0e10cSrcweir impl_createEventAttacher_nothrow(); 201*cdf0e10cSrcweir } 202*cdf0e10cSrcweir 203*cdf0e10cSrcweir //------------------------------------------------------------------------------ 204*cdf0e10cSrcweir OInterfaceContainer::OInterfaceContainer( ::osl::Mutex& _rMutex, const OInterfaceContainer& _cloneSource ) 205*cdf0e10cSrcweir :OInterfaceContainer_BASE() 206*cdf0e10cSrcweir ,m_rMutex( _rMutex ) 207*cdf0e10cSrcweir ,m_aContainerListeners( _rMutex ) 208*cdf0e10cSrcweir ,m_aElementType( _cloneSource.m_aElementType ) 209*cdf0e10cSrcweir ,m_xServiceFactory( _cloneSource.m_xServiceFactory ) 210*cdf0e10cSrcweir { 211*cdf0e10cSrcweir impl_createEventAttacher_nothrow(); 212*cdf0e10cSrcweir } 213*cdf0e10cSrcweir 214*cdf0e10cSrcweir //------------------------------------------------------------------------------ 215*cdf0e10cSrcweir void OInterfaceContainer::clonedFrom( const OInterfaceContainer& _cloneSource ) 216*cdf0e10cSrcweir { 217*cdf0e10cSrcweir try 218*cdf0e10cSrcweir { 219*cdf0e10cSrcweir const Reference< XIndexAccess > xSourceHierarchy( const_cast< OInterfaceContainer* >( &_cloneSource ) ); 220*cdf0e10cSrcweir const sal_Int32 nCount = xSourceHierarchy->getCount(); 221*cdf0e10cSrcweir for ( sal_Int32 i=0; i<nCount; ++i ) 222*cdf0e10cSrcweir { 223*cdf0e10cSrcweir Reference< XCloneable > xCloneable( xSourceHierarchy->getByIndex( i ), UNO_QUERY_THROW ); 224*cdf0e10cSrcweir Reference< XInterface > xClone( xCloneable->createClone() ); 225*cdf0e10cSrcweir insertByIndex( i, makeAny( xClone ) ); 226*cdf0e10cSrcweir } 227*cdf0e10cSrcweir } 228*cdf0e10cSrcweir catch( const Exception& ) 229*cdf0e10cSrcweir { 230*cdf0e10cSrcweir throw WrappedTargetException( 231*cdf0e10cSrcweir ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Could not clone the given interface hierarchy." ) ), 232*cdf0e10cSrcweir static_cast< XIndexContainer* >( const_cast< OInterfaceContainer* >( &_cloneSource ) ), 233*cdf0e10cSrcweir ::cppu::getCaughtException() 234*cdf0e10cSrcweir ); 235*cdf0e10cSrcweir } 236*cdf0e10cSrcweir } 237*cdf0e10cSrcweir 238*cdf0e10cSrcweir //------------------------------------------------------------------------------ 239*cdf0e10cSrcweir void OInterfaceContainer::impl_createEventAttacher_nothrow() 240*cdf0e10cSrcweir { 241*cdf0e10cSrcweir try 242*cdf0e10cSrcweir { 243*cdf0e10cSrcweir m_xEventAttacher.set( ::comphelper::createEventAttacherManager( m_xServiceFactory ), UNO_SET_THROW ); 244*cdf0e10cSrcweir } 245*cdf0e10cSrcweir catch( const Exception& ) 246*cdf0e10cSrcweir { 247*cdf0e10cSrcweir DBG_UNHANDLED_EXCEPTION(); 248*cdf0e10cSrcweir } 249*cdf0e10cSrcweir } 250*cdf0e10cSrcweir 251*cdf0e10cSrcweir //------------------------------------------------------------------------------ 252*cdf0e10cSrcweir OInterfaceContainer::~OInterfaceContainer() 253*cdf0e10cSrcweir { 254*cdf0e10cSrcweir } 255*cdf0e10cSrcweir 256*cdf0e10cSrcweir //------------------------------------------------------------------------------ 257*cdf0e10cSrcweir void OInterfaceContainer::disposing() 258*cdf0e10cSrcweir { 259*cdf0e10cSrcweir // dispose all elements 260*cdf0e10cSrcweir for (sal_Int32 i = m_aItems.size(); i > 0; --i) 261*cdf0e10cSrcweir { 262*cdf0e10cSrcweir Reference<XPropertySet> xSet(m_aItems[i - 1], UNO_QUERY); 263*cdf0e10cSrcweir if (xSet.is()) 264*cdf0e10cSrcweir xSet->removePropertyChangeListener(PROPERTY_NAME, this); 265*cdf0e10cSrcweir 266*cdf0e10cSrcweir // revoke event knittings 267*cdf0e10cSrcweir if ( m_xEventAttacher.is() ) 268*cdf0e10cSrcweir { 269*cdf0e10cSrcweir Reference< XInterface > xIfc( xSet, UNO_QUERY ); 270*cdf0e10cSrcweir m_xEventAttacher->detach( i - 1, xIfc ); 271*cdf0e10cSrcweir m_xEventAttacher->removeEntry( i - 1 ); 272*cdf0e10cSrcweir } 273*cdf0e10cSrcweir 274*cdf0e10cSrcweir Reference<XComponent> xComponent(xSet, UNO_QUERY); 275*cdf0e10cSrcweir if (xComponent.is()) 276*cdf0e10cSrcweir xComponent->dispose(); 277*cdf0e10cSrcweir } 278*cdf0e10cSrcweir m_aMap.clear(); 279*cdf0e10cSrcweir m_aItems.clear(); 280*cdf0e10cSrcweir 281*cdf0e10cSrcweir EventObject aEvt(static_cast<XContainer*>(this)); 282*cdf0e10cSrcweir m_aContainerListeners.disposeAndClear(aEvt); 283*cdf0e10cSrcweir } 284*cdf0e10cSrcweir 285*cdf0e10cSrcweir // XPersistObject 286*cdf0e10cSrcweir //------------------------------------------------------------------------------ 287*cdf0e10cSrcweir namespace 288*cdf0e10cSrcweir { 289*cdf0e10cSrcweir //.......................................................................... 290*cdf0e10cSrcweir void lcl_saveEvents( ::std::vector< Sequence< ScriptEventDescriptor > >& _rSave, 291*cdf0e10cSrcweir const Reference< XEventAttacherManager >& _rxManager, const sal_Int32 _nItemCount ) 292*cdf0e10cSrcweir { 293*cdf0e10cSrcweir OSL_ENSURE( _rxManager.is(), "lcl_saveEvents: invalid event attacher manager!" ); 294*cdf0e10cSrcweir if ( !_rxManager.is() ) 295*cdf0e10cSrcweir return; 296*cdf0e10cSrcweir 297*cdf0e10cSrcweir // reserve the space needed 298*cdf0e10cSrcweir _rSave.reserve( _nItemCount ); 299*cdf0e10cSrcweir 300*cdf0e10cSrcweir // copy the events 301*cdf0e10cSrcweir for (sal_Int32 i=0; i<_nItemCount; ++i) 302*cdf0e10cSrcweir _rSave.push_back(_rxManager->getScriptEvents( i )); 303*cdf0e10cSrcweir } 304*cdf0e10cSrcweir 305*cdf0e10cSrcweir //.......................................................................... 306*cdf0e10cSrcweir void lcl_restoreEvents( const ::std::vector< Sequence< ScriptEventDescriptor > >& _rSave, 307*cdf0e10cSrcweir const Reference< XEventAttacherManager >& _rxManager ) 308*cdf0e10cSrcweir { 309*cdf0e10cSrcweir OSL_ENSURE( _rxManager.is(), "lcl_restoreEvents: invalid event attacher manager!" ); 310*cdf0e10cSrcweir if ( !_rxManager.is() ) 311*cdf0e10cSrcweir return; 312*cdf0e10cSrcweir 313*cdf0e10cSrcweir ::std::vector< Sequence< ScriptEventDescriptor > >::const_iterator aLoop = _rSave.begin(); 314*cdf0e10cSrcweir ::std::vector< Sequence< ScriptEventDescriptor > >::const_iterator aEnd = _rSave.end(); 315*cdf0e10cSrcweir for ( sal_Int32 i=0; aLoop != aEnd; ++aLoop, ++i ) 316*cdf0e10cSrcweir { 317*cdf0e10cSrcweir _rxManager->revokeScriptEvents( i ); 318*cdf0e10cSrcweir _rxManager->registerScriptEvents( i, *aLoop ); 319*cdf0e10cSrcweir } 320*cdf0e10cSrcweir } 321*cdf0e10cSrcweir } 322*cdf0e10cSrcweir 323*cdf0e10cSrcweir //------------------------------------------------------------------------------ 324*cdf0e10cSrcweir void SAL_CALL OInterfaceContainer::writeEvents(const Reference<XObjectOutputStream>& _rxOutStream) 325*cdf0e10cSrcweir { 326*cdf0e10cSrcweir // We're writing a document in SO 5.2 format (or even from earlier versions) 327*cdf0e10cSrcweir // -> convert the events from the new runtime format to the format of the 5.2 files 328*cdf0e10cSrcweir // but before, remember the current script events set for our children 329*cdf0e10cSrcweir ::std::vector< Sequence< ScriptEventDescriptor > > aSave; 330*cdf0e10cSrcweir if ( m_xEventAttacher.is() ) 331*cdf0e10cSrcweir lcl_saveEvents( aSave, m_xEventAttacher, m_aItems.size() ); 332*cdf0e10cSrcweir 333*cdf0e10cSrcweir transformEvents( efVersionSO5x ); 334*cdf0e10cSrcweir 335*cdf0e10cSrcweir try 336*cdf0e10cSrcweir { 337*cdf0e10cSrcweir Reference<XMarkableStream> xMark(_rxOutStream, UNO_QUERY); 338*cdf0e10cSrcweir sal_Int32 nMark = xMark->createMark(); 339*cdf0e10cSrcweir 340*cdf0e10cSrcweir sal_Int32 nObjLen = 0; 341*cdf0e10cSrcweir _rxOutStream->writeLong(nObjLen); 342*cdf0e10cSrcweir 343*cdf0e10cSrcweir Reference<XPersistObject> xScripts(m_xEventAttacher, UNO_QUERY); 344*cdf0e10cSrcweir if (xScripts.is()) 345*cdf0e10cSrcweir xScripts->write(_rxOutStream); 346*cdf0e10cSrcweir 347*cdf0e10cSrcweir // feststellen der Laenge 348*cdf0e10cSrcweir nObjLen = xMark->offsetToMark(nMark) - 4; 349*cdf0e10cSrcweir xMark->jumpToMark(nMark); 350*cdf0e10cSrcweir _rxOutStream->writeLong(nObjLen); 351*cdf0e10cSrcweir xMark->jumpToFurthest(); 352*cdf0e10cSrcweir xMark->deleteMark(nMark); 353*cdf0e10cSrcweir } 354*cdf0e10cSrcweir catch( const Exception& ) 355*cdf0e10cSrcweir { 356*cdf0e10cSrcweir // restore the events 357*cdf0e10cSrcweir if ( m_xEventAttacher.is() ) 358*cdf0e10cSrcweir lcl_restoreEvents( aSave, m_xEventAttacher ); 359*cdf0e10cSrcweir throw; 360*cdf0e10cSrcweir } 361*cdf0e10cSrcweir 362*cdf0e10cSrcweir // restore the events 363*cdf0e10cSrcweir if ( m_xEventAttacher.is() ) 364*cdf0e10cSrcweir lcl_restoreEvents( aSave, m_xEventAttacher ); 365*cdf0e10cSrcweir } 366*cdf0e10cSrcweir 367*cdf0e10cSrcweir //------------------------------------------------------------------------------ 368*cdf0e10cSrcweir struct TransformEventTo52Format : public ::std::unary_function< ScriptEventDescriptor, void > 369*cdf0e10cSrcweir { 370*cdf0e10cSrcweir void operator()( ScriptEventDescriptor& _rDescriptor ) 371*cdf0e10cSrcweir { 372*cdf0e10cSrcweir if ( 0 == _rDescriptor.ScriptType.compareToAscii( "StarBasic" ) ) 373*cdf0e10cSrcweir { // it's a starbasic macro 374*cdf0e10cSrcweir sal_Int32 nPrefixLength = _rDescriptor.ScriptCode.indexOf( ':' ); 375*cdf0e10cSrcweir if ( 0 <= nPrefixLength ) 376*cdf0e10cSrcweir { // the macro name does not already contain a : 377*cdf0e10cSrcweir #ifdef DBG_UTIL 378*cdf0e10cSrcweir const ::rtl::OUString sPrefix = _rDescriptor.ScriptCode.copy( 0, nPrefixLength ); 379*cdf0e10cSrcweir DBG_ASSERT( 0 == sPrefix.compareToAscii( "document" ) 380*cdf0e10cSrcweir || 0 == sPrefix.compareToAscii( "application" ), 381*cdf0e10cSrcweir "TransformEventTo52Format: invalid (unknown) prefix!" ); 382*cdf0e10cSrcweir #endif 383*cdf0e10cSrcweir // cut the prefix 384*cdf0e10cSrcweir _rDescriptor.ScriptCode = _rDescriptor.ScriptCode.copy( nPrefixLength + 1 ); 385*cdf0e10cSrcweir } 386*cdf0e10cSrcweir } 387*cdf0e10cSrcweir } 388*cdf0e10cSrcweir }; 389*cdf0e10cSrcweir 390*cdf0e10cSrcweir //------------------------------------------------------------------------------ 391*cdf0e10cSrcweir struct TransformEventTo60Format : public ::std::unary_function< ScriptEventDescriptor, void > 392*cdf0e10cSrcweir { 393*cdf0e10cSrcweir void operator()( ScriptEventDescriptor& _rDescriptor ) 394*cdf0e10cSrcweir { 395*cdf0e10cSrcweir if ( 0 == _rDescriptor.ScriptType.compareToAscii( "StarBasic" ) ) 396*cdf0e10cSrcweir { // it's a starbasic macro 397*cdf0e10cSrcweir if ( _rDescriptor.ScriptCode.indexOf( ':' ) < 0 ) 398*cdf0e10cSrcweir { // the macro name does not already contain a : 399*cdf0e10cSrcweir // -> default the type to "document" 400*cdf0e10cSrcweir ::rtl::OUString sNewScriptCode( RTL_CONSTASCII_USTRINGPARAM( "document:" ) ); 401*cdf0e10cSrcweir sNewScriptCode += _rDescriptor.ScriptCode; 402*cdf0e10cSrcweir _rDescriptor.ScriptCode = sNewScriptCode; 403*cdf0e10cSrcweir } 404*cdf0e10cSrcweir } 405*cdf0e10cSrcweir } 406*cdf0e10cSrcweir }; 407*cdf0e10cSrcweir 408*cdf0e10cSrcweir //------------------------------------------------------------------------------ 409*cdf0e10cSrcweir void OInterfaceContainer::transformEvents( const EventFormat _eTargetFormat ) 410*cdf0e10cSrcweir { 411*cdf0e10cSrcweir OSL_ENSURE( m_xEventAttacher.is(), "OInterfaceContainer::transformEvents: no event attacher manager!" ); 412*cdf0e10cSrcweir if ( !m_xEventAttacher.is() ) 413*cdf0e10cSrcweir return; 414*cdf0e10cSrcweir 415*cdf0e10cSrcweir try 416*cdf0e10cSrcweir { 417*cdf0e10cSrcweir // loop through all our children 418*cdf0e10cSrcweir sal_Int32 nItems = m_aItems.size(); 419*cdf0e10cSrcweir Sequence< ScriptEventDescriptor > aChildEvents; 420*cdf0e10cSrcweir 421*cdf0e10cSrcweir for (sal_Int32 i=0; i<nItems; ++i) 422*cdf0e10cSrcweir { 423*cdf0e10cSrcweir // get the script events for this object 424*cdf0e10cSrcweir aChildEvents = m_xEventAttacher->getScriptEvents( i ); 425*cdf0e10cSrcweir 426*cdf0e10cSrcweir if ( aChildEvents.getLength() ) 427*cdf0e10cSrcweir { 428*cdf0e10cSrcweir // the "iterators" for the events for this child 429*cdf0e10cSrcweir ScriptEventDescriptor* pChildEvents = aChildEvents.getArray(); 430*cdf0e10cSrcweir ScriptEventDescriptor* pChildEventsEnd = pChildEvents + aChildEvents.getLength(); 431*cdf0e10cSrcweir 432*cdf0e10cSrcweir // do the transformation 433*cdf0e10cSrcweir if ( efVersionSO6x == _eTargetFormat ) 434*cdf0e10cSrcweir ::std::for_each( pChildEvents, pChildEventsEnd, TransformEventTo60Format() ); 435*cdf0e10cSrcweir else 436*cdf0e10cSrcweir ::std::for_each( pChildEvents, pChildEventsEnd, TransformEventTo52Format() ); 437*cdf0e10cSrcweir 438*cdf0e10cSrcweir // revoke the script events 439*cdf0e10cSrcweir m_xEventAttacher->revokeScriptEvents( i ); 440*cdf0e10cSrcweir // and re-register them 441*cdf0e10cSrcweir m_xEventAttacher->registerScriptEvents( i, aChildEvents ); 442*cdf0e10cSrcweir } 443*cdf0e10cSrcweir } 444*cdf0e10cSrcweir } 445*cdf0e10cSrcweir catch( const Exception& ) 446*cdf0e10cSrcweir { 447*cdf0e10cSrcweir DBG_UNHANDLED_EXCEPTION(); 448*cdf0e10cSrcweir } 449*cdf0e10cSrcweir } 450*cdf0e10cSrcweir 451*cdf0e10cSrcweir //------------------------------------------------------------------------------ 452*cdf0e10cSrcweir void SAL_CALL OInterfaceContainer::readEvents(const Reference<XObjectInputStream>& _rxInStream) 453*cdf0e10cSrcweir { 454*cdf0e10cSrcweir ::osl::MutexGuard aGuard( m_rMutex ); 455*cdf0e10cSrcweir 456*cdf0e10cSrcweir // Scripting Info lesen 457*cdf0e10cSrcweir Reference<XMarkableStream> xMark(_rxInStream, UNO_QUERY); 458*cdf0e10cSrcweir sal_Int32 nObjLen = _rxInStream->readLong(); 459*cdf0e10cSrcweir if (nObjLen) 460*cdf0e10cSrcweir { 461*cdf0e10cSrcweir sal_Int32 nMark = xMark->createMark(); 462*cdf0e10cSrcweir Reference<XPersistObject> xObj(m_xEventAttacher, UNO_QUERY); 463*cdf0e10cSrcweir if (xObj.is()) 464*cdf0e10cSrcweir xObj->read(_rxInStream); 465*cdf0e10cSrcweir xMark->jumpToMark(nMark); 466*cdf0e10cSrcweir _rxInStream->skipBytes(nObjLen); 467*cdf0e10cSrcweir xMark->deleteMark(nMark); 468*cdf0e10cSrcweir } 469*cdf0e10cSrcweir 470*cdf0e10cSrcweir // Attachement lesen 471*cdf0e10cSrcweir if ( m_xEventAttacher.is() ) 472*cdf0e10cSrcweir { 473*cdf0e10cSrcweir OInterfaceArray::const_iterator aAttach = m_aItems.begin(); 474*cdf0e10cSrcweir OInterfaceArray::const_iterator aAttachEnd = m_aItems.end(); 475*cdf0e10cSrcweir for ( sal_Int32 i=0; aAttach != aAttachEnd; ++aAttach, ++i ) 476*cdf0e10cSrcweir { 477*cdf0e10cSrcweir Reference< XInterface > xAsIFace( *aAttach, UNO_QUERY ); // important to normalize this .... 478*cdf0e10cSrcweir Reference< XPropertySet > xAsSet( xAsIFace, UNO_QUERY ); 479*cdf0e10cSrcweir m_xEventAttacher->attach( i, xAsIFace, makeAny( xAsSet ) ); 480*cdf0e10cSrcweir } 481*cdf0e10cSrcweir } 482*cdf0e10cSrcweir } 483*cdf0e10cSrcweir 484*cdf0e10cSrcweir //------------------------------------------------------------------------------ 485*cdf0e10cSrcweir void SAL_CALL OInterfaceContainer::write( const Reference< XObjectOutputStream >& _rxOutStream ) throw(IOException, RuntimeException) 486*cdf0e10cSrcweir { 487*cdf0e10cSrcweir ::osl::MutexGuard aGuard( m_rMutex ); 488*cdf0e10cSrcweir sal_Int32 nLen = m_aItems.size(); 489*cdf0e10cSrcweir 490*cdf0e10cSrcweir // schreiben der laenge 491*cdf0e10cSrcweir _rxOutStream->writeLong(nLen); 492*cdf0e10cSrcweir 493*cdf0e10cSrcweir if (nLen) 494*cdf0e10cSrcweir { 495*cdf0e10cSrcweir // 1. Version 496*cdf0e10cSrcweir _rxOutStream->writeShort(0x0001); 497*cdf0e10cSrcweir 498*cdf0e10cSrcweir // 2. Objekte 499*cdf0e10cSrcweir for (sal_Int32 i = 0; i < nLen; i++) 500*cdf0e10cSrcweir { 501*cdf0e10cSrcweir Reference<XPersistObject> xObj(m_aItems[i], UNO_QUERY); 502*cdf0e10cSrcweir if (xObj.is()) 503*cdf0e10cSrcweir _rxOutStream->writeObject(xObj); 504*cdf0e10cSrcweir else 505*cdf0e10cSrcweir { 506*cdf0e10cSrcweir // ::com::sun::star::chaos::Error 507*cdf0e10cSrcweir } 508*cdf0e10cSrcweir } 509*cdf0e10cSrcweir 510*cdf0e10cSrcweir // 3. Scripts 511*cdf0e10cSrcweir writeEvents(_rxOutStream); 512*cdf0e10cSrcweir } 513*cdf0e10cSrcweir } 514*cdf0e10cSrcweir 515*cdf0e10cSrcweir //------------------------------------------------------------------------------ 516*cdf0e10cSrcweir namespace 517*cdf0e10cSrcweir { 518*cdf0e10cSrcweir Reference< XPersistObject > lcl_createPlaceHolder( const Reference< XMultiServiceFactory >& _rxORB ) 519*cdf0e10cSrcweir { 520*cdf0e10cSrcweir Reference< XPersistObject > xObject( _rxORB->createInstance( FRM_COMPONENT_HIDDENCONTROL ), UNO_QUERY ); 521*cdf0e10cSrcweir DBG_ASSERT( xObject.is(), "lcl_createPlaceHolder: could not create a substitute for the unknown object!" ); 522*cdf0e10cSrcweir if ( xObject.is() ) 523*cdf0e10cSrcweir { 524*cdf0e10cSrcweir // set some properties describing what we did 525*cdf0e10cSrcweir Reference< XPropertySet > xObjProps( xObject, UNO_QUERY ); 526*cdf0e10cSrcweir if ( xObject.is() ) 527*cdf0e10cSrcweir { 528*cdf0e10cSrcweir try 529*cdf0e10cSrcweir { 530*cdf0e10cSrcweir xObjProps->setPropertyValue( PROPERTY_NAME, makeAny( FRM_RES_STRING( RID_STR_CONTROL_SUBSTITUTED_NAME ) ) ); 531*cdf0e10cSrcweir xObjProps->setPropertyValue( PROPERTY_TAG, makeAny( FRM_RES_STRING( RID_STR_CONTROL_SUBSTITUTED_EPXPLAIN ) ) ); 532*cdf0e10cSrcweir } 533*cdf0e10cSrcweir catch(Exception&) 534*cdf0e10cSrcweir { 535*cdf0e10cSrcweir } 536*cdf0e10cSrcweir } 537*cdf0e10cSrcweir } 538*cdf0e10cSrcweir return xObject; 539*cdf0e10cSrcweir } 540*cdf0e10cSrcweir } 541*cdf0e10cSrcweir 542*cdf0e10cSrcweir //------------------------------------------------------------------------------ 543*cdf0e10cSrcweir void SAL_CALL OInterfaceContainer::read( const Reference< XObjectInputStream >& _rxInStream ) throw(IOException, RuntimeException) 544*cdf0e10cSrcweir { 545*cdf0e10cSrcweir ::osl::MutexGuard aGuard( m_rMutex ); 546*cdf0e10cSrcweir 547*cdf0e10cSrcweir // after ::read the object is expected to be in the state it was when ::write was called, so we have 548*cdf0e10cSrcweir // to empty ourself here 549*cdf0e10cSrcweir // FS - 71598 - 12.01.00 550*cdf0e10cSrcweir while (getCount()) 551*cdf0e10cSrcweir removeByIndex(0); 552*cdf0e10cSrcweir 553*cdf0e10cSrcweir // Schreibt nur in Abhaengigkeit der Laenge 554*cdf0e10cSrcweir sal_Int32 nLen = _rxInStream->readLong(); 555*cdf0e10cSrcweir 556*cdf0e10cSrcweir if (nLen) 557*cdf0e10cSrcweir { 558*cdf0e10cSrcweir // 1. Version 559*cdf0e10cSrcweir sal_uInt16 nVersion = _rxInStream->readShort(); (void)nVersion; 560*cdf0e10cSrcweir 561*cdf0e10cSrcweir // 2. Objekte 562*cdf0e10cSrcweir for (sal_Int32 i = 0; i < nLen; i++) 563*cdf0e10cSrcweir { 564*cdf0e10cSrcweir Reference<XPersistObject> xObj; 565*cdf0e10cSrcweir try 566*cdf0e10cSrcweir { 567*cdf0e10cSrcweir xObj = _rxInStream->readObject(); 568*cdf0e10cSrcweir } 569*cdf0e10cSrcweir catch(WrongFormatException& e) 570*cdf0e10cSrcweir { 571*cdf0e10cSrcweir (void)e; // make compiler happy 572*cdf0e10cSrcweir // the object could not be read 573*cdf0e10cSrcweir // create a object (so the readEvents below will assign the events to the right controls) 574*cdf0e10cSrcweir xObj = lcl_createPlaceHolder( m_xServiceFactory ); 575*cdf0e10cSrcweir if ( !xObj.is() ) 576*cdf0e10cSrcweir // couldn't handle it 577*cdf0e10cSrcweir throw; 578*cdf0e10cSrcweir // 72133 - 09.02.00 - FS 579*cdf0e10cSrcweir } 580*cdf0e10cSrcweir catch(Exception&) 581*cdf0e10cSrcweir { 582*cdf0e10cSrcweir // unsere Map leeren 583*cdf0e10cSrcweir while (!m_aItems.empty()) 584*cdf0e10cSrcweir removeElementsNoEvents(0); 585*cdf0e10cSrcweir 586*cdf0e10cSrcweir // und die Exception nach aussen 587*cdf0e10cSrcweir throw; 588*cdf0e10cSrcweir } 589*cdf0e10cSrcweir 590*cdf0e10cSrcweir if ( xObj.is() ) 591*cdf0e10cSrcweir { 592*cdf0e10cSrcweir Reference< XPropertySet > xElement( xObj, UNO_QUERY ); 593*cdf0e10cSrcweir try 594*cdf0e10cSrcweir { 595*cdf0e10cSrcweir implInsert( 596*cdf0e10cSrcweir m_aItems.size(), // position 597*cdf0e10cSrcweir xElement, // element to insert 598*cdf0e10cSrcweir sal_False, // no event attacher manager handling 599*cdf0e10cSrcweir NULL, // not yet approved - let implInsert do it 600*cdf0e10cSrcweir sal_True // fire the event 601*cdf0e10cSrcweir ); 602*cdf0e10cSrcweir } 603*cdf0e10cSrcweir catch( const Exception& ) 604*cdf0e10cSrcweir { 605*cdf0e10cSrcweir DBG_ERROR( "OInterfaceContainerHelper::read: reading succeeded, but not inserting!" ); 606*cdf0e10cSrcweir // create a placeholder 607*cdf0e10cSrcweir xElement = xElement.query( lcl_createPlaceHolder( m_xServiceFactory ) ); 608*cdf0e10cSrcweir if ( !xElement.is() ) 609*cdf0e10cSrcweir // couldn't handle it 610*cdf0e10cSrcweir throw; 611*cdf0e10cSrcweir // insert the placeholder 612*cdf0e10cSrcweir implInsert( m_aItems.size(), xElement, sal_False, NULL, sal_True ); 613*cdf0e10cSrcweir } 614*cdf0e10cSrcweir } 615*cdf0e10cSrcweir } 616*cdf0e10cSrcweir 617*cdf0e10cSrcweir readEvents(_rxInStream); 618*cdf0e10cSrcweir } 619*cdf0e10cSrcweir else 620*cdf0e10cSrcweir { 621*cdf0e10cSrcweir try 622*cdf0e10cSrcweir { 623*cdf0e10cSrcweir m_xEventAttacher = ::comphelper::createEventAttacherManager( m_xServiceFactory ); 624*cdf0e10cSrcweir OSL_ENSURE( m_xEventAttacher.is(), "OInterfaceContainer::read: could not create an event attacher manager!" ); 625*cdf0e10cSrcweir } 626*cdf0e10cSrcweir catch( const Exception& ) 627*cdf0e10cSrcweir { 628*cdf0e10cSrcweir DBG_UNHANDLED_EXCEPTION(); 629*cdf0e10cSrcweir } 630*cdf0e10cSrcweir } 631*cdf0e10cSrcweir } 632*cdf0e10cSrcweir 633*cdf0e10cSrcweir // XContainer 634*cdf0e10cSrcweir //------------------------------------------------------------------------------ 635*cdf0e10cSrcweir void SAL_CALL OInterfaceContainer::addContainerListener(const Reference<XContainerListener>& _rxListener) throw( RuntimeException ) 636*cdf0e10cSrcweir { 637*cdf0e10cSrcweir m_aContainerListeners.addInterface(_rxListener); 638*cdf0e10cSrcweir } 639*cdf0e10cSrcweir 640*cdf0e10cSrcweir //------------------------------------------------------------------------------ 641*cdf0e10cSrcweir void SAL_CALL OInterfaceContainer::removeContainerListener(const Reference<XContainerListener>& _rxListener) throw( RuntimeException ) 642*cdf0e10cSrcweir { 643*cdf0e10cSrcweir m_aContainerListeners.removeInterface(_rxListener); 644*cdf0e10cSrcweir } 645*cdf0e10cSrcweir 646*cdf0e10cSrcweir // XEventListener 647*cdf0e10cSrcweir //------------------------------------------------------------------------------ 648*cdf0e10cSrcweir void SAL_CALL OInterfaceContainer::disposing(const EventObject& _rSource) throw( RuntimeException ) 649*cdf0e10cSrcweir { 650*cdf0e10cSrcweir ::osl::MutexGuard aGuard( m_rMutex ); 651*cdf0e10cSrcweir 652*cdf0e10cSrcweir Reference< XInterface > xSource( _rSource.Source, UNO_QUERY ); 653*cdf0e10cSrcweir // normalized source 654*cdf0e10cSrcweir 655*cdf0e10cSrcweir OInterfaceArray::iterator j; 656*cdf0e10cSrcweir for ( j = m_aItems.begin(); j != m_aItems.end(); ++j ) 657*cdf0e10cSrcweir { 658*cdf0e10cSrcweir DBG_ASSERT( j->get() == Reference< XInterface >( *j, UNO_QUERY ).get(), 659*cdf0e10cSrcweir "OInterfaceContainer::disposing: vector element not normalized!" ); 660*cdf0e10cSrcweir 661*cdf0e10cSrcweir if ( xSource.get() == j->get() ) 662*cdf0e10cSrcweir // found the element 663*cdf0e10cSrcweir break; 664*cdf0e10cSrcweir } 665*cdf0e10cSrcweir 666*cdf0e10cSrcweir if ( m_aItems.end() != j ) 667*cdf0e10cSrcweir { 668*cdf0e10cSrcweir m_aItems.erase(j); 669*cdf0e10cSrcweir 670*cdf0e10cSrcweir // look up in, and erase from, m_aMap, too 671*cdf0e10cSrcweir OInterfaceMap::iterator i = m_aMap.begin(); 672*cdf0e10cSrcweir while ( i != m_aMap.end() ) 673*cdf0e10cSrcweir { 674*cdf0e10cSrcweir DBG_ASSERT( i->second.get() == Reference< XInterface >( i->second, UNO_QUERY ).get(), 675*cdf0e10cSrcweir "OInterfaceContainer::disposing: map element not normalized!" ); 676*cdf0e10cSrcweir 677*cdf0e10cSrcweir if ( i->second.get() == xSource.get() ) 678*cdf0e10cSrcweir { 679*cdf0e10cSrcweir // found it 680*cdf0e10cSrcweir m_aMap.erase(i); 681*cdf0e10cSrcweir break; 682*cdf0e10cSrcweir } 683*cdf0e10cSrcweir 684*cdf0e10cSrcweir ++i; 685*cdf0e10cSrcweir 686*cdf0e10cSrcweir DBG_ASSERT( i != m_aMap.end(), "OInterfaceContainer::disposing: inconsistency: the element was in m_aItems, but not in m_aMap!" ); 687*cdf0e10cSrcweir } 688*cdf0e10cSrcweir } 689*cdf0e10cSrcweir } 690*cdf0e10cSrcweir 691*cdf0e10cSrcweir // XPropertyChangeListener 692*cdf0e10cSrcweir //------------------------------------------------------------------------------ 693*cdf0e10cSrcweir void OInterfaceContainer::propertyChange(const PropertyChangeEvent& evt) 694*cdf0e10cSrcweir throw (::com::sun::star::uno::RuntimeException) { 695*cdf0e10cSrcweir if (evt.PropertyName == PROPERTY_NAME) 696*cdf0e10cSrcweir { 697*cdf0e10cSrcweir ::osl::MutexGuard aGuard( m_rMutex ); 698*cdf0e10cSrcweir OInterfaceMap::iterator i = ::std::find(m_aMap.begin(), m_aMap.end(), 699*cdf0e10cSrcweir ::std::pair<const ::rtl::OUString, InterfaceRef >(::comphelper::getString(evt.OldValue),evt.Source)); 700*cdf0e10cSrcweir if (i != m_aMap.end()) 701*cdf0e10cSrcweir { 702*cdf0e10cSrcweir InterfaceRef xCorrectType((*i).second); 703*cdf0e10cSrcweir m_aMap.erase(i); 704*cdf0e10cSrcweir m_aMap.insert(::std::pair<const ::rtl::OUString, InterfaceRef >(::comphelper::getString(evt.NewValue),xCorrectType)); 705*cdf0e10cSrcweir } 706*cdf0e10cSrcweir } 707*cdf0e10cSrcweir } 708*cdf0e10cSrcweir 709*cdf0e10cSrcweir // XElementAccess 710*cdf0e10cSrcweir //------------------------------------------------------------------------------ 711*cdf0e10cSrcweir sal_Bool SAL_CALL OInterfaceContainer::hasElements() throw( RuntimeException ) 712*cdf0e10cSrcweir { 713*cdf0e10cSrcweir return !m_aMap.empty(); 714*cdf0e10cSrcweir } 715*cdf0e10cSrcweir 716*cdf0e10cSrcweir //------------------------------------------------------------------------------ 717*cdf0e10cSrcweir Type SAL_CALL OInterfaceContainer::getElementType() throw(RuntimeException) 718*cdf0e10cSrcweir { 719*cdf0e10cSrcweir return m_aElementType; 720*cdf0e10cSrcweir } 721*cdf0e10cSrcweir 722*cdf0e10cSrcweir // XEnumerationAccess 723*cdf0e10cSrcweir //------------------------------------------------------------------------------ 724*cdf0e10cSrcweir Reference<XEnumeration> SAL_CALL OInterfaceContainer::createEnumeration() throw( RuntimeException ) 725*cdf0e10cSrcweir { 726*cdf0e10cSrcweir ::osl::MutexGuard aGuard( m_rMutex ); 727*cdf0e10cSrcweir return new ::comphelper::OEnumerationByIndex(static_cast<XIndexAccess*>(this)); 728*cdf0e10cSrcweir } 729*cdf0e10cSrcweir 730*cdf0e10cSrcweir // XNameAccess 731*cdf0e10cSrcweir //------------------------------------------------------------------------------ 732*cdf0e10cSrcweir Any SAL_CALL OInterfaceContainer::getByName( const ::rtl::OUString& _rName ) throw(NoSuchElementException, WrappedTargetException, RuntimeException) 733*cdf0e10cSrcweir { 734*cdf0e10cSrcweir ::std::pair <OInterfaceMap::iterator, 735*cdf0e10cSrcweir OInterfaceMap::iterator> aPair = m_aMap.equal_range(_rName); 736*cdf0e10cSrcweir 737*cdf0e10cSrcweir if (aPair.first == aPair.second) 738*cdf0e10cSrcweir throw NoSuchElementException(); 739*cdf0e10cSrcweir 740*cdf0e10cSrcweir return (*aPair.first).second->queryInterface( m_aElementType ); 741*cdf0e10cSrcweir } 742*cdf0e10cSrcweir 743*cdf0e10cSrcweir //------------------------------------------------------------------------------ 744*cdf0e10cSrcweir StringSequence SAL_CALL OInterfaceContainer::getElementNames() throw(RuntimeException) 745*cdf0e10cSrcweir { 746*cdf0e10cSrcweir StringSequence aNameList(m_aItems.size()); 747*cdf0e10cSrcweir ::rtl::OUString* pStringArray = aNameList.getArray(); 748*cdf0e10cSrcweir 749*cdf0e10cSrcweir for (OInterfaceMap::const_iterator i = m_aMap.begin(); i != m_aMap.end(); ++i, ++pStringArray) 750*cdf0e10cSrcweir { 751*cdf0e10cSrcweir *pStringArray = (*i).first; 752*cdf0e10cSrcweir } 753*cdf0e10cSrcweir return aNameList; 754*cdf0e10cSrcweir } 755*cdf0e10cSrcweir 756*cdf0e10cSrcweir //------------------------------------------------------------------------------ 757*cdf0e10cSrcweir sal_Bool SAL_CALL OInterfaceContainer::hasByName( const ::rtl::OUString& _rName ) throw(RuntimeException) 758*cdf0e10cSrcweir { 759*cdf0e10cSrcweir ::std::pair <OInterfaceMap::iterator, 760*cdf0e10cSrcweir OInterfaceMap::iterator> aPair = m_aMap.equal_range(_rName); 761*cdf0e10cSrcweir return aPair.first != aPair.second; 762*cdf0e10cSrcweir } 763*cdf0e10cSrcweir 764*cdf0e10cSrcweir // XIndexAccess 765*cdf0e10cSrcweir //------------------------------------------------------------------------------ 766*cdf0e10cSrcweir sal_Int32 OInterfaceContainer::getCount() throw( RuntimeException ) 767*cdf0e10cSrcweir { 768*cdf0e10cSrcweir return m_aItems.size(); 769*cdf0e10cSrcweir } 770*cdf0e10cSrcweir 771*cdf0e10cSrcweir //------------------------------------------------------------------------------ 772*cdf0e10cSrcweir Any OInterfaceContainer::getByIndex(sal_Int32 _nIndex) throw( IndexOutOfBoundsException, WrappedTargetException, RuntimeException ) 773*cdf0e10cSrcweir { 774*cdf0e10cSrcweir if (_nIndex < 0 || (_nIndex >= (sal_Int32)m_aItems.size())) 775*cdf0e10cSrcweir throw IndexOutOfBoundsException(); 776*cdf0e10cSrcweir 777*cdf0e10cSrcweir return m_aItems[_nIndex]->queryInterface( m_aElementType ); 778*cdf0e10cSrcweir } 779*cdf0e10cSrcweir 780*cdf0e10cSrcweir //------------------------------------------------------------------------------ 781*cdf0e10cSrcweir void OInterfaceContainer::approveNewElement( const Reference< XPropertySet >& _rxObject, ElementDescription* _pElement ) 782*cdf0e10cSrcweir { 783*cdf0e10cSrcweir // it has to be non-NULL 784*cdf0e10cSrcweir if ( !_rxObject.is() ) 785*cdf0e10cSrcweir throw IllegalArgumentException(FRM_RES_STRING(RID_STR_NEED_NON_NULL_OBJECT), static_cast<XContainer*>(this), 1); 786*cdf0e10cSrcweir 787*cdf0e10cSrcweir // it has to support our element type interface 788*cdf0e10cSrcweir Any aCorrectType = _rxObject->queryInterface( m_aElementType ); 789*cdf0e10cSrcweir if ( !aCorrectType.hasValue() ) 790*cdf0e10cSrcweir lcl_throwIllegalArgumentException(); 791*cdf0e10cSrcweir 792*cdf0e10cSrcweir // it has to have a "Name" property 793*cdf0e10cSrcweir if ( !hasProperty( PROPERTY_NAME, _rxObject ) ) 794*cdf0e10cSrcweir lcl_throwIllegalArgumentException(); 795*cdf0e10cSrcweir 796*cdf0e10cSrcweir // it has to be a child, and it must not have a parent already 797*cdf0e10cSrcweir Reference< XChild > xChild( _rxObject, UNO_QUERY ); 798*cdf0e10cSrcweir if ( !xChild.is() || xChild->getParent().is() ) 799*cdf0e10cSrcweir { 800*cdf0e10cSrcweir #ifdef FS_PRIV_DEBUG 801*cdf0e10cSrcweir ::rtl::OUString sChildName, sParentName; 802*cdf0e10cSrcweir Reference< XNamed > xNamed( xChild, UNO_QUERY ); 803*cdf0e10cSrcweir if ( xNamed.is() ) 804*cdf0e10cSrcweir sChildName = xNamed->getName(); 805*cdf0e10cSrcweir if ( xChild.is() ) 806*cdf0e10cSrcweir { 807*cdf0e10cSrcweir xNamed = xNamed.query( xChild->getParent() ); 808*cdf0e10cSrcweir if ( xNamed.is() ) 809*cdf0e10cSrcweir sParentName = xNamed->getName(); 810*cdf0e10cSrcweir } 811*cdf0e10cSrcweir #endif 812*cdf0e10cSrcweir lcl_throwIllegalArgumentException(); 813*cdf0e10cSrcweir } 814*cdf0e10cSrcweir 815*cdf0e10cSrcweir // passed all tests. cache the information we have so far 816*cdf0e10cSrcweir DBG_ASSERT( _pElement, "OInterfaceContainer::approveNewElement: invalid event descriptor!" ); 817*cdf0e10cSrcweir if ( _pElement ) 818*cdf0e10cSrcweir { 819*cdf0e10cSrcweir _pElement->xPropertySet = _rxObject; 820*cdf0e10cSrcweir _pElement->xChild = xChild; 821*cdf0e10cSrcweir _pElement->aElementTypeInterface = aCorrectType; 822*cdf0e10cSrcweir _pElement->xInterface = Reference< XInterface >( _rxObject, UNO_QUERY ); // normalized XInterface 823*cdf0e10cSrcweir } 824*cdf0e10cSrcweir } 825*cdf0e10cSrcweir 826*cdf0e10cSrcweir //------------------------------------------------------------------------------ 827*cdf0e10cSrcweir void OInterfaceContainer::implInsert(sal_Int32 _nIndex, const Reference< XPropertySet >& _rxElement, 828*cdf0e10cSrcweir sal_Bool _bEvents, ElementDescription* _pApprovalResult, sal_Bool _bFire ) throw( IllegalArgumentException ) 829*cdf0e10cSrcweir { 830*cdf0e10cSrcweir const bool bHandleEvents = _bEvents && m_xEventAttacher.is(); 831*cdf0e10cSrcweir 832*cdf0e10cSrcweir // SYNCHRONIZED -----> 833*cdf0e10cSrcweir ::osl::ClearableMutexGuard aGuard( m_rMutex ); 834*cdf0e10cSrcweir 835*cdf0e10cSrcweir ::std::auto_ptr< ElementDescription > aAutoDeleteMetaData; 836*cdf0e10cSrcweir ElementDescription* pElementMetaData = _pApprovalResult; 837*cdf0e10cSrcweir if ( !pElementMetaData ) 838*cdf0e10cSrcweir { // not yet approved by the caller -> do ourself 839*cdf0e10cSrcweir pElementMetaData = createElementMetaData(); 840*cdf0e10cSrcweir DBG_ASSERT( pElementMetaData, "OInterfaceContainer::implInsert: createElementMetaData returned nonsense!" ); 841*cdf0e10cSrcweir 842*cdf0e10cSrcweir // ensure that the meta data structure will be deleted later on 843*cdf0e10cSrcweir aAutoDeleteMetaData = ::std::auto_ptr< ElementDescription >( pElementMetaData ); 844*cdf0e10cSrcweir 845*cdf0e10cSrcweir // will throw an exception if necessary 846*cdf0e10cSrcweir approveNewElement( _rxElement, pElementMetaData ); 847*cdf0e10cSrcweir } 848*cdf0e10cSrcweir 849*cdf0e10cSrcweir 850*cdf0e10cSrcweir // approveNewElement (no matter if called here or outside) has ensure that all relevant interfaces 851*cdf0e10cSrcweir // exist 852*cdf0e10cSrcweir 853*cdf0e10cSrcweir // set the name, and add as change listener for the name 854*cdf0e10cSrcweir ::rtl::OUString sName; 855*cdf0e10cSrcweir _rxElement->getPropertyValue(PROPERTY_NAME) >>= sName; 856*cdf0e10cSrcweir _rxElement->addPropertyChangeListener(PROPERTY_NAME, this); 857*cdf0e10cSrcweir 858*cdf0e10cSrcweir // insert the object into our internal structures 859*cdf0e10cSrcweir if (_nIndex > (sal_Int32)m_aItems.size()) // ermitteln des tatsaechlichen Indexs 860*cdf0e10cSrcweir { 861*cdf0e10cSrcweir _nIndex = m_aItems.size(); 862*cdf0e10cSrcweir m_aItems.push_back( pElementMetaData->xInterface ); 863*cdf0e10cSrcweir } 864*cdf0e10cSrcweir else 865*cdf0e10cSrcweir m_aItems.insert( m_aItems.begin() + _nIndex, pElementMetaData->xInterface ); 866*cdf0e10cSrcweir 867*cdf0e10cSrcweir m_aMap.insert( ::std::pair< const ::rtl::OUString, InterfaceRef >( sName, pElementMetaData->xInterface ) ); 868*cdf0e10cSrcweir 869*cdf0e10cSrcweir // announce ourself as parent to the new element 870*cdf0e10cSrcweir pElementMetaData->xChild->setParent(static_cast<XContainer*>(this)); 871*cdf0e10cSrcweir 872*cdf0e10cSrcweir // handle the events 873*cdf0e10cSrcweir if ( bHandleEvents ) 874*cdf0e10cSrcweir { 875*cdf0e10cSrcweir m_xEventAttacher->insertEntry(_nIndex); 876*cdf0e10cSrcweir m_xEventAttacher->attach( _nIndex, pElementMetaData->xInterface, makeAny( _rxElement ) ); 877*cdf0e10cSrcweir } 878*cdf0e10cSrcweir 879*cdf0e10cSrcweir // notify derived classes 880*cdf0e10cSrcweir implInserted( pElementMetaData ); 881*cdf0e10cSrcweir 882*cdf0e10cSrcweir aGuard.clear(); 883*cdf0e10cSrcweir // <----- SYNCHRONIZED 884*cdf0e10cSrcweir 885*cdf0e10cSrcweir // insert faked VBA events? 886*cdf0e10cSrcweir if ( bHandleEvents ) 887*cdf0e10cSrcweir { 888*cdf0e10cSrcweir Reference< XEventAttacherManager > xMgr ( pElementMetaData->xInterface, UNO_QUERY ); 889*cdf0e10cSrcweir if ( xMgr.is() ) 890*cdf0e10cSrcweir { 891*cdf0e10cSrcweir OInterfaceContainer* pIfcMgr = dynamic_cast< OInterfaceContainer* >( xMgr.get() ); 892*cdf0e10cSrcweir sal_Int32 nLen = pIfcMgr->getCount(); 893*cdf0e10cSrcweir for ( sal_Int32 i = 0; (i < nLen) && pIfcMgr ; ++i ) 894*cdf0e10cSrcweir { 895*cdf0e10cSrcweir // add fake events to the control at index i 896*cdf0e10cSrcweir pIfcMgr->impl_addVbEvents_nolck_nothrow( i ); 897*cdf0e10cSrcweir } 898*cdf0e10cSrcweir } 899*cdf0e10cSrcweir else 900*cdf0e10cSrcweir { 901*cdf0e10cSrcweir // add fake events to the control at index i 902*cdf0e10cSrcweir impl_addVbEvents_nolck_nothrow( _nIndex ); 903*cdf0e10cSrcweir } 904*cdf0e10cSrcweir } 905*cdf0e10cSrcweir 906*cdf0e10cSrcweir // fire the notification about the change 907*cdf0e10cSrcweir if ( _bFire ) 908*cdf0e10cSrcweir { 909*cdf0e10cSrcweir // notify listeners 910*cdf0e10cSrcweir ContainerEvent aEvt; 911*cdf0e10cSrcweir aEvt.Source = static_cast<XContainer*>(this); 912*cdf0e10cSrcweir aEvt.Accessor <<= _nIndex; 913*cdf0e10cSrcweir aEvt.Element = pElementMetaData->aElementTypeInterface; 914*cdf0e10cSrcweir 915*cdf0e10cSrcweir aGuard.clear(); 916*cdf0e10cSrcweir m_aContainerListeners.notifyEach( &XContainerListener::elementInserted, aEvt ); 917*cdf0e10cSrcweir } 918*cdf0e10cSrcweir } 919*cdf0e10cSrcweir 920*cdf0e10cSrcweir //------------------------------------------------------------------------------ 921*cdf0e10cSrcweir void OInterfaceContainer::removeElementsNoEvents(sal_Int32 nIndex) 922*cdf0e10cSrcweir { 923*cdf0e10cSrcweir OInterfaceArray::iterator i = m_aItems.begin() + nIndex; 924*cdf0e10cSrcweir InterfaceRef xElement(*i); 925*cdf0e10cSrcweir 926*cdf0e10cSrcweir OInterfaceMap::iterator j = m_aMap.begin(); 927*cdf0e10cSrcweir while (j != m_aMap.end() && (*j).second != xElement) ++j; 928*cdf0e10cSrcweir 929*cdf0e10cSrcweir m_aItems.erase(i); 930*cdf0e10cSrcweir m_aMap.erase(j); 931*cdf0e10cSrcweir 932*cdf0e10cSrcweir Reference<XPropertySet> xSet(xElement, UNO_QUERY); 933*cdf0e10cSrcweir if (xSet.is()) 934*cdf0e10cSrcweir xSet->removePropertyChangeListener(PROPERTY_NAME, this); 935*cdf0e10cSrcweir 936*cdf0e10cSrcweir Reference<XChild> xChild(xElement, UNO_QUERY); 937*cdf0e10cSrcweir if (xChild.is()) 938*cdf0e10cSrcweir xChild->setParent(InterfaceRef ()); 939*cdf0e10cSrcweir } 940*cdf0e10cSrcweir 941*cdf0e10cSrcweir //------------------------------------------------------------------------------ 942*cdf0e10cSrcweir void OInterfaceContainer::implInserted( const ElementDescription* /*_pElement*/ ) 943*cdf0e10cSrcweir { 944*cdf0e10cSrcweir // not inrerested in 945*cdf0e10cSrcweir } 946*cdf0e10cSrcweir 947*cdf0e10cSrcweir //------------------------------------------------------------------------------ 948*cdf0e10cSrcweir void OInterfaceContainer::implRemoved( const InterfaceRef& /*_rxObject*/ ) 949*cdf0e10cSrcweir { 950*cdf0e10cSrcweir // not inrerested in 951*cdf0e10cSrcweir } 952*cdf0e10cSrcweir 953*cdf0e10cSrcweir //------------------------------------------------------------------------------ 954*cdf0e10cSrcweir void OInterfaceContainer::impl_replacedElement( const ContainerEvent& _rEvent, ::osl::ClearableMutexGuard& _rInstanceLock ) 955*cdf0e10cSrcweir { 956*cdf0e10cSrcweir _rInstanceLock.clear(); 957*cdf0e10cSrcweir m_aContainerListeners.notifyEach( &XContainerListener::elementReplaced, _rEvent ); 958*cdf0e10cSrcweir } 959*cdf0e10cSrcweir 960*cdf0e10cSrcweir // XIndexContainer 961*cdf0e10cSrcweir //------------------------------------------------------------------------------ 962*cdf0e10cSrcweir void SAL_CALL OInterfaceContainer::insertByIndex( sal_Int32 _nIndex, const Any& _rElement ) throw(IllegalArgumentException, IndexOutOfBoundsException, WrappedTargetException, RuntimeException) 963*cdf0e10cSrcweir { 964*cdf0e10cSrcweir Reference< XPropertySet > xElement; 965*cdf0e10cSrcweir _rElement >>= xElement; 966*cdf0e10cSrcweir implInsert( _nIndex, xElement, sal_True /* event handling */ , NULL /* not yet approved */ , sal_True /* notification */ ); 967*cdf0e10cSrcweir } 968*cdf0e10cSrcweir 969*cdf0e10cSrcweir //------------------------------------------------------------------------------ 970*cdf0e10cSrcweir void OInterfaceContainer::implReplaceByIndex( const sal_Int32 _nIndex, const Any& _rNewElement, ::osl::ClearableMutexGuard& _rClearBeforeNotify ) 971*cdf0e10cSrcweir { 972*cdf0e10cSrcweir OSL_PRECOND( ( _nIndex >= 0 ) && ( _nIndex < (sal_Int32)m_aItems.size() ), "OInterfaceContainer::implReplaceByIndex: precondition not met (index)!" ); 973*cdf0e10cSrcweir 974*cdf0e10cSrcweir // approve the new object 975*cdf0e10cSrcweir ::std::auto_ptr< ElementDescription > aElementMetaData( createElementMetaData() ); 976*cdf0e10cSrcweir DBG_ASSERT( aElementMetaData.get(), "OInterfaceContainer::implReplaceByIndex: createElementMetaData returned nonsense!" ); 977*cdf0e10cSrcweir { 978*cdf0e10cSrcweir Reference< XPropertySet > xElementProps; 979*cdf0e10cSrcweir _rNewElement >>= xElementProps; 980*cdf0e10cSrcweir approveNewElement( xElementProps, aElementMetaData.get() ); 981*cdf0e10cSrcweir } 982*cdf0e10cSrcweir 983*cdf0e10cSrcweir // get the old element 984*cdf0e10cSrcweir InterfaceRef xOldElement( m_aItems[ _nIndex ] ); 985*cdf0e10cSrcweir DBG_ASSERT( xOldElement.get() == Reference< XInterface >( xOldElement, UNO_QUERY ).get(), 986*cdf0e10cSrcweir "OInterfaceContainer::implReplaceByIndex: elements should be held normalized!" ); 987*cdf0e10cSrcweir 988*cdf0e10cSrcweir // locate the old element in the map 989*cdf0e10cSrcweir OInterfaceMap::iterator j = m_aMap.begin(); 990*cdf0e10cSrcweir while ( ( j != m_aMap.end() ) && ( j->second.get() != xOldElement.get() ) ) 991*cdf0e10cSrcweir ++j; 992*cdf0e10cSrcweir 993*cdf0e10cSrcweir // remove event knittings 994*cdf0e10cSrcweir if ( m_xEventAttacher.is() ) 995*cdf0e10cSrcweir { 996*cdf0e10cSrcweir InterfaceRef xNormalized( xOldElement, UNO_QUERY ); 997*cdf0e10cSrcweir m_xEventAttacher->detach( _nIndex, xNormalized ); 998*cdf0e10cSrcweir m_xEventAttacher->removeEntry( _nIndex ); 999*cdf0e10cSrcweir } 1000*cdf0e10cSrcweir 1001*cdf0e10cSrcweir // don't listen for property changes anymore 1002*cdf0e10cSrcweir Reference<XPropertySet> xSet( xOldElement, UNO_QUERY ); 1003*cdf0e10cSrcweir if (xSet.is()) 1004*cdf0e10cSrcweir xSet->removePropertyChangeListener(PROPERTY_NAME, this); 1005*cdf0e10cSrcweir 1006*cdf0e10cSrcweir // give the old element a new (void) parent 1007*cdf0e10cSrcweir Reference<XChild> xChild(xOldElement, UNO_QUERY); 1008*cdf0e10cSrcweir if (xChild.is()) 1009*cdf0e10cSrcweir xChild->setParent(InterfaceRef ()); 1010*cdf0e10cSrcweir 1011*cdf0e10cSrcweir // remove the old one 1012*cdf0e10cSrcweir m_aMap.erase(j); 1013*cdf0e10cSrcweir 1014*cdf0e10cSrcweir // examine the new element 1015*cdf0e10cSrcweir ::rtl::OUString sName; 1016*cdf0e10cSrcweir DBG_ASSERT( aElementMetaData.get()->xPropertySet.is(), "OInterfaceContainer::implReplaceByIndex: what did approveNewElement do?" ); 1017*cdf0e10cSrcweir 1018*cdf0e10cSrcweir aElementMetaData.get()->xPropertySet->getPropertyValue(PROPERTY_NAME) >>= sName; 1019*cdf0e10cSrcweir aElementMetaData.get()->xPropertySet->addPropertyChangeListener(PROPERTY_NAME, this); 1020*cdf0e10cSrcweir 1021*cdf0e10cSrcweir // insert the new one 1022*cdf0e10cSrcweir m_aMap.insert( ::std::pair<const ::rtl::OUString, InterfaceRef >( sName, aElementMetaData.get()->xInterface ) ); 1023*cdf0e10cSrcweir m_aItems[ _nIndex ] = aElementMetaData.get()->xInterface; 1024*cdf0e10cSrcweir 1025*cdf0e10cSrcweir aElementMetaData.get()->xChild->setParent(static_cast<XContainer*>(this)); 1026*cdf0e10cSrcweir 1027*cdf0e10cSrcweir if ( m_xEventAttacher.is() ) 1028*cdf0e10cSrcweir { 1029*cdf0e10cSrcweir m_xEventAttacher->insertEntry( _nIndex ); 1030*cdf0e10cSrcweir m_xEventAttacher->attach( _nIndex, aElementMetaData.get()->xInterface, makeAny( aElementMetaData.get()->xPropertySet ) ); 1031*cdf0e10cSrcweir } 1032*cdf0e10cSrcweir 1033*cdf0e10cSrcweir ContainerEvent aReplaceEvent; 1034*cdf0e10cSrcweir aReplaceEvent.Source = static_cast< XContainer* >( this ); 1035*cdf0e10cSrcweir aReplaceEvent.Accessor <<= _nIndex; 1036*cdf0e10cSrcweir aReplaceEvent.Element = aElementMetaData.get()->xInterface->queryInterface( m_aElementType ); 1037*cdf0e10cSrcweir aReplaceEvent.ReplacedElement = xOldElement->queryInterface( m_aElementType ); 1038*cdf0e10cSrcweir 1039*cdf0e10cSrcweir impl_replacedElement( aReplaceEvent, _rClearBeforeNotify ); 1040*cdf0e10cSrcweir } 1041*cdf0e10cSrcweir 1042*cdf0e10cSrcweir //------------------------------------------------------------------------------ 1043*cdf0e10cSrcweir void OInterfaceContainer::implCheckIndex( const sal_Int32 _nIndex ) SAL_THROW( ( ::com::sun::star::lang::IndexOutOfBoundsException ) ) 1044*cdf0e10cSrcweir { 1045*cdf0e10cSrcweir if (_nIndex < 0 || _nIndex >= (sal_Int32)m_aItems.size()) 1046*cdf0e10cSrcweir throw IndexOutOfBoundsException(); 1047*cdf0e10cSrcweir } 1048*cdf0e10cSrcweir 1049*cdf0e10cSrcweir //------------------------------------------------------------------------------ 1050*cdf0e10cSrcweir void SAL_CALL OInterfaceContainer::replaceByIndex(sal_Int32 _nIndex, const Any& Element) throw( IllegalArgumentException, IndexOutOfBoundsException, WrappedTargetException, RuntimeException ) 1051*cdf0e10cSrcweir { 1052*cdf0e10cSrcweir ::osl::ClearableMutexGuard aGuard( m_rMutex ); 1053*cdf0e10cSrcweir // check the index 1054*cdf0e10cSrcweir implCheckIndex( _nIndex ); 1055*cdf0e10cSrcweir // do the replace 1056*cdf0e10cSrcweir implReplaceByIndex( _nIndex, Element, aGuard ); 1057*cdf0e10cSrcweir } 1058*cdf0e10cSrcweir 1059*cdf0e10cSrcweir //------------------------------------------------------------------------------ 1060*cdf0e10cSrcweir void OInterfaceContainer::implRemoveByIndex( const sal_Int32 _nIndex, ::osl::ClearableMutexGuard& _rClearBeforeNotify ) 1061*cdf0e10cSrcweir { 1062*cdf0e10cSrcweir OSL_PRECOND( ( _nIndex >= 0 ) && ( _nIndex < (sal_Int32)m_aItems.size() ), "OInterfaceContainer::implRemoveByIndex: precondition not met (index)!" ); 1063*cdf0e10cSrcweir 1064*cdf0e10cSrcweir OInterfaceArray::iterator i = m_aItems.begin() + _nIndex; 1065*cdf0e10cSrcweir InterfaceRef xElement(*i); 1066*cdf0e10cSrcweir 1067*cdf0e10cSrcweir OInterfaceMap::iterator j = m_aMap.begin(); 1068*cdf0e10cSrcweir while (j != m_aMap.end() && (*j).second != xElement) ++j; 1069*cdf0e10cSrcweir 1070*cdf0e10cSrcweir m_aItems.erase(i); 1071*cdf0e10cSrcweir m_aMap.erase(j); 1072*cdf0e10cSrcweir 1073*cdf0e10cSrcweir // remove event knittings 1074*cdf0e10cSrcweir if ( m_xEventAttacher.is() ) 1075*cdf0e10cSrcweir { 1076*cdf0e10cSrcweir InterfaceRef xNormalized( xElement, UNO_QUERY ); 1077*cdf0e10cSrcweir m_xEventAttacher->detach( _nIndex, xNormalized ); 1078*cdf0e10cSrcweir m_xEventAttacher->removeEntry( _nIndex ); 1079*cdf0e10cSrcweir } 1080*cdf0e10cSrcweir 1081*cdf0e10cSrcweir Reference<XPropertySet> xSet(xElement, UNO_QUERY); 1082*cdf0e10cSrcweir if (xSet.is()) 1083*cdf0e10cSrcweir xSet->removePropertyChangeListener(PROPERTY_NAME, this); 1084*cdf0e10cSrcweir 1085*cdf0e10cSrcweir Reference<XChild> xChild(xElement, UNO_QUERY); 1086*cdf0e10cSrcweir if (xChild.is()) 1087*cdf0e10cSrcweir xChild->setParent(InterfaceRef ()); 1088*cdf0e10cSrcweir 1089*cdf0e10cSrcweir // notify derived classes 1090*cdf0e10cSrcweir implRemoved(xElement); 1091*cdf0e10cSrcweir 1092*cdf0e10cSrcweir // notify listeners 1093*cdf0e10cSrcweir ContainerEvent aEvt; 1094*cdf0e10cSrcweir aEvt.Source = static_cast<XContainer*>(this); 1095*cdf0e10cSrcweir aEvt.Element = xElement->queryInterface( m_aElementType ); 1096*cdf0e10cSrcweir aEvt.Accessor <<= _nIndex; 1097*cdf0e10cSrcweir 1098*cdf0e10cSrcweir _rClearBeforeNotify.clear(); 1099*cdf0e10cSrcweir m_aContainerListeners.notifyEach( &XContainerListener::elementRemoved, aEvt ); 1100*cdf0e10cSrcweir } 1101*cdf0e10cSrcweir 1102*cdf0e10cSrcweir //------------------------------------------------------------------------------ 1103*cdf0e10cSrcweir void SAL_CALL OInterfaceContainer::removeByIndex(sal_Int32 _nIndex) throw( IndexOutOfBoundsException, WrappedTargetException, RuntimeException ) 1104*cdf0e10cSrcweir { 1105*cdf0e10cSrcweir ::osl::ClearableMutexGuard aGuard( m_rMutex ); 1106*cdf0e10cSrcweir // check the index 1107*cdf0e10cSrcweir implCheckIndex( _nIndex ); 1108*cdf0e10cSrcweir // do the removal 1109*cdf0e10cSrcweir implRemoveByIndex( _nIndex, aGuard ); 1110*cdf0e10cSrcweir } 1111*cdf0e10cSrcweir 1112*cdf0e10cSrcweir //------------------------------------------------------------------------ 1113*cdf0e10cSrcweir ElementDescription* OInterfaceContainer::createElementMetaData( ) 1114*cdf0e10cSrcweir { 1115*cdf0e10cSrcweir return new ElementDescription; 1116*cdf0e10cSrcweir } 1117*cdf0e10cSrcweir 1118*cdf0e10cSrcweir //------------------------------------------------------------------------ 1119*cdf0e10cSrcweir void SAL_CALL OInterfaceContainer::insertByName(const ::rtl::OUString& _rName, const Any& _rElement) throw( IllegalArgumentException, ElementExistException, WrappedTargetException, RuntimeException ) 1120*cdf0e10cSrcweir { 1121*cdf0e10cSrcweir Reference< XPropertySet > xElementProps; 1122*cdf0e10cSrcweir 1123*cdf0e10cSrcweir ::std::auto_ptr< ElementDescription > aElementMetaData( createElementMetaData() ); 1124*cdf0e10cSrcweir DBG_ASSERT( aElementMetaData.get(), "OInterfaceContainer::insertByName: createElementMetaData returned nonsense!" ); 1125*cdf0e10cSrcweir 1126*cdf0e10cSrcweir // ensure the correct name of the element 1127*cdf0e10cSrcweir try 1128*cdf0e10cSrcweir { 1129*cdf0e10cSrcweir _rElement >>= xElementProps; 1130*cdf0e10cSrcweir approveNewElement( xElementProps, aElementMetaData.get() ); 1131*cdf0e10cSrcweir 1132*cdf0e10cSrcweir xElementProps->setPropertyValue( PROPERTY_NAME, makeAny( _rName ) ); 1133*cdf0e10cSrcweir } 1134*cdf0e10cSrcweir catch( const IllegalArgumentException& ) 1135*cdf0e10cSrcweir { 1136*cdf0e10cSrcweir throw; // allowed to leave 1137*cdf0e10cSrcweir } 1138*cdf0e10cSrcweir catch( const ElementExistException& ) 1139*cdf0e10cSrcweir { 1140*cdf0e10cSrcweir throw; // allowed to leave 1141*cdf0e10cSrcweir } 1142*cdf0e10cSrcweir catch( const Exception& ) 1143*cdf0e10cSrcweir { 1144*cdf0e10cSrcweir DBG_ERROR( "OInterfaceContainer::insertByName: caught an exception!" ); 1145*cdf0e10cSrcweir } 1146*cdf0e10cSrcweir implInsert( m_aItems.size(), xElementProps, sal_True, aElementMetaData.get(), sal_True ); 1147*cdf0e10cSrcweir } 1148*cdf0e10cSrcweir 1149*cdf0e10cSrcweir //------------------------------------------------------------------------ 1150*cdf0e10cSrcweir void SAL_CALL OInterfaceContainer::replaceByName(const ::rtl::OUString& Name, const Any& Element) throw( IllegalArgumentException, NoSuchElementException, WrappedTargetException, RuntimeException ) 1151*cdf0e10cSrcweir { 1152*cdf0e10cSrcweir ::osl::ClearableMutexGuard aGuard( m_rMutex ); 1153*cdf0e10cSrcweir ::std::pair <OInterfaceMap::iterator, 1154*cdf0e10cSrcweir OInterfaceMap::iterator> aPair = m_aMap.equal_range(Name); 1155*cdf0e10cSrcweir if (aPair.first == aPair.second) 1156*cdf0e10cSrcweir throw NoSuchElementException(); 1157*cdf0e10cSrcweir 1158*cdf0e10cSrcweir if (Element.getValueType().getTypeClass() != TypeClass_INTERFACE) 1159*cdf0e10cSrcweir lcl_throwIllegalArgumentException(); 1160*cdf0e10cSrcweir 1161*cdf0e10cSrcweir Reference<XPropertySet> xSet; 1162*cdf0e10cSrcweir Element >>= xSet; 1163*cdf0e10cSrcweir if (xSet.is()) 1164*cdf0e10cSrcweir { 1165*cdf0e10cSrcweir if (!hasProperty(PROPERTY_NAME, xSet)) 1166*cdf0e10cSrcweir lcl_throwIllegalArgumentException(); 1167*cdf0e10cSrcweir 1168*cdf0e10cSrcweir xSet->setPropertyValue(PROPERTY_NAME, makeAny(Name)); 1169*cdf0e10cSrcweir } 1170*cdf0e10cSrcweir 1171*cdf0e10cSrcweir // determine the element pos 1172*cdf0e10cSrcweir sal_Int32 nPos = ::std::find(m_aItems.begin(), m_aItems.end(), (*aPair.first).second) - m_aItems.begin(); 1173*cdf0e10cSrcweir 1174*cdf0e10cSrcweir implReplaceByIndex( nPos, Element, aGuard ); 1175*cdf0e10cSrcweir } 1176*cdf0e10cSrcweir 1177*cdf0e10cSrcweir //------------------------------------------------------------------------ 1178*cdf0e10cSrcweir void SAL_CALL OInterfaceContainer::removeByName(const ::rtl::OUString& Name) throw( NoSuchElementException, WrappedTargetException, RuntimeException ) 1179*cdf0e10cSrcweir { 1180*cdf0e10cSrcweir ::osl::MutexGuard aGuard( m_rMutex ); 1181*cdf0e10cSrcweir ::std::pair <OInterfaceMap::iterator, 1182*cdf0e10cSrcweir OInterfaceMap::iterator> aPair = m_aMap.equal_range(Name); 1183*cdf0e10cSrcweir if (aPair.first == aPair.second) 1184*cdf0e10cSrcweir throw NoSuchElementException(); 1185*cdf0e10cSrcweir 1186*cdf0e10cSrcweir sal_Int32 nPos = ::std::find(m_aItems.begin(), m_aItems.end(), (*aPair.first).second) - m_aItems.begin(); 1187*cdf0e10cSrcweir removeByIndex(nPos); 1188*cdf0e10cSrcweir } 1189*cdf0e10cSrcweir 1190*cdf0e10cSrcweir 1191*cdf0e10cSrcweir // XEventAttacherManager 1192*cdf0e10cSrcweir //------------------------------------------------------------------------ 1193*cdf0e10cSrcweir void SAL_CALL OInterfaceContainer::registerScriptEvent( sal_Int32 nIndex, const ScriptEventDescriptor& aScriptEvent ) throw(IllegalArgumentException, RuntimeException) 1194*cdf0e10cSrcweir { 1195*cdf0e10cSrcweir ::osl::ClearableMutexGuard aGuard( m_rMutex ); 1196*cdf0e10cSrcweir if ( m_xEventAttacher.is() ) 1197*cdf0e10cSrcweir { 1198*cdf0e10cSrcweir m_xEventAttacher->registerScriptEvent( nIndex, aScriptEvent ); 1199*cdf0e10cSrcweir aGuard.clear(); 1200*cdf0e10cSrcweir impl_addVbEvents_nolck_nothrow( nIndex ); // add fake vba events 1201*cdf0e10cSrcweir } 1202*cdf0e10cSrcweir } 1203*cdf0e10cSrcweir 1204*cdf0e10cSrcweir //------------------------------------------------------------------------ 1205*cdf0e10cSrcweir void SAL_CALL OInterfaceContainer::registerScriptEvents( sal_Int32 nIndex, const Sequence< ScriptEventDescriptor >& aScriptEvents ) throw(IllegalArgumentException, RuntimeException) 1206*cdf0e10cSrcweir { 1207*cdf0e10cSrcweir ::osl::ClearableMutexGuard aGuard( m_rMutex ); 1208*cdf0e10cSrcweir if ( m_xEventAttacher.is() ) 1209*cdf0e10cSrcweir { 1210*cdf0e10cSrcweir m_xEventAttacher->registerScriptEvents( nIndex, aScriptEvents ); 1211*cdf0e10cSrcweir aGuard.clear(); 1212*cdf0e10cSrcweir impl_addVbEvents_nolck_nothrow( nIndex ); // add fake vba events 1213*cdf0e10cSrcweir } 1214*cdf0e10cSrcweir } 1215*cdf0e10cSrcweir 1216*cdf0e10cSrcweir //------------------------------------------------------------------------ 1217*cdf0e10cSrcweir void SAL_CALL OInterfaceContainer::revokeScriptEvent( sal_Int32 nIndex, const ::rtl::OUString& aListenerType, const ::rtl::OUString& aEventMethod, const ::rtl::OUString& aRemoveListenerParam ) throw(IllegalArgumentException, RuntimeException) 1218*cdf0e10cSrcweir { 1219*cdf0e10cSrcweir if ( m_xEventAttacher.is() ) 1220*cdf0e10cSrcweir m_xEventAttacher->revokeScriptEvent( nIndex, aListenerType, aEventMethod, aRemoveListenerParam ); 1221*cdf0e10cSrcweir } 1222*cdf0e10cSrcweir 1223*cdf0e10cSrcweir //------------------------------------------------------------------------ 1224*cdf0e10cSrcweir void SAL_CALL OInterfaceContainer::revokeScriptEvents( sal_Int32 nIndex ) throw(IllegalArgumentException, RuntimeException) 1225*cdf0e10cSrcweir { 1226*cdf0e10cSrcweir if ( m_xEventAttacher.is() ) 1227*cdf0e10cSrcweir m_xEventAttacher->revokeScriptEvents( nIndex ); 1228*cdf0e10cSrcweir } 1229*cdf0e10cSrcweir 1230*cdf0e10cSrcweir //------------------------------------------------------------------------ 1231*cdf0e10cSrcweir void SAL_CALL OInterfaceContainer::insertEntry( sal_Int32 nIndex ) throw(IllegalArgumentException, RuntimeException) 1232*cdf0e10cSrcweir { 1233*cdf0e10cSrcweir if ( m_xEventAttacher.is() ) 1234*cdf0e10cSrcweir m_xEventAttacher->insertEntry( nIndex ); 1235*cdf0e10cSrcweir } 1236*cdf0e10cSrcweir 1237*cdf0e10cSrcweir //------------------------------------------------------------------------ 1238*cdf0e10cSrcweir void SAL_CALL OInterfaceContainer::removeEntry( sal_Int32 nIndex ) throw(IllegalArgumentException, RuntimeException) 1239*cdf0e10cSrcweir { 1240*cdf0e10cSrcweir if ( m_xEventAttacher.is() ) 1241*cdf0e10cSrcweir m_xEventAttacher->removeEntry( nIndex ); 1242*cdf0e10cSrcweir } 1243*cdf0e10cSrcweir 1244*cdf0e10cSrcweir //------------------------------------------------------------------------ 1245*cdf0e10cSrcweir Sequence< ScriptEventDescriptor > SAL_CALL OInterfaceContainer::getScriptEvents( sal_Int32 nIndex ) throw(IllegalArgumentException, RuntimeException) 1246*cdf0e10cSrcweir { 1247*cdf0e10cSrcweir Sequence< ScriptEventDescriptor > aReturn; 1248*cdf0e10cSrcweir if ( m_xEventAttacher.is() ) 1249*cdf0e10cSrcweir { 1250*cdf0e10cSrcweir aReturn = m_xEventAttacher->getScriptEvents( nIndex ); 1251*cdf0e10cSrcweir if ( lcl_hasVbaEvents( aReturn ) ) 1252*cdf0e10cSrcweir { 1253*cdf0e10cSrcweir aReturn = lcl_stripVbaEvents( aReturn ); 1254*cdf0e10cSrcweir } 1255*cdf0e10cSrcweir } 1256*cdf0e10cSrcweir return aReturn; 1257*cdf0e10cSrcweir } 1258*cdf0e10cSrcweir 1259*cdf0e10cSrcweir //------------------------------------------------------------------------ 1260*cdf0e10cSrcweir void SAL_CALL OInterfaceContainer::attach( sal_Int32 nIndex, const Reference< XInterface >& xObject, const Any& aHelper ) throw(IllegalArgumentException, ServiceNotRegisteredException, RuntimeException) 1261*cdf0e10cSrcweir { 1262*cdf0e10cSrcweir if ( m_xEventAttacher.is() ) 1263*cdf0e10cSrcweir m_xEventAttacher->attach( nIndex, xObject, aHelper ); 1264*cdf0e10cSrcweir } 1265*cdf0e10cSrcweir 1266*cdf0e10cSrcweir //------------------------------------------------------------------------ 1267*cdf0e10cSrcweir void SAL_CALL OInterfaceContainer::detach( sal_Int32 nIndex, const Reference< XInterface >& xObject ) throw(IllegalArgumentException, RuntimeException) 1268*cdf0e10cSrcweir { 1269*cdf0e10cSrcweir if ( m_xEventAttacher.is() ) 1270*cdf0e10cSrcweir m_xEventAttacher->detach( nIndex, xObject ); 1271*cdf0e10cSrcweir } 1272*cdf0e10cSrcweir 1273*cdf0e10cSrcweir //------------------------------------------------------------------------ 1274*cdf0e10cSrcweir void SAL_CALL OInterfaceContainer::addScriptListener( const Reference< XScriptListener >& xListener ) throw(IllegalArgumentException, RuntimeException) 1275*cdf0e10cSrcweir { 1276*cdf0e10cSrcweir if ( m_xEventAttacher.is() ) 1277*cdf0e10cSrcweir m_xEventAttacher->addScriptListener( xListener ); 1278*cdf0e10cSrcweir } 1279*cdf0e10cSrcweir 1280*cdf0e10cSrcweir //------------------------------------------------------------------------ 1281*cdf0e10cSrcweir void SAL_CALL OInterfaceContainer::removeScriptListener( const Reference< XScriptListener >& xListener ) throw(IllegalArgumentException, RuntimeException) 1282*cdf0e10cSrcweir { 1283*cdf0e10cSrcweir if ( m_xEventAttacher.is() ) 1284*cdf0e10cSrcweir m_xEventAttacher->removeScriptListener( xListener ); 1285*cdf0e10cSrcweir } 1286*cdf0e10cSrcweir 1287*cdf0e10cSrcweir //================================================================== 1288*cdf0e10cSrcweir //= OFormComponents 1289*cdf0e10cSrcweir //================================================================== 1290*cdf0e10cSrcweir //------------------------------------------------------------------------------ 1291*cdf0e10cSrcweir Any SAL_CALL OFormComponents::queryAggregation(const Type& _rType) throw(RuntimeException) 1292*cdf0e10cSrcweir { 1293*cdf0e10cSrcweir Any aReturn = OFormComponents_BASE::queryInterface(_rType); 1294*cdf0e10cSrcweir if (!aReturn.hasValue()) 1295*cdf0e10cSrcweir { 1296*cdf0e10cSrcweir aReturn = OInterfaceContainer::queryInterface(_rType); 1297*cdf0e10cSrcweir 1298*cdf0e10cSrcweir if (!aReturn.hasValue()) 1299*cdf0e10cSrcweir aReturn = FormComponentsBase::queryAggregation(_rType); 1300*cdf0e10cSrcweir } 1301*cdf0e10cSrcweir 1302*cdf0e10cSrcweir return aReturn; 1303*cdf0e10cSrcweir } 1304*cdf0e10cSrcweir 1305*cdf0e10cSrcweir //------------------------------------------------------------------ 1306*cdf0e10cSrcweir Sequence<Type> SAL_CALL OFormComponents::getTypes() throw(RuntimeException) 1307*cdf0e10cSrcweir { 1308*cdf0e10cSrcweir return ::comphelper::concatSequences(OInterfaceContainer::getTypes(), FormComponentsBase::getTypes(), OFormComponents_BASE::getTypes()); 1309*cdf0e10cSrcweir } 1310*cdf0e10cSrcweir 1311*cdf0e10cSrcweir //------------------------------------------------------------------------------ 1312*cdf0e10cSrcweir OFormComponents::OFormComponents(const Reference<XMultiServiceFactory>& _rxFactory) 1313*cdf0e10cSrcweir :FormComponentsBase( m_aMutex ) 1314*cdf0e10cSrcweir ,OInterfaceContainer( _rxFactory, m_aMutex, XFormComponent::static_type() ) 1315*cdf0e10cSrcweir ,OFormComponents_BASE() 1316*cdf0e10cSrcweir { 1317*cdf0e10cSrcweir } 1318*cdf0e10cSrcweir 1319*cdf0e10cSrcweir //------------------------------------------------------------------------------ 1320*cdf0e10cSrcweir OFormComponents::OFormComponents( const OFormComponents& _cloneSource ) 1321*cdf0e10cSrcweir :FormComponentsBase( m_aMutex ) 1322*cdf0e10cSrcweir ,OInterfaceContainer( m_aMutex, _cloneSource ) 1323*cdf0e10cSrcweir ,OFormComponents_BASE() 1324*cdf0e10cSrcweir { 1325*cdf0e10cSrcweir } 1326*cdf0e10cSrcweir 1327*cdf0e10cSrcweir //------------------------------------------------------------------------------ 1328*cdf0e10cSrcweir OFormComponents::~OFormComponents() 1329*cdf0e10cSrcweir { 1330*cdf0e10cSrcweir if (!FormComponentsBase::rBHelper.bDisposed) 1331*cdf0e10cSrcweir { 1332*cdf0e10cSrcweir acquire(); 1333*cdf0e10cSrcweir dispose(); 1334*cdf0e10cSrcweir } 1335*cdf0e10cSrcweir } 1336*cdf0e10cSrcweir 1337*cdf0e10cSrcweir // OComponentHelper 1338*cdf0e10cSrcweir //------------------------------------------------------------------------------ 1339*cdf0e10cSrcweir void OFormComponents::disposing() 1340*cdf0e10cSrcweir { 1341*cdf0e10cSrcweir OInterfaceContainer::disposing(); 1342*cdf0e10cSrcweir FormComponentsBase::disposing(); 1343*cdf0e10cSrcweir m_xParent = NULL; 1344*cdf0e10cSrcweir } 1345*cdf0e10cSrcweir 1346*cdf0e10cSrcweir //XChild 1347*cdf0e10cSrcweir //------------------------------------------------------------------------------ 1348*cdf0e10cSrcweir void OFormComponents::setParent(const InterfaceRef& Parent) throw( NoSupportException, RuntimeException ) 1349*cdf0e10cSrcweir { 1350*cdf0e10cSrcweir ::osl::MutexGuard aGuard( m_aMutex ); 1351*cdf0e10cSrcweir m_xParent = Parent; 1352*cdf0e10cSrcweir } 1353*cdf0e10cSrcweir 1354*cdf0e10cSrcweir //------------------------------------------------------------------------------ 1355*cdf0e10cSrcweir InterfaceRef OFormComponents::getParent() throw( RuntimeException ) 1356*cdf0e10cSrcweir { 1357*cdf0e10cSrcweir return m_xParent; 1358*cdf0e10cSrcweir } 1359*cdf0e10cSrcweir 1360*cdf0e10cSrcweir //......................................................................... 1361*cdf0e10cSrcweir } // namespace frm 1362*cdf0e10cSrcweir //......................................................................... 1363*cdf0e10cSrcweir 1364