]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/bitmap.cpp
Added wxWindowBase::CentreOnParent to allow top level windows to be
[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{
62bd5cf0 107 wxCHECK_RET( (width > 0) && (height > 0), _T("invalid bitmap size") )
eefa26be 108
fd0eed64 109 GdkWindow *parent = (GdkWindow*) &gdk_root_parent;
eefa26be
RR
110 if (depth == -1) depth = gdk_window_get_visual( parent )->depth;
111
112 wxCHECK_RET( (depth == gdk_window_get_visual( parent )->depth) ||
113 (depth == 1), _T("invalid bitmap depth") )
8bbe427f 114
eefa26be 115 m_refData = new wxBitmapRefData();
fd0eed64 116 M_BMPDATA->m_mask = (wxMask *) NULL;
fd0eed64
RR
117 M_BMPDATA->m_width = width;
118 M_BMPDATA->m_height = height;
eefa26be
RR
119 if (depth == 1)
120 {
121 M_BMPDATA->m_bitmap = gdk_pixmap_new( parent, width, height, 1 );
122 M_BMPDATA->m_bpp = 1;
123 }
124 else
125 {
126 M_BMPDATA->m_pixmap = gdk_pixmap_new( parent, width, height, depth );
127 M_BMPDATA->m_bpp = gdk_window_get_visual( parent )->depth;
128 }
8bbe427f 129
fd0eed64 130 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
ff7b1510 131}
c801d85f 132
e52f60e6
RR
133wxBitmap::wxBitmap( const char **bits )
134{
62bd5cf0 135 wxCHECK_RET( bits != NULL, _T("invalid bitmap data") )
8bbe427f 136
e52f60e6
RR
137 m_refData = new wxBitmapRefData();
138
139 GdkBitmap *mask = (GdkBitmap*) NULL;
140 GdkWindow *parent = (GdkWindow*) &gdk_root_parent;
8bbe427f 141
e52f60e6 142 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm_d( parent, &mask, NULL, (gchar **) bits );
8bbe427f 143
e52f60e6
RR
144 if (mask)
145 {
146 M_BMPDATA->m_mask = new wxMask();
147 M_BMPDATA->m_mask->m_bitmap = mask;
148 }
8bbe427f 149
e52f60e6 150 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
8bbe427f 151
e52f60e6
RR
152 M_BMPDATA->m_bpp = gdk_window_get_visual( parent )->depth; // ?
153 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
154}
8bbe427f 155
c801d85f
KB
156wxBitmap::wxBitmap( char **bits )
157{
62bd5cf0 158 wxCHECK_RET( bits != NULL, _T("invalid bitmap data") )
8bbe427f 159
fd0eed64 160 m_refData = new wxBitmapRefData();
c801d85f 161
fd0eed64
RR
162 GdkBitmap *mask = (GdkBitmap*) NULL;
163 GdkWindow *parent = (GdkWindow*) &gdk_root_parent;
8bbe427f 164
fd0eed64 165 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm_d( parent, &mask, NULL, (gchar **) bits );
8bbe427f 166
62bd5cf0 167 wxCHECK_RET( M_BMPDATA->m_pixmap, _T("couldn't create pixmap") );
8bbe427f 168
fd0eed64
RR
169 if (mask)
170 {
171 M_BMPDATA->m_mask = new wxMask();
172 M_BMPDATA->m_mask->m_bitmap = mask;
173 }
8bbe427f 174
fd0eed64 175 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
8bbe427f 176
fd0eed64 177 M_BMPDATA->m_bpp = gdk_window_get_visual( parent )->depth; // ?
fd0eed64 178 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
ff7b1510 179}
8bbe427f 180
c801d85f
KB
181wxBitmap::wxBitmap( const wxBitmap& bmp )
182{
fd0eed64 183 Ref( bmp );
8bbe427f 184
fd0eed64 185 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
ff7b1510 186}
6f65e337 187
debe6624 188wxBitmap::wxBitmap( const wxString &filename, int type )
c801d85f 189{
fd0eed64 190 LoadFile( filename, type );
8bbe427f 191
fd0eed64 192 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
ff7b1510 193}
c801d85f 194
debe6624 195wxBitmap::wxBitmap( const char bits[], int width, int height, int WXUNUSED(depth))
6f65e337 196{
fd0eed64 197 m_refData = new wxBitmapRefData();
6f65e337 198
fd0eed64 199 M_BMPDATA->m_mask = (wxMask *) NULL;
8bbe427f 200 M_BMPDATA->m_bitmap =
fd0eed64
RR
201 gdk_bitmap_create_from_data( (GdkWindow*) &gdk_root_parent, (gchar *) bits, width, height );
202 M_BMPDATA->m_width = width;
203 M_BMPDATA->m_height = height;
204 M_BMPDATA->m_bpp = 1;
6f65e337 205
62bd5cf0 206 wxCHECK_RET( M_BMPDATA->m_bitmap, _T("couldn't create bitmap") );
8bbe427f 207
fd0eed64 208 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
6f65e337 209}
8bbe427f
VZ
210
211wxBitmap::~wxBitmap()
c801d85f 212{
fd0eed64 213 if (wxTheBitmapList) wxTheBitmapList->DeleteObject(this);
ff7b1510 214}
8bbe427f 215
c801d85f
KB
216wxBitmap& wxBitmap::operator = ( const wxBitmap& bmp )
217{
8bbe427f
VZ
218 if (*this == bmp) return (*this);
219 Ref( bmp );
220 return *this;
ff7b1510 221}
8bbe427f 222
c801d85f
KB
223bool wxBitmap::operator == ( const wxBitmap& bmp )
224{
8bbe427f 225 return m_refData == bmp.m_refData;
ff7b1510 226}
8bbe427f 227
c801d85f
KB
228bool wxBitmap::operator != ( const wxBitmap& bmp )
229{
8bbe427f 230 return m_refData != bmp.m_refData;
ff7b1510 231}
8bbe427f 232
c801d85f
KB
233bool wxBitmap::Ok(void) const
234{
fd0eed64 235 return (m_refData != NULL);
ff7b1510 236}
8bbe427f 237
c801d85f
KB
238int wxBitmap::GetHeight(void) const
239{
62bd5cf0 240 wxCHECK_MSG( Ok(), -1, _T("invalid bitmap") );
e55ad60e 241
fd0eed64 242 return M_BMPDATA->m_height;
ff7b1510 243}
c801d85f
KB
244
245int wxBitmap::GetWidth(void) const
246{
62bd5cf0 247 wxCHECK_MSG( Ok(), -1, _T("invalid bitmap") );
8bbe427f 248
fd0eed64 249 return M_BMPDATA->m_width;
ff7b1510 250}
c801d85f
KB
251
252int wxBitmap::GetDepth(void) const
253{
62bd5cf0 254 wxCHECK_MSG( Ok(), -1, _T("invalid bitmap") );
8bbe427f 255
fd0eed64 256 return M_BMPDATA->m_bpp;
ff7b1510 257}
c801d85f 258
c801d85f
KB
259wxMask *wxBitmap::GetMask(void) const
260{
62bd5cf0 261 wxCHECK_MSG( Ok(), (wxMask *) NULL, _T("invalid bitmap") );
8bbe427f 262
fd0eed64 263 return M_BMPDATA->m_mask;
ff7b1510 264}
c801d85f
KB
265
266void wxBitmap::SetMask( wxMask *mask )
267{
62bd5cf0 268 wxCHECK_RET( Ok(), _T("invalid bitmap") );
8bbe427f 269
fd0eed64 270 if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask;
8bbe427f 271
fd0eed64 272 M_BMPDATA->m_mask = mask;
ff7b1510 273}
c801d85f 274
fd0eed64 275bool wxBitmap::SaveFile( const wxString &name, int type, wxPalette *WXUNUSED(palette) )
c801d85f 276{
62bd5cf0 277 wxCHECK_MSG( Ok(), FALSE, _T("invalid bitmap") );
8bbe427f 278
fd0eed64
RR
279 if (type == wxBITMAP_TYPE_PNG)
280 {
4bc67cc5 281 wxImage image( *this );
fd0eed64
RR
282 if (image.Ok()) return image.SaveFile( name, type );
283 }
8bbe427f 284
fd0eed64 285 return FALSE;
ff7b1510 286}
c801d85f 287
fd0eed64 288bool wxBitmap::LoadFile( const wxString &name, int type )
c801d85f 289{
fd0eed64 290 UnRef();
8bbe427f 291
fd0eed64 292 if (!wxFileExists(name)) return FALSE;
8bbe427f 293
fd0eed64
RR
294 if (type == wxBITMAP_TYPE_XPM)
295 {
296 m_refData = new wxBitmapRefData();
8bbe427f 297
fd0eed64
RR
298 GdkBitmap *mask = (GdkBitmap*) NULL;
299 GdkWindow *parent = (GdkWindow*) &gdk_root_parent;
8bbe427f 300
62bd5cf0 301 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm( parent, &mask, NULL, name.fn_str() );
8bbe427f 302
fd0eed64
RR
303 if (mask)
304 {
305 M_BMPDATA->m_mask = new wxMask();
306 M_BMPDATA->m_mask->m_bitmap = mask;
307 }
8bbe427f 308
fd0eed64 309 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
8bbe427f 310 M_BMPDATA->m_bpp = gdk_window_get_visual( parent )->depth;
fd0eed64
RR
311 }
312 else if (type == wxBITMAP_TYPE_PNG)
313 {
314 wxImage image;
315 image.LoadFile( name, type );
4bc67cc5 316 if (image.Ok()) *this = image.ConvertToBitmap();
fd0eed64
RR
317 }
318 else if (type == wxBITMAP_TYPE_BMP)
319 {
320 wxImage image;
321 image.LoadFile( name, type );
4bc67cc5 322 if (image.Ok()) *this = image.ConvertToBitmap();
fd0eed64 323 }
8bbe427f 324 else
fd0eed64 325 return FALSE;
8bbe427f 326
fd0eed64 327 return TRUE;
ff7b1510 328}
8bbe427f 329
c801d85f
KB
330wxPalette *wxBitmap::GetPalette(void) const
331{
fd0eed64 332 if (!Ok()) return (wxPalette *) NULL;
8bbe427f 333
fd0eed64 334 return M_BMPDATA->m_palette;
ff7b1510 335}
c801d85f 336
4bc67cc5
RR
337void wxBitmap::SetHeight( int height )
338{
339 if (!m_refData) m_refData = new wxBitmapRefData();
340
341 M_BMPDATA->m_height = height;
342}
343
344void wxBitmap::SetWidth( int width )
345{
346 if (!m_refData) m_refData = new wxBitmapRefData();
347
348 M_BMPDATA->m_width = width;
349}
350
351void wxBitmap::SetDepth( int depth )
352{
353 if (!m_refData) m_refData = new wxBitmapRefData();
354
355 M_BMPDATA->m_bpp = depth;
356}
357
358void wxBitmap::SetPixmap( GdkPixmap *pixmap )
359{
360 if (!m_refData) m_refData = new wxBitmapRefData();
361
362 M_BMPDATA->m_pixmap = pixmap;
363}
364
c801d85f
KB
365GdkPixmap *wxBitmap::GetPixmap(void) const
366{
62bd5cf0 367 wxCHECK_MSG( Ok(), (GdkPixmap *) NULL, _T("invalid bitmap") );
8bbe427f 368
fd0eed64 369 return M_BMPDATA->m_pixmap;
ff7b1510 370}
8bbe427f 371
6f65e337
JS
372GdkBitmap *wxBitmap::GetBitmap(void) const
373{
62bd5cf0 374 wxCHECK_MSG( Ok(), (GdkBitmap *) NULL, _T("invalid bitmap") );
8bbe427f 375
fd0eed64 376 return M_BMPDATA->m_bitmap;
ff7b1510 377}