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