diff -r 7b46fd99a8272e4072cfa6012c7ac3cd8cc9c32f basic/source/classes/eventatt.cxx --- a/basic/source/classes/eventatt.cxx Wed Nov 04 14:48:15 2009 +0100 +++ b/basic/source/classes/eventatt.cxx Thu Nov 05 10:10:23 2009 +0100 @@ -95,53 +95,50 @@ Reference< frame::XModel > getModelFromBasic( SbxObject* pBasic ) { - Reference< frame::XModel > xModel; + OSL_PRECOND( pBasic != NULL, "getModelFromBasic: illegal call!" ); + if ( !pBasic ) + return NULL; - SbxObject* basicChosen = pBasic; + // look for the ThisComponent variable, first in the parent (which + // might be the document's Basic), then in the parent's parent (which might be + // the application Basic) + const ::rtl::OUString sThisComponent( RTL_CONSTASCII_USTRINGPARAM( "ThisComponent" ) ); + SbxVariable* pThisComponent = NULL; - if ( basicChosen == NULL) + SbxObject* pLookup = pBasic->GetParent(); + while ( pLookup && !pThisComponent ) { - OSL_TRACE("getModelFromBasic() StarBASIC* is NULL" ); - return xModel; + pThisComponent = pLookup->Find( sThisComponent, SbxCLASS_OBJECT ); + pLookup = pLookup->GetParent(); } - SbxObject* p = pBasic; - SbxObject* pParent = p->GetParent(); - SbxObject* pParentParent = pParent ? pParent->GetParent() : NULL; - - if( pParentParent ) + if ( !pThisComponent ) { - basicChosen = pParentParent; - } - else if( pParent ) - { - basicChosen = pParent; + OSL_TRACE("Failed to get ThisComponent"); + return NULL; } + Any aThisComponent( sbxToUnoValue( pThisComponent ) ); + Reference< frame::XModel > xModel( aThisComponent, UNO_QUERY ); + if ( !xModel.is() ) + { + // it's no XModel. Okay, ThisComponent nowadays is allowed to be a controller. + Reference< frame::XController > xController( aThisComponent, UNO_QUERY ); + if ( xController.is() ) + xModel = xController->getModel(); + } - Any aModel; - SbxVariable *pCompVar = basicChosen->Find( UniString(RTL_CONSTASCII_USTRINGPARAM("ThisComponent")), SbxCLASS_OBJECT ); + if ( !xModel.is() ) + { + OSL_ENSURE( false, "::getModelFromBasic: ThisComponent didn't lead me to a document model!" ); + return NULL; + } - if ( pCompVar ) - { - aModel = sbxToUnoValue( pCompVar ); - if ( sal_False == ( aModel >>= xModel ) || - !xModel.is() ) - { - OSL_TRACE("Failed to extract model from thisComponent "); - return xModel; - } - else - { - OSL_TRACE("Have model ThisComponent points to url %s", - ::rtl::OUStringToOString( xModel->getURL(), - RTL_TEXTENCODING_ASCII_US ).pData->buffer ); +#if OSL_DEBUG_LEVEL > 0 + OSL_TRACE("Have model ThisComponent points to url %s", + ::rtl::OUStringToOString( xModel->getURL(), + RTL_TEXTENCODING_ASCII_US ).pData->buffer ); +#endif - } - } - else - { - OSL_TRACE("Failed to get ThisComponent"); - } return xModel; }