LCOV - code coverage report
Current view: top level - libreoffice/framework/source/uielement - menubarwrapper.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 52 129 40.3 %
Date: 2012-12-27 Functions: 8 19 42.1 %
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 <uielement/menubarwrapper.hxx>
      21             : #include <threadhelp/resetableguard.hxx>
      22             : #include <framework/actiontriggerhelper.hxx>
      23             : #include <services.h>
      24             : 
      25             : #include <com/sun/star/lang/XServiceInfo.hpp>
      26             : #include <com/sun/star/beans/XPropertySet.hpp>
      27             : #include <com/sun/star/awt/XSystemDependentMenuPeer.hpp>
      28             : #include <com/sun/star/awt/XMenuBar.hpp>
      29             : #include <com/sun/star/container/XIndexContainer.hpp>
      30             : #include <com/sun/star/container/XNameAccess.hpp>
      31             : #include <com/sun/star/ui/UIElementType.hpp>
      32             : #include <com/sun/star/frame/ModuleManager.hpp>
      33             : #include <com/sun/star/util/URLTransformer.hpp>
      34             : 
      35             : #include <comphelper/processfactory.hxx>
      36             : #include <tools/solar.h>
      37             : #include <vcl/svapp.hxx>
      38             : #include <rtl/logfile.hxx>
      39             : 
      40             : using namespace com::sun::star;
      41             : using namespace com::sun::star::uno;
      42             : using namespace com::sun::star::beans;
      43             : using namespace com::sun::star::frame;
      44             : using namespace com::sun::star::lang;
      45             : using namespace com::sun::star::container;
      46             : using namespace com::sun::star::awt;
      47             : using namespace com::sun::star::util;
      48             : using namespace ::com::sun::star::ui;
      49             : 
      50             : namespace framework
      51             : {
      52             : 
      53             : //*****************************************************************************************************************
      54             : //  XInterface, XTypeProvider
      55             : //*****************************************************************************************************************
      56        3870 : DEFINE_XINTERFACE_11    (   MenuBarWrapper                                                    ,
      57             :                             UIConfigElementWrapperBase                                        ,
      58             :                             DIRECT_INTERFACE( ::com::sun::star::lang::XTypeProvider          ),
      59             :                             DIRECT_INTERFACE( ::com::sun::star::ui::XUIElement               ),
      60             :                             DIRECT_INTERFACE( ::com::sun::star::ui::XUIElementSettings       ),
      61             :                             DIRECT_INTERFACE( ::com::sun::star::beans::XMultiPropertySet     ),
      62             :                             DIRECT_INTERFACE( ::com::sun::star::beans::XFastPropertySet      ),
      63             :                             DIRECT_INTERFACE( ::com::sun::star::beans::XPropertySet          ),
      64             :                             DIRECT_INTERFACE( ::com::sun::star::lang::XInitialization        ),
      65             :                             DIRECT_INTERFACE( ::com::sun::star::lang::XComponent             ),
      66             :                             DIRECT_INTERFACE( ::com::sun::star::util::XUpdatable             ),
      67             :                             DIRECT_INTERFACE( ::com::sun::star::ui::XUIConfigurationListener ),
      68             :                             DERIVED_INTERFACE( ::com::sun::star::container::XNameAccess, ::com::sun::star::container::XElementAccess )
      69             :                         )
      70             : 
      71           0 : DEFINE_XTYPEPROVIDER_11 (   MenuBarWrapper                                  ,
      72             :                             ::com::sun::star::lang::XTypeProvider           ,
      73             :                             ::com::sun::star::ui::XUIElement                ,
      74             :                             ::com::sun::star::ui::XUIElementSettings        ,
      75             :                             ::com::sun::star::beans::XMultiPropertySet      ,
      76             :                             ::com::sun::star::beans::XFastPropertySet       ,
      77             :                             ::com::sun::star::beans::XPropertySet           ,
      78             :                             ::com::sun::star::lang::XInitialization         ,
      79             :                             ::com::sun::star::lang::XComponent              ,
      80             :                             ::com::sun::star::util::XUpdatable              ,
      81             :                             ::com::sun::star::ui::XUIConfigurationListener  ,
      82             :                             ::com::sun::star::container::XNameAccess
      83             :                         )
      84             : 
      85         236 : MenuBarWrapper::MenuBarWrapper(
      86             :     const com::sun::star::uno::Reference< com::sun::star::lang::XMultiServiceFactory >& xServiceManager
      87             :     )
      88             : :    UIConfigElementWrapperBase( UIElementType::MENUBAR,xServiceManager ),
      89         236 :      m_bRefreshPopupControllerCache( sal_True )
      90             : {
      91         236 : }
      92             : 
      93         126 : MenuBarWrapper::~MenuBarWrapper()
      94             : {
      95         126 : }
      96             : 
      97          63 : void SAL_CALL MenuBarWrapper::dispose() throw (::com::sun::star::uno::RuntimeException)
      98             : {
      99          63 :     Reference< XComponent > xThis( static_cast< OWeakObject* >(this), UNO_QUERY );
     100             : 
     101          63 :     com::sun::star::lang::EventObject aEvent( xThis );
     102          63 :     m_aListenerContainer.disposeAndClear( aEvent );
     103             : 
     104          63 :     ResetableGuard aLock( m_aLock );
     105             : 
     106          63 :     m_xMenuBarManager->dispose();
     107          63 :     m_xMenuBarManager.clear();
     108          63 :     m_xConfigSource.clear();
     109          63 :     m_xConfigData.clear();
     110             : 
     111          63 :     m_xMenuBar.clear();
     112          63 :     m_bDisposed = sal_True;
     113          63 : }
     114             : 
     115             : // XInitialization
     116         236 : void SAL_CALL MenuBarWrapper::initialize( const Sequence< Any >& aArguments ) throw ( Exception, RuntimeException )
     117             : {
     118             :     RTL_LOGFILE_CONTEXT( aLog, "framework (cd100003) ::MenuBarWrapper::initialize" );
     119             : 
     120         236 :     ResetableGuard aLock( m_aLock );
     121             : 
     122         236 :     if ( m_bDisposed )
     123           0 :         throw DisposedException();
     124             : 
     125         236 :     if ( !m_bInitialized )
     126             :     {
     127         236 :         rtl::OUString aModuleIdentifier;
     128         236 :         UIConfigElementWrapperBase::initialize( aArguments );
     129             : 
     130         236 :         Reference< XFrame > xFrame( m_xWeakFrame );
     131         236 :         if ( xFrame.is() && m_xConfigSource.is() )
     132             :         {
     133             :             // Create VCL menubar which will be filled with settings data
     134         236 :             MenuBar*        pVCLMenuBar = 0;
     135         236 :             VCLXMenuBar*    pAwtMenuBar = 0;
     136             :             {
     137         236 :                 SolarMutexGuard aSolarMutexGuard;
     138         236 :                 pVCLMenuBar = new MenuBar();
     139             :             }
     140             : 
     141         236 :             Reference< XModuleManager2 > xModuleManager = ModuleManager::create( comphelper::getComponentContext(m_xServiceFactory) );
     142             : 
     143             :             try
     144             :             {
     145         236 :                 aModuleIdentifier = xModuleManager->identify( xFrame );
     146             :             }
     147           0 :             catch( const Exception& )
     148             :             {
     149             :             }
     150             : 
     151         236 :             Reference< XURLTransformer > xTrans;
     152             :             try
     153             :             {
     154         236 :                 xTrans.set( URLTransformer::create(::comphelper::getComponentContext(m_xServiceFactory)) );
     155         236 :                 m_xConfigData = m_xConfigSource->getSettings( m_aResourceURL, sal_False );
     156           0 :                 if ( m_xConfigData.is() )
     157             :                 {
     158             :                     // Fill menubar with container contents
     159           0 :                     sal_uInt16 nId = 1;
     160           0 :                     MenuBarManager::FillMenuWithConfiguration( nId, pVCLMenuBar, aModuleIdentifier, m_xConfigData, xTrans );
     161             :                 }
     162             :             }
     163         236 :             catch ( const NoSuchElementException& )
     164             :             {
     165             :             }
     166             : 
     167         236 :             sal_Bool bMenuOnly( sal_False );
     168        1416 :             for ( sal_Int32 n = 0; n < aArguments.getLength(); n++ )
     169             :             {
     170        1180 :                 PropertyValue aPropValue;
     171        1180 :                 if ( aArguments[n] >>= aPropValue )
     172             :                 {
     173        1180 :                     if ( aPropValue.Name == "MenuOnly" )
     174         236 :                         aPropValue.Value >>= bMenuOnly;
     175             :                 }
     176        1180 :             }
     177             : 
     178         236 :             if ( !bMenuOnly )
     179             :             {
     180             :                 // Initialize menubar manager with our vcl menu bar. There are some situations where we only want to get the menu without any
     181             :                 // interaction which is done by the menu bar manager. This must be requested by a special property called "MenuOnly". Be careful
     182             :                 // a menu bar created with this property is not fully supported. It must be attached to a real menu bar manager to have full
     183             :                 // support. This feature is currently used for "Inplace editing"!
     184         236 :                 Reference< XDispatchProvider > xDispatchProvider;
     185             : 
     186             :                 MenuBarManager* pMenuBarManager = new MenuBarManager( m_xServiceFactory,
     187             :                                                                       xFrame,
     188             :                                                                       xTrans,
     189             :                                                                       xDispatchProvider,
     190             :                                                                       aModuleIdentifier,
     191             :                                                                       pVCLMenuBar,
     192             :                                                                       sal_False,
     193         236 :                                                                       sal_True );
     194             : 
     195         236 :                 m_xMenuBarManager = Reference< XComponent >( static_cast< OWeakObject *>( pMenuBarManager ), UNO_QUERY );
     196             :             }
     197             : 
     198             :             // Initialize toolkit menu bar implementation to have awt::XMenuBar for data exchange.
     199             :             // Don't use this toolkit menu bar or one of its functions. It is only used as a data container!
     200         236 :             pAwtMenuBar = new VCLXMenuBar( pVCLMenuBar );
     201         236 :             m_xMenuBar = Reference< XMenuBar >( static_cast< OWeakObject *>( pAwtMenuBar ), UNO_QUERY );
     202         236 :         }
     203         236 :     }
     204         236 : }
     205             : 
     206             : // XUIElementSettings
     207           0 : void SAL_CALL MenuBarWrapper::updateSettings() throw ( RuntimeException )
     208             : {
     209           0 :     ResetableGuard aLock( m_aLock );
     210             : 
     211           0 :     if ( m_bDisposed )
     212           0 :         throw DisposedException();
     213             : 
     214           0 :     if ( m_xMenuBarManager.is() )
     215             :     {
     216           0 :         if ( m_xConfigSource.is() && m_bPersistent )
     217             :         {
     218             :             try
     219             :             {
     220           0 :                 MenuBarManager* pMenuBarManager = static_cast< MenuBarManager *>( m_xMenuBarManager.get() );
     221             : 
     222           0 :                 m_xConfigData = m_xConfigSource->getSettings( m_aResourceURL, sal_False );
     223           0 :                 if ( m_xConfigData.is() )
     224           0 :                     pMenuBarManager->SetItemContainer( m_xConfigData );
     225             :             }
     226           0 :             catch ( const NoSuchElementException& )
     227             :             {
     228             :             }
     229             :         }
     230           0 :         else if ( !m_bPersistent )
     231             :         {
     232             :             // Transient menubar: do nothing
     233             :         }
     234           0 :     }
     235           0 : }
     236           0 : void MenuBarWrapper::impl_fillNewData()
     237             : {
     238             :     // Transient menubar => Fill menubar with new data
     239           0 :     MenuBarManager* pMenuBarManager = static_cast< MenuBarManager *>( m_xMenuBarManager.get() );
     240             : 
     241           0 :     if ( pMenuBarManager )
     242           0 :         pMenuBarManager->SetItemContainer( m_xConfigData );
     243           0 : }
     244             : 
     245             : 
     246           0 : void MenuBarWrapper::fillPopupControllerCache()
     247             : {
     248           0 :     if ( m_bRefreshPopupControllerCache )
     249             :     {
     250           0 :         MenuBarManager* pMenuBarManager = static_cast< MenuBarManager *>( m_xMenuBarManager.get() );
     251           0 :         if ( pMenuBarManager )
     252           0 :             pMenuBarManager->GetPopupController( m_aPopupControllerCache );
     253           0 :         if ( !m_aPopupControllerCache.empty() )
     254           0 :             m_bRefreshPopupControllerCache = sal_False;
     255             :     }
     256           0 : }
     257             : 
     258             : // XElementAccess
     259           0 : Type SAL_CALL MenuBarWrapper::getElementType()
     260             : throw (::com::sun::star::uno::RuntimeException)
     261             : {
     262           0 :     return ::getCppuType(( Reference< XDispatchProvider >*)0);
     263             : }
     264             : 
     265           0 : ::sal_Bool SAL_CALL MenuBarWrapper::hasElements()
     266             : throw (::com::sun::star::uno::RuntimeException)
     267             : {
     268           0 :     ResetableGuard aLock( m_aLock );
     269             : 
     270           0 :     if ( m_bDisposed )
     271           0 :         throw DisposedException();
     272             : 
     273           0 :     fillPopupControllerCache();
     274           0 :     return ( !m_aPopupControllerCache.empty() );
     275             : }
     276             : 
     277             : // XNameAccess
     278           0 : Any SAL_CALL MenuBarWrapper::getByName(
     279             :     const ::rtl::OUString& aName )
     280             : throw ( container::NoSuchElementException,
     281             :         lang::WrappedTargetException,
     282             :         uno::RuntimeException)
     283             : {
     284           0 :     ResetableGuard aLock( m_aLock );
     285             : 
     286           0 :     if ( m_bDisposed )
     287           0 :         throw DisposedException();
     288             : 
     289           0 :     fillPopupControllerCache();
     290             : 
     291           0 :     PopupControllerCache::const_iterator pIter = m_aPopupControllerCache.find( aName );
     292           0 :     if ( pIter != m_aPopupControllerCache.end() )
     293             :     {
     294           0 :         uno::Reference< frame::XDispatchProvider > xDispatchProvider;
     295           0 :         xDispatchProvider = pIter->second.m_xDispatchProvider;
     296           0 :         return uno::makeAny( xDispatchProvider );
     297             :     }
     298             :     else
     299           0 :         throw container::NoSuchElementException();
     300             : }
     301             : 
     302           0 : Sequence< ::rtl::OUString > SAL_CALL MenuBarWrapper::getElementNames()
     303             : throw (::com::sun::star::uno::RuntimeException)
     304             : {
     305           0 :     ResetableGuard aLock( m_aLock );
     306             : 
     307           0 :     if ( m_bDisposed )
     308           0 :         throw DisposedException();
     309             : 
     310           0 :     fillPopupControllerCache();
     311             : 
     312           0 :     Sequence< rtl::OUString > aSeq( m_aPopupControllerCache.size() );
     313             : 
     314           0 :     sal_Int32 i( 0 );
     315           0 :     PopupControllerCache::const_iterator pIter = m_aPopupControllerCache.begin();
     316           0 :     while ( pIter != m_aPopupControllerCache.end() )
     317             :     {
     318           0 :         aSeq[i++] = pIter->first;
     319           0 :         ++pIter;
     320             :     }
     321             : 
     322           0 :     return aSeq;
     323             : }
     324             : 
     325           0 : ::sal_Bool SAL_CALL MenuBarWrapper::hasByName(
     326             :     const ::rtl::OUString& aName )
     327             : throw (::com::sun::star::uno::RuntimeException)
     328             : {
     329           0 :     ResetableGuard aLock( m_aLock );
     330             : 
     331           0 :     if ( m_bDisposed )
     332           0 :         throw DisposedException();
     333             : 
     334           0 :     fillPopupControllerCache();
     335             : 
     336           0 :     PopupControllerCache::const_iterator pIter = m_aPopupControllerCache.find( aName );
     337           0 :     if ( pIter != m_aPopupControllerCache.end() )
     338           0 :         return sal_True;
     339             :     else
     340           0 :         return sal_False;
     341             : }
     342             : 
     343             : // XUIElement
     344           0 : Reference< XInterface > SAL_CALL MenuBarWrapper::getRealInterface() throw ( RuntimeException )
     345             : {
     346           0 :     if ( m_bDisposed )
     347           0 :         throw DisposedException();
     348             : 
     349           0 :     return Reference< XInterface >( m_xMenuBarManager, UNO_QUERY );
     350             : }
     351             : 
     352             : } // namespace framework
     353             : 
     354             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10