]>
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 //-----------------------------------------------------------------------------
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 wxCHECK_RET( (width
> 0) && (height
> 0), wxT("invalid bitmap size") )
248 GdkVisual
*visual
= gdk_window_get_visual( wxRootWindow
->window
);
251 if (depth
== -1) depth
= visual
->depth
;
253 wxCHECK_RET( (depth
== visual
->depth
) ||
254 (depth
== 1), wxT("invalid bitmap depth") )
256 m_refData
= new wxBitmapRefData();
257 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
258 M_BMPDATA
->m_width
= width
;
259 M_BMPDATA
->m_height
= height
;
262 M_BMPDATA
->m_bitmap
= gdk_pixmap_new( wxRootWindow
->window
, width
, height
, 1 );
263 M_BMPDATA
->m_bpp
= 1;
267 M_BMPDATA
->m_pixmap
= gdk_pixmap_new( wxRootWindow
->window
, width
, height
, depth
);
268 M_BMPDATA
->m_bpp
= visual
->depth
;
271 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
274 bool wxBitmap::CreateFromXpm( const char **bits
)
276 wxCHECK_MSG( bits
!= NULL
, FALSE
, wxT("invalid bitmap data") )
278 GdkVisual
*visual
= gdk_window_get_visual( wxRootWindow
->window
);
281 m_refData
= new wxBitmapRefData();
283 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
285 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm_d( wxRootWindow
->window
, &mask
, NULL
, (gchar
**) bits
);
287 wxCHECK_MSG( M_BMPDATA
->m_pixmap
, FALSE
, wxT("couldn't create pixmap") );
291 M_BMPDATA
->m_mask
= new wxMask();
292 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
295 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
297 M_BMPDATA
->m_bpp
= visual
->depth
; // ?
299 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
304 wxBitmap::wxBitmap( const wxBitmap
& bmp
)
308 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
311 wxBitmap::wxBitmap( const wxString
&filename
, int type
)
313 LoadFile( filename
, type
);
315 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
318 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
))
320 m_refData
= new wxBitmapRefData();
322 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
323 M_BMPDATA
->m_bitmap
=
324 gdk_bitmap_create_from_data( wxRootWindow
->window
, (gchar
*) bits
, width
, height
);
325 M_BMPDATA
->m_width
= width
;
326 M_BMPDATA
->m_height
= height
;
327 M_BMPDATA
->m_bpp
= 1;
329 wxCHECK_RET( M_BMPDATA
->m_bitmap
, wxT("couldn't create bitmap") );
331 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
334 wxBitmap::~wxBitmap()
336 if (wxTheBitmapList
) wxTheBitmapList
->DeleteObject(this);
339 wxBitmap
& wxBitmap::operator = ( const wxBitmap
& bmp
)
341 if (*this == bmp
) return (*this);
346 bool wxBitmap::operator == ( const wxBitmap
& bmp
)
348 return m_refData
== bmp
.m_refData
;
351 bool wxBitmap::operator != ( const wxBitmap
& bmp
)
353 return m_refData
!= bmp
.m_refData
;
356 bool wxBitmap::Ok() const
358 return (m_refData
!= NULL
);
361 int wxBitmap::GetHeight() const
363 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
365 return M_BMPDATA
->m_height
;
368 int wxBitmap::GetWidth() const
370 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
372 return M_BMPDATA
->m_width
;
375 int wxBitmap::GetDepth() const
377 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
379 return M_BMPDATA
->m_bpp
;
382 wxMask
*wxBitmap::GetMask() const
384 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
386 return M_BMPDATA
->m_mask
;
389 void wxBitmap::SetMask( wxMask
*mask
)
391 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
393 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
395 M_BMPDATA
->m_mask
= mask
;
398 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
401 (rect
.x
>= 0) && (rect
.y
>= 0) &&
402 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) && (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
403 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
405 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
406 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
410 GdkGC
*gc
= gdk_gc_new( ret
.GetPixmap() );
411 gdk_draw_pixmap( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
412 gdk_gc_destroy( gc
);
416 GdkGC
*gc
= gdk_gc_new( ret
.GetBitmap() );
417 gdk_draw_bitmap( ret
.GetBitmap(), gc
, GetBitmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
418 gdk_gc_destroy( gc
);
423 wxMask
*mask
= new wxMask
;
424 mask
->m_bitmap
= gdk_pixmap_new( wxRootWindow
->window
, rect
.width
, rect
.height
, 1 );
426 GdkGC
*gc
= gdk_gc_new( mask
->m_bitmap
);
427 gdk_draw_bitmap( mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, 0, 0, rect
.x
, rect
.y
, rect
.width
, rect
.height
);
428 gdk_gc_destroy( gc
);
436 bool wxBitmap::SaveFile( const wxString
&name
, int type
, wxPalette
*WXUNUSED(palette
) )
438 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid bitmap") );
440 // Try to save the bitmap via wxImage handlers:
442 wxImage
image( *this );
443 if (image
.Ok()) return image
.SaveFile( name
, type
);
449 bool wxBitmap::LoadFile( const wxString
&name
, int type
)
453 if (!wxFileExists(name
)) return FALSE
;
455 GdkVisual
*visual
= gdk_window_get_visual( wxRootWindow
->window
);
458 if (type
== wxBITMAP_TYPE_XPM
)
460 m_refData
= new wxBitmapRefData();
462 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
464 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm( wxRootWindow
->window
, &mask
, NULL
, name
.fn_str() );
468 M_BMPDATA
->m_mask
= new wxMask();
469 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
472 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
474 M_BMPDATA
->m_bpp
= visual
->depth
;
476 else // try if wxImage can load it
479 if (!image
.LoadFile( name
, type
)) return FALSE
;
480 if (image
.Ok()) *this = image
.ConvertToBitmap();
487 wxPalette
*wxBitmap::GetPalette() const
489 if (!Ok()) return (wxPalette
*) NULL
;
491 return M_BMPDATA
->m_palette
;
494 void wxBitmap::SetHeight( int height
)
496 if (!m_refData
) m_refData
= new wxBitmapRefData();
498 M_BMPDATA
->m_height
= height
;
501 void wxBitmap::SetWidth( int width
)
503 if (!m_refData
) m_refData
= new wxBitmapRefData();
505 M_BMPDATA
->m_width
= width
;
508 void wxBitmap::SetDepth( int depth
)
510 if (!m_refData
) m_refData
= new wxBitmapRefData();
512 M_BMPDATA
->m_bpp
= depth
;
515 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
517 if (!m_refData
) m_refData
= new wxBitmapRefData();
519 M_BMPDATA
->m_pixmap
= pixmap
;
522 void wxBitmap::SetBitmap( GdkPixmap
*bitmap
)
524 if (!m_refData
) m_refData
= new wxBitmapRefData();
526 M_BMPDATA
->m_bitmap
= bitmap
;
529 GdkPixmap
*wxBitmap::GetPixmap() const
531 wxCHECK_MSG( Ok(), (GdkPixmap
*) NULL
, wxT("invalid bitmap") );
533 return M_BMPDATA
->m_pixmap
;
536 GdkBitmap
*wxBitmap::GetBitmap() const
538 wxCHECK_MSG( Ok(), (GdkBitmap
*) NULL
, wxT("invalid bitmap") );
540 return M_BMPDATA
->m_bitmap
;