Commits

Andreas Bregas  committed 752ace1

mib20: #163789# Handle class module factories document specific

  • Participants
  • Parent commits 177e446

Comments (0)

Files changed (1)

File basic/source/classes/sb.cxx

 SbClassFactory::~SbClassFactory()
 {}
 
+
+struct DocBasicItem
+{
+	StarBASIC*		m_pDocBasic;
+	SbxObjectRef	m_xClassModules;
+
+	DocBasicItem( void )
+		: m_pDocBasic( NULL )
+	{}
+	DocBasicItem( StarBASIC* pDocBasic )
+		: m_pDocBasic( pDocBasic )
+	{
+		m_xClassModules = new SbxObject( String() );
+	}
+};
+typedef std::vector< DocBasicItem > DocBasicItemVector;
+static DocBasicItemVector GaDocBasicItems;
+
+inline DocBasicItemVector::iterator lcl_findDocBasicItem( StarBASIC* pDocBasic )
+{
+	DocBasicItemVector::iterator it;
+	for( it = GaDocBasicItems.begin() ; it != GaDocBasicItems.end() ; ++it )
+	{
+		if( it->m_pDocBasic == pDocBasic )
+			break;
+	}
+	return it;
+}
+
+inline StarBASIC* lcl_getDocBasicForModule( SbModule* pModule )
+{
+	StarBASIC* pRetBasic = NULL;
+    SbxObject* pCurParent = pModule;
+	while( pCurParent->GetParent() != NULL )
+	{
+		pCurParent = pCurParent->GetParent();
+		StarBASIC* pDocBasic = PTR_CAST( StarBASIC, pCurParent );
+		if( pDocBasic != NULL && pDocBasic->IsDocBasic() )
+		{
+			pRetBasic = pDocBasic;
+			break;
+		}
+	}
+	return pRetBasic;
+}
+
 void SbClassFactory::AddClassModule( SbModule* pClassModule )
 {
+	SbxObjectRef xToUseClassModules = xClassModules;
+
+	StarBASIC* pDocBasic = lcl_getDocBasicForModule( pClassModule );
+	if( pDocBasic != NULL )
+	{
+		DocBasicItemVector::iterator it = lcl_findDocBasicItem( pDocBasic );
+		if( it != GaDocBasicItems.end() )
+			xToUseClassModules = it->m_xClassModules;
+	}
+
 	SbxObject* pParent = pClassModule->GetParent();
-	xClassModules->Insert( pClassModule );
+	xToUseClassModules->Insert( pClassModule );
 	pClassModule->SetParent( pParent );
 }
 
 
 SbxObject* SbClassFactory::CreateObject( const String& rClassName )
 {
-	SbxVariable* pVar = xClassModules->Find( rClassName, SbxCLASS_DONTCARE );
+	SbxObjectRef xToUseClassModules = xClassModules;
+
+	SbModule* pMod = pMOD;
+	StarBASIC* pDocBasic = pMod != NULL ? lcl_getDocBasicForModule( pMod ) : NULL;
+	if( pDocBasic != NULL )
+	{
+		DocBasicItemVector::iterator it = lcl_findDocBasicItem( pDocBasic );
+		if( it != GaDocBasicItems.end() )
+			xToUseClassModules = it->m_xClassModules;
+	}
+
+	SbxVariable* pVar = xToUseClassModules->Find( rClassName, SbxCLASS_OBJECT );
 	SbxObject* pRet = NULL;
 	if( pVar )
 	{
 	return pMod;
 }
 
-typedef std::vector< StarBASIC* > DocBasicVector;
-static DocBasicVector GaDocBasics;
-
 StarBASIC::StarBASIC( StarBASIC* p, BOOL bIsDocBasic  )
 	: SbxObject( String( RTL_CONSTASCII_USTRINGPARAM("StarBASIC") ) ), bDocBasic( bIsDocBasic )
 {
 	bQuit = FALSE;
 
 	if( bDocBasic )
-		GaDocBasics.push_back( this );
+		GaDocBasicItems.push_back( DocBasicItem( this ) );
 }
 
 // #51727 Override SetModified so that the modified state
 
 StarBASIC::~StarBASIC()
 {
+	// Needs to be first action as it can trigger events
+	disposeComVariablesForBasic( this );
+
 	if( !--GetSbData()->nInst )
 	{
 		RemoveFactory( pSBFAC );
 	{
 	    SbxError eOld = SbxBase::GetError();
 
-		DocBasicVector::iterator it;
-		for( it = GaDocBasics.begin() ; it != GaDocBasics.end() ; ++it )
+		DocBasicItemVector::iterator it = lcl_findDocBasicItem( this );
+		if( it != GaDocBasicItems.end() )
+			GaDocBasicItems.erase( it );
+		for( it = GaDocBasicItems.begin() ; it != GaDocBasicItems.end() ; ++it )
 		{
-			if( *it == this )
-			{
-				GaDocBasics.erase( it );
-				break;
-			}
-		}
-		for( it = GaDocBasics.begin() ; it != GaDocBasics.end() ; ++it )
-		{
-			StarBASIC* pBasic = *it;
+			StarBASIC* pBasic = it->m_pDocBasic;
 			pBasic->implClearDependingVarsOnDelete( this );
 		}
 
     }
 
     clearUnoMethodsForBasic( this );
-	disposeComVariablesForBasic( this );
 }
 
 // Override new() operator, so that everyone can create a new instance