bump product version to 7.2.5.1
[LibreOffice.git] / scripting / source / dlgprov / dlgevtatt.cxx
blob43b5851c225a7eb2c66bdca1cc98457d481f42dc
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 "dlgevtatt.hxx"
22 #include "dlgprov.hxx"
24 #include <sfx2/strings.hrc>
25 #include <sfx2/sfxresid.hxx>
26 #include <vcl/svapp.hxx>
27 #include <vcl/weld.hxx>
28 #include <tools/diagnose_ex.h>
30 #include <com/sun/star/awt/XControl.hpp>
31 #include <com/sun/star/awt/XControlContainer.hpp>
32 #include <com/sun/star/awt/XDialogEventHandler.hpp>
33 #include <com/sun/star/awt/XContainerWindowEventHandler.hpp>
34 #include <com/sun/star/beans/XPropertySet.hpp>
35 #include <com/sun/star/script/ScriptEventDescriptor.hpp>
36 #include <com/sun/star/script/XScriptEventsSupplier.hpp>
37 #include <com/sun/star/script/provider/XScriptProvider.hpp>
38 #include <com/sun/star/script/provider/theMasterScriptProviderFactory.hpp>
39 #include <com/sun/star/script/provider/XScriptProviderSupplier.hpp>
40 #include <com/sun/star/script/vba/XVBACompatibility.hpp>
41 #include <com/sun/star/lang/ServiceNotRegisteredException.hpp>
42 #include <com/sun/star/reflection/XIdlMethod.hpp>
43 #include <com/sun/star/beans/MethodConcept.hpp>
44 #include <com/sun/star/beans/XMaterialHolder.hpp>
46 #include <ooo/vba/XVBAToOOEventDescGen.hpp>
48 using namespace ::com::sun::star;
49 using namespace ::com::sun::star::awt;
50 using namespace ::com::sun::star::beans;
51 using namespace ::com::sun::star::lang;
52 using namespace ::com::sun::star::script;
53 using namespace ::com::sun::star::uno;
54 using namespace ::com::sun::star::reflection;
57 namespace dlgprov
59 namespace {
61 class DialogSFScriptListenerImpl : public DialogScriptListenerImpl
63 protected:
64 Reference< frame::XModel > m_xModel;
65 virtual void firing_impl( const script::ScriptEvent& aScriptEvent, uno::Any* pRet ) override;
66 public:
67 DialogSFScriptListenerImpl( const Reference< XComponentContext >& rxContext, const Reference< frame::XModel >& rxModel ) : DialogScriptListenerImpl( rxContext ), m_xModel( rxModel ) {}
70 class DialogLegacyScriptListenerImpl : public DialogSFScriptListenerImpl
72 protected:
73 virtual void firing_impl( const script::ScriptEvent& aScriptEvent, uno::Any* pRet ) override;
74 public:
75 DialogLegacyScriptListenerImpl( const Reference< XComponentContext >& rxContext, const Reference< frame::XModel >& rxModel ) : DialogSFScriptListenerImpl( rxContext, rxModel ){}
78 class DialogUnoScriptListenerImpl : public DialogSFScriptListenerImpl
80 Reference< awt::XControl > m_xControl;
81 Reference< XInterface > m_xHandler;
82 Reference< beans::XIntrospectionAccess > m_xIntrospectionAccess;
83 bool m_bDialogProviderMode;
85 virtual void firing_impl( const script::ScriptEvent& aScriptEvent, uno::Any* pRet ) override;
87 public:
88 DialogUnoScriptListenerImpl( const Reference< XComponentContext >& rxContext,
89 const Reference< frame::XModel >& rxModel,
90 const Reference< awt::XControl >& rxControl,
91 const Reference< XInterface >& rxHandler,
92 const Reference< beans::XIntrospectionAccess >& rxIntrospectionAccess,
93 bool bDialogProviderMode ); // false: ContainerWindowProvider mode
97 class DialogVBAScriptListenerImpl : public DialogScriptListenerImpl
99 protected:
100 OUString msDialogCodeName;
101 OUString msDialogLibName;
102 Reference< script::XScriptListener > mxListener;
103 virtual void firing_impl( const script::ScriptEvent& aScriptEvent, uno::Any* pRet ) override;
104 public:
105 DialogVBAScriptListenerImpl( const Reference< XComponentContext >& rxContext, const Reference< awt::XControl >& rxControl, const Reference< frame::XModel >& xModel, const OUString& sDialogLibName );
110 DialogVBAScriptListenerImpl::DialogVBAScriptListenerImpl( const Reference< XComponentContext >& rxContext, const Reference< awt::XControl >& rxControl, const Reference< frame::XModel >& xModel, const OUString& sDialogLibName ) : DialogScriptListenerImpl( rxContext ), msDialogLibName( sDialogLibName )
112 Reference< XMultiComponentFactory > xSMgr( m_xContext->getServiceManager() );
113 Sequence< Any > args(1);
114 if ( xSMgr.is() )
116 args[0] <<= xModel;
117 mxListener.set( xSMgr->createInstanceWithArgumentsAndContext( "ooo.vba.EventListener", args, m_xContext ), UNO_QUERY );
119 if ( !rxControl.is() )
120 return;
124 Reference< XPropertySet > xProps( rxControl->getModel(), UNO_QUERY_THROW );
125 xProps->getPropertyValue("Name") >>= msDialogCodeName;
126 xProps.set( mxListener, UNO_QUERY_THROW );
127 xProps->setPropertyValue("Model", args[ 0 ] );
129 catch( const Exception& )
131 DBG_UNHANDLED_EXCEPTION("scripting");
136 void DialogVBAScriptListenerImpl::firing_impl( const script::ScriptEvent& aScriptEvent, uno::Any* )
138 if ( !(aScriptEvent.ScriptType == "VBAInterop" && mxListener.is()) )
139 return;
141 ScriptEvent aScriptEventCopy( aScriptEvent );
142 aScriptEventCopy.ScriptCode = msDialogLibName + "." + msDialogCodeName;
145 mxListener->firing( aScriptEventCopy );
147 catch( const Exception& )
149 DBG_UNHANDLED_EXCEPTION("scripting");
154 // DialogEventsAttacherImpl
157 DialogEventsAttacherImpl::DialogEventsAttacherImpl( const Reference< XComponentContext >& rxContext, const Reference< frame::XModel >& rxModel, const Reference< awt::XControl >& rxControl, const Reference< XInterface >& rxHandler, const Reference< beans::XIntrospectionAccess >& rxIntrospect, bool bProviderMode, const Reference< script::XScriptListener >& rxRTLListener, const OUString& sDialogLibName )
158 :mbUseFakeVBAEvents( false ), m_xContext( rxContext )
160 // key listeners by protocol when ScriptType = 'Script'
161 // otherwise key is the ScriptType e.g. StarBasic
162 if ( rxRTLListener.is() ) // set up handler for RTL_BASIC
163 listenersForTypes[ OUString("StarBasic") ] = rxRTLListener;
164 else
165 listenersForTypes[ OUString("StarBasic") ] = new DialogLegacyScriptListenerImpl( rxContext, rxModel );
166 // handler for Script & OUString("vnd.sun.star.UNO:")
167 listenersForTypes[ OUString("vnd.sun.star.UNO") ] = new DialogUnoScriptListenerImpl( rxContext, rxModel, rxControl, rxHandler, rxIntrospect, bProviderMode );
168 listenersForTypes[ OUString("vnd.sun.star.script") ] = new DialogSFScriptListenerImpl( rxContext, rxModel );
170 // determine the VBA compatibility mode from the Basic library container
173 uno::Reference< beans::XPropertySet > xModelProps( rxModel, uno::UNO_QUERY_THROW );
174 uno::Reference< script::vba::XVBACompatibility > xVBACompat(
175 xModelProps->getPropertyValue("BasicLibraries"), uno::UNO_QUERY_THROW );
176 mbUseFakeVBAEvents = xVBACompat->getVBACompatibilityMode();
178 catch( uno::Exception& )
181 if ( mbUseFakeVBAEvents )
182 listenersForTypes[ OUString("VBAInterop") ] = new DialogVBAScriptListenerImpl( rxContext, rxControl, rxModel, sDialogLibName );
186 DialogEventsAttacherImpl::~DialogEventsAttacherImpl()
191 Reference< script::XScriptListener > const &
192 DialogEventsAttacherImpl::getScriptListenerForKey( const OUString& sKey )
194 ListenerHash::iterator it = listenersForTypes.find( sKey );
195 if ( it == listenersForTypes.end() )
196 throw RuntimeException(); // more text info here please
197 return it->second;
199 Reference< XScriptEventsSupplier > DialogEventsAttacherImpl::getFakeVbaEventsSupplier( const Reference< XControl >& xControl, OUString const & sControlName )
201 Reference< XScriptEventsSupplier > xEventsSupplier;
202 Reference< XMultiComponentFactory > xSMgr( m_xContext->getServiceManager() );
203 if ( xSMgr.is() )
205 Reference< ooo::vba::XVBAToOOEventDescGen > xVBAToOOEvtDesc( xSMgr->createInstanceWithContext("ooo.vba.VBAToOOEventDesc", m_xContext ), UNO_QUERY );
206 if ( xVBAToOOEvtDesc.is() )
207 xEventsSupplier = xVBAToOOEvtDesc->getEventSupplier( xControl, sControlName );
210 return xEventsSupplier;
214 void DialogEventsAttacherImpl::attachEventsToControl( const Reference< XControl>& xControl, const Reference< XScriptEventsSupplier >& xEventsSupplier, const Any& Helper )
216 if ( !xEventsSupplier.is() )
217 return;
219 Reference< container::XNameContainer > xEventCont = xEventsSupplier->getEvents();
221 Reference< XControlModel > xControlModel = xControl->getModel();
222 if ( !xEventCont.is() )
223 return;
225 const Sequence< OUString > aNames = xEventCont->getElementNames();
227 for ( const OUString& rName : aNames )
229 ScriptEventDescriptor aDesc;
231 Any aElement = xEventCont->getByName( rName );
232 aElement >>= aDesc;
233 OUString sKey = aDesc.ScriptType;
234 if ( aDesc.ScriptType == "Script" || aDesc.ScriptType == "UNO" )
236 sal_Int32 nIndex = aDesc.ScriptCode.indexOf( ':' );
237 sKey = aDesc.ScriptCode.copy( 0, nIndex );
239 Reference< XAllListener > xAllListener =
240 new DialogAllListenerImpl( getScriptListenerForKey( sKey ), aDesc.ScriptType, aDesc.ScriptCode );
242 // try first to attach event to the ControlModel
243 bool bSuccess = false;
246 Reference< XEventListener > xListener_ = m_xEventAttacher->attachSingleEventListener(
247 xControlModel, xAllListener, Helper, aDesc.ListenerType,
248 aDesc.AddListenerParam, aDesc.EventMethod );
250 if ( xListener_.is() )
251 bSuccess = true;
253 catch ( const Exception& )
255 DBG_UNHANDLED_EXCEPTION("scripting");
260 // if we had no success, try to attach to the control
261 if ( !bSuccess )
263 m_xEventAttacher->attachSingleEventListener(
264 xControl, xAllListener, Helper, aDesc.ListenerType,
265 aDesc.AddListenerParam, aDesc.EventMethod );
268 catch ( const Exception& )
270 DBG_UNHANDLED_EXCEPTION("scripting");
276 void DialogEventsAttacherImpl::nestedAttachEvents( const Sequence< Reference< XInterface > >& Objects, const Any& Helper, OUString& sDialogCodeName )
278 for ( const Reference< XInterface >& rObject : Objects )
280 // We know that we have to do with instances of XControl.
281 // Otherwise this is not the right implementation for
282 // XScriptEventsAttacher and we have to give up.
283 Reference< XControl > xControl( rObject, UNO_QUERY );
284 Reference< XControlContainer > xControlContainer( xControl, UNO_QUERY );
285 Reference< XDialog > xDialog( xControl, UNO_QUERY );
286 if ( !xControl.is() )
287 throw IllegalArgumentException();
289 // get XEventsSupplier from control model
290 Reference< XControlModel > xControlModel = xControl->getModel();
291 Reference< XScriptEventsSupplier > xEventsSupplier( xControlModel, UNO_QUERY );
292 attachEventsToControl( xControl, xEventsSupplier, Helper );
293 if ( mbUseFakeVBAEvents )
295 xEventsSupplier.set( getFakeVbaEventsSupplier( xControl, sDialogCodeName ) );
296 Any newHelper(xControl );
297 attachEventsToControl( xControl, xEventsSupplier, newHelper );
299 if ( xControlContainer.is() && !xDialog.is() )
301 Sequence< Reference< XControl > > aControls = xControlContainer->getControls();
302 sal_Int32 nControlCount = aControls.getLength();
304 Sequence< Reference< XInterface > > aObjects( nControlCount );
305 Reference< XInterface >* pObjects2 = aObjects.getArray();
306 const Reference< XControl >* pControls = aControls.getConstArray();
308 for ( sal_Int32 i2 = 0; i2 < nControlCount; ++i2 )
310 pObjects2[i2].set( pControls[i2], UNO_QUERY );
312 nestedAttachEvents( aObjects, Helper, sDialogCodeName );
318 // XScriptEventsAttacher
321 void SAL_CALL DialogEventsAttacherImpl::attachEvents( const Sequence< Reference< XInterface > >& Objects,
322 const css::uno::Reference<css::script::XScriptListener>&,
323 const Any& Helper )
325 // get EventAttacher
327 ::osl::MutexGuard aGuard( getMutex() );
329 if ( !m_xEventAttacher.is() )
331 Reference< XMultiComponentFactory > xSMgr( m_xContext->getServiceManager() );
332 if ( !xSMgr.is() )
333 throw RuntimeException();
335 m_xEventAttacher.set( xSMgr->createInstanceWithContext(
336 "com.sun.star.script.EventAttacher", m_xContext ), UNO_QUERY );
338 if ( !m_xEventAttacher.is() )
339 throw ServiceNotRegisteredException();
342 OUString sDialogCodeName;
343 sal_Int32 nObjCount = Objects.getLength();
344 Reference< awt::XControl > xDlgControl( Objects[ nObjCount - 1 ], uno::UNO_QUERY ); // last object is the dialog
345 if ( xDlgControl.is() )
347 Reference< XPropertySet > xProps( xDlgControl->getModel(), UNO_QUERY );
350 xProps->getPropertyValue("Name") >>= sDialogCodeName;
352 catch( Exception& ){}
354 // go over all objects
355 nestedAttachEvents( Objects, Helper, sDialogCodeName );
359 // DialogAllListenerImpl
362 DialogAllListenerImpl::DialogAllListenerImpl( const Reference< XScriptListener >& rxListener,
363 const OUString& rScriptType, const OUString& rScriptCode )
364 :m_xScriptListener( rxListener )
365 ,m_sScriptType( rScriptType )
366 ,m_sScriptCode( rScriptCode )
371 DialogAllListenerImpl::~DialogAllListenerImpl()
376 void DialogAllListenerImpl::firing_impl( const AllEventObject& Event, Any* pRet )
378 ScriptEvent aScriptEvent;
379 aScriptEvent.Source = static_cast<OWeakObject *>(this); // get correct XInterface
380 aScriptEvent.ListenerType = Event.ListenerType;
381 aScriptEvent.MethodName = Event.MethodName;
382 aScriptEvent.Arguments = Event.Arguments;
383 aScriptEvent.Helper = Event.Helper;
384 aScriptEvent.ScriptType = m_sScriptType;
385 aScriptEvent.ScriptCode = m_sScriptCode;
387 if ( m_xScriptListener.is() )
389 if ( pRet )
390 *pRet = m_xScriptListener->approveFiring( aScriptEvent );
391 else
392 m_xScriptListener->firing( aScriptEvent );
397 // XEventListener
400 void DialogAllListenerImpl::disposing(const EventObject& )
405 // XAllListener
408 void DialogAllListenerImpl::firing( const AllEventObject& Event )
410 //::osl::MutexGuard aGuard( getMutex() );
412 firing_impl( Event, nullptr );
416 Any DialogAllListenerImpl::approveFiring( const AllEventObject& Event )
418 //::osl::MutexGuard aGuard( getMutex() );
420 Any aReturn;
421 firing_impl( Event, &aReturn );
422 return aReturn;
426 // DialogScriptListenerImpl
429 DialogUnoScriptListenerImpl::DialogUnoScriptListenerImpl( const Reference< XComponentContext >& rxContext,
430 const Reference< css::frame::XModel >& rxModel,
431 const Reference< css::awt::XControl >& rxControl,
432 const Reference< css::uno::XInterface >& rxHandler,
433 const Reference< css::beans::XIntrospectionAccess >& rxIntrospectionAccess,
434 bool bDialogProviderMode )
435 : DialogSFScriptListenerImpl( rxContext, rxModel )
436 ,m_xControl( rxControl )
437 ,m_xHandler( rxHandler )
438 ,m_xIntrospectionAccess( rxIntrospectionAccess )
439 ,m_bDialogProviderMode( bDialogProviderMode )
444 DialogScriptListenerImpl::~DialogScriptListenerImpl()
449 void DialogSFScriptListenerImpl::firing_impl( const ScriptEvent& aScriptEvent, Any* pRet )
453 Reference< provider::XScriptProvider > xScriptProvider;
454 if ( m_xModel.is() )
456 Reference< provider::XScriptProviderSupplier > xSupplier( m_xModel, UNO_QUERY );
457 OSL_ENSURE( xSupplier.is(), "DialogScriptListenerImpl::firing_impl: failed to get script provider supplier" );
458 if ( xSupplier.is() )
459 xScriptProvider.set( xSupplier->getScriptProvider() );
461 else
463 OSL_ASSERT( m_xContext.is() );
464 if ( m_xContext.is() )
466 Reference< provider::XScriptProviderFactory > xFactory =
467 provider::theMasterScriptProviderFactory::get( m_xContext );
469 Any aCtx;
470 aCtx <<= OUString("user");
471 xScriptProvider = xFactory->createScriptProvider( aCtx );
475 OSL_ENSURE( xScriptProvider.is(), "DialogScriptListenerImpl::firing_impl: failed to get script provider" );
477 if ( xScriptProvider.is() )
479 Reference< provider::XScript > xScript = xScriptProvider->getScript( aScriptEvent.ScriptCode );
480 OSL_ENSURE( xScript.is(), "DialogScriptListenerImpl::firing_impl: failed to get script" );
482 if ( xScript.is() )
484 Sequence< Any > aInParams;
485 Sequence< sal_Int16 > aOutParamsIndex;
486 Sequence< Any > aOutParams;
488 // get arguments for script
489 aInParams = aScriptEvent.Arguments;
491 Any aResult = xScript->invoke( aInParams, aOutParamsIndex, aOutParams );
492 if ( pRet )
493 *pRet = aResult;
497 catch ( const Exception& )
499 DBG_UNHANDLED_EXCEPTION("scripting");
503 void DialogLegacyScriptListenerImpl::firing_impl( const ScriptEvent& aScriptEvent, Any* pRet )
505 OUString sScriptURL;
506 OUString sScriptCode( aScriptEvent.ScriptCode );
508 if ( aScriptEvent.ScriptType != "StarBasic" )
509 return;
511 // StarBasic script: convert ScriptCode to scriptURL
512 sal_Int32 nIndex = sScriptCode.indexOf( ':' );
513 if ( nIndex >= 0 && nIndex < sScriptCode.getLength() )
515 sScriptURL = OUString::Concat("vnd.sun.star.script:") +
516 sScriptCode.subView( nIndex + 1 ) +
517 "?language=Basic&location=" +
518 sScriptCode.subView( 0, nIndex );
520 ScriptEvent aSFScriptEvent( aScriptEvent );
521 aSFScriptEvent.ScriptCode = sScriptURL;
522 DialogSFScriptListenerImpl::firing_impl( aSFScriptEvent, pRet );
525 void DialogUnoScriptListenerImpl::firing_impl( const ScriptEvent& aScriptEvent, Any* pRet )
527 OUString aMethodName = aScriptEvent.ScriptCode.copy( strlen("vnd.sun.star.UNO:") );
529 const Any* pArguments = aScriptEvent.Arguments.getConstArray();
530 Any aEventObject = pArguments[0];
532 bool bHandled = false;
533 if( m_xHandler.is() )
535 if( m_bDialogProviderMode )
537 Reference< XDialogEventHandler > xDialogEventHandler( m_xHandler, UNO_QUERY );
538 if( xDialogEventHandler.is() )
540 Reference< XDialog > xDialog( m_xControl, UNO_QUERY );
541 bHandled = xDialogEventHandler->callHandlerMethod( xDialog, aEventObject, aMethodName );
544 else
546 Reference< XContainerWindowEventHandler > xContainerWindowEventHandler( m_xHandler, UNO_QUERY );
547 if( xContainerWindowEventHandler.is() )
549 Reference< XWindow > xWindow( m_xControl, UNO_QUERY );
550 bHandled = xContainerWindowEventHandler->callHandlerMethod( xWindow, aEventObject, aMethodName );
555 Any aRet;
556 if( !bHandled && m_xIntrospectionAccess.is() )
560 // call method
561 const Reference< XIdlMethod >& rxMethod = m_xIntrospectionAccess->
562 getMethod( aMethodName, MethodConcept::ALL - MethodConcept::DANGEROUS );
564 Reference< XMaterialHolder > xMaterialHolder =
565 Reference< XMaterialHolder >::query( m_xIntrospectionAccess );
566 Any aHandlerObject = xMaterialHolder->getMaterial();
568 Sequence< Reference< XIdlClass > > aParamTypeSeq = rxMethod->getParameterTypes();
569 sal_Int32 nParamCount = aParamTypeSeq.getLength();
570 if( nParamCount == 0 )
572 Sequence<Any> args;
573 rxMethod->invoke( aHandlerObject, args );
574 bHandled = true;
576 else if( nParamCount == 2 )
578 // Signature check automatically done by reflection
579 Sequence<Any> Args(2);
580 Any* pArgs = Args.getArray();
581 if( m_bDialogProviderMode )
583 Reference< XDialog > xDialog( m_xControl, UNO_QUERY );
584 pArgs[0] <<= xDialog;
586 else
588 Reference< XWindow > xWindow( m_xControl, UNO_QUERY );
589 pArgs[0] <<= xWindow;
591 pArgs[1] = aEventObject;
592 aRet = rxMethod->invoke( aHandlerObject, Args );
593 bHandled = true;
596 catch( const Exception& )
598 DBG_UNHANDLED_EXCEPTION("scripting");
602 if( bHandled )
604 if( pRet )
605 *pRet = aRet;
607 else
609 OUString aRes(SfxResId(STR_ERRUNOEVENTBINDUNG));
610 OUString aQuoteChar( "\"" );
612 sal_Int32 nIndex = aRes.indexOf( '%' );
614 OUString aOUFinal =
615 aRes.subView( 0, nIndex ) +
616 aQuoteChar + aMethodName + aQuoteChar +
617 aRes.subView( nIndex + 2 );
619 std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(nullptr,
620 VclMessageType::Warning, VclButtonsType::Ok, aOUFinal));
621 xBox->run();
626 // XEventListener
629 void DialogScriptListenerImpl::disposing(const EventObject& )
634 // XScriptListener
637 void DialogScriptListenerImpl::firing( const ScriptEvent& aScriptEvent )
639 //::osl::MutexGuard aGuard( getMutex() );
641 firing_impl( aScriptEvent, nullptr );
645 Any DialogScriptListenerImpl::approveFiring( const ScriptEvent& aScriptEvent )
647 //::osl::MutexGuard aGuard( getMutex() );
649 Any aReturn;
650 firing_impl( aScriptEvent, &aReturn );
651 return aReturn;
655 } // namespace dlgprov
658 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */