LCOV - code coverage report
Current view: top level - dbaccess/source/ui/misc - controllerframe.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 89 125 71.2 %
Date: 2014-11-03 Functions: 21 29 72.4 %
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             : #include <dbaccess/controllerframe.hxx>
      21             : #include <dbaccess/IController.hxx>
      22             : 
      23             : #include <com/sun/star/sdb/XOfficeDatabaseDocument.hpp>
      24             : #include <com/sun/star/awt/XTopWindow.hpp>
      25             : #include <com/sun/star/awt/XWindow2.hpp>
      26             : #include <com/sun/star/lang/DisposedException.hpp>
      27             : #include <com/sun/star/document/XDocumentEventBroadcaster.hpp>
      28             : #include <com/sun/star/frame/XController2.hpp>
      29             : 
      30             : #include <cppuhelper/implbase1.hxx>
      31             : #include <rtl/ref.hxx>
      32             : #include <sfx2/objsh.hxx>
      33             : #include <tools/diagnose_ex.h>
      34             : #include <toolkit/helper/vclunohelper.hxx>
      35             : #include <vcl/window.hxx>
      36             : 
      37             : namespace dbaui
      38             : {
      39             : 
      40             :     using ::com::sun::star::uno::Reference;
      41             :     using ::com::sun::star::uno::XInterface;
      42             :     using ::com::sun::star::uno::UNO_QUERY;
      43             :     using ::com::sun::star::uno::UNO_QUERY_THROW;
      44             :     using ::com::sun::star::uno::UNO_SET_THROW;
      45             :     using ::com::sun::star::uno::Exception;
      46             :     using ::com::sun::star::uno::RuntimeException;
      47             :     using ::com::sun::star::uno::Any;
      48             :     using ::com::sun::star::uno::makeAny;
      49             :     using ::com::sun::star::frame::XFrame;
      50             :     using ::com::sun::star::frame::FrameAction;
      51             :     using ::com::sun::star::frame::FrameAction_FRAME_ACTIVATED;
      52             :     using ::com::sun::star::frame::FrameAction_FRAME_UI_ACTIVATED;
      53             :     using ::com::sun::star::frame::FrameAction_FRAME_DEACTIVATING;
      54             :     using ::com::sun::star::frame::FrameAction_FRAME_UI_DEACTIVATING;
      55             :     using ::com::sun::star::frame::XModel;
      56             :     using ::com::sun::star::frame::XController;
      57             :     using ::com::sun::star::frame::XController2;
      58             :     using ::com::sun::star::frame::XFramesSupplier;
      59             :     using ::com::sun::star::sdb::XOfficeDatabaseDocument;
      60             :     using ::com::sun::star::awt::XTopWindow;
      61             :     using ::com::sun::star::awt::XTopWindowListener;
      62             :     using ::com::sun::star::awt::XWindow2;
      63             :     using ::com::sun::star::lang::DisposedException;
      64             :     using ::com::sun::star::lang::EventObject;
      65             :     using ::com::sun::star::document::XDocumentEventBroadcaster;
      66             :     using ::com::sun::star::awt::XWindow;
      67             : 
      68             :     // FrameWindowActivationListener
      69             :     typedef ::cppu::WeakImplHelper1 <   XTopWindowListener
      70             :                                     >   FrameWindowActivationListener_Base;
      71             :     class FrameWindowActivationListener : public FrameWindowActivationListener_Base
      72             :     {
      73             :     public:
      74             :         FrameWindowActivationListener( ControllerFrame_Data& _rData );
      75             : 
      76             :         void dispose();
      77             : 
      78             :     protected:
      79             :         virtual ~FrameWindowActivationListener();
      80             : 
      81             :         // XTopWindowListener
      82             :         virtual void SAL_CALL windowOpened( const ::com::sun::star::lang::EventObject& e ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
      83             :         virtual void SAL_CALL windowClosing( const ::com::sun::star::lang::EventObject& e ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
      84             :         virtual void SAL_CALL windowClosed( const ::com::sun::star::lang::EventObject& e ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
      85             :         virtual void SAL_CALL windowMinimized( const ::com::sun::star::lang::EventObject& e ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
      86             :         virtual void SAL_CALL windowNormalized( const ::com::sun::star::lang::EventObject& e ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
      87             :         virtual void SAL_CALL windowActivated( const ::com::sun::star::lang::EventObject& e ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
      88             :         virtual void SAL_CALL windowDeactivated( const ::com::sun::star::lang::EventObject& e ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
      89             : 
      90             :         // XEventListener
      91             :         virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
      92             : 
      93             :     private:
      94             :         void impl_checkDisposed_throw() const;
      95             :         void impl_registerOnFrameContainerWindow_nothrow( bool _bRegister );
      96             : 
      97             :     private:
      98             :         ControllerFrame_Data*   m_pData;
      99             :     };
     100             : 
     101             :     // ControllerFrame_Data
     102          16 :     struct ControllerFrame_Data
     103             :     {
     104          20 :         ControllerFrame_Data( IController& _rController )
     105             :             :m_rController( _rController )
     106             :             ,m_xFrame()
     107             :             ,m_xDocEventBroadcaster()
     108             :             ,m_pListener()
     109             :             ,m_bActive( false )
     110          20 :             ,m_bIsTopLevelDocumentWindow( false )
     111             :         {
     112          20 :         }
     113             : 
     114             :         IController&                                        m_rController;
     115             :         Reference< XFrame >                                 m_xFrame;
     116             :         Reference< XDocumentEventBroadcaster >              m_xDocEventBroadcaster;
     117             :         ::rtl::Reference< FrameWindowActivationListener >   m_pListener;
     118             :         bool                                                m_bActive;
     119             :         bool                                                m_bIsTopLevelDocumentWindow;
     120             :     };
     121             : 
     122             :     // helper
     123          48 :     static void lcl_setFrame_nothrow( ControllerFrame_Data& _rData, const Reference< XFrame >& _rxFrame )
     124             :     {
     125             :         // release old listener
     126          48 :         if ( _rData.m_pListener.get() )
     127             :         {
     128          14 :             _rData.m_pListener->dispose();
     129          14 :             _rData.m_pListener = NULL;
     130             :         }
     131             : 
     132             :         // remember new frame
     133          48 :         _rData.m_xFrame = _rxFrame;
     134             : 
     135             :         // create new listener
     136          48 :         if ( _rData.m_xFrame.is() )
     137          18 :             _rData.m_pListener = new FrameWindowActivationListener( _rData );
     138             : 
     139             :         // at this point in time, we can assume the controller also has a model set, if it supports models
     140             :         try
     141             :         {
     142          48 :             Reference< XController > xController( _rData.m_rController.getXController(), UNO_SET_THROW );
     143          96 :             Reference< XModel > xModel( xController->getModel() );
     144          48 :             if ( xModel.is() )
     145          76 :                 _rData.m_xDocEventBroadcaster.set( xModel, UNO_QUERY );
     146             :         }
     147           0 :         catch( const Exception& )
     148             :         {
     149             :             DBG_UNHANDLED_EXCEPTION();
     150             :         }
     151          48 :     }
     152             : 
     153          48 :     static bool lcl_isActive_nothrow( const Reference< XFrame >& _rxFrame )
     154             :     {
     155          48 :         bool bIsActive = false;
     156             :         try
     157             :         {
     158          48 :             if ( _rxFrame.is() )
     159             :             {
     160          18 :                 Reference< XWindow2 > xWindow( _rxFrame->getContainerWindow(), UNO_QUERY_THROW );
     161          18 :                 bIsActive = xWindow->isActive();
     162             :             }
     163             : 
     164             :         }
     165           0 :         catch( const Exception& )
     166             :         {
     167             :             DBG_UNHANDLED_EXCEPTION();
     168             :         }
     169          48 :         return bIsActive;
     170             :     }
     171             : 
     172             :     /** updates various global and local states with a new active component
     173             : 
     174             :         In particular, the following are updated
     175             :             * the global working document (aka Basic's ThisComponent in the application
     176             :             Basic), with our controller's model, or the controller itself if there is no such
     177             :             model.
     178             :     */
     179          14 :     static void lcl_updateActiveComponents_nothrow( const ControllerFrame_Data& _rData )
     180             :     {
     181             :         try
     182             :         {
     183          14 :             Reference< XController > xCompController( _rData.m_rController.getXController() );
     184             :             OSL_ENSURE( xCompController.is(), "lcl_updateActiveComponents_nothrow: can't do anything without a controller!" );
     185          14 :             if ( !xCompController.is() )
     186          14 :                 return;
     187             : 
     188          14 :             if ( _rData.m_bActive && _rData.m_bIsTopLevelDocumentWindow )
     189             :             {
     190             :                 // set the "current component" at the SfxObjectShell
     191          14 :                 Reference< XModel > xModel( xCompController->getModel() );
     192          28 :                 Reference< XInterface > xCurrentComponent;
     193          14 :                 if ( xModel.is() )
     194          14 :                     xCurrentComponent = xModel;
     195             :                 else
     196           0 :                     xCurrentComponent = xCompController;
     197          28 :                 SfxObjectShell::SetCurrentComponent( xCurrentComponent );
     198          14 :             }
     199             :         }
     200           0 :         catch( const Exception& )
     201             :         {
     202             :             DBG_UNHANDLED_EXCEPTION();
     203             :         }
     204             :     }
     205             : 
     206             :     /** broadcasts the OnFocus resp. OnUnfocus event
     207             :     */
     208          14 :     static void lcl_notifyFocusChange_nothrow( ControllerFrame_Data& _rData, bool _bActive )
     209             :     {
     210             :         try
     211             :         {
     212          14 :             if ( _rData.m_xDocEventBroadcaster.is() )
     213             :             {
     214          14 :                 OUString sEventName = _bActive ? OUString("OnFocus") : OUString("OnUnfocus");
     215          28 :                 Reference< XController2 > xController( _rData.m_rController.getXController(), UNO_QUERY_THROW );
     216          28 :                 _rData.m_xDocEventBroadcaster->notifyDocumentEvent( sEventName, xController, Any() );
     217             :             }
     218             :         }
     219           0 :         catch( const Exception& )
     220             :         {
     221             :             DBG_UNHANDLED_EXCEPTION();
     222             :         }
     223          14 :     }
     224             : 
     225          14 :     static void lcl_updateActive_nothrow( ControllerFrame_Data& _rData, bool _bActive )
     226             :     {
     227          14 :         if ( _rData.m_bActive == _bActive )
     228          28 :             return;
     229           0 :         _rData.m_bActive = _bActive;
     230             : 
     231           0 :         lcl_updateActiveComponents_nothrow( _rData );
     232           0 :         lcl_notifyFocusChange_nothrow( _rData, _bActive );
     233             :     }
     234             : 
     235          18 :     FrameWindowActivationListener::FrameWindowActivationListener( ControllerFrame_Data& _rData )
     236          18 :         :m_pData( &_rData )
     237             :     {
     238          18 :         impl_registerOnFrameContainerWindow_nothrow( true );
     239          18 :     }
     240             : 
     241          28 :     FrameWindowActivationListener::~FrameWindowActivationListener()
     242             :     {
     243          28 :     }
     244             : 
     245          14 :     void FrameWindowActivationListener::dispose()
     246             :     {
     247          14 :         impl_registerOnFrameContainerWindow_nothrow( false );
     248          14 :         m_pData = NULL;
     249          14 :     }
     250             : 
     251          32 :     void FrameWindowActivationListener::impl_registerOnFrameContainerWindow_nothrow( bool _bRegister )
     252             :     {
     253             :         OSL_ENSURE( m_pData && m_pData->m_xFrame.is(), "FrameWindowActivationListener::impl_registerOnFrameContainerWindow_nothrow: no frame!" );
     254          32 :         if ( !m_pData || !m_pData->m_xFrame.is() )
     255          32 :             return;
     256             : 
     257             :         try
     258             :         {
     259             :             void ( SAL_CALL XTopWindow::*pListenerAction )( const Reference< XTopWindowListener >& ) =
     260          32 :                 _bRegister ? &XTopWindow::addTopWindowListener : &XTopWindow::removeTopWindowListener;
     261             : 
     262          32 :             const Reference< XWindow > xContainerWindow( m_pData->m_xFrame->getContainerWindow(), UNO_SET_THROW );
     263          32 :             if ( _bRegister )
     264             :             {
     265          18 :                 const vcl::Window* pContainerWindow = VCLUnoHelper::GetWindow( xContainerWindow );
     266          18 :                 ENSURE_OR_THROW( pContainerWindow, "no Window implementation for the frame's container window!" );
     267             : 
     268          18 :                 m_pData->m_bIsTopLevelDocumentWindow = ( pContainerWindow->GetExtendedStyle() & WB_EXT_DOCUMENT ) != 0;
     269             :             }
     270             : 
     271          64 :             const Reference< XTopWindow > xFrameContainer( xContainerWindow, UNO_QUERY );
     272          32 :             if ( xFrameContainer.is() )
     273          64 :                 (xFrameContainer.get()->*pListenerAction)( this );
     274             :         }
     275           0 :         catch( const Exception& )
     276             :         {
     277             :             DBG_UNHANDLED_EXCEPTION();
     278             :         }
     279             :     }
     280             : 
     281           0 :     void FrameWindowActivationListener::impl_checkDisposed_throw() const
     282             :     {
     283           0 :         if ( !m_pData )
     284           0 :             throw DisposedException( OUString(), *const_cast< FrameWindowActivationListener* >( this ) );
     285           0 :     }
     286             : 
     287           0 :     void SAL_CALL FrameWindowActivationListener::windowOpened( const EventObject& /*_rEvent*/ ) throw (RuntimeException, std::exception)
     288             :     {
     289             :         // not interested in
     290           0 :     }
     291             : 
     292           0 :     void SAL_CALL FrameWindowActivationListener::windowClosing( const EventObject& /*_rEvent*/ ) throw (RuntimeException, std::exception)
     293             :     {
     294             :         // not interested in
     295           0 :     }
     296             : 
     297           2 :     void SAL_CALL FrameWindowActivationListener::windowClosed( const EventObject& /*_rEvent*/ ) throw (RuntimeException, std::exception)
     298             :     {
     299             :         // not interested in
     300           2 :     }
     301             : 
     302           0 :     void SAL_CALL FrameWindowActivationListener::windowMinimized( const EventObject& /*_rEvent*/ ) throw (RuntimeException, std::exception)
     303             :     {
     304             :         // not interested in
     305           0 :     }
     306             : 
     307           0 :     void SAL_CALL FrameWindowActivationListener::windowNormalized( const EventObject& /*_rEvent*/ ) throw (RuntimeException, std::exception)
     308             :     {
     309             :         // not interested in
     310           0 :     }
     311             : 
     312           0 :     void SAL_CALL FrameWindowActivationListener::windowActivated( const EventObject& /*_rEvent*/ ) throw (RuntimeException, std::exception)
     313             :     {
     314           0 :         impl_checkDisposed_throw();
     315           0 :         lcl_updateActive_nothrow( *m_pData, true );
     316           0 :     }
     317             : 
     318           0 :     void SAL_CALL FrameWindowActivationListener::windowDeactivated( const EventObject& /*_rEvent*/ ) throw (RuntimeException, std::exception)
     319             :     {
     320           0 :         impl_checkDisposed_throw();
     321           0 :         lcl_updateActive_nothrow( *m_pData, false );
     322           0 :     }
     323             : 
     324           0 :     void SAL_CALL FrameWindowActivationListener::disposing( const EventObject& /*_rEvent*/ ) throw (RuntimeException, std::exception)
     325             :     {
     326           0 :         dispose();
     327           0 :     }
     328             : 
     329             :     // ControllerFrame
     330          20 :     ControllerFrame::ControllerFrame( IController& _rController )
     331          20 :         :m_pData( new ControllerFrame_Data( _rController ) )
     332             :     {
     333          20 :     }
     334             : 
     335          16 :     ControllerFrame::~ControllerFrame()
     336             :     {
     337          16 :     }
     338             : 
     339          48 :     const Reference< XFrame >& ControllerFrame::attachFrame( const Reference< XFrame >& _rxFrame )
     340             :     {
     341             :         // set new frame, including listener handling
     342          48 :         lcl_setFrame_nothrow( *m_pData, _rxFrame );
     343             : 
     344             :         // determine whether we're active
     345          48 :         m_pData->m_bActive = lcl_isActive_nothrow( m_pData->m_xFrame );
     346             : 
     347             :         // update active component
     348          48 :         if ( m_pData->m_bActive )
     349             :         {
     350          14 :             lcl_updateActiveComponents_nothrow( *m_pData );
     351          14 :             lcl_notifyFocusChange_nothrow( *m_pData, true );
     352             :         }
     353             : 
     354          48 :         return m_pData->m_xFrame;
     355             :     }
     356             : 
     357         138 :     const Reference< XFrame >& ControllerFrame::getFrame() const
     358             :     {
     359         138 :         return m_pData->m_xFrame;
     360             :     }
     361             : 
     362          79 :     bool ControllerFrame::isActive() const
     363             :     {
     364          79 :         return m_pData->m_bActive;
     365             :     }
     366             : 
     367          14 :     void ControllerFrame::frameAction( FrameAction _eAction )
     368             :     {
     369          14 :         bool bActive = m_pData->m_bActive;
     370             : 
     371          14 :         switch ( _eAction )
     372             :         {
     373             :             case FrameAction_FRAME_ACTIVATED:
     374             :             case FrameAction_FRAME_UI_ACTIVATED:
     375           0 :                 bActive = true;
     376           0 :                 break;
     377             : 
     378             :             case FrameAction_FRAME_DEACTIVATING:
     379             :             case FrameAction_FRAME_UI_DEACTIVATING:
     380           0 :                 bActive = false;
     381           0 :                 break;
     382             : 
     383             :             default:
     384          14 :                 break;
     385             :         }
     386             : 
     387          14 :         lcl_updateActive_nothrow( *m_pData, bActive );
     388          14 :     }
     389             : 
     390          72 : } // namespace dbaui
     391             : 
     392             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10