bump product version to 5.0.4.1
[LibreOffice.git] / sfx2 / source / toolbox / imgmgr.cxx
blob7bdda110e27975f2cad01e4919256e0e0628ccc0
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 <boost/shared_ptr.hpp>
23 #include <sfx2/imgmgr.hxx>
24 #include <sfx2/sfx.hrc>
25 #include <sfx2/app.hxx>
26 #include <sfx2/sfxresid.hxx>
27 #include <sfx2/bindings.hxx>
28 #include "statcach.hxx"
29 #include <sfx2/module.hxx>
30 #include <vcl/bitmap.hxx>
31 #include <vcl/toolbox.hxx>
33 #include <tools/rcid.h>
34 #include <tools/link.hxx>
35 #include <svtools/miscopt.hxx>
36 #include <osl/mutex.hxx>
37 #include <rtl/instance.hxx>
39 #include <comphelper/processfactory.hxx>
41 #include <unordered_map>
43 const sal_uInt32 IMAGELIST_COUNT = 4; // small, small-hi, large, large-hi
45 struct ToolBoxInf_Impl
47 VclPtr<ToolBox> pToolBox;
48 SfxToolboxFlags 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_rModule;
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* );
68 SfxImageManager_Impl(SfxModule& rModule);
69 ~SfxImageManager_Impl();
72 namespace
74 typedef std::unordered_map< SfxModule*, boost::shared_ptr<SfxImageManager_Impl> > SfxImageManagerImplMap;
76 class theImageManagerImplMap :
77 public rtl::Static<SfxImageManagerImplMap, theImageManagerImplMap> {};
80 static SfxImageManager_Impl* GetImageManager(SfxModule& rModule)
82 SolarMutexGuard aGuard;
84 SfxImageManagerImplMap &rImageManager_ImplMap =
85 theImageManagerImplMap::get();
86 SfxImageManager_Impl* pImpl( 0 );
87 SfxModule* pModule(&rModule);
88 SfxImageManagerImplMap::const_iterator pIter = rImageManager_ImplMap.find(pModule);
89 if ( pIter != rImageManager_ImplMap.end() )
90 pImpl = pIter->second.get();
91 else
93 rImageManager_ImplMap[pModule].reset(new SfxImageManager_Impl(rModule));
94 pImpl = rImageManager_ImplMap[pModule].get();
96 return pImpl;
99 static sal_Int16 impl_convertBools( bool bLarge )
101 sal_Int16 nIndex( 0 );
102 if ( bLarge )
103 nIndex += 1;
104 return nIndex;
107 SfxImageManager_Impl::SfxImageManager_Impl(SfxModule& rModule)
108 : m_rModule(rModule)
109 , m_bAppEventListener(false)
111 m_nSymbolsSize = m_aOpt.GetCurrentSymbolsSize();
113 for ( sal_uInt32 i = 0; i < IMAGELIST_COUNT; i++ )
114 m_pImageList[i] = 0;
116 m_aOpt.AddListenerLink( LINK( this, SfxImageManager_Impl, OptionsChanged_Impl ) );
117 Application::AddEventListener( LINK( this, SfxImageManager_Impl, SettingsChanged_Impl ) );
118 m_bAppEventListener = true;
123 SfxImageManager_Impl::~SfxImageManager_Impl()
125 m_aOpt.RemoveListenerLink( LINK( this, SfxImageManager_Impl, OptionsChanged_Impl ) );
126 if (m_bAppEventListener)
127 Application::RemoveEventListener( LINK( this, SfxImageManager_Impl, SettingsChanged_Impl ) );
128 for ( sal_uInt32 i = 0; i < m_aToolBoxes.size(); i++ )
129 delete m_aToolBoxes[i];
134 ImageList* SfxImageManager_Impl::GetImageList( bool bBig )
136 sal_Int32 nIndex = impl_convertBools( bBig );
137 if ( !m_pImageList[nIndex] )
139 m_pImageList[nIndex] = m_rModule.GetImageList_Impl( bBig );
142 return m_pImageList[nIndex];
147 Image SfxImageManager_Impl::GetImage( sal_uInt16 nId, bool bBig )
149 ImageList* pImageList = GetImageList( bBig );
150 if ( pImageList )
151 return pImageList->GetImage( nId );
152 return Image();
157 void SfxImageManager_Impl::SetSymbolsSize_Impl( sal_Int16 nNewSymbolsSize )
159 SolarMutexGuard aGuard;
161 if ( nNewSymbolsSize != m_nSymbolsSize )
163 m_nSymbolsSize = nNewSymbolsSize;
164 bool bLarge( m_nSymbolsSize == SFX_SYMBOLS_SIZE_LARGE );
166 for ( sal_uInt32 n=0; n < m_aToolBoxes.size(); n++ )
168 ToolBoxInf_Impl *pInf = m_aToolBoxes[n];
169 if ( pInf->nFlags & SfxToolboxFlags::CHANGESYMBOLSET )
171 ToolBox *pBox = pInf->pToolBox;
172 sal_uInt16 nCount = pBox->GetItemCount();
173 for ( sal_uInt16 nPos=0; nPos<nCount; nPos++ )
175 sal_uInt16 nId = pBox->GetItemId( nPos );
176 if ( pBox->GetItemType(nPos) == ToolBoxItemType::BUTTON )
178 pBox->SetItemImage( nId, GetImage( nId, bLarge ) );
179 SfxStateCache *pCache = SfxViewFrame::Current()->GetBindings().GetStateCache( nId );
180 if ( pCache )
181 pCache->SetCachedState();
185 if ( !pBox->IsFloatingMode() )
187 Size aActSize( pBox->GetSizePixel() );
188 Size aSize( pBox->CalcWindowSizePixel() );
189 if ( pBox->IsHorizontal() )
190 aSize.Width() = aActSize.Width();
191 else
192 aSize.Height() = aActSize.Height();
194 pBox->SetSizePixel( aSize );
203 IMPL_LINK_NOARG(SfxImageManager_Impl, OptionsChanged_Impl)
205 SetSymbolsSize_Impl( m_aOpt.GetCurrentSymbolsSize() );
206 return 0L;
211 IMPL_LINK( SfxImageManager_Impl, SettingsChanged_Impl, VclWindowEvent*, pEvent)
213 if (pEvent)
215 switch (pEvent->GetId())
217 case VCLEVENT_OBJECT_DYING:
218 if (m_bAppEventListener)
220 Application::RemoveEventListener( LINK( this, SfxImageManager_Impl, SettingsChanged_Impl ) );
221 m_bAppEventListener = false;
223 break;
224 case VCLEVENT_APPLICATION_DATACHANGED:
225 // Check if toolbar button size have changed and we have to use system settings
227 sal_Int16 nSymbolsSize = m_aOpt.GetCurrentSymbolsSize();
228 if (m_nSymbolsSize != nSymbolsSize)
229 SetSymbolsSize_Impl(nSymbolsSize);
231 break;
232 default:
233 break;
236 return 0L;
239 SfxImageManager::SfxImageManager(SfxModule& rModule)
241 pImp = ::GetImageManager(rModule);
244 SfxImageManager::~SfxImageManager()
248 namespace
250 typedef std::unordered_map< SfxModule*, boost::shared_ptr<SfxImageManager> > SfxImageManagerMap;
252 class theImageManagerMap :
253 public rtl::Static<SfxImageManagerMap, theImageManagerMap> {};
256 SfxImageManager* SfxImageManager::GetImageManager(SfxModule& rModule)
258 SolarMutexGuard aGuard;
259 SfxImageManager* pSfxImageManager(0);
261 SfxImageManagerMap &rImageManagerMap = theImageManagerMap::get();
262 SfxModule* pModule = &rModule;
263 SfxImageManagerMap::const_iterator pIter = rImageManagerMap.find(pModule);
264 if ( pIter != rImageManagerMap.end() )
265 pSfxImageManager = pIter->second.get();
266 else
268 rImageManagerMap[pModule].reset(new SfxImageManager(rModule));
269 pSfxImageManager = rImageManagerMap[pModule].get();
271 return pSfxImageManager;
274 Image SfxImageManager::GetImage( sal_uInt16 nId, bool bBig ) const
276 ImageList* pImageList = pImp->GetImageList( bBig );
277 if ( pImageList && pImageList->HasImageAtPos( nId ) )
278 return pImageList->GetImage( nId );
279 return Image();
282 Image SfxImageManager::GetImage( sal_uInt16 nId ) const
284 bool bLarge = SvtMiscOptions().AreCurrentSymbolsLarge();
285 return GetImage( nId, bLarge );
288 Image SfxImageManager::SeekImage( sal_uInt16 nId, bool bBig ) const
290 ImageList* pImageList = pImp->GetImageList( bBig );
291 if (pImageList && pImageList->HasImageAtPos(nId))
292 return pImageList->GetImage( nId );
293 return Image();
296 Image SfxImageManager::SeekImage( sal_uInt16 nId ) const
298 bool bLarge = SvtMiscOptions().AreCurrentSymbolsLarge();
299 return SeekImage( nId, bLarge );
302 void SfxImageManager::RegisterToolBox( ToolBox *pBox, SfxToolboxFlags nFlags )
304 SolarMutexGuard aGuard;
306 ToolBoxInf_Impl* pInf = new ToolBoxInf_Impl;
307 pInf->pToolBox = pBox;
308 pInf->nFlags = nFlags;
309 pImp->m_aToolBoxes.push_back( pInf );
314 void SfxImageManager::ReleaseToolBox( ToolBox *pBox )
316 SolarMutexGuard aGuard;
318 for ( sal_uInt32 n=0; n < pImp->m_aToolBoxes.size(); n++ )
320 if ((pImp->m_aToolBoxes[n])->pToolBox == pBox )
322 delete pImp->m_aToolBoxes[n];
323 pImp->m_aToolBoxes.erase( pImp->m_aToolBoxes.begin() + n );
324 return;
329 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */