1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxArtProvider class
4 // Author: Vaclav Slavik
8 // Copyright: (c) Vaclav Slavik
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ---------------------------------------------------------------------------
14 // ---------------------------------------------------------------------------
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"
37 // For the purposes of forcing this module to link
38 extern char g_ArtProviderModule
;
40 // ===========================================================================
42 // ===========================================================================
44 #include "wx/listimpl.cpp"
45 WX_DECLARE_LIST(wxArtProvider
, wxArtProvidersList
);
46 WX_DEFINE_LIST(wxArtProvidersList
);
48 // ----------------------------------------------------------------------------
49 // Cache class - stores already requested bitmaps
50 // ----------------------------------------------------------------------------
52 WX_DECLARE_EXPORTED_STRING_HASH_MAP(wxBitmap
, wxArtProviderBitmapsHash
);
54 class WXDLLEXPORT wxArtProviderCache
57 bool GetBitmap(const wxString
& full_id
, wxBitmap
* bmp
);
58 void PutBitmap(const wxString
& full_id
, const wxBitmap
& bmp
)
59 { m_bitmapsHash
[full_id
] = bmp
; }
63 static wxString
ConstructHashID(const wxArtID
& id
,
64 const wxArtClient
& client
,
68 wxArtProviderBitmapsHash m_bitmapsHash
;
71 bool wxArtProviderCache::GetBitmap(const wxString
& full_id
, wxBitmap
* bmp
)
73 wxArtProviderBitmapsHash::iterator entry
= m_bitmapsHash
.find(full_id
);
74 if ( entry
== m_bitmapsHash
.end() )
85 void wxArtProviderCache::Clear()
87 // Hack to make the default provider link
88 // with the application
89 g_ArtProviderModule
= 0;
90 m_bitmapsHash
.clear();
93 /*static*/ wxString
wxArtProviderCache::ConstructHashID(
94 const wxArtID
& id
, const wxArtClient
& client
,
98 str
.Printf(wxT("%s-%s-%i-%i"), id
.c_str(), client
.c_str(), size
.x
, size
.y
);
103 // ----------------------------------------------------------------------------
104 // wxArtProvider class
105 // ----------------------------------------------------------------------------
107 IMPLEMENT_ABSTRACT_CLASS(wxArtProvider
, wxObject
)
109 wxArtProvidersList
*wxArtProvider::sm_providers
= NULL
;
110 wxArtProviderCache
*wxArtProvider::sm_cache
= NULL
;
112 /*static*/ void wxArtProvider::PushProvider(wxArtProvider
*provider
)
116 sm_providers
= new wxArtProvidersList
;
117 sm_cache
= new wxArtProviderCache
;
120 sm_providers
->Insert(provider
);
124 /*static*/ bool wxArtProvider::PopProvider()
126 wxCHECK_MSG( sm_providers
, FALSE
, _T("no wxArtProvider exists") );
127 wxCHECK_MSG( sm_providers
->GetCount() > 0, FALSE
, _T("wxArtProviders stack is empty") );
129 delete sm_providers
->GetFirst()->GetData();
130 sm_providers
->Erase(sm_providers
->GetFirst());
135 /*static*/ bool wxArtProvider::RemoveProvider(wxArtProvider
*provider
)
137 wxCHECK_MSG( sm_providers
, FALSE
, _T("no wxArtProvider exists") );
139 if ( sm_providers
->DeleteObject(provider
) )
149 /*static*/ void wxArtProvider::CleanUpProviders()
151 WX_CLEAR_LIST(wxArtProvidersList
, *sm_providers
);
152 wxDELETE(sm_providers
);
156 /*static*/ wxBitmap
wxArtProvider::GetBitmap(const wxArtID
& id
,
157 const wxArtClient
& client
,
160 // safety-check against writing client,id,size instead of id,client,size:
161 wxASSERT_MSG( client
.Last() == _T('C'), _T("invalid 'client' parameter") );
163 wxCHECK_MSG( sm_providers
, wxNullBitmap
, _T("no wxArtProvider exists") );
165 wxString hashId
= wxArtProviderCache::ConstructHashID(id
, client
, size
);
168 if ( !sm_cache
->GetBitmap(hashId
, &bmp
) )
170 for (wxArtProvidersList::compatibility_iterator node
= sm_providers
->GetFirst();
171 node
; node
= node
->GetNext())
173 bmp
= node
->GetData()->CreateBitmap(id
, client
, size
);
176 if ( size
!= wxDefaultSize
&&
177 (bmp
.GetWidth() != size
.x
|| bmp
.GetHeight() != size
.y
) )
179 wxImage img
= bmp
.ConvertToImage();
180 img
.Rescale(size
.x
, size
.y
);
187 sm_cache
->PutBitmap(hashId
, bmp
);
193 /*static*/ wxIcon
wxArtProvider::GetIcon(const wxArtID
& id
,
194 const wxArtClient
& client
,
197 wxCHECK_MSG( sm_providers
, wxNullIcon
, _T("no wxArtProvider exists") );
199 wxBitmap bmp
= GetBitmap(id
, client
, size
);
204 icon
.CopyFromBitmap(bmp
);
209 class wxArtProviderModule
: public wxModule
214 wxArtProvider::InitStdProvider();
219 wxArtProvider::CleanUpProviders();
222 DECLARE_DYNAMIC_CLASS(wxArtProviderModule
)
225 IMPLEMENT_DYNAMIC_CLASS(wxArtProviderModule
, wxModule
)