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