]> git.saurik.com Git - wxWidgets.git/blob - src/common/bmpbase.cpp
use common bottleneck
[wxWidgets.git] / src / common / bmpbase.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/bmpbase.cpp
3 // Purpose: wxBitmapBase
4 // Author: VaclavSlavik
5 // Created: 2001/04/11
6 // RCS-ID: $Id$
7 // Copyright: (c) 2001, Vaclav Slavik
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
13
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
17
18 #include "wx/bitmap.h"
19
20 #ifndef WX_PRECOMP
21 #include "wx/colour.h"
22 #include "wx/icon.h"
23 #include "wx/image.h"
24 #endif // WX_PRECOMP
25
26 #if wxUSE_IMAGE && wxUSE_LIBPNG && wxUSE_STREAMS
27 #define wxHAS_PNG_LOAD
28
29 #include "wx/mstream.h"
30 #endif
31
32 // ----------------------------------------------------------------------------
33 // wxVariant support
34 // ----------------------------------------------------------------------------
35
36 #if wxUSE_VARIANT
37 IMPLEMENT_VARIANT_OBJECT_EXPORTED_SHALLOWCMP(wxBitmap,WXDLLEXPORT)
38 IMPLEMENT_VARIANT_OBJECT_EXPORTED_SHALLOWCMP(wxIcon,WXDLLEXPORT)
39 #endif
40
41 #if wxUSE_EXTENDED_RTTI
42 //WX_IMPLEMENT_ANY_VALUE_TYPE(wxAnyValueTypeImpl<wxBitmap>)
43 //WX_IMPLEMENT_ANY_VALUE_TYPE(wxAnyValueTypeImpl<wxIcon>)
44 #endif
45
46 // ----------------------------------------------------------------------------
47 // wxBitmapHelpers
48 // ----------------------------------------------------------------------------
49
50 // wxOSX has a native version and doesn't use this one.
51
52 #ifndef __WXOSX__
53
54 /* static */
55 wxBitmap wxBitmapHelpers::NewFromPNGData(const void* data, size_t size)
56 {
57 wxBitmap bitmap;
58
59 #ifdef wxHAS_PNG_LOAD
60 wxMemoryInputStream is(data, size);
61 wxImage image(is, wxBITMAP_TYPE_PNG);
62 if ( image.IsOk() )
63 bitmap = wxBitmap(image);
64 #endif // wxHAS_PNG_LOAD
65
66 return bitmap;
67 }
68
69 #endif // !__WXOSX__
70
71 // ----------------------------------------------------------------------------
72 // wxBitmapBase
73 // ----------------------------------------------------------------------------
74
75 #if wxUSE_BITMAP_BASE
76
77 #ifndef WX_PRECOMP
78 #include "wx/log.h"
79 #include "wx/utils.h"
80 #include "wx/palette.h"
81 #include "wx/module.h"
82 #endif // WX_PRECOMP
83
84
85 IMPLEMENT_ABSTRACT_CLASS(wxBitmapBase, wxGDIObject)
86 IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler, wxObject)
87
88 wxList wxBitmapBase::sm_handlers;
89
90 void wxBitmapBase::AddHandler(wxBitmapHandler *handler)
91 {
92 sm_handlers.Append(handler);
93 }
94
95 void wxBitmapBase::InsertHandler(wxBitmapHandler *handler)
96 {
97 sm_handlers.Insert(handler);
98 }
99
100 bool wxBitmapBase::RemoveHandler(const wxString& name)
101 {
102 wxBitmapHandler *handler = FindHandler(name);
103 if ( handler )
104 {
105 sm_handlers.DeleteObject(handler);
106 return true;
107 }
108 else
109 return false;
110 }
111
112 wxBitmapHandler *wxBitmapBase::FindHandler(const wxString& name)
113 {
114 wxList::compatibility_iterator node = sm_handlers.GetFirst();
115 while ( node )
116 {
117 wxBitmapHandler *handler = (wxBitmapHandler *)node->GetData();
118 if ( handler->GetName() == name )
119 return handler;
120 node = node->GetNext();
121 }
122 return NULL;
123 }
124
125 wxBitmapHandler *wxBitmapBase::FindHandler(const wxString& extension, wxBitmapType bitmapType)
126 {
127 wxList::compatibility_iterator node = sm_handlers.GetFirst();
128 while ( node )
129 {
130 wxBitmapHandler *handler = (wxBitmapHandler *)node->GetData();
131 if ( handler->GetExtension() == extension &&
132 (bitmapType == wxBITMAP_TYPE_ANY || handler->GetType() == bitmapType) )
133 return handler;
134 node = node->GetNext();
135 }
136 return NULL;
137 }
138
139 wxBitmapHandler *wxBitmapBase::FindHandler(wxBitmapType bitmapType)
140 {
141 wxList::compatibility_iterator node = sm_handlers.GetFirst();
142 while ( node )
143 {
144 wxBitmapHandler *handler = (wxBitmapHandler *)node->GetData();
145 if (handler->GetType() == bitmapType)
146 return handler;
147 node = node->GetNext();
148 }
149 return NULL;
150 }
151
152 void wxBitmapBase::CleanUpHandlers()
153 {
154 wxList::compatibility_iterator node = sm_handlers.GetFirst();
155 while ( node )
156 {
157 wxBitmapHandler *handler = (wxBitmapHandler *)node->GetData();
158 wxList::compatibility_iterator next = node->GetNext();
159 delete handler;
160 sm_handlers.Erase(node);
161 node = next;
162 }
163 }
164
165 class wxBitmapBaseModule: public wxModule
166 {
167 DECLARE_DYNAMIC_CLASS(wxBitmapBaseModule)
168 public:
169 wxBitmapBaseModule() {}
170 bool OnInit() { wxBitmap::InitStandardHandlers(); return true; }
171 void OnExit() { wxBitmap::CleanUpHandlers(); }
172 };
173
174 IMPLEMENT_DYNAMIC_CLASS(wxBitmapBaseModule, wxModule)
175
176 #endif // wxUSE_BITMAP_BASE
177
178 // ----------------------------------------------------------------------------
179 // wxBitmap common
180 // ----------------------------------------------------------------------------
181
182 #if !(defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXX11__))
183
184 wxBitmap::wxBitmap(const char* const* bits)
185 {
186 wxCHECK2_MSG(bits != NULL, return, wxT("invalid bitmap data"));
187
188 #if wxUSE_IMAGE && wxUSE_XPM
189 wxImage image(bits);
190 wxCHECK2_MSG(image.IsOk(), return, wxT("invalid bitmap data"));
191
192 *this = wxBitmap(image);
193 #else
194 wxFAIL_MSG(wxT("creating bitmaps from XPMs not supported"));
195 #endif // wxUSE_IMAGE && wxUSE_XPM
196 }
197 #endif // !(defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXX11__))
198
199 // ----------------------------------------------------------------------------
200 // wxMaskBase
201 // ----------------------------------------------------------------------------
202
203 bool wxMaskBase::Create(const wxBitmap& bitmap, const wxColour& colour)
204 {
205 FreeData();
206
207 return InitFromColour(bitmap, colour);
208 }
209
210 #if wxUSE_PALETTE
211
212 bool wxMaskBase::Create(const wxBitmap& bitmap, int paletteIndex)
213 {
214 wxPalette *pal = bitmap.GetPalette();
215
216 wxCHECK_MSG( pal, false,
217 wxT("Cannot create mask from palette index of a bitmap without palette") );
218
219 unsigned char r,g,b;
220 pal->GetRGB(paletteIndex, &r, &g, &b);
221
222 return Create(bitmap, wxColour(r, g, b));
223 }
224
225 #endif // wxUSE_PALETTE
226
227 bool wxMaskBase::Create(const wxBitmap& bitmap)
228 {
229 FreeData();
230
231 return InitFromMonoBitmap(bitmap);
232 }