1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <memory>
#include <sfx2/imgmgr.hxx>
#include <sfx2/sfx.hrc>
#include <sfx2/app.hxx>
#include <sfx2/sfxresid.hxx>
#include <sfx2/bindings.hxx>
#include "statcach.hxx"
#include <sfx2/module.hxx>
#include <vcl/bitmap.hxx>
#include <vcl/toolbox.hxx>
#include <tools/rcid.h>
#include <tools/link.hxx>
#include <svtools/miscopt.hxx>
#include <osl/mutex.hxx>
#include <rtl/instance.hxx>
#include <comphelper/processfactory.hxx>
#include <unordered_map>
const sal_uInt32 IMAGELIST_COUNT = 4; // small, small-hi, large, large-hi
struct ToolBoxInf_Impl
{
VclPtr<ToolBox> pToolBox;
SfxToolboxFlags nFlags;
};
class SfxImageManager_Impl
{
public:
SvtMiscOptions m_aOpt;
std::vector< ToolBoxInf_Impl* > m_aToolBoxes;
sal_Int16 m_nSymbolsSize;
ImageList* m_pImageList[IMAGELIST_COUNT];
SfxModule& m_rModule;
bool m_bAppEventListener;
ImageList* GetImageList( bool bBig );
Image GetImage( sal_uInt16 nId, bool bBig );
void SetSymbolsSize_Impl( sal_Int16 );
DECL_LINK_TYPED( OptionsChanged_Impl, LinkParamNone*, void );
DECL_LINK_TYPED( SettingsChanged_Impl, VclSimpleEvent&, void );
explicit SfxImageManager_Impl(SfxModule& rModule);
~SfxImageManager_Impl();
};
namespace
{
typedef std::unordered_map< SfxModule*, std::shared_ptr<SfxImageManager_Impl> > SfxImageManagerImplMap;
class theImageManagerImplMap :
public rtl::Static<SfxImageManagerImplMap, theImageManagerImplMap> {};
}
static SfxImageManager_Impl* GetImageManager(SfxModule& rModule)
{
SolarMutexGuard aGuard;
SfxImageManagerImplMap &rImageManager_ImplMap =
theImageManagerImplMap::get();
SfxImageManager_Impl* pImpl( nullptr );
SfxModule* pModule(&rModule);
SfxImageManagerImplMap::const_iterator pIter = rImageManager_ImplMap.find(pModule);
if ( pIter != rImageManager_ImplMap.end() )
pImpl = pIter->second.get();
else
{
rImageManager_ImplMap[pModule].reset(new SfxImageManager_Impl(rModule));
pImpl = rImageManager_ImplMap[pModule].get();
}
return pImpl;
}
static sal_Int16 impl_convertBools( bool bLarge )
{
sal_Int16 nIndex( 0 );
if ( bLarge )
nIndex += 1;
return nIndex;
}
SfxImageManager_Impl::SfxImageManager_Impl(SfxModule& rModule)
: m_rModule(rModule)
, m_bAppEventListener(false)
{
m_nSymbolsSize = m_aOpt.GetCurrentSymbolsSize();
for (ImageList* & rp : m_pImageList)
rp = nullptr;
m_aOpt.AddListenerLink( LINK( this, SfxImageManager_Impl, OptionsChanged_Impl ) );
Application::AddEventListener( LINK( this, SfxImageManager_Impl, SettingsChanged_Impl ) );
m_bAppEventListener = true;
}
SfxImageManager_Impl::~SfxImageManager_Impl()
{
m_aOpt.RemoveListenerLink( LINK( this, SfxImageManager_Impl, OptionsChanged_Impl ) );
if (m_bAppEventListener)
Application::RemoveEventListener( LINK( this, SfxImageManager_Impl, SettingsChanged_Impl ) );
for (ToolBoxInf_Impl* p : m_aToolBoxes)
delete p;
}
ImageList* SfxImageManager_Impl::GetImageList( bool bBig )
{
sal_Int32 nIndex = impl_convertBools( bBig );
if ( !m_pImageList[nIndex] )
{
m_pImageList[nIndex] = m_rModule.GetImageList_Impl( bBig );
}
return m_pImageList[nIndex];
}
Image SfxImageManager_Impl::GetImage( sal_uInt16 nId, bool bBig )
{
ImageList* pImageList = GetImageList( bBig );
if ( pImageList )
return pImageList->GetImage( nId );
return Image();
}
void SfxImageManager_Impl::SetSymbolsSize_Impl( sal_Int16 nNewSymbolsSize )
{
SolarMutexGuard aGuard;
if ( nNewSymbolsSize != m_nSymbolsSize )
{
m_nSymbolsSize = nNewSymbolsSize;
bool bLarge( m_nSymbolsSize == SFX_SYMBOLS_SIZE_LARGE );
for (ToolBoxInf_Impl* pInf : m_aToolBoxes)
{
if ( pInf->nFlags & SfxToolboxFlags::CHANGESYMBOLSET )
{
ToolBox *pBox = pInf->pToolBox;
sal_uInt16 nCount = pBox->GetItemCount();
for ( sal_uInt16 nPos=0; nPos<nCount; nPos++ )
{
sal_uInt16 nId = pBox->GetItemId( nPos );
if ( pBox->GetItemType(nPos) == ToolBoxItemType::BUTTON )
{
pBox->SetItemImage( nId, GetImage( nId, bLarge ) );
SfxStateCache *pCache = SfxViewFrame::Current()->GetBindings().GetStateCache( nId );
if ( pCache )
pCache->SetCachedState();
}
}
if ( !pBox->IsFloatingMode() )
{
Size aActSize( pBox->GetSizePixel() );
Size aSize( pBox->CalcWindowSizePixel() );
if ( pBox->IsHorizontal() )
aSize.Width() = aActSize.Width();
else
aSize.Height() = aActSize.Height();
pBox->SetSizePixel( aSize );
}
}
}
}
}
IMPL_LINK_NOARG_TYPED(SfxImageManager_Impl, OptionsChanged_Impl, LinkParamNone*, void)
{
SetSymbolsSize_Impl( m_aOpt.GetCurrentSymbolsSize() );
}
IMPL_LINK_TYPED( SfxImageManager_Impl, SettingsChanged_Impl, VclSimpleEvent&, rEvent, void)
{
switch (rEvent.GetId())
{
case VCLEVENT_OBJECT_DYING:
if (m_bAppEventListener)
{
Application::RemoveEventListener( LINK( this, SfxImageManager_Impl, SettingsChanged_Impl ) );
m_bAppEventListener = false;
}
break;
case VCLEVENT_APPLICATION_DATACHANGED:
// Check if toolbar button size have changed and we have to use system settings
{
sal_Int16 nSymbolsSize = m_aOpt.GetCurrentSymbolsSize();
if (m_nSymbolsSize != nSymbolsSize)
SetSymbolsSize_Impl(nSymbolsSize);
}
break;
default:
break;
}
}
SfxImageManager::SfxImageManager(SfxModule& rModule)
{
pImp = ::GetImageManager(rModule);
}
SfxImageManager::~SfxImageManager()
{
}
namespace
{
typedef std::unordered_map< SfxModule*, std::shared_ptr<SfxImageManager> > SfxImageManagerMap;
class theImageManagerMap :
public rtl::Static<SfxImageManagerMap, theImageManagerMap> {};
}
SfxImageManager* SfxImageManager::GetImageManager(SfxModule& rModule)
{
SolarMutexGuard aGuard;
SfxImageManager* pSfxImageManager(nullptr);
SfxImageManagerMap &rImageManagerMap = theImageManagerMap::get();
SfxModule* pModule = &rModule;
SfxImageManagerMap::const_iterator pIter = rImageManagerMap.find(pModule);
if ( pIter != rImageManagerMap.end() )
pSfxImageManager = pIter->second.get();
else
{
rImageManagerMap[pModule].reset(new SfxImageManager(rModule));
pSfxImageManager = rImageManagerMap[pModule].get();
}
return pSfxImageManager;
}
Image SfxImageManager::GetImage( sal_uInt16 nId, bool bBig ) const
{
ImageList* pImageList = pImp->GetImageList( bBig );
if ( pImageList && pImageList->HasImageAtPos( nId ) )
return pImageList->GetImage( nId );
return Image();
}
Image SfxImageManager::GetImage( sal_uInt16 nId ) const
{
bool bLarge = SvtMiscOptions().AreCurrentSymbolsLarge();
return GetImage( nId, bLarge );
}
Image SfxImageManager::SeekImage( sal_uInt16 nId, bool bBig ) const
{
ImageList* pImageList = pImp->GetImageList( bBig );
if (pImageList && pImageList->HasImageAtPos(nId))
return pImageList->GetImage( nId );
return Image();
}
Image SfxImageManager::SeekImage( sal_uInt16 nId ) const
{
bool bLarge = SvtMiscOptions().AreCurrentSymbolsLarge();
return SeekImage( nId, bLarge );
}
void SfxImageManager::RegisterToolBox( ToolBox *pBox, SfxToolboxFlags nFlags )
{
SolarMutexGuard aGuard;
ToolBoxInf_Impl* pInf = new ToolBoxInf_Impl;
pInf->pToolBox = pBox;
pInf->nFlags = nFlags;
pImp->m_aToolBoxes.push_back( pInf );
}
void SfxImageManager::ReleaseToolBox( ToolBox *pBox )
{
SolarMutexGuard aGuard;
for ( size_t n=0; n < pImp->m_aToolBoxes.size(); n++ )
{
if ((pImp->m_aToolBoxes[n])->pToolBox == pBox )
{
delete pImp->m_aToolBoxes[n];
pImp->m_aToolBoxes.erase( pImp->m_aToolBoxes.begin() + n );
return;
}
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|