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