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