]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/common/artprov.cpp
updates from Adrián González Alba
[wxWidgets.git] / src / common / artprov.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/common/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// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#if defined(__BORLANDC__)
20 #pragma hdrstop
21#endif
22
23#ifndef WX_PRECOMP
24 #include "wx/list.h"
25 #include "wx/log.h"
26 #include "wx/hashmap.h"
27 #include "wx/image.h"
28#endif
29
30#include "wx/artprov.h"
31#include "wx/module.h"
32
33// ===========================================================================
34// implementation
35// ===========================================================================
36
37#include "wx/listimpl.cpp"
38WX_DECLARE_LIST(wxArtProvider, wxArtProvidersList);
39WX_DEFINE_LIST(wxArtProvidersList)
40
41// ----------------------------------------------------------------------------
42// Cache class - stores already requested bitmaps
43// ----------------------------------------------------------------------------
44
45WX_DECLARE_EXPORTED_STRING_HASH_MAP(wxBitmap, wxArtProviderBitmapsHash);
46
47class WXDLLEXPORT wxArtProviderCache
48{
49public:
50 bool GetBitmap(const wxString& full_id, wxBitmap* bmp);
51 void PutBitmap(const wxString& full_id, const wxBitmap& bmp)
52 { m_bitmapsHash[full_id] = bmp; }
53
54 void Clear();
55
56 static wxString ConstructHashID(const wxArtID& id,
57 const wxArtClient& client,
58 const wxSize& size);
59
60private:
61 wxArtProviderBitmapsHash m_bitmapsHash;
62};
63
64bool wxArtProviderCache::GetBitmap(const wxString& full_id, wxBitmap* bmp)
65{
66 wxArtProviderBitmapsHash::iterator entry = m_bitmapsHash.find(full_id);
67 if ( entry == m_bitmapsHash.end() )
68 {
69 return false;
70 }
71 else
72 {
73 *bmp = entry->second;
74 return true;
75 }
76}
77
78void wxArtProviderCache::Clear()
79{
80 m_bitmapsHash.clear();
81}
82
83/*static*/ wxString wxArtProviderCache::ConstructHashID(
84 const wxArtID& id, const wxArtClient& client,
85 const wxSize& size)
86{
87 wxString str;
88 str.Printf(wxT("%s-%s-%i-%i"), id.c_str(), client.c_str(), size.x, size.y);
89 return str;
90}
91
92
93// ----------------------------------------------------------------------------
94// wxArtProvider class
95// ----------------------------------------------------------------------------
96
97IMPLEMENT_ABSTRACT_CLASS(wxArtProvider, wxObject)
98
99wxArtProvidersList *wxArtProvider::sm_providers = NULL;
100wxArtProviderCache *wxArtProvider::sm_cache = NULL;
101
102/*static*/ void wxArtProvider::PushProvider(wxArtProvider *provider)
103{
104 if ( !sm_providers )
105 {
106 sm_providers = new wxArtProvidersList;
107 sm_cache = new wxArtProviderCache;
108 }
109
110 sm_providers->Insert(provider);
111 sm_cache->Clear();
112}
113
114/*static*/ bool wxArtProvider::PopProvider()
115{
116 wxCHECK_MSG( sm_providers, false, _T("no wxArtProvider exists") );
117 wxCHECK_MSG( sm_providers->GetCount() > 0, false, _T("wxArtProviders stack is empty") );
118
119 delete sm_providers->GetFirst()->GetData();
120 sm_providers->Erase(sm_providers->GetFirst());
121 sm_cache->Clear();
122 return true;
123}
124
125/*static*/ bool wxArtProvider::RemoveProvider(wxArtProvider *provider)
126{
127 wxCHECK_MSG( sm_providers, false, _T("no wxArtProvider exists") );
128
129 if ( sm_providers->DeleteObject(provider) )
130 {
131 delete provider;
132 sm_cache->Clear();
133 return true;
134 }
135
136 return false;
137}
138
139/*static*/ void wxArtProvider::CleanUpProviders()
140{
141 WX_CLEAR_LIST(wxArtProvidersList, *sm_providers);
142 wxDELETE(sm_providers);
143 wxDELETE(sm_cache);
144}
145
146/*static*/ wxBitmap wxArtProvider::GetBitmap(const wxArtID& id,
147 const wxArtClient& client,
148 const wxSize& size)
149{
150 // safety-check against writing client,id,size instead of id,client,size:
151 wxASSERT_MSG( client.Last() == _T('C'), _T("invalid 'client' parameter") );
152
153 wxCHECK_MSG( sm_providers, wxNullBitmap, _T("no wxArtProvider exists") );
154
155 wxString hashId = wxArtProviderCache::ConstructHashID(id, client, size);
156
157 wxBitmap bmp;
158 if ( !sm_cache->GetBitmap(hashId, &bmp) )
159 {
160 for (wxArtProvidersList::compatibility_iterator node = sm_providers->GetFirst();
161 node; node = node->GetNext())
162 {
163 bmp = node->GetData()->CreateBitmap(id, client, size);
164 if ( bmp.Ok() )
165 {
166#if wxUSE_IMAGE && (!defined(__WXMSW__) || wxUSE_WXDIB)
167 if ( size != wxDefaultSize &&
168 (bmp.GetWidth() != size.x || bmp.GetHeight() != size.y) )
169 {
170 wxImage img = bmp.ConvertToImage();
171 img.Rescale(size.x, size.y);
172 bmp = wxBitmap(img);
173 }
174#endif
175 break;
176 }
177 }
178
179 sm_cache->PutBitmap(hashId, bmp);
180 }
181
182 return bmp;
183}
184
185/*static*/ wxIcon wxArtProvider::GetIcon(const wxArtID& id,
186 const wxArtClient& client,
187 const wxSize& size)
188{
189 wxCHECK_MSG( sm_providers, wxNullIcon, _T("no wxArtProvider exists") );
190
191 wxBitmap bmp = GetBitmap(id, client, size);
192 if ( !bmp.Ok() )
193 return wxNullIcon;
194
195 wxIcon icon;
196 icon.CopyFromBitmap(bmp);
197 return icon;
198}
199
200#if defined(__WXGTK20__) && !defined(__WXUNIVERSAL__)
201 #include "wx/gtk/private.h"
202 extern GtkIconSize wxArtClientToIconSize(const wxArtClient& client);
203#endif // defined(__WXGTK20__) && !defined(__WXUNIVERSAL__)
204
205/*static*/ wxSize wxArtProvider::GetSizeHint(const wxArtClient& client,
206 bool platform_dependent)
207{
208 if (!platform_dependent)
209 {
210 wxArtProvidersList::compatibility_iterator node = sm_providers->GetFirst();
211 if (node)
212 return node->GetData()->DoGetSizeHint(client);
213 }
214
215 // else return platform dependent size
216
217#if defined(__WXGTK20__) && !defined(__WXUNIVERSAL__)
218 // Gtk has specific sizes for each client, see artgtk.cpp
219 GtkIconSize gtk_size = wxArtClientToIconSize(client);
220 // no size hints for this client
221 if (gtk_size == GTK_ICON_SIZE_INVALID)
222 return wxDefaultSize;
223 gint width, height;
224 gtk_icon_size_lookup( gtk_size, &width, &height);
225 return wxSize(width, height);
226#else // !GTK+ 2
227 // NB: These size hints may have to be adjusted per platform
228 if (client == wxART_TOOLBAR)
229 return wxSize(16, 15);
230 else if (client == wxART_MENU)
231 return wxSize(16, 15);
232 else if (client == wxART_FRAME_ICON)
233 return wxSize(16, 15);
234 else if (client == wxART_CMN_DIALOG || client == wxART_MESSAGE_BOX)
235 return wxSize(32, 32);
236 else if (client == wxART_HELP_BROWSER)
237 return wxSize(16, 15);
238 else if (client == wxART_BUTTON)
239 return wxSize(16, 15);
240 else // wxART_OTHER or perhaps a user's client, no specified size
241 return wxDefaultSize;
242#endif // GTK+ 2/else
243}
244
245
246class wxArtProviderModule: public wxModule
247{
248public:
249 bool OnInit()
250 {
251 wxArtProvider::InitStdProvider();
252 wxArtProvider::InitNativeProvider();
253 return true;
254 }
255 void OnExit()
256 {
257 wxArtProvider::CleanUpProviders();
258 }
259
260 DECLARE_DYNAMIC_CLASS(wxArtProviderModule)
261};
262
263IMPLEMENT_DYNAMIC_CLASS(wxArtProviderModule, wxModule)