Bump version to 4.1-6
[LibreOffice.git] / sfx2 / source / toolbox / imgmgr.cxx
blob58c54ed2af0bb366d2ffcf7b9b6dc50da3e11464
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <stdio.h>
22 #include <boost/unordered_map.hpp>
23 #include <boost/shared_ptr.hpp>
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>
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>
41 #include <comphelper/processfactory.hxx>
43 const sal_uInt32 IMAGELIST_COUNT = 4; // small, small-hi, large, large-hi
45 struct ToolBoxInf_Impl
47 ToolBox* pToolBox;
48 sal_uInt16 nFlags;
51 class SfxImageManager_Impl
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;
61 ImageList* GetImageList( bool bBig );
62 Image GetImage( sal_uInt16 nId, bool bBig );
63 void SetSymbolsSize_Impl( sal_Int16 );
65 DECL_LINK( OptionsChanged_Impl, void* );
66 DECL_LINK( SettingsChanged_Impl, VclWindowEvent* );
69 SfxImageManager_Impl( SfxModule* pModule );
70 ~SfxImageManager_Impl();
73 namespace
75 typedef boost::unordered_map< SfxModule*, boost::shared_ptr<SfxImageManager_Impl> > SfxImageManagerImplMap;
77 class theImageManagerImplMap :
78 public rtl::Static<SfxImageManagerImplMap, theImageManagerImplMap> {};
80 class theGlobalImageManager :
81 public rtl::StaticWithArg<SfxImageManager_Impl, SfxModule*,
82 theGlobalImageManager> {};
85 static SfxImageManager_Impl* GetImageManager( SfxModule* pModule )
87 SolarMutexGuard aGuard;
89 if ( pModule == 0 )
91 return &theGlobalImageManager::get(NULL);
93 else
95 SfxImageManagerImplMap &rImageManager_ImplMap =
96 theImageManagerImplMap::get();
97 SfxImageManager_Impl* pImpl( 0 );
98 SfxImageManagerImplMap::const_iterator pIter = rImageManager_ImplMap.find(pModule);
99 if ( pIter != rImageManager_ImplMap.end() )
100 pImpl = pIter->second.get();
101 else
103 rImageManager_ImplMap[pModule].reset(new SfxImageManager_Impl(pModule));
104 pImpl = rImageManager_ImplMap[pModule].get();
106 return pImpl;
110 // Global image list
111 static ImageList* GetImageList( bool bBig )
113 SolarMutexGuard aGuard;
114 ImageList* rpList = NULL;
116 ResMgr *pResMgr = SfxApplication::GetOrCreate()->GetOffResManager_Impl();
118 ResId aResId( bBig ? ( RID_DEFAULTIMAGELIST_LC ) : ( RID_DEFAULTIMAGELIST_SC ), *pResMgr);
120 aResId.SetRT( RSC_IMAGELIST );
122 DBG_ASSERT( pResMgr->IsAvailable(aResId), "No default ImageList!" );
124 if ( pResMgr->IsAvailable(aResId) )
125 rpList = new ImageList( aResId );
126 else
127 rpList = new ImageList();
129 return rpList;
132 static sal_Int16 impl_convertBools( sal_Bool bLarge )
134 sal_Int16 nIndex( 0 );
135 if ( bLarge )
136 nIndex += 1;
137 return nIndex;
140 //=========================================================================
142 SfxImageManager_Impl::SfxImageManager_Impl( SfxModule* pModule )
143 : m_pModule(pModule)
144 , m_bAppEventListener(false)
146 m_nSymbolsSize = m_aOpt.GetCurrentSymbolsSize();
148 for ( sal_uInt32 i = 0; i < IMAGELIST_COUNT; i++ )
149 m_pImageList[i] = 0;
151 m_aOpt.AddListenerLink( LINK( this, SfxImageManager_Impl, OptionsChanged_Impl ) );
152 Application::AddEventListener( LINK( this, SfxImageManager_Impl, SettingsChanged_Impl ) );
153 m_bAppEventListener = true;
156 //-------------------------------------------------------------------------
158 SfxImageManager_Impl::~SfxImageManager_Impl()
160 m_aOpt.RemoveListenerLink( LINK( this, SfxImageManager_Impl, OptionsChanged_Impl ) );
161 if (m_bAppEventListener)
162 Application::RemoveEventListener( LINK( this, SfxImageManager_Impl, SettingsChanged_Impl ) );
163 for ( sal_uInt32 i = 0; i < m_aToolBoxes.size(); i++ )
164 delete m_aToolBoxes[i];
167 //-------------------------------------------------------------------------
169 ImageList* SfxImageManager_Impl::GetImageList( bool bBig )
171 sal_Int32 nIndex = impl_convertBools( bBig );
172 if ( !m_pImageList[nIndex] )
174 if ( !m_pModule )
175 m_pImageList[nIndex] = ::GetImageList( bBig );
176 else
177 m_pImageList[nIndex] = m_pModule->GetImageList_Impl( bBig );
180 return m_pImageList[nIndex];
183 //-------------------------------------------------------------------------
185 Image SfxImageManager_Impl::GetImage( sal_uInt16 nId, bool bBig )
187 ImageList* pImageList = GetImageList( bBig );
188 if ( pImageList )
189 return pImageList->GetImage( nId );
190 return Image();
193 //-------------------------------------------------------------------------
195 void SfxImageManager_Impl::SetSymbolsSize_Impl( sal_Int16 nNewSymbolsSize )
197 SolarMutexGuard aGuard;
199 if ( nNewSymbolsSize != m_nSymbolsSize )
201 m_nSymbolsSize = nNewSymbolsSize;
202 sal_Bool bLarge( m_nSymbolsSize == SFX_SYMBOLS_SIZE_LARGE );
204 for ( sal_uInt32 n=0; n < m_aToolBoxes.size(); n++ )
206 ToolBoxInf_Impl *pInf = m_aToolBoxes[n];
207 if ( pInf->nFlags & SFX_TOOLBOX_CHANGESYMBOLSET )
209 ToolBox *pBox = pInf->pToolBox;
210 sal_uInt16 nCount = pBox->GetItemCount();
211 for ( sal_uInt16 nPos=0; nPos<nCount; nPos++ )
213 sal_uInt16 nId = pBox->GetItemId( nPos );
214 if ( pBox->GetItemType(nPos) == TOOLBOXITEM_BUTTON )
216 pBox->SetItemImage( nId, GetImage( nId, bLarge ) );
217 SfxStateCache *pCache = SfxViewFrame::Current()->GetBindings().GetStateCache( nId );
218 if ( pCache )
219 pCache->SetCachedState();
223 if ( !pBox->IsFloatingMode() )
225 Size aActSize( pBox->GetSizePixel() );
226 Size aSize( pBox->CalcWindowSizePixel() );
227 if ( pBox->IsHorizontal() )
228 aSize.Width() = aActSize.Width();
229 else
230 aSize.Height() = aActSize.Height();
232 pBox->SetSizePixel( aSize );
239 //-------------------------------------------------------------------------
241 IMPL_LINK_NOARG(SfxImageManager_Impl, OptionsChanged_Impl)
243 SetSymbolsSize_Impl( m_aOpt.GetCurrentSymbolsSize() );
244 return 0L;
247 //-------------------------------------------------------------------------
249 IMPL_LINK( SfxImageManager_Impl, SettingsChanged_Impl, VclWindowEvent*, pEvent)
251 if (pEvent)
253 switch (pEvent->GetId())
255 case VCLEVENT_OBJECT_DYING:
256 if (m_bAppEventListener)
258 Application::RemoveEventListener( LINK( this, SfxImageManager_Impl, SettingsChanged_Impl ) );
259 m_bAppEventListener = false;
261 break;
262 case VCLEVENT_APPLICATION_DATACHANGED:
263 // Check if toolbar button size have changed and we have to use system settings
265 sal_Int16 nSymbolsSize = m_aOpt.GetCurrentSymbolsSize();
266 if (m_nSymbolsSize != nSymbolsSize)
267 SetSymbolsSize_Impl(nSymbolsSize);
269 break;
270 default:
271 break;
274 return 0L;
277 //-------------------------------------------------------------------------
279 //=========================================================================
281 SfxImageManager::SfxImageManager( SfxModule* pModule )
283 pImp = ::GetImageManager( pModule );
286 //-------------------------------------------------------------------------
288 SfxImageManager::~SfxImageManager()
292 //-------------------------------------------------------------------------
294 namespace
296 typedef boost::unordered_map< SfxModule*, boost::shared_ptr<SfxImageManager> > SfxImageManagerMap;
298 class theImageManagerMap :
299 public rtl::Static<SfxImageManagerMap, theImageManagerMap> {};
302 SfxImageManager* SfxImageManager::GetImageManager( SfxModule* pModule )
304 SolarMutexGuard aGuard;
305 SfxImageManager* pSfxImageManager(0);
307 SfxImageManagerMap &rImageManagerMap = theImageManagerMap::get();
309 SfxImageManagerMap::const_iterator pIter = rImageManagerMap.find(pModule);
310 if ( pIter != rImageManagerMap.end() )
311 pSfxImageManager = pIter->second.get();
312 else
314 rImageManagerMap[pModule].reset(new SfxImageManager(pModule));
315 pSfxImageManager = rImageManagerMap[pModule].get();
317 return pSfxImageManager;
320 //-------------------------------------------------------------------------
322 Image SfxImageManager::GetImage( sal_uInt16 nId, bool bBig ) const
324 ImageList* pImageList = pImp->GetImageList( bBig );
325 if ( pImageList && pImageList->HasImageAtPos( nId ) )
326 return pImageList->GetImage( nId );
327 return Image();
330 //-------------------------------------------------------------------------
332 Image SfxImageManager::GetImage( sal_uInt16 nId ) const
334 sal_Bool bLarge = SvtMiscOptions().AreCurrentSymbolsLarge();
335 return GetImage( nId, bLarge );
338 //-------------------------------------------------------------------------
340 Image SfxImageManager::SeekImage( sal_uInt16 nId, bool bBig ) const
342 sal_Bool bGlobal = ( pImp->m_pModule == 0 );
343 ImageList* pImageList = pImp->GetImageList( bBig );
344 if ( pImageList && pImageList->HasImageAtPos( nId ) )
345 return pImageList->GetImage( nId );
346 else if ( !bGlobal )
348 pImageList = ::GetImageManager( 0 )->GetImageList( bBig );
349 if ( pImageList )
350 return pImageList->GetImage( nId );
352 return Image();
355 //-------------------------------------------------------------------------
357 Image SfxImageManager::SeekImage( sal_uInt16 nId ) const
359 sal_Bool bLarge = SvtMiscOptions().AreCurrentSymbolsLarge();
360 return SeekImage( nId, bLarge );
363 //-------------------------------------------------------------------------
365 void SfxImageManager::RegisterToolBox( ToolBox *pBox, sal_uInt16 nFlags )
367 SolarMutexGuard aGuard;
369 ToolBoxInf_Impl* pInf = new ToolBoxInf_Impl;
370 pInf->pToolBox = pBox;
371 pInf->nFlags = nFlags;
372 pImp->m_aToolBoxes.push_back( pInf );
375 //-------------------------------------------------------------------------
377 void SfxImageManager::ReleaseToolBox( ToolBox *pBox )
379 SolarMutexGuard aGuard;
381 for ( sal_uInt32 n=0; n < pImp->m_aToolBoxes.size(); n++ )
383 if ((pImp->m_aToolBoxes[n])->pToolBox == pBox )
385 delete pImp->m_aToolBoxes[n];
386 pImp->m_aToolBoxes.erase( pImp->m_aToolBoxes.begin() + n );
387 return;
392 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */