]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/bitmap.cpp
no message
[wxWidgets.git] / src / gtk1 / bitmap.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: bitmap.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Created: 01/02/97
6 // RCS-ID: $Id$
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"
16 #include "wx/icon.h"
17 #include "gdk/gdkprivate.h"
18
19 #ifdef wxUSE_GDK_IMLIB
20
21 #include "../gdk_imlib/gdk_imlib.h"
22 #include "gdk/gdkx.h" // GDK_DISPLAY
23 #include <X11/Xlib.h>
24 #include <X11/Xutil.h>
25
26 #endif
27
28 //-----------------------------------------------------------------------------
29 // wxMask
30 //-----------------------------------------------------------------------------
31
32 IMPLEMENT_DYNAMIC_CLASS(wxMask,wxObject)
33
34 wxMask::wxMask(void)
35 {
36 m_bitmap = (GdkBitmap *) NULL;
37 }
38
39 wxMask::wxMask( const wxBitmap& WXUNUSED(bitmap), const wxColour& WXUNUSED(colour) )
40 {
41 }
42
43 wxMask::wxMask( const wxBitmap& WXUNUSED(bitmap), int WXUNUSED(paletteIndex) )
44 {
45 }
46
47 wxMask::wxMask( const wxBitmap& WXUNUSED(bitmap) )
48 {
49 }
50
51 wxMask::~wxMask(void)
52 {
53 #ifdef wxUSE_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
58 }
59
60 GdkBitmap *wxMask::GetBitmap(void) const
61 {
62 return m_bitmap;
63 }
64
65 //-----------------------------------------------------------------------------
66 // wxBitmap
67 //-----------------------------------------------------------------------------
68
69 // CMB 20/5/98: added m_bitmap for GdkBitmaps
70 class wxBitmapRefData: public wxObjectRefData
71 {
72 public:
73
74 wxBitmapRefData(void);
75 ~wxBitmapRefData(void);
76
77 GdkPixmap *m_pixmap;
78 GdkBitmap *m_bitmap;
79 wxMask *m_mask;
80 int m_width;
81 int m_height;
82 int m_bpp;
83 #ifdef wxUSE_GDK_IMLIB
84 GdkImlibImage *m_image;
85 #endif
86 wxPalette *m_palette;
87 };
88
89 wxBitmapRefData::wxBitmapRefData(void)
90 {
91 m_pixmap = (GdkPixmap *) NULL;
92 m_bitmap = (GdkBitmap *) NULL;
93 m_mask = (wxMask *) NULL;
94 m_width = 0;
95 m_height = 0;
96 m_bpp = 0;
97 m_palette = (wxPalette *) NULL;
98 #ifdef wxUSE_GDK_IMLIB
99 m_image = (GdkImlibImage *) NULL;
100 #endif
101 }
102
103 wxBitmapRefData::~wxBitmapRefData(void)
104 {
105 #ifdef wxUSE_GDK_IMLIB
106 if (m_pixmap) gdk_imlib_free_pixmap( m_pixmap );
107 if (m_image) gdk_imlib_kill_image( m_image );
108 #else
109 if (m_pixmap) gdk_pixmap_unref( m_pixmap );
110 #endif
111 if (m_bitmap) gdk_bitmap_unref( m_bitmap );
112 if (m_mask) delete m_mask;
113 if (m_palette) delete m_palette;
114 }
115
116 //-----------------------------------------------------------------------------
117
118 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
119
120 IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxGDIObject)
121
122 wxBitmap::wxBitmap(void)
123 {
124 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
125 }
126
127 wxBitmap::wxBitmap( int width, int height, int depth )
128 {
129 m_refData = new wxBitmapRefData();
130 M_BMPDATA->m_mask = (wxMask *) NULL;
131 M_BMPDATA->m_pixmap =
132 gdk_pixmap_new( (GdkWindow*) &gdk_root_parent, width, height, depth );
133 M_BMPDATA->m_width = width;
134 M_BMPDATA->m_height = height;
135 M_BMPDATA->m_bpp = depth;
136
137 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
138 }
139
140 wxBitmap::wxBitmap( char **bits )
141 {
142 m_refData = new wxBitmapRefData();
143
144 #ifndef wxUSE_GDK_IMLIB
145
146 GdkBitmap *mask = NULL;
147
148 M_BMPDATA->m_pixmap =
149 gdk_pixmap_create_from_xpm_d( (GdkWindow*) &gdk_root_parent, &mask, NULL, (gchar **) bits );
150
151 if (mask)
152 {
153 M_BMPDATA->m_mask = new wxMask();
154 M_BMPDATA->m_mask->m_bitmap = mask;
155 }
156
157 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
158
159 #else
160
161 M_BMPDATA->m_image = gdk_imlib_create_image_from_xpm_data( bits );
162 Render();
163
164 #endif
165
166 M_BMPDATA->m_bpp = 24; // ?
167
168 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
169 }
170
171 wxBitmap::wxBitmap( const wxBitmap& bmp )
172 {
173 Ref( bmp );
174
175 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
176 }
177
178 wxBitmap::wxBitmap( const wxBitmap* bmp )
179 {
180 if (bmp) Ref( *bmp );
181
182 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
183 }
184
185 wxBitmap::wxBitmap( const wxString &filename, int type )
186 {
187 LoadFile( filename, type );
188
189 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
190 }
191
192 // CMB 15/5/98: add constructor for xbm bitmaps
193 wxBitmap::wxBitmap( const char bits[], int width, int height, int WXUNUSED(depth))
194 {
195 m_refData = new wxBitmapRefData();
196
197 M_BMPDATA->m_mask = (wxMask *) NULL;
198 M_BMPDATA->m_bitmap =
199 gdk_bitmap_create_from_data( (GdkWindow*) &gdk_root_parent, (gchar *) bits, width, height );
200 M_BMPDATA->m_width = width;
201 M_BMPDATA->m_height = height;
202 M_BMPDATA->m_bpp = 1;
203
204 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
205 }
206
207 wxBitmap::~wxBitmap(void)
208 {
209 if (wxTheBitmapList) wxTheBitmapList->DeleteObject(this);
210 }
211
212 wxBitmap& wxBitmap::operator = ( const wxBitmap& bmp )
213 {
214 if (*this == bmp) return (*this);
215 Ref( bmp );
216 return *this;
217 }
218
219 bool wxBitmap::operator == ( const wxBitmap& bmp )
220 {
221 return m_refData == bmp.m_refData;
222 }
223
224 bool wxBitmap::operator != ( const wxBitmap& bmp )
225 {
226 return m_refData != bmp.m_refData;
227 }
228
229 bool wxBitmap::Ok(void) const
230 {
231 return (m_refData != NULL);
232 }
233
234 int wxBitmap::GetHeight(void) const
235 {
236 if (!Ok())
237 {
238 wxFAIL_MSG( "invalid bitmap" );
239 return -1;
240 }
241
242 return M_BMPDATA->m_height;
243 }
244
245 int wxBitmap::GetWidth(void) const
246 {
247 if (!Ok())
248 {
249 wxFAIL_MSG( "invalid bitmap" );
250 return -1;
251 }
252
253 return M_BMPDATA->m_width;
254 }
255
256 int wxBitmap::GetDepth(void) const
257 {
258 if (!Ok())
259 {
260 wxFAIL_MSG( "invalid bitmap" );
261 return -1;
262 }
263
264 return M_BMPDATA->m_bpp;
265 }
266
267 void wxBitmap::SetHeight( int height )
268 {
269 if (!Ok()) return;
270
271 wxFAIL_MSG( "wxBitmap::SetHeight not implemented" );
272
273 M_BMPDATA->m_height = height;
274 }
275
276 void wxBitmap::SetWidth( int width )
277 {
278 if (!Ok()) return;
279
280 wxFAIL_MSG( "wxBitmap::SetWidth not implemented" );
281
282 M_BMPDATA->m_width = width;
283 }
284
285 void wxBitmap::SetDepth( int depth )
286 {
287 if (!Ok()) return;
288
289 wxFAIL_MSG( "wxBitmap::SetDepth not implemented" );
290
291 M_BMPDATA->m_bpp = depth;
292 }
293
294 wxMask *wxBitmap::GetMask(void) const
295 {
296 if (!Ok())
297 {
298 wxFAIL_MSG( "invalid bitmap" );
299 return (wxMask *) NULL;
300 }
301
302 return M_BMPDATA->m_mask;
303 }
304
305 void wxBitmap::SetMask( wxMask *mask )
306 {
307 if (!Ok())
308 {
309 wxFAIL_MSG( "invalid bitmap" );
310 return;
311 }
312
313 if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask;
314
315 M_BMPDATA->m_mask = mask;
316 }
317
318 void wxBitmap::Resize( int height, int width )
319 {
320 if (!Ok())
321 {
322 wxFAIL_MSG( "invalid bitmap" );
323 return;
324 }
325
326 #ifdef wxUSE_GDK_IMLIB
327
328 if (M_BMPDATA->m_bitmap)
329 {
330 wxFAIL_MSG( "wxBitmap::Resize not supported for mono-bitmaps" );
331 return;
332 }
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
347 #else
348
349 wxFAIL_MSG( "wxBitmap::Resize not implemented without GdkImlib" );
350
351 #endif
352 }
353
354 bool wxBitmap::SaveFile( const wxString &name, int WXUNUSED(type),
355 wxPalette *WXUNUSED(palette) )
356 {
357 if (!Ok())
358 {
359 wxFAIL_MSG( "invalid bitmap" );
360 return FALSE;
361 }
362
363 #ifdef wxUSE_GDK_IMLIB
364
365 if (M_BMPDATA->m_bitmap)
366 {
367 wxFAIL_MSG( "wxBitmap::SaveFile not supported for mono-bitmaps" );
368 return FALSE;
369 }
370
371 if (!M_BMPDATA->m_image) RecreateImage();
372
373 return gdk_imlib_save_image( M_BMPDATA->m_image, WXSTRINGCAST name, (GdkImlibSaveInfo *) NULL );
374
375 #else
376
377 wxFAIL_MSG( "wxBitmap::SaveFile not implemented without GdkImlib" );
378
379 #endif
380
381 return FALSE;
382 }
383
384 bool wxBitmap::LoadFile( const wxString &name, int WXUNUSED(type) )
385 {
386 #ifdef wxUSE_GDK_IMLIB
387
388 UnRef();
389 m_refData = new wxBitmapRefData();
390
391 M_BMPDATA->m_image = gdk_imlib_load_image( WXSTRINGCAST name );
392
393 if (!M_BMPDATA->m_image)
394 {
395 UnRef();
396 return FALSE;
397 }
398
399 Render();
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;
405
406 #else
407
408 wxFAIL_MSG( "wxBitmap::LoadFile not implemented without GdkImlib" );
409
410 #endif
411
412 return FALSE;
413 }
414
415 wxPalette *wxBitmap::GetPalette(void) const
416 {
417 if (!Ok()) return (wxPalette *) NULL;
418 return M_BMPDATA->m_palette;
419 }
420
421 GdkPixmap *wxBitmap::GetPixmap(void) const
422 {
423 if (!Ok())
424 {
425 wxFAIL_MSG( "invalid bitmap" );
426 return (GdkPixmap *) NULL;
427 }
428
429 // if (!M_BMPDATA->m_image) RecreateImage();
430
431 return M_BMPDATA->m_pixmap;
432 }
433
434 GdkBitmap *wxBitmap::GetBitmap(void) const
435 {
436 if (!Ok())
437 {
438 wxFAIL_MSG( "invalid bitmap" );
439 return (GdkBitmap *) NULL;
440 }
441
442 return M_BMPDATA->m_bitmap;
443 }
444
445 void wxBitmap::DestroyImage(void)
446 {
447 if (!Ok())
448 {
449 wxFAIL_MSG( "invalid bitmap" );
450 return;
451 }
452
453 if (M_BMPDATA->m_image)
454 {
455 gdk_imlib_destroy_image( M_BMPDATA->m_image );
456 M_BMPDATA->m_image = (GdkImlibImage *) NULL;
457 }
458 }
459
460 void wxBitmap::RecreateImage(void)
461 {
462 if (!Ok())
463 {
464 wxFAIL_MSG( "invalid bitmap" );
465 return;
466 }
467
468 #ifdef wxUSE_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
513 }
514
515 void wxBitmap::Render(void)
516 {
517 if (!Ok())
518 {
519 wxFAIL_MSG( "invalid bitmap" );
520 return;
521 }
522
523 #ifdef wxUSE_GDK_IMLIB
524
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
538 gdk_imlib_render( M_BMPDATA->m_image, M_BMPDATA->m_image->rgb_width, M_BMPDATA->m_image->rgb_height );
539 M_BMPDATA->m_width = M_BMPDATA->m_image->rgb_width;
540 M_BMPDATA->m_height = M_BMPDATA->m_image->rgb_height;
541 M_BMPDATA->m_pixmap = gdk_imlib_move_image( M_BMPDATA->m_image );
542
543 wxCHECK_RET( M_BMPDATA->m_pixmap != NULL, "pixmap rendering failed" )
544
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;
550 }
551
552 #else
553
554 wxFAIL_MSG( "wxBitmap::Render not implemented without GdkImlib" );
555
556 #endif
557 }
558
559