LCOV - code coverage report
Current view: top level - framework/source/uielement - complextoolbarcontroller.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 42 152 27.6 %
Date: 2015-06-13 12:38:46 Functions: 5 18 27.8 %
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/complextoolbarcontroller.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             : #include <com/sun/star/frame/XControlNotificationListener.hpp>
      31             : 
      32             : #include <comphelper/processfactory.hxx>
      33             : #include <svtools/toolboxcontroller.hxx>
      34             : #include <osl/mutex.hxx>
      35             : #include <vcl/svapp.hxx>
      36             : #include <vcl/mnemonic.hxx>
      37             : #include <vcl/toolbox.hxx>
      38             : #include <vcl/settings.hxx>
      39             : 
      40             : using namespace ::com::sun::star;
      41             : using namespace ::com::sun::star::awt;
      42             : using namespace ::com::sun::star::uno;
      43             : using namespace ::com::sun::star::beans;
      44             : using namespace ::com::sun::star::lang;
      45             : using namespace ::com::sun::star::frame;
      46             : using namespace ::com::sun::star::frame::status;
      47             : using namespace ::com::sun::star::util;
      48             : 
      49             : namespace framework
      50             : {
      51             : 
      52        2758 : ComplexToolbarController::ComplexToolbarController(
      53             :     const Reference< XComponentContext >& rxContext,
      54             :     const Reference< XFrame >&            rFrame,
      55             :     ToolBox*                              pToolbar,
      56             :     sal_uInt16                            nID,
      57             :     const OUString&                       aCommand ) :
      58             :     svt::ToolboxController( rxContext, rFrame, aCommand )
      59             :     ,   m_pToolbar( pToolbar )
      60             :     ,   m_nID( nID )
      61        2758 :     ,   m_bMadeInvisible( false )
      62             : {
      63        2758 :     m_xURLTransformer.set( URLTransformer::create(m_xContext) );
      64        2758 : }
      65             : 
      66        2755 : ComplexToolbarController::~ComplexToolbarController()
      67             : {
      68        2755 : }
      69             : 
      70        2755 : void SAL_CALL ComplexToolbarController::dispose()
      71             : throw ( RuntimeException, std::exception )
      72             : {
      73        2755 :     SolarMutexGuard aSolarMutexGuard;
      74             : 
      75        2755 :     m_pToolbar->SetItemWindow( m_nID, 0 );
      76        2755 :     svt::ToolboxController::dispose();
      77             : 
      78        2755 :     m_xURLTransformer.clear();
      79        2755 :     m_pToolbar.clear();
      80        2755 :     m_nID = 0;
      81        2755 : }
      82             : 
      83           0 : Sequence<PropertyValue> ComplexToolbarController::getExecuteArgs(sal_Int16 KeyModifier) const
      84             : {
      85           0 :     Sequence<PropertyValue> aArgs( 1 );
      86             : 
      87             :     // Add key modifier to argument list
      88           0 :     aArgs[0].Name = "KeyModifier";
      89           0 :     aArgs[0].Value <<= KeyModifier;
      90           0 :     return aArgs;
      91             : }
      92             : 
      93           0 : void SAL_CALL ComplexToolbarController::execute( sal_Int16 KeyModifier )
      94             : throw ( RuntimeException, std::exception )
      95             : {
      96           0 :     Reference< XDispatch >       xDispatch;
      97           0 :     Reference< XURLTransformer > xURLTransformer;
      98           0 :     OUString                     aCommandURL;
      99           0 :     ::com::sun::star::util::URL  aTargetURL;
     100           0 :     Sequence<PropertyValue> aArgs;
     101             : 
     102             :     {
     103           0 :         SolarMutexGuard aSolarMutexGuard;
     104             : 
     105           0 :         if ( m_bDisposed )
     106           0 :             throw DisposedException();
     107             : 
     108           0 :         if ( m_bInitialized &&
     109           0 :              m_xFrame.is() &&
     110           0 :              !m_aCommandURL.isEmpty() )
     111             :         {
     112           0 :             xURLTransformer = m_xURLTransformer;
     113           0 :             xDispatch = getDispatchFromCommand( m_aCommandURL );
     114           0 :             aCommandURL = m_aCommandURL;
     115           0 :             aTargetURL = getInitializedURL();
     116           0 :             aArgs = getExecuteArgs(KeyModifier);
     117           0 :         }
     118             :     }
     119             : 
     120           0 :     if ( xDispatch.is() && !aTargetURL.Complete.isEmpty() )
     121             :     {
     122             :         // Execute dispatch asynchronously
     123           0 :         ExecuteInfo* pExecuteInfo = new ExecuteInfo;
     124           0 :         pExecuteInfo->xDispatch     = xDispatch;
     125           0 :         pExecuteInfo->aTargetURL    = aTargetURL;
     126           0 :         pExecuteInfo->aArgs         = aArgs;
     127           0 :         Application::PostUserEvent( LINK(0, ComplexToolbarController , ExecuteHdl_Impl), pExecuteInfo );
     128           0 :     }
     129           0 : }
     130             : 
     131        2065 : void ComplexToolbarController::statusChanged( const FeatureStateEvent& Event )
     132             : throw ( RuntimeException, std::exception )
     133             : {
     134        2065 :     SolarMutexGuard aSolarMutexGuard;
     135             : 
     136        2065 :     if ( m_bDisposed )
     137        2065 :         return;
     138             : 
     139        2065 :     if ( m_pToolbar )
     140             :     {
     141        2065 :         m_pToolbar->EnableItem( m_nID, Event.IsEnabled );
     142             : 
     143        2065 :         ToolBoxItemBits nItemBits = m_pToolbar->GetItemBits( m_nID );
     144        2065 :         nItemBits &= ~ToolBoxItemBits::CHECKABLE;
     145        2065 :         TriState eTri = TRISTATE_FALSE;
     146             : 
     147             :         bool            bValue;
     148        2065 :         OUString        aStrValue;
     149        4130 :         ItemStatus      aItemState;
     150        2065 :         Visibility      aItemVisibility;
     151        4130 :         ControlCommand  aControlCommand;
     152             : 
     153        2065 :         if ( Event.State >>= bValue )
     154             :         {
     155             :             // Boolean, treat it as checked/unchecked
     156           0 :             if ( m_bMadeInvisible )
     157           0 :                 m_pToolbar->ShowItem( m_nID, true );
     158           0 :             m_pToolbar->CheckItem( m_nID, bValue );
     159           0 :             if ( bValue )
     160           0 :                 eTri = TRISTATE_TRUE;
     161           0 :             nItemBits |= ToolBoxItemBits::CHECKABLE;
     162             :         }
     163        2065 :         else if ( Event.State >>= aStrValue )
     164             :         {
     165           0 :             OUString aText( MnemonicGenerator::EraseAllMnemonicChars( aStrValue ) );
     166           0 :             m_pToolbar->SetItemText( m_nID, aText );
     167           0 :             m_pToolbar->SetQuickHelpText( m_nID, aText );
     168             : 
     169           0 :             if ( m_bMadeInvisible )
     170           0 :                 m_pToolbar->ShowItem( m_nID, true );
     171             :         }
     172        2065 :         else if ( Event.State >>= aItemState )
     173             :         {
     174           0 :             eTri = TRISTATE_INDET;
     175           0 :             nItemBits |= ToolBoxItemBits::CHECKABLE;
     176           0 :             if ( m_bMadeInvisible )
     177           0 :                 m_pToolbar->ShowItem( m_nID, true );
     178             :         }
     179        2065 :         else if ( Event.State >>= aItemVisibility )
     180             :         {
     181           0 :             m_pToolbar->ShowItem( m_nID, aItemVisibility.bVisible );
     182           0 :             m_bMadeInvisible = !aItemVisibility.bVisible;
     183             :         }
     184        2065 :         else if ( Event.State >>= aControlCommand )
     185             :         {
     186           0 :             executeControlCommand( aControlCommand );
     187           0 :             if ( m_bMadeInvisible )
     188           0 :                 m_pToolbar->ShowItem( m_nID, true );
     189             :         }
     190             : 
     191        2065 :         else if ( m_bMadeInvisible )
     192           0 :             m_pToolbar->ShowItem( m_nID, true );
     193             : 
     194        2065 :         m_pToolbar->SetItemState( m_nID, eTri );
     195        4130 :         m_pToolbar->SetItemBits( m_nID, nItemBits );
     196        2065 :     }
     197             : }
     198             : 
     199           0 : IMPL_STATIC_LINK( ComplexToolbarController, ExecuteHdl_Impl, ExecuteInfo*, pExecuteInfo )
     200             : {
     201           0 :    SolarMutexReleaser aReleaser;
     202             :    try
     203             :    {
     204             :        // Asynchronous execution as this can lead to our own destruction!
     205             :        // Framework can recycle our current frame and the layout manager disposes all user interface
     206             :        // elements if a component gets detached from its frame!
     207           0 :        pExecuteInfo->xDispatch->dispatch( pExecuteInfo->aTargetURL, pExecuteInfo->aArgs );
     208             :    }
     209           0 :    catch ( const Exception& )
     210             :    {
     211             :    }
     212             : 
     213           0 :    delete pExecuteInfo;
     214           0 :    return 0;
     215             : }
     216             : 
     217           0 : IMPL_STATIC_LINK( ComplexToolbarController, Notify_Impl, NotifyInfo*, pNotifyInfo )
     218             : {
     219           0 :    SolarMutexReleaser aReleaser;
     220             :    try
     221             :    {
     222             :        // Asynchronous execution: As this can lead to our own destruction!
     223             :        // Framework can recycle our current frame and the layout manager disposes all user interface
     224             :        // elements if a component gets detached from its frame!
     225           0 :        frame::ControlEvent aEvent;
     226           0 :        aEvent.aURL  = pNotifyInfo->aSourceURL;
     227           0 :        aEvent.Event = pNotifyInfo->aEventName;
     228           0 :        aEvent.aInformation = pNotifyInfo->aInfoSeq;
     229           0 :        pNotifyInfo->xNotifyListener->controlEvent( aEvent );
     230             :    }
     231           0 :    catch ( const Exception& )
     232             :    {
     233             :    }
     234             : 
     235           0 :    delete pNotifyInfo;
     236           0 :    return 0;
     237             : }
     238             : 
     239           0 : void ComplexToolbarController::addNotifyInfo(
     240             :     const OUString&                      aEventName,
     241             :     const uno::Reference< frame::XDispatch >& xDispatch,
     242             :     const uno::Sequence< beans::NamedValue >& rInfo )
     243             : {
     244           0 :     uno::Reference< frame::XControlNotificationListener > xControlNotify( xDispatch, uno::UNO_QUERY );
     245             : 
     246           0 :     if ( xControlNotify.is() )
     247             :     {
     248             :         // Execute notification asynchronously
     249           0 :         NotifyInfo* pNotifyInfo = new NotifyInfo;
     250             : 
     251           0 :         pNotifyInfo->aEventName      = aEventName;
     252           0 :         pNotifyInfo->xNotifyListener = xControlNotify;
     253           0 :         pNotifyInfo->aSourceURL      = getInitializedURL();
     254             : 
     255             :         // Add frame as source to the information sequence
     256           0 :         sal_Int32 nCount = rInfo.getLength();
     257           0 :         uno::Sequence< beans::NamedValue > aInfoSeq( rInfo );
     258           0 :         aInfoSeq.realloc( nCount+1 );
     259           0 :         aInfoSeq[nCount].Name  = "Source";
     260           0 :         aInfoSeq[nCount].Value = uno::makeAny( getFrameInterface() );
     261           0 :         pNotifyInfo->aInfoSeq  = aInfoSeq;
     262             : 
     263           0 :         Application::PostUserEvent( LINK(0, ComplexToolbarController, Notify_Impl), pNotifyInfo );
     264           0 :     }
     265           0 : }
     266             : 
     267        2758 : sal_Int32 ComplexToolbarController::getFontSizePixel( const vcl::Window* pWindow )
     268             : {
     269        2758 :     const StyleSettings& rSettings = Application::GetSettings().GetStyleSettings();
     270        2758 :     const vcl::Font&     rFont     = rSettings.GetAppFont();
     271             : 
     272             :     // Calculate height of the application font used by window
     273        2758 :     sal_Int32 nHeight = sal_Int32( rFont.GetHeight() );
     274        2758 :     ::Size aPixelSize = pWindow->LogicToPixel( ::Size( 0, nHeight ), MAP_APPFONT );
     275        2758 :     return aPixelSize.Height();
     276             : }
     277             : 
     278           0 : uno::Reference< frame::XDispatch > ComplexToolbarController::getDispatchFromCommand( const OUString& aCommand ) const
     279             : {
     280           0 :     uno::Reference< frame::XDispatch > xDispatch;
     281             : 
     282           0 :     if ( m_bInitialized && m_xFrame.is() && !aCommand.isEmpty() )
     283             :     {
     284           0 :         URLToDispatchMap::const_iterator pIter = m_aListenerMap.find( aCommand );
     285           0 :         if ( pIter != m_aListenerMap.end() )
     286           0 :             xDispatch = pIter->second;
     287             :     }
     288             : 
     289           0 :     return xDispatch;
     290             : }
     291             : 
     292           0 : const ::com::sun::star::util::URL& ComplexToolbarController::getInitializedURL()
     293             : {
     294           0 :     if ( m_aURL.Complete.isEmpty() )
     295             :     {
     296           0 :         m_aURL.Complete = m_aCommandURL;
     297           0 :         m_xURLTransformer->parseStrict( m_aURL );
     298             :     }
     299           0 :     return m_aURL;
     300             : }
     301             : 
     302           0 : void ComplexToolbarController::notifyFocusGet()
     303             : {
     304             :     // send focus get notification
     305           0 :     uno::Sequence< beans::NamedValue > aInfo;
     306             :     addNotifyInfo( OUString( "FocusSet" ),
     307             :                     getDispatchFromCommand( m_aCommandURL ),
     308           0 :                     aInfo );
     309           0 : }
     310             : 
     311           0 : void ComplexToolbarController::notifyFocusLost()
     312             : {
     313             :     // send focus lost notification
     314           0 :     uno::Sequence< beans::NamedValue > aInfo;
     315             :     addNotifyInfo( OUString( "FocusLost" ),
     316             :                     getDispatchFromCommand( m_aCommandURL ),
     317           0 :                     aInfo );
     318           0 : }
     319             : 
     320           0 : void ComplexToolbarController::notifyTextChanged( const OUString& aText )
     321             : {
     322             :     // send text changed notification
     323           0 :     uno::Sequence< beans::NamedValue > aInfo( 1 );
     324           0 :     aInfo[0].Name  = "Text";
     325           0 :     aInfo[0].Value <<= aText;
     326             :     addNotifyInfo( OUString( "TextChanged" ),
     327             :                    getDispatchFromCommand( m_aCommandURL ),
     328           0 :                    aInfo );
     329           0 : }
     330             : 
     331             : } // namespace
     332             : 
     333             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11