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