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