]> git.saurik.com Git - wxWidgets.git/blob - src/motif/bitmap.cpp
Some more Motif work; included utils.h in fileconf.cpp (for wxGetHomeDir or something)
[wxWidgets.git] / src / motif / bitmap.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: bitmap.cpp
3 // Purpose: wxBitmap
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 17/09/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "bitmap.h"
14 #endif
15
16 #include "wx/setup.h"
17 #include "wx/utils.h"
18 #include "wx/palette.h"
19 #include "wx/bitmap.h"
20 #include "wx/icon.h"
21 #include "wx/log.h"
22
23 #if !USE_SHARED_LIBRARIES
24 IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject)
25 IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject)
26 #endif
27
28 wxBitmapRefData::wxBitmapRefData()
29 {
30 m_ok = FALSE;
31 m_width = 0;
32 m_height = 0;
33 m_depth = 0;
34 m_quality = 0;
35 m_numColors = 0;
36 m_bitmapMask = NULL;
37
38 m_pixmap = (WXPixmap) 0;
39 m_display = (WXDisplay*) 0;
40 }
41
42 wxBitmapRefData::~wxBitmapRefData()
43 {
44 /*
45 * TODO: delete the bitmap data here.
46 */
47
48 if (m_bitmapMask)
49 delete m_bitmapMask;
50 m_bitmapMask = NULL;
51 }
52
53 wxList wxBitmap::sm_handlers;
54
55 wxBitmap::wxBitmap()
56 {
57 m_refData = NULL;
58
59 if ( wxTheBitmapList )
60 wxTheBitmapList->AddBitmap(this);
61 }
62
63 wxBitmap::~wxBitmap()
64 {
65 if (wxTheBitmapList)
66 wxTheBitmapList->DeleteObject(this);
67 }
68
69 wxBitmap::wxBitmap(const char bits[], int the_width, int the_height, int no_bits)
70 {
71 m_refData = new wxBitmapRefData;
72
73 M_BITMAPDATA->m_width = the_width ;
74 M_BITMAPDATA->m_height = the_height ;
75 M_BITMAPDATA->m_depth = no_bits ;
76 M_BITMAPDATA->m_numColors = 0;
77
78 /* TODO: create the bitmap from data */
79
80 if ( wxTheBitmapList )
81 wxTheBitmapList->AddBitmap(this);
82 }
83
84 wxBitmap::wxBitmap(int w, int h, int d)
85 {
86 (void)Create(w, h, d);
87
88 if ( wxTheBitmapList )
89 wxTheBitmapList->AddBitmap(this);
90 }
91
92 wxBitmap::wxBitmap(void *data, long type, int width, int height, int depth)
93 {
94 (void) Create(data, type, width, height, depth);
95
96 if ( wxTheBitmapList )
97 wxTheBitmapList->AddBitmap(this);
98 }
99
100 wxBitmap::wxBitmap(const wxString& filename, long type)
101 {
102 LoadFile(filename, (int)type);
103
104 if ( wxTheBitmapList )
105 wxTheBitmapList->AddBitmap(this);
106 }
107
108 /* TODO: maybe allow creation from XPM
109 // Create from data
110 wxBitmap::wxBitmap(const char **data)
111 {
112 (void) Create((void *)data, wxBITMAP_TYPE_XPM_DATA, 0, 0, 0);
113 }
114 */
115
116 bool wxBitmap::Create(int w, int h, int d)
117 {
118 UnRef();
119
120 m_refData = new wxBitmapRefData;
121
122 M_BITMAPDATA->m_width = w;
123 M_BITMAPDATA->m_height = h;
124 M_BITMAPDATA->m_depth = d;
125
126 /* TODO: create new bitmap */
127
128 return M_BITMAPDATA->m_ok;
129 }
130
131 bool wxBitmap::LoadFile(const wxString& filename, long type)
132 {
133 UnRef();
134
135 m_refData = new wxBitmapRefData;
136
137 wxBitmapHandler *handler = FindHandler(type);
138
139 if ( handler == NULL ) {
140 wxLogWarning("no bitmap handler for type %d defined.", type);
141
142 return FALSE;
143 }
144
145 return handler->LoadFile(this, filename, type, -1, -1);
146 }
147
148 bool wxBitmap::Create(void *data, long type, int width, int height, int depth)
149 {
150 UnRef();
151
152 m_refData = new wxBitmapRefData;
153
154 wxBitmapHandler *handler = FindHandler(type);
155
156 if ( handler == NULL ) {
157 wxLogWarning("no bitmap handler for type %d defined.", type);
158
159 return FALSE;
160 }
161
162 return handler->Create(this, data, type, width, height, depth);
163 }
164
165 bool wxBitmap::SaveFile(const wxString& filename, int type, const wxPalette *palette)
166 {
167 wxBitmapHandler *handler = FindHandler(type);
168
169 if ( handler == NULL ) {
170 wxLogWarning("no bitmap handler for type %d defined.", type);
171
172 return FALSE;
173 }
174
175 return handler->SaveFile(this, filename, type, palette);
176 }
177
178 void wxBitmap::SetWidth(int w)
179 {
180 if (!M_BITMAPDATA)
181 m_refData = new wxBitmapRefData;
182
183 M_BITMAPDATA->m_width = w;
184 }
185
186 void wxBitmap::SetHeight(int h)
187 {
188 if (!M_BITMAPDATA)
189 m_refData = new wxBitmapRefData;
190
191 M_BITMAPDATA->m_height = h;
192 }
193
194 void wxBitmap::SetDepth(int d)
195 {
196 if (!M_BITMAPDATA)
197 m_refData = new wxBitmapRefData;
198
199 M_BITMAPDATA->m_depth = d;
200 }
201
202 void wxBitmap::SetQuality(int q)
203 {
204 if (!M_BITMAPDATA)
205 m_refData = new wxBitmapRefData;
206
207 M_BITMAPDATA->m_quality = q;
208 }
209
210 void wxBitmap::SetOk(bool isOk)
211 {
212 if (!M_BITMAPDATA)
213 m_refData = new wxBitmapRefData;
214
215 M_BITMAPDATA->m_ok = isOk;
216 }
217
218 void wxBitmap::SetPalette(const wxPalette& palette)
219 {
220 if (!M_BITMAPDATA)
221 m_refData = new wxBitmapRefData;
222
223 M_BITMAPDATA->m_bitmapPalette = palette ;
224 }
225
226 void wxBitmap::SetMask(wxMask *mask)
227 {
228 if (!M_BITMAPDATA)
229 m_refData = new wxBitmapRefData;
230
231 M_BITMAPDATA->m_bitmapMask = mask ;
232 }
233
234 void wxBitmap::AddHandler(wxBitmapHandler *handler)
235 {
236 sm_handlers.Append(handler);
237 }
238
239 void wxBitmap::InsertHandler(wxBitmapHandler *handler)
240 {
241 sm_handlers.Insert(handler);
242 }
243
244 bool wxBitmap::RemoveHandler(const wxString& name)
245 {
246 wxBitmapHandler *handler = FindHandler(name);
247 if ( handler )
248 {
249 sm_handlers.DeleteObject(handler);
250 return TRUE;
251 }
252 else
253 return FALSE;
254 }
255
256 wxBitmapHandler *wxBitmap::FindHandler(const wxString& name)
257 {
258 wxNode *node = sm_handlers.First();
259 while ( node )
260 {
261 wxBitmapHandler *handler = (wxBitmapHandler *)node->Data();
262 if ( handler->GetName() == name )
263 return handler;
264 node = node->Next();
265 }
266 return NULL;
267 }
268
269 wxBitmapHandler *wxBitmap::FindHandler(const wxString& extension, long bitmapType)
270 {
271 wxNode *node = sm_handlers.First();
272 while ( node )
273 {
274 wxBitmapHandler *handler = (wxBitmapHandler *)node->Data();
275 if ( handler->GetExtension() == extension &&
276 (bitmapType == -1 || handler->GetType() == bitmapType) )
277 return handler;
278 node = node->Next();
279 }
280 return NULL;
281 }
282
283 wxBitmapHandler *wxBitmap::FindHandler(long bitmapType)
284 {
285 wxNode *node = sm_handlers.First();
286 while ( node )
287 {
288 wxBitmapHandler *handler = (wxBitmapHandler *)node->Data();
289 if (handler->GetType() == bitmapType)
290 return handler;
291 node = node->Next();
292 }
293 return NULL;
294 }
295
296 /*
297 * wxMask
298 */
299
300 wxMask::wxMask()
301 {
302 m_pixmap = (WXPixmap) 0;
303 }
304
305 // Construct a mask from a bitmap and a colour indicating
306 // the transparent area
307 wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
308 {
309 m_pixmap = (WXPixmap) 0;
310
311 Create(bitmap, colour);
312 }
313
314 // Construct a mask from a bitmap and a palette index indicating
315 // the transparent area
316 wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex)
317 {
318 m_pixmap = (WXPixmap) 0;
319
320 Create(bitmap, paletteIndex);
321 }
322
323 // Construct a mask from a mono bitmap (copies the bitmap).
324 wxMask::wxMask(const wxBitmap& bitmap)
325 {
326 m_pixmap = (WXPixmap) 0;
327
328 Create(bitmap);
329 }
330
331 wxMask::~wxMask()
332 {
333 // TODO: delete mask bitmap
334 }
335
336 // Create a mask from a mono bitmap (copies the bitmap).
337 bool wxMask::Create(const wxBitmap& bitmap)
338 {
339 // TODO
340 return FALSE;
341 }
342
343 // Create a mask from a bitmap and a palette index indicating
344 // the transparent area
345 bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex)
346 {
347 // TODO
348 return FALSE;
349 }
350
351 // Create a mask from a bitmap and a colour indicating
352 // the transparent area
353 bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
354 {
355 // TODO
356 return FALSE;
357 }
358
359 /*
360 * wxBitmapHandler
361 */
362
363 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler, wxObject)
364
365 bool wxBitmapHandler::Create(wxBitmap *bitmap, void *data, long type, int width, int height, int depth)
366 {
367 return FALSE;
368 }
369
370 bool wxBitmapHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long type,
371 int desiredWidth, int desiredHeight)
372 {
373 return FALSE;
374 }
375
376 bool wxBitmapHandler::SaveFile(wxBitmap *bitmap, const wxString& name, int type, const wxPalette *palette)
377 {
378 return FALSE;
379 }
380
381 /*
382 * Standard handlers
383 */
384
385 /* TODO: bitmap handlers, a bit like this:
386 class WXDLLEXPORT wxBMPResourceHandler: public wxBitmapHandler
387 {
388 DECLARE_DYNAMIC_CLASS(wxBMPResourceHandler)
389 public:
390 inline wxBMPResourceHandler()
391 {
392 m_name = "Windows bitmap resource";
393 m_extension = "";
394 m_type = wxBITMAP_TYPE_BMP_RESOURCE;
395 };
396
397 virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
398 int desiredWidth, int desiredHeight);
399 };
400 IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler, wxBitmapHandler)
401 */
402
403 void wxBitmap::CleanUpHandlers()
404 {
405 wxNode *node = sm_handlers.First();
406 while ( node )
407 {
408 wxBitmapHandler *handler = (wxBitmapHandler *)node->Data();
409 wxNode *next = node->Next();
410 delete handler;
411 delete node;
412 node = next;
413 }
414 }
415
416 void wxBitmap::InitStandardHandlers()
417 {
418 /* TODO: initialize all standard bitmap or derive class handlers here.
419 AddHandler(new wxBMPResourceHandler);
420 AddHandler(new wxBMPFileHandler);
421 AddHandler(new wxXPMFileHandler);
422 AddHandler(new wxXPMDataHandler);
423 AddHandler(new wxICOResourceHandler);
424 AddHandler(new wxICOFileHandler);
425 */
426 }