]>
Commit | Line | Data |
---|---|---|
2aca4a98 VS |
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 | |
65571936 | 9 | // Licence: wxWindows licence |
2aca4a98 VS |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // --------------------------------------------------------------------------- | |
13 | // headers | |
14 | // --------------------------------------------------------------------------- | |
15 | ||
14f355c2 | 16 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
2aca4a98 VS |
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" | |
cfb57561 | 35 | #if wxUSE_IMAGE |
3242feb1 | 36 | #include "wx/image.h" |
cfb57561 | 37 | #endif |
2aca4a98 | 38 | |
2aca4a98 VS |
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 | ||
9e7ed2b0 | 51 | WX_DECLARE_EXPORTED_STRING_HASH_MAP(wxBitmap, wxArtProviderBitmapsHash); |
2aca4a98 VS |
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(); | |
9e7ed2b0 | 61 | |
57b0987b VS |
62 | static wxString ConstructHashID(const wxArtID& id, |
63 | const wxArtClient& client, | |
2aca4a98 VS |
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 | { | |
4629016d | 75 | return false; |
2aca4a98 VS |
76 | } |
77 | else | |
78 | { | |
79 | *bmp = entry->second; | |
4629016d | 80 | return true; |
2aca4a98 VS |
81 | } |
82 | } | |
83 | ||
84 | void wxArtProviderCache::Clear() | |
85 | { | |
86 | m_bitmapsHash.clear(); | |
87 | } | |
88 | ||
89 | /*static*/ wxString wxArtProviderCache::ConstructHashID( | |
57b0987b VS |
90 | const wxArtID& id, const wxArtClient& client, |
91 | const wxSize& size) | |
2aca4a98 VS |
92 | { |
93 | wxString str; | |
57b0987b | 94 | str.Printf(wxT("%s-%s-%i-%i"), id.c_str(), client.c_str(), size.x, size.y); |
2aca4a98 VS |
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; | |
2aca4a98 VS |
113 | sm_cache = new wxArtProviderCache; |
114 | } | |
115 | ||
116 | sm_providers->Insert(provider); | |
c3357a03 | 117 | sm_cache->Clear(); |
2aca4a98 VS |
118 | } |
119 | ||
120 | /*static*/ bool wxArtProvider::PopProvider() | |
121 | { | |
4629016d WS |
122 | wxCHECK_MSG( sm_providers, false, _T("no wxArtProvider exists") ); |
123 | wxCHECK_MSG( sm_providers->GetCount() > 0, false, _T("wxArtProviders stack is empty") ); | |
2aca4a98 | 124 | |
222ed1d6 MB |
125 | delete sm_providers->GetFirst()->GetData(); |
126 | sm_providers->Erase(sm_providers->GetFirst()); | |
2aca4a98 | 127 | sm_cache->Clear(); |
4629016d | 128 | return true; |
2aca4a98 VS |
129 | } |
130 | ||
131 | /*static*/ bool wxArtProvider::RemoveProvider(wxArtProvider *provider) | |
132 | { | |
4629016d | 133 | wxCHECK_MSG( sm_providers, false, _T("no wxArtProvider exists") ); |
2aca4a98 VS |
134 | |
135 | if ( sm_providers->DeleteObject(provider) ) | |
136 | { | |
222ed1d6 | 137 | delete provider; |
2aca4a98 | 138 | sm_cache->Clear(); |
4629016d | 139 | return true; |
2aca4a98 | 140 | } |
9e7ed2b0 | 141 | |
4629016d | 142 | return false; |
2aca4a98 VS |
143 | } |
144 | ||
145 | /*static*/ void wxArtProvider::CleanUpProviders() | |
146 | { | |
89c20ac1 | 147 | WX_CLEAR_LIST(wxArtProvidersList, *sm_providers); |
2aca4a98 VS |
148 | wxDELETE(sm_providers); |
149 | wxDELETE(sm_cache); | |
150 | } | |
151 | ||
57b0987b VS |
152 | /*static*/ wxBitmap wxArtProvider::GetBitmap(const wxArtID& id, |
153 | const wxArtClient& client, | |
b737ad10 | 154 | const wxSize& reqSize) |
2aca4a98 | 155 | { |
57b0987b VS |
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 | ||
2aca4a98 VS |
159 | wxCHECK_MSG( sm_providers, wxNullBitmap, _T("no wxArtProvider exists") ); |
160 | ||
b737ad10 RR |
161 | wxSize bestSize = (reqSize != wxDefaultSize) ? reqSize : GetSize(client); |
162 | ||
163 | wxString hashId = wxArtProviderCache::ConstructHashID(id, client, bestSize); | |
2aca4a98 VS |
164 | |
165 | wxBitmap bmp; | |
166 | if ( !sm_cache->GetBitmap(hashId, &bmp) ) | |
167 | { | |
222ed1d6 | 168 | for (wxArtProvidersList::compatibility_iterator node = sm_providers->GetFirst(); |
2aca4a98 VS |
169 | node; node = node->GetNext()) |
170 | { | |
b737ad10 | 171 | bmp = node->GetData()->CreateBitmap(id, client, bestSize); |
2aca4a98 | 172 | if ( bmp.Ok() ) |
3242feb1 | 173 | { |
cfb57561 | 174 | #if wxUSE_IMAGE |
b737ad10 RR |
175 | int bmp_w = bmp.GetWidth(); |
176 | int bmp_h = bmp.GetHeight(); | |
177 | // want default size but it's smaller, paste into transparent image | |
f6d43eda | 178 | if ((reqSize == wxDefaultSize) && |
b737ad10 RR |
179 | (bmp_h < bestSize.x) && (bmp_w < bestSize.y)) |
180 | { | |
181 | wxPoint offset((bestSize.x - bmp_w)/2, (bestSize.y - bmp_h)/2); | |
182 | wxImage img = bmp.ConvertToImage(); | |
183 | img.Resize(bestSize, offset); | |
184 | bmp = wxBitmap(img); | |
185 | } | |
186 | else if ( (bmp_w != bestSize.x) || (bmp_h != bestSize.y) ) | |
3242feb1 VS |
187 | { |
188 | wxImage img = bmp.ConvertToImage(); | |
b737ad10 | 189 | img.Rescale(bestSize.x, bestSize.y); |
3242feb1 VS |
190 | bmp = wxBitmap(img); |
191 | } | |
4629016d | 192 | #endif |
2aca4a98 | 193 | break; |
3242feb1 | 194 | } |
2aca4a98 | 195 | } |
3242feb1 | 196 | |
2aca4a98 VS |
197 | sm_cache->PutBitmap(hashId, bmp); |
198 | } | |
199 | ||
200 | return bmp; | |
201 | } | |
202 | ||
57b0987b VS |
203 | /*static*/ wxIcon wxArtProvider::GetIcon(const wxArtID& id, |
204 | const wxArtClient& client, | |
2aca4a98 VS |
205 | const wxSize& size) |
206 | { | |
207 | wxCHECK_MSG( sm_providers, wxNullIcon, _T("no wxArtProvider exists") ); | |
208 | ||
57b0987b | 209 | wxBitmap bmp = GetBitmap(id, client, size); |
25dd6997 | 210 | if ( !bmp.Ok() ) |
2aca4a98 | 211 | return wxNullIcon; |
2aca4a98 | 212 | |
25dd6997 VZ |
213 | wxIcon icon; |
214 | icon.CopyFromBitmap(bmp); | |
215 | return icon; | |
216 | } | |
2aca4a98 | 217 | |
f6d43eda | 218 | #if defined(__WXGTK20__) && !defined(__WXUNIVERSAL__) |
b737ad10 RR |
219 | #include <gtk/gtk.h> |
220 | extern GtkIconSize wxArtClientToIconSize(const wxArtClient& client); | |
221 | #endif // __WXGTK__ | |
222 | ||
f6d43eda | 223 | /*static*/ wxSize wxArtProvider::GetSize(const wxArtClient& client, |
b737ad10 RR |
224 | bool platform_dependent) |
225 | { | |
226 | if (!platform_dependent) | |
227 | { | |
228 | wxArtProvidersList::compatibility_iterator node = sm_providers->GetFirst(); | |
229 | if (node) | |
230 | return node->GetData()->DoGetSize(client); | |
f6d43eda | 231 | |
b737ad10 | 232 | // else return platform dependent size |
f6d43eda VZ |
233 | } |
234 | ||
235 | #if defined(__WXGTK20__) && !defined(__WXUNIVERSAL__) | |
b737ad10 RR |
236 | GtkIconSize gtk_size = wxArtClientToIconSize(client); |
237 | gint width, height; | |
238 | gtk_icon_size_lookup( gtk_size, &width, &height); | |
239 | return wxSize(width, height); | |
f6d43eda | 240 | #else // !GTK+ 2 |
b737ad10 RR |
241 | if (client == wxART_TOOLBAR) |
242 | return wxSize(32, 32); | |
243 | else if (client == wxART_MENU) | |
244 | return wxSize(16, 15); | |
245 | else if (client == wxART_CMN_DIALOG || client == wxART_MESSAGE_BOX) | |
246 | return wxSize(32, 32); | |
247 | else if (client == wxART_BUTTON) | |
248 | return wxSize(16, 15); | |
249 | else | |
f6d43eda VZ |
250 | return wxSize(16, 15); // this is arbitrary |
251 | #endif // GTK+ 2/else | |
b737ad10 RR |
252 | } |
253 | ||
2aca4a98 VS |
254 | |
255 | class wxArtProviderModule: public wxModule | |
256 | { | |
257 | public: | |
2b5f62a0 VZ |
258 | bool OnInit() |
259 | { | |
260 | wxArtProvider::InitStdProvider(); | |
7bebedd8 | 261 | wxArtProvider::InitNativeProvider(); |
4629016d | 262 | return true; |
2b5f62a0 VZ |
263 | } |
264 | void OnExit() | |
265 | { | |
266 | wxArtProvider::CleanUpProviders(); | |
267 | } | |
2aca4a98 VS |
268 | |
269 | DECLARE_DYNAMIC_CLASS(wxArtProviderModule) | |
270 | }; | |
271 | ||
272 | IMPLEMENT_DYNAMIC_CLASS(wxArtProviderModule, wxModule) |