]> git.saurik.com Git - wxWidgets.git/blame - src/common/artprov.cpp
added wxVaCopy() and use it to fix wxVsnprintf() crash (see bug 1017651)
[wxWidgets.git] / src / common / artprov.cpp
CommitLineData
2aca4a98
VS
1/////////////////////////////////////////////////////////////////////////////
2// Name: artprov.cpp
3// Purpose: wxArtProvider class
4// Author: Vaclav Slavik
5// Modified by:
6// Created: 18/03/2002
7// RCS-ID: $Id$
8// Copyright: (c) Vaclav Slavik
65571936 9// Licence: wxWindows licence
2aca4a98
VS
10/////////////////////////////////////////////////////////////////////////////
11
12// ---------------------------------------------------------------------------
13// headers
14// ---------------------------------------------------------------------------
15
14f355c2 16#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
2aca4a98
VS
17 #pragma implementation "artprov.h"
18#endif
19
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#if defined(__BORLANDC__)
24 #pragma hdrstop
25#endif
26
27#ifndef WX_PRECOMP
28 #include "wx/log.h"
29 #include "wx/list.h"
30#endif
31
32#include "wx/artprov.h"
33#include "wx/hashmap.h"
34#include "wx/module.h"
cfb57561 35#if wxUSE_IMAGE
3242feb1 36#include "wx/image.h"
cfb57561 37#endif
2aca4a98 38
2aca4a98
VS
39// ===========================================================================
40// implementation
41// ===========================================================================
42
43#include "wx/listimpl.cpp"
44WX_DECLARE_LIST(wxArtProvider, wxArtProvidersList);
45WX_DEFINE_LIST(wxArtProvidersList);
46
47// ----------------------------------------------------------------------------
48// Cache class - stores already requested bitmaps
49// ----------------------------------------------------------------------------
50
9e7ed2b0 51WX_DECLARE_EXPORTED_STRING_HASH_MAP(wxBitmap, wxArtProviderBitmapsHash);
2aca4a98
VS
52
53class WXDLLEXPORT wxArtProviderCache
54{
55public:
56 bool GetBitmap(const wxString& full_id, wxBitmap* bmp);
57 void PutBitmap(const wxString& full_id, const wxBitmap& bmp)
58 { m_bitmapsHash[full_id] = bmp; }
59
60 void Clear();
9e7ed2b0 61
57b0987b
VS
62 static wxString ConstructHashID(const wxArtID& id,
63 const wxArtClient& client,
2aca4a98
VS
64 const wxSize& size);
65
66private:
67 wxArtProviderBitmapsHash m_bitmapsHash;
68};
69
70bool wxArtProviderCache::GetBitmap(const wxString& full_id, wxBitmap* bmp)
71{
72 wxArtProviderBitmapsHash::iterator entry = m_bitmapsHash.find(full_id);
73 if ( entry == m_bitmapsHash.end() )
74 {
4629016d 75 return false;
2aca4a98
VS
76 }
77 else
78 {
79 *bmp = entry->second;
4629016d 80 return true;
2aca4a98
VS
81 }
82}
83
84void wxArtProviderCache::Clear()
85{
86 m_bitmapsHash.clear();
87}
88
89/*static*/ wxString wxArtProviderCache::ConstructHashID(
57b0987b
VS
90 const wxArtID& id, const wxArtClient& client,
91 const wxSize& size)
2aca4a98
VS
92{
93 wxString str;
57b0987b 94 str.Printf(wxT("%s-%s-%i-%i"), id.c_str(), client.c_str(), size.x, size.y);
2aca4a98
VS
95 return str;
96}
97
98
99// ----------------------------------------------------------------------------
100// wxArtProvider class
101// ----------------------------------------------------------------------------
102
103IMPLEMENT_ABSTRACT_CLASS(wxArtProvider, wxObject)
104
105wxArtProvidersList *wxArtProvider::sm_providers = NULL;
106wxArtProviderCache *wxArtProvider::sm_cache = NULL;
107
108/*static*/ void wxArtProvider::PushProvider(wxArtProvider *provider)
109{
110 if ( !sm_providers )
111 {
112 sm_providers = new wxArtProvidersList;
2aca4a98
VS
113 sm_cache = new wxArtProviderCache;
114 }
115
116 sm_providers->Insert(provider);
c3357a03 117 sm_cache->Clear();
2aca4a98
VS
118}
119
120/*static*/ bool wxArtProvider::PopProvider()
121{
4629016d
WS
122 wxCHECK_MSG( sm_providers, false, _T("no wxArtProvider exists") );
123 wxCHECK_MSG( sm_providers->GetCount() > 0, false, _T("wxArtProviders stack is empty") );
2aca4a98 124
222ed1d6
MB
125 delete sm_providers->GetFirst()->GetData();
126 sm_providers->Erase(sm_providers->GetFirst());
2aca4a98 127 sm_cache->Clear();
4629016d 128 return true;
2aca4a98
VS
129}
130
131/*static*/ bool wxArtProvider::RemoveProvider(wxArtProvider *provider)
132{
4629016d 133 wxCHECK_MSG( sm_providers, false, _T("no wxArtProvider exists") );
2aca4a98
VS
134
135 if ( sm_providers->DeleteObject(provider) )
136 {
222ed1d6 137 delete provider;
2aca4a98 138 sm_cache->Clear();
4629016d 139 return true;
2aca4a98 140 }
9e7ed2b0 141
4629016d 142 return false;
2aca4a98
VS
143}
144
145/*static*/ void wxArtProvider::CleanUpProviders()
146{
89c20ac1 147 WX_CLEAR_LIST(wxArtProvidersList, *sm_providers);
2aca4a98
VS
148 wxDELETE(sm_providers);
149 wxDELETE(sm_cache);
150}
151
57b0987b
VS
152/*static*/ wxBitmap wxArtProvider::GetBitmap(const wxArtID& id,
153 const wxArtClient& client,
2aca4a98
VS
154 const wxSize& size)
155{
57b0987b
VS
156 // safety-check against writing client,id,size instead of id,client,size:
157 wxASSERT_MSG( client.Last() == _T('C'), _T("invalid 'client' parameter") );
158
2aca4a98
VS
159 wxCHECK_MSG( sm_providers, wxNullBitmap, _T("no wxArtProvider exists") );
160
57b0987b 161 wxString hashId = wxArtProviderCache::ConstructHashID(id, client, size);
2aca4a98
VS
162
163 wxBitmap bmp;
164 if ( !sm_cache->GetBitmap(hashId, &bmp) )
165 {
222ed1d6 166 for (wxArtProvidersList::compatibility_iterator node = sm_providers->GetFirst();
2aca4a98
VS
167 node; node = node->GetNext())
168 {
57b0987b 169 bmp = node->GetData()->CreateBitmap(id, client, size);
2aca4a98 170 if ( bmp.Ok() )
3242feb1 171 {
cfb57561 172#if wxUSE_IMAGE
9e7ed2b0 173 if ( size != wxDefaultSize &&
3242feb1
VS
174 (bmp.GetWidth() != size.x || bmp.GetHeight() != size.y) )
175 {
176 wxImage img = bmp.ConvertToImage();
177 img.Rescale(size.x, size.y);
178 bmp = wxBitmap(img);
179 }
4629016d 180#endif
2aca4a98 181 break;
3242feb1 182 }
2aca4a98 183 }
3242feb1 184
2aca4a98
VS
185 sm_cache->PutBitmap(hashId, bmp);
186 }
187
188 return bmp;
189}
190
57b0987b
VS
191/*static*/ wxIcon wxArtProvider::GetIcon(const wxArtID& id,
192 const wxArtClient& client,
2aca4a98
VS
193 const wxSize& size)
194{
195 wxCHECK_MSG( sm_providers, wxNullIcon, _T("no wxArtProvider exists") );
196
57b0987b 197 wxBitmap bmp = GetBitmap(id, client, size);
25dd6997 198 if ( !bmp.Ok() )
2aca4a98 199 return wxNullIcon;
2aca4a98 200
25dd6997
VZ
201 wxIcon icon;
202 icon.CopyFromBitmap(bmp);
203 return icon;
204}
2aca4a98
VS
205
206
207class wxArtProviderModule: public wxModule
208{
209public:
2b5f62a0
VZ
210 bool OnInit()
211 {
212 wxArtProvider::InitStdProvider();
7bebedd8 213 wxArtProvider::InitNativeProvider();
4629016d 214 return true;
2b5f62a0
VZ
215 }
216 void OnExit()
217 {
218 wxArtProvider::CleanUpProviders();
219 }
2aca4a98
VS
220
221 DECLARE_DYNAMIC_CLASS(wxArtProviderModule)
222};
223
224IMPLEMENT_DYNAMIC_CLASS(wxArtProviderModule, wxModule)