]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/bitmap.cpp
guarding scrollbar
[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"
559a723c 17 #include "wx/palette.h"
923d28da 18 #include "wx/icon.h"
18680f86 19 #include "wx/math.h"
155ecd4c 20 #include "wx/image.h"
5ff14574 21 #include "wx/colour.h"
93763ad5 22#endif
22bd9387 23
ce7c8a97 24#include "wx/rawbmp.h"
9e691f46 25
f3d74739
VZ
26#include "wx/gtk/private/object.h"
27
d76fe38b 28#include <gtk/gtk.h>
13111b2a 29
d76fe38b
RR
30//-----------------------------------------------------------------------------
31// data
32//-----------------------------------------------------------------------------
33
c2fa61e8 34extern GtkWidget *wxGetRootWindow();
c801d85f
KB
35
36//-----------------------------------------------------------------------------
37// wxMask
38//-----------------------------------------------------------------------------
39
40IMPLEMENT_DYNAMIC_CLASS(wxMask,wxObject)
41
8bbe427f 42wxMask::wxMask()
c801d85f 43{
d3b9f782 44 m_bitmap = NULL;
ff7b1510 45}
c801d85f 46
91b8de8d 47wxMask::wxMask( const wxBitmap& bitmap, const wxColour& colour )
c801d85f 48{
d3b9f782 49 m_bitmap = NULL;
91b8de8d 50 Create( bitmap, colour );
ff7b1510 51}
c801d85f 52
0b04c4e0 53#if wxUSE_PALETTE
91b8de8d 54wxMask::wxMask( const wxBitmap& bitmap, int paletteIndex )
c801d85f 55{
d3b9f782 56 m_bitmap = NULL;
91b8de8d 57 Create( bitmap, paletteIndex );
ff7b1510 58}
0b04c4e0 59#endif // wxUSE_PALETTE
c801d85f 60
91b8de8d 61wxMask::wxMask( const wxBitmap& bitmap )
c801d85f 62{
d3b9f782 63 m_bitmap = NULL;
91b8de8d 64 Create( bitmap );
ff7b1510 65}
c801d85f 66
8bbe427f 67wxMask::~wxMask()
c801d85f 68{
13111b2a 69 if (m_bitmap)
3fe39b0c 70 g_object_unref (m_bitmap);
ff7b1510 71}
c801d85f 72
1fb4de31
RR
73bool wxMask::Create( const wxBitmap& bitmap,
74 const wxColour& colour )
91b8de8d
RR
75{
76 if (m_bitmap)
284b4c88 77 {
3fe39b0c 78 g_object_unref (m_bitmap);
d3b9f782 79 m_bitmap = NULL;
91b8de8d 80 }
13111b2a 81
13785a4b
PC
82 const int w = bitmap.GetWidth();
83 const int h = bitmap.GetHeight();
13111b2a 84
5ac1d44a 85 // create mask as XBM format bitmap
13111b2a 86
5ac1d44a
PC
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];
d0e79755 90 // set bits are unmasked
5ac1d44a
PC
91 memset(out, 0xff, out_size);
92 unsigned bit_index = 0;
13785a4b 93 if (bitmap.HasPixbuf())
1fb4de31 94 {
5ac1d44a
PC
95 const wxByte r_mask = colour.Red();
96 const wxByte g_mask = colour.Green();
97 const wxByte b_mask = colour.Blue();
13785a4b 98 GdkPixbuf* pixbuf = bitmap.GetPixbuf();
5ac1d44a
PC
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 }
2eefae6e 110 }
13785a4b 111 else
1fb4de31 112 {
5ac1d44a 113 GdkImage* image = gdk_drawable_get_image(bitmap.GetPixmap(), 0, 0, w, h);
13785a4b 114 GdkColormap* colormap = gdk_image_get_colormap(image);
a7cfe08a
PC
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
13785a4b
PC
120 {
121 wxColor c(colour);
122 c.CalcPixel(colormap);
123 mask_pixel = c.GetPixel();
124 }
5ac1d44a 125 for (int y = 0; y < h; y++)
1fb4de31 126 {
5ac1d44a
PC
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;
f9ee644e 131 }
13785a4b 132 g_object_unref(image);
5ac1d44a
PC
133 }
134 m_bitmap = gdk_bitmap_create_from_data(wxGetRootWindow()->window, (char*)out, w, h);
135 delete[] out;
902725ee 136 return true;
91b8de8d
RR
137}
138
0b04c4e0 139#if wxUSE_PALETTE
b5f01ae0 140bool wxMask::Create( const wxBitmap& bitmap, int paletteIndex )
91b8de8d 141{
b5f01ae0
VS
142 unsigned char r,g,b;
143 wxPalette *pal = bitmap.GetPalette();
284b4c88 144
902725ee 145 wxCHECK_MSG( pal, false, wxT("Cannot create mask from bitmap without palette") );
c2fa61e8 146
b5f01ae0 147 pal->GetRGB(paletteIndex, &r, &g, &b);
284b4c88 148
b5f01ae0 149 return Create(bitmap, wxColour(r, g, b));
91b8de8d 150}
0b04c4e0 151#endif // wxUSE_PALETTE
91b8de8d
RR
152
153bool wxMask::Create( const wxBitmap& bitmap )
154{
155 if (m_bitmap)
284b4c88 156 {
3fe39b0c 157 g_object_unref (m_bitmap);
d3b9f782 158 m_bitmap = NULL;
91b8de8d 159 }
284b4c88 160
3d37a968 161 if (!bitmap.IsOk()) return false;
284b4c88 162
b85229d1 163 wxCHECK_MSG( bitmap.GetDepth() == 1, false, wxT("Cannot create mask from colour bitmap") );
284b4c88 164
c2fa61e8 165 m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, bitmap.GetWidth(), bitmap.GetHeight(), 1 );
284b4c88 166
902725ee 167 if (!m_bitmap) return false;
284b4c88 168
f3d74739 169 wxGtkObject<GdkGC> gc(gdk_gc_new( m_bitmap ));
9be3f755
PC
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());
284b4c88 172
902725ee 173 return true;
91b8de8d
RR
174}
175
176GdkBitmap *wxMask::GetBitmap() const
c801d85f 177{
fd0eed64 178 return m_bitmap;
ff7b1510 179}
8bbe427f 180
c801d85f 181//-----------------------------------------------------------------------------
732d8c74 182// wxBitmapRefData
c801d85f
KB
183//-----------------------------------------------------------------------------
184
8f884a0d 185class wxBitmapRefData: public wxGDIRefData
c801d85f 186{
fd0eed64 187public:
f2593d0d 188 wxBitmapRefData();
d3c7fc99 189 virtual ~wxBitmapRefData();
f2593d0d 190
8f884a0d
VZ
191 virtual bool IsOk() const { return m_pixmap || m_pixbuf; }
192
f2593d0d 193 GdkPixmap *m_pixmap;
feac7937 194 GdkPixbuf *m_pixbuf;
f2593d0d
RR
195 wxMask *m_mask;
196 int m_width;
197 int m_height;
198 int m_bpp;
23656673 199#if wxUSE_PALETTE
f2593d0d 200 wxPalette *m_palette;
23656673 201#endif // wxUSE_PALETTE
c801d85f
KB
202};
203
8bbe427f 204wxBitmapRefData::wxBitmapRefData()
c801d85f 205{
d3b9f782
VZ
206 m_pixmap = NULL;
207 m_pixbuf = NULL;
208 m_mask = NULL;
fd0eed64
RR
209 m_width = 0;
210 m_height = 0;
211 m_bpp = 0;
23656673 212#if wxUSE_PALETTE
d3b9f782 213 m_palette = NULL;
23656673 214#endif // wxUSE_PALETTE
ff7b1510 215}
c801d85f 216
8bbe427f 217wxBitmapRefData::~wxBitmapRefData()
c801d85f 218{
3ebcd89d 219 if (m_pixmap)
3fe39b0c 220 g_object_unref (m_pixmap);
feac7937 221 if (m_pixbuf)
3fe39b0c 222 g_object_unref (m_pixbuf);
3ebcd89d 223 delete m_mask;
0b04c4e0 224#if wxUSE_PALETTE
3ebcd89d 225 delete m_palette;
0b04c4e0 226#endif // wxUSE_PALETTE
ff7b1510 227}
c801d85f 228
732d8c74
FM
229
230//-----------------------------------------------------------------------------
231// wxBitmap
c801d85f
KB
232//-----------------------------------------------------------------------------
233
5c33522f 234#define M_BMPDATA static_cast<wxBitmapRefData*>(m_refData)
c801d85f
KB
235
236IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxGDIObject)
237
23656673
PC
238wxBitmap::wxBitmap(const wxString &filename, wxBitmapType type)
239{
240 LoadFile(filename, type);
241}
242
243wxBitmap::wxBitmap(const char bits[], int width, int height, int depth)
c801d85f 244{
23656673
PC
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));
23656673 249 }
ff7b1510 250}
8bbe427f 251
452418c4
PC
252wxBitmap::wxBitmap(const char* const* bits)
253{
254 wxCHECK2_MSG(bits != NULL, return, wxT("invalid bitmap data"));
255
256 GdkBitmap* mask = NULL;
5c33522f 257 SetPixmap(gdk_pixmap_create_from_xpm_d(wxGetRootWindow()->window, &mask, NULL, const_cast<char**>(bits)));
3d37a968
FM
258 if (!M_BMPDATA)
259 return;
452418c4
PC
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
23656673 268wxBitmap::~wxBitmap()
c801d85f 269{
c826213d
RR
270}
271
272bool wxBitmap::Create( int width, int height, int depth )
273{
274 UnRef();
275
3ebcd89d
VZ
276 if ( width <= 0 || height <= 0 )
277 {
278 return false;
279 }
284b4c88 280
4b230a33 281 const GdkVisual* visual = wxTheApp->GetGdkVisual();
3d37a968 282
b85229d1 283 if (depth == 32)
284f2b59 284 {
23656673 285 SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB, true, 8, width, height), 32);
3d37a968
FM
286
287 if (!M_BMPDATA)
288 return false;
289
150a349a
PC
290 // must initialize alpha, otherwise GetPixmap()
291 // will create a mask out of garbage
292 gdk_pixbuf_fill(M_BMPDATA->m_pixbuf, 0x000000ff);
3d37a968
FM
293 }
294 else if (depth == 24)
4b230a33
RR
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);
284f2b59 300 }
eefa26be
RR
301 else
302 {
5588ce92
PC
303 if (depth != 1)
304 {
5588ce92
PC
305 if (depth == -1)
306 depth = visual->depth;
5588ce92 307 }
5588ce92 308 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window, width, height, depth));
eefa26be 309 }
8bbe427f 310
3d37a968 311 return IsOk();
ff7b1510 312}
b5f01ae0 313
c0521644
VZ
314#if wxUSE_IMAGE
315
feac7937 316bool wxBitmap::CreateFromImage(const wxImage& image, int depth)
b5f01ae0 317{
a11672a4
RR
318 UnRef();
319
3d37a968 320 wxCHECK_MSG( image.IsOk(), false, wxT("invalid image") );
637b7e4f 321 wxCHECK_MSG( depth == -1 || depth == 1, false, wxT("invalid bitmap depth") );
b5f01ae0 322
feac7937
VS
323 if (image.GetWidth() <= 0 || image.GetHeight() <= 0)
324 return false;
902725ee 325
70029506
PC
326 // create pixbuf if image has alpha and requested depth is compatible
327 if (image.HasAlpha() && (depth == -1 || depth == 32))
5588ce92
PC
328 return CreateFromImageAsPixbuf(image);
329
70029506 330 // otherwise create pixmap, if alpha is present it will be converted to mask
5ac1d44a 331 return CreateFromImageAsPixmap(image, depth);
feac7937 332}
3ebcd89d 333
5ac1d44a 334bool wxBitmap::CreateFromImageAsPixmap(const wxImage& image, int depth)
feac7937 335{
5ac1d44a
PC
336 const int w = image.GetWidth();
337 const int h = image.GetHeight();
338 if (depth == 1)
feac7937 339 {
5ac1d44a
PC
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];
d0e79755 345 // set bits are black
5ac1d44a
PC
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;
3d37a968
FM
359
360 if (!M_BMPDATA) // SetPixmap may have failed
361 return false;
5ac1d44a
PC
362 }
363 else
364 {
365 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window, w, h, depth));
3d37a968
FM
366 if (!M_BMPDATA)
367 return false;
368
f3d74739 369 wxGtkObject<GdkGC> gc(gdk_gc_new(M_BMPDATA->m_pixmap));
5ac1d44a
PC
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);
feac7937 374 }
b5f01ae0 375
5ac1d44a
PC
376 const wxByte* alpha = image.GetAlpha();
377 if (alpha != NULL || image.HasMask())
feac7937 378 {
5ac1d44a
PC
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)
b5f01ae0 386 {
5ac1d44a 387 for (int y = 0; y < h; y++)
b5f01ae0 388 {
5ac1d44a
PC
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;
feac7937 393 }
5ac1d44a
PC
394 }
395 else
b5f01ae0 396 {
5ac1d44a
PC
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 }
3d37a968 414 return IsOk();
b5f01ae0
VS
415}
416
feac7937 417bool wxBitmap::CreateFromImageAsPixbuf(const wxImage& image)
b5f01ae0 418{
70029506
PC
419 wxASSERT(image.HasAlpha());
420
feac7937
VS
421 int width = image.GetWidth();
422 int height = image.GetHeight();
2eefae6e 423
23656673
PC
424 Create(width, height, 32);
425 GdkPixbuf* pixbuf = M_BMPDATA->m_pixbuf;
feac7937
VS
426 if (!pixbuf)
427 return false;
c2fa61e8 428
feac7937 429 // Copy the data:
23656673 430 const unsigned char* in = image.GetData();
feac7937
VS
431 unsigned char *out = gdk_pixbuf_get_pixels(pixbuf);
432 unsigned char *alpha = image.GetAlpha();
902725ee 433
23656673 434 int rowpad = gdk_pixbuf_get_rowstride(pixbuf) - 4 * width;
b5f01ae0 435
23656673 436 for (int y = 0; y < height; y++, out += rowpad)
b5f01ae0 437 {
feac7937
VS
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 }
b5f01ae0 445 }
902725ee 446
feac7937
VS
447 return true;
448}
b5f01ae0 449
feac7937
VS
450wxImage wxBitmap::ConvertToImage() const
451{
3d37a968 452 wxCHECK_MSG( IsOk(), wxNullImage, wxT("invalid bitmap") );
902725ee 453
afbfbfdf
PC
454 const int w = GetWidth();
455 const int h = GetHeight();
23656673 456 wxImage image(w, h, false);
feac7937 457 unsigned char *data = image.GetData();
2eefae6e 458
5588ce92 459 wxCHECK_MSG(data != NULL, wxNullImage, wxT("couldn't create image") );
b5f01ae0 460
70029506 461 // prefer pixbuf if available, it will preserve alpha and should be quicker
feac7937 462 if (HasPixbuf())
b5f01ae0 463 {
feac7937 464 GdkPixbuf *pixbuf = GetPixbuf();
70029506
PC
465 unsigned char* alpha = NULL;
466 if (gdk_pixbuf_get_has_alpha(pixbuf))
467 {
468 image.SetAlpha();
469 alpha = image.GetAlpha();
470 }
23656673 471 const unsigned char* in = gdk_pixbuf_get_pixels(pixbuf);
feac7937 472 unsigned char *out = data;
70029506 473 const int inc = 3 + int(alpha != NULL);
23656673 474 const int rowpad = gdk_pixbuf_get_rowstride(pixbuf) - inc * w;
b5f01ae0 475
23656673 476 for (int y = 0; y < h; y++, in += rowpad)
feac7937 477 {
70029506 478 for (int x = 0; x < w; x++, in += inc, out += 3)
feac7937
VS
479 {
480 out[0] = in[0];
481 out[1] = in[1];
482 out[2] = in[2];
70029506
PC
483 if (alpha != NULL)
484 *alpha++ = in[3];
feac7937
VS
485 }
486 }
b5f01ae0 487 }
feac7937 488 else
b5f01ae0 489 {
afbfbfdf
PC
490 GdkPixmap* pixmap = GetPixmap();
491 GdkPixmap* pixmap_invert = NULL;
492 if (GetDepth() == 1)
b5f01ae0 493 {
d0e79755 494 // mono bitmaps are inverted, i.e. 0 is white
afbfbfdf 495 pixmap_invert = gdk_pixmap_new(pixmap, w, h, 1);
f3d74739 496 wxGtkObject<GdkGC> gc(gdk_gc_new(pixmap_invert));
afbfbfdf
PC
497 gdk_gc_set_function(gc, GDK_COPY_INVERT);
498 gdk_draw_drawable(pixmap_invert, gc, pixmap, 0, 0, 0, 0, w, h);
afbfbfdf 499 pixmap = pixmap_invert;
feac7937 500 }
afbfbfdf
PC
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);
feac7937 504
afbfbfdf 505 gdk_pixbuf_get_from_drawable(pixbuf, pixmap, NULL, 0, 0, 0, 0, w, h);
feac7937 506
afbfbfdf
PC
507 g_object_unref(pixbuf);
508 if (pixmap_invert != NULL)
509 g_object_unref(pixmap_invert);
70029506
PC
510 }
511 // convert mask, unless there is already alpha
512 if (GetMask() && !image.HasAlpha())
513 {
23656673
PC
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
70029506
PC
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;
feac7937 522
70029506
PC
523 image.SetMaskColour(MASK_RED, MASK_GREEN, MASK_BLUE);
524 GdkImage* image_mask = gdk_drawable_get_image(GetMask()->GetBitmap(), 0, 0, w, h);
feac7937 525
70029506
PC
526 for (int y = 0; y < h; y++)
527 {
528 for (int x = 0; x < w; x++, data += 3)
8ab696e0 529 {
70029506 530 if (gdk_image_get_pixel(image_mask, x, y) == 0)
8ab696e0 531 {
70029506
PC
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 {
23656673
PC
538 // we have to fudge the colour a bit to prevent
539 // this pixel from appearing transparent
70029506 540 data[2] = MASK_BLUE_REPLACEMENT;
feac7937 541 }
feac7937 542 }
b5f01ae0 543 }
70029506 544 g_object_unref(image_mask);
feac7937 545 }
b5f01ae0
VS
546
547 return image;
548}
549
c0521644
VZ
550#endif // wxUSE_IMAGE
551
91b8de8d 552int wxBitmap::GetHeight() const
c801d85f 553{
3d37a968 554 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
e55ad60e 555
fd0eed64 556 return M_BMPDATA->m_height;
ff7b1510 557}
c801d85f 558
91b8de8d 559int wxBitmap::GetWidth() const
c801d85f 560{
3d37a968 561 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
8bbe427f 562
fd0eed64 563 return M_BMPDATA->m_width;
ff7b1510 564}
c801d85f 565
91b8de8d 566int wxBitmap::GetDepth() const
c801d85f 567{
3d37a968 568 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
8bbe427f 569
fd0eed64 570 return M_BMPDATA->m_bpp;
ff7b1510 571}
c801d85f 572
91b8de8d 573wxMask *wxBitmap::GetMask() const
c801d85f 574{
d3b9f782 575 wxCHECK_MSG( IsOk(), NULL, wxT("invalid bitmap") );
8bbe427f 576
fd0eed64 577 return M_BMPDATA->m_mask;
ff7b1510 578}
c801d85f
KB
579
580void wxBitmap::SetMask( wxMask *mask )
581{
3d37a968 582 wxCHECK_RET( IsOk(), wxT("invalid bitmap") );
8bbe427f 583
e3e89a93 584 AllocExclusive();
23656673 585 delete M_BMPDATA->m_mask;
fd0eed64 586 M_BMPDATA->m_mask = mask;
ff7b1510 587}
c801d85f 588
db0aec83
VS
589bool wxBitmap::CopyFromIcon(const wxIcon& icon)
590{
591 *this = icon;
3d37a968 592 return IsOk();
db0aec83
VS
593}
594
17bec151
RR
595wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const
596{
5588ce92
PC
597 wxBitmap ret;
598
3d37a968 599 wxCHECK_MSG(IsOk(), ret, wxT("invalid bitmap"));
23656673
PC
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"));
13111b2a 604
23656673 605 if (HasPixbuf() || M_BMPDATA->m_bpp == 32)
17bec151 606 {
feac7937 607 GdkPixbuf *pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB,
70029506 608 gdk_pixbuf_get_has_alpha(GetPixbuf()),
17f504ec 609 8, rect.width, rect.height);
3d37a968
FM
610 if (!pixbuf)
611 return ret;
612
23656673 613 ret.SetPixbuf(pixbuf, M_BMPDATA->m_bpp);
6db34764 614 gdk_pixbuf_copy_area(GetPixbuf(),
feac7937
VS
615 rect.x, rect.y, rect.width, rect.height,
616 pixbuf, 0, 0);
17bec151
RR
617 }
618 else
619 {
23656673 620 ret.Create(rect.width, rect.height, M_BMPDATA->m_bpp);
f3d74739 621 wxGtkObject<GdkGC> gc(gdk_gc_new( ret.GetPixmap() ));
9be3f755 622 gdk_draw_drawable( ret.GetPixmap(), gc, GetPixmap(), rect.x, rect.y, 0, 0, rect.width, rect.height );
17bec151 623 }
70029506
PC
624 // make mask, unless there is already alpha
625 if (GetMask() && !HasAlpha())
17bec151
RR
626 {
627 wxMask *mask = new wxMask;
c2fa61e8 628 mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, rect.width, rect.height, 1 );
13111b2a 629
f3d74739 630 wxGtkObject<GdkGC> gc(gdk_gc_new( mask->m_bitmap ));
9be3f755 631 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
632
633 ret.SetMask( mask );
17bec151 634 }
13111b2a 635
17bec151
RR
636 return ret;
637}
638
4611dd06 639bool wxBitmap::SaveFile( const wxString &name, wxBitmapType type, const wxPalette *WXUNUSED(palette) ) const
c801d85f 640{
3d37a968 641 wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") );
8bbe427f 642
c0521644 643#if wxUSE_IMAGE
b75dd496 644 // Try to save the bitmap via wxImage handlers:
5588ce92
PC
645 wxImage image = ConvertToImage();
646 return image.Ok() && image.SaveFile(name, type);
c0521644 647#else // !wxUSE_IMAGE
8d22935d
VZ
648 wxUnusedVar(name);
649 wxUnusedVar(type);
650
c0521644
VZ
651 return false;
652#endif // wxUSE_IMAGE
ff7b1510 653}
c801d85f 654
4611dd06 655bool wxBitmap::LoadFile( const wxString &name, wxBitmapType type )
c801d85f 656{
fd0eed64 657 UnRef();
8bbe427f 658
fd0eed64
RR
659 if (type == wxBITMAP_TYPE_XPM)
660 {
d3b9f782 661 GdkBitmap *mask = NULL;
5588ce92 662 SetPixmap(gdk_pixmap_create_from_xpm(wxGetRootWindow()->window, &mask, NULL, name.fn_str()));
3d37a968
FM
663 if (!M_BMPDATA)
664 return false; // do not set the mask
8bbe427f 665
fd0eed64
RR
666 if (mask)
667 {
5588ce92
PC
668 M_BMPDATA->m_mask = new wxMask;
669 M_BMPDATA->m_mask->m_bitmap = mask;
fd0eed64 670 }
fd0eed64 671 }
c0521644 672#if wxUSE_IMAGE
b75dd496 673 else // try if wxImage can load it
fd0eed64
RR
674 {
675 wxImage image;
5588ce92 676 if (image.LoadFile(name, type) && image.Ok())
23656673 677 CreateFromImage(image, -1);
fd0eed64 678 }
c0521644 679#endif // wxUSE_IMAGE
8bbe427f 680
3d37a968 681 return IsOk();
ff7b1510 682}
8bbe427f 683
0b04c4e0 684#if wxUSE_PALETTE
91b8de8d 685wxPalette *wxBitmap::GetPalette() const
c801d85f 686{
3d37a968 687 wxCHECK_MSG(IsOk(), NULL, wxT("invalid bitmap"));
8bbe427f 688
fd0eed64 689 return M_BMPDATA->m_palette;
ff7b1510 690}
c801d85f 691
4611dd06
RR
692void wxBitmap::SetPalette(const wxPalette& WXUNUSED(palette))
693{
694 // TODO
695}
0b04c4e0 696#endif // wxUSE_PALETTE
4611dd06 697
4bc67cc5
RR
698void wxBitmap::SetHeight( int height )
699{
e3e89a93 700 AllocExclusive();
4bc67cc5
RR
701 M_BMPDATA->m_height = height;
702}
703
704void wxBitmap::SetWidth( int width )
705{
e3e89a93 706 AllocExclusive();
4bc67cc5
RR
707 M_BMPDATA->m_width = width;
708}
709
710void wxBitmap::SetDepth( int depth )
711{
e3e89a93 712 AllocExclusive();
4bc67cc5
RR
713 M_BMPDATA->m_bpp = depth;
714}
715
716void wxBitmap::SetPixmap( GdkPixmap *pixmap )
717{
3d37a968
FM
718 if (!pixmap)
719 return;
720
3ebcd89d 721 if (!m_refData)
5588ce92 722 m_refData = new wxBitmapRefData;
4bc67cc5 723
e3e89a93
PC
724 // AllocExclusive should not be needed for this internal function
725 wxASSERT(m_refData->GetRefCount() == 1);
5588ce92 726 wxASSERT(M_BMPDATA->m_pixmap == NULL);
4bc67cc5 727 M_BMPDATA->m_pixmap = pixmap;
5588ce92
PC
728 gdk_drawable_get_size(pixmap, &M_BMPDATA->m_width, &M_BMPDATA->m_height);
729 M_BMPDATA->m_bpp = gdk_drawable_get_depth(pixmap);
6db34764 730 PurgeOtherRepresentations(Pixmap);
4bc67cc5
RR
731}
732
91b8de8d 733GdkPixmap *wxBitmap::GetPixmap() const
c801d85f 734{
d3b9f782 735 wxCHECK_MSG( IsOk(), NULL, wxT("invalid bitmap") );
8bbe427f 736
feac7937 737 // create the pixmap on the fly if we use Pixbuf representation:
5588ce92 738 if (M_BMPDATA->m_pixmap == NULL)
feac7937 739 {
70029506
PC
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 }
feac7937
VS
748 gdk_pixbuf_render_pixmap_and_mask(M_BMPDATA->m_pixbuf,
749 &M_BMPDATA->m_pixmap,
70029506 750 pmask,
c0521644 751 0x80 /* alpha threshold */);
feac7937 752 }
feac7937 753
fd0eed64 754 return M_BMPDATA->m_pixmap;
ff7b1510 755}
8bbe427f 756
feac7937
VS
757bool wxBitmap::HasPixmap() const
758{
3d37a968 759 wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") );
feac7937
VS
760
761 return M_BMPDATA->m_pixmap != NULL;
762}
763
feac7937
VS
764GdkPixbuf *wxBitmap::GetPixbuf() const
765{
3d37a968 766 wxCHECK_MSG( IsOk(), NULL, wxT("invalid bitmap") );
feac7937 767
5588ce92 768 if (M_BMPDATA->m_pixbuf == NULL)
6db34764 769 {
3d37a968 770
6db34764
VS
771 int width = GetWidth();
772 int height = GetHeight();
902725ee 773
6db34764 774 GdkPixbuf *pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB,
70029506 775 GetMask() != NULL,
6db34764 776 8, width, height);
23656673
PC
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);
6db34764
VS
780 // apply the mask to created pixbuf:
781 if (M_BMPDATA->m_pixbuf && M_BMPDATA->m_mask)
782 {
902725ee 783 GdkPixbuf *pmask =
6db34764
VS
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 }
902725ee 804
3fe39b0c 805 g_object_unref (pmask);
6db34764
VS
806 }
807 }
808 }
809
feac7937
VS
810 return M_BMPDATA->m_pixbuf;
811}
812
813bool wxBitmap::HasPixbuf() const
814{
3d37a968 815 wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") );
feac7937
VS
816
817 return M_BMPDATA->m_pixbuf != NULL;
818}
819
23656673 820void wxBitmap::SetPixbuf(GdkPixbuf* pixbuf, int depth)
feac7937 821{
3d37a968
FM
822 if (!pixbuf)
823 return;
824
feac7937 825 if (!m_refData)
5588ce92 826 m_refData = new wxBitmapRefData;
feac7937 827
e3e89a93
PC
828 // AllocExclusive should not be needed for this internal function
829 wxASSERT(m_refData->GetRefCount() == 1);
5588ce92 830 wxASSERT(M_BMPDATA->m_pixbuf == NULL);
feac7937 831 M_BMPDATA->m_pixbuf = pixbuf;
5588ce92
PC
832 M_BMPDATA->m_width = gdk_pixbuf_get_width(pixbuf);
833 M_BMPDATA->m_height = gdk_pixbuf_get_height(pixbuf);
23656673
PC
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;
6db34764 840 PurgeOtherRepresentations(Pixbuf);
feac7937
VS
841}
842
843void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep)
844{
845 if (keep == Pixmap && HasPixbuf())
846 {
3fe39b0c 847 g_object_unref (M_BMPDATA->m_pixbuf);
feac7937
VS
848 M_BMPDATA->m_pixbuf = NULL;
849 }
850 if (keep == Pixbuf && HasPixmap())
851 {
3fe39b0c 852 g_object_unref (M_BMPDATA->m_pixmap);
feac7937
VS
853 M_BMPDATA->m_pixmap = NULL;
854 }
855}
856
ce7c8a97 857#ifdef wxHAS_RAW_BITMAP
284f2b59
RR
858void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp)
859{
70029506 860 void* bits = NULL;
284f2b59 861 GdkPixbuf *pixbuf = GetPixbuf();
70029506 862 const bool hasAlpha = HasAlpha();
3d37a968 863
70029506 864 // allow access if bpp is valid and matches existence of alpha
c74fc1e1 865 if ( pixbuf && ((bpp == 24 && !hasAlpha) || (bpp == 32 && hasAlpha)) )
70029506
PC
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;
284f2b59
RR
873}
874
17a1ebd1 875void wxBitmap::UngetRawData(wxPixelDataBase& WXUNUSED(data))
284f2b59
RR
876{
877}
ce7c8a97 878#endif // wxHAS_RAW_BITMAP
284f2b59 879
902725ee 880bool wxBitmap::HasAlpha() const
0ff2a74d 881{
70029506
PC
882 return m_refData != NULL && M_BMPDATA->m_pixbuf != NULL &&
883 gdk_pixbuf_get_has_alpha(M_BMPDATA->m_pixbuf);
0ff2a74d
RR
884}
885
8f884a0d 886wxGDIRefData* wxBitmap::CreateGDIRefData() const
e3e89a93
PC
887{
888 return new wxBitmapRefData;
889}
890
8f884a0d 891wxGDIRefData* wxBitmap::CloneGDIRefData(const wxGDIRefData* data) const
e3e89a93 892{
5c33522f 893 const wxBitmapRefData* oldRef = static_cast<const wxBitmapRefData*>(data);
e3e89a93
PC
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));
f3d74739 904 wxGtkObject<GdkGC> gc(gdk_gc_new(newRef->m_pixmap));
e3e89a93
PC
905 gdk_draw_drawable(
906 newRef->m_pixmap, gc, oldRef->m_pixmap, 0, 0, 0, 0, -1, -1);
e3e89a93
PC
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);
f3d74739 917 wxGtkObject<GdkGC> gc(gdk_gc_new(newRef->m_mask->m_bitmap));
e3e89a93
PC
918 gdk_draw_drawable(newRef->m_mask->m_bitmap,
919 gc, oldRef->m_mask->m_bitmap, 0, 0, 0, 0, -1, -1);
e3e89a93
PC
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
4b61c88d
RR
929/* static */ void wxBitmap::InitStandardHandlers()
930{
931 // TODO: Insert handler based on GdkPixbufs handler later
932}