LCOV - code coverage report
Current view: top level - framework/source/uielement - generictoolbarcontroller.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 108 167 64.7 %
Date: 2014-04-11 Functions: 10 20 50.0 %
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/generictoolbarcontroller.hxx"
      21             : 
      22             : #include <com/sun/star/util/URLTransformer.hpp>
      23             : #include <com/sun/star/util/XURLTransformer.hpp>
      24             : #include <com/sun/star/frame/XDispatchProvider.hpp>
      25             : #include <com/sun/star/beans/PropertyValue.hpp>
      26             : #include <com/sun/star/lang/DisposedException.hpp>
      27             : #include <com/sun/star/frame/status/ItemStatus.hpp>
      28             : #include <com/sun/star/frame/status/ItemState.hpp>
      29             : #include <com/sun/star/frame/status/Visibility.hpp>
      30             : 
      31             : #include <comphelper/processfactory.hxx>
      32             : #include <svtools/toolboxcontroller.hxx>
      33             : #include <osl/mutex.hxx>
      34             : #include <vcl/svapp.hxx>
      35             : #include <vcl/mnemonic.hxx>
      36             : #include <tools/urlobj.hxx>
      37             : #include <classes/resource.hrc>
      38             : #include <classes/fwkresid.hxx>
      39             : #include <framework/menuconfiguration.hxx>
      40             : #include <uielement/menubarmanager.hxx>
      41             : 
      42             : using namespace ::com::sun::star::awt;
      43             : using namespace ::com::sun::star::uno;
      44             : using namespace ::com::sun::star::beans;
      45             : using namespace ::com::sun::star::lang;
      46             : using namespace ::com::sun::star::frame;
      47             : using namespace ::com::sun::star::frame::status;
      48             : using namespace ::com::sun::star::util;
      49             : using namespace ::com::sun::star::container;
      50             : 
      51             : namespace framework
      52             : {
      53             : 
      54       45412 : static bool isEnumCommand( const OUString& rCommand )
      55             : {
      56       45412 :     INetURLObject aURL( rCommand );
      57             : 
      58      166069 :     if (( aURL.GetProtocol() == INET_PROT_UNO ) &&
      59      134911 :         ( aURL.GetURLPath().indexOf( '.' ) != -1))
      60         126 :         return true;
      61             : 
      62       45286 :     return false;
      63             : }
      64             : 
      65       45412 : static OUString getEnumCommand( const OUString& rCommand )
      66             : {
      67       45412 :     INetURLObject aURL( rCommand );
      68             : 
      69       45412 :     OUString   aEnumCommand;
      70       90824 :     OUString   aURLPath = aURL.GetURLPath();
      71       45412 :     sal_Int32  nIndex   = aURLPath.indexOf( '.' );
      72       45412 :     if (( nIndex > 0 ) && ( nIndex < aURLPath.getLength() ))
      73       15705 :         aEnumCommand = aURLPath.copy( nIndex+1 );
      74             : 
      75       90824 :     return aEnumCommand;
      76             : }
      77             : 
      78         126 : static OUString getMasterCommand( const OUString& rCommand )
      79             : {
      80         126 :     OUString aMasterCommand( rCommand );
      81         252 :     INetURLObject aURL( rCommand );
      82         126 :     if ( aURL.GetProtocol() == INET_PROT_UNO )
      83             :     {
      84         126 :         sal_Int32 nIndex = aURL.GetURLPath().indexOf( '.' );
      85         126 :         if ( nIndex )
      86             :         {
      87         126 :             aURL.SetURLPath( aURL.GetURLPath().copy( 0, nIndex ) );
      88         126 :             aMasterCommand = aURL.GetMainURL( INetURLObject::NO_DECODE );
      89             :         }
      90             :     }
      91         252 :     return aMasterCommand;
      92             : }
      93             : 
      94           0 : struct ExecuteInfo
      95             : {
      96             :     ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatch >     xDispatch;
      97             :     ::com::sun::star::util::URL                                                aTargetURL;
      98             :     ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >  aArgs;
      99             : };
     100             : 
     101       45412 : GenericToolbarController::GenericToolbarController( const Reference< XComponentContext >&    rxContext,
     102             :                                                     const Reference< XFrame >&               rFrame,
     103             :                                                     ToolBox*                                 pToolbar,
     104             :                                                     sal_uInt16                               nID,
     105             :                                                     const OUString&                          aCommand ) :
     106             :     svt::ToolboxController( rxContext, rFrame, aCommand )
     107             :     ,   m_pToolbar( pToolbar )
     108             :     ,   m_nID( nID )
     109       45412 :     ,   m_bEnumCommand( isEnumCommand( aCommand ))
     110             :     ,   m_bMadeInvisible( false )
     111       90824 :     ,   m_aEnumCommand( getEnumCommand( aCommand ))
     112             : {
     113       45412 :     if ( m_bEnumCommand )
     114         126 :         addStatusListener( getMasterCommand( aCommand ) );
     115       45412 : }
     116             : 
     117       90806 : GenericToolbarController::~GenericToolbarController()
     118             : {
     119       90806 : }
     120             : 
     121       45403 : void SAL_CALL GenericToolbarController::dispose()
     122             : throw ( RuntimeException, std::exception )
     123             : {
     124       45403 :     SolarMutexGuard aSolarMutexGuard;
     125             : 
     126       45403 :     svt::ToolboxController::dispose();
     127             : 
     128       45403 :     m_pToolbar = 0;
     129       45403 :     m_nID = 0;
     130       45403 : }
     131             : 
     132           2 : void SAL_CALL GenericToolbarController::execute( sal_Int16 KeyModifier )
     133             : throw ( RuntimeException, std::exception )
     134             : {
     135           2 :     Reference< XDispatch >       xDispatch;
     136           4 :     Reference< XURLTransformer > xURLTransformer;
     137           4 :     OUString                     aCommandURL;
     138             : 
     139             :     {
     140           2 :         SolarMutexGuard aSolarMutexGuard;
     141             : 
     142           2 :         if ( m_bDisposed )
     143           0 :             throw DisposedException();
     144             : 
     145           4 :         if ( m_bInitialized &&
     146           4 :              m_xFrame.is() &&
     147           2 :              !m_aCommandURL.isEmpty() )
     148             :         {
     149           2 :             xURLTransformer = URLTransformer::create(m_xContext);
     150             : 
     151           2 :             aCommandURL = m_aCommandURL;
     152           2 :             URLToDispatchMap::iterator pIter = m_aListenerMap.find( m_aCommandURL );
     153           2 :             if ( pIter != m_aListenerMap.end() )
     154           2 :                 xDispatch = pIter->second;
     155           2 :         }
     156             :     }
     157             : 
     158           2 :     if ( xDispatch.is() && xURLTransformer.is() )
     159             :     {
     160           2 :         com::sun::star::util::URL aTargetURL;
     161           4 :         Sequence<PropertyValue>   aArgs( 1 );
     162             : 
     163             :         // Add key modifier to argument list
     164           2 :         aArgs[0].Name  = "KeyModifier";
     165           2 :         aArgs[0].Value <<= KeyModifier;
     166             : 
     167           2 :         aTargetURL.Complete = aCommandURL;
     168           2 :         xURLTransformer->parseStrict( aTargetURL );
     169             : 
     170             :         // Execute dispatch asynchronously
     171           2 :         ExecuteInfo* pExecuteInfo = new ExecuteInfo;
     172           2 :         pExecuteInfo->xDispatch     = xDispatch;
     173           2 :         pExecuteInfo->aTargetURL    = aTargetURL;
     174           2 :         pExecuteInfo->aArgs         = aArgs;
     175           4 :         Application::PostUserEvent( STATIC_LINK(0, GenericToolbarController , ExecuteHdl_Impl), pExecuteInfo );
     176           2 :     }
     177           2 : }
     178             : 
     179       63081 : void GenericToolbarController::statusChanged( const FeatureStateEvent& Event )
     180             : throw ( RuntimeException, std::exception )
     181             : {
     182       63081 :     SolarMutexGuard aSolarMutexGuard;
     183             : 
     184       63081 :     if ( m_bDisposed )
     185       63081 :         return;
     186             : 
     187       63081 :     if ( m_pToolbar )
     188             :     {
     189       63081 :         m_pToolbar->EnableItem( m_nID, Event.IsEnabled );
     190             : 
     191       63081 :         sal_uInt16 nItemBits = m_pToolbar->GetItemBits( m_nID );
     192       63081 :         nItemBits &= ~TIB_CHECKABLE;
     193       63081 :         TriState eTri = TRISTATE_FALSE;
     194             : 
     195             :         bool            bValue;
     196       63081 :         OUString        aStrValue;
     197      126162 :         ItemStatus      aItemState;
     198       63081 :         Visibility      aItemVisibility;
     199             : 
     200       63081 :         if (( Event.State >>= bValue ) && !m_bEnumCommand )
     201             :         {
     202             :             // Boolean, treat it as checked/unchecked
     203       22825 :             if ( m_bMadeInvisible )
     204           0 :                 m_pToolbar->ShowItem( m_nID, true );
     205       22825 :             m_pToolbar->CheckItem( m_nID, bValue );
     206       22825 :             if ( bValue )
     207        3867 :                 eTri = TRISTATE_TRUE;
     208       22825 :             nItemBits |= TIB_CHECKABLE;
     209             :         }
     210       40256 :         else if ( Event.State >>= aStrValue )
     211             :         {
     212        2408 :             if ( m_bEnumCommand )
     213             :             {
     214           0 :                 if ( aStrValue == m_aEnumCommand )
     215           0 :                     bValue = true;
     216             :                 else
     217           0 :                     bValue = false;
     218             : 
     219           0 :                 m_pToolbar->CheckItem( m_nID, bValue );
     220           0 :                 if ( bValue )
     221           0 :                     eTri = TRISTATE_TRUE;
     222           0 :                 nItemBits |= TIB_CHECKABLE;
     223             :             }
     224             :             else
     225             :             {
     226             :                 // Replacement for place holders
     227        2408 :                 if ( aStrValue.matchAsciiL( "($1)", 4 ))
     228             :                 {
     229           0 :                     OUString aTmp(FwkResId(STR_UPDATEDOC));
     230           0 :                     aTmp += " ";
     231           0 :                     aTmp += aStrValue.copy( 4 );
     232           0 :                     aStrValue = aTmp;
     233             :                 }
     234        2408 :                 else if ( aStrValue.matchAsciiL( "($2)", 4 ))
     235             :                 {
     236           0 :                     OUString aTmp(FWK_RESSTR(STR_CLOSEDOC_ANDRETURN));
     237           0 :                     aTmp += aStrValue.copy( 4 );
     238           0 :                     aStrValue = aTmp;
     239             :                 }
     240        2408 :                 else if ( aStrValue.matchAsciiL( "($3)", 4 ))
     241             :                 {
     242           0 :                     OUString aTmp(FWK_RESSTR(STR_SAVECOPYDOC));
     243           0 :                     aTmp += aStrValue.copy( 4 );
     244           0 :                     aStrValue = aTmp;
     245             :                 }
     246        2408 :                 OUString aText( MnemonicGenerator::EraseAllMnemonicChars( aStrValue ) );
     247        2408 :                 m_pToolbar->SetItemText( m_nID, aText );
     248        2408 :                 m_pToolbar->SetQuickHelpText( m_nID, aText );
     249             :             }
     250             : 
     251        2408 :             if ( m_bMadeInvisible )
     252           0 :                 m_pToolbar->ShowItem( m_nID, true );
     253             :         }
     254       37848 :         else if (( Event.State >>= aItemState ) && !m_bEnumCommand )
     255             :         {
     256          10 :             eTri = TRISTATE_INDET;
     257          10 :             nItemBits |= TIB_CHECKABLE;
     258          10 :             if ( m_bMadeInvisible )
     259           0 :                 m_pToolbar->ShowItem( m_nID, true );
     260             :         }
     261       37838 :         else if ( Event.State >>= aItemVisibility )
     262             :         {
     263           0 :             m_pToolbar->ShowItem( m_nID, aItemVisibility.bVisible );
     264           0 :             m_bMadeInvisible = !aItemVisibility.bVisible;
     265             :         }
     266       37838 :         else if ( m_bMadeInvisible )
     267           0 :             m_pToolbar->ShowItem( m_nID, true );
     268             : 
     269       63081 :         m_pToolbar->SetItemState( m_nID, eTri );
     270      126162 :         m_pToolbar->SetItemBits( m_nID, nItemBits );
     271       63081 :     }
     272             : }
     273             : 
     274           2 : IMPL_STATIC_LINK_NOINSTANCE( GenericToolbarController, ExecuteHdl_Impl, ExecuteInfo*, pExecuteInfo )
     275             : {
     276           2 :    const sal_uInt32 nRef = Application::ReleaseSolarMutex();
     277             :    try
     278             :    {
     279             :         // Asynchronous execution as this can lead to our own destruction!
     280             :         // Framework can recycle our current frame and the layout manager disposes all user interface
     281             :         // elements if a component gets detached from its frame!
     282           2 :         pExecuteInfo->xDispatch->dispatch( pExecuteInfo->aTargetURL, pExecuteInfo->aArgs );
     283             :    }
     284           0 :    catch ( const Exception& )
     285             :    {
     286             :    }
     287             : 
     288           2 :    Application::AcquireSolarMutex( nRef );
     289           2 :    delete pExecuteInfo;
     290           2 :    return 0;
     291             : }
     292             : 
     293           0 : MenuToolbarController::MenuToolbarController( const Reference< XComponentContext >& rxContext,
     294             :                                               const Reference< XFrame >& rFrame,
     295             :                                               ToolBox* pToolBar,
     296             :                                               sal_uInt16   nID,
     297             :                                               const OUString& aCommand,
     298             :                                               const OUString& aModuleIdentifier,
     299             :                                               const Reference< XIndexAccess >& xMenuDesc )
     300             :     : GenericToolbarController( rxContext, rFrame, pToolBar, nID, aCommand ),
     301             :       m_xMenuDesc( xMenuDesc ),
     302             :       pMenu( NULL ),
     303           0 :       m_aModuleIdentifier( aModuleIdentifier )
     304             : {
     305           0 : }
     306             : 
     307           0 : MenuToolbarController::~MenuToolbarController()
     308             : {
     309             :     try
     310             :     {
     311           0 :         if ( m_xMenuManager.is() )
     312           0 :             m_xMenuManager->dispose();
     313             :     }
     314           0 :     catch( const Exception& ) {}
     315           0 :     if ( pMenu )
     316             :     {
     317           0 :         delete pMenu;
     318           0 :         pMenu = NULL;
     319             :     }
     320             : 
     321           0 : }
     322             : 
     323             : class Toolbarmenu : public ::PopupMenu
     324             : {
     325             :     public:
     326             :     Toolbarmenu();
     327             :     virtual ~Toolbarmenu();
     328             : };
     329             : 
     330           0 : Toolbarmenu::Toolbarmenu()
     331             : {
     332             :     OSL_TRACE("**** contstructing Toolbarmenu 0x%x", this );
     333           0 : }
     334             : 
     335           0 : Toolbarmenu::~Toolbarmenu()
     336             : {
     337             :     OSL_TRACE("**** destructing Toolbarmenu 0x%x", this );
     338           0 : }
     339             : 
     340           0 : void SAL_CALL MenuToolbarController::click() throw (RuntimeException, std::exception)
     341             : {
     342           0 :     createPopupWindow();
     343           0 : }
     344             : 
     345             : Reference< XWindow > SAL_CALL
     346           0 : MenuToolbarController::createPopupWindow() throw (::com::sun::star::uno::RuntimeException, std::exception)
     347             : {
     348           0 :     if ( !pMenu )
     349             :     {
     350           0 :         Reference< XDispatchProvider > xDispatch;
     351           0 :         Reference< XURLTransformer > xURLTransformer = URLTransformer::create( m_xContext );
     352           0 :         pMenu = new Toolbarmenu();
     353           0 :         m_xMenuManager.set( new MenuBarManager( m_xContext, m_xFrame, xURLTransformer, xDispatch, m_aModuleIdentifier, pMenu, true, true ) );
     354           0 :         if ( m_xMenuManager.is() )
     355             :         {
     356           0 :             MenuBarManager* pMgr = dynamic_cast< MenuBarManager* >( m_xMenuManager.get() );
     357           0 :             pMgr->SetItemContainer( m_xMenuDesc );
     358           0 :         }
     359             :     }
     360             : 
     361           0 :     if ( !pMenu || !m_pToolbar )
     362           0 :         return NULL;
     363             : 
     364             :     OSL_ENSURE ( pMenu->GetItemCount(), "Empty PopupMenu!" );
     365             : 
     366           0 :     ::Rectangle aRect( m_pToolbar->GetItemRect( m_nID ) );
     367           0 :     pMenu->Execute( m_pToolbar, aRect, POPUPMENU_EXECUTE_DOWN );
     368             : 
     369           0 :     return NULL;
     370             : }
     371             : } // namespace
     372             : 
     373             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10