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"
36 // For the purposes of forcing this module to link
37 extern char g_ArtProviderModule
;
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_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 // Hack to make the default provider link
87 // with the application
88 g_ArtProviderModule
= 0;
89 m_bitmapsHash
.clear();
92 /*static*/ wxString wxArtProviderCache
::ConstructHashID(
93 const wxArtID
& id
, const wxArtClient
& client
,
97 str
.Printf(wxT("%s-%s-%i-%i"), id
.c_str(), client
.c_str(), size
.x
, size
.y
);
102 // ----------------------------------------------------------------------------
103 // wxArtProvider class
104 // ----------------------------------------------------------------------------
106 IMPLEMENT_ABSTRACT_CLASS(wxArtProvider
, wxObject
)
108 wxArtProvidersList
*wxArtProvider
::sm_providers
= NULL
;
109 wxArtProviderCache
*wxArtProvider
::sm_cache
= NULL
;
111 /*static*/ void wxArtProvider
::PushProvider(wxArtProvider
*provider
)
115 sm_providers
= new wxArtProvidersList
;
116 sm_providers
->DeleteContents(TRUE
);
117 sm_cache
= new wxArtProviderCache
;
120 sm_providers
->Insert(provider
);
123 /*static*/ bool wxArtProvider
::PopProvider()
125 wxCHECK_MSG( sm_providers
, FALSE
, _T("no wxArtProvider exists") );
126 wxCHECK_MSG( sm_providers
->GetCount() > 0, FALSE
, _T("wxArtProviders stack is empty") );
128 sm_providers
->DeleteNode(sm_providers
->GetFirst());
133 /*static*/ bool wxArtProvider
::RemoveProvider(wxArtProvider
*provider
)
135 wxCHECK_MSG( sm_providers
, FALSE
, _T("no wxArtProvider exists") );
137 if ( sm_providers
->DeleteObject(provider
) )
146 /*static*/ void wxArtProvider
::CleanUpProviders()
148 wxDELETE(sm_providers
);
152 /*static*/ wxBitmap wxArtProvider
::GetBitmap(const wxArtID
& id
,
153 const wxArtClient
& client
,
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 wxString hashId
= wxArtProviderCache
::ConstructHashID(id
, client
, size
);
164 if ( !sm_cache
->GetBitmap(hashId
, &bmp
) )
166 for (wxArtProvidersList
::Node
*node
= sm_providers
->GetFirst();
167 node
; node
= node
->GetNext())
169 bmp
= node
->GetData()->CreateBitmap(id
, client
, size
);
173 sm_cache
->PutBitmap(hashId
, bmp
);
179 /*static*/ wxIcon wxArtProvider
::GetIcon(const wxArtID
& id
,
180 const wxArtClient
& client
,
183 wxCHECK_MSG( sm_providers
, wxNullIcon
, _T("no wxArtProvider exists") );
185 wxBitmap bmp
= GetBitmap(id
, client
, size
);
189 icon
.CopyFromBitmap(bmp
);
200 class wxArtProviderModule
: public wxModule
203 bool OnInit() { return TRUE
; }
204 void OnExit() { wxArtProvider
::CleanUpProviders(); }
206 DECLARE_DYNAMIC_CLASS(wxArtProviderModule
)
209 IMPLEMENT_DYNAMIC_CLASS(wxArtProviderModule
, wxModule
)