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