Use the associated document manager, not the global one
[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 // ----------------------------------------------------------------------------
103 // wxArtProvider ctors/dtor
104 // ----------------------------------------------------------------------------
105
106 wxArtProvider::~wxArtProvider()
107 {
108 Remove(this);
109 }
110
111 // ----------------------------------------------------------------------------
112 // wxArtProvider operations on provider stack
113 // ----------------------------------------------------------------------------
114
115 /*static*/ void wxArtProvider::CommonAddingProvider()
116 {
117 if ( !sm_providers )
118 {
119 sm_providers = new wxArtProvidersList;
120 sm_cache = new wxArtProviderCache;
121 }
122
123 sm_cache->Clear();
124 }
125
126 /*static*/ void wxArtProvider::Push(wxArtProvider *provider)
127 {
128 CommonAddingProvider();
129 sm_providers->Insert(provider);
130 }
131
132 /*static*/ void wxArtProvider::Insert(wxArtProvider *provider)
133 {
134 CommonAddingProvider();
135 sm_providers->Append(provider);
136 }
137
138 /*static*/ bool wxArtProvider::Pop()
139 {
140 wxCHECK_MSG( sm_providers, false, _T("no wxArtProvider exists") );
141 wxCHECK_MSG( !sm_providers->empty(), false, _T("wxArtProviders stack is empty") );
142
143 delete sm_providers->GetFirst()->GetData();
144 sm_cache->Clear();
145 return true;
146 }
147
148 /*static*/ bool wxArtProvider::Remove(wxArtProvider *provider)
149 {
150 wxCHECK_MSG( sm_providers, false, _T("no wxArtProvider exists") );
151
152 if ( sm_providers->DeleteObject(provider) )
153 {
154 sm_cache->Clear();
155 return true;
156 }
157
158 return false;
159 }
160
161 /*static*/ bool wxArtProvider::Delete(wxArtProvider *provider)
162 {
163 // provider will remove itself from the stack in its dtor
164 delete provider;
165
166 return true;
167 }
168
169 /*static*/ void wxArtProvider::CleanUpProviders()
170 {
171 if ( sm_providers )
172 {
173 while ( !sm_providers->empty() )
174 delete *sm_providers->begin();
175
176 delete sm_providers;
177 sm_providers = NULL;
178
179 delete sm_cache;
180 sm_cache = NULL;
181 }
182 }
183
184 // ----------------------------------------------------------------------------
185 // wxArtProvider: retrieving bitmaps/icons
186 // ----------------------------------------------------------------------------
187
188 /*static*/ wxBitmap wxArtProvider::GetBitmap(const wxArtID& id,
189 const wxArtClient& client,
190 const wxSize& size)
191 {
192 // safety-check against writing client,id,size instead of id,client,size:
193 wxASSERT_MSG( client.Last() == _T('C'), _T("invalid 'client' parameter") );
194
195 wxCHECK_MSG( sm_providers, wxNullBitmap, _T("no wxArtProvider exists") );
196
197 wxString hashId = wxArtProviderCache::ConstructHashID(id, client, size);
198
199 wxBitmap bmp;
200 if ( !sm_cache->GetBitmap(hashId, &bmp) )
201 {
202 for (wxArtProvidersList::compatibility_iterator node = sm_providers->GetFirst();
203 node; node = node->GetNext())
204 {
205 bmp = node->GetData()->CreateBitmap(id, client, size);
206 if ( bmp.Ok() )
207 {
208 #if wxUSE_IMAGE && (!defined(__WXMSW__) || wxUSE_WXDIB)
209 if ( size != wxDefaultSize &&
210 (bmp.GetWidth() != size.x || bmp.GetHeight() != size.y) )
211 {
212 wxImage img = bmp.ConvertToImage();
213 img.Rescale(size.x, size.y);
214 bmp = wxBitmap(img);
215 }
216 #endif
217 break;
218 }
219 }
220
221 sm_cache->PutBitmap(hashId, bmp);
222 }
223
224 return bmp;
225 }
226
227 /*static*/ wxIcon wxArtProvider::GetIcon(const wxArtID& id,
228 const wxArtClient& client,
229 const wxSize& size)
230 {
231 wxCHECK_MSG( sm_providers, wxNullIcon, _T("no wxArtProvider exists") );
232
233 wxBitmap bmp = GetBitmap(id, client, size);
234 if ( !bmp.Ok() )
235 return wxNullIcon;
236
237 wxIcon icon;
238 icon.CopyFromBitmap(bmp);
239 return icon;
240 }
241
242 #if defined(__WXGTK20__) && !defined(__WXUNIVERSAL__)
243 #include "wx/gtk/private.h"
244 extern GtkIconSize wxArtClientToIconSize(const wxArtClient& client);
245 #endif // defined(__WXGTK20__) && !defined(__WXUNIVERSAL__)
246
247 /*static*/ wxSize wxArtProvider::GetSizeHint(const wxArtClient& client,
248 bool platform_dependent)
249 {
250 if (!platform_dependent)
251 {
252 wxArtProvidersList::compatibility_iterator node = sm_providers->GetFirst();
253 if (node)
254 return node->GetData()->DoGetSizeHint(client);
255 }
256
257 // else return platform dependent size
258
259 #if defined(__WXGTK20__) && !defined(__WXUNIVERSAL__)
260 // Gtk has specific sizes for each client, see artgtk.cpp
261 GtkIconSize gtk_size = wxArtClientToIconSize(client);
262 // no size hints for this client
263 if (gtk_size == GTK_ICON_SIZE_INVALID)
264 return wxDefaultSize;
265 gint width, height;
266 gtk_icon_size_lookup( gtk_size, &width, &height);
267 return wxSize(width, height);
268 #else // !GTK+ 2
269 // NB: These size hints may have to be adjusted per platform
270 if (client == wxART_TOOLBAR)
271 return wxSize(16, 15);
272 else if (client == wxART_MENU)
273 return wxSize(16, 15);
274 else if (client == wxART_FRAME_ICON)
275 return wxSize(16, 15);
276 else if (client == wxART_CMN_DIALOG || client == wxART_MESSAGE_BOX)
277 return wxSize(32, 32);
278 else if (client == wxART_HELP_BROWSER)
279 return wxSize(16, 15);
280 else if (client == wxART_BUTTON)
281 return wxSize(16, 15);
282 else // wxART_OTHER or perhaps a user's client, no specified size
283 return wxDefaultSize;
284 #endif // GTK+ 2/else
285 }
286
287 // ----------------------------------------------------------------------------
288 // deprecated wxArtProvider methods
289 // ----------------------------------------------------------------------------
290
291 #if WXWIN_COMPATIBILITY_2_6
292
293 /* static */ void wxArtProvider::PushProvider(wxArtProvider *provider)
294 {
295 Push(provider);
296 }
297
298 /* static */ void wxArtProvider::InsertProvider(wxArtProvider *provider)
299 {
300 Insert(provider);
301 }
302
303 /* static */ bool wxArtProvider::PopProvider()
304 {
305 return Pop();
306 }
307
308 /* static */ bool wxArtProvider::RemoveProvider(wxArtProvider *provider)
309 {
310 // RemoveProvider() used to delete the provider being removed so this is
311 // not a typo, we must call Delete() and not Remove() here
312 return Delete(provider);
313 }
314
315 #endif // WXWIN_COMPATIBILITY_2_6
316
317 // ============================================================================
318 // wxArtProviderModule
319 // ============================================================================
320
321 class wxArtProviderModule: public wxModule
322 {
323 public:
324 bool OnInit()
325 {
326 wxArtProvider::InitStdProvider();
327 wxArtProvider::InitNativeProvider();
328 return true;
329 }
330 void OnExit()
331 {
332 wxArtProvider::CleanUpProviders();
333 }
334
335 DECLARE_DYNAMIC_CLASS(wxArtProviderModule)
336 };
337
338 IMPLEMENT_DYNAMIC_CLASS(wxArtProviderModule, wxModule)