]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/bitmap.cpp
Automatically adjust toolbar's tool size if the provided bitmaps
[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 // wxBitmap
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 #define M_BMPDATA static_cast<wxBitmapRefData*>(m_refData)
232
233 IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxGDIObject)
234
235 wxBitmap::wxBitmap(int width, int height, int depth)
236 {
237 Create(width, height, depth);
238 }
239
240 wxBitmap::wxBitmap(const wxString &filename, wxBitmapType type)
241 {
242 LoadFile(filename, type);
243 }
244
245 wxBitmap::wxBitmap(const char bits[], int width, int height, int depth)
246 {
247 wxASSERT(depth == 1);
248 if (width > 0 && height > 0 && depth == 1)
249 {
250 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window, bits, width, height));
251 }
252 }
253
254 wxBitmap::wxBitmap(const char* const* bits)
255 {
256 wxCHECK2_MSG(bits != NULL, return, wxT("invalid bitmap data"));
257
258 GdkBitmap* mask = NULL;
259 SetPixmap(gdk_pixmap_create_from_xpm_d(wxGetRootWindow()->window, &mask, NULL, const_cast<char**>(bits)));
260 if (!M_BMPDATA)
261 return;
262
263 if (M_BMPDATA->m_pixmap != NULL && mask != NULL)
264 {
265 M_BMPDATA->m_mask = new wxMask;
266 M_BMPDATA->m_mask->m_bitmap = mask;
267 }
268 }
269
270 wxBitmap::~wxBitmap()
271 {
272 }
273
274 bool wxBitmap::Create( int width, int height, int depth )
275 {
276 UnRef();
277
278 if ( width <= 0 || height <= 0 )
279 {
280 return false;
281 }
282
283 const GdkVisual* visual = wxTheApp->GetGdkVisual();
284
285 if (depth == 32)
286 {
287 SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB, true, 8, width, height), 32);
288
289 if (!M_BMPDATA)
290 return false;
291
292 // must initialize alpha, otherwise GetPixmap()
293 // will create a mask out of garbage
294 gdk_pixbuf_fill(M_BMPDATA->m_pixbuf, 0x000000ff);
295 }
296 else if (depth == 24)
297 {
298 if (visual->depth == depth)
299 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window, width, height, depth));
300 else
301 SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB, false, 8, width, height), 24);
302 }
303 else
304 {
305 if (depth != 1)
306 {
307 if (depth == -1)
308 depth = visual->depth;
309 }
310 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window, width, height, depth));
311 }
312
313 return IsOk();
314 }
315
316 #if wxUSE_IMAGE
317
318 bool wxBitmap::CreateFromImage(const wxImage& image, int depth)
319 {
320 UnRef();
321
322 wxCHECK_MSG( image.IsOk(), false, wxT("invalid image") );
323 wxCHECK_MSG( depth == -1 || depth == 1, false, wxT("invalid bitmap depth") );
324
325 if (image.GetWidth() <= 0 || image.GetHeight() <= 0)
326 return false;
327
328 // create pixbuf if image has alpha and requested depth is compatible
329 if (image.HasAlpha() && (depth == -1 || depth == 32))
330 return CreateFromImageAsPixbuf(image);
331
332 // otherwise create pixmap, if alpha is present it will be converted to mask
333 return CreateFromImageAsPixmap(image, depth);
334 }
335
336 bool wxBitmap::CreateFromImageAsPixmap(const wxImage& image, int depth)
337 {
338 const int w = image.GetWidth();
339 const int h = image.GetHeight();
340 if (depth == 1)
341 {
342 // create XBM format bitmap
343
344 // one bit per pixel, each row starts on a byte boundary
345 const size_t out_size = size_t((w + 7) / 8) * unsigned(h);
346 wxByte* out = new wxByte[out_size];
347 // set bits are black
348 memset(out, 0xff, out_size);
349 const wxByte* in = image.GetData();
350 unsigned bit_index = 0;
351 for (int y = 0; y < h; y++)
352 {
353 for (int x = 0; x < w; x++, in += 3, bit_index++)
354 if (in[0] == 255 && in[1] == 255 && in[2] == 255)
355 out[bit_index >> 3] ^= 1 << (bit_index & 7);
356 // move index to next byte boundary
357 bit_index = (bit_index + 7) & ~7u;
358 }
359 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window, (char*)out, w, h));
360 delete[] out;
361
362 if (!M_BMPDATA) // SetPixmap may have failed
363 return false;
364 }
365 else
366 {
367 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window, w, h, depth));
368 if (!M_BMPDATA)
369 return false;
370
371 wxGtkObject<GdkGC> gc(gdk_gc_new(M_BMPDATA->m_pixmap));
372 gdk_draw_rgb_image(
373 M_BMPDATA->m_pixmap, gc,
374 0, 0, w, h,
375 GDK_RGB_DITHER_NONE, image.GetData(), w * 3);
376 }
377
378 const wxByte* alpha = image.GetAlpha();
379 if (alpha != NULL || image.HasMask())
380 {
381 // create mask as XBM format bitmap
382
383 const size_t out_size = size_t((w + 7) / 8) * unsigned(h);
384 wxByte* out = new wxByte[out_size];
385 memset(out, 0xff, out_size);
386 unsigned bit_index = 0;
387 if (alpha != NULL)
388 {
389 for (int y = 0; y < h; y++)
390 {
391 for (int x = 0; x < w; x++, bit_index++)
392 if (*alpha++ < wxIMAGE_ALPHA_THRESHOLD)
393 out[bit_index >> 3] ^= 1 << (bit_index & 7);
394 bit_index = (bit_index + 7) & ~7u;
395 }
396 }
397 else
398 {
399 const wxByte r_mask = image.GetMaskRed();
400 const wxByte g_mask = image.GetMaskGreen();
401 const wxByte b_mask = image.GetMaskBlue();
402 const wxByte* in = image.GetData();
403 for (int y = 0; y < h; y++)
404 {
405 for (int x = 0; x < w; x++, in += 3, bit_index++)
406 if (in[0] == r_mask && in[1] == g_mask && in[2] == b_mask)
407 out[bit_index >> 3] ^= 1 << (bit_index & 7);
408 bit_index = (bit_index + 7) & ~7u;
409 }
410 }
411 wxMask* mask = new wxMask;
412 mask->m_bitmap = gdk_bitmap_create_from_data(M_BMPDATA->m_pixmap, (char*)out, w, h);
413 SetMask(mask);
414 delete[] out;
415 }
416 return IsOk();
417 }
418
419 bool wxBitmap::CreateFromImageAsPixbuf(const wxImage& image)
420 {
421 wxASSERT(image.HasAlpha());
422
423 int width = image.GetWidth();
424 int height = image.GetHeight();
425
426 Create(width, height, 32);
427 GdkPixbuf* pixbuf = M_BMPDATA->m_pixbuf;
428 if (!pixbuf)
429 return false;
430
431 // Copy the data:
432 const unsigned char* in = image.GetData();
433 unsigned char *out = gdk_pixbuf_get_pixels(pixbuf);
434 unsigned char *alpha = image.GetAlpha();
435
436 int rowpad = gdk_pixbuf_get_rowstride(pixbuf) - 4 * width;
437
438 for (int y = 0; y < height; y++, out += rowpad)
439 {
440 for (int x = 0; x < width; x++, alpha++, out += 4, in += 3)
441 {
442 out[0] = in[0];
443 out[1] = in[1];
444 out[2] = in[2];
445 out[3] = *alpha;
446 }
447 }
448
449 return true;
450 }
451
452 wxImage wxBitmap::ConvertToImage() const
453 {
454 wxCHECK_MSG( IsOk(), wxNullImage, wxT("invalid bitmap") );
455
456 const int w = GetWidth();
457 const int h = GetHeight();
458 wxImage image(w, h, false);
459 unsigned char *data = image.GetData();
460
461 wxCHECK_MSG(data != NULL, wxNullImage, wxT("couldn't create image") );
462
463 // prefer pixbuf if available, it will preserve alpha and should be quicker
464 if (HasPixbuf())
465 {
466 GdkPixbuf *pixbuf = GetPixbuf();
467 unsigned char* alpha = NULL;
468 if (gdk_pixbuf_get_has_alpha(pixbuf))
469 {
470 image.SetAlpha();
471 alpha = image.GetAlpha();
472 }
473 const unsigned char* in = gdk_pixbuf_get_pixels(pixbuf);
474 unsigned char *out = data;
475 const int inc = 3 + int(alpha != NULL);
476 const int rowpad = gdk_pixbuf_get_rowstride(pixbuf) - inc * w;
477
478 for (int y = 0; y < h; y++, in += rowpad)
479 {
480 for (int x = 0; x < w; x++, in += inc, out += 3)
481 {
482 out[0] = in[0];
483 out[1] = in[1];
484 out[2] = in[2];
485 if (alpha != NULL)
486 *alpha++ = in[3];
487 }
488 }
489 }
490 else
491 {
492 GdkPixmap* pixmap = GetPixmap();
493 GdkPixmap* pixmap_invert = NULL;
494 if (GetDepth() == 1)
495 {
496 // mono bitmaps are inverted, i.e. 0 is white
497 pixmap_invert = gdk_pixmap_new(pixmap, w, h, 1);
498 wxGtkObject<GdkGC> gc(gdk_gc_new(pixmap_invert));
499 gdk_gc_set_function(gc, GDK_COPY_INVERT);
500 gdk_draw_drawable(pixmap_invert, gc, pixmap, 0, 0, 0, 0, w, h);
501 pixmap = pixmap_invert;
502 }
503 // create a pixbuf which shares data with the wxImage
504 GdkPixbuf* pixbuf = gdk_pixbuf_new_from_data(
505 data, GDK_COLORSPACE_RGB, false, 8, w, h, 3 * w, NULL, NULL);
506
507 gdk_pixbuf_get_from_drawable(pixbuf, pixmap, NULL, 0, 0, 0, 0, w, h);
508
509 g_object_unref(pixbuf);
510 if (pixmap_invert != NULL)
511 g_object_unref(pixmap_invert);
512 }
513 // convert mask, unless there is already alpha
514 if (GetMask() && !image.HasAlpha())
515 {
516 // we hard code the mask colour for now but we could also make an
517 // effort (and waste time) to choose a colour not present in the
518 // image already to avoid having to fudge the pixels below --
519 // whether it's worth to do it is unclear however
520 const int MASK_RED = 1;
521 const int MASK_GREEN = 2;
522 const int MASK_BLUE = 3;
523 const int MASK_BLUE_REPLACEMENT = 2;
524
525 image.SetMaskColour(MASK_RED, MASK_GREEN, MASK_BLUE);
526 GdkImage* image_mask = gdk_drawable_get_image(GetMask()->GetBitmap(), 0, 0, w, h);
527
528 for (int y = 0; y < h; y++)
529 {
530 for (int x = 0; x < w; x++, data += 3)
531 {
532 if (gdk_image_get_pixel(image_mask, x, y) == 0)
533 {
534 data[0] = MASK_RED;
535 data[1] = MASK_GREEN;
536 data[2] = MASK_BLUE;
537 }
538 else if (data[0] == MASK_RED && data[1] == MASK_GREEN && data[2] == MASK_BLUE)
539 {
540 // we have to fudge the colour a bit to prevent
541 // this pixel from appearing transparent
542 data[2] = MASK_BLUE_REPLACEMENT;
543 }
544 }
545 }
546 g_object_unref(image_mask);
547 }
548
549 return image;
550 }
551
552 #endif // wxUSE_IMAGE
553
554 int wxBitmap::GetHeight() const
555 {
556 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
557
558 return M_BMPDATA->m_height;
559 }
560
561 int wxBitmap::GetWidth() const
562 {
563 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
564
565 return M_BMPDATA->m_width;
566 }
567
568 int wxBitmap::GetDepth() const
569 {
570 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
571
572 return M_BMPDATA->m_bpp;
573 }
574
575 wxMask *wxBitmap::GetMask() const
576 {
577 wxCHECK_MSG( IsOk(), NULL, wxT("invalid bitmap") );
578
579 return M_BMPDATA->m_mask;
580 }
581
582 void wxBitmap::SetMask( wxMask *mask )
583 {
584 wxCHECK_RET( IsOk(), wxT("invalid bitmap") );
585
586 AllocExclusive();
587 delete M_BMPDATA->m_mask;
588 M_BMPDATA->m_mask = mask;
589 }
590
591 bool wxBitmap::CopyFromIcon(const wxIcon& icon)
592 {
593 *this = icon;
594 return IsOk();
595 }
596
597 wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const
598 {
599 wxBitmap ret;
600
601 wxCHECK_MSG(IsOk(), ret, wxT("invalid bitmap"));
602 wxCHECK_MSG(rect.x >= 0 && rect.y >= 0 &&
603 rect.x + rect.width <= M_BMPDATA->m_width &&
604 rect.y + rect.height <= M_BMPDATA->m_height,
605 ret, wxT("invalid bitmap region"));
606
607 if (HasPixbuf() || M_BMPDATA->m_bpp == 32)
608 {
609 GdkPixbuf *pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB,
610 gdk_pixbuf_get_has_alpha(GetPixbuf()),
611 8, rect.width, rect.height);
612 if (!pixbuf)
613 return ret;
614
615 ret.SetPixbuf(pixbuf, M_BMPDATA->m_bpp);
616 gdk_pixbuf_copy_area(GetPixbuf(),
617 rect.x, rect.y, rect.width, rect.height,
618 pixbuf, 0, 0);
619 }
620 else
621 {
622 ret.Create(rect.width, rect.height, M_BMPDATA->m_bpp);
623 wxGtkObject<GdkGC> gc(gdk_gc_new( ret.GetPixmap() ));
624 gdk_draw_drawable( ret.GetPixmap(), gc, GetPixmap(), rect.x, rect.y, 0, 0, rect.width, rect.height );
625 }
626 // make mask, unless there is already alpha
627 if (GetMask() && !HasAlpha())
628 {
629 wxMask *mask = new wxMask;
630 mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, rect.width, rect.height, 1 );
631
632 wxGtkObject<GdkGC> gc(gdk_gc_new( mask->m_bitmap ));
633 gdk_draw_drawable(mask->m_bitmap, gc, M_BMPDATA->m_mask->m_bitmap, rect.x, rect.y, 0, 0, rect.width, rect.height);
634
635 ret.SetMask( mask );
636 }
637
638 return ret;
639 }
640
641 bool wxBitmap::SaveFile( const wxString &name, wxBitmapType type, const wxPalette *WXUNUSED(palette) ) const
642 {
643 wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") );
644
645 #if wxUSE_IMAGE
646 // Try to save the bitmap via wxImage handlers:
647 wxImage image = ConvertToImage();
648 return image.Ok() && image.SaveFile(name, type);
649 #else // !wxUSE_IMAGE
650 wxUnusedVar(name);
651 wxUnusedVar(type);
652
653 return false;
654 #endif // wxUSE_IMAGE
655 }
656
657 bool wxBitmap::LoadFile( const wxString &name, wxBitmapType type )
658 {
659 UnRef();
660
661 if (type == wxBITMAP_TYPE_XPM)
662 {
663 GdkBitmap *mask = NULL;
664 SetPixmap(gdk_pixmap_create_from_xpm(wxGetRootWindow()->window, &mask, NULL, name.fn_str()));
665 if (!M_BMPDATA)
666 return false; // do not set the mask
667
668 if (mask)
669 {
670 M_BMPDATA->m_mask = new wxMask;
671 M_BMPDATA->m_mask->m_bitmap = mask;
672 }
673 }
674 #if wxUSE_IMAGE
675 else // try if wxImage can load it
676 {
677 wxImage image;
678 if (image.LoadFile(name, type) && image.Ok())
679 CreateFromImage(image, -1);
680 }
681 #endif // wxUSE_IMAGE
682
683 return IsOk();
684 }
685
686 #if wxUSE_PALETTE
687 wxPalette *wxBitmap::GetPalette() const
688 {
689 wxCHECK_MSG(IsOk(), NULL, wxT("invalid bitmap"));
690
691 return M_BMPDATA->m_palette;
692 }
693
694 void wxBitmap::SetPalette(const wxPalette& WXUNUSED(palette))
695 {
696 // TODO
697 }
698 #endif // wxUSE_PALETTE
699
700 void wxBitmap::SetHeight( int height )
701 {
702 AllocExclusive();
703 M_BMPDATA->m_height = height;
704 }
705
706 void wxBitmap::SetWidth( int width )
707 {
708 AllocExclusive();
709 M_BMPDATA->m_width = width;
710 }
711
712 void wxBitmap::SetDepth( int depth )
713 {
714 AllocExclusive();
715 M_BMPDATA->m_bpp = depth;
716 }
717
718 void wxBitmap::SetPixmap( GdkPixmap *pixmap )
719 {
720 if (!pixmap)
721 return;
722
723 if (!m_refData)
724 m_refData = new wxBitmapRefData;
725
726 // AllocExclusive should not be needed for this internal function
727 wxASSERT(m_refData->GetRefCount() == 1);
728 wxASSERT(M_BMPDATA->m_pixmap == NULL);
729 M_BMPDATA->m_pixmap = pixmap;
730 gdk_drawable_get_size(pixmap, &M_BMPDATA->m_width, &M_BMPDATA->m_height);
731 M_BMPDATA->m_bpp = gdk_drawable_get_depth(pixmap);
732 PurgeOtherRepresentations(Pixmap);
733 }
734
735 GdkPixmap *wxBitmap::GetPixmap() const
736 {
737 wxCHECK_MSG( IsOk(), NULL, wxT("invalid bitmap") );
738
739 // create the pixmap on the fly if we use Pixbuf representation:
740 if (M_BMPDATA->m_pixmap == NULL)
741 {
742 GdkPixmap** pmask = NULL;
743 if (gdk_pixbuf_get_has_alpha(M_BMPDATA->m_pixbuf))
744 {
745 // make new mask from alpha
746 delete M_BMPDATA->m_mask;
747 M_BMPDATA->m_mask = new wxMask;
748 pmask = &M_BMPDATA->m_mask->m_bitmap;
749 }
750 gdk_pixbuf_render_pixmap_and_mask(M_BMPDATA->m_pixbuf,
751 &M_BMPDATA->m_pixmap,
752 pmask,
753 0x80 /* alpha threshold */);
754 }
755
756 return M_BMPDATA->m_pixmap;
757 }
758
759 bool wxBitmap::HasPixmap() const
760 {
761 wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") );
762
763 return M_BMPDATA->m_pixmap != NULL;
764 }
765
766 GdkPixbuf *wxBitmap::GetPixbuf() const
767 {
768 wxCHECK_MSG( IsOk(), NULL, wxT("invalid bitmap") );
769
770 if (M_BMPDATA->m_pixbuf == NULL)
771 {
772
773 int width = GetWidth();
774 int height = GetHeight();
775
776 GdkPixbuf *pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB,
777 GetMask() != NULL,
778 8, width, height);
779 M_BMPDATA->m_pixbuf = pixbuf;
780 gdk_pixbuf_get_from_drawable(pixbuf, M_BMPDATA->m_pixmap, NULL,
781 0, 0, 0, 0, width, height);
782 // apply the mask to created pixbuf:
783 if (M_BMPDATA->m_pixbuf && M_BMPDATA->m_mask)
784 {
785 GdkPixbuf *pmask =
786 gdk_pixbuf_get_from_drawable(NULL,
787 M_BMPDATA->m_mask->GetBitmap(),
788 NULL,
789 0, 0, 0, 0, width, height);
790 if (pmask)
791 {
792 guchar *bmp = gdk_pixbuf_get_pixels(pixbuf);
793 guchar *mask = gdk_pixbuf_get_pixels(pmask);
794 int bmprowinc = gdk_pixbuf_get_rowstride(pixbuf) - 4 * width;
795 int maskrowinc = gdk_pixbuf_get_rowstride(pmask) - 3 * width;
796
797 for (int y = 0; y < height;
798 y++, bmp += bmprowinc, mask += maskrowinc)
799 {
800 for (int x = 0; x < width; x++, bmp += 4, mask += 3)
801 {
802 if (mask[0] == 0 /*black pixel*/)
803 bmp[3] = 0;
804 }
805 }
806
807 g_object_unref (pmask);
808 }
809 }
810 }
811
812 return M_BMPDATA->m_pixbuf;
813 }
814
815 bool wxBitmap::HasPixbuf() const
816 {
817 wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") );
818
819 return M_BMPDATA->m_pixbuf != NULL;
820 }
821
822 void wxBitmap::SetPixbuf(GdkPixbuf* pixbuf, int depth)
823 {
824 if (!pixbuf)
825 return;
826
827 if (!m_refData)
828 m_refData = new wxBitmapRefData;
829
830 // AllocExclusive should not be needed for this internal function
831 wxASSERT(m_refData->GetRefCount() == 1);
832 wxASSERT(M_BMPDATA->m_pixbuf == NULL);
833 M_BMPDATA->m_pixbuf = pixbuf;
834 M_BMPDATA->m_width = gdk_pixbuf_get_width(pixbuf);
835 M_BMPDATA->m_height = gdk_pixbuf_get_height(pixbuf);
836 // if depth specified
837 if (depth != 0)
838 M_BMPDATA->m_bpp = depth;
839 else if (M_BMPDATA->m_bpp == 0)
840 // use something reasonable
841 M_BMPDATA->m_bpp = wxTheApp->GetGdkVisual()->depth;
842 PurgeOtherRepresentations(Pixbuf);
843 }
844
845 void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep)
846 {
847 if (keep == Pixmap && HasPixbuf())
848 {
849 g_object_unref (M_BMPDATA->m_pixbuf);
850 M_BMPDATA->m_pixbuf = NULL;
851 }
852 if (keep == Pixbuf && HasPixmap())
853 {
854 g_object_unref (M_BMPDATA->m_pixmap);
855 M_BMPDATA->m_pixmap = NULL;
856 }
857 }
858
859 void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp)
860 {
861 void* bits = NULL;
862 GdkPixbuf *pixbuf = GetPixbuf();
863 const bool hasAlpha = HasAlpha();
864
865 // allow access if bpp is valid and matches existence of alpha
866 if ( pixbuf && ((bpp == 24 && !hasAlpha) || (bpp == 32 && hasAlpha)) )
867 {
868 data.m_height = gdk_pixbuf_get_height( pixbuf );
869 data.m_width = gdk_pixbuf_get_width( pixbuf );
870 data.m_stride = gdk_pixbuf_get_rowstride( pixbuf );
871 bits = gdk_pixbuf_get_pixels(pixbuf);
872 }
873 return bits;
874 }
875
876 void wxBitmap::UngetRawData(wxPixelDataBase& WXUNUSED(data))
877 {
878 }
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 }