1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/artprov.cpp
3 // Purpose: wxArtProvider class
4 // Author: Vaclav Slavik
8 // Copyright: (c) Vaclav Slavik
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ---------------------------------------------------------------------------
14 // ---------------------------------------------------------------------------
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
19 #if defined(__BORLANDC__)
26 #include "wx/hashmap.h"
30 #include "wx/artprov.h"
31 #include "wx/module.h"
33 // ===========================================================================
35 // ===========================================================================
37 #include "wx/listimpl.cpp"
38 WX_DECLARE_LIST(wxArtProvider
, wxArtProvidersList
);
39 WX_DEFINE_LIST(wxArtProvidersList
)
41 // ----------------------------------------------------------------------------
42 // Cache class - stores already requested bitmaps
43 // ----------------------------------------------------------------------------
45 WX_DECLARE_EXPORTED_STRING_HASH_MAP(wxBitmap
, wxArtProviderBitmapsHash
);
47 class WXDLLEXPORT wxArtProviderCache
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
; }
56 static wxString
ConstructHashID(const wxArtID
& id
,
57 const wxArtClient
& client
,
61 wxArtProviderBitmapsHash m_bitmapsHash
;
64 bool wxArtProviderCache::GetBitmap(const wxString
& full_id
, wxBitmap
* bmp
)
66 wxArtProviderBitmapsHash::iterator entry
= m_bitmapsHash
.find(full_id
);
67 if ( entry
== m_bitmapsHash
.end() )
78 void wxArtProviderCache::Clear()
80 m_bitmapsHash
.clear();
83 /*static*/ wxString
wxArtProviderCache::ConstructHashID(
84 const wxArtID
& id
, const wxArtClient
& client
,
88 str
.Printf(wxT("%s-%s-%i-%i"), id
.c_str(), client
.c_str(), size
.x
, size
.y
);
93 // ----------------------------------------------------------------------------
94 // wxArtProvider class
95 // ----------------------------------------------------------------------------
97 IMPLEMENT_ABSTRACT_CLASS(wxArtProvider
, wxObject
)
99 wxArtProvidersList
*wxArtProvider::sm_providers
= NULL
;
100 wxArtProviderCache
*wxArtProvider::sm_cache
= NULL
;
102 /*static*/ void wxArtProvider::PushProvider(wxArtProvider
*provider
)
106 sm_providers
= new wxArtProvidersList
;
107 sm_cache
= new wxArtProviderCache
;
110 sm_providers
->Insert(provider
);
114 /*static*/ bool wxArtProvider::PopProvider()
116 wxCHECK_MSG( sm_providers
, false, _T("no wxArtProvider exists") );
117 wxCHECK_MSG( sm_providers
->GetCount() > 0, false, _T("wxArtProviders stack is empty") );
119 delete sm_providers
->GetFirst()->GetData();
120 sm_providers
->Erase(sm_providers
->GetFirst());
125 /*static*/ bool wxArtProvider::RemoveProvider(wxArtProvider
*provider
)
127 wxCHECK_MSG( sm_providers
, false, _T("no wxArtProvider exists") );
129 if ( sm_providers
->DeleteObject(provider
) )
139 /*static*/ void wxArtProvider::CleanUpProviders()
141 WX_CLEAR_LIST(wxArtProvidersList
, *sm_providers
);
142 wxDELETE(sm_providers
);
146 /*static*/ wxBitmap
wxArtProvider::GetBitmap(const wxArtID
& id
,
147 const wxArtClient
& client
,
150 // safety-check against writing client,id,size instead of id,client,size:
151 wxASSERT_MSG( client
.Last() == _T('C'), _T("invalid 'client' parameter") );
153 wxCHECK_MSG( sm_providers
, wxNullBitmap
, _T("no wxArtProvider exists") );
155 wxString hashId
= wxArtProviderCache::ConstructHashID(id
, client
, size
);
158 if ( !sm_cache
->GetBitmap(hashId
, &bmp
) )
160 for (wxArtProvidersList::compatibility_iterator node
= sm_providers
->GetFirst();
161 node
; node
= node
->GetNext())
163 bmp
= node
->GetData()->CreateBitmap(id
, client
, size
);
166 #if wxUSE_IMAGE && (!defined(__WXMSW__) || wxUSE_WXDIB)
167 if ( size
!= wxDefaultSize
&&
168 (bmp
.GetWidth() != size
.x
|| bmp
.GetHeight() != size
.y
) )
170 wxImage img
= bmp
.ConvertToImage();
171 img
.Rescale(size
.x
, size
.y
);
179 sm_cache
->PutBitmap(hashId
, bmp
);
185 /*static*/ wxIcon
wxArtProvider::GetIcon(const wxArtID
& id
,
186 const wxArtClient
& client
,
189 wxCHECK_MSG( sm_providers
, wxNullIcon
, _T("no wxArtProvider exists") );
191 wxBitmap bmp
= GetBitmap(id
, client
, size
);
196 icon
.CopyFromBitmap(bmp
);
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__)
205 /*static*/ wxSize
wxArtProvider::GetSizeHint(const wxArtClient
& client
,
206 bool platform_dependent
)
208 if (!platform_dependent
)
210 wxArtProvidersList::compatibility_iterator node
= sm_providers
->GetFirst();
212 return node
->GetData()->DoGetSizeHint(client
);
215 // else return platform dependent size
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
;
224 gtk_icon_size_lookup( gtk_size
, &width
, &height
);
225 return wxSize(width
, height
);
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
246 class wxArtProviderModule
: public wxModule
251 wxArtProvider::InitStdProvider();
252 wxArtProvider::InitNativeProvider();
257 wxArtProvider::CleanUpProviders();
260 DECLARE_DYNAMIC_CLASS(wxArtProviderModule
)
263 IMPLEMENT_DYNAMIC_CLASS(wxArtProviderModule
, wxModule
)