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__)
23 #include "wx/artprov.h"
28 #include "wx/hashmap.h"
30 #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 // ----------------------------------------------------------------------------
103 // wxArtProvider ctors/dtor
104 // ----------------------------------------------------------------------------
106 wxArtProvider::~wxArtProvider()
111 // ----------------------------------------------------------------------------
112 // wxArtProvider operations on provider stack
113 // ----------------------------------------------------------------------------
115 /*static*/ void wxArtProvider::CommonAddingProvider()
119 sm_providers
= new wxArtProvidersList
;
120 sm_cache
= new wxArtProviderCache
;
126 /*static*/ void wxArtProvider::Push(wxArtProvider
*provider
)
128 CommonAddingProvider();
129 sm_providers
->Insert(provider
);
132 /*static*/ void wxArtProvider::Insert(wxArtProvider
*provider
)
134 CommonAddingProvider();
135 sm_providers
->Append(provider
);
138 /*static*/ bool wxArtProvider::Pop()
140 wxCHECK_MSG( sm_providers
, false, _T("no wxArtProvider exists") );
141 wxCHECK_MSG( !sm_providers
->empty(), false, _T("wxArtProviders stack is empty") );
143 delete sm_providers
->GetFirst()->GetData();
148 /*static*/ bool wxArtProvider::Remove(wxArtProvider
*provider
)
150 wxCHECK_MSG( sm_providers
, false, _T("no wxArtProvider exists") );
152 if ( sm_providers
->DeleteObject(provider
) )
161 /*static*/ bool wxArtProvider::Delete(wxArtProvider
*provider
)
163 // provider will remove itself from the stack in its dtor
169 /*static*/ void wxArtProvider::CleanUpProviders()
173 while ( !sm_providers
->empty() )
174 delete *sm_providers
->begin();
184 // ----------------------------------------------------------------------------
185 // wxArtProvider: retrieving bitmaps/icons
186 // ----------------------------------------------------------------------------
188 /*static*/ wxBitmap
wxArtProvider::GetBitmap(const wxArtID
& id
,
189 const wxArtClient
& client
,
192 // safety-check against writing client,id,size instead of id,client,size:
193 wxASSERT_MSG( client
.Last() == _T('C'), _T("invalid 'client' parameter") );
195 wxCHECK_MSG( sm_providers
, wxNullBitmap
, _T("no wxArtProvider exists") );
197 wxString hashId
= wxArtProviderCache::ConstructHashID(id
, client
, size
);
200 if ( !sm_cache
->GetBitmap(hashId
, &bmp
) )
202 for (wxArtProvidersList::compatibility_iterator node
= sm_providers
->GetFirst();
203 node
; node
= node
->GetNext())
205 bmp
= node
->GetData()->CreateBitmap(id
, client
, size
);
208 #if wxUSE_IMAGE && (!defined(__WXMSW__) || wxUSE_WXDIB)
209 if ( size
!= wxDefaultSize
&&
210 (bmp
.GetWidth() != size
.x
|| bmp
.GetHeight() != size
.y
) )
212 wxImage img
= bmp
.ConvertToImage();
213 img
.Rescale(size
.x
, size
.y
);
221 sm_cache
->PutBitmap(hashId
, bmp
);
227 /*static*/ wxIcon
wxArtProvider::GetIcon(const wxArtID
& id
,
228 const wxArtClient
& client
,
231 wxCHECK_MSG( sm_providers
, wxNullIcon
, _T("no wxArtProvider exists") );
233 wxBitmap bmp
= GetBitmap(id
, client
, size
);
238 icon
.CopyFromBitmap(bmp
);
242 #if defined(__WXGTK20__) && !defined(__WXUNIVERSAL__)
243 #include "wx/gtk/private.h"
244 extern GtkIconSize
wxArtClientToIconSize(const wxArtClient
& client
);
245 #endif // defined(__WXGTK20__) && !defined(__WXUNIVERSAL__)
247 /*static*/ wxSize
wxArtProvider::GetSizeHint(const wxArtClient
& client
,
248 bool platform_dependent
)
250 if (!platform_dependent
)
252 wxArtProvidersList::compatibility_iterator node
= sm_providers
->GetFirst();
254 return node
->GetData()->DoGetSizeHint(client
);
257 // else return platform dependent size
259 #if defined(__WXGTK20__) && !defined(__WXUNIVERSAL__)
260 // Gtk has specific sizes for each client, see artgtk.cpp
261 GtkIconSize gtk_size
= wxArtClientToIconSize(client
);
262 // no size hints for this client
263 if (gtk_size
== GTK_ICON_SIZE_INVALID
)
264 return wxDefaultSize
;
266 gtk_icon_size_lookup( gtk_size
, &width
, &height
);
267 return wxSize(width
, height
);
269 // NB: These size hints may have to be adjusted per platform
270 if (client
== wxART_TOOLBAR
)
271 return wxSize(16, 15);
272 else if (client
== wxART_MENU
)
273 return wxSize(16, 15);
274 else if (client
== wxART_FRAME_ICON
)
275 return wxSize(16, 15);
276 else if (client
== wxART_CMN_DIALOG
|| client
== wxART_MESSAGE_BOX
)
277 return wxSize(32, 32);
278 else if (client
== wxART_HELP_BROWSER
)
279 return wxSize(16, 15);
280 else if (client
== wxART_BUTTON
)
281 return wxSize(16, 15);
282 else // wxART_OTHER or perhaps a user's client, no specified size
283 return wxDefaultSize
;
284 #endif // GTK+ 2/else
287 // ----------------------------------------------------------------------------
288 // deprecated wxArtProvider methods
289 // ----------------------------------------------------------------------------
291 #if WXWIN_COMPATIBILITY_2_6
293 /* static */ void wxArtProvider::PushProvider(wxArtProvider
*provider
)
298 /* static */ void wxArtProvider::InsertProvider(wxArtProvider
*provider
)
303 /* static */ bool wxArtProvider::PopProvider()
308 /* static */ bool wxArtProvider::RemoveProvider(wxArtProvider
*provider
)
310 // RemoveProvider() used to delete the provider being removed so this is
311 // not a typo, we must call Delete() and not Remove() here
312 return Delete(provider
);
315 #endif // WXWIN_COMPATIBILITY_2_6
317 // ============================================================================
318 // wxArtProviderModule
319 // ============================================================================
321 class wxArtProviderModule
: public wxModule
326 wxArtProvider::InitStdProvider();
327 wxArtProvider::InitNativeProvider();
332 wxArtProvider::CleanUpProviders();
335 DECLARE_DYNAMIC_CLASS(wxArtProviderModule
)
338 IMPLEMENT_DYNAMIC_CLASS(wxArtProviderModule
, wxModule
)