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