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