LibreOffice Module sfx2 (master) 1
userinputinterception.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20
22
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>
32
34#include <cppuhelper/weak.hxx>
35#include <vcl/event.hxx>
36#include <vcl/window.hxx>
37#include <osl/diagnose.h>
38
39namespace sfx2
40{
41
42
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;
53
54 namespace MouseButton = ::com::sun::star::awt::MouseButton;
55 namespace KeyModifier = ::com::sun::star::awt::KeyModifier;
56
58 {
59 public:
63
64 public:
65 UserInputInterception_Data( ::cppu::OWeakObject& _rControllerImpl, ::osl::Mutex& _rMutex )
66 :m_rControllerImpl( _rControllerImpl )
67 ,m_aKeyHandlers( _rMutex )
68 ,m_aMouseClickHandlers( _rMutex )
69 {
70 }
71 };
72
73 namespace
74 {
75 template< class VCLEVENT >
76 void lcl_initModifiers( InputEvent& _rEvent, const VCLEVENT& _rVclEvent )
77 {
78 _rEvent.Modifiers = 0;
79
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;
88 }
89
90 void lcl_initKeyEvent( KeyEvent& rEvent, const ::KeyEvent& rEvt )
91 {
92 lcl_initModifiers( rEvent, rEvt.GetKeyCode() );
93
94 rEvent.KeyCode = rEvt.GetKeyCode().GetCode();
95 rEvent.KeyChar = rEvt.GetCharCode();
96 rEvent.KeyFunc = sal::static_int_cast< sal_Int16 >( rEvt.GetKeyCode().GetFunction());
97 }
98
99 void lcl_initMouseEvent( MouseEvent& rEvent, const ::MouseEvent& rEvt )
100 {
101 lcl_initModifiers( rEvent, rEvt );
102
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;
110
111 rEvent.X = rEvt.GetPosPixel().X();
112 rEvent.Y = rEvt.GetPosPixel().Y();
113 rEvent.ClickCount = rEvt.GetClicks();
114 rEvent.PopupTrigger = false;
115 }
116
117 }
118
119
120 //= UserInputInterception
121
122
123 UserInputInterception::UserInputInterception( ::cppu::OWeakObject& _rControllerImpl, ::osl::Mutex& _rMutex )
124 :m_pData( new UserInputInterception_Data( _rControllerImpl, _rMutex ) )
125 {
126 }
127
128
130 {
131 }
132
133
134 void UserInputInterception::addKeyHandler( const Reference< XKeyHandler >& _rxHandler )
135 {
136 if ( _rxHandler.is() )
137 m_pData->m_aKeyHandlers.addInterface( _rxHandler );
138 }
139
140
141 void UserInputInterception::removeKeyHandler( const Reference< XKeyHandler >& _rxHandler )
142 {
143 m_pData->m_aKeyHandlers.removeInterface( _rxHandler );
144 }
145
146
147 void UserInputInterception::addMouseClickHandler( const Reference< XMouseClickHandler >& _rxHandler )
148 {
149 if ( _rxHandler.is() )
150 m_pData->m_aMouseClickHandlers.addInterface( _rxHandler );
151 }
152
153
154 void UserInputInterception::removeMouseClickHandler( const Reference< XMouseClickHandler >& _rxHandler )
155 {
156 m_pData->m_aMouseClickHandlers.removeInterface( _rxHandler );
157 }
158
159
161 {
162 return m_pData->m_aKeyHandlers.getLength() > 0;
163 }
164
165
167 {
168 return m_pData->m_aMouseClickHandlers.getLength() > 0;
169 }
170
171
173 {
174 Reference < XInterface > xHoldAlive( m_pData->m_rControllerImpl );
175
176 NotifyEventType nType = _rEvent.GetType();
177 bool bHandled = false;
178
179 switch ( nType )
180 {
181 case NotifyEventType::KEYINPUT:
182 case NotifyEventType::KEYUP:
183 {
185 lcl_initKeyEvent( aEvent, *_rEvent.GetKeyEvent() );
186 if ( _rEvent.GetWindow() )
187 aEvent.Source = _rEvent.GetWindow()->GetComponentInterface();
188
189 ::comphelper::OInterfaceIteratorHelper3 aIterator( m_pData->m_aKeyHandlers );
190 while ( aIterator.hasMoreElements() )
191 {
192 Reference< XKeyHandler > xHandler( aIterator.next() );
193 try
194 {
195 if ( nType == NotifyEventType::KEYINPUT )
196 bHandled = xHandler->keyPressed( aEvent );
197 else
198 bHandled = xHandler->keyReleased( aEvent );
199 }
200 catch( const DisposedException& e )
201 {
202 if ( e.Context == xHandler )
203 aIterator.remove();
204 }
205 catch( const RuntimeException& )
206 {
207 throw;
208 }
209 catch( const Exception& )
210 {
211 }
212 }
213 }
214 break;
215
216 case NotifyEventType::MOUSEBUTTONDOWN:
217 case NotifyEventType::MOUSEBUTTONUP:
218 {
220 lcl_initMouseEvent( aEvent, *_rEvent.GetMouseEvent() );
221 if ( _rEvent.GetWindow() )
222 aEvent.Source = _rEvent.GetWindow()->GetComponentInterface();
223
224 ::comphelper::OInterfaceIteratorHelper3 aIterator( m_pData->m_aMouseClickHandlers );
225 while ( aIterator.hasMoreElements() )
226 {
227 Reference< XMouseClickHandler > xHandler( aIterator.next() );
228 try
229 {
230 if ( nType == NotifyEventType::MOUSEBUTTONDOWN )
231 bHandled = xHandler->mousePressed( aEvent );
232 else
233 bHandled = xHandler->mouseReleased( aEvent );
234 }
235 catch( const DisposedException& e )
236 {
237 if ( e.Context == xHandler )
238 aIterator.remove();
239 }
240 catch( const RuntimeException& )
241 {
242 throw;
243 }
244 catch( const Exception& )
245 {
246 }
247 }
248 }
249 break;
250
251 default:
252 OSL_FAIL( "UserInputInterception::handleNotifyEvent: illegal event type!" );
253 break;
254 }
255
256 return bHandled;
257 }
258
259
260} // namespace sfx2
261
262
263/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
AnyEventRef aEvent
sal_Unicode GetCharCode() const
const vcl::KeyCode & GetKeyCode() const
sal_uInt16 GetClicks() const
const Point & GetPosPixel() const
const KeyEvent * GetKeyEvent() const
vcl::Window * GetWindow() const
const MouseEvent * GetMouseEvent() const
NotifyEventType GetType() const
constexpr tools::Long Y() const
constexpr tools::Long X() const
css::uno::Reference< ListenerT > const & next()
void removeMouseClickHandler(const css::uno::Reference< css::awt::XMouseClickHandler > &xHandler)
bool handleNotifyEvent(const NotifyEvent &_rEvent)
void removeKeyHandler(const css::uno::Reference< css::awt::XKeyHandler > &xHandler)
void addMouseClickHandler(const css::uno::Reference< css::awt::XMouseClickHandler > &xHandler)
UserInputInterception(::cppu::OWeakObject &_rControllerImpl, ::osl::Mutex &_rMutex)
void addKeyHandler(const css::uno::Reference< css::awt::XKeyHandler > &xHandler)
::std::unique_ptr< UserInputInterception_Data > m_pData
sal_uInt16 GetCode() const
virtual css::uno::Reference< css::awt::XVclWindowPeer > GetComponentInterface(bool bCreate=true)
NotifyEventType
FmFilterData * m_pData
@ Exception
QPRO_FUNC_TYPE nType
::comphelper::OInterfaceContainerHelper3< XKeyHandler > m_aKeyHandlers
::comphelper::OInterfaceContainerHelper3< XMouseClickHandler > m_aMouseClickHandlers
UserInputInterception_Data(::cppu::OWeakObject &_rControllerImpl, ::osl::Mutex &_rMutex)