]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/bitmap.cpp
Font fixes and icons in report view for list control
[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
c801d85f
KB
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
10#ifdef __GNUG__
11#pragma implementation "bitmap.h"
12#endif
13
14#include "wx/bitmap.h"
52cbfcf0 15#include "wx/icon.h"
c801d85f 16#include "gdk/gdkprivate.h"
01111366 17#include "gdk/gdkx.h"
c801d85f
KB
18
19//-----------------------------------------------------------------------------
20// wxMask
21//-----------------------------------------------------------------------------
22
23IMPLEMENT_DYNAMIC_CLASS(wxMask,wxObject)
24
25wxMask::wxMask(void)
26{
c67daf87 27 m_bitmap = (GdkBitmap *) NULL;
ff7b1510 28}
c801d85f
KB
29
30wxMask::wxMask( const wxBitmap& WXUNUSED(bitmap), const wxColour& WXUNUSED(colour) )
31{
ff7b1510 32}
c801d85f 33
debe6624 34wxMask::wxMask( const wxBitmap& WXUNUSED(bitmap), int WXUNUSED(paletteIndex) )
c801d85f 35{
ff7b1510 36}
c801d85f
KB
37
38wxMask::wxMask( const wxBitmap& WXUNUSED(bitmap) )
39{
ff7b1510 40}
c801d85f
KB
41
42wxMask::~wxMask(void)
43{
c801d85f 44 if (m_bitmap) gdk_bitmap_unref( m_bitmap );
ff7b1510 45}
c801d85f
KB
46
47GdkBitmap *wxMask::GetBitmap(void) const
48{
49 return m_bitmap;
ff7b1510 50}
c801d85f
KB
51
52//-----------------------------------------------------------------------------
53// wxBitmap
54//-----------------------------------------------------------------------------
55
56class wxBitmapRefData: public wxObjectRefData
57{
58 public:
59
60 wxBitmapRefData(void);
61 ~wxBitmapRefData(void);
62
63 GdkPixmap *m_pixmap;
6f65e337 64 GdkBitmap *m_bitmap;
c801d85f
KB
65 wxMask *m_mask;
66 int m_width;
67 int m_height;
68 int m_bpp;
69 wxPalette *m_palette;
70};
71
72wxBitmapRefData::wxBitmapRefData(void)
73{
c67daf87
UR
74 m_pixmap = (GdkPixmap *) NULL;
75 m_bitmap = (GdkBitmap *) NULL;
76 m_mask = (wxMask *) NULL;
c801d85f
KB
77 m_width = 0;
78 m_height = 0;
79 m_bpp = 0;
c67daf87 80 m_palette = (wxPalette *) NULL;
ff7b1510 81}
c801d85f
KB
82
83wxBitmapRefData::~wxBitmapRefData(void)
84{
c801d85f 85 if (m_pixmap) gdk_pixmap_unref( m_pixmap );
219f895a 86 if (m_bitmap) gdk_bitmap_unref( m_bitmap );
c801d85f
KB
87 if (m_mask) delete m_mask;
88 if (m_palette) delete m_palette;
ff7b1510 89}
c801d85f
KB
90
91//-----------------------------------------------------------------------------
92
93#define M_BMPDATA ((wxBitmapRefData *)m_refData)
94
95IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxGDIObject)
96
97wxBitmap::wxBitmap(void)
98{
99 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
ff7b1510 100}
c801d85f 101
debe6624 102wxBitmap::wxBitmap( int width, int height, int depth )
c801d85f 103{
fee04295
RR
104 wxCHECK_RET( (width > 0) && (height > 0), "invalid bitmap size" )
105 wxCHECK_RET( (depth > 0) || (depth == -1), "invalid bitmap depth" )
106
c801d85f 107 m_refData = new wxBitmapRefData();
fee04295
RR
108
109 GdkWindow *parent = (GdkWindow*) &gdk_root_parent;
110
c67daf87 111 M_BMPDATA->m_mask = (wxMask *) NULL;
fee04295 112 M_BMPDATA->m_pixmap = gdk_pixmap_new( parent, width, height, depth );
0180d5da
RR
113 M_BMPDATA->m_width = width;
114 M_BMPDATA->m_height = height;
fee04295 115 M_BMPDATA->m_bpp = gdk_window_get_visual( parent )->depth;
c801d85f
KB
116
117 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
ff7b1510 118}
c801d85f
KB
119
120wxBitmap::wxBitmap( char **bits )
121{
fee04295
RR
122 wxCHECK_RET( bits != NULL, "invalid bitmap data" )
123
c801d85f 124 m_refData = new wxBitmapRefData();
c801d85f 125
fee04295
RR
126 GdkBitmap *mask = (GdkBitmap*) NULL;
127 GdkWindow *parent = (GdkWindow*) &gdk_root_parent;
219f895a 128
fee04295 129 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm_d( parent, &mask, NULL, (gchar **) bits );
219f895a 130
c801d85f
KB
131 if (mask)
132 {
133 M_BMPDATA->m_mask = new wxMask();
134 M_BMPDATA->m_mask->m_bitmap = mask;
ff7b1510 135 }
219f895a 136
0180d5da
RR
137 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
138
fee04295 139 M_BMPDATA->m_bpp = gdk_window_get_visual( parent )->depth; // ?
c801d85f
KB
140
141 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
ff7b1510 142}
c801d85f
KB
143
144wxBitmap::wxBitmap( const wxBitmap& bmp )
145{
146 Ref( bmp );
147
148 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
ff7b1510 149}
c801d85f
KB
150
151wxBitmap::wxBitmap( const wxBitmap* bmp )
152{
153 if (bmp) Ref( *bmp );
154
155 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
ff7b1510 156}
6f65e337 157
debe6624 158wxBitmap::wxBitmap( const wxString &filename, int type )
c801d85f
KB
159{
160 LoadFile( filename, type );
ff7b1510
RR
161
162 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
163}
c801d85f 164
debe6624 165wxBitmap::wxBitmap( const char bits[], int width, int height, int WXUNUSED(depth))
6f65e337
JS
166{
167 m_refData = new wxBitmapRefData();
168
c67daf87 169 M_BMPDATA->m_mask = (wxMask *) NULL;
6f65e337
JS
170 M_BMPDATA->m_bitmap =
171 gdk_bitmap_create_from_data( (GdkWindow*) &gdk_root_parent, (gchar *) bits, width, height );
0180d5da
RR
172 M_BMPDATA->m_width = width;
173 M_BMPDATA->m_height = height;
6f65e337
JS
174 M_BMPDATA->m_bpp = 1;
175
176 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
177}
178
c801d85f
KB
179wxBitmap::~wxBitmap(void)
180{
181 if (wxTheBitmapList) wxTheBitmapList->DeleteObject(this);
ff7b1510 182}
c801d85f
KB
183
184wxBitmap& wxBitmap::operator = ( const wxBitmap& bmp )
185{
186 if (*this == bmp) return (*this);
187 Ref( bmp );
188 return *this;
ff7b1510 189}
c801d85f
KB
190
191bool wxBitmap::operator == ( const wxBitmap& bmp )
192{
193 return m_refData == bmp.m_refData;
ff7b1510 194}
c801d85f
KB
195
196bool wxBitmap::operator != ( const wxBitmap& bmp )
197{
198 return m_refData != bmp.m_refData;
ff7b1510 199}
c801d85f
KB
200
201bool wxBitmap::Ok(void) const
202{
cf7a7e13 203 return (m_refData != NULL);
ff7b1510 204}
c801d85f
KB
205
206int wxBitmap::GetHeight(void) const
207{
e55ad60e
RR
208 if (!Ok())
209 {
210 wxFAIL_MSG( "invalid bitmap" );
211 return -1;
212 }
213
c801d85f 214 return M_BMPDATA->m_height;
ff7b1510 215}
c801d85f
KB
216
217int wxBitmap::GetWidth(void) const
218{
e55ad60e
RR
219 if (!Ok())
220 {
221 wxFAIL_MSG( "invalid bitmap" );
222 return -1;
223 }
224
c801d85f 225 return M_BMPDATA->m_width;
ff7b1510 226}
c801d85f
KB
227
228int wxBitmap::GetDepth(void) const
229{
e55ad60e
RR
230 if (!Ok())
231 {
232 wxFAIL_MSG( "invalid bitmap" );
233 return -1;
234 }
235
c801d85f 236 return M_BMPDATA->m_bpp;
ff7b1510 237}
c801d85f 238
debe6624 239void wxBitmap::SetHeight( int height )
c801d85f
KB
240{
241 if (!Ok()) return;
cf7a7e13
RR
242
243 wxFAIL_MSG( "wxBitmap::SetHeight not implemented" );
244
c801d85f 245 M_BMPDATA->m_height = height;
ff7b1510 246}
c801d85f 247
debe6624 248void wxBitmap::SetWidth( int width )
c801d85f
KB
249{
250 if (!Ok()) return;
cf7a7e13
RR
251
252 wxFAIL_MSG( "wxBitmap::SetWidth not implemented" );
253
c801d85f 254 M_BMPDATA->m_width = width;
ff7b1510 255}
c801d85f 256
debe6624 257void wxBitmap::SetDepth( int depth )
c801d85f
KB
258{
259 if (!Ok()) return;
cf7a7e13
RR
260
261 wxFAIL_MSG( "wxBitmap::SetDepth not implemented" );
262
c801d85f 263 M_BMPDATA->m_bpp = depth;
ff7b1510 264}
c801d85f
KB
265
266wxMask *wxBitmap::GetMask(void) const
267{
e55ad60e
RR
268 if (!Ok())
269 {
270 wxFAIL_MSG( "invalid bitmap" );
271 return (wxMask *) NULL;
272 }
219f895a 273
c801d85f 274 return M_BMPDATA->m_mask;
ff7b1510 275}
c801d85f
KB
276
277void wxBitmap::SetMask( wxMask *mask )
278{
e55ad60e
RR
279 if (!Ok())
280 {
281 wxFAIL_MSG( "invalid bitmap" );
282 return;
283 }
219f895a 284
c801d85f 285 if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask;
cf7a7e13 286
c801d85f 287 M_BMPDATA->m_mask = mask;
ff7b1510 288}
c801d85f 289
01111366 290bool wxBitmap::SaveFile( const wxString &WXUNUSED(name), int WXUNUSED(type),
c801d85f
KB
291 wxPalette *WXUNUSED(palette) )
292{
e55ad60e
RR
293 if (!Ok())
294 {
295 wxFAIL_MSG( "invalid bitmap" );
296 return FALSE;
297 }
298
c801d85f 299 return FALSE;
ff7b1510 300}
c801d85f 301
01111366 302bool wxBitmap::LoadFile( const wxString &WXUNUSED(name), int WXUNUSED(type) )
c801d85f 303{
01111366 304 if (!Ok())
c801d85f 305 {
01111366 306 wxFAIL_MSG( "invalid bitmap" );
c801d85f 307 return FALSE;
ff7b1510 308 }
cf7a7e13 309
c801d85f 310 return FALSE;
ff7b1510 311}
c801d85f
KB
312
313wxPalette *wxBitmap::GetPalette(void) const
314{
c67daf87 315 if (!Ok()) return (wxPalette *) NULL;
c801d85f 316 return M_BMPDATA->m_palette;
ff7b1510 317}
c801d85f
KB
318
319GdkPixmap *wxBitmap::GetPixmap(void) const
320{
e55ad60e
RR
321 if (!Ok())
322 {
323 wxFAIL_MSG( "invalid bitmap" );
324 return (GdkPixmap *) NULL;
325 }
cf7a7e13 326
c801d85f 327 return M_BMPDATA->m_pixmap;
ff7b1510 328}
c801d85f 329
6f65e337
JS
330GdkBitmap *wxBitmap::GetBitmap(void) const
331{
e55ad60e
RR
332 if (!Ok())
333 {
334 wxFAIL_MSG( "invalid bitmap" );
335 return (GdkBitmap *) NULL;
336 }
219f895a 337
6f65e337 338 return M_BMPDATA->m_bitmap;
ff7b1510 339}
6f65e337 340
01111366 341wxBitmap::wxBitmap( const wxImage &image )
219f895a 342{
01111366 343 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
219f895a 344
01111366 345 if (!image.Ok()) return;
219f895a 346
01111366 347 m_refData = new wxBitmapRefData();
cf7a7e13 348
01111366
RR
349 M_BMPDATA->m_height = image.GetHeight();
350 M_BMPDATA->m_width = image.GetWidth();
351 int width = image.GetWidth();
352 int height = image.GetHeight();
353
354 // Create picture
cf7a7e13 355
01111366
RR
356 GdkImage *data_image =
357 gdk_image_new( GDK_IMAGE_FASTEST, gdk_visual_get_system(), width, height );
cf7a7e13 358
01111366
RR
359 M_BMPDATA->m_pixmap =
360 gdk_pixmap_new( (GdkWindow*)&gdk_root_parent, width, height, -1 );
361
362 // Create mask
cf7a7e13 363
01111366 364 GdkImage *mask_image = (GdkImage*) NULL;
cf7a7e13 365
01111366 366 if (image.HasMask())
cf7a7e13 367 {
01111366
RR
368 unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
369
370 mask_image = gdk_image_new_bitmap( gdk_visual_get_system(), mask_data, width, height );
371
372 M_BMPDATA->m_mask = new wxMask();
373 M_BMPDATA->m_mask->m_bitmap = gdk_pixmap_new( (GdkWindow*)&gdk_root_parent, width, height, 1 );
cf7a7e13
RR
374 }
375
01111366
RR
376 // Retrieve depth
377
378 M_BMPDATA->m_bpp = data_image->depth;
379
380 int render_depth = 8;
381 if (M_BMPDATA->m_bpp > 8) render_depth = M_BMPDATA->m_bpp;
382
383 // Render
fee04295
RR
384
385 enum byte_order { RGB, RBG, BRG, BGR, GRB, GBR };
386 byte_order b_o;
387
388 if (render_depth >= 24)
389 {
390 GdkVisual *visual = gdk_visual_get_system();
391 if ((visual->red_mask > visual->green_mask) && (visual->green_mask > visual->blue_mask)) b_o = RGB;
392 else if ((visual->red_mask > visual->blue_mask) && (visual->blue_mask > visual->green_mask)) b_o = RGB;
393 else if ((visual->blue_mask > visual->red_mask) && (visual->red_mask > visual->green_mask)) b_o = BRG;
394 else if ((visual->blue_mask > visual->green_mask) && (visual->green_mask > visual->red_mask)) b_o = BGR;
395 else if ((visual->green_mask > visual->red_mask) && (visual->red_mask > visual->blue_mask)) b_o = GRB;
396 else if ((visual->green_mask > visual->blue_mask) && (visual->blue_mask > visual->red_mask)) b_o = GBR;
397 }
01111366
RR
398
399 int r_mask = image.GetMaskRed();
400 int g_mask = image.GetMaskGreen();
401 int b_mask = image.GetMaskBlue();
402
403 unsigned char* data = image.GetData();
404
405 int index = 0;
406 for (int y = 0; y < height; y++)
407 for (int x = 0; x < width; x++)
408 {
409 int r = data[index];
410 index++;
411 int g = data[index];
412 index++;
413 int b = data[index];
414 index++;
415
416 if (image.HasMask())
417 {
418 if ((r == r_mask) && (b = b_mask) && (g = g_mask))
419 gdk_image_put_pixel( mask_image, x, y, 0 );
420 else
421 gdk_image_put_pixel( mask_image, x, y, 1 );
422 }
423
424 switch (render_depth)
425 {
426 case 8:
427 {
428 GdkColormap *cmap = gtk_widget_get_default_colormap();
429 GdkColor *colors = cmap->colors;
430 int max = 3 * (65536);
431 int index = -1;
432
433 for (int i = 0; i < cmap->size; i++)
434 {
435 int rdiff = (r << 8) - colors[i].red;
436 int gdiff = (g << 8) - colors[i].green;
437 int bdiff = (b << 8) - colors[i].blue;
438 int sum = ABS (rdiff) + ABS (gdiff) + ABS (bdiff);
439 if (sum < max) { index = i; max = sum; }
440 }
441
442 gdk_image_put_pixel( data_image, x, y, index );
443
444 break;
445 }
446 case 15:
447 {
448 guint32 pixel = ((r & 0xf8) << 7) | ((g & 0xf8) << 2) | ((b & 0xf8) >> 3);
449 gdk_image_put_pixel( data_image, x, y, pixel );
450 break;
451 }
452 case 16:
453 {
454 guint32 pixel = ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | ((b & 0xf8) >> 3);
455 gdk_image_put_pixel( data_image, x, y, pixel );
456 break;
457 }
01111366 458 case 32:
fee04295 459 case 24:
01111366 460 {
fee04295
RR
461 guint32 pixel = 0;
462 switch (b_o)
463 {
464 case RGB: pixel = (r << 16) | (g << 8) | b; break;
465 case RBG: pixel = (r << 16) | (b << 8) | g; break;
466 case BRG: pixel = (b << 16) | (r << 8) | g; break;
467 case BGR: pixel = (b << 16) | (g << 8) | r; break;
468 case GRB: pixel = (g << 16) | (r << 8) | b; break;
469 case GBR: pixel = (g << 16) | (b << 8) | r; break;
470 }
471 gdk_image_put_pixel( data_image, x, y, pixel );
01111366
RR
472 }
473 default: break;
474 }
475 }
476
477 // Blit picture
cf7a7e13 478
01111366 479 GdkGC *data_gc = gdk_gc_new( M_BMPDATA->m_pixmap );
cf7a7e13 480
01111366 481 gdk_draw_image( M_BMPDATA->m_pixmap, data_gc, data_image, 0, 0, 0, 0, width, height );
cf7a7e13 482
01111366
RR
483 gdk_image_destroy( data_image );
484 gdk_gc_unref( data_gc );
cf7a7e13 485
01111366 486 // Blit mask
cf7a7e13 487
01111366
RR
488 if (image.HasMask())
489 {
490 GdkGC *mask_gc = gdk_gc_new( M_BMPDATA->m_mask->m_bitmap );
cf7a7e13 491
01111366 492 gdk_draw_image( M_BMPDATA->m_mask->m_bitmap, mask_gc, mask_image, 0, 0, 0, 0, width, height );
cf7a7e13 493
01111366
RR
494 gdk_image_destroy( mask_image );
495 gdk_gc_unref( mask_gc );
496 }
497
ff7b1510 498}
219f895a 499
01111366 500wxImage wxBitmap::ConvertToImage() const
219f895a 501{
01111366
RR
502 wxImage image;
503
e55ad60e
RR
504 if (!Ok())
505 {
506 wxFAIL_MSG( "invalid bitmap" );
01111366 507 return image;
e55ad60e 508 }
219f895a 509
01111366 510 GdkImage *gdk_image = gdk_image_get( M_BMPDATA->m_pixmap, 0, 0, M_BMPDATA->m_width, M_BMPDATA->m_height );
cf7a7e13 511
01111366 512 if (!gdk_image) return image;
cf7a7e13 513
01111366
RR
514 image.Create( M_BMPDATA->m_width, M_BMPDATA->m_height );
515 char unsigned *data = image.GetData();
cf7a7e13 516
01111366
RR
517 int bpp = gdk_image->bpp;
518 GdkColormap *cmap = gtk_widget_get_default_colormap();
cf7a7e13 519
01111366
RR
520 long pos = 0;
521 for (int j = 0; j < M_BMPDATA->m_height; j++)
219f895a 522 {
01111366
RR
523 for (int i = 0; i < M_BMPDATA->m_width; i++)
524 {
525 int pixel = gdk_image_get_pixel( gdk_image, i, j );
526 if (bpp <= 8)
527 {
528 data[pos] = cmap->colors[pixel].red >> 8;
529 data[pos+1] = cmap->colors[pixel].green >> 8;
530 data[pos+2] = cmap->colors[pixel].blue >> 8;
531 } else if (bpp == 15)
532 {
533 data[pos] = (pixel >> 7) & 0xf8;
534 data[pos+1] = (pixel >> 3) & 0xf8;
535 data[pos+2] = (pixel << 3) & 0xf8;
536 } else if (bpp == 16)
537 {
538 data[pos] = (pixel >> 8) & 0xf8;
539 data[pos+1] = (pixel >> 3) & 0xfc;
540 data[pos+2] = (pixel << 3) & 0xf8;
541 } else
542 {
543 data[pos] = (pixel >> 16) & 0xff;
544 data[pos+1] = (pixel >> 8) & 0xff;
545 data[pos+2] = pixel & 0xff;
546 }
547
548 pos += 3;
549 }
ff7b1510 550 }
219f895a 551
01111366 552 gdk_image_destroy( gdk_image );
cf7a7e13 553
01111366 554 return image;
ff7b1510 555}
219f895a
RR
556
557
01111366 558