]>
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 | |
9 | // Licence: wxWindows licence | |
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 | |
e5f4aeb6 JS |
39 | // For the purposes of forcing this module to link |
40 | extern char g_ArtProviderModule; | |
2aca4a98 VS |
41 | |
42 | // =========================================================================== | |
43 | // implementation | |
44 | // =========================================================================== | |
45 | ||
46 | #include "wx/listimpl.cpp" | |
47 | WX_DECLARE_LIST(wxArtProvider, wxArtProvidersList); | |
48 | WX_DEFINE_LIST(wxArtProvidersList); | |
49 | ||
50 | // ---------------------------------------------------------------------------- | |
51 | // Cache class - stores already requested bitmaps | |
52 | // ---------------------------------------------------------------------------- | |
53 | ||
9e7ed2b0 | 54 | WX_DECLARE_EXPORTED_STRING_HASH_MAP(wxBitmap, wxArtProviderBitmapsHash); |
2aca4a98 VS |
55 | |
56 | class WXDLLEXPORT wxArtProviderCache | |
57 | { | |
58 | public: | |
59 | bool GetBitmap(const wxString& full_id, wxBitmap* bmp); | |
60 | void PutBitmap(const wxString& full_id, const wxBitmap& bmp) | |
61 | { m_bitmapsHash[full_id] = bmp; } | |
62 | ||
63 | void Clear(); | |
9e7ed2b0 | 64 | |
57b0987b VS |
65 | static wxString ConstructHashID(const wxArtID& id, |
66 | const wxArtClient& client, | |
2aca4a98 VS |
67 | const wxSize& size); |
68 | ||
69 | private: | |
70 | wxArtProviderBitmapsHash m_bitmapsHash; | |
71 | }; | |
72 | ||
73 | bool wxArtProviderCache::GetBitmap(const wxString& full_id, wxBitmap* bmp) | |
74 | { | |
75 | wxArtProviderBitmapsHash::iterator entry = m_bitmapsHash.find(full_id); | |
76 | if ( entry == m_bitmapsHash.end() ) | |
77 | { | |
78 | return FALSE; | |
79 | } | |
80 | else | |
81 | { | |
82 | *bmp = entry->second; | |
83 | return TRUE; | |
84 | } | |
85 | } | |
86 | ||
87 | void wxArtProviderCache::Clear() | |
88 | { | |
e5f4aeb6 JS |
89 | // Hack to make the default provider link |
90 | // with the application | |
91 | g_ArtProviderModule = 0; | |
2aca4a98 VS |
92 | m_bitmapsHash.clear(); |
93 | } | |
94 | ||
95 | /*static*/ wxString wxArtProviderCache::ConstructHashID( | |
57b0987b VS |
96 | const wxArtID& id, const wxArtClient& client, |
97 | const wxSize& size) | |
2aca4a98 VS |
98 | { |
99 | wxString str; | |
57b0987b | 100 | str.Printf(wxT("%s-%s-%i-%i"), id.c_str(), client.c_str(), size.x, size.y); |
2aca4a98 VS |
101 | return str; |
102 | } | |
103 | ||
104 | ||
105 | // ---------------------------------------------------------------------------- | |
106 | // wxArtProvider class | |
107 | // ---------------------------------------------------------------------------- | |
108 | ||
109 | IMPLEMENT_ABSTRACT_CLASS(wxArtProvider, wxObject) | |
110 | ||
111 | wxArtProvidersList *wxArtProvider::sm_providers = NULL; | |
112 | wxArtProviderCache *wxArtProvider::sm_cache = NULL; | |
113 | ||
114 | /*static*/ void wxArtProvider::PushProvider(wxArtProvider *provider) | |
115 | { | |
116 | if ( !sm_providers ) | |
117 | { | |
118 | sm_providers = new wxArtProvidersList; | |
2aca4a98 VS |
119 | sm_cache = new wxArtProviderCache; |
120 | } | |
121 | ||
122 | sm_providers->Insert(provider); | |
c3357a03 | 123 | sm_cache->Clear(); |
2aca4a98 VS |
124 | } |
125 | ||
126 | /*static*/ bool wxArtProvider::PopProvider() | |
127 | { | |
128 | wxCHECK_MSG( sm_providers, FALSE, _T("no wxArtProvider exists") ); | |
129 | wxCHECK_MSG( sm_providers->GetCount() > 0, FALSE, _T("wxArtProviders stack is empty") ); | |
130 | ||
222ed1d6 MB |
131 | delete sm_providers->GetFirst()->GetData(); |
132 | sm_providers->Erase(sm_providers->GetFirst()); | |
2aca4a98 VS |
133 | sm_cache->Clear(); |
134 | return TRUE; | |
135 | } | |
136 | ||
137 | /*static*/ bool wxArtProvider::RemoveProvider(wxArtProvider *provider) | |
138 | { | |
139 | wxCHECK_MSG( sm_providers, FALSE, _T("no wxArtProvider exists") ); | |
140 | ||
141 | if ( sm_providers->DeleteObject(provider) ) | |
142 | { | |
222ed1d6 | 143 | delete provider; |
2aca4a98 VS |
144 | sm_cache->Clear(); |
145 | return TRUE; | |
146 | } | |
9e7ed2b0 | 147 | |
2aca4a98 VS |
148 | return FALSE; |
149 | } | |
150 | ||
151 | /*static*/ void wxArtProvider::CleanUpProviders() | |
152 | { | |
89c20ac1 | 153 | WX_CLEAR_LIST(wxArtProvidersList, *sm_providers); |
2aca4a98 VS |
154 | wxDELETE(sm_providers); |
155 | wxDELETE(sm_cache); | |
156 | } | |
157 | ||
57b0987b VS |
158 | /*static*/ wxBitmap wxArtProvider::GetBitmap(const wxArtID& id, |
159 | const wxArtClient& client, | |
2aca4a98 VS |
160 | const wxSize& size) |
161 | { | |
57b0987b VS |
162 | // safety-check against writing client,id,size instead of id,client,size: |
163 | wxASSERT_MSG( client.Last() == _T('C'), _T("invalid 'client' parameter") ); | |
164 | ||
2aca4a98 VS |
165 | wxCHECK_MSG( sm_providers, wxNullBitmap, _T("no wxArtProvider exists") ); |
166 | ||
57b0987b | 167 | wxString hashId = wxArtProviderCache::ConstructHashID(id, client, size); |
2aca4a98 VS |
168 | |
169 | wxBitmap bmp; | |
170 | if ( !sm_cache->GetBitmap(hashId, &bmp) ) | |
171 | { | |
222ed1d6 | 172 | for (wxArtProvidersList::compatibility_iterator node = sm_providers->GetFirst(); |
2aca4a98 VS |
173 | node; node = node->GetNext()) |
174 | { | |
57b0987b | 175 | bmp = node->GetData()->CreateBitmap(id, client, size); |
2aca4a98 | 176 | if ( bmp.Ok() ) |
3242feb1 | 177 | { |
cfb57561 | 178 | #if wxUSE_IMAGE |
9e7ed2b0 | 179 | if ( size != wxDefaultSize && |
3242feb1 VS |
180 | (bmp.GetWidth() != size.x || bmp.GetHeight() != size.y) ) |
181 | { | |
182 | wxImage img = bmp.ConvertToImage(); | |
183 | img.Rescale(size.x, size.y); | |
184 | bmp = wxBitmap(img); | |
185 | } | |
cfb57561 | 186 | #endif |
2aca4a98 | 187 | break; |
3242feb1 | 188 | } |
2aca4a98 | 189 | } |
3242feb1 | 190 | |
2aca4a98 VS |
191 | sm_cache->PutBitmap(hashId, bmp); |
192 | } | |
193 | ||
194 | return bmp; | |
195 | } | |
196 | ||
57b0987b VS |
197 | /*static*/ wxIcon wxArtProvider::GetIcon(const wxArtID& id, |
198 | const wxArtClient& client, | |
2aca4a98 VS |
199 | const wxSize& size) |
200 | { | |
201 | wxCHECK_MSG( sm_providers, wxNullIcon, _T("no wxArtProvider exists") ); | |
202 | ||
57b0987b | 203 | wxBitmap bmp = GetBitmap(id, client, size); |
25dd6997 | 204 | if ( !bmp.Ok() ) |
2aca4a98 | 205 | return wxNullIcon; |
2aca4a98 | 206 | |
25dd6997 VZ |
207 | wxIcon icon; |
208 | icon.CopyFromBitmap(bmp); | |
209 | return icon; | |
210 | } | |
2aca4a98 VS |
211 | |
212 | ||
213 | class wxArtProviderModule: public wxModule | |
214 | { | |
215 | public: | |
2b5f62a0 VZ |
216 | bool OnInit() |
217 | { | |
218 | wxArtProvider::InitStdProvider(); | |
219 | return TRUE; | |
220 | } | |
221 | void OnExit() | |
222 | { | |
223 | wxArtProvider::CleanUpProviders(); | |
224 | } | |
2aca4a98 VS |
225 | |
226 | DECLARE_DYNAMIC_CLASS(wxArtProviderModule) | |
227 | }; | |
228 | ||
229 | IMPLEMENT_DYNAMIC_CLASS(wxArtProviderModule, wxModule) |