merge the formfield patch from ooo-build
[ooovba.git] / sfx2 / source / toolbox / imgmgr.cxx
blobf69afa02fc8eafc0666f830de6dfc08ac64e54c3
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: imgmgr.cxx,v $
10 * $Revision: 1.32 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sfx2.hxx"
34 #include <stdio.h>
35 #include <hash_map>
37 #include "imgmgr.hxx"
38 #include <sfx2/sfx.hrc>
39 #include <sfx2/app.hxx>
40 #include "sfxresid.hxx"
41 #include <sfx2/bindings.hxx>
42 #include "statcach.hxx"
43 #include <sfx2/module.hxx>
44 #include <vcl/bitmap.hxx>
45 #include <vcl/toolbox.hxx>
47 #include <tools/rcid.h>
48 #include <tools/link.hxx>
49 #include <svtools/miscopt.hxx>
50 #include <vos/mutex.hxx>
52 #ifndef GCC
53 #endif
55 #ifndef _UNOTOOLS_PROCESSFACTORY_HXX
56 #include <comphelper/processfactory.hxx>
57 #endif
59 const sal_uInt32 IMAGELIST_COUNT = 4; // small, small-hi, large, large-hi
61 struct ToolBoxInf_Impl
63 ToolBox* pToolBox;
64 USHORT nFlags;
67 class SfxImageManager_Impl
69 public:
70 sal_Int16 m_nSymbolsSize;
71 SvtMiscOptions m_aOpt;
72 std::vector< ToolBoxInf_Impl* > m_aToolBoxes;
73 ImageList* m_pImageList[IMAGELIST_COUNT];
74 SfxModule* m_pModule;
76 ImageList* GetImageList( BOOL bBig, BOOL bHiContrast );
77 Image GetImage( USHORT nId, BOOL bBig, BOOL bHiContrast );
78 void SetSymbolsSize_Impl( sal_Int16 );
80 DECL_LINK( OptionsChanged_Impl, void* );
81 DECL_LINK( SettingsChanged_Impl, void* );
84 SfxImageManager_Impl( SfxModule* pModule );
85 ~SfxImageManager_Impl();
88 typedef std::hash_map< sal_Int64, sal_Int64 > SfxImageManagerMap;
90 // global image lists
91 static SfxImageManager_Impl* pGlobalImageManager = 0;
92 static SfxImageManagerMap m_ImageManager_ImplMap;
93 static SfxImageManagerMap m_ImageManagerMap;
94 static ImageList* pImageListSmall=0;
95 static ImageList* pImageListBig=0;
96 static ImageList* pImageListHiSmall=0;
97 static ImageList* pImageListHiBig=0;
99 static SfxImageManager_Impl* GetImageManager( SfxModule* pModule )
101 ::vos::OGuard aGuard( Application::GetSolarMutex() );
103 if ( pModule == 0 )
105 if ( !pGlobalImageManager )
106 pGlobalImageManager = new SfxImageManager_Impl( 0 );
107 return pGlobalImageManager;
109 else
111 SfxImageManager_Impl* pImpl( 0 );
112 SfxImageManagerMap::const_iterator pIter = m_ImageManager_ImplMap.find( sal::static_int_cast< sal_Int64>( reinterpret_cast< sal_IntPtr >( pModule )));
113 if ( pIter != m_ImageManager_ImplMap.end() )
114 pImpl = reinterpret_cast< SfxImageManager_Impl* >( sal::static_int_cast< sal_IntPtr >( pIter->second ));
115 else
117 pImpl = new SfxImageManager_Impl( pModule );
118 m_ImageManager_ImplMap.insert(
119 SfxImageManagerMap::value_type(
120 sal::static_int_cast< sal_Int64 >( reinterpret_cast< sal_IntPtr >( pModule )),
121 sal::static_int_cast< sal_Int64 >( reinterpret_cast< sal_IntPtr >( pImpl )) ));
123 return pImpl;
127 // Global image list
128 static ImageList* GetImageList( BOOL bBig, BOOL bHiContrast )
130 ::vos::OGuard aGuard( Application::GetSolarMutex() );
132 // Has to be changed if we know how the IDs are named!!!
133 ImageList*& rpList = bBig ? ( bHiContrast ? pImageListHiBig : pImageListBig ) :
134 ( bHiContrast ? pImageListHiSmall : pImageListSmall );
135 if ( !rpList )
137 ResMgr *pResMgr = SfxApplication::GetOrCreate()->GetOffResManager_Impl();
139 ResId aResId( bBig ? ( bHiContrast ? RID_DEFAULTIMAGELIST_LCH : RID_DEFAULTIMAGELIST_LC ) :
140 ( bHiContrast ? RID_DEFAULTIMAGELIST_SCH : RID_DEFAULTIMAGELIST_SC ), *pResMgr);
142 aResId.SetRT( RSC_IMAGELIST );
144 DBG_ASSERT( pResMgr->IsAvailable(aResId), "No default ImageList!" );
146 if ( pResMgr->IsAvailable(aResId) )
147 rpList = new ImageList( aResId );
148 else
149 rpList = new ImageList();
152 return rpList;
155 static sal_Int16 impl_convertBools( sal_Bool bLarge, sal_Bool bHiContrast )
157 sal_Int16 nIndex( 0 );
158 if ( bLarge )
159 nIndex += 1;
160 if ( bHiContrast )
161 nIndex += 2;
162 return nIndex;
165 //=========================================================================
167 SfxImageManager_Impl::SfxImageManager_Impl( SfxModule* pModule ) :
168 m_nSymbolsSize( SvtMiscOptions().GetCurrentSymbolsSize() ),
169 m_pModule( pModule )
171 for ( sal_uInt32 i = 0; i < IMAGELIST_COUNT; i++ )
172 m_pImageList[i] = 0;
174 m_aOpt.AddListener( LINK( this, SfxImageManager_Impl, OptionsChanged_Impl ) );
175 Application::AddEventListener( LINK( this, SfxImageManager_Impl, SettingsChanged_Impl ) );
178 //-------------------------------------------------------------------------
180 SfxImageManager_Impl::~SfxImageManager_Impl()
182 m_aOpt.RemoveListener( LINK( this, SfxImageManager_Impl, OptionsChanged_Impl ) );
183 Application::RemoveEventListener( LINK( this, SfxImageManager_Impl, SettingsChanged_Impl ) );
185 for ( sal_uInt32 i = 0; i < m_aToolBoxes.size(); i++ )
186 delete m_aToolBoxes[i];
189 //-------------------------------------------------------------------------
191 ImageList* SfxImageManager_Impl::GetImageList( BOOL bBig, BOOL bHiContrast )
193 sal_Int32 nIndex = impl_convertBools( bBig, bHiContrast );
194 if ( !m_pImageList[nIndex] )
196 if ( !m_pModule )
197 m_pImageList[nIndex] = ::GetImageList( bBig, bHiContrast );
198 else
199 m_pImageList[nIndex] = m_pModule->GetImageList_Impl( bBig, bHiContrast );
202 return m_pImageList[nIndex];
205 //-------------------------------------------------------------------------
207 Image SfxImageManager_Impl::GetImage( USHORT nId, BOOL bBig, BOOL bHiContrast )
209 ImageList* pImageList = GetImageList( bBig, bHiContrast );
210 if ( pImageList )
211 return pImageList->GetImage( nId );
212 return Image();
215 //-------------------------------------------------------------------------
217 void SfxImageManager_Impl::SetSymbolsSize_Impl( sal_Int16 nNewSymbolsSize )
219 ::vos::OGuard aGuard( Application::GetSolarMutex() );
221 if ( nNewSymbolsSize != m_nSymbolsSize )
223 m_nSymbolsSize = nNewSymbolsSize;
224 BOOL bLarge( m_nSymbolsSize == SFX_SYMBOLS_SIZE_LARGE );
226 for ( sal_uInt32 n=0; n < m_aToolBoxes.size(); n++ )
228 ToolBoxInf_Impl *pInf = m_aToolBoxes[n];
229 if ( pInf->nFlags & SFX_TOOLBOX_CHANGESYMBOLSET )
231 ToolBox *pBox = pInf->pToolBox;
232 BOOL bHiContrast = pBox->GetBackground().GetColor().IsDark();
233 USHORT nCount = pBox->GetItemCount();
234 for ( USHORT nPos=0; nPos<nCount; nPos++ )
236 USHORT nId = pBox->GetItemId( nPos );
237 if ( pBox->GetItemType(nPos) == TOOLBOXITEM_BUTTON )
239 pBox->SetItemImage( nId, GetImage( nId, bLarge, bHiContrast ) );
240 SfxStateCache *pCache = SfxViewFrame::Current()->GetBindings().GetStateCache( nId );
241 if ( pCache )
242 pCache->SetCachedState();
246 if ( !pBox->IsFloatingMode() )
248 Size aActSize( pBox->GetSizePixel() );
249 Size aSize( pBox->CalcWindowSizePixel() );
250 if ( pBox->IsHorizontal() )
251 aSize.Width() = aActSize.Width();
252 else
253 aSize.Height() = aActSize.Height();
255 pBox->SetSizePixel( aSize );
262 //-------------------------------------------------------------------------
264 IMPL_LINK( SfxImageManager_Impl, OptionsChanged_Impl, void*, EMPTYARG )
266 SetSymbolsSize_Impl( SvtMiscOptions().GetCurrentSymbolsSize() );
267 return 0L;
270 //-------------------------------------------------------------------------
272 IMPL_LINK( SfxImageManager_Impl, SettingsChanged_Impl, void*, EMPTYARG )
274 // Check if toolbar button size have changed and we have to use system settings
275 sal_Int16 nSymbolsSize = SvtMiscOptions().GetCurrentSymbolsSize();
276 if ( m_nSymbolsSize != nSymbolsSize )
277 SetSymbolsSize_Impl( nSymbolsSize );
278 return 0L;
281 //-------------------------------------------------------------------------
283 //=========================================================================
285 SfxImageManager::SfxImageManager( SfxModule* pModule )
287 pImp = ::GetImageManager( pModule );
290 //-------------------------------------------------------------------------
292 SfxImageManager::~SfxImageManager()
296 //-------------------------------------------------------------------------
298 SfxImageManager* SfxImageManager::GetImageManager( SfxModule* pModule )
300 ::vos::OGuard aGuard( Application::GetSolarMutex() );
302 SfxImageManagerMap::const_iterator pIter =
303 m_ImageManagerMap.find( sal::static_int_cast< sal_Int64 >( reinterpret_cast< sal_IntPtr >( pModule )));
304 if ( pIter != m_ImageManagerMap.end() )
305 return reinterpret_cast< SfxImageManager* >( sal::static_int_cast< sal_IntPtr >( pIter->second ));
306 else
308 SfxImageManager* pSfxImageManager = new SfxImageManager( pModule );
309 m_ImageManagerMap.insert( SfxImageManagerMap::value_type(
310 sal::static_int_cast< sal_Int64 >( reinterpret_cast< sal_IntPtr >( pModule )),
311 sal::static_int_cast< sal_Int64 >( reinterpret_cast< sal_IntPtr >( pSfxImageManager )) ));
312 return pSfxImageManager;
316 //-------------------------------------------------------------------------
318 Image SfxImageManager::GetImage( USHORT nId, BOOL bBig, BOOL bHiContrast ) const
320 ImageList* pImageList = pImp->GetImageList( bBig, bHiContrast );
321 if ( pImageList && pImageList->HasImageAtPos( nId ) )
322 return pImageList->GetImage( nId );
323 return Image();
326 //-------------------------------------------------------------------------
328 Image SfxImageManager::GetImage( USHORT nId, BOOL bHiContrast ) const
330 BOOL bLarge = SvtMiscOptions().AreCurrentSymbolsLarge();
331 return GetImage( nId, bLarge, bHiContrast );
334 //-------------------------------------------------------------------------
336 Image SfxImageManager::SeekImage( USHORT nId, BOOL bBig, BOOL bHiContrast ) const
338 sal_Bool bGlobal = ( pImp->m_pModule == 0 );
339 ImageList* pImageList = pImp->GetImageList( bBig, bHiContrast );
340 if ( pImageList && pImageList->HasImageAtPos( nId ) )
341 return pImageList->GetImage( nId );
342 else if ( !bGlobal )
344 pImageList = ::GetImageManager( 0 )->GetImageList( bBig, bHiContrast );
345 if ( pImageList )
346 return pImageList->GetImage( nId );
348 return Image();
351 //-------------------------------------------------------------------------
353 Image SfxImageManager::SeekImage( USHORT nId, BOOL bHiContrast ) const
355 BOOL bLarge = SvtMiscOptions().AreCurrentSymbolsLarge();
356 return SeekImage( nId, bLarge, bHiContrast );
359 //-------------------------------------------------------------------------
361 void SfxImageManager::RegisterToolBox( ToolBox *pBox, USHORT nFlags )
363 ::vos::OGuard aGuard( Application::GetSolarMutex() );
365 ToolBoxInf_Impl* pInf = new ToolBoxInf_Impl;
366 pInf->pToolBox = pBox;
367 pInf->nFlags = nFlags;
368 pImp->m_aToolBoxes.push_back( pInf );
371 //-------------------------------------------------------------------------
373 void SfxImageManager::ReleaseToolBox( ToolBox *pBox )
375 ::vos::OGuard aGuard( Application::GetSolarMutex() );
377 for ( sal_uInt32 n=0; n < pImp->m_aToolBoxes.size(); n++ )
379 if ((pImp->m_aToolBoxes[n])->pToolBox == pBox )
381 delete pImp->m_aToolBoxes[n];
382 pImp->m_aToolBoxes.erase( pImp->m_aToolBoxes.begin() + n );
383 return;
388 //-------------------------------------------------------------------------
390 void SfxImageManager::SetImages( ToolBox& rToolBox, BOOL bHiContrast, BOOL bLarge )
392 SetImagesForceSize( rToolBox, bLarge, bHiContrast );
395 //-------------------------------------------------------------------------
397 void SfxImageManager::SetImagesForceSize( ToolBox& rToolBox, BOOL bHiContrast, BOOL bLarge )
399 ImageList* pImageList = pImp->GetImageList( bLarge, bHiContrast );
401 USHORT nCount = rToolBox.GetItemCount();
402 for (USHORT n=0; n<nCount; n++)
404 USHORT nId = rToolBox.GetItemId(n);
405 switch ( rToolBox.GetItemType(n) )
407 case TOOLBOXITEM_BUTTON:
409 if ( pImageList && pImageList->HasImageAtPos( nId ) )
410 rToolBox.SetItemImage( nId, pImageList->GetImage( nId ));
411 else
412 rToolBox.SetItemImage( nId, Image() );
415 case TOOLBOXITEM_SEPARATOR:
416 case TOOLBOXITEM_SPACE:
417 case TOOLBOXITEM_BREAK:
418 default:
419 break;
424 void SfxImageManager::SetImages( ToolBox& rToolBox )
426 BOOL bLarge = ( pImp->m_nSymbolsSize == SFX_SYMBOLS_SIZE_LARGE );
427 BOOL bHiContrast = rToolBox.GetBackground().GetColor().IsDark();
428 SetImagesForceSize( rToolBox, bHiContrast, bLarge );