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