1*d119d52dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*d119d52dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*d119d52dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*d119d52dSAndrew Rist  * distributed with this work for additional information
6*d119d52dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*d119d52dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*d119d52dSAndrew Rist  * "License"); you may not use this file except in compliance
9*d119d52dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*d119d52dSAndrew Rist  *
11*d119d52dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*d119d52dSAndrew Rist  *
13*d119d52dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*d119d52dSAndrew Rist  * software distributed under the License is distributed on an
15*d119d52dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*d119d52dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*d119d52dSAndrew Rist  * specific language governing permissions and limitations
18*d119d52dSAndrew Rist  * under the License.
19*d119d52dSAndrew Rist  *
20*d119d52dSAndrew Rist  *************************************************************/
21*d119d52dSAndrew Rist 
22*d119d52dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sfx2.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "sfx2/userinputinterception.hxx"
28cdf0e10cSrcweir 
29cdf0e10cSrcweir /** === begin UNO includes === **/
30cdf0e10cSrcweir #include <com/sun/star/awt/MouseButton.hpp>
31cdf0e10cSrcweir #include <com/sun/star/awt/KeyModifier.hpp>
32cdf0e10cSrcweir /** === end UNO includes === **/
33cdf0e10cSrcweir 
34cdf0e10cSrcweir #include <cppuhelper/interfacecontainer.hxx>
35cdf0e10cSrcweir #include <cppuhelper/weak.hxx>
36cdf0e10cSrcweir #include <vcl/event.hxx>
37cdf0e10cSrcweir #include <vcl/window.hxx>
38cdf0e10cSrcweir 
39cdf0e10cSrcweir //........................................................................
40cdf0e10cSrcweir namespace sfx2
41cdf0e10cSrcweir {
42cdf0e10cSrcweir //........................................................................
43cdf0e10cSrcweir 
44cdf0e10cSrcweir 	/** === begin UNO using === **/
45cdf0e10cSrcweir 	using ::com::sun::star::uno::Reference;
46cdf0e10cSrcweir 	using ::com::sun::star::uno::XInterface;
47cdf0e10cSrcweir 	using ::com::sun::star::uno::UNO_QUERY;
48cdf0e10cSrcweir 	using ::com::sun::star::uno::UNO_QUERY_THROW;
49cdf0e10cSrcweir 	using ::com::sun::star::uno::UNO_SET_THROW;
50cdf0e10cSrcweir 	using ::com::sun::star::uno::Exception;
51cdf0e10cSrcweir 	using ::com::sun::star::uno::RuntimeException;
52cdf0e10cSrcweir 	using ::com::sun::star::uno::Any;
53cdf0e10cSrcweir 	using ::com::sun::star::uno::makeAny;
54cdf0e10cSrcweir 	using ::com::sun::star::awt::MouseEvent;
55cdf0e10cSrcweir 	using ::com::sun::star::awt::KeyEvent;
56cdf0e10cSrcweir 	using ::com::sun::star::awt::InputEvent;
57cdf0e10cSrcweir     using ::com::sun::star::awt::XKeyHandler;
58cdf0e10cSrcweir     using ::com::sun::star::awt::XMouseClickHandler;
59cdf0e10cSrcweir     using ::com::sun::star::lang::DisposedException;
60cdf0e10cSrcweir 	/** === end UNO using === **/
61cdf0e10cSrcweir     namespace MouseButton = ::com::sun::star::awt::MouseButton;
62cdf0e10cSrcweir     namespace KeyModifier = ::com::sun::star::awt::KeyModifier;
63cdf0e10cSrcweir 
64cdf0e10cSrcweir     struct UserInputInterception_Data
65cdf0e10cSrcweir     {
66cdf0e10cSrcweir     public:
67cdf0e10cSrcweir         ::cppu::OWeakObject&                m_rControllerImpl;
68cdf0e10cSrcweir         ::cppu::OInterfaceContainerHelper   m_aKeyHandlers;
69cdf0e10cSrcweir         ::cppu::OInterfaceContainerHelper   m_aMouseClickHandlers;
70cdf0e10cSrcweir 
71cdf0e10cSrcweir     public:
UserInputInterception_Datasfx2::UserInputInterception_Data72cdf0e10cSrcweir         UserInputInterception_Data( ::cppu::OWeakObject& _rControllerImpl, ::osl::Mutex& _rMutex )
73cdf0e10cSrcweir             :m_rControllerImpl( _rControllerImpl )
74cdf0e10cSrcweir             ,m_aKeyHandlers( _rMutex )
75cdf0e10cSrcweir             ,m_aMouseClickHandlers( _rMutex )
76cdf0e10cSrcweir         {
77cdf0e10cSrcweir         }
78cdf0e10cSrcweir     };
79cdf0e10cSrcweir 
80cdf0e10cSrcweir     namespace
81cdf0e10cSrcweir     {
82cdf0e10cSrcweir         template< class VLCEVENT >
lcl_initModifiers(InputEvent & _rEvent,const VLCEVENT & _rVclEvent)83cdf0e10cSrcweir         void lcl_initModifiers( InputEvent& _rEvent, const VLCEVENT& _rVclEvent )
84cdf0e10cSrcweir         {
85cdf0e10cSrcweir 	        _rEvent.Modifiers = 0;
86cdf0e10cSrcweir 
87cdf0e10cSrcweir             if ( _rVclEvent.IsShift() )
88cdf0e10cSrcweir 		        _rEvent.Modifiers |= KeyModifier::SHIFT;
89cdf0e10cSrcweir 	        if ( _rVclEvent.IsMod1() )
90cdf0e10cSrcweir 		        _rEvent.Modifiers |= KeyModifier::MOD1;
91cdf0e10cSrcweir 	        if ( _rVclEvent.IsMod2() )
92cdf0e10cSrcweir 		        _rEvent.Modifiers |= KeyModifier::MOD2;
93cdf0e10cSrcweir                 if ( _rVclEvent.IsMod3() )
94cdf0e10cSrcweir                         _rEvent.Modifiers |= KeyModifier::MOD3;
95cdf0e10cSrcweir         }
96cdf0e10cSrcweir 
lcl_initKeyEvent(KeyEvent & rEvent,const::KeyEvent & rEvt)97cdf0e10cSrcweir         void lcl_initKeyEvent( KeyEvent& rEvent, const ::KeyEvent& rEvt )
98cdf0e10cSrcweir         {
99cdf0e10cSrcweir             lcl_initModifiers( rEvent, rEvt.GetKeyCode() );
100cdf0e10cSrcweir 
101cdf0e10cSrcweir 	        rEvent.KeyCode = rEvt.GetKeyCode().GetCode();
102cdf0e10cSrcweir 	        rEvent.KeyChar = rEvt.GetCharCode();
103cdf0e10cSrcweir 	        rEvent.KeyFunc = sal::static_int_cast< sal_Int16 >( rEvt.GetKeyCode().GetFunction());
104cdf0e10cSrcweir         }
105cdf0e10cSrcweir 
lcl_initMouseEvent(MouseEvent & rEvent,const::MouseEvent & rEvt)106cdf0e10cSrcweir         void lcl_initMouseEvent( MouseEvent& rEvent, const ::MouseEvent& rEvt )
107cdf0e10cSrcweir         {
108cdf0e10cSrcweir             lcl_initModifiers( rEvent, rEvt );
109cdf0e10cSrcweir 
110cdf0e10cSrcweir 	        rEvent.Buttons = 0;
111cdf0e10cSrcweir 	        if ( rEvt.IsLeft() )
112cdf0e10cSrcweir 		        rEvent.Buttons |= MouseButton::LEFT;
113cdf0e10cSrcweir 	        if ( rEvt.IsRight() )
114cdf0e10cSrcweir 		        rEvent.Buttons |= MouseButton::RIGHT;
115cdf0e10cSrcweir 	        if ( rEvt.IsMiddle() )
116cdf0e10cSrcweir 		        rEvent.Buttons |= MouseButton::MIDDLE;
117cdf0e10cSrcweir 
118cdf0e10cSrcweir 	        rEvent.X = rEvt.GetPosPixel().X();
119cdf0e10cSrcweir 	        rEvent.Y = rEvt.GetPosPixel().Y();
120cdf0e10cSrcweir 	        rEvent.ClickCount = rEvt.GetClicks();
121cdf0e10cSrcweir 	        rEvent.PopupTrigger = sal_False;
122cdf0e10cSrcweir         }
123cdf0e10cSrcweir 
124cdf0e10cSrcweir     }
125cdf0e10cSrcweir 
126cdf0e10cSrcweir 	//====================================================================
127cdf0e10cSrcweir 	//= UserInputInterception
128cdf0e10cSrcweir 	//====================================================================
129cdf0e10cSrcweir 	//--------------------------------------------------------------------
UserInputInterception(::cppu::OWeakObject & _rControllerImpl,::osl::Mutex & _rMutex)130cdf0e10cSrcweir     UserInputInterception::UserInputInterception( ::cppu::OWeakObject& _rControllerImpl, ::osl::Mutex& _rMutex )
131cdf0e10cSrcweir         :m_pData( new UserInputInterception_Data( _rControllerImpl, _rMutex ) )
132cdf0e10cSrcweir     {
133cdf0e10cSrcweir     }
134cdf0e10cSrcweir 
135cdf0e10cSrcweir     //--------------------------------------------------------------------
~UserInputInterception()136cdf0e10cSrcweir     UserInputInterception::~UserInputInterception()
137cdf0e10cSrcweir     {
138cdf0e10cSrcweir     }
139cdf0e10cSrcweir 
140cdf0e10cSrcweir     //--------------------------------------------------------------------
addKeyHandler(const Reference<XKeyHandler> & _rxHandler)141cdf0e10cSrcweir     void UserInputInterception::addKeyHandler( const Reference< XKeyHandler >& _rxHandler ) throw (RuntimeException)
142cdf0e10cSrcweir     {
143cdf0e10cSrcweir         if ( _rxHandler.is() )
144cdf0e10cSrcweir 	        m_pData->m_aKeyHandlers.addInterface( _rxHandler );
145cdf0e10cSrcweir     }
146cdf0e10cSrcweir 
147cdf0e10cSrcweir     //--------------------------------------------------------------------
removeKeyHandler(const Reference<XKeyHandler> & _rxHandler)148cdf0e10cSrcweir     void UserInputInterception::removeKeyHandler( const Reference< XKeyHandler >& _rxHandler ) throw (RuntimeException)
149cdf0e10cSrcweir     {
150cdf0e10cSrcweir         m_pData->m_aKeyHandlers.removeInterface( _rxHandler );
151cdf0e10cSrcweir     }
152cdf0e10cSrcweir 
153cdf0e10cSrcweir     //--------------------------------------------------------------------
addMouseClickHandler(const Reference<XMouseClickHandler> & _rxHandler)154cdf0e10cSrcweir     void UserInputInterception::addMouseClickHandler( const Reference< XMouseClickHandler >& _rxHandler ) throw (RuntimeException)
155cdf0e10cSrcweir     {
156cdf0e10cSrcweir         if ( _rxHandler.is() )
157cdf0e10cSrcweir 	        m_pData->m_aMouseClickHandlers.addInterface( _rxHandler );
158cdf0e10cSrcweir     }
159cdf0e10cSrcweir 
160cdf0e10cSrcweir     //--------------------------------------------------------------------
removeMouseClickHandler(const Reference<XMouseClickHandler> & _rxHandler)161cdf0e10cSrcweir     void UserInputInterception::removeMouseClickHandler( const Reference< XMouseClickHandler >& _rxHandler ) throw (RuntimeException)
162cdf0e10cSrcweir     {
163cdf0e10cSrcweir         m_pData->m_aMouseClickHandlers.removeInterface( _rxHandler );
164cdf0e10cSrcweir     }
165cdf0e10cSrcweir 
166cdf0e10cSrcweir     //--------------------------------------------------------------------
hasKeyHandlers() const167cdf0e10cSrcweir     bool UserInputInterception::hasKeyHandlers() const
168cdf0e10cSrcweir     {
169cdf0e10cSrcweir         return m_pData->m_aKeyHandlers.getLength() > 0;
170cdf0e10cSrcweir     }
171cdf0e10cSrcweir 
172cdf0e10cSrcweir     //--------------------------------------------------------------------
hasMouseClickListeners() const173cdf0e10cSrcweir     bool UserInputInterception::hasMouseClickListeners() const
174cdf0e10cSrcweir     {
175cdf0e10cSrcweir         return m_pData->m_aMouseClickHandlers.getLength() > 0;
176cdf0e10cSrcweir     }
177cdf0e10cSrcweir 
178cdf0e10cSrcweir     //--------------------------------------------------------------------
handleNotifyEvent(const NotifyEvent & _rEvent)179cdf0e10cSrcweir     bool UserInputInterception::handleNotifyEvent( const NotifyEvent& _rEvent )
180cdf0e10cSrcweir     {
181cdf0e10cSrcweir         Reference < XInterface > xHoldAlive( m_pData->m_rControllerImpl );
182cdf0e10cSrcweir 
183cdf0e10cSrcweir         sal_uInt16 nType = _rEvent.GetType();
184cdf0e10cSrcweir         bool bHandled = false;
185cdf0e10cSrcweir 
186cdf0e10cSrcweir 		switch ( nType )
187cdf0e10cSrcweir         {
188cdf0e10cSrcweir             case EVENT_KEYINPUT:
189cdf0e10cSrcweir             case EVENT_KEYUP:
190cdf0e10cSrcweir             {
191cdf0e10cSrcweir                 KeyEvent aEvent;
192cdf0e10cSrcweir                 lcl_initKeyEvent( aEvent, *_rEvent.GetKeyEvent() );
193cdf0e10cSrcweir                 if ( _rEvent.GetWindow() )
194cdf0e10cSrcweir                     aEvent.Source = _rEvent.GetWindow()->GetComponentInterface();
195cdf0e10cSrcweir 
196cdf0e10cSrcweir                 ::cppu::OInterfaceIteratorHelper aIterator( m_pData->m_aKeyHandlers );
197cdf0e10cSrcweir                 while ( aIterator.hasMoreElements() )
198cdf0e10cSrcweir                 {
199cdf0e10cSrcweir                     Reference< XKeyHandler > xHandler( static_cast< XKeyHandler* >( aIterator.next() ) );
200cdf0e10cSrcweir                     if ( !xHandler.is() )
201cdf0e10cSrcweir                         continue;
202cdf0e10cSrcweir 
203cdf0e10cSrcweir                     try
204cdf0e10cSrcweir                     {
205cdf0e10cSrcweir                         if ( nType == EVENT_KEYINPUT )
206cdf0e10cSrcweir                             bHandled = xHandler->keyPressed( aEvent );
207cdf0e10cSrcweir                         else
208cdf0e10cSrcweir                             bHandled = xHandler->keyReleased( aEvent );
209cdf0e10cSrcweir                     }
210cdf0e10cSrcweir                     catch( const DisposedException& e )
211cdf0e10cSrcweir                     {
212cdf0e10cSrcweir                         if ( e.Context == xHandler )
213cdf0e10cSrcweir                             aIterator.remove();
214cdf0e10cSrcweir                     }
215cdf0e10cSrcweir                     catch( const RuntimeException& )
216cdf0e10cSrcweir                     {
217cdf0e10cSrcweir                         throw;
218cdf0e10cSrcweir                     }
219cdf0e10cSrcweir                     catch( const Exception& )
220cdf0e10cSrcweir                     {
221cdf0e10cSrcweir                     }
222cdf0e10cSrcweir                 }
223cdf0e10cSrcweir             }
224cdf0e10cSrcweir             break;
225cdf0e10cSrcweir 
226cdf0e10cSrcweir             case EVENT_MOUSEBUTTONDOWN:
227cdf0e10cSrcweir             case EVENT_MOUSEBUTTONUP:
228cdf0e10cSrcweir             {
229cdf0e10cSrcweir                 MouseEvent aEvent;
230cdf0e10cSrcweir                 lcl_initMouseEvent( aEvent, *_rEvent.GetMouseEvent() );
231cdf0e10cSrcweir                 if ( _rEvent.GetWindow() )
232cdf0e10cSrcweir                     aEvent.Source = _rEvent.GetWindow()->GetComponentInterface();
233cdf0e10cSrcweir 
234cdf0e10cSrcweir                 ::cppu::OInterfaceIteratorHelper aIterator( m_pData->m_aMouseClickHandlers );
235cdf0e10cSrcweir                 while ( aIterator.hasMoreElements() )
236cdf0e10cSrcweir                 {
237cdf0e10cSrcweir                     Reference< XMouseClickHandler > xHandler( static_cast< XMouseClickHandler* >( aIterator.next() ) );
238cdf0e10cSrcweir                     if ( !xHandler.is() )
239cdf0e10cSrcweir                         continue;
240cdf0e10cSrcweir 
241cdf0e10cSrcweir                     try
242cdf0e10cSrcweir                     {
243cdf0e10cSrcweir                         if ( nType == EVENT_MOUSEBUTTONDOWN )
244cdf0e10cSrcweir                             bHandled = xHandler->mousePressed( aEvent );
245cdf0e10cSrcweir                         else
246cdf0e10cSrcweir                             bHandled = xHandler->mouseReleased( aEvent );
247cdf0e10cSrcweir                     }
248cdf0e10cSrcweir                     catch( const DisposedException& e )
249cdf0e10cSrcweir                     {
250cdf0e10cSrcweir                         if ( e.Context == xHandler )
251cdf0e10cSrcweir                             aIterator.remove();
252cdf0e10cSrcweir                     }
253cdf0e10cSrcweir                     catch( const RuntimeException& )
254cdf0e10cSrcweir                     {
255cdf0e10cSrcweir                         throw;
256cdf0e10cSrcweir                     }
257cdf0e10cSrcweir                     catch( const Exception& )
258cdf0e10cSrcweir                     {
259cdf0e10cSrcweir                     }
260cdf0e10cSrcweir                 }
261cdf0e10cSrcweir             }
262cdf0e10cSrcweir             break;
263cdf0e10cSrcweir 
264cdf0e10cSrcweir             default:
265cdf0e10cSrcweir                 OSL_ENSURE( false, "UserInputInterception::handleNotifyEvent: illegal event type!" );
266cdf0e10cSrcweir                 break;
267cdf0e10cSrcweir         }
268cdf0e10cSrcweir 
269cdf0e10cSrcweir         return bHandled;
270cdf0e10cSrcweir     }
271cdf0e10cSrcweir 
272cdf0e10cSrcweir //........................................................................
273cdf0e10cSrcweir } // namespace sfx2
274cdf0e10cSrcweir //........................................................................
275