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