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/KeyModifier.hpp>
26 #include <cppuhelper/interfacecontainer.hxx>
27 #include <cppuhelper/weak.hxx>
28 #include <vcl/event.hxx>
29 #include <vcl/window.hxx>
36 using ::com::sun::star::uno::Reference
;
37 using ::com::sun::star::uno::XInterface
;
38 using ::com::sun::star::uno::UNO_QUERY
;
39 using ::com::sun::star::uno::UNO_QUERY_THROW
;
40 using ::com::sun::star::uno::UNO_SET_THROW
;
41 using ::com::sun::star::uno::Exception
;
42 using ::com::sun::star::uno::RuntimeException
;
43 using ::com::sun::star::uno::Any
;
44 using ::com::sun::star::uno::makeAny
;
45 using ::com::sun::star::awt::MouseEvent
;
46 using ::com::sun::star::awt::KeyEvent
;
47 using ::com::sun::star::awt::InputEvent
;
48 using ::com::sun::star::awt::XKeyHandler
;
49 using ::com::sun::star::awt::XMouseClickHandler
;
50 using ::com::sun::star::lang::DisposedException
;
52 namespace MouseButton
= ::com::sun::star::awt::MouseButton
;
53 namespace KeyModifier
= ::com::sun::star::awt::KeyModifier
;
55 struct UserInputInterception_Data
58 ::cppu::OWeakObject
& m_rControllerImpl
;
59 ::cppu::OInterfaceContainerHelper m_aKeyHandlers
;
60 ::cppu::OInterfaceContainerHelper m_aMouseClickHandlers
;
63 UserInputInterception_Data( ::cppu::OWeakObject
& _rControllerImpl
, ::osl::Mutex
& _rMutex
)
64 :m_rControllerImpl( _rControllerImpl
)
65 ,m_aKeyHandlers( _rMutex
)
66 ,m_aMouseClickHandlers( _rMutex
)
73 template< class VLCEVENT
>
74 void lcl_initModifiers( InputEvent
& _rEvent
, const VLCEVENT
& _rVclEvent
)
76 _rEvent
.Modifiers
= 0;
78 if ( _rVclEvent
.IsShift() )
79 _rEvent
.Modifiers
|= KeyModifier::SHIFT
;
80 if ( _rVclEvent
.IsMod1() )
81 _rEvent
.Modifiers
|= KeyModifier::MOD1
;
82 if ( _rVclEvent
.IsMod2() )
83 _rEvent
.Modifiers
|= KeyModifier::MOD2
;
84 if ( _rVclEvent
.IsMod3() )
85 _rEvent
.Modifiers
|= KeyModifier::MOD3
;
88 void lcl_initKeyEvent( KeyEvent
& rEvent
, const ::KeyEvent
& rEvt
)
90 lcl_initModifiers( rEvent
, rEvt
.GetKeyCode() );
92 rEvent
.KeyCode
= rEvt
.GetKeyCode().GetCode();
93 rEvent
.KeyChar
= rEvt
.GetCharCode();
94 rEvent
.KeyFunc
= sal::static_int_cast
< sal_Int16
>( rEvt
.GetKeyCode().GetFunction());
97 void lcl_initMouseEvent( MouseEvent
& rEvent
, const ::MouseEvent
& rEvt
)
99 lcl_initModifiers( rEvent
, rEvt
);
103 rEvent
.Buttons
|= MouseButton::LEFT
;
104 if ( rEvt
.IsRight() )
105 rEvent
.Buttons
|= MouseButton::RIGHT
;
106 if ( rEvt
.IsMiddle() )
107 rEvent
.Buttons
|= MouseButton::MIDDLE
;
109 rEvent
.X
= rEvt
.GetPosPixel().X();
110 rEvent
.Y
= rEvt
.GetPosPixel().Y();
111 rEvent
.ClickCount
= rEvt
.GetClicks();
112 rEvent
.PopupTrigger
= sal_False
;
118 //= UserInputInterception
121 UserInputInterception::UserInputInterception( ::cppu::OWeakObject
& _rControllerImpl
, ::osl::Mutex
& _rMutex
)
122 :m_pData( new UserInputInterception_Data( _rControllerImpl
, _rMutex
) )
127 UserInputInterception::~UserInputInterception()
132 void UserInputInterception::addKeyHandler( const Reference
< XKeyHandler
>& _rxHandler
) throw (RuntimeException
)
134 if ( _rxHandler
.is() )
135 m_pData
->m_aKeyHandlers
.addInterface( _rxHandler
);
139 void UserInputInterception::removeKeyHandler( const Reference
< XKeyHandler
>& _rxHandler
) throw (RuntimeException
)
141 m_pData
->m_aKeyHandlers
.removeInterface( _rxHandler
);
145 void UserInputInterception::addMouseClickHandler( const Reference
< XMouseClickHandler
>& _rxHandler
) throw (RuntimeException
)
147 if ( _rxHandler
.is() )
148 m_pData
->m_aMouseClickHandlers
.addInterface( _rxHandler
);
152 void UserInputInterception::removeMouseClickHandler( const Reference
< XMouseClickHandler
>& _rxHandler
) throw (RuntimeException
)
154 m_pData
->m_aMouseClickHandlers
.removeInterface( _rxHandler
);
158 bool UserInputInterception::hasKeyHandlers() const
160 return m_pData
->m_aKeyHandlers
.getLength() > 0;
164 bool UserInputInterception::hasMouseClickListeners() const
166 return m_pData
->m_aMouseClickHandlers
.getLength() > 0;
170 bool UserInputInterception::handleNotifyEvent( const NotifyEvent
& _rEvent
)
172 Reference
< XInterface
> xHoldAlive( m_pData
->m_rControllerImpl
);
174 MouseNotifyEvent nType
= _rEvent
.GetType();
175 bool bHandled
= false;
179 case MouseNotifyEvent::KEYINPUT
:
180 case MouseNotifyEvent::KEYUP
:
183 lcl_initKeyEvent( aEvent
, *_rEvent
.GetKeyEvent() );
184 if ( _rEvent
.GetWindow() )
185 aEvent
.Source
= _rEvent
.GetWindow()->GetComponentInterface();
187 ::cppu::OInterfaceIteratorHelper
aIterator( m_pData
->m_aKeyHandlers
);
188 while ( aIterator
.hasMoreElements() )
190 Reference
< XKeyHandler
> xHandler( static_cast< XKeyHandler
* >( aIterator
.next() ) );
191 if ( !xHandler
.is() )
196 if ( nType
== MouseNotifyEvent::KEYINPUT
)
197 bHandled
= xHandler
->keyPressed( aEvent
);
199 bHandled
= xHandler
->keyReleased( aEvent
);
201 catch( const DisposedException
& e
)
203 if ( e
.Context
== xHandler
)
206 catch( const RuntimeException
& )
210 catch( const Exception
& )
217 case MouseNotifyEvent::MOUSEBUTTONDOWN
:
218 case MouseNotifyEvent::MOUSEBUTTONUP
:
221 lcl_initMouseEvent( aEvent
, *_rEvent
.GetMouseEvent() );
222 if ( _rEvent
.GetWindow() )
223 aEvent
.Source
= _rEvent
.GetWindow()->GetComponentInterface();
225 ::cppu::OInterfaceIteratorHelper
aIterator( m_pData
->m_aMouseClickHandlers
);
226 while ( aIterator
.hasMoreElements() )
228 Reference
< XMouseClickHandler
> xHandler( static_cast< XMouseClickHandler
* >( aIterator
.next() ) );
229 if ( !xHandler
.is() )
234 if ( nType
== MouseNotifyEvent::MOUSEBUTTONDOWN
)
235 bHandled
= xHandler
->mousePressed( aEvent
);
237 bHandled
= xHandler
->mouseReleased( aEvent
);
239 catch( const DisposedException
& e
)
241 if ( e
.Context
== xHandler
)
244 catch( const RuntimeException
& )
248 catch( const Exception
& )
256 OSL_FAIL( "UserInputInterception::handleNotifyEvent: illegal event type!" );
267 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */