/[pcsx2_0.9.7]/trunk/3rdparty/wxWidgets/src/common/artprov.cpp
ViewVC logotype

Contents of /trunk/3rdparty/wxWidgets/src/common/artprov.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 31 - (show annotations) (download)
Tue Sep 7 03:24:11 2010 UTC (10 years, 2 months ago) by william
File size: 10204 byte(s)
committing r3113 initial commit again...
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: artprov.cpp 57701 2008-12-31 23:40:06Z VS $
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 #ifdef __WXMSW__
34 #include "wx/msw/wrapwin.h"
35 #endif
36
37 // ===========================================================================
38 // implementation
39 // ===========================================================================
40
41 #include "wx/listimpl.cpp"
42 WX_DECLARE_LIST(wxArtProvider, wxArtProvidersList);
43 WX_DEFINE_LIST(wxArtProvidersList)
44
45 // ----------------------------------------------------------------------------
46 // Cache class - stores already requested bitmaps
47 // ----------------------------------------------------------------------------
48
49 WX_DECLARE_EXPORTED_STRING_HASH_MAP(wxBitmap, wxArtProviderBitmapsHash);
50
51 class WXDLLEXPORT wxArtProviderCache
52 {
53 public:
54 bool GetBitmap(const wxString& full_id, wxBitmap* bmp);
55 void PutBitmap(const wxString& full_id, const wxBitmap& bmp)
56 { m_bitmapsHash[full_id] = bmp; }
57
58 void Clear();
59
60 static wxString ConstructHashID(const wxArtID& id,
61 const wxArtClient& client,
62 const wxSize& size);
63
64 private:
65 wxArtProviderBitmapsHash m_bitmapsHash;
66 };
67
68 bool wxArtProviderCache::GetBitmap(const wxString& full_id, wxBitmap* bmp)
69 {
70 wxArtProviderBitmapsHash::iterator entry = m_bitmapsHash.find(full_id);
71 if ( entry == m_bitmapsHash.end() )
72 {
73 return false;
74 }
75 else
76 {
77 *bmp = entry->second;
78 return true;
79 }
80 }
81
82 void wxArtProviderCache::Clear()
83 {
84 m_bitmapsHash.clear();
85 }
86
87 /*static*/ wxString wxArtProviderCache::ConstructHashID(
88 const wxArtID& id, const wxArtClient& client,
89 const wxSize& size)
90 {
91 wxString str;
92 str.Printf(wxT("%s-%s-%i-%i"), id.c_str(), client.c_str(), size.x, size.y);
93 return str;
94 }
95
96
97 // ============================================================================
98 // wxArtProvider class
99 // ============================================================================
100
101 IMPLEMENT_ABSTRACT_CLASS(wxArtProvider, wxObject)
102
103 wxArtProvidersList *wxArtProvider::sm_providers = NULL;
104 wxArtProviderCache *wxArtProvider::sm_cache = NULL;
105
106 // ----------------------------------------------------------------------------
107 // wxArtProvider ctors/dtor
108 // ----------------------------------------------------------------------------
109
110 wxArtProvider::~wxArtProvider()
111 {
112 Remove(this);
113 }
114
115 // ----------------------------------------------------------------------------
116 // wxArtProvider operations on provider stack
117 // ----------------------------------------------------------------------------
118
119 /*static*/ void wxArtProvider::CommonAddingProvider()
120 {
121 if ( !sm_providers )
122 {
123 sm_providers = new wxArtProvidersList;
124 sm_cache = new wxArtProviderCache;
125 }
126
127 sm_cache->Clear();
128 }
129
130 /*static*/ void wxArtProvider::Push(wxArtProvider *provider)
131 {
132 CommonAddingProvider();
133 sm_providers->Insert(provider);
134 }
135
136 /*static*/ void wxArtProvider::Insert(wxArtProvider *provider)
137 {
138 CommonAddingProvider();
139 sm_providers->Append(provider);
140 }
141
142 /*static*/ void wxArtProvider::PushBack(wxArtProvider *provider)
143 {
144 Insert(provider);
145 }
146
147 /*static*/ bool wxArtProvider::Pop()
148 {
149 wxCHECK_MSG( sm_providers, false, _T("no wxArtProvider exists") );
150 wxCHECK_MSG( !sm_providers->empty(), false, _T("wxArtProviders stack is empty") );
151
152 delete sm_providers->GetFirst()->GetData();
153 sm_cache->Clear();
154 return true;
155 }
156
157 /*static*/ bool wxArtProvider::Remove(wxArtProvider *provider)
158 {
159 wxCHECK_MSG( sm_providers, false, _T("no wxArtProvider exists") );
160
161 if ( sm_providers->DeleteObject(provider) )
162 {
163 sm_cache->Clear();
164 return true;
165 }
166
167 return false;
168 }
169
170 /*static*/ bool wxArtProvider::Delete(wxArtProvider *provider)
171 {
172 // provider will remove itself from the stack in its dtor
173 delete provider;
174
175 return true;
176 }
177
178 /*static*/ void wxArtProvider::CleanUpProviders()
179 {
180 if ( sm_providers )
181 {
182 while ( !sm_providers->empty() )
183 delete *sm_providers->begin();
184
185 delete sm_providers;
186 sm_providers = NULL;
187
188 delete sm_cache;
189 sm_cache = NULL;
190 }
191 }
192
193 // ----------------------------------------------------------------------------
194 // wxArtProvider: retrieving bitmaps/icons
195 // ----------------------------------------------------------------------------
196
197 /*static*/ wxBitmap wxArtProvider::GetBitmap(const wxArtID& id,
198 const wxArtClient& client,
199 const wxSize& size)
200 {
201 // safety-check against writing client,id,size instead of id,client,size:
202 wxASSERT_MSG( client.Last() == _T('C'), _T("invalid 'client' parameter") );
203
204 wxCHECK_MSG( sm_providers, wxNullBitmap, _T("no wxArtProvider exists") );
205
206 wxString hashId = wxArtProviderCache::ConstructHashID(id, client, size);
207
208 wxBitmap bmp;
209 if ( !sm_cache->GetBitmap(hashId, &bmp) )
210 {
211 for (wxArtProvidersList::compatibility_iterator node = sm_providers->GetFirst();
212 node; node = node->GetNext())
213 {
214 bmp = node->GetData()->CreateBitmap(id, client, size);
215 if ( bmp.Ok() )
216 {
217 #if wxUSE_IMAGE && (!defined(__WXMSW__) || wxUSE_WXDIB)
218 if ( size != wxDefaultSize &&
219 (bmp.GetWidth() != size.x || bmp.GetHeight() != size.y) )
220 {
221 wxImage img = bmp.ConvertToImage();
222 img.Rescale(size.x, size.y);
223 bmp = wxBitmap(img);
224 }
225 #endif
226 break;
227 }
228 }
229
230 sm_cache->PutBitmap(hashId, bmp);
231 }
232
233 return bmp;
234 }
235
236 /*static*/ wxIcon wxArtProvider::GetIcon(const wxArtID& id,
237 const wxArtClient& client,
238 const wxSize& size)
239 {
240 wxCHECK_MSG( sm_providers, wxNullIcon, _T("no wxArtProvider exists") );
241
242 wxBitmap bmp = GetBitmap(id, client, size);
243 if ( !bmp.Ok() )
244 return wxNullIcon;
245
246 wxIcon icon;
247 icon.CopyFromBitmap(bmp);
248 return icon;
249 }
250
251 #if defined(__WXGTK20__) && !defined(__WXUNIVERSAL__)
252 #include "wx/gtk/private.h"
253 extern GtkIconSize wxArtClientToIconSize(const wxArtClient& client);
254 #endif // defined(__WXGTK20__) && !defined(__WXUNIVERSAL__)
255
256 /*static*/ wxSize wxArtProvider::GetSizeHint(const wxArtClient& client,
257 bool platform_dependent)
258 {
259 if (!platform_dependent)
260 {
261 wxArtProvidersList::compatibility_iterator node = sm_providers->GetFirst();
262 if (node)
263 return node->GetData()->DoGetSizeHint(client);
264 }
265
266 // else return platform dependent size
267
268 #if defined(__WXGTK20__) && !defined(__WXUNIVERSAL__)
269 // Gtk has specific sizes for each client, see artgtk.cpp
270 GtkIconSize gtk_size = wxArtClientToIconSize(client);
271 // no size hints for this client
272 if (gtk_size == GTK_ICON_SIZE_INVALID)
273 return wxDefaultSize;
274 gint width, height;
275 gtk_icon_size_lookup( gtk_size, &width, &height);
276 return wxSize(width, height);
277 #else // !GTK+ 2
278 // NB: These size hints may have to be adjusted per platform
279 if (client == wxART_TOOLBAR)
280 return wxSize(16, 15);
281 else if (client == wxART_MENU)
282 return wxSize(16, 15);
283 else if (client == wxART_FRAME_ICON)
284 {
285 #ifdef __WXMSW__
286 return wxSize(::GetSystemMetrics(SM_CXSMICON),
287 ::GetSystemMetrics(SM_CYSMICON));
288 #else
289 return wxSize(16, 16);
290 #endif // __WXMSW__/!__WXMSW__
291 }
292 else if (client == wxART_CMN_DIALOG || client == wxART_MESSAGE_BOX)
293 return wxSize(32, 32);
294 else if (client == wxART_HELP_BROWSER)
295 return wxSize(16, 15);
296 else if (client == wxART_BUTTON)
297 return wxSize(16, 15);
298 else // wxART_OTHER or perhaps a user's client, no specified size
299 return wxDefaultSize;
300 #endif // GTK+ 2/else
301 }
302
303 // ----------------------------------------------------------------------------
304 // deprecated wxArtProvider methods
305 // ----------------------------------------------------------------------------
306
307 #if WXWIN_COMPATIBILITY_2_6
308
309 /* static */ void wxArtProvider::PushProvider(wxArtProvider *provider)
310 {
311 Push(provider);
312 }
313
314 /* static */ void wxArtProvider::InsertProvider(wxArtProvider *provider)
315 {
316 Insert(provider);
317 }
318
319 /* static */ bool wxArtProvider::PopProvider()
320 {
321 return Pop();
322 }
323
324 /* static */ bool wxArtProvider::RemoveProvider(wxArtProvider *provider)
325 {
326 // RemoveProvider() used to delete the provider being removed so this is
327 // not a typo, we must call Delete() and not Remove() here
328 return Delete(provider);
329 }
330
331 #endif // WXWIN_COMPATIBILITY_2_6
332
333 // ============================================================================
334 // wxArtProviderModule
335 // ============================================================================
336
337 class wxArtProviderModule: public wxModule
338 {
339 public:
340 bool OnInit()
341 {
342 wxArtProvider::InitStdProvider();
343 wxArtProvider::InitNativeProvider();
344 return true;
345 }
346 void OnExit()
347 {
348 wxArtProvider::CleanUpProviders();
349 }
350
351 DECLARE_DYNAMIC_CLASS(wxArtProviderModule)
352 };
353
354 IMPLEMENT_DYNAMIC_CLASS(wxArtProviderModule, wxModule)

  ViewVC Help
Powered by ViewVC 1.1.22