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

Generated by: LCOV version 1.11