]>
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"
18 #include "wx/dcmemory.h"
23 //-----------------------------------------------------------------------------
25 //-----------------------------------------------------------------------------
27 extern GtkWidget
*wxRootWindow
;
29 //-----------------------------------------------------------------------------
31 //-----------------------------------------------------------------------------
33 IMPLEMENT_DYNAMIC_CLASS(wxMask
,wxObject
)
37 m_bitmap
= (GdkBitmap
*) NULL
;
40 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
42 m_bitmap
= (GdkBitmap
*) NULL
;
43 Create( bitmap
, colour
);
46 wxMask::wxMask( const wxBitmap
& bitmap
, int paletteIndex
)
48 m_bitmap
= (GdkBitmap
*) NULL
;
49 Create( bitmap
, paletteIndex
);
52 wxMask::wxMask( const wxBitmap
& bitmap
)
54 m_bitmap
= (GdkBitmap
*) NULL
;
61 gdk_bitmap_unref( m_bitmap
);
64 bool wxMask::Create( const wxBitmap
& bitmap
,
65 const wxColour
& colour
)
69 gdk_bitmap_unref( m_bitmap
);
70 m_bitmap
= (GdkBitmap
*) NULL
;
73 wxImage
image( bitmap
);
74 if (!image
.Ok()) return FALSE
;
76 m_bitmap
= gdk_pixmap_new( wxRootWindow
->window
, image
.GetWidth(), image
.GetHeight(), 1 );
77 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
84 gdk_gc_set_foreground( gc
, &color
);
85 gdk_gc_set_fill( gc
, GDK_SOLID
);
86 gdk_draw_rectangle( m_bitmap
, gc
, TRUE
, 0, 0, image
.GetWidth(), image
.GetHeight() );
88 unsigned char *data
= image
.GetData();
91 unsigned char red
= colour
.Red();
92 unsigned char green
= colour
.Green();
93 unsigned char blue
= colour
.Blue();
95 GdkVisual
*visual
= gdk_window_get_visual( wxRootWindow
->window
);
98 int bpp
= visual
->depth
;
99 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800)) bpp
= 15;
104 green
= green
& 0xf8;
110 green
= green
& 0xf8;
117 gdk_gc_set_foreground( gc
, &color
);
119 for (int j
= 0; j
< image
.GetHeight(); j
++)
123 for (i
= 0; i
< image
.GetWidth(); i
++)
125 if ((data
[index
] == red
) &&
126 (data
[index
+1] == green
) &&
127 (data
[index
+2] == blue
))
136 gdk_draw_line( m_bitmap
, gc
, start_x
, j
, i
-1, j
);
143 gdk_draw_line( m_bitmap
, gc
, start_x
, j
, i
, j
);
151 bool wxMask::Create( const wxBitmap
& WXUNUSED(bitmap
),
152 int WXUNUSED(paletteIndex
) )
156 gdk_bitmap_unref( m_bitmap
);
157 m_bitmap
= (GdkBitmap
*) NULL
;
160 wxFAIL_MSG( wxT("not implemented") );
165 bool wxMask::Create( const wxBitmap
& bitmap
)
169 gdk_bitmap_unref( m_bitmap
);
170 m_bitmap
= (GdkBitmap
*) NULL
;
173 if (!bitmap
.Ok()) return FALSE
;
175 wxCHECK_MSG( bitmap
.GetBitmap(), FALSE
, wxT("Cannot create mask from colour bitmap") );
177 m_bitmap
= gdk_pixmap_new( wxRootWindow
->window
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
179 if (!m_bitmap
) return FALSE
;
181 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
183 gdk_draw_bitmap( m_bitmap
, gc
, bitmap
.GetBitmap(), 0, 0, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight() );
190 GdkBitmap
*wxMask::GetBitmap() const
195 //-----------------------------------------------------------------------------
197 //-----------------------------------------------------------------------------
199 class wxBitmapRefData
: public wxObjectRefData
211 wxPalette
*m_palette
;
214 wxBitmapRefData::wxBitmapRefData()
216 m_pixmap
= (GdkPixmap
*) NULL
;
217 m_bitmap
= (GdkBitmap
*) NULL
;
218 m_mask
= (wxMask
*) NULL
;
222 m_palette
= (wxPalette
*) NULL
;
225 wxBitmapRefData::~wxBitmapRefData()
227 if (m_pixmap
) gdk_pixmap_unref( m_pixmap
);
228 if (m_bitmap
) gdk_bitmap_unref( m_bitmap
);
229 if (m_mask
) delete m_mask
;
230 if (m_palette
) delete m_palette
;
233 //-----------------------------------------------------------------------------
235 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
237 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
241 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
244 wxBitmap::wxBitmap( int width
, int height
, int depth
)
246 Create( width
, height
, depth
);
248 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
251 bool wxBitmap::Create( int width
, int height
, int depth
)
255 wxCHECK_MSG( (width
> 0) && (height
> 0), FALSE
, wxT("invalid bitmap size") )
257 GdkVisual
*visual
= gdk_window_get_visual( wxRootWindow
->window
);
260 if (depth
== -1) depth
= visual
->depth
;
262 wxCHECK_MSG( (depth
== visual
->depth
) ||
263 (depth
== 1), FALSE
, wxT("invalid bitmap depth") )
265 m_refData
= new wxBitmapRefData();
266 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
267 M_BMPDATA
->m_width
= width
;
268 M_BMPDATA
->m_height
= height
;
271 M_BMPDATA
->m_bitmap
= gdk_pixmap_new( wxRootWindow
->window
, width
, height
, 1 );
272 M_BMPDATA
->m_bpp
= 1;
276 M_BMPDATA
->m_pixmap
= gdk_pixmap_new( wxRootWindow
->window
, width
, height
, depth
);
277 M_BMPDATA
->m_bpp
= visual
->depth
;
282 bool wxBitmap::CreateFromXpm( const char **bits
)
284 wxCHECK_MSG( bits
!= NULL
, FALSE
, wxT("invalid bitmap data") )
286 GdkVisual
*visual
= gdk_window_get_visual( wxRootWindow
->window
);
289 m_refData
= new wxBitmapRefData();
291 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
293 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm_d( wxRootWindow
->window
, &mask
, NULL
, (gchar
**) bits
);
295 wxCHECK_MSG( M_BMPDATA
->m_pixmap
, FALSE
, wxT("couldn't create pixmap") );
299 M_BMPDATA
->m_mask
= new wxMask();
300 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
303 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
305 M_BMPDATA
->m_bpp
= visual
->depth
; // ?
307 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
312 wxBitmap::wxBitmap( const wxBitmap
& bmp
)
316 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
319 wxBitmap::wxBitmap( const wxString
&filename
, int type
)
321 LoadFile( filename
, type
);
323 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
326 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
))
328 m_refData
= new wxBitmapRefData();
330 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
331 M_BMPDATA
->m_bitmap
=
332 gdk_bitmap_create_from_data( wxRootWindow
->window
, (gchar
*) bits
, width
, height
);
333 M_BMPDATA
->m_width
= width
;
334 M_BMPDATA
->m_height
= height
;
335 M_BMPDATA
->m_bpp
= 1;
337 wxCHECK_RET( M_BMPDATA
->m_bitmap
, wxT("couldn't create bitmap") );
339 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
342 wxBitmap::~wxBitmap()
344 if (wxTheBitmapList
) wxTheBitmapList
->DeleteObject(this);
347 wxBitmap
& wxBitmap::operator = ( const wxBitmap
& bmp
)
349 if (*this == bmp
) return (*this);
354 bool wxBitmap::operator == ( const wxBitmap
& bmp
)
356 return m_refData
== bmp
.m_refData
;
359 bool wxBitmap::operator != ( const wxBitmap
& bmp
)
361 return m_refData
!= bmp
.m_refData
;
364 bool wxBitmap::Ok() const
366 return (m_refData
!= NULL
);
369 int wxBitmap::GetHeight() const
371 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
373 return M_BMPDATA
->m_height
;
376 int wxBitmap::GetWidth() const
378 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
380 return M_BMPDATA
->m_width
;
383 int wxBitmap::GetDepth() const
385 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
387 return M_BMPDATA
->m_bpp
;
390 wxMask
*wxBitmap::GetMask() const
392 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
394 return M_BMPDATA
->m_mask
;
397 void wxBitmap::SetMask( wxMask
*mask
)
399 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
401 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
403 M_BMPDATA
->m_mask
= mask
;
406 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
409 (rect
.x
>= 0) && (rect
.y
>= 0) &&
410 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) && (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
411 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
413 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
414 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
418 GdkGC
*gc
= gdk_gc_new( ret
.GetPixmap() );
419 gdk_draw_pixmap( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
420 gdk_gc_destroy( gc
);
424 GdkGC
*gc
= gdk_gc_new( ret
.GetBitmap() );
425 gdk_draw_bitmap( ret
.GetBitmap(), gc
, GetBitmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
426 gdk_gc_destroy( gc
);
431 wxMask
*mask
= new wxMask
;
432 mask
->m_bitmap
= gdk_pixmap_new( wxRootWindow
->window
, rect
.width
, rect
.height
, 1 );
434 GdkGC
*gc
= gdk_gc_new( mask
->m_bitmap
);
435 gdk_draw_bitmap( mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, 0, 0, rect
.x
, rect
.y
, rect
.width
, rect
.height
);
436 gdk_gc_destroy( gc
);
444 bool wxBitmap::SaveFile( const wxString
&name
, int type
, wxPalette
*WXUNUSED(palette
) )
446 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid bitmap") );
448 // Try to save the bitmap via wxImage handlers:
450 wxImage
image( *this );
451 if (image
.Ok()) return image
.SaveFile( name
, type
);
457 bool wxBitmap::LoadFile( const wxString
&name
, int type
)
461 if (!wxFileExists(name
)) return FALSE
;
463 GdkVisual
*visual
= gdk_window_get_visual( wxRootWindow
->window
);
466 if (type
== wxBITMAP_TYPE_XPM
)
468 m_refData
= new wxBitmapRefData();
470 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
472 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm( wxRootWindow
->window
, &mask
, NULL
, name
.fn_str() );
476 M_BMPDATA
->m_mask
= new wxMask();
477 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
480 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
482 M_BMPDATA
->m_bpp
= visual
->depth
;
484 else // try if wxImage can load it
487 if (!image
.LoadFile( name
, type
)) return FALSE
;
488 if (image
.Ok()) *this = image
.ConvertToBitmap();
495 wxPalette
*wxBitmap::GetPalette() const
497 if (!Ok()) return (wxPalette
*) NULL
;
499 return M_BMPDATA
->m_palette
;
502 void wxBitmap::SetHeight( int height
)
504 if (!m_refData
) m_refData
= new wxBitmapRefData();
506 M_BMPDATA
->m_height
= height
;
509 void wxBitmap::SetWidth( int width
)
511 if (!m_refData
) m_refData
= new wxBitmapRefData();
513 M_BMPDATA
->m_width
= width
;
516 void wxBitmap::SetDepth( int depth
)
518 if (!m_refData
) m_refData
= new wxBitmapRefData();
520 M_BMPDATA
->m_bpp
= depth
;
523 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
525 if (!m_refData
) m_refData
= new wxBitmapRefData();
527 M_BMPDATA
->m_pixmap
= pixmap
;
530 void wxBitmap::SetBitmap( GdkPixmap
*bitmap
)
532 if (!m_refData
) m_refData
= new wxBitmapRefData();
534 M_BMPDATA
->m_bitmap
= bitmap
;
537 GdkPixmap
*wxBitmap::GetPixmap() const
539 wxCHECK_MSG( Ok(), (GdkPixmap
*) NULL
, wxT("invalid bitmap") );
541 return M_BMPDATA
->m_pixmap
;
544 GdkBitmap
*wxBitmap::GetBitmap() const
546 wxCHECK_MSG( Ok(), (GdkBitmap
*) NULL
, wxT("invalid bitmap") );
548 return M_BMPDATA
->m_bitmap
;