]>
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"
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
& WXUNUSED(bitmap
), const wxColour
& WXUNUSED(colour
) )
38 wxMask::wxMask( const wxBitmap
& WXUNUSED(bitmap
), int WXUNUSED(paletteIndex
) )
42 wxMask::wxMask( const wxBitmap
& WXUNUSED(bitmap
) )
48 if (m_bitmap
) gdk_bitmap_unref( m_bitmap
);
51 GdkBitmap
*wxMask::GetBitmap(void) const
56 //-----------------------------------------------------------------------------
58 //-----------------------------------------------------------------------------
60 class wxBitmapRefData
: public wxObjectRefData
63 wxBitmapRefData(void);
64 ~wxBitmapRefData(void);
75 wxBitmapRefData::wxBitmapRefData()
77 m_pixmap
= (GdkPixmap
*) NULL
;
78 m_bitmap
= (GdkBitmap
*) NULL
;
79 m_mask
= (wxMask
*) NULL
;
83 m_palette
= (wxPalette
*) NULL
;
86 wxBitmapRefData::~wxBitmapRefData()
88 if (m_pixmap
) gdk_pixmap_unref( m_pixmap
);
89 if (m_bitmap
) gdk_bitmap_unref( m_bitmap
);
90 if (m_mask
) delete m_mask
;
91 if (m_palette
) delete m_palette
;
94 //-----------------------------------------------------------------------------
96 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
98 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
102 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
105 wxBitmap::wxBitmap( int width
, int height
, int depth
)
107 wxCHECK_RET( (width
> 0) && (height
> 0), _T("invalid bitmap size") )
108 wxCHECK_RET( (depth
> 0) || (depth
== -1), _T("invalid bitmap depth") )
110 m_refData
= new wxBitmapRefData();
112 GdkWindow
*parent
= (GdkWindow
*) &gdk_root_parent
;
114 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
115 M_BMPDATA
->m_pixmap
= gdk_pixmap_new( parent
, width
, height
, depth
);
116 M_BMPDATA
->m_width
= width
;
117 M_BMPDATA
->m_height
= height
;
118 M_BMPDATA
->m_bpp
= gdk_window_get_visual( parent
)->depth
;
120 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
123 wxBitmap::wxBitmap( const char **bits
)
125 wxCHECK_RET( bits
!= NULL
, _T("invalid bitmap data") )
127 m_refData
= new wxBitmapRefData();
129 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
130 GdkWindow
*parent
= (GdkWindow
*) &gdk_root_parent
;
132 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm_d( parent
, &mask
, NULL
, (gchar
**) bits
);
136 M_BMPDATA
->m_mask
= new wxMask();
137 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
140 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
142 M_BMPDATA
->m_bpp
= gdk_window_get_visual( parent
)->depth
; // ?
143 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
146 wxBitmap::wxBitmap( char **bits
)
148 wxCHECK_RET( bits
!= NULL
, _T("invalid bitmap data") )
150 m_refData
= new wxBitmapRefData();
152 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
153 GdkWindow
*parent
= (GdkWindow
*) &gdk_root_parent
;
155 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm_d( parent
, &mask
, NULL
, (gchar
**) bits
);
157 wxCHECK_RET( M_BMPDATA
->m_pixmap
, _T("couldn't create pixmap") );
161 M_BMPDATA
->m_mask
= new wxMask();
162 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
165 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
167 M_BMPDATA
->m_bpp
= gdk_window_get_visual( parent
)->depth
; // ?
168 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
171 wxBitmap::wxBitmap( const wxBitmap
& bmp
)
175 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
178 wxBitmap::wxBitmap( const wxString
&filename
, int type
)
180 LoadFile( filename
, type
);
182 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
185 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
))
187 m_refData
= new wxBitmapRefData();
189 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
190 M_BMPDATA
->m_bitmap
=
191 gdk_bitmap_create_from_data( (GdkWindow
*) &gdk_root_parent
, (gchar
*) bits
, width
, height
);
192 M_BMPDATA
->m_width
= width
;
193 M_BMPDATA
->m_height
= height
;
194 M_BMPDATA
->m_bpp
= 1;
196 wxCHECK_RET( M_BMPDATA
->m_bitmap
, _T("couldn't create bitmap") );
198 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
201 wxBitmap::~wxBitmap()
203 if (wxTheBitmapList
) wxTheBitmapList
->DeleteObject(this);
206 wxBitmap
& wxBitmap::operator = ( const wxBitmap
& bmp
)
208 if (*this == bmp
) return (*this);
213 bool wxBitmap::operator == ( const wxBitmap
& bmp
)
215 return m_refData
== bmp
.m_refData
;
218 bool wxBitmap::operator != ( const wxBitmap
& bmp
)
220 return m_refData
!= bmp
.m_refData
;
223 bool wxBitmap::Ok(void) const
225 return (m_refData
!= NULL
);
228 int wxBitmap::GetHeight(void) const
230 wxCHECK_MSG( Ok(), -1, _T("invalid bitmap") );
232 return M_BMPDATA
->m_height
;
235 int wxBitmap::GetWidth(void) const
237 wxCHECK_MSG( Ok(), -1, _T("invalid bitmap") );
239 return M_BMPDATA
->m_width
;
242 int wxBitmap::GetDepth(void) const
244 wxCHECK_MSG( Ok(), -1, _T("invalid bitmap") );
246 return M_BMPDATA
->m_bpp
;
249 wxMask
*wxBitmap::GetMask(void) const
251 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, _T("invalid bitmap") );
253 return M_BMPDATA
->m_mask
;
256 void wxBitmap::SetMask( wxMask
*mask
)
258 wxCHECK_RET( Ok(), _T("invalid bitmap") );
260 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
262 M_BMPDATA
->m_mask
= mask
;
265 bool wxBitmap::SaveFile( const wxString
&name
, int type
, wxPalette
*WXUNUSED(palette
) )
267 wxCHECK_MSG( Ok(), FALSE
, _T("invalid bitmap") );
269 if (type
== wxBITMAP_TYPE_PNG
)
271 wxImage
image( *this );
272 if (image
.Ok()) return image
.SaveFile( name
, type
);
278 bool wxBitmap::LoadFile( const wxString
&name
, int type
)
282 if (!wxFileExists(name
)) return FALSE
;
284 if (type
== wxBITMAP_TYPE_XPM
)
286 m_refData
= new wxBitmapRefData();
288 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
289 GdkWindow
*parent
= (GdkWindow
*) &gdk_root_parent
;
291 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm( parent
, &mask
, NULL
, name
.fn_str() );
295 M_BMPDATA
->m_mask
= new wxMask();
296 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
299 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
300 M_BMPDATA
->m_bpp
= gdk_window_get_visual( parent
)->depth
;
302 else if (type
== wxBITMAP_TYPE_PNG
)
305 image
.LoadFile( name
, type
);
306 if (image
.Ok()) *this = image
.ConvertToBitmap();
308 else if (type
== wxBITMAP_TYPE_BMP
)
311 image
.LoadFile( name
, type
);
312 if (image
.Ok()) *this = image
.ConvertToBitmap();
320 wxPalette
*wxBitmap::GetPalette(void) const
322 if (!Ok()) return (wxPalette
*) NULL
;
324 return M_BMPDATA
->m_palette
;
327 void wxBitmap::SetHeight( int height
)
329 if (!m_refData
) m_refData
= new wxBitmapRefData();
331 M_BMPDATA
->m_height
= height
;
334 void wxBitmap::SetWidth( int width
)
336 if (!m_refData
) m_refData
= new wxBitmapRefData();
338 M_BMPDATA
->m_width
= width
;
341 void wxBitmap::SetDepth( int depth
)
343 if (!m_refData
) m_refData
= new wxBitmapRefData();
345 M_BMPDATA
->m_bpp
= depth
;
348 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
350 if (!m_refData
) m_refData
= new wxBitmapRefData();
352 M_BMPDATA
->m_pixmap
= pixmap
;
355 GdkPixmap
*wxBitmap::GetPixmap(void) const
357 wxCHECK_MSG( Ok(), (GdkPixmap
*) NULL
, _T("invalid bitmap") );
359 return M_BMPDATA
->m_pixmap
;
362 GdkBitmap
*wxBitmap::GetBitmap(void) const
364 wxCHECK_MSG( Ok(), (GdkBitmap
*) NULL
, _T("invalid bitmap") );
366 return M_BMPDATA
->m_bitmap
;