]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk1/bitmap.cpp
bbde88901498891389b2f360631430c60bf34420
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_visual_get_system();
96 int bpp
= visual
->depth
;
97 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800)) bpp
= 15;
102 green
= green
& 0xf8;
108 green
= green
& 0xf8;
115 gdk_gc_set_foreground( gc
, &color
);
117 for (int j
= 0; j
< image
.GetHeight(); j
++)
121 for (i
= 0; i
< image
.GetWidth(); i
++)
123 if ((data
[index
] == red
) &&
124 (data
[index
+1] == green
) &&
125 (data
[index
+2] == blue
))
134 gdk_draw_line( m_bitmap
, gc
, start_x
, j
, i
-1, j
);
141 gdk_draw_line( m_bitmap
, gc
, start_x
, j
, i
, j
);
149 bool wxMask::Create( const wxBitmap
& WXUNUSED(bitmap
),
150 int WXUNUSED(paletteIndex
) )
154 gdk_bitmap_unref( m_bitmap
);
155 m_bitmap
= (GdkBitmap
*) NULL
;
158 wxFAIL_MSG( wxT("not implemented") );
163 bool wxMask::Create( const wxBitmap
& bitmap
)
167 gdk_bitmap_unref( m_bitmap
);
168 m_bitmap
= (GdkBitmap
*) NULL
;
171 if (!bitmap
.Ok()) return FALSE
;
173 wxCHECK_MSG( bitmap
.GetBitmap(), FALSE
, wxT("Cannot create mask from colour bitmap") );
175 m_bitmap
= gdk_pixmap_new( wxRootWindow
->window
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
177 if (!m_bitmap
) return FALSE
;
179 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
181 gdk_draw_bitmap( m_bitmap
, gc
, bitmap
.GetBitmap(), 0, 0, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight() );
188 GdkBitmap
*wxMask::GetBitmap() const
193 //-----------------------------------------------------------------------------
195 //-----------------------------------------------------------------------------
197 class wxBitmapRefData
: public wxObjectRefData
209 wxPalette
*m_palette
;
212 wxBitmapRefData::wxBitmapRefData()
214 m_pixmap
= (GdkPixmap
*) NULL
;
215 m_bitmap
= (GdkBitmap
*) NULL
;
216 m_mask
= (wxMask
*) NULL
;
220 m_palette
= (wxPalette
*) NULL
;
223 wxBitmapRefData::~wxBitmapRefData()
225 if (m_pixmap
) gdk_pixmap_unref( m_pixmap
);
226 if (m_bitmap
) gdk_bitmap_unref( m_bitmap
);
227 if (m_mask
) delete m_mask
;
228 if (m_palette
) delete m_palette
;
231 //-----------------------------------------------------------------------------
233 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
235 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
239 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
242 wxBitmap::wxBitmap( int width
, int height
, int depth
)
244 wxCHECK_RET( (width
> 0) && (height
> 0), wxT("invalid bitmap size") )
246 if (depth
== -1) depth
= gdk_window_get_visual( wxRootWindow
->window
)->depth
;
248 wxCHECK_RET( (depth
== gdk_window_get_visual( wxRootWindow
->window
)->depth
) ||
249 (depth
== 1), wxT("invalid bitmap depth") )
251 m_refData
= new wxBitmapRefData();
252 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
253 M_BMPDATA
->m_width
= width
;
254 M_BMPDATA
->m_height
= height
;
257 M_BMPDATA
->m_bitmap
= gdk_pixmap_new( wxRootWindow
->window
, width
, height
, 1 );
258 M_BMPDATA
->m_bpp
= 1;
262 M_BMPDATA
->m_pixmap
= gdk_pixmap_new( wxRootWindow
->window
, width
, height
, depth
);
263 M_BMPDATA
->m_bpp
= gdk_window_get_visual( wxRootWindow
->window
)->depth
;
266 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
269 bool wxBitmap::CreateFromXpm( const char **bits
)
271 wxCHECK_MSG( bits
!= NULL
, FALSE
, wxT("invalid bitmap data") )
273 m_refData
= new wxBitmapRefData();
275 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
277 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm_d( wxRootWindow
->window
, &mask
, NULL
, (gchar
**) bits
);
279 wxCHECK_MSG( M_BMPDATA
->m_pixmap
, FALSE
, wxT("couldn't create pixmap") );
283 M_BMPDATA
->m_mask
= new wxMask();
284 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
287 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
289 M_BMPDATA
->m_bpp
= gdk_window_get_visual( wxRootWindow
->window
)->depth
; // ?
290 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
295 wxBitmap::wxBitmap( const wxBitmap
& bmp
)
299 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
302 wxBitmap::wxBitmap( const wxString
&filename
, int type
)
304 LoadFile( filename
, type
);
306 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
309 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
))
311 m_refData
= new wxBitmapRefData();
313 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
314 M_BMPDATA
->m_bitmap
=
315 gdk_bitmap_create_from_data( wxRootWindow
->window
, (gchar
*) bits
, width
, height
);
316 M_BMPDATA
->m_width
= width
;
317 M_BMPDATA
->m_height
= height
;
318 M_BMPDATA
->m_bpp
= 1;
320 wxCHECK_RET( M_BMPDATA
->m_bitmap
, wxT("couldn't create bitmap") );
322 if (wxTheBitmapList
) wxTheBitmapList
->AddBitmap(this);
325 wxBitmap::~wxBitmap()
327 if (wxTheBitmapList
) wxTheBitmapList
->DeleteObject(this);
330 wxBitmap
& wxBitmap::operator = ( const wxBitmap
& bmp
)
332 if (*this == bmp
) return (*this);
337 bool wxBitmap::operator == ( const wxBitmap
& bmp
)
339 return m_refData
== bmp
.m_refData
;
342 bool wxBitmap::operator != ( const wxBitmap
& bmp
)
344 return m_refData
!= bmp
.m_refData
;
347 bool wxBitmap::Ok() const
349 return (m_refData
!= NULL
);
352 int wxBitmap::GetHeight() const
354 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
356 return M_BMPDATA
->m_height
;
359 int wxBitmap::GetWidth() const
361 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
363 return M_BMPDATA
->m_width
;
366 int wxBitmap::GetDepth() const
368 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
370 return M_BMPDATA
->m_bpp
;
373 wxMask
*wxBitmap::GetMask() const
375 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
377 return M_BMPDATA
->m_mask
;
380 void wxBitmap::SetMask( wxMask
*mask
)
382 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
384 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
386 M_BMPDATA
->m_mask
= mask
;
389 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
392 (rect
.x
>= 0) && (rect
.y
>= 0) &&
393 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) && (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
394 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
396 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
397 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
401 GdkGC
*gc
= gdk_gc_new( ret
.GetPixmap() );
402 gdk_draw_pixmap( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
403 gdk_gc_destroy( gc
);
407 GdkGC
*gc
= gdk_gc_new( ret
.GetBitmap() );
408 gdk_draw_bitmap( ret
.GetBitmap(), gc
, GetBitmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
409 gdk_gc_destroy( gc
);
414 wxMask
*mask
= new wxMask
;
415 mask
->m_bitmap
= gdk_pixmap_new( wxRootWindow
->window
, rect
.width
, rect
.height
, 1 );
417 GdkGC
*gc
= gdk_gc_new( mask
->m_bitmap
);
418 gdk_draw_bitmap( mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, 0, 0, rect
.x
, rect
.y
, rect
.width
, rect
.height
);
419 gdk_gc_destroy( gc
);
427 bool wxBitmap::SaveFile( const wxString
&name
, int type
, wxPalette
*WXUNUSED(palette
) )
429 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid bitmap") );
431 // Try to save the bitmap via wxImage handlers:
433 wxImage
image( *this );
434 if (image
.Ok()) return image
.SaveFile( name
, type
);
440 bool wxBitmap::LoadFile( const wxString
&name
, int type
)
444 if (!wxFileExists(name
)) return FALSE
;
446 if (type
== wxBITMAP_TYPE_XPM
)
448 m_refData
= new wxBitmapRefData();
450 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
452 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm( wxRootWindow
->window
, &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( wxRootWindow
->window
)->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 void wxBitmap::SetBitmap( GdkPixmap
*bitmap
)
511 if (!m_refData
) m_refData
= new wxBitmapRefData();
513 M_BMPDATA
->m_bitmap
= bitmap
;
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
;