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