1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxArtProvider class
4 // Author: Vaclav Slavik
8 // Copyright: (c) Vaclav Slavik
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ---------------------------------------------------------------------------
14 // ---------------------------------------------------------------------------
16 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
17 #pragma implementation "artprov.h"
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
23 #if defined(__BORLANDC__)
32 #include "wx/artprov.h"
33 #include "wx/hashmap.h"
34 #include "wx/module.h"
39 // ===========================================================================
41 // ===========================================================================
43 #include "wx/listimpl.cpp"
44 WX_DECLARE_LIST(wxArtProvider
, wxArtProvidersList
);
45 WX_DEFINE_LIST(wxArtProvidersList
);
47 // ----------------------------------------------------------------------------
48 // Cache class - stores already requested bitmaps
49 // ----------------------------------------------------------------------------
51 WX_DECLARE_EXPORTED_STRING_HASH_MAP(wxBitmap
, wxArtProviderBitmapsHash
);
53 class WXDLLEXPORT wxArtProviderCache
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
; }
62 static wxString
ConstructHashID(const wxArtID
& id
,
63 const wxArtClient
& client
,
67 wxArtProviderBitmapsHash m_bitmapsHash
;
70 bool wxArtProviderCache::GetBitmap(const wxString
& full_id
, wxBitmap
* bmp
)
72 wxArtProviderBitmapsHash::iterator entry
= m_bitmapsHash
.find(full_id
);
73 if ( entry
== m_bitmapsHash
.end() )
84 void wxArtProviderCache::Clear()
86 m_bitmapsHash
.clear();
89 /*static*/ wxString
wxArtProviderCache::ConstructHashID(
90 const wxArtID
& id
, const wxArtClient
& client
,
94 str
.Printf(wxT("%s-%s-%i-%i"), id
.c_str(), client
.c_str(), size
.x
, size
.y
);
99 // ----------------------------------------------------------------------------
100 // wxArtProvider class
101 // ----------------------------------------------------------------------------
103 IMPLEMENT_ABSTRACT_CLASS(wxArtProvider
, wxObject
)
105 wxArtProvidersList
*wxArtProvider::sm_providers
= NULL
;
106 wxArtProviderCache
*wxArtProvider::sm_cache
= NULL
;
108 /*static*/ void wxArtProvider::PushProvider(wxArtProvider
*provider
)
112 sm_providers
= new wxArtProvidersList
;
113 sm_cache
= new wxArtProviderCache
;
116 sm_providers
->Insert(provider
);
120 /*static*/ bool wxArtProvider::PopProvider()
122 wxCHECK_MSG( sm_providers
, false, _T("no wxArtProvider exists") );
123 wxCHECK_MSG( sm_providers
->GetCount() > 0, false, _T("wxArtProviders stack is empty") );
125 delete sm_providers
->GetFirst()->GetData();
126 sm_providers
->Erase(sm_providers
->GetFirst());
131 /*static*/ bool wxArtProvider::RemoveProvider(wxArtProvider
*provider
)
133 wxCHECK_MSG( sm_providers
, false, _T("no wxArtProvider exists") );
135 if ( sm_providers
->DeleteObject(provider
) )
145 /*static*/ void wxArtProvider::CleanUpProviders()
147 WX_CLEAR_LIST(wxArtProvidersList
, *sm_providers
);
148 wxDELETE(sm_providers
);
152 /*static*/ wxBitmap
wxArtProvider::GetBitmap(const wxArtID
& id
,
153 const wxArtClient
& client
,
154 const wxSize
& reqSize
)
156 // safety-check against writing client,id,size instead of id,client,size:
157 wxASSERT_MSG( client
.Last() == _T('C'), _T("invalid 'client' parameter") );
159 wxCHECK_MSG( sm_providers
, wxNullBitmap
, _T("no wxArtProvider exists") );
161 wxSize bestSize
= (reqSize
!= wxDefaultSize
) ? reqSize
: GetSize(client
);
163 wxString hashId
= wxArtProviderCache::ConstructHashID(id
, client
, bestSize
);
166 if ( !sm_cache
->GetBitmap(hashId
, &bmp
) )
168 for (wxArtProvidersList::compatibility_iterator node
= sm_providers
->GetFirst();
169 node
; node
= node
->GetNext())
171 bmp
= node
->GetData()->CreateBitmap(id
, client
, bestSize
);
175 int bmp_w
= bmp
.GetWidth();
176 int bmp_h
= bmp
.GetHeight();
177 // want default size but it's smaller, paste into transparent image
178 if ((reqSize
== wxDefaultSize
) &&
179 (bmp_h
< bestSize
.x
) && (bmp_w
< bestSize
.y
))
181 wxPoint
offset((bestSize
.x
- bmp_w
)/2, (bestSize
.y
- bmp_h
)/2);
182 wxImage img
= bmp
.ConvertToImage();
183 img
.Resize(bestSize
, offset
);
186 else if ( (bmp_w
!= bestSize
.x
) || (bmp_h
!= bestSize
.y
) )
188 wxImage img
= bmp
.ConvertToImage();
189 img
.Rescale(bestSize
.x
, bestSize
.y
);
197 sm_cache
->PutBitmap(hashId
, bmp
);
203 /*static*/ wxIcon
wxArtProvider::GetIcon(const wxArtID
& id
,
204 const wxArtClient
& client
,
207 wxCHECK_MSG( sm_providers
, wxNullIcon
, _T("no wxArtProvider exists") );
209 wxBitmap bmp
= GetBitmap(id
, client
, size
);
214 icon
.CopyFromBitmap(bmp
);
218 #if defined(__WXGTK20__) && !defined(__WXUNIVERSAL__)
220 extern GtkIconSize
wxArtClientToIconSize(const wxArtClient
& client
);
223 /*static*/ wxSize
wxArtProvider::GetSize(const wxArtClient
& client
,
224 bool platform_dependent
)
226 if (!platform_dependent
)
228 wxArtProvidersList::compatibility_iterator node
= sm_providers
->GetFirst();
230 return node
->GetData()->DoGetSize(client
);
232 // else return platform dependent size
235 #if defined(__WXGTK20__) && !defined(__WXUNIVERSAL__)
236 GtkIconSize gtk_size
= wxArtClientToIconSize(client
);
238 gtk_icon_size_lookup( gtk_size
, &width
, &height
);
239 return wxSize(width
, height
);
241 if (client
== wxART_TOOLBAR
)
242 return wxSize(32, 32);
243 else if (client
== wxART_MENU
)
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_BUTTON
)
248 return wxSize(16, 15);
250 return wxSize(16, 15); // this is arbitrary
251 #endif // GTK+ 2/else
255 class wxArtProviderModule
: public wxModule
260 wxArtProvider::InitStdProvider();
261 wxArtProvider::InitNativeProvider();
266 wxArtProvider::CleanUpProviders();
269 DECLARE_DYNAMIC_CLASS(wxArtProviderModule
)
272 IMPLEMENT_DYNAMIC_CLASS(wxArtProviderModule
, wxModule
)