]> git.saurik.com Git - wxWidgets.git/blob - src/common/artprov.cpp
Added more conveniently placed 'List of All Members' link on all class doc pages...
[wxWidgets.git] / src / common / artprov.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/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
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ---------------------------------------------------------------------------
13 // headers
14 // ---------------------------------------------------------------------------
15
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
23 #include "wx/artprov.h"
24
25 #ifndef WX_PRECOMP
26 #include "wx/list.h"
27 #include "wx/log.h"
28 #include "wx/hashmap.h"
29 #include "wx/image.h"
30 #include "wx/module.h"
31 #endif
32
33 // ===========================================================================
34 // implementation
35 // ===========================================================================
36
37 #include "wx/listimpl.cpp"
38 WX_DECLARE_LIST(wxArtProvider, wxArtProvidersList);
39 WX_DEFINE_LIST(wxArtProvidersList)
40
41 // ----------------------------------------------------------------------------
42 // Cache class - stores already requested bitmaps
43 // ----------------------------------------------------------------------------
44
45 WX_DECLARE_EXPORTED_STRING_HASH_MAP(wxBitmap, wxArtProviderBitmapsHash);
46 WX_DECLARE_EXPORTED_STRING_HASH_MAP(wxIconBundle, wxArtProviderIconBundlesHash);
47
48 class WXDLLEXPORT wxArtProviderCache
49 {
50 public:
51 bool GetBitmap(const wxString& full_id, wxBitmap* bmp);
52 void PutBitmap(const wxString& full_id, const wxBitmap& bmp)
53 { m_bitmapsHash[full_id] = bmp; }
54
55 bool GetIconBundle(const wxString& full_id, wxIconBundle* bmp);
56 void PutIconBundle(const wxString& full_id, const wxIconBundle& iconbundle)
57 { m_iconBundlesHash[full_id] = iconbundle; }
58
59 void Clear();
60
61 static wxString ConstructHashID(const wxArtID& id,
62 const wxArtClient& client,
63 const wxSize& size);
64
65 static wxString ConstructHashID(const wxArtID& id,
66 const wxArtClient& client);
67
68 private:
69 wxArtProviderBitmapsHash m_bitmapsHash; // cache of wxBitmaps
70 wxArtProviderIconBundlesHash m_iconBundlesHash; // cache of wxIconBundles
71 };
72
73 bool wxArtProviderCache::GetBitmap(const wxString& full_id, wxBitmap* bmp)
74 {
75 wxArtProviderBitmapsHash::iterator entry = m_bitmapsHash.find(full_id);
76 if ( entry == m_bitmapsHash.end() )
77 {
78 return false;
79 }
80 else
81 {
82 *bmp = entry->second;
83 return true;
84 }
85 }
86
87 bool wxArtProviderCache::GetIconBundle(const wxString& full_id, wxIconBundle* bmp)
88 {
89 wxArtProviderIconBundlesHash::iterator entry = m_iconBundlesHash.find(full_id);
90 if ( entry == m_iconBundlesHash.end() )
91 {
92 return false;
93 }
94 else
95 {
96 *bmp = entry->second;
97 return true;
98 }
99 }
100
101 void wxArtProviderCache::Clear()
102 {
103 m_bitmapsHash.clear();
104 m_iconBundlesHash.clear();
105 }
106
107 /* static */ wxString
108 wxArtProviderCache::ConstructHashID(const wxArtID& id,
109 const wxArtClient& client)
110 {
111 return id + wxT('-') + client;
112 }
113
114
115 /* static */ wxString
116 wxArtProviderCache::ConstructHashID(const wxArtID& id,
117 const wxArtClient& client,
118 const wxSize& size)
119 {
120 return ConstructHashID(id, client) + wxT('-') +
121 wxString::Format(wxT("%d-%d"), size.x, size.y);
122 }
123
124 // ============================================================================
125 // wxArtProvider class
126 // ============================================================================
127
128 IMPLEMENT_ABSTRACT_CLASS(wxArtProvider, wxObject)
129
130 wxArtProvidersList *wxArtProvider::sm_providers = NULL;
131 wxArtProviderCache *wxArtProvider::sm_cache = NULL;
132
133 // ----------------------------------------------------------------------------
134 // wxArtProvider ctors/dtor
135 // ----------------------------------------------------------------------------
136
137 wxArtProvider::~wxArtProvider()
138 {
139 Remove(this);
140 }
141
142 // ----------------------------------------------------------------------------
143 // wxArtProvider operations on provider stack
144 // ----------------------------------------------------------------------------
145
146 /*static*/ void wxArtProvider::CommonAddingProvider()
147 {
148 if ( !sm_providers )
149 {
150 sm_providers = new wxArtProvidersList;
151 sm_cache = new wxArtProviderCache;
152 }
153
154 sm_cache->Clear();
155 }
156
157 /*static*/ void wxArtProvider::Push(wxArtProvider *provider)
158 {
159 CommonAddingProvider();
160 sm_providers->Insert(provider);
161 }
162
163 /*static*/ void wxArtProvider::PushBack(wxArtProvider *provider)
164 {
165 CommonAddingProvider();
166 sm_providers->Append(provider);
167 }
168
169 /*static*/ bool wxArtProvider::Pop()
170 {
171 wxCHECK_MSG( sm_providers, false, wxT("no wxArtProvider exists") );
172 wxCHECK_MSG( !sm_providers->empty(), false, wxT("wxArtProviders stack is empty") );
173
174 delete sm_providers->GetFirst()->GetData();
175 sm_cache->Clear();
176 return true;
177 }
178
179 /*static*/ bool wxArtProvider::Remove(wxArtProvider *provider)
180 {
181 wxCHECK_MSG( sm_providers, false, wxT("no wxArtProvider exists") );
182
183 if ( sm_providers->DeleteObject(provider) )
184 {
185 sm_cache->Clear();
186 return true;
187 }
188
189 return false;
190 }
191
192 /*static*/ bool wxArtProvider::Delete(wxArtProvider *provider)
193 {
194 // provider will remove itself from the stack in its dtor
195 delete provider;
196
197 return true;
198 }
199
200 /*static*/ void wxArtProvider::CleanUpProviders()
201 {
202 if ( sm_providers )
203 {
204 while ( !sm_providers->empty() )
205 delete *sm_providers->begin();
206
207 wxDELETE(sm_providers);
208 wxDELETE(sm_cache);
209 }
210 }
211
212 // ----------------------------------------------------------------------------
213 // wxArtProvider: retrieving bitmaps/icons
214 // ----------------------------------------------------------------------------
215
216 /*static*/ wxBitmap wxArtProvider::GetBitmap(const wxArtID& id,
217 const wxArtClient& client,
218 const wxSize& size)
219 {
220 // safety-check against writing client,id,size instead of id,client,size:
221 wxASSERT_MSG( client.Last() == wxT('C'), wxT("invalid 'client' parameter") );
222
223 wxCHECK_MSG( sm_providers, wxNullBitmap, wxT("no wxArtProvider exists") );
224
225 wxString hashId = wxArtProviderCache::ConstructHashID(id, client, size);
226
227 wxBitmap bmp;
228 if ( !sm_cache->GetBitmap(hashId, &bmp) )
229 {
230 for (wxArtProvidersList::compatibility_iterator node = sm_providers->GetFirst();
231 node; node = node->GetNext())
232 {
233 bmp = node->GetData()->CreateBitmap(id, client, size);
234 if ( bmp.IsOk() )
235 break;
236 }
237
238 wxSize sizeNeeded = size;
239 if ( !bmp.IsOk() )
240 {
241 // no bitmap created -- as a fallback, try if we can find desired
242 // icon in a bundle
243 wxIconBundle iconBundle = DoGetIconBundle(id, client);
244 if ( iconBundle.IsOk() )
245 {
246 if ( sizeNeeded == wxDefaultSize )
247 sizeNeeded = GetNativeSizeHint(client);
248
249 wxIcon icon(iconBundle.GetIcon(sizeNeeded));
250 if ( icon.IsOk() )
251 {
252 // this icon may be not of the correct size, it will be
253 // rescaled below in such case
254 bmp.CopyFromIcon(icon);
255 }
256 }
257 }
258
259 // if we didn't get the correct size, resize the bitmap
260 #if wxUSE_IMAGE && (!defined(__WXMSW__) || wxUSE_WXDIB)
261 if ( bmp.IsOk() && sizeNeeded != wxDefaultSize )
262 {
263 if ( bmp.GetSize() != sizeNeeded )
264 {
265 wxImage img = bmp.ConvertToImage();
266 img.Rescale(sizeNeeded.x, sizeNeeded.y);
267 bmp = wxBitmap(img);
268 }
269 }
270 #endif // wxUSE_IMAGE
271
272 sm_cache->PutBitmap(hashId, bmp);
273 }
274
275 return bmp;
276 }
277
278 /*static*/
279 wxIconBundle wxArtProvider::GetIconBundle(const wxArtID& id, const wxArtClient& client)
280 {
281 wxIconBundle iconbundle(DoGetIconBundle(id, client));
282
283 if ( iconbundle.IsOk() )
284 {
285 return iconbundle;
286 }
287 else
288 {
289 // fall back to single-icon bundle
290 return wxIconBundle(GetIcon(id, client));
291 }
292 }
293
294 /*static*/
295 wxIconBundle wxArtProvider::DoGetIconBundle(const wxArtID& id, const wxArtClient& client)
296 {
297 // safety-check against writing client,id,size instead of id,client,size:
298 wxASSERT_MSG( client.Last() == wxT('C'), wxT("invalid 'client' parameter") );
299
300 wxCHECK_MSG( sm_providers, wxNullIconBundle, wxT("no wxArtProvider exists") );
301
302 wxString hashId = wxArtProviderCache::ConstructHashID(id, client);
303
304 wxIconBundle iconbundle;
305 if ( !sm_cache->GetIconBundle(hashId, &iconbundle) )
306 {
307 for (wxArtProvidersList::compatibility_iterator node = sm_providers->GetFirst();
308 node; node = node->GetNext())
309 {
310 iconbundle = node->GetData()->CreateIconBundle(id, client);
311 if ( iconbundle.IsOk() )
312 break;
313 }
314
315 sm_cache->PutIconBundle(hashId, iconbundle);
316 }
317
318 return iconbundle;
319 }
320
321 /*static*/ wxIcon wxArtProvider::GetIcon(const wxArtID& id,
322 const wxArtClient& client,
323 const wxSize& size)
324 {
325 wxBitmap bmp = GetBitmap(id, client, size);
326
327 if ( !bmp.IsOk() )
328 return wxNullIcon;
329
330 wxIcon icon;
331 icon.CopyFromBitmap(bmp);
332 return icon;
333 }
334
335 /* static */
336 wxArtID wxArtProvider::GetMessageBoxIconId(int flags)
337 {
338 switch ( flags & wxICON_MASK )
339 {
340 default:
341 wxFAIL_MSG(wxT("incorrect message box icon flags"));
342 // fall through
343
344 case wxICON_ERROR:
345 return wxART_ERROR;
346
347 case wxICON_INFORMATION:
348 return wxART_INFORMATION;
349
350 case wxICON_WARNING:
351 return wxART_WARNING;
352
353 case wxICON_QUESTION:
354 return wxART_QUESTION;
355 }
356 }
357
358 /*static*/ wxSize wxArtProvider::GetSizeHint(const wxArtClient& client,
359 bool platform_dependent)
360 {
361 if (!platform_dependent)
362 {
363 wxArtProvidersList::compatibility_iterator node = sm_providers->GetFirst();
364 if (node)
365 return node->GetData()->DoGetSizeHint(client);
366 }
367
368 return GetNativeSizeHint(client);
369 }
370
371 #ifndef wxHAS_NATIVE_ART_PROVIDER_IMPL
372 /*static*/
373 wxSize wxArtProvider::GetNativeSizeHint(const wxArtClient& WXUNUSED(client))
374 {
375 // rather than returning some arbitrary value that doesn't make much
376 // sense (as 2.8 used to do), tell the caller that we don't have a clue:
377 return wxDefaultSize;
378 }
379
380 /*static*/
381 void wxArtProvider::InitNativeProvider()
382 {
383 }
384 #endif // !wxHAS_NATIVE_ART_PROVIDER_IMPL
385
386
387 /* static */
388 bool wxArtProvider::HasNativeProvider()
389 {
390 #ifdef __WXGTK20__
391 return true;
392 #else
393 return false;
394 #endif
395 }
396
397 // ----------------------------------------------------------------------------
398 // deprecated wxArtProvider methods
399 // ----------------------------------------------------------------------------
400
401 #if WXWIN_COMPATIBILITY_2_6
402
403 /* static */ void wxArtProvider::PushProvider(wxArtProvider *provider)
404 {
405 Push(provider);
406 }
407
408 /* static */ void wxArtProvider::InsertProvider(wxArtProvider *provider)
409 {
410 PushBack(provider);
411 }
412
413 /* static */ bool wxArtProvider::PopProvider()
414 {
415 return Pop();
416 }
417
418 /* static */ bool wxArtProvider::RemoveProvider(wxArtProvider *provider)
419 {
420 // RemoveProvider() used to delete the provider being removed so this is
421 // not a typo, we must call Delete() and not Remove() here
422 return Delete(provider);
423 }
424
425 #endif // WXWIN_COMPATIBILITY_2_6
426
427 #if WXWIN_COMPATIBILITY_2_8
428 /* static */ void wxArtProvider::Insert(wxArtProvider *provider)
429 {
430 PushBack(provider);
431 }
432 #endif // WXWIN_COMPATIBILITY_2_8
433
434 // ============================================================================
435 // wxArtProviderModule
436 // ============================================================================
437
438 class wxArtProviderModule: public wxModule
439 {
440 public:
441 bool OnInit()
442 {
443 // The order here is such that the native provider will be used first
444 // and the standard one last as all these default providers add
445 // themselves to the bottom of the stack.
446 wxArtProvider::InitNativeProvider();
447 #if wxUSE_ARTPROVIDER_TANGO
448 wxArtProvider::InitTangoProvider();
449 #endif // wxUSE_ARTPROVIDER_TANGO
450 #if wxUSE_ARTPROVIDER_STD
451 wxArtProvider::InitStdProvider();
452 #endif // wxUSE_ARTPROVIDER_STD
453 return true;
454 }
455 void OnExit()
456 {
457 wxArtProvider::CleanUpProviders();
458 }
459
460 DECLARE_DYNAMIC_CLASS(wxArtProviderModule)
461 };
462
463 IMPLEMENT_DYNAMIC_CLASS(wxArtProviderModule, wxModule)