]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/bitmap.cpp
ignore clicks on a toolbar but outside any button
[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
22bd9387
VZ
14#include "wx/defs.h"
15
16#include "wx/palette.h"
c801d85f 17#include "wx/bitmap.h"
52cbfcf0 18#include "wx/icon.h"
fd0eed64 19#include "wx/filefn.h"
83624f79 20#include "wx/image.h"
f9ee644e 21#include "wx/dcmemory.h"
b5f01ae0 22#include "wx/app.h"
83624f79 23
20e05ffb 24#include <gdk/gdk.h>
d76fe38b 25#include <gtk/gtk.h>
b5f01ae0
VS
26#include <gdk/gdkx.h>
27
28#if (GTK_MINOR_VERSION > 0)
29#include <gdk/gdkrgb.h>
30#endif
13111b2a 31
f6bcfd97
BP
32extern void gdk_wx_draw_bitmap (GdkDrawable *drawable,
33 GdkGC *gc,
34 GdkDrawable *src,
35 gint xsrc,
36 gint ysrc,
37 gint xdest,
38 gint ydest,
39 gint width,
40 gint height);
41
d76fe38b
RR
42//-----------------------------------------------------------------------------
43// data
44//-----------------------------------------------------------------------------
45
c2fa61e8 46extern GtkWidget *wxGetRootWindow();
c801d85f
KB
47
48//-----------------------------------------------------------------------------
49// wxMask
50//-----------------------------------------------------------------------------
51
52IMPLEMENT_DYNAMIC_CLASS(wxMask,wxObject)
53
8bbe427f 54wxMask::wxMask()
c801d85f 55{
fd0eed64 56 m_bitmap = (GdkBitmap *) NULL;
ff7b1510 57}
c801d85f 58
91b8de8d 59wxMask::wxMask( const wxBitmap& bitmap, const wxColour& colour )
c801d85f 60{
72a7edf0 61 m_bitmap = (GdkBitmap *) NULL;
91b8de8d 62 Create( bitmap, colour );
ff7b1510 63}
c801d85f 64
91b8de8d 65wxMask::wxMask( const wxBitmap& bitmap, int paletteIndex )
c801d85f 66{
72a7edf0 67 m_bitmap = (GdkBitmap *) NULL;
91b8de8d 68 Create( bitmap, paletteIndex );
ff7b1510 69}
c801d85f 70
91b8de8d 71wxMask::wxMask( const wxBitmap& bitmap )
c801d85f 72{
72a7edf0 73 m_bitmap = (GdkBitmap *) NULL;
91b8de8d 74 Create( bitmap );
ff7b1510 75}
c801d85f 76
8bbe427f 77wxMask::~wxMask()
c801d85f 78{
13111b2a 79 if (m_bitmap)
72a7edf0 80 gdk_bitmap_unref( m_bitmap );
ff7b1510 81}
c801d85f 82
1fb4de31
RR
83bool wxMask::Create( const wxBitmap& bitmap,
84 const wxColour& colour )
91b8de8d
RR
85{
86 if (m_bitmap)
284b4c88 87 {
91b8de8d 88 gdk_bitmap_unref( m_bitmap );
284b4c88 89 m_bitmap = (GdkBitmap*) NULL;
91b8de8d 90 }
13111b2a 91
1fb4de31
RR
92 wxImage image( bitmap );
93 if (!image.Ok()) return FALSE;
13111b2a 94
c2fa61e8 95 m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, image.GetWidth(), image.GetHeight(), 1 );
f9ee644e 96 GdkGC *gc = gdk_gc_new( m_bitmap );
13111b2a 97
f9ee644e
RR
98 GdkColor color;
99 color.red = 65000;
100 color.green = 65000;
101 color.blue = 65000;
102 color.pixel = 1;
103 gdk_gc_set_foreground( gc, &color );
104 gdk_gc_set_fill( gc, GDK_SOLID );
105 gdk_draw_rectangle( m_bitmap, gc, TRUE, 0, 0, image.GetWidth(), image.GetHeight() );
13111b2a 106
1fb4de31
RR
107 unsigned char *data = image.GetData();
108 int index = 0;
13111b2a 109
1fb4de31
RR
110 unsigned char red = colour.Red();
111 unsigned char green = colour.Green();
112 unsigned char blue = colour.Blue();
13111b2a 113
005f5d18 114 GdkVisual *visual = wxTheApp->GetGdkVisual();
c2fa61e8 115
1fb4de31
RR
116 int bpp = visual->depth;
117 if ((bpp == 16) && (visual->red_mask != 0xf800)) bpp = 15;
118 if (bpp == 15)
119 {
120 red = red & 0xf8;
1fb4de31 121 green = green & 0xf8;
f6bcfd97 122 blue = blue & 0xf8;
005f5d18 123 } else
1fb4de31
RR
124 if (bpp == 16)
125 {
126 red = red & 0xf8;
f6bcfd97
BP
127 green = green & 0xfc;
128 blue = blue & 0xf8;
005f5d18
RR
129 } else
130 if (bpp == 12)
131 {
132 red = red & 0xf0;
133 green = green & 0xf0;
134 blue = blue & 0xf0;
1fb4de31 135 }
13111b2a 136
f9ee644e
RR
137 color.red = 0;
138 color.green = 0;
139 color.blue = 0;
140 color.pixel = 0;
141 gdk_gc_set_foreground( gc, &color );
13111b2a 142
1fb4de31 143 for (int j = 0; j < image.GetHeight(); j++)
f9ee644e 144 {
f2593d0d
RR
145 int start_x = -1;
146 int i;
147 for (i = 0; i < image.GetWidth(); i++)
1fb4de31 148 {
13111b2a
VZ
149 if ((data[index] == red) &&
150 (data[index+1] == green) &&
151 (data[index+2] == blue))
152 {
153 if (start_x == -1)
154 start_x = i;
155 }
156 else
157 {
158 if (start_x != -1)
159 {
160 gdk_draw_line( m_bitmap, gc, start_x, j, i-1, j );
161 start_x = -1;
162 }
f9ee644e
RR
163 }
164 index += 3;
165 }
166 if (start_x != -1)
167 gdk_draw_line( m_bitmap, gc, start_x, j, i, j );
168 }
1fb4de31 169
f9ee644e 170 gdk_gc_unref( gc );
1fb4de31 171
f9ee644e 172 return TRUE;
91b8de8d
RR
173}
174
b5f01ae0 175bool wxMask::Create( const wxBitmap& bitmap, int paletteIndex )
91b8de8d 176{
b5f01ae0
VS
177 unsigned char r,g,b;
178 wxPalette *pal = bitmap.GetPalette();
284b4c88 179
b5f01ae0 180 wxCHECK_MSG( pal, FALSE, wxT("Cannot create mask from bitmap without palette") );
c2fa61e8 181
b5f01ae0 182 pal->GetRGB(paletteIndex, &r, &g, &b);
284b4c88 183
b5f01ae0 184 return Create(bitmap, wxColour(r, g, b));
91b8de8d
RR
185}
186
187bool wxMask::Create( const wxBitmap& bitmap )
188{
189 if (m_bitmap)
284b4c88 190 {
91b8de8d 191 gdk_bitmap_unref( m_bitmap );
284b4c88 192 m_bitmap = (GdkBitmap*) NULL;
91b8de8d 193 }
284b4c88 194
91b8de8d 195 if (!bitmap.Ok()) return FALSE;
284b4c88 196
223d09f6 197 wxCHECK_MSG( bitmap.GetBitmap(), FALSE, wxT("Cannot create mask from colour bitmap") );
284b4c88 198
c2fa61e8 199 m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, bitmap.GetWidth(), bitmap.GetHeight(), 1 );
284b4c88 200
91b8de8d 201 if (!m_bitmap) return FALSE;
284b4c88 202
91b8de8d 203 GdkGC *gc = gdk_gc_new( m_bitmap );
284b4c88 204
f6bcfd97 205 gdk_wx_draw_bitmap( m_bitmap, gc, bitmap.GetBitmap(), 0, 0, 0, 0, bitmap.GetWidth(), bitmap.GetHeight() );
284b4c88 206
91b8de8d 207 gdk_gc_unref( gc );
284b4c88 208
91b8de8d
RR
209 return TRUE;
210}
211
212GdkBitmap *wxMask::GetBitmap() const
c801d85f 213{
fd0eed64 214 return m_bitmap;
ff7b1510 215}
8bbe427f 216
c801d85f
KB
217//-----------------------------------------------------------------------------
218// wxBitmap
219//-----------------------------------------------------------------------------
220
221class wxBitmapRefData: public wxObjectRefData
222{
fd0eed64 223public:
f2593d0d
RR
224 wxBitmapRefData();
225 ~wxBitmapRefData();
226
227 GdkPixmap *m_pixmap;
228 GdkBitmap *m_bitmap;
229 wxMask *m_mask;
230 int m_width;
231 int m_height;
232 int m_bpp;
233 wxPalette *m_palette;
c801d85f
KB
234};
235
8bbe427f 236wxBitmapRefData::wxBitmapRefData()
c801d85f 237{
fd0eed64
RR
238 m_pixmap = (GdkPixmap *) NULL;
239 m_bitmap = (GdkBitmap *) NULL;
240 m_mask = (wxMask *) NULL;
241 m_width = 0;
242 m_height = 0;
243 m_bpp = 0;
244 m_palette = (wxPalette *) NULL;
ff7b1510 245}
c801d85f 246
8bbe427f 247wxBitmapRefData::~wxBitmapRefData()
c801d85f 248{
fd0eed64
RR
249 if (m_pixmap) gdk_pixmap_unref( m_pixmap );
250 if (m_bitmap) gdk_bitmap_unref( m_bitmap );
251 if (m_mask) delete m_mask;
252 if (m_palette) delete m_palette;
ff7b1510 253}
c801d85f
KB
254
255//-----------------------------------------------------------------------------
256
257#define M_BMPDATA ((wxBitmapRefData *)m_refData)
258
259IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxGDIObject)
260
8bbe427f 261wxBitmap::wxBitmap()
c801d85f 262{
ff7b1510 263}
8bbe427f 264
debe6624 265wxBitmap::wxBitmap( int width, int height, int depth )
c801d85f 266{
c826213d 267 Create( width, height, depth );
c826213d
RR
268}
269
270bool wxBitmap::Create( int width, int height, int depth )
271{
272 UnRef();
273
274 wxCHECK_MSG( (width > 0) && (height > 0), FALSE, wxT("invalid bitmap size") )
284b4c88 275
005f5d18 276 GdkVisual *visual = wxTheApp->GetGdkVisual();
284b4c88 277
103aab26
RR
278 if (depth == -1) depth = visual->depth;
279
c826213d
RR
280 wxCHECK_MSG( (depth == visual->depth) ||
281 (depth == 1), FALSE, wxT("invalid bitmap depth") )
8bbe427f 282
eefa26be 283 m_refData = new wxBitmapRefData();
fd0eed64 284 M_BMPDATA->m_mask = (wxMask *) NULL;
fd0eed64
RR
285 M_BMPDATA->m_width = width;
286 M_BMPDATA->m_height = height;
eefa26be
RR
287 if (depth == 1)
288 {
c2fa61e8 289 M_BMPDATA->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 );
eefa26be
RR
290 M_BMPDATA->m_bpp = 1;
291 }
292 else
293 {
c2fa61e8 294 M_BMPDATA->m_pixmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, depth );
103aab26 295 M_BMPDATA->m_bpp = visual->depth;
eefa26be 296 }
8bbe427f 297
c826213d 298 return Ok();
ff7b1510 299}
b5f01ae0 300
e838cc14 301bool wxBitmap::CreateFromXpm( const char **bits )
e52f60e6 302{
a11672a4
RR
303 UnRef();
304
e838cc14 305 wxCHECK_MSG( bits != NULL, FALSE, wxT("invalid bitmap data") )
8bbe427f 306
005f5d18 307 GdkVisual *visual = wxTheApp->GetGdkVisual();
c2fa61e8 308
e52f60e6
RR
309 m_refData = new wxBitmapRefData();
310
311 GdkBitmap *mask = (GdkBitmap*) NULL;
8bbe427f 312
c2fa61e8 313 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm_d( wxGetRootWindow()->window, &mask, NULL, (gchar **) bits );
8bbe427f 314
e838cc14 315 wxCHECK_MSG( M_BMPDATA->m_pixmap, FALSE, wxT("couldn't create pixmap") );
8bbe427f 316
fd0eed64
RR
317 if (mask)
318 {
319 M_BMPDATA->m_mask = new wxMask();
320 M_BMPDATA->m_mask->m_bitmap = mask;
321 }
8bbe427f 322
fd0eed64 323 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
8bbe427f 324
103aab26 325 M_BMPDATA->m_bpp = visual->depth; // ?
c2fa61e8 326
e838cc14 327 return TRUE;
ff7b1510 328}
b5f01ae0
VS
329
330bool wxBitmap::CreateFromImage( const wxImage& image, int depth )
331{
a11672a4
RR
332 UnRef();
333
b5f01ae0
VS
334 wxCHECK_MSG( image.Ok(), FALSE, wxT("invalid image") )
335 wxCHECK_MSG( depth == -1 || depth == 1, FALSE, wxT("invalid bitmap depth") )
336
337 m_refData = new wxBitmapRefData();
c2fa61e8 338
b5f01ae0
VS
339 // ------
340 // convertion to mono bitmap:
341 // ------
342 if (depth == 1)
343 {
344 int width = image.GetWidth();
345 int height = image.GetHeight();
346
347 SetHeight( height );
348 SetWidth( width );
349
c2fa61e8 350 SetBitmap( gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 ) );
b5f01ae0
VS
351
352 SetDepth( 1 );
353
005f5d18 354 GdkVisual *visual = wxTheApp->GetGdkVisual();
b5f01ae0
VS
355
356 // Create picture image
357
358 unsigned char *data_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
359
360 GdkImage *data_image =
361 gdk_image_new_bitmap( visual, data_data, width, height );
362
363 // Create mask image
364
365 GdkImage *mask_image = (GdkImage*) NULL;
366
367 if (image.HasMask())
368 {
369 unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
370
371 mask_image = gdk_image_new_bitmap( visual, mask_data, width, height );
372
373 wxMask *mask = new wxMask();
c2fa61e8 374 mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 );
b5f01ae0
VS
375
376 SetMask( mask );
377 }
378
379 int r_mask = image.GetMaskRed();
380 int g_mask = image.GetMaskGreen();
381 int b_mask = image.GetMaskBlue();
382
383 unsigned char* data = image.GetData();
384
385 int index = 0;
386 for (int y = 0; y < height; y++)
387 {
388 for (int x = 0; x < width; x++)
389 {
390 int r = data[index];
391 index++;
392 int g = data[index];
393 index++;
394 int b = data[index];
395 index++;
396
397 if (image.HasMask())
398 {
399 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
400 gdk_image_put_pixel( mask_image, x, y, 1 );
401 else
402 gdk_image_put_pixel( mask_image, x, y, 0 );
403 }
404
405 if ((r == 255) && (b == 255) && (g == 255))
406 gdk_image_put_pixel( data_image, x, y, 1 );
407 else
408 gdk_image_put_pixel( data_image, x, y, 0 );
409
410 } // for
411 } // for
412
413 // Blit picture
414
415 GdkGC *data_gc = gdk_gc_new( GetBitmap() );
416
417 gdk_draw_image( GetBitmap(), data_gc, data_image, 0, 0, 0, 0, width, height );
418
419 gdk_image_destroy( data_image );
420 gdk_gc_unref( data_gc );
421
422 // Blit mask
423
424 if (image.HasMask())
425 {
426 GdkGC *mask_gc = gdk_gc_new( GetMask()->GetBitmap() );
427
428 gdk_draw_image( GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height );
429
430 gdk_image_destroy( mask_image );
431 gdk_gc_unref( mask_gc );
432 }
433 }
c2fa61e8 434
b5f01ae0
VS
435 // ------
436 // convertion to colour bitmap:
437 // ------
438 else
439 {
440 int width = image.GetWidth();
441 int height = image.GetHeight();
442
443 SetHeight( height );
444 SetWidth( width );
445
c2fa61e8 446 SetPixmap( gdk_pixmap_new( wxGetRootWindow()->window, width, height, -1 ) );
b5f01ae0 447
005f5d18 448 GdkVisual *visual = wxTheApp->GetGdkVisual();
b5f01ae0
VS
449
450 int bpp = visual->depth;
451
452 SetDepth( bpp );
453
454 if ((bpp == 16) && (visual->red_mask != 0xf800)) bpp = 15;
455 if (bpp < 8) bpp = 8;
456
8ab696e0
RR
457 // We handle 8-bit bitmaps ourselves using the colour cube, 12-bit
458 // visuals are not supported by GDK so we do these ourselves, too.
459 // 15-bit and 16-bit should actually work and 24-bit certainly does.
9b72a74d
RR
460#ifdef __sgi
461 if (!image.HasMask() && (bpp > 16))
462#else
8ab696e0 463 if (!image.HasMask() && (bpp > 12))
9b72a74d 464#endif
b5f01ae0
VS
465 {
466 static bool s_hasInitialized = FALSE;
467
468 if (!s_hasInitialized)
469 {
470 gdk_rgb_init();
471 s_hasInitialized = TRUE;
472 }
473
474 GdkGC *gc = gdk_gc_new( GetPixmap() );
475
476 gdk_draw_rgb_image( GetPixmap(),
477 gc,
478 0, 0,
479 width, height,
480 GDK_RGB_DITHER_NONE,
481 image.GetData(),
482 width*3 );
483
484 gdk_gc_unref( gc );
485 return TRUE;
486 }
487
b5f01ae0
VS
488 // Create picture image
489
490 GdkImage *data_image =
491 gdk_image_new( GDK_IMAGE_FASTEST, visual, width, height );
492
493 // Create mask image
494
495 GdkImage *mask_image = (GdkImage*) NULL;
496
497 if (image.HasMask())
498 {
499 unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
500
501 mask_image = gdk_image_new_bitmap( visual, mask_data, width, height );
502
503 wxMask *mask = new wxMask();
c2fa61e8 504 mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 );
b5f01ae0
VS
505
506 SetMask( mask );
507 }
508
509 // Render
510
511 enum byte_order { RGB, RBG, BRG, BGR, GRB, GBR };
512 byte_order b_o = RGB;
513
8ab696e0 514 if (bpp > 8)
b5f01ae0
VS
515 {
516 if ((visual->red_mask > visual->green_mask) && (visual->green_mask > visual->blue_mask)) b_o = RGB;
a11672a4 517 else if ((visual->red_mask > visual->blue_mask) && (visual->blue_mask > visual->green_mask)) b_o = RBG;
b5f01ae0
VS
518 else if ((visual->blue_mask > visual->red_mask) && (visual->red_mask > visual->green_mask)) b_o = BRG;
519 else if ((visual->blue_mask > visual->green_mask) && (visual->green_mask > visual->red_mask)) b_o = BGR;
520 else if ((visual->green_mask > visual->red_mask) && (visual->red_mask > visual->blue_mask)) b_o = GRB;
521 else if ((visual->green_mask > visual->blue_mask) && (visual->blue_mask > visual->red_mask)) b_o = GBR;
522 }
523
524 int r_mask = image.GetMaskRed();
525 int g_mask = image.GetMaskGreen();
526 int b_mask = image.GetMaskBlue();
527
528 unsigned char* data = image.GetData();
529
530 int index = 0;
531 for (int y = 0; y < height; y++)
532 {
533 for (int x = 0; x < width; x++)
534 {
535 int r = data[index];
536 index++;
537 int g = data[index];
538 index++;
539 int b = data[index];
540 index++;
541
542 if (image.HasMask())
543 {
544 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
545 gdk_image_put_pixel( mask_image, x, y, 1 );
546 else
547 gdk_image_put_pixel( mask_image, x, y, 0 );
548 }
549
550 switch (bpp)
551 {
8ab696e0 552 case 8:
b5f01ae0
VS
553 {
554 int pixel = -1;
555 if (wxTheApp->m_colorCube)
556 {
557 pixel = wxTheApp->m_colorCube[ ((r & 0xf8) << 7) + ((g & 0xf8) << 2) + ((b & 0xf8) >> 3) ];
558 }
559 else
560 {
561 GdkColormap *cmap = gtk_widget_get_default_colormap();
562 GdkColor *colors = cmap->colors;
563 int max = 3 * (65536);
8ab696e0 564
b5f01ae0
VS
565 for (int i = 0; i < cmap->size; i++)
566 {
567 int rdiff = (r << 8) - colors[i].red;
568 int gdiff = (g << 8) - colors[i].green;
569 int bdiff = (b << 8) - colors[i].blue;
570 int sum = ABS (rdiff) + ABS (gdiff) + ABS (bdiff);
571 if (sum < max) { pixel = i; max = sum; }
572 }
573 }
8ab696e0 574
b5f01ae0 575 gdk_image_put_pixel( data_image, x, y, pixel );
8ab696e0 576
b5f01ae0
VS
577 break;
578 }
8ab696e0 579 case 12: // SGI only
b5f01ae0 580 {
8ab696e0
RR
581 guint32 pixel = 0;
582 switch (b_o)
583 {
584 case RGB: pixel = ((r & 0xf0) << 4) | (g & 0xf0) | ((b & 0xf0) >> 4); break;
585 case RBG: pixel = ((r & 0xf0) << 4) | (b & 0xf0) | ((g & 0xf0) >> 4); break;
586 case GRB: pixel = ((g & 0xf0) << 4) | (r & 0xf0) | ((b & 0xf0) >> 4); break;
587 case GBR: pixel = ((g & 0xf0) << 4) | (b & 0xf0) | ((r & 0xf0) >> 4); break;
588 case BRG: pixel = ((b & 0xf0) << 4) | (r & 0xf0) | ((g & 0xf0) >> 4); break;
589 case BGR: pixel = ((b & 0xf0) << 4) | (g & 0xf0) | ((r & 0xf0) >> 4); break;
590 }
b5f01ae0
VS
591 gdk_image_put_pixel( data_image, x, y, pixel );
592 break;
593 }
8ab696e0 594 case 15:
b5f01ae0 595 {
8ab696e0
RR
596 guint32 pixel = 0;
597 switch (b_o)
598 {
599 case RGB: pixel = ((r & 0xf8) << 7) | ((g & 0xf8) << 2) | ((b & 0xf8) >> 3); break;
600 case RBG: pixel = ((r & 0xf8) << 7) | ((b & 0xf8) << 2) | ((g & 0xf8) >> 3); break;
601 case GRB: pixel = ((g & 0xf8) << 7) | ((r & 0xf8) << 2) | ((b & 0xf8) >> 3); break;
602 case GBR: pixel = ((g & 0xf8) << 7) | ((b & 0xf8) << 2) | ((r & 0xf8) >> 3); break;
603 case BRG: pixel = ((b & 0xf8) << 7) | ((r & 0xf8) << 2) | ((g & 0xf8) >> 3); break;
604 case BGR: pixel = ((b & 0xf8) << 7) | ((g & 0xf8) << 2) | ((r & 0xf8) >> 3); break;
605 }
b5f01ae0
VS
606 gdk_image_put_pixel( data_image, x, y, pixel );
607 break;
608 }
8ab696e0 609 case 16:
b5f01ae0 610 {
8ab696e0
RR
611 // I actually don't know if for 16-bit displays, it is alway the green
612 // component or the second component which has 6 bits.
b5f01ae0
VS
613 guint32 pixel = 0;
614 switch (b_o)
615 {
005f5d18
RR
616 case RGB: pixel = ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | ((b & 0xf8) >> 3); break;
617 case RBG: pixel = ((r & 0xf8) << 8) | ((b & 0xfc) << 3) | ((g & 0xf8) >> 3); break;
618 case GRB: pixel = ((g & 0xf8) << 8) | ((r & 0xfc) << 3) | ((b & 0xf8) >> 3); break;
619 case GBR: pixel = ((g & 0xf8) << 8) | ((b & 0xfc) << 3) | ((r & 0xf8) >> 3); break;
620 case BRG: pixel = ((b & 0xf8) << 8) | ((r & 0xfc) << 3) | ((g & 0xf8) >> 3); break;
621 case BGR: pixel = ((b & 0xf8) << 8) | ((g & 0xfc) << 3) | ((r & 0xf8) >> 3); break;
8ab696e0
RR
622 }
623 gdk_image_put_pixel( data_image, x, y, pixel );
624 break;
625 }
626 case 32:
627 case 24:
628 {
629 guint32 pixel = 0;
630 switch (b_o)
631 {
632 case RGB: pixel = (r << 16) | (g << 8) | b; break;
633 case RBG: pixel = (r << 16) | (b << 8) | g; break;
634 case BRG: pixel = (b << 16) | (r << 8) | g; break;
635 case BGR: pixel = (b << 16) | (g << 8) | r; break;
636 case GRB: pixel = (g << 16) | (r << 8) | b; break;
637 case GBR: pixel = (g << 16) | (b << 8) | r; break;
b5f01ae0
VS
638 }
639 gdk_image_put_pixel( data_image, x, y, pixel );
640 }
8ab696e0 641 default: break;
b5f01ae0
VS
642 }
643 } // for
644 } // for
645
646 // Blit picture
647
648 GdkGC *data_gc = gdk_gc_new( GetPixmap() );
649
650 gdk_draw_image( GetPixmap(), data_gc, data_image, 0, 0, 0, 0, width, height );
651
652 gdk_image_destroy( data_image );
653 gdk_gc_unref( data_gc );
654
655 // Blit mask
656
657 if (image.HasMask())
658 {
659 GdkGC *mask_gc = gdk_gc_new( GetMask()->GetBitmap() );
660
661 gdk_draw_image( GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height );
662
663 gdk_image_destroy( mask_image );
664 gdk_gc_unref( mask_gc );
665 }
666 }
667
668 return TRUE;
669}
670
671wxImage wxBitmap::ConvertToImage() const
672{
673 wxImage image;
c2fa61e8 674
b5f01ae0
VS
675 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
676
677 GdkImage *gdk_image = (GdkImage*) NULL;
678 if (GetPixmap())
679 {
680 gdk_image = gdk_image_get( GetPixmap(),
681 0, 0,
682 GetWidth(), GetHeight() );
683 } else
684 if (GetBitmap())
685 {
686 gdk_image = gdk_image_get( GetBitmap(),
687 0, 0,
688 GetWidth(), GetHeight() );
689 } else
690 {
691 wxFAIL_MSG( wxT("Ill-formed bitmap") );
692 }
693
694 wxCHECK_MSG( gdk_image, wxNullImage, wxT("couldn't create image") );
c2fa61e8 695
b5f01ae0
VS
696 image.Create( GetWidth(), GetHeight() );
697 char unsigned *data = image.GetData();
698
699 if (!data)
700 {
701 gdk_image_destroy( gdk_image );
702 wxFAIL_MSG( wxT("couldn't create image") );
703 return wxNullImage;
704 }
705
706 GdkImage *gdk_image_mask = (GdkImage*) NULL;
707 if (GetMask())
708 {
709 gdk_image_mask = gdk_image_get( GetMask()->GetBitmap(),
710 0, 0,
711 GetWidth(), GetHeight() );
712
713 image.SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
714 }
715
716 int bpp = -1;
717 int red_shift_right = 0;
718 int green_shift_right = 0;
719 int blue_shift_right = 0;
720 int red_shift_left = 0;
721 int green_shift_left = 0;
722 int blue_shift_left = 0;
723 bool use_shift = FALSE;
724
725 if (GetPixmap())
726 {
727 GdkVisual *visual = gdk_window_get_visual( GetPixmap() );
005f5d18
RR
728 if (visual == NULL)
729 visual = wxTheApp->GetGdkVisual();
730
b5f01ae0
VS
731 bpp = visual->depth;
732 if (bpp == 16) bpp = visual->red_prec + visual->green_prec + visual->blue_prec;
733 red_shift_right = visual->red_shift;
734 red_shift_left = 8-visual->red_prec;
735 green_shift_right = visual->green_shift;
736 green_shift_left = 8-visual->green_prec;
737 blue_shift_right = visual->blue_shift;
738 blue_shift_left = 8-visual->blue_prec;
739
740 use_shift = (visual->type == GDK_VISUAL_TRUE_COLOR) || (visual->type == GDK_VISUAL_DIRECT_COLOR);
741 }
742 if (GetBitmap())
743 {
744 bpp = 1;
745 }
746
747
748 GdkColormap *cmap = gtk_widget_get_default_colormap();
749
750 long pos = 0;
751 for (int j = 0; j < GetHeight(); j++)
752 {
753 for (int i = 0; i < GetWidth(); i++)
754 {
755 wxUint32 pixel = gdk_image_get_pixel( gdk_image, i, j );
8ab696e0
RR
756 if (bpp == 1)
757 {
758 if (pixel == 0)
b5f01ae0 759 {
b5f01ae0
VS
760 data[pos] = 0;
761 data[pos+1] = 0;
762 data[pos+2] = 0;
8ab696e0
RR
763 }
764 else
765 {
b5f01ae0
VS
766 data[pos] = 255;
767 data[pos+1] = 255;
768 data[pos+2] = 255;
8ab696e0 769 }
b5f01ae0
VS
770 }
771 else if (use_shift)
772 {
773 data[pos] = (pixel >> red_shift_right) << red_shift_left;
774 data[pos+1] = (pixel >> green_shift_right) << green_shift_left;
775 data[pos+2] = (pixel >> blue_shift_right) << blue_shift_left;
8ab696e0 776 }
b5f01ae0
VS
777 else if (cmap->colors)
778 {
779 data[pos] = cmap->colors[pixel].red >> 8;
780 data[pos+1] = cmap->colors[pixel].green >> 8;
781 data[pos+2] = cmap->colors[pixel].blue >> 8;
782 }
783 else
784 {
785 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
786 }
787
788 if (gdk_image_mask)
789 {
790 int mask_pixel = gdk_image_get_pixel( gdk_image_mask, i, j );
791 if (mask_pixel == 0)
792 {
793 data[pos] = 16;
794 data[pos+1] = 16;
795 data[pos+2] = 16;
796 }
797 }
798
799 pos += 3;
800 }
801 }
802
803 gdk_image_destroy( gdk_image );
c2fa61e8 804 if (gdk_image_mask) gdk_image_destroy( gdk_image_mask );
b5f01ae0
VS
805
806 return image;
807}
808
c801d85f
KB
809wxBitmap::wxBitmap( const wxBitmap& bmp )
810{
fd0eed64 811 Ref( bmp );
ff7b1510 812}
6f65e337 813
debe6624 814wxBitmap::wxBitmap( const wxString &filename, int type )
c801d85f 815{
fd0eed64 816 LoadFile( filename, type );
ff7b1510 817}
c801d85f 818
debe6624 819wxBitmap::wxBitmap( const char bits[], int width, int height, int WXUNUSED(depth))
6f65e337 820{
fd0eed64 821 m_refData = new wxBitmapRefData();
6f65e337 822
fd0eed64 823 M_BMPDATA->m_mask = (wxMask *) NULL;
8bbe427f 824 M_BMPDATA->m_bitmap =
c2fa61e8 825 gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) bits, width, height );
fd0eed64
RR
826 M_BMPDATA->m_width = width;
827 M_BMPDATA->m_height = height;
828 M_BMPDATA->m_bpp = 1;
6f65e337 829
223d09f6 830 wxCHECK_RET( M_BMPDATA->m_bitmap, wxT("couldn't create bitmap") );
6f65e337 831}
8bbe427f
VZ
832
833wxBitmap::~wxBitmap()
c801d85f 834{
ff7b1510 835}
8bbe427f 836
c801d85f
KB
837wxBitmap& wxBitmap::operator = ( const wxBitmap& bmp )
838{
7ecb8b06
VZ
839 if ( m_refData != bmp.m_refData )
840 Ref( bmp );
841
8bbe427f 842 return *this;
ff7b1510 843}
8bbe427f 844
f6bcfd97 845bool wxBitmap::operator == ( const wxBitmap& bmp ) const
c801d85f 846{
8bbe427f 847 return m_refData == bmp.m_refData;
ff7b1510 848}
8bbe427f 849
f6bcfd97 850bool wxBitmap::operator != ( const wxBitmap& bmp ) const
c801d85f 851{
8bbe427f 852 return m_refData != bmp.m_refData;
ff7b1510 853}
8bbe427f 854
91b8de8d 855bool wxBitmap::Ok() const
c801d85f 856{
fd0eed64 857 return (m_refData != NULL);
ff7b1510 858}
8bbe427f 859
91b8de8d 860int wxBitmap::GetHeight() const
c801d85f 861{
223d09f6 862 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
e55ad60e 863
fd0eed64 864 return M_BMPDATA->m_height;
ff7b1510 865}
c801d85f 866
91b8de8d 867int wxBitmap::GetWidth() const
c801d85f 868{
223d09f6 869 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
8bbe427f 870
fd0eed64 871 return M_BMPDATA->m_width;
ff7b1510 872}
c801d85f 873
91b8de8d 874int wxBitmap::GetDepth() const
c801d85f 875{
223d09f6 876 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
8bbe427f 877
fd0eed64 878 return M_BMPDATA->m_bpp;
ff7b1510 879}
c801d85f 880
91b8de8d 881wxMask *wxBitmap::GetMask() const
c801d85f 882{
223d09f6 883 wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") );
8bbe427f 884
fd0eed64 885 return M_BMPDATA->m_mask;
ff7b1510 886}
c801d85f
KB
887
888void wxBitmap::SetMask( wxMask *mask )
889{
223d09f6 890 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
8bbe427f 891
fd0eed64 892 if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask;
8bbe427f 893
fd0eed64 894 M_BMPDATA->m_mask = mask;
ff7b1510 895}
c801d85f 896
db0aec83
VS
897bool wxBitmap::CopyFromIcon(const wxIcon& icon)
898{
899 *this = icon;
900 return TRUE;
901}
902
17bec151
RR
903wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const
904{
905 wxCHECK_MSG( Ok() &&
13111b2a
VZ
906 (rect.x >= 0) && (rect.y >= 0) &&
907 (rect.x+rect.width <= M_BMPDATA->m_width) && (rect.y+rect.height <= M_BMPDATA->m_height),
17bec151 908 wxNullBitmap, wxT("invalid bitmap or bitmap region") );
13111b2a 909
17bec151
RR
910 wxBitmap ret( rect.width, rect.height, M_BMPDATA->m_bpp );
911 wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") );
13111b2a 912
17bec151
RR
913 if (ret.GetPixmap())
914 {
915 GdkGC *gc = gdk_gc_new( ret.GetPixmap() );
13111b2a
VZ
916 gdk_draw_pixmap( ret.GetPixmap(), gc, GetPixmap(), rect.x, rect.y, 0, 0, rect.width, rect.height );
917 gdk_gc_destroy( gc );
17bec151
RR
918 }
919 else
920 {
921 GdkGC *gc = gdk_gc_new( ret.GetBitmap() );
f6bcfd97 922 gdk_wx_draw_bitmap( ret.GetBitmap(), gc, GetBitmap(), rect.x, rect.y, 0, 0, rect.width, rect.height );
13111b2a 923 gdk_gc_destroy( gc );
17bec151 924 }
13111b2a 925
17bec151
RR
926 if (GetMask())
927 {
928 wxMask *mask = new wxMask;
c2fa61e8 929 mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, rect.width, rect.height, 1 );
13111b2a 930
17bec151 931 GdkGC *gc = gdk_gc_new( mask->m_bitmap );
f6bcfd97 932 gdk_wx_draw_bitmap( mask->m_bitmap, gc, M_BMPDATA->m_mask->m_bitmap, 0, 0, rect.x, rect.y, rect.width, rect.height );
13111b2a
VZ
933 gdk_gc_destroy( gc );
934
935 ret.SetMask( mask );
17bec151 936 }
13111b2a 937
17bec151
RR
938 return ret;
939}
940
fd0eed64 941bool wxBitmap::SaveFile( const wxString &name, int type, wxPalette *WXUNUSED(palette) )
c801d85f 942{
223d09f6 943 wxCHECK_MSG( Ok(), FALSE, wxT("invalid bitmap") );
8bbe427f 944
b75dd496 945 // Try to save the bitmap via wxImage handlers:
fd0eed64 946 {
4bc67cc5 947 wxImage image( *this );
284b4c88 948 if (image.Ok()) return image.SaveFile( name, type );
fd0eed64 949 }
8bbe427f 950
fd0eed64 951 return FALSE;
ff7b1510 952}
c801d85f 953
fd0eed64 954bool wxBitmap::LoadFile( const wxString &name, int type )
c801d85f 955{
fd0eed64 956 UnRef();
8bbe427f 957
fd0eed64 958 if (!wxFileExists(name)) return FALSE;
8bbe427f 959
005f5d18 960 GdkVisual *visual = wxTheApp->GetGdkVisual();
c2fa61e8 961
fd0eed64
RR
962 if (type == wxBITMAP_TYPE_XPM)
963 {
964 m_refData = new wxBitmapRefData();
8bbe427f 965
fd0eed64 966 GdkBitmap *mask = (GdkBitmap*) NULL;
8bbe427f 967
c2fa61e8 968 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm( wxGetRootWindow()->window, &mask, NULL, name.fn_str() );
8bbe427f 969
fd0eed64
RR
970 if (mask)
971 {
972 M_BMPDATA->m_mask = new wxMask();
973 M_BMPDATA->m_mask->m_bitmap = mask;
974 }
8bbe427f 975
fd0eed64 976 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
c2fa61e8 977
103aab26 978 M_BMPDATA->m_bpp = visual->depth;
fd0eed64 979 }
b75dd496 980 else // try if wxImage can load it
fd0eed64
RR
981 {
982 wxImage image;
b75dd496 983 if (!image.LoadFile( name, type )) return FALSE;
4bc67cc5 984 if (image.Ok()) *this = image.ConvertToBitmap();
b75dd496 985 else return FALSE;
fd0eed64 986 }
8bbe427f 987
fd0eed64 988 return TRUE;
ff7b1510 989}
8bbe427f 990
91b8de8d 991wxPalette *wxBitmap::GetPalette() const
c801d85f 992{
fd0eed64 993 if (!Ok()) return (wxPalette *) NULL;
8bbe427f 994
fd0eed64 995 return M_BMPDATA->m_palette;
ff7b1510 996}
c801d85f 997
4bc67cc5
RR
998void wxBitmap::SetHeight( int height )
999{
1000 if (!m_refData) m_refData = new wxBitmapRefData();
1001
1002 M_BMPDATA->m_height = height;
1003}
1004
1005void wxBitmap::SetWidth( int width )
1006{
1007 if (!m_refData) m_refData = new wxBitmapRefData();
1008
1009 M_BMPDATA->m_width = width;
1010}
1011
1012void wxBitmap::SetDepth( int depth )
1013{
1014 if (!m_refData) m_refData = new wxBitmapRefData();
1015
1016 M_BMPDATA->m_bpp = depth;
1017}
1018
1019void wxBitmap::SetPixmap( GdkPixmap *pixmap )
1020{
1021 if (!m_refData) m_refData = new wxBitmapRefData();
1022
1023 M_BMPDATA->m_pixmap = pixmap;
1024}
1025
82ea63e6
RR
1026void wxBitmap::SetBitmap( GdkPixmap *bitmap )
1027{
1028 if (!m_refData) m_refData = new wxBitmapRefData();
1029
1030 M_BMPDATA->m_bitmap = bitmap;
1031}
1032
91b8de8d 1033GdkPixmap *wxBitmap::GetPixmap() const
c801d85f 1034{
223d09f6 1035 wxCHECK_MSG( Ok(), (GdkPixmap *) NULL, wxT("invalid bitmap") );
8bbe427f 1036
fd0eed64 1037 return M_BMPDATA->m_pixmap;
ff7b1510 1038}
8bbe427f 1039
91b8de8d 1040GdkBitmap *wxBitmap::GetBitmap() const
6f65e337 1041{
223d09f6 1042 wxCHECK_MSG( Ok(), (GdkBitmap *) NULL, wxT("invalid bitmap") );
8bbe427f 1043
fd0eed64 1044 return M_BMPDATA->m_bitmap;
ff7b1510 1045}