Use correct object
[LibreOffice.git] / sfx2 / source / view / userinputinterception.cxx
blobf609652ea7280353daed73d8c3ff1d4532a1a076
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/XVclWindowPeer.hpp>
31 #include <com/sun/star/uno/XInterface.hpp>
33 #include <comphelper/interfacecontainer3.hxx>
34 #include <cppuhelper/weak.hxx>
35 #include <vcl/event.hxx>
36 #include <vcl/window.hxx>
37 #include <osl/diagnose.h>
39 namespace sfx2
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
59 public:
60 ::cppu::OWeakObject& m_rControllerImpl;
61 ::comphelper::OInterfaceContainerHelper3<XKeyHandler> m_aKeyHandlers;
62 ::comphelper::OInterfaceContainerHelper3<XMouseClickHandler> m_aMouseClickHandlers;
64 public:
65 UserInputInterception_Data( ::cppu::OWeakObject& _rControllerImpl, ::osl::Mutex& _rMutex )
66 :m_rControllerImpl( _rControllerImpl )
67 ,m_aKeyHandlers( _rMutex )
68 ,m_aMouseClickHandlers( _rMutex )
73 namespace
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 );
103 rEvent.Buttons = 0;
104 if ( rEvt.IsLeft() )
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 NotifyEventType nType = _rEvent.GetType();
177 bool bHandled = false;
179 switch ( nType )
181 case NotifyEventType::KEYINPUT:
182 case NotifyEventType::KEYUP:
184 KeyEvent aEvent;
185 lcl_initKeyEvent( aEvent, *_rEvent.GetKeyEvent() );
186 if ( _rEvent.GetWindow() )
187 aEvent.Source = _rEvent.GetWindow()->GetComponentInterface();
189 ::comphelper::OInterfaceIteratorHelper3 aIterator( m_pData->m_aKeyHandlers );
190 while ( aIterator.hasMoreElements() )
192 Reference< XKeyHandler > xHandler( aIterator.next() );
195 if ( nType == NotifyEventType::KEYINPUT )
196 bHandled = xHandler->keyPressed( aEvent );
197 else
198 bHandled = xHandler->keyReleased( aEvent );
200 catch( const DisposedException& e )
202 if ( e.Context == xHandler )
203 aIterator.remove();
205 catch( const RuntimeException& )
207 throw;
209 catch( const Exception& )
214 break;
216 case NotifyEventType::MOUSEBUTTONDOWN:
217 case NotifyEventType::MOUSEBUTTONUP:
219 MouseEvent aEvent;
220 lcl_initMouseEvent( aEvent, *_rEvent.GetMouseEvent() );
221 if ( _rEvent.GetWindow() )
222 aEvent.Source = _rEvent.GetWindow()->GetComponentInterface();
224 ::comphelper::OInterfaceIteratorHelper3 aIterator( m_pData->m_aMouseClickHandlers );
225 while ( aIterator.hasMoreElements() )
227 Reference< XMouseClickHandler > xHandler( aIterator.next() );
230 if ( nType == NotifyEventType::MOUSEBUTTONDOWN )
231 bHandled = xHandler->mousePressed( aEvent );
232 else
233 bHandled = xHandler->mouseReleased( aEvent );
235 catch( const DisposedException& e )
237 if ( e.Context == xHandler )
238 aIterator.remove();
240 catch( const RuntimeException& )
242 throw;
244 catch( const Exception& )
249 break;
251 default:
252 OSL_FAIL( "UserInputInterception::handleNotifyEvent: illegal event type!" );
253 break;
256 return bHandled;
260 } // namespace sfx2
263 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */