Avoid potential negative array index access to cached text.
[LibreOffice.git] / scripting / source / vbaevents / eventhelper.cxx
blob9f4bdc1f72fe1cdbc9588e2ed8932d157ec82535
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/macros.h>
21 #include <sal/log.hxx>
22 #include <comphelper/processfactory.hxx>
23 #include <comphelper/uno3.hxx>
24 #include <comphelper/proparrhlp.hxx>
25 #include <comphelper/propertycontainer.hxx>
26 #include <comphelper/diagnose_ex.hxx>
28 #include <ooo/vba/XVBAToOOEventDescGen.hpp>
30 #include <com/sun/star/beans/XPropertySet.hpp>
31 #include <com/sun/star/beans/theIntrospection.hpp>
32 #include <com/sun/star/beans/PropertyAttribute.hpp>
34 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
35 #include <com/sun/star/lang/XServiceInfo.hpp>
36 #include <com/sun/star/lang/XInitialization.hpp>
38 #include <com/sun/star/util/XCloseListener.hpp>
39 #include <com/sun/star/util/XCloseBroadcaster.hpp>
41 #include <com/sun/star/frame/XModel.hpp>
43 #include <com/sun/star/script/ScriptEventDescriptor.hpp>
44 #include <com/sun/star/script/provider/XScriptProviderSupplier.hpp>
46 #include <com/sun/star/container/XNamed.hpp>
48 #include <com/sun/star/drawing/XControlShape.hpp>
50 #include <com/sun/star/awt/XControl.hpp>
51 #include <com/sun/star/awt/XDialog.hpp>
52 #include <com/sun/star/awt/KeyEvent.hpp>
53 #include <com/sun/star/awt/MouseEvent.hpp>
54 #include <com/sun/star/awt/XFixedText.hpp>
55 #include <com/sun/star/awt/XTextComponent.hpp>
56 #include <com/sun/star/awt/XComboBox.hpp>
57 #include <com/sun/star/awt/XRadioButton.hpp>
58 #include <com/sun/star/awt/XListBox.hpp>
60 #include <sfx2/objsh.hxx>
61 #include <basic/basmgr.hxx>
62 #include <filter/msfilter/msvbahelper.hxx>
63 #include <vbahelper/vbareturntypes.hxx>
65 #include <com/sun/star/script/XScriptListener.hpp>
66 #include <cppuhelper/implbase.hxx>
67 #include <cppuhelper/supportsservice.hxx>
68 #include <comphelper/evtmethodhelper.hxx>
70 #include <vector>
71 #include <unordered_map>
73 using namespace ::com::sun::star;
74 using namespace ::com::sun::star::script;
75 using namespace ::com::sun::star::uno;
76 using namespace ::ooo::vba;
78 // Some constants
79 constexpr std::u16string_view DELIM = u"::";
80 constexpr sal_Int32 DELIMLEN = DELIM.size();
82 static bool isKeyEventOk( awt::KeyEvent& evt, const Sequence< Any >& params )
84 return params.hasElements() && ( params[ 0 ] >>= evt );
87 static bool isMouseEventOk( awt::MouseEvent& evt, const Sequence< Any >& params )
89 return params.hasElements() && ( params[ 0 ] >>= evt );
92 static Sequence< Any > ooMouseEvtToVBADblClick( const Sequence< Any >& params )
94 awt::MouseEvent evt;
96 if ( !( isMouseEventOk(evt, params)) ||
97 (evt.ClickCount != 2) )
98 return Sequence< Any >();
99 // give back orig params, this will signal that the event is good
100 return params;
103 static Sequence< Any > ooMouseEvtToVBAMouseEvt( const Sequence< Any >& params )
105 awt::MouseEvent evt;
107 if ( !isMouseEventOk(evt, params) )
108 return Sequence< Any >();
110 Sequence< Any > translatedParams{ Any(evt.Buttons), // Buttons
111 Any(evt.Modifiers), // Shift
112 Any(evt.X), // X
113 Any(evt.Y) }; // Y
114 return translatedParams;
117 static Sequence< Any > ooKeyPressedToVBAKeyPressed( const Sequence< Any >& params )
119 awt::KeyEvent evt;
121 if ( !isKeyEventOk( evt, params ) )
122 return Sequence< Any >();
124 Reference< msforms::XReturnInteger> xKeyCode = new ReturnInteger( sal_Int32( evt.KeyCode ) );
125 Sequence< Any > translatedParams{ Any(xKeyCode) };
126 return translatedParams;
129 static Sequence< Any > ooKeyPressedToVBAKeyUpDown( const Sequence< Any >& params )
131 awt::KeyEvent evt;
133 if ( !isKeyEventOk( evt, params ) )
134 return Sequence< Any >();
136 Reference< msforms::XReturnInteger> xKeyCode = new ReturnInteger( evt.KeyCode );
137 sal_Int8 shift = sal::static_int_cast<sal_Int8>( evt.Modifiers );
139 // #TODO check whether values from OOO conform to values generated from vba
140 Sequence< Any > translatedParams{ Any(xKeyCode), Any(shift) };
141 return translatedParams;
144 typedef Sequence< Any > (*Translator)(const Sequence< Any >&);
146 namespace {
148 //expand the "TranslateInfo" struct to support more kinds of events
149 struct TranslateInfo
151 OUString sVBAName; //vba event name
152 Translator toVBA; //the method to convert OO event parameters to VBA event parameters
153 bool (*ApproveRule)(const ScriptEvent& evt, void const * pPara); //this method is used to determine which types of controls should execute the event
154 void const *pPara; //Parameters for the above approve method
159 typedef std::unordered_map<
160 OUString,
161 std::vector< TranslateInfo > > EventInfoHash;
163 namespace {
165 struct TranslatePropMap
167 OUString sEventInfo; //OO event name
168 TranslateInfo aTransInfo;
173 static bool ApproveAll(const ScriptEvent& evt, void const * pPara); //allow all types of controls to execute the event
174 static bool ApproveType(const ScriptEvent& evt, void const * pPara); //certain types of controls should execute the event, those types are given by pPara
175 static bool DenyType(const ScriptEvent& evt, void const * pPara); //certain types of controls should not execute the event, those types are given by pPara
176 static bool DenyMouseDrag(const ScriptEvent& evt, void const * pPara); //used for VBA MouseMove event when "Shift" key is pressed
178 namespace {
180 struct TypeList
182 uno::Type const * pTypeList;
183 int nListLength;
188 Type const typeXFixedText = cppu::UnoType<awt::XFixedText>::get();
189 Type const typeXTextComponent = cppu::UnoType<awt::XTextComponent>::get();
190 Type const typeXComboBox = cppu::UnoType<awt::XComboBox>::get();
191 Type const typeXRadioButton = cppu::UnoType<awt::XRadioButton>::get();
192 Type const typeXListBox = cppu::UnoType<awt::XListBox>::get();
195 TypeList const fixedTextList = {&typeXFixedText, 1};
196 TypeList const textCompList = {&typeXTextComponent, 1};
197 TypeList const radioButtonList = {&typeXRadioButton, 1};
198 TypeList const comboBoxList = {&typeXComboBox, 1};
199 TypeList const listBoxList = {&typeXListBox, 1};
201 //this array stores the OO event to VBA event translation info
202 static TranslatePropMap aTranslatePropMap_Impl[] =
204 { OUString("actionPerformed"), { OUString("_Change"), nullptr, DenyType, static_cast<void const *>(&radioButtonList) } },
205 // actionPerformed ooo event
206 { OUString("actionPerformed"), { OUString("_Click"), nullptr, ApproveAll, nullptr } },
207 { OUString("itemStateChanged"), { OUString("_Change"), nullptr, ApproveType, static_cast<void const *>(&radioButtonList) } },
208 // itemStateChanged ooo event
209 { OUString("itemStateChanged"), { OUString("_Click"), nullptr, ApproveType, static_cast<void const *>(&comboBoxList) } },
211 { OUString("itemStateChanged"), { OUString("_Click"), nullptr, ApproveType, static_cast<void const *>(&listBoxList) } },
212 // changed ooo event
213 { OUString("changed"), { OUString("_Change"), nullptr, ApproveAll, nullptr } },
215 // focusGained ooo event
216 { OUString("focusGained"), { OUString("_GotFocus"), nullptr, ApproveAll, nullptr } },
218 // focusLost ooo event
219 { OUString("focusLost"), { OUString("_LostFocus"), nullptr, ApproveAll, nullptr } },
220 { OUString("focusLost"), { OUString("_Exit"), nullptr, ApproveType, static_cast<void const *>(&textCompList) } }, // support VBA TextBox_Exit event
222 // adjustmentValueChanged ooo event
223 { OUString("adjustmentValueChanged"), { OUString("_Scroll"), nullptr, ApproveAll, nullptr } },
224 { OUString("adjustmentValueChanged"), { OUString("_Change"), nullptr, ApproveAll, nullptr } },
226 // textChanged ooo event
227 { OUString("textChanged"), { OUString("_Change"), nullptr, ApproveAll, nullptr } },
229 // keyReleased ooo event
230 { OUString("keyReleased"), { OUString("_KeyUp"), ooKeyPressedToVBAKeyUpDown, ApproveAll, nullptr } },
232 // mouseReleased ooo event
233 { OUString("mouseReleased"), { OUString("_Click"), ooMouseEvtToVBAMouseEvt, ApproveType, static_cast<void const *>(&fixedTextList) } },
234 { OUString("mouseReleased"), { OUString("_MouseUp"), ooMouseEvtToVBAMouseEvt, ApproveAll, nullptr } },
236 // mousePressed ooo event
237 { OUString("mousePressed"), { OUString("_MouseDown"), ooMouseEvtToVBAMouseEvt, ApproveAll, nullptr } },
238 { OUString("mousePressed"), { OUString("_DblClick"), ooMouseEvtToVBADblClick, ApproveAll, nullptr } },
240 // mouseMoved ooo event
241 { OUString("mouseMoved"), { OUString("_MouseMove"), ooMouseEvtToVBAMouseEvt, ApproveAll, nullptr } },
242 { OUString("mouseDragged"), { OUString("_MouseMove"), ooMouseEvtToVBAMouseEvt, DenyMouseDrag, nullptr } },
244 // keyPressed ooo event
245 { OUString("keyPressed"), { OUString("_KeyDown"), ooKeyPressedToVBAKeyUpDown, ApproveAll, nullptr } },
246 { OUString("keyPressed"), { OUString("_KeyPress"), ooKeyPressedToVBAKeyPressed, ApproveAll, nullptr } }
249 static EventInfoHash& getEventTransInfo()
251 static EventInfoHash eventTransInfo = []()
253 EventInfoHash tmp;
254 OUString sEventInfo;
255 TranslatePropMap* pTransProp = aTranslatePropMap_Impl;
256 int nCount = SAL_N_ELEMENTS(aTranslatePropMap_Impl);
258 int i = 0;
259 while (i < nCount)
261 sEventInfo = pTransProp->sEventInfo;
262 std::vector< TranslateInfo > infoList;
265 infoList.push_back( pTransProp->aTransInfo );
266 pTransProp++;
267 i++;
268 }while(i < nCount && sEventInfo == pTransProp->sEventInfo);
269 tmp[sEventInfo] = std::move(infoList);
271 return tmp;
272 }();
273 return eventTransInfo;
277 // Helper class
279 namespace {
281 class ScriptEventHelper
283 public:
284 explicit ScriptEventHelper( const Reference< XInterface >& xControl );
285 explicit ScriptEventHelper( const OUString& sCntrlServiceName );
286 ~ScriptEventHelper();
287 Sequence< ScriptEventDescriptor > createEvents( const OUString& sCodeName );
288 Sequence< OUString > getEventListeners() const;
289 private:
290 Reference< XComponentContext > m_xCtx;
291 Reference< XInterface > m_xControl;
292 bool m_bDispose;
297 static bool
298 eventMethodToDescriptor( std::u16string_view rEventMethod, ScriptEventDescriptor& evtDesc, const OUString& sCodeName )
300 // format of ControlListener is TypeName::methodname e.g.
301 // "com.sun.star.awt.XActionListener::actionPerformed" or
302 // "XActionListener::actionPerformed
304 OUString sMethodName;
305 OUString sTypeName;
306 size_t nDelimPos = rEventMethod.find( DELIM );
307 if ( nDelimPos == std::u16string_view::npos )
309 return false;
311 sMethodName = rEventMethod.substr( nDelimPos + DELIMLEN );
312 sTypeName = rEventMethod.substr( 0, nDelimPos );
314 EventInfoHash& infos = getEventTransInfo();
316 // Only create an ScriptEventDescriptor for an event we can translate
317 // or emulate
318 if ( !sMethodName.isEmpty()
319 && !sTypeName.isEmpty()
320 && ( infos.find( sMethodName ) != infos.end() ) )
322 // just fill in CodeName, when the event fires the other
323 // info is gathered from the event source to determine what
324 // event handler we try to call
325 evtDesc.ScriptCode = sCodeName;
326 evtDesc.ListenerType = sTypeName;
327 evtDesc.EventMethod = sMethodName;
329 // set this it VBAInterop, ensures that it doesn't
330 // get persisted or shown in property editors
331 evtDesc.ScriptType = "VBAInterop";
332 return true;
334 return false;
338 ScriptEventHelper::ScriptEventHelper( const Reference< XInterface >& xControl ) :
339 m_xCtx( comphelper::getProcessComponentContext() ),
340 m_xControl( xControl ),
341 m_bDispose( false )
344 ScriptEventHelper::ScriptEventHelper( const OUString& sCntrlServiceName ) :
345 m_xCtx( comphelper::getProcessComponentContext() ),
346 m_bDispose( true )
348 m_xControl.set( m_xCtx->getServiceManager()->createInstanceWithContext( sCntrlServiceName, m_xCtx ), uno::UNO_QUERY );
351 ScriptEventHelper::~ScriptEventHelper()
353 // dispose control ( and remove any associated event registrations )
354 if ( m_bDispose )
358 uno::Reference< lang::XComponent > xComp( m_xControl, uno::UNO_QUERY );
359 if (xComp)
360 xComp->dispose();
362 // destructor can't throw
363 catch( uno::Exception& )
369 Sequence< OUString >
370 ScriptEventHelper::getEventListeners() const
372 std::vector< OUString > eventMethods;
374 Reference< beans::XIntrospection > xIntrospection = beans::theIntrospection::get( m_xCtx );
376 Reference< beans::XIntrospectionAccess > xIntrospectionAccess =
377 xIntrospection->inspect( Any( m_xControl ) );
378 const Sequence< Type > aControlListeners =
379 xIntrospectionAccess->getSupportedListeners();
380 for ( const Type& listType : aControlListeners )
382 OUString sFullTypeName = listType.getTypeName();
383 const Sequence< OUString > sMeths =
384 comphelper::getEventMethodsForType( listType );
385 std::transform(sMeths.begin(), sMeths.end(), std::back_inserter(eventMethods),
386 [&sFullTypeName](const OUString& rMeth) -> OUString { return sFullTypeName + DELIM + rMeth; });
389 return comphelper::containerToSequence(eventMethods);
392 Sequence< ScriptEventDescriptor >
393 ScriptEventHelper::createEvents( const OUString& sCodeName )
395 const Sequence< OUString > aControlListeners = getEventListeners();
396 sal_Int32 nLength = aControlListeners.getLength();
398 Sequence< ScriptEventDescriptor > aDest( nLength );
399 sal_Int32 nEvts = 0;
400 for ( OUString const & i : aControlListeners)
402 // from getListeners eventName is of form
403 // "com.sun.star.awt.XActionListener::actionPerformed"
404 // we need to strip "com.sun.star.awt." from that for form
405 // controls
406 ScriptEventDescriptor evtDesc;
407 if ( eventMethodToDescriptor( i, evtDesc, sCodeName ) )
409 sal_Int32 dIndex = nEvts;
410 ++nEvts;
411 if ( nEvts > aDest.getLength() )
412 aDest.realloc( nEvts );// should never happen
413 aDest.getArray()[ dIndex ] = evtDesc;
416 aDest.realloc( nEvts );
418 return aDest;
422 typedef ::cppu::WeakImplHelper< container::XNameContainer > NameContainer_BASE;
424 namespace {
426 class ReadOnlyEventsNameContainer : public NameContainer_BASE
428 public:
429 ReadOnlyEventsNameContainer( const Sequence< OUString >& eventMethods, const OUString& sCodeName );
430 // XNameContainer
432 virtual void SAL_CALL insertByName( const OUString&, const Any& ) override
434 throw RuntimeException("ReadOnly container" );
437 virtual void SAL_CALL removeByName( const OUString& ) override
439 throw RuntimeException("ReadOnly container" );
442 // XNameReplace
443 virtual void SAL_CALL replaceByName( const OUString&, const Any& ) override
445 throw RuntimeException("ReadOnly container" );
449 // XNameAccess
450 virtual Any SAL_CALL getByName( const OUString& aName ) override;
451 virtual Sequence< OUString > SAL_CALL getElementNames( ) override;
452 virtual sal_Bool SAL_CALL hasByName( const OUString& aName ) override;
454 // XElementAccess
455 virtual Type SAL_CALL getElementType( ) override
456 { return cppu::UnoType<OUString>::get(); }
457 virtual sal_Bool SAL_CALL hasElements( ) override
458 { return !m_hEvents.empty(); }
459 private:
461 typedef std::unordered_map< OUString, Any > EventSupplierHash;
463 EventSupplierHash m_hEvents;
468 ReadOnlyEventsNameContainer::ReadOnlyEventsNameContainer( const Sequence< OUString >& eventMethods, const OUString& sCodeName )
470 for ( const OUString& rSrc : eventMethods )
472 Any aDesc;
473 ScriptEventDescriptor evtDesc;
474 if ( eventMethodToDescriptor( rSrc, evtDesc, sCodeName ) )
476 aDesc <<= evtDesc;
477 m_hEvents[ rSrc ] = aDesc;
482 Any SAL_CALL
483 ReadOnlyEventsNameContainer::getByName( const OUString& aName ){
484 EventSupplierHash::const_iterator it = m_hEvents.find( aName );
485 if ( it == m_hEvents.end() )
486 throw container::NoSuchElementException();
487 return it->second;
490 Sequence< OUString > SAL_CALL
491 ReadOnlyEventsNameContainer::getElementNames( )
493 return comphelper::mapKeysToSequence(m_hEvents);
496 sal_Bool SAL_CALL
497 ReadOnlyEventsNameContainer::hasByName( const OUString& aName )
499 EventSupplierHash::const_iterator it = m_hEvents.find( aName );
500 if ( it == m_hEvents.end() )
501 return false;
502 return true;
505 namespace {
507 class ReadOnlyEventsSupplier : public ::cppu::WeakImplHelper< XScriptEventsSupplier >
509 public:
510 ReadOnlyEventsSupplier( const Sequence< OUString >& eventMethods, const OUString& sCodeName )
511 { m_xNameContainer = new ReadOnlyEventsNameContainer( eventMethods, sCodeName ); }
513 // XScriptEventSupplier
514 virtual Reference< container::XNameContainer > SAL_CALL getEvents( ) override { return m_xNameContainer; }
515 private:
516 Reference< container::XNameContainer > m_xNameContainer;
521 typedef ::cppu::WeakImplHelper< XScriptListener, util::XCloseListener, lang::XInitialization, css::lang::XServiceInfo > EventListener_BASE;
523 #define EVENTLSTNR_PROPERTY_ID_MODEL 1
524 constexpr OUStringLiteral EVENTLSTNR_PROPERTY_MODEL = u"Model";
526 namespace {
528 class EventListener : public EventListener_BASE
529 ,public ::comphelper::OMutexAndBroadcastHelper
530 ,public ::comphelper::OPropertyContainer
531 ,public ::comphelper::OPropertyArrayUsageHelper< EventListener >
534 public:
535 EventListener();
536 // XEventListener
537 virtual void SAL_CALL disposing(const lang::EventObject& Source) override;
538 using cppu::OPropertySetHelper::disposing;
540 // XScriptListener
541 virtual void SAL_CALL firing(const ScriptEvent& evt) override;
542 virtual Any SAL_CALL approveFiring(const ScriptEvent& evt) override;
543 // XCloseListener
544 virtual void SAL_CALL queryClosing( const lang::EventObject& Source, sal_Bool GetsOwnership ) override;
545 virtual void SAL_CALL notifyClosing( const lang::EventObject& Source ) override;
546 // XPropertySet
547 virtual css::uno::Reference< css::beans::XPropertySetInfo > SAL_CALL getPropertySetInfo( ) override;
548 // XInitialization
549 virtual void SAL_CALL initialize( const Sequence< Any >& aArguments ) override;
550 // XInterface
551 DECLARE_XINTERFACE()
553 // XTypeProvider
554 DECLARE_XTYPEPROVIDER()
555 virtual void SAL_CALL setFastPropertyValue( sal_Int32 nHandle, const css::uno::Any& rValue ) override
557 if ( nHandle == EVENTLSTNR_PROPERTY_ID_MODEL )
559 uno::Reference< frame::XModel > xModel( rValue, uno::UNO_QUERY );
560 if( xModel != m_xModel)
562 // Remove the listener from the old XCloseBroadcaster.
563 uno::Reference< util::XCloseBroadcaster > xCloseBroadcaster( m_xModel, uno::UNO_QUERY );
564 if (xCloseBroadcaster.is())
566 xCloseBroadcaster->removeCloseListener( this );
568 // Add the listener into the new XCloseBroadcaster.
569 xCloseBroadcaster.set( xModel, uno::UNO_QUERY );
570 if (xCloseBroadcaster.is())
572 xCloseBroadcaster->addCloseListener( this );
576 OPropertyContainer::setFastPropertyValue( nHandle, rValue );
577 if ( nHandle == EVENTLSTNR_PROPERTY_ID_MODEL )
578 setShellFromModel();
581 OUString SAL_CALL getImplementationName() override
583 return "ooo.vba.EventListener";
586 sal_Bool SAL_CALL supportsService(OUString const & ServiceName) override
588 return cppu::supportsService(this, ServiceName);
591 css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override
593 return { getImplementationName() };
596 protected:
597 // OPropertySetHelper
598 virtual ::cppu::IPropertyArrayHelper& SAL_CALL getInfoHelper( ) override;
600 // OPropertyArrayUsageHelper
601 virtual ::cppu::IPropertyArrayHelper* createArrayHelper( ) const override;
603 private:
604 void setShellFromModel();
605 /// @throws RuntimeException
606 void firing_Impl( const ScriptEvent& evt, Any *pSyncRet );
608 Reference< frame::XModel > m_xModel;
609 bool m_bDocClosed;
610 SfxObjectShell* mpShell;
615 EventListener::EventListener() :
616 OPropertyContainer(GetBroadcastHelper()), m_bDocClosed(false), mpShell( nullptr )
618 registerProperty( EVENTLSTNR_PROPERTY_MODEL, EVENTLSTNR_PROPERTY_ID_MODEL,
619 beans::PropertyAttribute::TRANSIENT, &m_xModel, cppu::UnoType<decltype(m_xModel)>::get() );
622 void
623 EventListener::setShellFromModel()
625 // reset mpShell
626 mpShell = nullptr;
627 SfxObjectShell* pShell = SfxObjectShell::GetFirst();
628 while ( m_xModel.is() && pShell )
630 if ( pShell->GetModel() == m_xModel )
632 mpShell = pShell;
633 break;
635 pShell = SfxObjectShell::GetNext( *pShell );
639 //XEventListener
640 void
641 EventListener::disposing(const lang::EventObject&)
645 //XScriptListener
647 void SAL_CALL
648 EventListener::firing(const ScriptEvent& evt)
650 firing_Impl( evt, nullptr );
653 Any SAL_CALL
654 EventListener::approveFiring(const ScriptEvent& evt)
656 Any ret;
657 firing_Impl( evt, &ret );
658 return ret;
661 // XCloseListener
662 void SAL_CALL
663 EventListener::queryClosing( const lang::EventObject& /*Source*/, sal_Bool /*GetsOwnership*/ )
665 //Nothing to do
668 void SAL_CALL
669 EventListener::notifyClosing( const lang::EventObject& /*Source*/ )
671 m_bDocClosed = true;
672 uno::Reference< util::XCloseBroadcaster > xCloseBroadcaster( m_xModel, uno::UNO_QUERY );
673 if (xCloseBroadcaster.is())
675 xCloseBroadcaster->removeCloseListener( this );
679 // XInitialization
680 void SAL_CALL
681 EventListener::initialize( const Sequence< Any >& aArguments )
683 if ( aArguments.getLength() == 1 )
684 aArguments[0] >>= m_xModel;
685 SAL_INFO(
686 "scripting",
687 "args " << aArguments.getLength() << " m_xModel " << m_xModel.is());
690 // XInterface
692 IMPLEMENT_FORWARD_XINTERFACE2( EventListener, EventListener_BASE, OPropertyContainer )
694 // XTypeProvider
696 IMPLEMENT_FORWARD_XTYPEPROVIDER2( EventListener, EventListener_BASE, OPropertyContainer )
698 // OPropertySetHelper
700 ::cppu::IPropertyArrayHelper&
701 EventListener::getInfoHelper( )
703 return *getArrayHelper();
706 // OPropertyArrayUsageHelper
708 ::cppu::IPropertyArrayHelper*
709 EventListener::createArrayHelper( ) const
711 Sequence< beans::Property > aProps;
712 describeProperties( aProps );
713 return new ::cppu::OPropertyArrayHelper( aProps );
716 // XPropertySet
717 Reference< beans::XPropertySetInfo >
718 EventListener::getPropertySetInfo( )
720 Reference< beans::XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
721 return xInfo;
725 //decide if the control should execute the event
726 bool ApproveAll(SAL_UNUSED_PARAMETER const ScriptEvent&, SAL_UNUSED_PARAMETER void const * )
728 return true;
731 //for the given control type in evt.Arguments[0], look for if it appears in the type list in pPara
732 static bool FindControl(const ScriptEvent& evt, void const * pPara)
734 lang::EventObject aEvent;
735 evt.Arguments[ 0 ] >>= aEvent;
736 uno::Reference< uno::XInterface > xInterface( aEvent.Source, uno::UNO_QUERY );
738 TypeList const * pTypeListInfo = static_cast<TypeList const *>(pPara);
739 Type const * pType = pTypeListInfo->pTypeList;
740 int nLen = pTypeListInfo->nListLength;
742 for (int i = 0; i < nLen; i++)
744 if ( xInterface->queryInterface( *pType ).hasValue() )
746 return true;
748 pType++;
751 return false;
754 //if the given control type in evt.Arguments[0] appears in the type list in pPara, then approve the execution
755 bool ApproveType(const ScriptEvent& evt, void const * pPara)
757 return FindControl(evt, pPara);
760 //if the given control type in evt.Arguments[0] appears in the type list in pPara, then deny the execution
761 bool DenyType(const ScriptEvent& evt, void const * pPara)
763 return !FindControl(evt, pPara);
766 //when mouse is moving, either the mouse button is pressed or some key is pressed can trigger the OO mouseDragged event,
767 //the former should be denied, and the latter allowed, only by doing so can the VBA MouseMove event when the "Shift" key is
768 //pressed can be correctly triggered
769 bool DenyMouseDrag(const ScriptEvent& evt, SAL_UNUSED_PARAMETER void const * )
771 awt::MouseEvent aEvent;
772 evt.Arguments[ 0 ] >>= aEvent;
773 return aEvent.Buttons == 0;
777 // EventListener
779 void
780 EventListener::firing_Impl(const ScriptEvent& evt, Any* pRet )
782 // let default handlers deal with non vba stuff
783 if ( evt.ScriptType != "VBAInterop" )
784 return;
785 lang::EventObject aEvent;
786 evt.Arguments[ 0 ] >>= aEvent;
787 OUString sName = "UserForm";
789 uno::Reference< awt::XDialog > xDlg( aEvent.Source, uno::UNO_QUERY );
790 if ( !xDlg.is() )
792 // evt.Source is
793 // a) Dialog
794 // b) xShapeControl ( from api (sheet control) )
795 // c) eventmanager ( I guess )
796 // d) vba control ( from api also )
797 uno::Reference< drawing::XControlShape > xCntrlShape( evt.Source, uno::UNO_QUERY );
798 uno::Reference< awt::XControl > xControl( aEvent.Source, uno::UNO_QUERY );
799 if ( xCntrlShape.is() )
801 // for sheet controls ( that fire from the api ) we don't
802 // have the real control ( that's only available from the view )
803 // api code creates just a control instance that is transferred
804 // via aEvent.Arguments[ 0 ] that control though has no
805 // info like name etc.
806 uno::Reference< container::XNamed > xName( xCntrlShape->getControl(), uno::UNO_QUERY_THROW );
807 sName = xName->getName();
809 else
811 // Userform control ( fired from the api or from event manager )
812 uno::Reference< beans::XPropertySet > xProps;
813 xProps.set( xControl->getModel(), uno::UNO_QUERY_THROW );
814 xProps->getPropertyValue("Name") >>= sName;
817 //dumpEvent( evt );
818 EventInfoHash& infos = getEventTransInfo();
819 EventInfoHash::const_iterator eventInfo_it = infos.find( evt.MethodName );
820 EventInfoHash::const_iterator it_end = infos.end();
821 if ( eventInfo_it == it_end )
823 SAL_WARN("scripting", "Bogus event for " << evt.ScriptType );
824 return;
827 uno::Reference< script::provider::XScriptProviderSupplier > xSPS( m_xModel, uno::UNO_QUERY );
828 uno::Reference< script::provider::XScriptProvider > xScriptProvider;
829 if ( xSPS.is() )
831 xScriptProvider = xSPS->getScriptProvider();
833 if ( !(xScriptProvider.is() && mpShell) )
834 return;
836 BasicManager* pBasicManager = mpShell->GetBasicManager();
837 OUString sProject;
838 OUString sScriptCode( evt.ScriptCode );
839 // dialogs pass their own library, presence of Dot determines that
840 if ( sScriptCode.indexOf( '.' ) == -1 )
842 //'Project' is a better default but I want to force failures
843 //OUString sMacroLoc("Project");
844 sProject = "Standard";
846 if (!pBasicManager->GetName().isEmpty())
848 sProject = pBasicManager->GetName();
851 else
853 sal_Int32 nIndex = sScriptCode.indexOf( '.' );
854 sProject = sScriptCode.copy( 0, nIndex );
855 sScriptCode = sScriptCode.copy( nIndex + 1 );
857 OUString sMacroLoc = sProject + "." + sScriptCode + ".";
859 for (const auto& rTxInfo : eventInfo_it->second)
861 // If the document is closed, we should not execute macro.
862 if (m_bDocClosed)
864 break;
867 // see if we have a match for the handlerextension
868 // where ScriptCode is methodname_handlerextension
869 OUString sToResolve = sMacroLoc + sName + rTxInfo.sVBAName;
871 ooo::vba::MacroResolvedInfo aMacroResolvedInfo = ooo::vba::resolveVBAMacro( mpShell, sToResolve );
872 if ( aMacroResolvedInfo.mbFound )
875 if (! rTxInfo.ApproveRule(evt, rTxInfo.pPara) )
877 continue;
880 // !! translate arguments & emulate events where necessary
881 Sequence< Any > aArguments;
882 if ( rTxInfo.toVBA )
884 aArguments = rTxInfo.toVBA( evt.Arguments );
886 else
888 aArguments = evt.Arguments;
890 if ( aArguments.hasElements() )
892 // call basic event handlers for event
894 // create script url
895 OUString url = aMacroResolvedInfo.msResolvedMacro;
898 uno::Any aDummyCaller( OUString("Error") );
899 if ( pRet )
901 ooo::vba::executeMacro( mpShell, url, aArguments, *pRet, aDummyCaller );
903 else
905 uno::Any aRet;
906 ooo::vba::executeMacro( mpShell, url, aArguments, aRet, aDummyCaller );
909 catch ( const uno::Exception& )
911 TOOLS_WARN_EXCEPTION("scripting", "event script raised" );
918 namespace {
920 class VBAToOOEventDescGen : public ::cppu::WeakImplHelper< XVBAToOOEventDescGen, css::lang::XServiceInfo >
922 public:
923 VBAToOOEventDescGen();
925 // XVBAToOOEventDescGen
926 virtual Sequence< ScriptEventDescriptor > SAL_CALL getEventDescriptions( const OUString& sCtrlServiceName, const OUString& sCodeName ) override;
927 virtual Reference< XScriptEventsSupplier > SAL_CALL getEventSupplier( const Reference< XInterface >& xControl, const OUString& sCodeName ) override;
929 OUString SAL_CALL getImplementationName() override
931 return "ooo.vba.VBAToOOEventDesc";
934 sal_Bool SAL_CALL supportsService(OUString const & ServiceName) override
936 return cppu::supportsService(this, ServiceName);
939 css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override
941 return { getImplementationName() };
948 VBAToOOEventDescGen::VBAToOOEventDescGen() {}
950 Sequence< ScriptEventDescriptor > SAL_CALL
951 VBAToOOEventDescGen::getEventDescriptions( const OUString& sCntrlServiceName, const OUString& sCodeName )
953 ScriptEventHelper evntHelper( sCntrlServiceName );
954 return evntHelper.createEvents( sCodeName );
957 Reference< XScriptEventsSupplier > SAL_CALL
958 VBAToOOEventDescGen::getEventSupplier( const Reference< XInterface >& xControl, const OUString& sCodeName )
960 ScriptEventHelper evntHelper( xControl );
961 Reference< XScriptEventsSupplier > xSupplier =
962 new ReadOnlyEventsSupplier(
963 evntHelper.getEventListeners(), sCodeName ) ;
964 return xSupplier;
967 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
968 ooo_vba_EventListener_get_implementation(css::uno::XComponentContext*,
969 css::uno::Sequence<css::uno::Any> const &)
971 return cppu::acquire(new EventListener);
975 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
976 ooo_vba_VBAToOOEventDesc_get_implementation(css::uno::XComponentContext*,
977 css::uno::Sequence<css::uno::Any> const &)
979 return cppu::acquire(new VBAToOOEventDescGen);
983 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */