Added missing image.h header
[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 // ----------------------------------------------------------------------------
27 // wxVariant support
28 // ----------------------------------------------------------------------------
29
30 #if wxUSE_VARIANT
31 IMPLEMENT_VARIANT_OBJECT_EXPORTED(wxBitmap,WXDLLEXPORT)
32 IMPLEMENT_VARIANT_OBJECT_EXPORTED(wxIcon,WXDLLEXPORT)
33 #endif
34
35 // ----------------------------------------------------------------------------
36 // wxBitmapBase
37 // ----------------------------------------------------------------------------
38
39 #if wxUSE_BITMAP_BASE
40
41 #ifndef WX_PRECOMP
42 #include "wx/log.h"
43 #include "wx/utils.h"
44 #include "wx/palette.h"
45 #include "wx/image.h"
46 #include "wx/module.h"
47 #endif // WX_PRECOMP
48
49
50 IMPLEMENT_ABSTRACT_CLASS(wxBitmapBase, wxGDIObject)
51 IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandlerBase,wxObject)
52
53 wxList wxBitmapBase::sm_handlers;
54
55 void wxBitmapBase::AddHandler(wxBitmapHandlerBase *handler)
56 {
57 sm_handlers.Append(handler);
58 }
59
60 void wxBitmapBase::InsertHandler(wxBitmapHandlerBase *handler)
61 {
62 sm_handlers.Insert(handler);
63 }
64
65 bool wxBitmapBase::RemoveHandler(const wxString& name)
66 {
67 wxBitmapHandler *handler = FindHandler(name);
68 if ( handler )
69 {
70 sm_handlers.DeleteObject(handler);
71 return true;
72 }
73 else
74 return false;
75 }
76
77 wxBitmapHandler *wxBitmapBase::FindHandler(const wxString& name)
78 {
79 wxList::compatibility_iterator node = sm_handlers.GetFirst();
80 while ( node )
81 {
82 wxBitmapHandler *handler = (wxBitmapHandler *)node->GetData();
83 if ( handler->GetName() == name )
84 return handler;
85 node = node->GetNext();
86 }
87 return NULL;
88 }
89
90 wxBitmapHandler *wxBitmapBase::FindHandler(const wxString& extension, wxBitmapType bitmapType)
91 {
92 wxList::compatibility_iterator node = sm_handlers.GetFirst();
93 while ( node )
94 {
95 wxBitmapHandler *handler = (wxBitmapHandler *)node->GetData();
96 if ( handler->GetExtension() == extension &&
97 (bitmapType == wxBITMAP_TYPE_ANY || handler->GetType() == bitmapType) )
98 return handler;
99 node = node->GetNext();
100 }
101 return NULL;
102 }
103
104 wxBitmapHandler *wxBitmapBase::FindHandler(wxBitmapType bitmapType)
105 {
106 wxList::compatibility_iterator node = sm_handlers.GetFirst();
107 while ( node )
108 {
109 wxBitmapHandler *handler = (wxBitmapHandler *)node->GetData();
110 if (handler->GetType() == bitmapType)
111 return handler;
112 node = node->GetNext();
113 }
114 return NULL;
115 }
116
117 void wxBitmapBase::CleanUpHandlers()
118 {
119 wxList::compatibility_iterator node = sm_handlers.GetFirst();
120 while ( node )
121 {
122 wxBitmapHandler *handler = (wxBitmapHandler *)node->GetData();
123 wxList::compatibility_iterator next = node->GetNext();
124 delete handler;
125 sm_handlers.Erase(node);
126 node = next;
127 }
128 }
129
130 bool wxBitmapHandlerBase::Create(wxBitmap*, const void*, long, int, int, int)
131 {
132 return false;
133 }
134
135 bool wxBitmapHandlerBase::LoadFile(wxBitmap*, const wxString&, long, int, int)
136 {
137 return false;
138 }
139
140 bool wxBitmapHandlerBase::SaveFile(const wxBitmap*, const wxString&, int, const wxPalette*)
141 {
142 return false;
143 }
144
145 class wxBitmapBaseModule: public wxModule
146 {
147 DECLARE_DYNAMIC_CLASS(wxBitmapBaseModule)
148 public:
149 wxBitmapBaseModule() {}
150 bool OnInit() { wxBitmap::InitStandardHandlers(); return true; };
151 void OnExit() { wxBitmap::CleanUpHandlers(); };
152 };
153
154 IMPLEMENT_DYNAMIC_CLASS(wxBitmapBaseModule, wxModule)
155
156 #endif // wxUSE_BITMAP_BASE
157
158 // ----------------------------------------------------------------------------
159 // wxBitmap common
160 // ----------------------------------------------------------------------------
161
162 #if !(defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXX11__))
163
164 wxBitmap::wxBitmap(const char* const* bits)
165 {
166 wxCHECK2_MSG(bits != NULL, return, wxT("invalid bitmap data"));
167
168 #if wxUSE_IMAGE && wxUSE_XPM
169 wxImage image(bits);
170 wxCHECK2_MSG(image.Ok(), return, wxT("invalid bitmap data"));
171
172 *this = wxBitmap(image);
173 #else
174 wxFAIL_MSG(_T("creating bitmaps from XPMs not supported"));
175 #endif // wxUSE_IMAGE && wxUSE_XPM
176 }
177 #endif // !(defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXX11__))
178
179 // ----------------------------------------------------------------------------
180 // wxMaskBase
181 // ----------------------------------------------------------------------------
182
183 bool wxMaskBase::Create(const wxBitmap& bitmap, const wxColour& colour)
184 {
185 FreeData();
186
187 return InitFromColour(bitmap, colour);
188 }
189
190 #if wxUSE_PALETTE
191
192 bool wxMaskBase::Create(const wxBitmap& bitmap, int paletteIndex)
193 {
194 wxPalette *pal = bitmap.GetPalette();
195
196 wxCHECK_MSG( pal, false,
197 wxT("Cannot create mask from palette index of a bitmap without palette") );
198
199 unsigned char r,g,b;
200 pal->GetRGB(paletteIndex, &r, &g, &b);
201
202 return Create(bitmap, wxColour(r, g, b));
203 }
204
205 #endif // wxUSE_PALETTE
206
207 bool wxMaskBase::Create(const wxBitmap& bitmap)
208 {
209 FreeData();
210
211 return InitFromMonoBitmap(bitmap);
212 }