]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/bitmap.cpp
don't save the initial selection if it's invalid in CBN_DROPDOWN (refixes #8474 witho...
[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
5696ebb3 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{
fd0eed64 44 m_bitmap = (GdkBitmap *) NULL;
ff7b1510 45}
c801d85f 46
91b8de8d 47wxMask::wxMask( const wxBitmap& bitmap, const wxColour& colour )
c801d85f 48{
72a7edf0 49 m_bitmap = (GdkBitmap *) 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{
72a7edf0 56 m_bitmap = (GdkBitmap *) NULL;
91b8de8d 57 Create( bitmap, paletteIndex );
ff7b1510 58}
0b04c4e0 59#endif // wxUSE_PALETTE
c801d85f 60
91b8de8d 61wxMask::wxMask( const wxBitmap& bitmap )
c801d85f 62{
72a7edf0 63 m_bitmap = (GdkBitmap *) 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);
284b4c88 79 m_bitmap = (GdkBitmap*) 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);
284b4c88 158 m_bitmap = (GdkBitmap*) NULL;
91b8de8d 159 }
284b4c88 160
902725ee 161 if (!bitmap.Ok()) 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
KB
181//-----------------------------------------------------------------------------
182// wxBitmap
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{
fd0eed64 206 m_pixmap = (GdkPixmap *) NULL;
feac7937 207 m_pixbuf = (GdkPixbuf *) NULL;
fd0eed64
RR
208 m_mask = (wxMask *) NULL;
209 m_width = 0;
210 m_height = 0;
211 m_bpp = 0;
23656673 212#if wxUSE_PALETTE
fd0eed64 213 m_palette = (wxPalette *) 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
KB
228
229//-----------------------------------------------------------------------------
230
b85229d1 231#define M_BMPDATA wx_static_cast(wxBitmapRefData*, m_refData)
c801d85f
KB
232
233IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxGDIObject)
234
23656673
PC
235wxBitmap::wxBitmap(int width, int height, int depth)
236{
237 Create(width, height, depth);
238}
239
240wxBitmap::wxBitmap(const wxString &filename, wxBitmapType type)
241{
242 LoadFile(filename, type);
243}
244
245wxBitmap::wxBitmap(const char bits[], int width, int height, int depth)
c801d85f 246{
23656673
PC
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 wxASSERT_MSG( M_BMPDATA->m_pixmap, wxT("couldn't create bitmap") );
253 }
ff7b1510 254}
8bbe427f 255
452418c4
PC
256wxBitmap::wxBitmap(const char* const* bits)
257{
258 wxCHECK2_MSG(bits != NULL, return, wxT("invalid bitmap data"));
259
260 GdkBitmap* mask = NULL;
261 SetPixmap(gdk_pixmap_create_from_xpm_d(wxGetRootWindow()->window, &mask, NULL, wx_const_cast(char**, bits)));
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
23656673 270wxBitmap::~wxBitmap()
c801d85f 271{
c826213d
RR
272}
273
274bool wxBitmap::Create( int width, int height, int depth )
275{
276 UnRef();
277
3ebcd89d
VZ
278 if ( width <= 0 || height <= 0 )
279 {
280 return false;
281 }
284b4c88 282
4b230a33
RR
283 const GdkVisual* visual = wxTheApp->GetGdkVisual();
284
b85229d1 285 if (depth == 32)
284f2b59 286 {
23656673 287 SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB, true, 8, width, height), 32);
4b230a33
RR
288 } else
289 if (depth == 24)
290 {
291 if (visual->depth == depth)
292 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window, width, height, depth));
293 else
294 SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB, false, 8, width, height), 24);
284f2b59 295 }
eefa26be
RR
296 else
297 {
5588ce92
PC
298 if (depth != 1)
299 {
5588ce92
PC
300 if (depth == -1)
301 depth = visual->depth;
5588ce92 302 }
5588ce92 303 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window, width, height, depth));
eefa26be 304 }
8bbe427f 305
c826213d 306 return Ok();
ff7b1510 307}
b5f01ae0 308
98d8a7ec
VZ
309wxBitmap wxBitmap::Rescale(int clipx, int clipy, int clipwidth, int clipheight,
310 int width, int height) const
783da845 311{
5588ce92
PC
312 wxBitmap bmp;
313
314 wxCHECK_MSG(Ok(), bmp, wxT("invalid bitmap"));
783da845 315
98d8a7ec
VZ
316 width = wxMax(width, 1);
317 height = wxMax(height, 1);
902725ee 318
98d8a7ec
VZ
319 const double scale_x = double(width) / clipwidth;
320 const double scale_y = double(height) / clipheight;
533b005a
PC
321
322 // Converting to pixbuf, scaling with gdk_pixbuf_scale, and converting
323 // back, is faster than scaling pixmap ourselves.
324
325 // use pixbuf if already available,
326 // otherwise create temporary pixbuf from pixmap
327 GdkPixbuf* pixbuf = M_BMPDATA->m_pixbuf;
328 if (pixbuf)
329 g_object_ref(pixbuf);
feac7937 330 else
533b005a
PC
331 pixbuf = gdk_pixbuf_get_from_drawable(
332 NULL, M_BMPDATA->m_pixmap, NULL,
333 0, 0, 0, 0, M_BMPDATA->m_width, M_BMPDATA->m_height);
feac7937 334
533b005a
PC
335 // new pixbuf for scaled wxBitmap
336 GdkPixbuf* pixbuf_scaled = gdk_pixbuf_new(
337 GDK_COLORSPACE_RGB, gdk_pixbuf_get_has_alpha(pixbuf), 8, width, height);
902725ee 338
533b005a
PC
339 // GDK_INTERP_NEAREST is the lowest-quality method for continuous-tone
340 // images, but the only one which preserves sharp edges
341 gdk_pixbuf_scale(
342 pixbuf, pixbuf_scaled,
98d8a7ec 343 0, 0, width, height, -clipx*scale_x, -clipy*scale_y, scale_x, scale_y,
533b005a 344 GDK_INTERP_NEAREST);
5588ce92 345
533b005a
PC
346 g_object_unref(pixbuf);
347 bmp.SetPixbuf(pixbuf_scaled, M_BMPDATA->m_bpp);
902725ee 348
533b005a
PC
349 if (M_BMPDATA->m_mask)
350 {
351 pixbuf = gdk_pixbuf_get_from_drawable(
352 NULL, M_BMPDATA->m_mask->m_bitmap, NULL,
353 0, 0, 0, 0, M_BMPDATA->m_width, M_BMPDATA->m_height);
902725ee 354
533b005a
PC
355 pixbuf_scaled = gdk_pixbuf_new(
356 GDK_COLORSPACE_RGB, false, 8, width, height);
feac7937 357
533b005a
PC
358 gdk_pixbuf_scale(
359 pixbuf, pixbuf_scaled,
35d2f1b8 360 0, 0, width, height, -clipx, -clipy, scale_x, scale_y,
533b005a 361 GDK_INTERP_NEAREST);
902725ee 362
533b005a 363 g_object_unref(pixbuf);
feac7937 364
533b005a
PC
365 // use existing functionality to create mask from scaled pixbuf
366 wxBitmap maskbmp;
367 maskbmp.SetPixbuf(pixbuf_scaled);
368 bmp.SetMask(new wxMask(maskbmp, *wxBLACK));
feac7937 369 }
902725ee 370 return bmp;
783da845
RR
371}
372
c0521644
VZ
373#if wxUSE_IMAGE
374
feac7937 375bool wxBitmap::CreateFromImage(const wxImage& image, int depth)
b5f01ae0 376{
a11672a4
RR
377 UnRef();
378
637b7e4f
VZ
379 wxCHECK_MSG( image.Ok(), false, wxT("invalid image") );
380 wxCHECK_MSG( depth == -1 || depth == 1, false, wxT("invalid bitmap depth") );
b5f01ae0 381
feac7937
VS
382 if (image.GetWidth() <= 0 || image.GetHeight() <= 0)
383 return false;
902725ee 384
70029506
PC
385 // create pixbuf if image has alpha and requested depth is compatible
386 if (image.HasAlpha() && (depth == -1 || depth == 32))
5588ce92
PC
387 return CreateFromImageAsPixbuf(image);
388
70029506 389 // otherwise create pixmap, if alpha is present it will be converted to mask
5ac1d44a 390 return CreateFromImageAsPixmap(image, depth);
feac7937 391}
3ebcd89d 392
5ac1d44a 393bool wxBitmap::CreateFromImageAsPixmap(const wxImage& image, int depth)
feac7937 394{
5ac1d44a
PC
395 const int w = image.GetWidth();
396 const int h = image.GetHeight();
397 if (depth == 1)
feac7937 398 {
5ac1d44a
PC
399 // create XBM format bitmap
400
401 // one bit per pixel, each row starts on a byte boundary
402 const size_t out_size = size_t((w + 7) / 8) * unsigned(h);
403 wxByte* out = new wxByte[out_size];
d0e79755 404 // set bits are black
5ac1d44a
PC
405 memset(out, 0xff, out_size);
406 const wxByte* in = image.GetData();
407 unsigned bit_index = 0;
408 for (int y = 0; y < h; y++)
409 {
410 for (int x = 0; x < w; x++, in += 3, bit_index++)
411 if (in[0] == 255 && in[1] == 255 && in[2] == 255)
412 out[bit_index >> 3] ^= 1 << (bit_index & 7);
413 // move index to next byte boundary
414 bit_index = (bit_index + 7) & ~7u;
415 }
416 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window, (char*)out, w, h));
417 delete[] out;
418 }
419 else
420 {
421 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window, w, h, depth));
f3d74739 422 wxGtkObject<GdkGC> gc(gdk_gc_new(M_BMPDATA->m_pixmap));
5ac1d44a
PC
423 gdk_draw_rgb_image(
424 M_BMPDATA->m_pixmap, gc,
425 0, 0, w, h,
426 GDK_RGB_DITHER_NONE, image.GetData(), w * 3);
feac7937 427 }
b5f01ae0 428
5ac1d44a
PC
429 const wxByte* alpha = image.GetAlpha();
430 if (alpha != NULL || image.HasMask())
feac7937 431 {
5ac1d44a
PC
432 // create mask as XBM format bitmap
433
434 const size_t out_size = size_t((w + 7) / 8) * unsigned(h);
435 wxByte* out = new wxByte[out_size];
436 memset(out, 0xff, out_size);
437 unsigned bit_index = 0;
438 if (alpha != NULL)
b5f01ae0 439 {
5ac1d44a 440 for (int y = 0; y < h; y++)
b5f01ae0 441 {
5ac1d44a
PC
442 for (int x = 0; x < w; x++, bit_index++)
443 if (*alpha++ < wxIMAGE_ALPHA_THRESHOLD)
444 out[bit_index >> 3] ^= 1 << (bit_index & 7);
445 bit_index = (bit_index + 7) & ~7u;
feac7937 446 }
5ac1d44a
PC
447 }
448 else
b5f01ae0 449 {
5ac1d44a
PC
450 const wxByte r_mask = image.GetMaskRed();
451 const wxByte g_mask = image.GetMaskGreen();
452 const wxByte b_mask = image.GetMaskBlue();
453 const wxByte* in = image.GetData();
454 for (int y = 0; y < h; y++)
455 {
456 for (int x = 0; x < w; x++, in += 3, bit_index++)
457 if (in[0] == r_mask && in[1] == g_mask && in[2] == b_mask)
458 out[bit_index >> 3] ^= 1 << (bit_index & 7);
459 bit_index = (bit_index + 7) & ~7u;
460 }
461 }
462 wxMask* mask = new wxMask;
463 mask->m_bitmap = gdk_bitmap_create_from_data(M_BMPDATA->m_pixmap, (char*)out, w, h);
464 SetMask(mask);
465 delete[] out;
466 }
feac7937 467 return true;
b5f01ae0
VS
468}
469
feac7937 470bool wxBitmap::CreateFromImageAsPixbuf(const wxImage& image)
b5f01ae0 471{
70029506
PC
472 wxASSERT(image.HasAlpha());
473
feac7937
VS
474 int width = image.GetWidth();
475 int height = image.GetHeight();
2eefae6e 476
23656673
PC
477 Create(width, height, 32);
478 GdkPixbuf* pixbuf = M_BMPDATA->m_pixbuf;
feac7937
VS
479 if (!pixbuf)
480 return false;
c2fa61e8 481
feac7937 482 // Copy the data:
23656673 483 const unsigned char* in = image.GetData();
feac7937
VS
484 unsigned char *out = gdk_pixbuf_get_pixels(pixbuf);
485 unsigned char *alpha = image.GetAlpha();
902725ee 486
23656673 487 int rowpad = gdk_pixbuf_get_rowstride(pixbuf) - 4 * width;
b5f01ae0 488
23656673 489 for (int y = 0; y < height; y++, out += rowpad)
b5f01ae0 490 {
feac7937
VS
491 for (int x = 0; x < width; x++, alpha++, out += 4, in += 3)
492 {
493 out[0] = in[0];
494 out[1] = in[1];
495 out[2] = in[2];
496 out[3] = *alpha;
497 }
b5f01ae0 498 }
902725ee 499
feac7937
VS
500 return true;
501}
b5f01ae0 502
feac7937
VS
503wxImage wxBitmap::ConvertToImage() const
504{
feac7937 505 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
902725ee 506
afbfbfdf
PC
507 const int w = GetWidth();
508 const int h = GetHeight();
23656673 509 wxImage image(w, h, false);
feac7937 510 unsigned char *data = image.GetData();
2eefae6e 511
5588ce92 512 wxCHECK_MSG(data != NULL, wxNullImage, wxT("couldn't create image") );
b5f01ae0 513
70029506 514 // prefer pixbuf if available, it will preserve alpha and should be quicker
feac7937 515 if (HasPixbuf())
b5f01ae0 516 {
feac7937 517 GdkPixbuf *pixbuf = GetPixbuf();
70029506
PC
518 unsigned char* alpha = NULL;
519 if (gdk_pixbuf_get_has_alpha(pixbuf))
520 {
521 image.SetAlpha();
522 alpha = image.GetAlpha();
523 }
23656673 524 const unsigned char* in = gdk_pixbuf_get_pixels(pixbuf);
feac7937 525 unsigned char *out = data;
70029506 526 const int inc = 3 + int(alpha != NULL);
23656673 527 const int rowpad = gdk_pixbuf_get_rowstride(pixbuf) - inc * w;
b5f01ae0 528
23656673 529 for (int y = 0; y < h; y++, in += rowpad)
feac7937 530 {
70029506 531 for (int x = 0; x < w; x++, in += inc, out += 3)
feac7937
VS
532 {
533 out[0] = in[0];
534 out[1] = in[1];
535 out[2] = in[2];
70029506
PC
536 if (alpha != NULL)
537 *alpha++ = in[3];
feac7937
VS
538 }
539 }
b5f01ae0 540 }
feac7937 541 else
b5f01ae0 542 {
afbfbfdf
PC
543 GdkPixmap* pixmap = GetPixmap();
544 GdkPixmap* pixmap_invert = NULL;
545 if (GetDepth() == 1)
b5f01ae0 546 {
d0e79755 547 // mono bitmaps are inverted, i.e. 0 is white
afbfbfdf 548 pixmap_invert = gdk_pixmap_new(pixmap, w, h, 1);
f3d74739 549 wxGtkObject<GdkGC> gc(gdk_gc_new(pixmap_invert));
afbfbfdf
PC
550 gdk_gc_set_function(gc, GDK_COPY_INVERT);
551 gdk_draw_drawable(pixmap_invert, gc, pixmap, 0, 0, 0, 0, w, h);
afbfbfdf 552 pixmap = pixmap_invert;
feac7937 553 }
afbfbfdf
PC
554 // create a pixbuf which shares data with the wxImage
555 GdkPixbuf* pixbuf = gdk_pixbuf_new_from_data(
556 data, GDK_COLORSPACE_RGB, false, 8, w, h, 3 * w, NULL, NULL);
feac7937 557
afbfbfdf 558 gdk_pixbuf_get_from_drawable(pixbuf, pixmap, NULL, 0, 0, 0, 0, w, h);
feac7937 559
afbfbfdf
PC
560 g_object_unref(pixbuf);
561 if (pixmap_invert != NULL)
562 g_object_unref(pixmap_invert);
70029506
PC
563 }
564 // convert mask, unless there is already alpha
565 if (GetMask() && !image.HasAlpha())
566 {
23656673
PC
567 // we hard code the mask colour for now but we could also make an
568 // effort (and waste time) to choose a colour not present in the
569 // image already to avoid having to fudge the pixels below --
570 // whether it's worth to do it is unclear however
70029506
PC
571 const int MASK_RED = 1;
572 const int MASK_GREEN = 2;
573 const int MASK_BLUE = 3;
574 const int MASK_BLUE_REPLACEMENT = 2;
feac7937 575
70029506
PC
576 image.SetMaskColour(MASK_RED, MASK_GREEN, MASK_BLUE);
577 GdkImage* image_mask = gdk_drawable_get_image(GetMask()->GetBitmap(), 0, 0, w, h);
feac7937 578
70029506
PC
579 for (int y = 0; y < h; y++)
580 {
581 for (int x = 0; x < w; x++, data += 3)
8ab696e0 582 {
70029506 583 if (gdk_image_get_pixel(image_mask, x, y) == 0)
8ab696e0 584 {
70029506
PC
585 data[0] = MASK_RED;
586 data[1] = MASK_GREEN;
587 data[2] = MASK_BLUE;
588 }
589 else if (data[0] == MASK_RED && data[1] == MASK_GREEN && data[2] == MASK_BLUE)
590 {
23656673
PC
591 // we have to fudge the colour a bit to prevent
592 // this pixel from appearing transparent
70029506 593 data[2] = MASK_BLUE_REPLACEMENT;
feac7937 594 }
feac7937 595 }
b5f01ae0 596 }
70029506 597 g_object_unref(image_mask);
feac7937 598 }
b5f01ae0
VS
599
600 return image;
601}
602
c0521644
VZ
603#endif // wxUSE_IMAGE
604
91b8de8d 605int wxBitmap::GetHeight() const
c801d85f 606{
223d09f6 607 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
e55ad60e 608
fd0eed64 609 return M_BMPDATA->m_height;
ff7b1510 610}
c801d85f 611
91b8de8d 612int wxBitmap::GetWidth() const
c801d85f 613{
223d09f6 614 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
8bbe427f 615
fd0eed64 616 return M_BMPDATA->m_width;
ff7b1510 617}
c801d85f 618
91b8de8d 619int wxBitmap::GetDepth() const
c801d85f 620{
223d09f6 621 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
8bbe427f 622
fd0eed64 623 return M_BMPDATA->m_bpp;
ff7b1510 624}
c801d85f 625
91b8de8d 626wxMask *wxBitmap::GetMask() const
c801d85f 627{
223d09f6 628 wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") );
8bbe427f 629
fd0eed64 630 return M_BMPDATA->m_mask;
ff7b1510 631}
c801d85f
KB
632
633void wxBitmap::SetMask( wxMask *mask )
634{
223d09f6 635 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
8bbe427f 636
e3e89a93 637 AllocExclusive();
23656673 638 delete M_BMPDATA->m_mask;
fd0eed64 639 M_BMPDATA->m_mask = mask;
ff7b1510 640}
c801d85f 641
db0aec83
VS
642bool wxBitmap::CopyFromIcon(const wxIcon& icon)
643{
644 *this = icon;
5588ce92 645 return Ok();
db0aec83
VS
646}
647
17bec151
RR
648wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const
649{
5588ce92
PC
650 wxBitmap ret;
651
23656673
PC
652 wxCHECK_MSG(Ok(), ret, wxT("invalid bitmap"));
653 wxCHECK_MSG(rect.x >= 0 && rect.y >= 0 &&
654 rect.x + rect.width <= M_BMPDATA->m_width &&
655 rect.y + rect.height <= M_BMPDATA->m_height,
656 ret, wxT("invalid bitmap region"));
13111b2a 657
23656673 658 if (HasPixbuf() || M_BMPDATA->m_bpp == 32)
17bec151 659 {
feac7937 660 GdkPixbuf *pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB,
70029506 661 gdk_pixbuf_get_has_alpha(GetPixbuf()),
17f504ec 662 8, rect.width, rect.height);
23656673 663 ret.SetPixbuf(pixbuf, M_BMPDATA->m_bpp);
6db34764 664 gdk_pixbuf_copy_area(GetPixbuf(),
feac7937
VS
665 rect.x, rect.y, rect.width, rect.height,
666 pixbuf, 0, 0);
17bec151
RR
667 }
668 else
669 {
23656673 670 ret.Create(rect.width, rect.height, M_BMPDATA->m_bpp);
f3d74739 671 wxGtkObject<GdkGC> gc(gdk_gc_new( ret.GetPixmap() ));
9be3f755 672 gdk_draw_drawable( ret.GetPixmap(), gc, GetPixmap(), rect.x, rect.y, 0, 0, rect.width, rect.height );
17bec151 673 }
70029506
PC
674 // make mask, unless there is already alpha
675 if (GetMask() && !HasAlpha())
17bec151
RR
676 {
677 wxMask *mask = new wxMask;
c2fa61e8 678 mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, rect.width, rect.height, 1 );
13111b2a 679
f3d74739 680 wxGtkObject<GdkGC> gc(gdk_gc_new( mask->m_bitmap ));
9be3f755 681 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
682
683 ret.SetMask( mask );
17bec151 684 }
13111b2a 685
17bec151
RR
686 return ret;
687}
688
4611dd06 689bool wxBitmap::SaveFile( const wxString &name, wxBitmapType type, const wxPalette *WXUNUSED(palette) ) const
c801d85f 690{
902725ee 691 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
8bbe427f 692
c0521644 693#if wxUSE_IMAGE
b75dd496 694 // Try to save the bitmap via wxImage handlers:
5588ce92
PC
695 wxImage image = ConvertToImage();
696 return image.Ok() && image.SaveFile(name, type);
c0521644 697#else // !wxUSE_IMAGE
8d22935d
VZ
698 wxUnusedVar(name);
699 wxUnusedVar(type);
700
c0521644
VZ
701 return false;
702#endif // wxUSE_IMAGE
ff7b1510 703}
c801d85f 704
4611dd06 705bool wxBitmap::LoadFile( const wxString &name, wxBitmapType type )
c801d85f 706{
fd0eed64 707 UnRef();
8bbe427f 708
fd0eed64
RR
709 if (type == wxBITMAP_TYPE_XPM)
710 {
fd0eed64 711 GdkBitmap *mask = (GdkBitmap*) NULL;
5588ce92 712 SetPixmap(gdk_pixmap_create_from_xpm(wxGetRootWindow()->window, &mask, NULL, name.fn_str()));
8bbe427f 713
fd0eed64
RR
714 if (mask)
715 {
5588ce92
PC
716 M_BMPDATA->m_mask = new wxMask;
717 M_BMPDATA->m_mask->m_bitmap = mask;
fd0eed64 718 }
fd0eed64 719 }
c0521644 720#if wxUSE_IMAGE
b75dd496 721 else // try if wxImage can load it
fd0eed64
RR
722 {
723 wxImage image;
5588ce92 724 if (image.LoadFile(name, type) && image.Ok())
23656673 725 CreateFromImage(image, -1);
fd0eed64 726 }
c0521644 727#endif // wxUSE_IMAGE
8bbe427f 728
5588ce92 729 return Ok();
ff7b1510 730}
8bbe427f 731
0b04c4e0 732#if wxUSE_PALETTE
91b8de8d 733wxPalette *wxBitmap::GetPalette() const
c801d85f 734{
23656673 735 wxCHECK_MSG(Ok(), NULL, wxT("invalid bitmap"));
8bbe427f 736
fd0eed64 737 return M_BMPDATA->m_palette;
ff7b1510 738}
c801d85f 739
4611dd06
RR
740void wxBitmap::SetPalette(const wxPalette& WXUNUSED(palette))
741{
742 // TODO
743}
0b04c4e0 744#endif // wxUSE_PALETTE
4611dd06 745
4bc67cc5
RR
746void wxBitmap::SetHeight( int height )
747{
e3e89a93 748 AllocExclusive();
4bc67cc5
RR
749 M_BMPDATA->m_height = height;
750}
751
752void wxBitmap::SetWidth( int width )
753{
e3e89a93 754 AllocExclusive();
4bc67cc5
RR
755 M_BMPDATA->m_width = width;
756}
757
758void wxBitmap::SetDepth( int depth )
759{
e3e89a93 760 AllocExclusive();
4bc67cc5
RR
761 M_BMPDATA->m_bpp = depth;
762}
763
764void wxBitmap::SetPixmap( GdkPixmap *pixmap )
765{
3ebcd89d 766 if (!m_refData)
5588ce92 767 m_refData = new wxBitmapRefData;
4bc67cc5 768
e3e89a93
PC
769 // AllocExclusive should not be needed for this internal function
770 wxASSERT(m_refData->GetRefCount() == 1);
5588ce92 771 wxASSERT(M_BMPDATA->m_pixmap == NULL);
4bc67cc5 772 M_BMPDATA->m_pixmap = pixmap;
5588ce92
PC
773 gdk_drawable_get_size(pixmap, &M_BMPDATA->m_width, &M_BMPDATA->m_height);
774 M_BMPDATA->m_bpp = gdk_drawable_get_depth(pixmap);
6db34764 775 PurgeOtherRepresentations(Pixmap);
4bc67cc5
RR
776}
777
91b8de8d 778GdkPixmap *wxBitmap::GetPixmap() const
c801d85f 779{
223d09f6 780 wxCHECK_MSG( Ok(), (GdkPixmap *) NULL, wxT("invalid bitmap") );
8bbe427f 781
feac7937 782 // create the pixmap on the fly if we use Pixbuf representation:
5588ce92 783 if (M_BMPDATA->m_pixmap == NULL)
feac7937 784 {
70029506
PC
785 GdkPixmap** pmask = NULL;
786 if (gdk_pixbuf_get_has_alpha(M_BMPDATA->m_pixbuf))
787 {
788 // make new mask from alpha
789 delete M_BMPDATA->m_mask;
790 M_BMPDATA->m_mask = new wxMask;
791 pmask = &M_BMPDATA->m_mask->m_bitmap;
792 }
feac7937
VS
793 gdk_pixbuf_render_pixmap_and_mask(M_BMPDATA->m_pixbuf,
794 &M_BMPDATA->m_pixmap,
70029506 795 pmask,
c0521644 796 0x80 /* alpha threshold */);
feac7937 797 }
feac7937 798
fd0eed64 799 return M_BMPDATA->m_pixmap;
ff7b1510 800}
8bbe427f 801
feac7937
VS
802bool wxBitmap::HasPixmap() const
803{
804 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
805
806 return M_BMPDATA->m_pixmap != NULL;
807}
808
feac7937
VS
809GdkPixbuf *wxBitmap::GetPixbuf() const
810{
811 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") );
812
5588ce92 813 if (M_BMPDATA->m_pixbuf == NULL)
6db34764 814 {
4b230a33 815
6db34764
VS
816 int width = GetWidth();
817 int height = GetHeight();
902725ee 818
6db34764 819 GdkPixbuf *pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB,
70029506 820 GetMask() != NULL,
6db34764 821 8, width, height);
23656673
PC
822 M_BMPDATA->m_pixbuf = pixbuf;
823 gdk_pixbuf_get_from_drawable(pixbuf, M_BMPDATA->m_pixmap, NULL,
824 0, 0, 0, 0, width, height);
6db34764
VS
825 // apply the mask to created pixbuf:
826 if (M_BMPDATA->m_pixbuf && M_BMPDATA->m_mask)
827 {
902725ee 828 GdkPixbuf *pmask =
6db34764
VS
829 gdk_pixbuf_get_from_drawable(NULL,
830 M_BMPDATA->m_mask->GetBitmap(),
831 NULL,
832 0, 0, 0, 0, width, height);
833 if (pmask)
834 {
835 guchar *bmp = gdk_pixbuf_get_pixels(pixbuf);
836 guchar *mask = gdk_pixbuf_get_pixels(pmask);
837 int bmprowinc = gdk_pixbuf_get_rowstride(pixbuf) - 4 * width;
838 int maskrowinc = gdk_pixbuf_get_rowstride(pmask) - 3 * width;
839
840 for (int y = 0; y < height;
841 y++, bmp += bmprowinc, mask += maskrowinc)
842 {
843 for (int x = 0; x < width; x++, bmp += 4, mask += 3)
844 {
845 if (mask[0] == 0 /*black pixel*/)
846 bmp[3] = 0;
847 }
848 }
902725ee 849
3fe39b0c 850 g_object_unref (pmask);
6db34764
VS
851 }
852 }
853 }
854
feac7937
VS
855 return M_BMPDATA->m_pixbuf;
856}
857
858bool wxBitmap::HasPixbuf() const
859{
860 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
861
862 return M_BMPDATA->m_pixbuf != NULL;
863}
864
23656673 865void wxBitmap::SetPixbuf(GdkPixbuf* pixbuf, int depth)
feac7937
VS
866{
867 if (!m_refData)
5588ce92 868 m_refData = new wxBitmapRefData;
feac7937 869
e3e89a93
PC
870 // AllocExclusive should not be needed for this internal function
871 wxASSERT(m_refData->GetRefCount() == 1);
5588ce92 872 wxASSERT(M_BMPDATA->m_pixbuf == NULL);
feac7937 873 M_BMPDATA->m_pixbuf = pixbuf;
5588ce92
PC
874 M_BMPDATA->m_width = gdk_pixbuf_get_width(pixbuf);
875 M_BMPDATA->m_height = gdk_pixbuf_get_height(pixbuf);
23656673
PC
876 // if depth specified
877 if (depth != 0)
878 M_BMPDATA->m_bpp = depth;
879 else if (M_BMPDATA->m_bpp == 0)
880 // use something reasonable
881 M_BMPDATA->m_bpp = wxTheApp->GetGdkVisual()->depth;
6db34764 882 PurgeOtherRepresentations(Pixbuf);
feac7937
VS
883}
884
885void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep)
886{
887 if (keep == Pixmap && HasPixbuf())
888 {
3fe39b0c 889 g_object_unref (M_BMPDATA->m_pixbuf);
feac7937
VS
890 M_BMPDATA->m_pixbuf = NULL;
891 }
892 if (keep == Pixbuf && HasPixmap())
893 {
3fe39b0c 894 g_object_unref (M_BMPDATA->m_pixmap);
feac7937
VS
895 M_BMPDATA->m_pixmap = NULL;
896 }
897}
898
284f2b59
RR
899void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp)
900{
70029506 901 void* bits = NULL;
284f2b59 902 GdkPixbuf *pixbuf = GetPixbuf();
70029506 903 const bool hasAlpha = HasAlpha();
4b230a33 904
70029506 905 // allow access if bpp is valid and matches existence of alpha
c74fc1e1 906 if ( pixbuf && ((bpp == 24 && !hasAlpha) || (bpp == 32 && hasAlpha)) )
70029506
PC
907 {
908 data.m_height = gdk_pixbuf_get_height( pixbuf );
909 data.m_width = gdk_pixbuf_get_width( pixbuf );
910 data.m_stride = gdk_pixbuf_get_rowstride( pixbuf );
911 bits = gdk_pixbuf_get_pixels(pixbuf);
912 }
913 return bits;
284f2b59
RR
914}
915
17a1ebd1 916void wxBitmap::UngetRawData(wxPixelDataBase& WXUNUSED(data))
284f2b59
RR
917{
918}
919
902725ee 920bool wxBitmap::HasAlpha() const
0ff2a74d 921{
70029506
PC
922 return m_refData != NULL && M_BMPDATA->m_pixbuf != NULL &&
923 gdk_pixbuf_get_has_alpha(M_BMPDATA->m_pixbuf);
0ff2a74d
RR
924}
925
8f884a0d 926wxGDIRefData* wxBitmap::CreateGDIRefData() const
e3e89a93
PC
927{
928 return new wxBitmapRefData;
929}
930
8f884a0d 931wxGDIRefData* wxBitmap::CloneGDIRefData(const wxGDIRefData* data) const
e3e89a93
PC
932{
933 const wxBitmapRefData* oldRef = wx_static_cast(const wxBitmapRefData*, data);
934 wxBitmapRefData* newRef = new wxBitmapRefData;
935 newRef->m_width = oldRef->m_width;
936 newRef->m_height = oldRef->m_height;
937 newRef->m_bpp = oldRef->m_bpp;
938 if (oldRef->m_pixmap != NULL)
939 {
940 newRef->m_pixmap = gdk_pixmap_new(
941 oldRef->m_pixmap, oldRef->m_width, oldRef->m_height,
942 // use pixmap depth, m_bpp may not match
943 gdk_drawable_get_depth(oldRef->m_pixmap));
f3d74739 944 wxGtkObject<GdkGC> gc(gdk_gc_new(newRef->m_pixmap));
e3e89a93
PC
945 gdk_draw_drawable(
946 newRef->m_pixmap, gc, oldRef->m_pixmap, 0, 0, 0, 0, -1, -1);
e3e89a93
PC
947 }
948 if (oldRef->m_pixbuf != NULL)
949 {
950 newRef->m_pixbuf = gdk_pixbuf_copy(oldRef->m_pixbuf);
951 }
952 if (oldRef->m_mask != NULL)
953 {
954 newRef->m_mask = new wxMask;
955 newRef->m_mask->m_bitmap = gdk_pixmap_new(
956 oldRef->m_mask->m_bitmap, oldRef->m_width, oldRef->m_height, 1);
f3d74739 957 wxGtkObject<GdkGC> gc(gdk_gc_new(newRef->m_mask->m_bitmap));
e3e89a93
PC
958 gdk_draw_drawable(newRef->m_mask->m_bitmap,
959 gc, oldRef->m_mask->m_bitmap, 0, 0, 0, 0, -1, -1);
e3e89a93
PC
960 }
961#if wxUSE_PALETTE
962 // implement this if SetPalette is ever implemented
963 wxASSERT(oldRef->m_palette == NULL);
964#endif
965
966 return newRef;
967}
968
4b61c88d
RR
969/* static */ void wxBitmap::InitStandardHandlers()
970{
971 // TODO: Insert handler based on GdkPixbufs handler later
972}