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