]> git.saurik.com Git - wxWidgets.git/blame - src/common/artprov.cpp
updated samples/html/widget to avoid confusion
[wxWidgets.git] / src / common / artprov.cpp
CommitLineData
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
36
37// ===========================================================================
38// implementation
39// ===========================================================================
40
41#include "wx/listimpl.cpp"
42WX_DECLARE_LIST(wxArtProvider, wxArtProvidersList);
43WX_DEFINE_LIST(wxArtProvidersList);
44
45// ----------------------------------------------------------------------------
46// Cache class - stores already requested bitmaps
47// ----------------------------------------------------------------------------
48
49WX_DECLARE_STRING_HASH_MAP(wxBitmap, wxArtProviderBitmapsHash);
50
51class WXDLLEXPORT wxArtProviderCache
52{
53public:
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 wxArtDomain& domain,
61 const wxArtID& id,
62 const wxSize& size);
63
64private:
65 wxArtProviderBitmapsHash m_bitmapsHash;
66};
67
68bool 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
82void wxArtProviderCache::Clear()
83{
84 m_bitmapsHash.clear();
85}
86
87/*static*/ wxString wxArtProviderCache::ConstructHashID(
88 const wxArtDomain& domain,
89 const wxArtID& id, const wxSize& size)
90{
91 wxString str;
92 str.Printf(wxT("%s-%s-%i-%i"), domain.c_str(), id.c_str(), size.x, size.y);
93 return str;
94}
95
96
97// ----------------------------------------------------------------------------
98// wxArtProvider class
99// ----------------------------------------------------------------------------
100
101IMPLEMENT_ABSTRACT_CLASS(wxArtProvider, wxObject)
102
103wxArtProvidersList *wxArtProvider::sm_providers = NULL;
104wxArtProviderCache *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 wxArtDomain& domain,
148 const wxArtID& id,
149 const wxSize& size)
150{
151 wxCHECK_MSG( sm_providers, wxNullBitmap, _T("no wxArtProvider exists") );
152
153 wxString hashId = wxArtProviderCache::ConstructHashID(domain, id, size);
154
155 wxBitmap bmp;
156 if ( !sm_cache->GetBitmap(hashId, &bmp) )
157 {
158 for (wxArtProvidersList::Node *node = sm_providers->GetFirst();
159 node; node = node->GetNext())
160 {
161 bmp = node->GetData()->CreateBitmap(domain, id, size);
162 if ( bmp.Ok() )
163 break;
164 }
165 sm_cache->PutBitmap(hashId, bmp);
166 }
167
168 return bmp;
169}
170
171/*static*/ wxIcon wxArtProvider::GetIcon(const wxArtDomain& domain,
172 const wxArtID& id,
173 const wxSize& size)
174{
175 wxCHECK_MSG( sm_providers, wxNullIcon, _T("no wxArtProvider exists") );
176
177 wxBitmap bmp = GetBitmap(domain, id, size);
178 if ( bmp.Ok() )
179 {
180 wxIcon icon;
181 icon.CopyFromBitmap(bmp);
182 return icon;
183 }
184 else
185 {
186 return wxNullIcon;
187 }
188}
189
190
191
192class wxArtProviderModule: public wxModule
193{
194public:
195 bool OnInit() { return TRUE; }
196 void OnExit() { wxArtProvider::CleanUpProviders(); }
197
198 DECLARE_DYNAMIC_CLASS(wxArtProviderModule)
199};
200
201IMPLEMENT_DYNAMIC_CLASS(wxArtProviderModule, wxModule)