diff options
author | Matthew J. Francis <mjay.francis@gmail.com> | 2015-10-03 04:31:48 +0800 |
---|---|---|
committer | Matthew Francis <mjay.francis@gmail.com> | 2015-10-16 12:03:30 +0000 |
commit | 59c38539bcabf3a5c949745ff8f501ed7c98a528 (patch) | |
tree | f53668f6e0f0554171718b3491b8ff0183598a21 | |
parent | fa2a7665194c7fff311d14932ee7930f80e6c7e2 (diff) |
Allow injection of keyboard and mouse events through UNO
Change-Id: I3d139c6378f5274be1e7bfd88f72d1576c13243d
Reviewed-on: https://gerrit.libreoffice.org/19321
Reviewed-by: Matthew Francis <mjay.francis@gmail.com>
Tested-by: Matthew Francis <mjay.francis@gmail.com>
-rw-r--r-- | include/toolkit/helper/vclunohelper.hxx | 4 | ||||
-rw-r--r-- | offapi/UnoApi_offapi.mk | 1 | ||||
-rw-r--r-- | offapi/com/sun/star/awt/XToolkitRobot.idl | 40 | ||||
-rw-r--r-- | toolkit/source/awt/vclxtoolkit.cxx | 99 | ||||
-rw-r--r-- | toolkit/source/helper/vclunohelper.cxx | 20 |
5 files changed, 164 insertions, 0 deletions
diff --git a/include/toolkit/helper/vclunohelper.hxx b/include/toolkit/helper/vclunohelper.hxx index 0b3e944be19b..a03e89655b18 100644 --- a/include/toolkit/helper/vclunohelper.hxx +++ b/include/toolkit/helper/vclunohelper.hxx @@ -145,11 +145,15 @@ public: const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _rxContext ); + static ::MouseEvent createVCLMouseEvent( const ::com::sun::star::awt::MouseEvent& _rAwtEvent ); + static ::com::sun::star::awt::KeyEvent createKeyEvent( const ::KeyEvent& _rVclEvent, const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _rxContext ); + + static ::KeyEvent createVCLKeyEvent( const ::com::sun::star::awt::KeyEvent& _rAwtEvent ); }; diff --git a/offapi/UnoApi_offapi.mk b/offapi/UnoApi_offapi.mk index 0ede4c2b41b4..c8880078ed6f 100644 --- a/offapi/UnoApi_offapi.mk +++ b/offapi/UnoApi_offapi.mk @@ -1889,6 +1889,7 @@ $(eval $(call gb_UnoApi_add_idlfiles,offapi,com/sun/star/awt,\ XToolkit \ XToolkit2 \ XToolkitExperimental \ + XToolkitRobot \ XTopWindow \ XTopWindow2 \ XTopWindowListener \ diff --git a/offapi/com/sun/star/awt/XToolkitRobot.idl b/offapi/com/sun/star/awt/XToolkitRobot.idl new file mode 100644 index 000000000000..a7b430dc8618 --- /dev/null +++ b/offapi/com/sun/star/awt/XToolkitRobot.idl @@ -0,0 +1,40 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#ifndef __com_sun_star_awt_XToolkitRobot_idl__ +#define __com_sun_star_awt_XToolkitRobot_idl__ + +#include <com/sun/star/awt/KeyEvent.idl> +#include <com/sun/star/awt/XTopWindow.idl> + +module com { module sun { module star { module awt { + +/** Allows injection of keyboard and mouse events + */ + +interface XToolkitRobot +{ + + void keyPress( [in] com::sun::star::awt::KeyEvent aKeyEvent ); + + void keyRelease( [in] com::sun::star::awt::KeyEvent aKeyEvent ); + + void mousePress( [in] com::sun::star::awt::MouseEvent aMouseEvent ); + + void mouseRelease( [in] com::sun::star::awt::MouseEvent aMouseEvent ); + + void mouseMove( [in] com::sun::star::awt::MouseEvent aMouseEvent ); + +}; + +}; }; }; }; + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/toolkit/source/awt/vclxtoolkit.cxx b/toolkit/source/awt/vclxtoolkit.cxx index 31ed8c409efd..f97be03731bc 100644 --- a/toolkit/source/awt/vclxtoolkit.cxx +++ b/toolkit/source/awt/vclxtoolkit.cxx @@ -42,6 +42,7 @@ #include <com/sun/star/datatransfer/clipboard/SystemClipboard.hpp> #include <com/sun/star/lang/XServiceInfo.hpp> #include <com/sun/star/awt/XToolkitExperimental.hpp> +#include <com/sun/star/awt/XToolkitRobot.hpp> #include <com/sun/star/awt/XMessageBoxFactory.hpp> #include <cppuhelper/bootstrap.hxx> @@ -149,6 +150,7 @@ protected: class VCLXToolkit : public VCLXToolkitMutexHelper, public cppu::WeakComponentImplHelper< css::awt::XToolkitExperimental, + css::awt::XToolkitRobot, css::lang::XServiceInfo > { css::uno::Reference< css::datatransfer::clipboard::XClipboard > mxClipboard; @@ -277,6 +279,23 @@ public: // css::awt::XReschedule: virtual void SAL_CALL reschedule() throw (css::uno::RuntimeException, std::exception) override; + + // css:awt:XToolkitRobot + virtual void SAL_CALL keyPress( const css::awt::KeyEvent & aKeyEvent ) + throw (css::uno::RuntimeException, std::exception) override; + + virtual void SAL_CALL keyRelease( const css::awt::KeyEvent & aKeyEvent ) + throw (css::uno::RuntimeException, std::exception) override; + + virtual void SAL_CALL mousePress( const css::awt::MouseEvent & aMouseEvent ) + throw (css::uno::RuntimeException, std::exception) override; + + virtual void SAL_CALL mouseRelease( const css::awt::MouseEvent & aMouseEvent ) + throw (css::uno::RuntimeException, std::exception) override; + + virtual void SAL_CALL mouseMove( const css::awt::MouseEvent & aMouseEvent ) + throw (css::uno::RuntimeException, std::exception) override; + }; WinBits ImplGetWinBits( sal_uInt32 nComponentAttribs, sal_uInt16 nCompType ) @@ -650,6 +669,7 @@ static void SAL_CALL ToolkitWorkerFunction( void* pArgs ) VCLXToolkit::VCLXToolkit(): cppu::WeakComponentImplHelper< ::com::sun::star::awt::XToolkitExperimental, + ::com::sun::star::awt::XToolkitRobot, ::com::sun::star::lang::XServiceInfo>( GetMutex() ), m_aTopWindowListeners(rBHelper.rMutex), m_aKeyHandlers(rBHelper.rMutex), @@ -1896,6 +1916,85 @@ void SAL_CALL VCLXToolkit::processEventsToIdle() Scheduler::ProcessTaskScheduling(false); } +// css:awt:XToolkitRobot + +void SAL_CALL VCLXToolkit::keyPress( const css::awt::KeyEvent & aKeyEvent ) + throw (css::uno::RuntimeException, std::exception) +{ + css::uno::Reference<css::awt::XWindow> xWindow ( aKeyEvent.Source, css::uno::UNO_QUERY ); + if( !xWindow.is() ) + throw css::uno::RuntimeException( "invalid event source" ); + + vcl::Window * pWindow = VCLUnoHelper::GetWindow( xWindow ); + if( !pWindow ) + throw css::uno::RuntimeException( "invalid event source" ); + + ::KeyEvent aVCLKeyEvent = VCLUnoHelper::createVCLKeyEvent( aKeyEvent ); + ::Application::PostKeyEvent( VCLEVENT_WINDOW_KEYINPUT, pWindow, &aVCLKeyEvent ); +} + +void SAL_CALL VCLXToolkit::keyRelease( const css::awt::KeyEvent & aKeyEvent ) + throw (css::uno::RuntimeException, std::exception) +{ + css::uno::Reference<css::awt::XWindow> xWindow ( aKeyEvent.Source, css::uno::UNO_QUERY ); + if( !xWindow.is() ) + throw css::uno::RuntimeException( "invalid event source" ); + + vcl::Window * pWindow = VCLUnoHelper::GetWindow( xWindow ); + if( !pWindow ) + throw css::uno::RuntimeException( "invalid event source" ); + + ::KeyEvent aVCLKeyEvent = VCLUnoHelper::createVCLKeyEvent( aKeyEvent ); + ::Application::PostKeyEvent( VCLEVENT_WINDOW_KEYUP, pWindow, &aVCLKeyEvent ); +} + + +void SAL_CALL VCLXToolkit::mousePress( const css::awt::MouseEvent & aMouseEvent ) + throw (css::uno::RuntimeException, std::exception) +{ + css::uno::Reference<css::awt::XWindow> xWindow ( aMouseEvent.Source, css::uno::UNO_QUERY ); + if( !xWindow.is() ) + throw css::uno::RuntimeException( "invalid event source" ); + + vcl::Window * pWindow = VCLUnoHelper::GetWindow( xWindow ); + if( !pWindow ) + throw css::uno::RuntimeException( "invalid event source" ); + + ::MouseEvent aVCLMouseEvent = VCLUnoHelper::createVCLMouseEvent( aMouseEvent ); + ::Application::PostMouseEvent( VCLEVENT_WINDOW_MOUSEBUTTONDOWN, pWindow, &aVCLMouseEvent ); +} + +void SAL_CALL VCLXToolkit::mouseRelease( const css::awt::MouseEvent & aMouseEvent ) + throw (css::uno::RuntimeException, std::exception) +{ + css::uno::Reference<css::awt::XWindow> xWindow ( aMouseEvent.Source, css::uno::UNO_QUERY ); + if( !xWindow.is() ) + throw css::uno::RuntimeException( "invalid event source" ); + + vcl::Window * pWindow = VCLUnoHelper::GetWindow( xWindow ); + if( !pWindow ) + throw css::uno::RuntimeException( "invalid event source" ); + + ::MouseEvent aVCLMouseEvent = VCLUnoHelper::createVCLMouseEvent( aMouseEvent ); + ::Application::PostMouseEvent( VCLEVENT_WINDOW_MOUSEBUTTONUP, pWindow, &aVCLMouseEvent ); +} + +void SAL_CALL VCLXToolkit::mouseMove( const css::awt::MouseEvent & aMouseEvent ) + throw (css::uno::RuntimeException, std::exception) +{ + css::uno::Reference<css::awt::XWindow> xWindow ( aMouseEvent.Source, css::uno::UNO_QUERY ); + if( !xWindow.is() ) + throw css::uno::RuntimeException( "invalid event source" ); + + vcl::Window * pWindow = VCLUnoHelper::GetWindow( xWindow ); + if( !pWindow ) + throw css::uno::RuntimeException( "invalid event source" ); + + ::MouseEvent aVCLMouseEvent = VCLUnoHelper::createVCLMouseEvent( aMouseEvent ); + ::Application::PostMouseEvent( VCLEVENT_WINDOW_MOUSEMOVE, pWindow, &aVCLMouseEvent ); +} + + } extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL diff --git a/toolkit/source/helper/vclunohelper.cxx b/toolkit/source/helper/vclunohelper.cxx index 1cc223a59e66..57d398158858 100644 --- a/toolkit/source/helper/vclunohelper.cxx +++ b/toolkit/source/helper/vclunohelper.cxx @@ -736,6 +736,14 @@ awt::MouseEvent VCLUnoHelper::createMouseEvent( const ::MouseEvent& _rVclEvent, return aMouseEvent; } +::MouseEvent VCLUnoHelper::createVCLMouseEvent( const awt::MouseEvent& _rAwtEvent ) +{ + ::MouseEvent aMouseEvent( Point( _rAwtEvent.X, _rAwtEvent.Y ), _rAwtEvent.ClickCount, + ::MouseEventModifiers::NONE, _rAwtEvent.Buttons, _rAwtEvent.Modifiers ); + + return aMouseEvent; +} + awt::KeyEvent VCLUnoHelper::createKeyEvent( const ::KeyEvent& _rVclEvent, const uno::Reference< uno::XInterface >& _rxContext ) { awt::KeyEvent aKeyEvent; @@ -758,4 +766,16 @@ awt::KeyEvent VCLUnoHelper::createKeyEvent( const ::KeyEvent& _rVclEvent, const return aKeyEvent; } +::KeyEvent VCLUnoHelper::createVCLKeyEvent( const awt::KeyEvent& _rAwtEvent ) +{ + sal_Unicode nChar = _rAwtEvent.KeyChar; + vcl::KeyCode aKeyCode( _rAwtEvent.KeyCode, _rAwtEvent.Modifiers & awt::KeyModifier::SHIFT, + _rAwtEvent.Modifiers & awt::KeyModifier::MOD1, + _rAwtEvent.Modifiers & awt::KeyModifier::MOD2, + _rAwtEvent.Modifiers & awt::KeyModifier::MOD3 ); + + return ::KeyEvent (nChar, aKeyCode); + +} + /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |