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