first pass of wxUniv merge - nothing works, most parts don't even compile
[wxWidgets.git] / src / gtk1 / bitmap.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #ifdef __GNUG__
11 #pragma implementation "bitmap.h"
12 #endif
13
14 #include "wx/bitmap.h"
15 #include "wx/icon.h"
16 #include "wx/filefn.h"
17 #include "wx/image.h"
18 #include "wx/dcmemory.h"
19 #include "wx/app.h"
20
21 #include <gdk/gdk.h>
22 #include <gtk/gtk.h>
23 #include <gdk/gdkx.h>
24
25 #if (GTK_MINOR_VERSION > 0)
26 #include <gdk/gdkrgb.h>
27 #endif
28
29 extern void gdk_wx_draw_bitmap (GdkDrawable *drawable,
30 GdkGC *gc,
31 GdkDrawable *src,
32 gint xsrc,
33 gint ysrc,
34 gint xdest,
35 gint ydest,
36 gint width,
37 gint height);
38
39 //-----------------------------------------------------------------------------
40 // data
41 //-----------------------------------------------------------------------------
42
43 extern GtkWidget *wxGetRootWindow();
44
45 //-----------------------------------------------------------------------------
46 // wxMask
47 //-----------------------------------------------------------------------------
48
49 IMPLEMENT_DYNAMIC_CLASS(wxMask,wxObject)
50
51 wxMask::wxMask()
52 {
53 m_bitmap = (GdkBitmap *) NULL;
54 }
55
56 wxMask::wxMask( const wxBitmap& bitmap, const wxColour& colour )
57 {
58 m_bitmap = (GdkBitmap *) NULL;
59 Create( bitmap, colour );
60 }
61
62 wxMask::wxMask( const wxBitmap& bitmap, int paletteIndex )
63 {
64 m_bitmap = (GdkBitmap *) NULL;
65 Create( bitmap, paletteIndex );
66 }
67
68 wxMask::wxMask( const wxBitmap& bitmap )
69 {
70 m_bitmap = (GdkBitmap *) NULL;
71 Create( bitmap );
72 }
73
74 wxMask::~wxMask()
75 {
76 if (m_bitmap)
77 gdk_bitmap_unref( m_bitmap );
78 }
79
80 bool wxMask::Create( const wxBitmap& bitmap,
81 const wxColour& colour )
82 {
83 if (m_bitmap)
84 {
85 gdk_bitmap_unref( m_bitmap );
86 m_bitmap = (GdkBitmap*) NULL;
87 }
88
89 wxImage image( bitmap );
90 if (!image.Ok()) return FALSE;
91
92 m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, image.GetWidth(), image.GetHeight(), 1 );
93 GdkGC *gc = gdk_gc_new( m_bitmap );
94
95 GdkColor color;
96 color.red = 65000;
97 color.green = 65000;
98 color.blue = 65000;
99 color.pixel = 1;
100 gdk_gc_set_foreground( gc, &color );
101 gdk_gc_set_fill( gc, GDK_SOLID );
102 gdk_draw_rectangle( m_bitmap, gc, TRUE, 0, 0, image.GetWidth(), image.GetHeight() );
103
104 unsigned char *data = image.GetData();
105 int index = 0;
106
107 unsigned char red = colour.Red();
108 unsigned char green = colour.Green();
109 unsigned char blue = colour.Blue();
110
111 GdkVisual *visual = gdk_window_get_visual( wxGetRootWindow()->window );
112 wxASSERT( visual );
113
114 int bpp = visual->depth;
115 if ((bpp == 16) && (visual->red_mask != 0xf800)) bpp = 15;
116 if (bpp == 15)
117 {
118 red = red & 0xf8;
119 green = green & 0xf8;
120 blue = blue & 0xf8;
121 }
122 if (bpp == 16)
123 {
124 red = red & 0xf8;
125 green = green & 0xfc;
126 blue = blue & 0xf8;
127 }
128
129 color.red = 0;
130 color.green = 0;
131 color.blue = 0;
132 color.pixel = 0;
133 gdk_gc_set_foreground( gc, &color );
134
135 for (int j = 0; j < image.GetHeight(); j++)
136 {
137 int start_x = -1;
138 int i;
139 for (i = 0; i < image.GetWidth(); i++)
140 {
141 if ((data[index] == red) &&
142 (data[index+1] == green) &&
143 (data[index+2] == blue))
144 {
145 if (start_x == -1)
146 start_x = i;
147 }
148 else
149 {
150 if (start_x != -1)
151 {
152 gdk_draw_line( m_bitmap, gc, start_x, j, i-1, j );
153 start_x = -1;
154 }
155 }
156 index += 3;
157 }
158 if (start_x != -1)
159 gdk_draw_line( m_bitmap, gc, start_x, j, i, j );
160 }
161
162 gdk_gc_unref( gc );
163
164 return TRUE;
165 }
166
167 bool wxMask::Create( const wxBitmap& bitmap, int paletteIndex )
168 {
169 unsigned char r,g,b;
170 wxPalette *pal = bitmap.GetPalette();
171
172 wxCHECK_MSG( pal, FALSE, wxT("Cannot create mask from bitmap without palette") );
173
174 pal->GetRGB(paletteIndex, &r, &g, &b);
175
176 return Create(bitmap, wxColour(r, g, b));
177 }
178
179 bool wxMask::Create( const wxBitmap& bitmap )
180 {
181 if (m_bitmap)
182 {
183 gdk_bitmap_unref( m_bitmap );
184 m_bitmap = (GdkBitmap*) NULL;
185 }
186
187 if (!bitmap.Ok()) return FALSE;
188
189 wxCHECK_MSG( bitmap.GetBitmap(), FALSE, wxT("Cannot create mask from colour bitmap") );
190
191 m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, bitmap.GetWidth(), bitmap.GetHeight(), 1 );
192
193 if (!m_bitmap) return FALSE;
194
195 GdkGC *gc = gdk_gc_new( m_bitmap );
196
197 gdk_wx_draw_bitmap( m_bitmap, gc, bitmap.GetBitmap(), 0, 0, 0, 0, bitmap.GetWidth(), bitmap.GetHeight() );
198
199 gdk_gc_unref( gc );
200
201 return TRUE;
202 }
203
204 GdkBitmap *wxMask::GetBitmap() const
205 {
206 return m_bitmap;
207 }
208
209 //-----------------------------------------------------------------------------
210 // wxBitmap
211 //-----------------------------------------------------------------------------
212
213 class wxBitmapRefData: public wxObjectRefData
214 {
215 public:
216 wxBitmapRefData();
217 ~wxBitmapRefData();
218
219 GdkPixmap *m_pixmap;
220 GdkBitmap *m_bitmap;
221 wxMask *m_mask;
222 int m_width;
223 int m_height;
224 int m_bpp;
225 wxPalette *m_palette;
226 };
227
228 wxBitmapRefData::wxBitmapRefData()
229 {
230 m_pixmap = (GdkPixmap *) NULL;
231 m_bitmap = (GdkBitmap *) NULL;
232 m_mask = (wxMask *) NULL;
233 m_width = 0;
234 m_height = 0;
235 m_bpp = 0;
236 m_palette = (wxPalette *) NULL;
237 }
238
239 wxBitmapRefData::~wxBitmapRefData()
240 {
241 if (m_pixmap) gdk_pixmap_unref( m_pixmap );
242 if (m_bitmap) gdk_bitmap_unref( m_bitmap );
243 if (m_mask) delete m_mask;
244 if (m_palette) delete m_palette;
245 }
246
247 //-----------------------------------------------------------------------------
248
249 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
250
251 IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxGDIObject)
252
253 wxBitmap::wxBitmap()
254 {
255 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
256 }
257
258 wxBitmap::wxBitmap( int width, int height, int depth )
259 {
260 Create( width, height, depth );
261
262 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
263 }
264
265 bool wxBitmap::Create( int width, int height, int depth )
266 {
267 UnRef();
268
269 wxCHECK_MSG( (width > 0) && (height > 0), FALSE, wxT("invalid bitmap size") )
270
271 GdkVisual *visual = gdk_window_get_visual( wxGetRootWindow()->window );
272 wxASSERT( visual );
273
274 if (depth == -1) depth = visual->depth;
275
276 wxCHECK_MSG( (depth == visual->depth) ||
277 (depth == 1), FALSE, wxT("invalid bitmap depth") )
278
279 m_refData = new wxBitmapRefData();
280 M_BMPDATA->m_mask = (wxMask *) NULL;
281 M_BMPDATA->m_width = width;
282 M_BMPDATA->m_height = height;
283 if (depth == 1)
284 {
285 M_BMPDATA->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 );
286 M_BMPDATA->m_bpp = 1;
287 }
288 else
289 {
290 M_BMPDATA->m_pixmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, depth );
291 M_BMPDATA->m_bpp = visual->depth;
292 }
293
294 return Ok();
295 }
296
297 bool wxBitmap::CreateFromXpm( const char **bits )
298 {
299 wxCHECK_MSG( bits != NULL, FALSE, wxT("invalid bitmap data") )
300
301 GdkVisual *visual = gdk_window_get_visual( wxGetRootWindow()->window );
302 wxASSERT( visual );
303
304 m_refData = new wxBitmapRefData();
305
306 GdkBitmap *mask = (GdkBitmap*) NULL;
307
308 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm_d( wxGetRootWindow()->window, &mask, NULL, (gchar **) bits );
309
310 wxCHECK_MSG( M_BMPDATA->m_pixmap, FALSE, wxT("couldn't create pixmap") );
311
312 if (mask)
313 {
314 M_BMPDATA->m_mask = new wxMask();
315 M_BMPDATA->m_mask->m_bitmap = mask;
316 }
317
318 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
319
320 M_BMPDATA->m_bpp = visual->depth; // ?
321
322 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
323
324 return TRUE;
325 }
326
327 bool wxBitmap::CreateFromImage( const wxImage& image, int depth )
328 {
329 wxCHECK_MSG( image.Ok(), FALSE, wxT("invalid image") )
330 wxCHECK_MSG( depth == -1 || depth == 1, FALSE, wxT("invalid bitmap depth") )
331
332 m_refData = new wxBitmapRefData();
333
334 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
335
336 // ------
337 // convertion to mono bitmap:
338 // ------
339 if (depth == 1)
340 {
341 int width = image.GetWidth();
342 int height = image.GetHeight();
343
344 SetHeight( height );
345 SetWidth( width );
346
347 SetBitmap( gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 ) );
348
349 SetDepth( 1 );
350
351 GdkVisual *visual = gdk_window_get_visual( wxGetRootWindow()->window );
352 wxASSERT( visual );
353
354 // Create picture image
355
356 unsigned char *data_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
357
358 GdkImage *data_image =
359 gdk_image_new_bitmap( visual, data_data, width, height );
360
361 // Create mask image
362
363 GdkImage *mask_image = (GdkImage*) NULL;
364
365 if (image.HasMask())
366 {
367 unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
368
369 mask_image = gdk_image_new_bitmap( visual, mask_data, width, height );
370
371 wxMask *mask = new wxMask();
372 mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 );
373
374 SetMask( mask );
375 }
376
377 int r_mask = image.GetMaskRed();
378 int g_mask = image.GetMaskGreen();
379 int b_mask = image.GetMaskBlue();
380
381 unsigned char* data = image.GetData();
382
383 int index = 0;
384 for (int y = 0; y < height; y++)
385 {
386 for (int x = 0; x < width; x++)
387 {
388 int r = data[index];
389 index++;
390 int g = data[index];
391 index++;
392 int b = data[index];
393 index++;
394
395 if (image.HasMask())
396 {
397 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
398 gdk_image_put_pixel( mask_image, x, y, 1 );
399 else
400 gdk_image_put_pixel( mask_image, x, y, 0 );
401 }
402
403 if ((r == 255) && (b == 255) && (g == 255))
404 gdk_image_put_pixel( data_image, x, y, 1 );
405 else
406 gdk_image_put_pixel( data_image, x, y, 0 );
407
408 } // for
409 } // for
410
411 // Blit picture
412
413 GdkGC *data_gc = gdk_gc_new( GetBitmap() );
414
415 gdk_draw_image( GetBitmap(), data_gc, data_image, 0, 0, 0, 0, width, height );
416
417 gdk_image_destroy( data_image );
418 gdk_gc_unref( data_gc );
419
420 // Blit mask
421
422 if (image.HasMask())
423 {
424 GdkGC *mask_gc = gdk_gc_new( GetMask()->GetBitmap() );
425
426 gdk_draw_image( GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height );
427
428 gdk_image_destroy( mask_image );
429 gdk_gc_unref( mask_gc );
430 }
431 }
432
433 // ------
434 // convertion to colour bitmap:
435 // ------
436 else
437 {
438 int width = image.GetWidth();
439 int height = image.GetHeight();
440
441 SetHeight( height );
442 SetWidth( width );
443
444 SetPixmap( gdk_pixmap_new( wxGetRootWindow()->window, width, height, -1 ) );
445
446 // Retrieve depth
447
448 GdkVisual *visual = gdk_window_get_visual( wxGetRootWindow()->window );
449 wxASSERT( visual );
450
451 int bpp = visual->depth;
452
453 SetDepth( bpp );
454
455 if ((bpp == 16) && (visual->red_mask != 0xf800)) bpp = 15;
456 if (bpp < 8) bpp = 8;
457
458 #if (GTK_MINOR_VERSION > 0)
459
460 if (!image.HasMask() && (bpp > 8))
461 {
462 static bool s_hasInitialized = FALSE;
463
464 if (!s_hasInitialized)
465 {
466 gdk_rgb_init();
467 s_hasInitialized = TRUE;
468 }
469
470 GdkGC *gc = gdk_gc_new( GetPixmap() );
471
472 gdk_draw_rgb_image( GetPixmap(),
473 gc,
474 0, 0,
475 width, height,
476 GDK_RGB_DITHER_NONE,
477 image.GetData(),
478 width*3 );
479
480 gdk_gc_unref( gc );
481 return TRUE;
482 }
483
484 #endif
485
486 // Create picture image
487
488 GdkImage *data_image =
489 gdk_image_new( GDK_IMAGE_FASTEST, visual, width, height );
490
491 // Create mask image
492
493 GdkImage *mask_image = (GdkImage*) NULL;
494
495 if (image.HasMask())
496 {
497 unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
498
499 mask_image = gdk_image_new_bitmap( visual, mask_data, width, height );
500
501 wxMask *mask = new wxMask();
502 mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 );
503
504 SetMask( mask );
505 }
506
507 // Render
508
509 enum byte_order { RGB, RBG, BRG, BGR, GRB, GBR };
510 byte_order b_o = RGB;
511
512 if (bpp >= 24)
513 {
514 if ((visual->red_mask > visual->green_mask) && (visual->green_mask > visual->blue_mask)) b_o = RGB;
515 else if ((visual->red_mask > visual->blue_mask) && (visual->blue_mask > visual->green_mask)) b_o = RGB;
516 else if ((visual->blue_mask > visual->red_mask) && (visual->red_mask > visual->green_mask)) b_o = BRG;
517 else if ((visual->blue_mask > visual->green_mask) && (visual->green_mask > visual->red_mask)) b_o = BGR;
518 else if ((visual->green_mask > visual->red_mask) && (visual->red_mask > visual->blue_mask)) b_o = GRB;
519 else if ((visual->green_mask > visual->blue_mask) && (visual->blue_mask > visual->red_mask)) b_o = GBR;
520 }
521
522 int r_mask = image.GetMaskRed();
523 int g_mask = image.GetMaskGreen();
524 int b_mask = image.GetMaskBlue();
525
526 unsigned char* data = image.GetData();
527
528 int index = 0;
529 for (int y = 0; y < height; y++)
530 {
531 for (int x = 0; x < width; x++)
532 {
533 int r = data[index];
534 index++;
535 int g = data[index];
536 index++;
537 int b = data[index];
538 index++;
539
540 if (image.HasMask())
541 {
542 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
543 gdk_image_put_pixel( mask_image, x, y, 1 );
544 else
545 gdk_image_put_pixel( mask_image, x, y, 0 );
546 }
547
548 switch (bpp)
549 {
550 case 8:
551 {
552 int pixel = -1;
553 if (wxTheApp->m_colorCube)
554 {
555 pixel = wxTheApp->m_colorCube[ ((r & 0xf8) << 7) + ((g & 0xf8) << 2) + ((b & 0xf8) >> 3) ];
556 }
557 else
558 {
559 GdkColormap *cmap = gtk_widget_get_default_colormap();
560 GdkColor *colors = cmap->colors;
561 int max = 3 * (65536);
562
563 for (int i = 0; i < cmap->size; i++)
564 {
565 int rdiff = (r << 8) - colors[i].red;
566 int gdiff = (g << 8) - colors[i].green;
567 int bdiff = (b << 8) - colors[i].blue;
568 int sum = ABS (rdiff) + ABS (gdiff) + ABS (bdiff);
569 if (sum < max) { pixel = i; max = sum; }
570 }
571 }
572
573 gdk_image_put_pixel( data_image, x, y, pixel );
574
575 break;
576 }
577 case 15:
578 {
579 guint32 pixel = ((r & 0xf8) << 7) | ((g & 0xf8) << 2) | ((b & 0xf8) >> 3);
580 gdk_image_put_pixel( data_image, x, y, pixel );
581 break;
582 }
583 case 16:
584 {
585 guint32 pixel = ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | ((b & 0xf8) >> 3);
586 gdk_image_put_pixel( data_image, x, y, pixel );
587 break;
588 }
589 case 32:
590 case 24:
591 {
592 guint32 pixel = 0;
593 switch (b_o)
594 {
595 case RGB: pixel = (r << 16) | (g << 8) | b; break;
596 case RBG: pixel = (r << 16) | (b << 8) | g; break;
597 case BRG: pixel = (b << 16) | (r << 8) | g; break;
598 case BGR: pixel = (b << 16) | (g << 8) | r; break;
599 case GRB: pixel = (g << 16) | (r << 8) | b; break;
600 case GBR: pixel = (g << 16) | (b << 8) | r; break;
601 }
602 gdk_image_put_pixel( data_image, x, y, pixel );
603 }
604 default: break;
605 }
606 } // for
607 } // for
608
609 // Blit picture
610
611 GdkGC *data_gc = gdk_gc_new( GetPixmap() );
612
613 gdk_draw_image( GetPixmap(), data_gc, data_image, 0, 0, 0, 0, width, height );
614
615 gdk_image_destroy( data_image );
616 gdk_gc_unref( data_gc );
617
618 // Blit mask
619
620 if (image.HasMask())
621 {
622 GdkGC *mask_gc = gdk_gc_new( GetMask()->GetBitmap() );
623
624 gdk_draw_image( GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height );
625
626 gdk_image_destroy( mask_image );
627 gdk_gc_unref( mask_gc );
628 }
629 }
630
631 return TRUE;
632 }
633
634 wxImage wxBitmap::ConvertToImage() const
635 {
636 wxImage image;
637
638 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
639
640 GdkImage *gdk_image = (GdkImage*) NULL;
641 if (GetPixmap())
642 {
643 gdk_image = gdk_image_get( GetPixmap(),
644 0, 0,
645 GetWidth(), GetHeight() );
646 } else
647 if (GetBitmap())
648 {
649 gdk_image = gdk_image_get( GetBitmap(),
650 0, 0,
651 GetWidth(), GetHeight() );
652 } else
653 {
654 wxFAIL_MSG( wxT("Ill-formed bitmap") );
655 }
656
657 wxCHECK_MSG( gdk_image, wxNullImage, wxT("couldn't create image") );
658
659 image.Create( GetWidth(), GetHeight() );
660 char unsigned *data = image.GetData();
661
662 if (!data)
663 {
664 gdk_image_destroy( gdk_image );
665 wxFAIL_MSG( wxT("couldn't create image") );
666 return wxNullImage;
667 }
668
669 GdkImage *gdk_image_mask = (GdkImage*) NULL;
670 if (GetMask())
671 {
672 gdk_image_mask = gdk_image_get( GetMask()->GetBitmap(),
673 0, 0,
674 GetWidth(), GetHeight() );
675
676 image.SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
677 }
678
679 int bpp = -1;
680 int red_shift_right = 0;
681 int green_shift_right = 0;
682 int blue_shift_right = 0;
683 int red_shift_left = 0;
684 int green_shift_left = 0;
685 int blue_shift_left = 0;
686 bool use_shift = FALSE;
687
688 if (GetPixmap())
689 {
690 GdkVisual *visual = gdk_window_get_visual( GetPixmap() );
691
692 if (visual == NULL) visual = gdk_window_get_visual( wxGetRootWindow()->window );
693 bpp = visual->depth;
694 if (bpp == 16) bpp = visual->red_prec + visual->green_prec + visual->blue_prec;
695 red_shift_right = visual->red_shift;
696 red_shift_left = 8-visual->red_prec;
697 green_shift_right = visual->green_shift;
698 green_shift_left = 8-visual->green_prec;
699 blue_shift_right = visual->blue_shift;
700 blue_shift_left = 8-visual->blue_prec;
701
702 use_shift = (visual->type == GDK_VISUAL_TRUE_COLOR) || (visual->type == GDK_VISUAL_DIRECT_COLOR);
703 }
704 if (GetBitmap())
705 {
706 bpp = 1;
707 }
708
709
710 GdkColormap *cmap = gtk_widget_get_default_colormap();
711
712 long pos = 0;
713 for (int j = 0; j < GetHeight(); j++)
714 {
715 for (int i = 0; i < GetWidth(); i++)
716 {
717 wxUint32 pixel = gdk_image_get_pixel( gdk_image, i, j );
718 if (bpp == 1)
719 {
720 if (pixel == 0)
721 {
722 data[pos] = 0;
723 data[pos+1] = 0;
724 data[pos+2] = 0;
725 }
726 else
727 {
728 data[pos] = 255;
729 data[pos+1] = 255;
730 data[pos+2] = 255;
731 }
732 }
733 else if (use_shift)
734 {
735 data[pos] = (pixel >> red_shift_right) << red_shift_left;
736 data[pos+1] = (pixel >> green_shift_right) << green_shift_left;
737 data[pos+2] = (pixel >> blue_shift_right) << blue_shift_left;
738 }
739 else if (cmap->colors)
740 {
741 data[pos] = cmap->colors[pixel].red >> 8;
742 data[pos+1] = cmap->colors[pixel].green >> 8;
743 data[pos+2] = cmap->colors[pixel].blue >> 8;
744 }
745 else
746 {
747 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
748 }
749
750 if (gdk_image_mask)
751 {
752 int mask_pixel = gdk_image_get_pixel( gdk_image_mask, i, j );
753 if (mask_pixel == 0)
754 {
755 data[pos] = 16;
756 data[pos+1] = 16;
757 data[pos+2] = 16;
758 }
759 }
760
761 pos += 3;
762 }
763 }
764
765 gdk_image_destroy( gdk_image );
766 if (gdk_image_mask) gdk_image_destroy( gdk_image_mask );
767
768 return image;
769 }
770
771 wxBitmap::wxBitmap( const wxBitmap& bmp )
772 {
773 Ref( bmp );
774
775 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
776 }
777
778 wxBitmap::wxBitmap( const wxString &filename, int type )
779 {
780 LoadFile( filename, type );
781
782 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
783 }
784
785 wxBitmap::wxBitmap( const char bits[], int width, int height, int WXUNUSED(depth))
786 {
787 m_refData = new wxBitmapRefData();
788
789 M_BMPDATA->m_mask = (wxMask *) NULL;
790 M_BMPDATA->m_bitmap =
791 gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) bits, width, height );
792 M_BMPDATA->m_width = width;
793 M_BMPDATA->m_height = height;
794 M_BMPDATA->m_bpp = 1;
795
796 wxCHECK_RET( M_BMPDATA->m_bitmap, wxT("couldn't create bitmap") );
797
798 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
799 }
800
801 wxBitmap::~wxBitmap()
802 {
803 if (wxTheBitmapList) wxTheBitmapList->DeleteObject(this);
804 }
805
806 wxBitmap& wxBitmap::operator = ( const wxBitmap& bmp )
807 {
808 if (*this == bmp) return (*this);
809 Ref( bmp );
810 return *this;
811 }
812
813 bool wxBitmap::operator == ( const wxBitmap& bmp ) const
814 {
815 return m_refData == bmp.m_refData;
816 }
817
818 bool wxBitmap::operator != ( const wxBitmap& bmp ) const
819 {
820 return m_refData != bmp.m_refData;
821 }
822
823 bool wxBitmap::Ok() const
824 {
825 return (m_refData != NULL);
826 }
827
828 int wxBitmap::GetHeight() const
829 {
830 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
831
832 return M_BMPDATA->m_height;
833 }
834
835 int wxBitmap::GetWidth() const
836 {
837 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
838
839 return M_BMPDATA->m_width;
840 }
841
842 int wxBitmap::GetDepth() const
843 {
844 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
845
846 return M_BMPDATA->m_bpp;
847 }
848
849 wxMask *wxBitmap::GetMask() const
850 {
851 wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") );
852
853 return M_BMPDATA->m_mask;
854 }
855
856 void wxBitmap::SetMask( wxMask *mask )
857 {
858 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
859
860 if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask;
861
862 M_BMPDATA->m_mask = mask;
863 }
864
865 wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const
866 {
867 wxCHECK_MSG( Ok() &&
868 (rect.x >= 0) && (rect.y >= 0) &&
869 (rect.x+rect.width <= M_BMPDATA->m_width) && (rect.y+rect.height <= M_BMPDATA->m_height),
870 wxNullBitmap, wxT("invalid bitmap or bitmap region") );
871
872 wxBitmap ret( rect.width, rect.height, M_BMPDATA->m_bpp );
873 wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") );
874
875 if (ret.GetPixmap())
876 {
877 GdkGC *gc = gdk_gc_new( ret.GetPixmap() );
878 gdk_draw_pixmap( ret.GetPixmap(), gc, GetPixmap(), rect.x, rect.y, 0, 0, rect.width, rect.height );
879 gdk_gc_destroy( gc );
880 }
881 else
882 {
883 GdkGC *gc = gdk_gc_new( ret.GetBitmap() );
884 gdk_wx_draw_bitmap( ret.GetBitmap(), gc, GetBitmap(), rect.x, rect.y, 0, 0, rect.width, rect.height );
885 gdk_gc_destroy( gc );
886 }
887
888 if (GetMask())
889 {
890 wxMask *mask = new wxMask;
891 mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, rect.width, rect.height, 1 );
892
893 GdkGC *gc = gdk_gc_new( mask->m_bitmap );
894 gdk_wx_draw_bitmap( mask->m_bitmap, gc, M_BMPDATA->m_mask->m_bitmap, 0, 0, rect.x, rect.y, rect.width, rect.height );
895 gdk_gc_destroy( gc );
896
897 ret.SetMask( mask );
898 }
899
900 return ret;
901 }
902
903 bool wxBitmap::SaveFile( const wxString &name, int type, wxPalette *WXUNUSED(palette) )
904 {
905 wxCHECK_MSG( Ok(), FALSE, wxT("invalid bitmap") );
906
907 // Try to save the bitmap via wxImage handlers:
908 {
909 wxImage image( *this );
910 if (image.Ok()) return image.SaveFile( name, type );
911 }
912
913 return FALSE;
914 }
915
916 bool wxBitmap::LoadFile( const wxString &name, int type )
917 {
918 UnRef();
919
920 if (!wxFileExists(name)) return FALSE;
921
922 GdkVisual *visual = gdk_window_get_visual( wxGetRootWindow()->window );
923 wxASSERT( visual );
924
925 if (type == wxBITMAP_TYPE_XPM)
926 {
927 m_refData = new wxBitmapRefData();
928
929 GdkBitmap *mask = (GdkBitmap*) NULL;
930
931 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm( wxGetRootWindow()->window, &mask, NULL, name.fn_str() );
932
933 if (mask)
934 {
935 M_BMPDATA->m_mask = new wxMask();
936 M_BMPDATA->m_mask->m_bitmap = mask;
937 }
938
939 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
940
941 M_BMPDATA->m_bpp = visual->depth;
942 }
943 else // try if wxImage can load it
944 {
945 wxImage image;
946 if (!image.LoadFile( name, type )) return FALSE;
947 if (image.Ok()) *this = image.ConvertToBitmap();
948 else return FALSE;
949 }
950
951 return TRUE;
952 }
953
954 wxPalette *wxBitmap::GetPalette() const
955 {
956 if (!Ok()) return (wxPalette *) NULL;
957
958 return M_BMPDATA->m_palette;
959 }
960
961 void wxBitmap::SetHeight( int height )
962 {
963 if (!m_refData) m_refData = new wxBitmapRefData();
964
965 M_BMPDATA->m_height = height;
966 }
967
968 void wxBitmap::SetWidth( int width )
969 {
970 if (!m_refData) m_refData = new wxBitmapRefData();
971
972 M_BMPDATA->m_width = width;
973 }
974
975 void wxBitmap::SetDepth( int depth )
976 {
977 if (!m_refData) m_refData = new wxBitmapRefData();
978
979 M_BMPDATA->m_bpp = depth;
980 }
981
982 void wxBitmap::SetPixmap( GdkPixmap *pixmap )
983 {
984 if (!m_refData) m_refData = new wxBitmapRefData();
985
986 M_BMPDATA->m_pixmap = pixmap;
987 }
988
989 void wxBitmap::SetBitmap( GdkPixmap *bitmap )
990 {
991 if (!m_refData) m_refData = new wxBitmapRefData();
992
993 M_BMPDATA->m_bitmap = bitmap;
994 }
995
996 GdkPixmap *wxBitmap::GetPixmap() const
997 {
998 wxCHECK_MSG( Ok(), (GdkPixmap *) NULL, wxT("invalid bitmap") );
999
1000 return M_BMPDATA->m_pixmap;
1001 }
1002
1003 GdkBitmap *wxBitmap::GetBitmap() const
1004 {
1005 wxCHECK_MSG( Ok(), (GdkBitmap *) NULL, wxT("invalid bitmap") );
1006
1007 return M_BMPDATA->m_bitmap;
1008 }