]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/bitmap.cpp
1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
7 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "bitmap.h"
15 #include "wx/bitmap.h"
17 #include "gdk/gdkprivate.h"
21 #include "../gdk_imlib/gdk_imlib.h"
22 #include "gdk/gdkx.h" // GDK_DISPLAY
24 #include <X11/Xutil.h>
28 //-----------------------------------------------------------------------------
30 //-----------------------------------------------------------------------------
32 IMPLEMENT_DYNAMIC_CLASS(wxMask
,wxObject
)
36 m_bitmap
= (GdkBitmap
*) NULL
;
39 wxMask::wxMask( const wxBitmap
& WXUNUSED(bitmap
), const wxColour
& WXUNUSED(colour
) )
43 wxMask::wxMask( const wxBitmap
& WXUNUSED(bitmap
), int WXUNUSED(paletteIndex
) )
47 wxMask::wxMask( const wxBitmap
& WXUNUSED(bitmap
) )
54 // do not delete the mask, gdk_imlib does it for you
56 if (m_bitmap
) gdk_bitmap_unref( m_bitmap
);
60 GdkBitmap
*wxMask::GetBitmap(void) const
65 //-----------------------------------------------------------------------------
67 //-----------------------------------------------------------------------------
69 // CMB 20/5/98: added m_bitmap for GdkBitmaps
70 class wxBitmapRefData
: public wxObjectRefData
74 wxBitmapRefData(void);
75 ~wxBitmapRefData(void);
84 GdkImlibImage
*m_image
;
89 wxBitmapRefData::wxBitmapRefData(void)
91 m_pixmap
= (GdkPixmap
*) NULL
;
92 m_bitmap
= (GdkBitmap
*) NULL
;
93 m_mask
= (wxMask
*) NULL
;
97 m_palette
= (wxPalette
*) NULL
;
99 m_image
= (GdkImlibImage
*) NULL
;
103 wxBitmapRefData::~wxBitmapRefData(void)
106 if (m_pixmap
) gdk_imlib_free_pixmap( m_pixmap
);
107 if (m_image
) gdk_imlib_kill_image( m_image
);
109 if (m_pixmap
) gdk_pixmap_unref( m_pixmap
);
111 if (m_bitmap
) gdk_bitmap_unref( m_bitmap
);
112 if (m_mask
) delete m_mask
;
113 if (m_palette
) delete m_palette
;
116 //-----------------------------------------------------------------------------
118 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
120 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
122 wxBitmap::wxBitmap(void)
124 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
127 wxBitmap::wxBitmap( int width
, int height
, int depth
)
129 m_refData
= new wxBitmapRefData();
130 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
131 M_BMPDATA
->m_pixmap
=
132 gdk_pixmap_new( (GdkWindow
*) &gdk_root_parent
, width
, height
, depth
);
133 M_BMPDATA
->m_width
= width
;
134 M_BMPDATA
->m_height
= height
;
135 M_BMPDATA
->m_bpp
= depth
;
137 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
140 wxBitmap::wxBitmap( char **bits
)
142 m_refData
= new wxBitmapRefData();
144 #ifndef USE_GDK_IMLIB
146 GdkBitmap
*mask
= NULL
;
148 M_BMPDATA
->m_pixmap
=
149 gdk_pixmap_create_from_xpm_d( (GdkWindow
*) &gdk_root_parent
, &mask
, NULL
, (gchar
**) bits
);
153 M_BMPDATA
->m_mask
= new wxMask();
154 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
157 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
161 M_BMPDATA
->m_image
= gdk_imlib_create_image_from_xpm_data( bits
);
166 M_BMPDATA
->m_bpp
= 24; // ?
168 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
171 wxBitmap::wxBitmap( const wxBitmap
& bmp
)
175 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
178 wxBitmap::wxBitmap( const wxBitmap
* bmp
)
180 if (bmp
) Ref( *bmp
);
182 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
185 wxBitmap::wxBitmap( const wxString
&filename
, int type
)
187 LoadFile( filename
, type
);
189 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
192 // CMB 15/5/98: add constructor for xbm bitmaps
193 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
))
195 m_refData
= new wxBitmapRefData();
197 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
198 M_BMPDATA
->m_bitmap
=
199 gdk_bitmap_create_from_data( (GdkWindow
*) &gdk_root_parent
, (gchar
*) bits
, width
, height
);
200 M_BMPDATA
->m_width
= width
;
201 M_BMPDATA
->m_height
= height
;
202 M_BMPDATA
->m_bpp
= 1;
204 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
207 wxBitmap::~wxBitmap(void)
209 if (wxTheBitmapList
) wxTheBitmapList
->DeleteObject(this);
212 wxBitmap
& wxBitmap::operator = ( const wxBitmap
& bmp
)
214 if (*this == bmp
) return (*this);
219 bool wxBitmap::operator == ( const wxBitmap
& bmp
)
221 return m_refData
== bmp
.m_refData
;
224 bool wxBitmap::operator != ( const wxBitmap
& bmp
)
226 return m_refData
!= bmp
.m_refData
;
229 bool wxBitmap::Ok(void) const
231 wxASSERT_MSG( m_refData
!= NULL
, "invalid bitmap" );
232 return (m_refData
!= NULL
);
235 int wxBitmap::GetHeight(void) const
238 return M_BMPDATA
->m_height
;
241 int wxBitmap::GetWidth(void) const
244 return M_BMPDATA
->m_width
;
247 int wxBitmap::GetDepth(void) const
250 return M_BMPDATA
->m_bpp
;
253 void wxBitmap::SetHeight( int height
)
257 wxFAIL_MSG( "wxBitmap::SetHeight not implemented" );
259 M_BMPDATA
->m_height
= height
;
262 void wxBitmap::SetWidth( int width
)
266 wxFAIL_MSG( "wxBitmap::SetWidth not implemented" );
268 M_BMPDATA
->m_width
= width
;
271 void wxBitmap::SetDepth( int depth
)
275 wxFAIL_MSG( "wxBitmap::SetDepth not implemented" );
277 M_BMPDATA
->m_bpp
= depth
;
280 wxMask
*wxBitmap::GetMask(void) const
282 if (!Ok()) return (wxMask
*) NULL
;
284 return M_BMPDATA
->m_mask
;
287 void wxBitmap::SetMask( wxMask
*mask
)
291 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
293 M_BMPDATA
->m_mask
= mask
;
296 void wxBitmap::Resize( int height
, int width
)
302 if (M_BMPDATA
->m_bitmap
) return; // not supported for bitmaps
304 if (!M_BMPDATA
->m_image
) RecreateImage();
306 if (M_BMPDATA
->m_pixmap
) gdk_imlib_free_pixmap( M_BMPDATA
->m_pixmap
);
307 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
309 GdkImlibImage
* image
= gdk_imlib_clone_scaled_image( M_BMPDATA
->m_image
, height
, width
);
310 gdk_imlib_destroy_image( M_BMPDATA
->m_image
);
311 M_BMPDATA
->m_image
= image
;
312 M_BMPDATA
->m_height
= height
;
313 M_BMPDATA
->m_width
= width
;
319 wxFAIL_MSG( "wxBitmap::Resize not implemented without GdkImlib" );
324 bool wxBitmap::SaveFile( const wxString
&name
, int WXUNUSED(type
),
325 wxPalette
*WXUNUSED(palette
) )
329 if (!Ok()) return FALSE
;
331 if (!M_BMPDATA
->m_image
) RecreateImage();
333 return gdk_imlib_save_image( M_BMPDATA
->m_image
, WXSTRINGCAST name
, (GdkImlibSaveInfo
*) NULL
);
337 wxFAIL_MSG( "wxBitmap::SaveFile not implemented without GdkImlib" );
344 bool wxBitmap::LoadFile( const wxString
&name
, int WXUNUSED(type
) )
349 m_refData
= new wxBitmapRefData();
351 M_BMPDATA
->m_image
= gdk_imlib_load_image( WXSTRINGCAST name
);
353 if (!M_BMPDATA
->m_image
)
361 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
362 M_BMPDATA
->m_bpp
= 24; // ?
368 wxFAIL_MSG( "wxBitmap::LoadFile not implemented without GdkImlib" );
375 wxPalette
*wxBitmap::GetPalette(void) const
377 if (!Ok()) return (wxPalette
*) NULL
;
378 return M_BMPDATA
->m_palette
;
381 GdkPixmap
*wxBitmap::GetPixmap(void) const
383 if (!Ok()) return (GdkPixmap
*) NULL
;
385 // if (!M_BMPDATA->m_image) RecreateImage();
387 return M_BMPDATA
->m_pixmap
;
390 GdkBitmap
*wxBitmap::GetBitmap(void) const
392 if (!Ok()) return (GdkBitmap
*) NULL
;
394 return M_BMPDATA
->m_bitmap
;
397 void wxBitmap::DestroyImage(void)
401 if (M_BMPDATA
->m_image
)
403 gdk_imlib_destroy_image( M_BMPDATA
->m_image
);
404 M_BMPDATA
->m_image
= (GdkImlibImage
*) NULL
;
408 void wxBitmap::RecreateImage(void)
416 wxCHECK_RET( M_BMPDATA
->m_pixmap
!= NULL
, "invalid bitmap" );
418 long size
= (long)(M_BMPDATA
->m_width
)*(long)(M_BMPDATA
->m_height
)*(long)3;
419 unsigned char *data
= new unsigned char[size
];
420 for (long i
= 0; i
< size
; i
++) data
[i
] = 100;
422 GdkImage
*image
= gdk_image_get( M_BMPDATA
->m_pixmap
, 0, 0, M_BMPDATA
->m_width
, M_BMPDATA
->m_height
);
425 for (int j
= 0; j
< M_BMPDATA
->m_height
; j
++)
427 for (int i
= 0; i
< M_BMPDATA
->m_width
; i
++)
430 xcol
.pixel
= gdk_image_get_pixel( image
, i
, j
);
431 Colormap cm
= ((GdkColormapPrivate
*)gdk_imlib_get_colormap())->xcolormap
;
432 XQueryColor( gdk_display
, cm
, &xcol
);
434 data
[pos
] = xcol
.red
;
435 data
[pos
+1] = xcol
.green
;
436 data
[pos
+2] = xcol
.blue
;
441 wxCHECK_RET( M_BMPDATA
->m_pixmap
!= NULL
, "invalid bitmap" );
443 M_BMPDATA
->m_image
= gdk_imlib_create_image_from_data(
444 data
, (unsigned char*)NULL
, M_BMPDATA
->m_width
, M_BMPDATA
->m_height
);
448 gdk_image_destroy( image
);
454 wxFAIL_MSG( "wxBitmap::RecreateImage not implemented without GdkImlib" );
459 void wxBitmap::Render(void)
465 if (!M_BMPDATA
->m_image
) RecreateImage();
467 if (M_BMPDATA
->m_pixmap
)
469 gdk_imlib_free_pixmap( M_BMPDATA
->m_pixmap
);
470 M_BMPDATA
->m_pixmap
= (GdkPixmap
*) NULL
;
472 if (M_BMPDATA
->m_mask
)
474 delete M_BMPDATA
->m_mask
;
475 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
478 gdk_imlib_render( M_BMPDATA
->m_image
, M_BMPDATA
->m_image
->rgb_width
, M_BMPDATA
->m_image
->rgb_height
);
479 M_BMPDATA
->m_width
= M_BMPDATA
->m_image
->rgb_width
;
480 M_BMPDATA
->m_height
= M_BMPDATA
->m_image
->rgb_height
;
481 M_BMPDATA
->m_pixmap
= gdk_imlib_move_image( M_BMPDATA
->m_image
);
483 wxCHECK_RET( M_BMPDATA
->m_pixmap
!= NULL
, "pixmap rendering failed" )
485 GdkBitmap
*mask
= gdk_imlib_move_mask( M_BMPDATA
->m_image
);
488 M_BMPDATA
->m_mask
= new wxMask();
489 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
494 wxFAIL_MSG( "wxBitmap::Render not implemented without GdkImlib" );