]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/bitmap.cpp
1. the common helper functions are now in src/unix/utilsunx.cpp
[wxWidgets.git] / src / gtk1 / bitmap.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: bitmap.cpp
3// Purpose:
4// Author: Robert Roebling
6f65e337 5// RCS-ID: $Id$
01111366 6// Copyright: (c) 1998 Robert Roebling
8bbe427f 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
10#ifdef __GNUG__
11#pragma implementation "bitmap.h"
12#endif
13
14#include "wx/bitmap.h"
52cbfcf0 15#include "wx/icon.h"
fd0eed64 16#include "wx/filefn.h"
83624f79
RR
17#include "wx/image.h"
18
19#include "gdk/gdk.h"
c801d85f 20#include "gdk/gdkprivate.h"
01111366 21#include "gdk/gdkx.h"
c801d85f
KB
22
23//-----------------------------------------------------------------------------
24// wxMask
25//-----------------------------------------------------------------------------
26
27IMPLEMENT_DYNAMIC_CLASS(wxMask,wxObject)
28
8bbe427f 29wxMask::wxMask()
c801d85f 30{
fd0eed64 31 m_bitmap = (GdkBitmap *) 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 45
8bbe427f 46wxMask::~wxMask()
c801d85f 47{
fd0eed64 48 if (m_bitmap) gdk_bitmap_unref( m_bitmap );
ff7b1510 49}
c801d85f
KB
50
51GdkBitmap *wxMask::GetBitmap(void) const
52{
fd0eed64 53 return m_bitmap;
ff7b1510 54}
8bbe427f 55
c801d85f
KB
56//-----------------------------------------------------------------------------
57// wxBitmap
58//-----------------------------------------------------------------------------
59
60class wxBitmapRefData: public wxObjectRefData
61{
fd0eed64
RR
62public:
63 wxBitmapRefData(void);
64 ~wxBitmapRefData(void);
8bbe427f 65
fd0eed64
RR
66 GdkPixmap *m_pixmap;
67 GdkBitmap *m_bitmap;
68 wxMask *m_mask;
69 int m_width;
70 int m_height;
71 int m_bpp;
72 wxPalette *m_palette;
c801d85f
KB
73};
74
8bbe427f 75wxBitmapRefData::wxBitmapRefData()
c801d85f 76{
fd0eed64
RR
77 m_pixmap = (GdkPixmap *) NULL;
78 m_bitmap = (GdkBitmap *) NULL;
79 m_mask = (wxMask *) NULL;
80 m_width = 0;
81 m_height = 0;
82 m_bpp = 0;
83 m_palette = (wxPalette *) NULL;
ff7b1510 84}
c801d85f 85
8bbe427f 86wxBitmapRefData::~wxBitmapRefData()
c801d85f 87{
fd0eed64
RR
88 if (m_pixmap) gdk_pixmap_unref( m_pixmap );
89 if (m_bitmap) gdk_bitmap_unref( m_bitmap );
90 if (m_mask) delete m_mask;
91 if (m_palette) delete m_palette;
ff7b1510 92}
c801d85f
KB
93
94//-----------------------------------------------------------------------------
95
96#define M_BMPDATA ((wxBitmapRefData *)m_refData)
97
98IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxGDIObject)
99
8bbe427f 100wxBitmap::wxBitmap()
c801d85f 101{
fd0eed64 102 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
ff7b1510 103}
8bbe427f 104
debe6624 105wxBitmap::wxBitmap( int width, int height, int depth )
c801d85f 106{
fd0eed64
RR
107 wxCHECK_RET( (width > 0) && (height > 0), "invalid bitmap size" )
108 wxCHECK_RET( (depth > 0) || (depth == -1), "invalid bitmap depth" )
fee04295 109
fd0eed64 110 m_refData = new wxBitmapRefData();
8bbe427f 111
fd0eed64 112 GdkWindow *parent = (GdkWindow*) &gdk_root_parent;
8bbe427f 113
fd0eed64
RR
114 M_BMPDATA->m_mask = (wxMask *) NULL;
115 M_BMPDATA->m_pixmap = gdk_pixmap_new( parent, width, height, depth );
116 M_BMPDATA->m_width = width;
117 M_BMPDATA->m_height = height;
118 M_BMPDATA->m_bpp = gdk_window_get_visual( parent )->depth;
8bbe427f 119
fd0eed64 120 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
ff7b1510 121}
c801d85f 122
e52f60e6
RR
123wxBitmap::wxBitmap( const char **bits )
124{
125 wxCHECK_RET( bits != NULL, "invalid bitmap data" )
8bbe427f 126
e52f60e6
RR
127 m_refData = new wxBitmapRefData();
128
129 GdkBitmap *mask = (GdkBitmap*) NULL;
130 GdkWindow *parent = (GdkWindow*) &gdk_root_parent;
8bbe427f 131
e52f60e6 132 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm_d( parent, &mask, NULL, (gchar **) bits );
8bbe427f 133
e52f60e6
RR
134 if (mask)
135 {
136 M_BMPDATA->m_mask = new wxMask();
137 M_BMPDATA->m_mask->m_bitmap = mask;
138 }
8bbe427f 139
e52f60e6 140 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
8bbe427f 141
e52f60e6
RR
142 M_BMPDATA->m_bpp = gdk_window_get_visual( parent )->depth; // ?
143 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
144}
8bbe427f 145
c801d85f
KB
146wxBitmap::wxBitmap( char **bits )
147{
fd0eed64 148 wxCHECK_RET( bits != NULL, "invalid bitmap data" )
8bbe427f 149
fd0eed64 150 m_refData = new wxBitmapRefData();
c801d85f 151
fd0eed64
RR
152 GdkBitmap *mask = (GdkBitmap*) NULL;
153 GdkWindow *parent = (GdkWindow*) &gdk_root_parent;
8bbe427f 154
fd0eed64 155 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm_d( parent, &mask, NULL, (gchar **) bits );
8bbe427f 156
4bc67cc5 157 wxCHECK_RET( M_BMPDATA->m_pixmap, "couldn't create pixmap" );
8bbe427f 158
fd0eed64
RR
159 if (mask)
160 {
161 M_BMPDATA->m_mask = new wxMask();
162 M_BMPDATA->m_mask->m_bitmap = mask;
163 }
8bbe427f 164
fd0eed64 165 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
8bbe427f 166
fd0eed64 167 M_BMPDATA->m_bpp = gdk_window_get_visual( parent )->depth; // ?
fd0eed64 168 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
ff7b1510 169}
8bbe427f 170
c801d85f
KB
171wxBitmap::wxBitmap( const wxBitmap& bmp )
172{
fd0eed64 173 Ref( bmp );
8bbe427f 174
fd0eed64 175 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
ff7b1510 176}
6f65e337 177
debe6624 178wxBitmap::wxBitmap( const wxString &filename, int type )
c801d85f 179{
fd0eed64 180 LoadFile( filename, type );
8bbe427f 181
fd0eed64 182 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
ff7b1510 183}
c801d85f 184
debe6624 185wxBitmap::wxBitmap( const char bits[], int width, int height, int WXUNUSED(depth))
6f65e337 186{
fd0eed64 187 m_refData = new wxBitmapRefData();
6f65e337 188
fd0eed64 189 M_BMPDATA->m_mask = (wxMask *) NULL;
8bbe427f 190 M_BMPDATA->m_bitmap =
fd0eed64
RR
191 gdk_bitmap_create_from_data( (GdkWindow*) &gdk_root_parent, (gchar *) bits, width, height );
192 M_BMPDATA->m_width = width;
193 M_BMPDATA->m_height = height;
194 M_BMPDATA->m_bpp = 1;
6f65e337 195
4bc67cc5 196 wxCHECK_RET( M_BMPDATA->m_bitmap, "couldn't create bitmap" );
8bbe427f 197
fd0eed64 198 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
6f65e337 199}
8bbe427f
VZ
200
201wxBitmap::~wxBitmap()
c801d85f 202{
fd0eed64 203 if (wxTheBitmapList) wxTheBitmapList->DeleteObject(this);
ff7b1510 204}
8bbe427f 205
c801d85f
KB
206wxBitmap& wxBitmap::operator = ( const wxBitmap& bmp )
207{
8bbe427f
VZ
208 if (*this == bmp) return (*this);
209 Ref( bmp );
210 return *this;
ff7b1510 211}
8bbe427f 212
c801d85f
KB
213bool wxBitmap::operator == ( const wxBitmap& bmp )
214{
8bbe427f 215 return m_refData == bmp.m_refData;
ff7b1510 216}
8bbe427f 217
c801d85f
KB
218bool wxBitmap::operator != ( const wxBitmap& bmp )
219{
8bbe427f 220 return m_refData != bmp.m_refData;
ff7b1510 221}
8bbe427f 222
c801d85f
KB
223bool wxBitmap::Ok(void) const
224{
fd0eed64 225 return (m_refData != NULL);
ff7b1510 226}
8bbe427f 227
c801d85f
KB
228int wxBitmap::GetHeight(void) const
229{
33a5bc52 230 wxCHECK_MSG( Ok(), -1, "invalid bitmap" );
e55ad60e 231
fd0eed64 232 return M_BMPDATA->m_height;
ff7b1510 233}
c801d85f
KB
234
235int wxBitmap::GetWidth(void) const
236{
33a5bc52 237 wxCHECK_MSG( Ok(), -1, "invalid bitmap" );
8bbe427f 238
fd0eed64 239 return M_BMPDATA->m_width;
ff7b1510 240}
c801d85f
KB
241
242int wxBitmap::GetDepth(void) const
243{
33a5bc52 244 wxCHECK_MSG( Ok(), -1, "invalid bitmap" );
8bbe427f 245
fd0eed64 246 return M_BMPDATA->m_bpp;
ff7b1510 247}
c801d85f 248
c801d85f
KB
249wxMask *wxBitmap::GetMask(void) const
250{
33a5bc52 251 wxCHECK_MSG( Ok(), (wxMask *) NULL, "invalid bitmap" );
8bbe427f 252
fd0eed64 253 return M_BMPDATA->m_mask;
ff7b1510 254}
c801d85f
KB
255
256void wxBitmap::SetMask( wxMask *mask )
257{
33a5bc52 258 wxCHECK_RET( Ok(), "invalid bitmap" );
8bbe427f 259
fd0eed64 260 if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask;
8bbe427f 261
fd0eed64 262 M_BMPDATA->m_mask = mask;
ff7b1510 263}
c801d85f 264
fd0eed64 265bool wxBitmap::SaveFile( const wxString &name, int type, wxPalette *WXUNUSED(palette) )
c801d85f 266{
33a5bc52 267 wxCHECK_MSG( Ok(), FALSE, "invalid bitmap" );
8bbe427f 268
fd0eed64
RR
269 if (type == wxBITMAP_TYPE_PNG)
270 {
4bc67cc5 271 wxImage image( *this );
fd0eed64
RR
272 if (image.Ok()) return image.SaveFile( name, type );
273 }
8bbe427f 274
fd0eed64 275 return FALSE;
ff7b1510 276}
c801d85f 277
fd0eed64 278bool wxBitmap::LoadFile( const wxString &name, int type )
c801d85f 279{
fd0eed64 280 UnRef();
8bbe427f 281
fd0eed64 282 if (!wxFileExists(name)) return FALSE;
8bbe427f 283
fd0eed64
RR
284 if (type == wxBITMAP_TYPE_XPM)
285 {
286 m_refData = new wxBitmapRefData();
8bbe427f 287
fd0eed64
RR
288 GdkBitmap *mask = (GdkBitmap*) NULL;
289 GdkWindow *parent = (GdkWindow*) &gdk_root_parent;
8bbe427f 290
fd0eed64 291 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm( parent, &mask, NULL, name );
8bbe427f 292
fd0eed64
RR
293 if (mask)
294 {
295 M_BMPDATA->m_mask = new wxMask();
296 M_BMPDATA->m_mask->m_bitmap = mask;
297 }
8bbe427f 298
fd0eed64 299 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
8bbe427f 300 M_BMPDATA->m_bpp = gdk_window_get_visual( parent )->depth;
fd0eed64
RR
301 }
302 else if (type == wxBITMAP_TYPE_PNG)
303 {
304 wxImage image;
305 image.LoadFile( name, type );
4bc67cc5 306 if (image.Ok()) *this = image.ConvertToBitmap();
fd0eed64
RR
307 }
308 else if (type == wxBITMAP_TYPE_BMP)
309 {
310 wxImage image;
311 image.LoadFile( name, type );
4bc67cc5 312 if (image.Ok()) *this = image.ConvertToBitmap();
fd0eed64 313 }
8bbe427f 314 else
fd0eed64 315 return FALSE;
8bbe427f 316
fd0eed64 317 return TRUE;
ff7b1510 318}
8bbe427f 319
c801d85f
KB
320wxPalette *wxBitmap::GetPalette(void) const
321{
fd0eed64 322 if (!Ok()) return (wxPalette *) NULL;
8bbe427f 323
fd0eed64 324 return M_BMPDATA->m_palette;
ff7b1510 325}
c801d85f 326
4bc67cc5
RR
327void wxBitmap::SetHeight( int height )
328{
329 if (!m_refData) m_refData = new wxBitmapRefData();
330
331 M_BMPDATA->m_height = height;
332}
333
334void wxBitmap::SetWidth( int width )
335{
336 if (!m_refData) m_refData = new wxBitmapRefData();
337
338 M_BMPDATA->m_width = width;
339}
340
341void wxBitmap::SetDepth( int depth )
342{
343 if (!m_refData) m_refData = new wxBitmapRefData();
344
345 M_BMPDATA->m_bpp = depth;
346}
347
348void wxBitmap::SetPixmap( GdkPixmap *pixmap )
349{
350 if (!m_refData) m_refData = new wxBitmapRefData();
351
352 M_BMPDATA->m_pixmap = pixmap;
353}
354
c801d85f
KB
355GdkPixmap *wxBitmap::GetPixmap(void) const
356{
33a5bc52 357 wxCHECK_MSG( Ok(), (GdkPixmap *) NULL, "invalid bitmap" );
8bbe427f 358
fd0eed64 359 return M_BMPDATA->m_pixmap;
ff7b1510 360}
8bbe427f 361
6f65e337
JS
362GdkBitmap *wxBitmap::GetBitmap(void) const
363{
33a5bc52 364 wxCHECK_MSG( Ok(), (GdkBitmap *) NULL, "invalid bitmap" );
8bbe427f 365
fd0eed64 366 return M_BMPDATA->m_bitmap;
ff7b1510 367}
8bbe427f 368
01111366 369