]>
Commit | Line | Data |
---|---|---|
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 + _T('-') + 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) + _T('-') + | |
121 | wxString::Format(_T("%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::Insert(wxArtProvider *provider) | |
164 | { | |
165 | CommonAddingProvider(); | |
166 | sm_providers->Append(provider); | |
167 | } | |
168 | ||
169 | /*static*/ bool wxArtProvider::Pop() | |
170 | { | |
171 | wxCHECK_MSG( sm_providers, false, _T("no wxArtProvider exists") ); | |
172 | wxCHECK_MSG( !sm_providers->empty(), false, _T("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, _T("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 | delete sm_providers; | |
208 | sm_providers = NULL; | |
209 | ||
210 | delete sm_cache; | |
211 | sm_cache = NULL; | |
212 | } | |
213 | } | |
214 | ||
215 | // ---------------------------------------------------------------------------- | |
216 | // wxArtProvider: retrieving bitmaps/icons | |
217 | // ---------------------------------------------------------------------------- | |
218 | ||
219 | /*static*/ wxBitmap wxArtProvider::GetBitmap(const wxArtID& id, | |
220 | const wxArtClient& client, | |
221 | const wxSize& size) | |
222 | { | |
223 | // safety-check against writing client,id,size instead of id,client,size: | |
224 | wxASSERT_MSG( client.Last() == _T('C'), _T("invalid 'client' parameter") ); | |
225 | ||
226 | wxCHECK_MSG( sm_providers, wxNullBitmap, _T("no wxArtProvider exists") ); | |
227 | ||
228 | wxString hashId = wxArtProviderCache::ConstructHashID(id, client, size); | |
229 | ||
230 | wxBitmap bmp; | |
231 | if ( !sm_cache->GetBitmap(hashId, &bmp) ) | |
232 | { | |
233 | for (wxArtProvidersList::compatibility_iterator node = sm_providers->GetFirst(); | |
234 | node; node = node->GetNext()) | |
235 | { | |
236 | bmp = node->GetData()->CreateBitmap(id, client, size); | |
237 | if ( bmp.Ok() ) | |
238 | { | |
239 | #if wxUSE_IMAGE && (!defined(__WXMSW__) || wxUSE_WXDIB) | |
240 | if ( size != wxDefaultSize && | |
241 | (bmp.GetWidth() != size.x || bmp.GetHeight() != size.y) ) | |
242 | { | |
243 | wxImage img = bmp.ConvertToImage(); | |
244 | img.Rescale(size.x, size.y); | |
245 | bmp = wxBitmap(img); | |
246 | } | |
247 | #endif | |
248 | break; | |
249 | } | |
250 | // We could try the IconBundles here and convert what we find | |
251 | // to a bitmap. | |
252 | } | |
253 | sm_cache->PutBitmap(hashId, bmp); | |
254 | } | |
255 | ||
256 | return bmp; | |
257 | } | |
258 | ||
259 | /*static*/ wxIconBundle wxArtProvider::GetIconBundle(const wxArtID& id, const wxArtClient& client) | |
260 | { | |
261 | // safety-check against writing client,id,size instead of id,client,size: | |
262 | wxASSERT_MSG( client.Last() == _T('C'), _T("invalid 'client' parameter") ); | |
263 | ||
264 | wxCHECK_MSG( sm_providers, wxNullIconBundle, _T("no wxArtProvider exists") ); | |
265 | ||
266 | wxString hashId = wxArtProviderCache::ConstructHashID(id, client); | |
267 | ||
268 | wxIconBundle iconbundle; | |
269 | if ( !sm_cache->GetIconBundle(hashId, &iconbundle) ) | |
270 | { | |
271 | for (wxArtProvidersList::compatibility_iterator node = sm_providers->GetFirst(); | |
272 | node; node = node->GetNext()) | |
273 | { | |
274 | iconbundle = node->GetData()->CreateIconBundle(id, client); | |
275 | if ( iconbundle.IsOk() ) | |
276 | break; | |
277 | } | |
278 | ||
279 | sm_cache->PutIconBundle(hashId, iconbundle); | |
280 | } | |
281 | ||
282 | return iconbundle; | |
283 | } | |
284 | ||
285 | /*static*/ wxIcon wxArtProvider::GetIcon(const wxArtID& id, | |
286 | const wxArtClient& client, | |
287 | const wxSize& size) | |
288 | { | |
289 | wxCHECK_MSG( sm_providers, wxNullIcon, _T("no wxArtProvider exists") ); | |
290 | ||
291 | // First look for an appropriate icon bundle - this will give us the best icon | |
292 | wxIconBundle iconBundle = GetIconBundle(id, client); | |
293 | if ( iconBundle.IsOk() ) | |
294 | return iconBundle.GetIcon(size); | |
295 | ||
296 | // If there is no icon bundle then look for a bitmap | |
297 | wxBitmap bmp = GetBitmap(id, client, size); | |
298 | if ( !bmp.Ok() ) | |
299 | return wxNullIcon; | |
300 | ||
301 | wxIcon icon; | |
302 | icon.CopyFromBitmap(bmp); | |
303 | return icon; | |
304 | } | |
305 | ||
306 | #if defined(__WXGTK20__) && !defined(__WXUNIVERSAL__) | |
307 | #include "wx/gtk/private.h" | |
308 | extern GtkIconSize wxArtClientToIconSize(const wxArtClient& client); | |
309 | #endif // defined(__WXGTK20__) && !defined(__WXUNIVERSAL__) | |
310 | ||
311 | /*static*/ wxSize wxArtProvider::GetSizeHint(const wxArtClient& client, | |
312 | bool platform_dependent) | |
313 | { | |
314 | if (!platform_dependent) | |
315 | { | |
316 | wxArtProvidersList::compatibility_iterator node = sm_providers->GetFirst(); | |
317 | if (node) | |
318 | return node->GetData()->DoGetSizeHint(client); | |
319 | } | |
320 | ||
321 | // else return platform dependent size | |
322 | ||
323 | #if defined(__WXGTK20__) && !defined(__WXUNIVERSAL__) | |
324 | // Gtk has specific sizes for each client, see artgtk.cpp | |
325 | GtkIconSize gtk_size = wxArtClientToIconSize(client); | |
326 | // no size hints for this client | |
327 | if (gtk_size == GTK_ICON_SIZE_INVALID) | |
328 | return wxDefaultSize; | |
329 | gint width, height; | |
330 | gtk_icon_size_lookup( gtk_size, &width, &height); | |
331 | return wxSize(width, height); | |
332 | #else // !GTK+ 2 | |
333 | // NB: These size hints may have to be adjusted per platform | |
334 | if (client == wxART_TOOLBAR) | |
335 | return wxSize(16, 15); | |
336 | else if (client == wxART_MENU) | |
337 | return wxSize(16, 15); | |
338 | else if (client == wxART_FRAME_ICON) | |
339 | return wxSize(16, 15); | |
340 | else if (client == wxART_CMN_DIALOG || client == wxART_MESSAGE_BOX) | |
341 | return wxSize(32, 32); | |
342 | else if (client == wxART_HELP_BROWSER) | |
343 | return wxSize(16, 15); | |
344 | else if (client == wxART_BUTTON) | |
345 | return wxSize(16, 15); | |
346 | else // wxART_OTHER or perhaps a user's client, no specified size | |
347 | return wxDefaultSize; | |
348 | #endif // GTK+ 2/else | |
349 | } | |
350 | ||
351 | // ---------------------------------------------------------------------------- | |
352 | // deprecated wxArtProvider methods | |
353 | // ---------------------------------------------------------------------------- | |
354 | ||
355 | #if WXWIN_COMPATIBILITY_2_6 | |
356 | ||
357 | /* static */ void wxArtProvider::PushProvider(wxArtProvider *provider) | |
358 | { | |
359 | Push(provider); | |
360 | } | |
361 | ||
362 | /* static */ void wxArtProvider::InsertProvider(wxArtProvider *provider) | |
363 | { | |
364 | Insert(provider); | |
365 | } | |
366 | ||
367 | /* static */ bool wxArtProvider::PopProvider() | |
368 | { | |
369 | return Pop(); | |
370 | } | |
371 | ||
372 | /* static */ bool wxArtProvider::RemoveProvider(wxArtProvider *provider) | |
373 | { | |
374 | // RemoveProvider() used to delete the provider being removed so this is | |
375 | // not a typo, we must call Delete() and not Remove() here | |
376 | return Delete(provider); | |
377 | } | |
378 | ||
379 | #endif // WXWIN_COMPATIBILITY_2_6 | |
380 | ||
381 | // ============================================================================ | |
382 | // wxArtProviderModule | |
383 | // ============================================================================ | |
384 | ||
385 | class wxArtProviderModule: public wxModule | |
386 | { | |
387 | public: | |
388 | bool OnInit() | |
389 | { | |
390 | wxArtProvider::InitStdProvider(); | |
391 | wxArtProvider::InitNativeProvider(); | |
392 | return true; | |
393 | } | |
394 | void OnExit() | |
395 | { | |
396 | wxArtProvider::CleanUpProviders(); | |
397 | } | |
398 | ||
399 | DECLARE_DYNAMIC_CLASS(wxArtProviderModule) | |
400 | }; | |
401 | ||
402 | IMPLEMENT_DYNAMIC_CLASS(wxArtProviderModule, wxModule) |