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