LCOV - code coverage report
Current view: top level - libreoffice/sfx2/source/view - userinputinterception.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 9 105 8.6 %
Date: 2012-12-27 Functions: 4 15 26.7 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10