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