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