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 /*static*/ void wxArtProvider::CommonAddingProvider()
106 sm_providers
= new wxArtProvidersList
;
107 sm_cache
= new wxArtProviderCache
;
113 /*static*/ void wxArtProvider::PushProvider(wxArtProvider
*provider
)
115 CommonAddingProvider();
116 sm_providers
->Insert(provider
);
119 /*static*/ void wxArtProvider::InsertProvider(wxArtProvider
*provider
)
121 CommonAddingProvider();
122 sm_providers
->Append(provider
);
125 /*static*/ bool wxArtProvider::PopProvider()
127 wxCHECK_MSG( sm_providers
, false, _T("no wxArtProvider exists") );
128 wxCHECK_MSG( sm_providers
->GetCount() > 0, false, _T("wxArtProviders stack is empty") );
130 delete sm_providers
->GetFirst()->GetData();
131 sm_providers
->Erase(sm_providers
->GetFirst());
136 /*static*/ bool wxArtProvider::RemoveProvider(wxArtProvider
*provider
)
138 wxCHECK_MSG( sm_providers
, false, _T("no wxArtProvider exists") );
140 if ( sm_providers
->DeleteObject(provider
) )
150 /*static*/ void wxArtProvider::CleanUpProviders()
152 WX_CLEAR_LIST(wxArtProvidersList
, *sm_providers
);
153 wxDELETE(sm_providers
);
157 /*static*/ wxBitmap
wxArtProvider::GetBitmap(const wxArtID
& id
,
158 const wxArtClient
& client
,
161 // safety-check against writing client,id,size instead of id,client,size:
162 wxASSERT_MSG( client
.Last() == _T('C'), _T("invalid 'client' parameter") );
164 wxCHECK_MSG( sm_providers
, wxNullBitmap
, _T("no wxArtProvider exists") );
166 wxString hashId
= wxArtProviderCache::ConstructHashID(id
, client
, size
);
169 if ( !sm_cache
->GetBitmap(hashId
, &bmp
) )
171 for (wxArtProvidersList::compatibility_iterator node
= sm_providers
->GetFirst();
172 node
; node
= node
->GetNext())
174 bmp
= node
->GetData()->CreateBitmap(id
, client
, size
);
177 #if wxUSE_IMAGE && (!defined(__WXMSW__) || wxUSE_WXDIB)
178 if ( size
!= wxDefaultSize
&&
179 (bmp
.GetWidth() != size
.x
|| bmp
.GetHeight() != size
.y
) )
181 wxImage img
= bmp
.ConvertToImage();
182 img
.Rescale(size
.x
, size
.y
);
190 sm_cache
->PutBitmap(hashId
, bmp
);
196 /*static*/ wxIcon
wxArtProvider::GetIcon(const wxArtID
& id
,
197 const wxArtClient
& client
,
200 wxCHECK_MSG( sm_providers
, wxNullIcon
, _T("no wxArtProvider exists") );
202 wxBitmap bmp
= GetBitmap(id
, client
, size
);
207 icon
.CopyFromBitmap(bmp
);
211 #if defined(__WXGTK20__) && !defined(__WXUNIVERSAL__)
212 #include "wx/gtk/private.h"
213 extern GtkIconSize
wxArtClientToIconSize(const wxArtClient
& client
);
214 #endif // defined(__WXGTK20__) && !defined(__WXUNIVERSAL__)
216 /*static*/ wxSize
wxArtProvider::GetSizeHint(const wxArtClient
& client
,
217 bool platform_dependent
)
219 if (!platform_dependent
)
221 wxArtProvidersList::compatibility_iterator node
= sm_providers
->GetFirst();
223 return node
->GetData()->DoGetSizeHint(client
);
226 // else return platform dependent size
228 #if defined(__WXGTK20__) && !defined(__WXUNIVERSAL__)
229 // Gtk has specific sizes for each client, see artgtk.cpp
230 GtkIconSize gtk_size
= wxArtClientToIconSize(client
);
231 // no size hints for this client
232 if (gtk_size
== GTK_ICON_SIZE_INVALID
)
233 return wxDefaultSize
;
235 gtk_icon_size_lookup( gtk_size
, &width
, &height
);
236 return wxSize(width
, height
);
238 // NB: These size hints may have to be adjusted per platform
239 if (client
== wxART_TOOLBAR
)
240 return wxSize(16, 15);
241 else if (client
== wxART_MENU
)
242 return wxSize(16, 15);
243 else if (client
== wxART_FRAME_ICON
)
244 return wxSize(16, 15);
245 else if (client
== wxART_CMN_DIALOG
|| client
== wxART_MESSAGE_BOX
)
246 return wxSize(32, 32);
247 else if (client
== wxART_HELP_BROWSER
)
248 return wxSize(16, 15);
249 else if (client
== wxART_BUTTON
)
250 return wxSize(16, 15);
251 else // wxART_OTHER or perhaps a user's client, no specified size
252 return wxDefaultSize
;
253 #endif // GTK+ 2/else
257 class wxArtProviderModule
: public wxModule
262 wxArtProvider::InitStdProvider();
263 wxArtProvider::InitNativeProvider();
268 wxArtProvider::CleanUpProviders();
271 DECLARE_DYNAMIC_CLASS(wxArtProviderModule
)
274 IMPLEMENT_DYNAMIC_CLASS(wxArtProviderModule
, wxModule
)