]>
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"
20 #include <gdk/gdkprivate.h>
23 //-----------------------------------------------------------------------------
25 //-----------------------------------------------------------------------------
27 IMPLEMENT_DYNAMIC_CLASS(wxMask
,wxObject
)
31 m_bitmap
= (GdkBitmap
*) NULL
;
34 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
36 m_bitmap
= (GdkBitmap
*) NULL
;
37 Create( bitmap
, colour
);
40 wxMask::wxMask( const wxBitmap
& bitmap
, int paletteIndex
)
42 m_bitmap
= (GdkBitmap
*) NULL
;
43 Create( bitmap
, paletteIndex
);
46 wxMask::wxMask( const wxBitmap
& bitmap
)
48 m_bitmap
= (GdkBitmap
*) NULL
;
55 gdk_bitmap_unref( m_bitmap
);
58 bool wxMask::Create( const wxBitmap
& bitmap
,
59 const wxColour
& colour
)
63 gdk_bitmap_unref( m_bitmap
);
64 m_bitmap
= (GdkBitmap
*) NULL
;
67 wxImage
image( bitmap
);
68 if (!image
.Ok()) return FALSE
;
70 GdkVisual
*visual
= gdk_visual_get_system();
72 GdkImage
*mask_image
= gdk_image_new( GDK_IMAGE_FASTEST
, visual
, image
.GetWidth(), image
.GetHeight() );
73 if (!mask_image
) return FALSE
;
75 GdkWindow
*parent
= (GdkWindow
*) &gdk_root_parent
;
76 m_bitmap
= gdk_pixmap_new( parent
, image
.GetWidth(), image
.GetHeight(), 1 );
79 unsigned char *data
= image
.GetData();
82 unsigned char red
= colour
.Red();
83 unsigned char green
= colour
.Green();
84 unsigned char blue
= colour
.Blue();
86 int bpp
= visual
->depth
;
87 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800)) bpp
= 15;
101 for (int j
= 0; j
< image
.GetHeight(); j
++)
102 for (int i
= 0; i
< image
.GetWidth(); i
++)
104 if ((data
[index
] == red
) &&
105 (data
[index
+1] == green
) &&
106 (data
[index
+2] == blue
))
108 gdk_image_put_pixel( mask_image
, i
, j
, 1 );
112 gdk_image_put_pixel( mask_image
, i
, j
, 1 );
117 GdkGC
*mask_gc
= gdk_gc_new( m_bitmap
);
119 gdk_draw_image( m_bitmap
, mask_gc
, mask_image
, 0, 0, 0, 0, image
.GetWidth(), image
.GetHeight() );
121 gdk_gc_unref( mask_gc
);
122 gdk_image_destroy( mask_image
);
127 bool wxMask::Create( const wxBitmap
& WXUNUSED(bitmap
),
128 int WXUNUSED(paletteIndex
) )
132 gdk_bitmap_unref( m_bitmap
);
133 m_bitmap
= (GdkBitmap
*) NULL
;
136 wxFAIL_MSG( wxT("not implemented") );
141 bool wxMask::Create( const wxBitmap
& bitmap
)
145 gdk_bitmap_unref( m_bitmap
);
146 m_bitmap
= (GdkBitmap
*) NULL
;
149 if (!bitmap
.Ok()) return FALSE
;
151 wxCHECK_MSG( bitmap
.GetBitmap(), FALSE
, wxT("Cannot create mask from colour bitmap") );
153 m_bitmap
= gdk_pixmap_new( (GdkWindow
*) &gdk_root_parent
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
155 if (!m_bitmap
) return FALSE
;
157 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
159 gdk_draw_bitmap( m_bitmap
, gc
, bitmap
.GetBitmap(), 0, 0, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight() );
166 GdkBitmap
*wxMask::GetBitmap() const
171 //-----------------------------------------------------------------------------
173 //-----------------------------------------------------------------------------
175 class wxBitmapRefData
: public wxObjectRefData
187 wxPalette
*m_palette
;
190 wxBitmapRefData::wxBitmapRefData()
192 m_pixmap
= (GdkPixmap
*) NULL
;
193 m_bitmap
= (GdkBitmap
*) NULL
;
194 m_mask
= (wxMask
*) NULL
;
198 m_palette
= (wxPalette
*) NULL
;
201 wxBitmapRefData::~wxBitmapRefData()
203 if (m_pixmap
) gdk_pixmap_unref( m_pixmap
);
204 if (m_bitmap
) gdk_bitmap_unref( m_bitmap
);
205 if (m_mask
) delete m_mask
;
206 if (m_palette
) delete m_palette
;
209 //-----------------------------------------------------------------------------
211 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
213 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
217 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
220 wxBitmap::wxBitmap( int width
, int height
, int depth
)
222 wxCHECK_RET( (width
> 0) && (height
> 0), wxT("invalid bitmap size") )
224 GdkWindow
*parent
= (GdkWindow
*) &gdk_root_parent
;
225 if (depth
== -1) depth
= gdk_window_get_visual( parent
)->depth
;
227 wxCHECK_RET( (depth
== gdk_window_get_visual( parent
)->depth
) ||
228 (depth
== 1), wxT("invalid bitmap depth") )
230 m_refData
= new wxBitmapRefData();
231 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
232 M_BMPDATA
->m_width
= width
;
233 M_BMPDATA
->m_height
= height
;
236 M_BMPDATA
->m_bitmap
= gdk_pixmap_new( parent
, width
, height
, 1 );
237 M_BMPDATA
->m_bpp
= 1;
241 M_BMPDATA
->m_pixmap
= gdk_pixmap_new( parent
, width
, height
, depth
);
242 M_BMPDATA
->m_bpp
= gdk_window_get_visual( parent
)->depth
;
245 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
248 wxBitmap::wxBitmap( const char **bits
)
250 wxCHECK_RET( bits
!= NULL
, wxT("invalid bitmap data") )
252 m_refData
= new wxBitmapRefData();
254 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
255 GdkWindow
*parent
= (GdkWindow
*) &gdk_root_parent
;
257 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm_d( parent
, &mask
, NULL
, (gchar
**) bits
);
261 M_BMPDATA
->m_mask
= new wxMask();
262 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
265 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
267 M_BMPDATA
->m_bpp
= gdk_window_get_visual( parent
)->depth
; // ?
268 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
271 wxBitmap::wxBitmap( char **bits
)
273 wxCHECK_RET( bits
!= NULL
, wxT("invalid bitmap data") )
275 m_refData
= new wxBitmapRefData();
277 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
278 GdkWindow
*parent
= (GdkWindow
*) &gdk_root_parent
;
280 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm_d( parent
, &mask
, NULL
, (gchar
**) bits
);
282 wxCHECK_RET( M_BMPDATA
->m_pixmap
, wxT("couldn't create pixmap") );
286 M_BMPDATA
->m_mask
= new wxMask();
287 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
290 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
292 M_BMPDATA
->m_bpp
= gdk_window_get_visual( parent
)->depth
; // ?
293 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
296 wxBitmap::wxBitmap( const wxBitmap
& bmp
)
300 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
303 wxBitmap::wxBitmap( const wxString
&filename
, int type
)
305 LoadFile( filename
, type
);
307 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
310 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
))
312 m_refData
= new wxBitmapRefData();
314 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
315 M_BMPDATA
->m_bitmap
=
316 gdk_bitmap_create_from_data( (GdkWindow
*) &gdk_root_parent
, (gchar
*) bits
, width
, height
);
317 M_BMPDATA
->m_width
= width
;
318 M_BMPDATA
->m_height
= height
;
319 M_BMPDATA
->m_bpp
= 1;
321 wxCHECK_RET( M_BMPDATA
->m_bitmap
, wxT("couldn't create bitmap") );
323 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
326 wxBitmap::~wxBitmap()
328 if (wxTheBitmapList
) wxTheBitmapList
->DeleteObject(this);
331 wxBitmap
& wxBitmap::operator = ( const wxBitmap
& bmp
)
333 if (*this == bmp
) return (*this);
338 bool wxBitmap::operator == ( const wxBitmap
& bmp
)
340 return m_refData
== bmp
.m_refData
;
343 bool wxBitmap::operator != ( const wxBitmap
& bmp
)
345 return m_refData
!= bmp
.m_refData
;
348 bool wxBitmap::Ok() const
350 return (m_refData
!= NULL
);
353 int wxBitmap::GetHeight() const
355 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
357 return M_BMPDATA
->m_height
;
360 int wxBitmap::GetWidth() const
362 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
364 return M_BMPDATA
->m_width
;
367 int wxBitmap::GetDepth() const
369 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
371 return M_BMPDATA
->m_bpp
;
374 wxMask
*wxBitmap::GetMask() const
376 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
378 return M_BMPDATA
->m_mask
;
381 void wxBitmap::SetMask( wxMask
*mask
)
383 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
385 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
387 M_BMPDATA
->m_mask
= mask
;
390 bool wxBitmap::SaveFile( const wxString
&name
, int type
, wxPalette
*WXUNUSED(palette
) )
392 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid bitmap") );
394 // Try to save the bitmap via wxImage handlers:
396 wxImage
image( *this );
397 if (image
.Ok()) return image
.SaveFile( name
, type
);
403 bool wxBitmap::LoadFile( const wxString
&name
, int type
)
407 if (!wxFileExists(name
)) return FALSE
;
409 if (type
== wxBITMAP_TYPE_XPM
)
411 m_refData
= new wxBitmapRefData();
413 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
414 GdkWindow
*parent
= (GdkWindow
*) &gdk_root_parent
;
416 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm( parent
, &mask
, NULL
, name
.fn_str() );
420 M_BMPDATA
->m_mask
= new wxMask();
421 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
424 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
425 M_BMPDATA
->m_bpp
= gdk_window_get_visual( parent
)->depth
;
427 else // try if wxImage can load it
430 if (!image
.LoadFile( name
, type
)) return FALSE
;
431 if (image
.Ok()) *this = image
.ConvertToBitmap();
438 wxPalette
*wxBitmap::GetPalette() const
440 if (!Ok()) return (wxPalette
*) NULL
;
442 return M_BMPDATA
->m_palette
;
445 void wxBitmap::SetHeight( int height
)
447 if (!m_refData
) m_refData
= new wxBitmapRefData();
449 M_BMPDATA
->m_height
= height
;
452 void wxBitmap::SetWidth( int width
)
454 if (!m_refData
) m_refData
= new wxBitmapRefData();
456 M_BMPDATA
->m_width
= width
;
459 void wxBitmap::SetDepth( int depth
)
461 if (!m_refData
) m_refData
= new wxBitmapRefData();
463 M_BMPDATA
->m_bpp
= depth
;
466 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
468 if (!m_refData
) m_refData
= new wxBitmapRefData();
470 M_BMPDATA
->m_pixmap
= pixmap
;
473 GdkPixmap
*wxBitmap::GetPixmap() const
475 wxCHECK_MSG( Ok(), (GdkPixmap
*) NULL
, wxT("invalid bitmap") );
477 return M_BMPDATA
->m_pixmap
;
480 GdkBitmap
*wxBitmap::GetBitmap() const
482 wxCHECK_MSG( Ok(), (GdkBitmap
*) NULL
, wxT("invalid bitmap") );
484 return M_BMPDATA
->m_bitmap
;