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