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