]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/bitmap.cpp
*** empty log message ***
[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 bool wxBitmap::SaveFile( const wxString &name, int type, wxPalette *WXUNUSED(palette) )
370 {
371 wxCHECK_MSG( Ok(), FALSE, wxT("invalid bitmap") );
372
373 // Try to save the bitmap via wxImage handlers:
374 {
375 wxImage image( *this );
376 if (image.Ok()) return image.SaveFile( name, type );
377 }
378
379 return FALSE;
380 }
381
382 bool wxBitmap::LoadFile( const wxString &name, int type )
383 {
384 UnRef();
385
386 if (!wxFileExists(name)) return FALSE;
387
388 if (type == wxBITMAP_TYPE_XPM)
389 {
390 m_refData = new wxBitmapRefData();
391
392 GdkBitmap *mask = (GdkBitmap*) NULL;
393 GdkWindow *parent = (GdkWindow*) &gdk_root_parent;
394
395 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm( parent, &mask, NULL, name.fn_str() );
396
397 if (mask)
398 {
399 M_BMPDATA->m_mask = new wxMask();
400 M_BMPDATA->m_mask->m_bitmap = mask;
401 }
402
403 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
404 M_BMPDATA->m_bpp = gdk_window_get_visual( parent )->depth;
405 }
406 else // try if wxImage can load it
407 {
408 wxImage image;
409 if (!image.LoadFile( name, type )) return FALSE;
410 if (image.Ok()) *this = image.ConvertToBitmap();
411 else return FALSE;
412 }
413
414 return TRUE;
415 }
416
417 wxPalette *wxBitmap::GetPalette() const
418 {
419 if (!Ok()) return (wxPalette *) NULL;
420
421 return M_BMPDATA->m_palette;
422 }
423
424 void wxBitmap::SetHeight( int height )
425 {
426 if (!m_refData) m_refData = new wxBitmapRefData();
427
428 M_BMPDATA->m_height = height;
429 }
430
431 void wxBitmap::SetWidth( int width )
432 {
433 if (!m_refData) m_refData = new wxBitmapRefData();
434
435 M_BMPDATA->m_width = width;
436 }
437
438 void wxBitmap::SetDepth( int depth )
439 {
440 if (!m_refData) m_refData = new wxBitmapRefData();
441
442 M_BMPDATA->m_bpp = depth;
443 }
444
445 void wxBitmap::SetPixmap( GdkPixmap *pixmap )
446 {
447 if (!m_refData) m_refData = new wxBitmapRefData();
448
449 M_BMPDATA->m_pixmap = pixmap;
450 }
451
452 GdkPixmap *wxBitmap::GetPixmap() const
453 {
454 wxCHECK_MSG( Ok(), (GdkPixmap *) NULL, wxT("invalid bitmap") );
455
456 return M_BMPDATA->m_pixmap;
457 }
458
459 GdkBitmap *wxBitmap::GetBitmap() const
460 {
461 wxCHECK_MSG( Ok(), (GdkBitmap *) NULL, wxT("invalid bitmap") );
462
463 return M_BMPDATA->m_bitmap;
464 }