]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/bitmap.cpp
Added PNM image handler
[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 33
91b8de8d 34wxMask::wxMask( const wxBitmap& bitmap, const wxColour& colour )
c801d85f 35{
91b8de8d 36 Create( bitmap, colour );
ff7b1510 37}
c801d85f 38
91b8de8d 39wxMask::wxMask( const wxBitmap& bitmap, int paletteIndex )
c801d85f 40{
91b8de8d 41 Create( bitmap, paletteIndex );
ff7b1510 42}
c801d85f 43
91b8de8d 44wxMask::wxMask( const wxBitmap& bitmap )
c801d85f 45{
91b8de8d 46 Create( bitmap );
ff7b1510 47}
c801d85f 48
8bbe427f 49wxMask::~wxMask()
c801d85f 50{
fd0eed64 51 if (m_bitmap) gdk_bitmap_unref( m_bitmap );
ff7b1510 52}
c801d85f 53
91b8de8d
RR
54bool wxMask::Create( const wxBitmap& bitmap, const wxColour& colour )
55{
56 if (m_bitmap)
57 {
58 gdk_bitmap_unref( m_bitmap );
59 m_bitmap = (GdkBitmap*) NULL;
60 }
61
dcf924a3 62 wxFAIL_MSG( _T("TODO") );
91b8de8d
RR
63
64 return FALSE;
65}
66
67bool wxMask::Create( const wxBitmap& WXUNUSED(bitmap), int WXUNUSED(paletteIndex) )
68{
69 if (m_bitmap)
70 {
71 gdk_bitmap_unref( m_bitmap );
72 m_bitmap = (GdkBitmap*) NULL;
73 }
74
dcf924a3 75 wxFAIL_MSG( _T("not implemented") );
91b8de8d
RR
76
77 return FALSE;
78}
79
80bool wxMask::Create( const wxBitmap& bitmap )
81{
82 if (m_bitmap)
83 {
84 gdk_bitmap_unref( m_bitmap );
85 m_bitmap = (GdkBitmap*) NULL;
86 }
87
88 if (!bitmap.Ok()) return FALSE;
89
dcf924a3 90 wxCHECK_MSG( bitmap.GetBitmap(), FALSE, _T("Cannot create mask from colour bitmap") );
91b8de8d
RR
91
92 m_bitmap = gdk_pixmap_new( (GdkWindow*) &gdk_root_parent, bitmap.GetWidth(), bitmap.GetHeight(), 1 );
93
94 if (!m_bitmap) return FALSE;
95
96 GdkGC *gc = gdk_gc_new( m_bitmap );
97
98 gdk_draw_bitmap( m_bitmap, gc, bitmap.GetBitmap(), 0, 0, 0, 0, bitmap.GetWidth(), bitmap.GetHeight() );
99
100 gdk_gc_unref( gc );
101
102 return TRUE;
103}
104
105GdkBitmap *wxMask::GetBitmap() const
c801d85f 106{
fd0eed64 107 return m_bitmap;
ff7b1510 108}
8bbe427f 109
c801d85f
KB
110//-----------------------------------------------------------------------------
111// wxBitmap
112//-----------------------------------------------------------------------------
113
114class wxBitmapRefData: public wxObjectRefData
115{
fd0eed64 116public:
91b8de8d
RR
117 wxBitmapRefData();
118 ~wxBitmapRefData();
8bbe427f 119
fd0eed64
RR
120 GdkPixmap *m_pixmap;
121 GdkBitmap *m_bitmap;
122 wxMask *m_mask;
123 int m_width;
124 int m_height;
125 int m_bpp;
126 wxPalette *m_palette;
c801d85f
KB
127};
128
8bbe427f 129wxBitmapRefData::wxBitmapRefData()
c801d85f 130{
fd0eed64
RR
131 m_pixmap = (GdkPixmap *) NULL;
132 m_bitmap = (GdkBitmap *) NULL;
133 m_mask = (wxMask *) NULL;
134 m_width = 0;
135 m_height = 0;
136 m_bpp = 0;
137 m_palette = (wxPalette *) NULL;
ff7b1510 138}
c801d85f 139
8bbe427f 140wxBitmapRefData::~wxBitmapRefData()
c801d85f 141{
fd0eed64
RR
142 if (m_pixmap) gdk_pixmap_unref( m_pixmap );
143 if (m_bitmap) gdk_bitmap_unref( m_bitmap );
144 if (m_mask) delete m_mask;
145 if (m_palette) delete m_palette;
ff7b1510 146}
c801d85f
KB
147
148//-----------------------------------------------------------------------------
149
150#define M_BMPDATA ((wxBitmapRefData *)m_refData)
151
152IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxGDIObject)
153
8bbe427f 154wxBitmap::wxBitmap()
c801d85f 155{
fd0eed64 156 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
ff7b1510 157}
8bbe427f 158
debe6624 159wxBitmap::wxBitmap( int width, int height, int depth )
c801d85f 160{
62bd5cf0 161 wxCHECK_RET( (width > 0) && (height > 0), _T("invalid bitmap size") )
eefa26be 162
fd0eed64 163 GdkWindow *parent = (GdkWindow*) &gdk_root_parent;
eefa26be
RR
164 if (depth == -1) depth = gdk_window_get_visual( parent )->depth;
165
166 wxCHECK_RET( (depth == gdk_window_get_visual( parent )->depth) ||
167 (depth == 1), _T("invalid bitmap depth") )
8bbe427f 168
eefa26be 169 m_refData = new wxBitmapRefData();
fd0eed64 170 M_BMPDATA->m_mask = (wxMask *) NULL;
fd0eed64
RR
171 M_BMPDATA->m_width = width;
172 M_BMPDATA->m_height = height;
eefa26be
RR
173 if (depth == 1)
174 {
175 M_BMPDATA->m_bitmap = gdk_pixmap_new( parent, width, height, 1 );
176 M_BMPDATA->m_bpp = 1;
177 }
178 else
179 {
180 M_BMPDATA->m_pixmap = gdk_pixmap_new( parent, width, height, depth );
181 M_BMPDATA->m_bpp = gdk_window_get_visual( parent )->depth;
182 }
8bbe427f 183
fd0eed64 184 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
ff7b1510 185}
c801d85f 186
e52f60e6
RR
187wxBitmap::wxBitmap( const char **bits )
188{
62bd5cf0 189 wxCHECK_RET( bits != NULL, _T("invalid bitmap data") )
8bbe427f 190
e52f60e6
RR
191 m_refData = new wxBitmapRefData();
192
193 GdkBitmap *mask = (GdkBitmap*) NULL;
194 GdkWindow *parent = (GdkWindow*) &gdk_root_parent;
8bbe427f 195
e52f60e6 196 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm_d( parent, &mask, NULL, (gchar **) bits );
8bbe427f 197
e52f60e6
RR
198 if (mask)
199 {
200 M_BMPDATA->m_mask = new wxMask();
201 M_BMPDATA->m_mask->m_bitmap = mask;
202 }
8bbe427f 203
e52f60e6 204 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
8bbe427f 205
e52f60e6
RR
206 M_BMPDATA->m_bpp = gdk_window_get_visual( parent )->depth; // ?
207 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
208}
8bbe427f 209
c801d85f
KB
210wxBitmap::wxBitmap( char **bits )
211{
62bd5cf0 212 wxCHECK_RET( bits != NULL, _T("invalid bitmap data") )
8bbe427f 213
fd0eed64 214 m_refData = new wxBitmapRefData();
c801d85f 215
fd0eed64
RR
216 GdkBitmap *mask = (GdkBitmap*) NULL;
217 GdkWindow *parent = (GdkWindow*) &gdk_root_parent;
8bbe427f 218
fd0eed64 219 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm_d( parent, &mask, NULL, (gchar **) bits );
8bbe427f 220
62bd5cf0 221 wxCHECK_RET( M_BMPDATA->m_pixmap, _T("couldn't create pixmap") );
8bbe427f 222
fd0eed64
RR
223 if (mask)
224 {
225 M_BMPDATA->m_mask = new wxMask();
226 M_BMPDATA->m_mask->m_bitmap = mask;
227 }
8bbe427f 228
fd0eed64 229 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
8bbe427f 230
fd0eed64 231 M_BMPDATA->m_bpp = gdk_window_get_visual( parent )->depth; // ?
fd0eed64 232 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
ff7b1510 233}
8bbe427f 234
c801d85f
KB
235wxBitmap::wxBitmap( const wxBitmap& bmp )
236{
fd0eed64 237 Ref( bmp );
8bbe427f 238
fd0eed64 239 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
ff7b1510 240}
6f65e337 241
debe6624 242wxBitmap::wxBitmap( const wxString &filename, int type )
c801d85f 243{
fd0eed64 244 LoadFile( filename, type );
8bbe427f 245
fd0eed64 246 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
ff7b1510 247}
c801d85f 248
debe6624 249wxBitmap::wxBitmap( const char bits[], int width, int height, int WXUNUSED(depth))
6f65e337 250{
fd0eed64 251 m_refData = new wxBitmapRefData();
6f65e337 252
fd0eed64 253 M_BMPDATA->m_mask = (wxMask *) NULL;
8bbe427f 254 M_BMPDATA->m_bitmap =
fd0eed64
RR
255 gdk_bitmap_create_from_data( (GdkWindow*) &gdk_root_parent, (gchar *) bits, width, height );
256 M_BMPDATA->m_width = width;
257 M_BMPDATA->m_height = height;
258 M_BMPDATA->m_bpp = 1;
6f65e337 259
62bd5cf0 260 wxCHECK_RET( M_BMPDATA->m_bitmap, _T("couldn't create bitmap") );
8bbe427f 261
fd0eed64 262 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
6f65e337 263}
8bbe427f
VZ
264
265wxBitmap::~wxBitmap()
c801d85f 266{
fd0eed64 267 if (wxTheBitmapList) wxTheBitmapList->DeleteObject(this);
ff7b1510 268}
8bbe427f 269
c801d85f
KB
270wxBitmap& wxBitmap::operator = ( const wxBitmap& bmp )
271{
8bbe427f
VZ
272 if (*this == bmp) return (*this);
273 Ref( bmp );
274 return *this;
ff7b1510 275}
8bbe427f 276
c801d85f
KB
277bool wxBitmap::operator == ( const wxBitmap& bmp )
278{
8bbe427f 279 return m_refData == bmp.m_refData;
ff7b1510 280}
8bbe427f 281
c801d85f
KB
282bool wxBitmap::operator != ( const wxBitmap& bmp )
283{
8bbe427f 284 return m_refData != bmp.m_refData;
ff7b1510 285}
8bbe427f 286
91b8de8d 287bool wxBitmap::Ok() const
c801d85f 288{
fd0eed64 289 return (m_refData != NULL);
ff7b1510 290}
8bbe427f 291
91b8de8d 292int wxBitmap::GetHeight() const
c801d85f 293{
62bd5cf0 294 wxCHECK_MSG( Ok(), -1, _T("invalid bitmap") );
e55ad60e 295
fd0eed64 296 return M_BMPDATA->m_height;
ff7b1510 297}
c801d85f 298
91b8de8d 299int wxBitmap::GetWidth() const
c801d85f 300{
62bd5cf0 301 wxCHECK_MSG( Ok(), -1, _T("invalid bitmap") );
8bbe427f 302
fd0eed64 303 return M_BMPDATA->m_width;
ff7b1510 304}
c801d85f 305
91b8de8d 306int wxBitmap::GetDepth() const
c801d85f 307{
62bd5cf0 308 wxCHECK_MSG( Ok(), -1, _T("invalid bitmap") );
8bbe427f 309
fd0eed64 310 return M_BMPDATA->m_bpp;
ff7b1510 311}
c801d85f 312
91b8de8d 313wxMask *wxBitmap::GetMask() const
c801d85f 314{
62bd5cf0 315 wxCHECK_MSG( Ok(), (wxMask *) NULL, _T("invalid bitmap") );
8bbe427f 316
fd0eed64 317 return M_BMPDATA->m_mask;
ff7b1510 318}
c801d85f
KB
319
320void wxBitmap::SetMask( wxMask *mask )
321{
62bd5cf0 322 wxCHECK_RET( Ok(), _T("invalid bitmap") );
8bbe427f 323
fd0eed64 324 if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask;
8bbe427f 325
fd0eed64 326 M_BMPDATA->m_mask = mask;
ff7b1510 327}
c801d85f 328
fd0eed64 329bool wxBitmap::SaveFile( const wxString &name, int type, wxPalette *WXUNUSED(palette) )
c801d85f 330{
62bd5cf0 331 wxCHECK_MSG( Ok(), FALSE, _T("invalid bitmap") );
8bbe427f 332
fd0eed64
RR
333 if (type == wxBITMAP_TYPE_PNG)
334 {
4bc67cc5 335 wxImage image( *this );
fd0eed64
RR
336 if (image.Ok()) return image.SaveFile( name, type );
337 }
8bbe427f 338
fd0eed64 339 return FALSE;
ff7b1510 340}
c801d85f 341
fd0eed64 342bool wxBitmap::LoadFile( const wxString &name, int type )
c801d85f 343{
fd0eed64 344 UnRef();
8bbe427f 345
fd0eed64 346 if (!wxFileExists(name)) return FALSE;
8bbe427f 347
fd0eed64
RR
348 if (type == wxBITMAP_TYPE_XPM)
349 {
350 m_refData = new wxBitmapRefData();
8bbe427f 351
fd0eed64
RR
352 GdkBitmap *mask = (GdkBitmap*) NULL;
353 GdkWindow *parent = (GdkWindow*) &gdk_root_parent;
8bbe427f 354
62bd5cf0 355 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm( parent, &mask, NULL, name.fn_str() );
8bbe427f 356
fd0eed64
RR
357 if (mask)
358 {
359 M_BMPDATA->m_mask = new wxMask();
360 M_BMPDATA->m_mask->m_bitmap = mask;
361 }
8bbe427f 362
fd0eed64 363 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
8bbe427f 364 M_BMPDATA->m_bpp = gdk_window_get_visual( parent )->depth;
fd0eed64
RR
365 }
366 else if (type == wxBITMAP_TYPE_PNG)
367 {
368 wxImage image;
369 image.LoadFile( name, type );
4bc67cc5 370 if (image.Ok()) *this = image.ConvertToBitmap();
fd0eed64
RR
371 }
372 else if (type == wxBITMAP_TYPE_BMP)
373 {
374 wxImage image;
375 image.LoadFile( name, type );
4bc67cc5 376 if (image.Ok()) *this = image.ConvertToBitmap();
fd0eed64 377 }
8bbe427f 378 else
fd0eed64 379 return FALSE;
8bbe427f 380
fd0eed64 381 return TRUE;
ff7b1510 382}
8bbe427f 383
91b8de8d 384wxPalette *wxBitmap::GetPalette() const
c801d85f 385{
fd0eed64 386 if (!Ok()) return (wxPalette *) NULL;
8bbe427f 387
fd0eed64 388 return M_BMPDATA->m_palette;
ff7b1510 389}
c801d85f 390
4bc67cc5
RR
391void wxBitmap::SetHeight( int height )
392{
393 if (!m_refData) m_refData = new wxBitmapRefData();
394
395 M_BMPDATA->m_height = height;
396}
397
398void wxBitmap::SetWidth( int width )
399{
400 if (!m_refData) m_refData = new wxBitmapRefData();
401
402 M_BMPDATA->m_width = width;
403}
404
405void wxBitmap::SetDepth( int depth )
406{
407 if (!m_refData) m_refData = new wxBitmapRefData();
408
409 M_BMPDATA->m_bpp = depth;
410}
411
412void wxBitmap::SetPixmap( GdkPixmap *pixmap )
413{
414 if (!m_refData) m_refData = new wxBitmapRefData();
415
416 M_BMPDATA->m_pixmap = pixmap;
417}
418
91b8de8d 419GdkPixmap *wxBitmap::GetPixmap() const
c801d85f 420{
62bd5cf0 421 wxCHECK_MSG( Ok(), (GdkPixmap *) NULL, _T("invalid bitmap") );
8bbe427f 422
fd0eed64 423 return M_BMPDATA->m_pixmap;
ff7b1510 424}
8bbe427f 425
91b8de8d 426GdkBitmap *wxBitmap::GetBitmap() const
6f65e337 427{
62bd5cf0 428 wxCHECK_MSG( Ok(), (GdkBitmap *) NULL, _T("invalid bitmap") );
8bbe427f 429
fd0eed64 430 return M_BMPDATA->m_bitmap;
ff7b1510 431}