Line data Source code
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 :
21 : #include <sfx2/userinputinterception.hxx>
22 :
23 : #include <com/sun/star/awt/MouseButton.hpp>
24 : #include <com/sun/star/awt/KeyModifier.hpp>
25 :
26 : #include <cppuhelper/interfacecontainer.hxx>
27 : #include <cppuhelper/weak.hxx>
28 : #include <vcl/event.hxx>
29 : #include <vcl/window.hxx>
30 :
31 :
32 : namespace sfx2
33 : {
34 :
35 :
36 : using ::com::sun::star::uno::Reference;
37 : using ::com::sun::star::uno::XInterface;
38 : using ::com::sun::star::uno::UNO_QUERY;
39 : using ::com::sun::star::uno::UNO_QUERY_THROW;
40 : using ::com::sun::star::uno::UNO_SET_THROW;
41 : using ::com::sun::star::uno::Exception;
42 : using ::com::sun::star::uno::RuntimeException;
43 : using ::com::sun::star::uno::Any;
44 : using ::com::sun::star::uno::makeAny;
45 : using ::com::sun::star::awt::MouseEvent;
46 : using ::com::sun::star::awt::KeyEvent;
47 : using ::com::sun::star::awt::InputEvent;
48 : using ::com::sun::star::awt::XKeyHandler;
49 : using ::com::sun::star::awt::XMouseClickHandler;
50 : using ::com::sun::star::lang::DisposedException;
51 :
52 : namespace MouseButton = ::com::sun::star::awt::MouseButton;
53 : namespace KeyModifier = ::com::sun::star::awt::KeyModifier;
54 :
55 3067 : struct UserInputInterception_Data
56 : {
57 : public:
58 : ::cppu::OWeakObject& m_rControllerImpl;
59 : ::cppu::OInterfaceContainerHelper m_aKeyHandlers;
60 : ::cppu::OInterfaceContainerHelper m_aMouseClickHandlers;
61 :
62 : public:
63 3279 : UserInputInterception_Data( ::cppu::OWeakObject& _rControllerImpl, ::osl::Mutex& _rMutex )
64 : :m_rControllerImpl( _rControllerImpl )
65 : ,m_aKeyHandlers( _rMutex )
66 3279 : ,m_aMouseClickHandlers( _rMutex )
67 : {
68 3279 : }
69 : };
70 :
71 : namespace
72 : {
73 : template< class VLCEVENT >
74 0 : void lcl_initModifiers( InputEvent& _rEvent, const VLCEVENT& _rVclEvent )
75 : {
76 0 : _rEvent.Modifiers = 0;
77 :
78 0 : if ( _rVclEvent.IsShift() )
79 0 : _rEvent.Modifiers |= KeyModifier::SHIFT;
80 0 : if ( _rVclEvent.IsMod1() )
81 0 : _rEvent.Modifiers |= KeyModifier::MOD1;
82 0 : if ( _rVclEvent.IsMod2() )
83 0 : _rEvent.Modifiers |= KeyModifier::MOD2;
84 0 : if ( _rVclEvent.IsMod3() )
85 0 : _rEvent.Modifiers |= KeyModifier::MOD3;
86 0 : }
87 :
88 0 : void lcl_initKeyEvent( KeyEvent& rEvent, const ::KeyEvent& rEvt )
89 : {
90 0 : lcl_initModifiers( rEvent, rEvt.GetKeyCode() );
91 :
92 0 : rEvent.KeyCode = rEvt.GetKeyCode().GetCode();
93 0 : rEvent.KeyChar = rEvt.GetCharCode();
94 0 : rEvent.KeyFunc = sal::static_int_cast< sal_Int16 >( rEvt.GetKeyCode().GetFunction());
95 0 : }
96 :
97 0 : void lcl_initMouseEvent( MouseEvent& rEvent, const ::MouseEvent& rEvt )
98 : {
99 0 : lcl_initModifiers( rEvent, rEvt );
100 :
101 0 : rEvent.Buttons = 0;
102 0 : if ( rEvt.IsLeft() )
103 0 : rEvent.Buttons |= MouseButton::LEFT;
104 0 : if ( rEvt.IsRight() )
105 0 : rEvent.Buttons |= MouseButton::RIGHT;
106 0 : if ( rEvt.IsMiddle() )
107 0 : rEvent.Buttons |= MouseButton::MIDDLE;
108 :
109 0 : rEvent.X = rEvt.GetPosPixel().X();
110 0 : rEvent.Y = rEvt.GetPosPixel().Y();
111 0 : rEvent.ClickCount = rEvt.GetClicks();
112 0 : rEvent.PopupTrigger = sal_False;
113 0 : }
114 :
115 : }
116 :
117 :
118 : //= UserInputInterception
119 :
120 :
121 3279 : UserInputInterception::UserInputInterception( ::cppu::OWeakObject& _rControllerImpl, ::osl::Mutex& _rMutex )
122 3279 : :m_pData( new UserInputInterception_Data( _rControllerImpl, _rMutex ) )
123 : {
124 3279 : }
125 :
126 :
127 3067 : UserInputInterception::~UserInputInterception()
128 : {
129 3067 : }
130 :
131 :
132 0 : void UserInputInterception::addKeyHandler( const Reference< XKeyHandler >& _rxHandler ) throw (RuntimeException)
133 : {
134 0 : if ( _rxHandler.is() )
135 0 : m_pData->m_aKeyHandlers.addInterface( _rxHandler );
136 0 : }
137 :
138 :
139 0 : void UserInputInterception::removeKeyHandler( const Reference< XKeyHandler >& _rxHandler ) throw (RuntimeException)
140 : {
141 0 : m_pData->m_aKeyHandlers.removeInterface( _rxHandler );
142 0 : }
143 :
144 :
145 0 : void UserInputInterception::addMouseClickHandler( const Reference< XMouseClickHandler >& _rxHandler ) throw (RuntimeException)
146 : {
147 0 : if ( _rxHandler.is() )
148 0 : m_pData->m_aMouseClickHandlers.addInterface( _rxHandler );
149 0 : }
150 :
151 :
152 0 : void UserInputInterception::removeMouseClickHandler( const Reference< XMouseClickHandler >& _rxHandler ) throw (RuntimeException)
153 : {
154 0 : m_pData->m_aMouseClickHandlers.removeInterface( _rxHandler );
155 0 : }
156 :
157 :
158 0 : bool UserInputInterception::hasKeyHandlers() const
159 : {
160 0 : return m_pData->m_aKeyHandlers.getLength() > 0;
161 : }
162 :
163 :
164 0 : bool UserInputInterception::hasMouseClickListeners() const
165 : {
166 0 : return m_pData->m_aMouseClickHandlers.getLength() > 0;
167 : }
168 :
169 :
170 0 : bool UserInputInterception::handleNotifyEvent( const NotifyEvent& _rEvent )
171 : {
172 0 : Reference < XInterface > xHoldAlive( m_pData->m_rControllerImpl );
173 :
174 0 : MouseNotifyEvent nType = _rEvent.GetType();
175 0 : bool bHandled = false;
176 :
177 0 : switch ( nType )
178 : {
179 : case MouseNotifyEvent::KEYINPUT:
180 : case MouseNotifyEvent::KEYUP:
181 : {
182 0 : KeyEvent aEvent;
183 0 : lcl_initKeyEvent( aEvent, *_rEvent.GetKeyEvent() );
184 0 : if ( _rEvent.GetWindow() )
185 0 : aEvent.Source = _rEvent.GetWindow()->GetComponentInterface();
186 :
187 0 : ::cppu::OInterfaceIteratorHelper aIterator( m_pData->m_aKeyHandlers );
188 0 : while ( aIterator.hasMoreElements() )
189 : {
190 0 : Reference< XKeyHandler > xHandler( static_cast< XKeyHandler* >( aIterator.next() ) );
191 0 : if ( !xHandler.is() )
192 0 : continue;
193 :
194 : try
195 : {
196 0 : if ( nType == MouseNotifyEvent::KEYINPUT )
197 0 : bHandled = xHandler->keyPressed( aEvent );
198 : else
199 0 : bHandled = xHandler->keyReleased( aEvent );
200 : }
201 0 : catch( const DisposedException& e )
202 : {
203 0 : if ( e.Context == xHandler )
204 0 : aIterator.remove();
205 : }
206 0 : catch( const RuntimeException& )
207 : {
208 0 : throw;
209 : }
210 0 : catch( const Exception& )
211 : {
212 : }
213 0 : }
214 : }
215 0 : break;
216 :
217 : case MouseNotifyEvent::MOUSEBUTTONDOWN:
218 : case MouseNotifyEvent::MOUSEBUTTONUP:
219 : {
220 0 : MouseEvent aEvent;
221 0 : lcl_initMouseEvent( aEvent, *_rEvent.GetMouseEvent() );
222 0 : if ( _rEvent.GetWindow() )
223 0 : aEvent.Source = _rEvent.GetWindow()->GetComponentInterface();
224 :
225 0 : ::cppu::OInterfaceIteratorHelper aIterator( m_pData->m_aMouseClickHandlers );
226 0 : while ( aIterator.hasMoreElements() )
227 : {
228 0 : Reference< XMouseClickHandler > xHandler( static_cast< XMouseClickHandler* >( aIterator.next() ) );
229 0 : if ( !xHandler.is() )
230 0 : continue;
231 :
232 : try
233 : {
234 0 : if ( nType == MouseNotifyEvent::MOUSEBUTTONDOWN )
235 0 : bHandled = xHandler->mousePressed( aEvent );
236 : else
237 0 : bHandled = xHandler->mouseReleased( aEvent );
238 : }
239 0 : catch( const DisposedException& e )
240 : {
241 0 : if ( e.Context == xHandler )
242 0 : aIterator.remove();
243 : }
244 0 : catch( const RuntimeException& )
245 : {
246 0 : throw;
247 : }
248 0 : catch( const Exception& )
249 : {
250 : }
251 0 : }
252 : }
253 0 : break;
254 :
255 : default:
256 : OSL_FAIL( "UserInputInterception::handleNotifyEvent: illegal event type!" );
257 0 : break;
258 : }
259 :
260 0 : return bHandled;
261 : }
262 :
263 :
264 : } // namespace sfx2
265 :
266 :
267 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|