]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/bitmap.cpp
ignore requests to install idle handler when app instance is not valid
[wxWidgets.git] / src / gtk1 / bitmap.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
3cbab641 2// Name: src/gtk1/bitmap.cpp
c801d85f
KB
3// Purpose:
4// Author: Robert Roebling
6f65e337 5// RCS-ID: $Id$
01111366 6// Copyright: (c) 1998 Robert Roebling
65571936 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
14f355c2
VS
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
c801d85f 13#include "wx/bitmap.h"
670f9935
WS
14
15#ifndef WX_PRECOMP
16 #include "wx/app.h"
f38924e8 17 #include "wx/dcmemory.h"
559a723c 18 #include "wx/palette.h"
923d28da 19 #include "wx/icon.h"
18680f86 20 #include "wx/math.h"
155ecd4c 21 #include "wx/image.h"
670f9935
WS
22#endif // WX_PRECOMP
23
fd0eed64 24#include "wx/filefn.h"
83624f79 25
20e05ffb 26#include <gdk/gdk.h>
d76fe38b 27#include <gtk/gtk.h>
b5f01ae0
VS
28#include <gdk/gdkx.h>
29
3cbab641 30#include <gdk/gdkrgb.h>
13111b2a 31
3cbab641
MR
32extern
33void gdk_wx_draw_bitmap (GdkDrawable *drawable,
34 GdkGC *gc,
35 GdkDrawable *src,
36 gint xsrc,
37 gint ysrc,
38 gint xdest,
39 gint ydest,
40 gint width,
41 gint height);
f6bcfd97 42
d76fe38b
RR
43//-----------------------------------------------------------------------------
44// data
45//-----------------------------------------------------------------------------
46
c2fa61e8 47extern GtkWidget *wxGetRootWindow();
c801d85f
KB
48
49//-----------------------------------------------------------------------------
50// wxMask
51//-----------------------------------------------------------------------------
52
53IMPLEMENT_DYNAMIC_CLASS(wxMask,wxObject)
54
8bbe427f 55wxMask::wxMask()
c801d85f 56{
fd0eed64 57 m_bitmap = (GdkBitmap *) NULL;
ff7b1510 58}
c801d85f 59
91b8de8d 60wxMask::wxMask( const wxBitmap& bitmap, const wxColour& colour )
c801d85f 61{
72a7edf0 62 m_bitmap = (GdkBitmap *) NULL;
91b8de8d 63 Create( bitmap, colour );
ff7b1510 64}
c801d85f 65
0b04c4e0 66#if wxUSE_PALETTE
91b8de8d 67wxMask::wxMask( const wxBitmap& bitmap, int paletteIndex )
c801d85f 68{
72a7edf0 69 m_bitmap = (GdkBitmap *) NULL;
91b8de8d 70 Create( bitmap, paletteIndex );
ff7b1510 71}
0b04c4e0 72#endif // wxUSE_PALETTE
c801d85f 73
91b8de8d 74wxMask::wxMask( const wxBitmap& bitmap )
c801d85f 75{
72a7edf0 76 m_bitmap = (GdkBitmap *) NULL;
91b8de8d 77 Create( bitmap );
ff7b1510 78}
c801d85f 79
8bbe427f 80wxMask::~wxMask()
c801d85f 81{
13111b2a 82 if (m_bitmap)
72a7edf0 83 gdk_bitmap_unref( m_bitmap );
ff7b1510 84}
c801d85f 85
1fb4de31
RR
86bool wxMask::Create( const wxBitmap& bitmap,
87 const wxColour& colour )
91b8de8d
RR
88{
89 if (m_bitmap)
284b4c88 90 {
91b8de8d 91 gdk_bitmap_unref( m_bitmap );
284b4c88 92 m_bitmap = (GdkBitmap*) NULL;
91b8de8d 93 }
13111b2a 94
368d59f0 95 wxImage image = bitmap.ConvertToImage();
902725ee 96 if (!image.Ok()) return false;
13111b2a 97
c2fa61e8 98 m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, image.GetWidth(), image.GetHeight(), 1 );
f9ee644e 99 GdkGC *gc = gdk_gc_new( m_bitmap );
13111b2a 100
f9ee644e
RR
101 GdkColor color;
102 color.red = 65000;
103 color.green = 65000;
104 color.blue = 65000;
105 color.pixel = 1;
106 gdk_gc_set_foreground( gc, &color );
107 gdk_gc_set_fill( gc, GDK_SOLID );
108 gdk_draw_rectangle( m_bitmap, gc, TRUE, 0, 0, image.GetWidth(), image.GetHeight() );
13111b2a 109
1fb4de31
RR
110 unsigned char *data = image.GetData();
111 int index = 0;
13111b2a 112
1fb4de31
RR
113 unsigned char red = colour.Red();
114 unsigned char green = colour.Green();
115 unsigned char blue = colour.Blue();
13111b2a 116
005f5d18 117 GdkVisual *visual = wxTheApp->GetGdkVisual();
c2fa61e8 118
1fb4de31 119 int bpp = visual->depth;
2eefae6e
VZ
120 if ((bpp == 16) && (visual->red_mask != 0xf800))
121 bpp = 15;
1fb4de31
RR
122 if (bpp == 15)
123 {
124 red = red & 0xf8;
1fb4de31 125 green = green & 0xf8;
f6bcfd97 126 blue = blue & 0xf8;
2eefae6e
VZ
127 }
128 else if (bpp == 16)
1fb4de31
RR
129 {
130 red = red & 0xf8;
f6bcfd97
BP
131 green = green & 0xfc;
132 blue = blue & 0xf8;
2eefae6e
VZ
133 }
134 else if (bpp == 12)
005f5d18
RR
135 {
136 red = red & 0xf0;
137 green = green & 0xf0;
138 blue = blue & 0xf0;
1fb4de31 139 }
13111b2a 140
f9ee644e
RR
141 color.red = 0;
142 color.green = 0;
143 color.blue = 0;
144 color.pixel = 0;
145 gdk_gc_set_foreground( gc, &color );
13111b2a 146
1fb4de31 147 for (int j = 0; j < image.GetHeight(); j++)
f9ee644e 148 {
f2593d0d
RR
149 int start_x = -1;
150 int i;
151 for (i = 0; i < image.GetWidth(); i++)
1fb4de31 152 {
13111b2a
VZ
153 if ((data[index] == red) &&
154 (data[index+1] == green) &&
155 (data[index+2] == blue))
156 {
157 if (start_x == -1)
158 start_x = i;
159 }
160 else
161 {
162 if (start_x != -1)
163 {
164 gdk_draw_line( m_bitmap, gc, start_x, j, i-1, j );
165 start_x = -1;
166 }
f9ee644e
RR
167 }
168 index += 3;
169 }
170 if (start_x != -1)
171 gdk_draw_line( m_bitmap, gc, start_x, j, i, j );
172 }
1fb4de31 173
f9ee644e 174 gdk_gc_unref( gc );
1fb4de31 175
902725ee 176 return true;
91b8de8d
RR
177}
178
0b04c4e0 179#if wxUSE_PALETTE
b5f01ae0 180bool wxMask::Create( const wxBitmap& bitmap, int paletteIndex )
91b8de8d 181{
b5f01ae0
VS
182 unsigned char r,g,b;
183 wxPalette *pal = bitmap.GetPalette();
284b4c88 184
902725ee 185 wxCHECK_MSG( pal, false, wxT("Cannot create mask from bitmap without palette") );
c2fa61e8 186
b5f01ae0 187 pal->GetRGB(paletteIndex, &r, &g, &b);
284b4c88 188
b5f01ae0 189 return Create(bitmap, wxColour(r, g, b));
91b8de8d 190}
0b04c4e0 191#endif // wxUSE_PALETTE
91b8de8d
RR
192
193bool wxMask::Create( const wxBitmap& bitmap )
194{
195 if (m_bitmap)
284b4c88 196 {
91b8de8d 197 gdk_bitmap_unref( m_bitmap );
284b4c88 198 m_bitmap = (GdkBitmap*) NULL;
91b8de8d 199 }
284b4c88 200
902725ee 201 if (!bitmap.Ok()) return false;
284b4c88 202
902725ee 203 wxCHECK_MSG( bitmap.GetBitmap(), false, wxT("Cannot create mask from colour bitmap") );
284b4c88 204
c2fa61e8 205 m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, bitmap.GetWidth(), bitmap.GetHeight(), 1 );
284b4c88 206
902725ee 207 if (!m_bitmap) return false;
284b4c88 208
91b8de8d 209 GdkGC *gc = gdk_gc_new( m_bitmap );
284b4c88 210
f6bcfd97 211 gdk_wx_draw_bitmap( m_bitmap, gc, bitmap.GetBitmap(), 0, 0, 0, 0, bitmap.GetWidth(), bitmap.GetHeight() );
284b4c88 212
91b8de8d 213 gdk_gc_unref( gc );
284b4c88 214
902725ee 215 return true;
91b8de8d
RR
216}
217
218GdkBitmap *wxMask::GetBitmap() const
c801d85f 219{
fd0eed64 220 return m_bitmap;
ff7b1510 221}
8bbe427f 222
c801d85f
KB
223//-----------------------------------------------------------------------------
224// wxBitmap
225//-----------------------------------------------------------------------------
226
227class wxBitmapRefData: public wxObjectRefData
228{
fd0eed64 229public:
f2593d0d 230 wxBitmapRefData();
39b0ab4a
VZ
231 wxBitmapRefData(const wxBitmapRefData& data);
232 bool Create(int width, int height, int bpp);
d3c7fc99 233 virtual ~wxBitmapRefData();
f2593d0d
RR
234
235 GdkPixmap *m_pixmap;
236 GdkBitmap *m_bitmap;
237 wxMask *m_mask;
238 int m_width;
239 int m_height;
240 int m_bpp;
39b0ab4a 241#if wxUSE_PALETTE
f2593d0d 242 wxPalette *m_palette;
39b0ab4a 243#endif // wxUSE_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;
39b0ab4a 254#if wxUSE_PALETTE
fd0eed64 255 m_palette = (wxPalette *) NULL;
39b0ab4a
VZ
256#endif // wxUSE_PALETTE
257}
258
259wxBitmapRefData::wxBitmapRefData(const wxBitmapRefData& data)
260{
261 Create(data.m_width, data.m_height, data.m_bpp);
262
263 m_mask = data.m_mask ? new wxMask(*data.m_mask) : NULL;
264
265#if wxUSE_PALETTE
266 wxASSERT_MSG( !data.m_palette,
267 _T("copying bitmaps palette not implemented") );
268#endif // wxUSE_PALETTE
269
270
271 // copy the bitmap data by simply drawing the source bitmap on this one
272 GdkPixmap **dst;
273 if ( data.m_pixmap )
274 {
275 dst = &m_pixmap;
276 }
277 else if ( data.m_bitmap )
278 {
279 dst = &m_bitmap;
280 }
281 else // invalid bitmap?
282 {
283 return;
284 }
285
286 GdkGC *gc = gdk_gc_new(*dst);
287 if ( m_bpp == 1 )
288 {
289 gdk_wx_draw_bitmap(m_bitmap, gc, data.m_bitmap, 0, 0, 0, 0, -1, -1);
290 }
291 else // colour pixmap
292 {
293 gdk_draw_pixmap(m_pixmap, gc, data.m_pixmap, 0, 0, 0, 0, -1, -1);
294 }
295
296 gdk_gc_unref(gc);
297}
298
299bool wxBitmapRefData::Create(int width, int height, int bpp)
300{
301 m_width = width;
302 m_height = height;
303 m_bpp = bpp;
304
305 m_mask = NULL;
306#if wxUSE_PALETTE
307 m_palette = NULL;
308#endif
309
310 // to understand how this compiles you should know that GdkPixmap and
311 // GdkBitmap are one and the same type in GTK+ 1
312 GdkPixmap **ppix;
313 if ( m_bpp != 1 )
314 {
315 const GdkVisual * const visual = wxTheApp->GetGdkVisual();
316
317 wxCHECK_MSG( (bpp == -1) || (bpp == visual->depth) || (bpp == 32), false,
318 wxT("invalid bitmap depth") );
319
320 m_bpp = visual->depth;
321
322 ppix = &m_pixmap;
323 m_bitmap = NULL;
324 }
325 else // mono bitmap
326 {
327 ppix = &m_bitmap;
328 m_pixmap = NULL;
329 }
330
331 *ppix = gdk_pixmap_new( wxGetRootWindow()->window, width, height, m_bpp );
332
333 return *ppix != NULL;
ff7b1510 334}
c801d85f 335
8bbe427f 336wxBitmapRefData::~wxBitmapRefData()
c801d85f 337{
3ebcd89d
VZ
338 if (m_pixmap)
339 gdk_pixmap_unref( m_pixmap );
340 if (m_bitmap)
341 gdk_bitmap_unref( m_bitmap );
342 delete m_mask;
0b04c4e0 343#if wxUSE_PALETTE
3ebcd89d 344 delete m_palette;
0b04c4e0 345#endif // wxUSE_PALETTE
ff7b1510 346}
c801d85f
KB
347
348//-----------------------------------------------------------------------------
349
350#define M_BMPDATA ((wxBitmapRefData *)m_refData)
351
352IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxGDIObject)
353
8bbe427f 354wxBitmap::wxBitmap()
c801d85f 355{
ff7b1510 356}
8bbe427f 357
debe6624 358wxBitmap::wxBitmap( int width, int height, int depth )
c801d85f 359{
c826213d 360 Create( width, height, depth );
c826213d
RR
361}
362
39b0ab4a
VZ
363wxObjectRefData *wxBitmap::CreateRefData() const
364{
365 return new wxBitmapRefData;
366}
367
368wxObjectRefData *wxBitmap::CloneRefData(const wxObjectRefData *data) const
369{
370 return new wxBitmapRefData(*wx_static_cast(const wxBitmapRefData *, data));
371}
372
c826213d
RR
373bool wxBitmap::Create( int width, int height, int depth )
374{
375 UnRef();
376
3ebcd89d
VZ
377 if ( width <= 0 || height <= 0 )
378 {
379 return false;
380 }
284b4c88 381
eefa26be 382 m_refData = new wxBitmapRefData();
39b0ab4a 383 return M_BMPDATA->Create(width, height, depth);
ff7b1510 384}
b5f01ae0 385
452418c4 386wxBitmap::wxBitmap(const char* const* bits)
e52f60e6 387{
452418c4 388 wxCHECK2_MSG(bits != NULL, return, wxT("invalid bitmap data"));
8bbe427f 389
005f5d18 390 GdkVisual *visual = wxTheApp->GetGdkVisual();
c2fa61e8 391
e52f60e6
RR
392 m_refData = new wxBitmapRefData();
393
394 GdkBitmap *mask = (GdkBitmap*) NULL;
8bbe427f 395
c2fa61e8 396 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm_d( wxGetRootWindow()->window, &mask, NULL, (gchar **) bits );
8bbe427f 397
452418c4 398 wxCHECK2_MSG(M_BMPDATA->m_pixmap, return, wxT("couldn't create pixmap"));
8bbe427f 399
fd0eed64
RR
400 if (mask)
401 {
402 M_BMPDATA->m_mask = new wxMask();
403 M_BMPDATA->m_mask->m_bitmap = mask;
404 }
8bbe427f 405
fd0eed64 406 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
2eefae6e 407
8cce8940 408 M_BMPDATA->m_bpp = visual->depth; // Can we get a different depth from create_from_xpm_d() ?
ff7b1510 409}
b5f01ae0 410
783da845
RR
411wxBitmap wxBitmap::Rescale( int clipx, int clipy, int clipwidth, int clipheight, int newx, int newy )
412{
413 wxCHECK_MSG( Ok(), wxNullBitmap, wxT("invalid bitmap") );
414
415 if (newy==M_BMPDATA->m_width && newy==M_BMPDATA->m_height)
416 return *this;
902725ee 417
783da845
RR
418 int width = wxMax(newx, 1);
419 int height = wxMax(newy, 1);
420 width = wxMin(width, clipwidth);
421 height = wxMin(height, clipheight);
902725ee 422
feac7937 423 wxBitmap bmp;
783da845 424
3cbab641
MR
425 GdkImage *img = (GdkImage*) NULL;
426 if (GetPixmap())
427 img = gdk_image_get( GetPixmap(), 0, 0, GetWidth(), GetHeight() );
428 else if (GetBitmap())
429 img = gdk_image_get( GetBitmap(), 0, 0, GetWidth(), GetHeight() );
430 else
431 wxFAIL_MSG( wxT("Ill-formed bitmap") );
432
433 wxCHECK_MSG( img, wxNullBitmap, wxT("couldn't create image") );
434
435 int bpp = -1;
436
437
438 GdkGC *gc = NULL;
439 GdkPixmap *dstpix = NULL;
440 if (GetPixmap())
783da845 441 {
3cbab641
MR
442 GdkVisual *visual = gdk_window_get_visual( GetPixmap() );
443 if (visual == NULL)
444 visual = wxTheApp->GetGdkVisual();
445
446 bpp = visual->depth;
447 bmp = wxBitmap(width,height,bpp);
448 dstpix = bmp.GetPixmap();
449 gc = gdk_gc_new( dstpix );
783da845 450 }
3cbab641
MR
451
452 char *dst = NULL;
453 long dstbyteperline = 0;
454
455 if (GetBitmap())
783da845 456 {
3cbab641
MR
457 bpp = 1;
458 dstbyteperline = width/8*M_BMPDATA->m_bpp;
459 if (width*M_BMPDATA->m_bpp % 8 != 0)
460 dstbyteperline++;
461 dst = (char*) malloc(dstbyteperline*height);
462 }
feac7937 463
3cbab641
MR
464 // be careful to use the right scaling factor
465 float scx = (float)M_BMPDATA->m_width/(float)newx;
466 float scy = (float)M_BMPDATA->m_height/(float)newy;
467 // prepare accel-tables
468 int *tablex = (int *)calloc(width,sizeof(int));
469 int *tabley = (int *)calloc(height,sizeof(int));
783da845 470
3cbab641
MR
471 // accel table filled with clipped values
472 for (int x = 0; x < width; x++)
473 tablex[x] = (int) (scx * (x+clipx));
474 for (int y = 0; y < height; y++)
475 tabley[y] = (int) (scy * (y+clipy));
feac7937 476
3cbab641
MR
477 // Main rescaling routine starts here
478 for (int h = 0; h < height; h++)
479 {
480 char outbyte = 0;
481 int old_x = -1;
482 guint32 old_pixval = 0;
902725ee 483
3cbab641 484 for (int w = 0; w < width; w++)
783da845 485 {
3cbab641
MR
486 guint32 pixval;
487 int x = tablex[w];
488 if (x == old_x)
489 pixval = old_pixval;
490 else
491 {
492 pixval = gdk_image_get_pixel( img, x, tabley[h] );
493 old_pixval = pixval;
494 old_x = x;
495 }
783da845 496
3cbab641
MR
497 if (bpp == 1)
498 {
499 if (!pixval)
500 {
501 char bit=1;
502 char shift = bit << (w % 8);
503 outbyte |= shift;
504 }
902725ee 505
3cbab641
MR
506 if ((w+1)%8==0)
507 {
508 dst[h*dstbyteperline+w/8] = outbyte;
509 outbyte = 0;
510 }
511 }
512 else
513 {
514 GdkColor col;
515 col.pixel = pixval;
516 gdk_gc_set_foreground( gc, &col );
517 gdk_draw_point( dstpix, gc, w, h);
518 }
feac7937 519 }
902725ee 520
3cbab641
MR
521 // do not forget the last byte
522 if ((bpp == 1) && (width % 8 != 0))
523 dst[h*dstbyteperline+width/8] = outbyte;
524 }
feac7937 525
3cbab641
MR
526 gdk_image_destroy( img );
527 if (gc) gdk_gc_unref( gc );
528
529 if (bpp == 1)
530 {
531 bmp = wxBitmap( (const char *)dst, width, height, 1 );
532 free( dst );
533 }
534
535 if (GetMask())
536 {
537 dstbyteperline = width/8;
538 if (width % 8 != 0)
539 dstbyteperline++;
540 dst = (char*) malloc(dstbyteperline*height);
541 img = gdk_image_get( GetMask()->GetBitmap(), 0, 0, GetWidth(), GetHeight() );
783da845
RR
542
543 for (int h = 0; h < height; h++)
544 {
545 char outbyte = 0;
f9ebac93 546 int old_x = -1;
b63b07a8 547 guint32 old_pixval = 0;
feac7937 548
f9ebac93 549 for (int w = 0; w < width; w++)
783da845 550 {
f9ebac93
RR
551 guint32 pixval;
552 int x = tablex[w];
553 if (x == old_x)
554 pixval = old_pixval;
555 else
556 {
557 pixval = gdk_image_get_pixel( img, x, tabley[h] );
558 old_pixval = pixval;
559 old_x = x;
560 }
902725ee 561
3cbab641 562 if (pixval)
783da845 563 {
3cbab641
MR
564 char bit=1;
565 char shift = bit << (w % 8);
566 outbyte |= shift;
783da845 567 }
3cbab641
MR
568
569 if ((w+1)%8 == 0)
783da845 570 {
3cbab641
MR
571 dst[h*dstbyteperline+w/8] = outbyte;
572 outbyte = 0;
783da845
RR
573 }
574 }
902725ee 575
783da845 576 // do not forget the last byte
3cbab641 577 if (width % 8 != 0)
f9ebac93 578 dst[h*dstbyteperline+width/8] = outbyte;
783da845 579 }
3cbab641
MR
580 wxMask* mask = new wxMask;
581 mask->m_bitmap = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) dst, width, height );
582 bmp.SetMask(mask);
902725ee 583
3cbab641 584 free( dst );
783da845 585 gdk_image_destroy( img );
feac7937 586 }
902725ee 587
3cbab641
MR
588 free( tablex );
589 free( tabley );
590
902725ee 591 return bmp;
783da845
RR
592}
593
feac7937 594bool wxBitmap::CreateFromImage(const wxImage& image, int depth)
b5f01ae0 595{
a11672a4
RR
596 UnRef();
597
637b7e4f
VZ
598 wxCHECK_MSG( image.Ok(), false, wxT("invalid image") );
599 wxCHECK_MSG( depth == -1 || depth == 1, false, wxT("invalid bitmap depth") );
b5f01ae0 600
feac7937
VS
601 if (image.GetWidth() <= 0 || image.GetHeight() <= 0)
602 return false;
902725ee 603
feac7937 604 m_refData = new wxBitmapRefData();
3ebcd89d 605
feac7937 606 if (depth == 1)
3ebcd89d 607 {
feac7937 608 return CreateFromImageAsBitmap(image);
3ebcd89d 609 }
feac7937
VS
610 else
611 {
feac7937
VS
612 return CreateFromImageAsPixmap(image);
613 }
614}
3ebcd89d 615
feac7937
VS
616// conversion to mono bitmap:
617bool wxBitmap::CreateFromImageAsBitmap(const wxImage& img)
618{
619 // convert alpha channel to mask, if it is present:
620 wxImage image(img);
621 image.ConvertAlphaToMask();
902725ee 622
feac7937
VS
623 int width = image.GetWidth();
624 int height = image.GetHeight();
c2fa61e8 625
3ebcd89d
VZ
626 SetHeight( height );
627 SetWidth( width );
628
feac7937 629 SetBitmap( gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 ) );
b5f01ae0 630
feac7937 631 SetDepth( 1 );
2eefae6e 632
feac7937 633 GdkVisual *visual = wxTheApp->GetGdkVisual();
b5f01ae0 634
feac7937 635 // Create picture image
b5f01ae0 636
feac7937 637 unsigned char *data_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
b5f01ae0 638
feac7937
VS
639 GdkImage *data_image =
640 gdk_image_new_bitmap( visual, data_data, width, height );
b5f01ae0 641
feac7937 642 // Create mask image
b5f01ae0 643
feac7937 644 GdkImage *mask_image = (GdkImage*) NULL;
b5f01ae0 645
feac7937
VS
646 if (image.HasMask())
647 {
648 unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
b5f01ae0 649
feac7937 650 mask_image = gdk_image_new_bitmap( visual, mask_data, width, height );
b5f01ae0 651
feac7937
VS
652 wxMask *mask = new wxMask();
653 mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 );
b5f01ae0 654
feac7937
VS
655 SetMask( mask );
656 }
b5f01ae0 657
feac7937
VS
658 int r_mask = image.GetMaskRed();
659 int g_mask = image.GetMaskGreen();
660 int b_mask = image.GetMaskBlue();
b5f01ae0 661
feac7937 662 unsigned char* data = image.GetData();
b5f01ae0 663
feac7937
VS
664 int index = 0;
665 for (int y = 0; y < height; y++)
666 {
667 for (int x = 0; x < width; x++)
b5f01ae0 668 {
feac7937
VS
669 int r = data[index];
670 index++;
671 int g = data[index];
672 index++;
673 int b = data[index];
674 index++;
675
676 if (image.HasMask())
b5f01ae0 677 {
feac7937
VS
678 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
679 gdk_image_put_pixel( mask_image, x, y, 1 );
b5f01ae0 680 else
feac7937
VS
681 gdk_image_put_pixel( mask_image, x, y, 0 );
682 }
b5f01ae0 683
feac7937
VS
684 if ((r == 255) && (b == 255) && (g == 255))
685 gdk_image_put_pixel( data_image, x, y, 1 );
686 else
687 gdk_image_put_pixel( data_image, x, y, 0 );
b5f01ae0 688
feac7937
VS
689 } // for
690 } // for
b5f01ae0 691
feac7937 692 // Blit picture
b5f01ae0 693
feac7937 694 GdkGC *data_gc = gdk_gc_new( GetBitmap() );
b5f01ae0 695
feac7937 696 gdk_draw_image( GetBitmap(), data_gc, data_image, 0, 0, 0, 0, width, height );
b5f01ae0 697
feac7937
VS
698 gdk_image_destroy( data_image );
699 gdk_gc_unref( data_gc );
b5f01ae0 700
feac7937 701 // Blit mask
b5f01ae0 702
feac7937
VS
703 if (image.HasMask())
704 {
705 GdkGC *mask_gc = gdk_gc_new( GetMask()->GetBitmap() );
b5f01ae0 706
feac7937
VS
707 gdk_draw_image( GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height );
708
709 gdk_image_destroy( mask_image );
710 gdk_gc_unref( mask_gc );
b5f01ae0 711 }
c2fa61e8 712
feac7937
VS
713 return true;
714}
902725ee 715
feac7937
VS
716// conversion to colour bitmap:
717bool wxBitmap::CreateFromImageAsPixmap(const wxImage& img)
718{
719 // convert alpha channel to mask, if it is present:
720 wxImage image(img);
721 image.ConvertAlphaToMask();
902725ee 722
feac7937
VS
723 int width = image.GetWidth();
724 int height = image.GetHeight();
725
726 SetHeight( height );
727 SetWidth( width );
728
729 SetPixmap( gdk_pixmap_new( wxGetRootWindow()->window, width, height, -1 ) );
b5f01ae0 730
feac7937 731 GdkVisual *visual = wxTheApp->GetGdkVisual();
b5f01ae0 732
feac7937 733 int bpp = visual->depth;
b5f01ae0 734
feac7937 735 SetDepth( bpp );
b5f01ae0 736
feac7937
VS
737 if ((bpp == 16) && (visual->red_mask != 0xf800))
738 bpp = 15;
739 else if (bpp < 8)
740 bpp = 8;
2eefae6e 741
feac7937
VS
742 // We handle 8-bit bitmaps ourselves using the colour cube, 12-bit
743 // visuals are not supported by GDK so we do these ourselves, too.
744 // 15-bit and 16-bit should actually work and 24-bit certainly does.
9b72a74d 745#ifdef __sgi
feac7937 746 if (!image.HasMask() && (bpp > 16))
9b72a74d 747#else
feac7937 748 if (!image.HasMask() && (bpp > 12))
9b72a74d 749#endif
feac7937 750 {
902725ee 751 static bool s_hasInitialized = false;
b5f01ae0 752
feac7937
VS
753 if (!s_hasInitialized)
754 {
755 gdk_rgb_init();
902725ee 756 s_hasInitialized = true;
feac7937 757 }
b5f01ae0 758
feac7937 759 GdkGC *gc = gdk_gc_new( GetPixmap() );
b5f01ae0 760
feac7937
VS
761 gdk_draw_rgb_image( GetPixmap(),
762 gc,
763 0, 0,
764 width, height,
765 GDK_RGB_DITHER_NONE,
766 image.GetData(),
767 width*3 );
b5f01ae0 768
feac7937 769 gdk_gc_unref( gc );
902725ee 770 return true;
feac7937 771 }
b5f01ae0 772
feac7937 773 // Create picture image
b5f01ae0 774
feac7937
VS
775 GdkImage *data_image =
776 gdk_image_new( GDK_IMAGE_FASTEST, visual, width, height );
b5f01ae0 777
feac7937 778 // Create mask image
b5f01ae0 779
feac7937 780 GdkImage *mask_image = (GdkImage*) NULL;
b5f01ae0 781
feac7937
VS
782 if (image.HasMask())
783 {
784 unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
b5f01ae0 785
feac7937 786 mask_image = gdk_image_new_bitmap( visual, mask_data, width, height );
b5f01ae0 787
feac7937
VS
788 wxMask *mask = new wxMask();
789 mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 );
b5f01ae0 790
feac7937
VS
791 SetMask( mask );
792 }
b5f01ae0 793
feac7937 794 // Render
b5f01ae0 795
feac7937
VS
796 enum byte_order { RGB, RBG, BRG, BGR, GRB, GBR };
797 byte_order b_o = RGB;
b5f01ae0 798
feac7937
VS
799 if (bpp > 8)
800 {
801 if ((visual->red_mask > visual->green_mask) && (visual->green_mask > visual->blue_mask)) b_o = RGB;
802 else if ((visual->red_mask > visual->blue_mask) && (visual->blue_mask > visual->green_mask)) b_o = RBG;
803 else if ((visual->blue_mask > visual->red_mask) && (visual->red_mask > visual->green_mask)) b_o = BRG;
804 else if ((visual->blue_mask > visual->green_mask) && (visual->green_mask > visual->red_mask)) b_o = BGR;
805 else if ((visual->green_mask > visual->red_mask) && (visual->red_mask > visual->blue_mask)) b_o = GRB;
806 else if ((visual->green_mask > visual->blue_mask) && (visual->blue_mask > visual->red_mask)) b_o = GBR;
807 }
b5f01ae0 808
feac7937
VS
809 int r_mask = image.GetMaskRed();
810 int g_mask = image.GetMaskGreen();
811 int b_mask = image.GetMaskBlue();
b5f01ae0 812
feac7937 813 unsigned char* data = image.GetData();
b5f01ae0 814
feac7937
VS
815 int index = 0;
816 for (int y = 0; y < height; y++)
817 {
818 for (int x = 0; x < width; x++)
b5f01ae0 819 {
feac7937
VS
820 int r = data[index];
821 index++;
822 int g = data[index];
823 index++;
824 int b = data[index];
825 index++;
826
827 if (image.HasMask())
b5f01ae0 828 {
feac7937
VS
829 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
830 gdk_image_put_pixel( mask_image, x, y, 1 );
831 else
832 gdk_image_put_pixel( mask_image, x, y, 0 );
833 }
b5f01ae0 834
feac7937
VS
835 switch (bpp)
836 {
837 case 8:
b5f01ae0 838 {
feac7937
VS
839 int pixel = -1;
840 if (wxTheApp->m_colorCube)
b5f01ae0 841 {
feac7937
VS
842 pixel = wxTheApp->m_colorCube[ ((r & 0xf8) << 7) + ((g & 0xf8) << 2) + ((b & 0xf8) >> 3) ];
843 }
844 else
845 {
846 GdkColormap *cmap = gtk_widget_get_default_colormap();
847 GdkColor *colors = cmap->colors;
848 int max = 3 * (65536);
849
850 for (int i = 0; i < cmap->size; i++)
b5f01ae0 851 {
feac7937
VS
852 int rdiff = (r << 8) - colors[i].red;
853 int gdiff = (g << 8) - colors[i].green;
854 int bdiff = (b << 8) - colors[i].blue;
855 int sum = ABS (rdiff) + ABS (gdiff) + ABS (bdiff);
856 if (sum < max) { pixel = i; max = sum; }
b5f01ae0 857 }
feac7937 858 }
2eefae6e 859
feac7937 860 gdk_image_put_pixel( data_image, x, y, pixel );
2eefae6e 861
feac7937
VS
862 break;
863 }
864 case 12: // SGI only
865 {
866 guint32 pixel = 0;
867 switch (b_o)
b5f01ae0 868 {
feac7937
VS
869 case RGB: pixel = ((r & 0xf0) << 4) | (g & 0xf0) | ((b & 0xf0) >> 4); break;
870 case RBG: pixel = ((r & 0xf0) << 4) | (b & 0xf0) | ((g & 0xf0) >> 4); break;
871 case GRB: pixel = ((g & 0xf0) << 4) | (r & 0xf0) | ((b & 0xf0) >> 4); break;
872 case GBR: pixel = ((g & 0xf0) << 4) | (b & 0xf0) | ((r & 0xf0) >> 4); break;
873 case BRG: pixel = ((b & 0xf0) << 4) | (r & 0xf0) | ((g & 0xf0) >> 4); break;
874 case BGR: pixel = ((b & 0xf0) << 4) | (g & 0xf0) | ((r & 0xf0) >> 4); break;
b5f01ae0 875 }
feac7937
VS
876 gdk_image_put_pixel( data_image, x, y, pixel );
877 break;
878 }
879 case 15:
880 {
881 guint32 pixel = 0;
882 switch (b_o)
b5f01ae0 883 {
feac7937
VS
884 case RGB: pixel = ((r & 0xf8) << 7) | ((g & 0xf8) << 2) | ((b & 0xf8) >> 3); break;
885 case RBG: pixel = ((r & 0xf8) << 7) | ((b & 0xf8) << 2) | ((g & 0xf8) >> 3); break;
886 case GRB: pixel = ((g & 0xf8) << 7) | ((r & 0xf8) << 2) | ((b & 0xf8) >> 3); break;
887 case GBR: pixel = ((g & 0xf8) << 7) | ((b & 0xf8) << 2) | ((r & 0xf8) >> 3); break;
888 case BRG: pixel = ((b & 0xf8) << 7) | ((r & 0xf8) << 2) | ((g & 0xf8) >> 3); break;
889 case BGR: pixel = ((b & 0xf8) << 7) | ((g & 0xf8) << 2) | ((r & 0xf8) >> 3); break;
b5f01ae0 890 }
feac7937
VS
891 gdk_image_put_pixel( data_image, x, y, pixel );
892 break;
893 }
894 case 16:
895 {
896 // I actually don't know if for 16-bit displays, it is alway the green
897 // component or the second component which has 6 bits.
898 guint32 pixel = 0;
899 switch (b_o)
b5f01ae0 900 {
feac7937
VS
901 case RGB: pixel = ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | ((b & 0xf8) >> 3); break;
902 case RBG: pixel = ((r & 0xf8) << 8) | ((b & 0xfc) << 3) | ((g & 0xf8) >> 3); break;
903 case GRB: pixel = ((g & 0xf8) << 8) | ((r & 0xfc) << 3) | ((b & 0xf8) >> 3); break;
904 case GBR: pixel = ((g & 0xf8) << 8) | ((b & 0xfc) << 3) | ((r & 0xf8) >> 3); break;
905 case BRG: pixel = ((b & 0xf8) << 8) | ((r & 0xfc) << 3) | ((g & 0xf8) >> 3); break;
906 case BGR: pixel = ((b & 0xf8) << 8) | ((g & 0xfc) << 3) | ((r & 0xf8) >> 3); break;
8ab696e0 907 }
feac7937
VS
908 gdk_image_put_pixel( data_image, x, y, pixel );
909 break;
910 }
911 case 32:
912 case 24:
913 {
914 guint32 pixel = 0;
915 switch (b_o)
8ab696e0 916 {
feac7937
VS
917 case RGB: pixel = (r << 16) | (g << 8) | b; break;
918 case RBG: pixel = (r << 16) | (b << 8) | g; break;
919 case BRG: pixel = (b << 16) | (r << 8) | g; break;
920 case BGR: pixel = (b << 16) | (g << 8) | r; break;
921 case GRB: pixel = (g << 16) | (r << 8) | b; break;
922 case GBR: pixel = (g << 16) | (b << 8) | r; break;
b5f01ae0 923 }
feac7937 924 gdk_image_put_pixel( data_image, x, y, pixel );
902725ee 925 break;
b5f01ae0 926 }
feac7937
VS
927 default: break;
928 }
929 } // for
930 } // for
b5f01ae0 931
feac7937 932 // Blit picture
b5f01ae0 933
feac7937 934 GdkGC *data_gc = gdk_gc_new( GetPixmap() );
b5f01ae0 935
feac7937 936 gdk_draw_image( GetPixmap(), data_gc, data_image, 0, 0, 0, 0, width, height );
b5f01ae0 937
feac7937
VS
938 gdk_image_destroy( data_image );
939 gdk_gc_unref( data_gc );
b5f01ae0 940
feac7937 941 // Blit mask
b5f01ae0 942
feac7937
VS
943 if (image.HasMask())
944 {
945 GdkGC *mask_gc = gdk_gc_new( GetMask()->GetBitmap() );
b5f01ae0 946
feac7937 947 gdk_draw_image( GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height );
b5f01ae0 948
feac7937
VS
949 gdk_image_destroy( mask_image );
950 gdk_gc_unref( mask_gc );
b5f01ae0
VS
951 }
952
feac7937 953 return true;
b5f01ae0
VS
954}
955
feac7937
VS
956wxImage wxBitmap::ConvertToImage() const
957{
958 wxImage image;
c2fa61e8 959
feac7937 960 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
902725ee 961
feac7937
VS
962 image.Create(GetWidth(), GetHeight());
963 unsigned char *data = image.GetData();
2eefae6e 964
b5f01ae0
VS
965 if (!data)
966 {
b5f01ae0
VS
967 wxFAIL_MSG( wxT("couldn't create image") );
968 return wxNullImage;
969 }
970
3cbab641
MR
971 // the colour used as transparent one in wxImage and the one it is
972 // replaced with when it really occurs in the bitmap
973 static const int MASK_RED = 1;
974 static const int MASK_GREEN = 2;
975 static const int MASK_BLUE = 3;
976 static const int MASK_BLUE_REPLACEMENT = 2;
b5f01ae0 977
3cbab641 978 GdkImage *gdk_image = (GdkImage*) NULL;
b5f01ae0 979
3cbab641
MR
980 if (HasPixmap())
981 {
982 gdk_image = gdk_image_get( GetPixmap(),
983 0, 0,
984 GetWidth(), GetHeight() );
985 }
986 else if (GetBitmap())
987 {
988 gdk_image = gdk_image_get( GetBitmap(),
989 0, 0,
990 GetWidth(), GetHeight() );
b5f01ae0 991 }
feac7937 992 else
b5f01ae0 993 {
3cbab641
MR
994 wxFAIL_MSG( wxT("Ill-formed bitmap") );
995 }
b5f01ae0 996
3cbab641 997 wxCHECK_MSG( gdk_image, wxNullImage, wxT("couldn't create image") );
feac7937 998
3cbab641
MR
999 GdkImage *gdk_image_mask = (GdkImage*) NULL;
1000 if (GetMask())
1001 {
1002 gdk_image_mask = gdk_image_get( GetMask()->GetBitmap(),
1003 0, 0,
1004 GetWidth(), GetHeight() );
feac7937 1005
3cbab641
MR
1006 image.SetMaskColour( MASK_RED, MASK_GREEN, MASK_BLUE );
1007 }
feac7937 1008
3cbab641
MR
1009 int bpp = -1;
1010 int red_shift_right = 0;
1011 int green_shift_right = 0;
1012 int blue_shift_right = 0;
1013 int red_shift_left = 0;
1014 int green_shift_left = 0;
1015 int blue_shift_left = 0;
1016 bool use_shift = false;
feac7937 1017
3cbab641
MR
1018 if (GetPixmap())
1019 {
1020 GdkVisual *visual = gdk_window_get_visual( GetPixmap() );
1021 if (visual == NULL)
1022 visual = wxTheApp->GetGdkVisual();
1023
1024 bpp = visual->depth;
1025 if (bpp == 16)
1026 bpp = visual->red_prec + visual->green_prec + visual->blue_prec;
1027 red_shift_right = visual->red_shift;
1028 red_shift_left = 8-visual->red_prec;
1029 green_shift_right = visual->green_shift;
1030 green_shift_left = 8-visual->green_prec;
1031 blue_shift_right = visual->blue_shift;
1032 blue_shift_left = 8-visual->blue_prec;
1033
1034 use_shift = (visual->type == GDK_VISUAL_TRUE_COLOR) || (visual->type == GDK_VISUAL_DIRECT_COLOR);
1035 }
1036 if (GetBitmap())
1037 {
1038 bpp = 1;
1039 }
feac7937
VS
1040
1041
3cbab641 1042 GdkColormap *cmap = gtk_widget_get_default_colormap();
feac7937 1043
3cbab641
MR
1044 long pos = 0;
1045 for (int j = 0; j < GetHeight(); j++)
1046 {
1047 for (int i = 0; i < GetWidth(); i++)
feac7937 1048 {
3cbab641
MR
1049 wxUint32 pixel = gdk_image_get_pixel( gdk_image, i, j );
1050 if (bpp == 1)
8ab696e0 1051 {
3cbab641 1052 if (pixel == 0)
8ab696e0 1053 {
3cbab641
MR
1054 data[pos] = 0;
1055 data[pos+1] = 0;
1056 data[pos+2] = 0;
2eefae6e 1057 }
feac7937 1058 else
2eefae6e 1059 {
3cbab641
MR
1060 data[pos] = 255;
1061 data[pos+1] = 255;
1062 data[pos+2] = 255;
b5f01ae0 1063 }
3cbab641
MR
1064 }
1065 else if (use_shift)
1066 {
1067 data[pos] = (pixel >> red_shift_right) << red_shift_left;
1068 data[pos+1] = (pixel >> green_shift_right) << green_shift_left;
1069 data[pos+2] = (pixel >> blue_shift_right) << blue_shift_left;
1070 }
1071 else if (cmap->colors)
1072 {
1073 data[pos] = cmap->colors[pixel].red >> 8;
1074 data[pos+1] = cmap->colors[pixel].green >> 8;
1075 data[pos+2] = cmap->colors[pixel].blue >> 8;
1076 }
1077 else
1078 {
1079 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
1080 }
b5f01ae0 1081
3cbab641
MR
1082 if (gdk_image_mask)
1083 {
1084 int mask_pixel = gdk_image_get_pixel( gdk_image_mask, i, j );
1085 if (mask_pixel == 0)
feac7937 1086 {
3cbab641
MR
1087 data[pos] = MASK_RED;
1088 data[pos+1] = MASK_GREEN;
1089 data[pos+2] = MASK_BLUE;
1090 }
1091 else if ( data[pos] == MASK_RED &&
1092 data[pos+1] == MASK_GREEN &&
1093 data[pos+2] == MASK_BLUE )
1094 {
1095 data[pos+2] = MASK_BLUE_REPLACEMENT;
feac7937 1096 }
feac7937 1097 }
b5f01ae0 1098
3cbab641
MR
1099 pos += 3;
1100 }
feac7937 1101 }
b5f01ae0 1102
3cbab641
MR
1103 gdk_image_destroy( gdk_image );
1104 if (gdk_image_mask) gdk_image_destroy( gdk_image_mask );
1105
b5f01ae0
VS
1106 return image;
1107}
1108
4611dd06 1109wxBitmap::wxBitmap( const wxString &filename, wxBitmapType type )
c801d85f 1110{
fd0eed64 1111 LoadFile( filename, type );
ff7b1510 1112}
c801d85f 1113
debe6624 1114wxBitmap::wxBitmap( const char bits[], int width, int height, int WXUNUSED(depth))
6f65e337 1115{
3ebcd89d
VZ
1116 if ( width > 0 && height > 0 )
1117 {
1118 m_refData = new wxBitmapRefData();
6f65e337 1119
3ebcd89d
VZ
1120 M_BMPDATA->m_mask = (wxMask *) NULL;
1121 M_BMPDATA->m_bitmap = gdk_bitmap_create_from_data
1122 (
1123 wxGetRootWindow()->window,
1124 (gchar *) bits,
1125 width,
1126 height
1127 );
1128 M_BMPDATA->m_width = width;
1129 M_BMPDATA->m_height = height;
1130 M_BMPDATA->m_bpp = 1;
6f65e337 1131
3ebcd89d
VZ
1132 wxASSERT_MSG( M_BMPDATA->m_bitmap, wxT("couldn't create bitmap") );
1133 }
6f65e337 1134}
8bbe427f
VZ
1135
1136wxBitmap::~wxBitmap()
c801d85f 1137{
ff7b1510 1138}
8bbe427f 1139
b7cacb43 1140bool wxBitmap::IsOk() const
c801d85f 1141{
902725ee 1142 return (m_refData != NULL) &&
3cbab641 1143 (M_BMPDATA->m_bitmap || M_BMPDATA->m_pixmap);
ff7b1510 1144}
8bbe427f 1145
91b8de8d 1146int wxBitmap::GetHeight() const
c801d85f 1147{
223d09f6 1148 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
e55ad60e 1149
fd0eed64 1150 return M_BMPDATA->m_height;
ff7b1510 1151}
c801d85f 1152
91b8de8d 1153int wxBitmap::GetWidth() const
c801d85f 1154{
223d09f6 1155 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
8bbe427f 1156
fd0eed64 1157 return M_BMPDATA->m_width;
ff7b1510 1158}
c801d85f 1159
91b8de8d 1160int wxBitmap::GetDepth() const
c801d85f 1161{
223d09f6 1162 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
8bbe427f 1163
fd0eed64 1164 return M_BMPDATA->m_bpp;
ff7b1510 1165}
c801d85f 1166
91b8de8d 1167wxMask *wxBitmap::GetMask() const
c801d85f 1168{
223d09f6 1169 wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") );
8bbe427f 1170
fd0eed64 1171 return M_BMPDATA->m_mask;
ff7b1510 1172}
c801d85f
KB
1173
1174void wxBitmap::SetMask( wxMask *mask )
1175{
223d09f6 1176 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
8bbe427f 1177
55ccdb93 1178 AllocExclusive();
fd0eed64 1179 if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask;
8bbe427f 1180
fd0eed64 1181 M_BMPDATA->m_mask = mask;
ff7b1510 1182}
c801d85f 1183
db0aec83
VS
1184bool wxBitmap::CopyFromIcon(const wxIcon& icon)
1185{
1186 *this = icon;
902725ee 1187 return true;
db0aec83
VS
1188}
1189
17bec151
RR
1190wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const
1191{
1192 wxCHECK_MSG( Ok() &&
13111b2a
VZ
1193 (rect.x >= 0) && (rect.y >= 0) &&
1194 (rect.x+rect.width <= M_BMPDATA->m_width) && (rect.y+rect.height <= M_BMPDATA->m_height),
17bec151 1195 wxNullBitmap, wxT("invalid bitmap or bitmap region") );
13111b2a 1196
17bec151
RR
1197 wxBitmap ret( rect.width, rect.height, M_BMPDATA->m_bpp );
1198 wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") );
13111b2a 1199
3cbab641 1200 if (ret.GetPixmap())
17bec151 1201 {
3cbab641
MR
1202 GdkGC *gc = gdk_gc_new( ret.GetPixmap() );
1203 gdk_draw_pixmap( ret.GetPixmap(), gc, GetPixmap(), rect.x, rect.y, 0, 0, rect.width, rect.height );
1204 gdk_gc_destroy( gc );
17bec151
RR
1205 }
1206 else
1207 {
3cbab641
MR
1208 GdkGC *gc = gdk_gc_new( ret.GetBitmap() );
1209 GdkColor col;
1210 col.pixel = 0xFFFFFF;
1211 gdk_gc_set_foreground( gc, &col );
1212 col.pixel = 0;
1213 gdk_gc_set_background( gc, &col );
1214 gdk_wx_draw_bitmap( ret.GetBitmap(), gc, GetBitmap(), rect.x, rect.y, 0, 0, rect.width, rect.height );
1215 gdk_gc_destroy( gc );
17bec151 1216 }
13111b2a 1217
17bec151
RR
1218 if (GetMask())
1219 {
1220 wxMask *mask = new wxMask;
c2fa61e8 1221 mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, rect.width, rect.height, 1 );
13111b2a 1222
17bec151 1223 GdkGC *gc = gdk_gc_new( mask->m_bitmap );
f9ebac93
RR
1224 GdkColor col;
1225 col.pixel = 0xFFFFFF;
1226 gdk_gc_set_foreground( gc, &col );
1227 col.pixel = 0;
1228 gdk_gc_set_background( gc, &col );
1229 gdk_wx_draw_bitmap( mask->m_bitmap, gc, M_BMPDATA->m_mask->m_bitmap, rect.x, rect.y, 0, 0, rect.width, rect.height );
13111b2a
VZ
1230 gdk_gc_destroy( gc );
1231
1232 ret.SetMask( mask );
17bec151 1233 }
13111b2a 1234
17bec151
RR
1235 return ret;
1236}
1237
4611dd06 1238bool wxBitmap::SaveFile( const wxString &name, wxBitmapType type, const wxPalette *WXUNUSED(palette) ) const
c801d85f 1239{
902725ee 1240 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
8bbe427f 1241
b75dd496 1242 // Try to save the bitmap via wxImage handlers:
fd0eed64 1243 {
368d59f0 1244 wxImage image = ConvertToImage();
284b4c88 1245 if (image.Ok()) return image.SaveFile( name, type );
fd0eed64 1246 }
8bbe427f 1247
902725ee 1248 return false;
ff7b1510 1249}
c801d85f 1250
4611dd06 1251bool wxBitmap::LoadFile( const wxString &name, wxBitmapType type )
c801d85f 1252{
fd0eed64 1253 UnRef();
8bbe427f 1254
3ebcd89d 1255 if (!wxFileExists(name))
902725ee 1256 return false;
8bbe427f 1257
005f5d18 1258 GdkVisual *visual = wxTheApp->GetGdkVisual();
c2fa61e8 1259
fd0eed64
RR
1260 if (type == wxBITMAP_TYPE_XPM)
1261 {
1262 m_refData = new wxBitmapRefData();
8bbe427f 1263
fd0eed64 1264 GdkBitmap *mask = (GdkBitmap*) NULL;
8bbe427f 1265
3ebcd89d
VZ
1266 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm
1267 (
1268 wxGetRootWindow()->window,
1269 &mask,
1270 NULL,
1271 name.fn_str()
1272 );
8bbe427f 1273
fd0eed64
RR
1274 if (mask)
1275 {
1276 M_BMPDATA->m_mask = new wxMask();
1277 M_BMPDATA->m_mask->m_bitmap = mask;
1278 }
8bbe427f 1279
fd0eed64 1280 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
c2fa61e8 1281
103aab26 1282 M_BMPDATA->m_bpp = visual->depth;
fd0eed64 1283 }
b75dd496 1284 else // try if wxImage can load it
fd0eed64
RR
1285 {
1286 wxImage image;
3ebcd89d 1287 if ( !image.LoadFile( name, type ) || !image.Ok() )
902725ee 1288 return false;
3ebcd89d
VZ
1289
1290 *this = wxBitmap(image);
fd0eed64 1291 }
8bbe427f 1292
902725ee 1293 return true;
ff7b1510 1294}
8bbe427f 1295
0b04c4e0 1296#if wxUSE_PALETTE
91b8de8d 1297wxPalette *wxBitmap::GetPalette() const
c801d85f 1298{
3ebcd89d
VZ
1299 if (!Ok())
1300 return (wxPalette *) NULL;
8bbe427f 1301
fd0eed64 1302 return M_BMPDATA->m_palette;
ff7b1510 1303}
c801d85f 1304
4611dd06
RR
1305void wxBitmap::SetPalette(const wxPalette& WXUNUSED(palette))
1306{
1307 // TODO
1308}
0b04c4e0 1309#endif // wxUSE_PALETTE
4611dd06 1310
4bc67cc5
RR
1311void wxBitmap::SetHeight( int height )
1312{
55ccdb93 1313 AllocExclusive();
4bc67cc5
RR
1314 M_BMPDATA->m_height = height;
1315}
1316
1317void wxBitmap::SetWidth( int width )
1318{
55ccdb93 1319 AllocExclusive();
4bc67cc5
RR
1320 M_BMPDATA->m_width = width;
1321}
1322
1323void wxBitmap::SetDepth( int depth )
1324{
55ccdb93 1325 AllocExclusive();
4bc67cc5
RR
1326 M_BMPDATA->m_bpp = depth;
1327}
1328
1329void wxBitmap::SetPixmap( GdkPixmap *pixmap )
1330{
3ebcd89d
VZ
1331 if (!m_refData)
1332 m_refData = new wxBitmapRefData();
4bc67cc5
RR
1333
1334 M_BMPDATA->m_pixmap = pixmap;
1335}
1336
82ea63e6
RR
1337void wxBitmap::SetBitmap( GdkPixmap *bitmap )
1338{
3ebcd89d
VZ
1339 if (!m_refData)
1340 m_refData = new wxBitmapRefData();
82ea63e6
RR
1341
1342 M_BMPDATA->m_bitmap = bitmap;
1343}
1344
91b8de8d 1345GdkPixmap *wxBitmap::GetPixmap() const
c801d85f 1346{
223d09f6 1347 wxCHECK_MSG( Ok(), (GdkPixmap *) NULL, wxT("invalid bitmap") );
8bbe427f 1348
fd0eed64 1349 return M_BMPDATA->m_pixmap;
ff7b1510 1350}
8bbe427f 1351
feac7937
VS
1352bool wxBitmap::HasPixmap() const
1353{
1354 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1355
1356 return M_BMPDATA->m_pixmap != NULL;
1357}
1358
91b8de8d 1359GdkBitmap *wxBitmap::GetBitmap() const
6f65e337 1360{
223d09f6 1361 wxCHECK_MSG( Ok(), (GdkBitmap *) NULL, wxT("invalid bitmap") );
8bbe427f 1362
fd0eed64 1363 return M_BMPDATA->m_bitmap;
ff7b1510 1364}
feac7937 1365
284f2b59
RR
1366void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp)
1367{
284f2b59 1368 return NULL;
284f2b59
RR
1369}
1370
17a1ebd1 1371void wxBitmap::UngetRawData(wxPixelDataBase& WXUNUSED(data))
284f2b59
RR
1372{
1373}
1374
902725ee 1375bool wxBitmap::HasAlpha() const
0ff2a74d 1376{
0ff2a74d 1377 return false;
0ff2a74d
RR
1378}
1379
1380void wxBitmap::UseAlpha()
902725ee 1381{
0ff2a74d
RR
1382}
1383
4b61c88d
RR
1384//-----------------------------------------------------------------------------
1385// wxBitmapHandler
1386//-----------------------------------------------------------------------------
1387
452418c4 1388IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler, wxBitmapHandlerBase)
4b61c88d
RR
1389
1390/* static */ void wxBitmap::InitStandardHandlers()
1391{
1392 // TODO: Insert handler based on GdkPixbufs handler later
1393}