]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/bitmap.cpp
Fixed Get...String functions.
[wxWidgets.git] / src / gtk1 / 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
RR
231 wxASSERT_MSG( m_refData != NULL, "invalid bitmap" );
232 return (m_refData != NULL);
ff7b1510 233}
c801d85f
KB
234
235int wxBitmap::GetHeight(void) const
236{
237 if (!Ok()) return 0;
238 return M_BMPDATA->m_height;
ff7b1510 239}
c801d85f
KB
240
241int wxBitmap::GetWidth(void) const
242{
243 if (!Ok()) return 0;
244 return M_BMPDATA->m_width;
ff7b1510 245}
c801d85f
KB
246
247int wxBitmap::GetDepth(void) const
248{
249 if (!Ok()) return 0;
250 return M_BMPDATA->m_bpp;
ff7b1510 251}
c801d85f 252
debe6624 253void wxBitmap::SetHeight( int height )
c801d85f
KB
254{
255 if (!Ok()) return;
cf7a7e13
RR
256
257 wxFAIL_MSG( "wxBitmap::SetHeight not implemented" );
258
c801d85f 259 M_BMPDATA->m_height = height;
ff7b1510 260}
c801d85f 261
debe6624 262void wxBitmap::SetWidth( int width )
c801d85f
KB
263{
264 if (!Ok()) return;
cf7a7e13
RR
265
266 wxFAIL_MSG( "wxBitmap::SetWidth not implemented" );
267
c801d85f 268 M_BMPDATA->m_width = width;
ff7b1510 269}
c801d85f 270
debe6624 271void wxBitmap::SetDepth( int depth )
c801d85f
KB
272{
273 if (!Ok()) return;
cf7a7e13
RR
274
275 wxFAIL_MSG( "wxBitmap::SetDepth not implemented" );
276
c801d85f 277 M_BMPDATA->m_bpp = depth;
ff7b1510 278}
c801d85f
KB
279
280wxMask *wxBitmap::GetMask(void) const
281{
c67daf87 282 if (!Ok()) return (wxMask *) NULL;
219f895a 283
c801d85f 284 return M_BMPDATA->m_mask;
ff7b1510 285}
c801d85f
KB
286
287void wxBitmap::SetMask( wxMask *mask )
288{
289 if (!Ok()) return;
219f895a 290
c801d85f 291 if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask;
cf7a7e13 292
c801d85f 293 M_BMPDATA->m_mask = mask;
ff7b1510 294}
c801d85f 295
219f895a
RR
296void wxBitmap::Resize( int height, int width )
297{
298 if (!Ok()) return;
299
219f895a
RR
300#ifdef USE_GDK_IMLIB
301
302 if (M_BMPDATA->m_bitmap) return; // not supported for bitmaps
303
304 if (!M_BMPDATA->m_image) RecreateImage();
305
306 if (M_BMPDATA->m_pixmap) gdk_imlib_free_pixmap( M_BMPDATA->m_pixmap );
307 if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask;
308
309 GdkImlibImage* image = gdk_imlib_clone_scaled_image( M_BMPDATA->m_image, height, width );
310 gdk_imlib_destroy_image( M_BMPDATA->m_image );
311 M_BMPDATA->m_image = image;
312 M_BMPDATA->m_height = height;
313 M_BMPDATA->m_width = width;
314
315 Render();
316
cf7a7e13
RR
317#else
318
319 wxFAIL_MSG( "wxBitmap::Resize not implemented without GdkImlib" );
320
219f895a 321#endif
ff7b1510 322}
219f895a
RR
323
324bool wxBitmap::SaveFile( const wxString &name, int WXUNUSED(type),
c801d85f
KB
325 wxPalette *WXUNUSED(palette) )
326{
219f895a
RR
327#ifdef USE_GDK_IMLIB
328
329 if (!Ok()) return FALSE;
330
331 if (!M_BMPDATA->m_image) RecreateImage();
332
c67daf87 333 return gdk_imlib_save_image( M_BMPDATA->m_image, WXSTRINGCAST name, (GdkImlibSaveInfo *) NULL );
219f895a 334
cf7a7e13
RR
335#else
336
337 wxFAIL_MSG( "wxBitmap::SaveFile not implemented without GdkImlib" );
338
219f895a
RR
339#endif
340
c801d85f 341 return FALSE;
ff7b1510 342}
c801d85f 343
debe6624 344bool wxBitmap::LoadFile( const wxString &name, int WXUNUSED(type) )
c801d85f
KB
345{
346#ifdef USE_GDK_IMLIB
347
348 UnRef();
349 m_refData = new wxBitmapRefData();
c801d85f 350
219f895a 351 M_BMPDATA->m_image = gdk_imlib_load_image( WXSTRINGCAST name );
c801d85f 352
219f895a 353 if (!M_BMPDATA->m_image)
c801d85f
KB
354 {
355 UnRef();
356 return FALSE;
ff7b1510 357 }
c801d85f 358
219f895a 359 Render();
c801d85f
KB
360
361 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
362 M_BMPDATA->m_bpp = 24; // ?
363
364 return TRUE;
cf7a7e13
RR
365
366#else
367
368 wxFAIL_MSG( "wxBitmap::LoadFile not implemented without GdkImlib" );
369
c801d85f
KB
370#endif
371
372 return FALSE;
ff7b1510 373}
c801d85f
KB
374
375wxPalette *wxBitmap::GetPalette(void) const
376{
c67daf87 377 if (!Ok()) return (wxPalette *) NULL;
c801d85f 378 return M_BMPDATA->m_palette;
ff7b1510 379}
c801d85f
KB
380
381GdkPixmap *wxBitmap::GetPixmap(void) const
382{
c67daf87 383 if (!Ok()) return (GdkPixmap *) NULL;
cf7a7e13
RR
384
385// if (!M_BMPDATA->m_image) RecreateImage();
386
c801d85f 387 return M_BMPDATA->m_pixmap;
ff7b1510 388}
c801d85f 389
6f65e337
JS
390GdkBitmap *wxBitmap::GetBitmap(void) const
391{
c67daf87 392 if (!Ok()) return (GdkBitmap *) NULL;
219f895a 393
6f65e337 394 return M_BMPDATA->m_bitmap;
ff7b1510 395}
6f65e337 396
219f895a
RR
397void wxBitmap::DestroyImage(void)
398{
399 if (!Ok()) return;
400
401 if (M_BMPDATA->m_image)
402 {
403 gdk_imlib_destroy_image( M_BMPDATA->m_image );
c67daf87 404 M_BMPDATA->m_image = (GdkImlibImage *) NULL;
ff7b1510
RR
405 }
406}
219f895a
RR
407
408void wxBitmap::RecreateImage(void)
409{
cf7a7e13
RR
410 if (!Ok()) return;
411
412#ifdef USE_GDK_IMLIB
413
414 DestroyImage();
415
416 wxCHECK_RET( M_BMPDATA->m_pixmap != NULL, "invalid bitmap" );
417
418 long size = (long)(M_BMPDATA->m_width)*(long)(M_BMPDATA->m_height)*(long)3;
419 unsigned char *data = new unsigned char[size];
420 for (long i = 0; i < size; i++) data[i] = 100;
421
422 GdkImage *image = gdk_image_get( M_BMPDATA->m_pixmap, 0, 0, M_BMPDATA->m_width, M_BMPDATA->m_height );
423
424 long pos = 0;
425 for (int j = 0; j < M_BMPDATA->m_height; j++)
426 {
427 for (int i = 0; i < M_BMPDATA->m_width; i++)
428 {
429 XColor xcol;
430 xcol.pixel = gdk_image_get_pixel( image, i, j );
431 Colormap cm = ((GdkColormapPrivate*)gdk_imlib_get_colormap())->xcolormap;
432 XQueryColor( gdk_display, cm, &xcol );
433
434 data[pos] = xcol.red;
435 data[pos+1] = xcol.green;
436 data[pos+2] = xcol.blue;
437 pos += 3;
438 }
439 }
440
441 wxCHECK_RET( M_BMPDATA->m_pixmap != NULL, "invalid bitmap" );
442
443 M_BMPDATA->m_image = gdk_imlib_create_image_from_data(
444 data, (unsigned char*)NULL, M_BMPDATA->m_width, M_BMPDATA->m_height );
445
446 delete[] data;
447
448 gdk_image_destroy( image );
449
450 Render();
451
452#else
453
454 wxFAIL_MSG( "wxBitmap::RecreateImage not implemented without GdkImlib" );
455
456#endif
ff7b1510 457}
219f895a
RR
458
459void wxBitmap::Render(void)
460{
461 if (!Ok()) return;
462
463#ifdef USE_GDK_IMLIB
464
cf7a7e13
RR
465 if (!M_BMPDATA->m_image) RecreateImage();
466
467 if (M_BMPDATA->m_pixmap)
468 {
469 gdk_imlib_free_pixmap( M_BMPDATA->m_pixmap );
470 M_BMPDATA->m_pixmap = (GdkPixmap*) NULL;
471 }
472 if (M_BMPDATA->m_mask)
473 {
474 delete M_BMPDATA->m_mask;
475 M_BMPDATA->m_mask = (wxMask*) NULL;
476 }
477
219f895a 478 gdk_imlib_render( M_BMPDATA->m_image, M_BMPDATA->m_image->rgb_width, M_BMPDATA->m_image->rgb_height );
0180d5da
RR
479 M_BMPDATA->m_width = M_BMPDATA->m_image->rgb_width;
480 M_BMPDATA->m_height = M_BMPDATA->m_image->rgb_height;
219f895a 481 M_BMPDATA->m_pixmap = gdk_imlib_move_image( M_BMPDATA->m_image );
cf7a7e13
RR
482
483 wxCHECK_RET( M_BMPDATA->m_pixmap != NULL, "pixmap rendering failed" )
484
219f895a
RR
485 GdkBitmap *mask = gdk_imlib_move_mask( M_BMPDATA->m_image );
486 if (mask)
487 {
488 M_BMPDATA->m_mask = new wxMask();
489 M_BMPDATA->m_mask->m_bitmap = mask;
ff7b1510 490 }
219f895a 491
cf7a7e13
RR
492#else
493
494 wxFAIL_MSG( "wxBitmap::Render not implemented without GdkImlib" );
495
219f895a 496#endif
ff7b1510 497}
219f895a
RR
498
499