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