doc view code inteprets wxSTREAM_EOF as correct,
[wxWidgets.git] / src / gtk1 / bitmap.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: bitmap.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // RCS-ID: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifdef __GNUG__
11 #pragma implementation "bitmap.h"
12 #endif
13
14 #include "wx/bitmap.h"
15 #include "wx/icon.h"
16 #include "wx/filefn.h"
17 #include "wx/image.h"
18
19 #include <gdk/gdk.h>
20 #include <gdk/gdkprivate.h>
21 #include <gdk/gdkx.h>
22
23 //-----------------------------------------------------------------------------
24 // wxMask
25 //-----------------------------------------------------------------------------
26
27 IMPLEMENT_DYNAMIC_CLASS(wxMask,wxObject)
28
29 wxMask::wxMask()
30 {
31 m_bitmap = (GdkBitmap *) NULL;
32 }
33
34 wxMask::wxMask( const wxBitmap& bitmap, const wxColour& colour )
35 {
36 m_bitmap = (GdkBitmap *) NULL;
37 Create( bitmap, colour );
38 }
39
40 wxMask::wxMask( const wxBitmap& bitmap, int paletteIndex )
41 {
42 m_bitmap = (GdkBitmap *) NULL;
43 Create( bitmap, paletteIndex );
44 }
45
46 wxMask::wxMask( const wxBitmap& bitmap )
47 {
48 m_bitmap = (GdkBitmap *) NULL;
49 Create( bitmap );
50 }
51
52 wxMask::~wxMask()
53 {
54 if (m_bitmap)
55 gdk_bitmap_unref( m_bitmap );
56 }
57
58 bool wxMask::Create( const wxBitmap& bitmap,
59 const wxColour& colour )
60 {
61 if (m_bitmap)
62 {
63 gdk_bitmap_unref( m_bitmap );
64 m_bitmap = (GdkBitmap*) NULL;
65 }
66
67 wxImage image( bitmap );
68 if (!image.Ok()) return FALSE;
69
70 GdkVisual *visual = gdk_visual_get_system();
71
72 GdkImage *mask_image = gdk_image_new( GDK_IMAGE_FASTEST, visual, image.GetWidth(), image.GetHeight() );
73 if (!mask_image) return FALSE;
74
75 GdkWindow *parent = (GdkWindow*) &gdk_root_parent;
76 m_bitmap = gdk_pixmap_new( parent, image.GetWidth(), image.GetHeight(), 1 );
77
78
79 unsigned char *data = image.GetData();
80 int index = 0;
81
82 unsigned char red = colour.Red();
83 unsigned char green = colour.Green();
84 unsigned char blue = colour.Blue();
85
86 int bpp = visual->depth;
87 if ((bpp == 16) && (visual->red_mask != 0xf800)) bpp = 15;
88 if (bpp == 15)
89 {
90 red = red & 0xf8;
91 blue = blue & 0xf8;
92 green = green & 0xf8;
93 }
94 if (bpp == 16)
95 {
96 red = red & 0xf8;
97 blue = blue & 0xfc;
98 green = green & 0xf8;
99 }
100
101 for (int j = 0; j < image.GetHeight(); j++)
102 for (int i = 0; i < image.GetWidth(); i++)
103 {
104 if ((data[index] == red) &&
105 (data[index+1] == green) &&
106 (data[index+2] == blue))
107 {
108 gdk_image_put_pixel( mask_image, i, j, 1 );
109 }
110 else
111 {
112 gdk_image_put_pixel( mask_image, i, j, 1 );
113 }
114 index += 3;
115 }
116
117 GdkGC *mask_gc = gdk_gc_new( m_bitmap );
118
119 gdk_draw_image( m_bitmap, mask_gc, mask_image, 0, 0, 0, 0, image.GetWidth(), image.GetHeight() );
120
121 gdk_gc_unref( mask_gc );
122 gdk_image_destroy( mask_image );
123
124 return FALSE;
125 }
126
127 bool wxMask::Create( const wxBitmap& WXUNUSED(bitmap),
128 int WXUNUSED(paletteIndex) )
129 {
130 if (m_bitmap)
131 {
132 gdk_bitmap_unref( m_bitmap );
133 m_bitmap = (GdkBitmap*) NULL;
134 }
135
136 wxFAIL_MSG( wxT("not implemented") );
137
138 return FALSE;
139 }
140
141 bool wxMask::Create( const wxBitmap& bitmap )
142 {
143 if (m_bitmap)
144 {
145 gdk_bitmap_unref( m_bitmap );
146 m_bitmap = (GdkBitmap*) NULL;
147 }
148
149 if (!bitmap.Ok()) return FALSE;
150
151 wxCHECK_MSG( bitmap.GetBitmap(), FALSE, wxT("Cannot create mask from colour bitmap") );
152
153 m_bitmap = gdk_pixmap_new( (GdkWindow*) &gdk_root_parent, bitmap.GetWidth(), bitmap.GetHeight(), 1 );
154
155 if (!m_bitmap) return FALSE;
156
157 GdkGC *gc = gdk_gc_new( m_bitmap );
158
159 gdk_draw_bitmap( m_bitmap, gc, bitmap.GetBitmap(), 0, 0, 0, 0, bitmap.GetWidth(), bitmap.GetHeight() );
160
161 gdk_gc_unref( gc );
162
163 return TRUE;
164 }
165
166 GdkBitmap *wxMask::GetBitmap() const
167 {
168 return m_bitmap;
169 }
170
171 //-----------------------------------------------------------------------------
172 // wxBitmap
173 //-----------------------------------------------------------------------------
174
175 class wxBitmapRefData: public wxObjectRefData
176 {
177 public:
178 wxBitmapRefData();
179 ~wxBitmapRefData();
180
181 GdkPixmap *m_pixmap;
182 GdkBitmap *m_bitmap;
183 wxMask *m_mask;
184 int m_width;
185 int m_height;
186 int m_bpp;
187 wxPalette *m_palette;
188 };
189
190 wxBitmapRefData::wxBitmapRefData()
191 {
192 m_pixmap = (GdkPixmap *) NULL;
193 m_bitmap = (GdkBitmap *) NULL;
194 m_mask = (wxMask *) NULL;
195 m_width = 0;
196 m_height = 0;
197 m_bpp = 0;
198 m_palette = (wxPalette *) NULL;
199 }
200
201 wxBitmapRefData::~wxBitmapRefData()
202 {
203 if (m_pixmap) gdk_pixmap_unref( m_pixmap );
204 if (m_bitmap) gdk_bitmap_unref( m_bitmap );
205 if (m_mask) delete m_mask;
206 if (m_palette) delete m_palette;
207 }
208
209 //-----------------------------------------------------------------------------
210
211 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
212
213 IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxGDIObject)
214
215 wxBitmap::wxBitmap()
216 {
217 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
218 }
219
220 wxBitmap::wxBitmap( int width, int height, int depth )
221 {
222 wxCHECK_RET( (width > 0) && (height > 0), wxT("invalid bitmap size") )
223
224 GdkWindow *parent = (GdkWindow*) &gdk_root_parent;
225 if (depth == -1) depth = gdk_window_get_visual( parent )->depth;
226
227 wxCHECK_RET( (depth == gdk_window_get_visual( parent )->depth) ||
228 (depth == 1), wxT("invalid bitmap depth") )
229
230 m_refData = new wxBitmapRefData();
231 M_BMPDATA->m_mask = (wxMask *) NULL;
232 M_BMPDATA->m_width = width;
233 M_BMPDATA->m_height = height;
234 if (depth == 1)
235 {
236 M_BMPDATA->m_bitmap = gdk_pixmap_new( parent, width, height, 1 );
237 M_BMPDATA->m_bpp = 1;
238 }
239 else
240 {
241 M_BMPDATA->m_pixmap = gdk_pixmap_new( parent, width, height, depth );
242 M_BMPDATA->m_bpp = gdk_window_get_visual( parent )->depth;
243 }
244
245 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
246 }
247
248 wxBitmap::wxBitmap( const char **bits )
249 {
250 wxCHECK_RET( bits != NULL, wxT("invalid bitmap data") )
251
252 m_refData = new wxBitmapRefData();
253
254 GdkBitmap *mask = (GdkBitmap*) NULL;
255 GdkWindow *parent = (GdkWindow*) &gdk_root_parent;
256
257 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm_d( parent, &mask, NULL, (gchar **) bits );
258
259 if (mask)
260 {
261 M_BMPDATA->m_mask = new wxMask();
262 M_BMPDATA->m_mask->m_bitmap = mask;
263 }
264
265 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
266
267 M_BMPDATA->m_bpp = gdk_window_get_visual( parent )->depth; // ?
268 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
269 }
270
271 wxBitmap::wxBitmap( char **bits )
272 {
273 wxCHECK_RET( bits != NULL, wxT("invalid bitmap data") )
274
275 m_refData = new wxBitmapRefData();
276
277 GdkBitmap *mask = (GdkBitmap*) NULL;
278 GdkWindow *parent = (GdkWindow*) &gdk_root_parent;
279
280 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm_d( parent, &mask, NULL, (gchar **) bits );
281
282 wxCHECK_RET( M_BMPDATA->m_pixmap, wxT("couldn't create pixmap") );
283
284 if (mask)
285 {
286 M_BMPDATA->m_mask = new wxMask();
287 M_BMPDATA->m_mask->m_bitmap = mask;
288 }
289
290 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
291
292 M_BMPDATA->m_bpp = gdk_window_get_visual( parent )->depth; // ?
293 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
294 }
295
296 wxBitmap::wxBitmap( const wxBitmap& bmp )
297 {
298 Ref( bmp );
299
300 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
301 }
302
303 wxBitmap::wxBitmap( const wxString &filename, int type )
304 {
305 LoadFile( filename, type );
306
307 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
308 }
309
310 wxBitmap::wxBitmap( const char bits[], int width, int height, int WXUNUSED(depth))
311 {
312 m_refData = new wxBitmapRefData();
313
314 M_BMPDATA->m_mask = (wxMask *) NULL;
315 M_BMPDATA->m_bitmap =
316 gdk_bitmap_create_from_data( (GdkWindow*) &gdk_root_parent, (gchar *) bits, width, height );
317 M_BMPDATA->m_width = width;
318 M_BMPDATA->m_height = height;
319 M_BMPDATA->m_bpp = 1;
320
321 wxCHECK_RET( M_BMPDATA->m_bitmap, wxT("couldn't create bitmap") );
322
323 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
324 }
325
326 wxBitmap::~wxBitmap()
327 {
328 if (wxTheBitmapList) wxTheBitmapList->DeleteObject(this);
329 }
330
331 wxBitmap& wxBitmap::operator = ( const wxBitmap& bmp )
332 {
333 if (*this == bmp) return (*this);
334 Ref( bmp );
335 return *this;
336 }
337
338 bool wxBitmap::operator == ( const wxBitmap& bmp )
339 {
340 return m_refData == bmp.m_refData;
341 }
342
343 bool wxBitmap::operator != ( const wxBitmap& bmp )
344 {
345 return m_refData != bmp.m_refData;
346 }
347
348 bool wxBitmap::Ok() const
349 {
350 return (m_refData != NULL);
351 }
352
353 int wxBitmap::GetHeight() const
354 {
355 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
356
357 return M_BMPDATA->m_height;
358 }
359
360 int wxBitmap::GetWidth() const
361 {
362 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
363
364 return M_BMPDATA->m_width;
365 }
366
367 int wxBitmap::GetDepth() const
368 {
369 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
370
371 return M_BMPDATA->m_bpp;
372 }
373
374 wxMask *wxBitmap::GetMask() const
375 {
376 wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") );
377
378 return M_BMPDATA->m_mask;
379 }
380
381 void wxBitmap::SetMask( wxMask *mask )
382 {
383 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
384
385 if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask;
386
387 M_BMPDATA->m_mask = mask;
388 }
389
390 bool wxBitmap::SaveFile( const wxString &name, int type, wxPalette *WXUNUSED(palette) )
391 {
392 wxCHECK_MSG( Ok(), FALSE, wxT("invalid bitmap") );
393
394 // Try to save the bitmap via wxImage handlers:
395 {
396 wxImage image( *this );
397 if (image.Ok()) return image.SaveFile( name, type );
398 }
399
400 return FALSE;
401 }
402
403 bool wxBitmap::LoadFile( const wxString &name, int type )
404 {
405 UnRef();
406
407 if (!wxFileExists(name)) return FALSE;
408
409 if (type == wxBITMAP_TYPE_XPM)
410 {
411 m_refData = new wxBitmapRefData();
412
413 GdkBitmap *mask = (GdkBitmap*) NULL;
414 GdkWindow *parent = (GdkWindow*) &gdk_root_parent;
415
416 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm( parent, &mask, NULL, name.fn_str() );
417
418 if (mask)
419 {
420 M_BMPDATA->m_mask = new wxMask();
421 M_BMPDATA->m_mask->m_bitmap = mask;
422 }
423
424 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
425 M_BMPDATA->m_bpp = gdk_window_get_visual( parent )->depth;
426 }
427 else // try if wxImage can load it
428 {
429 wxImage image;
430 if (!image.LoadFile( name, type )) return FALSE;
431 if (image.Ok()) *this = image.ConvertToBitmap();
432 else return FALSE;
433 }
434
435 return TRUE;
436 }
437
438 wxPalette *wxBitmap::GetPalette() const
439 {
440 if (!Ok()) return (wxPalette *) NULL;
441
442 return M_BMPDATA->m_palette;
443 }
444
445 void wxBitmap::SetHeight( int height )
446 {
447 if (!m_refData) m_refData = new wxBitmapRefData();
448
449 M_BMPDATA->m_height = height;
450 }
451
452 void wxBitmap::SetWidth( int width )
453 {
454 if (!m_refData) m_refData = new wxBitmapRefData();
455
456 M_BMPDATA->m_width = width;
457 }
458
459 void wxBitmap::SetDepth( int depth )
460 {
461 if (!m_refData) m_refData = new wxBitmapRefData();
462
463 M_BMPDATA->m_bpp = depth;
464 }
465
466 void wxBitmap::SetPixmap( GdkPixmap *pixmap )
467 {
468 if (!m_refData) m_refData = new wxBitmapRefData();
469
470 M_BMPDATA->m_pixmap = pixmap;
471 }
472
473 GdkPixmap *wxBitmap::GetPixmap() const
474 {
475 wxCHECK_MSG( Ok(), (GdkPixmap *) NULL, wxT("invalid bitmap") );
476
477 return M_BMPDATA->m_pixmap;
478 }
479
480 GdkBitmap *wxBitmap::GetBitmap() const
481 {
482 wxCHECK_MSG( Ok(), (GdkBitmap *) NULL, wxT("invalid bitmap") );
483
484 return M_BMPDATA->m_bitmap;
485 }