]>
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 m_bitmap
= (GdkBitmap
*) NULL
;
37 Create( bitmap
, colour
);
40 wxMask::wxMask( const wxBitmap
& bitmap
, int paletteIndex
)
42 m_bitmap
= (GdkBitmap
*) NULL
;
43 Create( bitmap
, paletteIndex
);
46 wxMask::wxMask( const wxBitmap
& bitmap
)
48 m_bitmap
= (GdkBitmap
*) NULL
;
55 gdk_bitmap_unref( m_bitmap
);
58 bool wxMask::Create( const wxBitmap
& WXUNUSED(bitmap
),
59 const wxColour
& WXUNUSED(colour
) )
63 gdk_bitmap_unref( m_bitmap
);
64 m_bitmap
= (GdkBitmap
*) NULL
;
67 wxFAIL_MSG( wxT("TODO") );
72 bool wxMask::Create( const wxBitmap
& WXUNUSED(bitmap
),
73 int WXUNUSED(paletteIndex
) )
77 gdk_bitmap_unref( m_bitmap
);
78 m_bitmap
= (GdkBitmap
*) NULL
;
81 wxFAIL_MSG( wxT("not implemented") );
86 bool wxMask::Create( const wxBitmap
& bitmap
)
90 gdk_bitmap_unref( m_bitmap
);
91 m_bitmap
= (GdkBitmap
*) NULL
;
94 if (!bitmap
.Ok()) return FALSE
;
96 wxCHECK_MSG( bitmap
.GetBitmap(), FALSE
, wxT("Cannot create mask from colour bitmap") );
98 m_bitmap
= gdk_pixmap_new( (GdkWindow
*) &gdk_root_parent
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
100 if (!m_bitmap
) return FALSE
;
102 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
104 gdk_draw_bitmap( m_bitmap
, gc
, bitmap
.GetBitmap(), 0, 0, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight() );
111 GdkBitmap
*wxMask::GetBitmap() const
116 //-----------------------------------------------------------------------------
118 //-----------------------------------------------------------------------------
120 class wxBitmapRefData
: public wxObjectRefData
132 wxPalette
*m_palette
;
135 wxBitmapRefData::wxBitmapRefData()
137 m_pixmap
= (GdkPixmap
*) NULL
;
138 m_bitmap
= (GdkBitmap
*) NULL
;
139 m_mask
= (wxMask
*) NULL
;
143 m_palette
= (wxPalette
*) NULL
;
146 wxBitmapRefData::~wxBitmapRefData()
148 if (m_pixmap
) gdk_pixmap_unref( m_pixmap
);
149 if (m_bitmap
) gdk_bitmap_unref( m_bitmap
);
150 if (m_mask
) delete m_mask
;
151 if (m_palette
) delete m_palette
;
154 //-----------------------------------------------------------------------------
156 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
158 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
162 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
165 wxBitmap::wxBitmap( int width
, int height
, int depth
)
167 wxCHECK_RET( (width
> 0) && (height
> 0), wxT("invalid bitmap size") )
169 GdkWindow
*parent
= (GdkWindow
*) &gdk_root_parent
;
170 if (depth
== -1) depth
= gdk_window_get_visual( parent
)->depth
;
172 wxCHECK_RET( (depth
== gdk_window_get_visual( parent
)->depth
) ||
173 (depth
== 1), wxT("invalid bitmap depth") )
175 m_refData
= new wxBitmapRefData();
176 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
177 M_BMPDATA
->m_width
= width
;
178 M_BMPDATA
->m_height
= height
;
181 M_BMPDATA
->m_bitmap
= gdk_pixmap_new( parent
, width
, height
, 1 );
182 M_BMPDATA
->m_bpp
= 1;
186 M_BMPDATA
->m_pixmap
= gdk_pixmap_new( parent
, width
, height
, depth
);
187 M_BMPDATA
->m_bpp
= gdk_window_get_visual( parent
)->depth
;
190 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
193 wxBitmap::wxBitmap( const char **bits
)
195 wxCHECK_RET( bits
!= NULL
, wxT("invalid bitmap data") )
197 m_refData
= new wxBitmapRefData();
199 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
200 GdkWindow
*parent
= (GdkWindow
*) &gdk_root_parent
;
202 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm_d( parent
, &mask
, NULL
, (gchar
**) bits
);
206 M_BMPDATA
->m_mask
= new wxMask();
207 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
210 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
212 M_BMPDATA
->m_bpp
= gdk_window_get_visual( parent
)->depth
; // ?
213 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
216 wxBitmap::wxBitmap( char **bits
)
218 wxCHECK_RET( bits
!= NULL
, wxT("invalid bitmap data") )
220 m_refData
= new wxBitmapRefData();
222 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
223 GdkWindow
*parent
= (GdkWindow
*) &gdk_root_parent
;
225 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm_d( parent
, &mask
, NULL
, (gchar
**) bits
);
227 wxCHECK_RET( M_BMPDATA
->m_pixmap
, wxT("couldn't create pixmap") );
231 M_BMPDATA
->m_mask
= new wxMask();
232 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
235 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
237 M_BMPDATA
->m_bpp
= gdk_window_get_visual( parent
)->depth
; // ?
238 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
241 wxBitmap::wxBitmap( const wxBitmap
& bmp
)
245 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
248 wxBitmap::wxBitmap( const wxString
&filename
, int type
)
250 LoadFile( filename
, type
);
252 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
255 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
))
257 m_refData
= new wxBitmapRefData();
259 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
260 M_BMPDATA
->m_bitmap
=
261 gdk_bitmap_create_from_data( (GdkWindow
*) &gdk_root_parent
, (gchar
*) bits
, width
, height
);
262 M_BMPDATA
->m_width
= width
;
263 M_BMPDATA
->m_height
= height
;
264 M_BMPDATA
->m_bpp
= 1;
266 wxCHECK_RET( M_BMPDATA
->m_bitmap
, wxT("couldn't create bitmap") );
268 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
271 wxBitmap::~wxBitmap()
273 if (wxTheBitmapList
) wxTheBitmapList
->DeleteObject(this);
276 wxBitmap
& wxBitmap::operator = ( const wxBitmap
& bmp
)
278 if (*this == bmp
) return (*this);
283 bool wxBitmap::operator == ( const wxBitmap
& bmp
)
285 return m_refData
== bmp
.m_refData
;
288 bool wxBitmap::operator != ( const wxBitmap
& bmp
)
290 return m_refData
!= bmp
.m_refData
;
293 bool wxBitmap::Ok() const
295 return (m_refData
!= NULL
);
298 int wxBitmap::GetHeight() const
300 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
302 return M_BMPDATA
->m_height
;
305 int wxBitmap::GetWidth() const
307 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
309 return M_BMPDATA
->m_width
;
312 int wxBitmap::GetDepth() const
314 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
316 return M_BMPDATA
->m_bpp
;
319 wxMask
*wxBitmap::GetMask() const
321 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
323 return M_BMPDATA
->m_mask
;
326 void wxBitmap::SetMask( wxMask
*mask
)
328 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
330 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
332 M_BMPDATA
->m_mask
= mask
;
335 bool wxBitmap::SaveFile( const wxString
&name
, int type
, wxPalette
*WXUNUSED(palette
) )
337 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid bitmap") );
339 // Try to save the bitmap via wxImage handlers:
341 wxImage
image( *this );
342 if (image
.Ok()) return image
.SaveFile( name
, type
);
348 bool wxBitmap::LoadFile( const wxString
&name
, int type
)
352 if (!wxFileExists(name
)) return FALSE
;
354 if (type
== wxBITMAP_TYPE_XPM
)
356 m_refData
= new wxBitmapRefData();
358 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
359 GdkWindow
*parent
= (GdkWindow
*) &gdk_root_parent
;
361 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm( parent
, &mask
, NULL
, name
.fn_str() );
365 M_BMPDATA
->m_mask
= new wxMask();
366 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
369 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
370 M_BMPDATA
->m_bpp
= gdk_window_get_visual( parent
)->depth
;
372 else // try if wxImage can load it
375 if (!image
.LoadFile( name
, type
)) return FALSE
;
376 if (image
.Ok()) *this = image
.ConvertToBitmap();
383 wxPalette
*wxBitmap::GetPalette() const
385 if (!Ok()) return (wxPalette
*) NULL
;
387 return M_BMPDATA
->m_palette
;
390 void wxBitmap::SetHeight( int height
)
392 if (!m_refData
) m_refData
= new wxBitmapRefData();
394 M_BMPDATA
->m_height
= height
;
397 void wxBitmap::SetWidth( int width
)
399 if (!m_refData
) m_refData
= new wxBitmapRefData();
401 M_BMPDATA
->m_width
= width
;
404 void wxBitmap::SetDepth( int depth
)
406 if (!m_refData
) m_refData
= new wxBitmapRefData();
408 M_BMPDATA
->m_bpp
= depth
;
411 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
413 if (!m_refData
) m_refData
= new wxBitmapRefData();
415 M_BMPDATA
->m_pixmap
= pixmap
;
418 GdkPixmap
*wxBitmap::GetPixmap() const
420 wxCHECK_MSG( Ok(), (GdkPixmap
*) NULL
, wxT("invalid bitmap") );
422 return M_BMPDATA
->m_pixmap
;
425 GdkBitmap
*wxBitmap::GetBitmap() const
427 wxCHECK_MSG( Ok(), (GdkBitmap
*) NULL
, wxT("invalid bitmap") );
429 return M_BMPDATA
->m_bitmap
;