]>
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 bool wxBitmap::CreateFromXpm( const char **bits
)
250 wxCHECK_MSG( bits
!= NULL
, FALSE
, 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
);
259 wxCHECK_MSG( M_BMPDATA
->m_pixmap
, FALSE
, wxT("couldn't create pixmap") );
263 M_BMPDATA
->m_mask
= new wxMask();
264 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
267 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
269 M_BMPDATA
->m_bpp
= gdk_window_get_visual( parent
)->depth
; // ?
270 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
275 wxBitmap::wxBitmap( const wxBitmap
& bmp
)
279 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
282 wxBitmap::wxBitmap( const wxString
&filename
, int type
)
284 LoadFile( filename
, type
);
286 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
289 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
))
291 m_refData
= new wxBitmapRefData();
293 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
294 M_BMPDATA
->m_bitmap
=
295 gdk_bitmap_create_from_data( (GdkWindow
*) &gdk_root_parent
, (gchar
*) bits
, width
, height
);
296 M_BMPDATA
->m_width
= width
;
297 M_BMPDATA
->m_height
= height
;
298 M_BMPDATA
->m_bpp
= 1;
300 wxCHECK_RET( M_BMPDATA
->m_bitmap
, wxT("couldn't create bitmap") );
302 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
305 wxBitmap::~wxBitmap()
307 if (wxTheBitmapList
) wxTheBitmapList
->DeleteObject(this);
310 wxBitmap
& wxBitmap::operator = ( const wxBitmap
& bmp
)
312 if (*this == bmp
) return (*this);
317 bool wxBitmap::operator == ( const wxBitmap
& bmp
)
319 return m_refData
== bmp
.m_refData
;
322 bool wxBitmap::operator != ( const wxBitmap
& bmp
)
324 return m_refData
!= bmp
.m_refData
;
327 bool wxBitmap::Ok() const
329 return (m_refData
!= NULL
);
332 int wxBitmap::GetHeight() const
334 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
336 return M_BMPDATA
->m_height
;
339 int wxBitmap::GetWidth() const
341 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
343 return M_BMPDATA
->m_width
;
346 int wxBitmap::GetDepth() const
348 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
350 return M_BMPDATA
->m_bpp
;
353 wxMask
*wxBitmap::GetMask() const
355 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
357 return M_BMPDATA
->m_mask
;
360 void wxBitmap::SetMask( wxMask
*mask
)
362 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
364 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
366 M_BMPDATA
->m_mask
= mask
;
369 bool wxBitmap::SaveFile( const wxString
&name
, int type
, wxPalette
*WXUNUSED(palette
) )
371 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid bitmap") );
373 // Try to save the bitmap via wxImage handlers:
375 wxImage
image( *this );
376 if (image
.Ok()) return image
.SaveFile( name
, type
);
382 bool wxBitmap::LoadFile( const wxString
&name
, int type
)
386 if (!wxFileExists(name
)) return FALSE
;
388 if (type
== wxBITMAP_TYPE_XPM
)
390 m_refData
= new wxBitmapRefData();
392 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
393 GdkWindow
*parent
= (GdkWindow
*) &gdk_root_parent
;
395 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm( parent
, &mask
, NULL
, name
.fn_str() );
399 M_BMPDATA
->m_mask
= new wxMask();
400 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
403 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
404 M_BMPDATA
->m_bpp
= gdk_window_get_visual( parent
)->depth
;
406 else // try if wxImage can load it
409 if (!image
.LoadFile( name
, type
)) return FALSE
;
410 if (image
.Ok()) *this = image
.ConvertToBitmap();
417 wxPalette
*wxBitmap::GetPalette() const
419 if (!Ok()) return (wxPalette
*) NULL
;
421 return M_BMPDATA
->m_palette
;
424 void wxBitmap::SetHeight( int height
)
426 if (!m_refData
) m_refData
= new wxBitmapRefData();
428 M_BMPDATA
->m_height
= height
;
431 void wxBitmap::SetWidth( int width
)
433 if (!m_refData
) m_refData
= new wxBitmapRefData();
435 M_BMPDATA
->m_width
= width
;
438 void wxBitmap::SetDepth( int depth
)
440 if (!m_refData
) m_refData
= new wxBitmapRefData();
442 M_BMPDATA
->m_bpp
= depth
;
445 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
447 if (!m_refData
) m_refData
= new wxBitmapRefData();
449 M_BMPDATA
->m_pixmap
= pixmap
;
452 GdkPixmap
*wxBitmap::GetPixmap() const
454 wxCHECK_MSG( Ok(), (GdkPixmap
*) NULL
, wxT("invalid bitmap") );
456 return M_BMPDATA
->m_pixmap
;
459 GdkBitmap
*wxBitmap::GetBitmap() const
461 wxCHECK_MSG( Ok(), (GdkBitmap
*) NULL
, wxT("invalid bitmap") );
463 return M_BMPDATA
->m_bitmap
;