1 /*************************************************************************
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 * Copyright 2008 by Sun Microsystems, Inc.
6 * OpenOffice.org - a multi-platform office productivity suite
8 * $RCSfile: userinputinterception.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
28 ************************************************************************/
30 // MARKER(update_precomp.py): autogen include statement, do not remove
31 #include "precompiled_sfx2.hxx"
33 #include "sfx2/userinputinterception.hxx"
35 /** === begin UNO includes === **/
36 #include <com/sun/star/awt/MouseButton.hpp>
37 #include <com/sun/star/awt/KeyModifier.hpp>
38 /** === end UNO includes === **/
40 #include <cppuhelper/interfacecontainer.hxx>
41 #include <cppuhelper/weak.hxx>
42 #include <vcl/event.hxx>
43 #include <vcl/window.hxx>
45 //........................................................................
48 //........................................................................
50 /** === begin UNO using === **/
51 using ::com::sun::star::uno::Reference
;
52 using ::com::sun::star::uno::XInterface
;
53 using ::com::sun::star::uno::UNO_QUERY
;
54 using ::com::sun::star::uno::UNO_QUERY_THROW
;
55 using ::com::sun::star::uno::UNO_SET_THROW
;
56 using ::com::sun::star::uno::Exception
;
57 using ::com::sun::star::uno::RuntimeException
;
58 using ::com::sun::star::uno::Any
;
59 using ::com::sun::star::uno::makeAny
;
60 using ::com::sun::star::awt::MouseEvent
;
61 using ::com::sun::star::awt::KeyEvent
;
62 using ::com::sun::star::awt::InputEvent
;
63 using ::com::sun::star::awt::XKeyHandler
;
64 using ::com::sun::star::awt::XMouseClickHandler
;
65 using ::com::sun::star::lang::DisposedException
;
66 /** === end UNO using === **/
67 namespace MouseButton
= ::com::sun::star::awt::MouseButton
;
68 namespace KeyModifier
= ::com::sun::star::awt::KeyModifier
;
70 struct UserInputInterception_Data
73 ::cppu::OWeakObject
& m_rControllerImpl
;
74 ::cppu::OInterfaceContainerHelper m_aKeyHandlers
;
75 ::cppu::OInterfaceContainerHelper m_aMouseClickHandlers
;
78 UserInputInterception_Data( ::cppu::OWeakObject
& _rControllerImpl
, ::osl::Mutex
& _rMutex
)
79 :m_rControllerImpl( _rControllerImpl
)
80 ,m_aKeyHandlers( _rMutex
)
81 ,m_aMouseClickHandlers( _rMutex
)
88 template< class VLCEVENT
>
89 void lcl_initModifiers( InputEvent
& _rEvent
, const VLCEVENT
& _rVclEvent
)
91 _rEvent
.Modifiers
= 0;
93 if ( _rVclEvent
.IsShift() )
94 _rEvent
.Modifiers
|= KeyModifier::SHIFT
;
95 if ( _rVclEvent
.IsMod1() )
96 _rEvent
.Modifiers
|= KeyModifier::MOD1
;
97 if ( _rVclEvent
.IsMod2() )
98 _rEvent
.Modifiers
|= KeyModifier::MOD2
;
99 if ( _rVclEvent
.IsMod3() )
100 _rEvent
.Modifiers
|= KeyModifier::MOD3
;
103 void lcl_initKeyEvent( KeyEvent
& rEvent
, const ::KeyEvent
& rEvt
)
105 lcl_initModifiers( rEvent
, rEvt
.GetKeyCode() );
107 rEvent
.KeyCode
= rEvt
.GetKeyCode().GetCode();
108 rEvent
.KeyChar
= rEvt
.GetCharCode();
109 rEvent
.KeyFunc
= sal::static_int_cast
< sal_Int16
>( rEvt
.GetKeyCode().GetFunction());
112 void lcl_initMouseEvent( MouseEvent
& rEvent
, const ::MouseEvent
& rEvt
)
114 lcl_initModifiers( rEvent
, rEvt
);
118 rEvent
.Buttons
|= MouseButton::LEFT
;
119 if ( rEvt
.IsRight() )
120 rEvent
.Buttons
|= MouseButton::RIGHT
;
121 if ( rEvt
.IsMiddle() )
122 rEvent
.Buttons
|= MouseButton::MIDDLE
;
124 rEvent
.X
= rEvt
.GetPosPixel().X();
125 rEvent
.Y
= rEvt
.GetPosPixel().Y();
126 rEvent
.ClickCount
= rEvt
.GetClicks();
127 rEvent
.PopupTrigger
= sal_False
;
132 //====================================================================
133 //= UserInputInterception
134 //====================================================================
135 //--------------------------------------------------------------------
136 UserInputInterception::UserInputInterception( ::cppu::OWeakObject
& _rControllerImpl
, ::osl::Mutex
& _rMutex
)
137 :m_pData( new UserInputInterception_Data( _rControllerImpl
, _rMutex
) )
141 //--------------------------------------------------------------------
142 UserInputInterception::~UserInputInterception()
146 //--------------------------------------------------------------------
147 void UserInputInterception::addKeyHandler( const Reference
< XKeyHandler
>& _rxHandler
) throw (RuntimeException
)
149 if ( _rxHandler
.is() )
150 m_pData
->m_aKeyHandlers
.addInterface( _rxHandler
);
153 //--------------------------------------------------------------------
154 void UserInputInterception::removeKeyHandler( const Reference
< XKeyHandler
>& _rxHandler
) throw (RuntimeException
)
156 m_pData
->m_aKeyHandlers
.removeInterface( _rxHandler
);
159 //--------------------------------------------------------------------
160 void UserInputInterception::addMouseClickHandler( const Reference
< XMouseClickHandler
>& _rxHandler
) throw (RuntimeException
)
162 if ( _rxHandler
.is() )
163 m_pData
->m_aMouseClickHandlers
.addInterface( _rxHandler
);
166 //--------------------------------------------------------------------
167 void UserInputInterception::removeMouseClickHandler( const Reference
< XMouseClickHandler
>& _rxHandler
) throw (RuntimeException
)
169 m_pData
->m_aMouseClickHandlers
.removeInterface( _rxHandler
);
172 //--------------------------------------------------------------------
173 bool UserInputInterception::hasKeyHandlers() const
175 return m_pData
->m_aKeyHandlers
.getLength() > 0;
178 //--------------------------------------------------------------------
179 bool UserInputInterception::hasMouseClickListeners() const
181 return m_pData
->m_aMouseClickHandlers
.getLength() > 0;
184 //--------------------------------------------------------------------
185 bool UserInputInterception::handleNotifyEvent( const NotifyEvent
& _rEvent
)
187 Reference
< XInterface
> xHoldAlive( m_pData
->m_rControllerImpl
);
189 USHORT nType
= _rEvent
.GetType();
190 bool bHandled
= false;
198 lcl_initKeyEvent( aEvent
, *_rEvent
.GetKeyEvent() );
199 if ( _rEvent
.GetWindow() )
200 aEvent
.Source
= _rEvent
.GetWindow()->GetComponentInterface();
202 ::cppu::OInterfaceIteratorHelper
aIterator( m_pData
->m_aKeyHandlers
);
203 while ( aIterator
.hasMoreElements() )
205 Reference
< XKeyHandler
> xHandler( static_cast< XKeyHandler
* >( aIterator
.next() ) );
206 if ( !xHandler
.is() )
211 if ( nType
== EVENT_KEYINPUT
)
212 bHandled
= xHandler
->keyPressed( aEvent
);
214 bHandled
= xHandler
->keyReleased( aEvent
);
216 catch( const DisposedException
& e
)
218 if ( e
.Context
== xHandler
)
225 case EVENT_MOUSEBUTTONDOWN
:
226 case EVENT_MOUSEBUTTONUP
:
229 lcl_initMouseEvent( aEvent
, *_rEvent
.GetMouseEvent() );
230 if ( _rEvent
.GetWindow() )
231 aEvent
.Source
= _rEvent
.GetWindow()->GetComponentInterface();
233 ::cppu::OInterfaceIteratorHelper
aIterator( m_pData
->m_aMouseClickHandlers
);
234 while ( aIterator
.hasMoreElements() )
236 Reference
< XMouseClickHandler
> xHandler( static_cast< XMouseClickHandler
* >( aIterator
.next() ) );
237 if ( !xHandler
.is() )
242 if ( nType
== EVENT_MOUSEBUTTONDOWN
)
243 bHandled
= xHandler
->mousePressed( aEvent
);
245 bHandled
= xHandler
->mouseReleased( aEvent
);
247 catch( const DisposedException
& e
)
249 if ( e
.Context
== xHandler
)
257 OSL_ENSURE( false, "UserInputInterception::handleNotifyEvent: illegal event type!" );
264 //........................................................................
266 //........................................................................