]> git.saurik.com Git - wxWidgets.git/blame - src/common/artprov.cpp
[ 1509599 ] 'Split pickers page in widgets sample' with more icons and rebaking.
[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
23#ifndef WX_PRECOMP
2aca4a98 24 #include "wx/list.h"
8ecff181 25 #include "wx/log.h"
df69528b 26 #include "wx/hashmap.h"
2aca4a98
VS
27#endif
28
29#include "wx/artprov.h"
2aca4a98 30#include "wx/module.h"
8ecff181 31
cfb57561 32#if wxUSE_IMAGE
8ecff181 33 #include "wx/image.h"
cfb57561 34#endif
2aca4a98 35
2aca4a98
VS
36// ===========================================================================
37// implementation
38// ===========================================================================
39
40#include "wx/listimpl.cpp"
41WX_DECLARE_LIST(wxArtProvider, wxArtProvidersList);
259c43f6 42WX_DEFINE_LIST(wxArtProvidersList)
2aca4a98
VS
43
44// ----------------------------------------------------------------------------
45// Cache class - stores already requested bitmaps
46// ----------------------------------------------------------------------------
47
3f5c62f9 48WX_DECLARE_EXPORTED_STRING_HASH_MAP(wxBitmap, wxArtProviderBitmapsHash);
2aca4a98
VS
49
50class WXDLLEXPORT wxArtProviderCache
51{
52public:
53 bool GetBitmap(const wxString& full_id, wxBitmap* bmp);
54 void PutBitmap(const wxString& full_id, const wxBitmap& bmp)
55 { m_bitmapsHash[full_id] = bmp; }
56
57 void Clear();
9e7ed2b0 58
57b0987b
VS
59 static wxString ConstructHashID(const wxArtID& id,
60 const wxArtClient& client,
2aca4a98
VS
61 const wxSize& size);
62
63private:
64 wxArtProviderBitmapsHash m_bitmapsHash;
65};
66
67bool wxArtProviderCache::GetBitmap(const wxString& full_id, wxBitmap* bmp)
68{
69 wxArtProviderBitmapsHash::iterator entry = m_bitmapsHash.find(full_id);
70 if ( entry == m_bitmapsHash.end() )
71 {
4629016d 72 return false;
2aca4a98
VS
73 }
74 else
75 {
76 *bmp = entry->second;
4629016d 77 return true;
2aca4a98
VS
78 }
79}
80
81void wxArtProviderCache::Clear()
82{
83 m_bitmapsHash.clear();
84}
85
86/*static*/ wxString wxArtProviderCache::ConstructHashID(
57b0987b
VS
87 const wxArtID& id, const wxArtClient& client,
88 const wxSize& size)
2aca4a98
VS
89{
90 wxString str;
57b0987b 91 str.Printf(wxT("%s-%s-%i-%i"), id.c_str(), client.c_str(), size.x, size.y);
2aca4a98
VS
92 return str;
93}
94
95
96// ----------------------------------------------------------------------------
97// wxArtProvider class
98// ----------------------------------------------------------------------------
99
100IMPLEMENT_ABSTRACT_CLASS(wxArtProvider, wxObject)
101
102wxArtProvidersList *wxArtProvider::sm_providers = NULL;
103wxArtProviderCache *wxArtProvider::sm_cache = NULL;
104
105/*static*/ void wxArtProvider::PushProvider(wxArtProvider *provider)
106{
107 if ( !sm_providers )
108 {
109 sm_providers = new wxArtProvidersList;
2aca4a98
VS
110 sm_cache = new wxArtProviderCache;
111 }
112
113 sm_providers->Insert(provider);
c3357a03 114 sm_cache->Clear();
2aca4a98
VS
115}
116
117/*static*/ bool wxArtProvider::PopProvider()
118{
4629016d
WS
119 wxCHECK_MSG( sm_providers, false, _T("no wxArtProvider exists") );
120 wxCHECK_MSG( sm_providers->GetCount() > 0, false, _T("wxArtProviders stack is empty") );
2aca4a98 121
222ed1d6
MB
122 delete sm_providers->GetFirst()->GetData();
123 sm_providers->Erase(sm_providers->GetFirst());
2aca4a98 124 sm_cache->Clear();
4629016d 125 return true;
2aca4a98
VS
126}
127
128/*static*/ bool wxArtProvider::RemoveProvider(wxArtProvider *provider)
129{
4629016d 130 wxCHECK_MSG( sm_providers, false, _T("no wxArtProvider exists") );
2aca4a98
VS
131
132 if ( sm_providers->DeleteObject(provider) )
133 {
222ed1d6 134 delete provider;
2aca4a98 135 sm_cache->Clear();
4629016d 136 return true;
2aca4a98 137 }
9e7ed2b0 138
4629016d 139 return false;
2aca4a98
VS
140}
141
142/*static*/ void wxArtProvider::CleanUpProviders()
143{
89c20ac1 144 WX_CLEAR_LIST(wxArtProvidersList, *sm_providers);
2aca4a98
VS
145 wxDELETE(sm_providers);
146 wxDELETE(sm_cache);
147}
148
57b0987b
VS
149/*static*/ wxBitmap wxArtProvider::GetBitmap(const wxArtID& id,
150 const wxArtClient& client,
e53a95bc 151 const wxSize& size)
2aca4a98 152{
57b0987b
VS
153 // safety-check against writing client,id,size instead of id,client,size:
154 wxASSERT_MSG( client.Last() == _T('C'), _T("invalid 'client' parameter") );
155
2aca4a98
VS
156 wxCHECK_MSG( sm_providers, wxNullBitmap, _T("no wxArtProvider exists") );
157
e53a95bc 158 wxString hashId = wxArtProviderCache::ConstructHashID(id, client, size);
2aca4a98
VS
159
160 wxBitmap bmp;
161 if ( !sm_cache->GetBitmap(hashId, &bmp) )
162 {
222ed1d6 163 for (wxArtProvidersList::compatibility_iterator node = sm_providers->GetFirst();
2aca4a98
VS
164 node; node = node->GetNext())
165 {
e53a95bc 166 bmp = node->GetData()->CreateBitmap(id, client, size);
2aca4a98 167 if ( bmp.Ok() )
3242feb1 168 {
64c288fa 169#if wxUSE_IMAGE && (!defined(__WXMSW__) || wxUSE_WXDIB)
e53a95bc
RR
170 if ( size != wxDefaultSize &&
171 (bmp.GetWidth() != size.x || bmp.GetHeight() != size.y) )
3242feb1
VS
172 {
173 wxImage img = bmp.ConvertToImage();
e53a95bc 174 img.Rescale(size.x, size.y);
3242feb1
VS
175 bmp = wxBitmap(img);
176 }
4629016d 177#endif
2aca4a98 178 break;
3242feb1 179 }
2aca4a98 180 }
3242feb1 181
2aca4a98
VS
182 sm_cache->PutBitmap(hashId, bmp);
183 }
184
185 return bmp;
186}
187
57b0987b
VS
188/*static*/ wxIcon wxArtProvider::GetIcon(const wxArtID& id,
189 const wxArtClient& client,
2aca4a98
VS
190 const wxSize& size)
191{
192 wxCHECK_MSG( sm_providers, wxNullIcon, _T("no wxArtProvider exists") );
193
57b0987b 194 wxBitmap bmp = GetBitmap(id, client, size);
25dd6997 195 if ( !bmp.Ok() )
2aca4a98 196 return wxNullIcon;
2aca4a98 197
25dd6997
VZ
198 wxIcon icon;
199 icon.CopyFromBitmap(bmp);
200 return icon;
201}
2aca4a98 202
f6d43eda 203#if defined(__WXGTK20__) && !defined(__WXUNIVERSAL__)
abc736fd 204 #include "wx/gtk/private.h"
b737ad10 205 extern GtkIconSize wxArtClientToIconSize(const wxArtClient& client);
e53a95bc 206#endif // defined(__WXGTK20__) && !defined(__WXUNIVERSAL__)
b737ad10 207
e53a95bc 208/*static*/ wxSize wxArtProvider::GetSizeHint(const wxArtClient& client,
b737ad10
RR
209 bool platform_dependent)
210{
211 if (!platform_dependent)
212 {
213 wxArtProvidersList::compatibility_iterator node = sm_providers->GetFirst();
214 if (node)
e53a95bc
RR
215 return node->GetData()->DoGetSizeHint(client);
216 }
f6d43eda 217
b737ad10 218 // else return platform dependent size
f6d43eda
VZ
219
220#if defined(__WXGTK20__) && !defined(__WXUNIVERSAL__)
e53a95bc 221 // Gtk has specific sizes for each client, see artgtk.cpp
b737ad10 222 GtkIconSize gtk_size = wxArtClientToIconSize(client);
e53a95bc
RR
223 // no size hints for this client
224 if (gtk_size == GTK_ICON_SIZE_INVALID)
225 return wxDefaultSize;
b737ad10
RR
226 gint width, height;
227 gtk_icon_size_lookup( gtk_size, &width, &height);
228 return wxSize(width, height);
f6d43eda 229#else // !GTK+ 2
e53a95bc 230 // NB: These size hints may have to be adjusted per platform
b737ad10 231 if (client == wxART_TOOLBAR)
e53a95bc 232 return wxSize(16, 15);
b737ad10
RR
233 else if (client == wxART_MENU)
234 return wxSize(16, 15);
e53a95bc
RR
235 else if (client == wxART_FRAME_ICON)
236 return wxSize(16, 15);
b737ad10
RR
237 else if (client == wxART_CMN_DIALOG || client == wxART_MESSAGE_BOX)
238 return wxSize(32, 32);
e53a95bc
RR
239 else if (client == wxART_HELP_BROWSER)
240 return wxSize(16, 15);
b737ad10
RR
241 else if (client == wxART_BUTTON)
242 return wxSize(16, 15);
e53a95bc 243 else // wxART_OTHER or perhaps a user's client, no specified size
8ecff181 244 return wxDefaultSize;
f6d43eda 245#endif // GTK+ 2/else
b737ad10
RR
246}
247
2aca4a98
VS
248
249class wxArtProviderModule: public wxModule
250{
251public:
2b5f62a0
VZ
252 bool OnInit()
253 {
254 wxArtProvider::InitStdProvider();
7bebedd8 255 wxArtProvider::InitNativeProvider();
4629016d 256 return true;
2b5f62a0
VZ
257 }
258 void OnExit()
259 {
260 wxArtProvider::CleanUpProviders();
261 }
2aca4a98
VS
262
263 DECLARE_DYNAMIC_CLASS(wxArtProviderModule)
264};
265
266IMPLEMENT_DYNAMIC_CLASS(wxArtProviderModule, wxModule)