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