]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: bitmap.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
5 | // Created: 01/02/97 | |
6f65e337 | 6 | // RCS-ID: $Id$ |
c801d85f KB |
7 | // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | #ifdef __GNUG__ | |
12 | #pragma implementation "bitmap.h" | |
13 | #endif | |
14 | ||
15 | #include "wx/bitmap.h" | |
16 | #include "gdk/gdkprivate.h" | |
17 | ||
18 | #ifdef USE_GDK_IMLIB | |
1f0299c1 | 19 | #include "../gdk_imlib/gdk_imlib.h" |
c801d85f KB |
20 | #endif |
21 | ||
22 | //----------------------------------------------------------------------------- | |
23 | // wxMask | |
24 | //----------------------------------------------------------------------------- | |
25 | ||
26 | IMPLEMENT_DYNAMIC_CLASS(wxMask,wxObject) | |
27 | ||
28 | wxMask::wxMask(void) | |
29 | { | |
30 | m_bitmap = NULL; | |
31 | }; | |
32 | ||
33 | wxMask::wxMask( const wxBitmap& WXUNUSED(bitmap), const wxColour& WXUNUSED(colour) ) | |
34 | { | |
35 | }; | |
36 | ||
debe6624 | 37 | wxMask::wxMask( const wxBitmap& WXUNUSED(bitmap), int WXUNUSED(paletteIndex) ) |
c801d85f KB |
38 | { |
39 | }; | |
40 | ||
41 | wxMask::wxMask( const wxBitmap& WXUNUSED(bitmap) ) | |
42 | { | |
43 | }; | |
44 | ||
45 | wxMask::~wxMask(void) | |
46 | { | |
47 | #ifdef USE_GDK_IMLIB | |
48 | // do not delete the mask, gdk_imlib does it for you | |
49 | #else | |
50 | if (m_bitmap) gdk_bitmap_unref( m_bitmap ); | |
51 | #endif | |
52 | }; | |
53 | ||
54 | GdkBitmap *wxMask::GetBitmap(void) const | |
55 | { | |
56 | return m_bitmap; | |
57 | }; | |
58 | ||
59 | //----------------------------------------------------------------------------- | |
60 | // wxBitmap | |
61 | //----------------------------------------------------------------------------- | |
62 | ||
6f65e337 | 63 | // CMB 20/5/98: added m_bitmap for GdkBitmaps |
c801d85f KB |
64 | class wxBitmapRefData: public wxObjectRefData |
65 | { | |
66 | public: | |
67 | ||
68 | wxBitmapRefData(void); | |
69 | ~wxBitmapRefData(void); | |
70 | ||
71 | GdkPixmap *m_pixmap; | |
6f65e337 | 72 | GdkBitmap *m_bitmap; |
c801d85f KB |
73 | wxMask *m_mask; |
74 | int m_width; | |
75 | int m_height; | |
76 | int m_bpp; | |
219f895a RR |
77 | #ifdef USE_GDK_IMLIB |
78 | GdkImlibImage *m_image; | |
79 | #endif | |
c801d85f KB |
80 | wxPalette *m_palette; |
81 | }; | |
82 | ||
83 | wxBitmapRefData::wxBitmapRefData(void) | |
84 | { | |
85 | m_pixmap = NULL; | |
6f65e337 | 86 | m_bitmap = NULL; |
c801d85f KB |
87 | m_mask = NULL; |
88 | m_width = 0; | |
89 | m_height = 0; | |
90 | m_bpp = 0; | |
91 | m_palette = NULL; | |
92 | }; | |
93 | ||
94 | wxBitmapRefData::~wxBitmapRefData(void) | |
95 | { | |
96 | #ifdef USE_GDK_IMLIB | |
97 | if (m_pixmap) gdk_imlib_free_pixmap( m_pixmap ); | |
219f895a | 98 | if (m_image) gdk_imlib_destroy_image( m_image ); |
c801d85f KB |
99 | #else |
100 | if (m_pixmap) gdk_pixmap_unref( m_pixmap ); | |
101 | #endif | |
219f895a | 102 | if (m_bitmap) gdk_bitmap_unref( m_bitmap ); |
c801d85f KB |
103 | if (m_mask) delete m_mask; |
104 | if (m_palette) delete m_palette; | |
105 | }; | |
106 | ||
107 | //----------------------------------------------------------------------------- | |
108 | ||
109 | #define M_BMPDATA ((wxBitmapRefData *)m_refData) | |
110 | ||
111 | IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxGDIObject) | |
112 | ||
113 | wxBitmap::wxBitmap(void) | |
114 | { | |
115 | if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this); | |
116 | }; | |
117 | ||
debe6624 | 118 | wxBitmap::wxBitmap( int width, int height, int depth ) |
c801d85f KB |
119 | { |
120 | m_refData = new wxBitmapRefData(); | |
121 | M_BMPDATA->m_mask = NULL; | |
122 | M_BMPDATA->m_pixmap = | |
123 | gdk_pixmap_new( (GdkWindow*) &gdk_root_parent, width, height, depth ); | |
124 | gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) ); | |
125 | M_BMPDATA->m_bpp = depth; | |
126 | ||
127 | if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this); | |
128 | }; | |
129 | ||
130 | wxBitmap::wxBitmap( char **bits ) | |
131 | { | |
132 | m_refData = new wxBitmapRefData(); | |
c801d85f KB |
133 | |
134 | #ifndef USE_GDK_IMLIB | |
219f895a RR |
135 | |
136 | GdkBitmap *mask = NULL; | |
137 | ||
c801d85f KB |
138 | M_BMPDATA->m_pixmap = |
139 | gdk_pixmap_create_from_xpm_d( (GdkWindow*) &gdk_root_parent, &mask, NULL, (gchar **) bits ); | |
219f895a | 140 | |
c801d85f KB |
141 | if (mask) |
142 | { | |
143 | M_BMPDATA->m_mask = new wxMask(); | |
144 | M_BMPDATA->m_mask->m_bitmap = mask; | |
145 | }; | |
219f895a RR |
146 | |
147 | #else | |
148 | ||
149 | M_BMPDATA->m_image = gdk_imlib_create_image_from_xpm_data( bits ); | |
150 | Render(); | |
151 | ||
152 | #endif | |
c801d85f KB |
153 | |
154 | gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) ); | |
155 | M_BMPDATA->m_bpp = 24; // ? | |
156 | ||
157 | if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this); | |
158 | }; | |
159 | ||
160 | wxBitmap::wxBitmap( const wxBitmap& bmp ) | |
161 | { | |
162 | Ref( bmp ); | |
163 | ||
164 | if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this); | |
165 | }; | |
166 | ||
167 | wxBitmap::wxBitmap( const wxBitmap* bmp ) | |
168 | { | |
169 | if (bmp) Ref( *bmp ); | |
170 | ||
171 | if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this); | |
172 | }; | |
6f65e337 | 173 | |
debe6624 | 174 | wxBitmap::wxBitmap( const wxString &filename, int type ) |
c801d85f KB |
175 | { |
176 | LoadFile( filename, type ); | |
177 | }; | |
178 | ||
6f65e337 | 179 | // CMB 15/5/98: add constructor for xbm bitmaps |
debe6624 | 180 | wxBitmap::wxBitmap( const char bits[], int width, int height, int WXUNUSED(depth)) |
6f65e337 JS |
181 | { |
182 | m_refData = new wxBitmapRefData(); | |
183 | ||
184 | M_BMPDATA->m_mask = NULL; | |
185 | M_BMPDATA->m_bitmap = | |
186 | gdk_bitmap_create_from_data( (GdkWindow*) &gdk_root_parent, (gchar *) bits, width, height ); | |
187 | gdk_window_get_size( M_BMPDATA->m_bitmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) ); | |
188 | M_BMPDATA->m_bpp = 1; | |
189 | ||
190 | if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this); | |
191 | } | |
192 | ||
c801d85f KB |
193 | wxBitmap::~wxBitmap(void) |
194 | { | |
195 | if (wxTheBitmapList) wxTheBitmapList->DeleteObject(this); | |
196 | }; | |
197 | ||
198 | wxBitmap& wxBitmap::operator = ( const wxBitmap& bmp ) | |
199 | { | |
200 | if (*this == bmp) return (*this); | |
201 | Ref( bmp ); | |
202 | return *this; | |
203 | }; | |
204 | ||
205 | bool wxBitmap::operator == ( const wxBitmap& bmp ) | |
206 | { | |
207 | return m_refData == bmp.m_refData; | |
208 | }; | |
209 | ||
210 | bool wxBitmap::operator != ( const wxBitmap& bmp ) | |
211 | { | |
212 | return m_refData != bmp.m_refData; | |
213 | }; | |
214 | ||
215 | bool wxBitmap::Ok(void) const | |
216 | { | |
217 | return m_refData != NULL; | |
218 | }; | |
219 | ||
220 | int wxBitmap::GetHeight(void) const | |
221 | { | |
222 | if (!Ok()) return 0; | |
223 | return M_BMPDATA->m_height; | |
224 | }; | |
225 | ||
226 | int wxBitmap::GetWidth(void) const | |
227 | { | |
228 | if (!Ok()) return 0; | |
229 | return M_BMPDATA->m_width; | |
230 | }; | |
231 | ||
232 | int wxBitmap::GetDepth(void) const | |
233 | { | |
234 | if (!Ok()) return 0; | |
235 | return M_BMPDATA->m_bpp; | |
236 | }; | |
237 | ||
debe6624 | 238 | void wxBitmap::SetHeight( int height ) |
c801d85f KB |
239 | { |
240 | if (!Ok()) return; | |
241 | M_BMPDATA->m_height = height; | |
242 | }; | |
243 | ||
debe6624 | 244 | void wxBitmap::SetWidth( int width ) |
c801d85f KB |
245 | { |
246 | if (!Ok()) return; | |
247 | M_BMPDATA->m_width = width; | |
248 | }; | |
249 | ||
debe6624 | 250 | void wxBitmap::SetDepth( int depth ) |
c801d85f KB |
251 | { |
252 | if (!Ok()) return; | |
253 | M_BMPDATA->m_bpp = depth; | |
254 | }; | |
255 | ||
256 | wxMask *wxBitmap::GetMask(void) const | |
257 | { | |
258 | if (!Ok()) return NULL; | |
219f895a | 259 | |
c801d85f KB |
260 | return M_BMPDATA->m_mask; |
261 | }; | |
262 | ||
263 | void wxBitmap::SetMask( wxMask *mask ) | |
264 | { | |
265 | if (!Ok()) return; | |
219f895a | 266 | |
c801d85f KB |
267 | if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask; |
268 | M_BMPDATA->m_mask = mask; | |
269 | }; | |
270 | ||
219f895a RR |
271 | void wxBitmap::Resize( int height, int width ) |
272 | { | |
273 | if (!Ok()) return; | |
274 | ||
275 | return; | |
276 | ||
277 | #ifdef USE_GDK_IMLIB | |
278 | ||
279 | if (M_BMPDATA->m_bitmap) return; // not supported for bitmaps | |
280 | ||
281 | if (!M_BMPDATA->m_image) RecreateImage(); | |
282 | ||
283 | if (M_BMPDATA->m_pixmap) gdk_imlib_free_pixmap( M_BMPDATA->m_pixmap ); | |
284 | if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask; | |
285 | ||
286 | GdkImlibImage* image = gdk_imlib_clone_scaled_image( M_BMPDATA->m_image, height, width ); | |
287 | gdk_imlib_destroy_image( M_BMPDATA->m_image ); | |
288 | M_BMPDATA->m_image = image; | |
289 | M_BMPDATA->m_height = height; | |
290 | M_BMPDATA->m_width = width; | |
291 | ||
292 | Render(); | |
293 | ||
294 | #endif | |
295 | }; | |
296 | ||
297 | bool wxBitmap::SaveFile( const wxString &name, int WXUNUSED(type), | |
c801d85f KB |
298 | wxPalette *WXUNUSED(palette) ) |
299 | { | |
219f895a RR |
300 | #ifdef USE_GDK_IMLIB |
301 | ||
302 | if (!Ok()) return FALSE; | |
303 | ||
304 | if (!M_BMPDATA->m_image) RecreateImage(); | |
305 | ||
306 | return gdk_imlib_save_image( M_BMPDATA->m_image, WXSTRINGCAST name, NULL ); | |
307 | ||
308 | #endif | |
309 | ||
c801d85f KB |
310 | return FALSE; |
311 | }; | |
312 | ||
debe6624 | 313 | bool wxBitmap::LoadFile( const wxString &name, int WXUNUSED(type) ) |
c801d85f KB |
314 | { |
315 | #ifdef USE_GDK_IMLIB | |
316 | ||
317 | UnRef(); | |
318 | m_refData = new wxBitmapRefData(); | |
c801d85f | 319 | |
219f895a | 320 | M_BMPDATA->m_image = gdk_imlib_load_image( WXSTRINGCAST name ); |
c801d85f | 321 | |
219f895a | 322 | if (!M_BMPDATA->m_image) |
c801d85f KB |
323 | { |
324 | UnRef(); | |
325 | return FALSE; | |
326 | }; | |
327 | ||
219f895a | 328 | Render(); |
c801d85f KB |
329 | |
330 | gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) ); | |
331 | M_BMPDATA->m_bpp = 24; // ? | |
332 | ||
333 | return TRUE; | |
334 | #endif | |
335 | ||
336 | return FALSE; | |
337 | }; | |
338 | ||
339 | wxPalette *wxBitmap::GetPalette(void) const | |
340 | { | |
341 | if (!Ok()) return NULL; | |
342 | return M_BMPDATA->m_palette; | |
343 | }; | |
344 | ||
345 | GdkPixmap *wxBitmap::GetPixmap(void) const | |
346 | { | |
347 | if (!Ok()) return NULL; | |
348 | return M_BMPDATA->m_pixmap; | |
349 | }; | |
350 | ||
6f65e337 JS |
351 | GdkBitmap *wxBitmap::GetBitmap(void) const |
352 | { | |
353 | if (!Ok()) return NULL; | |
219f895a | 354 | |
6f65e337 JS |
355 | return M_BMPDATA->m_bitmap; |
356 | }; | |
357 | ||
219f895a RR |
358 | void wxBitmap::DestroyImage(void) |
359 | { | |
360 | if (!Ok()) return; | |
361 | ||
362 | if (M_BMPDATA->m_image) | |
363 | { | |
364 | gdk_imlib_destroy_image( M_BMPDATA->m_image ); | |
365 | M_BMPDATA->m_image = NULL; | |
366 | }; | |
367 | }; | |
368 | ||
369 | void wxBitmap::RecreateImage(void) | |
370 | { | |
371 | }; | |
372 | ||
373 | void wxBitmap::Render(void) | |
374 | { | |
375 | if (!Ok()) return; | |
376 | ||
377 | #ifdef USE_GDK_IMLIB | |
378 | ||
379 | gdk_imlib_render( M_BMPDATA->m_image, M_BMPDATA->m_image->rgb_width, M_BMPDATA->m_image->rgb_height ); | |
380 | ||
381 | M_BMPDATA->m_pixmap = gdk_imlib_move_image( M_BMPDATA->m_image ); | |
382 | GdkBitmap *mask = gdk_imlib_move_mask( M_BMPDATA->m_image ); | |
383 | if (mask) | |
384 | { | |
385 | M_BMPDATA->m_mask = new wxMask(); | |
386 | M_BMPDATA->m_mask->m_bitmap = mask; | |
387 | }; | |
388 | ||
389 | #endif | |
390 | }; | |
391 | ||
392 |