]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/bitmap.cpp
Added wxSizer::Insert()
[wxWidgets.git] / src / gtk / bitmap.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: bitmap.cpp
3// Purpose:
4// Author: Robert Roebling
6f65e337 5// RCS-ID: $Id$
01111366 6// Copyright: (c) 1998 Robert Roebling
8bbe427f 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
10#ifdef __GNUG__
11#pragma implementation "bitmap.h"
12#endif
13
14#include "wx/bitmap.h"
52cbfcf0 15#include "wx/icon.h"
fd0eed64 16#include "wx/filefn.h"
83624f79
RR
17#include "wx/image.h"
18
20e05ffb
RR
19#include <gdk/gdk.h>
20#include <gdk/gdkprivate.h>
21#include <gdk/gdkx.h>
c801d85f
KB
22
23//-----------------------------------------------------------------------------
24// wxMask
25//-----------------------------------------------------------------------------
26
27IMPLEMENT_DYNAMIC_CLASS(wxMask,wxObject)
28
8bbe427f 29wxMask::wxMask()
c801d85f 30{
fd0eed64 31 m_bitmap = (GdkBitmap *) NULL;
ff7b1510 32}
c801d85f 33
91b8de8d 34wxMask::wxMask( const wxBitmap& bitmap, const wxColour& colour )
c801d85f 35{
72a7edf0 36 m_bitmap = (GdkBitmap *) NULL;
91b8de8d 37 Create( bitmap, colour );
ff7b1510 38}
c801d85f 39
91b8de8d 40wxMask::wxMask( const wxBitmap& bitmap, int paletteIndex )
c801d85f 41{
72a7edf0 42 m_bitmap = (GdkBitmap *) NULL;
91b8de8d 43 Create( bitmap, paletteIndex );
ff7b1510 44}
c801d85f 45
91b8de8d 46wxMask::wxMask( const wxBitmap& bitmap )
c801d85f 47{
72a7edf0 48 m_bitmap = (GdkBitmap *) NULL;
91b8de8d 49 Create( bitmap );
ff7b1510 50}
c801d85f 51
8bbe427f 52wxMask::~wxMask()
c801d85f 53{
72a7edf0
RR
54 if (m_bitmap)
55 gdk_bitmap_unref( m_bitmap );
ff7b1510 56}
c801d85f 57
1fb4de31
RR
58bool wxMask::Create( const wxBitmap& bitmap,
59 const wxColour& colour )
91b8de8d
RR
60{
61 if (m_bitmap)
284b4c88 62 {
91b8de8d 63 gdk_bitmap_unref( m_bitmap );
284b4c88 64 m_bitmap = (GdkBitmap*) NULL;
91b8de8d 65 }
1fb4de31
RR
66
67 wxImage image( bitmap );
68 if (!image.Ok()) return FALSE;
69
70 GdkVisual *visual = gdk_visual_get_system();
71
72 GdkImage *mask_image = gdk_image_new( GDK_IMAGE_FASTEST, visual, image.GetWidth(), image.GetHeight() );
73 if (!mask_image) return FALSE;
74
75 GdkWindow *parent = (GdkWindow*) &gdk_root_parent;
76 m_bitmap = gdk_pixmap_new( parent, image.GetWidth(), image.GetHeight(), 1 );
77
78
79 unsigned char *data = image.GetData();
80 int index = 0;
81
82 unsigned char red = colour.Red();
83 unsigned char green = colour.Green();
84 unsigned char blue = colour.Blue();
85
86 int bpp = visual->depth;
87 if ((bpp == 16) && (visual->red_mask != 0xf800)) bpp = 15;
88 if (bpp == 15)
89 {
90 red = red & 0xf8;
91 blue = blue & 0xf8;
92 green = green & 0xf8;
93 }
94 if (bpp == 16)
95 {
96 red = red & 0xf8;
97 blue = blue & 0xfc;
98 green = green & 0xf8;
99 }
100
101 for (int j = 0; j < image.GetHeight(); j++)
102 for (int i = 0; i < image.GetWidth(); i++)
103 {
104 if ((data[index] == red) &&
105 (data[index+1] == green) &&
106 (data[index+2] == blue))
107 {
108 gdk_image_put_pixel( mask_image, i, j, 1 );
109 }
110 else
111 {
112 gdk_image_put_pixel( mask_image, i, j, 1 );
113 }
114 index += 3;
115 }
116
117 GdkGC *mask_gc = gdk_gc_new( m_bitmap );
118
119 gdk_draw_image( m_bitmap, mask_gc, mask_image, 0, 0, 0, 0, image.GetWidth(), image.GetHeight() );
120
121 gdk_gc_unref( mask_gc );
122 gdk_image_destroy( mask_image );
123
91b8de8d
RR
124 return FALSE;
125}
126
284b4c88
VZ
127bool wxMask::Create( const wxBitmap& WXUNUSED(bitmap),
128 int WXUNUSED(paletteIndex) )
91b8de8d
RR
129{
130 if (m_bitmap)
284b4c88 131 {
91b8de8d 132 gdk_bitmap_unref( m_bitmap );
284b4c88 133 m_bitmap = (GdkBitmap*) NULL;
91b8de8d 134 }
284b4c88 135
223d09f6 136 wxFAIL_MSG( wxT("not implemented") );
284b4c88 137
91b8de8d
RR
138 return FALSE;
139}
140
141bool wxMask::Create( const wxBitmap& bitmap )
142{
143 if (m_bitmap)
284b4c88 144 {
91b8de8d 145 gdk_bitmap_unref( m_bitmap );
284b4c88 146 m_bitmap = (GdkBitmap*) NULL;
91b8de8d 147 }
284b4c88 148
91b8de8d 149 if (!bitmap.Ok()) return FALSE;
284b4c88 150
223d09f6 151 wxCHECK_MSG( bitmap.GetBitmap(), FALSE, wxT("Cannot create mask from colour bitmap") );
284b4c88 152
91b8de8d 153 m_bitmap = gdk_pixmap_new( (GdkWindow*) &gdk_root_parent, bitmap.GetWidth(), bitmap.GetHeight(), 1 );
284b4c88 154
91b8de8d 155 if (!m_bitmap) return FALSE;
284b4c88 156
91b8de8d 157 GdkGC *gc = gdk_gc_new( m_bitmap );
284b4c88 158
91b8de8d 159 gdk_draw_bitmap( m_bitmap, gc, bitmap.GetBitmap(), 0, 0, 0, 0, bitmap.GetWidth(), bitmap.GetHeight() );
284b4c88 160
91b8de8d 161 gdk_gc_unref( gc );
284b4c88 162
91b8de8d
RR
163 return TRUE;
164}
165
166GdkBitmap *wxMask::GetBitmap() const
c801d85f 167{
fd0eed64 168 return m_bitmap;
ff7b1510 169}
8bbe427f 170
c801d85f
KB
171//-----------------------------------------------------------------------------
172// wxBitmap
173//-----------------------------------------------------------------------------
174
175class wxBitmapRefData: public wxObjectRefData
176{
fd0eed64 177public:
91b8de8d
RR
178 wxBitmapRefData();
179 ~wxBitmapRefData();
8bbe427f 180
fd0eed64
RR
181 GdkPixmap *m_pixmap;
182 GdkBitmap *m_bitmap;
183 wxMask *m_mask;
184 int m_width;
185 int m_height;
186 int m_bpp;
187 wxPalette *m_palette;
c801d85f
KB
188};
189
8bbe427f 190wxBitmapRefData::wxBitmapRefData()
c801d85f 191{
fd0eed64
RR
192 m_pixmap = (GdkPixmap *) NULL;
193 m_bitmap = (GdkBitmap *) NULL;
194 m_mask = (wxMask *) NULL;
195 m_width = 0;
196 m_height = 0;
197 m_bpp = 0;
198 m_palette = (wxPalette *) NULL;
ff7b1510 199}
c801d85f 200
8bbe427f 201wxBitmapRefData::~wxBitmapRefData()
c801d85f 202{
fd0eed64
RR
203 if (m_pixmap) gdk_pixmap_unref( m_pixmap );
204 if (m_bitmap) gdk_bitmap_unref( m_bitmap );
205 if (m_mask) delete m_mask;
206 if (m_palette) delete m_palette;
ff7b1510 207}
c801d85f
KB
208
209//-----------------------------------------------------------------------------
210
211#define M_BMPDATA ((wxBitmapRefData *)m_refData)
212
213IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxGDIObject)
214
8bbe427f 215wxBitmap::wxBitmap()
c801d85f 216{
fd0eed64 217 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
ff7b1510 218}
8bbe427f 219
debe6624 220wxBitmap::wxBitmap( int width, int height, int depth )
c801d85f 221{
223d09f6 222 wxCHECK_RET( (width > 0) && (height > 0), wxT("invalid bitmap size") )
284b4c88 223
fd0eed64 224 GdkWindow *parent = (GdkWindow*) &gdk_root_parent;
eefa26be 225 if (depth == -1) depth = gdk_window_get_visual( parent )->depth;
284b4c88 226
eefa26be 227 wxCHECK_RET( (depth == gdk_window_get_visual( parent )->depth) ||
223d09f6 228 (depth == 1), wxT("invalid bitmap depth") )
8bbe427f 229
eefa26be 230 m_refData = new wxBitmapRefData();
fd0eed64 231 M_BMPDATA->m_mask = (wxMask *) NULL;
fd0eed64
RR
232 M_BMPDATA->m_width = width;
233 M_BMPDATA->m_height = height;
eefa26be
RR
234 if (depth == 1)
235 {
236 M_BMPDATA->m_bitmap = gdk_pixmap_new( parent, width, height, 1 );
237 M_BMPDATA->m_bpp = 1;
238 }
239 else
240 {
241 M_BMPDATA->m_pixmap = gdk_pixmap_new( parent, width, height, depth );
242 M_BMPDATA->m_bpp = gdk_window_get_visual( parent )->depth;
243 }
8bbe427f 244
fd0eed64 245 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
ff7b1510 246}
c801d85f 247
e52f60e6
RR
248wxBitmap::wxBitmap( const char **bits )
249{
223d09f6 250 wxCHECK_RET( bits != NULL, wxT("invalid bitmap data") )
8bbe427f 251
e52f60e6
RR
252 m_refData = new wxBitmapRefData();
253
254 GdkBitmap *mask = (GdkBitmap*) NULL;
255 GdkWindow *parent = (GdkWindow*) &gdk_root_parent;
8bbe427f 256
e52f60e6 257 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm_d( parent, &mask, NULL, (gchar **) bits );
8bbe427f 258
e52f60e6
RR
259 if (mask)
260 {
261 M_BMPDATA->m_mask = new wxMask();
262 M_BMPDATA->m_mask->m_bitmap = mask;
263 }
8bbe427f 264
e52f60e6 265 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
8bbe427f 266
e52f60e6
RR
267 M_BMPDATA->m_bpp = gdk_window_get_visual( parent )->depth; // ?
268 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
269}
8bbe427f 270
c801d85f
KB
271wxBitmap::wxBitmap( char **bits )
272{
223d09f6 273 wxCHECK_RET( bits != NULL, wxT("invalid bitmap data") )
8bbe427f 274
fd0eed64 275 m_refData = new wxBitmapRefData();
c801d85f 276
fd0eed64
RR
277 GdkBitmap *mask = (GdkBitmap*) NULL;
278 GdkWindow *parent = (GdkWindow*) &gdk_root_parent;
8bbe427f 279
fd0eed64 280 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm_d( parent, &mask, NULL, (gchar **) bits );
8bbe427f 281
223d09f6 282 wxCHECK_RET( M_BMPDATA->m_pixmap, wxT("couldn't create pixmap") );
8bbe427f 283
fd0eed64
RR
284 if (mask)
285 {
286 M_BMPDATA->m_mask = new wxMask();
287 M_BMPDATA->m_mask->m_bitmap = mask;
288 }
8bbe427f 289
fd0eed64 290 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
8bbe427f 291
fd0eed64 292 M_BMPDATA->m_bpp = gdk_window_get_visual( parent )->depth; // ?
fd0eed64 293 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
ff7b1510 294}
8bbe427f 295
c801d85f
KB
296wxBitmap::wxBitmap( const wxBitmap& bmp )
297{
fd0eed64 298 Ref( bmp );
8bbe427f 299
fd0eed64 300 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
ff7b1510 301}
6f65e337 302
debe6624 303wxBitmap::wxBitmap( const wxString &filename, int type )
c801d85f 304{
fd0eed64 305 LoadFile( filename, type );
8bbe427f 306
fd0eed64 307 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
ff7b1510 308}
c801d85f 309
debe6624 310wxBitmap::wxBitmap( const char bits[], int width, int height, int WXUNUSED(depth))
6f65e337 311{
fd0eed64 312 m_refData = new wxBitmapRefData();
6f65e337 313
fd0eed64 314 M_BMPDATA->m_mask = (wxMask *) NULL;
8bbe427f 315 M_BMPDATA->m_bitmap =
fd0eed64
RR
316 gdk_bitmap_create_from_data( (GdkWindow*) &gdk_root_parent, (gchar *) bits, width, height );
317 M_BMPDATA->m_width = width;
318 M_BMPDATA->m_height = height;
319 M_BMPDATA->m_bpp = 1;
6f65e337 320
223d09f6 321 wxCHECK_RET( M_BMPDATA->m_bitmap, wxT("couldn't create bitmap") );
8bbe427f 322
fd0eed64 323 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
6f65e337 324}
8bbe427f
VZ
325
326wxBitmap::~wxBitmap()
c801d85f 327{
fd0eed64 328 if (wxTheBitmapList) wxTheBitmapList->DeleteObject(this);
ff7b1510 329}
8bbe427f 330
c801d85f
KB
331wxBitmap& wxBitmap::operator = ( const wxBitmap& bmp )
332{
8bbe427f
VZ
333 if (*this == bmp) return (*this);
334 Ref( bmp );
335 return *this;
ff7b1510 336}
8bbe427f 337
c801d85f
KB
338bool wxBitmap::operator == ( const wxBitmap& bmp )
339{
8bbe427f 340 return m_refData == bmp.m_refData;
ff7b1510 341}
8bbe427f 342
c801d85f
KB
343bool wxBitmap::operator != ( const wxBitmap& bmp )
344{
8bbe427f 345 return m_refData != bmp.m_refData;
ff7b1510 346}
8bbe427f 347
91b8de8d 348bool wxBitmap::Ok() const
c801d85f 349{
fd0eed64 350 return (m_refData != NULL);
ff7b1510 351}
8bbe427f 352
91b8de8d 353int wxBitmap::GetHeight() const
c801d85f 354{
223d09f6 355 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
e55ad60e 356
fd0eed64 357 return M_BMPDATA->m_height;
ff7b1510 358}
c801d85f 359
91b8de8d 360int wxBitmap::GetWidth() const
c801d85f 361{
223d09f6 362 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
8bbe427f 363
fd0eed64 364 return M_BMPDATA->m_width;
ff7b1510 365}
c801d85f 366
91b8de8d 367int wxBitmap::GetDepth() const
c801d85f 368{
223d09f6 369 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
8bbe427f 370
fd0eed64 371 return M_BMPDATA->m_bpp;
ff7b1510 372}
c801d85f 373
91b8de8d 374wxMask *wxBitmap::GetMask() const
c801d85f 375{
223d09f6 376 wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") );
8bbe427f 377
fd0eed64 378 return M_BMPDATA->m_mask;
ff7b1510 379}
c801d85f
KB
380
381void wxBitmap::SetMask( wxMask *mask )
382{
223d09f6 383 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
8bbe427f 384
fd0eed64 385 if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask;
8bbe427f 386
fd0eed64 387 M_BMPDATA->m_mask = mask;
ff7b1510 388}
c801d85f 389
fd0eed64 390bool wxBitmap::SaveFile( const wxString &name, int type, wxPalette *WXUNUSED(palette) )
c801d85f 391{
223d09f6 392 wxCHECK_MSG( Ok(), FALSE, wxT("invalid bitmap") );
8bbe427f 393
b75dd496 394 // Try to save the bitmap via wxImage handlers:
fd0eed64 395 {
4bc67cc5 396 wxImage image( *this );
284b4c88 397 if (image.Ok()) return image.SaveFile( name, type );
fd0eed64 398 }
8bbe427f 399
fd0eed64 400 return FALSE;
ff7b1510 401}
c801d85f 402
fd0eed64 403bool wxBitmap::LoadFile( const wxString &name, int type )
c801d85f 404{
fd0eed64 405 UnRef();
8bbe427f 406
fd0eed64 407 if (!wxFileExists(name)) return FALSE;
8bbe427f 408
fd0eed64
RR
409 if (type == wxBITMAP_TYPE_XPM)
410 {
411 m_refData = new wxBitmapRefData();
8bbe427f 412
fd0eed64
RR
413 GdkBitmap *mask = (GdkBitmap*) NULL;
414 GdkWindow *parent = (GdkWindow*) &gdk_root_parent;
8bbe427f 415
62bd5cf0 416 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm( parent, &mask, NULL, name.fn_str() );
8bbe427f 417
fd0eed64
RR
418 if (mask)
419 {
420 M_BMPDATA->m_mask = new wxMask();
421 M_BMPDATA->m_mask->m_bitmap = mask;
422 }
8bbe427f 423
fd0eed64 424 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
8bbe427f 425 M_BMPDATA->m_bpp = gdk_window_get_visual( parent )->depth;
fd0eed64 426 }
b75dd496 427 else // try if wxImage can load it
fd0eed64
RR
428 {
429 wxImage image;
b75dd496 430 if (!image.LoadFile( name, type )) return FALSE;
4bc67cc5 431 if (image.Ok()) *this = image.ConvertToBitmap();
b75dd496 432 else return FALSE;
fd0eed64 433 }
8bbe427f 434
fd0eed64 435 return TRUE;
ff7b1510 436}
8bbe427f 437
91b8de8d 438wxPalette *wxBitmap::GetPalette() const
c801d85f 439{
fd0eed64 440 if (!Ok()) return (wxPalette *) NULL;
8bbe427f 441
fd0eed64 442 return M_BMPDATA->m_palette;
ff7b1510 443}
c801d85f 444
4bc67cc5
RR
445void wxBitmap::SetHeight( int height )
446{
447 if (!m_refData) m_refData = new wxBitmapRefData();
448
449 M_BMPDATA->m_height = height;
450}
451
452void wxBitmap::SetWidth( int width )
453{
454 if (!m_refData) m_refData = new wxBitmapRefData();
455
456 M_BMPDATA->m_width = width;
457}
458
459void wxBitmap::SetDepth( int depth )
460{
461 if (!m_refData) m_refData = new wxBitmapRefData();
462
463 M_BMPDATA->m_bpp = depth;
464}
465
466void wxBitmap::SetPixmap( GdkPixmap *pixmap )
467{
468 if (!m_refData) m_refData = new wxBitmapRefData();
469
470 M_BMPDATA->m_pixmap = pixmap;
471}
472
91b8de8d 473GdkPixmap *wxBitmap::GetPixmap() const
c801d85f 474{
223d09f6 475 wxCHECK_MSG( Ok(), (GdkPixmap *) NULL, wxT("invalid bitmap") );
8bbe427f 476
fd0eed64 477 return M_BMPDATA->m_pixmap;
ff7b1510 478}
8bbe427f 479
91b8de8d 480GdkBitmap *wxBitmap::GetBitmap() const
6f65e337 481{
223d09f6 482 wxCHECK_MSG( Ok(), (GdkBitmap *) NULL, wxT("invalid bitmap") );
8bbe427f 483
fd0eed64 484 return M_BMPDATA->m_bitmap;
ff7b1510 485}