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