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