make wxMask pixmap member private
[wxWidgets.git] / src / gtk / bitmap.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/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 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #include "wx/bitmap.h"
14
15 #ifndef WX_PRECOMP
16 #include "wx/icon.h"
17 #include "wx/image.h"
18 #include "wx/colour.h"
19 #endif
20
21 #include "wx/rawbmp.h"
22
23 #include "wx/gtk/private/object.h"
24
25 #include <gtk/gtk.h>
26
27 extern GtkWidget *wxGetRootWindow();
28
29 static void PixmapToPixbuf(GdkPixmap* pixmap, GdkPixbuf* pixbuf, int w, int h)
30 {
31 gdk_pixbuf_get_from_drawable(pixbuf, pixmap, NULL, 0, 0, 0, 0, w, h);
32 if (gdk_drawable_get_depth(pixmap) == 1)
33 {
34 // invert to match XBM convention
35 guchar* p = gdk_pixbuf_get_pixels(pixbuf);
36 const int inc = 3 + int(gdk_pixbuf_get_has_alpha(pixbuf) != 0);
37 const int rowpad = gdk_pixbuf_get_rowstride(pixbuf) - w * inc;
38 for (int y = h; y; y--, p += rowpad)
39 for (int x = w; x; x--, p += inc)
40 {
41 // pixels are either (0,0,0) or (0xff,0xff,0xff)
42 p[0] = ~p[0];
43 p[1] = ~p[1];
44 p[2] = ~p[2];
45 }
46 }
47 }
48
49 static void MaskToAlpha(GdkPixmap* mask, GdkPixbuf* pixbuf, int w, int h)
50 {
51 GdkPixbuf* mask_pixbuf = gdk_pixbuf_get_from_drawable(
52 NULL, mask, NULL, 0, 0, 0, 0, w, h);
53 guchar* p = gdk_pixbuf_get_pixels(pixbuf) + 3;
54 const guchar* mask_data = gdk_pixbuf_get_pixels(mask_pixbuf);
55 const int rowpad = gdk_pixbuf_get_rowstride(pixbuf) - w * 4;
56 const int mask_rowpad = gdk_pixbuf_get_rowstride(mask_pixbuf) - w * 3;
57 for (int y = h; y; y--, p += rowpad, mask_data += mask_rowpad)
58 {
59 for (int x = w; x; x--, p += 4, mask_data += 3)
60 {
61 *p = 255;
62 // no need to test all 3 components,
63 // pixels are either (0,0,0) or (0xff,0xff,0xff)
64 if (mask_data[0] == 0)
65 *p = 0;
66 }
67 }
68 g_object_unref(mask_pixbuf);
69 }
70
71 //-----------------------------------------------------------------------------
72 // wxMask
73 //-----------------------------------------------------------------------------
74
75 IMPLEMENT_DYNAMIC_CLASS(wxMask, wxMaskBase)
76
77 wxMask::wxMask()
78 {
79 m_bitmap = NULL;
80 }
81
82 wxMask::wxMask(const wxMask& mask)
83 {
84 if ( !mask.m_bitmap )
85 {
86 m_bitmap = NULL;
87 return;
88 }
89
90 // create a copy of an existing mask
91 gint w, h;
92 gdk_drawable_get_size(mask.m_bitmap, &w, &h);
93 m_bitmap = gdk_pixmap_new(mask.m_bitmap, w, h, 1);
94
95 wxGtkObject<GdkGC> gc(gdk_gc_new(m_bitmap));
96 gdk_draw_drawable(m_bitmap, gc, mask.m_bitmap, 0, 0, 0, 0, -1, -1);
97 }
98
99 wxMask::wxMask( const wxBitmap& bitmap, const wxColour& colour )
100 {
101 m_bitmap = NULL;
102 InitFromColour(bitmap, colour);
103 }
104
105 #if wxUSE_PALETTE
106 wxMask::wxMask( const wxBitmap& bitmap, int paletteIndex )
107 {
108 m_bitmap = NULL;
109 Create( bitmap, paletteIndex );
110 }
111 #endif // wxUSE_PALETTE
112
113 wxMask::wxMask( const wxBitmap& bitmap )
114 {
115 m_bitmap = NULL;
116 InitFromMonoBitmap(bitmap);
117 }
118
119 wxMask::wxMask(GdkPixmap* bitmap)
120 {
121 m_bitmap = bitmap;
122 }
123
124 wxMask::~wxMask()
125 {
126 if (m_bitmap)
127 g_object_unref (m_bitmap);
128 }
129
130 void wxMask::FreeData()
131 {
132 if (m_bitmap)
133 {
134 g_object_unref (m_bitmap);
135 m_bitmap = NULL;
136 }
137 }
138
139 bool wxMask::InitFromColour(const wxBitmap& bitmap, const wxColour& colour)
140 {
141 const int w = bitmap.GetWidth();
142 const int h = bitmap.GetHeight();
143
144 // create mask as XBM format bitmap
145
146 // one bit per pixel, each row starts on a byte boundary
147 const size_t out_size = size_t((w + 7) / 8) * unsigned(h);
148 wxByte* out = new wxByte[out_size];
149 // set bits are unmasked
150 memset(out, 0xff, out_size);
151 unsigned bit_index = 0;
152 if (bitmap.HasPixbuf())
153 {
154 const wxByte r_mask = colour.Red();
155 const wxByte g_mask = colour.Green();
156 const wxByte b_mask = colour.Blue();
157 GdkPixbuf* pixbuf = bitmap.GetPixbuf();
158 const wxByte* in = gdk_pixbuf_get_pixels(pixbuf);
159 const int inc = 3 + int(gdk_pixbuf_get_has_alpha(pixbuf) != 0);
160 const int rowpadding = gdk_pixbuf_get_rowstride(pixbuf) - inc * w;
161 for (int y = 0; y < h; y++, in += rowpadding)
162 {
163 for (int x = 0; x < w; x++, in += inc, bit_index++)
164 if (in[0] == r_mask && in[1] == g_mask && in[2] == b_mask)
165 out[bit_index >> 3] ^= 1 << (bit_index & 7);
166 // move index to next byte boundary
167 bit_index = (bit_index + 7) & ~7u;
168 }
169 }
170 else
171 {
172 GdkImage* image = gdk_drawable_get_image(bitmap.GetPixmap(), 0, 0, w, h);
173 GdkColormap* colormap = gdk_image_get_colormap(image);
174 guint32 mask_pixel;
175 if (colormap == NULL)
176 // mono bitmap, white is pixel value 0
177 mask_pixel = guint32(colour.Red() != 255 || colour.Green() != 255 || colour.Blue() != 255);
178 else
179 {
180 wxColor c(colour);
181 c.CalcPixel(colormap);
182 mask_pixel = c.GetPixel();
183 }
184 for (int y = 0; y < h; y++)
185 {
186 for (int x = 0; x < w; x++, bit_index++)
187 if (gdk_image_get_pixel(image, x, y) == mask_pixel)
188 out[bit_index >> 3] ^= 1 << (bit_index & 7);
189 bit_index = (bit_index + 7) & ~7u;
190 }
191 g_object_unref(image);
192 }
193 m_bitmap = gdk_bitmap_create_from_data(wxGetRootWindow()->window, (char*)out, w, h);
194 delete[] out;
195 return true;
196 }
197
198 bool wxMask::InitFromMonoBitmap(const wxBitmap& bitmap)
199 {
200 if (!bitmap.IsOk()) return false;
201
202 wxCHECK_MSG( bitmap.GetDepth() == 1, false, wxT("Cannot create mask from colour bitmap") );
203
204 m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, bitmap.GetWidth(), bitmap.GetHeight(), 1 );
205
206 if (!m_bitmap) return false;
207
208 wxGtkObject<GdkGC> gc(gdk_gc_new( m_bitmap ));
209 gdk_gc_set_function(gc, GDK_COPY_INVERT);
210 gdk_draw_drawable(m_bitmap, gc, bitmap.GetPixmap(), 0, 0, 0, 0, bitmap.GetWidth(), bitmap.GetHeight());
211
212 return true;
213 }
214
215 GdkPixmap* wxMask::GetBitmap() const
216 {
217 return m_bitmap;
218 }
219
220 //-----------------------------------------------------------------------------
221 // wxBitmapRefData
222 //-----------------------------------------------------------------------------
223
224 class wxBitmapRefData: public wxGDIRefData
225 {
226 public:
227 wxBitmapRefData(int width, int height, int depth);
228 virtual ~wxBitmapRefData();
229
230 virtual bool IsOk() const;
231
232 GdkPixmap *m_pixmap;
233 GdkPixbuf *m_pixbuf;
234 wxMask *m_mask;
235 int m_width;
236 int m_height;
237 int m_bpp;
238 bool m_alphaRequested;
239
240 private:
241 // We don't provide a copy ctor as copying m_pixmap and m_pixbuf properly
242 // is expensive and we don't want to do it implicitly (and possibly
243 // accidentally). wxBitmap::CloneGDIRefData() which does need to do it does
244 // it explicitly itself.
245 wxDECLARE_NO_COPY_CLASS(wxBitmapRefData);
246 };
247
248 wxBitmapRefData::wxBitmapRefData(int width, int height, int depth)
249 {
250 m_pixmap = NULL;
251 m_pixbuf = NULL;
252 m_mask = NULL;
253 m_width = width;
254 m_height = height;
255 m_bpp = depth;
256 if (m_bpp < 0)
257 m_bpp = gdk_drawable_get_depth(wxGetRootWindow()->window);
258 m_alphaRequested = depth == 32;
259 }
260
261 wxBitmapRefData::~wxBitmapRefData()
262 {
263 if (m_pixmap)
264 g_object_unref (m_pixmap);
265 if (m_pixbuf)
266 g_object_unref (m_pixbuf);
267 delete m_mask;
268 }
269
270 bool wxBitmapRefData::IsOk() const
271 {
272 return m_bpp != 0;
273 }
274
275 //-----------------------------------------------------------------------------
276 // wxBitmap
277 //-----------------------------------------------------------------------------
278
279 #define M_BMPDATA static_cast<wxBitmapRefData*>(m_refData)
280
281 IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxGDIObject)
282
283 wxBitmap::wxBitmap(const wxString &filename, wxBitmapType type)
284 {
285 LoadFile(filename, type);
286 }
287
288 wxBitmap::wxBitmap(const char bits[], int width, int height, int depth)
289 {
290 wxASSERT(depth == 1);
291 if (width > 0 && height > 0 && depth == 1)
292 {
293 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window, bits, width, height));
294 }
295 }
296
297 wxBitmap::wxBitmap(const char* const* bits)
298 {
299 wxCHECK2_MSG(bits != NULL, return, wxT("invalid bitmap data"));
300
301 GdkBitmap* mask = NULL;
302 SetPixmap(gdk_pixmap_create_from_xpm_d(wxGetRootWindow()->window, &mask, NULL, const_cast<char**>(bits)));
303 if (!M_BMPDATA)
304 return;
305
306 if (M_BMPDATA->m_pixmap != NULL && mask != NULL)
307 {
308 M_BMPDATA->m_mask = new wxMask(mask);
309 }
310 }
311
312 wxBitmap::~wxBitmap()
313 {
314 }
315
316 bool wxBitmap::Create( int width, int height, int depth )
317 {
318 UnRef();
319 wxCHECK_MSG(width >= 0 && height >= 0, false, "invalid bitmap size");
320 m_refData = new wxBitmapRefData(width, height, depth);
321 return true;
322 }
323
324 #if wxUSE_IMAGE
325
326 bool wxBitmap::CreateFromImage(const wxImage& image, int depth)
327 {
328 UnRef();
329
330 wxCHECK_MSG( image.IsOk(), false, wxT("invalid image") );
331
332 if (image.GetWidth() <= 0 || image.GetHeight() <= 0)
333 return false;
334
335 if (depth == 32 || (depth == -1 && image.HasAlpha()))
336 return CreateFromImageAsPixbuf(image);
337
338 // otherwise create pixmap, if alpha is present it will be converted to mask
339 return CreateFromImageAsPixmap(image, depth);
340 }
341
342 bool wxBitmap::CreateFromImageAsPixmap(const wxImage& image, int depth)
343 {
344 const int w = image.GetWidth();
345 const int h = image.GetHeight();
346 if (depth == 1)
347 {
348 // create XBM format bitmap
349
350 // one bit per pixel, each row starts on a byte boundary
351 const size_t out_size = size_t((w + 7) / 8) * unsigned(h);
352 wxByte* out = new wxByte[out_size];
353 // set bits are black
354 memset(out, 0xff, out_size);
355 const wxByte* in = image.GetData();
356 unsigned bit_index = 0;
357 for (int y = 0; y < h; y++)
358 {
359 for (int x = 0; x < w; x++, in += 3, bit_index++)
360 if (in[0] == 255 && in[1] == 255 && in[2] == 255)
361 out[bit_index >> 3] ^= 1 << (bit_index & 7);
362 // move index to next byte boundary
363 bit_index = (bit_index + 7) & ~7u;
364 }
365 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window, (char*)out, w, h));
366 delete[] out;
367
368 if (!M_BMPDATA) // SetPixmap may have failed
369 return false;
370 }
371 else
372 {
373 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window, w, h, depth));
374 if (!M_BMPDATA)
375 return false;
376
377 wxGtkObject<GdkGC> gc(gdk_gc_new(M_BMPDATA->m_pixmap));
378 gdk_draw_rgb_image(
379 M_BMPDATA->m_pixmap, gc,
380 0, 0, w, h,
381 GDK_RGB_DITHER_NONE, image.GetData(), w * 3);
382 }
383
384 const wxByte* alpha = image.GetAlpha();
385 if (alpha != NULL || image.HasMask())
386 {
387 // create mask as XBM format bitmap
388
389 const size_t out_size = size_t((w + 7) / 8) * unsigned(h);
390 wxByte* out = new wxByte[out_size];
391 memset(out, 0xff, out_size);
392 unsigned bit_index = 0;
393 if (alpha != NULL)
394 {
395 for (int y = 0; y < h; y++)
396 {
397 for (int x = 0; x < w; x++, bit_index++)
398 if (*alpha++ < wxIMAGE_ALPHA_THRESHOLD)
399 out[bit_index >> 3] ^= 1 << (bit_index & 7);
400 bit_index = (bit_index + 7) & ~7u;
401 }
402 }
403 else
404 {
405 const wxByte r_mask = image.GetMaskRed();
406 const wxByte g_mask = image.GetMaskGreen();
407 const wxByte b_mask = image.GetMaskBlue();
408 const wxByte* in = image.GetData();
409 for (int y = 0; y < h; y++)
410 {
411 for (int x = 0; x < w; x++, in += 3, bit_index++)
412 if (in[0] == r_mask && in[1] == g_mask && in[2] == b_mask)
413 out[bit_index >> 3] ^= 1 << (bit_index & 7);
414 bit_index = (bit_index + 7) & ~7u;
415 }
416 }
417 SetMask(new wxMask(gdk_bitmap_create_from_data(M_BMPDATA->m_pixmap, (char*)out, w, h)));
418 delete[] out;
419 }
420 return IsOk();
421 }
422
423 bool wxBitmap::CreateFromImageAsPixbuf(const wxImage& image)
424 {
425 int width = image.GetWidth();
426 int height = image.GetHeight();
427
428 Create(width, height, 32);
429 GdkPixbuf* pixbuf = GetPixbuf();
430 if (!pixbuf)
431 return false;
432
433 // Copy the data:
434 const unsigned char* in = image.GetData();
435 unsigned char *out = gdk_pixbuf_get_pixels(pixbuf);
436 unsigned char *alpha = image.GetAlpha();
437
438 int rowpad = gdk_pixbuf_get_rowstride(pixbuf) - 4 * width;
439
440 for (int y = 0; y < height; y++, out += rowpad)
441 {
442 for (int x = 0; x < width; x++, out += 4, in += 3)
443 {
444 out[0] = in[0];
445 out[1] = in[1];
446 out[2] = in[2];
447 if (alpha)
448 out[3] = *alpha++;
449 }
450 }
451
452 return true;
453 }
454
455 wxImage wxBitmap::ConvertToImage() const
456 {
457 wxCHECK_MSG( IsOk(), wxNullImage, wxT("invalid bitmap") );
458
459 const int w = GetWidth();
460 const int h = GetHeight();
461 wxImage image(w, h, false);
462 unsigned char *data = image.GetData();
463
464 wxCHECK_MSG(data != NULL, wxNullImage, wxT("couldn't create image") );
465
466 // prefer pixbuf if available, it will preserve alpha and should be quicker
467 if (HasPixbuf())
468 {
469 GdkPixbuf *pixbuf = GetPixbuf();
470 unsigned char* alpha = NULL;
471 if (gdk_pixbuf_get_has_alpha(pixbuf))
472 {
473 image.SetAlpha();
474 alpha = image.GetAlpha();
475 }
476 const unsigned char* in = gdk_pixbuf_get_pixels(pixbuf);
477 unsigned char *out = data;
478 const int inc = 3 + int(alpha != NULL);
479 const int rowpad = gdk_pixbuf_get_rowstride(pixbuf) - inc * w;
480
481 for (int y = 0; y < h; y++, in += rowpad)
482 {
483 for (int x = 0; x < w; x++, in += inc, out += 3)
484 {
485 out[0] = in[0];
486 out[1] = in[1];
487 out[2] = in[2];
488 if (alpha != NULL)
489 *alpha++ = in[3];
490 }
491 }
492 }
493 else
494 {
495 GdkPixmap* pixmap = GetPixmap();
496 GdkPixmap* pixmap_invert = NULL;
497 if (GetDepth() == 1)
498 {
499 // mono bitmaps are inverted, i.e. 0 is white
500 pixmap_invert = gdk_pixmap_new(pixmap, w, h, 1);
501 wxGtkObject<GdkGC> gc(gdk_gc_new(pixmap_invert));
502 gdk_gc_set_function(gc, GDK_COPY_INVERT);
503 gdk_draw_drawable(pixmap_invert, gc, pixmap, 0, 0, 0, 0, w, h);
504 pixmap = pixmap_invert;
505 }
506 // create a pixbuf which shares data with the wxImage
507 GdkPixbuf* pixbuf = gdk_pixbuf_new_from_data(
508 data, GDK_COLORSPACE_RGB, false, 8, w, h, 3 * w, NULL, NULL);
509
510 gdk_pixbuf_get_from_drawable(pixbuf, pixmap, NULL, 0, 0, 0, 0, w, h);
511
512 g_object_unref(pixbuf);
513 if (pixmap_invert != NULL)
514 g_object_unref(pixmap_invert);
515 }
516 // convert mask, unless there is already alpha
517 if (GetMask() && !image.HasAlpha())
518 {
519 // we hard code the mask colour for now but we could also make an
520 // effort (and waste time) to choose a colour not present in the
521 // image already to avoid having to fudge the pixels below --
522 // whether it's worth to do it is unclear however
523 const int MASK_RED = 1;
524 const int MASK_GREEN = 2;
525 const int MASK_BLUE = 3;
526 const int MASK_BLUE_REPLACEMENT = 2;
527
528 image.SetMaskColour(MASK_RED, MASK_GREEN, MASK_BLUE);
529 GdkImage* image_mask = gdk_drawable_get_image(GetMask()->GetBitmap(), 0, 0, w, h);
530
531 for (int y = 0; y < h; y++)
532 {
533 for (int x = 0; x < w; x++, data += 3)
534 {
535 if (gdk_image_get_pixel(image_mask, x, y) == 0)
536 {
537 data[0] = MASK_RED;
538 data[1] = MASK_GREEN;
539 data[2] = MASK_BLUE;
540 }
541 else if (data[0] == MASK_RED && data[1] == MASK_GREEN && data[2] == MASK_BLUE)
542 {
543 // we have to fudge the colour a bit to prevent
544 // this pixel from appearing transparent
545 data[2] = MASK_BLUE_REPLACEMENT;
546 }
547 }
548 }
549 g_object_unref(image_mask);
550 }
551
552 return image;
553 }
554
555 #endif // wxUSE_IMAGE
556
557 int wxBitmap::GetHeight() const
558 {
559 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
560
561 return M_BMPDATA->m_height;
562 }
563
564 int wxBitmap::GetWidth() const
565 {
566 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
567
568 return M_BMPDATA->m_width;
569 }
570
571 int wxBitmap::GetDepth() const
572 {
573 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
574
575 return M_BMPDATA->m_bpp;
576 }
577
578 wxMask *wxBitmap::GetMask() const
579 {
580 wxCHECK_MSG( IsOk(), NULL, wxT("invalid bitmap") );
581
582 return M_BMPDATA->m_mask;
583 }
584
585 void wxBitmap::SetMask( wxMask *mask )
586 {
587 wxCHECK_RET( IsOk(), wxT("invalid bitmap") );
588
589 AllocExclusive();
590 delete M_BMPDATA->m_mask;
591 M_BMPDATA->m_mask = mask;
592 }
593
594 bool wxBitmap::CopyFromIcon(const wxIcon& icon)
595 {
596 *this = icon;
597 return IsOk();
598 }
599
600 wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const
601 {
602 wxBitmap ret;
603
604 wxCHECK_MSG(IsOk(), ret, wxT("invalid bitmap"));
605
606 const int w = rect.width;
607 const int h = rect.height;
608 const wxBitmapRefData* bmpData = M_BMPDATA;
609
610 wxCHECK_MSG(rect.x >= 0 && rect.y >= 0 &&
611 rect.x + w <= bmpData->m_width &&
612 rect.y + h <= bmpData->m_height,
613 ret, wxT("invalid bitmap region"));
614
615 wxBitmapRefData * const newRef = new wxBitmapRefData(w, h, bmpData->m_bpp);
616 ret.m_refData = newRef;
617
618 if (bmpData->m_pixbuf)
619 {
620 GdkPixbuf* pixbuf =
621 gdk_pixbuf_new_subpixbuf(bmpData->m_pixbuf, rect.x, rect.y, w, h);
622 newRef->m_pixbuf = gdk_pixbuf_copy(pixbuf);
623 g_object_unref(pixbuf);
624 }
625 if (bmpData->m_pixmap)
626 {
627 newRef->m_pixmap = gdk_pixmap_new(bmpData->m_pixmap, w, h, -1);
628 GdkGC* gc = gdk_gc_new(newRef->m_pixmap);
629 gdk_draw_drawable(
630 newRef->m_pixmap, gc, bmpData->m_pixmap, rect.x, rect.y, 0, 0, w, h);
631 g_object_unref(gc);
632 }
633 GdkPixmap* mask = NULL;
634 if (bmpData->m_mask)
635 mask = bmpData->m_mask->GetBitmap();
636 if (mask)
637 {
638 GdkPixmap* sub_mask = gdk_pixmap_new(mask, w, h, 1);
639 newRef->m_mask = new wxMask(sub_mask);
640 GdkGC* gc = gdk_gc_new(sub_mask);
641 gdk_draw_drawable(
642 sub_mask, gc, mask, rect.x, rect.y, 0, 0, w, h);
643 g_object_unref(gc);
644 }
645
646 return ret;
647 }
648
649 bool wxBitmap::SaveFile( const wxString &name, wxBitmapType type, const wxPalette *WXUNUSED(palette) ) const
650 {
651 wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") );
652
653 #if wxUSE_IMAGE
654 wxImage image = ConvertToImage();
655 if (image.IsOk() && image.SaveFile(name, type))
656 return true;
657 #endif
658 const char* type_name = NULL;
659 switch (type)
660 {
661 case wxBITMAP_TYPE_BMP: type_name = "bmp"; break;
662 case wxBITMAP_TYPE_ICO: type_name = "ico"; break;
663 case wxBITMAP_TYPE_JPEG: type_name = "jpeg"; break;
664 case wxBITMAP_TYPE_PNG: type_name = "png"; break;
665 default: break;
666 }
667 return type_name &&
668 gdk_pixbuf_save(GetPixbuf(), name.fn_str(), type_name, NULL, NULL);
669 }
670
671 bool wxBitmap::LoadFile( const wxString &name, wxBitmapType type )
672 {
673 #if wxUSE_IMAGE
674 wxImage image;
675 if (image.LoadFile(name, type) && image.IsOk())
676 *this = wxBitmap(image);
677 else
678 #endif
679 {
680 wxUnusedVar(type); // The type is detected automatically by GDK.
681
682 UnRef();
683 GdkPixbuf* pixbuf = gdk_pixbuf_new_from_file(name.fn_str(), NULL);
684 if (pixbuf)
685 SetPixbuf(pixbuf);
686 }
687
688 return IsOk();
689 }
690
691 #if wxUSE_PALETTE
692 wxPalette *wxBitmap::GetPalette() const
693 {
694 return NULL;
695 }
696
697 void wxBitmap::SetPalette(const wxPalette& WXUNUSED(palette))
698 {
699 // TODO
700 }
701 #endif // wxUSE_PALETTE
702
703 void wxBitmap::SetHeight( int height )
704 {
705 AllocExclusive();
706 M_BMPDATA->m_height = height;
707 }
708
709 void wxBitmap::SetWidth( int width )
710 {
711 AllocExclusive();
712 M_BMPDATA->m_width = width;
713 }
714
715 void wxBitmap::SetDepth( int depth )
716 {
717 AllocExclusive();
718 M_BMPDATA->m_bpp = depth;
719 }
720
721 void wxBitmap::SetPixmap( GdkPixmap *pixmap )
722 {
723 UnRef();
724
725 if (!pixmap)
726 return;
727
728 int w, h;
729 gdk_drawable_get_size(pixmap, &w, &h);
730 wxBitmapRefData* bmpData = new wxBitmapRefData(w, h, 0);
731 m_refData = bmpData;
732 bmpData->m_pixmap = pixmap;
733 bmpData->m_bpp = gdk_drawable_get_depth(pixmap);
734 }
735
736 GdkPixmap *wxBitmap::GetPixmap() const
737 {
738 wxCHECK_MSG( IsOk(), NULL, wxT("invalid bitmap") );
739
740 wxBitmapRefData* bmpData = M_BMPDATA;
741 if (bmpData->m_pixmap)
742 return bmpData->m_pixmap;
743
744 if (bmpData->m_pixbuf)
745 {
746 GdkPixmap* pixmap = NULL;
747 GdkPixmap** mask_pixmap = NULL;
748 if (gdk_pixbuf_get_has_alpha(bmpData->m_pixbuf))
749 {
750 // make new mask from alpha
751 mask_pixmap = &pixmap;
752 }
753 gdk_pixbuf_render_pixmap_and_mask(
754 bmpData->m_pixbuf, &bmpData->m_pixmap, mask_pixmap, 128);
755 if (pixmap)
756 {
757 delete bmpData->m_mask;
758 bmpData->m_mask = new wxMask(pixmap);
759 }
760 }
761 else
762 {
763 bmpData->m_pixmap = gdk_pixmap_new(wxGetRootWindow()->window,
764 bmpData->m_width, bmpData->m_height, bmpData->m_bpp == 1 ? 1 : -1);
765 }
766 return bmpData->m_pixmap;
767 }
768
769 bool wxBitmap::HasPixmap() const
770 {
771 wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") );
772
773 return M_BMPDATA->m_pixmap != NULL;
774 }
775
776 GdkPixbuf *wxBitmap::GetPixbuf() const
777 {
778 wxCHECK_MSG( IsOk(), NULL, wxT("invalid bitmap") );
779
780 wxBitmapRefData* bmpData = M_BMPDATA;
781 if (bmpData->m_pixbuf)
782 return bmpData->m_pixbuf;
783
784 const int w = bmpData->m_width;
785 const int h = bmpData->m_height;
786 GdkPixmap* mask = NULL;
787 if (bmpData->m_mask)
788 mask = bmpData->m_mask->GetBitmap();
789 const bool useAlpha = bmpData->m_alphaRequested || mask;
790 bmpData->m_pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, useAlpha, 8, w, h);
791 if (bmpData->m_pixmap)
792 PixmapToPixbuf(bmpData->m_pixmap, bmpData->m_pixbuf, w, h);
793 if (mask)
794 MaskToAlpha(mask, bmpData->m_pixbuf, w, h);
795 return bmpData->m_pixbuf;
796 }
797
798 bool wxBitmap::HasPixbuf() const
799 {
800 wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") );
801
802 return M_BMPDATA->m_pixbuf != NULL;
803 }
804
805 void wxBitmap::SetPixbuf(GdkPixbuf* pixbuf)
806 {
807 UnRef();
808
809 if (!pixbuf)
810 return;
811
812 int depth = -1;
813 if (gdk_pixbuf_get_has_alpha(pixbuf))
814 depth = 32;
815 m_refData = new wxBitmapRefData(
816 gdk_pixbuf_get_width(pixbuf), gdk_pixbuf_get_height(pixbuf), depth);
817
818 M_BMPDATA->m_pixbuf = pixbuf;
819 }
820
821 void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep)
822 {
823 if (keep == Pixmap && HasPixbuf())
824 {
825 g_object_unref (M_BMPDATA->m_pixbuf);
826 M_BMPDATA->m_pixbuf = NULL;
827 }
828 if (keep == Pixbuf && HasPixmap())
829 {
830 g_object_unref (M_BMPDATA->m_pixmap);
831 M_BMPDATA->m_pixmap = NULL;
832 }
833 }
834
835 #ifdef wxHAS_RAW_BITMAP
836 void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp)
837 {
838 void* bits = NULL;
839 GdkPixbuf *pixbuf = GetPixbuf();
840 const bool hasAlpha = HasAlpha();
841
842 // allow access if bpp is valid and matches existence of alpha
843 if ( pixbuf && ((bpp == 24 && !hasAlpha) || (bpp == 32 && hasAlpha)) )
844 {
845 data.m_height = gdk_pixbuf_get_height( pixbuf );
846 data.m_width = gdk_pixbuf_get_width( pixbuf );
847 data.m_stride = gdk_pixbuf_get_rowstride( pixbuf );
848 bits = gdk_pixbuf_get_pixels(pixbuf);
849 }
850 return bits;
851 }
852
853 void wxBitmap::UngetRawData(wxPixelDataBase& WXUNUSED(data))
854 {
855 }
856 #endif // wxHAS_RAW_BITMAP
857
858 bool wxBitmap::HasAlpha() const
859 {
860 const wxBitmapRefData* bmpData = M_BMPDATA;
861 return bmpData && (bmpData->m_alphaRequested ||
862 (bmpData->m_pixbuf && gdk_pixbuf_get_has_alpha(bmpData->m_pixbuf)));
863 }
864
865 wxGDIRefData* wxBitmap::CreateGDIRefData() const
866 {
867 return new wxBitmapRefData(0, 0, 0);
868 }
869
870 wxGDIRefData* wxBitmap::CloneGDIRefData(const wxGDIRefData* data) const
871 {
872 const wxBitmapRefData* oldRef = static_cast<const wxBitmapRefData*>(data);
873 wxBitmapRefData * const newRef = new wxBitmapRefData(oldRef->m_width,
874 oldRef->m_height,
875 oldRef->m_bpp);
876 if (oldRef->m_pixmap != NULL)
877 {
878 newRef->m_pixmap = gdk_pixmap_new(
879 oldRef->m_pixmap, oldRef->m_width, oldRef->m_height,
880 // use pixmap depth, m_bpp may not match
881 gdk_drawable_get_depth(oldRef->m_pixmap));
882 wxGtkObject<GdkGC> gc(gdk_gc_new(newRef->m_pixmap));
883 gdk_draw_drawable(
884 newRef->m_pixmap, gc, oldRef->m_pixmap, 0, 0, 0, 0, -1, -1);
885 }
886 if (oldRef->m_pixbuf != NULL)
887 {
888 newRef->m_pixbuf = gdk_pixbuf_copy(oldRef->m_pixbuf);
889 }
890 if (oldRef->m_mask != NULL)
891 {
892 newRef->m_mask = new wxMask(*oldRef->m_mask);
893 }
894
895 return newRef;
896 }
897
898 /* static */ void wxBitmap::InitStandardHandlers()
899 {
900 // TODO: Insert handler based on GdkPixbufs handler later
901 }