]>
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") )
109 GdkWindow
*parent
= (GdkWindow
*) &gdk_root_parent
;
110 if (depth
== -1) depth
= gdk_window_get_visual( parent
)->depth
;
112 wxCHECK_RET( (depth
== gdk_window_get_visual( parent
)->depth
) ||
113 (depth
== 1), _T("invalid bitmap depth") )
115 m_refData
= new wxBitmapRefData();
116 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
117 M_BMPDATA
->m_width
= width
;
118 M_BMPDATA
->m_height
= height
;
121 M_BMPDATA
->m_bitmap
= gdk_pixmap_new( parent
, width
, height
, 1 );
122 M_BMPDATA
->m_bpp
= 1;
126 M_BMPDATA
->m_pixmap
= gdk_pixmap_new( parent
, width
, height
, depth
);
127 M_BMPDATA
->m_bpp
= gdk_window_get_visual( parent
)->depth
;
130 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
133 wxBitmap::wxBitmap( const char **bits
)
135 wxCHECK_RET( bits
!= NULL
, _T("invalid bitmap data") )
137 m_refData
= new wxBitmapRefData();
139 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
140 GdkWindow
*parent
= (GdkWindow
*) &gdk_root_parent
;
142 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm_d( parent
, &mask
, NULL
, (gchar
**) bits
);
146 M_BMPDATA
->m_mask
= new wxMask();
147 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
150 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
152 M_BMPDATA
->m_bpp
= gdk_window_get_visual( parent
)->depth
; // ?
153 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
156 wxBitmap::wxBitmap( char **bits
)
158 wxCHECK_RET( bits
!= NULL
, _T("invalid bitmap data") )
160 m_refData
= new wxBitmapRefData();
162 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
163 GdkWindow
*parent
= (GdkWindow
*) &gdk_root_parent
;
165 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm_d( parent
, &mask
, NULL
, (gchar
**) bits
);
167 wxCHECK_RET( M_BMPDATA
->m_pixmap
, _T("couldn't create pixmap") );
171 M_BMPDATA
->m_mask
= new wxMask();
172 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
175 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
177 M_BMPDATA
->m_bpp
= gdk_window_get_visual( parent
)->depth
; // ?
178 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
181 wxBitmap::wxBitmap( const wxBitmap
& bmp
)
185 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
188 wxBitmap::wxBitmap( const wxString
&filename
, int type
)
190 LoadFile( filename
, type
);
192 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
195 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
))
197 m_refData
= new wxBitmapRefData();
199 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
200 M_BMPDATA
->m_bitmap
=
201 gdk_bitmap_create_from_data( (GdkWindow
*) &gdk_root_parent
, (gchar
*) bits
, width
, height
);
202 M_BMPDATA
->m_width
= width
;
203 M_BMPDATA
->m_height
= height
;
204 M_BMPDATA
->m_bpp
= 1;
206 wxCHECK_RET( M_BMPDATA
->m_bitmap
, _T("couldn't create bitmap") );
208 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
211 wxBitmap::~wxBitmap()
213 if (wxTheBitmapList
) wxTheBitmapList
->DeleteObject(this);
216 wxBitmap
& wxBitmap::operator = ( const wxBitmap
& bmp
)
218 if (*this == bmp
) return (*this);
223 bool wxBitmap::operator == ( const wxBitmap
& bmp
)
225 return m_refData
== bmp
.m_refData
;
228 bool wxBitmap::operator != ( const wxBitmap
& bmp
)
230 return m_refData
!= bmp
.m_refData
;
233 bool wxBitmap::Ok(void) const
235 return (m_refData
!= NULL
);
238 int wxBitmap::GetHeight(void) const
240 wxCHECK_MSG( Ok(), -1, _T("invalid bitmap") );
242 return M_BMPDATA
->m_height
;
245 int wxBitmap::GetWidth(void) const
247 wxCHECK_MSG( Ok(), -1, _T("invalid bitmap") );
249 return M_BMPDATA
->m_width
;
252 int wxBitmap::GetDepth(void) const
254 wxCHECK_MSG( Ok(), -1, _T("invalid bitmap") );
256 return M_BMPDATA
->m_bpp
;
259 wxMask
*wxBitmap::GetMask(void) const
261 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, _T("invalid bitmap") );
263 return M_BMPDATA
->m_mask
;
266 void wxBitmap::SetMask( wxMask
*mask
)
268 wxCHECK_RET( Ok(), _T("invalid bitmap") );
270 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
272 M_BMPDATA
->m_mask
= mask
;
275 bool wxBitmap::SaveFile( const wxString
&name
, int type
, wxPalette
*WXUNUSED(palette
) )
277 wxCHECK_MSG( Ok(), FALSE
, _T("invalid bitmap") );
279 if (type
== wxBITMAP_TYPE_PNG
)
281 wxImage
image( *this );
282 if (image
.Ok()) return image
.SaveFile( name
, type
);
288 bool wxBitmap::LoadFile( const wxString
&name
, int type
)
292 if (!wxFileExists(name
)) return FALSE
;
294 if (type
== wxBITMAP_TYPE_XPM
)
296 m_refData
= new wxBitmapRefData();
298 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
299 GdkWindow
*parent
= (GdkWindow
*) &gdk_root_parent
;
301 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm( parent
, &mask
, NULL
, name
.fn_str() );
305 M_BMPDATA
->m_mask
= new wxMask();
306 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
309 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
310 M_BMPDATA
->m_bpp
= gdk_window_get_visual( parent
)->depth
;
312 else if (type
== wxBITMAP_TYPE_PNG
)
315 image
.LoadFile( name
, type
);
316 if (image
.Ok()) *this = image
.ConvertToBitmap();
318 else if (type
== wxBITMAP_TYPE_BMP
)
321 image
.LoadFile( name
, type
);
322 if (image
.Ok()) *this = image
.ConvertToBitmap();
330 wxPalette
*wxBitmap::GetPalette(void) const
332 if (!Ok()) return (wxPalette
*) NULL
;
334 return M_BMPDATA
->m_palette
;
337 void wxBitmap::SetHeight( int height
)
339 if (!m_refData
) m_refData
= new wxBitmapRefData();
341 M_BMPDATA
->m_height
= height
;
344 void wxBitmap::SetWidth( int width
)
346 if (!m_refData
) m_refData
= new wxBitmapRefData();
348 M_BMPDATA
->m_width
= width
;
351 void wxBitmap::SetDepth( int depth
)
353 if (!m_refData
) m_refData
= new wxBitmapRefData();
355 M_BMPDATA
->m_bpp
= depth
;
358 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
360 if (!m_refData
) m_refData
= new wxBitmapRefData();
362 M_BMPDATA
->m_pixmap
= pixmap
;
365 GdkPixmap
*wxBitmap::GetPixmap(void) const
367 wxCHECK_MSG( Ok(), (GdkPixmap
*) NULL
, _T("invalid bitmap") );
369 return M_BMPDATA
->m_pixmap
;
372 GdkBitmap
*wxBitmap::GetBitmap(void) const
374 wxCHECK_MSG( Ok(), (GdkBitmap
*) NULL
, _T("invalid bitmap") );
376 return M_BMPDATA
->m_bitmap
;