LCOV - code coverage report
Current view: top level - sfx2/source/toolbox - imgmgr.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 83 149 55.7 %
Date: 2014-04-11 Functions: 14 21 66.7 %
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             : 
      21             : #include <stdio.h>
      22             : #include <boost/unordered_map.hpp>
      23             : #include <boost/shared_ptr.hpp>
      24             : 
      25             : #include <sfx2/imgmgr.hxx>
      26             : #include <sfx2/sfx.hrc>
      27             : #include <sfx2/app.hxx>
      28             : #include <sfx2/sfxresid.hxx>
      29             : #include <sfx2/bindings.hxx>
      30             : #include "statcach.hxx"
      31             : #include <sfx2/module.hxx>
      32             : #include <vcl/bitmap.hxx>
      33             : #include <vcl/toolbox.hxx>
      34             : 
      35             : #include <tools/rcid.h>
      36             : #include <tools/link.hxx>
      37             : #include <svtools/miscopt.hxx>
      38             : #include <osl/mutex.hxx>
      39             : #include <rtl/instance.hxx>
      40             : 
      41             : #include <comphelper/processfactory.hxx>
      42             : 
      43             : const sal_uInt32 IMAGELIST_COUNT = 4; // small, small-hi, large, large-hi
      44             : 
      45             : struct ToolBoxInf_Impl
      46             : {
      47             :     ToolBox* pToolBox;
      48             :     sal_uInt16   nFlags;
      49             : };
      50             : 
      51             : class SfxImageManager_Impl
      52             : {
      53             : public:
      54             :     SvtMiscOptions                  m_aOpt;
      55             :     std::vector< ToolBoxInf_Impl* > m_aToolBoxes;
      56             :     sal_Int16                       m_nSymbolsSize;
      57             :     ImageList*                      m_pImageList[IMAGELIST_COUNT];
      58             :     SfxModule*                      m_pModule;
      59             :     bool                            m_bAppEventListener;
      60             : 
      61             :     ImageList*              GetImageList( bool bBig );
      62             :     Image                   GetImage( sal_uInt16 nId, bool bBig );
      63             :     void                    SetSymbolsSize_Impl( sal_Int16 );
      64             : 
      65             :     DECL_LINK( OptionsChanged_Impl, void* );
      66             :     DECL_LINK( SettingsChanged_Impl, VclWindowEvent* );
      67             : 
      68             : 
      69             :     SfxImageManager_Impl( SfxModule* pModule );
      70             :     ~SfxImageManager_Impl();
      71             : };
      72             : 
      73             : namespace
      74             : {
      75             :     typedef boost::unordered_map< SfxModule*, boost::shared_ptr<SfxImageManager_Impl> > SfxImageManagerImplMap;
      76             : 
      77             :     class theImageManagerImplMap :
      78             :         public rtl::Static<SfxImageManagerImplMap, theImageManagerImplMap> {};
      79             : 
      80             :     class theGlobalImageManager :
      81             :         public rtl::StaticWithArg<SfxImageManager_Impl, SfxModule*,
      82             :             theGlobalImageManager> {};
      83             : }
      84             : 
      85          27 : static SfxImageManager_Impl* GetImageManager( SfxModule* pModule )
      86             : {
      87          27 :     SolarMutexGuard aGuard;
      88             : 
      89          27 :     if ( pModule == 0 )
      90             :     {
      91           0 :         return &theGlobalImageManager::get(NULL);
      92             :     }
      93             :     else
      94             :     {
      95             :         SfxImageManagerImplMap &rImageManager_ImplMap =
      96          27 :             theImageManagerImplMap::get();
      97          27 :         SfxImageManager_Impl* pImpl( 0 );
      98          27 :         SfxImageManagerImplMap::const_iterator pIter = rImageManager_ImplMap.find(pModule);
      99          27 :         if ( pIter != rImageManager_ImplMap.end() )
     100           0 :             pImpl = pIter->second.get();
     101             :         else
     102             :         {
     103          27 :             rImageManager_ImplMap[pModule].reset(new SfxImageManager_Impl(pModule));
     104          27 :             pImpl = rImageManager_ImplMap[pModule].get();
     105             :         }
     106          27 :         return pImpl;
     107          27 :     }
     108             : }
     109             : 
     110             : // Global image list
     111           0 : static ImageList* GetImageList( bool bBig )
     112             : {
     113           0 :     SolarMutexGuard aGuard;
     114           0 :     ImageList* rpList = NULL;
     115             : 
     116           0 :     ResMgr *pResMgr = SfxApplication::GetOrCreate()->GetOffResManager_Impl();
     117             : 
     118           0 :     ResId aResId( bBig ? ( RID_DEFAULTIMAGELIST_LC ) : ( RID_DEFAULTIMAGELIST_SC ), *pResMgr);
     119             : 
     120           0 :     aResId.SetRT( RSC_IMAGELIST );
     121             : 
     122             :     DBG_ASSERT( pResMgr->IsAvailable(aResId), "No default ImageList!" );
     123             : 
     124           0 :     if ( pResMgr->IsAvailable(aResId) )
     125           0 :         rpList = new ImageList( aResId );
     126             :     else
     127           0 :         rpList = new ImageList();
     128             : 
     129           0 :     return rpList;
     130             : }
     131             : 
     132         666 : static sal_Int16 impl_convertBools( bool bLarge )
     133             : {
     134         666 :     sal_Int16 nIndex( 0 );
     135         666 :     if ( bLarge  )
     136           0 :         nIndex += 1;
     137         666 :     return nIndex;
     138             : }
     139             : 
     140             : 
     141             : 
     142          27 : SfxImageManager_Impl::SfxImageManager_Impl( SfxModule* pModule )
     143             :     : m_pModule(pModule)
     144          27 :     , m_bAppEventListener(false)
     145             : {
     146          27 :     m_nSymbolsSize = m_aOpt.GetCurrentSymbolsSize();
     147             : 
     148         135 :     for ( sal_uInt32 i = 0; i < IMAGELIST_COUNT; i++ )
     149         108 :         m_pImageList[i] = 0;
     150             : 
     151          27 :     m_aOpt.AddListenerLink( LINK( this, SfxImageManager_Impl, OptionsChanged_Impl ) );
     152          27 :     Application::AddEventListener( LINK( this, SfxImageManager_Impl, SettingsChanged_Impl ) );
     153          27 :     m_bAppEventListener = true;
     154          27 : }
     155             : 
     156             : 
     157             : 
     158          54 : SfxImageManager_Impl::~SfxImageManager_Impl()
     159             : {
     160          27 :     m_aOpt.RemoveListenerLink( LINK( this, SfxImageManager_Impl, OptionsChanged_Impl ) );
     161          27 :     if (m_bAppEventListener)
     162           1 :         Application::RemoveEventListener( LINK( this, SfxImageManager_Impl, SettingsChanged_Impl ) );
     163          31 :     for ( sal_uInt32 i = 0; i < m_aToolBoxes.size(); i++ )
     164           4 :         delete m_aToolBoxes[i];
     165          27 : }
     166             : 
     167             : 
     168             : 
     169         666 : ImageList* SfxImageManager_Impl::GetImageList( bool bBig )
     170             : {
     171         666 :     sal_Int32 nIndex = impl_convertBools( bBig );
     172         666 :     if ( !m_pImageList[nIndex] )
     173             :     {
     174          27 :         if ( !m_pModule )
     175           0 :             m_pImageList[nIndex] = ::GetImageList( bBig );
     176             :         else
     177          27 :             m_pImageList[nIndex] = m_pModule->GetImageList_Impl( bBig );
     178             :     }
     179             : 
     180         666 :     return m_pImageList[nIndex];
     181             : }
     182             : 
     183             : 
     184             : 
     185           0 : Image SfxImageManager_Impl::GetImage( sal_uInt16 nId, bool bBig )
     186             : {
     187           0 :     ImageList* pImageList = GetImageList( bBig );
     188           0 :     if ( pImageList )
     189           0 :         return pImageList->GetImage( nId );
     190           0 :     return Image();
     191             : }
     192             : 
     193             : 
     194             : 
     195           0 : void SfxImageManager_Impl::SetSymbolsSize_Impl( sal_Int16 nNewSymbolsSize )
     196             : {
     197           0 :     SolarMutexGuard aGuard;
     198             : 
     199           0 :     if ( nNewSymbolsSize != m_nSymbolsSize )
     200             :     {
     201           0 :         m_nSymbolsSize = nNewSymbolsSize;
     202           0 :         bool bLarge( m_nSymbolsSize == SFX_SYMBOLS_SIZE_LARGE );
     203             : 
     204           0 :         for ( sal_uInt32 n=0; n < m_aToolBoxes.size(); n++ )
     205             :         {
     206           0 :             ToolBoxInf_Impl *pInf = m_aToolBoxes[n];
     207           0 :             if ( pInf->nFlags & SFX_TOOLBOX_CHANGESYMBOLSET )
     208             :             {
     209           0 :                 ToolBox *pBox       = pInf->pToolBox;
     210           0 :                 sal_uInt16  nCount      = pBox->GetItemCount();
     211           0 :                 for ( sal_uInt16 nPos=0; nPos<nCount; nPos++ )
     212             :                 {
     213           0 :                     sal_uInt16 nId = pBox->GetItemId( nPos );
     214           0 :                     if ( pBox->GetItemType(nPos) == TOOLBOXITEM_BUTTON )
     215             :                     {
     216           0 :                         pBox->SetItemImage( nId, GetImage( nId, bLarge ) );
     217           0 :                         SfxStateCache *pCache = SfxViewFrame::Current()->GetBindings().GetStateCache( nId );
     218           0 :                         if ( pCache )
     219           0 :                             pCache->SetCachedState();
     220             :                     }
     221             :                 }
     222             : 
     223           0 :                 if ( !pBox->IsFloatingMode() )
     224             :                 {
     225           0 :                     Size aActSize( pBox->GetSizePixel() );
     226           0 :                     Size aSize( pBox->CalcWindowSizePixel() );
     227           0 :                     if ( pBox->IsHorizontal() )
     228           0 :                         aSize.Width() = aActSize.Width();
     229             :                     else
     230           0 :                         aSize.Height() = aActSize.Height();
     231             : 
     232           0 :                     pBox->SetSizePixel( aSize );
     233             :                 }
     234             :             }
     235             :         }
     236           0 :     }
     237           0 : }
     238             : 
     239             : 
     240             : 
     241           0 : IMPL_LINK_NOARG(SfxImageManager_Impl, OptionsChanged_Impl)
     242             : {
     243           0 :     SetSymbolsSize_Impl( m_aOpt.GetCurrentSymbolsSize() );
     244           0 :     return 0L;
     245             : }
     246             : 
     247             : 
     248             : 
     249       11834 : IMPL_LINK( SfxImageManager_Impl, SettingsChanged_Impl, VclWindowEvent*, pEvent)
     250             : {
     251        5917 :     if (pEvent)
     252             :     {
     253        5917 :         switch (pEvent->GetId())
     254             :         {
     255             :             case VCLEVENT_OBJECT_DYING:
     256          26 :                 if (m_bAppEventListener)
     257             :                 {
     258          26 :                     Application::RemoveEventListener( LINK( this, SfxImageManager_Impl, SettingsChanged_Impl ) );
     259          26 :                     m_bAppEventListener = false;
     260             :                 }
     261          26 :                 break;
     262             :             case VCLEVENT_APPLICATION_DATACHANGED:
     263             :                 // Check if toolbar button size have changed and we have to use system settings
     264             :                 {
     265           0 :                     sal_Int16 nSymbolsSize = m_aOpt.GetCurrentSymbolsSize();
     266           0 :                     if (m_nSymbolsSize != nSymbolsSize)
     267           0 :                         SetSymbolsSize_Impl(nSymbolsSize);
     268             :                 }
     269           0 :                 break;
     270             :             default:
     271        5891 :                 break;
     272             :         }
     273             :     }
     274        5917 :     return 0L;
     275             : }
     276             : 
     277             : 
     278             : 
     279             : 
     280             : 
     281          27 : SfxImageManager::SfxImageManager( SfxModule* pModule )
     282             : {
     283          27 :     pImp = ::GetImageManager( pModule );
     284          27 : }
     285             : 
     286             : 
     287             : 
     288          27 : SfxImageManager::~SfxImageManager()
     289             : {
     290          27 : }
     291             : 
     292             : 
     293             : 
     294             : namespace
     295             : {
     296             :     typedef boost::unordered_map< SfxModule*, boost::shared_ptr<SfxImageManager> > SfxImageManagerMap;
     297             : 
     298             :     class theImageManagerMap :
     299             :         public rtl::Static<SfxImageManagerMap, theImageManagerMap> {};
     300             : }
     301             : 
     302        1208 : SfxImageManager* SfxImageManager::GetImageManager( SfxModule* pModule )
     303             : {
     304        1208 :     SolarMutexGuard aGuard;
     305        1208 :     SfxImageManager* pSfxImageManager(0);
     306             : 
     307        1208 :     SfxImageManagerMap &rImageManagerMap = theImageManagerMap::get();
     308             : 
     309        1208 :     SfxImageManagerMap::const_iterator pIter = rImageManagerMap.find(pModule);
     310        1208 :     if ( pIter != rImageManagerMap.end() )
     311        1181 :         pSfxImageManager = pIter->second.get();
     312             :     else
     313             :     {
     314          27 :         rImageManagerMap[pModule].reset(new SfxImageManager(pModule));
     315          27 :         pSfxImageManager = rImageManagerMap[pModule].get();
     316             :     }
     317        1208 :     return pSfxImageManager;
     318             : }
     319             : 
     320             : 
     321             : 
     322           0 : Image SfxImageManager::GetImage( sal_uInt16 nId, bool bBig ) const
     323             : {
     324           0 :     ImageList* pImageList = pImp->GetImageList( bBig );
     325           0 :     if ( pImageList && pImageList->HasImageAtPos( nId ) )
     326           0 :         return pImageList->GetImage( nId );
     327           0 :     return Image();
     328             : }
     329             : 
     330             : 
     331             : 
     332           0 : Image SfxImageManager::GetImage( sal_uInt16 nId ) const
     333             : {
     334           0 :     bool bLarge = SvtMiscOptions().AreCurrentSymbolsLarge();
     335           0 :     return GetImage( nId, bLarge );
     336             : }
     337             : 
     338             : 
     339             : 
     340         666 : Image SfxImageManager::SeekImage( sal_uInt16 nId, bool bBig ) const
     341             : {
     342         666 :     bool bGlobal = ( pImp->m_pModule == 0 );
     343         666 :     ImageList* pImageList = pImp->GetImageList( bBig );
     344         666 :     if ( pImageList && pImageList->HasImageAtPos( nId ) )
     345         666 :         return pImageList->GetImage( nId );
     346           0 :     else if ( !bGlobal )
     347             :     {
     348           0 :         pImageList = ::GetImageManager( 0 )->GetImageList( bBig );
     349           0 :         if ( pImageList )
     350           0 :             return pImageList->GetImage( nId );
     351             :     }
     352           0 :     return Image();
     353             : }
     354             : 
     355             : 
     356             : 
     357         666 : Image SfxImageManager::SeekImage( sal_uInt16 nId ) const
     358             : {
     359         666 :     bool bLarge = SvtMiscOptions().AreCurrentSymbolsLarge();
     360         666 :     return SeekImage( nId, bLarge );
     361             : }
     362             : 
     363             : 
     364             : 
     365         222 : void SfxImageManager::RegisterToolBox( ToolBox *pBox, sal_uInt16 nFlags )
     366             : {
     367         222 :     SolarMutexGuard aGuard;
     368             : 
     369         222 :     ToolBoxInf_Impl* pInf = new ToolBoxInf_Impl;
     370         222 :     pInf->pToolBox = pBox;
     371         222 :     pInf->nFlags   = nFlags;
     372         222 :     pImp->m_aToolBoxes.push_back( pInf );
     373         222 : }
     374             : 
     375             : 
     376             : 
     377         218 : void SfxImageManager::ReleaseToolBox( ToolBox *pBox )
     378             : {
     379         218 :     SolarMutexGuard aGuard;
     380             : 
     381         239 :     for ( sal_uInt32 n=0; n < pImp->m_aToolBoxes.size(); n++ )
     382             :     {
     383         239 :         if ((pImp->m_aToolBoxes[n])->pToolBox == pBox )
     384             :         {
     385         218 :             delete pImp->m_aToolBoxes[n];
     386         218 :             pImp->m_aToolBoxes.erase( pImp->m_aToolBoxes.begin() + n );
     387         436 :             return;
     388             :         }
     389           0 :     }
     390             : }
     391             : 
     392             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10