]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk1/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 // VZ: is this still needed? seems to compile fine without it...
37 #if (GTK_MINOR_VERSION > 0)
38 #include <gdk/gdkrgb.h>
40 #endif // GTK+ 2.0/1.2
42 extern void gdk_wx_draw_bitmap (GdkDrawable
*drawable
,
52 //-----------------------------------------------------------------------------
54 //-----------------------------------------------------------------------------
56 extern GtkWidget
*wxGetRootWindow();
58 //-----------------------------------------------------------------------------
60 //-----------------------------------------------------------------------------
62 IMPLEMENT_DYNAMIC_CLASS(wxMask
,wxObject
)
66 m_bitmap
= (GdkBitmap
*) NULL
;
69 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
71 m_bitmap
= (GdkBitmap
*) NULL
;
72 Create( bitmap
, colour
);
75 wxMask::wxMask( const wxBitmap
& bitmap
, int paletteIndex
)
77 m_bitmap
= (GdkBitmap
*) NULL
;
78 Create( bitmap
, paletteIndex
);
81 wxMask::wxMask( const wxBitmap
& bitmap
)
83 m_bitmap
= (GdkBitmap
*) NULL
;
90 gdk_bitmap_unref( m_bitmap
);
93 bool wxMask::Create( const wxBitmap
& bitmap
,
94 const wxColour
& colour
)
98 gdk_bitmap_unref( m_bitmap
);
99 m_bitmap
= (GdkBitmap
*) NULL
;
102 wxImage
image( bitmap
);
103 if (!image
.Ok()) return FALSE
;
105 m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, image
.GetWidth(), image
.GetHeight(), 1 );
106 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
113 gdk_gc_set_foreground( gc
, &color
);
114 gdk_gc_set_fill( gc
, GDK_SOLID
);
115 gdk_draw_rectangle( m_bitmap
, gc
, TRUE
, 0, 0, image
.GetWidth(), image
.GetHeight() );
117 unsigned char *data
= image
.GetData();
120 unsigned char red
= colour
.Red();
121 unsigned char green
= colour
.Green();
122 unsigned char blue
= colour
.Blue();
124 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
126 int bpp
= visual
->depth
;
127 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800)) bpp
= 15;
131 green
= green
& 0xf8;
137 green
= green
& 0xfc;
143 green
= green
& 0xf0;
151 gdk_gc_set_foreground( gc
, &color
);
153 for (int j
= 0; j
< image
.GetHeight(); j
++)
157 for (i
= 0; i
< image
.GetWidth(); i
++)
159 if ((data
[index
] == red
) &&
160 (data
[index
+1] == green
) &&
161 (data
[index
+2] == blue
))
170 gdk_draw_line( m_bitmap
, gc
, start_x
, j
, i
-1, j
);
177 gdk_draw_line( m_bitmap
, gc
, start_x
, j
, i
, j
);
185 bool wxMask::Create( const wxBitmap
& bitmap
, int paletteIndex
)
188 wxPalette
*pal
= bitmap
.GetPalette();
190 wxCHECK_MSG( pal
, FALSE
, wxT("Cannot create mask from bitmap without palette") );
192 pal
->GetRGB(paletteIndex
, &r
, &g
, &b
);
194 return Create(bitmap
, wxColour(r
, g
, b
));
197 bool wxMask::Create( const wxBitmap
& bitmap
)
201 gdk_bitmap_unref( m_bitmap
);
202 m_bitmap
= (GdkBitmap
*) NULL
;
205 if (!bitmap
.Ok()) return FALSE
;
207 wxCHECK_MSG( bitmap
.GetBitmap(), FALSE
, wxT("Cannot create mask from colour bitmap") );
209 m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
211 if (!m_bitmap
) return FALSE
;
213 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
215 gdk_wx_draw_bitmap( m_bitmap
, gc
, bitmap
.GetBitmap(), 0, 0, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight() );
222 GdkBitmap
*wxMask::GetBitmap() const
227 //-----------------------------------------------------------------------------
229 //-----------------------------------------------------------------------------
231 class wxBitmapRefData
: public wxObjectRefData
243 wxPalette
*m_palette
;
246 wxBitmapRefData::wxBitmapRefData()
248 m_pixmap
= (GdkPixmap
*) NULL
;
249 m_bitmap
= (GdkBitmap
*) NULL
;
250 m_mask
= (wxMask
*) NULL
;
254 m_palette
= (wxPalette
*) NULL
;
257 wxBitmapRefData::~wxBitmapRefData()
259 if (m_pixmap
) gdk_pixmap_unref( m_pixmap
);
260 if (m_bitmap
) gdk_bitmap_unref( m_bitmap
);
261 if (m_mask
) delete m_mask
;
262 if (m_palette
) delete m_palette
;
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 wxCHECK_MSG( (width
> 0) && (height
> 0), FALSE
, wxT("invalid bitmap size") )
286 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
288 if (depth
== -1) depth
= visual
->depth
;
290 wxCHECK_MSG( (depth
== visual
->depth
) ||
291 (depth
== 1), FALSE
, wxT("invalid bitmap depth") )
293 m_refData
= new wxBitmapRefData();
294 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
295 M_BMPDATA
->m_width
= width
;
296 M_BMPDATA
->m_height
= height
;
299 M_BMPDATA
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
300 M_BMPDATA
->m_bpp
= 1;
304 M_BMPDATA
->m_pixmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, depth
);
305 M_BMPDATA
->m_bpp
= visual
->depth
;
311 bool wxBitmap::CreateFromXpm( const char **bits
)
315 wxCHECK_MSG( bits
!= NULL
, FALSE
, wxT("invalid bitmap data") )
317 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
319 m_refData
= new wxBitmapRefData();
321 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
323 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm_d( wxGetRootWindow()->window
, &mask
, NULL
, (gchar
**) bits
);
325 wxCHECK_MSG( M_BMPDATA
->m_pixmap
, FALSE
, wxT("couldn't create pixmap") );
329 M_BMPDATA
->m_mask
= new wxMask();
330 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
333 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
335 M_BMPDATA
->m_bpp
= visual
->depth
; // ?
340 bool wxBitmap::CreateFromImage( const wxImage
& image
, int depth
)
344 wxCHECK_MSG( image
.Ok(), FALSE
, wxT("invalid image") )
345 wxCHECK_MSG( depth
== -1 || depth
== 1, FALSE
, wxT("invalid bitmap depth") )
347 m_refData
= new wxBitmapRefData();
350 // convertion to mono bitmap:
354 int width
= image
.GetWidth();
355 int height
= image
.GetHeight();
360 SetBitmap( gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 ) );
364 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
366 // Create picture image
368 unsigned char *data_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
370 GdkImage
*data_image
=
371 gdk_image_new_bitmap( visual
, data_data
, width
, height
);
375 GdkImage
*mask_image
= (GdkImage
*) NULL
;
379 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
381 mask_image
= gdk_image_new_bitmap( visual
, mask_data
, width
, height
);
383 wxMask
*mask
= new wxMask();
384 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
389 int r_mask
= image
.GetMaskRed();
390 int g_mask
= image
.GetMaskGreen();
391 int b_mask
= image
.GetMaskBlue();
393 unsigned char* data
= image
.GetData();
396 for (int y
= 0; y
< height
; y
++)
398 for (int x
= 0; x
< width
; x
++)
409 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
410 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
412 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
415 if ((r
== 255) && (b
== 255) && (g
== 255))
416 gdk_image_put_pixel( data_image
, x
, y
, 1 );
418 gdk_image_put_pixel( data_image
, x
, y
, 0 );
425 GdkGC
*data_gc
= gdk_gc_new( GetBitmap() );
427 gdk_draw_image( GetBitmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
429 gdk_image_destroy( data_image
);
430 gdk_gc_unref( data_gc
);
436 GdkGC
*mask_gc
= gdk_gc_new( GetMask()->GetBitmap() );
438 gdk_draw_image( GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
440 gdk_image_destroy( mask_image
);
441 gdk_gc_unref( mask_gc
);
446 // convertion to colour bitmap:
450 int width
= image
.GetWidth();
451 int height
= image
.GetHeight();
456 SetPixmap( gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, -1 ) );
458 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
460 int bpp
= visual
->depth
;
464 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800)) bpp
= 15;
465 if (bpp
< 8) bpp
= 8;
467 // We handle 8-bit bitmaps ourselves using the colour cube, 12-bit
468 // visuals are not supported by GDK so we do these ourselves, too.
469 // 15-bit and 16-bit should actually work and 24-bit certainly does.
471 if (!image
.HasMask() && (bpp
> 16))
473 if (!image
.HasMask() && (bpp
> 12))
476 static bool s_hasInitialized
= FALSE
;
478 if (!s_hasInitialized
)
481 s_hasInitialized
= TRUE
;
484 GdkGC
*gc
= gdk_gc_new( GetPixmap() );
486 gdk_draw_rgb_image( GetPixmap(),
498 // Create picture image
500 GdkImage
*data_image
=
501 gdk_image_new( GDK_IMAGE_FASTEST
, visual
, width
, height
);
505 GdkImage
*mask_image
= (GdkImage
*) NULL
;
509 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
511 mask_image
= gdk_image_new_bitmap( visual
, mask_data
, width
, height
);
513 wxMask
*mask
= new wxMask();
514 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
521 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
522 byte_order b_o
= RGB
;
526 if ((visual
->red_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->blue_mask
)) b_o
= RGB
;
527 else if ((visual
->red_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->green_mask
)) b_o
= RBG
;
528 else if ((visual
->blue_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->green_mask
)) b_o
= BRG
;
529 else if ((visual
->blue_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->red_mask
)) b_o
= BGR
;
530 else if ((visual
->green_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->blue_mask
)) b_o
= GRB
;
531 else if ((visual
->green_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->red_mask
)) b_o
= GBR
;
534 int r_mask
= image
.GetMaskRed();
535 int g_mask
= image
.GetMaskGreen();
536 int b_mask
= image
.GetMaskBlue();
538 unsigned char* data
= image
.GetData();
541 for (int y
= 0; y
< height
; y
++)
543 for (int x
= 0; x
< width
; x
++)
554 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
555 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
557 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
565 if (wxTheApp
->m_colorCube
)
567 pixel
= wxTheApp
->m_colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
571 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
572 GdkColor
*colors
= cmap
->colors
;
573 int max
= 3 * (65536);
575 for (int i
= 0; i
< cmap
->size
; i
++)
577 int rdiff
= (r
<< 8) - colors
[i
].red
;
578 int gdiff
= (g
<< 8) - colors
[i
].green
;
579 int bdiff
= (b
<< 8) - colors
[i
].blue
;
580 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
581 if (sum
< max
) { pixel
= i
; max
= sum
; }
585 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
594 case RGB
: pixel
= ((r
& 0xf0) << 4) | (g
& 0xf0) | ((b
& 0xf0) >> 4); break;
595 case RBG
: pixel
= ((r
& 0xf0) << 4) | (b
& 0xf0) | ((g
& 0xf0) >> 4); break;
596 case GRB
: pixel
= ((g
& 0xf0) << 4) | (r
& 0xf0) | ((b
& 0xf0) >> 4); break;
597 case GBR
: pixel
= ((g
& 0xf0) << 4) | (b
& 0xf0) | ((r
& 0xf0) >> 4); break;
598 case BRG
: pixel
= ((b
& 0xf0) << 4) | (r
& 0xf0) | ((g
& 0xf0) >> 4); break;
599 case BGR
: pixel
= ((b
& 0xf0) << 4) | (g
& 0xf0) | ((r
& 0xf0) >> 4); break;
601 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
609 case RGB
: pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
610 case RBG
: pixel
= ((r
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
611 case GRB
: pixel
= ((g
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
612 case GBR
: pixel
= ((g
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
613 case BRG
: pixel
= ((b
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
614 case BGR
: pixel
= ((b
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
616 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
621 // I actually don't know if for 16-bit displays, it is alway the green
622 // component or the second component which has 6 bits.
626 case RGB
: pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
627 case RBG
: pixel
= ((r
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
628 case GRB
: pixel
= ((g
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
629 case GBR
: pixel
= ((g
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
630 case BRG
: pixel
= ((b
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
631 case BGR
: pixel
= ((b
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
633 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
642 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
643 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
644 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
645 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
646 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
647 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
649 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
658 GdkGC
*data_gc
= gdk_gc_new( GetPixmap() );
660 gdk_draw_image( GetPixmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
662 gdk_image_destroy( data_image
);
663 gdk_gc_unref( data_gc
);
669 GdkGC
*mask_gc
= gdk_gc_new( GetMask()->GetBitmap() );
671 gdk_draw_image( GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
673 gdk_image_destroy( mask_image
);
674 gdk_gc_unref( mask_gc
);
681 wxImage
wxBitmap::ConvertToImage() const
685 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
687 GdkImage
*gdk_image
= (GdkImage
*) NULL
;
690 gdk_image
= gdk_image_get( GetPixmap(),
692 GetWidth(), GetHeight() );
696 gdk_image
= gdk_image_get( GetBitmap(),
698 GetWidth(), GetHeight() );
701 wxFAIL_MSG( wxT("Ill-formed bitmap") );
704 wxCHECK_MSG( gdk_image
, wxNullImage
, wxT("couldn't create image") );
706 image
.Create( GetWidth(), GetHeight() );
707 char unsigned *data
= image
.GetData();
711 gdk_image_destroy( gdk_image
);
712 wxFAIL_MSG( wxT("couldn't create image") );
716 GdkImage
*gdk_image_mask
= (GdkImage
*) NULL
;
719 gdk_image_mask
= gdk_image_get( GetMask()->GetBitmap(),
721 GetWidth(), GetHeight() );
723 image
.SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
727 int red_shift_right
= 0;
728 int green_shift_right
= 0;
729 int blue_shift_right
= 0;
730 int red_shift_left
= 0;
731 int green_shift_left
= 0;
732 int blue_shift_left
= 0;
733 bool use_shift
= FALSE
;
737 GdkVisual
*visual
= gdk_window_get_visual( GetPixmap() );
739 visual
= wxTheApp
->GetGdkVisual();
742 if (bpp
== 16) bpp
= visual
->red_prec
+ visual
->green_prec
+ visual
->blue_prec
;
743 red_shift_right
= visual
->red_shift
;
744 red_shift_left
= 8-visual
->red_prec
;
745 green_shift_right
= visual
->green_shift
;
746 green_shift_left
= 8-visual
->green_prec
;
747 blue_shift_right
= visual
->blue_shift
;
748 blue_shift_left
= 8-visual
->blue_prec
;
750 use_shift
= (visual
->type
== GDK_VISUAL_TRUE_COLOR
) || (visual
->type
== GDK_VISUAL_DIRECT_COLOR
);
758 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
761 for (int j
= 0; j
< GetHeight(); j
++)
763 for (int i
= 0; i
< GetWidth(); i
++)
765 wxUint32 pixel
= gdk_image_get_pixel( gdk_image
, i
, j
);
783 data
[pos
] = (pixel
>> red_shift_right
) << red_shift_left
;
784 data
[pos
+1] = (pixel
>> green_shift_right
) << green_shift_left
;
785 data
[pos
+2] = (pixel
>> blue_shift_right
) << blue_shift_left
;
787 else if (cmap
->colors
)
789 data
[pos
] = cmap
->colors
[pixel
].red
>> 8;
790 data
[pos
+1] = cmap
->colors
[pixel
].green
>> 8;
791 data
[pos
+2] = cmap
->colors
[pixel
].blue
>> 8;
795 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
800 int mask_pixel
= gdk_image_get_pixel( gdk_image_mask
, i
, j
);
813 gdk_image_destroy( gdk_image
);
814 if (gdk_image_mask
) gdk_image_destroy( gdk_image_mask
);
819 wxBitmap::wxBitmap( const wxBitmap
& bmp
)
824 wxBitmap::wxBitmap( const wxString
&filename
, int type
)
826 LoadFile( filename
, type
);
829 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
))
831 m_refData
= new wxBitmapRefData();
833 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
834 M_BMPDATA
->m_bitmap
=
835 gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) bits
, width
, height
);
836 M_BMPDATA
->m_width
= width
;
837 M_BMPDATA
->m_height
= height
;
838 M_BMPDATA
->m_bpp
= 1;
840 wxCHECK_RET( M_BMPDATA
->m_bitmap
, wxT("couldn't create bitmap") );
843 wxBitmap::~wxBitmap()
847 wxBitmap
& wxBitmap::operator = ( const wxBitmap
& bmp
)
849 if ( m_refData
!= bmp
.m_refData
)
855 bool wxBitmap::operator == ( const wxBitmap
& bmp
) const
857 return m_refData
== bmp
.m_refData
;
860 bool wxBitmap::operator != ( const wxBitmap
& bmp
) const
862 return m_refData
!= bmp
.m_refData
;
865 bool wxBitmap::Ok() const
867 return (m_refData
!= NULL
);
870 int wxBitmap::GetHeight() const
872 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
874 return M_BMPDATA
->m_height
;
877 int wxBitmap::GetWidth() const
879 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
881 return M_BMPDATA
->m_width
;
884 int wxBitmap::GetDepth() const
886 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
888 return M_BMPDATA
->m_bpp
;
891 wxMask
*wxBitmap::GetMask() const
893 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
895 return M_BMPDATA
->m_mask
;
898 void wxBitmap::SetMask( wxMask
*mask
)
900 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
902 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
904 M_BMPDATA
->m_mask
= mask
;
907 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
913 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
916 (rect
.x
>= 0) && (rect
.y
>= 0) &&
917 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) && (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
918 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
920 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
921 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
925 GdkGC
*gc
= gdk_gc_new( ret
.GetPixmap() );
926 gdk_draw_pixmap( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
927 gdk_gc_destroy( gc
);
931 GdkGC
*gc
= gdk_gc_new( ret
.GetBitmap() );
932 gdk_wx_draw_bitmap( ret
.GetBitmap(), gc
, GetBitmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
933 gdk_gc_destroy( gc
);
938 wxMask
*mask
= new wxMask
;
939 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, rect
.width
, rect
.height
, 1 );
941 GdkGC
*gc
= gdk_gc_new( mask
->m_bitmap
);
942 gdk_wx_draw_bitmap( mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, 0, 0, rect
.x
, rect
.y
, rect
.width
, rect
.height
);
943 gdk_gc_destroy( gc
);
951 bool wxBitmap::SaveFile( const wxString
&name
, int type
, wxPalette
*WXUNUSED(palette
) )
953 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid bitmap") );
955 // Try to save the bitmap via wxImage handlers:
957 wxImage
image( *this );
958 if (image
.Ok()) return image
.SaveFile( name
, type
);
964 bool wxBitmap::LoadFile( const wxString
&name
, int type
)
968 if (!wxFileExists(name
)) return FALSE
;
970 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
972 if (type
== wxBITMAP_TYPE_XPM
)
974 m_refData
= new wxBitmapRefData();
976 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
978 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm( wxGetRootWindow()->window
, &mask
, NULL
, name
.fn_str() );
982 M_BMPDATA
->m_mask
= new wxMask();
983 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
986 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
988 M_BMPDATA
->m_bpp
= visual
->depth
;
990 else // try if wxImage can load it
993 if (!image
.LoadFile( name
, type
)) return FALSE
;
994 if (image
.Ok()) *this = image
.ConvertToBitmap();
1001 wxPalette
*wxBitmap::GetPalette() const
1003 if (!Ok()) return (wxPalette
*) NULL
;
1005 return M_BMPDATA
->m_palette
;
1008 void wxBitmap::SetHeight( int height
)
1010 if (!m_refData
) m_refData
= new wxBitmapRefData();
1012 M_BMPDATA
->m_height
= height
;
1015 void wxBitmap::SetWidth( int width
)
1017 if (!m_refData
) m_refData
= new wxBitmapRefData();
1019 M_BMPDATA
->m_width
= width
;
1022 void wxBitmap::SetDepth( int depth
)
1024 if (!m_refData
) m_refData
= new wxBitmapRefData();
1026 M_BMPDATA
->m_bpp
= depth
;
1029 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
1031 if (!m_refData
) m_refData
= new wxBitmapRefData();
1033 M_BMPDATA
->m_pixmap
= pixmap
;
1036 void wxBitmap::SetBitmap( GdkPixmap
*bitmap
)
1038 if (!m_refData
) m_refData
= new wxBitmapRefData();
1040 M_BMPDATA
->m_bitmap
= bitmap
;
1043 GdkPixmap
*wxBitmap::GetPixmap() const
1045 wxCHECK_MSG( Ok(), (GdkPixmap
*) NULL
, wxT("invalid bitmap") );
1047 return M_BMPDATA
->m_pixmap
;
1050 GdkBitmap
*wxBitmap::GetBitmap() const
1052 wxCHECK_MSG( Ok(), (GdkBitmap
*) NULL
, wxT("invalid bitmap") );
1054 return M_BMPDATA
->m_bitmap
;