Added wxBitmap::GetSubBitmap()
[wxWidgets.git] / src / gtk1 / bitmap.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: bitmap.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // RCS-ID: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifdef __GNUG__
11 #pragma implementation "bitmap.h"
12 #endif
13
14 #include "wx/bitmap.h"
15 #include "wx/icon.h"
16 #include "wx/filefn.h"
17 #include "wx/image.h"
18
19 #include <gdk/gdk.h>
20 #include <gdk/gdkprivate.h>
21 #include <gdk/gdkx.h>
22
23 //-----------------------------------------------------------------------------
24 // wxMask
25 //-----------------------------------------------------------------------------
26
27 IMPLEMENT_DYNAMIC_CLASS(wxMask,wxObject)
28
29 wxMask::wxMask()
30 {
31 m_bitmap = (GdkBitmap *) NULL;
32 }
33
34 wxMask::wxMask( const wxBitmap& bitmap, const wxColour& colour )
35 {
36 m_bitmap = (GdkBitmap *) NULL;
37 Create( bitmap, colour );
38 }
39
40 wxMask::wxMask( const wxBitmap& bitmap, int paletteIndex )
41 {
42 m_bitmap = (GdkBitmap *) NULL;
43 Create( bitmap, paletteIndex );
44 }
45
46 wxMask::wxMask( const wxBitmap& bitmap )
47 {
48 m_bitmap = (GdkBitmap *) NULL;
49 Create( bitmap );
50 }
51
52 wxMask::~wxMask()
53 {
54 if (m_bitmap)
55 gdk_bitmap_unref( m_bitmap );
56 }
57
58 bool wxMask::Create( const wxBitmap& bitmap,
59 const wxColour& colour )
60 {
61 if (m_bitmap)
62 {
63 gdk_bitmap_unref( m_bitmap );
64 m_bitmap = (GdkBitmap*) NULL;
65 }
66
67 wxImage image( bitmap );
68 if (!image.Ok()) return FALSE;
69
70 GdkVisual *visual = gdk_visual_get_system();
71
72 GdkImage *mask_image = gdk_image_new( GDK_IMAGE_FASTEST, visual, image.GetWidth(), image.GetHeight() );
73 if (!mask_image) return FALSE;
74
75 GdkWindow *parent = (GdkWindow*) &gdk_root_parent;
76 m_bitmap = gdk_pixmap_new( parent, image.GetWidth(), image.GetHeight(), 1 );
77
78
79 unsigned char *data = image.GetData();
80 int index = 0;
81
82 unsigned char red = colour.Red();
83 unsigned char green = colour.Green();
84 unsigned char blue = colour.Blue();
85
86 int bpp = visual->depth;
87 if ((bpp == 16) && (visual->red_mask != 0xf800)) bpp = 15;
88 if (bpp == 15)
89 {
90 red = red & 0xf8;
91 blue = blue & 0xf8;
92 green = green & 0xf8;
93 }
94 if (bpp == 16)
95 {
96 red = red & 0xf8;
97 blue = blue & 0xfc;
98 green = green & 0xf8;
99 }
100
101 for (int j = 0; j < image.GetHeight(); j++)
102 for (int i = 0; i < image.GetWidth(); i++)
103 {
104 if ((data[index] == red) &&
105 (data[index+1] == green) &&
106 (data[index+2] == blue))
107 {
108 gdk_image_put_pixel( mask_image, i, j, 1 );
109 }
110 else
111 {
112 gdk_image_put_pixel( mask_image, i, j, 1 );
113 }
114 index += 3;
115 }
116
117 GdkGC *mask_gc = gdk_gc_new( m_bitmap );
118
119 gdk_draw_image( m_bitmap, mask_gc, mask_image, 0, 0, 0, 0, image.GetWidth(), image.GetHeight() );
120
121 gdk_gc_unref( mask_gc );
122 gdk_image_destroy( mask_image );
123
124 return FALSE;
125 }
126
127 bool wxMask::Create( const wxBitmap& WXUNUSED(bitmap),
128 int WXUNUSED(paletteIndex) )
129 {
130 if (m_bitmap)
131 {
132 gdk_bitmap_unref( m_bitmap );
133 m_bitmap = (GdkBitmap*) NULL;
134 }
135
136 wxFAIL_MSG( wxT("not implemented") );
137
138 return FALSE;
139 }
140
141 bool wxMask::Create( const wxBitmap& bitmap )
142 {
143 if (m_bitmap)
144 {
145 gdk_bitmap_unref( m_bitmap );
146 m_bitmap = (GdkBitmap*) NULL;
147 }
148
149 if (!bitmap.Ok()) return FALSE;
150
151 wxCHECK_MSG( bitmap.GetBitmap(), FALSE, wxT("Cannot create mask from colour bitmap") );
152
153 m_bitmap = gdk_pixmap_new( (GdkWindow*) &gdk_root_parent, bitmap.GetWidth(), bitmap.GetHeight(), 1 );
154
155 if (!m_bitmap) return FALSE;
156
157 GdkGC *gc = gdk_gc_new( m_bitmap );
158
159 gdk_draw_bitmap( m_bitmap, gc, bitmap.GetBitmap(), 0, 0, 0, 0, bitmap.GetWidth(), bitmap.GetHeight() );
160
161 gdk_gc_unref( gc );
162
163 return TRUE;
164 }
165
166 GdkBitmap *wxMask::GetBitmap() const
167 {
168 return m_bitmap;
169 }
170
171 //-----------------------------------------------------------------------------
172 // wxBitmap
173 //-----------------------------------------------------------------------------
174
175 class wxBitmapRefData: public wxObjectRefData
176 {
177 public:
178 wxBitmapRefData();
179 ~wxBitmapRefData();
180
181 GdkPixmap *m_pixmap;
182 GdkBitmap *m_bitmap;
183 wxMask *m_mask;
184 int m_width;
185 int m_height;
186 int m_bpp;
187 wxPalette *m_palette;
188 };
189
190 wxBitmapRefData::wxBitmapRefData()
191 {
192 m_pixmap = (GdkPixmap *) NULL;
193 m_bitmap = (GdkBitmap *) NULL;
194 m_mask = (wxMask *) NULL;
195 m_width = 0;
196 m_height = 0;
197 m_bpp = 0;
198 m_palette = (wxPalette *) NULL;
199 }
200
201 wxBitmapRefData::~wxBitmapRefData()
202 {
203 if (m_pixmap) gdk_pixmap_unref( m_pixmap );
204 if (m_bitmap) gdk_bitmap_unref( m_bitmap );
205 if (m_mask) delete m_mask;
206 if (m_palette) delete m_palette;
207 }
208
209 //-----------------------------------------------------------------------------
210
211 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
212
213 IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxGDIObject)
214
215 wxBitmap::wxBitmap()
216 {
217 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
218 }
219
220 wxBitmap::wxBitmap( int width, int height, int depth )
221 {
222 wxCHECK_RET( (width > 0) && (height > 0), wxT("invalid bitmap size") )
223
224 GdkWindow *parent = (GdkWindow*) &gdk_root_parent;
225 if (depth == -1) depth = gdk_window_get_visual( parent )->depth;
226
227 wxCHECK_RET( (depth == gdk_window_get_visual( parent )->depth) ||
228 (depth == 1), wxT("invalid bitmap depth") )
229
230 m_refData = new wxBitmapRefData();
231 M_BMPDATA->m_mask = (wxMask *) NULL;
232 M_BMPDATA->m_width = width;
233 M_BMPDATA->m_height = height;
234 if (depth == 1)
235 {
236 M_BMPDATA->m_bitmap = gdk_pixmap_new( parent, width, height, 1 );
237 M_BMPDATA->m_bpp = 1;
238 }
239 else
240 {
241 M_BMPDATA->m_pixmap = gdk_pixmap_new( parent, width, height, depth );
242 M_BMPDATA->m_bpp = gdk_window_get_visual( parent )->depth;
243 }
244
245 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
246 }
247
248 bool wxBitmap::CreateFromXpm( const char **bits )
249 {
250 wxCHECK_MSG( bits != NULL, FALSE, wxT("invalid bitmap data") )
251
252 m_refData = new wxBitmapRefData();
253
254 GdkBitmap *mask = (GdkBitmap*) NULL;
255 GdkWindow *parent = (GdkWindow*) &gdk_root_parent;
256
257 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm_d( parent, &mask, NULL, (gchar **) bits );
258
259 wxCHECK_MSG( M_BMPDATA->m_pixmap, FALSE, wxT("couldn't create pixmap") );
260
261 if (mask)
262 {
263 M_BMPDATA->m_mask = new wxMask();
264 M_BMPDATA->m_mask->m_bitmap = mask;
265 }
266
267 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
268
269 M_BMPDATA->m_bpp = gdk_window_get_visual( parent )->depth; // ?
270 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
271
272 return TRUE;
273 }
274
275 wxBitmap::wxBitmap( const wxBitmap& bmp )
276 {
277 Ref( bmp );
278
279 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
280 }
281
282 wxBitmap::wxBitmap( const wxString &filename, int type )
283 {
284 LoadFile( filename, type );
285
286 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
287 }
288
289 wxBitmap::wxBitmap( const char bits[], int width, int height, int WXUNUSED(depth))
290 {
291 m_refData = new wxBitmapRefData();
292
293 M_BMPDATA->m_mask = (wxMask *) NULL;
294 M_BMPDATA->m_bitmap =
295 gdk_bitmap_create_from_data( (GdkWindow*) &gdk_root_parent, (gchar *) bits, width, height );
296 M_BMPDATA->m_width = width;
297 M_BMPDATA->m_height = height;
298 M_BMPDATA->m_bpp = 1;
299
300 wxCHECK_RET( M_BMPDATA->m_bitmap, wxT("couldn't create bitmap") );
301
302 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
303 }
304
305 wxBitmap::~wxBitmap()
306 {
307 if (wxTheBitmapList) wxTheBitmapList->DeleteObject(this);
308 }
309
310 wxBitmap& wxBitmap::operator = ( const wxBitmap& bmp )
311 {
312 if (*this == bmp) return (*this);
313 Ref( bmp );
314 return *this;
315 }
316
317 bool wxBitmap::operator == ( const wxBitmap& bmp )
318 {
319 return m_refData == bmp.m_refData;
320 }
321
322 bool wxBitmap::operator != ( const wxBitmap& bmp )
323 {
324 return m_refData != bmp.m_refData;
325 }
326
327 bool wxBitmap::Ok() const
328 {
329 return (m_refData != NULL);
330 }
331
332 int wxBitmap::GetHeight() const
333 {
334 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
335
336 return M_BMPDATA->m_height;
337 }
338
339 int wxBitmap::GetWidth() const
340 {
341 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
342
343 return M_BMPDATA->m_width;
344 }
345
346 int wxBitmap::GetDepth() const
347 {
348 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
349
350 return M_BMPDATA->m_bpp;
351 }
352
353 wxMask *wxBitmap::GetMask() const
354 {
355 wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") );
356
357 return M_BMPDATA->m_mask;
358 }
359
360 void wxBitmap::SetMask( wxMask *mask )
361 {
362 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
363
364 if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask;
365
366 M_BMPDATA->m_mask = mask;
367 }
368
369 wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const
370 {
371 wxCHECK_MSG( Ok() &&
372 (rect.x >= 0) && (rect.y >= 0) &&
373 (rect.x+rect.width <= M_BMPDATA->m_width) && (rect.y+rect.height <= M_BMPDATA->m_height),
374 wxNullBitmap, wxT("invalid bitmap or bitmap region") );
375
376 wxBitmap ret( rect.width, rect.height, M_BMPDATA->m_bpp );
377 wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") );
378
379 if (ret.GetPixmap())
380 {
381 GdkGC *gc = gdk_gc_new( ret.GetPixmap() );
382 gdk_draw_pixmap( ret.GetPixmap(), gc, GetPixmap(), rect.x, rect.y, 0, 0, rect.width, rect.height );
383 gdk_gc_destroy( gc );
384 }
385 else
386 {
387 GdkGC *gc = gdk_gc_new( ret.GetBitmap() );
388 gdk_draw_bitmap( ret.GetBitmap(), gc, GetBitmap(), rect.x, rect.y, 0, 0, rect.width, rect.height );
389 gdk_gc_destroy( gc );
390 }
391
392 if (GetMask())
393 {
394 wxMask *mask = new wxMask;
395 GdkWindow *parent = (GdkWindow*) &gdk_root_parent;
396 mask->m_bitmap = gdk_pixmap_new( parent, rect.width, rect.height, 1 );
397
398 GdkGC *gc = gdk_gc_new( mask->m_bitmap );
399 gdk_draw_bitmap( mask->m_bitmap, gc, M_BMPDATA->m_mask->m_bitmap, 0, 0, rect.x, rect.y, rect.width, rect.height );
400 gdk_gc_destroy( gc );
401
402 ret.SetMask( mask );
403 }
404
405 return ret;
406 }
407
408 bool wxBitmap::SaveFile( const wxString &name, int type, wxPalette *WXUNUSED(palette) )
409 {
410 wxCHECK_MSG( Ok(), FALSE, wxT("invalid bitmap") );
411
412 // Try to save the bitmap via wxImage handlers:
413 {
414 wxImage image( *this );
415 if (image.Ok()) return image.SaveFile( name, type );
416 }
417
418 return FALSE;
419 }
420
421 bool wxBitmap::LoadFile( const wxString &name, int type )
422 {
423 UnRef();
424
425 if (!wxFileExists(name)) return FALSE;
426
427 if (type == wxBITMAP_TYPE_XPM)
428 {
429 m_refData = new wxBitmapRefData();
430
431 GdkBitmap *mask = (GdkBitmap*) NULL;
432 GdkWindow *parent = (GdkWindow*) &gdk_root_parent;
433
434 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm( parent, &mask, NULL, name.fn_str() );
435
436 if (mask)
437 {
438 M_BMPDATA->m_mask = new wxMask();
439 M_BMPDATA->m_mask->m_bitmap = mask;
440 }
441
442 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
443 M_BMPDATA->m_bpp = gdk_window_get_visual( parent )->depth;
444 }
445 else // try if wxImage can load it
446 {
447 wxImage image;
448 if (!image.LoadFile( name, type )) return FALSE;
449 if (image.Ok()) *this = image.ConvertToBitmap();
450 else return FALSE;
451 }
452
453 return TRUE;
454 }
455
456 wxPalette *wxBitmap::GetPalette() const
457 {
458 if (!Ok()) return (wxPalette *) NULL;
459
460 return M_BMPDATA->m_palette;
461 }
462
463 void wxBitmap::SetHeight( int height )
464 {
465 if (!m_refData) m_refData = new wxBitmapRefData();
466
467 M_BMPDATA->m_height = height;
468 }
469
470 void wxBitmap::SetWidth( int width )
471 {
472 if (!m_refData) m_refData = new wxBitmapRefData();
473
474 M_BMPDATA->m_width = width;
475 }
476
477 void wxBitmap::SetDepth( int depth )
478 {
479 if (!m_refData) m_refData = new wxBitmapRefData();
480
481 M_BMPDATA->m_bpp = depth;
482 }
483
484 void wxBitmap::SetPixmap( GdkPixmap *pixmap )
485 {
486 if (!m_refData) m_refData = new wxBitmapRefData();
487
488 M_BMPDATA->m_pixmap = pixmap;
489 }
490
491 GdkPixmap *wxBitmap::GetPixmap() const
492 {
493 wxCHECK_MSG( Ok(), (GdkPixmap *) NULL, wxT("invalid bitmap") );
494
495 return M_BMPDATA->m_pixmap;
496 }
497
498 GdkBitmap *wxBitmap::GetBitmap() const
499 {
500 wxCHECK_MSG( Ok(), (GdkBitmap *) NULL, wxT("invalid bitmap") );
501
502 return M_BMPDATA->m_bitmap;
503 }