]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/bitmap.cpp
make wxKeyEvent and wxMouseEvent derive from the same wxKeyboardState object (indirec...
[wxWidgets.git] / src / gtk / bitmap.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/bitmap.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // RCS-ID: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #include "wx/bitmap.h"
14
15 #ifndef WX_PRECOMP
16 #include "wx/app.h"
17 #include "wx/palette.h"
18 #include "wx/icon.h"
19 #include "wx/math.h"
20 #include "wx/image.h"
21 #include "wx/colour.h"
22 #endif
23
24 #include "wx/rawbmp.h"
25
26 #include "wx/gtk/private/object.h"
27
28 #include <gtk/gtk.h>
29
30 //-----------------------------------------------------------------------------
31 // data
32 //-----------------------------------------------------------------------------
33
34 extern GtkWidget *wxGetRootWindow();
35
36 //-----------------------------------------------------------------------------
37 // wxMask
38 //-----------------------------------------------------------------------------
39
40 IMPLEMENT_DYNAMIC_CLASS(wxMask,wxObject)
41
42 wxMask::wxMask()
43 {
44 m_bitmap = (GdkBitmap *) NULL;
45 }
46
47 wxMask::wxMask( const wxBitmap& bitmap, const wxColour& colour )
48 {
49 m_bitmap = (GdkBitmap *) NULL;
50 Create( bitmap, colour );
51 }
52
53 #if wxUSE_PALETTE
54 wxMask::wxMask( const wxBitmap& bitmap, int paletteIndex )
55 {
56 m_bitmap = (GdkBitmap *) NULL;
57 Create( bitmap, paletteIndex );
58 }
59 #endif // wxUSE_PALETTE
60
61 wxMask::wxMask( const wxBitmap& bitmap )
62 {
63 m_bitmap = (GdkBitmap *) NULL;
64 Create( bitmap );
65 }
66
67 wxMask::~wxMask()
68 {
69 if (m_bitmap)
70 g_object_unref (m_bitmap);
71 }
72
73 bool wxMask::Create( const wxBitmap& bitmap,
74 const wxColour& colour )
75 {
76 if (m_bitmap)
77 {
78 g_object_unref (m_bitmap);
79 m_bitmap = (GdkBitmap*) NULL;
80 }
81
82 const int w = bitmap.GetWidth();
83 const int h = bitmap.GetHeight();
84
85 // create mask as XBM format bitmap
86
87 // one bit per pixel, each row starts on a byte boundary
88 const size_t out_size = size_t((w + 7) / 8) * unsigned(h);
89 wxByte* out = new wxByte[out_size];
90 // set bits are unmasked
91 memset(out, 0xff, out_size);
92 unsigned bit_index = 0;
93 if (bitmap.HasPixbuf())
94 {
95 const wxByte r_mask = colour.Red();
96 const wxByte g_mask = colour.Green();
97 const wxByte b_mask = colour.Blue();
98 GdkPixbuf* pixbuf = bitmap.GetPixbuf();
99 const wxByte* in = gdk_pixbuf_get_pixels(pixbuf);
100 const int inc = 3 + int(gdk_pixbuf_get_has_alpha(pixbuf) != 0);
101 const int rowpadding = gdk_pixbuf_get_rowstride(pixbuf) - inc * w;
102 for (int y = 0; y < h; y++, in += rowpadding)
103 {
104 for (int x = 0; x < w; x++, in += inc, bit_index++)
105 if (in[0] == r_mask && in[1] == g_mask && in[2] == b_mask)
106 out[bit_index >> 3] ^= 1 << (bit_index & 7);
107 // move index to next byte boundary
108 bit_index = (bit_index + 7) & ~7u;
109 }
110 }
111 else
112 {
113 GdkImage* image = gdk_drawable_get_image(bitmap.GetPixmap(), 0, 0, w, h);
114 GdkColormap* colormap = gdk_image_get_colormap(image);
115 guint32 mask_pixel;
116 if (colormap == NULL)
117 // mono bitmap, white is pixel value 0
118 mask_pixel = guint32(colour.Red() != 255 || colour.Green() != 255 || colour.Blue() != 255);
119 else
120 {
121 wxColor c(colour);
122 c.CalcPixel(colormap);
123 mask_pixel = c.GetPixel();
124 }
125 for (int y = 0; y < h; y++)
126 {
127 for (int x = 0; x < w; x++, bit_index++)
128 if (gdk_image_get_pixel(image, x, y) == mask_pixel)
129 out[bit_index >> 3] ^= 1 << (bit_index & 7);
130 bit_index = (bit_index + 7) & ~7u;
131 }
132 g_object_unref(image);
133 }
134 m_bitmap = gdk_bitmap_create_from_data(wxGetRootWindow()->window, (char*)out, w, h);
135 delete[] out;
136 return true;
137 }
138
139 #if wxUSE_PALETTE
140 bool wxMask::Create( const wxBitmap& bitmap, int paletteIndex )
141 {
142 unsigned char r,g,b;
143 wxPalette *pal = bitmap.GetPalette();
144
145 wxCHECK_MSG( pal, false, wxT("Cannot create mask from bitmap without palette") );
146
147 pal->GetRGB(paletteIndex, &r, &g, &b);
148
149 return Create(bitmap, wxColour(r, g, b));
150 }
151 #endif // wxUSE_PALETTE
152
153 bool wxMask::Create( const wxBitmap& bitmap )
154 {
155 if (m_bitmap)
156 {
157 g_object_unref (m_bitmap);
158 m_bitmap = (GdkBitmap*) NULL;
159 }
160
161 if (!bitmap.Ok()) return false;
162
163 wxCHECK_MSG( bitmap.GetDepth() == 1, false, wxT("Cannot create mask from colour bitmap") );
164
165 m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, bitmap.GetWidth(), bitmap.GetHeight(), 1 );
166
167 if (!m_bitmap) return false;
168
169 wxGtkObject<GdkGC> gc(gdk_gc_new( m_bitmap ));
170 gdk_gc_set_function(gc, GDK_COPY_INVERT);
171 gdk_draw_drawable(m_bitmap, gc, bitmap.GetPixmap(), 0, 0, 0, 0, bitmap.GetWidth(), bitmap.GetHeight());
172
173 return true;
174 }
175
176 GdkBitmap *wxMask::GetBitmap() const
177 {
178 return m_bitmap;
179 }
180
181 //-----------------------------------------------------------------------------
182 // wxBitmap
183 //-----------------------------------------------------------------------------
184
185 class wxBitmapRefData: public wxGDIRefData
186 {
187 public:
188 wxBitmapRefData();
189 virtual ~wxBitmapRefData();
190
191 virtual bool IsOk() const { return m_pixmap || m_pixbuf; }
192
193 GdkPixmap *m_pixmap;
194 GdkPixbuf *m_pixbuf;
195 wxMask *m_mask;
196 int m_width;
197 int m_height;
198 int m_bpp;
199 #if wxUSE_PALETTE
200 wxPalette *m_palette;
201 #endif // wxUSE_PALETTE
202 };
203
204 wxBitmapRefData::wxBitmapRefData()
205 {
206 m_pixmap = (GdkPixmap *) NULL;
207 m_pixbuf = (GdkPixbuf *) NULL;
208 m_mask = (wxMask *) NULL;
209 m_width = 0;
210 m_height = 0;
211 m_bpp = 0;
212 #if wxUSE_PALETTE
213 m_palette = (wxPalette *) NULL;
214 #endif // wxUSE_PALETTE
215 }
216
217 wxBitmapRefData::~wxBitmapRefData()
218 {
219 if (m_pixmap)
220 g_object_unref (m_pixmap);
221 if (m_pixbuf)
222 g_object_unref (m_pixbuf);
223 delete m_mask;
224 #if wxUSE_PALETTE
225 delete m_palette;
226 #endif // wxUSE_PALETTE
227 }
228
229 //-----------------------------------------------------------------------------
230
231 #define M_BMPDATA wx_static_cast(wxBitmapRefData*, m_refData)
232
233 IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxGDIObject)
234
235 wxBitmap::wxBitmap(int width, int height, int depth)
236 {
237 Create(width, height, depth);
238 }
239
240 wxBitmap::wxBitmap(const wxString &filename, wxBitmapType type)
241 {
242 LoadFile(filename, type);
243 }
244
245 wxBitmap::wxBitmap(const char bits[], int width, int height, int depth)
246 {
247 wxASSERT(depth == 1);
248 if (width > 0 && height > 0 && depth == 1)
249 {
250 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window, bits, width, height));
251
252 wxASSERT_MSG( M_BMPDATA->m_pixmap, wxT("couldn't create bitmap") );
253 }
254 }
255
256 wxBitmap::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
270 wxBitmap::~wxBitmap()
271 {
272 }
273
274 bool wxBitmap::Create( int width, int height, int depth )
275 {
276 UnRef();
277
278 if ( width <= 0 || height <= 0 )
279 {
280 return false;
281 }
282
283 if (depth == 32)
284 {
285 SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB, true, 8, width, height), 32);
286 }
287 else
288 {
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));
299 }
300
301 return Ok();
302 }
303
304 wxBitmap wxBitmap::Rescale(int clipx, int clipy, int clipwidth, int clipheight,
305 int width, int height) const
306 {
307 wxBitmap bmp;
308
309 wxCHECK_MSG(Ok(), bmp, wxT("invalid bitmap"));
310
311 width = wxMax(width, 1);
312 height = wxMax(height, 1);
313
314 const double scale_x = double(width) / clipwidth;
315 const double scale_y = double(height) / clipheight;
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);
325 else
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);
329
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);
333
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,
338 0, 0, width, height, -clipx*scale_x, -clipy*scale_y, scale_x, scale_y,
339 GDK_INTERP_NEAREST);
340
341 g_object_unref(pixbuf);
342 bmp.SetPixbuf(pixbuf_scaled, M_BMPDATA->m_bpp);
343
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);
349
350 pixbuf_scaled = gdk_pixbuf_new(
351 GDK_COLORSPACE_RGB, false, 8, width, height);
352
353 gdk_pixbuf_scale(
354 pixbuf, pixbuf_scaled,
355 0, 0, width, height, -clipx, -clipy, scale_x, scale_y,
356 GDK_INTERP_NEAREST);
357
358 g_object_unref(pixbuf);
359
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));
364 }
365 return bmp;
366 }
367
368 #if wxUSE_IMAGE
369
370 bool wxBitmap::CreateFromImage(const wxImage& image, int depth)
371 {
372 UnRef();
373
374 wxCHECK_MSG( image.Ok(), false, wxT("invalid image") );
375 wxCHECK_MSG( depth == -1 || depth == 1, false, wxT("invalid bitmap depth") );
376
377 if (image.GetWidth() <= 0 || image.GetHeight() <= 0)
378 return false;
379
380 // create pixbuf if image has alpha and requested depth is compatible
381 if (image.HasAlpha() && (depth == -1 || depth == 32))
382 return CreateFromImageAsPixbuf(image);
383
384 // otherwise create pixmap, if alpha is present it will be converted to mask
385 return CreateFromImageAsPixmap(image, depth);
386 }
387
388 bool wxBitmap::CreateFromImageAsPixmap(const wxImage& image, int depth)
389 {
390 const int w = image.GetWidth();
391 const int h = image.GetHeight();
392 if (depth == 1)
393 {
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];
399 // set bits are black
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));
417 wxGtkObject<GdkGC> gc(gdk_gc_new(M_BMPDATA->m_pixmap));
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);
422 }
423
424 const wxByte* alpha = image.GetAlpha();
425 if (alpha != NULL || image.HasMask())
426 {
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)
434 {
435 for (int y = 0; y < h; y++)
436 {
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;
441 }
442 }
443 else
444 {
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 }
462 return true;
463 }
464
465 bool wxBitmap::CreateFromImageAsPixbuf(const wxImage& image)
466 {
467 wxASSERT(image.HasAlpha());
468
469 int width = image.GetWidth();
470 int height = image.GetHeight();
471
472 Create(width, height, 32);
473 GdkPixbuf* pixbuf = M_BMPDATA->m_pixbuf;
474 if (!pixbuf)
475 return false;
476
477 // Copy the data:
478 const unsigned char* in = image.GetData();
479 unsigned char *out = gdk_pixbuf_get_pixels(pixbuf);
480 unsigned char *alpha = image.GetAlpha();
481
482 int rowpad = gdk_pixbuf_get_rowstride(pixbuf) - 4 * width;
483
484 for (int y = 0; y < height; y++, out += rowpad)
485 {
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 }
493 }
494
495 return true;
496 }
497
498 wxImage wxBitmap::ConvertToImage() const
499 {
500 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
501
502 const int w = GetWidth();
503 const int h = GetHeight();
504 wxImage image(w, h, false);
505 unsigned char *data = image.GetData();
506
507 wxCHECK_MSG(data != NULL, wxNullImage, wxT("couldn't create image") );
508
509 // prefer pixbuf if available, it will preserve alpha and should be quicker
510 if (HasPixbuf())
511 {
512 GdkPixbuf *pixbuf = GetPixbuf();
513 unsigned char* alpha = NULL;
514 if (gdk_pixbuf_get_has_alpha(pixbuf))
515 {
516 image.SetAlpha();
517 alpha = image.GetAlpha();
518 }
519 const unsigned char* in = gdk_pixbuf_get_pixels(pixbuf);
520 unsigned char *out = data;
521 const int inc = 3 + int(alpha != NULL);
522 const int rowpad = gdk_pixbuf_get_rowstride(pixbuf) - inc * w;
523
524 for (int y = 0; y < h; y++, in += rowpad)
525 {
526 for (int x = 0; x < w; x++, in += inc, out += 3)
527 {
528 out[0] = in[0];
529 out[1] = in[1];
530 out[2] = in[2];
531 if (alpha != NULL)
532 *alpha++ = in[3];
533 }
534 }
535 }
536 else
537 {
538 GdkPixmap* pixmap = GetPixmap();
539 GdkPixmap* pixmap_invert = NULL;
540 if (GetDepth() == 1)
541 {
542 // mono bitmaps are inverted, i.e. 0 is white
543 pixmap_invert = gdk_pixmap_new(pixmap, w, h, 1);
544 wxGtkObject<GdkGC> gc(gdk_gc_new(pixmap_invert));
545 gdk_gc_set_function(gc, GDK_COPY_INVERT);
546 gdk_draw_drawable(pixmap_invert, gc, pixmap, 0, 0, 0, 0, w, h);
547 pixmap = pixmap_invert;
548 }
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);
552
553 gdk_pixbuf_get_from_drawable(pixbuf, pixmap, NULL, 0, 0, 0, 0, w, h);
554
555 g_object_unref(pixbuf);
556 if (pixmap_invert != NULL)
557 g_object_unref(pixmap_invert);
558 }
559 // convert mask, unless there is already alpha
560 if (GetMask() && !image.HasAlpha())
561 {
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
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;
570
571 image.SetMaskColour(MASK_RED, MASK_GREEN, MASK_BLUE);
572 GdkImage* image_mask = gdk_drawable_get_image(GetMask()->GetBitmap(), 0, 0, w, h);
573
574 for (int y = 0; y < h; y++)
575 {
576 for (int x = 0; x < w; x++, data += 3)
577 {
578 if (gdk_image_get_pixel(image_mask, x, y) == 0)
579 {
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 {
586 // we have to fudge the colour a bit to prevent
587 // this pixel from appearing transparent
588 data[2] = MASK_BLUE_REPLACEMENT;
589 }
590 }
591 }
592 g_object_unref(image_mask);
593 }
594
595 return image;
596 }
597
598 #endif // wxUSE_IMAGE
599
600 int wxBitmap::GetHeight() const
601 {
602 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
603
604 return M_BMPDATA->m_height;
605 }
606
607 int wxBitmap::GetWidth() const
608 {
609 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
610
611 return M_BMPDATA->m_width;
612 }
613
614 int wxBitmap::GetDepth() const
615 {
616 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
617
618 return M_BMPDATA->m_bpp;
619 }
620
621 wxMask *wxBitmap::GetMask() const
622 {
623 wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") );
624
625 return M_BMPDATA->m_mask;
626 }
627
628 void wxBitmap::SetMask( wxMask *mask )
629 {
630 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
631
632 AllocExclusive();
633 delete M_BMPDATA->m_mask;
634 M_BMPDATA->m_mask = mask;
635 }
636
637 bool wxBitmap::CopyFromIcon(const wxIcon& icon)
638 {
639 *this = icon;
640 return Ok();
641 }
642
643 wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const
644 {
645 wxBitmap ret;
646
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"));
652
653 if (HasPixbuf() || M_BMPDATA->m_bpp == 32)
654 {
655 GdkPixbuf *pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB,
656 gdk_pixbuf_get_has_alpha(GetPixbuf()),
657 8, rect.width, rect.height);
658 ret.SetPixbuf(pixbuf, M_BMPDATA->m_bpp);
659 gdk_pixbuf_copy_area(GetPixbuf(),
660 rect.x, rect.y, rect.width, rect.height,
661 pixbuf, 0, 0);
662 }
663 else
664 {
665 ret.Create(rect.width, rect.height, M_BMPDATA->m_bpp);
666 wxGtkObject<GdkGC> gc(gdk_gc_new( ret.GetPixmap() ));
667 gdk_draw_drawable( ret.GetPixmap(), gc, GetPixmap(), rect.x, rect.y, 0, 0, rect.width, rect.height );
668 }
669 // make mask, unless there is already alpha
670 if (GetMask() && !HasAlpha())
671 {
672 wxMask *mask = new wxMask;
673 mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, rect.width, rect.height, 1 );
674
675 wxGtkObject<GdkGC> gc(gdk_gc_new( mask->m_bitmap ));
676 gdk_draw_drawable(mask->m_bitmap, gc, M_BMPDATA->m_mask->m_bitmap, rect.x, rect.y, 0, 0, rect.width, rect.height);
677
678 ret.SetMask( mask );
679 }
680
681 return ret;
682 }
683
684 bool wxBitmap::SaveFile( const wxString &name, wxBitmapType type, const wxPalette *WXUNUSED(palette) ) const
685 {
686 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
687
688 #if wxUSE_IMAGE
689 // Try to save the bitmap via wxImage handlers:
690 wxImage image = ConvertToImage();
691 return image.Ok() && image.SaveFile(name, type);
692 #else // !wxUSE_IMAGE
693 wxUnusedVar(name);
694 wxUnusedVar(type);
695
696 return false;
697 #endif // wxUSE_IMAGE
698 }
699
700 bool wxBitmap::LoadFile( const wxString &name, wxBitmapType type )
701 {
702 UnRef();
703
704 if (type == wxBITMAP_TYPE_XPM)
705 {
706 GdkBitmap *mask = (GdkBitmap*) NULL;
707 SetPixmap(gdk_pixmap_create_from_xpm(wxGetRootWindow()->window, &mask, NULL, name.fn_str()));
708
709 if (mask)
710 {
711 M_BMPDATA->m_mask = new wxMask;
712 M_BMPDATA->m_mask->m_bitmap = mask;
713 }
714 }
715 #if wxUSE_IMAGE
716 else // try if wxImage can load it
717 {
718 wxImage image;
719 if (image.LoadFile(name, type) && image.Ok())
720 CreateFromImage(image, -1);
721 }
722 #endif // wxUSE_IMAGE
723
724 return Ok();
725 }
726
727 #if wxUSE_PALETTE
728 wxPalette *wxBitmap::GetPalette() const
729 {
730 wxCHECK_MSG(Ok(), NULL, wxT("invalid bitmap"));
731
732 return M_BMPDATA->m_palette;
733 }
734
735 void wxBitmap::SetPalette(const wxPalette& WXUNUSED(palette))
736 {
737 // TODO
738 }
739 #endif // wxUSE_PALETTE
740
741 void wxBitmap::SetHeight( int height )
742 {
743 AllocExclusive();
744 M_BMPDATA->m_height = height;
745 }
746
747 void wxBitmap::SetWidth( int width )
748 {
749 AllocExclusive();
750 M_BMPDATA->m_width = width;
751 }
752
753 void wxBitmap::SetDepth( int depth )
754 {
755 AllocExclusive();
756 M_BMPDATA->m_bpp = depth;
757 }
758
759 void wxBitmap::SetPixmap( GdkPixmap *pixmap )
760 {
761 if (!m_refData)
762 m_refData = new wxBitmapRefData;
763
764 // AllocExclusive should not be needed for this internal function
765 wxASSERT(m_refData->GetRefCount() == 1);
766 wxASSERT(M_BMPDATA->m_pixmap == NULL);
767 M_BMPDATA->m_pixmap = pixmap;
768 gdk_drawable_get_size(pixmap, &M_BMPDATA->m_width, &M_BMPDATA->m_height);
769 M_BMPDATA->m_bpp = gdk_drawable_get_depth(pixmap);
770 PurgeOtherRepresentations(Pixmap);
771 }
772
773 GdkPixmap *wxBitmap::GetPixmap() const
774 {
775 wxCHECK_MSG( Ok(), (GdkPixmap *) NULL, wxT("invalid bitmap") );
776
777 // create the pixmap on the fly if we use Pixbuf representation:
778 if (M_BMPDATA->m_pixmap == NULL)
779 {
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 }
788 gdk_pixbuf_render_pixmap_and_mask(M_BMPDATA->m_pixbuf,
789 &M_BMPDATA->m_pixmap,
790 pmask,
791 0x80 /* alpha threshold */);
792 }
793
794 return M_BMPDATA->m_pixmap;
795 }
796
797 bool wxBitmap::HasPixmap() const
798 {
799 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
800
801 return M_BMPDATA->m_pixmap != NULL;
802 }
803
804 GdkPixbuf *wxBitmap::GetPixbuf() const
805 {
806 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") );
807
808 if (M_BMPDATA->m_pixbuf == NULL)
809 {
810 int width = GetWidth();
811 int height = GetHeight();
812
813 GdkPixbuf *pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB,
814 GetMask() != NULL,
815 8, width, height);
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);
819
820 // apply the mask to created pixbuf:
821 if (M_BMPDATA->m_pixbuf && M_BMPDATA->m_mask)
822 {
823 GdkPixbuf *pmask =
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 }
844
845 g_object_unref (pmask);
846 }
847 }
848 }
849
850 return M_BMPDATA->m_pixbuf;
851 }
852
853 bool wxBitmap::HasPixbuf() const
854 {
855 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
856
857 return M_BMPDATA->m_pixbuf != NULL;
858 }
859
860 void wxBitmap::SetPixbuf(GdkPixbuf* pixbuf, int depth)
861 {
862 if (!m_refData)
863 m_refData = new wxBitmapRefData;
864
865 // AllocExclusive should not be needed for this internal function
866 wxASSERT(m_refData->GetRefCount() == 1);
867 wxASSERT(M_BMPDATA->m_pixbuf == NULL);
868 M_BMPDATA->m_pixbuf = pixbuf;
869 M_BMPDATA->m_width = gdk_pixbuf_get_width(pixbuf);
870 M_BMPDATA->m_height = gdk_pixbuf_get_height(pixbuf);
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;
877 PurgeOtherRepresentations(Pixbuf);
878 }
879
880 void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep)
881 {
882 if (keep == Pixmap && HasPixbuf())
883 {
884 g_object_unref (M_BMPDATA->m_pixbuf);
885 M_BMPDATA->m_pixbuf = NULL;
886 }
887 if (keep == Pixbuf && HasPixmap())
888 {
889 g_object_unref (M_BMPDATA->m_pixmap);
890 M_BMPDATA->m_pixmap = NULL;
891 }
892 }
893
894 void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp)
895 {
896 void* bits = NULL;
897 GdkPixbuf *pixbuf = GetPixbuf();
898 const bool hasAlpha = HasAlpha();
899 // allow access if bpp is valid and matches existence of alpha
900 if ( pixbuf && ((bpp == 24 && !hasAlpha) || (bpp == 32 && hasAlpha)) )
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;
908 }
909
910 void wxBitmap::UngetRawData(wxPixelDataBase& WXUNUSED(data))
911 {
912 }
913
914 bool wxBitmap::HasAlpha() const
915 {
916 return m_refData != NULL && M_BMPDATA->m_pixbuf != NULL &&
917 gdk_pixbuf_get_has_alpha(M_BMPDATA->m_pixbuf);
918 }
919
920 wxGDIRefData* wxBitmap::CreateGDIRefData() const
921 {
922 return new wxBitmapRefData;
923 }
924
925 wxGDIRefData* wxBitmap::CloneGDIRefData(const wxGDIRefData* data) const
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));
938 wxGtkObject<GdkGC> gc(gdk_gc_new(newRef->m_pixmap));
939 gdk_draw_drawable(
940 newRef->m_pixmap, gc, oldRef->m_pixmap, 0, 0, 0, 0, -1, -1);
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);
951 wxGtkObject<GdkGC> gc(gdk_gc_new(newRef->m_mask->m_bitmap));
952 gdk_draw_drawable(newRef->m_mask->m_bitmap,
953 gc, oldRef->m_mask->m_bitmap, 0, 0, 0, 0, -1, -1);
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
963 /* static */ void wxBitmap::InitStandardHandlers()
964 {
965 // TODO: Insert handler based on GdkPixbufs handler later
966 }