(hopefully) final touches to wxArtProvider
[wxWidgets.git] / src / common / artprov.cpp
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
36
37 // ===========================================================================
38 // implementation
39 // ===========================================================================
40
41 #include "wx/listimpl.cpp"
42 WX_DECLARE_LIST(wxArtProvider, wxArtProvidersList);
43 WX_DEFINE_LIST(wxArtProvidersList);
44
45 // ----------------------------------------------------------------------------
46 // Cache class - stores already requested bitmaps
47 // ----------------------------------------------------------------------------
48
49 WX_DECLARE_STRING_HASH_MAP(wxBitmap, wxArtProviderBitmapsHash);
50
51 class WXDLLEXPORT wxArtProviderCache
52 {
53 public:
54 bool GetBitmap(const wxString& full_id, wxBitmap* bmp);
55 void PutBitmap(const wxString& full_id, const wxBitmap& bmp)
56 { m_bitmapsHash[full_id] = bmp; }
57
58 void Clear();
59
60 static wxString ConstructHashID(const wxArtID& id,
61 const wxArtClient& client,
62 const wxSize& size);
63
64 private:
65 wxArtProviderBitmapsHash m_bitmapsHash;
66 };
67
68 bool wxArtProviderCache::GetBitmap(const wxString& full_id, wxBitmap* bmp)
69 {
70 wxArtProviderBitmapsHash::iterator entry = m_bitmapsHash.find(full_id);
71 if ( entry == m_bitmapsHash.end() )
72 {
73 return FALSE;
74 }
75 else
76 {
77 *bmp = entry->second;
78 return TRUE;
79 }
80 }
81
82 void wxArtProviderCache::Clear()
83 {
84 m_bitmapsHash.clear();
85 }
86
87 /*static*/ wxString wxArtProviderCache::ConstructHashID(
88 const wxArtID& id, const wxArtClient& client,
89 const wxSize& size)
90 {
91 wxString str;
92 str.Printf(wxT("%s-%s-%i-%i"), id.c_str(), client.c_str(), size.x, size.y);
93 return str;
94 }
95
96
97 // ----------------------------------------------------------------------------
98 // wxArtProvider class
99 // ----------------------------------------------------------------------------
100
101 IMPLEMENT_ABSTRACT_CLASS(wxArtProvider, wxObject)
102
103 wxArtProvidersList *wxArtProvider::sm_providers = NULL;
104 wxArtProviderCache *wxArtProvider::sm_cache = NULL;
105
106 /*static*/ void wxArtProvider::PushProvider(wxArtProvider *provider)
107 {
108 if ( !sm_providers )
109 {
110 sm_providers = new wxArtProvidersList;
111 sm_providers->DeleteContents(TRUE);
112 sm_cache = new wxArtProviderCache;
113 }
114
115 sm_providers->Insert(provider);
116 }
117
118 /*static*/ bool wxArtProvider::PopProvider()
119 {
120 wxCHECK_MSG( sm_providers, FALSE, _T("no wxArtProvider exists") );
121 wxCHECK_MSG( sm_providers->GetCount() > 0, FALSE, _T("wxArtProviders stack is empty") );
122
123 sm_providers->DeleteNode(sm_providers->GetFirst());
124 sm_cache->Clear();
125 return TRUE;
126 }
127
128 /*static*/ bool wxArtProvider::RemoveProvider(wxArtProvider *provider)
129 {
130 wxCHECK_MSG( sm_providers, FALSE, _T("no wxArtProvider exists") );
131
132 if ( sm_providers->DeleteObject(provider) )
133 {
134 sm_cache->Clear();
135 return TRUE;
136 }
137
138 return FALSE;
139 }
140
141 /*static*/ void wxArtProvider::CleanUpProviders()
142 {
143 wxDELETE(sm_providers);
144 wxDELETE(sm_cache);
145 }
146
147 /*static*/ wxBitmap wxArtProvider::GetBitmap(const wxArtID& id,
148 const wxArtClient& client,
149 const wxSize& size)
150 {
151 // safety-check against writing client,id,size instead of id,client,size:
152 wxASSERT_MSG( client.Last() == _T('C'), _T("invalid 'client' parameter") );
153
154 wxCHECK_MSG( sm_providers, wxNullBitmap, _T("no wxArtProvider exists") );
155
156 wxString hashId = wxArtProviderCache::ConstructHashID(id, client, size);
157
158 wxBitmap bmp;
159 if ( !sm_cache->GetBitmap(hashId, &bmp) )
160 {
161 for (wxArtProvidersList::Node *node = sm_providers->GetFirst();
162 node; node = node->GetNext())
163 {
164 bmp = node->GetData()->CreateBitmap(id, client, size);
165 if ( bmp.Ok() )
166 break;
167 }
168 sm_cache->PutBitmap(hashId, bmp);
169 }
170
171 return bmp;
172 }
173
174 /*static*/ wxIcon wxArtProvider::GetIcon(const wxArtID& id,
175 const wxArtClient& client,
176 const wxSize& size)
177 {
178 wxCHECK_MSG( sm_providers, wxNullIcon, _T("no wxArtProvider exists") );
179
180 wxBitmap bmp = GetBitmap(id, client, size);
181 if ( bmp.Ok() )
182 {
183 wxIcon icon;
184 icon.CopyFromBitmap(bmp);
185 return icon;
186 }
187 else
188 {
189 return wxNullIcon;
190 }
191 }
192
193
194
195 class wxArtProviderModule: public wxModule
196 {
197 public:
198 bool OnInit() { return TRUE; }
199 void OnExit() { wxArtProvider::CleanUpProviders(); }
200
201 DECLARE_DYNAMIC_CLASS(wxArtProviderModule)
202 };
203
204 IMPLEMENT_DYNAMIC_CLASS(wxArtProviderModule, wxModule)