]> git.saurik.com Git - wxWidgets.git/blame - src/common/artprov.cpp
Added part of patch
[wxWidgets.git] / src / common / artprov.cpp
CommitLineData
2aca4a98 1/////////////////////////////////////////////////////////////////////////////
8ecff181 2// Name: src/common/artprov.cpp
2aca4a98
VS
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
2aca4a98
VS
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#if defined(__BORLANDC__)
20 #pragma hdrstop
21#endif
22
02761f6c
WS
23#include "wx/artprov.h"
24
2aca4a98 25#ifndef WX_PRECOMP
2aca4a98 26 #include "wx/list.h"
8ecff181 27 #include "wx/log.h"
df69528b 28 #include "wx/hashmap.h"
155ecd4c 29 #include "wx/image.h"
02761f6c 30 #include "wx/module.h"
2aca4a98
VS
31#endif
32
2aca4a98
VS
33// ===========================================================================
34// implementation
35// ===========================================================================
36
37#include "wx/listimpl.cpp"
38WX_DECLARE_LIST(wxArtProvider, wxArtProvidersList);
259c43f6 39WX_DEFINE_LIST(wxArtProvidersList)
2aca4a98
VS
40
41// ----------------------------------------------------------------------------
42// Cache class - stores already requested bitmaps
43// ----------------------------------------------------------------------------
44
3f5c62f9 45WX_DECLARE_EXPORTED_STRING_HASH_MAP(wxBitmap, wxArtProviderBitmapsHash);
2aca4a98
VS
46
47class WXDLLEXPORT wxArtProviderCache
48{
49public:
50 bool GetBitmap(const wxString& full_id, wxBitmap* bmp);
51 void PutBitmap(const wxString& full_id, const wxBitmap& bmp)
52 { m_bitmapsHash[full_id] = bmp; }
53
54 void Clear();
9e7ed2b0 55
57b0987b
VS
56 static wxString ConstructHashID(const wxArtID& id,
57 const wxArtClient& client,
2aca4a98
VS
58 const wxSize& size);
59
60private:
61 wxArtProviderBitmapsHash m_bitmapsHash;
62};
63
64bool wxArtProviderCache::GetBitmap(const wxString& full_id, wxBitmap* bmp)
65{
66 wxArtProviderBitmapsHash::iterator entry = m_bitmapsHash.find(full_id);
67 if ( entry == m_bitmapsHash.end() )
68 {
4629016d 69 return false;
2aca4a98
VS
70 }
71 else
72 {
73 *bmp = entry->second;
4629016d 74 return true;
2aca4a98
VS
75 }
76}
77
78void wxArtProviderCache::Clear()
79{
80 m_bitmapsHash.clear();
81}
82
83/*static*/ wxString wxArtProviderCache::ConstructHashID(
57b0987b
VS
84 const wxArtID& id, const wxArtClient& client,
85 const wxSize& size)
2aca4a98
VS
86{
87 wxString str;
57b0987b 88 str.Printf(wxT("%s-%s-%i-%i"), id.c_str(), client.c_str(), size.x, size.y);
2aca4a98
VS
89 return str;
90}
91
92
571d2e0f 93// ============================================================================
2aca4a98 94// wxArtProvider class
571d2e0f 95// ============================================================================
2aca4a98
VS
96
97IMPLEMENT_ABSTRACT_CLASS(wxArtProvider, wxObject)
98
99wxArtProvidersList *wxArtProvider::sm_providers = NULL;
100wxArtProviderCache *wxArtProvider::sm_cache = NULL;
101
571d2e0f
VZ
102// ----------------------------------------------------------------------------
103// wxArtProvider ctors/dtor
104// ----------------------------------------------------------------------------
105
106wxArtProvider::~wxArtProvider()
107{
108 Remove(this);
109}
110
111// ----------------------------------------------------------------------------
112// wxArtProvider operations on provider stack
113// ----------------------------------------------------------------------------
114
dd7d379e 115/*static*/ void wxArtProvider::CommonAddingProvider()
2aca4a98
VS
116{
117 if ( !sm_providers )
118 {
119 sm_providers = new wxArtProvidersList;
2aca4a98
VS
120 sm_cache = new wxArtProviderCache;
121 }
122
c3357a03 123 sm_cache->Clear();
2aca4a98
VS
124}
125
571d2e0f 126/*static*/ void wxArtProvider::Push(wxArtProvider *provider)
dd7d379e
VS
127{
128 CommonAddingProvider();
129 sm_providers->Insert(provider);
130}
131
571d2e0f 132/*static*/ void wxArtProvider::Insert(wxArtProvider *provider)
dd7d379e
VS
133{
134 CommonAddingProvider();
135 sm_providers->Append(provider);
136}
137
571d2e0f 138/*static*/ bool wxArtProvider::Pop()
2aca4a98 139{
4629016d 140 wxCHECK_MSG( sm_providers, false, _T("no wxArtProvider exists") );
571d2e0f 141 wxCHECK_MSG( !sm_providers->empty(), false, _T("wxArtProviders stack is empty") );
2aca4a98 142
222ed1d6 143 delete sm_providers->GetFirst()->GetData();
2aca4a98 144 sm_cache->Clear();
4629016d 145 return true;
2aca4a98
VS
146}
147
571d2e0f 148/*static*/ bool wxArtProvider::Remove(wxArtProvider *provider)
2aca4a98 149{
4629016d 150 wxCHECK_MSG( sm_providers, false, _T("no wxArtProvider exists") );
2aca4a98
VS
151
152 if ( sm_providers->DeleteObject(provider) )
153 {
154 sm_cache->Clear();
4629016d 155 return true;
2aca4a98 156 }
9e7ed2b0 157
4629016d 158 return false;
2aca4a98
VS
159}
160
571d2e0f
VZ
161/*static*/ bool wxArtProvider::Delete(wxArtProvider *provider)
162{
163 // provider will remove itself from the stack in its dtor
164 delete provider;
165
166 return true;
167}
168
2aca4a98
VS
169/*static*/ void wxArtProvider::CleanUpProviders()
170{
571d2e0f
VZ
171 if ( sm_providers )
172 {
173 while ( !sm_providers->empty() )
174 delete *sm_providers->begin();
175
176 delete sm_providers;
177 sm_providers = NULL;
178
179 delete sm_cache;
180 sm_cache = NULL;
181 }
2aca4a98
VS
182}
183
571d2e0f
VZ
184// ----------------------------------------------------------------------------
185// wxArtProvider: retrieving bitmaps/icons
186// ----------------------------------------------------------------------------
187
57b0987b
VS
188/*static*/ wxBitmap wxArtProvider::GetBitmap(const wxArtID& id,
189 const wxArtClient& client,
e53a95bc 190 const wxSize& size)
2aca4a98 191{
57b0987b
VS
192 // safety-check against writing client,id,size instead of id,client,size:
193 wxASSERT_MSG( client.Last() == _T('C'), _T("invalid 'client' parameter") );
194
2aca4a98
VS
195 wxCHECK_MSG( sm_providers, wxNullBitmap, _T("no wxArtProvider exists") );
196
e53a95bc 197 wxString hashId = wxArtProviderCache::ConstructHashID(id, client, size);
2aca4a98
VS
198
199 wxBitmap bmp;
200 if ( !sm_cache->GetBitmap(hashId, &bmp) )
201 {
222ed1d6 202 for (wxArtProvidersList::compatibility_iterator node = sm_providers->GetFirst();
2aca4a98
VS
203 node; node = node->GetNext())
204 {
e53a95bc 205 bmp = node->GetData()->CreateBitmap(id, client, size);
2aca4a98 206 if ( bmp.Ok() )
3242feb1 207 {
64c288fa 208#if wxUSE_IMAGE && (!defined(__WXMSW__) || wxUSE_WXDIB)
e53a95bc
RR
209 if ( size != wxDefaultSize &&
210 (bmp.GetWidth() != size.x || bmp.GetHeight() != size.y) )
3242feb1
VS
211 {
212 wxImage img = bmp.ConvertToImage();
e53a95bc 213 img.Rescale(size.x, size.y);
3242feb1
VS
214 bmp = wxBitmap(img);
215 }
4629016d 216#endif
2aca4a98 217 break;
3242feb1 218 }
2aca4a98 219 }
3242feb1 220
2aca4a98
VS
221 sm_cache->PutBitmap(hashId, bmp);
222 }
223
224 return bmp;
225}
226
57b0987b
VS
227/*static*/ wxIcon wxArtProvider::GetIcon(const wxArtID& id,
228 const wxArtClient& client,
2aca4a98
VS
229 const wxSize& size)
230{
231 wxCHECK_MSG( sm_providers, wxNullIcon, _T("no wxArtProvider exists") );
232
57b0987b 233 wxBitmap bmp = GetBitmap(id, client, size);
25dd6997 234 if ( !bmp.Ok() )
2aca4a98 235 return wxNullIcon;
2aca4a98 236
25dd6997
VZ
237 wxIcon icon;
238 icon.CopyFromBitmap(bmp);
239 return icon;
240}
2aca4a98 241
f6d43eda 242#if defined(__WXGTK20__) && !defined(__WXUNIVERSAL__)
abc736fd 243 #include "wx/gtk/private.h"
b737ad10 244 extern GtkIconSize wxArtClientToIconSize(const wxArtClient& client);
e53a95bc 245#endif // defined(__WXGTK20__) && !defined(__WXUNIVERSAL__)
b737ad10 246
e53a95bc 247/*static*/ wxSize wxArtProvider::GetSizeHint(const wxArtClient& client,
b737ad10
RR
248 bool platform_dependent)
249{
250 if (!platform_dependent)
251 {
252 wxArtProvidersList::compatibility_iterator node = sm_providers->GetFirst();
253 if (node)
e53a95bc
RR
254 return node->GetData()->DoGetSizeHint(client);
255 }
f6d43eda 256
b737ad10 257 // else return platform dependent size
f6d43eda
VZ
258
259#if defined(__WXGTK20__) && !defined(__WXUNIVERSAL__)
e53a95bc 260 // Gtk has specific sizes for each client, see artgtk.cpp
b737ad10 261 GtkIconSize gtk_size = wxArtClientToIconSize(client);
e53a95bc
RR
262 // no size hints for this client
263 if (gtk_size == GTK_ICON_SIZE_INVALID)
264 return wxDefaultSize;
b737ad10
RR
265 gint width, height;
266 gtk_icon_size_lookup( gtk_size, &width, &height);
267 return wxSize(width, height);
f6d43eda 268#else // !GTK+ 2
e53a95bc 269 // NB: These size hints may have to be adjusted per platform
b737ad10 270 if (client == wxART_TOOLBAR)
e53a95bc 271 return wxSize(16, 15);
b737ad10
RR
272 else if (client == wxART_MENU)
273 return wxSize(16, 15);
e53a95bc
RR
274 else if (client == wxART_FRAME_ICON)
275 return wxSize(16, 15);
b737ad10
RR
276 else if (client == wxART_CMN_DIALOG || client == wxART_MESSAGE_BOX)
277 return wxSize(32, 32);
e53a95bc
RR
278 else if (client == wxART_HELP_BROWSER)
279 return wxSize(16, 15);
b737ad10
RR
280 else if (client == wxART_BUTTON)
281 return wxSize(16, 15);
e53a95bc 282 else // wxART_OTHER or perhaps a user's client, no specified size
8ecff181 283 return wxDefaultSize;
f6d43eda 284#endif // GTK+ 2/else
b737ad10
RR
285}
286
571d2e0f
VZ
287// ----------------------------------------------------------------------------
288// deprecated wxArtProvider methods
289// ----------------------------------------------------------------------------
290
291#if WXWIN_COMPATIBILITY_2_6
292
293/* static */ void wxArtProvider::PushProvider(wxArtProvider *provider)
294{
295 Push(provider);
296}
297
298/* static */ void wxArtProvider::InsertProvider(wxArtProvider *provider)
299{
300 Insert(provider);
301}
302
303/* static */ bool wxArtProvider::PopProvider()
304{
305 return Pop();
306}
307
308/* static */ bool wxArtProvider::RemoveProvider(wxArtProvider *provider)
309{
310 // RemoveProvider() used to delete the provider being removed so this is
311 // not a typo, we must call Delete() and not Remove() here
312 return Delete(provider);
313}
314
315#endif // WXWIN_COMPATIBILITY_2_6
316
317// ============================================================================
318// wxArtProviderModule
319// ============================================================================
2aca4a98
VS
320
321class wxArtProviderModule: public wxModule
322{
323public:
2b5f62a0
VZ
324 bool OnInit()
325 {
326 wxArtProvider::InitStdProvider();
7bebedd8 327 wxArtProvider::InitNativeProvider();
4629016d 328 return true;
2b5f62a0
VZ
329 }
330 void OnExit()
331 {
332 wxArtProvider::CleanUpProviders();
333 }
2aca4a98
VS
334
335 DECLARE_DYNAMIC_CLASS(wxArtProviderModule)
336};
337
338IMPLEMENT_DYNAMIC_CLASS(wxArtProviderModule, wxModule)