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