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 // ===========================================================================
39 // ===========================================================================
41 #include "wx/listimpl.cpp"
42 WX_DECLARE_LIST(wxArtProvider
, wxArtProvidersList
);
43 WX_DEFINE_LIST(wxArtProvidersList
);
45 // ----------------------------------------------------------------------------
46 // Cache class - stores already requested bitmaps
47 // ----------------------------------------------------------------------------
49 WX_DECLARE_STRING_HASH_MAP(wxBitmap
, wxArtProviderBitmapsHash
);
51 class WXDLLEXPORT wxArtProviderCache
54 bool GetBitmap(const wxString
& full_id
, wxBitmap
* bmp
);
55 void PutBitmap(const wxString
& full_id
, const wxBitmap
& bmp
)
56 { m_bitmapsHash
[full_id
] = bmp
; }
60 static wxString
ConstructHashID(const wxArtID
& id
,
61 const wxArtClient
& client
,
65 wxArtProviderBitmapsHash m_bitmapsHash
;
68 bool wxArtProviderCache::GetBitmap(const wxString
& full_id
, wxBitmap
* bmp
)
70 wxArtProviderBitmapsHash::iterator entry
= m_bitmapsHash
.find(full_id
);
71 if ( entry
== m_bitmapsHash
.end() )
82 void wxArtProviderCache::Clear()
84 m_bitmapsHash
.clear();
87 /*static*/ wxString
wxArtProviderCache::ConstructHashID(
88 const wxArtID
& id
, const wxArtClient
& client
,
92 str
.Printf(wxT("%s-%s-%i-%i"), id
.c_str(), client
.c_str(), size
.x
, size
.y
);
97 // ----------------------------------------------------------------------------
98 // wxArtProvider class
99 // ----------------------------------------------------------------------------
101 IMPLEMENT_ABSTRACT_CLASS(wxArtProvider
, wxObject
)
103 wxArtProvidersList
*wxArtProvider::sm_providers
= NULL
;
104 wxArtProviderCache
*wxArtProvider::sm_cache
= NULL
;
106 /*static*/ void wxArtProvider::PushProvider(wxArtProvider
*provider
)
110 sm_providers
= new wxArtProvidersList
;
111 sm_providers
->DeleteContents(TRUE
);
112 sm_cache
= new wxArtProviderCache
;
115 sm_providers
->Insert(provider
);
118 /*static*/ bool wxArtProvider::PopProvider()
120 wxCHECK_MSG( sm_providers
, FALSE
, _T("no wxArtProvider exists") );
121 wxCHECK_MSG( sm_providers
->GetCount() > 0, FALSE
, _T("wxArtProviders stack is empty") );
123 sm_providers
->DeleteNode(sm_providers
->GetFirst());
128 /*static*/ bool wxArtProvider::RemoveProvider(wxArtProvider
*provider
)
130 wxCHECK_MSG( sm_providers
, FALSE
, _T("no wxArtProvider exists") );
132 if ( sm_providers
->DeleteObject(provider
) )
141 /*static*/ void wxArtProvider::CleanUpProviders()
143 wxDELETE(sm_providers
);
147 /*static*/ wxBitmap
wxArtProvider::GetBitmap(const wxArtID
& id
,
148 const wxArtClient
& client
,
151 // safety-check against writing client,id,size instead of id,client,size:
152 wxASSERT_MSG( client
.Last() == _T('C'), _T("invalid 'client' parameter") );
154 wxCHECK_MSG( sm_providers
, wxNullBitmap
, _T("no wxArtProvider exists") );
156 wxString hashId
= wxArtProviderCache::ConstructHashID(id
, client
, size
);
159 if ( !sm_cache
->GetBitmap(hashId
, &bmp
) )
161 for (wxArtProvidersList::Node
*node
= sm_providers
->GetFirst();
162 node
; node
= node
->GetNext())
164 bmp
= node
->GetData()->CreateBitmap(id
, client
, size
);
168 sm_cache
->PutBitmap(hashId
, bmp
);
174 /*static*/ wxIcon
wxArtProvider::GetIcon(const wxArtID
& id
,
175 const wxArtClient
& client
,
178 wxCHECK_MSG( sm_providers
, wxNullIcon
, _T("no wxArtProvider exists") );
180 wxBitmap bmp
= GetBitmap(id
, client
, size
);
184 icon
.CopyFromBitmap(bmp
);
195 class wxArtProviderModule
: public wxModule
198 bool OnInit() { return TRUE
; }
199 void OnExit() { wxArtProvider::CleanUpProviders(); }
201 DECLARE_DYNAMIC_CLASS(wxArtProviderModule
)
204 IMPLEMENT_DYNAMIC_CLASS(wxArtProviderModule
, wxModule
)