1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ***********************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_sfx2.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "sfx2/userinputinterception.hxx"
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir /** === begin UNO includes === **/
34*cdf0e10cSrcweir #include <com/sun/star/awt/MouseButton.hpp>
35*cdf0e10cSrcweir #include <com/sun/star/awt/KeyModifier.hpp>
36*cdf0e10cSrcweir /** === end UNO includes === **/
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir #include <cppuhelper/interfacecontainer.hxx>
39*cdf0e10cSrcweir #include <cppuhelper/weak.hxx>
40*cdf0e10cSrcweir #include <vcl/event.hxx>
41*cdf0e10cSrcweir #include <vcl/window.hxx>
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir //........................................................................
44*cdf0e10cSrcweir namespace sfx2
45*cdf0e10cSrcweir {
46*cdf0e10cSrcweir //........................................................................
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir 	/** === begin UNO using === **/
49*cdf0e10cSrcweir 	using ::com::sun::star::uno::Reference;
50*cdf0e10cSrcweir 	using ::com::sun::star::uno::XInterface;
51*cdf0e10cSrcweir 	using ::com::sun::star::uno::UNO_QUERY;
52*cdf0e10cSrcweir 	using ::com::sun::star::uno::UNO_QUERY_THROW;
53*cdf0e10cSrcweir 	using ::com::sun::star::uno::UNO_SET_THROW;
54*cdf0e10cSrcweir 	using ::com::sun::star::uno::Exception;
55*cdf0e10cSrcweir 	using ::com::sun::star::uno::RuntimeException;
56*cdf0e10cSrcweir 	using ::com::sun::star::uno::Any;
57*cdf0e10cSrcweir 	using ::com::sun::star::uno::makeAny;
58*cdf0e10cSrcweir 	using ::com::sun::star::awt::MouseEvent;
59*cdf0e10cSrcweir 	using ::com::sun::star::awt::KeyEvent;
60*cdf0e10cSrcweir 	using ::com::sun::star::awt::InputEvent;
61*cdf0e10cSrcweir     using ::com::sun::star::awt::XKeyHandler;
62*cdf0e10cSrcweir     using ::com::sun::star::awt::XMouseClickHandler;
63*cdf0e10cSrcweir     using ::com::sun::star::lang::DisposedException;
64*cdf0e10cSrcweir 	/** === end UNO using === **/
65*cdf0e10cSrcweir     namespace MouseButton = ::com::sun::star::awt::MouseButton;
66*cdf0e10cSrcweir     namespace KeyModifier = ::com::sun::star::awt::KeyModifier;
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir     struct UserInputInterception_Data
69*cdf0e10cSrcweir     {
70*cdf0e10cSrcweir     public:
71*cdf0e10cSrcweir         ::cppu::OWeakObject&                m_rControllerImpl;
72*cdf0e10cSrcweir         ::cppu::OInterfaceContainerHelper   m_aKeyHandlers;
73*cdf0e10cSrcweir         ::cppu::OInterfaceContainerHelper   m_aMouseClickHandlers;
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir     public:
76*cdf0e10cSrcweir         UserInputInterception_Data( ::cppu::OWeakObject& _rControllerImpl, ::osl::Mutex& _rMutex )
77*cdf0e10cSrcweir             :m_rControllerImpl( _rControllerImpl )
78*cdf0e10cSrcweir             ,m_aKeyHandlers( _rMutex )
79*cdf0e10cSrcweir             ,m_aMouseClickHandlers( _rMutex )
80*cdf0e10cSrcweir         {
81*cdf0e10cSrcweir         }
82*cdf0e10cSrcweir     };
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir     namespace
85*cdf0e10cSrcweir     {
86*cdf0e10cSrcweir         template< class VLCEVENT >
87*cdf0e10cSrcweir         void lcl_initModifiers( InputEvent& _rEvent, const VLCEVENT& _rVclEvent )
88*cdf0e10cSrcweir         {
89*cdf0e10cSrcweir 	        _rEvent.Modifiers = 0;
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir             if ( _rVclEvent.IsShift() )
92*cdf0e10cSrcweir 		        _rEvent.Modifiers |= KeyModifier::SHIFT;
93*cdf0e10cSrcweir 	        if ( _rVclEvent.IsMod1() )
94*cdf0e10cSrcweir 		        _rEvent.Modifiers |= KeyModifier::MOD1;
95*cdf0e10cSrcweir 	        if ( _rVclEvent.IsMod2() )
96*cdf0e10cSrcweir 		        _rEvent.Modifiers |= KeyModifier::MOD2;
97*cdf0e10cSrcweir                 if ( _rVclEvent.IsMod3() )
98*cdf0e10cSrcweir                         _rEvent.Modifiers |= KeyModifier::MOD3;
99*cdf0e10cSrcweir         }
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir         void lcl_initKeyEvent( KeyEvent& rEvent, const ::KeyEvent& rEvt )
102*cdf0e10cSrcweir         {
103*cdf0e10cSrcweir             lcl_initModifiers( rEvent, rEvt.GetKeyCode() );
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir 	        rEvent.KeyCode = rEvt.GetKeyCode().GetCode();
106*cdf0e10cSrcweir 	        rEvent.KeyChar = rEvt.GetCharCode();
107*cdf0e10cSrcweir 	        rEvent.KeyFunc = sal::static_int_cast< sal_Int16 >( rEvt.GetKeyCode().GetFunction());
108*cdf0e10cSrcweir         }
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir         void lcl_initMouseEvent( MouseEvent& rEvent, const ::MouseEvent& rEvt )
111*cdf0e10cSrcweir         {
112*cdf0e10cSrcweir             lcl_initModifiers( rEvent, rEvt );
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir 	        rEvent.Buttons = 0;
115*cdf0e10cSrcweir 	        if ( rEvt.IsLeft() )
116*cdf0e10cSrcweir 		        rEvent.Buttons |= MouseButton::LEFT;
117*cdf0e10cSrcweir 	        if ( rEvt.IsRight() )
118*cdf0e10cSrcweir 		        rEvent.Buttons |= MouseButton::RIGHT;
119*cdf0e10cSrcweir 	        if ( rEvt.IsMiddle() )
120*cdf0e10cSrcweir 		        rEvent.Buttons |= MouseButton::MIDDLE;
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir 	        rEvent.X = rEvt.GetPosPixel().X();
123*cdf0e10cSrcweir 	        rEvent.Y = rEvt.GetPosPixel().Y();
124*cdf0e10cSrcweir 	        rEvent.ClickCount = rEvt.GetClicks();
125*cdf0e10cSrcweir 	        rEvent.PopupTrigger = sal_False;
126*cdf0e10cSrcweir         }
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir     }
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir 	//====================================================================
131*cdf0e10cSrcweir 	//= UserInputInterception
132*cdf0e10cSrcweir 	//====================================================================
133*cdf0e10cSrcweir 	//--------------------------------------------------------------------
134*cdf0e10cSrcweir     UserInputInterception::UserInputInterception( ::cppu::OWeakObject& _rControllerImpl, ::osl::Mutex& _rMutex )
135*cdf0e10cSrcweir         :m_pData( new UserInputInterception_Data( _rControllerImpl, _rMutex ) )
136*cdf0e10cSrcweir     {
137*cdf0e10cSrcweir     }
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir     //--------------------------------------------------------------------
140*cdf0e10cSrcweir     UserInputInterception::~UserInputInterception()
141*cdf0e10cSrcweir     {
142*cdf0e10cSrcweir     }
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir     //--------------------------------------------------------------------
145*cdf0e10cSrcweir     void UserInputInterception::addKeyHandler( const Reference< XKeyHandler >& _rxHandler ) throw (RuntimeException)
146*cdf0e10cSrcweir     {
147*cdf0e10cSrcweir         if ( _rxHandler.is() )
148*cdf0e10cSrcweir 	        m_pData->m_aKeyHandlers.addInterface( _rxHandler );
149*cdf0e10cSrcweir     }
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir     //--------------------------------------------------------------------
152*cdf0e10cSrcweir     void UserInputInterception::removeKeyHandler( const Reference< XKeyHandler >& _rxHandler ) throw (RuntimeException)
153*cdf0e10cSrcweir     {
154*cdf0e10cSrcweir         m_pData->m_aKeyHandlers.removeInterface( _rxHandler );
155*cdf0e10cSrcweir     }
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir     //--------------------------------------------------------------------
158*cdf0e10cSrcweir     void UserInputInterception::addMouseClickHandler( const Reference< XMouseClickHandler >& _rxHandler ) throw (RuntimeException)
159*cdf0e10cSrcweir     {
160*cdf0e10cSrcweir         if ( _rxHandler.is() )
161*cdf0e10cSrcweir 	        m_pData->m_aMouseClickHandlers.addInterface( _rxHandler );
162*cdf0e10cSrcweir     }
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir     //--------------------------------------------------------------------
165*cdf0e10cSrcweir     void UserInputInterception::removeMouseClickHandler( const Reference< XMouseClickHandler >& _rxHandler ) throw (RuntimeException)
166*cdf0e10cSrcweir     {
167*cdf0e10cSrcweir         m_pData->m_aMouseClickHandlers.removeInterface( _rxHandler );
168*cdf0e10cSrcweir     }
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir     //--------------------------------------------------------------------
171*cdf0e10cSrcweir     bool UserInputInterception::hasKeyHandlers() const
172*cdf0e10cSrcweir     {
173*cdf0e10cSrcweir         return m_pData->m_aKeyHandlers.getLength() > 0;
174*cdf0e10cSrcweir     }
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir     //--------------------------------------------------------------------
177*cdf0e10cSrcweir     bool UserInputInterception::hasMouseClickListeners() const
178*cdf0e10cSrcweir     {
179*cdf0e10cSrcweir         return m_pData->m_aMouseClickHandlers.getLength() > 0;
180*cdf0e10cSrcweir     }
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir     //--------------------------------------------------------------------
183*cdf0e10cSrcweir     bool UserInputInterception::handleNotifyEvent( const NotifyEvent& _rEvent )
184*cdf0e10cSrcweir     {
185*cdf0e10cSrcweir         Reference < XInterface > xHoldAlive( m_pData->m_rControllerImpl );
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir         sal_uInt16 nType = _rEvent.GetType();
188*cdf0e10cSrcweir         bool bHandled = false;
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir 		switch ( nType )
191*cdf0e10cSrcweir         {
192*cdf0e10cSrcweir             case EVENT_KEYINPUT:
193*cdf0e10cSrcweir             case EVENT_KEYUP:
194*cdf0e10cSrcweir             {
195*cdf0e10cSrcweir                 KeyEvent aEvent;
196*cdf0e10cSrcweir                 lcl_initKeyEvent( aEvent, *_rEvent.GetKeyEvent() );
197*cdf0e10cSrcweir                 if ( _rEvent.GetWindow() )
198*cdf0e10cSrcweir                     aEvent.Source = _rEvent.GetWindow()->GetComponentInterface();
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir                 ::cppu::OInterfaceIteratorHelper aIterator( m_pData->m_aKeyHandlers );
201*cdf0e10cSrcweir                 while ( aIterator.hasMoreElements() )
202*cdf0e10cSrcweir                 {
203*cdf0e10cSrcweir                     Reference< XKeyHandler > xHandler( static_cast< XKeyHandler* >( aIterator.next() ) );
204*cdf0e10cSrcweir                     if ( !xHandler.is() )
205*cdf0e10cSrcweir                         continue;
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir                     try
208*cdf0e10cSrcweir                     {
209*cdf0e10cSrcweir                         if ( nType == EVENT_KEYINPUT )
210*cdf0e10cSrcweir                             bHandled = xHandler->keyPressed( aEvent );
211*cdf0e10cSrcweir                         else
212*cdf0e10cSrcweir                             bHandled = xHandler->keyReleased( aEvent );
213*cdf0e10cSrcweir                     }
214*cdf0e10cSrcweir                     catch( const DisposedException& e )
215*cdf0e10cSrcweir                     {
216*cdf0e10cSrcweir                         if ( e.Context == xHandler )
217*cdf0e10cSrcweir                             aIterator.remove();
218*cdf0e10cSrcweir                     }
219*cdf0e10cSrcweir                     catch( const RuntimeException& )
220*cdf0e10cSrcweir                     {
221*cdf0e10cSrcweir                         throw;
222*cdf0e10cSrcweir                     }
223*cdf0e10cSrcweir                     catch( const Exception& )
224*cdf0e10cSrcweir                     {
225*cdf0e10cSrcweir                     }
226*cdf0e10cSrcweir                 }
227*cdf0e10cSrcweir             }
228*cdf0e10cSrcweir             break;
229*cdf0e10cSrcweir 
230*cdf0e10cSrcweir             case EVENT_MOUSEBUTTONDOWN:
231*cdf0e10cSrcweir             case EVENT_MOUSEBUTTONUP:
232*cdf0e10cSrcweir             {
233*cdf0e10cSrcweir                 MouseEvent aEvent;
234*cdf0e10cSrcweir                 lcl_initMouseEvent( aEvent, *_rEvent.GetMouseEvent() );
235*cdf0e10cSrcweir                 if ( _rEvent.GetWindow() )
236*cdf0e10cSrcweir                     aEvent.Source = _rEvent.GetWindow()->GetComponentInterface();
237*cdf0e10cSrcweir 
238*cdf0e10cSrcweir                 ::cppu::OInterfaceIteratorHelper aIterator( m_pData->m_aMouseClickHandlers );
239*cdf0e10cSrcweir                 while ( aIterator.hasMoreElements() )
240*cdf0e10cSrcweir                 {
241*cdf0e10cSrcweir                     Reference< XMouseClickHandler > xHandler( static_cast< XMouseClickHandler* >( aIterator.next() ) );
242*cdf0e10cSrcweir                     if ( !xHandler.is() )
243*cdf0e10cSrcweir                         continue;
244*cdf0e10cSrcweir 
245*cdf0e10cSrcweir                     try
246*cdf0e10cSrcweir                     {
247*cdf0e10cSrcweir                         if ( nType == EVENT_MOUSEBUTTONDOWN )
248*cdf0e10cSrcweir                             bHandled = xHandler->mousePressed( aEvent );
249*cdf0e10cSrcweir                         else
250*cdf0e10cSrcweir                             bHandled = xHandler->mouseReleased( aEvent );
251*cdf0e10cSrcweir                     }
252*cdf0e10cSrcweir                     catch( const DisposedException& e )
253*cdf0e10cSrcweir                     {
254*cdf0e10cSrcweir                         if ( e.Context == xHandler )
255*cdf0e10cSrcweir                             aIterator.remove();
256*cdf0e10cSrcweir                     }
257*cdf0e10cSrcweir                     catch( const RuntimeException& )
258*cdf0e10cSrcweir                     {
259*cdf0e10cSrcweir                         throw;
260*cdf0e10cSrcweir                     }
261*cdf0e10cSrcweir                     catch( const Exception& )
262*cdf0e10cSrcweir                     {
263*cdf0e10cSrcweir                     }
264*cdf0e10cSrcweir                 }
265*cdf0e10cSrcweir             }
266*cdf0e10cSrcweir             break;
267*cdf0e10cSrcweir 
268*cdf0e10cSrcweir             default:
269*cdf0e10cSrcweir                 OSL_ENSURE( false, "UserInputInterception::handleNotifyEvent: illegal event type!" );
270*cdf0e10cSrcweir                 break;
271*cdf0e10cSrcweir         }
272*cdf0e10cSrcweir 
273*cdf0e10cSrcweir         return bHandled;
274*cdf0e10cSrcweir     }
275*cdf0e10cSrcweir 
276*cdf0e10cSrcweir //........................................................................
277*cdf0e10cSrcweir } // namespace sfx2
278*cdf0e10cSrcweir //........................................................................
279