]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/bitmap.cpp
b06f111e55edc921f33db0902a253c02aab38b6b
[wxWidgets.git] / src / gtk / 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 USE_GDK_IMLIB
20 #include "../gdk_imlib/gdk_imlib.h"
21 #endif
22
23 //-----------------------------------------------------------------------------
24 // wxMask
25 //-----------------------------------------------------------------------------
26
27 IMPLEMENT_DYNAMIC_CLASS(wxMask,wxObject)
28
29 wxMask::wxMask(void)
30 {
31 m_bitmap = (GdkBitmap *) NULL;
32 }
33
34 wxMask::wxMask( const wxBitmap& WXUNUSED(bitmap), const wxColour& WXUNUSED(colour) )
35 {
36 }
37
38 wxMask::wxMask( const wxBitmap& WXUNUSED(bitmap), int WXUNUSED(paletteIndex) )
39 {
40 }
41
42 wxMask::wxMask( const wxBitmap& WXUNUSED(bitmap) )
43 {
44 }
45
46 wxMask::~wxMask(void)
47 {
48 #ifdef USE_GDK_IMLIB
49 // do not delete the mask, gdk_imlib does it for you
50 #else
51 if (m_bitmap) gdk_bitmap_unref( m_bitmap );
52 #endif
53 }
54
55 GdkBitmap *wxMask::GetBitmap(void) const
56 {
57 return m_bitmap;
58 }
59
60 //-----------------------------------------------------------------------------
61 // wxBitmap
62 //-----------------------------------------------------------------------------
63
64 // CMB 20/5/98: added m_bitmap for GdkBitmaps
65 class wxBitmapRefData: public wxObjectRefData
66 {
67 public:
68
69 wxBitmapRefData(void);
70 ~wxBitmapRefData(void);
71
72 GdkPixmap *m_pixmap;
73 GdkBitmap *m_bitmap;
74 wxMask *m_mask;
75 int m_width;
76 int m_height;
77 int m_bpp;
78 #ifdef USE_GDK_IMLIB
79 GdkImlibImage *m_image;
80 #endif
81 wxPalette *m_palette;
82 };
83
84 wxBitmapRefData::wxBitmapRefData(void)
85 {
86 m_pixmap = (GdkPixmap *) NULL;
87 m_bitmap = (GdkBitmap *) NULL;
88 m_mask = (wxMask *) NULL;
89 m_width = 0;
90 m_height = 0;
91 m_bpp = 0;
92 m_palette = (wxPalette *) NULL;
93 #ifdef USE_GDK_IMLIB
94 m_image = (GdkImlibImage *) NULL;
95 #endif
96 }
97
98 wxBitmapRefData::~wxBitmapRefData(void)
99 {
100 #ifdef USE_GDK_IMLIB
101 if (m_pixmap) gdk_imlib_free_pixmap( m_pixmap );
102 if (m_image) gdk_imlib_kill_image( m_image );
103 #else
104 if (m_pixmap) gdk_pixmap_unref( m_pixmap );
105 #endif
106 if (m_bitmap) gdk_bitmap_unref( m_bitmap );
107 if (m_mask) delete m_mask;
108 if (m_palette) delete m_palette;
109 }
110
111 //-----------------------------------------------------------------------------
112
113 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
114
115 IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxGDIObject)
116
117 wxBitmap::wxBitmap(void)
118 {
119 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
120 }
121
122 wxBitmap::wxBitmap( int width, int height, int depth )
123 {
124 m_refData = new wxBitmapRefData();
125 M_BMPDATA->m_mask = (wxMask *) NULL;
126 M_BMPDATA->m_pixmap =
127 gdk_pixmap_new( (GdkWindow*) &gdk_root_parent, width, height, depth );
128 M_BMPDATA->m_width = width;
129 M_BMPDATA->m_height = height;
130 M_BMPDATA->m_bpp = depth;
131
132 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
133 }
134
135 wxBitmap::wxBitmap( char **bits )
136 {
137 m_refData = new wxBitmapRefData();
138
139 #ifndef USE_GDK_IMLIB
140
141 GdkBitmap *mask = NULL;
142
143 M_BMPDATA->m_pixmap =
144 gdk_pixmap_create_from_xpm_d( (GdkWindow*) &gdk_root_parent, &mask, NULL, (gchar **) bits );
145
146 if (mask)
147 {
148 M_BMPDATA->m_mask = new wxMask();
149 M_BMPDATA->m_mask->m_bitmap = mask;
150 }
151
152 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
153
154 #else
155
156 M_BMPDATA->m_image = gdk_imlib_create_image_from_xpm_data( bits );
157 Render();
158
159 #endif
160
161 M_BMPDATA->m_bpp = 24; // ?
162
163 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
164 }
165
166 wxBitmap::wxBitmap( const wxBitmap& bmp )
167 {
168 Ref( bmp );
169
170 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
171 }
172
173 wxBitmap::wxBitmap( const wxBitmap* bmp )
174 {
175 if (bmp) Ref( *bmp );
176
177 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
178 }
179
180 wxBitmap::wxBitmap( const wxString &filename, int type )
181 {
182 LoadFile( filename, type );
183
184 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
185 }
186
187 // CMB 15/5/98: add constructor for xbm bitmaps
188 wxBitmap::wxBitmap( const char bits[], int width, int height, int WXUNUSED(depth))
189 {
190 m_refData = new wxBitmapRefData();
191
192 M_BMPDATA->m_mask = (wxMask *) NULL;
193 M_BMPDATA->m_bitmap =
194 gdk_bitmap_create_from_data( (GdkWindow*) &gdk_root_parent, (gchar *) bits, width, height );
195 M_BMPDATA->m_width = width;
196 M_BMPDATA->m_height = height;
197 M_BMPDATA->m_bpp = 1;
198
199 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
200 }
201
202 wxBitmap::~wxBitmap(void)
203 {
204 if (wxTheBitmapList) wxTheBitmapList->DeleteObject(this);
205 }
206
207 wxBitmap& wxBitmap::operator = ( const wxBitmap& bmp )
208 {
209 if (*this == bmp) return (*this);
210 Ref( bmp );
211 return *this;
212 }
213
214 bool wxBitmap::operator == ( const wxBitmap& bmp )
215 {
216 return m_refData == bmp.m_refData;
217 }
218
219 bool wxBitmap::operator != ( const wxBitmap& bmp )
220 {
221 return m_refData != bmp.m_refData;
222 }
223
224 bool wxBitmap::Ok(void) const
225 {
226 return m_refData != NULL;
227 }
228
229 int wxBitmap::GetHeight(void) const
230 {
231 if (!Ok()) return 0;
232 return M_BMPDATA->m_height;
233 }
234
235 int wxBitmap::GetWidth(void) const
236 {
237 if (!Ok()) return 0;
238 return M_BMPDATA->m_width;
239 }
240
241 int wxBitmap::GetDepth(void) const
242 {
243 if (!Ok()) return 0;
244 return M_BMPDATA->m_bpp;
245 }
246
247 void wxBitmap::SetHeight( int height )
248 {
249 if (!Ok()) return;
250 M_BMPDATA->m_height = height;
251 }
252
253 void wxBitmap::SetWidth( int width )
254 {
255 if (!Ok()) return;
256 M_BMPDATA->m_width = width;
257 }
258
259 void wxBitmap::SetDepth( int depth )
260 {
261 if (!Ok()) return;
262 M_BMPDATA->m_bpp = depth;
263 }
264
265 wxMask *wxBitmap::GetMask(void) const
266 {
267 if (!Ok()) return (wxMask *) NULL;
268
269 return M_BMPDATA->m_mask;
270 }
271
272 void wxBitmap::SetMask( wxMask *mask )
273 {
274 if (!Ok()) return;
275
276 if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask;
277 M_BMPDATA->m_mask = mask;
278 }
279
280 void wxBitmap::Resize( int height, int width )
281 {
282 if (!Ok()) return;
283
284 #ifdef USE_GDK_IMLIB
285
286 if (M_BMPDATA->m_bitmap) return; // not supported for bitmaps
287
288 if (!M_BMPDATA->m_image) RecreateImage();
289
290 if (M_BMPDATA->m_pixmap) gdk_imlib_free_pixmap( M_BMPDATA->m_pixmap );
291 if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask;
292
293 GdkImlibImage* image = gdk_imlib_clone_scaled_image( M_BMPDATA->m_image, height, width );
294 gdk_imlib_destroy_image( M_BMPDATA->m_image );
295 M_BMPDATA->m_image = image;
296 M_BMPDATA->m_height = height;
297 M_BMPDATA->m_width = width;
298
299 Render();
300
301 #endif
302 }
303
304 bool wxBitmap::SaveFile( const wxString &name, int WXUNUSED(type),
305 wxPalette *WXUNUSED(palette) )
306 {
307 #ifdef USE_GDK_IMLIB
308
309 if (!Ok()) return FALSE;
310
311 if (!M_BMPDATA->m_image) RecreateImage();
312
313 return gdk_imlib_save_image( M_BMPDATA->m_image, WXSTRINGCAST name, (GdkImlibSaveInfo *) NULL );
314
315 #endif
316
317 return FALSE;
318 }
319
320 bool wxBitmap::LoadFile( const wxString &name, int WXUNUSED(type) )
321 {
322 #ifdef USE_GDK_IMLIB
323
324 UnRef();
325 m_refData = new wxBitmapRefData();
326
327 M_BMPDATA->m_image = gdk_imlib_load_image( WXSTRINGCAST name );
328
329 if (!M_BMPDATA->m_image)
330 {
331 UnRef();
332 return FALSE;
333 }
334
335 Render();
336
337 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
338 M_BMPDATA->m_bpp = 24; // ?
339
340 return TRUE;
341 #endif
342
343 return FALSE;
344 }
345
346 wxPalette *wxBitmap::GetPalette(void) const
347 {
348 if (!Ok()) return (wxPalette *) NULL;
349 return M_BMPDATA->m_palette;
350 }
351
352 GdkPixmap *wxBitmap::GetPixmap(void) const
353 {
354 if (!Ok()) return (GdkPixmap *) NULL;
355 return M_BMPDATA->m_pixmap;
356 }
357
358 GdkBitmap *wxBitmap::GetBitmap(void) const
359 {
360 if (!Ok()) return (GdkBitmap *) NULL;
361
362 return M_BMPDATA->m_bitmap;
363 }
364
365 void wxBitmap::DestroyImage(void)
366 {
367 if (!Ok()) return;
368
369 if (M_BMPDATA->m_image)
370 {
371 gdk_imlib_destroy_image( M_BMPDATA->m_image );
372 M_BMPDATA->m_image = (GdkImlibImage *) NULL;
373 }
374 }
375
376 void wxBitmap::RecreateImage(void)
377 {
378 }
379
380 void wxBitmap::Render(void)
381 {
382 if (!Ok()) return;
383
384 #ifdef USE_GDK_IMLIB
385
386 gdk_imlib_render( M_BMPDATA->m_image, M_BMPDATA->m_image->rgb_width, M_BMPDATA->m_image->rgb_height );
387 M_BMPDATA->m_width = M_BMPDATA->m_image->rgb_width;
388 M_BMPDATA->m_height = M_BMPDATA->m_image->rgb_height;
389 M_BMPDATA->m_pixmap = gdk_imlib_move_image( M_BMPDATA->m_image );
390 GdkBitmap *mask = gdk_imlib_move_mask( M_BMPDATA->m_image );
391 if (mask)
392 {
393 M_BMPDATA->m_mask = new wxMask();
394 M_BMPDATA->m_mask->m_bitmap = mask;
395 }
396
397 #endif
398 }
399
400