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))
129 green
= green
& 0xf8;
135 green
= green
& 0xfc;
141 green
= green
& 0xf0;
149 gdk_gc_set_foreground( gc
, &color
);
151 for (int j
= 0; j
< image
.GetHeight(); j
++)
155 for (i
= 0; i
< image
.GetWidth(); i
++)
157 if ((data
[index
] == red
) &&
158 (data
[index
+1] == green
) &&
159 (data
[index
+2] == blue
))
168 gdk_draw_line( m_bitmap
, gc
, start_x
, j
, i
-1, j
);
175 gdk_draw_line( m_bitmap
, gc
, start_x
, j
, i
, j
);
183 bool wxMask::Create( const wxBitmap
& bitmap
, int paletteIndex
)
186 wxPalette
*pal
= bitmap
.GetPalette();
188 wxCHECK_MSG( pal
, FALSE
, wxT("Cannot create mask from bitmap without palette") );
190 pal
->GetRGB(paletteIndex
, &r
, &g
, &b
);
192 return Create(bitmap
, wxColour(r
, g
, b
));
195 bool wxMask::Create( const wxBitmap
& bitmap
)
199 gdk_bitmap_unref( m_bitmap
);
200 m_bitmap
= (GdkBitmap
*) NULL
;
203 if (!bitmap
.Ok()) return FALSE
;
205 wxCHECK_MSG( bitmap
.GetBitmap(), FALSE
, wxT("Cannot create mask from colour bitmap") );
207 m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
209 if (!m_bitmap
) return FALSE
;
211 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
213 gdk_wx_draw_bitmap( m_bitmap
, gc
, bitmap
.GetBitmap(), 0, 0, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight() );
220 GdkBitmap
*wxMask::GetBitmap() const
225 //-----------------------------------------------------------------------------
227 //-----------------------------------------------------------------------------
229 class wxBitmapRefData
: public wxObjectRefData
241 wxPalette
*m_palette
;
244 wxBitmapRefData::wxBitmapRefData()
246 m_pixmap
= (GdkPixmap
*) NULL
;
247 m_bitmap
= (GdkBitmap
*) NULL
;
248 m_mask
= (wxMask
*) NULL
;
252 m_palette
= (wxPalette
*) NULL
;
255 wxBitmapRefData::~wxBitmapRefData()
258 gdk_pixmap_unref( m_pixmap
);
260 gdk_bitmap_unref( m_bitmap
);
265 //-----------------------------------------------------------------------------
267 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
269 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
275 wxBitmap::wxBitmap( int width
, int height
, int depth
)
277 Create( width
, height
, depth
);
280 bool wxBitmap::Create( int width
, int height
, int depth
)
284 if ( width
<= 0 || height
<= 0 )
289 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
292 depth
= visual
->depth
;
294 wxCHECK_MSG( (depth
== visual
->depth
) || (depth
== 1), FALSE
,
295 wxT("invalid bitmap depth") )
297 m_refData
= new wxBitmapRefData();
298 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
299 M_BMPDATA
->m_width
= width
;
300 M_BMPDATA
->m_height
= height
;
303 M_BMPDATA
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
304 M_BMPDATA
->m_bpp
= 1;
308 M_BMPDATA
->m_pixmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, depth
);
309 M_BMPDATA
->m_bpp
= visual
->depth
;
315 bool wxBitmap::CreateFromXpm( const char **bits
)
319 wxCHECK_MSG( bits
!= NULL
, FALSE
, wxT("invalid bitmap data") )
321 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
323 m_refData
= new wxBitmapRefData();
325 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
327 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm_d( wxGetRootWindow()->window
, &mask
, NULL
, (gchar
**) bits
);
329 wxCHECK_MSG( M_BMPDATA
->m_pixmap
, FALSE
, wxT("couldn't create pixmap") );
333 M_BMPDATA
->m_mask
= new wxMask();
334 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
337 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
339 M_BMPDATA
->m_bpp
= visual
->depth
; // Can we get a different depth from create_from_xpm_d() ?
344 bool wxBitmap::CreateFromImage( const wxImage
& image
, int depth
)
348 wxCHECK_MSG( image
.Ok(), FALSE
, wxT("invalid image") )
349 wxCHECK_MSG( depth
== -1 || depth
== 1, FALSE
, wxT("invalid bitmap depth") )
351 int width
= image
.GetWidth();
352 int height
= image
.GetHeight();
354 if ( width
<= 0 || height
<= 0 )
359 m_refData
= new wxBitmapRefData();
365 // conversion to mono bitmap:
369 SetBitmap( gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 ) );
373 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
375 // Create picture image
377 unsigned char *data_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
379 GdkImage
*data_image
=
380 gdk_image_new_bitmap( visual
, data_data
, width
, height
);
384 GdkImage
*mask_image
= (GdkImage
*) NULL
;
388 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
390 mask_image
= gdk_image_new_bitmap( visual
, mask_data
, width
, height
);
392 wxMask
*mask
= new wxMask();
393 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
398 int r_mask
= image
.GetMaskRed();
399 int g_mask
= image
.GetMaskGreen();
400 int b_mask
= image
.GetMaskBlue();
402 unsigned char* data
= image
.GetData();
405 for (int y
= 0; y
< height
; y
++)
407 for (int x
= 0; x
< width
; x
++)
418 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
419 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
421 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
424 if ((r
== 255) && (b
== 255) && (g
== 255))
425 gdk_image_put_pixel( data_image
, x
, y
, 1 );
427 gdk_image_put_pixel( data_image
, x
, y
, 0 );
434 GdkGC
*data_gc
= gdk_gc_new( GetBitmap() );
436 gdk_draw_image( GetBitmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
438 gdk_image_destroy( data_image
);
439 gdk_gc_unref( data_gc
);
445 GdkGC
*mask_gc
= gdk_gc_new( GetMask()->GetBitmap() );
447 gdk_draw_image( GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
449 gdk_image_destroy( mask_image
);
450 gdk_gc_unref( mask_gc
);
455 // conversion to colour bitmap:
459 SetPixmap( gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, -1 ) );
461 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
463 int bpp
= visual
->depth
;
467 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800))
472 // We handle 8-bit bitmaps ourselves using the colour cube, 12-bit
473 // visuals are not supported by GDK so we do these ourselves, too.
474 // 15-bit and 16-bit should actually work and 24-bit certainly does.
476 if (!image
.HasMask() && (bpp
> 16))
478 if (!image
.HasMask() && (bpp
> 12))
481 static bool s_hasInitialized
= FALSE
;
483 if (!s_hasInitialized
)
486 s_hasInitialized
= TRUE
;
489 GdkGC
*gc
= gdk_gc_new( GetPixmap() );
491 gdk_draw_rgb_image( GetPixmap(),
503 // Create picture image
505 GdkImage
*data_image
=
506 gdk_image_new( GDK_IMAGE_FASTEST
, visual
, width
, height
);
510 GdkImage
*mask_image
= (GdkImage
*) NULL
;
514 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
516 mask_image
= gdk_image_new_bitmap( visual
, mask_data
, width
, height
);
518 wxMask
*mask
= new wxMask();
519 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
526 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
527 byte_order b_o
= RGB
;
531 if ((visual
->red_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->blue_mask
)) b_o
= RGB
;
532 else if ((visual
->red_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->green_mask
)) b_o
= RBG
;
533 else if ((visual
->blue_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->green_mask
)) b_o
= BRG
;
534 else if ((visual
->blue_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->red_mask
)) b_o
= BGR
;
535 else if ((visual
->green_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->blue_mask
)) b_o
= GRB
;
536 else if ((visual
->green_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->red_mask
)) b_o
= GBR
;
539 int r_mask
= image
.GetMaskRed();
540 int g_mask
= image
.GetMaskGreen();
541 int b_mask
= image
.GetMaskBlue();
543 unsigned char* data
= image
.GetData();
546 for (int y
= 0; y
< height
; y
++)
548 for (int x
= 0; x
< width
; x
++)
559 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
560 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
562 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
570 if (wxTheApp
->m_colorCube
)
572 pixel
= wxTheApp
->m_colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
576 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
577 GdkColor
*colors
= cmap
->colors
;
578 int max
= 3 * (65536);
580 for (int i
= 0; i
< cmap
->size
; i
++)
582 int rdiff
= (r
<< 8) - colors
[i
].red
;
583 int gdiff
= (g
<< 8) - colors
[i
].green
;
584 int bdiff
= (b
<< 8) - colors
[i
].blue
;
585 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
586 if (sum
< max
) { pixel
= i
; max
= sum
; }
590 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
599 case RGB
: pixel
= ((r
& 0xf0) << 4) | (g
& 0xf0) | ((b
& 0xf0) >> 4); break;
600 case RBG
: pixel
= ((r
& 0xf0) << 4) | (b
& 0xf0) | ((g
& 0xf0) >> 4); break;
601 case GRB
: pixel
= ((g
& 0xf0) << 4) | (r
& 0xf0) | ((b
& 0xf0) >> 4); break;
602 case GBR
: pixel
= ((g
& 0xf0) << 4) | (b
& 0xf0) | ((r
& 0xf0) >> 4); break;
603 case BRG
: pixel
= ((b
& 0xf0) << 4) | (r
& 0xf0) | ((g
& 0xf0) >> 4); break;
604 case BGR
: pixel
= ((b
& 0xf0) << 4) | (g
& 0xf0) | ((r
& 0xf0) >> 4); break;
606 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
614 case RGB
: pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
615 case RBG
: pixel
= ((r
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
616 case GRB
: pixel
= ((g
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
617 case GBR
: pixel
= ((g
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
618 case BRG
: pixel
= ((b
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
619 case BGR
: pixel
= ((b
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
621 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
626 // I actually don't know if for 16-bit displays, it is alway the green
627 // component or the second component which has 6 bits.
631 case RGB
: pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
632 case RBG
: pixel
= ((r
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
633 case GRB
: pixel
= ((g
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
634 case GBR
: pixel
= ((g
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
635 case BRG
: pixel
= ((b
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
636 case BGR
: pixel
= ((b
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
638 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
647 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
648 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
649 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
650 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
651 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
652 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
654 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
663 GdkGC
*data_gc
= gdk_gc_new( GetPixmap() );
665 gdk_draw_image( GetPixmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
667 gdk_image_destroy( data_image
);
668 gdk_gc_unref( data_gc
);
674 GdkGC
*mask_gc
= gdk_gc_new( GetMask()->GetBitmap() );
676 gdk_draw_image( GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
678 gdk_image_destroy( mask_image
);
679 gdk_gc_unref( mask_gc
);
686 wxImage
wxBitmap::ConvertToImage() const
688 // the colour used as transparent one in wxImage and the one it is replaced
689 // with when it really occurs in the bitmap
690 static const int MASK_RED
= 1;
691 static const int MASK_GREEN
= 2;
692 static const int MASK_BLUE
= 3;
693 static const int MASK_BLUE_REPLACEMENT
= 2;
697 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
699 GdkImage
*gdk_image
= (GdkImage
*) NULL
;
702 gdk_image
= gdk_image_get( GetPixmap(),
704 GetWidth(), GetHeight() );
706 else if (GetBitmap())
708 gdk_image
= gdk_image_get( GetBitmap(),
710 GetWidth(), GetHeight() );
714 wxFAIL_MSG( wxT("Ill-formed bitmap") );
717 wxCHECK_MSG( gdk_image
, wxNullImage
, wxT("couldn't create image") );
719 image
.Create( GetWidth(), GetHeight() );
720 char unsigned *data
= image
.GetData();
724 gdk_image_destroy( gdk_image
);
725 wxFAIL_MSG( wxT("couldn't create image") );
729 GdkImage
*gdk_image_mask
= (GdkImage
*) NULL
;
732 gdk_image_mask
= gdk_image_get( GetMask()->GetBitmap(),
734 GetWidth(), GetHeight() );
736 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
740 int red_shift_right
= 0;
741 int green_shift_right
= 0;
742 int blue_shift_right
= 0;
743 int red_shift_left
= 0;
744 int green_shift_left
= 0;
745 int blue_shift_left
= 0;
746 bool use_shift
= FALSE
;
750 GdkVisual
*visual
= gdk_window_get_visual( GetPixmap() );
752 visual
= wxTheApp
->GetGdkVisual();
756 bpp
= visual
->red_prec
+ visual
->green_prec
+ visual
->blue_prec
;
757 red_shift_right
= visual
->red_shift
;
758 red_shift_left
= 8-visual
->red_prec
;
759 green_shift_right
= visual
->green_shift
;
760 green_shift_left
= 8-visual
->green_prec
;
761 blue_shift_right
= visual
->blue_shift
;
762 blue_shift_left
= 8-visual
->blue_prec
;
764 use_shift
= (visual
->type
== GDK_VISUAL_TRUE_COLOR
) || (visual
->type
== GDK_VISUAL_DIRECT_COLOR
);
772 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
775 for (int j
= 0; j
< GetHeight(); j
++)
777 for (int i
= 0; i
< GetWidth(); i
++)
779 wxUint32 pixel
= gdk_image_get_pixel( gdk_image
, i
, j
);
797 data
[pos
] = (pixel
>> red_shift_right
) << red_shift_left
;
798 data
[pos
+1] = (pixel
>> green_shift_right
) << green_shift_left
;
799 data
[pos
+2] = (pixel
>> blue_shift_right
) << blue_shift_left
;
801 else if (cmap
->colors
)
803 data
[pos
] = cmap
->colors
[pixel
].red
>> 8;
804 data
[pos
+1] = cmap
->colors
[pixel
].green
>> 8;
805 data
[pos
+2] = cmap
->colors
[pixel
].blue
>> 8;
809 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
814 int mask_pixel
= gdk_image_get_pixel( gdk_image_mask
, i
, j
);
817 data
[pos
] = MASK_RED
;
818 data
[pos
+1] = MASK_GREEN
;
819 data
[pos
+2] = MASK_BLUE
;
821 else if ( data
[pos
] == MASK_RED
&&
822 data
[pos
+1] == MASK_GREEN
&&
823 data
[pos
+2] == MASK_BLUE
)
825 data
[pos
+2] = MASK_BLUE_REPLACEMENT
;
833 gdk_image_destroy( gdk_image
);
834 if (gdk_image_mask
) gdk_image_destroy( gdk_image_mask
);
839 wxBitmap::wxBitmap( const wxBitmap
& bmp
)
845 wxBitmap::wxBitmap( const wxString
&filename
, int type
)
847 LoadFile( filename
, type
);
850 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
))
852 if ( width
> 0 && height
> 0 )
854 m_refData
= new wxBitmapRefData();
856 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
857 M_BMPDATA
->m_bitmap
= gdk_bitmap_create_from_data
859 wxGetRootWindow()->window
,
864 M_BMPDATA
->m_width
= width
;
865 M_BMPDATA
->m_height
= height
;
866 M_BMPDATA
->m_bpp
= 1;
868 wxASSERT_MSG( M_BMPDATA
->m_bitmap
, wxT("couldn't create bitmap") );
872 wxBitmap::~wxBitmap()
876 wxBitmap
& wxBitmap::operator = ( const wxBitmap
& bmp
)
878 if ( m_refData
!= bmp
.m_refData
)
884 bool wxBitmap::operator == ( const wxBitmap
& bmp
) const
886 return m_refData
== bmp
.m_refData
;
889 bool wxBitmap::operator != ( const wxBitmap
& bmp
) const
891 return m_refData
!= bmp
.m_refData
;
894 bool wxBitmap::Ok() const
896 return (m_refData
!= NULL
);
899 int wxBitmap::GetHeight() const
901 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
903 return M_BMPDATA
->m_height
;
906 int wxBitmap::GetWidth() const
908 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
910 return M_BMPDATA
->m_width
;
913 int wxBitmap::GetDepth() const
915 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
917 return M_BMPDATA
->m_bpp
;
920 wxMask
*wxBitmap::GetMask() const
922 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
924 return M_BMPDATA
->m_mask
;
927 void wxBitmap::SetMask( wxMask
*mask
)
929 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
931 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
933 M_BMPDATA
->m_mask
= mask
;
936 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
942 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
945 (rect
.x
>= 0) && (rect
.y
>= 0) &&
946 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) && (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
947 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
949 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
950 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
954 GdkGC
*gc
= gdk_gc_new( ret
.GetPixmap() );
955 gdk_draw_pixmap( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
956 gdk_gc_destroy( gc
);
960 GdkGC
*gc
= gdk_gc_new( ret
.GetBitmap() );
961 gdk_wx_draw_bitmap( ret
.GetBitmap(), gc
, GetBitmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
962 gdk_gc_destroy( gc
);
967 wxMask
*mask
= new wxMask
;
968 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, rect
.width
, rect
.height
, 1 );
970 GdkGC
*gc
= gdk_gc_new( mask
->m_bitmap
);
971 gdk_wx_draw_bitmap( mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, 0, 0, rect
.x
, rect
.y
, rect
.width
, rect
.height
);
972 gdk_gc_destroy( gc
);
980 bool wxBitmap::SaveFile( const wxString
&name
, int type
, wxPalette
*WXUNUSED(palette
) )
982 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid bitmap") );
984 // Try to save the bitmap via wxImage handlers:
986 wxImage image
= ConvertToImage();
987 if (image
.Ok()) return image
.SaveFile( name
, type
);
993 bool wxBitmap::LoadFile( const wxString
&name
, int type
)
997 if (!wxFileExists(name
))
1000 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
1002 if (type
== wxBITMAP_TYPE_XPM
)
1004 m_refData
= new wxBitmapRefData();
1006 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
1008 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm
1010 wxGetRootWindow()->window
,
1018 M_BMPDATA
->m_mask
= new wxMask();
1019 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
1022 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
1024 M_BMPDATA
->m_bpp
= visual
->depth
;
1026 else // try if wxImage can load it
1029 if ( !image
.LoadFile( name
, type
) || !image
.Ok() )
1032 *this = wxBitmap(image
);
1038 wxPalette
*wxBitmap::GetPalette() const
1041 return (wxPalette
*) NULL
;
1043 return M_BMPDATA
->m_palette
;
1046 void wxBitmap::SetHeight( int height
)
1049 m_refData
= new wxBitmapRefData();
1051 M_BMPDATA
->m_height
= height
;
1054 void wxBitmap::SetWidth( int width
)
1057 m_refData
= new wxBitmapRefData();
1059 M_BMPDATA
->m_width
= width
;
1062 void wxBitmap::SetDepth( int depth
)
1065 m_refData
= new wxBitmapRefData();
1067 M_BMPDATA
->m_bpp
= depth
;
1070 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
1073 m_refData
= new wxBitmapRefData();
1075 M_BMPDATA
->m_pixmap
= pixmap
;
1078 void wxBitmap::SetBitmap( GdkPixmap
*bitmap
)
1081 m_refData
= new wxBitmapRefData();
1083 M_BMPDATA
->m_bitmap
= bitmap
;
1086 GdkPixmap
*wxBitmap::GetPixmap() const
1088 wxCHECK_MSG( Ok(), (GdkPixmap
*) NULL
, wxT("invalid bitmap") );
1090 return M_BMPDATA
->m_pixmap
;
1093 GdkBitmap
*wxBitmap::GetBitmap() const
1095 wxCHECK_MSG( Ok(), (GdkBitmap
*) NULL
, wxT("invalid bitmap") );
1097 return M_BMPDATA
->m_bitmap
;