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