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