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