]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/bitmap.cpp
added test for writing empty value to the config
[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
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
17bec151
RR
868wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const
869{
870 wxCHECK_MSG( Ok() &&
13111b2a
VZ
871 (rect.x >= 0) && (rect.y >= 0) &&
872 (rect.x+rect.width <= M_BMPDATA->m_width) && (rect.y+rect.height <= M_BMPDATA->m_height),
17bec151 873 wxNullBitmap, wxT("invalid bitmap or bitmap region") );
13111b2a 874
17bec151
RR
875 wxBitmap ret( rect.width, rect.height, M_BMPDATA->m_bpp );
876 wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") );
13111b2a 877
17bec151
RR
878 if (ret.GetPixmap())
879 {
880 GdkGC *gc = gdk_gc_new( ret.GetPixmap() );
13111b2a
VZ
881 gdk_draw_pixmap( ret.GetPixmap(), gc, GetPixmap(), rect.x, rect.y, 0, 0, rect.width, rect.height );
882 gdk_gc_destroy( gc );
17bec151
RR
883 }
884 else
885 {
886 GdkGC *gc = gdk_gc_new( ret.GetBitmap() );
f6bcfd97 887 gdk_wx_draw_bitmap( ret.GetBitmap(), gc, GetBitmap(), rect.x, rect.y, 0, 0, rect.width, rect.height );
13111b2a 888 gdk_gc_destroy( gc );
17bec151 889 }
13111b2a 890
17bec151
RR
891 if (GetMask())
892 {
893 wxMask *mask = new wxMask;
c2fa61e8 894 mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, rect.width, rect.height, 1 );
13111b2a 895
17bec151 896 GdkGC *gc = gdk_gc_new( mask->m_bitmap );
f6bcfd97 897 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
898 gdk_gc_destroy( gc );
899
900 ret.SetMask( mask );
17bec151 901 }
13111b2a 902
17bec151
RR
903 return ret;
904}
905
fd0eed64 906bool wxBitmap::SaveFile( const wxString &name, int type, wxPalette *WXUNUSED(palette) )
c801d85f 907{
223d09f6 908 wxCHECK_MSG( Ok(), FALSE, wxT("invalid bitmap") );
8bbe427f 909
b75dd496 910 // Try to save the bitmap via wxImage handlers:
fd0eed64 911 {
4bc67cc5 912 wxImage image( *this );
284b4c88 913 if (image.Ok()) return image.SaveFile( name, type );
fd0eed64 914 }
8bbe427f 915
fd0eed64 916 return FALSE;
ff7b1510 917}
c801d85f 918
fd0eed64 919bool wxBitmap::LoadFile( const wxString &name, int type )
c801d85f 920{
fd0eed64 921 UnRef();
8bbe427f 922
fd0eed64 923 if (!wxFileExists(name)) return FALSE;
8bbe427f 924
c2fa61e8 925 GdkVisual *visual = gdk_window_get_visual( wxGetRootWindow()->window );
103aab26 926 wxASSERT( visual );
c2fa61e8 927
fd0eed64
RR
928 if (type == wxBITMAP_TYPE_XPM)
929 {
930 m_refData = new wxBitmapRefData();
8bbe427f 931
fd0eed64 932 GdkBitmap *mask = (GdkBitmap*) NULL;
8bbe427f 933
c2fa61e8 934 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm( wxGetRootWindow()->window, &mask, NULL, name.fn_str() );
8bbe427f 935
fd0eed64
RR
936 if (mask)
937 {
938 M_BMPDATA->m_mask = new wxMask();
939 M_BMPDATA->m_mask->m_bitmap = mask;
940 }
8bbe427f 941
fd0eed64 942 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
c2fa61e8 943
103aab26 944 M_BMPDATA->m_bpp = visual->depth;
fd0eed64 945 }
b75dd496 946 else // try if wxImage can load it
fd0eed64
RR
947 {
948 wxImage image;
b75dd496 949 if (!image.LoadFile( name, type )) return FALSE;
4bc67cc5 950 if (image.Ok()) *this = image.ConvertToBitmap();
b75dd496 951 else return FALSE;
fd0eed64 952 }
8bbe427f 953
fd0eed64 954 return TRUE;
ff7b1510 955}
8bbe427f 956
91b8de8d 957wxPalette *wxBitmap::GetPalette() const
c801d85f 958{
fd0eed64 959 if (!Ok()) return (wxPalette *) NULL;
8bbe427f 960
fd0eed64 961 return M_BMPDATA->m_palette;
ff7b1510 962}
c801d85f 963
4bc67cc5
RR
964void wxBitmap::SetHeight( int height )
965{
966 if (!m_refData) m_refData = new wxBitmapRefData();
967
968 M_BMPDATA->m_height = height;
969}
970
971void wxBitmap::SetWidth( int width )
972{
973 if (!m_refData) m_refData = new wxBitmapRefData();
974
975 M_BMPDATA->m_width = width;
976}
977
978void wxBitmap::SetDepth( int depth )
979{
980 if (!m_refData) m_refData = new wxBitmapRefData();
981
982 M_BMPDATA->m_bpp = depth;
983}
984
985void wxBitmap::SetPixmap( GdkPixmap *pixmap )
986{
987 if (!m_refData) m_refData = new wxBitmapRefData();
988
989 M_BMPDATA->m_pixmap = pixmap;
990}
991
82ea63e6
RR
992void wxBitmap::SetBitmap( GdkPixmap *bitmap )
993{
994 if (!m_refData) m_refData = new wxBitmapRefData();
995
996 M_BMPDATA->m_bitmap = bitmap;
997}
998
91b8de8d 999GdkPixmap *wxBitmap::GetPixmap() const
c801d85f 1000{
223d09f6 1001 wxCHECK_MSG( Ok(), (GdkPixmap *) NULL, wxT("invalid bitmap") );
8bbe427f 1002
fd0eed64 1003 return M_BMPDATA->m_pixmap;
ff7b1510 1004}
8bbe427f 1005
91b8de8d 1006GdkBitmap *wxBitmap::GetBitmap() const
6f65e337 1007{
223d09f6 1008 wxCHECK_MSG( Ok(), (GdkBitmap *) NULL, wxT("invalid bitmap") );
8bbe427f 1009
fd0eed64 1010 return M_BMPDATA->m_bitmap;
ff7b1510 1011}