]>
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"
17 #include "gdk/gdkprivate.h"
21 //-----------------------------------------------------------------------------
23 //-----------------------------------------------------------------------------
25 IMPLEMENT_DYNAMIC_CLASS(wxMask
,wxObject
)
29 m_bitmap
= (GdkBitmap
*) NULL
;
32 wxMask::wxMask( const wxBitmap
& WXUNUSED(bitmap
), const wxColour
& WXUNUSED(colour
) )
36 wxMask::wxMask( const wxBitmap
& WXUNUSED(bitmap
), int WXUNUSED(paletteIndex
) )
40 wxMask::wxMask( const wxBitmap
& WXUNUSED(bitmap
) )
46 if (m_bitmap
) gdk_bitmap_unref( m_bitmap
);
49 GdkBitmap
*wxMask::GetBitmap(void) const
54 //-----------------------------------------------------------------------------
56 //-----------------------------------------------------------------------------
58 class wxBitmapRefData
: public wxObjectRefData
61 wxBitmapRefData(void);
62 ~wxBitmapRefData(void);
73 wxBitmapRefData::wxBitmapRefData()
75 m_pixmap
= (GdkPixmap
*) NULL
;
76 m_bitmap
= (GdkBitmap
*) NULL
;
77 m_mask
= (wxMask
*) NULL
;
81 m_palette
= (wxPalette
*) NULL
;
84 wxBitmapRefData::~wxBitmapRefData()
86 if (m_pixmap
) gdk_pixmap_unref( m_pixmap
);
87 if (m_bitmap
) gdk_bitmap_unref( m_bitmap
);
88 if (m_mask
) delete m_mask
;
89 if (m_palette
) delete m_palette
;
92 //-----------------------------------------------------------------------------
94 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
96 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
100 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
103 wxBitmap::wxBitmap( int width
, int height
, int depth
)
105 wxCHECK_RET( (width
> 0) && (height
> 0), "invalid bitmap size" )
106 wxCHECK_RET( (depth
> 0) || (depth
== -1), "invalid bitmap depth" )
108 m_refData
= new wxBitmapRefData();
110 GdkWindow
*parent
= (GdkWindow
*) &gdk_root_parent
;
112 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
113 M_BMPDATA
->m_pixmap
= gdk_pixmap_new( parent
, width
, height
, depth
);
114 M_BMPDATA
->m_width
= width
;
115 M_BMPDATA
->m_height
= height
;
116 M_BMPDATA
->m_bpp
= gdk_window_get_visual( parent
)->depth
;
118 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
121 wxBitmap::wxBitmap( const char **bits
)
123 wxCHECK_RET( bits
!= NULL
, "invalid bitmap data" )
125 m_refData
= new wxBitmapRefData();
127 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
128 GdkWindow
*parent
= (GdkWindow
*) &gdk_root_parent
;
130 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm_d( parent
, &mask
, NULL
, (gchar
**) bits
);
134 M_BMPDATA
->m_mask
= new wxMask();
135 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
138 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
140 M_BMPDATA
->m_bpp
= gdk_window_get_visual( parent
)->depth
; // ?
141 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
144 wxBitmap::wxBitmap( char **bits
)
146 wxCHECK_RET( bits
!= NULL
, "invalid bitmap data" )
148 m_refData
= new wxBitmapRefData();
150 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
151 GdkWindow
*parent
= (GdkWindow
*) &gdk_root_parent
;
153 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm_d( parent
, &mask
, NULL
, (gchar
**) bits
);
155 wxCHECK_RET( M_BMPDATA
->m_pixmap
, "couldn't create pixmap" );
159 M_BMPDATA
->m_mask
= new wxMask();
160 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
163 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
165 M_BMPDATA
->m_bpp
= gdk_window_get_visual( parent
)->depth
; // ?
166 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
169 wxBitmap::wxBitmap( const wxBitmap
& bmp
)
173 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
176 wxBitmap::wxBitmap( const wxString
&filename
, int type
)
178 LoadFile( filename
, type
);
180 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
183 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
))
185 m_refData
= new wxBitmapRefData();
187 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
188 M_BMPDATA
->m_bitmap
=
189 gdk_bitmap_create_from_data( (GdkWindow
*) &gdk_root_parent
, (gchar
*) bits
, width
, height
);
190 M_BMPDATA
->m_width
= width
;
191 M_BMPDATA
->m_height
= height
;
192 M_BMPDATA
->m_bpp
= 1;
194 wxCHECK_RET( M_BMPDATA
->m_bitmap
, "couldn't create bitmap" );
196 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
199 wxBitmap::~wxBitmap()
201 if (wxTheBitmapList
) wxTheBitmapList
->DeleteObject(this);
204 wxBitmap
& wxBitmap::operator = ( const wxBitmap
& bmp
)
206 if (*this == bmp
) return (*this);
211 bool wxBitmap::operator == ( const wxBitmap
& bmp
)
213 return m_refData
== bmp
.m_refData
;
216 bool wxBitmap::operator != ( const wxBitmap
& bmp
)
218 return m_refData
!= bmp
.m_refData
;
221 bool wxBitmap::Ok(void) const
223 return (m_refData
!= NULL
);
226 int wxBitmap::GetHeight(void) const
228 wxCHECK_MSG( Ok(), -1, "invalid bitmap" );
230 return M_BMPDATA
->m_height
;
233 int wxBitmap::GetWidth(void) const
235 wxCHECK_MSG( Ok(), -1, "invalid bitmap" );
237 return M_BMPDATA
->m_width
;
240 int wxBitmap::GetDepth(void) const
242 wxCHECK_MSG( Ok(), -1, "invalid bitmap" );
244 return M_BMPDATA
->m_bpp
;
247 wxMask
*wxBitmap::GetMask(void) const
249 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, "invalid bitmap" );
251 return M_BMPDATA
->m_mask
;
254 void wxBitmap::SetMask( wxMask
*mask
)
256 wxCHECK_RET( Ok(), "invalid bitmap" );
258 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
260 M_BMPDATA
->m_mask
= mask
;
263 bool wxBitmap::SaveFile( const wxString
&name
, int type
, wxPalette
*WXUNUSED(palette
) )
265 wxCHECK_MSG( Ok(), FALSE
, "invalid bitmap" );
267 if (type
== wxBITMAP_TYPE_PNG
)
269 wxImage
image( *this );
270 if (image
.Ok()) return image
.SaveFile( name
, type
);
276 bool wxBitmap::LoadFile( const wxString
&name
, int type
)
280 if (!wxFileExists(name
)) return FALSE
;
282 if (type
== wxBITMAP_TYPE_XPM
)
284 m_refData
= new wxBitmapRefData();
286 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
287 GdkWindow
*parent
= (GdkWindow
*) &gdk_root_parent
;
289 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm( parent
, &mask
, NULL
, name
);
293 M_BMPDATA
->m_mask
= new wxMask();
294 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
297 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
298 M_BMPDATA
->m_bpp
= gdk_window_get_visual( parent
)->depth
;
300 else if (type
== wxBITMAP_TYPE_PNG
)
303 image
.LoadFile( name
, type
);
304 if (image
.Ok()) *this = image
.ConvertToBitmap();
306 else if (type
== wxBITMAP_TYPE_BMP
)
309 image
.LoadFile( name
, type
);
310 if (image
.Ok()) *this = image
.ConvertToBitmap();
318 wxPalette
*wxBitmap::GetPalette(void) const
320 if (!Ok()) return (wxPalette
*) NULL
;
322 return M_BMPDATA
->m_palette
;
325 void wxBitmap::SetHeight( int height
)
327 if (!m_refData
) m_refData
= new wxBitmapRefData();
329 M_BMPDATA
->m_height
= height
;
332 void wxBitmap::SetWidth( int width
)
334 if (!m_refData
) m_refData
= new wxBitmapRefData();
336 M_BMPDATA
->m_width
= width
;
339 void wxBitmap::SetDepth( int depth
)
341 if (!m_refData
) m_refData
= new wxBitmapRefData();
343 M_BMPDATA
->m_bpp
= depth
;
346 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
348 if (!m_refData
) m_refData
= new wxBitmapRefData();
350 M_BMPDATA
->m_pixmap
= pixmap
;
353 GdkPixmap
*wxBitmap::GetPixmap(void) const
355 wxCHECK_MSG( Ok(), (GdkPixmap
*) NULL
, "invalid bitmap" );
357 return M_BMPDATA
->m_pixmap
;
360 GdkBitmap
*wxBitmap::GetBitmap(void) const
362 wxCHECK_MSG( Ok(), (GdkBitmap
*) NULL
, "invalid bitmap" );
364 return M_BMPDATA
->m_bitmap
;