LCOV - code coverage report
Current view: top level - scripting/source/vbaevents - eventhelper.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 308 391 78.8 %
Date: 2014-04-11 Functions: 51 71 71.8 %
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             : #include <sal/macros.h>
      21             : #include <comphelper/processfactory.hxx>
      22             : #include <comphelper/uno3.hxx>
      23             : #include <comphelper/proparrhlp.hxx>
      24             : #include <comphelper/propertycontainer.hxx>
      25             : 
      26             : #include <ooo/vba/XVBAToOOEventDescGen.hpp>
      27             : 
      28             : #include <com/sun/star/beans/XPropertySet.hpp>
      29             : #include <com/sun/star/beans/theIntrospection.hpp>
      30             : #include <com/sun/star/beans/PropertyAttribute.hpp>
      31             : 
      32             : #include <com/sun/star/lang/XMultiComponentFactory.hpp>
      33             : #include <com/sun/star/lang/XServiceName.hpp>
      34             : #include <com/sun/star/lang/XServiceInfo.hpp>
      35             : #include <com/sun/star/lang/XInitialization.hpp>
      36             : 
      37             : #include <com/sun/star/util/XCloseListener.hpp>
      38             : #include <com/sun/star/util/XCloseBroadcaster.hpp>
      39             : 
      40             : #include <com/sun/star/frame/XModel.hpp>
      41             : 
      42             : #include <com/sun/star/script/XLibraryContainer.hpp>
      43             : #include <com/sun/star/script/ScriptEventDescriptor.hpp>
      44             : #include <com/sun/star/script/provider/XScriptProviderSupplier.hpp>
      45             : #include <com/sun/star/script/vba/XVBACompatibility.hpp>
      46             : 
      47             : #include <com/sun/star/container/XNamed.hpp>
      48             : 
      49             : #include <com/sun/star/drawing/XControlShape.hpp>
      50             : 
      51             : #include <com/sun/star/awt/XControl.hpp>
      52             : #include <com/sun/star/awt/XDialog.hpp>
      53             : #include <com/sun/star/awt/KeyEvent.hpp>
      54             : #include <com/sun/star/awt/MouseEvent.hpp>
      55             : #include <com/sun/star/awt/XFixedText.hpp>
      56             : #include <com/sun/star/awt/XTextComponent.hpp>
      57             : #include <com/sun/star/awt/XComboBox.hpp>
      58             : #include <com/sun/star/awt/XRadioButton.hpp>
      59             : #include <com/sun/star/awt/XListBox.hpp>
      60             : 
      61             : #include <sfx2/objsh.hxx>
      62             : #include <basic/sbstar.hxx>
      63             : #include <basic/basmgr.hxx>
      64             : #include <basic/sbmeth.hxx>
      65             : #include <basic/sbmod.hxx>
      66             : #include <basic/sbx.hxx>
      67             : #include <filter/msfilter/msvbahelper.hxx>
      68             : #include <vbahelper/vbareturntypes.hxx>
      69             : 
      70             : // for debug
      71             : #include <comphelper/anytostring.hxx>
      72             : 
      73             : #include <com/sun/star/script/XScriptListener.hpp>
      74             : #include <cppuhelper/implbase1.hxx>
      75             : #include <cppuhelper/implbase3.hxx>
      76             : #include <cppuhelper/implbase2.hxx>
      77             : #include <comphelper/evtmethodhelper.hxx>
      78             : 
      79             : #include <list>
      80             : #include <boost/unordered_map.hpp>
      81             : #define ASYNC 0
      82             : 
      83             : // primitive support for asynchronous handling of
      84             : // events from controls ( all event will be processed asynchronously
      85             : // in the application thread )
      86             : #if ASYNC
      87             : #include <vcl/svapp.hxx>
      88             : #endif
      89             : 
      90             : using namespace ::com::sun::star;
      91             : using namespace ::com::sun::star::script;
      92             : using namespace ::com::sun::star::uno;
      93             : using namespace ::ooo::vba;
      94             : 
      95             : // Some constants
      96           2 : const static OUString DELIM("::");
      97           2 : const static sal_Int32 DELIMLEN = DELIM.getLength();
      98             : 
      99           1 : bool isKeyEventOk( awt::KeyEvent& evt, const Sequence< Any >& params )
     100             : {
     101           2 :     if ( !( params.getLength() > 0 ) ||
     102           1 :         !( params[ 0 ] >>= evt ) )
     103           0 :         return false;
     104           1 :     return true;
     105             : }
     106             : 
     107           0 : bool isMouseEventOk( awt::MouseEvent& evt, const Sequence< Any >& params )
     108             : {
     109           0 :     if ( !( params.getLength() > 0 ) ||
     110           0 :         !( params[ 0 ] >>= evt ) )
     111           0 :         return false;
     112           0 :     return true;
     113             : }
     114             : 
     115           0 : Sequence< Any > ooMouseEvtToVBADblClick( const Sequence< Any >& params )
     116             : {
     117           0 :     awt::MouseEvent evt;
     118             : 
     119           0 :     if ( !( isMouseEventOk(evt, params)) ||
     120           0 :         (evt.ClickCount != 2) )
     121           0 :         return Sequence< Any >();
     122             :     // give back orig params, this will signal that the event is good
     123           0 :     return params;
     124             : }
     125             : 
     126           0 : Sequence< Any > ooMouseEvtToVBAMouseEvt( const Sequence< Any >& params )
     127             : {
     128           0 :     Sequence< Any > translatedParams;
     129           0 :     awt::MouseEvent evt;
     130             : 
     131           0 :     if ( !isMouseEventOk(evt, params) )
     132           0 :         return Sequence< Any >();
     133             : 
     134           0 :     translatedParams.realloc(4);
     135             : 
     136             :     // Buttons
     137           0 :     translatedParams[ 0 ] <<= evt.Buttons;
     138             :     // Shift
     139           0 :     translatedParams[ 1 ] <<= evt.Modifiers;
     140             :     // X
     141           0 :     translatedParams[ 2 ] <<= evt.X;
     142             :     // Y
     143           0 :     translatedParams[ 3 ] <<= evt.Y;
     144           0 :     return translatedParams;
     145             : }
     146             : 
     147           1 : Sequence< Any > ooKeyPressedToVBAKeyPressed( const Sequence< Any >& params )
     148             : {
     149           1 :     Sequence< Any > translatedParams;
     150           2 :     awt::KeyEvent evt;
     151             : 
     152           1 :     if ( !isKeyEventOk( evt, params ) )
     153           0 :         return Sequence< Any >();
     154             : 
     155           1 :     translatedParams.realloc(1);
     156             : 
     157           2 :     Reference< msforms::XReturnInteger> xKeyCode = new ReturnInteger(  sal_Int32( evt.KeyCode ) );
     158           1 :     translatedParams[0] <<= xKeyCode;
     159           2 :     return  translatedParams;
     160             : }
     161             : 
     162           0 : Sequence< Any > ooKeyPressedToVBAKeyUpDown( const Sequence< Any >& params )
     163             : {
     164           0 :     Sequence< Any > translatedParams;
     165           0 :     awt::KeyEvent evt;
     166             : 
     167           0 :     if ( !isKeyEventOk( evt, params ) )
     168           0 :         return Sequence< Any >();
     169             : 
     170           0 :     translatedParams.realloc(2);
     171             : 
     172           0 :     Reference< msforms::XReturnInteger> xKeyCode = new ReturnInteger(  evt.KeyCode );
     173           0 :     sal_Int8 shift = sal::static_int_cast<sal_Int8>( evt.Modifiers );
     174             : 
     175             :     // #TODO check whether values from OOO conform to values generated from vba
     176           0 :     translatedParams[0] <<= xKeyCode;
     177           0 :     translatedParams[1] <<= shift;
     178           0 :     return  translatedParams;
     179             : }
     180             : 
     181             : typedef Sequence< Any > (*Translator)(const Sequence< Any >&);
     182             : 
     183             : //expand the "TranslateInfo" struct to support more kinds of events
     184          84 : struct TranslateInfo
     185             : {
     186             :     OUString sVBAName; //vba event name
     187             :     Translator toVBA;       //the method to convert OO event parameters to VBA event parameters
     188             :     bool (*ApproveRule)(const ScriptEvent& evt, void* pPara); //this method is used to determine which types of controls should execute the event
     189             :     void *pPara;            //Parameters for the above approve method
     190             : };
     191             : 
     192             : 
     193             : typedef boost::unordered_map< OUString,
     194             : std::list< TranslateInfo >,
     195             : OUStringHash,
     196             : ::std::equal_to< OUString > > EventInfoHash;
     197             : 
     198             : 
     199           0 : struct TranslatePropMap
     200             : {
     201             :     OUString sEventInfo;   //OO event name
     202             :     TranslateInfo aTransInfo;
     203             : };
     204             : 
     205             : bool ApproveAll(const ScriptEvent& evt, void* pPara); //allow all types of controls to execute the event
     206             : bool ApproveType(const ScriptEvent& evt, void* pPara); //certain types of controls should execute the event, those types are given by pPara
     207             : bool DenyType(const ScriptEvent& evt, void* pPara);    //certain types of controls should not execute the event, those types are given by pPara
     208             : bool DenyMouseDrag(const ScriptEvent& evt, void* pPara); //used for VBA MouseMove event when "Shift" key is pressed
     209             : 
     210             : struct TypeList
     211             : {
     212             :     uno::Type* pTypeList;
     213             :     int nListLength;
     214             : };
     215             : 
     216           2 : Type typeXFixedText = cppu::UnoType<awt::XFixedText>::get();
     217           2 : Type typeXTextComponent = cppu::UnoType<awt::XTextComponent>::get();
     218           2 : Type typeXComboBox = cppu::UnoType<awt::XComboBox>::get();
     219           2 : Type typeXRadioButton = cppu::UnoType<awt::XRadioButton>::get();
     220           2 : Type typeXListBox = cppu::UnoType<awt::XListBox>::get();
     221             : 
     222             : 
     223             : TypeList fixedTextList = {&typeXFixedText, 1};
     224             : TypeList textCompList = {&typeXTextComponent, 1};
     225             : TypeList radioButtonList = {&typeXRadioButton, 1};
     226             : TypeList comboBoxList = {&typeXComboBox, 1};
     227             : TypeList listBoxList = {&typeXListBox, 1};
     228             : 
     229             : //this array stores the OO event to VBA event translation info
     230           2 : static TranslatePropMap aTranslatePropMap_Impl[] =
     231             : {
     232             :     { OUString("actionPerformed"), { OUString("_Change"), NULL, DenyType, (void*)(&radioButtonList) } },
     233             :     // actionPerformed ooo event
     234             :     { OUString("actionPerformed"), { OUString("_Click"), NULL, ApproveAll, NULL } },
     235             :     { OUString("itemStateChanged"), { OUString("_Change"), NULL, ApproveType, (void*)(&radioButtonList) } },
     236             :     // itemStateChanged ooo event
     237             :     { OUString("itemStateChanged"), { OUString("_Click"), NULL, ApproveType, (void*)(&comboBoxList) } },
     238             : 
     239             :     { OUString("itemStateChanged"), { OUString("_Click"), NULL, ApproveType, (void*)(&listBoxList) } },
     240             :     // changed ooo event
     241             :     { OUString("changed"), { OUString("_Change"), NULL, ApproveAll, NULL } },
     242             : 
     243             :     // focusGained ooo event
     244             :     { OUString("focusGained"), { OUString("_GotFocus"), NULL, ApproveAll, NULL } },
     245             : 
     246             :     // focusLost ooo event
     247             :     { OUString("focusLost"), { OUString("_LostFocus"), NULL, ApproveAll, NULL } },
     248             :     { OUString("focusLost"), { OUString("_Exit"), NULL, ApproveType, (void*)(&textCompList) } }, // support VBA TextBox_Exit event
     249             : 
     250             :     // adjustmentValueChanged ooo event
     251             :     { OUString("adjustmentValueChanged"), { OUString("_Scroll"), NULL, ApproveAll, NULL } },
     252             :     { OUString("adjustmentValueChanged"), { OUString("_Change"), NULL, ApproveAll, NULL } },
     253             : 
     254             :     // textChanged ooo event
     255             :     { OUString("textChanged"), { OUString("_Change"), NULL, ApproveAll, NULL } },
     256             : 
     257             :     // keyReleased ooo event
     258             :     { OUString("keyReleased"), { OUString("_KeyUp"), ooKeyPressedToVBAKeyUpDown, ApproveAll, NULL } },
     259             : 
     260             :     // mouseReleased ooo event
     261             :     { OUString("mouseReleased"), { OUString("_Click"), ooMouseEvtToVBAMouseEvt, ApproveType, (void*)(&fixedTextList) } },
     262             :     { OUString("mouseReleased"), { OUString("_MouseUp"), ooMouseEvtToVBAMouseEvt, ApproveAll, NULL } },
     263             : 
     264             :     // mousePressed ooo event
     265             :     { OUString("mousePressed"), { OUString("_MouseDown"), ooMouseEvtToVBAMouseEvt, ApproveAll, NULL } },
     266             :     { OUString("mousePressed"), { OUString("_DblClick"), ooMouseEvtToVBADblClick, ApproveAll, NULL } },
     267             : 
     268             :     // mouseMoved ooo event
     269             :     { OUString("mouseMoved"), { OUString("_MouseMove"), ooMouseEvtToVBAMouseEvt, ApproveAll, NULL } },
     270             :     { OUString("mouseDragged"), { OUString("_MouseMove"), ooMouseEvtToVBAMouseEvt, DenyMouseDrag, NULL } },
     271             : 
     272             :     // keyPressed ooo event
     273             :     { OUString("keyPressed"), { OUString("_KeyDown"), ooKeyPressedToVBAKeyUpDown, ApproveAll, NULL } },
     274             :     { OUString("keyPressed"), { OUString("_KeyPress"), ooKeyPressedToVBAKeyPressed, ApproveAll, NULL } }
     275           2 : };
     276             : 
     277         227 : EventInfoHash& getEventTransInfo()
     278             : {
     279             :     static bool initialised = false;
     280         227 :     static EventInfoHash eventTransInfo;
     281         227 :     if ( !initialised )
     282             :     {
     283           1 :         OUString sEventInfo;
     284           1 :         TranslatePropMap* pTransProp = aTranslatePropMap_Impl;
     285           1 :         int nCount = sizeof(aTranslatePropMap_Impl) / sizeof(aTranslatePropMap_Impl[0]);
     286             : 
     287           1 :         int i = 0;
     288          15 :         while (i < nCount)
     289             :         {
     290          13 :             sEventInfo = pTransProp->sEventInfo;
     291          13 :             std::list< TranslateInfo > infoList;
     292          21 :             do
     293             :             {
     294          21 :                 infoList.push_back( pTransProp->aTransInfo );
     295          21 :                 pTransProp++;
     296          21 :                 i++;
     297          21 :             }while(i < nCount && sEventInfo == pTransProp->sEventInfo);
     298          13 :             eventTransInfo[sEventInfo] = infoList;
     299          13 :         }
     300           1 :         initialised = true;
     301             :     }
     302         227 :     return eventTransInfo;
     303             : }
     304             : 
     305             : 
     306             : // Helper class
     307             : 
     308             : class ScriptEventHelper
     309             : {
     310             : public:
     311             :     ScriptEventHelper( const Reference< XInterface >& xControl );
     312             :     ScriptEventHelper( const OUString& sCntrlServiceName );
     313             :     ~ScriptEventHelper();
     314             :     Sequence< ScriptEventDescriptor > createEvents( const OUString& sCodeName );
     315             :     Sequence< OUString > getEventListeners();
     316             : private:
     317             :     Reference< XComponentContext > m_xCtx;
     318             :     Reference< XInterface > m_xControl;
     319             :     bool m_bDispose;
     320             : };
     321             : 
     322             : bool
     323         199 : eventMethodToDescriptor( const OUString& rEventMethod, ScriptEventDescriptor& evtDesc, const OUString& sCodeName )
     324             : {
     325             :     // format of ControlListener is TypeName::methodname e.g.
     326             :     // "com.sun.star.awt.XActionListener::actionPerformed" or
     327             :     // "XActionListener::actionPerformed
     328             : 
     329         199 :     OUString sMethodName;
     330         398 :     OUString sTypeName;
     331         199 :     sal_Int32 nDelimPos = rEventMethod.indexOf( DELIM );
     332         199 :     if ( nDelimPos == -1 )
     333             :     {
     334           0 :         return false;
     335             :     }
     336         199 :     sMethodName = rEventMethod.copy( nDelimPos + DELIMLEN );
     337         199 :     sTypeName = rEventMethod.copy( 0, nDelimPos );
     338             : 
     339         199 :     EventInfoHash& infos = getEventTransInfo();
     340             : 
     341             :     // Only create an ScriptEventDescriptor for an event we can translate
     342             :     // or emulate
     343         597 :     if ( !sMethodName.isEmpty()
     344         199 :          && !sTypeName.isEmpty()
     345         995 :          && ( infos.find( sMethodName ) != infos.end() ) )
     346             :     {
     347             :         // just fill in CodeName, when the event fires the other
     348             :     // info is gathered from the event source to determine what
     349             :     // event handler we try to call
     350          87 :         evtDesc.ScriptCode = sCodeName;
     351          87 :         evtDesc.ListenerType = sTypeName;
     352          87 :         evtDesc.EventMethod = sMethodName;
     353             : 
     354             :         // set this it VBAInterop, ensures that it doesn't
     355             :         // get persisted or shown in property editors
     356          87 :         evtDesc.ScriptType = "VBAInterop";
     357          87 :         return true;
     358             :     }
     359         311 :     return false;
     360             : 
     361             : }
     362             : 
     363           8 : ScriptEventHelper::ScriptEventHelper( const Reference< XInterface >& xControl ) :
     364             :     m_xCtx( comphelper::getProcessComponentContext() ),
     365             :     m_xControl( xControl ),
     366           8 :     m_bDispose( false )
     367           8 : {}
     368             : 
     369           1 : ScriptEventHelper::ScriptEventHelper( const OUString& sCntrlServiceName ) :
     370             :     m_xCtx( comphelper::getProcessComponentContext() ),
     371           1 :     m_bDispose( true )
     372             : {
     373           1 :    m_xControl.set( m_xCtx->getServiceManager()->createInstanceWithContext( sCntrlServiceName, m_xCtx ), uno::UNO_QUERY );
     374           1 : }
     375             : 
     376          18 : ScriptEventHelper::~ScriptEventHelper()
     377             : {
     378             :     // dispose control ( and remove any associated event registrations )
     379           9 :     if ( m_bDispose )
     380             :     {
     381             :         try
     382             :         {
     383           1 :             uno::Reference< lang::XComponent > xComp( m_xControl, uno::UNO_QUERY_THROW );
     384           1 :             xComp->dispose();
     385             :         }
     386             :         // destructor can't throw
     387           0 :         catch( uno::Exception& )
     388             :         {
     389             :         }
     390             :     }
     391           9 : }
     392             : 
     393             : Sequence< OUString >
     394           9 : ScriptEventHelper::getEventListeners()
     395             : {
     396           9 :     std::list< OUString > eventMethods;
     397             : 
     398          18 :     Reference< beans::XIntrospection > xIntrospection = beans::theIntrospection::get( m_xCtx );
     399             : 
     400             :     Reference< beans::XIntrospectionAccess > xIntrospectionAccess =
     401          18 :         xIntrospection->inspect( makeAny( m_xControl ) );
     402             :     Sequence< Type > aControlListeners =
     403          18 :         xIntrospectionAccess->getSupportedListeners();
     404           9 :     sal_Int32 nLength = aControlListeners.getLength();
     405         111 :     for ( sal_Int32 i = 0; i< nLength; ++i )
     406             :     {
     407         102 :         Type& listType = aControlListeners[ i ];
     408         102 :         OUString sFullTypeName = listType.getTypeName();
     409             :         Sequence< OUString > sMeths =
     410         204 :             comphelper::getEventMethodsForType( listType );
     411         102 :         sal_Int32 sMethLen = sMeths.getLength();
     412         301 :         for ( sal_Int32 j=0 ; j < sMethLen; ++j )
     413             :         {
     414         199 :             OUString sEventMethod = sFullTypeName;
     415         199 :             sEventMethod += DELIM;
     416         199 :             sEventMethod += sMeths[ j ];
     417         199 :             eventMethods.push_back( sEventMethod );
     418         199 :         }
     419         102 :     }
     420             : 
     421           9 :     Sequence< OUString > sEventMethodNames( eventMethods.size() );
     422           9 :     std::list< OUString >::const_iterator it = eventMethods.begin();
     423           9 :     OUString* pDest = sEventMethodNames.getArray();
     424             : 
     425         208 :     for ( ; it != eventMethods.end(); ++it, ++pDest )
     426         199 :         *pDest = *it;
     427             : 
     428          18 :     return sEventMethodNames;
     429             : }
     430             : 
     431             : Sequence< ScriptEventDescriptor >
     432           1 : ScriptEventHelper::createEvents( const OUString& sCodeName )
     433             : {
     434           1 :     Sequence< OUString > aControlListeners = getEventListeners();
     435           1 :     OUString* pSrc = aControlListeners.getArray();
     436           1 :     sal_Int32 nLength = aControlListeners.getLength();
     437             : 
     438           1 :     Sequence< ScriptEventDescriptor > aDest( nLength );
     439           1 :     sal_Int32 nEvts = 0;
     440          23 :     for ( sal_Int32 i = 0; i< nLength; ++i, ++pSrc )
     441             :     {
     442             :         // from getListeners eventName is of form
     443             :         // "com.sun.star.awt.XActionListener::actionPerformed"
     444             :         // we need to strip "com.sun.star.awt." from that for form
     445             :         // controls
     446          22 :         ScriptEventDescriptor evtDesc;
     447          22 :         if ( eventMethodToDescriptor( *pSrc, evtDesc, sCodeName ) )
     448             :         {
     449          10 :             sal_Int32 dIndex = nEvts;
     450          10 :             ++nEvts;
     451          10 :             if ( nEvts > aDest.getLength() )
     452           0 :                 aDest.realloc( nEvts );// should never happen
     453          10 :             aDest[ dIndex ] = evtDesc;
     454             :         }
     455          22 :     }
     456           1 :     aDest.realloc( nEvts );
     457             : 
     458           1 :     return aDest;
     459             : }
     460             : 
     461             : 
     462             : typedef ::cppu::WeakImplHelper1< container::XNameContainer > NameContainer_BASE;
     463             : 
     464          16 : class ReadOnlyEventsNameContainer : public NameContainer_BASE
     465             : {
     466             : public:
     467             :     ReadOnlyEventsNameContainer( const Sequence< OUString >& eventMethods, const OUString& sCodeName );
     468             :     // XNameContainer
     469             : 
     470           0 :     virtual void SAL_CALL insertByName( const OUString&, const Any& ) throw (lang::IllegalArgumentException, container::ElementExistException, lang::WrappedTargetException, RuntimeException, std::exception) SAL_OVERRIDE
     471             :     {
     472           0 :         throw RuntimeException("ReadOnly container", Reference< XInterface >() );
     473             : 
     474             :     }
     475           0 :     virtual void SAL_CALL removeByName( const OUString& ) throw (::com::sun::star::container::NoSuchElementException, lang::WrappedTargetException, RuntimeException, std::exception) SAL_OVERRIDE
     476             :     {
     477           0 :         throw RuntimeException("ReadOnly container", Reference< XInterface >() );
     478             :     }
     479             : 
     480             :     // XNameReplace
     481           0 :     virtual void SAL_CALL replaceByName( const OUString&, const Any& ) throw (lang::IllegalArgumentException, container::NoSuchElementException, lang::WrappedTargetException, RuntimeException, std::exception) SAL_OVERRIDE
     482             :     {
     483           0 :         throw RuntimeException("ReadOnly container", Reference< XInterface >() );
     484             : 
     485             :     }
     486             : 
     487             :     // XNameAccess
     488             :     virtual Any SAL_CALL getByName( const OUString& aName ) throw (container::NoSuchElementException, lang::WrappedTargetException, RuntimeException, std::exception) SAL_OVERRIDE;
     489             :     virtual Sequence< OUString > SAL_CALL getElementNames(  ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
     490             :     virtual sal_Bool SAL_CALL hasByName( const OUString& aName ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
     491             : 
     492             :     // XElementAccess
     493           0 :     virtual Type SAL_CALL getElementType(  ) throw (RuntimeException, std::exception) SAL_OVERRIDE
     494           0 :     { return getCppuType(static_cast< const OUString * >(0) ); }
     495           0 :     virtual sal_Bool SAL_CALL hasElements(  ) throw (RuntimeException, std::exception) SAL_OVERRIDE
     496           0 :     { return ( ( m_hEvents.size() > 0 ? sal_True : sal_False ) ); }
     497             : private:
     498             : 
     499             : typedef boost::unordered_map< OUString, Any, OUStringHash,
     500             : ::std::equal_to< OUString > > EventSupplierHash;
     501             : 
     502             :     EventSupplierHash m_hEvents;
     503             : };
     504             : 
     505           8 : ReadOnlyEventsNameContainer::ReadOnlyEventsNameContainer( const Sequence< OUString >& eventMethods, const OUString& sCodeName )
     506             : {
     507           8 :     const OUString* pSrc = eventMethods.getConstArray();
     508           8 :     sal_Int32 nLen = eventMethods.getLength();
     509         185 :     for ( sal_Int32 index = 0; index < nLen; ++index, ++pSrc )
     510             :     {
     511         177 :         Any aDesc;
     512         354 :         ScriptEventDescriptor evtDesc;
     513         177 :         if (  eventMethodToDescriptor( *pSrc, evtDesc, sCodeName ) )
     514             :         {
     515          77 :             aDesc <<= evtDesc;
     516          77 :             m_hEvents[ *pSrc ] = aDesc;
     517             :         }
     518         177 :     }
     519           8 : }
     520             : 
     521             : Any SAL_CALL
     522          77 : ReadOnlyEventsNameContainer::getByName( const OUString& aName ) throw (container::NoSuchElementException, lang::WrappedTargetException, RuntimeException, std::exception){
     523          77 :     EventSupplierHash::const_iterator it = m_hEvents.find( aName );
     524          77 :     if ( it == m_hEvents.end() )
     525           0 :         throw container::NoSuchElementException();
     526          77 :     return it->second;
     527             : }
     528             : 
     529             : Sequence< OUString > SAL_CALL
     530           8 : ReadOnlyEventsNameContainer::getElementNames(  ) throw (RuntimeException, std::exception)
     531             : {
     532           8 :     Sequence< OUString > names(m_hEvents.size());
     533           8 :     OUString* pDest = names.getArray();
     534           8 :     EventSupplierHash::const_iterator it = m_hEvents.begin();
     535           8 :     EventSupplierHash::const_iterator it_end = m_hEvents.end();
     536          85 :     for ( sal_Int32 index = 0; it != it_end; ++index, ++pDest, ++it )
     537          77 :         *pDest = it->first;
     538           8 :     return names;
     539             : }
     540             : 
     541             : sal_Bool SAL_CALL
     542           0 : ReadOnlyEventsNameContainer::hasByName( const OUString& aName ) throw (RuntimeException, std::exception)
     543             : {
     544           0 :     EventSupplierHash::const_iterator it = m_hEvents.find( aName );
     545           0 :     if ( it == m_hEvents.end() )
     546           0 :         return sal_False;
     547           0 :     return sal_True;
     548             : }
     549             : 
     550             : typedef ::cppu::WeakImplHelper1< XScriptEventsSupplier > EventsSupplier_BASE;
     551             : 
     552          16 : class ReadOnlyEventsSupplier : public EventsSupplier_BASE
     553             : {
     554             : public:
     555           8 :     ReadOnlyEventsSupplier( const Sequence< OUString >& eventMethods, const OUString& sCodeName )
     556           8 :     { m_xNameContainer = new ReadOnlyEventsNameContainer( eventMethods, sCodeName ); }
     557             : 
     558             :     // XScriptEventSupplier
     559           8 :     virtual Reference< container::XNameContainer > SAL_CALL getEvents(  ) throw (RuntimeException, std::exception) SAL_OVERRIDE { return m_xNameContainer; }
     560             : private:
     561             :     Reference< container::XNameContainer > m_xNameContainer;
     562             : };
     563             : 
     564             : typedef ::cppu::WeakImplHelper3< XScriptListener, util::XCloseListener, lang::XInitialization > EventListener_BASE;
     565             : 
     566             : #define EVENTLSTNR_PROPERTY_ID_MODEL         1
     567             : #define EVENTLSTNR_PROPERTY_MODEL            OUString( "Model"  )
     568             : 
     569          58 : class EventListener : public EventListener_BASE
     570             :     ,public ::comphelper::OMutexAndBroadcastHelper
     571             :     ,public ::comphelper::OPropertyContainer
     572             :     ,public ::comphelper::OPropertyArrayUsageHelper< EventListener >
     573             : 
     574             : {
     575             : 
     576             : public:
     577             :     EventListener( const Reference< XComponentContext >& rxContext );
     578             :     // XEventListener
     579             :     virtual void SAL_CALL disposing(const lang::EventObject& Source) throw( RuntimeException, std::exception ) SAL_OVERRIDE;
     580             :     using cppu::OPropertySetHelper::disposing;
     581             : 
     582             :     // XScriptListener
     583             :     virtual void SAL_CALL firing(const ScriptEvent& evt) throw(RuntimeException, std::exception) SAL_OVERRIDE;
     584             :     virtual Any SAL_CALL approveFiring(const ScriptEvent& evt) throw(reflection::InvocationTargetException, RuntimeException, std::exception) SAL_OVERRIDE;
     585             :     // XCloseListener
     586             :     virtual void SAL_CALL queryClosing( const lang::EventObject& Source, sal_Bool GetsOwnership ) throw (util::CloseVetoException, uno::RuntimeException, std::exception) SAL_OVERRIDE;
     587             :     virtual void SAL_CALL notifyClosing( const lang::EventObject& Source ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE;
     588             :     // XPropertySet
     589             :     virtual ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySetInfo > SAL_CALL getPropertySetInfo(  ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
     590             :     // XInitialization
     591             :     virtual void SAL_CALL initialize( const Sequence< Any >& aArguments ) throw (Exception, RuntimeException, std::exception) SAL_OVERRIDE;
     592             :     // XInterface
     593             :     DECLARE_XINTERFACE()
     594             : 
     595             :     // XTypeProvider
     596             :     DECLARE_XTYPEPROVIDER()
     597          31 :     virtual void SAL_CALL setFastPropertyValue( sal_Int32 nHandle, const ::com::sun::star::uno::Any& rValue ) throw(::com::sun::star::beans::UnknownPropertyException, ::com::sun::star::beans::PropertyVetoException, ::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::lang::WrappedTargetException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
     598             :     {
     599          31 :         if ( nHandle == EVENTLSTNR_PROPERTY_ID_MODEL )
     600             :         {
     601          31 :             uno::Reference< frame::XModel > xModel( rValue, uno::UNO_QUERY );
     602          31 :             if( xModel != m_xModel)
     603             :             {
     604             :                 // Remove the listener from the old XCloseBroadcaster.
     605          29 :                 uno::Reference< util::XCloseBroadcaster > xCloseBroadcaster( m_xModel, uno::UNO_QUERY );
     606          29 :                 if (xCloseBroadcaster.is())
     607             :                 {
     608           0 :                     xCloseBroadcaster->removeCloseListener( this );
     609             :                 }
     610             :                 // Add the listener into the new XCloseBroadcaster.
     611          29 :                 xCloseBroadcaster = uno::Reference< util::XCloseBroadcaster >( xModel, uno::UNO_QUERY );
     612          29 :                 if (xCloseBroadcaster.is())
     613             :                 {
     614          29 :                     xCloseBroadcaster->addCloseListener( this );
     615          29 :                 }
     616          31 :             }
     617             :         }
     618          31 :         OPropertyContainer::setFastPropertyValue( nHandle, rValue );
     619          31 :     if ( nHandle == EVENTLSTNR_PROPERTY_ID_MODEL )
     620          31 :             setShellFromModel();
     621          31 :     }
     622             : 
     623             : protected:
     624             :     // OPropertySetHelper
     625             :     virtual ::cppu::IPropertyArrayHelper& SAL_CALL getInfoHelper(  ) SAL_OVERRIDE;
     626             : 
     627             :     // OPropertyArrayUsageHelper
     628             :     virtual ::cppu::IPropertyArrayHelper* createArrayHelper(  ) const SAL_OVERRIDE;
     629             : 
     630             : private:
     631             : #if ASYNC
     632             :     DECL_LINK( OnAsyncScriptEvent, ScriptEvent* );
     633             : #endif
     634             :     void setShellFromModel();
     635             :     void firing_Impl( const  ScriptEvent& evt, Any *pSyncRet=NULL ) throw( RuntimeException );
     636             : 
     637             :     Reference< XComponentContext > m_xContext;
     638             :     Reference< frame::XModel > m_xModel;
     639             :     bool m_bDocClosed;
     640             :     SfxObjectShell* mpShell;
     641             :     OUString msProject;
     642             : };
     643             : 
     644          31 : EventListener::EventListener( const Reference< XComponentContext >& rxContext ) :
     645          31 : OPropertyContainer(GetBroadcastHelper()), m_xContext( rxContext ), m_bDocClosed(false), mpShell( 0 )
     646             : {
     647             :     registerProperty( EVENTLSTNR_PROPERTY_MODEL, EVENTLSTNR_PROPERTY_ID_MODEL,
     648          31 :         beans::PropertyAttribute::TRANSIENT, &m_xModel, ::getCppuType( &m_xModel ) );
     649          31 :     msProject = "Standard";
     650          31 : }
     651             : 
     652             : void
     653          31 : EventListener::setShellFromModel()
     654             : {
     655             :     // reset mpShell
     656          31 :     mpShell = 0;
     657          31 :     SfxObjectShell* pShell = SfxObjectShell::GetFirst();
     658          62 :     while ( m_xModel.is() && pShell )
     659             :     {
     660          31 :         if ( pShell->GetModel() == m_xModel )
     661             :         {
     662          31 :             mpShell = pShell;
     663          31 :             break;
     664             :         }
     665           0 :         pShell = SfxObjectShell::GetNext( *pShell );
     666             :     }
     667             :     // set ProjectName from model
     668             :     try
     669             :     {
     670          31 :         uno::Reference< beans::XPropertySet > xProps( m_xModel, UNO_QUERY_THROW );
     671          62 :         uno::Reference< script::vba::XVBACompatibility > xVBAMode( xProps->getPropertyValue("BasicLibraries"), uno::UNO_QUERY_THROW );
     672          62 :         msProject = xVBAMode->getProjectName();
     673             :     }
     674           0 :     catch ( uno::Exception& ) {}
     675          31 : }
     676             : 
     677             : //XEventListener
     678             : void
     679           0 : EventListener::disposing(const lang::EventObject&)  throw( RuntimeException, std::exception )
     680             : {
     681           0 : }
     682             : 
     683             : //XScriptListener
     684             : 
     685             : void SAL_CALL
     686          29 : EventListener::firing(const ScriptEvent& evt) throw(RuntimeException, std::exception)
     687             : {
     688             : #if ASYNC
     689             :     // needs some logic to check if the event handler is oneway or not
     690             :     // if not oneway then firing_Impl otherwise... as below
     691             :     acquire();
     692             :     Application::PostUserEvent( LINK( this, EventListener, OnAsyncScriptEvent ), new ScriptEvent( evt ) );
     693             : #else
     694          29 :     firing_Impl( evt );
     695             : #endif
     696          29 : }
     697             : 
     698             : #if ASYNC
     699             : IMPL_LINK( EventListener, OnAsyncScriptEvent, ScriptEvent*, _pEvent )
     700             : {
     701             :     if ( !_pEvent )
     702             :         return 1L;
     703             : 
     704             :     {
     705             :         // #FIXME if we enable ASYNC we probably need something like
     706             :         // below
     707             :         //::osl::ClearableMutexGuard aGuard( m_aMutex );
     708             : 
     709             :         //if ( !impl_isDisposed_nothrow() )
     710             :         //  impl_doFireScriptEvent_nothrow( aGuard, *_pEvent, NULL );
     711             :         firing_Impl( *_pEvent, NULL );
     712             :     }
     713             : 
     714             :     delete _pEvent;
     715             :     // we acquired ourself immediately before posting the event
     716             :     release();
     717             :     return 0L;
     718             :  }
     719             : #endif
     720             : 
     721             : Any SAL_CALL
     722           0 : EventListener::approveFiring(const ScriptEvent& evt) throw(reflection::InvocationTargetException, RuntimeException, std::exception)
     723             : {
     724           0 :     Any ret;
     725           0 :     firing_Impl( evt, &ret );
     726           0 :     return ret;
     727             : }
     728             : 
     729             : // XCloseListener
     730             : void SAL_CALL
     731          29 : EventListener::queryClosing( const lang::EventObject& /*Source*/, sal_Bool /*GetsOwnership*/ ) throw (util::CloseVetoException, uno::RuntimeException, std::exception)
     732             : {
     733             :     //Nothing to do
     734          29 : }
     735             : 
     736             : void SAL_CALL
     737          29 : EventListener::notifyClosing( const lang::EventObject& /*Source*/ ) throw (uno::RuntimeException, std::exception)
     738             : {
     739          29 :     m_bDocClosed = true;
     740          29 :     uno::Reference< util::XCloseBroadcaster > xCloseBroadcaster( m_xModel, uno::UNO_QUERY );
     741          29 :     if (xCloseBroadcaster.is())
     742             :     {
     743          29 :         xCloseBroadcaster->removeCloseListener( this );
     744          29 :     }
     745          29 : }
     746             : 
     747             : // XInitialization
     748             : void SAL_CALL
     749           2 : EventListener::initialize( const Sequence< Any >& aArguments ) throw (Exception, RuntimeException, std::exception)
     750             : {
     751           2 :     if ( aArguments.getLength() == 1 )
     752           2 :         aArguments[0] >>= m_xModel;
     753             :     OSL_TRACE("EventListener::initialize() args %d m_xModel %d", aArguments.getLength(), m_xModel.is() );
     754           2 : }
     755             : 
     756             : // XInterface
     757             : 
     758         999 : IMPLEMENT_FORWARD_XINTERFACE2( EventListener, EventListener_BASE, OPropertyContainer )
     759             : 
     760             : // XTypeProvider
     761             : 
     762           0 : IMPLEMENT_FORWARD_XTYPEPROVIDER2( EventListener, EventListener_BASE, OPropertyContainer )
     763             : 
     764             : // OPropertySetHelper
     765             : 
     766             : ::cppu::IPropertyArrayHelper&
     767          91 : EventListener::getInfoHelper(  )
     768             : {
     769          91 :     return *getArrayHelper();
     770             : }
     771             : 
     772             : // OPropertyArrayUsageHelper
     773             : 
     774             : ::cppu::IPropertyArrayHelper*
     775           2 : EventListener::createArrayHelper(  ) const
     776             : {
     777           2 :     Sequence< beans::Property > aProps;
     778           2 :     describeProperties( aProps );
     779           2 :     return new ::cppu::OPropertyArrayHelper( aProps );
     780             : }
     781             : 
     782             : // XPropertySet
     783             : Reference< beans::XPropertySetInfo >
     784           0 : EventListener::getPropertySetInfo(  ) throw (RuntimeException, std::exception)
     785             : {
     786           0 :     Reference< beans::XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
     787           0 :     return xInfo;
     788             : }
     789             : 
     790             : 
     791             : //decide if the control should execute the event
     792          10 : bool ApproveAll(const ScriptEvent&, void* )
     793             : {
     794          10 :     return true;
     795             : }
     796             : 
     797             : //for the given control type in evt.Arguments[0], look for if it appears in the type list in pPara
     798           1 : bool FindControl(const ScriptEvent& evt, void* pPara)
     799             : {
     800           1 :     lang::EventObject aEvent;
     801           1 :     evt.Arguments[ 0 ] >>= aEvent;
     802           2 :     uno::Reference< uno::XInterface > xInterface( aEvent.Source, uno::UNO_QUERY );
     803             : 
     804           1 :     TypeList* pTypeListInfo = static_cast<TypeList*>(pPara);
     805           1 :     Type* pType = pTypeListInfo->pTypeList;
     806           1 :     int nLen = pTypeListInfo->nListLength;
     807             : 
     808           2 :     for (int i = 0; i < nLen; i++)
     809             :     {
     810           1 :         if ( xInterface->queryInterface( *pType ).hasValue() )
     811             :         {
     812           0 :             return true;
     813             :         }
     814           1 :         pType++;
     815             :     }
     816             : 
     817           2 :     return false;
     818             : }
     819             : 
     820             : //if the given control type in evt.Arguments[0] appears in the type list in pPara, then approve the execution
     821           0 : bool ApproveType(const ScriptEvent& evt, void* pPara)
     822             : {
     823           0 :     return FindControl(evt, pPara);
     824             : }
     825             : 
     826             : //if the given control type in evt.Arguments[0] appears in the type list in pPara, then deny the execution
     827           1 : bool DenyType(const ScriptEvent& evt, void* pPara)
     828             : {
     829           1 :     return !FindControl(evt, pPara);
     830             : }
     831             : 
     832             : //when mouse is moving, either the mouse button is pressed or some key is pressed can trigger the OO mouseDragged event,
     833             : //the former should be denyed, and the latter allowed, only by doing so can the VBA MouseMove event when the "Shift" key is
     834             : //pressed can be correctly triggered
     835           0 : bool DenyMouseDrag(const ScriptEvent& evt, void* )
     836             : {
     837           0 :     awt::MouseEvent aEvent;
     838           0 :     evt.Arguments[ 0 ] >>= aEvent;
     839           0 :     if (aEvent.Buttons == 0 )
     840             :     {
     841           0 :         return true;
     842             :     }
     843             :     else
     844             :     {
     845           0 :         return false;
     846           0 :     }
     847             : }
     848             : 
     849             : 
     850             : // EventListener
     851             : 
     852             : void
     853          29 : EventListener::firing_Impl(const ScriptEvent& evt, Any* pRet ) throw(RuntimeException)
     854             : {
     855             :     OSL_TRACE("EventListener::firing_Impl( FAKE VBA_EVENTS )");
     856             :     static const OUString vbaInterOp =
     857          29 :         OUString("VBAInterop");
     858             : 
     859             :     // let default handlers deal with non vba stuff
     860          29 :     if ( !evt.ScriptType.equals( vbaInterOp ) )
     861           2 :         return;
     862          28 :     lang::EventObject aEvent;
     863          28 :     evt.Arguments[ 0 ] >>= aEvent;
     864             :     OSL_TRACE("evt.MethodName is  %s", OUStringToOString( evt.MethodName, RTL_TEXTENCODING_UTF8 ).getStr() );
     865             :     OSL_TRACE("Argument[0] is  %s", OUStringToOString( comphelper::anyToString( evt.Arguments[0] ), RTL_TEXTENCODING_UTF8 ).getStr() );
     866             :     OSL_TRACE("Getting Control");
     867          56 :     OUString sName = "UserForm";
     868             :     OSL_TRACE("Getting Name");
     869             : 
     870          56 :     uno::Reference< awt::XDialog > xDlg( aEvent.Source, uno::UNO_QUERY );
     871          28 :     if ( !xDlg.is() )
     872             :     {
     873             :         OSL_TRACE("Getting Control");
     874             :         // evt.Source is
     875             :         // a) Dialog
     876             :         // b) xShapeControl ( from api (sheet control) )
     877             :         // c) eventmanager ( I guess )
     878             :         // d) vba control ( from api also )
     879          28 :         uno::Reference< drawing::XControlShape > xCntrlShape( evt.Source, uno::UNO_QUERY );
     880          56 :         uno::Reference< awt::XControl > xControl( aEvent.Source, uno::UNO_QUERY );
     881          28 :         if ( xCntrlShape.is() )
     882             :         {
     883             :             // for sheet controls ( that fire from the api ) we don't
     884             :             // have the real control ( thats only available from the view )
     885             :             // api code creates just a control instance that is transferred
     886             :             // via aEvent.Arguments[ 0 ] that control though has no
     887             :             // info like name etc.
     888             :             OSL_TRACE("Got control shape");
     889          16 :             uno::Reference< container::XNamed > xName( xCntrlShape->getControl(), uno::UNO_QUERY_THROW );
     890             :             OSL_TRACE("Got xnamed ");
     891          16 :             sName = xName->getName();
     892             :         }
     893             :         else
     894             :         {
     895             :             // Userform control ( fired from the api or from event manager )
     896          12 :             uno::Reference< beans::XPropertySet > xProps;
     897             :             OSL_TRACE("Getting properties");
     898          12 :             xProps.set( xControl->getModel(), uno::UNO_QUERY_THROW );
     899          12 :             xProps->getPropertyValue("Name") >>= sName;
     900          28 :         }
     901             :     }
     902             :     //dumpEvent( evt );
     903          28 :     EventInfoHash& infos = getEventTransInfo();
     904          28 :     EventInfoHash::const_iterator eventInfo_it = infos.find( evt.MethodName );
     905          28 :     EventInfoHash::const_iterator it_end = infos.end();
     906          28 :     if ( eventInfo_it == it_end )
     907             :     {
     908             :         OSL_TRACE("Bogus event for %s",
     909             :             OUStringToOString( evt.ScriptType, RTL_TEXTENCODING_UTF8 ).getStr() );
     910           0 :         return;
     911             :     }
     912             : 
     913          56 :     uno::Reference< script::provider::XScriptProviderSupplier > xSPS( m_xModel, uno::UNO_QUERY );
     914          56 :     uno::Reference< script::provider::XScriptProvider > xScriptProvider;
     915          28 :     if ( xSPS.is() )
     916             :     {
     917          28 :         xScriptProvider =  xSPS->getScriptProvider();
     918             :     }
     919          28 :     if ( xScriptProvider.is() && mpShell )
     920             :     {
     921             :         std::list< TranslateInfo >::const_iterator txInfo =
     922          28 :             eventInfo_it->second.begin();
     923          28 :         std::list< TranslateInfo >::const_iterator txInfo_end = eventInfo_it->second.end();
     924             : 
     925          28 :         BasicManager* pBasicManager = mpShell->GetBasicManager();
     926          28 :         OUString sProject;
     927          56 :         OUString sScriptCode( evt.ScriptCode );
     928             :         // dialogs pass their own library, presence of Dot determines that
     929          28 :         if ( sScriptCode.indexOf( '.' ) == -1 )
     930             :         {
     931             :             //'Project' is a better default but I want to force failures
     932             :             //OUString sMacroLoc("Project");
     933          16 :             sProject = "Standard";
     934             : 
     935          16 :             if (!pBasicManager->GetName().isEmpty())
     936             :             {
     937          16 :                 sProject =  pBasicManager->GetName();
     938             :             }
     939             :         }
     940             :         else
     941             :         {
     942          12 :             sal_Int32 nIndex = sScriptCode.indexOf( '.' );
     943          12 :             sProject = sScriptCode.copy( 0, nIndex );
     944          12 :             sScriptCode = sScriptCode.copy( nIndex + 1 );
     945             :         }
     946          56 :         OUString sMacroLoc = sProject;
     947          28 :         sMacroLoc = sMacroLoc.concat(  OUString(".") );
     948          28 :         sMacroLoc = sMacroLoc.concat( sScriptCode ).concat( OUString(".") );
     949             : 
     950             :         OSL_TRACE("sMacroLoc is %s", OUStringToOString( sMacroLoc, RTL_TEXTENCODING_UTF8 ).getStr() );
     951          70 :         for ( ; txInfo != txInfo_end; ++txInfo )
     952             :         {
     953             :             // If the document is closed, we should not execute macro.
     954          42 :             if (m_bDocClosed)
     955             :             {
     956           0 :                 break;
     957             :             }
     958             : 
     959          42 :             OUString sTemp = sName.concat( (*txInfo).sVBAName );
     960             :             // see if we have a match for the handlerextension
     961             :             // where ScriptCode is methodname_handlerextension
     962          84 :             OUString sToResolve = sMacroLoc.concat( sTemp );
     963             : 
     964             :             OSL_TRACE("*** trying to invoke %s ",
     965             :                 OUStringToOString( sToResolve, RTL_TEXTENCODING_UTF8 ).getStr() );
     966          84 :             ooo::vba::MacroResolvedInfo aMacroResolvedInfo = ooo::vba::resolveVBAMacro( mpShell, sToResolve );
     967          42 :             if ( aMacroResolvedInfo.mbFound )
     968             :             {
     969             : 
     970          11 :                 if (! txInfo->ApproveRule(evt, txInfo->pPara) )
     971             :                 {
     972           0 :                     continue;
     973             :                 }
     974             : 
     975             :                 // !! translate arguments & emulate events where necessary
     976          11 :                 Sequence< Any > aArguments;
     977          11 :                 if  ( (*txInfo).toVBA )
     978             :                 {
     979           1 :                     aArguments = (*txInfo).toVBA( evt.Arguments );
     980             :                 }
     981             :                 else
     982             :                 {
     983          10 :                     aArguments = evt.Arguments;
     984             :                 }
     985          11 :                 if ( aArguments.getLength() )
     986             :                 {
     987             :                     // call basic event handlers for event
     988             : 
     989             :                     // create script url
     990          11 :                     OUString url = aMacroResolvedInfo.msResolvedMacro;
     991             : 
     992             :                     OSL_TRACE("resolved script = %s",
     993             :                         OUStringToOString( url,
     994             :                             RTL_TEXTENCODING_UTF8 ).getStr() );
     995             :                     try
     996             :                     {
     997          11 :                         uno::Any aDummyCaller = uno::makeAny( OUString("Error") );
     998          11 :                         if ( pRet )
     999             :                         {
    1000           0 :                             ooo::vba::executeMacro( mpShell, url, aArguments, *pRet, aDummyCaller );
    1001             :                         }
    1002             :                         else
    1003             :                         {
    1004          11 :                             uno::Any aRet;
    1005          11 :                             ooo::vba::executeMacro( mpShell, url, aArguments, aRet, aDummyCaller );
    1006          11 :                         }
    1007             :                     }
    1008           0 :                     catch ( uno::Exception& e )
    1009             :                     {
    1010             :                         OSL_TRACE("event script raised %s", OUStringToOString( e.Message, RTL_TEXTENCODING_UTF8 ).getStr() );
    1011          11 :                     }
    1012          11 :                }
    1013             :            }
    1014          70 :        }
    1015          28 :     }
    1016             : }
    1017             : 
    1018             : typedef ::cppu::WeakImplHelper1< XVBAToOOEventDescGen > VBAToOOEventDescGen_BASE;
    1019             : 
    1020             : 
    1021          18 : class VBAToOOEventDescGen : public VBAToOOEventDescGen_BASE
    1022             : {
    1023             : public:
    1024             :     VBAToOOEventDescGen( const Reference< XComponentContext >& rxContext );
    1025             : 
    1026             :     // XVBAToOOEventDescGen
    1027             :     virtual Sequence< ScriptEventDescriptor > SAL_CALL getEventDescriptions( const OUString& sCtrlServiceName, const OUString& sCodeName ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
    1028             :     virtual Reference< XScriptEventsSupplier > SAL_CALL getEventSupplier( const Reference< XInterface >& xControl,  const OUString& sCodeName ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
    1029             : private:
    1030             :     Reference< XComponentContext > m_xContext;
    1031             : 
    1032             : };
    1033             : 
    1034           9 : VBAToOOEventDescGen::VBAToOOEventDescGen( const Reference< XComponentContext >& rxContext ):m_xContext( rxContext ) {}
    1035             : 
    1036             : Sequence< ScriptEventDescriptor > SAL_CALL
    1037           1 : VBAToOOEventDescGen::getEventDescriptions( const OUString& sCntrlServiceName, const OUString& sCodeName ) throw (RuntimeException, std::exception)
    1038             : {
    1039           1 :     ScriptEventHelper evntHelper( sCntrlServiceName );
    1040           1 :     return evntHelper.createEvents( sCodeName );
    1041             : }
    1042             : 
    1043             : Reference< XScriptEventsSupplier > SAL_CALL
    1044           8 : VBAToOOEventDescGen::getEventSupplier( const Reference< XInterface >& xControl, const OUString& sCodeName  ) throw (::com::sun::star::uno::RuntimeException, std::exception)
    1045             : {
    1046           8 :     ScriptEventHelper evntHelper( xControl );
    1047             :     Reference< XScriptEventsSupplier > xSupplier =
    1048             :         new ReadOnlyEventsSupplier(
    1049           8 :             evntHelper.getEventListeners(), sCodeName ) ;
    1050           8 :     return xSupplier;
    1051             : }
    1052             : 
    1053             : // Component related
    1054             : 
    1055             : namespace evtlstner
    1056             : {
    1057           5 :     OUString SAL_CALL getImplementationName()
    1058             :     {
    1059             :         static OUString* pImplName = 0;
    1060           5 :         if ( !pImplName )
    1061             :         {
    1062           2 :             ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
    1063           2 :             if ( !pImplName )
    1064             :             {
    1065           2 :                 static OUString aImplName( "ooo.vba.EventListener"  );
    1066           2 :                 pImplName = &aImplName;
    1067           2 :             }
    1068             :         }
    1069           5 :         return *pImplName;
    1070             :     }
    1071             : 
    1072          31 :     uno::Reference< XInterface > SAL_CALL create(
    1073             :     Reference< XComponentContext > const & xContext )
    1074             :     SAL_THROW(())
    1075             :     {
    1076          31 :         return static_cast< lang::XTypeProvider * >( new EventListener( xContext ) );
    1077             :     }
    1078             : 
    1079           2 :     Sequence< OUString > SAL_CALL getSupportedServiceNames()
    1080             :     {
    1081           2 :         const OUString strName( ::evtlstner::getImplementationName() );
    1082           2 :         return Sequence< OUString >( &strName, 1 );
    1083             :     }
    1084             : }
    1085             : namespace ooevtdescgen
    1086             : {
    1087           4 :     OUString SAL_CALL getImplementationName()
    1088             :     {
    1089             :         static OUString* pImplName = 0;
    1090           4 :         if ( !pImplName )
    1091             :         {
    1092           2 :             ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
    1093           2 :             if ( !pImplName )
    1094             :             {
    1095           2 :                 static OUString aImplName( "ooo.vba.VBAToOOEventDesc"  );
    1096           2 :                 pImplName = &aImplName;
    1097           2 :             }
    1098             :         }
    1099           4 :         return *pImplName;
    1100             :     }
    1101             : 
    1102           9 :     uno::Reference< XInterface > SAL_CALL create(
    1103             :         Reference< XComponentContext > const & xContext )
    1104             :         SAL_THROW(())
    1105             :     {
    1106           9 :         return static_cast< lang::XTypeProvider * >( new VBAToOOEventDescGen( xContext ) );
    1107             :     }
    1108             : 
    1109           1 :     Sequence< OUString > SAL_CALL getSupportedServiceNames()
    1110             :     {
    1111           1 :         const OUString strName( ::ooevtdescgen::getImplementationName() );
    1112           1 :         return Sequence< OUString >( &strName, 1 );
    1113             :     }
    1114           6 : }
    1115             : 
    1116             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10