]>
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 #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()
257 if (m_pixmap
) gdk_pixmap_unref( m_pixmap
);
258 if (m_bitmap
) gdk_bitmap_unref( m_bitmap
);
259 if (m_mask
) delete m_mask
;
260 if (m_palette
) delete m_palette
;
263 //-----------------------------------------------------------------------------
265 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
267 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
273 wxBitmap::wxBitmap( int width
, int height
, int depth
)
275 Create( width
, height
, depth
);
278 bool wxBitmap::Create( int width
, int height
, int depth
)
282 wxCHECK_MSG( (width
> 0) && (height
> 0), FALSE
, wxT("invalid bitmap size") )
284 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
286 if (depth
== -1) depth
= visual
->depth
;
288 wxCHECK_MSG( (depth
== visual
->depth
) ||
289 (depth
== 1), FALSE
, wxT("invalid bitmap depth") )
291 m_refData
= new wxBitmapRefData();
292 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
293 M_BMPDATA
->m_width
= width
;
294 M_BMPDATA
->m_height
= height
;
297 M_BMPDATA
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
298 M_BMPDATA
->m_bpp
= 1;
302 M_BMPDATA
->m_pixmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, depth
);
303 M_BMPDATA
->m_bpp
= visual
->depth
;
309 bool wxBitmap::CreateFromXpm( const char **bits
)
313 wxCHECK_MSG( bits
!= NULL
, FALSE
, wxT("invalid bitmap data") )
315 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
317 m_refData
= new wxBitmapRefData();
319 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
321 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm_d( wxGetRootWindow()->window
, &mask
, NULL
, (gchar
**) bits
);
323 wxCHECK_MSG( M_BMPDATA
->m_pixmap
, FALSE
, wxT("couldn't create pixmap") );
327 M_BMPDATA
->m_mask
= new wxMask();
328 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
331 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
333 M_BMPDATA
->m_bpp
= visual
->depth
; // Can we get a different depth from create_from_xpm_d() ?
338 bool wxBitmap::CreateFromImage( const wxImage
& image
, int depth
)
342 wxCHECK_MSG( image
.Ok(), FALSE
, wxT("invalid image") )
343 wxCHECK_MSG( depth
== -1 || depth
== 1, FALSE
, wxT("invalid bitmap depth") )
345 m_refData
= new wxBitmapRefData();
348 // conversion to mono bitmap:
352 int width
= image
.GetWidth();
353 int height
= image
.GetHeight();
358 SetBitmap( gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 ) );
362 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
364 // Create picture image
366 unsigned char *data_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
368 GdkImage
*data_image
=
369 gdk_image_new_bitmap( visual
, data_data
, width
, height
);
373 GdkImage
*mask_image
= (GdkImage
*) NULL
;
377 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
379 mask_image
= gdk_image_new_bitmap( visual
, mask_data
, width
, height
);
381 wxMask
*mask
= new wxMask();
382 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
387 int r_mask
= image
.GetMaskRed();
388 int g_mask
= image
.GetMaskGreen();
389 int b_mask
= image
.GetMaskBlue();
391 unsigned char* data
= image
.GetData();
394 for (int y
= 0; y
< height
; y
++)
396 for (int x
= 0; x
< width
; x
++)
407 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
408 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
410 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
413 if ((r
== 255) && (b
== 255) && (g
== 255))
414 gdk_image_put_pixel( data_image
, x
, y
, 1 );
416 gdk_image_put_pixel( data_image
, x
, y
, 0 );
423 GdkGC
*data_gc
= gdk_gc_new( GetBitmap() );
425 gdk_draw_image( GetBitmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
427 gdk_image_destroy( data_image
);
428 gdk_gc_unref( data_gc
);
434 GdkGC
*mask_gc
= gdk_gc_new( GetMask()->GetBitmap() );
436 gdk_draw_image( GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
438 gdk_image_destroy( mask_image
);
439 gdk_gc_unref( mask_gc
);
444 // conversion to colour bitmap:
448 int width
= image
.GetWidth();
449 int height
= image
.GetHeight();
454 SetPixmap( gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, -1 ) );
456 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
458 int bpp
= visual
->depth
;
462 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800))
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
683 // the colour used as transparent one in wxImage and the one it is replaced
684 // with when it really occurs in the bitmap
685 static const int MASK_RED
= 1;
686 static const int MASK_GREEN
= 2;
687 static const int MASK_BLUE
= 3;
688 static const int MASK_BLUE_REPLACEMENT
= 2;
692 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
694 GdkImage
*gdk_image
= (GdkImage
*) NULL
;
697 gdk_image
= gdk_image_get( GetPixmap(),
699 GetWidth(), GetHeight() );
701 else if (GetBitmap())
703 gdk_image
= gdk_image_get( GetBitmap(),
705 GetWidth(), GetHeight() );
709 wxFAIL_MSG( wxT("Ill-formed bitmap") );
712 wxCHECK_MSG( gdk_image
, wxNullImage
, wxT("couldn't create image") );
714 image
.Create( GetWidth(), GetHeight() );
715 char unsigned *data
= image
.GetData();
719 gdk_image_destroy( gdk_image
);
720 wxFAIL_MSG( wxT("couldn't create image") );
724 GdkImage
*gdk_image_mask
= (GdkImage
*) NULL
;
727 gdk_image_mask
= gdk_image_get( GetMask()->GetBitmap(),
729 GetWidth(), GetHeight() );
731 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
735 int red_shift_right
= 0;
736 int green_shift_right
= 0;
737 int blue_shift_right
= 0;
738 int red_shift_left
= 0;
739 int green_shift_left
= 0;
740 int blue_shift_left
= 0;
741 bool use_shift
= FALSE
;
745 GdkVisual
*visual
= gdk_window_get_visual( GetPixmap() );
747 visual
= wxTheApp
->GetGdkVisual();
751 bpp
= visual
->red_prec
+ visual
->green_prec
+ visual
->blue_prec
;
752 red_shift_right
= visual
->red_shift
;
753 red_shift_left
= 8-visual
->red_prec
;
754 green_shift_right
= visual
->green_shift
;
755 green_shift_left
= 8-visual
->green_prec
;
756 blue_shift_right
= visual
->blue_shift
;
757 blue_shift_left
= 8-visual
->blue_prec
;
759 use_shift
= (visual
->type
== GDK_VISUAL_TRUE_COLOR
) || (visual
->type
== GDK_VISUAL_DIRECT_COLOR
);
767 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
770 for (int j
= 0; j
< GetHeight(); j
++)
772 for (int i
= 0; i
< GetWidth(); i
++)
774 wxUint32 pixel
= gdk_image_get_pixel( gdk_image
, i
, j
);
792 data
[pos
] = (pixel
>> red_shift_right
) << red_shift_left
;
793 data
[pos
+1] = (pixel
>> green_shift_right
) << green_shift_left
;
794 data
[pos
+2] = (pixel
>> blue_shift_right
) << blue_shift_left
;
796 else if (cmap
->colors
)
798 data
[pos
] = cmap
->colors
[pixel
].red
>> 8;
799 data
[pos
+1] = cmap
->colors
[pixel
].green
>> 8;
800 data
[pos
+2] = cmap
->colors
[pixel
].blue
>> 8;
804 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
809 int mask_pixel
= gdk_image_get_pixel( gdk_image_mask
, i
, j
);
812 data
[pos
] = MASK_RED
;
813 data
[pos
+1] = MASK_GREEN
;
814 data
[pos
+2] = MASK_BLUE
;
816 else if ( data
[pos
] == MASK_RED
&&
817 data
[pos
+1] == MASK_GREEN
&&
818 data
[pos
+2] == MASK_BLUE
)
820 data
[pos
+2] = MASK_BLUE_REPLACEMENT
;
828 gdk_image_destroy( gdk_image
);
829 if (gdk_image_mask
) gdk_image_destroy( gdk_image_mask
);
834 wxBitmap::wxBitmap( const wxBitmap
& bmp
)
840 wxBitmap::wxBitmap( const wxString
&filename
, int type
)
842 LoadFile( filename
, type
);
845 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
))
847 m_refData
= new wxBitmapRefData();
849 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
850 M_BMPDATA
->m_bitmap
=
851 gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) bits
, width
, height
);
852 M_BMPDATA
->m_width
= width
;
853 M_BMPDATA
->m_height
= height
;
854 M_BMPDATA
->m_bpp
= 1;
856 wxCHECK_RET( M_BMPDATA
->m_bitmap
, wxT("couldn't create bitmap") );
859 wxBitmap::~wxBitmap()
863 wxBitmap
& wxBitmap::operator = ( const wxBitmap
& bmp
)
865 if ( m_refData
!= bmp
.m_refData
)
871 bool wxBitmap::operator == ( const wxBitmap
& bmp
) const
873 return m_refData
== bmp
.m_refData
;
876 bool wxBitmap::operator != ( const wxBitmap
& bmp
) const
878 return m_refData
!= bmp
.m_refData
;
881 bool wxBitmap::Ok() const
883 return (m_refData
!= NULL
);
886 int wxBitmap::GetHeight() const
888 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
890 return M_BMPDATA
->m_height
;
893 int wxBitmap::GetWidth() const
895 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
897 return M_BMPDATA
->m_width
;
900 int wxBitmap::GetDepth() const
902 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
904 return M_BMPDATA
->m_bpp
;
907 wxMask
*wxBitmap::GetMask() const
909 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
911 return M_BMPDATA
->m_mask
;
914 void wxBitmap::SetMask( wxMask
*mask
)
916 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
918 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
920 M_BMPDATA
->m_mask
= mask
;
923 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
929 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
932 (rect
.x
>= 0) && (rect
.y
>= 0) &&
933 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) && (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
934 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
936 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
937 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
941 GdkGC
*gc
= gdk_gc_new( ret
.GetPixmap() );
942 gdk_draw_pixmap( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
943 gdk_gc_destroy( gc
);
947 GdkGC
*gc
= gdk_gc_new( ret
.GetBitmap() );
948 gdk_wx_draw_bitmap( ret
.GetBitmap(), gc
, GetBitmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
949 gdk_gc_destroy( gc
);
954 wxMask
*mask
= new wxMask
;
955 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, rect
.width
, rect
.height
, 1 );
957 GdkGC
*gc
= gdk_gc_new( mask
->m_bitmap
);
958 gdk_wx_draw_bitmap( mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, 0, 0, rect
.x
, rect
.y
, rect
.width
, rect
.height
);
959 gdk_gc_destroy( gc
);
967 bool wxBitmap::SaveFile( const wxString
&name
, int type
, wxPalette
*WXUNUSED(palette
) )
969 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid bitmap") );
971 // Try to save the bitmap via wxImage handlers:
973 wxImage image
= ConvertToImage();
974 if (image
.Ok()) return image
.SaveFile( name
, type
);
980 bool wxBitmap::LoadFile( const wxString
&name
, int type
)
984 if (!wxFileExists(name
)) return FALSE
;
986 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
988 if (type
== wxBITMAP_TYPE_XPM
)
990 m_refData
= new wxBitmapRefData();
992 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
994 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm( wxGetRootWindow()->window
, &mask
, NULL
, name
.fn_str() );
998 M_BMPDATA
->m_mask
= new wxMask();
999 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
1002 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
1004 M_BMPDATA
->m_bpp
= visual
->depth
;
1006 else // try if wxImage can load it
1009 if (!image
.LoadFile( name
, type
)) return FALSE
;
1011 *this = wxBitmap(image
);
1018 wxPalette
*wxBitmap::GetPalette() const
1020 if (!Ok()) return (wxPalette
*) NULL
;
1022 return M_BMPDATA
->m_palette
;
1025 void wxBitmap::SetHeight( int height
)
1027 if (!m_refData
) m_refData
= new wxBitmapRefData();
1029 M_BMPDATA
->m_height
= height
;
1032 void wxBitmap::SetWidth( int width
)
1034 if (!m_refData
) m_refData
= new wxBitmapRefData();
1036 M_BMPDATA
->m_width
= width
;
1039 void wxBitmap::SetDepth( int depth
)
1041 if (!m_refData
) m_refData
= new wxBitmapRefData();
1043 M_BMPDATA
->m_bpp
= depth
;
1046 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
1048 if (!m_refData
) m_refData
= new wxBitmapRefData();
1050 M_BMPDATA
->m_pixmap
= pixmap
;
1053 void wxBitmap::SetBitmap( GdkPixmap
*bitmap
)
1055 if (!m_refData
) m_refData
= new wxBitmapRefData();
1057 M_BMPDATA
->m_bitmap
= bitmap
;
1060 GdkPixmap
*wxBitmap::GetPixmap() const
1062 wxCHECK_MSG( Ok(), (GdkPixmap
*) NULL
, wxT("invalid bitmap") );
1064 return M_BMPDATA
->m_pixmap
;
1067 GdkBitmap
*wxBitmap::GetBitmap() const
1069 wxCHECK_MSG( Ok(), (GdkBitmap
*) NULL
, wxT("invalid bitmap") );
1071 return M_BMPDATA
->m_bitmap
;