]>
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"
14 #include "wx/bitmap.h"
16 #include "wx/filefn.h"
18 #include "wx/dcmemory.h"
25 #if (GTK_MINOR_VERSION > 0)
26 #include <gdk/gdkrgb.h>
29 extern void gdk_wx_draw_bitmap (GdkDrawable
*drawable
,
39 //-----------------------------------------------------------------------------
41 //-----------------------------------------------------------------------------
43 extern GtkWidget
*wxRootWindow
;
45 //-----------------------------------------------------------------------------
47 //-----------------------------------------------------------------------------
49 IMPLEMENT_DYNAMIC_CLASS(wxMask
,wxObject
)
53 m_bitmap
= (GdkBitmap
*) NULL
;
56 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
58 m_bitmap
= (GdkBitmap
*) NULL
;
59 Create( bitmap
, colour
);
62 wxMask::wxMask( const wxBitmap
& bitmap
, int paletteIndex
)
64 m_bitmap
= (GdkBitmap
*) NULL
;
65 Create( bitmap
, paletteIndex
);
68 wxMask::wxMask( const wxBitmap
& bitmap
)
70 m_bitmap
= (GdkBitmap
*) NULL
;
77 gdk_bitmap_unref( m_bitmap
);
80 bool wxMask::Create( const wxBitmap
& bitmap
,
81 const wxColour
& colour
)
85 gdk_bitmap_unref( m_bitmap
);
86 m_bitmap
= (GdkBitmap
*) NULL
;
89 wxImage
image( bitmap
);
90 if (!image
.Ok()) return FALSE
;
92 m_bitmap
= gdk_pixmap_new( wxRootWindow
->window
, image
.GetWidth(), image
.GetHeight(), 1 );
93 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
100 gdk_gc_set_foreground( gc
, &color
);
101 gdk_gc_set_fill( gc
, GDK_SOLID
);
102 gdk_draw_rectangle( m_bitmap
, gc
, TRUE
, 0, 0, image
.GetWidth(), image
.GetHeight() );
104 unsigned char *data
= image
.GetData();
107 unsigned char red
= colour
.Red();
108 unsigned char green
= colour
.Green();
109 unsigned char blue
= colour
.Blue();
111 GdkVisual
*visual
= gdk_window_get_visual( wxRootWindow
->window
);
114 int bpp
= visual
->depth
;
115 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800)) bpp
= 15;
119 green
= green
& 0xf8;
125 green
= green
& 0xfc;
133 gdk_gc_set_foreground( gc
, &color
);
135 for (int j
= 0; j
< image
.GetHeight(); j
++)
139 for (i
= 0; i
< image
.GetWidth(); i
++)
141 if ((data
[index
] == red
) &&
142 (data
[index
+1] == green
) &&
143 (data
[index
+2] == blue
))
152 gdk_draw_line( m_bitmap
, gc
, start_x
, j
, i
-1, j
);
159 gdk_draw_line( m_bitmap
, gc
, start_x
, j
, i
, j
);
167 bool wxMask::Create( const wxBitmap
& bitmap
, int paletteIndex
)
170 wxPalette
*pal
= bitmap
.GetPalette();
172 wxCHECK_MSG( pal
, FALSE
, wxT("Cannot create mask from bitmap without palette") );
174 pal
->GetRGB(paletteIndex
, &r
, &g
, &b
);
176 return Create(bitmap
, wxColour(r
, g
, b
));
179 bool wxMask::Create( const wxBitmap
& bitmap
)
183 gdk_bitmap_unref( m_bitmap
);
184 m_bitmap
= (GdkBitmap
*) NULL
;
187 if (!bitmap
.Ok()) return FALSE
;
189 wxCHECK_MSG( bitmap
.GetBitmap(), FALSE
, wxT("Cannot create mask from colour bitmap") );
191 m_bitmap
= gdk_pixmap_new( wxRootWindow
->window
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
193 if (!m_bitmap
) return FALSE
;
195 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
197 gdk_wx_draw_bitmap( m_bitmap
, gc
, bitmap
.GetBitmap(), 0, 0, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight() );
204 GdkBitmap
*wxMask::GetBitmap() const
209 //-----------------------------------------------------------------------------
211 //-----------------------------------------------------------------------------
213 class wxBitmapRefData
: public wxObjectRefData
225 wxPalette
*m_palette
;
228 wxBitmapRefData::wxBitmapRefData()
230 m_pixmap
= (GdkPixmap
*) NULL
;
231 m_bitmap
= (GdkBitmap
*) NULL
;
232 m_mask
= (wxMask
*) NULL
;
236 m_palette
= (wxPalette
*) NULL
;
239 wxBitmapRefData::~wxBitmapRefData()
241 if (m_pixmap
) gdk_pixmap_unref( m_pixmap
);
242 if (m_bitmap
) gdk_bitmap_unref( m_bitmap
);
243 if (m_mask
) delete m_mask
;
244 if (m_palette
) delete m_palette
;
247 //-----------------------------------------------------------------------------
249 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
251 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
255 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
258 wxBitmap::wxBitmap( int width
, int height
, int depth
)
260 Create( width
, height
, depth
);
262 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
265 bool wxBitmap::Create( int width
, int height
, int depth
)
269 wxCHECK_MSG( (width
> 0) && (height
> 0), FALSE
, wxT("invalid bitmap size") )
271 GdkVisual
*visual
= gdk_window_get_visual( wxRootWindow
->window
);
274 if (depth
== -1) depth
= visual
->depth
;
276 wxCHECK_MSG( (depth
== visual
->depth
) ||
277 (depth
== 1), FALSE
, wxT("invalid bitmap depth") )
279 m_refData
= new wxBitmapRefData();
280 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
281 M_BMPDATA
->m_width
= width
;
282 M_BMPDATA
->m_height
= height
;
285 M_BMPDATA
->m_bitmap
= gdk_pixmap_new( wxRootWindow
->window
, width
, height
, 1 );
286 M_BMPDATA
->m_bpp
= 1;
290 M_BMPDATA
->m_pixmap
= gdk_pixmap_new( wxRootWindow
->window
, width
, height
, depth
);
291 M_BMPDATA
->m_bpp
= visual
->depth
;
297 bool wxBitmap::CreateFromXpm( const char **bits
)
299 wxCHECK_MSG( bits
!= NULL
, FALSE
, wxT("invalid bitmap data") )
301 GdkVisual
*visual
= gdk_window_get_visual( wxRootWindow
->window
);
304 m_refData
= new wxBitmapRefData();
306 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
308 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm_d( wxRootWindow
->window
, &mask
, NULL
, (gchar
**) bits
);
310 wxCHECK_MSG( M_BMPDATA
->m_pixmap
, FALSE
, wxT("couldn't create pixmap") );
314 M_BMPDATA
->m_mask
= new wxMask();
315 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
318 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
320 M_BMPDATA
->m_bpp
= visual
->depth
; // ?
322 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
327 extern GtkWidget
*wxRootWindow
;
329 bool wxBitmap::CreateFromImage( const wxImage
& image
, int depth
)
331 wxCHECK_MSG( image
.Ok(), FALSE
, wxT("invalid image") )
332 wxCHECK_MSG( depth
== -1 || depth
== 1, FALSE
, wxT("invalid bitmap depth") )
334 m_refData
= new wxBitmapRefData();
336 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
339 // convertion to mono bitmap:
343 int width
= image
.GetWidth();
344 int height
= image
.GetHeight();
349 SetBitmap( gdk_pixmap_new( wxRootWindow
->window
, width
, height
, 1 ) );
353 GdkVisual
*visual
= gdk_window_get_visual( wxRootWindow
->window
);
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( wxRootWindow
->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( wxRootWindow
->window
, width
, height
, -1 ) );
450 GdkVisual
*visual
= gdk_window_get_visual( wxRootWindow
->window
);
453 int bpp
= visual
->depth
;
457 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800)) bpp
= 15;
458 if (bpp
< 8) bpp
= 8;
460 #if (GTK_MINOR_VERSION > 0)
462 if (!image
.HasMask() && (bpp
> 8))
464 static bool s_hasInitialized
= FALSE
;
466 if (!s_hasInitialized
)
469 s_hasInitialized
= TRUE
;
472 GdkGC
*gc
= gdk_gc_new( GetPixmap() );
474 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( wxRootWindow
->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
= RGB
;
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
);
581 guint32 pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3);
582 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
587 guint32 pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3);
588 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
597 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
598 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
599 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
600 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
601 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
602 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
604 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
613 GdkGC
*data_gc
= gdk_gc_new( GetPixmap() );
615 gdk_draw_image( GetPixmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
617 gdk_image_destroy( data_image
);
618 gdk_gc_unref( data_gc
);
624 GdkGC
*mask_gc
= gdk_gc_new( GetMask()->GetBitmap() );
626 gdk_draw_image( GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
628 gdk_image_destroy( mask_image
);
629 gdk_gc_unref( mask_gc
);
636 wxImage
wxBitmap::ConvertToImage() const
640 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
642 GdkImage
*gdk_image
= (GdkImage
*) NULL
;
645 gdk_image
= gdk_image_get( GetPixmap(),
647 GetWidth(), GetHeight() );
651 gdk_image
= gdk_image_get( GetBitmap(),
653 GetWidth(), GetHeight() );
656 wxFAIL_MSG( wxT("Ill-formed bitmap") );
659 wxCHECK_MSG( gdk_image
, wxNullImage
, wxT("couldn't create image") );
661 image
.Create( GetWidth(), GetHeight() );
662 char unsigned *data
= image
.GetData();
666 gdk_image_destroy( gdk_image
);
667 wxFAIL_MSG( wxT("couldn't create image") );
671 GdkImage
*gdk_image_mask
= (GdkImage
*) NULL
;
674 gdk_image_mask
= gdk_image_get( GetMask()->GetBitmap(),
676 GetWidth(), GetHeight() );
678 image
.SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
682 int red_shift_right
= 0;
683 int green_shift_right
= 0;
684 int blue_shift_right
= 0;
685 int red_shift_left
= 0;
686 int green_shift_left
= 0;
687 int blue_shift_left
= 0;
688 bool use_shift
= FALSE
;
692 GdkVisual
*visual
= gdk_window_get_visual( GetPixmap() );
694 if (visual
== NULL
) visual
= gdk_window_get_visual( wxRootWindow
->window
);
696 if (bpp
== 16) bpp
= visual
->red_prec
+ visual
->green_prec
+ visual
->blue_prec
;
697 red_shift_right
= visual
->red_shift
;
698 red_shift_left
= 8-visual
->red_prec
;
699 green_shift_right
= visual
->green_shift
;
700 green_shift_left
= 8-visual
->green_prec
;
701 blue_shift_right
= visual
->blue_shift
;
702 blue_shift_left
= 8-visual
->blue_prec
;
704 use_shift
= (visual
->type
== GDK_VISUAL_TRUE_COLOR
) || (visual
->type
== GDK_VISUAL_DIRECT_COLOR
);
712 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
715 for (int j
= 0; j
< GetHeight(); j
++)
717 for (int i
= 0; i
< GetWidth(); i
++)
719 wxUint32 pixel
= gdk_image_get_pixel( gdk_image
, i
, j
);
737 data
[pos
] = (pixel
>> red_shift_right
) << red_shift_left
;
738 data
[pos
+1] = (pixel
>> green_shift_right
) << green_shift_left
;
739 data
[pos
+2] = (pixel
>> blue_shift_right
) << blue_shift_left
;
741 else if (cmap
->colors
)
743 data
[pos
] = cmap
->colors
[pixel
].red
>> 8;
744 data
[pos
+1] = cmap
->colors
[pixel
].green
>> 8;
745 data
[pos
+2] = cmap
->colors
[pixel
].blue
>> 8;
749 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
754 int mask_pixel
= gdk_image_get_pixel( gdk_image_mask
, i
, j
);
767 gdk_image_destroy( gdk_image
);
768 if (gdk_image_mask
) gdk_image_destroy( gdk_image_mask
);
773 wxBitmap::wxBitmap( const wxBitmap
& bmp
)
777 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
780 wxBitmap::wxBitmap( const wxString
&filename
, int type
)
782 LoadFile( filename
, type
);
784 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
787 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
))
789 m_refData
= new wxBitmapRefData();
791 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
792 M_BMPDATA
->m_bitmap
=
793 gdk_bitmap_create_from_data( wxRootWindow
->window
, (gchar
*) bits
, width
, height
);
794 M_BMPDATA
->m_width
= width
;
795 M_BMPDATA
->m_height
= height
;
796 M_BMPDATA
->m_bpp
= 1;
798 wxCHECK_RET( M_BMPDATA
->m_bitmap
, wxT("couldn't create bitmap") );
800 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
803 wxBitmap::~wxBitmap()
805 if (wxTheBitmapList
) wxTheBitmapList
->DeleteObject(this);
808 wxBitmap
& wxBitmap::operator = ( const wxBitmap
& bmp
)
810 if (*this == bmp
) return (*this);
815 bool wxBitmap::operator == ( const wxBitmap
& bmp
) const
817 return m_refData
== bmp
.m_refData
;
820 bool wxBitmap::operator != ( const wxBitmap
& bmp
) const
822 return m_refData
!= bmp
.m_refData
;
825 bool wxBitmap::Ok() const
827 return (m_refData
!= NULL
);
830 int wxBitmap::GetHeight() const
832 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
834 return M_BMPDATA
->m_height
;
837 int wxBitmap::GetWidth() const
839 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
841 return M_BMPDATA
->m_width
;
844 int wxBitmap::GetDepth() const
846 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
848 return M_BMPDATA
->m_bpp
;
851 wxMask
*wxBitmap::GetMask() const
853 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
855 return M_BMPDATA
->m_mask
;
858 void wxBitmap::SetMask( wxMask
*mask
)
860 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
862 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
864 M_BMPDATA
->m_mask
= mask
;
867 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
870 (rect
.x
>= 0) && (rect
.y
>= 0) &&
871 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) && (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
872 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
874 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
875 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
879 GdkGC
*gc
= gdk_gc_new( ret
.GetPixmap() );
880 gdk_draw_pixmap( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
881 gdk_gc_destroy( gc
);
885 GdkGC
*gc
= gdk_gc_new( ret
.GetBitmap() );
886 gdk_wx_draw_bitmap( ret
.GetBitmap(), gc
, GetBitmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
887 gdk_gc_destroy( gc
);
892 wxMask
*mask
= new wxMask
;
893 mask
->m_bitmap
= gdk_pixmap_new( wxRootWindow
->window
, rect
.width
, rect
.height
, 1 );
895 GdkGC
*gc
= gdk_gc_new( mask
->m_bitmap
);
896 gdk_wx_draw_bitmap( mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, 0, 0, rect
.x
, rect
.y
, rect
.width
, rect
.height
);
897 gdk_gc_destroy( gc
);
905 bool wxBitmap::SaveFile( const wxString
&name
, int type
, wxPalette
*WXUNUSED(palette
) )
907 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid bitmap") );
909 // Try to save the bitmap via wxImage handlers:
911 wxImage
image( *this );
912 if (image
.Ok()) return image
.SaveFile( name
, type
);
918 bool wxBitmap::LoadFile( const wxString
&name
, int type
)
922 if (!wxFileExists(name
)) return FALSE
;
924 GdkVisual
*visual
= gdk_window_get_visual( wxRootWindow
->window
);
927 if (type
== wxBITMAP_TYPE_XPM
)
929 m_refData
= new wxBitmapRefData();
931 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
933 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm( wxRootWindow
->window
, &mask
, NULL
, name
.fn_str() );
937 M_BMPDATA
->m_mask
= new wxMask();
938 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
941 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
943 M_BMPDATA
->m_bpp
= visual
->depth
;
945 else // try if wxImage can load it
948 if (!image
.LoadFile( name
, type
)) return FALSE
;
949 if (image
.Ok()) *this = image
.ConvertToBitmap();
956 wxPalette
*wxBitmap::GetPalette() const
958 if (!Ok()) return (wxPalette
*) NULL
;
960 return M_BMPDATA
->m_palette
;
963 void wxBitmap::SetHeight( int height
)
965 if (!m_refData
) m_refData
= new wxBitmapRefData();
967 M_BMPDATA
->m_height
= height
;
970 void wxBitmap::SetWidth( int width
)
972 if (!m_refData
) m_refData
= new wxBitmapRefData();
974 M_BMPDATA
->m_width
= width
;
977 void wxBitmap::SetDepth( int depth
)
979 if (!m_refData
) m_refData
= new wxBitmapRefData();
981 M_BMPDATA
->m_bpp
= depth
;
984 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
986 if (!m_refData
) m_refData
= new wxBitmapRefData();
988 M_BMPDATA
->m_pixmap
= pixmap
;
991 void wxBitmap::SetBitmap( GdkPixmap
*bitmap
)
993 if (!m_refData
) m_refData
= new wxBitmapRefData();
995 M_BMPDATA
->m_bitmap
= bitmap
;
998 GdkPixmap
*wxBitmap::GetPixmap() const
1000 wxCHECK_MSG( Ok(), (GdkPixmap
*) NULL
, wxT("invalid bitmap") );
1002 return M_BMPDATA
->m_pixmap
;
1005 GdkBitmap
*wxBitmap::GetBitmap() const
1007 wxCHECK_MSG( Ok(), (GdkBitmap
*) NULL
, wxT("invalid bitmap") );
1009 return M_BMPDATA
->m_bitmap
;