LCOV - code coverage report
Current view: top level - framework/source/uielement - toolbarsmenucontroller.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 271 434 62.4 %
Date: 2014-04-11 Functions: 29 35 82.9 %
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/toolbarsmenucontroller.hxx>
      21             : 
      22             : #include <algorithm>
      23             : 
      24             : #include "services.h"
      25             : #include <classes/resource.hrc>
      26             : #include <classes/fwkresid.hxx>
      27             : #include <framework/imageproducer.hxx>
      28             : #include <framework/sfxhelperfunctions.hxx>
      29             : #include <uiconfiguration/windowstateproperties.hxx>
      30             : 
      31             : #include <com/sun/star/awt/XDevice.hpp>
      32             : #include <com/sun/star/beans/PropertyValue.hpp>
      33             : #include <com/sun/star/awt/MenuItemStyle.hpp>
      34             : #include <com/sun/star/frame/ModuleManager.hpp>
      35             : #include <com/sun/star/frame/XDispatchProvider.hpp>
      36             : #include <com/sun/star/frame/theUICommandDescription.hpp>
      37             : #include <com/sun/star/container/XNameContainer.hpp>
      38             : #include <com/sun/star/beans/XPropertySet.hpp>
      39             : #include <com/sun/star/frame/XLayoutManager.hpp>
      40             : #include <com/sun/star/ui/XUIElementSettings.hpp>
      41             : #include <com/sun/star/ui/theModuleUIConfigurationManagerSupplier.hpp>
      42             : #include <com/sun/star/ui/XUIConfigurationManagerSupplier.hpp>
      43             : #include <com/sun/star/ui/UIElementType.hpp>
      44             : #include <com/sun/star/ui/theWindowStateConfiguration.hpp>
      45             : 
      46             : #include <vcl/menu.hxx>
      47             : #include <vcl/svapp.hxx>
      48             : #include <vcl/i18nhelp.hxx>
      49             : #include <vcl/image.hxx>
      50             : #include <vcl/settings.hxx>
      51             : #include <rtl/ustrbuf.hxx>
      52             : #include <toolkit/helper/vclunohelper.hxx>
      53             : #include <vcl/window.hxx>
      54             : #include <svtools/menuoptions.hxx>
      55             : #include <unotools/cmdoptions.hxx>
      56             : #include <svtools/miscopt.hxx>
      57             : 
      58             : //  Defines
      59             : 
      60             : using namespace ::com::sun::star;
      61             : using namespace ::com::sun::star::uno;
      62             : using namespace ::com::sun::star::lang;
      63             : using namespace ::com::sun::star::frame;
      64             : using namespace ::com::sun::star::beans;
      65             : using namespace ::com::sun::star::util;
      66             : using namespace ::com::sun::star::container;
      67             : using namespace ::com::sun::star::ui;
      68             : 
      69             : static const char CONFIGURE_TOOLBARS_CMD[]      = "ConfigureDialog";
      70             : static const char CONFIGURE_TOOLBARS[]          = ".uno:ConfigureDialog";
      71             : static const char CMD_COLORBAR[]                = ".uno:ColorControl";
      72             : static const char CMD_FORMULABAR[]              = ".uno:InsertFormula";
      73             : static const char CMD_INPUTLINEBAR[]            = ".uno:InputLineVisible";
      74             : static const char CMD_RESTOREVISIBILITY[]       = ".cmd:RestoreVisibility";
      75             : static const char ITEM_DESCRIPTOR_RESOURCEURL[] = "ResourceURL";
      76             : static const char ITEM_DESCRIPTOR_UINAME[]      = "UIName";
      77             : static const char STATIC_PRIVATE_TB_RESOURCE[]  = "private:resource/toolbar/";
      78             : 
      79             : static const char STATIC_CMD_PART[]             = ".uno:AvailableToolbars?Toolbar:string=";
      80             : static const char STATIC_INTERNAL_CMD_PART[]    = ".cmd:";
      81             : 
      82             : namespace framework
      83             : {
      84             : 
      85             : typedef boost::unordered_map< OUString, OUString, OUStringHash, ::std::equal_to< OUString > > ToolbarHashMap;
      86             : 
      87         316 : struct ToolBarEntry
      88             : {
      89             :     OUString           aUIName;
      90             :     OUString           aCommand;
      91             :     bool                bVisible;
      92             :     bool                bContextSensitive;
      93             :     const CollatorWrapper*  pCollatorWrapper;
      94             : };
      95             : 
      96         136 : bool CompareToolBarEntry( const ToolBarEntry& aOne, const ToolBarEntry& aTwo )
      97             : {
      98         136 :     sal_Int32 nComp = aOne.pCollatorWrapper->compareString( aOne.aUIName, aTwo.aUIName );
      99             : 
     100         136 :     if ( nComp < 0 )
     101          82 :         return true;
     102             :     else
     103          54 :         return false;
     104             : }
     105             : 
     106           1 : Reference< XLayoutManager > getLayoutManagerFromFrame( const Reference< XFrame >& rFrame )
     107             : {
     108           1 :     Reference< XPropertySet >   xPropSet( rFrame, UNO_QUERY );
     109           1 :     Reference< XLayoutManager > xLayoutManager;
     110             : 
     111             :     try
     112             :     {
     113           1 :         xPropSet->getPropertyValue("LayoutManager") >>= xLayoutManager;
     114             :     }
     115           0 :     catch ( const UnknownPropertyException& )
     116             :     {
     117             :     }
     118             : 
     119           1 :     return xLayoutManager;
     120             : }
     121             : 
     122          18 : struct ToolBarInfo
     123             : {
     124             :     OUString aToolBarResName;
     125             :     OUString aToolBarUIName;
     126             : };
     127             : 
     128          54 : DEFINE_XSERVICEINFO_MULTISERVICE_2      (   ToolbarsMenuController                  ,
     129             :                                             OWeakObject                             ,
     130             :                                             SERVICENAME_POPUPMENUCONTROLLER         ,
     131             :                                             IMPLEMENTATIONNAME_TOOLBARSMENUCONTROLLER
     132             :                                         )
     133             : 
     134           3 : DEFINE_INIT_SERVICE                     (   ToolbarsMenuController, {} )
     135             : 
     136           3 : ToolbarsMenuController::ToolbarsMenuController( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& xContext ) :
     137             :     svt::PopupMenuControllerBase( xContext ),
     138             :     m_xContext( xContext ),
     139             :     m_aPropUIName( "UIName" ),
     140             :     m_aPropResourceURL( "ResourceURL" ),
     141             :     m_bModuleIdentified( false ),
     142             :     m_bResetActive( false ),
     143           3 :     m_aIntlWrapper( xContext, Application::GetSettings().GetLanguageTag() )
     144             : {
     145           3 : }
     146             : 
     147           6 : ToolbarsMenuController::~ToolbarsMenuController()
     148             : {
     149           6 : }
     150             : 
     151           3 : void ToolbarsMenuController::addCommand(
     152             :     Reference< css::awt::XPopupMenu >& rPopupMenu, const OUString& rCommandURL, const OUString& rLabel )
     153             : {
     154           3 :     sal_uInt16        nItemId    = m_xPopupMenu->getItemCount()+1;
     155             : 
     156           3 :     OUString aLabel;
     157           3 :     if ( rLabel.isEmpty() )
     158           2 :         aLabel = getUINameFromCommand( rCommandURL );
     159             :     else
     160           1 :         aLabel = rLabel;
     161             : 
     162           3 :     rPopupMenu->insertItem( nItemId, aLabel, 0, nItemId );
     163           3 :     rPopupMenu->setCommand( nItemId, rCommandURL );
     164             : 
     165           3 :     bool bInternal = rCommandURL.startsWith( STATIC_INTERNAL_CMD_PART );
     166           3 :     if ( !bInternal )
     167             :     {
     168           2 :         if ( !getDispatchFromCommandURL( rCommandURL ).is() )
     169           0 :             m_xPopupMenu->enableItem( nItemId, sal_False );
     170             :     }
     171             : 
     172           6 :     SolarMutexGuard aSolarMutexGuard;
     173             : 
     174           6 :     Image                aImage;
     175           3 :     const StyleSettings& rSettings = Application::GetSettings().GetStyleSettings();
     176             : 
     177           3 :     if ( rSettings.GetUseImagesInMenus() )
     178           3 :         aImage = GetImageFromURL( m_xFrame, rCommandURL, false );
     179             : 
     180           3 :     VCLXPopupMenu* pPopupMenu = (VCLXPopupMenu *)VCLXPopupMenu::GetImplementation( rPopupMenu );
     181           3 :     if ( pPopupMenu )
     182             :     {
     183           3 :         PopupMenu* pVCLPopupMenu = (PopupMenu *)pPopupMenu->GetMenu();
     184           3 :         if ( !!aImage )
     185           1 :             pVCLPopupMenu->SetItemImage( nItemId, aImage );
     186             :     }
     187             : 
     188           6 :     m_aCommandVector.push_back( rCommandURL );
     189           3 : }
     190             : 
     191           2 : Reference< XDispatch > ToolbarsMenuController::getDispatchFromCommandURL( const OUString& rCommandURL )
     192             : {
     193           2 :     URL                          aTargetURL;
     194           4 :     Reference< XURLTransformer > xURLTransformer;
     195           4 :     Reference< XFrame >          xFrame;
     196             : 
     197             :     {
     198           2 :         SolarMutexGuard aSolarMutexGuard;
     199           2 :         xURLTransformer = m_xURLTransformer;
     200           2 :         xFrame = m_xFrame;
     201             :     }
     202             : 
     203           2 :     aTargetURL.Complete = rCommandURL;
     204           2 :     xURLTransformer->parseStrict( aTargetURL );
     205           4 :     Reference< XDispatchProvider > xDispatchProvider( xFrame, UNO_QUERY );
     206           2 :     if ( xDispatchProvider.is() )
     207           2 :         return xDispatchProvider->queryDispatch( aTargetURL, OUString(), 0 );
     208             :     else
     209           2 :         return Reference< XDispatch >();
     210             : }
     211             : 
     212             : // private function
     213           2 : OUString ToolbarsMenuController::getUINameFromCommand( const OUString& rCommandURL )
     214             : {
     215           2 :     OUString aLabel;
     216             : 
     217           2 :     if ( !m_bModuleIdentified  )
     218             :     {
     219             :         try
     220             :         {
     221           2 :             Reference< XModuleManager2 > xModuleManager = ModuleManager::create( m_xContext );
     222           2 :             m_aModuleIdentifier = xModuleManager->identify( m_xFrame );
     223           4 :             Reference< XNameAccess > xNameAccess = frame::theUICommandDescription::get( m_xContext );
     224           4 :             xNameAccess->getByName( m_aModuleIdentifier ) >>= m_xUICommandDescription;
     225             :         }
     226           0 :         catch ( const Exception& )
     227             :         {
     228             :         }
     229             :     }
     230             : 
     231           2 :     if ( m_xUICommandDescription.is() )
     232             :     {
     233             :         try
     234             :         {
     235           2 :             Sequence< PropertyValue > aPropSeq;
     236           4 :             OUString             aStr;
     237           2 :             if ( m_xUICommandDescription->getByName( rCommandURL ) >>= aPropSeq )
     238             :             {
     239           2 :                 for ( sal_Int32 i = 0; i < aPropSeq.getLength(); i++ )
     240             :                 {
     241           2 :                     if ( aPropSeq[i].Name == "Label" )
     242             :                     {
     243           2 :                         aPropSeq[i].Value >>= aStr;
     244           2 :                         break;
     245             :                     }
     246             :                 }
     247             :             }
     248           4 :             aLabel = aStr;
     249             :         }
     250           0 :         catch ( const Exception& )
     251             :         {
     252             :         }
     253             :     }
     254             : 
     255           2 :     return aLabel;
     256             : }
     257             : 
     258           3 : static void fillHashMap( const Sequence< Sequence< ::com::sun::star::beans::PropertyValue > >& rSeqToolBars,
     259             :                          ToolbarHashMap& rHashMap )
     260             : {
     261          45 :     for ( sal_Int32 i = 0; i < rSeqToolBars.getLength(); i++ )
     262             :     {
     263          42 :         OUString aResourceURL;
     264          84 :         OUString aUIName;
     265          42 :         const PropertyValue* pProperties = rSeqToolBars[i].getConstArray();
     266         126 :         for ( sal_Int32 j = 0; j < rSeqToolBars[i].getLength(); j++ )
     267             :         {
     268          84 :             if ( pProperties[j].Name == ITEM_DESCRIPTOR_RESOURCEURL )
     269          42 :                 pProperties[j].Value >>= aResourceURL;
     270          42 :             else if ( pProperties[j].Name == ITEM_DESCRIPTOR_UINAME )
     271          42 :                 pProperties[j].Value >>= aUIName;
     272             :         }
     273             : 
     274         210 :         if ( !aResourceURL.isEmpty() &&
     275         210 :              rHashMap.find( aResourceURL ) == rHashMap.end() )
     276          40 :             rHashMap.insert( ToolbarHashMap::value_type( aResourceURL, aUIName ));
     277          42 :     }
     278           3 : }
     279             : 
     280             : // private function
     281           1 : Sequence< Sequence< com::sun::star::beans::PropertyValue > > ToolbarsMenuController::getLayoutManagerToolbars( const Reference< ::com::sun::star::frame::XLayoutManager >& rLayoutManager )
     282             : {
     283           1 :     std::vector< ToolBarInfo > aToolBarArray;
     284           2 :     Sequence< Reference< XUIElement > > aUIElements = rLayoutManager->getElements();
     285           6 :     for ( sal_Int32 i = 0; i < aUIElements.getLength(); i++ )
     286             :     {
     287           5 :         Reference< XUIElement > xUIElement( aUIElements[i] );
     288          10 :         Reference< XPropertySet > xPropSet( aUIElements[i], UNO_QUERY );
     289           5 :         if ( xPropSet.is() && xUIElement.is() )
     290             :         {
     291             :             try
     292             :             {
     293           5 :                 OUString   aResName;
     294           5 :                 sal_Int16       nType( -1 );
     295           5 :                 xPropSet->getPropertyValue("Type") >>= nType;
     296           5 :                 xPropSet->getPropertyValue("ResourceURL") >>= aResName;
     297             : 
     298           8 :                 if (( nType == ::com::sun::star::ui::UIElementType::TOOLBAR ) &&
     299           3 :                     !aResName.isEmpty() )
     300             :                 {
     301           3 :                     ToolBarInfo aToolBarInfo;
     302             : 
     303           3 :                     aToolBarInfo.aToolBarResName = aResName;
     304             : 
     305           6 :                     SolarMutexGuard aGuard;
     306           6 :                     Reference< css::awt::XWindow > xWindow( xUIElement->getRealInterface(), UNO_QUERY );
     307           3 :                     Window* pWindow = VCLUnoHelper::GetWindow( xWindow );
     308           3 :                     if ( pWindow )
     309           3 :                         aToolBarInfo.aToolBarUIName = pWindow->GetText();
     310             : 
     311           6 :                     aToolBarArray.push_back( aToolBarInfo );
     312           5 :                 }
     313             :             }
     314           0 :             catch ( const Exception& )
     315             :             {
     316             :             }
     317             :         }
     318           5 :     }
     319             : 
     320           2 :     Sequence< com::sun::star::beans::PropertyValue > aTbSeq( 2 );
     321           1 :     aTbSeq[0].Name = m_aPropUIName;
     322           1 :     aTbSeq[1].Name = m_aPropResourceURL;
     323             : 
     324           1 :     Sequence< Sequence< com::sun::star::beans::PropertyValue > > aSeq( aToolBarArray.size() );
     325           1 :     const sal_uInt32 nCount = aToolBarArray.size();
     326           4 :     for ( sal_uInt32 i = 0; i < nCount; i++ )
     327             :     {
     328           3 :         aTbSeq[0].Value <<= aToolBarArray[i].aToolBarUIName;
     329           3 :         aTbSeq[1].Value <<= aToolBarArray[i].aToolBarResName;
     330           3 :         aSeq[i] = aTbSeq;
     331             :     }
     332             : 
     333           2 :     return aSeq;
     334             : }
     335             : 
     336           0 : bool ToolbarsMenuController::isContextSensitiveToolbarNonVisible()
     337             : {
     338           0 :     return m_bResetActive;
     339             : }
     340             : 
     341           1 : void ToolbarsMenuController::fillPopupMenu( Reference< css::awt::XPopupMenu >& rPopupMenu )
     342             : {
     343           1 :     if( SvtMiscOptions().DisableUICustomization() )
     344           1 :         return;
     345             : 
     346           1 :     SolarMutexGuard aSolarMutexGuard;
     347           1 :     resetPopupMenu( rPopupMenu );
     348             : 
     349           1 :     m_aCommandVector.clear();
     350             : 
     351             :     // Retrieve layout manager for additional information
     352           2 :     OUString aEmptyString;
     353           2 :     Reference< XLayoutManager > xLayoutManager( getLayoutManagerFromFrame( m_xFrame ));
     354             : 
     355           1 :     m_bResetActive = false;
     356           1 :     if ( xLayoutManager.is() )
     357             :     {
     358           1 :         ToolbarHashMap aToolbarHashMap;
     359             : 
     360           1 :         if ( m_xDocCfgMgr.is() )
     361             :         {
     362             :             Sequence< Sequence< com::sun::star::beans::PropertyValue > > aSeqDocToolBars =
     363           1 :                 m_xDocCfgMgr->getUIElementsInfo( UIElementType::TOOLBAR );
     364           1 :             fillHashMap( aSeqDocToolBars, aToolbarHashMap );
     365             :         }
     366             : 
     367           1 :         if ( m_xModuleCfgMgr.is() )
     368             :         {
     369             :             Sequence< Sequence< com::sun::star::beans::PropertyValue > > aSeqToolBars =
     370           1 :                 m_xModuleCfgMgr->getUIElementsInfo( UIElementType::TOOLBAR );
     371           1 :             fillHashMap( aSeqToolBars, aToolbarHashMap );
     372             :         }
     373             : 
     374           2 :         std::vector< ToolBarEntry > aSortedTbs;
     375           2 :         OUString               aStaticCmdPart( STATIC_CMD_PART );
     376             : 
     377           2 :         Sequence< Sequence< com::sun::star::beans::PropertyValue > > aSeqFrameToolBars = getLayoutManagerToolbars( xLayoutManager );
     378           1 :         fillHashMap( aSeqFrameToolBars, aToolbarHashMap );
     379             : 
     380           1 :         ToolbarHashMap::const_iterator pIter = aToolbarHashMap.begin();
     381          42 :         while ( pIter != aToolbarHashMap.end() )
     382             :         {
     383          40 :             OUString aUIName = pIter->second;
     384          40 :             bool      bHideFromMenu( false );
     385          40 :             bool      bContextSensitive( false );
     386          40 :             bool      bVisible( false );
     387          79 :             if ( aUIName.isEmpty() &&
     388          39 :                  m_xPersistentWindowState.is() )
     389             :             {
     390             :                 try
     391             :                 {
     392          39 :                     Sequence< PropertyValue > aWindowState;
     393          78 :                     Any                       a( m_xPersistentWindowState->getByName( pIter->first ));
     394             : 
     395          39 :                     if ( a >>= aWindowState )
     396             :                     {
     397         475 :                         for ( sal_Int32 i = 0; i < aWindowState.getLength(); i++ )
     398             :                         {
     399         436 :                             if ( aWindowState[i].Name == WINDOWSTATE_PROPERTY_UINAME )
     400          39 :                                 aWindowState[i].Value >>= aUIName;
     401         397 :                             else if ( aWindowState[i].Name == WINDOWSTATE_PROPERTY_HIDEFROMENU )
     402          39 :                                 aWindowState[i].Value >>= bHideFromMenu;
     403         358 :                             else if ( aWindowState[i].Name == WINDOWSTATE_PROPERTY_CONTEXT )
     404          39 :                                 aWindowState[i].Value >>= bContextSensitive;
     405         319 :                             else if ( aWindowState[i].Name == WINDOWSTATE_PROPERTY_VISIBLE )
     406          39 :                                 aWindowState[i].Value >>= bVisible;
     407             :                         }
     408          39 :                     }
     409             :                 }
     410           0 :                 catch ( const Exception& )
     411             :                 {
     412             :                 }
     413             : 
     414             :                 // Check if we have to enable/disable "Reset" menu item
     415          39 :                 if ( bContextSensitive && !bVisible )
     416           1 :                     m_bResetActive = true;
     417             : 
     418             :             }
     419             : 
     420          40 :             if ( !aUIName.isEmpty() && !bHideFromMenu )
     421             :             {
     422          24 :                 ToolBarEntry aTbEntry;
     423          24 :                 aTbEntry.aUIName = aUIName;
     424          24 :                 aTbEntry.aCommand = pIter->first;
     425          24 :                 aTbEntry.bVisible = xLayoutManager->isElementVisible( pIter->first );
     426          24 :                 aTbEntry.bContextSensitive = bContextSensitive;
     427          24 :                 aTbEntry.pCollatorWrapper = m_aIntlWrapper.getCaseCollator();
     428          24 :                 aSortedTbs.push_back( aTbEntry );
     429             :             }
     430          40 :             ++pIter;
     431          40 :         }
     432             : 
     433             :         // sort toolbars
     434           1 :         std::sort( aSortedTbs.begin(), aSortedTbs.end(), CompareToolBarEntry );
     435             : 
     436           1 :         sal_Int16 nIndex( 1 );
     437           1 :         const sal_uInt32 nCount = aSortedTbs.size();
     438          25 :         for ( sal_uInt32 i = 0; i < nCount; i++ )
     439             :         {
     440          24 :             sal_uInt16 nItemCount = m_xPopupMenu->getItemCount();
     441          24 :             m_xPopupMenu->insertItem( nIndex, aSortedTbs[i].aUIName, css::awt::MenuItemStyle::CHECKABLE, nItemCount );
     442          24 :             if ( aSortedTbs[i].bVisible )
     443           2 :                 m_xPopupMenu->checkItem( nIndex, sal_True );
     444             : 
     445             :             {
     446          24 :                 SolarMutexGuard aGuard;
     447          24 :                 VCLXPopupMenu* pXPopupMenu = (VCLXPopupMenu *)VCLXMenu::GetImplementation( m_xPopupMenu );
     448          24 :                 PopupMenu*     pVCLPopupMenu = (PopupMenu *)pXPopupMenu->GetMenu();
     449             : 
     450          24 :                 pVCLPopupMenu->SetUserValue( nIndex, sal_uIntPtr( aSortedTbs[i].bContextSensitive ? 1L : 0L ));
     451             :             }
     452             : 
     453             :             // use VCL popup menu pointer to set vital information that are not part of the awt implementation
     454          24 :             OUStringBuffer aStrBuf( aStaticCmdPart );
     455             : 
     456          24 :             sal_Int32 n = aSortedTbs[i].aCommand.lastIndexOf( '/' );
     457          24 :             if (( n > 0 ) && (( n+1 ) < aSortedTbs[i].aCommand.getLength() ))
     458          24 :                 aStrBuf.append( aSortedTbs[i].aCommand.copy( n+1 ));
     459             : 
     460          48 :             OUString aCmd( aStrBuf.makeStringAndClear() );
     461             : 
     462             :             // Store complete uno-command so it can also be dispatched. This is necessary to support
     463             :             // the test tool!
     464          24 :             rPopupMenu->setCommand( nIndex, aCmd );
     465          24 :             ++nIndex;
     466          24 :         }
     467             : 
     468             :         // Create commands for non-toolbars
     469           2 :         if ( m_aModuleIdentifier == "com.sun.star.text.TextDocument" ||
     470           0 :              m_aModuleIdentifier == "com.sun.star.text.WebDocument" ||
     471           0 :              m_aModuleIdentifier == "com.sun.star.text.GlobalDocument" ||
     472           0 :              m_aModuleIdentifier == "com.sun.star.drawing.DrawingDocument" ||
     473           1 :              m_aModuleIdentifier == "com.sun.star.presentation.PresentationDocument" ||
     474           0 :              m_aModuleIdentifier == "com.sun.star.sheet.SpreadsheetDocument" )
     475             :         {
     476           2 :             if ( m_aModuleIdentifier == "com.sun.star.drawing.DrawingDocument" ||
     477           1 :                  m_aModuleIdentifier == "com.sun.star.presentation.PresentationDocument" )
     478           0 :                 addCommand( m_xPopupMenu, OUString( CMD_COLORBAR ), aEmptyString );
     479           1 :             else if ( m_aModuleIdentifier == "com.sun.star.sheet.SpreadsheetDocument" )
     480           0 :                 addCommand( m_xPopupMenu, OUString( CMD_INPUTLINEBAR ), aEmptyString );
     481             :             else
     482           1 :                 addCommand( m_xPopupMenu, OUString( CMD_FORMULABAR ), aEmptyString );
     483             :         }
     484             : 
     485           1 :         bool          bAddCommand( true );
     486           2 :         SvtCommandOptions aCmdOptions;
     487           2 :         OUString     aConfigureToolbar( CONFIGURE_TOOLBARS );
     488             : 
     489           1 :         if ( aCmdOptions.HasEntries( SvtCommandOptions::CMDOPTION_DISABLED ))
     490             :         {
     491           0 :             if ( aCmdOptions.Lookup( SvtCommandOptions::CMDOPTION_DISABLED,
     492           0 :                                      OUString( CONFIGURE_TOOLBARS_CMD )))
     493           0 :                 bAddCommand = false;
     494             :         }
     495             : 
     496           1 :         if ( bAddCommand )
     497             :         {
     498             :             // Create command for configure
     499           1 :             if ( m_xPopupMenu->getItemCount() > 0 )
     500             :             {
     501           1 :                 sal_uInt16        nItemCount = m_xPopupMenu->getItemCount();
     502           1 :                 m_xPopupMenu->insertSeparator( nItemCount+1 );
     503             :             }
     504             : 
     505           1 :             addCommand( m_xPopupMenu, aConfigureToolbar, aEmptyString );
     506             :         }
     507             : 
     508             :         // Add separator if no configure has been added
     509           1 :         if ( !bAddCommand )
     510             :         {
     511             :             // Create command for configure
     512           0 :             if ( m_xPopupMenu->getItemCount() > 0 )
     513             :             {
     514           0 :                 sal_uInt16        nItemCount = m_xPopupMenu->getItemCount();
     515           0 :                 m_xPopupMenu->insertSeparator( nItemCount+1 );
     516             :             }
     517             :         }
     518             : 
     519           2 :         OUString aLabelStr(FWK_RESSTR(STR_RESTORE_TOOLBARS));
     520           2 :         OUString aRestoreCmd( CMD_RESTOREVISIBILITY );
     521           2 :         addCommand( m_xPopupMenu, aRestoreCmd, aLabelStr );
     522           1 :     }
     523             : }
     524             : 
     525             : // XEventListener
     526           1 : void SAL_CALL ToolbarsMenuController::disposing( const EventObject& ) throw ( RuntimeException, std::exception )
     527             : {
     528           1 :     Reference< css::awt::XMenuListener > xHolder(( OWeakObject *)this, UNO_QUERY );
     529             : 
     530           2 :     osl::MutexGuard aLock( m_aMutex );
     531           1 :     m_xFrame.clear();
     532           1 :     m_xDispatch.clear();
     533           1 :     m_xDocCfgMgr.clear();
     534           1 :     m_xModuleCfgMgr.clear();
     535           1 :     m_xContext.clear();
     536             : 
     537           1 :     if ( m_xPopupMenu.is() )
     538           1 :         m_xPopupMenu->removeMenuListener( Reference< css::awt::XMenuListener >(( OWeakObject *)this, UNO_QUERY ));
     539           2 :     m_xPopupMenu.clear();
     540           1 : }
     541             : 
     542             : // XStatusListener
     543           1 : void SAL_CALL ToolbarsMenuController::statusChanged( const FeatureStateEvent& Event ) throw ( RuntimeException, std::exception )
     544             : {
     545           1 :     OUString aFeatureURL( Event.FeatureURL.Complete );
     546             : 
     547             :     // All other status events will be processed here
     548           1 :     bool bSetCheckmark      = false;
     549           1 :     bool bCheckmark         = false;
     550             : 
     551           2 :     osl::ClearableMutexGuard aLock( m_aMutex );
     552           2 :     Reference< css::awt::XPopupMenu > xPopupMenu( m_xPopupMenu );
     553           1 :     aLock.clear();
     554             : 
     555           1 :     if ( xPopupMenu.is() )
     556             :     {
     557           0 :         SolarMutexGuard aGuard;
     558           0 :         VCLXPopupMenu* pXPopupMenu = (VCLXPopupMenu *)VCLXMenu::GetImplementation( xPopupMenu );
     559           0 :         PopupMenu*     pVCLPopupMenu = (PopupMenu *)pXPopupMenu->GetMenu();
     560             : 
     561           0 :         for ( sal_uInt16 i = 0; i < pVCLPopupMenu->GetItemCount(); i++ )
     562             :         {
     563           0 :             sal_uInt16 nId = pVCLPopupMenu->GetItemId( i );
     564           0 :             if ( nId == 0 )
     565           0 :                 continue;
     566             : 
     567           0 :             OUString aCmd = pVCLPopupMenu->GetItemCommand( nId );
     568           0 :             if ( aCmd == aFeatureURL )
     569             :             {
     570             :                 // Enable/disable item
     571           0 :                 pVCLPopupMenu->EnableItem( nId, Event.IsEnabled );
     572             : 
     573             :                 // Checkmark
     574           0 :                 if ( Event.State >>= bCheckmark )
     575           0 :                     bSetCheckmark = true;
     576             : 
     577           0 :                 if ( bSetCheckmark )
     578           0 :                     pVCLPopupMenu->CheckItem( nId, bCheckmark );
     579             :                 else
     580             :                 {
     581           0 :                     OUString aItemText;
     582             : 
     583           0 :                     if ( Event.State >>= aItemText )
     584           0 :                         pVCLPopupMenu->SetItemText( nId, aItemText );
     585             :                 }
     586             :             }
     587           0 :         }
     588           1 :     }
     589           1 : }
     590             : 
     591             : // XMenuListener
     592           0 : void SAL_CALL ToolbarsMenuController::itemSelected( const css::awt::MenuEvent& rEvent ) throw (RuntimeException, std::exception)
     593             : {
     594           0 :     Reference< css::awt::XPopupMenu >   xPopupMenu;
     595           0 :     Reference< XComponentContext >      xContext;
     596           0 :     Reference< XURLTransformer >        xURLTransformer;
     597           0 :     Reference< XFrame >                 xFrame;
     598           0 :     Reference< XNameAccess >            xPersistentWindowState;
     599             : 
     600           0 :     osl::ClearableMutexGuard aLock( m_aMutex );
     601           0 :     xPopupMenu             = m_xPopupMenu;
     602           0 :     xContext               = m_xContext;
     603           0 :     xURLTransformer        = m_xURLTransformer;
     604           0 :     xFrame                 = m_xFrame;
     605           0 :     xPersistentWindowState = m_xPersistentWindowState;
     606           0 :     aLock.clear();
     607             : 
     608           0 :     if ( xPopupMenu.is() )
     609             :     {
     610           0 :         VCLXPopupMenu* pPopupMenu = (VCLXPopupMenu *)VCLXPopupMenu::GetImplementation( xPopupMenu );
     611           0 :         if ( pPopupMenu )
     612             :         {
     613           0 :             SolarMutexGuard aSolarMutexGuard;
     614           0 :             PopupMenu* pVCLPopupMenu = (PopupMenu *)pPopupMenu->GetMenu();
     615             : 
     616           0 :             OUString aCmd( pVCLPopupMenu->GetItemCommand( rEvent.MenuId ));
     617           0 :             if ( aCmd.startsWith( STATIC_INTERNAL_CMD_PART ) )
     618             :             {
     619             :                 // Command to restore the visibility of all context sensitive toolbars
     620           0 :                 Reference< XNameReplace > xNameReplace( xPersistentWindowState, UNO_QUERY );
     621           0 :                 if ( xPersistentWindowState.is() && xNameReplace.is() )
     622             :                 {
     623             :                     try
     624             :                     {
     625           0 :                         Sequence< OUString > aElementNames = xPersistentWindowState->getElementNames();
     626           0 :                         sal_Int32 nCount = aElementNames.getLength();
     627           0 :                         bool      bRefreshToolbars( false );
     628             : 
     629           0 :                         for ( sal_Int32 i = 0; i < nCount; i++ )
     630             :                         {
     631             :                             try
     632             :                             {
     633           0 :                                 OUString aElementName = aElementNames[i];
     634           0 :                                 Sequence< PropertyValue > aWindowState;
     635             : 
     636           0 :                                 if ( xPersistentWindowState->getByName( aElementName ) >>= aWindowState )
     637             :                                 {
     638           0 :                                     bool  bVisible( false );
     639           0 :                                     bool  bContextSensitive( false );
     640           0 :                                     sal_Int32 nVisibleIndex( -1 );
     641           0 :                                     for ( sal_Int32 j = 0; j < aWindowState.getLength(); j++ )
     642             :                                     {
     643           0 :                                         if ( aWindowState[j].Name == WINDOWSTATE_PROPERTY_VISIBLE )
     644             :                                         {
     645           0 :                                             aWindowState[j].Value >>= bVisible;
     646           0 :                                             nVisibleIndex = j;
     647             :                                         }
     648           0 :                                         else if ( aWindowState[j].Name == WINDOWSTATE_PROPERTY_CONTEXT )
     649           0 :                                             aWindowState[j].Value >>= bContextSensitive;
     650             :                                     }
     651             : 
     652           0 :                                     if ( !bVisible && bContextSensitive && nVisibleIndex >= 0 )
     653             :                                     {
     654             :                                         // Default is: Every context sensitive toolbar is visible
     655           0 :                                         aWindowState[nVisibleIndex].Value <<= sal_True;
     656           0 :                                         xNameReplace->replaceByName( aElementName, makeAny( aWindowState ));
     657           0 :                                         bRefreshToolbars = true;
     658             :                                     }
     659           0 :                                 }
     660             :                             }
     661           0 :                             catch ( const NoSuchElementException& )
     662             :                             {
     663             :                             }
     664             :                         }
     665             : 
     666           0 :                         if ( bRefreshToolbars )
     667             :                         {
     668           0 :                             Reference< XLayoutManager > xLayoutManager( getLayoutManagerFromFrame( xFrame ));
     669           0 :                             if ( xLayoutManager.is() )
     670             :                             {
     671           0 :                                 Reference< XPropertySet > xPropSet( xLayoutManager, UNO_QUERY );
     672           0 :                                 if ( xPropSet.is() )
     673             :                                 {
     674             :                                     try
     675             :                                     {
     676           0 :                                         xPropSet->setPropertyValue("RefreshContextToolbarVisibility", makeAny( sal_True ));
     677             :                                     }
     678           0 :                                     catch ( const RuntimeException& )
     679             :                                     {
     680             :                                     }
     681           0 :                                     catch ( const Exception& )
     682             :                                     {
     683             :                                     }
     684           0 :                                 }
     685             :                             }
     686           0 :                             RefreshToolbars( xFrame );
     687           0 :                         }
     688             :                     }
     689           0 :                     catch ( const RuntimeException& )
     690             :                     {
     691           0 :                         throw;
     692             :                     }
     693           0 :                     catch ( const Exception& )
     694             :                     {
     695             :                     }
     696           0 :                 }
     697             :             }
     698           0 :             else if ( aCmd.indexOf( STATIC_CMD_PART ) < 0 )
     699             :             {
     700           0 :                 URL                     aTargetURL;
     701           0 :                 Sequence<PropertyValue> aArgs;
     702             : 
     703           0 :                 aTargetURL.Complete = aCmd;
     704           0 :                 xURLTransformer->parseStrict( aTargetURL );
     705           0 :                 Reference< XDispatchProvider > xDispatchProvider( m_xFrame, UNO_QUERY );
     706           0 :                 if ( xDispatchProvider.is() )
     707             :                 {
     708           0 :                     Reference< XDispatch > xDispatch = xDispatchProvider->queryDispatch(
     709           0 :                                                             aTargetURL, OUString(), 0 );
     710             : 
     711           0 :                     ExecuteInfo* pExecuteInfo = new ExecuteInfo;
     712           0 :                     pExecuteInfo->xDispatch     = xDispatch;
     713           0 :                     pExecuteInfo->aTargetURL    = aTargetURL;
     714           0 :                     pExecuteInfo->aArgs         = aArgs;
     715           0 :                     Application::PostUserEvent( STATIC_LINK(0, ToolbarsMenuController, ExecuteHdl_Impl), pExecuteInfo );
     716           0 :                 }
     717             :             }
     718             :             else
     719             :             {
     720           0 :                 Reference< XLayoutManager > xLayoutManager( getLayoutManagerFromFrame( xFrame ));
     721           0 :                 if ( xLayoutManager.is() )
     722             :                 {
     723             :                     // Extract toolbar name from the combined uno-command.
     724           0 :                     sal_Int32 nIndex = aCmd.indexOf( '=' );
     725           0 :                     if (( nIndex > 0 ) && (( nIndex+1 ) < aCmd.getLength() ))
     726             :                     {
     727           0 :                         OUStringBuffer aBuf( STATIC_PRIVATE_TB_RESOURCE );
     728           0 :                         aBuf.append( aCmd.copy( nIndex+1 ));
     729             : 
     730           0 :                         bool      bShow( !pVCLPopupMenu->IsItemChecked( rEvent.MenuId ));
     731           0 :                         OUString aToolBarResName( aBuf.makeStringAndClear() );
     732           0 :                         if ( bShow )
     733             :                         {
     734           0 :                             xLayoutManager->createElement( aToolBarResName );
     735           0 :                             xLayoutManager->showElement( aToolBarResName );
     736             :                         }
     737             :                         else
     738             :                         {
     739             :                             // closing means:
     740             :                             // hide and destroy element
     741           0 :                             xLayoutManager->hideElement( aToolBarResName );
     742           0 :                             xLayoutManager->destroyElement( aToolBarResName );
     743           0 :                         }
     744             :                     }
     745           0 :                 }
     746           0 :             }
     747             :         }
     748           0 :     }
     749           0 : }
     750             : 
     751           0 : void SAL_CALL ToolbarsMenuController::itemActivated( const css::awt::MenuEvent& ) throw (RuntimeException, std::exception)
     752             : {
     753           0 :     std::vector< OUString >   aCmdVector;
     754           0 :     Reference< XDispatchProvider > xDispatchProvider( m_xFrame, UNO_QUERY );
     755           0 :     Reference< XURLTransformer >   xURLTransformer( m_xURLTransformer );
     756             :     {
     757           0 :         osl::MutexGuard aLock( m_aMutex );
     758           0 :         fillPopupMenu( m_xPopupMenu );
     759           0 :         aCmdVector = m_aCommandVector;
     760             :     }
     761             : 
     762             :     // Update status for all commands inside our toolbars popup menu
     763           0 :     const sal_uInt32 nCount = aCmdVector.size();
     764           0 :     for ( sal_uInt32 i = 0; i < nCount; i++ )
     765             :     {
     766           0 :         bool bInternal = aCmdVector[i].startsWith( STATIC_INTERNAL_CMD_PART );
     767             : 
     768           0 :         if ( !bInternal )
     769             :         {
     770           0 :             URL aTargetURL;
     771           0 :             aTargetURL.Complete = aCmdVector[i];
     772           0 :             xURLTransformer->parseStrict( aTargetURL );
     773           0 :             Reference< XDispatch > xDispatch = xDispatchProvider->queryDispatch( aTargetURL, OUString(), 0 );
     774           0 :             if ( xDispatch.is() )
     775             :             {
     776           0 :                 xDispatch->addStatusListener( (static_cast< XStatusListener* >(this)), aTargetURL );
     777           0 :                 xDispatch->removeStatusListener( (static_cast< XStatusListener* >(this)), aTargetURL );
     778           0 :             }
     779             :         }
     780           0 :         else if ( aCmdVector[i] == CMD_RESTOREVISIBILITY )
     781             :         {
     782             :             // Special code to determine the enable/disable state of this command
     783           0 :             FeatureStateEvent aFeatureStateEvent;
     784           0 :             aFeatureStateEvent.FeatureURL.Complete = aCmdVector[i];
     785           0 :             aFeatureStateEvent.IsEnabled = isContextSensitiveToolbarNonVisible();
     786           0 :             statusChanged( aFeatureStateEvent );
     787             :         }
     788           0 :     }
     789           0 : }
     790             : 
     791             : // XPopupMenuController
     792           2 : void SAL_CALL ToolbarsMenuController::setPopupMenu( const Reference< css::awt::XPopupMenu >& xPopupMenu ) throw ( RuntimeException, std::exception )
     793             : {
     794           2 :     osl::MutexGuard aLock( m_aMutex );
     795             : 
     796           2 :     throwIfDisposed();
     797             : 
     798           2 :     if ( m_xFrame.is() && !m_xPopupMenu.is() )
     799             :     {
     800             :         // Create popup menu on demand
     801           1 :         SolarMutexGuard aSolarMutexGuard;
     802             : 
     803           1 :         m_xPopupMenu = xPopupMenu;
     804           1 :         m_xPopupMenu->addMenuListener( Reference< css::awt::XMenuListener >( (OWeakObject*)this, UNO_QUERY ));
     805           1 :         fillPopupMenu( m_xPopupMenu );
     806           2 :     }
     807           2 : }
     808             : 
     809             : // XInitialization
     810           2 : void SAL_CALL ToolbarsMenuController::initialize( const Sequence< Any >& aArguments ) throw ( Exception, RuntimeException, std::exception )
     811             : {
     812           2 :     osl::MutexGuard aLock( m_aMutex );
     813           2 :     bool bInitalized( m_bInitialized );
     814           2 :     if ( !bInitalized )
     815             :     {
     816           2 :         svt::PopupMenuControllerBase::initialize(aArguments);
     817             : 
     818           2 :         if ( m_bInitialized )
     819             :         {
     820           1 :             Reference< XModuleManager2 > xModuleManager = ModuleManager::create( m_xContext );
     821           2 :             Reference< XNameAccess > xPersistentWindowStateSupplier = ::com::sun::star::ui::theWindowStateConfiguration::get( m_xContext );
     822             : 
     823             :             // Retrieve persistent window state reference for our module
     824           2 :             OUString aModuleIdentifier;
     825             :             try
     826             :             {
     827           1 :                 aModuleIdentifier = xModuleManager->identify( m_xFrame );
     828           1 :                 xPersistentWindowStateSupplier->getByName( aModuleIdentifier ) >>= m_xPersistentWindowState;
     829             : 
     830             :                 Reference< XModuleUIConfigurationManagerSupplier > xModuleCfgSupplier =
     831           1 :                     theModuleUIConfigurationManagerSupplier::get( m_xContext );
     832           1 :                 m_xModuleCfgMgr = xModuleCfgSupplier->getUIConfigurationManager( aModuleIdentifier );
     833             : 
     834           2 :                 Reference< XController > xController = m_xFrame->getController();
     835           2 :                 Reference< XModel >      xModel;
     836           1 :                 if ( xController.is() )
     837           1 :                     xModel = xController->getModel();
     838           1 :                 if ( xModel.is() )
     839             :                 {
     840           1 :                     Reference< XUIConfigurationManagerSupplier > xUIConfigurationManagerSupplier( xModel, UNO_QUERY );
     841           1 :                     if ( xUIConfigurationManagerSupplier.is() )
     842           1 :                         m_xDocCfgMgr = xUIConfigurationManagerSupplier->getUIConfigurationManager();
     843             :                 }
     844           2 :                 m_aModuleIdentifier = aModuleIdentifier;
     845             :             }
     846           0 :             catch ( const Exception& )
     847             :             {
     848           1 :             }
     849             :         }
     850           2 :     }
     851           2 : }
     852             : 
     853           0 : IMPL_STATIC_LINK_NOINSTANCE( ToolbarsMenuController, ExecuteHdl_Impl, ExecuteInfo*, pExecuteInfo )
     854             : {
     855             :     try
     856             :     {
     857             :         // Asynchronous execution as this can lead to our own destruction!
     858             :         // Framework can recycle our current frame and the layout manager disposes all user interface
     859             :         // elements if a component gets detached from its frame!
     860           0 :         if ( pExecuteInfo->xDispatch.is() )
     861             :         {
     862           0 :             pExecuteInfo->xDispatch->dispatch( pExecuteInfo->aTargetURL, pExecuteInfo->aArgs );
     863             :         }
     864             :     }
     865           0 :     catch ( const Exception& )
     866             :     {
     867             :     }
     868             : 
     869           0 :     delete pExecuteInfo;
     870           0 :     return 0;
     871             : }
     872             : 
     873             : }
     874             : 
     875             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10