]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/bitmap.cpp
fixed wrong return value and child creation login in GetWindowChild() (includes patch...
[wxWidgets.git] / src / gtk1 / bitmap.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
3cbab641 2// Name: src/gtk1/bitmap.cpp
c801d85f
KB
3// Purpose:
4// Author: Robert Roebling
6f65e337 5// RCS-ID: $Id$
01111366 6// Copyright: (c) 1998 Robert Roebling
65571936 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
14f355c2
VS
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
c801d85f 13#include "wx/bitmap.h"
670f9935
WS
14
15#ifndef WX_PRECOMP
16 #include "wx/app.h"
f38924e8 17 #include "wx/dcmemory.h"
559a723c 18 #include "wx/palette.h"
923d28da 19 #include "wx/icon.h"
670f9935
WS
20#endif // WX_PRECOMP
21
fd0eed64 22#include "wx/filefn.h"
83624f79
RR
23#include "wx/image.h"
24
20e05ffb 25#include <gdk/gdk.h>
d76fe38b 26#include <gtk/gtk.h>
b5f01ae0
VS
27#include <gdk/gdkx.h>
28
3cbab641 29#include <gdk/gdkrgb.h>
13111b2a 30
463c4d71 31#include "wx/math.h"
783da845 32
3cbab641
MR
33extern
34void gdk_wx_draw_bitmap (GdkDrawable *drawable,
35 GdkGC *gc,
36 GdkDrawable *src,
37 gint xsrc,
38 gint ysrc,
39 gint xdest,
40 gint ydest,
41 gint width,
42 gint height);
f6bcfd97 43
d76fe38b
RR
44//-----------------------------------------------------------------------------
45// data
46//-----------------------------------------------------------------------------
47
c2fa61e8 48extern GtkWidget *wxGetRootWindow();
c801d85f
KB
49
50//-----------------------------------------------------------------------------
51// wxMask
52//-----------------------------------------------------------------------------
53
54IMPLEMENT_DYNAMIC_CLASS(wxMask,wxObject)
55
8bbe427f 56wxMask::wxMask()
c801d85f 57{
fd0eed64 58 m_bitmap = (GdkBitmap *) NULL;
ff7b1510 59}
c801d85f 60
91b8de8d 61wxMask::wxMask( const wxBitmap& bitmap, const wxColour& colour )
c801d85f 62{
72a7edf0 63 m_bitmap = (GdkBitmap *) NULL;
91b8de8d 64 Create( bitmap, colour );
ff7b1510 65}
c801d85f 66
0b04c4e0 67#if wxUSE_PALETTE
91b8de8d 68wxMask::wxMask( const wxBitmap& bitmap, int paletteIndex )
c801d85f 69{
72a7edf0 70 m_bitmap = (GdkBitmap *) NULL;
91b8de8d 71 Create( bitmap, paletteIndex );
ff7b1510 72}
0b04c4e0 73#endif // wxUSE_PALETTE
c801d85f 74
91b8de8d 75wxMask::wxMask( const wxBitmap& bitmap )
c801d85f 76{
72a7edf0 77 m_bitmap = (GdkBitmap *) NULL;
91b8de8d 78 Create( bitmap );
ff7b1510 79}
c801d85f 80
8bbe427f 81wxMask::~wxMask()
c801d85f 82{
13111b2a 83 if (m_bitmap)
72a7edf0 84 gdk_bitmap_unref( m_bitmap );
ff7b1510 85}
c801d85f 86
1fb4de31
RR
87bool wxMask::Create( const wxBitmap& bitmap,
88 const wxColour& colour )
91b8de8d
RR
89{
90 if (m_bitmap)
284b4c88 91 {
91b8de8d 92 gdk_bitmap_unref( m_bitmap );
284b4c88 93 m_bitmap = (GdkBitmap*) NULL;
91b8de8d 94 }
13111b2a 95
368d59f0 96 wxImage image = bitmap.ConvertToImage();
902725ee 97 if (!image.Ok()) return false;
13111b2a 98
c2fa61e8 99 m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, image.GetWidth(), image.GetHeight(), 1 );
f9ee644e 100 GdkGC *gc = gdk_gc_new( m_bitmap );
13111b2a 101
f9ee644e
RR
102 GdkColor color;
103 color.red = 65000;
104 color.green = 65000;
105 color.blue = 65000;
106 color.pixel = 1;
107 gdk_gc_set_foreground( gc, &color );
108 gdk_gc_set_fill( gc, GDK_SOLID );
109 gdk_draw_rectangle( m_bitmap, gc, TRUE, 0, 0, image.GetWidth(), image.GetHeight() );
13111b2a 110
1fb4de31
RR
111 unsigned char *data = image.GetData();
112 int index = 0;
13111b2a 113
1fb4de31
RR
114 unsigned char red = colour.Red();
115 unsigned char green = colour.Green();
116 unsigned char blue = colour.Blue();
13111b2a 117
005f5d18 118 GdkVisual *visual = wxTheApp->GetGdkVisual();
c2fa61e8 119
1fb4de31 120 int bpp = visual->depth;
2eefae6e
VZ
121 if ((bpp == 16) && (visual->red_mask != 0xf800))
122 bpp = 15;
1fb4de31
RR
123 if (bpp == 15)
124 {
125 red = red & 0xf8;
1fb4de31 126 green = green & 0xf8;
f6bcfd97 127 blue = blue & 0xf8;
2eefae6e
VZ
128 }
129 else if (bpp == 16)
1fb4de31
RR
130 {
131 red = red & 0xf8;
f6bcfd97
BP
132 green = green & 0xfc;
133 blue = blue & 0xf8;
2eefae6e
VZ
134 }
135 else if (bpp == 12)
005f5d18
RR
136 {
137 red = red & 0xf0;
138 green = green & 0xf0;
139 blue = blue & 0xf0;
1fb4de31 140 }
13111b2a 141
f9ee644e
RR
142 color.red = 0;
143 color.green = 0;
144 color.blue = 0;
145 color.pixel = 0;
146 gdk_gc_set_foreground( gc, &color );
13111b2a 147
1fb4de31 148 for (int j = 0; j < image.GetHeight(); j++)
f9ee644e 149 {
f2593d0d
RR
150 int start_x = -1;
151 int i;
152 for (i = 0; i < image.GetWidth(); i++)
1fb4de31 153 {
13111b2a
VZ
154 if ((data[index] == red) &&
155 (data[index+1] == green) &&
156 (data[index+2] == blue))
157 {
158 if (start_x == -1)
159 start_x = i;
160 }
161 else
162 {
163 if (start_x != -1)
164 {
165 gdk_draw_line( m_bitmap, gc, start_x, j, i-1, j );
166 start_x = -1;
167 }
f9ee644e
RR
168 }
169 index += 3;
170 }
171 if (start_x != -1)
172 gdk_draw_line( m_bitmap, gc, start_x, j, i, j );
173 }
1fb4de31 174
f9ee644e 175 gdk_gc_unref( gc );
1fb4de31 176
902725ee 177 return true;
91b8de8d
RR
178}
179
0b04c4e0 180#if wxUSE_PALETTE
b5f01ae0 181bool wxMask::Create( const wxBitmap& bitmap, int paletteIndex )
91b8de8d 182{
b5f01ae0
VS
183 unsigned char r,g,b;
184 wxPalette *pal = bitmap.GetPalette();
284b4c88 185
902725ee 186 wxCHECK_MSG( pal, false, wxT("Cannot create mask from bitmap without palette") );
c2fa61e8 187
b5f01ae0 188 pal->GetRGB(paletteIndex, &r, &g, &b);
284b4c88 189
b5f01ae0 190 return Create(bitmap, wxColour(r, g, b));
91b8de8d 191}
0b04c4e0 192#endif // wxUSE_PALETTE
91b8de8d
RR
193
194bool wxMask::Create( const wxBitmap& bitmap )
195{
196 if (m_bitmap)
284b4c88 197 {
91b8de8d 198 gdk_bitmap_unref( m_bitmap );
284b4c88 199 m_bitmap = (GdkBitmap*) NULL;
91b8de8d 200 }
284b4c88 201
902725ee 202 if (!bitmap.Ok()) return false;
284b4c88 203
902725ee 204 wxCHECK_MSG( bitmap.GetBitmap(), false, wxT("Cannot create mask from colour bitmap") );
284b4c88 205
c2fa61e8 206 m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, bitmap.GetWidth(), bitmap.GetHeight(), 1 );
284b4c88 207
902725ee 208 if (!m_bitmap) return false;
284b4c88 209
91b8de8d 210 GdkGC *gc = gdk_gc_new( m_bitmap );
284b4c88 211
f6bcfd97 212 gdk_wx_draw_bitmap( m_bitmap, gc, bitmap.GetBitmap(), 0, 0, 0, 0, bitmap.GetWidth(), bitmap.GetHeight() );
284b4c88 213
91b8de8d 214 gdk_gc_unref( gc );
284b4c88 215
902725ee 216 return true;
91b8de8d
RR
217}
218
219GdkBitmap *wxMask::GetBitmap() const
c801d85f 220{
fd0eed64 221 return m_bitmap;
ff7b1510 222}
8bbe427f 223
c801d85f
KB
224//-----------------------------------------------------------------------------
225// wxBitmap
226//-----------------------------------------------------------------------------
227
228class wxBitmapRefData: public wxObjectRefData
229{
fd0eed64 230public:
f2593d0d
RR
231 wxBitmapRefData();
232 ~wxBitmapRefData();
233
234 GdkPixmap *m_pixmap;
235 GdkBitmap *m_bitmap;
236 wxMask *m_mask;
237 int m_width;
238 int m_height;
239 int m_bpp;
240 wxPalette *m_palette;
c801d85f
KB
241};
242
8bbe427f 243wxBitmapRefData::wxBitmapRefData()
c801d85f 244{
fd0eed64
RR
245 m_pixmap = (GdkPixmap *) NULL;
246 m_bitmap = (GdkBitmap *) NULL;
247 m_mask = (wxMask *) NULL;
248 m_width = 0;
249 m_height = 0;
250 m_bpp = 0;
251 m_palette = (wxPalette *) NULL;
ff7b1510 252}
c801d85f 253
8bbe427f 254wxBitmapRefData::~wxBitmapRefData()
c801d85f 255{
3ebcd89d
VZ
256 if (m_pixmap)
257 gdk_pixmap_unref( m_pixmap );
258 if (m_bitmap)
259 gdk_bitmap_unref( m_bitmap );
260 delete m_mask;
0b04c4e0 261#if wxUSE_PALETTE
3ebcd89d 262 delete m_palette;
0b04c4e0 263#endif // wxUSE_PALETTE
ff7b1510 264}
c801d85f
KB
265
266//-----------------------------------------------------------------------------
267
268#define M_BMPDATA ((wxBitmapRefData *)m_refData)
269
270IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxGDIObject)
271
8bbe427f 272wxBitmap::wxBitmap()
c801d85f 273{
ff7b1510 274}
8bbe427f 275
debe6624 276wxBitmap::wxBitmap( int width, int height, int depth )
c801d85f 277{
c826213d 278 Create( width, height, depth );
c826213d
RR
279}
280
281bool wxBitmap::Create( int width, int height, int depth )
282{
283 UnRef();
284
3ebcd89d
VZ
285 if ( width <= 0 || height <= 0 )
286 {
287 return false;
288 }
284b4c88 289
005f5d18 290 GdkVisual *visual = wxTheApp->GetGdkVisual();
284b4c88 291
3ebcd89d
VZ
292 if (depth == -1)
293 depth = visual->depth;
103aab26 294
902725ee 295 wxCHECK_MSG( (depth == visual->depth) || (depth == 1) || (depth == 32), false,
80f21842 296 wxT("invalid bitmap depth") );
8bbe427f 297
eefa26be 298 m_refData = new wxBitmapRefData();
fd0eed64 299 M_BMPDATA->m_mask = (wxMask *) NULL;
fd0eed64
RR
300 M_BMPDATA->m_width = width;
301 M_BMPDATA->m_height = height;
eefa26be
RR
302 if (depth == 1)
303 {
c2fa61e8 304 M_BMPDATA->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 );
eefa26be
RR
305 M_BMPDATA->m_bpp = 1;
306 }
307 else
308 {
c2fa61e8 309 M_BMPDATA->m_pixmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, depth );
103aab26 310 M_BMPDATA->m_bpp = visual->depth;
eefa26be 311 }
8bbe427f 312
c826213d 313 return Ok();
ff7b1510 314}
b5f01ae0 315
e838cc14 316bool wxBitmap::CreateFromXpm( const char **bits )
e52f60e6 317{
a11672a4
RR
318 UnRef();
319
637b7e4f 320 wxCHECK_MSG( bits != NULL, false, wxT("invalid bitmap data") );
8bbe427f 321
005f5d18 322 GdkVisual *visual = wxTheApp->GetGdkVisual();
c2fa61e8 323
e52f60e6
RR
324 m_refData = new wxBitmapRefData();
325
326 GdkBitmap *mask = (GdkBitmap*) NULL;
8bbe427f 327
c2fa61e8 328 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm_d( wxGetRootWindow()->window, &mask, NULL, (gchar **) bits );
8bbe427f 329
902725ee 330 wxCHECK_MSG( M_BMPDATA->m_pixmap, false, wxT("couldn't create pixmap") );
8bbe427f 331
fd0eed64
RR
332 if (mask)
333 {
334 M_BMPDATA->m_mask = new wxMask();
335 M_BMPDATA->m_mask->m_bitmap = mask;
336 }
8bbe427f 337
fd0eed64 338 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
2eefae6e 339
8cce8940 340 M_BMPDATA->m_bpp = visual->depth; // Can we get a different depth from create_from_xpm_d() ?
c2fa61e8 341
902725ee 342 return true;
ff7b1510 343}
b5f01ae0 344
783da845
RR
345wxBitmap wxBitmap::Rescale( int clipx, int clipy, int clipwidth, int clipheight, int newx, int newy )
346{
347 wxCHECK_MSG( Ok(), wxNullBitmap, wxT("invalid bitmap") );
348
349 if (newy==M_BMPDATA->m_width && newy==M_BMPDATA->m_height)
350 return *this;
902725ee 351
783da845
RR
352 int width = wxMax(newx, 1);
353 int height = wxMax(newy, 1);
354 width = wxMin(width, clipwidth);
355 height = wxMin(height, clipheight);
902725ee 356
feac7937 357 wxBitmap bmp;
783da845 358
3cbab641
MR
359 GdkImage *img = (GdkImage*) NULL;
360 if (GetPixmap())
361 img = gdk_image_get( GetPixmap(), 0, 0, GetWidth(), GetHeight() );
362 else if (GetBitmap())
363 img = gdk_image_get( GetBitmap(), 0, 0, GetWidth(), GetHeight() );
364 else
365 wxFAIL_MSG( wxT("Ill-formed bitmap") );
366
367 wxCHECK_MSG( img, wxNullBitmap, wxT("couldn't create image") );
368
369 int bpp = -1;
370
371
372 GdkGC *gc = NULL;
373 GdkPixmap *dstpix = NULL;
374 if (GetPixmap())
783da845 375 {
3cbab641
MR
376 GdkVisual *visual = gdk_window_get_visual( GetPixmap() );
377 if (visual == NULL)
378 visual = wxTheApp->GetGdkVisual();
379
380 bpp = visual->depth;
381 bmp = wxBitmap(width,height,bpp);
382 dstpix = bmp.GetPixmap();
383 gc = gdk_gc_new( dstpix );
783da845 384 }
3cbab641
MR
385
386 char *dst = NULL;
387 long dstbyteperline = 0;
388
389 if (GetBitmap())
783da845 390 {
3cbab641
MR
391 bpp = 1;
392 dstbyteperline = width/8*M_BMPDATA->m_bpp;
393 if (width*M_BMPDATA->m_bpp % 8 != 0)
394 dstbyteperline++;
395 dst = (char*) malloc(dstbyteperline*height);
396 }
feac7937 397
3cbab641
MR
398 // be careful to use the right scaling factor
399 float scx = (float)M_BMPDATA->m_width/(float)newx;
400 float scy = (float)M_BMPDATA->m_height/(float)newy;
401 // prepare accel-tables
402 int *tablex = (int *)calloc(width,sizeof(int));
403 int *tabley = (int *)calloc(height,sizeof(int));
783da845 404
3cbab641
MR
405 // accel table filled with clipped values
406 for (int x = 0; x < width; x++)
407 tablex[x] = (int) (scx * (x+clipx));
408 for (int y = 0; y < height; y++)
409 tabley[y] = (int) (scy * (y+clipy));
feac7937 410
3cbab641
MR
411 // Main rescaling routine starts here
412 for (int h = 0; h < height; h++)
413 {
414 char outbyte = 0;
415 int old_x = -1;
416 guint32 old_pixval = 0;
902725ee 417
3cbab641 418 for (int w = 0; w < width; w++)
783da845 419 {
3cbab641
MR
420 guint32 pixval;
421 int x = tablex[w];
422 if (x == old_x)
423 pixval = old_pixval;
424 else
425 {
426 pixval = gdk_image_get_pixel( img, x, tabley[h] );
427 old_pixval = pixval;
428 old_x = x;
429 }
783da845 430
3cbab641
MR
431 if (bpp == 1)
432 {
433 if (!pixval)
434 {
435 char bit=1;
436 char shift = bit << (w % 8);
437 outbyte |= shift;
438 }
902725ee 439
3cbab641
MR
440 if ((w+1)%8==0)
441 {
442 dst[h*dstbyteperline+w/8] = outbyte;
443 outbyte = 0;
444 }
445 }
446 else
447 {
448 GdkColor col;
449 col.pixel = pixval;
450 gdk_gc_set_foreground( gc, &col );
451 gdk_draw_point( dstpix, gc, w, h);
452 }
feac7937 453 }
902725ee 454
3cbab641
MR
455 // do not forget the last byte
456 if ((bpp == 1) && (width % 8 != 0))
457 dst[h*dstbyteperline+width/8] = outbyte;
458 }
feac7937 459
3cbab641
MR
460 gdk_image_destroy( img );
461 if (gc) gdk_gc_unref( gc );
462
463 if (bpp == 1)
464 {
465 bmp = wxBitmap( (const char *)dst, width, height, 1 );
466 free( dst );
467 }
468
469 if (GetMask())
470 {
471 dstbyteperline = width/8;
472 if (width % 8 != 0)
473 dstbyteperline++;
474 dst = (char*) malloc(dstbyteperline*height);
475 img = gdk_image_get( GetMask()->GetBitmap(), 0, 0, GetWidth(), GetHeight() );
783da845
RR
476
477 for (int h = 0; h < height; h++)
478 {
479 char outbyte = 0;
f9ebac93 480 int old_x = -1;
b63b07a8 481 guint32 old_pixval = 0;
feac7937 482
f9ebac93 483 for (int w = 0; w < width; w++)
783da845 484 {
f9ebac93
RR
485 guint32 pixval;
486 int x = tablex[w];
487 if (x == old_x)
488 pixval = old_pixval;
489 else
490 {
491 pixval = gdk_image_get_pixel( img, x, tabley[h] );
492 old_pixval = pixval;
493 old_x = x;
494 }
902725ee 495
3cbab641 496 if (pixval)
783da845 497 {
3cbab641
MR
498 char bit=1;
499 char shift = bit << (w % 8);
500 outbyte |= shift;
783da845 501 }
3cbab641
MR
502
503 if ((w+1)%8 == 0)
783da845 504 {
3cbab641
MR
505 dst[h*dstbyteperline+w/8] = outbyte;
506 outbyte = 0;
783da845
RR
507 }
508 }
902725ee 509
783da845 510 // do not forget the last byte
3cbab641 511 if (width % 8 != 0)
f9ebac93 512 dst[h*dstbyteperline+width/8] = outbyte;
783da845 513 }
3cbab641
MR
514 wxMask* mask = new wxMask;
515 mask->m_bitmap = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) dst, width, height );
516 bmp.SetMask(mask);
902725ee 517
3cbab641 518 free( dst );
783da845 519 gdk_image_destroy( img );
feac7937 520 }
902725ee 521
3cbab641
MR
522 free( tablex );
523 free( tabley );
524
902725ee 525 return bmp;
783da845
RR
526}
527
feac7937 528bool wxBitmap::CreateFromImage(const wxImage& image, int depth)
b5f01ae0 529{
a11672a4
RR
530 UnRef();
531
637b7e4f
VZ
532 wxCHECK_MSG( image.Ok(), false, wxT("invalid image") );
533 wxCHECK_MSG( depth == -1 || depth == 1, false, wxT("invalid bitmap depth") );
b5f01ae0 534
feac7937
VS
535 if (image.GetWidth() <= 0 || image.GetHeight() <= 0)
536 return false;
902725ee 537
feac7937 538 m_refData = new wxBitmapRefData();
3ebcd89d 539
feac7937 540 if (depth == 1)
3ebcd89d 541 {
feac7937 542 return CreateFromImageAsBitmap(image);
3ebcd89d 543 }
feac7937
VS
544 else
545 {
feac7937
VS
546 return CreateFromImageAsPixmap(image);
547 }
548}
3ebcd89d 549
feac7937
VS
550// conversion to mono bitmap:
551bool wxBitmap::CreateFromImageAsBitmap(const wxImage& img)
552{
553 // convert alpha channel to mask, if it is present:
554 wxImage image(img);
555 image.ConvertAlphaToMask();
902725ee 556
feac7937
VS
557 int width = image.GetWidth();
558 int height = image.GetHeight();
c2fa61e8 559
3ebcd89d
VZ
560 SetHeight( height );
561 SetWidth( width );
562
feac7937 563 SetBitmap( gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 ) );
b5f01ae0 564
feac7937 565 SetDepth( 1 );
2eefae6e 566
feac7937 567 GdkVisual *visual = wxTheApp->GetGdkVisual();
b5f01ae0 568
feac7937 569 // Create picture image
b5f01ae0 570
feac7937 571 unsigned char *data_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
b5f01ae0 572
feac7937
VS
573 GdkImage *data_image =
574 gdk_image_new_bitmap( visual, data_data, width, height );
b5f01ae0 575
feac7937 576 // Create mask image
b5f01ae0 577
feac7937 578 GdkImage *mask_image = (GdkImage*) NULL;
b5f01ae0 579
feac7937
VS
580 if (image.HasMask())
581 {
582 unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
b5f01ae0 583
feac7937 584 mask_image = gdk_image_new_bitmap( visual, mask_data, width, height );
b5f01ae0 585
feac7937
VS
586 wxMask *mask = new wxMask();
587 mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 );
b5f01ae0 588
feac7937
VS
589 SetMask( mask );
590 }
b5f01ae0 591
feac7937
VS
592 int r_mask = image.GetMaskRed();
593 int g_mask = image.GetMaskGreen();
594 int b_mask = image.GetMaskBlue();
b5f01ae0 595
feac7937 596 unsigned char* data = image.GetData();
b5f01ae0 597
feac7937
VS
598 int index = 0;
599 for (int y = 0; y < height; y++)
600 {
601 for (int x = 0; x < width; x++)
b5f01ae0 602 {
feac7937
VS
603 int r = data[index];
604 index++;
605 int g = data[index];
606 index++;
607 int b = data[index];
608 index++;
609
610 if (image.HasMask())
b5f01ae0 611 {
feac7937
VS
612 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
613 gdk_image_put_pixel( mask_image, x, y, 1 );
b5f01ae0 614 else
feac7937
VS
615 gdk_image_put_pixel( mask_image, x, y, 0 );
616 }
b5f01ae0 617
feac7937
VS
618 if ((r == 255) && (b == 255) && (g == 255))
619 gdk_image_put_pixel( data_image, x, y, 1 );
620 else
621 gdk_image_put_pixel( data_image, x, y, 0 );
b5f01ae0 622
feac7937
VS
623 } // for
624 } // for
b5f01ae0 625
feac7937 626 // Blit picture
b5f01ae0 627
feac7937 628 GdkGC *data_gc = gdk_gc_new( GetBitmap() );
b5f01ae0 629
feac7937 630 gdk_draw_image( GetBitmap(), data_gc, data_image, 0, 0, 0, 0, width, height );
b5f01ae0 631
feac7937
VS
632 gdk_image_destroy( data_image );
633 gdk_gc_unref( data_gc );
b5f01ae0 634
feac7937 635 // Blit mask
b5f01ae0 636
feac7937
VS
637 if (image.HasMask())
638 {
639 GdkGC *mask_gc = gdk_gc_new( GetMask()->GetBitmap() );
b5f01ae0 640
feac7937
VS
641 gdk_draw_image( GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height );
642
643 gdk_image_destroy( mask_image );
644 gdk_gc_unref( mask_gc );
b5f01ae0 645 }
c2fa61e8 646
feac7937
VS
647 return true;
648}
902725ee 649
feac7937
VS
650// conversion to colour bitmap:
651bool wxBitmap::CreateFromImageAsPixmap(const wxImage& img)
652{
653 // convert alpha channel to mask, if it is present:
654 wxImage image(img);
655 image.ConvertAlphaToMask();
902725ee 656
feac7937
VS
657 int width = image.GetWidth();
658 int height = image.GetHeight();
659
660 SetHeight( height );
661 SetWidth( width );
662
663 SetPixmap( gdk_pixmap_new( wxGetRootWindow()->window, width, height, -1 ) );
b5f01ae0 664
feac7937 665 GdkVisual *visual = wxTheApp->GetGdkVisual();
b5f01ae0 666
feac7937 667 int bpp = visual->depth;
b5f01ae0 668
feac7937 669 SetDepth( bpp );
b5f01ae0 670
feac7937
VS
671 if ((bpp == 16) && (visual->red_mask != 0xf800))
672 bpp = 15;
673 else if (bpp < 8)
674 bpp = 8;
2eefae6e 675
feac7937
VS
676 // We handle 8-bit bitmaps ourselves using the colour cube, 12-bit
677 // visuals are not supported by GDK so we do these ourselves, too.
678 // 15-bit and 16-bit should actually work and 24-bit certainly does.
9b72a74d 679#ifdef __sgi
feac7937 680 if (!image.HasMask() && (bpp > 16))
9b72a74d 681#else
feac7937 682 if (!image.HasMask() && (bpp > 12))
9b72a74d 683#endif
feac7937 684 {
902725ee 685 static bool s_hasInitialized = false;
b5f01ae0 686
feac7937
VS
687 if (!s_hasInitialized)
688 {
689 gdk_rgb_init();
902725ee 690 s_hasInitialized = true;
feac7937 691 }
b5f01ae0 692
feac7937 693 GdkGC *gc = gdk_gc_new( GetPixmap() );
b5f01ae0 694
feac7937
VS
695 gdk_draw_rgb_image( GetPixmap(),
696 gc,
697 0, 0,
698 width, height,
699 GDK_RGB_DITHER_NONE,
700 image.GetData(),
701 width*3 );
b5f01ae0 702
feac7937 703 gdk_gc_unref( gc );
902725ee 704 return true;
feac7937 705 }
b5f01ae0 706
feac7937 707 // Create picture image
b5f01ae0 708
feac7937
VS
709 GdkImage *data_image =
710 gdk_image_new( GDK_IMAGE_FASTEST, visual, width, height );
b5f01ae0 711
feac7937 712 // Create mask image
b5f01ae0 713
feac7937 714 GdkImage *mask_image = (GdkImage*) NULL;
b5f01ae0 715
feac7937
VS
716 if (image.HasMask())
717 {
718 unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
b5f01ae0 719
feac7937 720 mask_image = gdk_image_new_bitmap( visual, mask_data, width, height );
b5f01ae0 721
feac7937
VS
722 wxMask *mask = new wxMask();
723 mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 );
b5f01ae0 724
feac7937
VS
725 SetMask( mask );
726 }
b5f01ae0 727
feac7937 728 // Render
b5f01ae0 729
feac7937
VS
730 enum byte_order { RGB, RBG, BRG, BGR, GRB, GBR };
731 byte_order b_o = RGB;
b5f01ae0 732
feac7937
VS
733 if (bpp > 8)
734 {
735 if ((visual->red_mask > visual->green_mask) && (visual->green_mask > visual->blue_mask)) b_o = RGB;
736 else if ((visual->red_mask > visual->blue_mask) && (visual->blue_mask > visual->green_mask)) b_o = RBG;
737 else if ((visual->blue_mask > visual->red_mask) && (visual->red_mask > visual->green_mask)) b_o = BRG;
738 else if ((visual->blue_mask > visual->green_mask) && (visual->green_mask > visual->red_mask)) b_o = BGR;
739 else if ((visual->green_mask > visual->red_mask) && (visual->red_mask > visual->blue_mask)) b_o = GRB;
740 else if ((visual->green_mask > visual->blue_mask) && (visual->blue_mask > visual->red_mask)) b_o = GBR;
741 }
b5f01ae0 742
feac7937
VS
743 int r_mask = image.GetMaskRed();
744 int g_mask = image.GetMaskGreen();
745 int b_mask = image.GetMaskBlue();
b5f01ae0 746
feac7937 747 unsigned char* data = image.GetData();
b5f01ae0 748
feac7937
VS
749 int index = 0;
750 for (int y = 0; y < height; y++)
751 {
752 for (int x = 0; x < width; x++)
b5f01ae0 753 {
feac7937
VS
754 int r = data[index];
755 index++;
756 int g = data[index];
757 index++;
758 int b = data[index];
759 index++;
760
761 if (image.HasMask())
b5f01ae0 762 {
feac7937
VS
763 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
764 gdk_image_put_pixel( mask_image, x, y, 1 );
765 else
766 gdk_image_put_pixel( mask_image, x, y, 0 );
767 }
b5f01ae0 768
feac7937
VS
769 switch (bpp)
770 {
771 case 8:
b5f01ae0 772 {
feac7937
VS
773 int pixel = -1;
774 if (wxTheApp->m_colorCube)
b5f01ae0 775 {
feac7937
VS
776 pixel = wxTheApp->m_colorCube[ ((r & 0xf8) << 7) + ((g & 0xf8) << 2) + ((b & 0xf8) >> 3) ];
777 }
778 else
779 {
780 GdkColormap *cmap = gtk_widget_get_default_colormap();
781 GdkColor *colors = cmap->colors;
782 int max = 3 * (65536);
783
784 for (int i = 0; i < cmap->size; i++)
b5f01ae0 785 {
feac7937
VS
786 int rdiff = (r << 8) - colors[i].red;
787 int gdiff = (g << 8) - colors[i].green;
788 int bdiff = (b << 8) - colors[i].blue;
789 int sum = ABS (rdiff) + ABS (gdiff) + ABS (bdiff);
790 if (sum < max) { pixel = i; max = sum; }
b5f01ae0 791 }
feac7937 792 }
2eefae6e 793
feac7937 794 gdk_image_put_pixel( data_image, x, y, pixel );
2eefae6e 795
feac7937
VS
796 break;
797 }
798 case 12: // SGI only
799 {
800 guint32 pixel = 0;
801 switch (b_o)
b5f01ae0 802 {
feac7937
VS
803 case RGB: pixel = ((r & 0xf0) << 4) | (g & 0xf0) | ((b & 0xf0) >> 4); break;
804 case RBG: pixel = ((r & 0xf0) << 4) | (b & 0xf0) | ((g & 0xf0) >> 4); break;
805 case GRB: pixel = ((g & 0xf0) << 4) | (r & 0xf0) | ((b & 0xf0) >> 4); break;
806 case GBR: pixel = ((g & 0xf0) << 4) | (b & 0xf0) | ((r & 0xf0) >> 4); break;
807 case BRG: pixel = ((b & 0xf0) << 4) | (r & 0xf0) | ((g & 0xf0) >> 4); break;
808 case BGR: pixel = ((b & 0xf0) << 4) | (g & 0xf0) | ((r & 0xf0) >> 4); break;
b5f01ae0 809 }
feac7937
VS
810 gdk_image_put_pixel( data_image, x, y, pixel );
811 break;
812 }
813 case 15:
814 {
815 guint32 pixel = 0;
816 switch (b_o)
b5f01ae0 817 {
feac7937
VS
818 case RGB: pixel = ((r & 0xf8) << 7) | ((g & 0xf8) << 2) | ((b & 0xf8) >> 3); break;
819 case RBG: pixel = ((r & 0xf8) << 7) | ((b & 0xf8) << 2) | ((g & 0xf8) >> 3); break;
820 case GRB: pixel = ((g & 0xf8) << 7) | ((r & 0xf8) << 2) | ((b & 0xf8) >> 3); break;
821 case GBR: pixel = ((g & 0xf8) << 7) | ((b & 0xf8) << 2) | ((r & 0xf8) >> 3); break;
822 case BRG: pixel = ((b & 0xf8) << 7) | ((r & 0xf8) << 2) | ((g & 0xf8) >> 3); break;
823 case BGR: pixel = ((b & 0xf8) << 7) | ((g & 0xf8) << 2) | ((r & 0xf8) >> 3); break;
b5f01ae0 824 }
feac7937
VS
825 gdk_image_put_pixel( data_image, x, y, pixel );
826 break;
827 }
828 case 16:
829 {
830 // I actually don't know if for 16-bit displays, it is alway the green
831 // component or the second component which has 6 bits.
832 guint32 pixel = 0;
833 switch (b_o)
b5f01ae0 834 {
feac7937
VS
835 case RGB: pixel = ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | ((b & 0xf8) >> 3); break;
836 case RBG: pixel = ((r & 0xf8) << 8) | ((b & 0xfc) << 3) | ((g & 0xf8) >> 3); break;
837 case GRB: pixel = ((g & 0xf8) << 8) | ((r & 0xfc) << 3) | ((b & 0xf8) >> 3); break;
838 case GBR: pixel = ((g & 0xf8) << 8) | ((b & 0xfc) << 3) | ((r & 0xf8) >> 3); break;
839 case BRG: pixel = ((b & 0xf8) << 8) | ((r & 0xfc) << 3) | ((g & 0xf8) >> 3); break;
840 case BGR: pixel = ((b & 0xf8) << 8) | ((g & 0xfc) << 3) | ((r & 0xf8) >> 3); break;
8ab696e0 841 }
feac7937
VS
842 gdk_image_put_pixel( data_image, x, y, pixel );
843 break;
844 }
845 case 32:
846 case 24:
847 {
848 guint32 pixel = 0;
849 switch (b_o)
8ab696e0 850 {
feac7937
VS
851 case RGB: pixel = (r << 16) | (g << 8) | b; break;
852 case RBG: pixel = (r << 16) | (b << 8) | g; break;
853 case BRG: pixel = (b << 16) | (r << 8) | g; break;
854 case BGR: pixel = (b << 16) | (g << 8) | r; break;
855 case GRB: pixel = (g << 16) | (r << 8) | b; break;
856 case GBR: pixel = (g << 16) | (b << 8) | r; break;
b5f01ae0 857 }
feac7937 858 gdk_image_put_pixel( data_image, x, y, pixel );
902725ee 859 break;
b5f01ae0 860 }
feac7937
VS
861 default: break;
862 }
863 } // for
864 } // for
b5f01ae0 865
feac7937 866 // Blit picture
b5f01ae0 867
feac7937 868 GdkGC *data_gc = gdk_gc_new( GetPixmap() );
b5f01ae0 869
feac7937 870 gdk_draw_image( GetPixmap(), data_gc, data_image, 0, 0, 0, 0, width, height );
b5f01ae0 871
feac7937
VS
872 gdk_image_destroy( data_image );
873 gdk_gc_unref( data_gc );
b5f01ae0 874
feac7937 875 // Blit mask
b5f01ae0 876
feac7937
VS
877 if (image.HasMask())
878 {
879 GdkGC *mask_gc = gdk_gc_new( GetMask()->GetBitmap() );
b5f01ae0 880
feac7937 881 gdk_draw_image( GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height );
b5f01ae0 882
feac7937
VS
883 gdk_image_destroy( mask_image );
884 gdk_gc_unref( mask_gc );
b5f01ae0
VS
885 }
886
feac7937 887 return true;
b5f01ae0
VS
888}
889
feac7937
VS
890wxImage wxBitmap::ConvertToImage() const
891{
892 wxImage image;
c2fa61e8 893
feac7937 894 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
902725ee 895
feac7937
VS
896 image.Create(GetWidth(), GetHeight());
897 unsigned char *data = image.GetData();
2eefae6e 898
b5f01ae0
VS
899 if (!data)
900 {
b5f01ae0
VS
901 wxFAIL_MSG( wxT("couldn't create image") );
902 return wxNullImage;
903 }
904
3cbab641
MR
905 // the colour used as transparent one in wxImage and the one it is
906 // replaced with when it really occurs in the bitmap
907 static const int MASK_RED = 1;
908 static const int MASK_GREEN = 2;
909 static const int MASK_BLUE = 3;
910 static const int MASK_BLUE_REPLACEMENT = 2;
b5f01ae0 911
3cbab641 912 GdkImage *gdk_image = (GdkImage*) NULL;
b5f01ae0 913
3cbab641
MR
914 if (HasPixmap())
915 {
916 gdk_image = gdk_image_get( GetPixmap(),
917 0, 0,
918 GetWidth(), GetHeight() );
919 }
920 else if (GetBitmap())
921 {
922 gdk_image = gdk_image_get( GetBitmap(),
923 0, 0,
924 GetWidth(), GetHeight() );
b5f01ae0 925 }
feac7937 926 else
b5f01ae0 927 {
3cbab641
MR
928 wxFAIL_MSG( wxT("Ill-formed bitmap") );
929 }
b5f01ae0 930
3cbab641 931 wxCHECK_MSG( gdk_image, wxNullImage, wxT("couldn't create image") );
feac7937 932
3cbab641
MR
933 GdkImage *gdk_image_mask = (GdkImage*) NULL;
934 if (GetMask())
935 {
936 gdk_image_mask = gdk_image_get( GetMask()->GetBitmap(),
937 0, 0,
938 GetWidth(), GetHeight() );
feac7937 939
3cbab641
MR
940 image.SetMaskColour( MASK_RED, MASK_GREEN, MASK_BLUE );
941 }
feac7937 942
3cbab641
MR
943 int bpp = -1;
944 int red_shift_right = 0;
945 int green_shift_right = 0;
946 int blue_shift_right = 0;
947 int red_shift_left = 0;
948 int green_shift_left = 0;
949 int blue_shift_left = 0;
950 bool use_shift = false;
feac7937 951
3cbab641
MR
952 if (GetPixmap())
953 {
954 GdkVisual *visual = gdk_window_get_visual( GetPixmap() );
955 if (visual == NULL)
956 visual = wxTheApp->GetGdkVisual();
957
958 bpp = visual->depth;
959 if (bpp == 16)
960 bpp = visual->red_prec + visual->green_prec + visual->blue_prec;
961 red_shift_right = visual->red_shift;
962 red_shift_left = 8-visual->red_prec;
963 green_shift_right = visual->green_shift;
964 green_shift_left = 8-visual->green_prec;
965 blue_shift_right = visual->blue_shift;
966 blue_shift_left = 8-visual->blue_prec;
967
968 use_shift = (visual->type == GDK_VISUAL_TRUE_COLOR) || (visual->type == GDK_VISUAL_DIRECT_COLOR);
969 }
970 if (GetBitmap())
971 {
972 bpp = 1;
973 }
feac7937
VS
974
975
3cbab641 976 GdkColormap *cmap = gtk_widget_get_default_colormap();
feac7937 977
3cbab641
MR
978 long pos = 0;
979 for (int j = 0; j < GetHeight(); j++)
980 {
981 for (int i = 0; i < GetWidth(); i++)
feac7937 982 {
3cbab641
MR
983 wxUint32 pixel = gdk_image_get_pixel( gdk_image, i, j );
984 if (bpp == 1)
8ab696e0 985 {
3cbab641 986 if (pixel == 0)
8ab696e0 987 {
3cbab641
MR
988 data[pos] = 0;
989 data[pos+1] = 0;
990 data[pos+2] = 0;
2eefae6e 991 }
feac7937 992 else
2eefae6e 993 {
3cbab641
MR
994 data[pos] = 255;
995 data[pos+1] = 255;
996 data[pos+2] = 255;
b5f01ae0 997 }
3cbab641
MR
998 }
999 else if (use_shift)
1000 {
1001 data[pos] = (pixel >> red_shift_right) << red_shift_left;
1002 data[pos+1] = (pixel >> green_shift_right) << green_shift_left;
1003 data[pos+2] = (pixel >> blue_shift_right) << blue_shift_left;
1004 }
1005 else if (cmap->colors)
1006 {
1007 data[pos] = cmap->colors[pixel].red >> 8;
1008 data[pos+1] = cmap->colors[pixel].green >> 8;
1009 data[pos+2] = cmap->colors[pixel].blue >> 8;
1010 }
1011 else
1012 {
1013 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
1014 }
b5f01ae0 1015
3cbab641
MR
1016 if (gdk_image_mask)
1017 {
1018 int mask_pixel = gdk_image_get_pixel( gdk_image_mask, i, j );
1019 if (mask_pixel == 0)
feac7937 1020 {
3cbab641
MR
1021 data[pos] = MASK_RED;
1022 data[pos+1] = MASK_GREEN;
1023 data[pos+2] = MASK_BLUE;
1024 }
1025 else if ( data[pos] == MASK_RED &&
1026 data[pos+1] == MASK_GREEN &&
1027 data[pos+2] == MASK_BLUE )
1028 {
1029 data[pos+2] = MASK_BLUE_REPLACEMENT;
feac7937 1030 }
feac7937 1031 }
b5f01ae0 1032
3cbab641
MR
1033 pos += 3;
1034 }
feac7937 1035 }
b5f01ae0 1036
3cbab641
MR
1037 gdk_image_destroy( gdk_image );
1038 if (gdk_image_mask) gdk_image_destroy( gdk_image_mask );
1039
b5f01ae0
VS
1040 return image;
1041}
1042
4611dd06 1043wxBitmap::wxBitmap( const wxString &filename, wxBitmapType type )
c801d85f 1044{
fd0eed64 1045 LoadFile( filename, type );
ff7b1510 1046}
c801d85f 1047
debe6624 1048wxBitmap::wxBitmap( const char bits[], int width, int height, int WXUNUSED(depth))
6f65e337 1049{
3ebcd89d
VZ
1050 if ( width > 0 && height > 0 )
1051 {
1052 m_refData = new wxBitmapRefData();
6f65e337 1053
3ebcd89d
VZ
1054 M_BMPDATA->m_mask = (wxMask *) NULL;
1055 M_BMPDATA->m_bitmap = gdk_bitmap_create_from_data
1056 (
1057 wxGetRootWindow()->window,
1058 (gchar *) bits,
1059 width,
1060 height
1061 );
1062 M_BMPDATA->m_width = width;
1063 M_BMPDATA->m_height = height;
1064 M_BMPDATA->m_bpp = 1;
6f65e337 1065
3ebcd89d
VZ
1066 wxASSERT_MSG( M_BMPDATA->m_bitmap, wxT("couldn't create bitmap") );
1067 }
6f65e337 1068}
8bbe427f
VZ
1069
1070wxBitmap::~wxBitmap()
c801d85f 1071{
ff7b1510 1072}
8bbe427f 1073
f6bcfd97 1074bool wxBitmap::operator == ( const wxBitmap& bmp ) const
c801d85f 1075{
8bbe427f 1076 return m_refData == bmp.m_refData;
ff7b1510 1077}
8bbe427f 1078
f6bcfd97 1079bool wxBitmap::operator != ( const wxBitmap& bmp ) const
c801d85f 1080{
8bbe427f 1081 return m_refData != bmp.m_refData;
ff7b1510 1082}
8bbe427f 1083
91b8de8d 1084bool wxBitmap::Ok() const
c801d85f 1085{
902725ee 1086 return (m_refData != NULL) &&
3cbab641 1087 (M_BMPDATA->m_bitmap || M_BMPDATA->m_pixmap);
ff7b1510 1088}
8bbe427f 1089
91b8de8d 1090int wxBitmap::GetHeight() const
c801d85f 1091{
223d09f6 1092 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
e55ad60e 1093
fd0eed64 1094 return M_BMPDATA->m_height;
ff7b1510 1095}
c801d85f 1096
91b8de8d 1097int wxBitmap::GetWidth() const
c801d85f 1098{
223d09f6 1099 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
8bbe427f 1100
fd0eed64 1101 return M_BMPDATA->m_width;
ff7b1510 1102}
c801d85f 1103
91b8de8d 1104int wxBitmap::GetDepth() const
c801d85f 1105{
223d09f6 1106 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
8bbe427f 1107
fd0eed64 1108 return M_BMPDATA->m_bpp;
ff7b1510 1109}
c801d85f 1110
91b8de8d 1111wxMask *wxBitmap::GetMask() const
c801d85f 1112{
223d09f6 1113 wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") );
8bbe427f 1114
fd0eed64 1115 return M_BMPDATA->m_mask;
ff7b1510 1116}
c801d85f
KB
1117
1118void wxBitmap::SetMask( wxMask *mask )
1119{
223d09f6 1120 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
8bbe427f 1121
fd0eed64 1122 if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask;
8bbe427f 1123
fd0eed64 1124 M_BMPDATA->m_mask = mask;
ff7b1510 1125}
c801d85f 1126
db0aec83
VS
1127bool wxBitmap::CopyFromIcon(const wxIcon& icon)
1128{
1129 *this = icon;
902725ee 1130 return true;
db0aec83
VS
1131}
1132
17bec151
RR
1133wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const
1134{
1135 wxCHECK_MSG( Ok() &&
13111b2a
VZ
1136 (rect.x >= 0) && (rect.y >= 0) &&
1137 (rect.x+rect.width <= M_BMPDATA->m_width) && (rect.y+rect.height <= M_BMPDATA->m_height),
17bec151 1138 wxNullBitmap, wxT("invalid bitmap or bitmap region") );
13111b2a 1139
17bec151
RR
1140 wxBitmap ret( rect.width, rect.height, M_BMPDATA->m_bpp );
1141 wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") );
13111b2a 1142
3cbab641 1143 if (ret.GetPixmap())
17bec151 1144 {
3cbab641
MR
1145 GdkGC *gc = gdk_gc_new( ret.GetPixmap() );
1146 gdk_draw_pixmap( ret.GetPixmap(), gc, GetPixmap(), rect.x, rect.y, 0, 0, rect.width, rect.height );
1147 gdk_gc_destroy( gc );
17bec151
RR
1148 }
1149 else
1150 {
3cbab641
MR
1151 GdkGC *gc = gdk_gc_new( ret.GetBitmap() );
1152 GdkColor col;
1153 col.pixel = 0xFFFFFF;
1154 gdk_gc_set_foreground( gc, &col );
1155 col.pixel = 0;
1156 gdk_gc_set_background( gc, &col );
1157 gdk_wx_draw_bitmap( ret.GetBitmap(), gc, GetBitmap(), rect.x, rect.y, 0, 0, rect.width, rect.height );
1158 gdk_gc_destroy( gc );
17bec151 1159 }
13111b2a 1160
17bec151
RR
1161 if (GetMask())
1162 {
1163 wxMask *mask = new wxMask;
c2fa61e8 1164 mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, rect.width, rect.height, 1 );
13111b2a 1165
17bec151 1166 GdkGC *gc = gdk_gc_new( mask->m_bitmap );
f9ebac93
RR
1167 GdkColor col;
1168 col.pixel = 0xFFFFFF;
1169 gdk_gc_set_foreground( gc, &col );
1170 col.pixel = 0;
1171 gdk_gc_set_background( gc, &col );
1172 gdk_wx_draw_bitmap( mask->m_bitmap, gc, M_BMPDATA->m_mask->m_bitmap, rect.x, rect.y, 0, 0, rect.width, rect.height );
13111b2a
VZ
1173 gdk_gc_destroy( gc );
1174
1175 ret.SetMask( mask );
17bec151 1176 }
13111b2a 1177
17bec151
RR
1178 return ret;
1179}
1180
4611dd06 1181bool wxBitmap::SaveFile( const wxString &name, wxBitmapType type, const wxPalette *WXUNUSED(palette) ) const
c801d85f 1182{
902725ee 1183 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
8bbe427f 1184
b75dd496 1185 // Try to save the bitmap via wxImage handlers:
fd0eed64 1186 {
368d59f0 1187 wxImage image = ConvertToImage();
284b4c88 1188 if (image.Ok()) return image.SaveFile( name, type );
fd0eed64 1189 }
8bbe427f 1190
902725ee 1191 return false;
ff7b1510 1192}
c801d85f 1193
4611dd06 1194bool wxBitmap::LoadFile( const wxString &name, wxBitmapType type )
c801d85f 1195{
fd0eed64 1196 UnRef();
8bbe427f 1197
3ebcd89d 1198 if (!wxFileExists(name))
902725ee 1199 return false;
8bbe427f 1200
005f5d18 1201 GdkVisual *visual = wxTheApp->GetGdkVisual();
c2fa61e8 1202
fd0eed64
RR
1203 if (type == wxBITMAP_TYPE_XPM)
1204 {
1205 m_refData = new wxBitmapRefData();
8bbe427f 1206
fd0eed64 1207 GdkBitmap *mask = (GdkBitmap*) NULL;
8bbe427f 1208
3ebcd89d
VZ
1209 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm
1210 (
1211 wxGetRootWindow()->window,
1212 &mask,
1213 NULL,
1214 name.fn_str()
1215 );
8bbe427f 1216
fd0eed64
RR
1217 if (mask)
1218 {
1219 M_BMPDATA->m_mask = new wxMask();
1220 M_BMPDATA->m_mask->m_bitmap = mask;
1221 }
8bbe427f 1222
fd0eed64 1223 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
c2fa61e8 1224
103aab26 1225 M_BMPDATA->m_bpp = visual->depth;
fd0eed64 1226 }
b75dd496 1227 else // try if wxImage can load it
fd0eed64
RR
1228 {
1229 wxImage image;
3ebcd89d 1230 if ( !image.LoadFile( name, type ) || !image.Ok() )
902725ee 1231 return false;
3ebcd89d
VZ
1232
1233 *this = wxBitmap(image);
fd0eed64 1234 }
8bbe427f 1235
902725ee 1236 return true;
ff7b1510 1237}
8bbe427f 1238
0b04c4e0 1239#if wxUSE_PALETTE
91b8de8d 1240wxPalette *wxBitmap::GetPalette() const
c801d85f 1241{
3ebcd89d
VZ
1242 if (!Ok())
1243 return (wxPalette *) NULL;
8bbe427f 1244
fd0eed64 1245 return M_BMPDATA->m_palette;
ff7b1510 1246}
c801d85f 1247
4611dd06
RR
1248void wxBitmap::SetPalette(const wxPalette& WXUNUSED(palette))
1249{
1250 // TODO
1251}
0b04c4e0 1252#endif // wxUSE_PALETTE
4611dd06 1253
4bc67cc5
RR
1254void wxBitmap::SetHeight( int height )
1255{
3ebcd89d
VZ
1256 if (!m_refData)
1257 m_refData = new wxBitmapRefData();
4bc67cc5
RR
1258
1259 M_BMPDATA->m_height = height;
1260}
1261
1262void wxBitmap::SetWidth( int width )
1263{
3ebcd89d
VZ
1264 if (!m_refData)
1265 m_refData = new wxBitmapRefData();
4bc67cc5
RR
1266
1267 M_BMPDATA->m_width = width;
1268}
1269
1270void wxBitmap::SetDepth( int depth )
1271{
3ebcd89d
VZ
1272 if (!m_refData)
1273 m_refData = new wxBitmapRefData();
4bc67cc5
RR
1274
1275 M_BMPDATA->m_bpp = depth;
1276}
1277
1278void wxBitmap::SetPixmap( GdkPixmap *pixmap )
1279{
3ebcd89d
VZ
1280 if (!m_refData)
1281 m_refData = new wxBitmapRefData();
4bc67cc5
RR
1282
1283 M_BMPDATA->m_pixmap = pixmap;
1284}
1285
82ea63e6
RR
1286void wxBitmap::SetBitmap( GdkPixmap *bitmap )
1287{
3ebcd89d
VZ
1288 if (!m_refData)
1289 m_refData = new wxBitmapRefData();
82ea63e6
RR
1290
1291 M_BMPDATA->m_bitmap = bitmap;
1292}
1293
91b8de8d 1294GdkPixmap *wxBitmap::GetPixmap() const
c801d85f 1295{
223d09f6 1296 wxCHECK_MSG( Ok(), (GdkPixmap *) NULL, wxT("invalid bitmap") );
8bbe427f 1297
fd0eed64 1298 return M_BMPDATA->m_pixmap;
ff7b1510 1299}
8bbe427f 1300
feac7937
VS
1301bool wxBitmap::HasPixmap() const
1302{
1303 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1304
1305 return M_BMPDATA->m_pixmap != NULL;
1306}
1307
91b8de8d 1308GdkBitmap *wxBitmap::GetBitmap() const
6f65e337 1309{
223d09f6 1310 wxCHECK_MSG( Ok(), (GdkBitmap *) NULL, wxT("invalid bitmap") );
8bbe427f 1311
fd0eed64 1312 return M_BMPDATA->m_bitmap;
ff7b1510 1313}
feac7937 1314
284f2b59
RR
1315void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp)
1316{
284f2b59 1317 return NULL;
284f2b59
RR
1318}
1319
17a1ebd1 1320void wxBitmap::UngetRawData(wxPixelDataBase& WXUNUSED(data))
284f2b59
RR
1321{
1322}
1323
0ff2a74d 1324
902725ee 1325bool wxBitmap::HasAlpha() const
0ff2a74d 1326{
0ff2a74d 1327 return false;
0ff2a74d
RR
1328}
1329
1330void wxBitmap::UseAlpha()
902725ee 1331{
0ff2a74d
RR
1332}
1333
4b61c88d
RR
1334//-----------------------------------------------------------------------------
1335// wxBitmapHandler
1336//-----------------------------------------------------------------------------
1337
1338IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler,wxBitmapHandlerBase)
1339
1340wxBitmapHandler::~wxBitmapHandler()
1341{
1342}
1343
17a1ebd1
VZ
1344bool wxBitmapHandler::Create(wxBitmap * WXUNUSED(bitmap),
1345 void * WXUNUSED(data),
1346 long WXUNUSED(type),
1347 int WXUNUSED(width),
1348 int WXUNUSED(height),
1349 int WXUNUSED(depth))
4b61c88d 1350{
17a1ebd1
VZ
1351 wxFAIL_MSG( _T("not implemented") );
1352
902725ee 1353 return false;
4b61c88d
RR
1354}
1355
17a1ebd1
VZ
1356bool wxBitmapHandler::LoadFile(wxBitmap * WXUNUSED(bitmap),
1357 const wxString& WXUNUSED(name),
1358 long WXUNUSED(flags),
1359 int WXUNUSED(desiredWidth),
1360 int WXUNUSED(desiredHeight))
4b61c88d 1361{
17a1ebd1
VZ
1362 wxFAIL_MSG( _T("not implemented") );
1363
902725ee 1364 return false;
4b61c88d
RR
1365}
1366
17a1ebd1
VZ
1367bool wxBitmapHandler::SaveFile(const wxBitmap * WXUNUSED(bitmap),
1368 const wxString& WXUNUSED(name),
1369 int WXUNUSED(type),
1370 const wxPalette * WXUNUSED(palette))
4b61c88d 1371{
17a1ebd1
VZ
1372 wxFAIL_MSG( _T("not implemented") );
1373
902725ee 1374 return false;
4b61c88d
RR
1375}
1376
1377/* static */ void wxBitmap::InitStandardHandlers()
1378{
1379 // TODO: Insert handler based on GdkPixbufs handler later
1380}