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