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 // For the purposes of forcing this module to link
40 extern char g_ArtProviderModule
;
42 // ===========================================================================
44 // ===========================================================================
46 #include "wx/listimpl.cpp"
47 WX_DECLARE_LIST(wxArtProvider
, wxArtProvidersList
);
48 WX_DEFINE_LIST(wxArtProvidersList
);
50 // ----------------------------------------------------------------------------
51 // Cache class - stores already requested bitmaps
52 // ----------------------------------------------------------------------------
54 WX_DECLARE_EXPORTED_STRING_HASH_MAP(wxBitmap
, wxArtProviderBitmapsHash
);
56 class WXDLLEXPORT wxArtProviderCache
59 bool GetBitmap(const wxString
& full_id
, wxBitmap
* bmp
);
60 void PutBitmap(const wxString
& full_id
, const wxBitmap
& bmp
)
61 { m_bitmapsHash
[full_id
] = bmp
; }
65 static wxString
ConstructHashID(const wxArtID
& id
,
66 const wxArtClient
& client
,
70 wxArtProviderBitmapsHash m_bitmapsHash
;
73 bool wxArtProviderCache::GetBitmap(const wxString
& full_id
, wxBitmap
* bmp
)
75 wxArtProviderBitmapsHash::iterator entry
= m_bitmapsHash
.find(full_id
);
76 if ( entry
== m_bitmapsHash
.end() )
87 void wxArtProviderCache::Clear()
89 // Hack to make the default provider link
90 // with the application
91 g_ArtProviderModule
= 0;
92 m_bitmapsHash
.clear();
95 /*static*/ wxString
wxArtProviderCache::ConstructHashID(
96 const wxArtID
& id
, const wxArtClient
& client
,
100 str
.Printf(wxT("%s-%s-%i-%i"), id
.c_str(), client
.c_str(), size
.x
, size
.y
);
105 // ----------------------------------------------------------------------------
106 // wxArtProvider class
107 // ----------------------------------------------------------------------------
109 IMPLEMENT_ABSTRACT_CLASS(wxArtProvider
, wxObject
)
111 wxArtProvidersList
*wxArtProvider::sm_providers
= NULL
;
112 wxArtProviderCache
*wxArtProvider::sm_cache
= NULL
;
114 /*static*/ void wxArtProvider::PushProvider(wxArtProvider
*provider
)
118 sm_providers
= new wxArtProvidersList
;
119 sm_cache
= new wxArtProviderCache
;
122 sm_providers
->Insert(provider
);
126 /*static*/ bool wxArtProvider::PopProvider()
128 wxCHECK_MSG( sm_providers
, FALSE
, _T("no wxArtProvider exists") );
129 wxCHECK_MSG( sm_providers
->GetCount() > 0, FALSE
, _T("wxArtProviders stack is empty") );
131 delete sm_providers
->GetFirst()->GetData();
132 sm_providers
->Erase(sm_providers
->GetFirst());
137 /*static*/ bool wxArtProvider::RemoveProvider(wxArtProvider
*provider
)
139 wxCHECK_MSG( sm_providers
, FALSE
, _T("no wxArtProvider exists") );
141 if ( sm_providers
->DeleteObject(provider
) )
151 /*static*/ void wxArtProvider::CleanUpProviders()
153 WX_CLEAR_LIST(wxArtProvidersList
, *sm_providers
);
154 wxDELETE(sm_providers
);
158 /*static*/ wxBitmap
wxArtProvider::GetBitmap(const wxArtID
& id
,
159 const wxArtClient
& client
,
162 // safety-check against writing client,id,size instead of id,client,size:
163 wxASSERT_MSG( client
.Last() == _T('C'), _T("invalid 'client' parameter") );
165 wxCHECK_MSG( sm_providers
, wxNullBitmap
, _T("no wxArtProvider exists") );
167 wxString hashId
= wxArtProviderCache::ConstructHashID(id
, client
, size
);
170 if ( !sm_cache
->GetBitmap(hashId
, &bmp
) )
172 for (wxArtProvidersList::compatibility_iterator node
= sm_providers
->GetFirst();
173 node
; node
= node
->GetNext())
175 bmp
= node
->GetData()->CreateBitmap(id
, client
, size
);
179 if ( size
!= wxDefaultSize
&&
180 (bmp
.GetWidth() != size
.x
|| bmp
.GetHeight() != size
.y
) )
182 wxImage img
= bmp
.ConvertToImage();
183 img
.Rescale(size
.x
, size
.y
);
191 sm_cache
->PutBitmap(hashId
, bmp
);
197 /*static*/ wxIcon
wxArtProvider::GetIcon(const wxArtID
& id
,
198 const wxArtClient
& client
,
201 wxCHECK_MSG( sm_providers
, wxNullIcon
, _T("no wxArtProvider exists") );
203 wxBitmap bmp
= GetBitmap(id
, client
, size
);
208 icon
.CopyFromBitmap(bmp
);
213 class wxArtProviderModule
: public wxModule
218 wxArtProvider::InitStdProvider();
223 wxArtProvider::CleanUpProviders();
226 DECLARE_DYNAMIC_CLASS(wxArtProviderModule
)
229 IMPLEMENT_DYNAMIC_CLASS(wxArtProviderModule
, wxModule
)