]>
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" | |
35 | ||
e5f4aeb6 JS |
36 | // For the purposes of forcing this module to link |
37 | extern char g_ArtProviderModule; | |
2aca4a98 VS |
38 | |
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 | ||
51 | WX_DECLARE_STRING_HASH_MAP(wxBitmap, wxArtProviderBitmapsHash); | |
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(); | |
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 | { | |
75 | return FALSE; | |
76 | } | |
77 | else | |
78 | { | |
79 | *bmp = entry->second; | |
80 | return TRUE; | |
81 | } | |
82 | } | |
83 | ||
84 | void wxArtProviderCache::Clear() | |
85 | { | |
e5f4aeb6 JS |
86 | // Hack to make the default provider link |
87 | // with the application | |
88 | g_ArtProviderModule = 0; | |
2aca4a98 VS |
89 | m_bitmapsHash.clear(); |
90 | } | |
91 | ||
92 | /*static*/ wxString wxArtProviderCache::ConstructHashID( | |
57b0987b VS |
93 | const wxArtID& id, const wxArtClient& client, |
94 | const wxSize& size) | |
2aca4a98 VS |
95 | { |
96 | wxString str; | |
57b0987b | 97 | str.Printf(wxT("%s-%s-%i-%i"), id.c_str(), client.c_str(), size.x, size.y); |
2aca4a98 VS |
98 | return str; |
99 | } | |
100 | ||
101 | ||
102 | // ---------------------------------------------------------------------------- | |
103 | // wxArtProvider class | |
104 | // ---------------------------------------------------------------------------- | |
105 | ||
106 | IMPLEMENT_ABSTRACT_CLASS(wxArtProvider, wxObject) | |
107 | ||
108 | wxArtProvidersList *wxArtProvider::sm_providers = NULL; | |
109 | wxArtProviderCache *wxArtProvider::sm_cache = NULL; | |
110 | ||
111 | /*static*/ void wxArtProvider::PushProvider(wxArtProvider *provider) | |
112 | { | |
113 | if ( !sm_providers ) | |
114 | { | |
115 | sm_providers = new wxArtProvidersList; | |
116 | sm_providers->DeleteContents(TRUE); | |
117 | sm_cache = new wxArtProviderCache; | |
118 | } | |
119 | ||
120 | sm_providers->Insert(provider); | |
121 | } | |
122 | ||
123 | /*static*/ bool wxArtProvider::PopProvider() | |
124 | { | |
125 | wxCHECK_MSG( sm_providers, FALSE, _T("no wxArtProvider exists") ); | |
126 | wxCHECK_MSG( sm_providers->GetCount() > 0, FALSE, _T("wxArtProviders stack is empty") ); | |
127 | ||
128 | sm_providers->DeleteNode(sm_providers->GetFirst()); | |
129 | sm_cache->Clear(); | |
130 | return TRUE; | |
131 | } | |
132 | ||
133 | /*static*/ bool wxArtProvider::RemoveProvider(wxArtProvider *provider) | |
134 | { | |
135 | wxCHECK_MSG( sm_providers, FALSE, _T("no wxArtProvider exists") ); | |
136 | ||
137 | if ( sm_providers->DeleteObject(provider) ) | |
138 | { | |
139 | sm_cache->Clear(); | |
140 | return TRUE; | |
141 | } | |
142 | ||
143 | return FALSE; | |
144 | } | |
145 | ||
146 | /*static*/ void wxArtProvider::CleanUpProviders() | |
147 | { | |
148 | wxDELETE(sm_providers); | |
149 | wxDELETE(sm_cache); | |
150 | } | |
151 | ||
57b0987b VS |
152 | /*static*/ wxBitmap wxArtProvider::GetBitmap(const wxArtID& id, |
153 | const wxArtClient& client, | |
2aca4a98 VS |
154 | const wxSize& size) |
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 | ||
57b0987b | 161 | wxString hashId = wxArtProviderCache::ConstructHashID(id, client, size); |
2aca4a98 VS |
162 | |
163 | wxBitmap bmp; | |
164 | if ( !sm_cache->GetBitmap(hashId, &bmp) ) | |
165 | { | |
166 | for (wxArtProvidersList::Node *node = sm_providers->GetFirst(); | |
167 | node; node = node->GetNext()) | |
168 | { | |
57b0987b | 169 | bmp = node->GetData()->CreateBitmap(id, client, size); |
2aca4a98 VS |
170 | if ( bmp.Ok() ) |
171 | break; | |
172 | } | |
173 | sm_cache->PutBitmap(hashId, bmp); | |
174 | } | |
175 | ||
176 | return bmp; | |
177 | } | |
178 | ||
57b0987b VS |
179 | /*static*/ wxIcon wxArtProvider::GetIcon(const wxArtID& id, |
180 | const wxArtClient& client, | |
2aca4a98 VS |
181 | const wxSize& size) |
182 | { | |
183 | wxCHECK_MSG( sm_providers, wxNullIcon, _T("no wxArtProvider exists") ); | |
184 | ||
57b0987b | 185 | wxBitmap bmp = GetBitmap(id, client, size); |
2aca4a98 VS |
186 | if ( bmp.Ok() ) |
187 | { | |
188 | wxIcon icon; | |
189 | icon.CopyFromBitmap(bmp); | |
190 | return icon; | |
191 | } | |
192 | else | |
193 | { | |
194 | return wxNullIcon; | |
195 | } | |
196 | } | |
197 | ||
198 | ||
199 | ||
200 | class wxArtProviderModule: public wxModule | |
201 | { | |
202 | public: | |
203 | bool OnInit() { return TRUE; } | |
204 | void OnExit() { wxArtProvider::CleanUpProviders(); } | |
205 | ||
206 | DECLARE_DYNAMIC_CLASS(wxArtProviderModule) | |
207 | }; | |
208 | ||
209 | IMPLEMENT_DYNAMIC_CLASS(wxArtProviderModule, wxModule) |