]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/bitmap.cpp
1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "bitmap.h"
16 #include "wx/palette.h"
17 #include "wx/bitmap.h"
19 #include "wx/filefn.h"
21 #include "wx/dcmemory.h"
25 // need this to get gdk_image_new_bitmap()
26 #define GDK_ENABLE_BROKEN
34 #include <gdk/gdkimage.h>
36 #include <gdk/gdkrgb.h>
37 #endif // GTK+ 2.0/1.2
39 extern void gdk_wx_draw_bitmap (GdkDrawable
*drawable
,
49 //-----------------------------------------------------------------------------
51 //-----------------------------------------------------------------------------
53 extern GtkWidget
*wxGetRootWindow();
55 //-----------------------------------------------------------------------------
57 //-----------------------------------------------------------------------------
59 IMPLEMENT_DYNAMIC_CLASS(wxMask
,wxObject
)
63 m_bitmap
= (GdkBitmap
*) NULL
;
66 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
68 m_bitmap
= (GdkBitmap
*) NULL
;
69 Create( bitmap
, colour
);
72 wxMask::wxMask( const wxBitmap
& bitmap
, int paletteIndex
)
74 m_bitmap
= (GdkBitmap
*) NULL
;
75 Create( bitmap
, paletteIndex
);
78 wxMask::wxMask( const wxBitmap
& bitmap
)
80 m_bitmap
= (GdkBitmap
*) NULL
;
87 gdk_bitmap_unref( m_bitmap
);
90 bool wxMask::Create( const wxBitmap
& bitmap
,
91 const wxColour
& colour
)
95 gdk_bitmap_unref( m_bitmap
);
96 m_bitmap
= (GdkBitmap
*) NULL
;
99 wxImage image
= bitmap
.ConvertToImage();
100 if (!image
.Ok()) return FALSE
;
102 m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, image
.GetWidth(), image
.GetHeight(), 1 );
103 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
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() );
114 unsigned char *data
= image
.GetData();
117 unsigned char red
= colour
.Red();
118 unsigned char green
= colour
.Green();
119 unsigned char blue
= colour
.Blue();
121 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
123 int bpp
= visual
->depth
;
124 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800)) bpp
= 15;
128 green
= green
& 0xf8;
134 green
= green
& 0xfc;
140 green
= green
& 0xf0;
148 gdk_gc_set_foreground( gc
, &color
);
150 for (int j
= 0; j
< image
.GetHeight(); j
++)
154 for (i
= 0; i
< image
.GetWidth(); i
++)
156 if ((data
[index
] == red
) &&
157 (data
[index
+1] == green
) &&
158 (data
[index
+2] == blue
))
167 gdk_draw_line( m_bitmap
, gc
, start_x
, j
, i
-1, j
);
174 gdk_draw_line( m_bitmap
, gc
, start_x
, j
, i
, j
);
182 bool wxMask::Create( const wxBitmap
& bitmap
, int paletteIndex
)
185 wxPalette
*pal
= bitmap
.GetPalette();
187 wxCHECK_MSG( pal
, FALSE
, wxT("Cannot create mask from bitmap without palette") );
189 pal
->GetRGB(paletteIndex
, &r
, &g
, &b
);
191 return Create(bitmap
, wxColour(r
, g
, b
));
194 bool wxMask::Create( const wxBitmap
& bitmap
)
198 gdk_bitmap_unref( m_bitmap
);
199 m_bitmap
= (GdkBitmap
*) NULL
;
202 if (!bitmap
.Ok()) return FALSE
;
204 wxCHECK_MSG( bitmap
.GetBitmap(), FALSE
, wxT("Cannot create mask from colour bitmap") );
206 m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
208 if (!m_bitmap
) return FALSE
;
210 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
212 gdk_wx_draw_bitmap( m_bitmap
, gc
, bitmap
.GetBitmap(), 0, 0, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight() );
219 GdkBitmap
*wxMask::GetBitmap() const
224 //-----------------------------------------------------------------------------
226 //-----------------------------------------------------------------------------
228 class wxBitmapRefData
: public wxObjectRefData
240 wxPalette
*m_palette
;
243 wxBitmapRefData::wxBitmapRefData()
245 m_pixmap
= (GdkPixmap
*) NULL
;
246 m_bitmap
= (GdkBitmap
*) NULL
;
247 m_mask
= (wxMask
*) NULL
;
251 m_palette
= (wxPalette
*) NULL
;
254 wxBitmapRefData::~wxBitmapRefData()
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
;
262 //-----------------------------------------------------------------------------
264 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
266 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
272 wxBitmap::wxBitmap( int width
, int height
, int depth
)
274 Create( width
, height
, depth
);
277 bool wxBitmap::Create( int width
, int height
, int depth
)
281 wxCHECK_MSG( (width
> 0) && (height
> 0), FALSE
, wxT("invalid bitmap size") )
283 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
285 if (depth
== -1) depth
= visual
->depth
;
287 wxCHECK_MSG( (depth
== visual
->depth
) ||
288 (depth
== 1), FALSE
, wxT("invalid bitmap depth") )
290 m_refData
= new wxBitmapRefData();
291 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
292 M_BMPDATA
->m_width
= width
;
293 M_BMPDATA
->m_height
= height
;
296 M_BMPDATA
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
297 M_BMPDATA
->m_bpp
= 1;
301 M_BMPDATA
->m_pixmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, depth
);
302 M_BMPDATA
->m_bpp
= visual
->depth
;
308 bool wxBitmap::CreateFromXpm( const char **bits
)
312 wxCHECK_MSG( bits
!= NULL
, FALSE
, wxT("invalid bitmap data") )
314 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
316 m_refData
= new wxBitmapRefData();
318 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
320 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm_d( wxGetRootWindow()->window
, &mask
, NULL
, (gchar
**) bits
);
322 wxCHECK_MSG( M_BMPDATA
->m_pixmap
, FALSE
, wxT("couldn't create pixmap") );
326 M_BMPDATA
->m_mask
= new wxMask();
327 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
330 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
332 M_BMPDATA
->m_bpp
= visual
->depth
; // ?
337 bool wxBitmap::CreateFromImage( const wxImage
& image
, int depth
)
341 wxCHECK_MSG( image
.Ok(), FALSE
, wxT("invalid image") )
342 wxCHECK_MSG( depth
== -1 || depth
== 1, FALSE
, wxT("invalid bitmap depth") )
344 m_refData
= new wxBitmapRefData();
347 // convertion to mono bitmap:
351 int width
= image
.GetWidth();
352 int height
= image
.GetHeight();
357 SetBitmap( gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 ) );
361 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
363 // Create picture image
365 unsigned char *data_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
367 GdkImage
*data_image
=
368 gdk_image_new_bitmap( visual
, data_data
, width
, height
);
372 GdkImage
*mask_image
= (GdkImage
*) NULL
;
376 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
378 mask_image
= gdk_image_new_bitmap( visual
, mask_data
, width
, height
);
380 wxMask
*mask
= new wxMask();
381 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
386 int r_mask
= image
.GetMaskRed();
387 int g_mask
= image
.GetMaskGreen();
388 int b_mask
= image
.GetMaskBlue();
390 unsigned char* data
= image
.GetData();
393 for (int y
= 0; y
< height
; y
++)
395 for (int x
= 0; x
< width
; x
++)
406 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
407 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
409 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
412 if ((r
== 255) && (b
== 255) && (g
== 255))
413 gdk_image_put_pixel( data_image
, x
, y
, 1 );
415 gdk_image_put_pixel( data_image
, x
, y
, 0 );
422 GdkGC
*data_gc
= gdk_gc_new( GetBitmap() );
424 gdk_draw_image( GetBitmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
426 gdk_image_destroy( data_image
);
427 gdk_gc_unref( data_gc
);
433 GdkGC
*mask_gc
= gdk_gc_new( GetMask()->GetBitmap() );
435 gdk_draw_image( GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
437 gdk_image_destroy( mask_image
);
438 gdk_gc_unref( mask_gc
);
443 // convertion to colour bitmap:
447 int width
= image
.GetWidth();
448 int height
= image
.GetHeight();
453 SetPixmap( gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, -1 ) );
455 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
457 int bpp
= visual
->depth
;
461 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800)) bpp
= 15;
462 if (bpp
< 8) bpp
= 8;
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.
468 if (!image
.HasMask() && (bpp
> 16))
470 if (!image
.HasMask() && (bpp
> 12))
473 static bool s_hasInitialized
= FALSE
;
475 if (!s_hasInitialized
)
478 s_hasInitialized
= TRUE
;
481 GdkGC
*gc
= gdk_gc_new( GetPixmap() );
483 gdk_draw_rgb_image( GetPixmap(),
495 // Create picture image
497 GdkImage
*data_image
=
498 gdk_image_new( GDK_IMAGE_FASTEST
, visual
, width
, height
);
502 GdkImage
*mask_image
= (GdkImage
*) NULL
;
506 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
508 mask_image
= gdk_image_new_bitmap( visual
, mask_data
, width
, height
);
510 wxMask
*mask
= new wxMask();
511 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
518 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
519 byte_order b_o
= RGB
;
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
;
531 int r_mask
= image
.GetMaskRed();
532 int g_mask
= image
.GetMaskGreen();
533 int b_mask
= image
.GetMaskBlue();
535 unsigned char* data
= image
.GetData();
538 for (int y
= 0; y
< height
; y
++)
540 for (int x
= 0; x
< width
; x
++)
551 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
552 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
554 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
562 if (wxTheApp
->m_colorCube
)
564 pixel
= wxTheApp
->m_colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
568 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
569 GdkColor
*colors
= cmap
->colors
;
570 int max
= 3 * (65536);
572 for (int i
= 0; i
< cmap
->size
; i
++)
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
; }
582 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
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;
598 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
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;
613 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
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.
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;
630 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
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;
646 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
655 GdkGC
*data_gc
= gdk_gc_new( GetPixmap() );
657 gdk_draw_image( GetPixmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
659 gdk_image_destroy( data_image
);
660 gdk_gc_unref( data_gc
);
666 GdkGC
*mask_gc
= gdk_gc_new( GetMask()->GetBitmap() );
668 gdk_draw_image( GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
670 gdk_image_destroy( mask_image
);
671 gdk_gc_unref( mask_gc
);
678 wxImage
wxBitmap::ConvertToImage() const
682 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
684 GdkImage
*gdk_image
= (GdkImage
*) NULL
;
687 gdk_image
= gdk_image_get( GetPixmap(),
689 GetWidth(), GetHeight() );
693 gdk_image
= gdk_image_get( GetBitmap(),
695 GetWidth(), GetHeight() );
698 wxFAIL_MSG( wxT("Ill-formed bitmap") );
701 wxCHECK_MSG( gdk_image
, wxNullImage
, wxT("couldn't create image") );
703 image
.Create( GetWidth(), GetHeight() );
704 char unsigned *data
= image
.GetData();
708 gdk_image_destroy( gdk_image
);
709 wxFAIL_MSG( wxT("couldn't create image") );
713 GdkImage
*gdk_image_mask
= (GdkImage
*) NULL
;
716 gdk_image_mask
= gdk_image_get( GetMask()->GetBitmap(),
718 GetWidth(), GetHeight() );
720 image
.SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
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
;
734 GdkVisual
*visual
= gdk_window_get_visual( GetPixmap() );
736 visual
= wxTheApp
->GetGdkVisual();
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
;
747 use_shift
= (visual
->type
== GDK_VISUAL_TRUE_COLOR
) || (visual
->type
== GDK_VISUAL_DIRECT_COLOR
);
755 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
758 for (int j
= 0; j
< GetHeight(); j
++)
760 for (int i
= 0; i
< GetWidth(); i
++)
762 wxUint32 pixel
= gdk_image_get_pixel( gdk_image
, i
, j
);
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
;
784 else if (cmap
->colors
)
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;
792 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
797 int mask_pixel
= gdk_image_get_pixel( gdk_image_mask
, i
, j
);
810 gdk_image_destroy( gdk_image
);
811 if (gdk_image_mask
) gdk_image_destroy( gdk_image_mask
);
816 wxBitmap::wxBitmap( const wxBitmap
& bmp
)
821 wxBitmap::wxBitmap( const wxString
&filename
, int type
)
823 LoadFile( filename
, type
);
826 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
))
828 m_refData
= new wxBitmapRefData();
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;
837 wxCHECK_RET( M_BMPDATA
->m_bitmap
, wxT("couldn't create bitmap") );
840 wxBitmap::~wxBitmap()
844 wxBitmap
& wxBitmap::operator = ( const wxBitmap
& bmp
)
846 if ( m_refData
!= bmp
.m_refData
)
852 bool wxBitmap::operator == ( const wxBitmap
& bmp
) const
854 return m_refData
== bmp
.m_refData
;
857 bool wxBitmap::operator != ( const wxBitmap
& bmp
) const
859 return m_refData
!= bmp
.m_refData
;
862 bool wxBitmap::Ok() const
864 return (m_refData
!= NULL
);
867 int wxBitmap::GetHeight() const
869 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
871 return M_BMPDATA
->m_height
;
874 int wxBitmap::GetWidth() const
876 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
878 return M_BMPDATA
->m_width
;
881 int wxBitmap::GetDepth() const
883 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
885 return M_BMPDATA
->m_bpp
;
888 wxMask
*wxBitmap::GetMask() const
890 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
892 return M_BMPDATA
->m_mask
;
895 void wxBitmap::SetMask( wxMask
*mask
)
897 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
899 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
901 M_BMPDATA
->m_mask
= mask
;
904 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
910 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
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") );
917 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
918 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
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
);
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
);
935 wxMask
*mask
= new wxMask
;
936 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, rect
.width
, rect
.height
, 1 );
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
);
948 bool wxBitmap::SaveFile( const wxString
&name
, int type
, wxPalette
*WXUNUSED(palette
) )
950 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid bitmap") );
952 // Try to save the bitmap via wxImage handlers:
954 wxImage image
= ConvertToImage();
955 if (image
.Ok()) return image
.SaveFile( name
, type
);
961 bool wxBitmap::LoadFile( const wxString
&name
, int type
)
965 if (!wxFileExists(name
)) return FALSE
;
967 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
969 if (type
== wxBITMAP_TYPE_XPM
)
971 m_refData
= new wxBitmapRefData();
973 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
975 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm( wxGetRootWindow()->window
, &mask
, NULL
, name
.fn_str() );
979 M_BMPDATA
->m_mask
= new wxMask();
980 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
983 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
985 M_BMPDATA
->m_bpp
= visual
->depth
;
987 else // try if wxImage can load it
990 if (!image
.LoadFile( name
, type
)) return FALSE
;
992 *this = wxBitmap(image
);
999 wxPalette
*wxBitmap::GetPalette() const
1001 if (!Ok()) return (wxPalette
*) NULL
;
1003 return M_BMPDATA
->m_palette
;
1006 void wxBitmap::SetHeight( int height
)
1008 if (!m_refData
) m_refData
= new wxBitmapRefData();
1010 M_BMPDATA
->m_height
= height
;
1013 void wxBitmap::SetWidth( int width
)
1015 if (!m_refData
) m_refData
= new wxBitmapRefData();
1017 M_BMPDATA
->m_width
= width
;
1020 void wxBitmap::SetDepth( int depth
)
1022 if (!m_refData
) m_refData
= new wxBitmapRefData();
1024 M_BMPDATA
->m_bpp
= depth
;
1027 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
1029 if (!m_refData
) m_refData
= new wxBitmapRefData();
1031 M_BMPDATA
->m_pixmap
= pixmap
;
1034 void wxBitmap::SetBitmap( GdkPixmap
*bitmap
)
1036 if (!m_refData
) m_refData
= new wxBitmapRefData();
1038 M_BMPDATA
->m_bitmap
= bitmap
;
1041 GdkPixmap
*wxBitmap::GetPixmap() const
1043 wxCHECK_MSG( Ok(), (GdkPixmap
*) NULL
, wxT("invalid bitmap") );
1045 return M_BMPDATA
->m_pixmap
;
1048 GdkBitmap
*wxBitmap::GetBitmap() const
1050 wxCHECK_MSG( Ok(), (GdkBitmap
*) NULL
, wxT("invalid bitmap") );
1052 return M_BMPDATA
->m_bitmap
;