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>
43 using ::com::sun::star::uno::Reference
;
44 using ::com::sun::star::uno::XInterface
;
45 using ::com::sun::star::uno::Exception
;
46 using ::com::sun::star::uno::RuntimeException
;
47 using ::com::sun::star::awt::MouseEvent
;
48 using ::com::sun::star::awt::KeyEvent
;
49 using ::com::sun::star::awt::InputEvent
;
50 using ::com::sun::star::awt::XKeyHandler
;
51 using ::com::sun::star::awt::XMouseClickHandler
;
52 using ::com::sun::star::lang::DisposedException
;
54 namespace MouseButton
= ::com::sun::star::awt::MouseButton
;
55 namespace KeyModifier
= ::com::sun::star::awt::KeyModifier
;
57 struct UserInputInterception_Data
60 ::cppu::OWeakObject
& m_rControllerImpl
;
61 ::comphelper::OInterfaceContainerHelper2 m_aKeyHandlers
;
62 ::comphelper::OInterfaceContainerHelper2 m_aMouseClickHandlers
;
65 UserInputInterception_Data( ::cppu::OWeakObject
& _rControllerImpl
, ::osl::Mutex
& _rMutex
)
66 :m_rControllerImpl( _rControllerImpl
)
67 ,m_aKeyHandlers( _rMutex
)
68 ,m_aMouseClickHandlers( _rMutex
)
75 template< class VCLEVENT
>
76 void lcl_initModifiers( InputEvent
& _rEvent
, const VCLEVENT
& _rVclEvent
)
78 _rEvent
.Modifiers
= 0;
80 if ( _rVclEvent
.IsShift() )
81 _rEvent
.Modifiers
|= KeyModifier::SHIFT
;
82 if ( _rVclEvent
.IsMod1() )
83 _rEvent
.Modifiers
|= KeyModifier::MOD1
;
84 if ( _rVclEvent
.IsMod2() )
85 _rEvent
.Modifiers
|= KeyModifier::MOD2
;
86 if ( _rVclEvent
.IsMod3() )
87 _rEvent
.Modifiers
|= KeyModifier::MOD3
;
90 void lcl_initKeyEvent( KeyEvent
& rEvent
, const ::KeyEvent
& rEvt
)
92 lcl_initModifiers( rEvent
, rEvt
.GetKeyCode() );
94 rEvent
.KeyCode
= rEvt
.GetKeyCode().GetCode();
95 rEvent
.KeyChar
= rEvt
.GetCharCode();
96 rEvent
.KeyFunc
= sal::static_int_cast
< sal_Int16
>( rEvt
.GetKeyCode().GetFunction());
99 void lcl_initMouseEvent( MouseEvent
& rEvent
, const ::MouseEvent
& rEvt
)
101 lcl_initModifiers( rEvent
, rEvt
);
105 rEvent
.Buttons
|= MouseButton::LEFT
;
106 if ( rEvt
.IsRight() )
107 rEvent
.Buttons
|= MouseButton::RIGHT
;
108 if ( rEvt
.IsMiddle() )
109 rEvent
.Buttons
|= MouseButton::MIDDLE
;
111 rEvent
.X
= rEvt
.GetPosPixel().X();
112 rEvent
.Y
= rEvt
.GetPosPixel().Y();
113 rEvent
.ClickCount
= rEvt
.GetClicks();
114 rEvent
.PopupTrigger
= false;
120 //= UserInputInterception
123 UserInputInterception::UserInputInterception( ::cppu::OWeakObject
& _rControllerImpl
, ::osl::Mutex
& _rMutex
)
124 :m_pData( new UserInputInterception_Data( _rControllerImpl
, _rMutex
) )
129 UserInputInterception::~UserInputInterception()
134 void UserInputInterception::addKeyHandler( const Reference
< XKeyHandler
>& _rxHandler
)
136 if ( _rxHandler
.is() )
137 m_pData
->m_aKeyHandlers
.addInterface( _rxHandler
);
141 void UserInputInterception::removeKeyHandler( const Reference
< XKeyHandler
>& _rxHandler
)
143 m_pData
->m_aKeyHandlers
.removeInterface( _rxHandler
);
147 void UserInputInterception::addMouseClickHandler( const Reference
< XMouseClickHandler
>& _rxHandler
)
149 if ( _rxHandler
.is() )
150 m_pData
->m_aMouseClickHandlers
.addInterface( _rxHandler
);
154 void UserInputInterception::removeMouseClickHandler( const Reference
< XMouseClickHandler
>& _rxHandler
)
156 m_pData
->m_aMouseClickHandlers
.removeInterface( _rxHandler
);
160 bool UserInputInterception::hasKeyHandlers() const
162 return m_pData
->m_aKeyHandlers
.getLength() > 0;
166 bool UserInputInterception::hasMouseClickListeners() const
168 return m_pData
->m_aMouseClickHandlers
.getLength() > 0;
172 bool UserInputInterception::handleNotifyEvent( const NotifyEvent
& _rEvent
)
174 Reference
< XInterface
> xHoldAlive( m_pData
->m_rControllerImpl
);
176 MouseNotifyEvent nType
= _rEvent
.GetType();
177 bool bHandled
= false;
181 case MouseNotifyEvent::KEYINPUT
:
182 case MouseNotifyEvent::KEYUP
:
185 lcl_initKeyEvent( aEvent
, *_rEvent
.GetKeyEvent() );
186 if ( _rEvent
.GetWindow() )
187 aEvent
.Source
= _rEvent
.GetWindow()->GetComponentInterface();
189 ::comphelper::OInterfaceIteratorHelper2
aIterator( m_pData
->m_aKeyHandlers
);
190 while ( aIterator
.hasMoreElements() )
192 Reference
< XKeyHandler
> xHandler( static_cast< XKeyHandler
* >( aIterator
.next() ) );
193 if ( !xHandler
.is() )
198 if ( nType
== MouseNotifyEvent::KEYINPUT
)
199 bHandled
= xHandler
->keyPressed( aEvent
);
201 bHandled
= xHandler
->keyReleased( aEvent
);
203 catch( const DisposedException
& e
)
205 if ( e
.Context
== xHandler
)
208 catch( const RuntimeException
& )
212 catch( const Exception
& )
219 case MouseNotifyEvent::MOUSEBUTTONDOWN
:
220 case MouseNotifyEvent::MOUSEBUTTONUP
:
223 lcl_initMouseEvent( aEvent
, *_rEvent
.GetMouseEvent() );
224 if ( _rEvent
.GetWindow() )
225 aEvent
.Source
= _rEvent
.GetWindow()->GetComponentInterface();
227 ::comphelper::OInterfaceIteratorHelper2
aIterator( m_pData
->m_aMouseClickHandlers
);
228 while ( aIterator
.hasMoreElements() )
230 Reference
< XMouseClickHandler
> xHandler( static_cast< XMouseClickHandler
* >( aIterator
.next() ) );
231 if ( !xHandler
.is() )
236 if ( nType
== MouseNotifyEvent::MOUSEBUTTONDOWN
)
237 bHandled
= xHandler
->mousePressed( aEvent
);
239 bHandled
= xHandler
->mouseReleased( aEvent
);
241 catch( const DisposedException
& e
)
243 if ( e
.Context
== xHandler
)
246 catch( const RuntimeException
& )
250 catch( const Exception
& )
258 OSL_FAIL( "UserInputInterception::handleNotifyEvent: illegal event type!" );
269 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */