]>
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
& 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
& WXUNUSED(bitmap
),
55 const wxColour
& WXUNUSED(colour
) )
59 gdk_bitmap_unref( m_bitmap
);
60 m_bitmap
= (GdkBitmap
*) NULL
;
63 wxFAIL_MSG( wxT("TODO") );
68 bool wxMask::Create( const wxBitmap
& WXUNUSED(bitmap
),
69 int WXUNUSED(paletteIndex
) )
73 gdk_bitmap_unref( m_bitmap
);
74 m_bitmap
= (GdkBitmap
*) NULL
;
77 wxFAIL_MSG( wxT("not implemented") );
82 bool wxMask::Create( const wxBitmap
& bitmap
)
86 gdk_bitmap_unref( m_bitmap
);
87 m_bitmap
= (GdkBitmap
*) NULL
;
90 if (!bitmap
.Ok()) return FALSE
;
92 wxCHECK_MSG( bitmap
.GetBitmap(), FALSE
, wxT("Cannot create mask from colour bitmap") );
94 m_bitmap
= gdk_pixmap_new( (GdkWindow
*) &gdk_root_parent
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
96 if (!m_bitmap
) return FALSE
;
98 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
100 gdk_draw_bitmap( m_bitmap
, gc
, bitmap
.GetBitmap(), 0, 0, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight() );
107 GdkBitmap
*wxMask::GetBitmap() const
112 //-----------------------------------------------------------------------------
114 //-----------------------------------------------------------------------------
116 class wxBitmapRefData
: public wxObjectRefData
128 wxPalette
*m_palette
;
131 wxBitmapRefData::wxBitmapRefData()
133 m_pixmap
= (GdkPixmap
*) NULL
;
134 m_bitmap
= (GdkBitmap
*) NULL
;
135 m_mask
= (wxMask
*) NULL
;
139 m_palette
= (wxPalette
*) NULL
;
142 wxBitmapRefData::~wxBitmapRefData()
144 if (m_pixmap
) gdk_pixmap_unref( m_pixmap
);
145 if (m_bitmap
) gdk_bitmap_unref( m_bitmap
);
146 if (m_mask
) delete m_mask
;
147 if (m_palette
) delete m_palette
;
150 //-----------------------------------------------------------------------------
152 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
154 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
158 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
161 wxBitmap::wxBitmap( int width
, int height
, int depth
)
163 wxCHECK_RET( (width
> 0) && (height
> 0), wxT("invalid bitmap size") )
165 GdkWindow
*parent
= (GdkWindow
*) &gdk_root_parent
;
166 if (depth
== -1) depth
= gdk_window_get_visual( parent
)->depth
;
168 wxCHECK_RET( (depth
== gdk_window_get_visual( parent
)->depth
) ||
169 (depth
== 1), wxT("invalid bitmap depth") )
171 m_refData
= new wxBitmapRefData();
172 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
173 M_BMPDATA
->m_width
= width
;
174 M_BMPDATA
->m_height
= height
;
177 M_BMPDATA
->m_bitmap
= gdk_pixmap_new( parent
, width
, height
, 1 );
178 M_BMPDATA
->m_bpp
= 1;
182 M_BMPDATA
->m_pixmap
= gdk_pixmap_new( parent
, width
, height
, depth
);
183 M_BMPDATA
->m_bpp
= gdk_window_get_visual( parent
)->depth
;
186 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
189 wxBitmap::wxBitmap( const char **bits
)
191 wxCHECK_RET( bits
!= NULL
, wxT("invalid bitmap data") )
193 m_refData
= new wxBitmapRefData();
195 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
196 GdkWindow
*parent
= (GdkWindow
*) &gdk_root_parent
;
198 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm_d( parent
, &mask
, NULL
, (gchar
**) bits
);
202 M_BMPDATA
->m_mask
= new wxMask();
203 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
206 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
208 M_BMPDATA
->m_bpp
= gdk_window_get_visual( parent
)->depth
; // ?
209 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
212 wxBitmap::wxBitmap( char **bits
)
214 wxCHECK_RET( bits
!= NULL
, wxT("invalid bitmap data") )
216 m_refData
= new wxBitmapRefData();
218 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
219 GdkWindow
*parent
= (GdkWindow
*) &gdk_root_parent
;
221 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm_d( parent
, &mask
, NULL
, (gchar
**) bits
);
223 wxCHECK_RET( M_BMPDATA
->m_pixmap
, wxT("couldn't create pixmap") );
227 M_BMPDATA
->m_mask
= new wxMask();
228 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
231 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
233 M_BMPDATA
->m_bpp
= gdk_window_get_visual( parent
)->depth
; // ?
234 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
237 wxBitmap::wxBitmap( const wxBitmap
& bmp
)
241 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
244 wxBitmap::wxBitmap( const wxString
&filename
, int type
)
246 LoadFile( filename
, type
);
248 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
251 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
))
253 m_refData
= new wxBitmapRefData();
255 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
256 M_BMPDATA
->m_bitmap
=
257 gdk_bitmap_create_from_data( (GdkWindow
*) &gdk_root_parent
, (gchar
*) bits
, width
, height
);
258 M_BMPDATA
->m_width
= width
;
259 M_BMPDATA
->m_height
= height
;
260 M_BMPDATA
->m_bpp
= 1;
262 wxCHECK_RET( M_BMPDATA
->m_bitmap
, wxT("couldn't create bitmap") );
264 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
267 wxBitmap::~wxBitmap()
269 if (wxTheBitmapList
) wxTheBitmapList
->DeleteObject(this);
272 wxBitmap
& wxBitmap::operator = ( const wxBitmap
& bmp
)
274 if (*this == bmp
) return (*this);
279 bool wxBitmap::operator == ( const wxBitmap
& bmp
)
281 return m_refData
== bmp
.m_refData
;
284 bool wxBitmap::operator != ( const wxBitmap
& bmp
)
286 return m_refData
!= bmp
.m_refData
;
289 bool wxBitmap::Ok() const
291 return (m_refData
!= NULL
);
294 int wxBitmap::GetHeight() const
296 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
298 return M_BMPDATA
->m_height
;
301 int wxBitmap::GetWidth() const
303 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
305 return M_BMPDATA
->m_width
;
308 int wxBitmap::GetDepth() const
310 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
312 return M_BMPDATA
->m_bpp
;
315 wxMask
*wxBitmap::GetMask() const
317 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
319 return M_BMPDATA
->m_mask
;
322 void wxBitmap::SetMask( wxMask
*mask
)
324 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
326 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
328 M_BMPDATA
->m_mask
= mask
;
331 bool wxBitmap::SaveFile( const wxString
&name
, int type
, wxPalette
*WXUNUSED(palette
) )
333 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid bitmap") );
335 // Try to save the bitmap via wxImage handlers:
337 wxImage
image( *this );
338 if (image
.Ok()) return image
.SaveFile( name
, type
);
344 bool wxBitmap::LoadFile( const wxString
&name
, int type
)
348 if (!wxFileExists(name
)) return FALSE
;
350 if (type
== wxBITMAP_TYPE_XPM
)
352 m_refData
= new wxBitmapRefData();
354 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
355 GdkWindow
*parent
= (GdkWindow
*) &gdk_root_parent
;
357 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm( parent
, &mask
, NULL
, name
.fn_str() );
361 M_BMPDATA
->m_mask
= new wxMask();
362 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
365 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
366 M_BMPDATA
->m_bpp
= gdk_window_get_visual( parent
)->depth
;
368 else // try if wxImage can load it
371 if (!image
.LoadFile( name
, type
)) return FALSE
;
372 if (image
.Ok()) *this = image
.ConvertToBitmap();
379 wxPalette
*wxBitmap::GetPalette() const
381 if (!Ok()) return (wxPalette
*) NULL
;
383 return M_BMPDATA
->m_palette
;
386 void wxBitmap::SetHeight( int height
)
388 if (!m_refData
) m_refData
= new wxBitmapRefData();
390 M_BMPDATA
->m_height
= height
;
393 void wxBitmap::SetWidth( int width
)
395 if (!m_refData
) m_refData
= new wxBitmapRefData();
397 M_BMPDATA
->m_width
= width
;
400 void wxBitmap::SetDepth( int depth
)
402 if (!m_refData
) m_refData
= new wxBitmapRefData();
404 M_BMPDATA
->m_bpp
= depth
;
407 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
409 if (!m_refData
) m_refData
= new wxBitmapRefData();
411 M_BMPDATA
->m_pixmap
= pixmap
;
414 GdkPixmap
*wxBitmap::GetPixmap() const
416 wxCHECK_MSG( Ok(), (GdkPixmap
*) NULL
, wxT("invalid bitmap") );
418 return M_BMPDATA
->m_pixmap
;
421 GdkBitmap
*wxBitmap::GetBitmap() const
423 wxCHECK_MSG( Ok(), (GdkBitmap
*) NULL
, wxT("invalid bitmap") );
425 return M_BMPDATA
->m_bitmap
;