]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk1/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 extern void gdk_wx_draw_bitmap (GdkDrawable
*drawable
,
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
37 extern GtkWidget
*wxRootWindow
;
39 //-----------------------------------------------------------------------------
41 //-----------------------------------------------------------------------------
43 IMPLEMENT_DYNAMIC_CLASS(wxMask
,wxObject
)
47 m_bitmap
= (GdkBitmap
*) NULL
;
50 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
52 m_bitmap
= (GdkBitmap
*) NULL
;
53 Create( bitmap
, colour
);
56 wxMask::wxMask( const wxBitmap
& bitmap
, int paletteIndex
)
58 m_bitmap
= (GdkBitmap
*) NULL
;
59 Create( bitmap
, paletteIndex
);
62 wxMask::wxMask( const wxBitmap
& bitmap
)
64 m_bitmap
= (GdkBitmap
*) NULL
;
71 gdk_bitmap_unref( m_bitmap
);
74 bool wxMask::Create( const wxBitmap
& bitmap
,
75 const wxColour
& colour
)
79 gdk_bitmap_unref( m_bitmap
);
80 m_bitmap
= (GdkBitmap
*) NULL
;
83 wxImage
image( bitmap
);
84 if (!image
.Ok()) return FALSE
;
86 m_bitmap
= gdk_pixmap_new( wxRootWindow
->window
, image
.GetWidth(), image
.GetHeight(), 1 );
87 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
94 gdk_gc_set_foreground( gc
, &color
);
95 gdk_gc_set_fill( gc
, GDK_SOLID
);
96 gdk_draw_rectangle( m_bitmap
, gc
, TRUE
, 0, 0, image
.GetWidth(), image
.GetHeight() );
98 unsigned char *data
= image
.GetData();
101 unsigned char red
= colour
.Red();
102 unsigned char green
= colour
.Green();
103 unsigned char blue
= colour
.Blue();
105 GdkVisual
*visual
= gdk_window_get_visual( wxRootWindow
->window
);
108 int bpp
= visual
->depth
;
109 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800)) bpp
= 15;
113 green
= green
& 0xf8;
119 green
= green
& 0xfc;
127 gdk_gc_set_foreground( gc
, &color
);
129 for (int j
= 0; j
< image
.GetHeight(); j
++)
133 for (i
= 0; i
< image
.GetWidth(); i
++)
135 if ((data
[index
] == red
) &&
136 (data
[index
+1] == green
) &&
137 (data
[index
+2] == blue
))
146 gdk_draw_line( m_bitmap
, gc
, start_x
, j
, i
-1, j
);
153 gdk_draw_line( m_bitmap
, gc
, start_x
, j
, i
, j
);
161 bool wxMask::Create( const wxBitmap
& WXUNUSED(bitmap
),
162 int WXUNUSED(paletteIndex
) )
166 gdk_bitmap_unref( m_bitmap
);
167 m_bitmap
= (GdkBitmap
*) NULL
;
170 wxFAIL_MSG( wxT("not implemented") );
175 bool wxMask::Create( const wxBitmap
& bitmap
)
179 gdk_bitmap_unref( m_bitmap
);
180 m_bitmap
= (GdkBitmap
*) NULL
;
183 if (!bitmap
.Ok()) return FALSE
;
185 wxCHECK_MSG( bitmap
.GetBitmap(), FALSE
, wxT("Cannot create mask from colour bitmap") );
187 m_bitmap
= gdk_pixmap_new( wxRootWindow
->window
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
189 if (!m_bitmap
) return FALSE
;
191 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
193 gdk_wx_draw_bitmap( m_bitmap
, gc
, bitmap
.GetBitmap(), 0, 0, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight() );
200 GdkBitmap
*wxMask::GetBitmap() const
205 //-----------------------------------------------------------------------------
207 //-----------------------------------------------------------------------------
209 class wxBitmapRefData
: public wxObjectRefData
221 wxPalette
*m_palette
;
224 wxBitmapRefData::wxBitmapRefData()
226 m_pixmap
= (GdkPixmap
*) NULL
;
227 m_bitmap
= (GdkBitmap
*) NULL
;
228 m_mask
= (wxMask
*) NULL
;
232 m_palette
= (wxPalette
*) NULL
;
235 wxBitmapRefData::~wxBitmapRefData()
237 if (m_pixmap
) gdk_pixmap_unref( m_pixmap
);
238 if (m_bitmap
) gdk_bitmap_unref( m_bitmap
);
239 if (m_mask
) delete m_mask
;
240 if (m_palette
) delete m_palette
;
243 //-----------------------------------------------------------------------------
245 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
247 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
251 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
254 wxBitmap::wxBitmap( int width
, int height
, int depth
)
256 Create( width
, height
, depth
);
258 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
261 bool wxBitmap::Create( int width
, int height
, int depth
)
265 wxCHECK_MSG( (width
> 0) && (height
> 0), FALSE
, wxT("invalid bitmap size") )
267 GdkVisual
*visual
= gdk_window_get_visual( wxRootWindow
->window
);
270 if (depth
== -1) depth
= visual
->depth
;
272 wxCHECK_MSG( (depth
== visual
->depth
) ||
273 (depth
== 1), FALSE
, wxT("invalid bitmap depth") )
275 m_refData
= new wxBitmapRefData();
276 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
277 M_BMPDATA
->m_width
= width
;
278 M_BMPDATA
->m_height
= height
;
281 M_BMPDATA
->m_bitmap
= gdk_pixmap_new( wxRootWindow
->window
, width
, height
, 1 );
282 M_BMPDATA
->m_bpp
= 1;
286 M_BMPDATA
->m_pixmap
= gdk_pixmap_new( wxRootWindow
->window
, width
, height
, depth
);
287 M_BMPDATA
->m_bpp
= visual
->depth
;
292 bool wxBitmap::CreateFromXpm( const char **bits
)
294 wxCHECK_MSG( bits
!= NULL
, FALSE
, wxT("invalid bitmap data") )
296 GdkVisual
*visual
= gdk_window_get_visual( wxRootWindow
->window
);
299 m_refData
= new wxBitmapRefData();
301 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
303 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm_d( wxRootWindow
->window
, &mask
, NULL
, (gchar
**) bits
);
305 wxCHECK_MSG( M_BMPDATA
->m_pixmap
, FALSE
, wxT("couldn't create pixmap") );
309 M_BMPDATA
->m_mask
= new wxMask();
310 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
313 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
315 M_BMPDATA
->m_bpp
= visual
->depth
; // ?
317 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
322 wxBitmap::wxBitmap( const wxBitmap
& bmp
)
326 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
329 wxBitmap::wxBitmap( const wxString
&filename
, int type
)
331 LoadFile( filename
, type
);
333 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
336 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
))
338 m_refData
= new wxBitmapRefData();
340 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
341 M_BMPDATA
->m_bitmap
=
342 gdk_bitmap_create_from_data( wxRootWindow
->window
, (gchar
*) bits
, width
, height
);
343 M_BMPDATA
->m_width
= width
;
344 M_BMPDATA
->m_height
= height
;
345 M_BMPDATA
->m_bpp
= 1;
347 wxCHECK_RET( M_BMPDATA
->m_bitmap
, wxT("couldn't create bitmap") );
349 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
352 wxBitmap::~wxBitmap()
354 if (wxTheBitmapList
) wxTheBitmapList
->DeleteObject(this);
357 wxBitmap
& wxBitmap::operator = ( const wxBitmap
& bmp
)
359 if (*this == bmp
) return (*this);
364 bool wxBitmap::operator == ( const wxBitmap
& bmp
) const
366 return m_refData
== bmp
.m_refData
;
369 bool wxBitmap::operator != ( const wxBitmap
& bmp
) const
371 return m_refData
!= bmp
.m_refData
;
374 bool wxBitmap::Ok() const
376 return (m_refData
!= NULL
);
379 int wxBitmap::GetHeight() const
381 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
383 return M_BMPDATA
->m_height
;
386 int wxBitmap::GetWidth() const
388 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
390 return M_BMPDATA
->m_width
;
393 int wxBitmap::GetDepth() const
395 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
397 return M_BMPDATA
->m_bpp
;
400 wxMask
*wxBitmap::GetMask() const
402 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
404 return M_BMPDATA
->m_mask
;
407 void wxBitmap::SetMask( wxMask
*mask
)
409 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
411 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
413 M_BMPDATA
->m_mask
= mask
;
416 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
419 (rect
.x
>= 0) && (rect
.y
>= 0) &&
420 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) && (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
421 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
423 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
424 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
428 GdkGC
*gc
= gdk_gc_new( ret
.GetPixmap() );
429 gdk_draw_pixmap( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
430 gdk_gc_destroy( gc
);
434 GdkGC
*gc
= gdk_gc_new( ret
.GetBitmap() );
435 gdk_wx_draw_bitmap( ret
.GetBitmap(), gc
, GetBitmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
436 gdk_gc_destroy( gc
);
441 wxMask
*mask
= new wxMask
;
442 mask
->m_bitmap
= gdk_pixmap_new( wxRootWindow
->window
, rect
.width
, rect
.height
, 1 );
444 GdkGC
*gc
= gdk_gc_new( mask
->m_bitmap
);
445 gdk_wx_draw_bitmap( mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, 0, 0, rect
.x
, rect
.y
, rect
.width
, rect
.height
);
446 gdk_gc_destroy( gc
);
454 bool wxBitmap::SaveFile( const wxString
&name
, int type
, wxPalette
*WXUNUSED(palette
) )
456 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid bitmap") );
458 // Try to save the bitmap via wxImage handlers:
460 wxImage
image( *this );
461 if (image
.Ok()) return image
.SaveFile( name
, type
);
467 bool wxBitmap::LoadFile( const wxString
&name
, int type
)
471 if (!wxFileExists(name
)) return FALSE
;
473 GdkVisual
*visual
= gdk_window_get_visual( wxRootWindow
->window
);
476 if (type
== wxBITMAP_TYPE_XPM
)
478 m_refData
= new wxBitmapRefData();
480 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
482 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm( wxRootWindow
->window
, &mask
, NULL
, name
.fn_str() );
486 M_BMPDATA
->m_mask
= new wxMask();
487 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
490 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
492 M_BMPDATA
->m_bpp
= visual
->depth
;
494 else // try if wxImage can load it
497 if (!image
.LoadFile( name
, type
)) return FALSE
;
498 if (image
.Ok()) *this = image
.ConvertToBitmap();
505 wxPalette
*wxBitmap::GetPalette() const
507 if (!Ok()) return (wxPalette
*) NULL
;
509 return M_BMPDATA
->m_palette
;
512 void wxBitmap::SetHeight( int height
)
514 if (!m_refData
) m_refData
= new wxBitmapRefData();
516 M_BMPDATA
->m_height
= height
;
519 void wxBitmap::SetWidth( int width
)
521 if (!m_refData
) m_refData
= new wxBitmapRefData();
523 M_BMPDATA
->m_width
= width
;
526 void wxBitmap::SetDepth( int depth
)
528 if (!m_refData
) m_refData
= new wxBitmapRefData();
530 M_BMPDATA
->m_bpp
= depth
;
533 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
535 if (!m_refData
) m_refData
= new wxBitmapRefData();
537 M_BMPDATA
->m_pixmap
= pixmap
;
540 void wxBitmap::SetBitmap( GdkPixmap
*bitmap
)
542 if (!m_refData
) m_refData
= new wxBitmapRefData();
544 M_BMPDATA
->m_bitmap
= bitmap
;
547 GdkPixmap
*wxBitmap::GetPixmap() const
549 wxCHECK_MSG( Ok(), (GdkPixmap
*) NULL
, wxT("invalid bitmap") );
551 return M_BMPDATA
->m_pixmap
;
554 GdkBitmap
*wxBitmap::GetBitmap() const
556 wxCHECK_MSG( Ok(), (GdkBitmap
*) NULL
, wxT("invalid bitmap") );
558 return M_BMPDATA
->m_bitmap
;