]>
Commit | Line | Data |
---|---|---|
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" | |
38 | WX_DECLARE_LIST(wxArtProvider, wxArtProvidersList); | |
259c43f6 | 39 | WX_DEFINE_LIST(wxArtProvidersList) |
2aca4a98 VS |
40 | |
41 | // ---------------------------------------------------------------------------- | |
42 | // Cache class - stores already requested bitmaps | |
43 | // ---------------------------------------------------------------------------- | |
44 | ||
3f5c62f9 | 45 | WX_DECLARE_EXPORTED_STRING_HASH_MAP(wxBitmap, wxArtProviderBitmapsHash); |
52734360 | 46 | WX_DECLARE_EXPORTED_STRING_HASH_MAP(wxIconBundle, wxArtProviderIconBundlesHash); |
2aca4a98 VS |
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 | ||
52734360 VZ |
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 | ||
2aca4a98 | 59 | void Clear(); |
9e7ed2b0 | 60 | |
57b0987b VS |
61 | static wxString ConstructHashID(const wxArtID& id, |
62 | const wxArtClient& client, | |
2aca4a98 VS |
63 | const wxSize& size); |
64 | ||
52734360 VZ |
65 | static wxString ConstructHashID(const wxArtID& id, |
66 | const wxArtClient& client); | |
67 | ||
2aca4a98 | 68 | private: |
52734360 VZ |
69 | wxArtProviderBitmapsHash m_bitmapsHash; // cache of wxBitmaps |
70 | wxArtProviderIconBundlesHash m_iconBundlesHash; // cache of wxIconBundles | |
2aca4a98 VS |
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 | { | |
4629016d | 78 | return false; |
2aca4a98 VS |
79 | } |
80 | else | |
81 | { | |
82 | *bmp = entry->second; | |
4629016d | 83 | return true; |
2aca4a98 VS |
84 | } |
85 | } | |
86 | ||
52734360 VZ |
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 | ||
2aca4a98 VS |
101 | void wxArtProviderCache::Clear() |
102 | { | |
103 | m_bitmapsHash.clear(); | |
52734360 | 104 | m_iconBundlesHash.clear(); |
2aca4a98 VS |
105 | } |
106 | ||
52734360 VZ |
107 | /* static */ wxString |
108 | wxArtProviderCache::ConstructHashID(const wxArtID& id, | |
109 | const wxArtClient& client) | |
2aca4a98 | 110 | { |
9a83f860 | 111 | return id + wxT('-') + client; |
2aca4a98 VS |
112 | } |
113 | ||
114 | ||
52734360 VZ |
115 | /* static */ wxString |
116 | wxArtProviderCache::ConstructHashID(const wxArtID& id, | |
117 | const wxArtClient& client, | |
118 | const wxSize& size) | |
119 | { | |
9a83f860 VZ |
120 | return ConstructHashID(id, client) + wxT('-') + |
121 | wxString::Format(wxT("%d-%d"), size.x, size.y); | |
52734360 VZ |
122 | } |
123 | ||
571d2e0f | 124 | // ============================================================================ |
2aca4a98 | 125 | // wxArtProvider class |
571d2e0f | 126 | // ============================================================================ |
2aca4a98 VS |
127 | |
128 | IMPLEMENT_ABSTRACT_CLASS(wxArtProvider, wxObject) | |
129 | ||
130 | wxArtProvidersList *wxArtProvider::sm_providers = NULL; | |
131 | wxArtProviderCache *wxArtProvider::sm_cache = NULL; | |
132 | ||
571d2e0f VZ |
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 | ||
dd7d379e | 146 | /*static*/ void wxArtProvider::CommonAddingProvider() |
2aca4a98 VS |
147 | { |
148 | if ( !sm_providers ) | |
149 | { | |
150 | sm_providers = new wxArtProvidersList; | |
2aca4a98 VS |
151 | sm_cache = new wxArtProviderCache; |
152 | } | |
153 | ||
c3357a03 | 154 | sm_cache->Clear(); |
2aca4a98 VS |
155 | } |
156 | ||
571d2e0f | 157 | /*static*/ void wxArtProvider::Push(wxArtProvider *provider) |
dd7d379e VS |
158 | { |
159 | CommonAddingProvider(); | |
160 | sm_providers->Insert(provider); | |
161 | } | |
162 | ||
14440cc6 | 163 | /*static*/ void wxArtProvider::PushBack(wxArtProvider *provider) |
dd7d379e VS |
164 | { |
165 | CommonAddingProvider(); | |
166 | sm_providers->Append(provider); | |
167 | } | |
168 | ||
571d2e0f | 169 | /*static*/ bool wxArtProvider::Pop() |
2aca4a98 | 170 | { |
9a83f860 VZ |
171 | wxCHECK_MSG( sm_providers, false, wxT("no wxArtProvider exists") ); |
172 | wxCHECK_MSG( !sm_providers->empty(), false, wxT("wxArtProviders stack is empty") ); | |
2aca4a98 | 173 | |
222ed1d6 | 174 | delete sm_providers->GetFirst()->GetData(); |
2aca4a98 | 175 | sm_cache->Clear(); |
4629016d | 176 | return true; |
2aca4a98 VS |
177 | } |
178 | ||
571d2e0f | 179 | /*static*/ bool wxArtProvider::Remove(wxArtProvider *provider) |
2aca4a98 | 180 | { |
9a83f860 | 181 | wxCHECK_MSG( sm_providers, false, wxT("no wxArtProvider exists") ); |
2aca4a98 VS |
182 | |
183 | if ( sm_providers->DeleteObject(provider) ) | |
184 | { | |
185 | sm_cache->Clear(); | |
4629016d | 186 | return true; |
2aca4a98 | 187 | } |
9e7ed2b0 | 188 | |
4629016d | 189 | return false; |
2aca4a98 VS |
190 | } |
191 | ||
571d2e0f VZ |
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 | ||
2aca4a98 VS |
200 | /*static*/ void wxArtProvider::CleanUpProviders() |
201 | { | |
571d2e0f VZ |
202 | if ( sm_providers ) |
203 | { | |
204 | while ( !sm_providers->empty() ) | |
205 | delete *sm_providers->begin(); | |
206 | ||
207 | delete sm_providers; | |
208 | sm_providers = NULL; | |
209 | ||
210 | delete sm_cache; | |
211 | sm_cache = NULL; | |
212 | } | |
2aca4a98 VS |
213 | } |
214 | ||
571d2e0f VZ |
215 | // ---------------------------------------------------------------------------- |
216 | // wxArtProvider: retrieving bitmaps/icons | |
217 | // ---------------------------------------------------------------------------- | |
218 | ||
57b0987b VS |
219 | /*static*/ wxBitmap wxArtProvider::GetBitmap(const wxArtID& id, |
220 | const wxArtClient& client, | |
e53a95bc | 221 | const wxSize& size) |
2aca4a98 | 222 | { |
57b0987b | 223 | // safety-check against writing client,id,size instead of id,client,size: |
9a83f860 | 224 | wxASSERT_MSG( client.Last() == wxT('C'), wxT("invalid 'client' parameter") ); |
57b0987b | 225 | |
9a83f860 | 226 | wxCHECK_MSG( sm_providers, wxNullBitmap, wxT("no wxArtProvider exists") ); |
2aca4a98 | 227 | |
e53a95bc | 228 | wxString hashId = wxArtProviderCache::ConstructHashID(id, client, size); |
2aca4a98 VS |
229 | |
230 | wxBitmap bmp; | |
231 | if ( !sm_cache->GetBitmap(hashId, &bmp) ) | |
232 | { | |
222ed1d6 | 233 | for (wxArtProvidersList::compatibility_iterator node = sm_providers->GetFirst(); |
2aca4a98 VS |
234 | node; node = node->GetNext()) |
235 | { | |
e53a95bc | 236 | bmp = node->GetData()->CreateBitmap(id, client, size); |
2aca4a98 | 237 | if ( bmp.Ok() ) |
b5c2a334 VS |
238 | break; |
239 | } | |
240 | ||
1d3dfc57 | 241 | wxSize sizeNeeded = size; |
b5c2a334 VS |
242 | if ( !bmp.Ok() ) |
243 | { | |
244 | // no bitmap created -- as a fallback, try if we can find desired | |
245 | // icon in a bundle | |
246 | wxIconBundle iconBundle = DoGetIconBundle(id, client); | |
247 | if ( iconBundle.IsOk() ) | |
3242feb1 | 248 | { |
1d3dfc57 VZ |
249 | if ( sizeNeeded == wxDefaultSize ) |
250 | sizeNeeded = GetNativeSizeHint(client); | |
251 | ||
252 | wxIcon icon(iconBundle.GetIcon(sizeNeeded)); | |
b5c2a334 | 253 | if ( icon.IsOk() ) |
1d3dfc57 VZ |
254 | { |
255 | // this icon may be not of the correct size, it will be | |
256 | // rescaled below in such case | |
b5c2a334 | 257 | bmp.CopyFromIcon(icon); |
1d3dfc57 | 258 | } |
b5c2a334 VS |
259 | } |
260 | } | |
261 | ||
1d3dfc57 | 262 | // if we didn't get the correct size, resize the bitmap |
64c288fa | 263 | #if wxUSE_IMAGE && (!defined(__WXMSW__) || wxUSE_WXDIB) |
1d3dfc57 VZ |
264 | if ( bmp.IsOk() && sizeNeeded != wxDefaultSize ) |
265 | { | |
266 | if ( bmp.GetSize() != sizeNeeded ) | |
b5c2a334 VS |
267 | { |
268 | wxImage img = bmp.ConvertToImage(); | |
1d3dfc57 | 269 | img.Rescale(sizeNeeded.x, sizeNeeded.y); |
b5c2a334 | 270 | bmp = wxBitmap(img); |
3242feb1 | 271 | } |
2aca4a98 | 272 | } |
1d3dfc57 | 273 | #endif // wxUSE_IMAGE |
b5c2a334 | 274 | |
2aca4a98 | 275 | sm_cache->PutBitmap(hashId, bmp); |
b5c2a334 | 276 | } |
2aca4a98 VS |
277 | |
278 | return bmp; | |
279 | } | |
280 | ||
b5c2a334 VS |
281 | /*static*/ |
282 | wxIconBundle wxArtProvider::GetIconBundle(const wxArtID& id, const wxArtClient& client) | |
283 | { | |
284 | wxIconBundle iconbundle(DoGetIconBundle(id, client)); | |
285 | ||
286 | if ( iconbundle.IsOk() ) | |
287 | { | |
288 | return iconbundle; | |
289 | } | |
290 | else | |
291 | { | |
292 | // fall back to single-icon bundle | |
293 | return wxIconBundle(GetIcon(id, client)); | |
294 | } | |
295 | } | |
296 | ||
297 | /*static*/ | |
298 | wxIconBundle wxArtProvider::DoGetIconBundle(const wxArtID& id, const wxArtClient& client) | |
52734360 VZ |
299 | { |
300 | // safety-check against writing client,id,size instead of id,client,size: | |
9a83f860 | 301 | wxASSERT_MSG( client.Last() == wxT('C'), wxT("invalid 'client' parameter") ); |
52734360 | 302 | |
9a83f860 | 303 | wxCHECK_MSG( sm_providers, wxNullIconBundle, wxT("no wxArtProvider exists") ); |
52734360 VZ |
304 | |
305 | wxString hashId = wxArtProviderCache::ConstructHashID(id, client); | |
306 | ||
307 | wxIconBundle iconbundle; | |
308 | if ( !sm_cache->GetIconBundle(hashId, &iconbundle) ) | |
309 | { | |
310 | for (wxArtProvidersList::compatibility_iterator node = sm_providers->GetFirst(); | |
311 | node; node = node->GetNext()) | |
312 | { | |
313 | iconbundle = node->GetData()->CreateIconBundle(id, client); | |
314 | if ( iconbundle.IsOk() ) | |
315 | break; | |
316 | } | |
317 | ||
318 | sm_cache->PutIconBundle(hashId, iconbundle); | |
319 | } | |
320 | ||
321 | return iconbundle; | |
322 | } | |
323 | ||
57b0987b VS |
324 | /*static*/ wxIcon wxArtProvider::GetIcon(const wxArtID& id, |
325 | const wxArtClient& client, | |
2aca4a98 VS |
326 | const wxSize& size) |
327 | { | |
57b0987b | 328 | wxBitmap bmp = GetBitmap(id, client, size); |
b5c2a334 VS |
329 | |
330 | if ( !bmp.IsOk() ) | |
2aca4a98 | 331 | return wxNullIcon; |
2aca4a98 | 332 | |
25dd6997 VZ |
333 | wxIcon icon; |
334 | icon.CopyFromBitmap(bmp); | |
335 | return icon; | |
336 | } | |
2aca4a98 | 337 | |
1970409e | 338 | /* static */ |
39f6b6ba | 339 | wxArtID wxArtProvider::GetMessageBoxIconId(int flags) |
1970409e | 340 | { |
1970409e VZ |
341 | switch ( flags & wxICON_MASK ) |
342 | { | |
343 | default: | |
9a83f860 | 344 | wxFAIL_MSG(wxT("incorrect message box icon flags")); |
1970409e VZ |
345 | // fall through |
346 | ||
347 | case wxICON_ERROR: | |
39f6b6ba | 348 | return wxART_ERROR; |
1970409e VZ |
349 | |
350 | case wxICON_INFORMATION: | |
39f6b6ba | 351 | return wxART_INFORMATION; |
1970409e VZ |
352 | |
353 | case wxICON_WARNING: | |
39f6b6ba | 354 | return wxART_WARNING; |
1970409e VZ |
355 | |
356 | case wxICON_QUESTION: | |
39f6b6ba | 357 | return wxART_QUESTION; |
1970409e | 358 | } |
1970409e VZ |
359 | } |
360 | ||
e53a95bc | 361 | /*static*/ wxSize wxArtProvider::GetSizeHint(const wxArtClient& client, |
b737ad10 RR |
362 | bool platform_dependent) |
363 | { | |
364 | if (!platform_dependent) | |
365 | { | |
366 | wxArtProvidersList::compatibility_iterator node = sm_providers->GetFirst(); | |
367 | if (node) | |
e53a95bc RR |
368 | return node->GetData()->DoGetSizeHint(client); |
369 | } | |
f6d43eda | 370 | |
a158acac VS |
371 | return GetNativeSizeHint(client); |
372 | } | |
373 | ||
374 | #ifndef wxHAS_NATIVE_ART_PROVIDER_IMPL | |
375 | /*static*/ | |
6f23f80b | 376 | wxSize wxArtProvider::GetNativeSizeHint(const wxArtClient& WXUNUSED(client)) |
a158acac VS |
377 | { |
378 | // rather than returning some arbitrary value that doesn't make much | |
379 | // sense (as 2.8 used to do), tell the caller that we don't have a clue: | |
380 | return wxDefaultSize; | |
381 | } | |
382 | ||
383 | /*static*/ | |
384 | void wxArtProvider::InitNativeProvider() | |
385 | { | |
b737ad10 | 386 | } |
a158acac VS |
387 | #endif // !wxHAS_NATIVE_ART_PROVIDER_IMPL |
388 | ||
b737ad10 | 389 | |
14440cc6 VS |
390 | /* static */ |
391 | bool wxArtProvider::HasNativeProvider() | |
392 | { | |
393 | #ifdef __WXGTK20__ | |
394 | return true; | |
395 | #else | |
396 | return false; | |
397 | #endif | |
398 | } | |
399 | ||
571d2e0f VZ |
400 | // ---------------------------------------------------------------------------- |
401 | // deprecated wxArtProvider methods | |
402 | // ---------------------------------------------------------------------------- | |
403 | ||
404 | #if WXWIN_COMPATIBILITY_2_6 | |
405 | ||
406 | /* static */ void wxArtProvider::PushProvider(wxArtProvider *provider) | |
407 | { | |
408 | Push(provider); | |
409 | } | |
410 | ||
411 | /* static */ void wxArtProvider::InsertProvider(wxArtProvider *provider) | |
412 | { | |
413 | Insert(provider); | |
414 | } | |
415 | ||
416 | /* static */ bool wxArtProvider::PopProvider() | |
417 | { | |
418 | return Pop(); | |
419 | } | |
420 | ||
421 | /* static */ bool wxArtProvider::RemoveProvider(wxArtProvider *provider) | |
422 | { | |
423 | // RemoveProvider() used to delete the provider being removed so this is | |
424 | // not a typo, we must call Delete() and not Remove() here | |
425 | return Delete(provider); | |
426 | } | |
427 | ||
428 | #endif // WXWIN_COMPATIBILITY_2_6 | |
429 | ||
14440cc6 VS |
430 | #if WXWIN_COMPATIBILITY_2_8 |
431 | /* static */ void wxArtProvider::Insert(wxArtProvider *provider) | |
432 | { | |
433 | PushBack(provider); | |
434 | } | |
435 | #endif // WXWIN_COMPATIBILITY_2_8 | |
436 | ||
571d2e0f VZ |
437 | // ============================================================================ |
438 | // wxArtProviderModule | |
439 | // ============================================================================ | |
2aca4a98 VS |
440 | |
441 | class wxArtProviderModule: public wxModule | |
442 | { | |
443 | public: | |
2b5f62a0 VZ |
444 | bool OnInit() |
445 | { | |
446 | wxArtProvider::InitStdProvider(); | |
7bebedd8 | 447 | wxArtProvider::InitNativeProvider(); |
4629016d | 448 | return true; |
2b5f62a0 VZ |
449 | } |
450 | void OnExit() | |
451 | { | |
452 | wxArtProvider::CleanUpProviders(); | |
453 | } | |
2aca4a98 VS |
454 | |
455 | DECLARE_DYNAMIC_CLASS(wxArtProviderModule) | |
456 | }; | |
457 | ||
458 | IMPLEMENT_DYNAMIC_CLASS(wxArtProviderModule, wxModule) |