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