1 /////////////////////////////////////////////////////////////////////////////
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__)
28 #include "wx/artprov.h"
29 #include "wx/hashmap.h"
30 #include "wx/module.h"
35 // ===========================================================================
37 // ===========================================================================
39 #include "wx/listimpl.cpp"
40 WX_DECLARE_LIST(wxArtProvider
, wxArtProvidersList
);
41 WX_DEFINE_LIST(wxArtProvidersList
);
43 // ----------------------------------------------------------------------------
44 // Cache class - stores already requested bitmaps
45 // ----------------------------------------------------------------------------
47 WX_DECLARE_EXPORTED_STRING_HASH_MAP(wxBitmap
, wxArtProviderBitmapsHash
);
49 class WXDLLEXPORT wxArtProviderCache
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
; }
58 static wxString
ConstructHashID(const wxArtID
& id
,
59 const wxArtClient
& client
,
63 wxArtProviderBitmapsHash m_bitmapsHash
;
66 bool wxArtProviderCache::GetBitmap(const wxString
& full_id
, wxBitmap
* bmp
)
68 wxArtProviderBitmapsHash::iterator entry
= m_bitmapsHash
.find(full_id
);
69 if ( entry
== m_bitmapsHash
.end() )
80 void wxArtProviderCache::Clear()
82 m_bitmapsHash
.clear();
85 /*static*/ wxString
wxArtProviderCache::ConstructHashID(
86 const wxArtID
& id
, const wxArtClient
& client
,
90 str
.Printf(wxT("%s-%s-%i-%i"), id
.c_str(), client
.c_str(), size
.x
, size
.y
);
95 // ----------------------------------------------------------------------------
96 // wxArtProvider class
97 // ----------------------------------------------------------------------------
99 IMPLEMENT_ABSTRACT_CLASS(wxArtProvider
, wxObject
)
101 wxArtProvidersList
*wxArtProvider::sm_providers
= NULL
;
102 wxArtProviderCache
*wxArtProvider::sm_cache
= NULL
;
104 /*static*/ void wxArtProvider::PushProvider(wxArtProvider
*provider
)
108 sm_providers
= new wxArtProvidersList
;
109 sm_cache
= new wxArtProviderCache
;
112 sm_providers
->Insert(provider
);
116 /*static*/ bool wxArtProvider::PopProvider()
118 wxCHECK_MSG( sm_providers
, false, _T("no wxArtProvider exists") );
119 wxCHECK_MSG( sm_providers
->GetCount() > 0, false, _T("wxArtProviders stack is empty") );
121 delete sm_providers
->GetFirst()->GetData();
122 sm_providers
->Erase(sm_providers
->GetFirst());
127 /*static*/ bool wxArtProvider::RemoveProvider(wxArtProvider
*provider
)
129 wxCHECK_MSG( sm_providers
, false, _T("no wxArtProvider exists") );
131 if ( sm_providers
->DeleteObject(provider
) )
141 /*static*/ void wxArtProvider::CleanUpProviders()
143 WX_CLEAR_LIST(wxArtProvidersList
, *sm_providers
);
144 wxDELETE(sm_providers
);
148 /*static*/ wxBitmap
wxArtProvider::GetBitmap(const wxArtID
& id
,
149 const wxArtClient
& client
,
152 // safety-check against writing client,id,size instead of id,client,size:
153 wxASSERT_MSG( client
.Last() == _T('C'), _T("invalid 'client' parameter") );
155 wxCHECK_MSG( sm_providers
, wxNullBitmap
, _T("no wxArtProvider exists") );
157 wxString hashId
= wxArtProviderCache::ConstructHashID(id
, client
, size
);
160 if ( !sm_cache
->GetBitmap(hashId
, &bmp
) )
162 for (wxArtProvidersList::compatibility_iterator node
= sm_providers
->GetFirst();
163 node
; node
= node
->GetNext())
165 bmp
= node
->GetData()->CreateBitmap(id
, client
, size
);
169 if ( size
!= wxDefaultSize
&&
170 (bmp
.GetWidth() != size
.x
|| bmp
.GetHeight() != size
.y
) )
172 wxImage img
= bmp
.ConvertToImage();
173 img
.Rescale(size
.x
, size
.y
);
181 sm_cache
->PutBitmap(hashId
, bmp
);
187 /*static*/ wxIcon
wxArtProvider::GetIcon(const wxArtID
& id
,
188 const wxArtClient
& client
,
191 wxCHECK_MSG( sm_providers
, wxNullIcon
, _T("no wxArtProvider exists") );
193 wxBitmap bmp
= GetBitmap(id
, client
, size
);
198 icon
.CopyFromBitmap(bmp
);
202 #if defined(__WXGTK20__) && !defined(__WXUNIVERSAL__)
203 #include "wx/gtk/private.h"
204 extern GtkIconSize
wxArtClientToIconSize(const wxArtClient
& client
);
205 #endif // defined(__WXGTK20__) && !defined(__WXUNIVERSAL__)
207 /*static*/ wxSize
wxArtProvider::GetSizeHint(const wxArtClient
& client
,
208 bool platform_dependent
)
210 if (!platform_dependent
)
212 wxArtProvidersList::compatibility_iterator node
= sm_providers
->GetFirst();
214 return node
->GetData()->DoGetSizeHint(client
);
217 // else return platform dependent size
219 #if defined(__WXGTK20__) && !defined(__WXUNIVERSAL__)
220 // Gtk has specific sizes for each client, see artgtk.cpp
221 GtkIconSize gtk_size
= wxArtClientToIconSize(client
);
222 // no size hints for this client
223 if (gtk_size
== GTK_ICON_SIZE_INVALID
)
224 return wxDefaultSize
;
226 gtk_icon_size_lookup( gtk_size
, &width
, &height
);
227 return wxSize(width
, height
);
229 // NB: These size hints may have to be adjusted per platform
230 if (client
== wxART_TOOLBAR
)
231 return wxSize(16, 15);
232 else if (client
== wxART_MENU
)
233 return wxSize(16, 15);
234 else if (client
== wxART_FRAME_ICON
)
235 return wxSize(16, 15);
236 else if (client
== wxART_CMN_DIALOG
|| client
== wxART_MESSAGE_BOX
)
237 return wxSize(32, 32);
238 else if (client
== wxART_HELP_BROWSER
)
239 return wxSize(16, 15);
240 else if (client
== wxART_BUTTON
)
241 return wxSize(16, 15);
242 else // wxART_OTHER or perhaps a user's client, no specified size
243 return wxDefaultSize
;
244 #endif // GTK+ 2/else
248 class wxArtProviderModule
: public wxModule
253 wxArtProvider::InitStdProvider();
254 wxArtProvider::InitNativeProvider();
259 wxArtProvider::CleanUpProviders();
262 DECLARE_DYNAMIC_CLASS(wxArtProviderModule
)
265 IMPLEMENT_DYNAMIC_CLASS(wxArtProviderModule
, wxModule
)