]> git.saurik.com Git - wxWidgets.git/blame - src/common/artprov.cpp
restore SetSelection(-1, -1) behaviour which was broken during wxTextEntry refactoring
[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);
52734360 46WX_DECLARE_EXPORTED_STRING_HASH_MAP(wxIconBundle, wxArtProviderIconBundlesHash);
2aca4a98
VS
47
48class WXDLLEXPORT wxArtProviderCache
49{
50public:
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 68private:
52734360
VZ
69 wxArtProviderBitmapsHash m_bitmapsHash; // cache of wxBitmaps
70 wxArtProviderIconBundlesHash m_iconBundlesHash; // cache of wxIconBundles
2aca4a98
VS
71};
72
73bool 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
87bool 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
101void wxArtProviderCache::Clear()
102{
103 m_bitmapsHash.clear();
52734360 104 m_iconBundlesHash.clear();
2aca4a98
VS
105}
106
52734360
VZ
107/* static */ wxString
108wxArtProviderCache::ConstructHashID(const wxArtID& id,
109 const wxArtClient& client)
2aca4a98 110{
52734360 111 return id + _T('-') + client;
2aca4a98
VS
112}
113
114
52734360
VZ
115/* static */ wxString
116wxArtProviderCache::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
571d2e0f 124// ============================================================================
2aca4a98 125// wxArtProvider class
571d2e0f 126// ============================================================================
2aca4a98
VS
127
128IMPLEMENT_ABSTRACT_CLASS(wxArtProvider, wxObject)
129
130wxArtProvidersList *wxArtProvider::sm_providers = NULL;
131wxArtProviderCache *wxArtProvider::sm_cache = NULL;
132
571d2e0f
VZ
133// ----------------------------------------------------------------------------
134// wxArtProvider ctors/dtor
135// ----------------------------------------------------------------------------
136
137wxArtProvider::~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
571d2e0f 163/*static*/ void wxArtProvider::Insert(wxArtProvider *provider)
dd7d379e
VS
164{
165 CommonAddingProvider();
166 sm_providers->Append(provider);
167}
168
571d2e0f 169/*static*/ bool wxArtProvider::Pop()
2aca4a98 170{
4629016d 171 wxCHECK_MSG( sm_providers, false, _T("no wxArtProvider exists") );
571d2e0f 172 wxCHECK_MSG( !sm_providers->empty(), false, _T("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{
4629016d 181 wxCHECK_MSG( sm_providers, false, _T("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
VS
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
2aca4a98
VS
226 wxCHECK_MSG( sm_providers, wxNullBitmap, _T("no wxArtProvider exists") );
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() )
3242feb1 238 {
64c288fa 239#if wxUSE_IMAGE && (!defined(__WXMSW__) || wxUSE_WXDIB)
e53a95bc
RR
240 if ( size != wxDefaultSize &&
241 (bmp.GetWidth() != size.x || bmp.GetHeight() != size.y) )
3242feb1
VS
242 {
243 wxImage img = bmp.ConvertToImage();
e53a95bc 244 img.Rescale(size.x, size.y);
3242feb1
VS
245 bmp = wxBitmap(img);
246 }
4629016d 247#endif
2aca4a98 248 break;
3242feb1 249 }
52734360
VZ
250 // We could try the IconBundles here and convert what we find
251 // to a bitmap.
2aca4a98
VS
252 }
253 sm_cache->PutBitmap(hashId, bmp);
52734360 254 }
2aca4a98
VS
255
256 return bmp;
257}
258
52734360
VZ
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
57b0987b
VS
285/*static*/ wxIcon wxArtProvider::GetIcon(const wxArtID& id,
286 const wxArtClient& client,
2aca4a98
VS
287 const wxSize& size)
288{
289 wxCHECK_MSG( sm_providers, wxNullIcon, _T("no wxArtProvider exists") );
290
52734360
VZ
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
57b0987b 297 wxBitmap bmp = GetBitmap(id, client, size);
25dd6997 298 if ( !bmp.Ok() )
2aca4a98 299 return wxNullIcon;
2aca4a98 300
25dd6997
VZ
301 wxIcon icon;
302 icon.CopyFromBitmap(bmp);
303 return icon;
304}
2aca4a98 305
1970409e
VZ
306/* static */
307wxIcon wxArtProvider::GetMessageBoxIcon(int flags)
308{
309 wxIcon icon;
310 switch ( flags & wxICON_MASK )
311 {
312 default:
313 wxFAIL_MSG(_T("incorrect message box icon flags"));
314 // fall through
315
316 case wxICON_ERROR:
317 icon = wxArtProvider::GetIcon(wxART_ERROR, wxART_MESSAGE_BOX);
318 break;
319
320 case wxICON_INFORMATION:
321 icon = wxArtProvider::GetIcon(wxART_INFORMATION, wxART_MESSAGE_BOX);
322 break;
323
324 case wxICON_WARNING:
325 icon = wxArtProvider::GetIcon(wxART_WARNING, wxART_MESSAGE_BOX);
326 break;
327
328 case wxICON_QUESTION:
329 icon = wxArtProvider::GetIcon(wxART_QUESTION, wxART_MESSAGE_BOX);
330 break;
331 }
332
333 return icon;
334}
335
f6d43eda 336#if defined(__WXGTK20__) && !defined(__WXUNIVERSAL__)
19abd352 337 #include <gtk/gtk.h>
b737ad10 338 extern GtkIconSize wxArtClientToIconSize(const wxArtClient& client);
e53a95bc 339#endif // defined(__WXGTK20__) && !defined(__WXUNIVERSAL__)
b737ad10 340
e53a95bc 341/*static*/ wxSize wxArtProvider::GetSizeHint(const wxArtClient& client,
b737ad10
RR
342 bool platform_dependent)
343{
344 if (!platform_dependent)
345 {
346 wxArtProvidersList::compatibility_iterator node = sm_providers->GetFirst();
347 if (node)
e53a95bc
RR
348 return node->GetData()->DoGetSizeHint(client);
349 }
f6d43eda 350
b737ad10 351 // else return platform dependent size
f6d43eda
VZ
352
353#if defined(__WXGTK20__) && !defined(__WXUNIVERSAL__)
e53a95bc 354 // Gtk has specific sizes for each client, see artgtk.cpp
b737ad10 355 GtkIconSize gtk_size = wxArtClientToIconSize(client);
e53a95bc
RR
356 // no size hints for this client
357 if (gtk_size == GTK_ICON_SIZE_INVALID)
358 return wxDefaultSize;
b737ad10
RR
359 gint width, height;
360 gtk_icon_size_lookup( gtk_size, &width, &height);
361 return wxSize(width, height);
f6d43eda 362#else // !GTK+ 2
e53a95bc 363 // NB: These size hints may have to be adjusted per platform
b737ad10 364 if (client == wxART_TOOLBAR)
e53a95bc 365 return wxSize(16, 15);
b737ad10
RR
366 else if (client == wxART_MENU)
367 return wxSize(16, 15);
e53a95bc
RR
368 else if (client == wxART_FRAME_ICON)
369 return wxSize(16, 15);
b737ad10
RR
370 else if (client == wxART_CMN_DIALOG || client == wxART_MESSAGE_BOX)
371 return wxSize(32, 32);
e53a95bc
RR
372 else if (client == wxART_HELP_BROWSER)
373 return wxSize(16, 15);
b737ad10
RR
374 else if (client == wxART_BUTTON)
375 return wxSize(16, 15);
e53a95bc 376 else // wxART_OTHER or perhaps a user's client, no specified size
8ecff181 377 return wxDefaultSize;
f6d43eda 378#endif // GTK+ 2/else
b737ad10
RR
379}
380
571d2e0f
VZ
381// ----------------------------------------------------------------------------
382// deprecated wxArtProvider methods
383// ----------------------------------------------------------------------------
384
385#if WXWIN_COMPATIBILITY_2_6
386
387/* static */ void wxArtProvider::PushProvider(wxArtProvider *provider)
388{
389 Push(provider);
390}
391
392/* static */ void wxArtProvider::InsertProvider(wxArtProvider *provider)
393{
394 Insert(provider);
395}
396
397/* static */ bool wxArtProvider::PopProvider()
398{
399 return Pop();
400}
401
402/* static */ bool wxArtProvider::RemoveProvider(wxArtProvider *provider)
403{
404 // RemoveProvider() used to delete the provider being removed so this is
405 // not a typo, we must call Delete() and not Remove() here
406 return Delete(provider);
407}
408
409#endif // WXWIN_COMPATIBILITY_2_6
410
411// ============================================================================
412// wxArtProviderModule
413// ============================================================================
2aca4a98
VS
414
415class wxArtProviderModule: public wxModule
416{
417public:
2b5f62a0
VZ
418 bool OnInit()
419 {
420 wxArtProvider::InitStdProvider();
7bebedd8 421 wxArtProvider::InitNativeProvider();
4629016d 422 return true;
2b5f62a0
VZ
423 }
424 void OnExit()
425 {
426 wxArtProvider::CleanUpProviders();
427 }
2aca4a98
VS
428
429 DECLARE_DYNAMIC_CLASS(wxArtProviderModule)
430};
431
432IMPLEMENT_DYNAMIC_CLASS(wxArtProviderModule, wxModule)