]>
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"
19 #ifdef wxUSE_GDK_IMLIB
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
) )
53 #ifdef wxUSE_GDK_IMLIB
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);
83 #ifdef wxUSE_GDK_IMLIB
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
;
98 #ifdef wxUSE_GDK_IMLIB
99 m_image
= (GdkImlibImage
*) NULL
;
103 wxBitmapRefData::~wxBitmapRefData(void)
105 #ifdef wxUSE_GDK_IMLIB
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 wxUSE_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 return (m_refData
!= NULL
);
234 int wxBitmap::GetHeight(void) const
238 wxFAIL_MSG( "invalid bitmap" );
242 return M_BMPDATA
->m_height
;
245 int wxBitmap::GetWidth(void) const
249 wxFAIL_MSG( "invalid bitmap" );
253 return M_BMPDATA
->m_width
;
256 int wxBitmap::GetDepth(void) const
260 wxFAIL_MSG( "invalid bitmap" );
264 return M_BMPDATA
->m_bpp
;
267 void wxBitmap::SetHeight( int height
)
271 wxFAIL_MSG( "wxBitmap::SetHeight not implemented" );
273 M_BMPDATA
->m_height
= height
;
276 void wxBitmap::SetWidth( int width
)
280 wxFAIL_MSG( "wxBitmap::SetWidth not implemented" );
282 M_BMPDATA
->m_width
= width
;
285 void wxBitmap::SetDepth( int depth
)
289 wxFAIL_MSG( "wxBitmap::SetDepth not implemented" );
291 M_BMPDATA
->m_bpp
= depth
;
294 wxMask
*wxBitmap::GetMask(void) const
298 wxFAIL_MSG( "invalid bitmap" );
299 return (wxMask
*) NULL
;
302 return M_BMPDATA
->m_mask
;
305 void wxBitmap::SetMask( wxMask
*mask
)
309 wxFAIL_MSG( "invalid bitmap" );
313 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
315 M_BMPDATA
->m_mask
= mask
;
318 void wxBitmap::Resize( int height
, int width
)
322 wxFAIL_MSG( "invalid bitmap" );
326 #ifdef wxUSE_GDK_IMLIB
328 if (M_BMPDATA
->m_bitmap
)
330 wxFAIL_MSG( "wxBitmap::Resize not supported for mono-bitmaps" );
334 if (!M_BMPDATA
->m_image
) RecreateImage();
336 if (M_BMPDATA
->m_pixmap
) gdk_imlib_free_pixmap( M_BMPDATA
->m_pixmap
);
337 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
339 GdkImlibImage
* image
= gdk_imlib_clone_scaled_image( M_BMPDATA
->m_image
, height
, width
);
340 gdk_imlib_destroy_image( M_BMPDATA
->m_image
);
341 M_BMPDATA
->m_image
= image
;
342 M_BMPDATA
->m_height
= height
;
343 M_BMPDATA
->m_width
= width
;
349 wxFAIL_MSG( "wxBitmap::Resize not implemented without GdkImlib" );
354 bool wxBitmap::SaveFile( const wxString
&name
, int WXUNUSED(type
),
355 wxPalette
*WXUNUSED(palette
) )
359 wxFAIL_MSG( "invalid bitmap" );
363 #ifdef wxUSE_GDK_IMLIB
365 if (M_BMPDATA
->m_bitmap
)
367 wxFAIL_MSG( "wxBitmap::SaveFile not supported for mono-bitmaps" );
371 if (!M_BMPDATA
->m_image
) RecreateImage();
373 return gdk_imlib_save_image( M_BMPDATA
->m_image
, WXSTRINGCAST name
, (GdkImlibSaveInfo
*) NULL
);
377 wxFAIL_MSG( "wxBitmap::SaveFile not implemented without GdkImlib" );
384 bool wxBitmap::LoadFile( const wxString
&name
, int WXUNUSED(type
) )
386 #ifdef wxUSE_GDK_IMLIB
389 m_refData
= new wxBitmapRefData();
391 M_BMPDATA
->m_image
= gdk_imlib_load_image( WXSTRINGCAST name
);
393 if (!M_BMPDATA
->m_image
)
401 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
402 M_BMPDATA
->m_bpp
= 24; // ?
408 wxFAIL_MSG( "wxBitmap::LoadFile not implemented without GdkImlib" );
415 wxPalette
*wxBitmap::GetPalette(void) const
417 if (!Ok()) return (wxPalette
*) NULL
;
418 return M_BMPDATA
->m_palette
;
421 GdkPixmap
*wxBitmap::GetPixmap(void) const
425 wxFAIL_MSG( "invalid bitmap" );
426 return (GdkPixmap
*) NULL
;
429 // if (!M_BMPDATA->m_image) RecreateImage();
431 return M_BMPDATA
->m_pixmap
;
434 GdkBitmap
*wxBitmap::GetBitmap(void) const
438 wxFAIL_MSG( "invalid bitmap" );
439 return (GdkBitmap
*) NULL
;
442 return M_BMPDATA
->m_bitmap
;
445 void wxBitmap::DestroyImage(void)
449 wxFAIL_MSG( "invalid bitmap" );
453 if (M_BMPDATA
->m_image
)
455 gdk_imlib_destroy_image( M_BMPDATA
->m_image
);
456 M_BMPDATA
->m_image
= (GdkImlibImage
*) NULL
;
460 void wxBitmap::RecreateImage(void)
464 wxFAIL_MSG( "invalid bitmap" );
468 #ifdef wxUSE_GDK_IMLIB
472 wxCHECK_RET( M_BMPDATA
->m_pixmap
!= NULL
, "invalid bitmap" );
474 long size
= (long)(M_BMPDATA
->m_width
)*(long)(M_BMPDATA
->m_height
)*(long)3;
475 unsigned char *data
= new unsigned char[size
];
476 for (long i
= 0; i
< size
; i
++) data
[i
] = 100;
478 GdkImage
*image
= gdk_image_get( M_BMPDATA
->m_pixmap
, 0, 0, M_BMPDATA
->m_width
, M_BMPDATA
->m_height
);
481 for (int j
= 0; j
< M_BMPDATA
->m_height
; j
++)
483 for (int i
= 0; i
< M_BMPDATA
->m_width
; i
++)
486 xcol
.pixel
= gdk_image_get_pixel( image
, i
, j
);
487 Colormap cm
= ((GdkColormapPrivate
*)gdk_imlib_get_colormap())->xcolormap
;
488 XQueryColor( gdk_display
, cm
, &xcol
);
490 data
[pos
] = xcol
.red
;
491 data
[pos
+1] = xcol
.green
;
492 data
[pos
+2] = xcol
.blue
;
497 wxCHECK_RET( M_BMPDATA
->m_pixmap
!= NULL
, "invalid bitmap" );
499 M_BMPDATA
->m_image
= gdk_imlib_create_image_from_data(
500 data
, (unsigned char*)NULL
, M_BMPDATA
->m_width
, M_BMPDATA
->m_height
);
504 gdk_image_destroy( image
);
510 wxFAIL_MSG( "wxBitmap::RecreateImage not implemented without GdkImlib" );
515 void wxBitmap::Render(void)
519 wxFAIL_MSG( "invalid bitmap" );
523 #ifdef wxUSE_GDK_IMLIB
525 if (!M_BMPDATA
->m_image
) RecreateImage();
527 if (M_BMPDATA
->m_pixmap
)
529 gdk_imlib_free_pixmap( M_BMPDATA
->m_pixmap
);
530 M_BMPDATA
->m_pixmap
= (GdkPixmap
*) NULL
;
532 if (M_BMPDATA
->m_mask
)
534 delete M_BMPDATA
->m_mask
;
535 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
538 gdk_imlib_render( M_BMPDATA
->m_image
, M_BMPDATA
->m_image
->rgb_width
, M_BMPDATA
->m_image
->rgb_height
);
539 M_BMPDATA
->m_width
= M_BMPDATA
->m_image
->rgb_width
;
540 M_BMPDATA
->m_height
= M_BMPDATA
->m_image
->rgb_height
;
541 M_BMPDATA
->m_pixmap
= gdk_imlib_move_image( M_BMPDATA
->m_image
);
543 wxCHECK_RET( M_BMPDATA
->m_pixmap
!= NULL
, "pixmap rendering failed" )
545 GdkBitmap
*mask
= gdk_imlib_move_mask( M_BMPDATA
->m_image
);
548 M_BMPDATA
->m_mask
= new wxMask();
549 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
554 wxFAIL_MSG( "wxBitmap::Render not implemented without GdkImlib" );