LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/basic/source/classes - sb.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 345 1026 33.6 %
Date: 2013-07-09 Functions: 70 154 45.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       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/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      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 .
      18             :  */
      19             : 
      20             : 
      21             : #include <stdio.h>
      22             : 
      23             : #include "sb.hxx"
      24             : #include <tools/rcid.h>
      25             : #include <tools/stream.hxx>
      26             : #include <tools/errinf.hxx>
      27             : #include <basic/sbx.hxx>
      28             : #include <tools/rc.hxx>
      29             : #include <vcl/svapp.hxx>
      30             : #include <comphelper/processfactory.hxx>
      31             : #include "image.hxx"
      32             : #include "sbunoobj.hxx"
      33             : #include "sbjsmeth.hxx"
      34             : #include "sbjsmod.hxx"
      35             : #include "sbintern.hxx"
      36             : #include "runtime.hxx"
      37             : #include <basic/sbuno.hxx>
      38             : #include <basic/sbobjmod.hxx>
      39             : #include "stdobj.hxx"
      40             : #include "filefmt.hxx"
      41             : #include "sb.hrc"
      42             : #include <basrid.hxx>
      43             : #include <osl/mutex.hxx>
      44             : #include <cppuhelper/implbase1.hxx>
      45             : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      46             : #include <com/sun/star/util/XCloseBroadcaster.hpp>
      47             : #include <com/sun/star/util/XCloseListener.hpp>
      48             : #include "errobject.hxx"
      49             : #include <boost/unordered_map.hpp>
      50             : 
      51             : #include <com/sun/star/script/ModuleType.hpp>
      52             : #include <com/sun/star/script/ModuleInfo.hpp>
      53             : using namespace ::com::sun::star::script;
      54             : 
      55       18441 : TYPEINIT1(StarBASIC,SbxObject)
      56             : 
      57             : #define RTLNAME "@SBRTL"
      58             : //  i#i68894#
      59             : using namespace ::com::sun::star;
      60             : using namespace ::com::sun::star::uno;
      61             : using com::sun::star::uno::Reference;
      62             : using com::sun::star::uno::Any;
      63             : using com::sun::star::uno::UNO_QUERY;
      64             : using com::sun::star::lang::XMultiServiceFactory;
      65             : 
      66             : // ============================================================================
      67             : 
      68             : class DocBasicItem : public ::cppu::WeakImplHelper1< util::XCloseListener >
      69             : {
      70             : public:
      71             :     explicit DocBasicItem( StarBASIC& rDocBasic );
      72             :     virtual ~DocBasicItem();
      73             : 
      74           4 :     inline const SbxObjectRef& getClassModules() const { return mxClassModules; }
      75           0 :     inline bool isDocClosed() const { return mbDocClosed; }
      76             : 
      77             :     void clearDependingVarsOnDelete( StarBASIC& rDeletedBasic );
      78             : 
      79             :     void startListening();
      80             :     void stopListening();
      81             : 
      82             :     virtual void SAL_CALL queryClosing( const lang::EventObject& rSource, sal_Bool bGetsOwnership ) throw (util::CloseVetoException, uno::RuntimeException);
      83             :     virtual void SAL_CALL notifyClosing( const lang::EventObject& rSource ) throw (uno::RuntimeException);
      84             :     virtual void SAL_CALL disposing( const lang::EventObject& rSource ) throw (uno::RuntimeException);
      85             : 
      86             : private:
      87             :     StarBASIC&      mrDocBasic;
      88             :     SbxObjectRef    mxClassModules;
      89             :     bool            mbDocClosed;
      90             :     bool            mbDisposed;
      91             : };
      92             : 
      93             : // ----------------------------------------------------------------------------
      94             : 
      95         431 : DocBasicItem::DocBasicItem( StarBASIC& rDocBasic ) :
      96             :     mrDocBasic( rDocBasic ),
      97         431 :     mxClassModules( new SbxObject( OUString() ) ),
      98             :     mbDocClosed( false ),
      99         862 :     mbDisposed( false )
     100             : {
     101         431 : }
     102             : 
     103        1293 : DocBasicItem::~DocBasicItem()
     104             : {
     105         431 :     stopListening();
     106         862 : }
     107             : 
     108           0 : void DocBasicItem::clearDependingVarsOnDelete( StarBASIC& rDeletedBasic )
     109             : {
     110           0 :     mrDocBasic.implClearDependingVarsOnDelete( &rDeletedBasic );
     111           0 : }
     112             : 
     113         431 : void DocBasicItem::startListening()
     114             : {
     115         431 :     Any aThisComp;
     116         431 :     mrDocBasic.GetUNOConstant( "ThisComponent", aThisComp );
     117         862 :     Reference< util::XCloseBroadcaster > xCloseBC( aThisComp, UNO_QUERY );
     118         431 :     mbDisposed = !xCloseBC.is();
     119         431 :     if( xCloseBC.is() )
     120             :     {
     121          49 :         try { xCloseBC->addCloseListener( this ); } catch(const uno::Exception& ) {}
     122         431 :     }
     123         431 : }
     124             : 
     125         483 : void DocBasicItem::stopListening()
     126             : {
     127         966 :     if( mbDisposed ) return;
     128          49 :     mbDisposed = true;
     129          49 :     Any aThisComp;
     130          49 :     mrDocBasic.GetUNOConstant( "ThisComponent", aThisComp );
     131          98 :     Reference< util::XCloseBroadcaster > xCloseBC( aThisComp, UNO_QUERY );
     132          49 :     if( xCloseBC.is() )
     133             :     {
     134          52 :         try { xCloseBC->removeCloseListener( this ); } catch(const uno::Exception& ) {}
     135          49 :     }
     136             : }
     137             : 
     138          49 : void SAL_CALL DocBasicItem::queryClosing( const lang::EventObject& /*rSource*/, sal_Bool /*bGetsOwnership*/ ) throw (util::CloseVetoException, uno::RuntimeException)
     139             : {
     140          49 : }
     141             : 
     142          49 : void SAL_CALL DocBasicItem::notifyClosing( const lang::EventObject& /*rEvent*/ ) throw (uno::RuntimeException)
     143             : {
     144          49 :     stopListening();
     145          49 :     mbDocClosed = true;
     146          49 : }
     147             : 
     148           3 : void SAL_CALL DocBasicItem::disposing( const lang::EventObject& /*rEvent*/ ) throw (uno::RuntimeException)
     149             : {
     150           3 :     stopListening();
     151           3 : }
     152             : 
     153             : // ----------------------------------------------------------------------------
     154             : 
     155             : namespace {
     156             : 
     157             : typedef ::rtl::Reference< DocBasicItem > DocBasicItemRef;
     158             : typedef boost::unordered_map< const StarBASIC *, DocBasicItemRef > DocBasicItemMap;
     159             : 
     160             : class GaDocBasicItems : public rtl::Static<DocBasicItemMap,GaDocBasicItems> {};
     161             : 
     162           4 : const DocBasicItem* lclFindDocBasicItem( const StarBASIC* pDocBasic )
     163             : {
     164           4 :     DocBasicItemMap::iterator it = GaDocBasicItems::get().find( pDocBasic );
     165           4 :     DocBasicItemMap::iterator end = GaDocBasicItems::get().end();
     166           4 :     return (it != end) ? it->second.get() : 0;
     167             : }
     168             : 
     169         431 : void lclInsertDocBasicItem( StarBASIC& rDocBasic )
     170             : {
     171         431 :     DocBasicItemRef& rxDocBasicItem = GaDocBasicItems::get()[ &rDocBasic ];
     172         431 :     rxDocBasicItem.set( new DocBasicItem( rDocBasic ) );
     173         431 :     rxDocBasicItem->startListening();
     174         431 : }
     175             : 
     176           0 : void lclRemoveDocBasicItem( StarBASIC& rDocBasic )
     177             : {
     178           0 :     DocBasicItemMap::iterator it = GaDocBasicItems::get().find( &rDocBasic );
     179           0 :     if( it != GaDocBasicItems::get().end() )
     180             :     {
     181           0 :         it->second->stopListening();
     182           0 :         GaDocBasicItems::get().erase( it );
     183             :     }
     184           0 :     DocBasicItemMap::iterator it_end = GaDocBasicItems::get().end();
     185           0 :     for( it = GaDocBasicItems::get().begin(); it != it_end; ++it )
     186             :     {
     187           0 :         it->second->clearDependingVarsOnDelete( rDocBasic );
     188             :     }
     189           0 : }
     190             : 
     191          20 : StarBASIC* lclGetDocBasicForModule( SbModule* pModule )
     192             : {
     193          20 :     StarBASIC* pRetBasic = NULL;
     194          20 :     SbxObject* pCurParent = pModule;
     195          56 :     while( pCurParent->GetParent() != NULL )
     196             :     {
     197          20 :         pCurParent = pCurParent->GetParent();
     198          20 :         StarBASIC* pDocBasic = PTR_CAST( StarBASIC, pCurParent );
     199          20 :         if( pDocBasic != NULL && pDocBasic->IsDocBasic() )
     200             :         {
     201           4 :             pRetBasic = pDocBasic;
     202           4 :             break;
     203             :         }
     204             :     }
     205          20 :     return pRetBasic;
     206             : }
     207             : 
     208             : } // namespace
     209             : 
     210             : // ============================================================================
     211             : 
     212         377 : SbxObject* StarBASIC::getVBAGlobals( )
     213             : {
     214         377 :     if ( !pVBAGlobals )
     215             :     {
     216          28 :         Any aThisDoc;
     217          28 :         if ( GetUNOConstant("ThisComponent", aThisDoc) )
     218             :         {
     219          23 :             Reference< XMultiServiceFactory > xDocFac( aThisDoc, UNO_QUERY );
     220          23 :             if ( xDocFac.is() )
     221             :             {
     222             :                 try
     223             :                 {
     224          23 :                     xDocFac->createInstance("ooo.vba.VBAGlobals");
     225             :                 }
     226           0 :                 catch(const Exception& )
     227             :                 {
     228             :                     // Ignore
     229             :                 }
     230          23 :             }
     231             :         }
     232          56 :         const OUString aVBAHook("VBAGlobals");
     233          56 :         pVBAGlobals = (SbUnoObject*)Find( aVBAHook , SbxCLASS_DONTCARE );
     234             :     }
     235         377 :     return pVBAGlobals;
     236             : }
     237             : 
     238             : //  i#i68894#
     239         377 : SbxVariable* StarBASIC::VBAFind( const OUString& rName, SbxClassType t )
     240             : {
     241         377 :     if( rName == "ThisComponent" )
     242             :     {
     243           0 :         return NULL;
     244             :     }
     245             :     // rename to init globals
     246         377 :     if ( getVBAGlobals( ) )
     247             :     {
     248         372 :         return pVBAGlobals->Find( rName, t );
     249             :     }
     250           5 :     return NULL;
     251             : }
     252             : 
     253             : // Create array for conversion SFX <-> VB error code
     254             : struct SFX_VB_ErrorItem
     255             : {
     256             :     sal_uInt16  nErrorVB;
     257             :     SbError nErrorSFX;
     258             : };
     259             : 
     260             : const SFX_VB_ErrorItem SFX_VB_ErrorTab[] =
     261             : {
     262             :     { 1, SbERR_BASIC_EXCEPTION },  // #87844 Map exception to error code 1
     263             :     { 2, SbERR_SYNTAX },
     264             :     { 3, SbERR_NO_GOSUB },
     265             :     { 4, SbERR_REDO_FROM_START },
     266             :     { 5, SbERR_BAD_ARGUMENT },
     267             :     { 6, SbERR_MATH_OVERFLOW },
     268             :     { 7, SbERR_NO_MEMORY },
     269             :     { 8, SbERR_ALREADY_DIM },
     270             :     { 9, SbERR_OUT_OF_RANGE },
     271             :     { 10, SbERR_DUPLICATE_DEF },
     272             :     { 11, SbERR_ZERODIV },
     273             :     { 12, SbERR_VAR_UNDEFINED },
     274             :     { 13, SbERR_CONVERSION },
     275             :     { 14, SbERR_BAD_PARAMETER },
     276             :     { 18, SbERR_USER_ABORT },
     277             :     { 20, SbERR_BAD_RESUME },
     278             :     { 28, SbERR_STACK_OVERFLOW },
     279             :     { 35, SbERR_PROC_UNDEFINED },
     280             :     { 48, SbERR_BAD_DLL_LOAD },
     281             :     { 49, SbERR_BAD_DLL_CALL },
     282             :     { 51, SbERR_INTERNAL_ERROR },
     283             :     { 52, SbERR_BAD_CHANNEL },
     284             :     { 53, SbERR_FILE_NOT_FOUND },
     285             :     { 54, SbERR_BAD_FILE_MODE },
     286             :     { 55, SbERR_FILE_ALREADY_OPEN },
     287             :     { 57, SbERR_IO_ERROR },
     288             :     { 58, SbERR_FILE_EXISTS },
     289             :     { 59, SbERR_BAD_RECORD_LENGTH },
     290             :     { 61, SbERR_DISK_FULL },
     291             :     { 62, SbERR_READ_PAST_EOF },
     292             :     { 63, SbERR_BAD_RECORD_NUMBER },
     293             :     { 67, SbERR_TOO_MANY_FILES },
     294             :     { 68, SbERR_NO_DEVICE },
     295             :     { 70, SbERR_ACCESS_DENIED },
     296             :     { 71, SbERR_NOT_READY },
     297             :     { 73, SbERR_NOT_IMPLEMENTED },
     298             :     { 74, SbERR_DIFFERENT_DRIVE },
     299             :     { 75, SbERR_ACCESS_ERROR },
     300             :     { 76, SbERR_PATH_NOT_FOUND },
     301             :     { 91, SbERR_NO_OBJECT },
     302             :     { 93, SbERR_BAD_PATTERN },
     303             :     { 94, SBERR_IS_NULL },
     304             :     { 250, SbERR_DDE_ERROR },
     305             :     { 280, SbERR_DDE_WAITINGACK },
     306             :     { 281, SbERR_DDE_OUTOFCHANNELS },
     307             :     { 282, SbERR_DDE_NO_RESPONSE },
     308             :     { 283, SbERR_DDE_MULT_RESPONSES },
     309             :     { 284, SbERR_DDE_CHANNEL_LOCKED },
     310             :     { 285, SbERR_DDE_NOTPROCESSED },
     311             :     { 286, SbERR_DDE_TIMEOUT },
     312             :     { 287, SbERR_DDE_USER_INTERRUPT },
     313             :     { 288, SbERR_DDE_BUSY },
     314             :     { 289, SbERR_DDE_NO_DATA },
     315             :     { 290, SbERR_DDE_WRONG_DATA_FORMAT },
     316             :     { 291, SbERR_DDE_PARTNER_QUIT },
     317             :     { 292, SbERR_DDE_CONV_CLOSED },
     318             :     { 293, SbERR_DDE_NO_CHANNEL },
     319             :     { 294, SbERR_DDE_INVALID_LINK },
     320             :     { 295, SbERR_DDE_QUEUE_OVERFLOW },
     321             :     { 296, SbERR_DDE_LINK_ALREADY_EST },
     322             :     { 297, SbERR_DDE_LINK_INV_TOPIC },
     323             :     { 298, SbERR_DDE_DLL_NOT_FOUND },
     324             :     { 323, SbERR_CANNOT_LOAD },
     325             :     { 341, SbERR_BAD_INDEX },
     326             :     { 366, SbERR_NO_ACTIVE_OBJECT },
     327             :     { 380, SbERR_BAD_PROP_VALUE },
     328             :     { 382, SbERR_PROP_READONLY },
     329             :     { 394, SbERR_PROP_WRITEONLY },
     330             :     { 420, SbERR_INVALID_OBJECT },
     331             :     { 423, SbERR_NO_METHOD },
     332             :     { 424, SbERR_NEEDS_OBJECT },
     333             :     { 425, SbERR_INVALID_USAGE_OBJECT },
     334             :     { 430, SbERR_NO_OLE },
     335             :     { 438, SbERR_BAD_METHOD },
     336             :     { 440, SbERR_OLE_ERROR },
     337             :     { 445, SbERR_BAD_ACTION },
     338             :     { 446, SbERR_NO_NAMED_ARGS },
     339             :     { 447, SbERR_BAD_LOCALE },
     340             :     { 448, SbERR_NAMED_NOT_FOUND },
     341             :     { 449, SbERR_NOT_OPTIONAL },
     342             :     { 450, SbERR_WRONG_ARGS },
     343             :     { 451, SbERR_NOT_A_COLL },
     344             :     { 452, SbERR_BAD_ORDINAL },
     345             :     { 453, SbERR_DLLPROC_NOT_FOUND },
     346             :     { 460, SbERR_BAD_CLIPBD_FORMAT },
     347             :     { 951, SbERR_UNEXPECTED },
     348             :     { 952, SbERR_EXPECTED },
     349             :     { 953, SbERR_SYMBOL_EXPECTED },
     350             :     { 954, SbERR_VAR_EXPECTED },
     351             :     { 955, SbERR_LABEL_EXPECTED },
     352             :     { 956, SbERR_LVALUE_EXPECTED },
     353             :     { 957, SbERR_VAR_DEFINED },
     354             :     { 958, SbERR_PROC_DEFINED },
     355             :     { 959, SbERR_LABEL_DEFINED },
     356             :     { 960, SbERR_UNDEF_VAR },
     357             :     { 961, SbERR_UNDEF_ARRAY },
     358             :     { 962, SbERR_UNDEF_PROC },
     359             :     { 963, SbERR_UNDEF_LABEL },
     360             :     { 964, SbERR_UNDEF_TYPE },
     361             :     { 965, SbERR_BAD_EXIT },
     362             :     { 966, SbERR_BAD_BLOCK },
     363             :     { 967, SbERR_BAD_BRACKETS },
     364             :     { 968, SbERR_BAD_DECLARATION },
     365             :     { 969, SbERR_BAD_PARAMETERS },
     366             :     { 970, SbERR_BAD_CHAR_IN_NUMBER },
     367             :     { 971, SbERR_MUST_HAVE_DIMS },
     368             :     { 972, SbERR_NO_IF },
     369             :     { 973, SbERR_NOT_IN_SUBR },
     370             :     { 974, SbERR_NOT_IN_MAIN },
     371             :     { 975, SbERR_WRONG_DIMS },
     372             :     { 976, SbERR_BAD_OPTION },
     373             :     { 977, SbERR_CONSTANT_REDECLARED },
     374             :     { 978, SbERR_PROG_TOO_LARGE },
     375             :     { 979, SbERR_NO_STRINGS_ARRAYS },
     376             :     { 1000, SbERR_PROPERTY_NOT_FOUND },
     377             :     { 1001, SbERR_METHOD_NOT_FOUND },
     378             :     { 1002, SbERR_ARG_MISSING },
     379             :     { 1003, SbERR_BAD_NUMBER_OF_ARGS },
     380             :     { 1004, SbERR_METHOD_FAILED },
     381             :     { 1005, SbERR_SETPROP_FAILED },
     382             :     { 1006, SbERR_GETPROP_FAILED },
     383             :     { 1007, SbERR_BASIC_COMPAT },
     384             :     { 0xFFFF, 0xFFFFFFFFL }     // End mark
     385             : };
     386             : 
     387             : // The StarBASIC factory is a hack. When a SbModule is created, its pointer
     388             : // is saved and given to the following SbProperties/SbMethods. This restores
     389             : // the Modul-relationshop. But it works only when a modul is loaded.
     390             : // Can cause troubles with separately loaded properties!
     391             : 
     392           0 : SbxBase* SbiFactory::Create( sal_uInt16 nSbxId, sal_uInt32 nCreator )
     393             : {
     394           0 :     if( nCreator ==  SBXCR_SBX )
     395             :     {
     396           0 :         OUString aEmpty;
     397           0 :         switch( nSbxId )
     398             :         {
     399             :         case SBXID_BASIC:
     400           0 :             return new StarBASIC( NULL );
     401             :         case SBXID_BASICMOD:
     402           0 :             return new SbModule( aEmpty );
     403             :         case SBXID_BASICPROP:
     404           0 :             return new SbProperty( aEmpty, SbxVARIANT, NULL );
     405             :         case SBXID_BASICMETHOD:
     406           0 :             return new SbMethod( aEmpty, SbxVARIANT, NULL );
     407             :         case SBXID_JSCRIPTMOD:
     408           0 :             return new SbJScriptModule( aEmpty );
     409             :         case SBXID_JSCRIPTMETH:
     410           0 :             return new SbJScriptMethod( aEmpty, SbxVARIANT, NULL );
     411           0 :         }
     412             :     }
     413           0 :     return NULL;
     414             : }
     415             : 
     416          20 : SbxObject* SbiFactory::CreateObject( const OUString& rClass )
     417             : {
     418          20 :     if( rClass.equalsIgnoreAsciiCase( "StarBASIC" ) )
     419             :     {
     420           0 :         return new StarBASIC( NULL );
     421             :     }
     422          20 :     else if( rClass.equalsIgnoreAsciiCase( "StarBASICModule" ) )
     423             :     {
     424           0 :         return new SbModule( OUString() );
     425             :     }
     426          20 :     else if( rClass.equalsIgnoreAsciiCase( "Collection" ) )
     427             :     {
     428           0 :         return new BasicCollection( OUString("Collection"));
     429             :     }
     430          20 :     else if( rClass.equalsIgnoreAsciiCase( "FileSystemObject" ) )
     431             :     {
     432             :         try
     433             :         {
     434           0 :             Reference< XMultiServiceFactory > xFactory( comphelper::getProcessServiceFactory(), UNO_SET_THROW );
     435           0 :             OUString aServiceName("ooo.vba.FileSystemObject");
     436           0 :             Reference< XInterface > xInterface( xFactory->createInstance( aServiceName ), UNO_SET_THROW );
     437           0 :             return new SbUnoObject( aServiceName, uno::makeAny( xInterface ) );
     438             :         }
     439           0 :         catch(const Exception& )
     440             :         {
     441             :         }
     442             :     }
     443          20 :     return NULL;
     444             : }
     445             : 
     446             : 
     447             : // Factory class to create OLE objects
     448         261 : class SbOLEFactory : public SbxFactory
     449             : {
     450             : public:
     451             :     virtual SbxBase* Create( sal_uInt16 nSbxId, sal_uInt32 = SBXCR_SBX );
     452             :     virtual SbxObject* CreateObject( const OUString& );
     453             : };
     454             : 
     455           0 : SbxBase* SbOLEFactory::Create( sal_uInt16, sal_uInt32 )
     456             : {
     457             :     // Not supported
     458           0 :     return NULL;
     459             : }
     460             : 
     461             : SbUnoObject* createOLEObject_Impl( const OUString& aType );  // sbunoobj.cxx
     462             : 
     463          20 : SbxObject* SbOLEFactory::CreateObject( const OUString& rClassName )
     464             : {
     465          20 :     SbxObject* pRet = createOLEObject_Impl( rClassName );
     466          20 :     return pRet;
     467             : }
     468             : 
     469             : 
     470             : //========================================================================
     471             : // SbFormFactory, show user forms by: dim as new <user form name>
     472             : 
     473         261 : class SbFormFactory : public SbxFactory
     474             : {
     475             : public:
     476             :     virtual SbxBase* Create( sal_uInt16 nSbxId, sal_uInt32 = SBXCR_SBX );
     477             :     virtual SbxObject* CreateObject( const OUString& );
     478             : };
     479             : 
     480           0 : SbxBase* SbFormFactory::Create( sal_uInt16, sal_uInt32 )
     481             : {
     482             :     // Not supported
     483           0 :     return NULL;
     484             : }
     485             : 
     486          20 : SbxObject* SbFormFactory::CreateObject( const OUString& rClassName )
     487             : {
     488          20 :     if( SbModule* pMod = GetSbData()->pMod )
     489             :     {
     490          20 :         if( SbxVariable* pVar = pMod->Find( rClassName, SbxCLASS_OBJECT ) )
     491             :         {
     492           0 :             if( SbUserFormModule* pFormModule = PTR_CAST( SbUserFormModule, pVar->GetObject() ) )
     493             :             {
     494           0 :                 bool bInitState = pFormModule->getInitState();
     495           0 :                 if( bInitState )
     496             :                 {
     497             :                     // Not the first instantiate, reset
     498           0 :                     bool bTriggerTerminateEvent = false;
     499           0 :                     pFormModule->ResetApiObj( bTriggerTerminateEvent );
     500           0 :                     pFormModule->setInitState( false );
     501             :                 }
     502             :                 else
     503             :                 {
     504           0 :                     pFormModule->Load();
     505             :                 }
     506           0 :                 return pFormModule->CreateInstance();
     507             :             }
     508             :         }
     509             :     }
     510          20 :     return 0;
     511             : }
     512             : 
     513             : 
     514             : //========================================================================
     515             : // SbTypeFactory
     516             : 
     517           0 : SbxObject* cloneTypeObjectImpl( const SbxObject& rTypeObj )
     518             : {
     519           0 :     SbxObject* pRet = new SbxObject( rTypeObj );
     520           0 :     pRet->PutObject( pRet );
     521             : 
     522             :     // Copy the properties, not only the reference to them
     523           0 :     SbxArray* pProps = pRet->GetProperties();
     524           0 :     sal_uInt32 nCount = pProps->Count32();
     525           0 :     for( sal_uInt32 i = 0 ; i < nCount ; i++ )
     526             :     {
     527           0 :         SbxVariable* pVar = pProps->Get32( i );
     528           0 :         SbxProperty* pProp = PTR_CAST( SbxProperty, pVar );
     529           0 :         if( pProp )
     530             :         {
     531           0 :             SbxProperty* pNewProp = new SbxProperty( *pProp );
     532           0 :             SbxDataType eVarType = pVar->GetType();
     533           0 :             if( eVarType & SbxARRAY )
     534             :             {
     535           0 :                 SbxBase* pParObj = pVar->GetObject();
     536           0 :                 SbxDimArray* pSource = PTR_CAST(SbxDimArray,pParObj);
     537           0 :                 SbxDimArray* pDest = new SbxDimArray( pVar->GetType() );
     538             : 
     539           0 :                 pDest->setHasFixedSize( pSource->hasFixedSize() );
     540           0 :                 if ( pSource->GetDims() && pSource->hasFixedSize() )
     541             :                 {
     542           0 :                     sal_Int32 lb = 0;
     543           0 :                     sal_Int32 ub = 0;
     544           0 :                     for ( sal_Int32 j = 1 ; j <= pSource->GetDims(); ++j )
     545             :                     {
     546           0 :                         pSource->GetDim32( (sal_Int32)j, lb, ub );
     547           0 :                         pDest->AddDim32( lb, ub );
     548             :                     }
     549             :                 }
     550             :                 else
     551             :                 {
     552           0 :                     pDest->unoAddDim( 0, -1 ); // variant array
     553             :                 }
     554           0 :                 sal_uInt16 nSavFlags = pVar->GetFlags();
     555           0 :                 pNewProp->ResetFlag( SBX_FIXED );
     556             :                 // need to reset the FIXED flag
     557             :                 // when calling PutObject ( because the type will not match Object )
     558           0 :                 pNewProp->PutObject( pDest );
     559           0 :                 pNewProp->SetFlags( nSavFlags );
     560             :             }
     561           0 :             if( eVarType == SbxOBJECT )
     562             :             {
     563           0 :                 SbxBase* pObjBase = pVar->GetObject();
     564           0 :                 SbxObject* pSrcObj = PTR_CAST(SbxObject,pObjBase);
     565           0 :                 SbxObject* pDestObj = NULL;
     566           0 :                 if( pSrcObj != NULL )
     567           0 :                     pDestObj = cloneTypeObjectImpl( *pSrcObj );
     568           0 :                 pNewProp->PutObject( pDestObj );
     569             :             }
     570           0 :             pProps->PutDirect( pNewProp, i );
     571             :         }
     572             :     }
     573           0 :     return pRet;
     574             : }
     575             : 
     576             : // Factory class to create user defined objects (type command)
     577         261 : class SbTypeFactory : public SbxFactory
     578             : {
     579             : public:
     580             :     virtual SbxBase* Create( sal_uInt16 nSbxId, sal_uInt32 = SBXCR_SBX );
     581             :     virtual SbxObject* CreateObject( const OUString& );
     582             : };
     583             : 
     584           0 : SbxBase* SbTypeFactory::Create( sal_uInt16, sal_uInt32 )
     585             : {
     586             :     // Not supported
     587           0 :     return NULL;
     588             : }
     589             : 
     590         154 : SbxObject* SbTypeFactory::CreateObject( const OUString& rClassName )
     591             : {
     592         154 :     SbxObject* pRet = NULL;
     593         154 :     SbModule* pMod = GetSbData()->pMod;
     594         154 :     if( pMod )
     595             :     {
     596         154 :         const SbxObject* pObj = pMod->FindType( rClassName );
     597         154 :         if( pObj )
     598             :         {
     599           0 :             pRet = cloneTypeObjectImpl( *pObj );
     600             :         }
     601             :     }
     602         154 :     return pRet;
     603             : }
     604             : 
     605         134 : SbxObject* createUserTypeImpl( const OUString& rClassName )
     606             : {
     607         134 :     SbxObject* pRetObj = GetSbData()->pTypeFac->CreateObject( rClassName );
     608         134 :     return pRetObj;
     609             : }
     610             : 
     611             : 
     612        5112 : TYPEINIT1(SbClassModuleObject,SbModule)
     613             : 
     614           0 : SbClassModuleObject::SbClassModuleObject( SbModule* pClassModule )
     615           0 :     : SbModule( pClassModule->GetName() )
     616             :     , mpClassModule( pClassModule )
     617           0 :     , mbInitializeEventDone( false )
     618             : {
     619           0 :     aOUSource = pClassModule->aOUSource;
     620           0 :     aComment = pClassModule->aComment;
     621           0 :     pImage = pClassModule->pImage;
     622           0 :     pBreaks = pClassModule->pBreaks;
     623             : 
     624           0 :     SetClassName( pClassModule->GetName() );
     625             : 
     626             :     // Allow search only internally
     627           0 :     ResetFlag( SBX_GBLSEARCH );
     628             : 
     629             :     // Copy the methods from original class module
     630           0 :     SbxArray* pClassMethods = pClassModule->GetMethods();
     631           0 :     sal_uInt32 nMethodCount = pClassMethods->Count32();
     632             :     sal_uInt32 i;
     633           0 :     for( i = 0 ; i < nMethodCount ; i++ )
     634             :     {
     635           0 :         SbxVariable* pVar = pClassMethods->Get32( i );
     636             : 
     637             :         // Exclude SbIfaceMapperMethod to copy them in a second step
     638           0 :         SbIfaceMapperMethod* pIfaceMethod = PTR_CAST( SbIfaceMapperMethod, pVar );
     639           0 :         if( !pIfaceMethod )
     640             :         {
     641           0 :             SbMethod* pMethod = PTR_CAST(SbMethod, pVar );
     642           0 :             if( pMethod )
     643             :             {
     644           0 :                 sal_uInt16 nFlags_ = pMethod->GetFlags();
     645           0 :                 pMethod->SetFlag( SBX_NO_BROADCAST );
     646           0 :                 SbMethod* pNewMethod = new SbMethod( *pMethod );
     647           0 :                 pNewMethod->ResetFlag( SBX_NO_BROADCAST );
     648           0 :                 pMethod->SetFlags( nFlags_ );
     649           0 :                 pNewMethod->pMod = this;
     650           0 :                 pNewMethod->SetParent( this );
     651           0 :                 pMethods->PutDirect( pNewMethod, i );
     652           0 :                 StartListening( pNewMethod->GetBroadcaster(), sal_True );
     653             :             }
     654             :         }
     655             :     }
     656             : 
     657             :     // Copy SbIfaceMapperMethod in a second step to ensure that
     658             :     // the corresponding base methods have already been copied
     659           0 :     for( i = 0 ; i < nMethodCount ; i++ )
     660             :     {
     661           0 :         SbxVariable* pVar = pClassMethods->Get32( i );
     662             : 
     663           0 :         SbIfaceMapperMethod* pIfaceMethod = PTR_CAST( SbIfaceMapperMethod, pVar );
     664           0 :         if( pIfaceMethod )
     665             :         {
     666           0 :             SbMethod* pImplMethod = pIfaceMethod->getImplMethod();
     667           0 :             if( !pImplMethod )
     668             :             {
     669             :                 OSL_FAIL( "No ImplMethod" );
     670           0 :                 continue;
     671             :             }
     672             : 
     673             :             // Search for own copy of ImplMethod
     674           0 :             SbxVariable* p = pMethods->Find( pImplMethod->GetName(), SbxCLASS_METHOD );
     675           0 :             SbMethod* pImplMethodCopy = p ? PTR_CAST(SbMethod,p) : NULL;
     676           0 :             if( !pImplMethodCopy )
     677             :             {
     678             :                 OSL_FAIL( "Found no ImplMethod copy" );
     679           0 :                 continue;
     680             :             }
     681             :             SbIfaceMapperMethod* pNewIfaceMethod =
     682           0 :                 new SbIfaceMapperMethod( pIfaceMethod->GetName(), pImplMethodCopy );
     683           0 :             pMethods->PutDirect( pNewIfaceMethod, i );
     684             :         }
     685             :     }
     686             : 
     687             :     // Copy the properties from original class module
     688           0 :     SbxArray* pClassProps = pClassModule->GetProperties();
     689           0 :     sal_uInt32 nPropertyCount = pClassProps->Count32();
     690           0 :     for( i = 0 ; i < nPropertyCount ; i++ )
     691             :     {
     692           0 :         SbxVariable* pVar = pClassProps->Get32( i );
     693           0 :         SbProcedureProperty* pProcedureProp = PTR_CAST( SbProcedureProperty, pVar );
     694           0 :         if( pProcedureProp )
     695             :         {
     696           0 :             sal_uInt16 nFlags_ = pProcedureProp->GetFlags();
     697           0 :             pProcedureProp->SetFlag( SBX_NO_BROADCAST );
     698             :             SbProcedureProperty* pNewProp = new SbProcedureProperty
     699           0 :                 ( pProcedureProp->GetName(), pProcedureProp->GetType() );
     700           0 :             pNewProp->SetFlags( nFlags_ ); // Copy flags
     701           0 :             pNewProp->ResetFlag( SBX_NO_BROADCAST ); // except the Broadcast if it was set
     702           0 :             pProcedureProp->SetFlags( nFlags_ );
     703           0 :             pProps->PutDirect( pNewProp, i );
     704           0 :             StartListening( pNewProp->GetBroadcaster(), sal_True );
     705             :         }
     706             :         else
     707             :         {
     708           0 :             SbxProperty* pProp = PTR_CAST( SbxProperty, pVar );
     709           0 :             if( pProp )
     710             :             {
     711           0 :                 sal_uInt16 nFlags_ = pProp->GetFlags();
     712           0 :                 pProp->SetFlag( SBX_NO_BROADCAST );
     713           0 :                 SbxProperty* pNewProp = new SbxProperty( *pProp );
     714             : 
     715             :                 // Special handling for modules instances and collections, they need
     716             :                 // to be instantiated, otherwise all refer to the same base object
     717           0 :                 SbxDataType eVarType = pProp->GetType();
     718           0 :                 if( eVarType == SbxOBJECT )
     719             :                 {
     720           0 :                     SbxBase* pObjBase = pProp->GetObject();
     721           0 :                     SbxObject* pObj = PTR_CAST(SbxObject,pObjBase);
     722           0 :                     if( pObj != NULL )
     723             :                     {
     724           0 :                         OUString aObjClass = pObj->GetClassName();
     725             : 
     726           0 :                         SbClassModuleObject* pClassModuleObj = PTR_CAST(SbClassModuleObject,pObjBase);
     727           0 :                         if( pClassModuleObj != NULL )
     728             :                         {
     729           0 :                             SbModule* pLclClassModule = pClassModuleObj->getClassModule();
     730           0 :                             SbClassModuleObject* pNewObj = new SbClassModuleObject( pLclClassModule );
     731           0 :                             pNewObj->SetName( pProp->GetName() );
     732           0 :                             pNewObj->SetParent( pLclClassModule->pParent );
     733           0 :                             pNewProp->PutObject( pNewObj );
     734             :                         }
     735           0 :                         else if( aObjClass.equalsIgnoreAsciiCase( "Collection" ) )
     736             :                         {
     737           0 :                             OUString aCollectionName("Collection");
     738           0 :                             BasicCollection* pNewCollection = new BasicCollection( aCollectionName );
     739           0 :                             pNewCollection->SetName( pProp->GetName() );
     740           0 :                             pNewCollection->SetParent( pClassModule->pParent );
     741           0 :                             pNewProp->PutObject( pNewCollection );
     742           0 :                         }
     743             :                     }
     744             :                 }
     745             : 
     746           0 :                 pNewProp->ResetFlag( SBX_NO_BROADCAST );
     747           0 :                 pNewProp->SetParent( this );
     748           0 :                 pProps->PutDirect( pNewProp, i );
     749           0 :                 pProp->SetFlags( nFlags_ );
     750             :             }
     751             :         }
     752             :     }
     753           0 :     SetModuleType( ModuleType::CLASS );
     754           0 :     mbVBACompat = pClassModule->mbVBACompat;
     755           0 : }
     756             : 
     757           0 : SbClassModuleObject::~SbClassModuleObject()
     758             : {
     759             :     // do not trigger termination event when document is already closed
     760           0 :     if( StarBASIC::IsRunning() )
     761           0 :         if( StarBASIC* pDocBasic = lclGetDocBasicForModule( this ) )
     762           0 :             if( const DocBasicItem* pDocBasicItem = lclFindDocBasicItem( pDocBasic ) )
     763           0 :                 if( !pDocBasicItem->isDocClosed() )
     764           0 :                     triggerTerminateEvent();
     765             : 
     766             :     // Must be deleted by base class dtor because this data
     767             :     // is not owned by the SbClassModuleObject object
     768           0 :     pImage = NULL;
     769           0 :     pBreaks = NULL;
     770           0 : }
     771             : 
     772           0 : void SbClassModuleObject::SFX_NOTIFY( SfxBroadcaster& rBC, const TypeId& rBCType,
     773             :                            const SfxHint& rHint, const TypeId& rHintType )
     774             : {
     775           0 :     handleProcedureProperties( rBC, rHint );
     776           0 : }
     777             : 
     778           0 : SbxVariable* SbClassModuleObject::Find( const OUString& rName, SbxClassType t )
     779             : {
     780           0 :     SbxVariable* pRes = SbxObject::Find( rName, t );
     781           0 :     if( pRes )
     782             :     {
     783           0 :         triggerInitializeEvent();
     784             : 
     785           0 :         SbIfaceMapperMethod* pIfaceMapperMethod = PTR_CAST(SbIfaceMapperMethod,pRes);
     786           0 :         if( pIfaceMapperMethod )
     787             :         {
     788           0 :             pRes = pIfaceMapperMethod->getImplMethod();
     789           0 :             pRes->SetFlag( SBX_EXTFOUND );
     790             :         }
     791             :     }
     792           0 :     return pRes;
     793             : }
     794             : 
     795           0 : void SbClassModuleObject::triggerInitializeEvent( void )
     796             : {
     797           0 :     if( mbInitializeEventDone )
     798             :     {
     799           0 :         return;
     800             :     }
     801             : 
     802           0 :     mbInitializeEventDone = true;
     803             : 
     804             :     // Search method
     805           0 :     SbxVariable* pMeth = SbxObject::Find(OUString("Class_Initialize"), SbxCLASS_METHOD);
     806           0 :     if( pMeth )
     807             :     {
     808           0 :         SbxValues aVals;
     809           0 :         pMeth->Get( aVals );
     810             :     }
     811             : }
     812             : 
     813           0 : void SbClassModuleObject::triggerTerminateEvent( void )
     814             : {
     815           0 :     if( !mbInitializeEventDone || GetSbData()->bRunInit )
     816             :     {
     817           0 :         return;
     818             :     }
     819             :     // Search method
     820           0 :     SbxVariable* pMeth = SbxObject::Find(OUString("Class_Terminate"), SbxCLASS_METHOD );
     821           0 :     if( pMeth )
     822             :     {
     823           0 :         SbxValues aVals;
     824           0 :         pMeth->Get( aVals );
     825             :     }
     826             : }
     827             : 
     828             : 
     829           0 : SbClassData::SbClassData( void )
     830             : {
     831           0 :     mxIfaces = new SbxArray();
     832           0 : }
     833             : 
     834           0 : void SbClassData::clear( void )
     835             : {
     836           0 :     mxIfaces->Clear();
     837           0 :     maRequiredTypes.clear();
     838           0 : }
     839             : 
     840          87 : SbClassFactory::SbClassFactory( void )
     841             : {
     842          87 :     OUString aDummyName;
     843          87 :     xClassModules = new SbxObject( aDummyName );
     844          87 : }
     845             : 
     846         174 : SbClassFactory::~SbClassFactory()
     847         174 : {}
     848             : 
     849           0 : void SbClassFactory::AddClassModule( SbModule* pClassModule )
     850             : {
     851           0 :     SbxObjectRef xToUseClassModules = xClassModules;
     852             : 
     853           0 :     if( StarBASIC* pDocBasic = lclGetDocBasicForModule( pClassModule ) )
     854           0 :         if( const DocBasicItem* pDocBasicItem = lclFindDocBasicItem( pDocBasic ) )
     855           0 :             xToUseClassModules = pDocBasicItem->getClassModules();
     856             : 
     857           0 :     SbxObject* pParent = pClassModule->GetParent();
     858           0 :     xToUseClassModules->Insert( pClassModule );
     859           0 :     pClassModule->SetParent( pParent );
     860           0 : }
     861             : 
     862         162 : void SbClassFactory::RemoveClassModule( SbModule* pClassModule )
     863             : {
     864         162 :     xClassModules->Remove( pClassModule );
     865         162 : }
     866             : 
     867           0 : SbxBase* SbClassFactory::Create( sal_uInt16, sal_uInt32 )
     868             : {
     869             :     // Not supported
     870           0 :     return NULL;
     871             : }
     872             : 
     873          20 : SbxObject* SbClassFactory::CreateObject( const OUString& rClassName )
     874             : {
     875          20 :     SbxObjectRef xToUseClassModules = xClassModules;
     876             : 
     877          20 :     if( SbModule* pMod = GetSbData()->pMod )
     878             :     {
     879          20 :         if( StarBASIC* pDocBasic = lclGetDocBasicForModule( pMod ) )
     880             :         {
     881           4 :             if( const DocBasicItem* pDocBasicItem = lclFindDocBasicItem( pDocBasic ) )
     882             :             {
     883           4 :                 xToUseClassModules = pDocBasicItem->getClassModules();
     884             :             }
     885             :         }
     886             :     }
     887          20 :     SbxVariable* pVar = xToUseClassModules->Find( rClassName, SbxCLASS_OBJECT );
     888          20 :     SbxObject* pRet = NULL;
     889          20 :     if( pVar )
     890             :     {
     891           0 :         SbModule* pVarMod = (SbModule*)pVar;
     892           0 :         pRet = new SbClassModuleObject( pVarMod );
     893             :     }
     894          20 :     return pRet;
     895             : }
     896             : 
     897         144 : SbModule* SbClassFactory::FindClass( const OUString& rClassName )
     898             : {
     899         144 :     SbxVariable* pVar = xClassModules->Find( rClassName, SbxCLASS_DONTCARE );
     900         144 :     SbModule* pMod = pVar ? (SbModule*)pVar : NULL;
     901         144 :     return pMod;
     902             : }
     903             : 
     904         774 : StarBASIC::StarBASIC( StarBASIC* p, bool bIsDocBasic  )
     905         774 :     : SbxObject( OUString("StarBASIC") ), bDocBasic( bIsDocBasic )
     906             : {
     907         774 :     SetParent( p );
     908         774 :     pLibInfo = NULL;
     909         774 :     bNoRtl = bBreak = false;
     910         774 :     bVBAEnabled = false;
     911         774 :     pModules = new SbxArray;
     912             : 
     913         774 :     if( !GetSbData()->nInst++ )
     914             :     {
     915          87 :         GetSbData()->pSbFac = new SbiFactory;
     916          87 :         AddFactory( GetSbData()->pSbFac );
     917          87 :         GetSbData()->pTypeFac = new SbTypeFactory;
     918          87 :         AddFactory( GetSbData()->pTypeFac );
     919          87 :         GetSbData()->pClassFac = new SbClassFactory;
     920          87 :         AddFactory( GetSbData()->pClassFac );
     921          87 :         GetSbData()->pOLEFac = new SbOLEFactory;
     922          87 :         AddFactory( GetSbData()->pOLEFac );
     923          87 :         GetSbData()->pFormFac = new SbFormFactory;
     924          87 :         AddFactory( GetSbData()->pFormFac );
     925          87 :         GetSbData()->pUnoFac = new SbUnoFactory;
     926          87 :         AddFactory( GetSbData()->pUnoFac );
     927             :     }
     928         774 :     pRtl = new SbiStdObject(OUString(RTLNAME), this );
     929             :     // Search via StarBasic is always global
     930         774 :     SetFlag( SBX_GBLSEARCH );
     931         774 :     pVBAGlobals = NULL;
     932         774 :     bQuit = false;
     933             : 
     934         774 :     if( bDocBasic )
     935             :     {
     936         431 :         lclInsertDocBasicItem( *this );
     937             :     }
     938         774 : }
     939             : 
     940             : // #51727 Override SetModified so that the modified state
     941             : // is not given to the parent
     942        9260 : void StarBASIC::SetModified( sal_Bool b )
     943             : {
     944        9260 :     SbxBase::SetModified( b );
     945        9260 : }
     946             : 
     947        1212 : StarBASIC::~StarBASIC()
     948             : {
     949             :     // Needs to be first action as it can trigger events
     950         303 :     disposeComVariablesForBasic( this );
     951             : 
     952         303 :     if( !--GetSbData()->nInst )
     953             :     {
     954          48 :         RemoveFactory( GetSbData()->pSbFac );
     955          48 :         delete GetSbData()->pSbFac; GetSbData()->pSbFac = NULL;
     956          48 :         RemoveFactory( GetSbData()->pUnoFac );
     957          48 :         delete GetSbData()->pUnoFac; GetSbData()->pUnoFac = NULL;
     958          48 :         RemoveFactory( GetSbData()->pTypeFac );
     959          48 :         delete GetSbData()->pTypeFac; GetSbData()->pTypeFac = NULL;
     960          48 :         RemoveFactory( GetSbData()->pClassFac );
     961          48 :         delete GetSbData()->pClassFac; GetSbData()->pClassFac = NULL;
     962          48 :         RemoveFactory( GetSbData()->pOLEFac );
     963          48 :         delete GetSbData()->pOLEFac; GetSbData()->pOLEFac = NULL;
     964          48 :         RemoveFactory( GetSbData()->pFormFac );
     965          48 :         delete GetSbData()->pFormFac; GetSbData()->pFormFac = NULL;
     966             : 
     967          48 :         if( SbiGlobals::pGlobals )
     968             :         {
     969          48 :             delete SbiGlobals::pGlobals;
     970          48 :             SbiGlobals::pGlobals = 0;
     971             :         }
     972             :     }
     973         255 :     else if( bDocBasic )
     974             :     {
     975           0 :         SbxError eOld = SbxBase::GetError();
     976             : 
     977           0 :         lclRemoveDocBasicItem( *this );
     978             : 
     979           0 :         SbxBase::ResetError();
     980           0 :         if( eOld != SbxERR_OK )
     981             :         {
     982           0 :             SbxBase::SetError( eOld );
     983             :         }
     984             :     }
     985             : 
     986             :     // #100326 Set Parent NULL in registered listeners
     987         303 :     if( xUnoListeners.Is() )
     988             :     {
     989           0 :         sal_uInt16 uCount = xUnoListeners->Count();
     990           0 :         for( sal_uInt16 i = 0 ; i < uCount ; i++ )
     991             :         {
     992           0 :             SbxVariable* pListenerObj = xUnoListeners->Get( i );
     993           0 :             pListenerObj->SetParent( NULL );
     994             :         }
     995           0 :         xUnoListeners = NULL;
     996             :     }
     997             : 
     998         303 :     clearUnoMethodsForBasic( this );
     999         909 : }
    1000             : 
    1001             : // Override new() operator, so that everyone can create a new instance
    1002         774 : void* StarBASIC::operator new( size_t n )
    1003             : {
    1004         774 :     if( n < sizeof( StarBASIC ) )
    1005             :     {
    1006           0 :         n = sizeof( StarBASIC );
    1007             :     }
    1008         774 :     return ::operator new( n );
    1009             : }
    1010             : 
    1011         303 : void StarBASIC::operator delete( void* p )
    1012             : {
    1013         303 :     ::operator delete( p );
    1014         303 : }
    1015             : 
    1016           0 : void StarBASIC::implClearDependingVarsOnDelete( StarBASIC* pDeletedBasic )
    1017             : {
    1018           0 :     if( this != pDeletedBasic )
    1019             :     {
    1020           0 :         for( sal_uInt16 i = 0; i < pModules->Count(); i++ )
    1021             :         {
    1022           0 :             SbModule* p = (SbModule*)pModules->Get( i );
    1023           0 :             p->ClearVarsDependingOnDeletedBasic( pDeletedBasic );
    1024             :         }
    1025             :     }
    1026             : 
    1027           0 :     for( sal_uInt16 nObj = 0; nObj < pObjs->Count(); nObj++ )
    1028             :     {
    1029           0 :         SbxVariable* pVar = pObjs->Get( nObj );
    1030           0 :         StarBASIC* pBasic = PTR_CAST(StarBASIC,pVar);
    1031           0 :         if( pBasic && pBasic != pDeletedBasic )
    1032             :         {
    1033           0 :             pBasic->implClearDependingVarsOnDelete( pDeletedBasic );
    1034             :         }
    1035             :     }
    1036           0 : }
    1037             : 
    1038             : 
    1039             : /**************************************************************************
    1040             : *
    1041             : *    Creation/Managment of modules
    1042             : *
    1043             : **************************************************************************/
    1044             : 
    1045          24 : SbModule* StarBASIC::MakeModule( const OUString& rName, const OUString& rSrc )
    1046             : {
    1047          24 :     return MakeModule32( rName, rSrc );
    1048             : }
    1049             : 
    1050          62 : SbModule* StarBASIC::MakeModule32( const OUString& rName, const OUString& rSrc )
    1051             : {
    1052          62 :     ModuleInfo mInfo;
    1053          62 :     mInfo.ModuleType = ModuleType::NORMAL;
    1054          62 :     return MakeModule32(  rName, mInfo, rSrc );
    1055             : }
    1056         210 : SbModule* StarBASIC::MakeModule32( const OUString& rName, const ModuleInfo& mInfo, const OUString& rSrc )
    1057             : {
    1058             : 
    1059             :     OSL_TRACE("create module %s type mInfo %d", OUStringToOString( rName, RTL_TEXTENCODING_UTF8 ).getStr(), mInfo.ModuleType );
    1060         210 :     SbModule* p = NULL;
    1061         210 :     switch ( mInfo.ModuleType )
    1062             :     {
    1063             :     case ModuleType::DOCUMENT:
    1064             :         // In theory we should be able to create Object modules
    1065             :         // in ordinary basic ( in vba mode thought these are create
    1066             :         // by the application/basic and not by the user )
    1067         118 :         p = new SbObjModule( rName, mInfo, isVBAEnabled() );
    1068         118 :         break;
    1069             :     case ModuleType::CLASS:
    1070           0 :         p = new SbModule( rName, isVBAEnabled() );
    1071           0 :         p->SetModuleType( ModuleType::CLASS );
    1072           0 :         break;
    1073             :     case ModuleType::FORM:
    1074           2 :         p = new SbUserFormModule( rName, mInfo, isVBAEnabled() );
    1075           2 :         break;
    1076             :     default:
    1077          90 :         p = new SbModule( rName, isVBAEnabled() );
    1078          90 :         break;
    1079             :     }
    1080         210 :     p->SetSource32( rSrc );
    1081         210 :     p->SetParent( this );
    1082         210 :     pModules->Insert( p, pModules->Count() );
    1083         210 :     SetModified( sal_True );
    1084         210 :     return p;
    1085             : }
    1086             : 
    1087        3953 : void StarBASIC::Insert( SbxVariable* pVar )
    1088             : {
    1089        3953 :     if( pVar->IsA( TYPE(SbModule) ) )
    1090             :     {
    1091           0 :         pModules->Insert( pVar, pModules->Count() );
    1092           0 :         pVar->SetParent( this );
    1093           0 :         StartListening( pVar->GetBroadcaster(), sal_True );
    1094             :     }
    1095             :     else
    1096             :     {
    1097        3953 :         sal_Bool bWasModified = IsModified();
    1098        3953 :         SbxObject::Insert( pVar );
    1099        3953 :         if( !bWasModified && pVar->IsSet( SBX_DONTSTORE ) )
    1100             :         {
    1101        2318 :             SetModified( sal_False );
    1102             :         }
    1103             :     }
    1104        3953 : }
    1105             : 
    1106           0 : void StarBASIC::Remove( SbxVariable* pVar )
    1107             : {
    1108           0 :     if( pVar->IsA( TYPE(SbModule) ) )
    1109             :     {
    1110             :         // #87540 Can be last reference!
    1111           0 :         SbxVariableRef xVar = pVar;
    1112           0 :         pModules->Remove( pVar );
    1113           0 :         pVar->SetParent( 0 );
    1114           0 :         EndListening( pVar->GetBroadcaster() );
    1115             :     }
    1116             :     else
    1117             :     {
    1118           0 :         SbxObject::Remove( pVar );
    1119             :     }
    1120           0 : }
    1121             : 
    1122           0 : sal_Bool StarBASIC::Compile( SbModule* pMod )
    1123             : {
    1124           0 :     return pMod ? pMod->Compile() : sal_False;
    1125             : }
    1126             : 
    1127           0 : void StarBASIC::Clear()
    1128             : {
    1129           0 :     while( pModules->Count() )
    1130             :     {
    1131           0 :         pModules->Remove( pModules->Count() - 1 );
    1132             :     }
    1133           0 : }
    1134             : 
    1135        3619 : SbModule* StarBASIC::FindModule( const OUString& rName )
    1136             : {
    1137        8294 :     for( sal_uInt16 i = 0; i < pModules->Count(); i++ )
    1138             :     {
    1139        8146 :         SbModule* p = (SbModule*) pModules->Get( i );
    1140        8146 :         if( p->GetName().equalsIgnoreAsciiCase( rName ) )
    1141             :         {
    1142        3471 :             return p;
    1143             :         }
    1144             :     }
    1145         148 :     return NULL;
    1146             : }
    1147             : 
    1148             : 
    1149             : struct ClassModuleRunInitItem
    1150             : {
    1151             :     SbModule*       m_pModule;
    1152             :     bool            m_bProcessing;
    1153             :     bool            m_bRunInitDone;
    1154             : 
    1155           0 :     ClassModuleRunInitItem( void )
    1156             :         : m_pModule( NULL )
    1157             :         , m_bProcessing( false )
    1158           0 :         , m_bRunInitDone( false )
    1159           0 :     {}
    1160           0 :     ClassModuleRunInitItem( SbModule* pModule )
    1161             :         : m_pModule( pModule )
    1162             :         , m_bProcessing( false )
    1163           0 :         , m_bRunInitDone( false )
    1164           0 :     {}
    1165             : };
    1166             : 
    1167             : // Derive from unordered_map type instead of typedef
    1168             : // to allow forward declaration in sbmod.hxx
    1169         618 : class ModuleInitDependencyMap : public
    1170             :     boost::unordered_map< OUString, ClassModuleRunInitItem,
    1171             :                           OUStringHash, ::std::equal_to< OUString > >
    1172             : {};
    1173             : 
    1174           0 : void SbModule::implProcessModuleRunInit( ModuleInitDependencyMap& rMap, ClassModuleRunInitItem& rItem )
    1175             : {
    1176           0 :     rItem.m_bProcessing = true;
    1177             : 
    1178           0 :     SbModule* pModule = rItem.m_pModule;
    1179           0 :     if( pModule->pClassData != NULL )
    1180             :     {
    1181           0 :         StringVector& rReqTypes = pModule->pClassData->maRequiredTypes;
    1182           0 :         if( rReqTypes.size() > 0 )
    1183             :         {
    1184           0 :             for( StringVector::iterator it = rReqTypes.begin() ; it != rReqTypes.end() ; ++it )
    1185             :             {
    1186           0 :                 OUString& rStr = *it;
    1187             : 
    1188             :                 // Is required type a class module?
    1189           0 :                 ModuleInitDependencyMap::iterator itFind = rMap.find( rStr );
    1190           0 :                 if( itFind != rMap.end() )
    1191             :                 {
    1192           0 :                     ClassModuleRunInitItem& rParentItem = itFind->second;
    1193           0 :                     if( rParentItem.m_bProcessing )
    1194             :                     {
    1195             :                         // TODO: raise error?
    1196             :                         OSL_FAIL( "Cyclic module dependency detected" );
    1197           0 :                         continue;
    1198             :                     }
    1199             : 
    1200           0 :                     if( !rParentItem.m_bRunInitDone )
    1201             :                     {
    1202           0 :                         implProcessModuleRunInit( rMap, rParentItem );
    1203             :                     }
    1204             :                 }
    1205             :             }
    1206             :         }
    1207             :     }
    1208             : 
    1209           0 :     pModule->RunInit();
    1210           0 :     rItem.m_bRunInitDone = true;
    1211           0 :     rItem.m_bProcessing = false;
    1212           0 : }
    1213             : 
    1214             : // Run Init-Code of all modules (including inserted libraries)
    1215         309 : void StarBASIC::InitAllModules( StarBASIC* pBasicNotToInit )
    1216             : {
    1217         309 :     SolarMutexGuard guard;
    1218             : 
    1219             :     // Init own modules
    1220         741 :     for ( sal_uInt16 nMod = 0; nMod < pModules->Count(); nMod++ )
    1221             :     {
    1222         432 :         SbModule* pModule = (SbModule*)pModules->Get( nMod );
    1223         432 :         if( !pModule->IsCompiled() )
    1224             :         {
    1225         110 :             pModule->Compile();
    1226             :         }
    1227             :     }
    1228             :     // compile modules first then RunInit ( otherwise there is
    1229             :     // can be order dependency, e.g. classmodule A has a member
    1230             :     // of of type classmodule B and classmodule B hasn't been compiled yet )
    1231             : 
    1232             :     // Consider required types to init in right order. Class modules
    1233             :     // that are required by other modules have to be initialized first.
    1234         618 :     ModuleInitDependencyMap aMIDMap;
    1235         741 :     for ( sal_uInt16 nMod = 0; nMod < pModules->Count(); nMod++ )
    1236             :     {
    1237         432 :         SbModule* pModule = (SbModule*)pModules->Get( nMod );
    1238         432 :         OUString aModuleName = pModule->GetName();
    1239         432 :         if( pModule->isProxyModule() )
    1240             :         {
    1241           0 :             aMIDMap[aModuleName] = ClassModuleRunInitItem( pModule );
    1242             :         }
    1243         432 :     }
    1244             : 
    1245         309 :     ModuleInitDependencyMap::iterator it;
    1246         309 :     for( it = aMIDMap.begin() ; it != aMIDMap.end(); ++it )
    1247             :     {
    1248           0 :         ClassModuleRunInitItem& rItem = it->second;
    1249           0 :         SbModule::implProcessModuleRunInit( aMIDMap, rItem );
    1250             :     }
    1251             : 
    1252             :     // Call RunInit on standard modules
    1253         741 :     for ( sal_uInt16 nMod = 0; nMod < pModules->Count(); nMod++ )
    1254             :     {
    1255         432 :         SbModule* pModule = (SbModule*)pModules->Get( nMod );
    1256         432 :         if( !pModule->isProxyModule() )
    1257             :         {
    1258         432 :             pModule->RunInit();
    1259             :         }
    1260             :     }
    1261             : 
    1262             :     // Check all objects if they are BASIC,
    1263             :     // if yes initialize
    1264        1123 :     for ( sal_uInt16 nObj = 0; nObj < pObjs->Count(); nObj++ )
    1265             :     {
    1266         814 :         SbxVariable* pVar = pObjs->Get( nObj );
    1267         814 :         StarBASIC* pBasic = PTR_CAST(StarBASIC,pVar);
    1268         814 :         if( pBasic && pBasic != pBasicNotToInit )
    1269             :         {
    1270          56 :             pBasic->InitAllModules();
    1271             :         }
    1272         309 :     }
    1273         309 : }
    1274             : 
    1275             : // #88329 Put modules back to not initialised state to
    1276             : // force reinitialisation at next start
    1277         145 : void StarBASIC::DeInitAllModules( void )
    1278             : {
    1279             :     // Deinit own modules
    1280         464 :     for ( sal_uInt16 nMod = 0; nMod < pModules->Count(); nMod++ )
    1281             :     {
    1282         319 :         SbModule* pModule = (SbModule*)pModules->Get( nMod );
    1283         319 :         if( pModule->pImage && !pModule->isProxyModule() && !pModule->ISA(SbObjModule) )
    1284             :         {
    1285         111 :             pModule->pImage->bInit = false;
    1286             :         }
    1287             :     }
    1288             : 
    1289         307 :     for ( sal_uInt16 nObj = 0; nObj < pObjs->Count(); nObj++ )
    1290             :     {
    1291         162 :         SbxVariable* pVar = pObjs->Get( nObj );
    1292         162 :         StarBASIC* pBasic = PTR_CAST(StarBASIC,pVar);
    1293         162 :         if( pBasic )
    1294             :         {
    1295          37 :             pBasic->DeInitAllModules();
    1296             :         }
    1297             :     }
    1298         145 : }
    1299             : 
    1300             : // This implementation at first searches within the runtime library,
    1301             : // then it looks for an element within one module. This moudle can be
    1302             : // a public var or an entrypoint. If it is not found and we look for a
    1303             : // method and a module with the given name is found the search continues
    1304             : // for entrypoint "Main".
    1305             : // If this fails again a conventional search over objects is performend.
    1306       33380 : SbxVariable* StarBASIC::Find( const OUString& rName, SbxClassType t )
    1307             : {
    1308       33380 :     SbxVariable* pRes = NULL;
    1309       33380 :     SbModule* pNamed = NULL;
    1310             :     // "Extended" search in Runtime Lib
    1311             :     // but only if SbiRuntime has not set the flag
    1312       33380 :     if( !bNoRtl )
    1313             :     {
    1314       30252 :         if( t == SbxCLASS_DONTCARE || t == SbxCLASS_OBJECT )
    1315             :         {
    1316       28536 :             if( rName.equalsIgnoreAsciiCase( RTLNAME ) )
    1317             :             {
    1318           0 :                 pRes = pRtl;
    1319             :             }
    1320             :         }
    1321       30252 :         if( !pRes )
    1322             :         {
    1323       30252 :             pRes = ((SbiStdObject*) (SbxObject*) pRtl)->Find( rName, t );
    1324             :         }
    1325       30252 :         if( pRes )
    1326             :         {
    1327           1 :             pRes->SetFlag( SBX_EXTFOUND );
    1328             :         }
    1329             :     }
    1330             :     // Search module
    1331       33380 :     if( !pRes )
    1332             :     {
    1333       52180 :         for( sal_uInt16 i = 0; i < pModules->Count(); i++ )
    1334             :         {
    1335       19056 :             SbModule* p = (SbModule*) pModules->Get( i );
    1336       19056 :             if( p->IsVisible() )
    1337             :             {
    1338             :                 // Remember modul fpr Main() call
    1339             :                 // or is the name equal?!?
    1340       19056 :                 if( p->GetName().equalsIgnoreAsciiCase( rName ) )
    1341             :                 {
    1342         142 :                     if( t == SbxCLASS_OBJECT || t == SbxCLASS_DONTCARE )
    1343             :                     {
    1344         142 :                         pRes = p; break;
    1345             :                     }
    1346           0 :                     pNamed = p;
    1347             :                 }
    1348             :                 // Only variables qualified by the Module Name e.g. Sheet1.foo
    1349             :                 // should work for Documant && Class type Modules
    1350       18914 :                 sal_Int32 nType = p->GetModuleType();
    1351       18914 :                 if ( nType == ModuleType::DOCUMENT || nType == ModuleType::FORM )
    1352             :                 {
    1353        3063 :                     continue;
    1354             :                 }
    1355             :                 // otherwise check if the element is available
    1356             :                 // unset GBLSEARCH-Flag (due to Rekursion)
    1357       15851 :                 sal_uInt16 nGblFlag = p->GetFlags() & SBX_GBLSEARCH;
    1358       15851 :                 p->ResetFlag( SBX_GBLSEARCH );
    1359       15851 :                 pRes = p->Find( rName, t );
    1360       15851 :                 p->SetFlag( nGblFlag );
    1361       15851 :                 if( pRes )
    1362             :                 {
    1363         113 :                     break;
    1364             :                 }
    1365             :             }
    1366             :         }
    1367             :     }
    1368       33380 :     OUString aMainStr("Main");
    1369       33380 :     if( !pRes && pNamed && ( t == SbxCLASS_METHOD || t == SbxCLASS_DONTCARE ) &&
    1370           0 :         !pNamed->GetName().equalsIgnoreAsciiCase( aMainStr ) )
    1371             :     {
    1372           0 :         pRes = pNamed->Find( aMainStr, SbxCLASS_METHOD );
    1373             :     }
    1374       33380 :     if( !pRes )
    1375             :     {
    1376       33124 :         pRes = SbxObject::Find( rName, t );
    1377             :     }
    1378       33380 :     return pRes;
    1379             : }
    1380             : 
    1381          18 : sal_Bool StarBASIC::Call( const OUString& rName, SbxArray* pParam )
    1382             : {
    1383          18 :     sal_Bool bRes = SbxObject::Call( rName, pParam );
    1384          18 :     if( !bRes )
    1385             :     {
    1386           0 :         SbxError eErr = SbxBase::GetError();
    1387           0 :         SbxBase::ResetError();
    1388           0 :         if( eErr != SbxERR_OK )
    1389             :         {
    1390           0 :             RTError( (SbError)eErr, 0, 0, 0 );
    1391             :         }
    1392             :     }
    1393          18 :     return bRes;
    1394             : }
    1395             : 
    1396             : // Find method via name (e.g. query via BASIC IDE)
    1397           0 : SbxBase* StarBASIC::FindSBXInCurrentScope( const OUString& rName )
    1398             : {
    1399           0 :     if( !GetSbData()->pInst )
    1400             :     {
    1401           0 :         return NULL;
    1402             :     }
    1403           0 :     if( !GetSbData()->pInst->pRun )
    1404             :     {
    1405           0 :         return NULL;
    1406             :     }
    1407           0 :     return GetSbData()->pInst->pRun->FindElementExtern( rName );
    1408             : }
    1409             : 
    1410           0 : void StarBASIC::QuitAndExitApplication()
    1411             : {
    1412           0 :     Stop();
    1413           0 :     bQuit = true;
    1414           0 : }
    1415             : 
    1416          83 : void StarBASIC::Stop()
    1417             : {
    1418          83 :     SbiInstance* p = GetSbData()->pInst;
    1419         166 :     while( p )
    1420             :     {
    1421           0 :         p->Stop();
    1422           0 :         p = p->pNext;
    1423             :     }
    1424          83 : }
    1425             : 
    1426           0 : bool StarBASIC::IsRunning()
    1427             : {
    1428           0 :     return GetSbData()->pInst != NULL;
    1429             : }
    1430             : 
    1431             : /**************************************************************************
    1432             : *
    1433             : *    Debugging and error handling
    1434             : *
    1435             : **************************************************************************/
    1436             : 
    1437           0 : SbMethod* StarBASIC::GetActiveMethod( sal_uInt16 nLevel )
    1438             : {
    1439           0 :     if( GetSbData()->pInst )
    1440             :     {
    1441           0 :         return GetSbData()->pInst->GetCaller( nLevel );
    1442             :     }
    1443             :     else
    1444             :     {
    1445           0 :         return NULL;
    1446             :     }
    1447             : }
    1448             : 
    1449           0 : SbModule* StarBASIC::GetActiveModule()
    1450             : {
    1451           0 :     if( GetSbData()->pInst && !IsCompilerError() )
    1452             :     {
    1453           0 :         return GetSbData()->pInst->GetActiveModule();
    1454             :     }
    1455             :     else
    1456             :     {
    1457           0 :         return GetSbData()->pCompMod;
    1458             :     }
    1459             : }
    1460             : 
    1461           0 : sal_uInt16 StarBASIC::BreakPoint( sal_Int32 l, sal_Int32 c1, sal_Int32 c2 )
    1462             : {
    1463           0 :     SetErrorData( 0, l, c1, c2 );
    1464           0 :     bBreak = true;
    1465           0 :     if( GetSbData()->aBreakHdl.IsSet() )
    1466             :     {
    1467           0 :         return (sal_uInt16) GetSbData()->aBreakHdl.Call( this );
    1468             :     }
    1469             :     else
    1470             :     {
    1471           0 :         return BreakHdl();
    1472             :     }
    1473             : }
    1474             : 
    1475           0 : sal_uInt16 StarBASIC::StepPoint( sal_Int32 l, sal_Int32 c1, sal_Int32 c2 )
    1476             : {
    1477           0 :     SetErrorData( 0, l, c1, c2 );
    1478           0 :     bBreak = false;
    1479           0 :     if( GetSbData()->aBreakHdl.IsSet() )
    1480             :     {
    1481           0 :         return (sal_uInt16) GetSbData()->aBreakHdl.Call( this );
    1482             :     }
    1483             :     else
    1484             :     {
    1485           0 :         return BreakHdl();
    1486             :     }
    1487             : }
    1488             : 
    1489           0 : sal_uInt16 StarBASIC::BreakHdl()
    1490             : {
    1491           0 :     return (sal_uInt16) ( aBreakHdl.IsSet() ? aBreakHdl.Call( this ) : SbDEBUG_CONTINUE );
    1492             : }
    1493             : 
    1494             : // Calls for error handler and break handler
    1495           0 : sal_uInt16 StarBASIC::GetLine()     { return GetSbData()->nLine; }
    1496           0 : sal_uInt16 StarBASIC::GetCol1()     { return GetSbData()->nCol1; }
    1497           0 : sal_uInt16 StarBASIC::GetCol2()     { return GetSbData()->nCol2; }
    1498             : 
    1499             : // Specific to error handler
    1500           0 : SbError StarBASIC::GetErrorCode()       { return GetSbData()->nCode; }
    1501          19 : const OUString& StarBASIC::GetErrorText() { return GetSbData()->aErrMsg; }
    1502           0 : bool StarBASIC::IsCompilerError()       { return GetSbData()->bCompiler; }
    1503             : 
    1504             : // From 1996-03-29:
    1505             : // The mapping between the old and the new error codes take place by searching
    1506             : // through the table SFX_VB_ErrorTab[]. This is indeed not with good performance,
    1507             : // but it consumes much less memory than corresponding switch blocs.
    1508             : // Because the conversion of error codes has not to be fast. there is no
    1509             : // binary search by VB Error -> Error SFX.
    1510             : 
    1511             : // Map back new error codes to old, Sbx-compatible
    1512          41 : sal_uInt16 StarBASIC::GetVBErrorCode( SbError nError )
    1513             : {
    1514          41 :     sal_uInt16 nRet = 0;
    1515             : 
    1516          41 :     if( SbiRuntime::isVBAEnabled() )
    1517             :     {
    1518          41 :         switch( nError )
    1519             :         {
    1520             :         case SbERR_BASIC_ARRAY_FIX:
    1521           0 :             return 10;
    1522             :         case SbERR_BASIC_STRING_OVERFLOW:
    1523           0 :             return 14;
    1524             :         case SbERR_BASIC_EXPR_TOO_COMPLEX:
    1525           0 :             return 16;
    1526             :         case SbERR_BASIC_OPER_NOT_PERFORM:
    1527           0 :             return 17;
    1528             :         case SbERR_BASIC_TOO_MANY_DLL:
    1529           0 :             return 47;
    1530             :         case SbERR_BASIC_LOOP_NOT_INIT:
    1531           0 :             return 92;
    1532             :         default:
    1533          41 :             nRet = 0;
    1534             :         }
    1535             :     }
    1536             : 
    1537             :     // search loop
    1538             :     const SFX_VB_ErrorItem* pErrItem;
    1539          41 :     sal_uInt16 nIndex = 0;
    1540         570 :     do
    1541             :     {
    1542         611 :         pErrItem = SFX_VB_ErrorTab + nIndex;
    1543         611 :         if( pErrItem->nErrorSFX == nError )
    1544             :         {
    1545          41 :             nRet = pErrItem->nErrorVB;
    1546          41 :             break;
    1547             :         }
    1548         570 :         nIndex++;
    1549             :     }
    1550         570 :     while( pErrItem->nErrorVB != 0xFFFF );      // up to end mark
    1551          41 :     return nRet;
    1552             : }
    1553             : 
    1554           0 : SbError StarBASIC::GetSfxFromVBError( sal_uInt16 nError )
    1555             : {
    1556           0 :     SbError nRet = 0L;
    1557             : 
    1558           0 :     if( SbiRuntime::isVBAEnabled() )
    1559             :     {
    1560           0 :         switch( nError )
    1561             :         {
    1562             :         case 1:
    1563             :         case 2:
    1564             :         case 4:
    1565             :         case 8:
    1566             :         case 12:
    1567             :         case 73:
    1568           0 :             return 0L;
    1569             :         case 10:
    1570           0 :             return SbERR_BASIC_ARRAY_FIX;
    1571             :         case 14:
    1572           0 :             return SbERR_BASIC_STRING_OVERFLOW;
    1573             :         case 16:
    1574           0 :             return SbERR_BASIC_EXPR_TOO_COMPLEX;
    1575             :         case 17:
    1576           0 :             return SbERR_BASIC_OPER_NOT_PERFORM;
    1577             :         case 47:
    1578           0 :             return SbERR_BASIC_TOO_MANY_DLL;
    1579             :         case 92:
    1580           0 :             return SbERR_BASIC_LOOP_NOT_INIT;
    1581             :         default:
    1582           0 :             nRet = 0L;
    1583             :         }
    1584             :     }
    1585             :     const SFX_VB_ErrorItem* pErrItem;
    1586           0 :     sal_uInt16 nIndex = 0;
    1587           0 :     do
    1588             :     {
    1589           0 :         pErrItem = SFX_VB_ErrorTab + nIndex;
    1590           0 :         if( pErrItem->nErrorVB == nError )
    1591             :         {
    1592           0 :             nRet = pErrItem->nErrorSFX;
    1593           0 :             break;
    1594             :         }
    1595           0 :         else if( pErrItem->nErrorVB > nError )
    1596             :         {
    1597           0 :             break;              // couldn't found anymore
    1598             :         }
    1599           0 :         nIndex++;
    1600             :     }
    1601           0 :     while( pErrItem->nErrorVB != 0xFFFF );      // up to end mark
    1602           0 :     return nRet;
    1603             : }
    1604             : 
    1605             : // set Error- / Break-data
    1606           0 : void StarBASIC::SetErrorData( SbError nCode, sal_uInt16 nLine,
    1607             :                               sal_uInt16 nCol1, sal_uInt16 nCol2 )
    1608             : {
    1609           0 :     SbiGlobals& aGlobals = *GetSbData();
    1610           0 :     aGlobals.nCode = nCode;
    1611           0 :     aGlobals.nLine = nLine;
    1612           0 :     aGlobals.nCol1 = nCol1;
    1613           0 :     aGlobals.nCol2 = nCol2;
    1614           0 : }
    1615             : 
    1616             : //----------------------------------------------------------------
    1617             : // help class for access to string SubResource of a Resource.
    1618             : // Source: sfx2\source\doc\docfile.cxx (TLX)
    1619             : struct BasicStringList_Impl : private Resource
    1620             : {
    1621             :     ResId aResId;
    1622             : 
    1623          19 :     BasicStringList_Impl( ResId& rErrIdP,  sal_uInt16 nId)
    1624          19 :         : Resource( rErrIdP ),aResId(nId, *rErrIdP.GetResMgr() ){}
    1625          19 :     ~BasicStringList_Impl() { FreeResource(); }
    1626             : 
    1627          38 :     OUString GetString(){ return aResId.toString(); }
    1628          19 :     sal_Bool IsErrorTextAvailable( void )
    1629          19 :         { return IsAvailableRes(aResId.SetRT(RSC_STRING)); }
    1630             : };
    1631             : //----------------------------------------------------------------
    1632             : 
    1633          19 : void StarBASIC::MakeErrorText( SbError nId, const OUString& aMsg )
    1634             : {
    1635          19 :     SolarMutexGuard aSolarGuard;
    1636          19 :     sal_uInt16 nOldID = GetVBErrorCode( nId );
    1637             : 
    1638             :     // intantiate the help class
    1639          19 :     BasResId aId( RID_BASIC_START );
    1640          38 :     BasicStringList_Impl aMyStringList( aId, sal_uInt16(nId & ERRCODE_RES_MASK) );
    1641             : 
    1642          19 :     if( aMyStringList.IsErrorTextAvailable() )
    1643             :     {
    1644             :         // merge message with additional text
    1645          19 :         OUStringBuffer aMsg1(aMyStringList.GetString());
    1646             :         // replace argument placeholder with %s
    1647          38 :         OUString aSrgStr( "$(ARG1)" );
    1648          19 :         sal_Int32 nResult = aMyStringList.GetString().indexOf( aSrgStr );
    1649             : 
    1650          19 :         if( nResult >= 0 )
    1651             :         {
    1652           0 :             aMsg1.remove(nResult, aSrgStr.getLength());
    1653           0 :             aMsg1.insert(nResult, aMsg);
    1654             :         }
    1655          38 :         GetSbData()->aErrMsg = aMsg1.makeStringAndClear();
    1656             :     }
    1657           0 :     else if( nOldID != 0 )
    1658             :     {
    1659           0 :         OUString aStdMsg = "Fehler " + OUString::number(nOldID) +
    1660           0 :                            ": Kein Fehlertext verfuegbar!";
    1661           0 :         GetSbData()->aErrMsg = aStdMsg;
    1662             :     }
    1663             :     else
    1664             :     {
    1665           0 :         GetSbData()->aErrMsg = "";
    1666          19 :     }
    1667          19 : }
    1668             : 
    1669           0 : sal_Bool StarBASIC::CError( SbError code, const OUString& rMsg,
    1670             :                             sal_Int32 l, sal_Int32 c1, sal_Int32 c2 )
    1671             : {
    1672           0 :     SolarMutexGuard aSolarGuard;
    1673             : 
    1674             :     // compiler error during runtime -> stop programm
    1675           0 :     if( IsRunning() )
    1676             :     {
    1677             :         // #109018 Check if running Basic is affected
    1678           0 :         StarBASIC* pStartedBasic = GetSbData()->pInst->GetBasic();
    1679           0 :         if( pStartedBasic != this )
    1680             :         {
    1681           0 :             return sal_False;
    1682             :         }
    1683           0 :         Stop();
    1684             :     }
    1685             : 
    1686             :     // set flag, so that GlobalRunInit notice the error
    1687           0 :     GetSbData()->bGlobalInitErr = true;
    1688             : 
    1689             :     // tinker the error message
    1690           0 :     MakeErrorText( code, rMsg );
    1691             : 
    1692             :     // Implementation of the code for the string transport to SFX-Error
    1693           0 :     if( !rMsg.isEmpty() )
    1694             :     {
    1695           0 :         code = (sal_uIntPtr)*new StringErrorInfo( code, rMsg );
    1696             :     }
    1697           0 :     SetErrorData( code, l, c1, c2 );
    1698           0 :     GetSbData()->bCompiler = true;
    1699             :     sal_Bool bRet;
    1700           0 :     if( GetSbData()->aErrHdl.IsSet() )
    1701             :     {
    1702           0 :         bRet = (sal_Bool) GetSbData()->aErrHdl.Call( this );
    1703             :     }
    1704             :     else
    1705             :     {
    1706           0 :         bRet = ErrorHdl();
    1707             :     }
    1708           0 :     GetSbData()->bCompiler = false;     // only true for error handler
    1709           0 :     return bRet;
    1710             : }
    1711             : 
    1712           0 : sal_Bool StarBASIC::RTError( SbError code, sal_Int32 l, sal_Int32 c1, sal_Int32 c2 )
    1713             : {
    1714           0 :     return RTError( code, OUString(), l, c1, c2 );
    1715             : }
    1716             : 
    1717           0 : sal_Bool StarBASIC::RTError( SbError code, const OUString& rMsg, sal_Int32 l, sal_Int32 c1, sal_Int32 c2 )
    1718             : {
    1719           0 :     SolarMutexGuard aSolarGuard;
    1720             : 
    1721           0 :     SbError c = code;
    1722           0 :     if( (c & ERRCODE_CLASS_MASK) == ERRCODE_CLASS_COMPILER )
    1723             :     {
    1724           0 :         c = 0;
    1725             :     }
    1726           0 :     MakeErrorText( c, rMsg );
    1727             : 
    1728             :     // Implementation of the code for the string transport to SFX-Error
    1729           0 :     if( !rMsg.isEmpty() )
    1730             :     {
    1731             :         // very confusing, even though MakeErrorText sets up the error text
    1732             :         // seems that this is not used ( if rMsg already has content )
    1733             :         // In the case of VBA MakeErrorText also formats the error to be alittle more
    1734             :         // like vba ( adds an error number etc )
    1735           0 :         if ( SbiRuntime::isVBAEnabled() && ( code == SbERR_BASIC_COMPAT ) )
    1736             :         {
    1737           0 :             OUString aTmp = "\'" + OUString::valueOf(SbxErrObject::getUnoErrObject()->getNumber()) +
    1738           0 :                             "\'\n" + OUString(!GetSbData()->aErrMsg.isEmpty() ? GetSbData()->aErrMsg : rMsg);
    1739           0 :             code = (sal_uIntPtr)*new StringErrorInfo( code, aTmp );
    1740             :         }
    1741             :         else
    1742             :         {
    1743           0 :             code = (sal_uIntPtr)*new StringErrorInfo( code, rMsg );
    1744             :         }
    1745             :     }
    1746             : 
    1747           0 :     SetErrorData( code, l, c1, c2 );
    1748           0 :     if( GetSbData()->aErrHdl.IsSet() )
    1749             :     {
    1750           0 :         return (sal_Bool) GetSbData()->aErrHdl.Call( this );
    1751             :     }
    1752             :     else
    1753             :     {
    1754           0 :         return ErrorHdl();
    1755           0 :     }
    1756             : }
    1757             : 
    1758           0 : void StarBASIC::Error( SbError n )
    1759             : {
    1760           0 :     Error( n, OUString() );
    1761           0 : }
    1762             : 
    1763           3 : void StarBASIC::Error( SbError n, const OUString& rMsg )
    1764             : {
    1765           3 :     if( GetSbData()->pInst )
    1766             :     {
    1767           3 :         GetSbData()->pInst->Error( n, rMsg );
    1768             :     }
    1769           3 : }
    1770             : 
    1771           0 : void StarBASIC::FatalError( SbError n )
    1772             : {
    1773           0 :     if( GetSbData()->pInst )
    1774             :     {
    1775           0 :         GetSbData()->pInst->FatalError( n );
    1776             :     }
    1777           0 : }
    1778             : 
    1779           0 : void StarBASIC::FatalError( SbError _errCode, const OUString& _details )
    1780             : {
    1781           0 :     if( GetSbData()->pInst )
    1782             :     {
    1783           0 :         GetSbData()->pInst->FatalError( _errCode, _details );
    1784             :     }
    1785           0 : }
    1786             : 
    1787           0 : SbError StarBASIC::GetErrBasic()
    1788             : {
    1789           0 :     if( GetSbData()->pInst )
    1790             :     {
    1791           0 :         return GetSbData()->pInst->GetErr();
    1792             :     }
    1793             :     else
    1794             :     {
    1795           0 :         return 0;
    1796             :     }
    1797             : }
    1798             : 
    1799             : // make the additional message for the RTL function error accessible
    1800           0 : OUString StarBASIC::GetErrorMsg()
    1801             : {
    1802           0 :     if( GetSbData()->pInst )
    1803             :     {
    1804           0 :         return GetSbData()->pInst->GetErrorMsg();
    1805             :     }
    1806             :     else
    1807             :     {
    1808           0 :         return OUString();
    1809             :     }
    1810             : }
    1811             : 
    1812           0 : sal_Int32 StarBASIC::GetErl()
    1813             : {
    1814           0 :     if( GetSbData()->pInst )
    1815             :     {
    1816           0 :         return GetSbData()->pInst->GetErl();
    1817             :     }
    1818             :     else
    1819             :     {
    1820           0 :         return 0;
    1821             :     }
    1822             : }
    1823             : 
    1824           0 : sal_Bool StarBASIC::ErrorHdl()
    1825             : {
    1826           0 :     return (sal_Bool) ( aErrorHdl.IsSet()
    1827           0 :         ? aErrorHdl.Call( this ) : sal_False );
    1828             : }
    1829             : 
    1830           0 : Link StarBASIC::GetGlobalErrorHdl()
    1831             : {
    1832           0 :     return GetSbData()->aErrHdl;
    1833             : }
    1834             : 
    1835         157 : void StarBASIC::SetGlobalErrorHdl( const Link& rLink )
    1836             : {
    1837         157 :     GetSbData()->aErrHdl = rLink;
    1838         157 : }
    1839             : 
    1840           0 : void StarBASIC::SetGlobalBreakHdl( const Link& rLink )
    1841             : {
    1842           0 :     GetSbData()->aBreakHdl = rLink;
    1843           0 : }
    1844             : 
    1845           6 : SbxArrayRef StarBASIC::getUnoListeners( void )
    1846             : {
    1847           6 :     if( !xUnoListeners.Is() )
    1848             :     {
    1849           1 :         xUnoListeners = new SbxArray();
    1850             :     }
    1851           6 :     return xUnoListeners;
    1852             : }
    1853             : 
    1854             : 
    1855             : /**************************************************************************
    1856             : *
    1857             : *   load and save
    1858             : *
    1859             : **************************************************************************/
    1860             : 
    1861           0 : sal_Bool StarBASIC::LoadData( SvStream& r, sal_uInt16 nVer )
    1862             : {
    1863           0 :     if( !SbxObject::LoadData( r, nVer ) )
    1864             :     {
    1865           0 :         return sal_False;
    1866             :     }
    1867             :     // #95459 Delete dialogs, otherwise endless recursion
    1868             :     // in SbxVarable::GetType() if dialogs are accessed
    1869           0 :     sal_uInt16 nObjCount = pObjs->Count();
    1870           0 :     SbxVariable** ppDeleteTab = new SbxVariable*[ nObjCount ];
    1871             :     sal_uInt16 nObj;
    1872             : 
    1873           0 :     for( nObj = 0 ; nObj < nObjCount ; nObj++ )
    1874             :     {
    1875           0 :         SbxVariable* pVar = pObjs->Get( nObj );
    1876           0 :         StarBASIC* pBasic = PTR_CAST( StarBASIC, pVar );
    1877           0 :         ppDeleteTab[nObj] = pBasic ? NULL : pVar;
    1878             :     }
    1879           0 :     for( nObj = 0 ; nObj < nObjCount ; nObj++ )
    1880             :     {
    1881           0 :         SbxVariable* pVar = ppDeleteTab[nObj];
    1882           0 :         if( pVar )
    1883             :         {
    1884           0 :             pObjs->Remove( pVar );
    1885             :         }
    1886             :     }
    1887           0 :     delete[] ppDeleteTab;
    1888             : 
    1889             :     sal_uInt16 nMod;
    1890           0 :     pModules->Clear();
    1891           0 :     r >> nMod;
    1892           0 :     for( sal_uInt16 i = 0; i < nMod; i++ )
    1893             :     {
    1894           0 :         SbModule* pMod = (SbModule*) SbxBase::Load( r );
    1895           0 :         if( !pMod )
    1896             :         {
    1897           0 :             return sal_False;
    1898             :         }
    1899           0 :         else if( pMod->ISA(SbJScriptModule) )
    1900             :         {
    1901             :             // assign Ref, so that pMod will be deleted
    1902           0 :             SbModuleRef xRef = pMod;
    1903             :         }
    1904             :         else
    1905             :         {
    1906           0 :             pMod->SetParent( this );
    1907           0 :             pModules->Put( pMod, i );
    1908             :         }
    1909             :     }
    1910             :     // HACK for SFX-Bullshit!
    1911           0 :     SbxVariable* p = Find( OUString("FALSE"), SbxCLASS_PROPERTY );
    1912           0 :     if( p )
    1913             :     {
    1914           0 :         Remove( p );
    1915             :     }
    1916           0 :     p = Find( OUString("TRUE"), SbxCLASS_PROPERTY );
    1917           0 :     if( p )
    1918             :     {
    1919           0 :         Remove( p );
    1920             :     }
    1921             :     // End of the hacks!
    1922             :     // Search via StarBASIC is at all times global
    1923             :     DBG_ASSERT( IsSet( SBX_GBLSEARCH ), "Basic loaded without GBLSEARCH" );
    1924           0 :     SetFlag( SBX_GBLSEARCH );
    1925           0 :     return sal_True;
    1926             : }
    1927             : 
    1928           0 : sal_Bool StarBASIC::StoreData( SvStream& r ) const
    1929             : {
    1930           0 :     if( !SbxObject::StoreData( r ) )
    1931             :     {
    1932           0 :         return sal_False;
    1933             :     }
    1934           0 :     r << (sal_uInt16) pModules->Count();
    1935           0 :     for( sal_uInt16 i = 0; i < pModules->Count(); i++ )
    1936             :     {
    1937           0 :         SbModule* p = (SbModule*) pModules->Get( i );
    1938           0 :         if( !p->Store( r ) )
    1939             :         {
    1940           0 :             return sal_False;
    1941             :         }
    1942             :     }
    1943           0 :     return sal_True;
    1944             : }
    1945             : 
    1946        4889 : bool StarBASIC::GetUNOConstant( const sal_Char* _pAsciiName, ::com::sun::star::uno::Any& aOut )
    1947             : {
    1948        4889 :     bool bRes = false;
    1949        4889 :     OUString sVarName( OUString::createFromAscii( _pAsciiName ) );
    1950        4889 :     SbUnoObject* pGlobs = dynamic_cast<SbUnoObject*>( Find( sVarName, SbxCLASS_DONTCARE ) );
    1951        4889 :     if ( pGlobs )
    1952             :     {
    1953        1215 :         aOut = pGlobs->getUnoAny();
    1954        1215 :         bRes = true;
    1955             :     }
    1956        4889 :     return bRes;
    1957             : }
    1958             : 
    1959           0 : Reference< frame::XModel > StarBASIC::GetModelFromBasic( SbxObject* pBasic )
    1960             : {
    1961             :     OSL_PRECOND( pBasic != NULL, "getModelFromBasic: illegal call!" );
    1962           0 :     if ( !pBasic )
    1963             :     {
    1964           0 :         return NULL;
    1965             :     }
    1966             :     // look for the ThisComponent variable, first in the parent (which
    1967             :     // might be the document's Basic), then in the parent's parent (which might be
    1968             :     // the application Basic)
    1969           0 :     const OUString sThisComponent( "ThisComponent");
    1970           0 :     SbxVariable* pThisComponent = NULL;
    1971             : 
    1972           0 :     SbxObject* pLookup = pBasic->GetParent();
    1973           0 :     while ( pLookup && !pThisComponent )
    1974             :     {
    1975           0 :         pThisComponent = pLookup->Find( sThisComponent, SbxCLASS_OBJECT );
    1976           0 :         pLookup = pLookup->GetParent();
    1977             :     }
    1978           0 :     if ( !pThisComponent )
    1979             :     {
    1980             :         OSL_TRACE("Failed to get ThisComponent");
    1981             :             // the application Basic, at the latest, should have this variable
    1982           0 :         return NULL;
    1983             :     }
    1984             : 
    1985           0 :     Any aThisComponentAny( sbxToUnoValue( pThisComponent ) );
    1986           0 :     Reference< frame::XModel > xModel( aThisComponentAny, UNO_QUERY );
    1987           0 :     if ( !xModel.is() )
    1988             :     {
    1989             :         // it's no XModel. Okay, ThisComponent nowadays is allowed to be a controller.
    1990           0 :         Reference< frame::XController > xController( aThisComponentAny, UNO_QUERY );
    1991           0 :         if ( xController.is() )
    1992             :         {
    1993           0 :             xModel = xController->getModel();
    1994           0 :         }
    1995             :     }
    1996           0 :     if ( !xModel.is() )
    1997             :     {
    1998           0 :         return NULL;
    1999             :     }
    2000             : #if OSL_DEBUG_LEVEL > 0
    2001             :     OSL_TRACE("Have model ThisComponent points to url %s",
    2002             :               OUStringToOString( xModel->getURL(),
    2003             :                                  RTL_TEXTENCODING_ASCII_US ).pData->buffer );
    2004             : #endif
    2005             : 
    2006           0 :     return xModel;
    2007             : }
    2008             : 
    2009             : 
    2010             : //========================================================================
    2011             : // #118116 Implementation Collection object
    2012             : 
    2013           2 : TYPEINIT1(BasicCollection,SbxObject)
    2014             : 
    2015             : static const char pCountStr[]   = "Count";
    2016             : static const char pAddStr[]     = "Add";
    2017             : static const char pItemStr[]    = "Item";
    2018             : static const char pRemoveStr[]  = "Remove";
    2019             : static sal_uInt16 nCountHash = 0, nAddHash, nItemHash, nRemoveHash;
    2020             : 
    2021         143 : SbxInfoRef BasicCollection::xAddInfo = NULL;
    2022         143 : SbxInfoRef BasicCollection::xItemInfo = NULL;
    2023             : 
    2024           0 : BasicCollection::BasicCollection( const OUString& rClass )
    2025           0 :              : SbxObject( rClass )
    2026             : {
    2027           0 :     if( !nCountHash )
    2028             :     {
    2029           0 :         nCountHash  = MakeHashCode( OUString::createFromAscii( pCountStr ) );
    2030           0 :         nAddHash    = MakeHashCode( OUString::createFromAscii( pAddStr ) );
    2031           0 :         nItemHash   = MakeHashCode( OUString::createFromAscii( pItemStr ) );
    2032           0 :         nRemoveHash = MakeHashCode( OUString::createFromAscii( pRemoveStr ) );
    2033             :     }
    2034           0 :     Initialize();
    2035             : 
    2036           0 : }
    2037             : 
    2038           0 : BasicCollection::~BasicCollection()
    2039           0 : {}
    2040             : 
    2041           0 : void BasicCollection::Clear()
    2042             : {
    2043           0 :     SbxObject::Clear();
    2044           0 :     Initialize();
    2045           0 : }
    2046             : 
    2047           0 : void BasicCollection::Initialize()
    2048             : {
    2049           0 :     xItemArray = new SbxArray();
    2050           0 :     SetType( SbxOBJECT );
    2051           0 :     SetFlag( SBX_FIXED );
    2052           0 :     ResetFlag( SBX_WRITE );
    2053             :     SbxVariable* p;
    2054           0 :     p = Make( OUString::createFromAscii( pCountStr ), SbxCLASS_PROPERTY, SbxINTEGER );
    2055           0 :     p->ResetFlag( SBX_WRITE );
    2056           0 :     p->SetFlag( SBX_DONTSTORE );
    2057           0 :     p = Make( OUString::createFromAscii( pAddStr ), SbxCLASS_METHOD, SbxEMPTY );
    2058           0 :     p->SetFlag( SBX_DONTSTORE );
    2059           0 :     p = Make( OUString::createFromAscii( pItemStr ), SbxCLASS_METHOD, SbxVARIANT );
    2060           0 :     p->SetFlag( SBX_DONTSTORE );
    2061           0 :     p = Make( OUString::createFromAscii( pRemoveStr ), SbxCLASS_METHOD, SbxEMPTY );
    2062           0 :     p->SetFlag( SBX_DONTSTORE );
    2063           0 :     if ( !xAddInfo.Is() )
    2064             :     {
    2065           0 :         xAddInfo = new SbxInfo;
    2066           0 :         xAddInfo->AddParam(  OUString( "Item" ), SbxVARIANT, SBX_READ );
    2067           0 :         xAddInfo->AddParam(  OUString( "Key" ), SbxVARIANT, SBX_READ | SBX_OPTIONAL );
    2068           0 :         xAddInfo->AddParam(  OUString( "Before" ), SbxVARIANT, SBX_READ | SBX_OPTIONAL );
    2069           0 :         xAddInfo->AddParam(  OUString( "After" ), SbxVARIANT, SBX_READ | SBX_OPTIONAL );
    2070             :     }
    2071           0 :     if ( !xItemInfo.Is() )
    2072             :     {
    2073           0 :         xItemInfo = new SbxInfo;
    2074           0 :         xItemInfo->AddParam(  OUString( "Index" ), SbxVARIANT, SBX_READ | SBX_OPTIONAL);
    2075             :     }
    2076           0 : }
    2077             : 
    2078           0 : SbxVariable* BasicCollection::Find( const OUString& rName, SbxClassType t )
    2079             : {
    2080           0 :     SbxVariable* pFind = SbxObject::Find( rName, t );
    2081           0 :     return pFind;
    2082             : }
    2083             : 
    2084           0 : void BasicCollection::SFX_NOTIFY( SfxBroadcaster& rCst, const TypeId& rId1,
    2085             :                                 const SfxHint& rHint, const TypeId& rId2 )
    2086             : {
    2087           0 :     const SbxHint* p = PTR_CAST(SbxHint,&rHint);
    2088           0 :     if( p )
    2089             :     {
    2090           0 :         sal_uIntPtr nId = p->GetId();
    2091           0 :         sal_Bool bRead  = sal_Bool( nId == SBX_HINT_DATAWANTED );
    2092           0 :         sal_Bool bWrite = sal_Bool( nId == SBX_HINT_DATACHANGED );
    2093           0 :         sal_Bool bRequestInfo = sal_Bool( nId == SBX_HINT_INFOWANTED );
    2094           0 :         SbxVariable* pVar = p->GetVar();
    2095           0 :         SbxArray* pArg = pVar->GetParameters();
    2096           0 :         OUString aVarName( pVar->GetName() );
    2097           0 :         if( bRead || bWrite )
    2098             :         {
    2099           0 :             if( pVar->GetHashCode() == nCountHash
    2100           0 :                   && aVarName.equalsIgnoreAsciiCaseAscii( pCountStr ) )
    2101             :             {
    2102           0 :                 pVar->PutLong( xItemArray->Count32() );
    2103             :             }
    2104           0 :             else if( pVar->GetHashCode() == nAddHash
    2105           0 :                   && aVarName.equalsIgnoreAsciiCaseAscii( pAddStr ) )
    2106             :             {
    2107           0 :                 CollAdd( pArg );
    2108             :             }
    2109           0 :             else if( pVar->GetHashCode() == nItemHash
    2110           0 :                   && aVarName.equalsIgnoreAsciiCaseAscii( pItemStr ) )
    2111             :             {
    2112           0 :                 CollItem( pArg );
    2113             :             }
    2114           0 :             else if( pVar->GetHashCode() == nRemoveHash
    2115           0 :                   && aVarName.equalsIgnoreAsciiCaseAscii( pRemoveStr ) )
    2116             :             {
    2117           0 :                 CollRemove( pArg );
    2118             :             }
    2119             :             else
    2120             :             {
    2121           0 :                 SbxObject::SFX_NOTIFY( rCst, rId1, rHint, rId2 );
    2122             :             }
    2123           0 :             return;
    2124             :         }
    2125           0 :         else if ( bRequestInfo )
    2126             :         {
    2127           0 :             if( pVar->GetHashCode() == nAddHash
    2128           0 :                   && aVarName.equalsIgnoreAsciiCaseAscii( pAddStr ) )
    2129             :             {
    2130           0 :                 pVar->SetInfo( xAddInfo );
    2131             :             }
    2132           0 :             else if( pVar->GetHashCode() == nItemHash
    2133           0 :                   && aVarName.equalsIgnoreAsciiCaseAscii( pItemStr ) )
    2134             :             {
    2135           0 :                 pVar->SetInfo( xItemInfo );
    2136             :             }
    2137           0 :         }
    2138             :     }
    2139           0 :     SbxObject::SFX_NOTIFY( rCst, rId1, rHint, rId2 );
    2140             : }
    2141             : 
    2142           0 : sal_Int32 BasicCollection::implGetIndex( SbxVariable* pIndexVar )
    2143             : {
    2144           0 :     sal_Int32 nIndex = -1;
    2145           0 :     if( pIndexVar->GetType() == SbxSTRING )
    2146             :     {
    2147           0 :         nIndex = implGetIndexForName( pIndexVar->GetOUString() );
    2148             :     }
    2149             :     else
    2150             :     {
    2151           0 :         nIndex = pIndexVar->GetLong() - 1;
    2152             :     }
    2153           0 :     return nIndex;
    2154             : }
    2155             : 
    2156           0 : sal_Int32 BasicCollection::implGetIndexForName( const OUString& rName )
    2157             : {
    2158           0 :     sal_Int32 nIndex = -1;
    2159           0 :     sal_Int32 nCount = xItemArray->Count32();
    2160           0 :     sal_Int32 nNameHash = MakeHashCode( rName );
    2161           0 :     for( sal_Int32 i = 0 ; i < nCount ; i++ )
    2162             :     {
    2163           0 :         SbxVariable* pVar = xItemArray->Get32( i );
    2164           0 :         if( pVar->GetHashCode() == nNameHash &&
    2165           0 :             pVar->GetName().equalsIgnoreAsciiCase( rName ) )
    2166             :         {
    2167           0 :             nIndex = i;
    2168           0 :             break;
    2169             :         }
    2170             :     }
    2171           0 :     return nIndex;
    2172             : }
    2173             : 
    2174           0 : void BasicCollection::CollAdd( SbxArray* pPar_ )
    2175             : {
    2176           0 :     sal_uInt16 nCount = pPar_->Count();
    2177           0 :     if( nCount < 2 || nCount > 5 )
    2178             :     {
    2179           0 :         SetError( SbxERR_WRONG_ARGS );
    2180           0 :         return;
    2181             :     }
    2182             : 
    2183           0 :     SbxVariable* pItem = pPar_->Get(1);
    2184           0 :     if( pItem )
    2185             :     {
    2186             :         int nNextIndex;
    2187           0 :         if( nCount < 4 )
    2188             :         {
    2189           0 :             nNextIndex = xItemArray->Count();
    2190             :         }
    2191             :         else
    2192             :         {
    2193           0 :             SbxVariable* pBefore = pPar_->Get(3);
    2194           0 :             if( nCount == 5 )
    2195             :             {
    2196           0 :                 if( !( pBefore->IsErr() || ( pBefore->GetType() == SbxEMPTY ) ) )
    2197             :                 {
    2198           0 :                     SetError( SbERR_BAD_ARGUMENT );
    2199           0 :                     return;
    2200             :                 }
    2201           0 :                 SbxVariable* pAfter = pPar_->Get(4);
    2202           0 :                 sal_Int32 nAfterIndex = implGetIndex( pAfter );
    2203           0 :                 if( nAfterIndex == -1 )
    2204             :                 {
    2205           0 :                     SetError( SbERR_BAD_ARGUMENT );
    2206           0 :                     return;
    2207             :                 }
    2208           0 :                 nNextIndex = nAfterIndex + 1;
    2209             :             }
    2210             :             else // if( nCount == 4 )
    2211             :             {
    2212           0 :                 sal_Int32 nBeforeIndex = implGetIndex( pBefore );
    2213           0 :                 if( nBeforeIndex == -1 )
    2214             :                 {
    2215           0 :                     SetError( SbERR_BAD_ARGUMENT );
    2216           0 :                     return;
    2217             :                 }
    2218           0 :                 nNextIndex = nBeforeIndex;
    2219             :             }
    2220             :         }
    2221             : 
    2222           0 :         SbxVariableRef pNewItem = new SbxVariable( *pItem );
    2223           0 :         if( nCount >= 3 )
    2224             :         {
    2225           0 :             SbxVariable* pKey = pPar_->Get(2);
    2226           0 :             if( !( pKey->IsErr() || ( pKey->GetType() == SbxEMPTY ) ) )
    2227             :             {
    2228           0 :                 if( pKey->GetType() != SbxSTRING )
    2229             :                 {
    2230           0 :                     SetError( SbERR_BAD_ARGUMENT );
    2231           0 :                     return;
    2232             :                 }
    2233           0 :                 OUString aKey = pKey->GetOUString();
    2234           0 :                 if( implGetIndexForName( aKey ) != -1 )
    2235             :                 {
    2236           0 :                     SetError( SbERR_BAD_ARGUMENT );
    2237           0 :                     return;
    2238             :                 }
    2239           0 :                 pNewItem->SetName( aKey );
    2240             :             }
    2241             :         }
    2242           0 :         pNewItem->SetFlag( SBX_READWRITE );
    2243           0 :         xItemArray->Insert32( pNewItem, nNextIndex );
    2244             :     }
    2245             :     else
    2246             :     {
    2247           0 :         SetError( SbERR_BAD_ARGUMENT );
    2248           0 :         return;
    2249             :     }
    2250             : }
    2251             : 
    2252           0 : void BasicCollection::CollItem( SbxArray* pPar_ )
    2253             : {
    2254           0 :     if( pPar_->Count() != 2 )
    2255             :     {
    2256           0 :         SetError( SbxERR_WRONG_ARGS );
    2257           0 :         return;
    2258             :     }
    2259           0 :     SbxVariable* pRes = NULL;
    2260           0 :     SbxVariable* p = pPar_->Get( 1 );
    2261           0 :     sal_Int32 nIndex = implGetIndex( p );
    2262           0 :     if( nIndex >= 0 && nIndex < (sal_Int32)xItemArray->Count32() )
    2263             :     {
    2264           0 :         pRes = xItemArray->Get32( nIndex );
    2265             :     }
    2266           0 :     if( !pRes )
    2267             :     {
    2268           0 :         SetError( SbERR_BAD_ARGUMENT );
    2269             :     }
    2270             :     else
    2271             :     {
    2272           0 :         *(pPar_->Get(0)) = *pRes;
    2273             :     }
    2274             : }
    2275             : 
    2276           0 : void BasicCollection::CollRemove( SbxArray* pPar_ )
    2277             : {
    2278           0 :     if( pPar_ == NULL || pPar_->Count() != 2 )
    2279             :     {
    2280           0 :         SetError( SbxERR_WRONG_ARGS );
    2281           0 :         return;
    2282             :     }
    2283             : 
    2284           0 :     SbxVariable* p = pPar_->Get( 1 );
    2285           0 :     sal_Int32 nIndex = implGetIndex( p );
    2286           0 :     if( nIndex >= 0 && nIndex < (sal_Int32)xItemArray->Count32() )
    2287             :     {
    2288           0 :         xItemArray->Remove32( nIndex );
    2289             : 
    2290             :         // Correct for stack if necessary
    2291           0 :         SbiInstance* pInst = GetSbData()->pInst;
    2292           0 :         SbiRuntime* pRT = pInst ? pInst->pRun : NULL;
    2293           0 :         if( pRT )
    2294             :         {
    2295           0 :             SbiForStack* pStack = pRT->FindForStackItemForCollection( this );
    2296           0 :             if( pStack != NULL )
    2297             :             {
    2298           0 :                 if( pStack->nCurCollectionIndex >= nIndex )
    2299             :                 {
    2300           0 :                     --pStack->nCurCollectionIndex;
    2301             :                 }
    2302             :             }
    2303             :         }
    2304             :     }
    2305             :     else
    2306             :     {
    2307           0 :         SetError( SbERR_BAD_ARGUMENT );
    2308             :     }
    2309         429 : }
    2310             : 
    2311             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10