]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/common/artprov.cpp
use best size instead of hard coded 80*26 in SetSize(wxSIZE_AUTO)
[wxWidgets.git] / src / common / artprov.cpp
... / ...
CommitLineData
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#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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#if wxUSE_IMAGE
36#include "wx/image.h"
37#endif
38
39// For the purposes of forcing this module to link
40extern char g_ArtProviderModule;
41
42// ===========================================================================
43// implementation
44// ===========================================================================
45
46#include "wx/listimpl.cpp"
47WX_DECLARE_LIST(wxArtProvider, wxArtProvidersList);
48WX_DEFINE_LIST(wxArtProvidersList);
49
50// ----------------------------------------------------------------------------
51// Cache class - stores already requested bitmaps
52// ----------------------------------------------------------------------------
53
54WX_DECLARE_EXPORTED_STRING_HASH_MAP(wxBitmap, wxArtProviderBitmapsHash);
55
56class WXDLLEXPORT wxArtProviderCache
57{
58public:
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();
64
65 static wxString ConstructHashID(const wxArtID& id,
66 const wxArtClient& client,
67 const wxSize& size);
68
69private:
70 wxArtProviderBitmapsHash m_bitmapsHash;
71};
72
73bool 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
87void wxArtProviderCache::Clear()
88{
89 // Hack to make the default provider link
90 // with the application
91 g_ArtProviderModule = 0;
92 m_bitmapsHash.clear();
93}
94
95/*static*/ wxString wxArtProviderCache::ConstructHashID(
96 const wxArtID& id, const wxArtClient& client,
97 const wxSize& size)
98{
99 wxString str;
100 str.Printf(wxT("%s-%s-%i-%i"), id.c_str(), client.c_str(), size.x, size.y);
101 return str;
102}
103
104
105// ----------------------------------------------------------------------------
106// wxArtProvider class
107// ----------------------------------------------------------------------------
108
109IMPLEMENT_ABSTRACT_CLASS(wxArtProvider, wxObject)
110
111wxArtProvidersList *wxArtProvider::sm_providers = NULL;
112wxArtProviderCache *wxArtProvider::sm_cache = NULL;
113
114/*static*/ void wxArtProvider::PushProvider(wxArtProvider *provider)
115{
116 if ( !sm_providers )
117 {
118 sm_providers = new wxArtProvidersList;
119 sm_cache = new wxArtProviderCache;
120 }
121
122 sm_providers->Insert(provider);
123 sm_cache->Clear();
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
131 delete sm_providers->GetFirst()->GetData();
132 sm_providers->Erase(sm_providers->GetFirst());
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 {
143 delete provider;
144 sm_cache->Clear();
145 return TRUE;
146 }
147
148 return FALSE;
149}
150
151/*static*/ void wxArtProvider::CleanUpProviders()
152{
153 WX_CLEAR_LIST(wxArtProvidersList, *sm_providers);
154 wxDELETE(sm_providers);
155 wxDELETE(sm_cache);
156}
157
158/*static*/ wxBitmap wxArtProvider::GetBitmap(const wxArtID& id,
159 const wxArtClient& client,
160 const wxSize& size)
161{
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
165 wxCHECK_MSG( sm_providers, wxNullBitmap, _T("no wxArtProvider exists") );
166
167 wxString hashId = wxArtProviderCache::ConstructHashID(id, client, size);
168
169 wxBitmap bmp;
170 if ( !sm_cache->GetBitmap(hashId, &bmp) )
171 {
172 for (wxArtProvidersList::compatibility_iterator node = sm_providers->GetFirst();
173 node; node = node->GetNext())
174 {
175 bmp = node->GetData()->CreateBitmap(id, client, size);
176 if ( bmp.Ok() )
177 {
178#if wxUSE_IMAGE
179 if ( size != wxDefaultSize &&
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 }
186#endif
187 break;
188 }
189 }
190
191 sm_cache->PutBitmap(hashId, bmp);
192 }
193
194 return bmp;
195}
196
197/*static*/ wxIcon wxArtProvider::GetIcon(const wxArtID& id,
198 const wxArtClient& client,
199 const wxSize& size)
200{
201 wxCHECK_MSG( sm_providers, wxNullIcon, _T("no wxArtProvider exists") );
202
203 wxBitmap bmp = GetBitmap(id, client, size);
204 if ( !bmp.Ok() )
205 return wxNullIcon;
206
207 wxIcon icon;
208 icon.CopyFromBitmap(bmp);
209 return icon;
210}
211
212
213class wxArtProviderModule: public wxModule
214{
215public:
216 bool OnInit()
217 {
218 wxArtProvider::InitStdProvider();
219 return TRUE;
220 }
221 void OnExit()
222 {
223 wxArtProvider::CleanUpProviders();
224 }
225
226 DECLARE_DYNAMIC_CLASS(wxArtProviderModule)
227};
228
229IMPLEMENT_DYNAMIC_CLASS(wxArtProviderModule, wxModule)