1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
21 #include <sfx2/userinputinterception.hxx>
23 #include <com/sun/star/awt/MouseButton.hpp>
24 #include <com/sun/star/awt/MouseEvent.hpp>
25 #include <com/sun/star/awt/InputEvent.hpp>
26 #include <com/sun/star/awt/KeyEvent.hpp>
27 #include <com/sun/star/awt/KeyModifier.hpp>
28 #include <com/sun/star/awt/XKeyHandler.hpp>
29 #include <com/sun/star/awt/XMouseClickHandler.hpp>
30 #include <com/sun/star/awt/XWindowPeer.hpp>
31 #include <com/sun/star/uno/XInterface.hpp>
33 #include <comphelper/interfacecontainer2.hxx>
34 #include <cppuhelper/weak.hxx>
35 #include <vcl/event.hxx>
36 #include <vcl/window.hxx>
37 #include <osl/diagnose.h>
44 using ::com::sun::star::uno::Reference
;
45 using ::com::sun::star::uno::XInterface
;
46 using ::com::sun::star::uno::Exception
;
47 using ::com::sun::star::uno::RuntimeException
;
48 using ::com::sun::star::awt::MouseEvent
;
49 using ::com::sun::star::awt::KeyEvent
;
50 using ::com::sun::star::awt::InputEvent
;
51 using ::com::sun::star::awt::XKeyHandler
;
52 using ::com::sun::star::awt::XMouseClickHandler
;
53 using ::com::sun::star::lang::DisposedException
;
55 namespace MouseButton
= ::com::sun::star::awt::MouseButton
;
56 namespace KeyModifier
= ::com::sun::star::awt::KeyModifier
;
58 struct UserInputInterception_Data
61 ::cppu::OWeakObject
& m_rControllerImpl
;
62 ::comphelper::OInterfaceContainerHelper2 m_aKeyHandlers
;
63 ::comphelper::OInterfaceContainerHelper2 m_aMouseClickHandlers
;
66 UserInputInterception_Data( ::cppu::OWeakObject
& _rControllerImpl
, ::osl::Mutex
& _rMutex
)
67 :m_rControllerImpl( _rControllerImpl
)
68 ,m_aKeyHandlers( _rMutex
)
69 ,m_aMouseClickHandlers( _rMutex
)
76 template< class VLCEVENT
>
77 void lcl_initModifiers( InputEvent
& _rEvent
, const VLCEVENT
& _rVclEvent
)
79 _rEvent
.Modifiers
= 0;
81 if ( _rVclEvent
.IsShift() )
82 _rEvent
.Modifiers
|= KeyModifier::SHIFT
;
83 if ( _rVclEvent
.IsMod1() )
84 _rEvent
.Modifiers
|= KeyModifier::MOD1
;
85 if ( _rVclEvent
.IsMod2() )
86 _rEvent
.Modifiers
|= KeyModifier::MOD2
;
87 if ( _rVclEvent
.IsMod3() )
88 _rEvent
.Modifiers
|= KeyModifier::MOD3
;
91 void lcl_initKeyEvent( KeyEvent
& rEvent
, const ::KeyEvent
& rEvt
)
93 lcl_initModifiers( rEvent
, rEvt
.GetKeyCode() );
95 rEvent
.KeyCode
= rEvt
.GetKeyCode().GetCode();
96 rEvent
.KeyChar
= rEvt
.GetCharCode();
97 rEvent
.KeyFunc
= sal::static_int_cast
< sal_Int16
>( rEvt
.GetKeyCode().GetFunction());
100 void lcl_initMouseEvent( MouseEvent
& rEvent
, const ::MouseEvent
& rEvt
)
102 lcl_initModifiers( rEvent
, rEvt
);
106 rEvent
.Buttons
|= MouseButton::LEFT
;
107 if ( rEvt
.IsRight() )
108 rEvent
.Buttons
|= MouseButton::RIGHT
;
109 if ( rEvt
.IsMiddle() )
110 rEvent
.Buttons
|= MouseButton::MIDDLE
;
112 rEvent
.X
= rEvt
.GetPosPixel().X();
113 rEvent
.Y
= rEvt
.GetPosPixel().Y();
114 rEvent
.ClickCount
= rEvt
.GetClicks();
115 rEvent
.PopupTrigger
= false;
121 //= UserInputInterception
124 UserInputInterception::UserInputInterception( ::cppu::OWeakObject
& _rControllerImpl
, ::osl::Mutex
& _rMutex
)
125 :m_pData( new UserInputInterception_Data( _rControllerImpl
, _rMutex
) )
130 UserInputInterception::~UserInputInterception()
135 void UserInputInterception::addKeyHandler( const Reference
< XKeyHandler
>& _rxHandler
)
137 if ( _rxHandler
.is() )
138 m_pData
->m_aKeyHandlers
.addInterface( _rxHandler
);
142 void UserInputInterception::removeKeyHandler( const Reference
< XKeyHandler
>& _rxHandler
)
144 m_pData
->m_aKeyHandlers
.removeInterface( _rxHandler
);
148 void UserInputInterception::addMouseClickHandler( const Reference
< XMouseClickHandler
>& _rxHandler
)
150 if ( _rxHandler
.is() )
151 m_pData
->m_aMouseClickHandlers
.addInterface( _rxHandler
);
155 void UserInputInterception::removeMouseClickHandler( const Reference
< XMouseClickHandler
>& _rxHandler
)
157 m_pData
->m_aMouseClickHandlers
.removeInterface( _rxHandler
);
161 bool UserInputInterception::hasKeyHandlers() const
163 return m_pData
->m_aKeyHandlers
.getLength() > 0;
167 bool UserInputInterception::hasMouseClickListeners() const
169 return m_pData
->m_aMouseClickHandlers
.getLength() > 0;
173 bool UserInputInterception::handleNotifyEvent( const NotifyEvent
& _rEvent
)
175 Reference
< XInterface
> xHoldAlive( m_pData
->m_rControllerImpl
);
177 MouseNotifyEvent nType
= _rEvent
.GetType();
178 bool bHandled
= false;
182 case MouseNotifyEvent::KEYINPUT
:
183 case MouseNotifyEvent::KEYUP
:
186 lcl_initKeyEvent( aEvent
, *_rEvent
.GetKeyEvent() );
187 if ( _rEvent
.GetWindow() )
188 aEvent
.Source
= _rEvent
.GetWindow()->GetComponentInterface();
190 ::comphelper::OInterfaceIteratorHelper2
aIterator( m_pData
->m_aKeyHandlers
);
191 while ( aIterator
.hasMoreElements() )
193 Reference
< XKeyHandler
> xHandler( static_cast< XKeyHandler
* >( aIterator
.next() ) );
194 if ( !xHandler
.is() )
199 if ( nType
== MouseNotifyEvent::KEYINPUT
)
200 bHandled
= xHandler
->keyPressed( aEvent
);
202 bHandled
= xHandler
->keyReleased( aEvent
);
204 catch( const DisposedException
& e
)
206 if ( e
.Context
== xHandler
)
209 catch( const RuntimeException
& )
213 catch( const Exception
& )
220 case MouseNotifyEvent::MOUSEBUTTONDOWN
:
221 case MouseNotifyEvent::MOUSEBUTTONUP
:
224 lcl_initMouseEvent( aEvent
, *_rEvent
.GetMouseEvent() );
225 if ( _rEvent
.GetWindow() )
226 aEvent
.Source
= _rEvent
.GetWindow()->GetComponentInterface();
228 ::comphelper::OInterfaceIteratorHelper2
aIterator( m_pData
->m_aMouseClickHandlers
);
229 while ( aIterator
.hasMoreElements() )
231 Reference
< XMouseClickHandler
> xHandler( static_cast< XMouseClickHandler
* >( aIterator
.next() ) );
232 if ( !xHandler
.is() )
237 if ( nType
== MouseNotifyEvent::MOUSEBUTTONDOWN
)
238 bHandled
= xHandler
->mousePressed( aEvent
);
240 bHandled
= xHandler
->mouseReleased( aEvent
);
242 catch( const DisposedException
& e
)
244 if ( e
.Context
== xHandler
)
247 catch( const RuntimeException
& )
251 catch( const Exception
& )
259 OSL_FAIL( "UserInputInterception::handleNotifyEvent: illegal event type!" );
270 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */