]>
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>
23 // in GTK+ 1.3 gdk_root_parent was renamed into gdk_parent_root
25 #define gdk_root_parent gdk_parent_root
27 // need to get the declaration of gdk_root_parent from private header
29 #endif // GTK+ 1.3/1.2
31 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
35 IMPLEMENT_DYNAMIC_CLASS(wxMask
,wxObject
)
39 m_bitmap
= (GdkBitmap
*) NULL
;
42 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
44 m_bitmap
= (GdkBitmap
*) NULL
;
45 Create( bitmap
, colour
);
48 wxMask::wxMask( const wxBitmap
& bitmap
, int paletteIndex
)
50 m_bitmap
= (GdkBitmap
*) NULL
;
51 Create( bitmap
, paletteIndex
);
54 wxMask::wxMask( const wxBitmap
& bitmap
)
56 m_bitmap
= (GdkBitmap
*) NULL
;
63 gdk_bitmap_unref( m_bitmap
);
66 bool wxMask::Create( const wxBitmap
& bitmap
,
67 const wxColour
& colour
)
71 gdk_bitmap_unref( m_bitmap
);
72 m_bitmap
= (GdkBitmap
*) NULL
;
75 wxImage
image( bitmap
);
76 if (!image
.Ok()) return FALSE
;
78 GdkWindow
*parent
= (GdkWindow
*) &gdk_root_parent
;
79 m_bitmap
= gdk_pixmap_new( parent
, image
.GetWidth(), image
.GetHeight(), 1 );
80 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
87 gdk_gc_set_foreground( gc
, &color
);
88 gdk_gc_set_fill( gc
, GDK_SOLID
);
89 gdk_draw_rectangle( m_bitmap
, gc
, TRUE
, 0, 0, image
.GetWidth(), image
.GetHeight() );
91 unsigned char *data
= image
.GetData();
94 unsigned char red
= colour
.Red();
95 unsigned char green
= colour
.Green();
96 unsigned char blue
= colour
.Blue();
98 GdkVisual
*visual
= gdk_visual_get_system();
99 int bpp
= visual
->depth
;
100 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800)) bpp
= 15;
105 green
= green
& 0xf8;
111 green
= green
& 0xf8;
118 gdk_gc_set_foreground( gc
, &color
);
120 for (int j
= 0; j
< image
.GetHeight(); j
++)
124 for (i
= 0; i
< image
.GetWidth(); i
++)
126 if ((data
[index
] == red
) &&
127 (data
[index
+1] == green
) &&
128 (data
[index
+2] == blue
))
137 gdk_draw_line( m_bitmap
, gc
, start_x
, j
, i
-1, j
);
144 gdk_draw_line( m_bitmap
, gc
, start_x
, j
, i
, j
);
152 bool wxMask::Create( const wxBitmap
& WXUNUSED(bitmap
),
153 int WXUNUSED(paletteIndex
) )
157 gdk_bitmap_unref( m_bitmap
);
158 m_bitmap
= (GdkBitmap
*) NULL
;
161 wxFAIL_MSG( wxT("not implemented") );
166 bool wxMask::Create( const wxBitmap
& bitmap
)
170 gdk_bitmap_unref( m_bitmap
);
171 m_bitmap
= (GdkBitmap
*) NULL
;
174 if (!bitmap
.Ok()) return FALSE
;
176 wxCHECK_MSG( bitmap
.GetBitmap(), FALSE
, wxT("Cannot create mask from colour bitmap") );
178 m_bitmap
= gdk_pixmap_new( (GdkWindow
*) &gdk_root_parent
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
180 if (!m_bitmap
) return FALSE
;
182 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
184 gdk_draw_bitmap( m_bitmap
, gc
, bitmap
.GetBitmap(), 0, 0, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight() );
191 GdkBitmap
*wxMask::GetBitmap() const
196 //-----------------------------------------------------------------------------
198 //-----------------------------------------------------------------------------
200 class wxBitmapRefData
: public wxObjectRefData
212 wxPalette
*m_palette
;
215 wxBitmapRefData::wxBitmapRefData()
217 m_pixmap
= (GdkPixmap
*) NULL
;
218 m_bitmap
= (GdkBitmap
*) NULL
;
219 m_mask
= (wxMask
*) NULL
;
223 m_palette
= (wxPalette
*) NULL
;
226 wxBitmapRefData::~wxBitmapRefData()
228 if (m_pixmap
) gdk_pixmap_unref( m_pixmap
);
229 if (m_bitmap
) gdk_bitmap_unref( m_bitmap
);
230 if (m_mask
) delete m_mask
;
231 if (m_palette
) delete m_palette
;
234 //-----------------------------------------------------------------------------
236 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
238 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
242 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
245 wxBitmap::wxBitmap( int width
, int height
, int depth
)
247 wxCHECK_RET( (width
> 0) && (height
> 0), wxT("invalid bitmap size") )
249 GdkWindow
*parent
= (GdkWindow
*) &gdk_root_parent
;
250 if (depth
== -1) depth
= gdk_window_get_visual( parent
)->depth
;
252 wxCHECK_RET( (depth
== gdk_window_get_visual( parent
)->depth
) ||
253 (depth
== 1), wxT("invalid bitmap depth") )
255 m_refData
= new wxBitmapRefData();
256 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
257 M_BMPDATA
->m_width
= width
;
258 M_BMPDATA
->m_height
= height
;
261 M_BMPDATA
->m_bitmap
= gdk_pixmap_new( parent
, width
, height
, 1 );
262 M_BMPDATA
->m_bpp
= 1;
266 M_BMPDATA
->m_pixmap
= gdk_pixmap_new( parent
, width
, height
, depth
);
267 M_BMPDATA
->m_bpp
= gdk_window_get_visual( parent
)->depth
;
270 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
273 bool wxBitmap::CreateFromXpm( const char **bits
)
275 wxCHECK_MSG( bits
!= NULL
, FALSE
, wxT("invalid bitmap data") )
277 m_refData
= new wxBitmapRefData();
279 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
280 GdkWindow
*parent
= (GdkWindow
*) &gdk_root_parent
;
282 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm_d( parent
, &mask
, NULL
, (gchar
**) bits
);
284 wxCHECK_MSG( M_BMPDATA
->m_pixmap
, FALSE
, wxT("couldn't create pixmap") );
288 M_BMPDATA
->m_mask
= new wxMask();
289 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
292 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
294 M_BMPDATA
->m_bpp
= gdk_window_get_visual( parent
)->depth
; // ?
295 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
300 wxBitmap::wxBitmap( const wxBitmap
& bmp
)
304 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
307 wxBitmap::wxBitmap( const wxString
&filename
, int type
)
309 LoadFile( filename
, type
);
311 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
314 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
))
316 m_refData
= new wxBitmapRefData();
318 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
319 M_BMPDATA
->m_bitmap
=
320 gdk_bitmap_create_from_data( (GdkWindow
*) &gdk_root_parent
, (gchar
*) bits
, width
, height
);
321 M_BMPDATA
->m_width
= width
;
322 M_BMPDATA
->m_height
= height
;
323 M_BMPDATA
->m_bpp
= 1;
325 wxCHECK_RET( M_BMPDATA
->m_bitmap
, wxT("couldn't create bitmap") );
327 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
330 wxBitmap::~wxBitmap()
332 if (wxTheBitmapList
) wxTheBitmapList
->DeleteObject(this);
335 wxBitmap
& wxBitmap::operator = ( const wxBitmap
& bmp
)
337 if (*this == bmp
) return (*this);
342 bool wxBitmap::operator == ( const wxBitmap
& bmp
)
344 return m_refData
== bmp
.m_refData
;
347 bool wxBitmap::operator != ( const wxBitmap
& bmp
)
349 return m_refData
!= bmp
.m_refData
;
352 bool wxBitmap::Ok() const
354 return (m_refData
!= NULL
);
357 int wxBitmap::GetHeight() const
359 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
361 return M_BMPDATA
->m_height
;
364 int wxBitmap::GetWidth() const
366 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
368 return M_BMPDATA
->m_width
;
371 int wxBitmap::GetDepth() const
373 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
375 return M_BMPDATA
->m_bpp
;
378 wxMask
*wxBitmap::GetMask() const
380 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
382 return M_BMPDATA
->m_mask
;
385 void wxBitmap::SetMask( wxMask
*mask
)
387 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
389 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
391 M_BMPDATA
->m_mask
= mask
;
394 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
397 (rect
.x
>= 0) && (rect
.y
>= 0) &&
398 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) && (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
399 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
401 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
402 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
406 GdkGC
*gc
= gdk_gc_new( ret
.GetPixmap() );
407 gdk_draw_pixmap( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
408 gdk_gc_destroy( gc
);
412 GdkGC
*gc
= gdk_gc_new( ret
.GetBitmap() );
413 gdk_draw_bitmap( ret
.GetBitmap(), gc
, GetBitmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
414 gdk_gc_destroy( gc
);
419 wxMask
*mask
= new wxMask
;
420 GdkWindow
*parent
= (GdkWindow
*) &gdk_root_parent
;
421 mask
->m_bitmap
= gdk_pixmap_new( parent
, rect
.width
, rect
.height
, 1 );
423 GdkGC
*gc
= gdk_gc_new( mask
->m_bitmap
);
424 gdk_draw_bitmap( mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, 0, 0, rect
.x
, rect
.y
, rect
.width
, rect
.height
);
425 gdk_gc_destroy( gc
);
433 bool wxBitmap::SaveFile( const wxString
&name
, int type
, wxPalette
*WXUNUSED(palette
) )
435 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid bitmap") );
437 // Try to save the bitmap via wxImage handlers:
439 wxImage
image( *this );
440 if (image
.Ok()) return image
.SaveFile( name
, type
);
446 bool wxBitmap::LoadFile( const wxString
&name
, int type
)
450 if (!wxFileExists(name
)) return FALSE
;
452 if (type
== wxBITMAP_TYPE_XPM
)
454 m_refData
= new wxBitmapRefData();
456 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
457 GdkWindow
*parent
= (GdkWindow
*) &gdk_root_parent
;
459 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm( parent
, &mask
, NULL
, name
.fn_str() );
463 M_BMPDATA
->m_mask
= new wxMask();
464 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
467 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
468 M_BMPDATA
->m_bpp
= gdk_window_get_visual( parent
)->depth
;
470 else // try if wxImage can load it
473 if (!image
.LoadFile( name
, type
)) return FALSE
;
474 if (image
.Ok()) *this = image
.ConvertToBitmap();
481 wxPalette
*wxBitmap::GetPalette() const
483 if (!Ok()) return (wxPalette
*) NULL
;
485 return M_BMPDATA
->m_palette
;
488 void wxBitmap::SetHeight( int height
)
490 if (!m_refData
) m_refData
= new wxBitmapRefData();
492 M_BMPDATA
->m_height
= height
;
495 void wxBitmap::SetWidth( int width
)
497 if (!m_refData
) m_refData
= new wxBitmapRefData();
499 M_BMPDATA
->m_width
= width
;
502 void wxBitmap::SetDepth( int depth
)
504 if (!m_refData
) m_refData
= new wxBitmapRefData();
506 M_BMPDATA
->m_bpp
= depth
;
509 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
511 if (!m_refData
) m_refData
= new wxBitmapRefData();
513 M_BMPDATA
->m_pixmap
= pixmap
;
516 GdkPixmap
*wxBitmap::GetPixmap() const
518 wxCHECK_MSG( Ok(), (GdkPixmap
*) NULL
, wxT("invalid bitmap") );
520 return M_BMPDATA
->m_pixmap
;
523 GdkBitmap
*wxBitmap::GetBitmap() const
525 wxCHECK_MSG( Ok(), (GdkBitmap
*) NULL
, wxT("invalid bitmap") );
527 return M_BMPDATA
->m_bitmap
;