]>
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"
21 #include <gdk/gdkprivate.h>
24 //-----------------------------------------------------------------------------
26 //-----------------------------------------------------------------------------
28 IMPLEMENT_DYNAMIC_CLASS(wxMask
,wxObject
)
32 m_bitmap
= (GdkBitmap
*) NULL
;
35 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
37 m_bitmap
= (GdkBitmap
*) NULL
;
38 Create( bitmap
, colour
);
41 wxMask::wxMask( const wxBitmap
& bitmap
, int paletteIndex
)
43 m_bitmap
= (GdkBitmap
*) NULL
;
44 Create( bitmap
, paletteIndex
);
47 wxMask::wxMask( const wxBitmap
& bitmap
)
49 m_bitmap
= (GdkBitmap
*) NULL
;
56 gdk_bitmap_unref( m_bitmap
);
59 bool wxMask::Create( const wxBitmap
& bitmap
,
60 const wxColour
& colour
)
64 gdk_bitmap_unref( m_bitmap
);
65 m_bitmap
= (GdkBitmap
*) NULL
;
68 wxImage
image( bitmap
);
69 if (!image
.Ok()) return FALSE
;
71 GdkWindow
*parent
= (GdkWindow
*) &gdk_root_parent
;
72 m_bitmap
= gdk_pixmap_new( parent
, image
.GetWidth(), image
.GetHeight(), 1 );
73 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
80 gdk_gc_set_foreground( gc
, &color
);
81 gdk_gc_set_fill( gc
, GDK_SOLID
);
82 gdk_draw_rectangle( m_bitmap
, gc
, TRUE
, 0, 0, image
.GetWidth(), image
.GetHeight() );
84 unsigned char *data
= image
.GetData();
87 unsigned char red
= colour
.Red();
88 unsigned char green
= colour
.Green();
89 unsigned char blue
= colour
.Blue();
91 GdkVisual
*visual
= gdk_visual_get_system();
92 int bpp
= visual
->depth
;
93 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800)) bpp
= 15;
104 green
= green
& 0xf8;
111 gdk_gc_set_foreground( gc
, &color
);
113 for (int j
= 0; j
< image
.GetHeight(); j
++)
117 for (i
= 0; i
< image
.GetWidth(); i
++)
119 if ((data
[index
] == red
) &&
120 (data
[index
+1] == green
) &&
121 (data
[index
+2] == blue
))
130 gdk_draw_line( m_bitmap
, gc
, start_x
, j
, i
-1, j
);
137 gdk_draw_line( m_bitmap
, gc
, start_x
, j
, i
, j
);
145 bool wxMask::Create( const wxBitmap
& WXUNUSED(bitmap
),
146 int WXUNUSED(paletteIndex
) )
150 gdk_bitmap_unref( m_bitmap
);
151 m_bitmap
= (GdkBitmap
*) NULL
;
154 wxFAIL_MSG( wxT("not implemented") );
159 bool wxMask::Create( const wxBitmap
& bitmap
)
163 gdk_bitmap_unref( m_bitmap
);
164 m_bitmap
= (GdkBitmap
*) NULL
;
167 if (!bitmap
.Ok()) return FALSE
;
169 wxCHECK_MSG( bitmap
.GetBitmap(), FALSE
, wxT("Cannot create mask from colour bitmap") );
171 m_bitmap
= gdk_pixmap_new( (GdkWindow
*) &gdk_root_parent
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
173 if (!m_bitmap
) return FALSE
;
175 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
177 gdk_draw_bitmap( m_bitmap
, gc
, bitmap
.GetBitmap(), 0, 0, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight() );
184 GdkBitmap
*wxMask::GetBitmap() const
189 //-----------------------------------------------------------------------------
191 //-----------------------------------------------------------------------------
193 class wxBitmapRefData
: public wxObjectRefData
205 wxPalette
*m_palette
;
208 wxBitmapRefData::wxBitmapRefData()
210 m_pixmap
= (GdkPixmap
*) NULL
;
211 m_bitmap
= (GdkBitmap
*) NULL
;
212 m_mask
= (wxMask
*) NULL
;
216 m_palette
= (wxPalette
*) NULL
;
219 wxBitmapRefData::~wxBitmapRefData()
221 if (m_pixmap
) gdk_pixmap_unref( m_pixmap
);
222 if (m_bitmap
) gdk_bitmap_unref( m_bitmap
);
223 if (m_mask
) delete m_mask
;
224 if (m_palette
) delete m_palette
;
227 //-----------------------------------------------------------------------------
229 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
231 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
235 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
238 wxBitmap::wxBitmap( int width
, int height
, int depth
)
240 wxCHECK_RET( (width
> 0) && (height
> 0), wxT("invalid bitmap size") )
242 GdkWindow
*parent
= (GdkWindow
*) &gdk_root_parent
;
243 if (depth
== -1) depth
= gdk_window_get_visual( parent
)->depth
;
245 wxCHECK_RET( (depth
== gdk_window_get_visual( parent
)->depth
) ||
246 (depth
== 1), wxT("invalid bitmap depth") )
248 m_refData
= new wxBitmapRefData();
249 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
250 M_BMPDATA
->m_width
= width
;
251 M_BMPDATA
->m_height
= height
;
254 M_BMPDATA
->m_bitmap
= gdk_pixmap_new( parent
, width
, height
, 1 );
255 M_BMPDATA
->m_bpp
= 1;
259 M_BMPDATA
->m_pixmap
= gdk_pixmap_new( parent
, width
, height
, depth
);
260 M_BMPDATA
->m_bpp
= gdk_window_get_visual( parent
)->depth
;
263 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
266 bool wxBitmap::CreateFromXpm( const char **bits
)
268 wxCHECK_MSG( bits
!= NULL
, FALSE
, wxT("invalid bitmap data") )
270 m_refData
= new wxBitmapRefData();
272 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
273 GdkWindow
*parent
= (GdkWindow
*) &gdk_root_parent
;
275 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm_d( parent
, &mask
, NULL
, (gchar
**) bits
);
277 wxCHECK_MSG( M_BMPDATA
->m_pixmap
, FALSE
, wxT("couldn't create pixmap") );
281 M_BMPDATA
->m_mask
= new wxMask();
282 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
285 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
287 M_BMPDATA
->m_bpp
= gdk_window_get_visual( parent
)->depth
; // ?
288 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
293 wxBitmap::wxBitmap( const wxBitmap
& bmp
)
297 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
300 wxBitmap::wxBitmap( const wxString
&filename
, int type
)
302 LoadFile( filename
, type
);
304 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
307 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
))
309 m_refData
= new wxBitmapRefData();
311 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
312 M_BMPDATA
->m_bitmap
=
313 gdk_bitmap_create_from_data( (GdkWindow
*) &gdk_root_parent
, (gchar
*) bits
, width
, height
);
314 M_BMPDATA
->m_width
= width
;
315 M_BMPDATA
->m_height
= height
;
316 M_BMPDATA
->m_bpp
= 1;
318 wxCHECK_RET( M_BMPDATA
->m_bitmap
, wxT("couldn't create bitmap") );
320 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
323 wxBitmap::~wxBitmap()
325 if (wxTheBitmapList
) wxTheBitmapList
->DeleteObject(this);
328 wxBitmap
& wxBitmap::operator = ( const wxBitmap
& bmp
)
330 if (*this == bmp
) return (*this);
335 bool wxBitmap::operator == ( const wxBitmap
& bmp
)
337 return m_refData
== bmp
.m_refData
;
340 bool wxBitmap::operator != ( const wxBitmap
& bmp
)
342 return m_refData
!= bmp
.m_refData
;
345 bool wxBitmap::Ok() const
347 return (m_refData
!= NULL
);
350 int wxBitmap::GetHeight() const
352 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
354 return M_BMPDATA
->m_height
;
357 int wxBitmap::GetWidth() const
359 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
361 return M_BMPDATA
->m_width
;
364 int wxBitmap::GetDepth() const
366 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
368 return M_BMPDATA
->m_bpp
;
371 wxMask
*wxBitmap::GetMask() const
373 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
375 return M_BMPDATA
->m_mask
;
378 void wxBitmap::SetMask( wxMask
*mask
)
380 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
382 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
384 M_BMPDATA
->m_mask
= mask
;
387 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
390 (rect
.x
>= 0) && (rect
.y
>= 0) &&
391 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) && (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
392 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
394 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
395 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
399 GdkGC
*gc
= gdk_gc_new( ret
.GetPixmap() );
400 gdk_draw_pixmap( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
401 gdk_gc_destroy( gc
);
405 GdkGC
*gc
= gdk_gc_new( ret
.GetBitmap() );
406 gdk_draw_bitmap( ret
.GetBitmap(), gc
, GetBitmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
407 gdk_gc_destroy( gc
);
412 wxMask
*mask
= new wxMask
;
413 GdkWindow
*parent
= (GdkWindow
*) &gdk_root_parent
;
414 mask
->m_bitmap
= gdk_pixmap_new( parent
, rect
.width
, rect
.height
, 1 );
416 GdkGC
*gc
= gdk_gc_new( mask
->m_bitmap
);
417 gdk_draw_bitmap( mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, 0, 0, rect
.x
, rect
.y
, rect
.width
, rect
.height
);
418 gdk_gc_destroy( gc
);
426 bool wxBitmap::SaveFile( const wxString
&name
, int type
, wxPalette
*WXUNUSED(palette
) )
428 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid bitmap") );
430 // Try to save the bitmap via wxImage handlers:
432 wxImage
image( *this );
433 if (image
.Ok()) return image
.SaveFile( name
, type
);
439 bool wxBitmap::LoadFile( const wxString
&name
, int type
)
443 if (!wxFileExists(name
)) return FALSE
;
445 if (type
== wxBITMAP_TYPE_XPM
)
447 m_refData
= new wxBitmapRefData();
449 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
450 GdkWindow
*parent
= (GdkWindow
*) &gdk_root_parent
;
452 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm( parent
, &mask
, NULL
, name
.fn_str() );
456 M_BMPDATA
->m_mask
= new wxMask();
457 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
460 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
461 M_BMPDATA
->m_bpp
= gdk_window_get_visual( parent
)->depth
;
463 else // try if wxImage can load it
466 if (!image
.LoadFile( name
, type
)) return FALSE
;
467 if (image
.Ok()) *this = image
.ConvertToBitmap();
474 wxPalette
*wxBitmap::GetPalette() const
476 if (!Ok()) return (wxPalette
*) NULL
;
478 return M_BMPDATA
->m_palette
;
481 void wxBitmap::SetHeight( int height
)
483 if (!m_refData
) m_refData
= new wxBitmapRefData();
485 M_BMPDATA
->m_height
= height
;
488 void wxBitmap::SetWidth( int width
)
490 if (!m_refData
) m_refData
= new wxBitmapRefData();
492 M_BMPDATA
->m_width
= width
;
495 void wxBitmap::SetDepth( int depth
)
497 if (!m_refData
) m_refData
= new wxBitmapRefData();
499 M_BMPDATA
->m_bpp
= depth
;
502 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
504 if (!m_refData
) m_refData
= new wxBitmapRefData();
506 M_BMPDATA
->m_pixmap
= pixmap
;
509 GdkPixmap
*wxBitmap::GetPixmap() const
511 wxCHECK_MSG( Ok(), (GdkPixmap
*) NULL
, wxT("invalid bitmap") );
513 return M_BMPDATA
->m_pixmap
;
516 GdkBitmap
*wxBitmap::GetBitmap() const
518 wxCHECK_MSG( Ok(), (GdkBitmap
*) NULL
, wxT("invalid bitmap") );
520 return M_BMPDATA
->m_bitmap
;