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