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