]>
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 Create( bitmap
, colour
);
39 wxMask::wxMask( const wxBitmap
& bitmap
, int paletteIndex
)
41 Create( bitmap
, paletteIndex
);
44 wxMask::wxMask( const wxBitmap
& bitmap
)
51 if (m_bitmap
) gdk_bitmap_unref( m_bitmap
);
54 bool wxMask::Create( const wxBitmap
& bitmap
, const wxColour
& colour
)
58 gdk_bitmap_unref( m_bitmap
);
59 m_bitmap
= (GdkBitmap
*) NULL
;
62 wxFAIL_MSG( _T("TODO") );
67 bool wxMask::Create( const wxBitmap
& WXUNUSED(bitmap
), int WXUNUSED(paletteIndex
) )
71 gdk_bitmap_unref( m_bitmap
);
72 m_bitmap
= (GdkBitmap
*) NULL
;
75 wxFAIL_MSG( _T("not implemented") );
80 bool wxMask::Create( const wxBitmap
& bitmap
)
84 gdk_bitmap_unref( m_bitmap
);
85 m_bitmap
= (GdkBitmap
*) NULL
;
88 if (!bitmap
.Ok()) return FALSE
;
90 wxCHECK_MSG( bitmap
.GetBitmap(), FALSE
, _T("Cannot create mask from colour bitmap") );
92 m_bitmap
= gdk_pixmap_new( (GdkWindow
*) &gdk_root_parent
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
94 if (!m_bitmap
) return FALSE
;
96 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
98 gdk_draw_bitmap( m_bitmap
, gc
, bitmap
.GetBitmap(), 0, 0, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight() );
105 GdkBitmap
*wxMask::GetBitmap() const
110 //-----------------------------------------------------------------------------
112 //-----------------------------------------------------------------------------
114 class wxBitmapRefData
: public wxObjectRefData
126 wxPalette
*m_palette
;
129 wxBitmapRefData::wxBitmapRefData()
131 m_pixmap
= (GdkPixmap
*) NULL
;
132 m_bitmap
= (GdkBitmap
*) NULL
;
133 m_mask
= (wxMask
*) NULL
;
137 m_palette
= (wxPalette
*) NULL
;
140 wxBitmapRefData::~wxBitmapRefData()
142 if (m_pixmap
) gdk_pixmap_unref( m_pixmap
);
143 if (m_bitmap
) gdk_bitmap_unref( m_bitmap
);
144 if (m_mask
) delete m_mask
;
145 if (m_palette
) delete m_palette
;
148 //-----------------------------------------------------------------------------
150 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
152 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
156 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
159 wxBitmap::wxBitmap( int width
, int height
, int depth
)
161 wxCHECK_RET( (width
> 0) && (height
> 0), _T("invalid bitmap size") )
163 GdkWindow
*parent
= (GdkWindow
*) &gdk_root_parent
;
164 if (depth
== -1) depth
= gdk_window_get_visual( parent
)->depth
;
166 wxCHECK_RET( (depth
== gdk_window_get_visual( parent
)->depth
) ||
167 (depth
== 1), _T("invalid bitmap depth") )
169 m_refData
= new wxBitmapRefData();
170 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
171 M_BMPDATA
->m_width
= width
;
172 M_BMPDATA
->m_height
= height
;
175 M_BMPDATA
->m_bitmap
= gdk_pixmap_new( parent
, width
, height
, 1 );
176 M_BMPDATA
->m_bpp
= 1;
180 M_BMPDATA
->m_pixmap
= gdk_pixmap_new( parent
, width
, height
, depth
);
181 M_BMPDATA
->m_bpp
= gdk_window_get_visual( parent
)->depth
;
184 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
187 wxBitmap::wxBitmap( const char **bits
)
189 wxCHECK_RET( bits
!= NULL
, _T("invalid bitmap data") )
191 m_refData
= new wxBitmapRefData();
193 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
194 GdkWindow
*parent
= (GdkWindow
*) &gdk_root_parent
;
196 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm_d( parent
, &mask
, NULL
, (gchar
**) bits
);
200 M_BMPDATA
->m_mask
= new wxMask();
201 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
204 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
206 M_BMPDATA
->m_bpp
= gdk_window_get_visual( parent
)->depth
; // ?
207 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
210 wxBitmap::wxBitmap( char **bits
)
212 wxCHECK_RET( bits
!= NULL
, _T("invalid bitmap data") )
214 m_refData
= new wxBitmapRefData();
216 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
217 GdkWindow
*parent
= (GdkWindow
*) &gdk_root_parent
;
219 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm_d( parent
, &mask
, NULL
, (gchar
**) bits
);
221 wxCHECK_RET( M_BMPDATA
->m_pixmap
, _T("couldn't create pixmap") );
225 M_BMPDATA
->m_mask
= new wxMask();
226 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
229 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
231 M_BMPDATA
->m_bpp
= gdk_window_get_visual( parent
)->depth
; // ?
232 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
235 wxBitmap::wxBitmap( const wxBitmap
& bmp
)
239 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
242 wxBitmap::wxBitmap( const wxString
&filename
, int type
)
244 LoadFile( filename
, type
);
246 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
249 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
))
251 m_refData
= new wxBitmapRefData();
253 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
254 M_BMPDATA
->m_bitmap
=
255 gdk_bitmap_create_from_data( (GdkWindow
*) &gdk_root_parent
, (gchar
*) bits
, width
, height
);
256 M_BMPDATA
->m_width
= width
;
257 M_BMPDATA
->m_height
= height
;
258 M_BMPDATA
->m_bpp
= 1;
260 wxCHECK_RET( M_BMPDATA
->m_bitmap
, _T("couldn't create bitmap") );
262 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
265 wxBitmap::~wxBitmap()
267 if (wxTheBitmapList
) wxTheBitmapList
->DeleteObject(this);
270 wxBitmap
& wxBitmap::operator = ( const wxBitmap
& bmp
)
272 if (*this == bmp
) return (*this);
277 bool wxBitmap::operator == ( const wxBitmap
& bmp
)
279 return m_refData
== bmp
.m_refData
;
282 bool wxBitmap::operator != ( const wxBitmap
& bmp
)
284 return m_refData
!= bmp
.m_refData
;
287 bool wxBitmap::Ok() const
289 return (m_refData
!= NULL
);
292 int wxBitmap::GetHeight() const
294 wxCHECK_MSG( Ok(), -1, _T("invalid bitmap") );
296 return M_BMPDATA
->m_height
;
299 int wxBitmap::GetWidth() const
301 wxCHECK_MSG( Ok(), -1, _T("invalid bitmap") );
303 return M_BMPDATA
->m_width
;
306 int wxBitmap::GetDepth() const
308 wxCHECK_MSG( Ok(), -1, _T("invalid bitmap") );
310 return M_BMPDATA
->m_bpp
;
313 wxMask
*wxBitmap::GetMask() const
315 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, _T("invalid bitmap") );
317 return M_BMPDATA
->m_mask
;
320 void wxBitmap::SetMask( wxMask
*mask
)
322 wxCHECK_RET( Ok(), _T("invalid bitmap") );
324 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
326 M_BMPDATA
->m_mask
= mask
;
329 bool wxBitmap::SaveFile( const wxString
&name
, int type
, wxPalette
*WXUNUSED(palette
) )
331 wxCHECK_MSG( Ok(), FALSE
, _T("invalid bitmap") );
333 if (type
== wxBITMAP_TYPE_PNG
)
335 wxImage
image( *this );
336 if (image
.Ok()) return image
.SaveFile( name
, type
);
342 bool wxBitmap::LoadFile( const wxString
&name
, int type
)
346 if (!wxFileExists(name
)) return FALSE
;
348 if (type
== wxBITMAP_TYPE_XPM
)
350 m_refData
= new wxBitmapRefData();
352 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
353 GdkWindow
*parent
= (GdkWindow
*) &gdk_root_parent
;
355 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm( parent
, &mask
, NULL
, name
.fn_str() );
359 M_BMPDATA
->m_mask
= new wxMask();
360 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
363 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
364 M_BMPDATA
->m_bpp
= gdk_window_get_visual( parent
)->depth
;
366 else if (type
== wxBITMAP_TYPE_PNG
)
369 image
.LoadFile( name
, type
);
370 if (image
.Ok()) *this = image
.ConvertToBitmap();
372 else if (type
== wxBITMAP_TYPE_BMP
)
375 image
.LoadFile( name
, type
);
376 if (image
.Ok()) *this = image
.ConvertToBitmap();
384 wxPalette
*wxBitmap::GetPalette() const
386 if (!Ok()) return (wxPalette
*) NULL
;
388 return M_BMPDATA
->m_palette
;
391 void wxBitmap::SetHeight( int height
)
393 if (!m_refData
) m_refData
= new wxBitmapRefData();
395 M_BMPDATA
->m_height
= height
;
398 void wxBitmap::SetWidth( int width
)
400 if (!m_refData
) m_refData
= new wxBitmapRefData();
402 M_BMPDATA
->m_width
= width
;
405 void wxBitmap::SetDepth( int depth
)
407 if (!m_refData
) m_refData
= new wxBitmapRefData();
409 M_BMPDATA
->m_bpp
= depth
;
412 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
414 if (!m_refData
) m_refData
= new wxBitmapRefData();
416 M_BMPDATA
->m_pixmap
= pixmap
;
419 GdkPixmap
*wxBitmap::GetPixmap() const
421 wxCHECK_MSG( Ok(), (GdkPixmap
*) NULL
, _T("invalid bitmap") );
423 return M_BMPDATA
->m_pixmap
;
426 GdkBitmap
*wxBitmap::GetBitmap() const
428 wxCHECK_MSG( Ok(), (GdkBitmap
*) NULL
, _T("invalid bitmap") );
430 return M_BMPDATA
->m_bitmap
;