]>
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"
28 #if (GTK_MINOR_VERSION > 0)
29 #include <gdk/gdkrgb.h>
32 extern void gdk_wx_draw_bitmap (GdkDrawable
*drawable
,
42 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
46 extern GtkWidget
*wxGetRootWindow();
48 //-----------------------------------------------------------------------------
50 //-----------------------------------------------------------------------------
52 IMPLEMENT_DYNAMIC_CLASS(wxMask
,wxObject
)
56 m_bitmap
= (GdkBitmap
*) NULL
;
59 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
61 m_bitmap
= (GdkBitmap
*) NULL
;
62 Create( bitmap
, colour
);
65 wxMask::wxMask( const wxBitmap
& bitmap
, int paletteIndex
)
67 m_bitmap
= (GdkBitmap
*) NULL
;
68 Create( bitmap
, paletteIndex
);
71 wxMask::wxMask( const wxBitmap
& bitmap
)
73 m_bitmap
= (GdkBitmap
*) NULL
;
80 gdk_bitmap_unref( m_bitmap
);
83 bool wxMask::Create( const wxBitmap
& bitmap
,
84 const wxColour
& colour
)
88 gdk_bitmap_unref( m_bitmap
);
89 m_bitmap
= (GdkBitmap
*) NULL
;
92 wxImage
image( bitmap
);
93 if (!image
.Ok()) return FALSE
;
95 m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, image
.GetWidth(), image
.GetHeight(), 1 );
96 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
103 gdk_gc_set_foreground( gc
, &color
);
104 gdk_gc_set_fill( gc
, GDK_SOLID
);
105 gdk_draw_rectangle( m_bitmap
, gc
, TRUE
, 0, 0, image
.GetWidth(), image
.GetHeight() );
107 unsigned char *data
= image
.GetData();
110 unsigned char red
= colour
.Red();
111 unsigned char green
= colour
.Green();
112 unsigned char blue
= colour
.Blue();
114 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
116 int bpp
= visual
->depth
;
117 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800)) bpp
= 15;
121 green
= green
& 0xf8;
127 green
= green
& 0xfc;
133 green
= green
& 0xf0;
141 gdk_gc_set_foreground( gc
, &color
);
143 for (int j
= 0; j
< image
.GetHeight(); j
++)
147 for (i
= 0; i
< image
.GetWidth(); i
++)
149 if ((data
[index
] == red
) &&
150 (data
[index
+1] == green
) &&
151 (data
[index
+2] == blue
))
160 gdk_draw_line( m_bitmap
, gc
, start_x
, j
, i
-1, j
);
167 gdk_draw_line( m_bitmap
, gc
, start_x
, j
, i
, j
);
175 bool wxMask::Create( const wxBitmap
& bitmap
, int paletteIndex
)
178 wxPalette
*pal
= bitmap
.GetPalette();
180 wxCHECK_MSG( pal
, FALSE
, wxT("Cannot create mask from bitmap without palette") );
182 pal
->GetRGB(paletteIndex
, &r
, &g
, &b
);
184 return Create(bitmap
, wxColour(r
, g
, b
));
187 bool wxMask::Create( const wxBitmap
& bitmap
)
191 gdk_bitmap_unref( m_bitmap
);
192 m_bitmap
= (GdkBitmap
*) NULL
;
195 if (!bitmap
.Ok()) return FALSE
;
197 wxCHECK_MSG( bitmap
.GetBitmap(), FALSE
, wxT("Cannot create mask from colour bitmap") );
199 m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
201 if (!m_bitmap
) return FALSE
;
203 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
205 gdk_wx_draw_bitmap( m_bitmap
, gc
, bitmap
.GetBitmap(), 0, 0, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight() );
212 GdkBitmap
*wxMask::GetBitmap() const
217 //-----------------------------------------------------------------------------
219 //-----------------------------------------------------------------------------
221 class wxBitmapRefData
: public wxObjectRefData
233 wxPalette
*m_palette
;
236 wxBitmapRefData::wxBitmapRefData()
238 m_pixmap
= (GdkPixmap
*) NULL
;
239 m_bitmap
= (GdkBitmap
*) NULL
;
240 m_mask
= (wxMask
*) NULL
;
244 m_palette
= (wxPalette
*) NULL
;
247 wxBitmapRefData::~wxBitmapRefData()
249 if (m_pixmap
) gdk_pixmap_unref( m_pixmap
);
250 if (m_bitmap
) gdk_bitmap_unref( m_bitmap
);
251 if (m_mask
) delete m_mask
;
252 if (m_palette
) delete m_palette
;
255 //-----------------------------------------------------------------------------
257 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
259 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
265 wxBitmap::wxBitmap( int width
, int height
, int depth
)
267 Create( width
, height
, depth
);
270 bool wxBitmap::Create( int width
, int height
, int depth
)
274 wxCHECK_MSG( (width
> 0) && (height
> 0), FALSE
, wxT("invalid bitmap size") )
276 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
278 if (depth
== -1) depth
= visual
->depth
;
280 wxCHECK_MSG( (depth
== visual
->depth
) ||
281 (depth
== 1), FALSE
, wxT("invalid bitmap depth") )
283 m_refData
= new wxBitmapRefData();
284 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
285 M_BMPDATA
->m_width
= width
;
286 M_BMPDATA
->m_height
= height
;
289 M_BMPDATA
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
290 M_BMPDATA
->m_bpp
= 1;
294 M_BMPDATA
->m_pixmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, depth
);
295 M_BMPDATA
->m_bpp
= visual
->depth
;
301 bool wxBitmap::CreateFromXpm( const char **bits
)
305 wxCHECK_MSG( bits
!= NULL
, FALSE
, wxT("invalid bitmap data") )
307 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
309 m_refData
= new wxBitmapRefData();
311 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
313 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm_d( wxGetRootWindow()->window
, &mask
, NULL
, (gchar
**) bits
);
315 wxCHECK_MSG( M_BMPDATA
->m_pixmap
, FALSE
, wxT("couldn't create pixmap") );
319 M_BMPDATA
->m_mask
= new wxMask();
320 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
323 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
325 M_BMPDATA
->m_bpp
= visual
->depth
; // ?
330 bool wxBitmap::CreateFromImage( const wxImage
& image
, int depth
)
334 wxCHECK_MSG( image
.Ok(), FALSE
, wxT("invalid image") )
335 wxCHECK_MSG( depth
== -1 || depth
== 1, FALSE
, wxT("invalid bitmap depth") )
337 m_refData
= new wxBitmapRefData();
340 // convertion to mono bitmap:
344 int width
= image
.GetWidth();
345 int height
= image
.GetHeight();
350 SetBitmap( gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 ) );
354 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
356 // Create picture image
358 unsigned char *data_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
360 GdkImage
*data_image
=
361 gdk_image_new_bitmap( visual
, data_data
, width
, height
);
365 GdkImage
*mask_image
= (GdkImage
*) NULL
;
369 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
371 mask_image
= gdk_image_new_bitmap( visual
, mask_data
, width
, height
);
373 wxMask
*mask
= new wxMask();
374 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
379 int r_mask
= image
.GetMaskRed();
380 int g_mask
= image
.GetMaskGreen();
381 int b_mask
= image
.GetMaskBlue();
383 unsigned char* data
= image
.GetData();
386 for (int y
= 0; y
< height
; y
++)
388 for (int x
= 0; x
< width
; x
++)
399 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
400 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
402 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
405 if ((r
== 255) && (b
== 255) && (g
== 255))
406 gdk_image_put_pixel( data_image
, x
, y
, 1 );
408 gdk_image_put_pixel( data_image
, x
, y
, 0 );
415 GdkGC
*data_gc
= gdk_gc_new( GetBitmap() );
417 gdk_draw_image( GetBitmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
419 gdk_image_destroy( data_image
);
420 gdk_gc_unref( data_gc
);
426 GdkGC
*mask_gc
= gdk_gc_new( GetMask()->GetBitmap() );
428 gdk_draw_image( GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
430 gdk_image_destroy( mask_image
);
431 gdk_gc_unref( mask_gc
);
436 // convertion to colour bitmap:
440 int width
= image
.GetWidth();
441 int height
= image
.GetHeight();
446 SetPixmap( gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, -1 ) );
448 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
450 int bpp
= visual
->depth
;
454 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800)) bpp
= 15;
455 if (bpp
< 8) bpp
= 8;
457 // We handle 8-bit bitmaps ourselves using the colour cube, 12-bit
458 // visuals are not supported by GDK so we do these ourselves, too.
459 // 15-bit and 16-bit should actually work and 24-bit certainly does.
461 if (!image
.HasMask() && (bpp
> 16))
463 if (!image
.HasMask() && (bpp
> 12))
466 static bool s_hasInitialized
= FALSE
;
468 if (!s_hasInitialized
)
471 s_hasInitialized
= TRUE
;
474 GdkGC
*gc
= gdk_gc_new( GetPixmap() );
476 gdk_draw_rgb_image( GetPixmap(),
488 // Create picture image
490 GdkImage
*data_image
=
491 gdk_image_new( GDK_IMAGE_FASTEST
, visual
, width
, height
);
495 GdkImage
*mask_image
= (GdkImage
*) NULL
;
499 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
501 mask_image
= gdk_image_new_bitmap( visual
, mask_data
, width
, height
);
503 wxMask
*mask
= new wxMask();
504 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
511 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
512 byte_order b_o
= RGB
;
516 if ((visual
->red_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->blue_mask
)) b_o
= RGB
;
517 else if ((visual
->red_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->green_mask
)) b_o
= RBG
;
518 else if ((visual
->blue_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->green_mask
)) b_o
= BRG
;
519 else if ((visual
->blue_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->red_mask
)) b_o
= BGR
;
520 else if ((visual
->green_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->blue_mask
)) b_o
= GRB
;
521 else if ((visual
->green_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->red_mask
)) b_o
= GBR
;
524 int r_mask
= image
.GetMaskRed();
525 int g_mask
= image
.GetMaskGreen();
526 int b_mask
= image
.GetMaskBlue();
528 unsigned char* data
= image
.GetData();
531 for (int y
= 0; y
< height
; y
++)
533 for (int x
= 0; x
< width
; x
++)
544 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
545 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
547 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
555 if (wxTheApp
->m_colorCube
)
557 pixel
= wxTheApp
->m_colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
561 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
562 GdkColor
*colors
= cmap
->colors
;
563 int max
= 3 * (65536);
565 for (int i
= 0; i
< cmap
->size
; i
++)
567 int rdiff
= (r
<< 8) - colors
[i
].red
;
568 int gdiff
= (g
<< 8) - colors
[i
].green
;
569 int bdiff
= (b
<< 8) - colors
[i
].blue
;
570 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
571 if (sum
< max
) { pixel
= i
; max
= sum
; }
575 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
584 case RGB
: pixel
= ((r
& 0xf0) << 4) | (g
& 0xf0) | ((b
& 0xf0) >> 4); break;
585 case RBG
: pixel
= ((r
& 0xf0) << 4) | (b
& 0xf0) | ((g
& 0xf0) >> 4); break;
586 case GRB
: pixel
= ((g
& 0xf0) << 4) | (r
& 0xf0) | ((b
& 0xf0) >> 4); break;
587 case GBR
: pixel
= ((g
& 0xf0) << 4) | (b
& 0xf0) | ((r
& 0xf0) >> 4); break;
588 case BRG
: pixel
= ((b
& 0xf0) << 4) | (r
& 0xf0) | ((g
& 0xf0) >> 4); break;
589 case BGR
: pixel
= ((b
& 0xf0) << 4) | (g
& 0xf0) | ((r
& 0xf0) >> 4); break;
591 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
599 case RGB
: pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
600 case RBG
: pixel
= ((r
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
601 case GRB
: pixel
= ((g
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
602 case GBR
: pixel
= ((g
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
603 case BRG
: pixel
= ((b
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
604 case BGR
: pixel
= ((b
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
606 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
611 // I actually don't know if for 16-bit displays, it is alway the green
612 // component or the second component which has 6 bits.
616 case RGB
: pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
617 case RBG
: pixel
= ((r
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
618 case GRB
: pixel
= ((g
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
619 case GBR
: pixel
= ((g
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
620 case BRG
: pixel
= ((b
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
621 case BGR
: pixel
= ((b
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
623 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
632 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
633 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
634 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
635 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
636 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
637 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
639 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
648 GdkGC
*data_gc
= gdk_gc_new( GetPixmap() );
650 gdk_draw_image( GetPixmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
652 gdk_image_destroy( data_image
);
653 gdk_gc_unref( data_gc
);
659 GdkGC
*mask_gc
= gdk_gc_new( GetMask()->GetBitmap() );
661 gdk_draw_image( GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
663 gdk_image_destroy( mask_image
);
664 gdk_gc_unref( mask_gc
);
671 wxImage
wxBitmap::ConvertToImage() const
675 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
677 GdkImage
*gdk_image
= (GdkImage
*) NULL
;
680 gdk_image
= gdk_image_get( GetPixmap(),
682 GetWidth(), GetHeight() );
686 gdk_image
= gdk_image_get( GetBitmap(),
688 GetWidth(), GetHeight() );
691 wxFAIL_MSG( wxT("Ill-formed bitmap") );
694 wxCHECK_MSG( gdk_image
, wxNullImage
, wxT("couldn't create image") );
696 image
.Create( GetWidth(), GetHeight() );
697 char unsigned *data
= image
.GetData();
701 gdk_image_destroy( gdk_image
);
702 wxFAIL_MSG( wxT("couldn't create image") );
706 GdkImage
*gdk_image_mask
= (GdkImage
*) NULL
;
709 gdk_image_mask
= gdk_image_get( GetMask()->GetBitmap(),
711 GetWidth(), GetHeight() );
713 image
.SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
717 int red_shift_right
= 0;
718 int green_shift_right
= 0;
719 int blue_shift_right
= 0;
720 int red_shift_left
= 0;
721 int green_shift_left
= 0;
722 int blue_shift_left
= 0;
723 bool use_shift
= FALSE
;
727 GdkVisual
*visual
= gdk_window_get_visual( GetPixmap() );
729 visual
= wxTheApp
->GetGdkVisual();
732 if (bpp
== 16) bpp
= visual
->red_prec
+ visual
->green_prec
+ visual
->blue_prec
;
733 red_shift_right
= visual
->red_shift
;
734 red_shift_left
= 8-visual
->red_prec
;
735 green_shift_right
= visual
->green_shift
;
736 green_shift_left
= 8-visual
->green_prec
;
737 blue_shift_right
= visual
->blue_shift
;
738 blue_shift_left
= 8-visual
->blue_prec
;
740 use_shift
= (visual
->type
== GDK_VISUAL_TRUE_COLOR
) || (visual
->type
== GDK_VISUAL_DIRECT_COLOR
);
748 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
751 for (int j
= 0; j
< GetHeight(); j
++)
753 for (int i
= 0; i
< GetWidth(); i
++)
755 wxUint32 pixel
= gdk_image_get_pixel( gdk_image
, i
, j
);
773 data
[pos
] = (pixel
>> red_shift_right
) << red_shift_left
;
774 data
[pos
+1] = (pixel
>> green_shift_right
) << green_shift_left
;
775 data
[pos
+2] = (pixel
>> blue_shift_right
) << blue_shift_left
;
777 else if (cmap
->colors
)
779 data
[pos
] = cmap
->colors
[pixel
].red
>> 8;
780 data
[pos
+1] = cmap
->colors
[pixel
].green
>> 8;
781 data
[pos
+2] = cmap
->colors
[pixel
].blue
>> 8;
785 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
790 int mask_pixel
= gdk_image_get_pixel( gdk_image_mask
, i
, j
);
803 gdk_image_destroy( gdk_image
);
804 if (gdk_image_mask
) gdk_image_destroy( gdk_image_mask
);
809 wxBitmap::wxBitmap( const wxBitmap
& bmp
)
814 wxBitmap::wxBitmap( const wxString
&filename
, int type
)
816 LoadFile( filename
, type
);
819 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
))
821 m_refData
= new wxBitmapRefData();
823 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
824 M_BMPDATA
->m_bitmap
=
825 gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) bits
, width
, height
);
826 M_BMPDATA
->m_width
= width
;
827 M_BMPDATA
->m_height
= height
;
828 M_BMPDATA
->m_bpp
= 1;
830 wxCHECK_RET( M_BMPDATA
->m_bitmap
, wxT("couldn't create bitmap") );
833 wxBitmap::~wxBitmap()
837 wxBitmap
& wxBitmap::operator = ( const wxBitmap
& bmp
)
839 if ( m_refData
!= bmp
.m_refData
)
845 bool wxBitmap::operator == ( const wxBitmap
& bmp
) const
847 return m_refData
== bmp
.m_refData
;
850 bool wxBitmap::operator != ( const wxBitmap
& bmp
) const
852 return m_refData
!= bmp
.m_refData
;
855 bool wxBitmap::Ok() const
857 return (m_refData
!= NULL
);
860 int wxBitmap::GetHeight() const
862 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
864 return M_BMPDATA
->m_height
;
867 int wxBitmap::GetWidth() const
869 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
871 return M_BMPDATA
->m_width
;
874 int wxBitmap::GetDepth() const
876 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
878 return M_BMPDATA
->m_bpp
;
881 wxMask
*wxBitmap::GetMask() const
883 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
885 return M_BMPDATA
->m_mask
;
888 void wxBitmap::SetMask( wxMask
*mask
)
890 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
892 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
894 M_BMPDATA
->m_mask
= mask
;
897 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
903 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
906 (rect
.x
>= 0) && (rect
.y
>= 0) &&
907 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) && (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
908 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
910 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
911 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
915 GdkGC
*gc
= gdk_gc_new( ret
.GetPixmap() );
916 gdk_draw_pixmap( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
917 gdk_gc_destroy( gc
);
921 GdkGC
*gc
= gdk_gc_new( ret
.GetBitmap() );
922 gdk_wx_draw_bitmap( ret
.GetBitmap(), gc
, GetBitmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
923 gdk_gc_destroy( gc
);
928 wxMask
*mask
= new wxMask
;
929 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, rect
.width
, rect
.height
, 1 );
931 GdkGC
*gc
= gdk_gc_new( mask
->m_bitmap
);
932 gdk_wx_draw_bitmap( mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, 0, 0, rect
.x
, rect
.y
, rect
.width
, rect
.height
);
933 gdk_gc_destroy( gc
);
941 bool wxBitmap::SaveFile( const wxString
&name
, int type
, wxPalette
*WXUNUSED(palette
) )
943 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid bitmap") );
945 // Try to save the bitmap via wxImage handlers:
947 wxImage
image( *this );
948 if (image
.Ok()) return image
.SaveFile( name
, type
);
954 bool wxBitmap::LoadFile( const wxString
&name
, int type
)
958 if (!wxFileExists(name
)) return FALSE
;
960 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
962 if (type
== wxBITMAP_TYPE_XPM
)
964 m_refData
= new wxBitmapRefData();
966 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
968 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm( wxGetRootWindow()->window
, &mask
, NULL
, name
.fn_str() );
972 M_BMPDATA
->m_mask
= new wxMask();
973 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
976 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
978 M_BMPDATA
->m_bpp
= visual
->depth
;
980 else // try if wxImage can load it
983 if (!image
.LoadFile( name
, type
)) return FALSE
;
984 if (image
.Ok()) *this = image
.ConvertToBitmap();
991 wxPalette
*wxBitmap::GetPalette() const
993 if (!Ok()) return (wxPalette
*) NULL
;
995 return M_BMPDATA
->m_palette
;
998 void wxBitmap::SetHeight( int height
)
1000 if (!m_refData
) m_refData
= new wxBitmapRefData();
1002 M_BMPDATA
->m_height
= height
;
1005 void wxBitmap::SetWidth( int width
)
1007 if (!m_refData
) m_refData
= new wxBitmapRefData();
1009 M_BMPDATA
->m_width
= width
;
1012 void wxBitmap::SetDepth( int depth
)
1014 if (!m_refData
) m_refData
= new wxBitmapRefData();
1016 M_BMPDATA
->m_bpp
= depth
;
1019 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
1021 if (!m_refData
) m_refData
= new wxBitmapRefData();
1023 M_BMPDATA
->m_pixmap
= pixmap
;
1026 void wxBitmap::SetBitmap( GdkPixmap
*bitmap
)
1028 if (!m_refData
) m_refData
= new wxBitmapRefData();
1030 M_BMPDATA
->m_bitmap
= bitmap
;
1033 GdkPixmap
*wxBitmap::GetPixmap() const
1035 wxCHECK_MSG( Ok(), (GdkPixmap
*) NULL
, wxT("invalid bitmap") );
1037 return M_BMPDATA
->m_pixmap
;
1040 GdkBitmap
*wxBitmap::GetBitmap() const
1042 wxCHECK_MSG( Ok(), (GdkBitmap
*) NULL
, wxT("invalid bitmap") );
1044 return M_BMPDATA
->m_bitmap
;