]>
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 | #include "wx/dcmemory.h" | |
19 | ||
20 | #include <gdk/gdk.h> | |
21 | #include <gdk/gdkprivate.h> | |
22 | ||
23 | // in GTK+ 1.3 gdk_root_parent was renamed into gdk_parent_root | |
24 | #ifdef __WXGTK13__ | |
25 | #define gdk_root_parent gdk_parent_root | |
26 | #else // GTK+ <= 1.2 | |
27 | // need to get the declaration of gdk_root_parent from private header | |
28 | #include <gdk/gdkx.h> | |
29 | #endif // GTK+ 1.3/1.2 | |
30 | ||
31 | //----------------------------------------------------------------------------- | |
32 | // wxMask | |
33 | //----------------------------------------------------------------------------- | |
34 | ||
35 | IMPLEMENT_DYNAMIC_CLASS(wxMask,wxObject) | |
36 | ||
37 | wxMask::wxMask() | |
38 | { | |
39 | m_bitmap = (GdkBitmap *) NULL; | |
40 | } | |
41 | ||
42 | wxMask::wxMask( const wxBitmap& bitmap, const wxColour& colour ) | |
43 | { | |
44 | m_bitmap = (GdkBitmap *) NULL; | |
45 | Create( bitmap, colour ); | |
46 | } | |
47 | ||
48 | wxMask::wxMask( const wxBitmap& bitmap, int paletteIndex ) | |
49 | { | |
50 | m_bitmap = (GdkBitmap *) NULL; | |
51 | Create( bitmap, paletteIndex ); | |
52 | } | |
53 | ||
54 | wxMask::wxMask( const wxBitmap& bitmap ) | |
55 | { | |
56 | m_bitmap = (GdkBitmap *) NULL; | |
57 | Create( bitmap ); | |
58 | } | |
59 | ||
60 | wxMask::~wxMask() | |
61 | { | |
62 | if (m_bitmap) | |
63 | gdk_bitmap_unref( m_bitmap ); | |
64 | } | |
65 | ||
66 | bool wxMask::Create( const wxBitmap& bitmap, | |
67 | const wxColour& colour ) | |
68 | { | |
69 | if (m_bitmap) | |
70 | { | |
71 | gdk_bitmap_unref( m_bitmap ); | |
72 | m_bitmap = (GdkBitmap*) NULL; | |
73 | } | |
74 | ||
75 | wxImage image( bitmap ); | |
76 | if (!image.Ok()) return FALSE; | |
77 | ||
78 | GdkWindow *parent = (GdkWindow*) &gdk_root_parent; | |
79 | m_bitmap = gdk_pixmap_new( parent, image.GetWidth(), image.GetHeight(), 1 ); | |
80 | GdkGC *gc = gdk_gc_new( m_bitmap ); | |
81 | ||
82 | GdkColor color; | |
83 | color.red = 65000; | |
84 | color.green = 65000; | |
85 | color.blue = 65000; | |
86 | color.pixel = 1; | |
87 | gdk_gc_set_foreground( gc, &color ); | |
88 | gdk_gc_set_fill( gc, GDK_SOLID ); | |
89 | gdk_draw_rectangle( m_bitmap, gc, TRUE, 0, 0, image.GetWidth(), image.GetHeight() ); | |
90 | ||
91 | unsigned char *data = image.GetData(); | |
92 | int index = 0; | |
93 | ||
94 | unsigned char red = colour.Red(); | |
95 | unsigned char green = colour.Green(); | |
96 | unsigned char blue = colour.Blue(); | |
97 | ||
98 | GdkVisual *visual = gdk_visual_get_system(); | |
99 | int bpp = visual->depth; | |
100 | if ((bpp == 16) && (visual->red_mask != 0xf800)) bpp = 15; | |
101 | if (bpp == 15) | |
102 | { | |
103 | red = red & 0xf8; | |
104 | blue = blue & 0xf8; | |
105 | green = green & 0xf8; | |
106 | } | |
107 | if (bpp == 16) | |
108 | { | |
109 | red = red & 0xf8; | |
110 | blue = blue & 0xfc; | |
111 | green = green & 0xf8; | |
112 | } | |
113 | ||
114 | color.red = 0; | |
115 | color.green = 0; | |
116 | color.blue = 0; | |
117 | color.pixel = 0; | |
118 | gdk_gc_set_foreground( gc, &color ); | |
119 | ||
120 | for (int j = 0; j < image.GetHeight(); j++) | |
121 | { | |
122 | int start_x = -1; | |
123 | int i; | |
124 | for (i = 0; i < image.GetWidth(); i++) | |
125 | { | |
126 | if ((data[index] == red) && | |
127 | (data[index+1] == green) && | |
128 | (data[index+2] == blue)) | |
129 | { | |
130 | if (start_x == -1) | |
131 | start_x = i; | |
132 | } | |
133 | else | |
134 | { | |
135 | if (start_x != -1) | |
136 | { | |
137 | gdk_draw_line( m_bitmap, gc, start_x, j, i-1, j ); | |
138 | start_x = -1; | |
139 | } | |
140 | } | |
141 | index += 3; | |
142 | } | |
143 | if (start_x != -1) | |
144 | gdk_draw_line( m_bitmap, gc, start_x, j, i, j ); | |
145 | } | |
146 | ||
147 | gdk_gc_unref( gc ); | |
148 | ||
149 | return TRUE; | |
150 | } | |
151 | ||
152 | bool wxMask::Create( const wxBitmap& WXUNUSED(bitmap), | |
153 | int WXUNUSED(paletteIndex) ) | |
154 | { | |
155 | if (m_bitmap) | |
156 | { | |
157 | gdk_bitmap_unref( m_bitmap ); | |
158 | m_bitmap = (GdkBitmap*) NULL; | |
159 | } | |
160 | ||
161 | wxFAIL_MSG( wxT("not implemented") ); | |
162 | ||
163 | return FALSE; | |
164 | } | |
165 | ||
166 | bool wxMask::Create( const wxBitmap& bitmap ) | |
167 | { | |
168 | if (m_bitmap) | |
169 | { | |
170 | gdk_bitmap_unref( m_bitmap ); | |
171 | m_bitmap = (GdkBitmap*) NULL; | |
172 | } | |
173 | ||
174 | if (!bitmap.Ok()) return FALSE; | |
175 | ||
176 | wxCHECK_MSG( bitmap.GetBitmap(), FALSE, wxT("Cannot create mask from colour bitmap") ); | |
177 | ||
178 | m_bitmap = gdk_pixmap_new( (GdkWindow*) &gdk_root_parent, bitmap.GetWidth(), bitmap.GetHeight(), 1 ); | |
179 | ||
180 | if (!m_bitmap) return FALSE; | |
181 | ||
182 | GdkGC *gc = gdk_gc_new( m_bitmap ); | |
183 | ||
184 | gdk_draw_bitmap( m_bitmap, gc, bitmap.GetBitmap(), 0, 0, 0, 0, bitmap.GetWidth(), bitmap.GetHeight() ); | |
185 | ||
186 | gdk_gc_unref( gc ); | |
187 | ||
188 | return TRUE; | |
189 | } | |
190 | ||
191 | GdkBitmap *wxMask::GetBitmap() const | |
192 | { | |
193 | return m_bitmap; | |
194 | } | |
195 | ||
196 | //----------------------------------------------------------------------------- | |
197 | // wxBitmap | |
198 | //----------------------------------------------------------------------------- | |
199 | ||
200 | class wxBitmapRefData: public wxObjectRefData | |
201 | { | |
202 | public: | |
203 | wxBitmapRefData(); | |
204 | ~wxBitmapRefData(); | |
205 | ||
206 | GdkPixmap *m_pixmap; | |
207 | GdkBitmap *m_bitmap; | |
208 | wxMask *m_mask; | |
209 | int m_width; | |
210 | int m_height; | |
211 | int m_bpp; | |
212 | wxPalette *m_palette; | |
213 | }; | |
214 | ||
215 | wxBitmapRefData::wxBitmapRefData() | |
216 | { | |
217 | m_pixmap = (GdkPixmap *) NULL; | |
218 | m_bitmap = (GdkBitmap *) NULL; | |
219 | m_mask = (wxMask *) NULL; | |
220 | m_width = 0; | |
221 | m_height = 0; | |
222 | m_bpp = 0; | |
223 | m_palette = (wxPalette *) NULL; | |
224 | } | |
225 | ||
226 | wxBitmapRefData::~wxBitmapRefData() | |
227 | { | |
228 | if (m_pixmap) gdk_pixmap_unref( m_pixmap ); | |
229 | if (m_bitmap) gdk_bitmap_unref( m_bitmap ); | |
230 | if (m_mask) delete m_mask; | |
231 | if (m_palette) delete m_palette; | |
232 | } | |
233 | ||
234 | //----------------------------------------------------------------------------- | |
235 | ||
236 | #define M_BMPDATA ((wxBitmapRefData *)m_refData) | |
237 | ||
238 | IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxGDIObject) | |
239 | ||
240 | wxBitmap::wxBitmap() | |
241 | { | |
242 | if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this); | |
243 | } | |
244 | ||
245 | wxBitmap::wxBitmap( int width, int height, int depth ) | |
246 | { | |
247 | wxCHECK_RET( (width > 0) && (height > 0), wxT("invalid bitmap size") ) | |
248 | ||
249 | GdkWindow *parent = (GdkWindow*) &gdk_root_parent; | |
250 | if (depth == -1) depth = gdk_window_get_visual( parent )->depth; | |
251 | ||
252 | wxCHECK_RET( (depth == gdk_window_get_visual( parent )->depth) || | |
253 | (depth == 1), wxT("invalid bitmap depth") ) | |
254 | ||
255 | m_refData = new wxBitmapRefData(); | |
256 | M_BMPDATA->m_mask = (wxMask *) NULL; | |
257 | M_BMPDATA->m_width = width; | |
258 | M_BMPDATA->m_height = height; | |
259 | if (depth == 1) | |
260 | { | |
261 | M_BMPDATA->m_bitmap = gdk_pixmap_new( parent, width, height, 1 ); | |
262 | M_BMPDATA->m_bpp = 1; | |
263 | } | |
264 | else | |
265 | { | |
266 | M_BMPDATA->m_pixmap = gdk_pixmap_new( parent, width, height, depth ); | |
267 | M_BMPDATA->m_bpp = gdk_window_get_visual( parent )->depth; | |
268 | } | |
269 | ||
270 | if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this); | |
271 | } | |
272 | ||
273 | bool wxBitmap::CreateFromXpm( const char **bits ) | |
274 | { | |
275 | wxCHECK_MSG( bits != NULL, FALSE, wxT("invalid bitmap data") ) | |
276 | ||
277 | m_refData = new wxBitmapRefData(); | |
278 | ||
279 | GdkBitmap *mask = (GdkBitmap*) NULL; | |
280 | GdkWindow *parent = (GdkWindow*) &gdk_root_parent; | |
281 | ||
282 | M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm_d( parent, &mask, NULL, (gchar **) bits ); | |
283 | ||
284 | wxCHECK_MSG( M_BMPDATA->m_pixmap, FALSE, wxT("couldn't create pixmap") ); | |
285 | ||
286 | if (mask) | |
287 | { | |
288 | M_BMPDATA->m_mask = new wxMask(); | |
289 | M_BMPDATA->m_mask->m_bitmap = mask; | |
290 | } | |
291 | ||
292 | gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) ); | |
293 | ||
294 | M_BMPDATA->m_bpp = gdk_window_get_visual( parent )->depth; // ? | |
295 | if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this); | |
296 | ||
297 | return TRUE; | |
298 | } | |
299 | ||
300 | wxBitmap::wxBitmap( const wxBitmap& bmp ) | |
301 | { | |
302 | Ref( bmp ); | |
303 | ||
304 | if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this); | |
305 | } | |
306 | ||
307 | wxBitmap::wxBitmap( const wxString &filename, int type ) | |
308 | { | |
309 | LoadFile( filename, type ); | |
310 | ||
311 | if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this); | |
312 | } | |
313 | ||
314 | wxBitmap::wxBitmap( const char bits[], int width, int height, int WXUNUSED(depth)) | |
315 | { | |
316 | m_refData = new wxBitmapRefData(); | |
317 | ||
318 | M_BMPDATA->m_mask = (wxMask *) NULL; | |
319 | M_BMPDATA->m_bitmap = | |
320 | gdk_bitmap_create_from_data( (GdkWindow*) &gdk_root_parent, (gchar *) bits, width, height ); | |
321 | M_BMPDATA->m_width = width; | |
322 | M_BMPDATA->m_height = height; | |
323 | M_BMPDATA->m_bpp = 1; | |
324 | ||
325 | wxCHECK_RET( M_BMPDATA->m_bitmap, wxT("couldn't create bitmap") ); | |
326 | ||
327 | if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this); | |
328 | } | |
329 | ||
330 | wxBitmap::~wxBitmap() | |
331 | { | |
332 | if (wxTheBitmapList) wxTheBitmapList->DeleteObject(this); | |
333 | } | |
334 | ||
335 | wxBitmap& wxBitmap::operator = ( const wxBitmap& bmp ) | |
336 | { | |
337 | if (*this == bmp) return (*this); | |
338 | Ref( bmp ); | |
339 | return *this; | |
340 | } | |
341 | ||
342 | bool wxBitmap::operator == ( const wxBitmap& bmp ) | |
343 | { | |
344 | return m_refData == bmp.m_refData; | |
345 | } | |
346 | ||
347 | bool wxBitmap::operator != ( const wxBitmap& bmp ) | |
348 | { | |
349 | return m_refData != bmp.m_refData; | |
350 | } | |
351 | ||
352 | bool wxBitmap::Ok() const | |
353 | { | |
354 | return (m_refData != NULL); | |
355 | } | |
356 | ||
357 | int wxBitmap::GetHeight() const | |
358 | { | |
359 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); | |
360 | ||
361 | return M_BMPDATA->m_height; | |
362 | } | |
363 | ||
364 | int wxBitmap::GetWidth() const | |
365 | { | |
366 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); | |
367 | ||
368 | return M_BMPDATA->m_width; | |
369 | } | |
370 | ||
371 | int wxBitmap::GetDepth() const | |
372 | { | |
373 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); | |
374 | ||
375 | return M_BMPDATA->m_bpp; | |
376 | } | |
377 | ||
378 | wxMask *wxBitmap::GetMask() const | |
379 | { | |
380 | wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") ); | |
381 | ||
382 | return M_BMPDATA->m_mask; | |
383 | } | |
384 | ||
385 | void wxBitmap::SetMask( wxMask *mask ) | |
386 | { | |
387 | wxCHECK_RET( Ok(), wxT("invalid bitmap") ); | |
388 | ||
389 | if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask; | |
390 | ||
391 | M_BMPDATA->m_mask = mask; | |
392 | } | |
393 | ||
394 | wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const | |
395 | { | |
396 | wxCHECK_MSG( Ok() && | |
397 | (rect.x >= 0) && (rect.y >= 0) && | |
398 | (rect.x+rect.width <= M_BMPDATA->m_width) && (rect.y+rect.height <= M_BMPDATA->m_height), | |
399 | wxNullBitmap, wxT("invalid bitmap or bitmap region") ); | |
400 | ||
401 | wxBitmap ret( rect.width, rect.height, M_BMPDATA->m_bpp ); | |
402 | wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") ); | |
403 | ||
404 | if (ret.GetPixmap()) | |
405 | { | |
406 | GdkGC *gc = gdk_gc_new( ret.GetPixmap() ); | |
407 | gdk_draw_pixmap( ret.GetPixmap(), gc, GetPixmap(), rect.x, rect.y, 0, 0, rect.width, rect.height ); | |
408 | gdk_gc_destroy( gc ); | |
409 | } | |
410 | else | |
411 | { | |
412 | GdkGC *gc = gdk_gc_new( ret.GetBitmap() ); | |
413 | gdk_draw_bitmap( ret.GetBitmap(), gc, GetBitmap(), rect.x, rect.y, 0, 0, rect.width, rect.height ); | |
414 | gdk_gc_destroy( gc ); | |
415 | } | |
416 | ||
417 | if (GetMask()) | |
418 | { | |
419 | wxMask *mask = new wxMask; | |
420 | GdkWindow *parent = (GdkWindow*) &gdk_root_parent; | |
421 | mask->m_bitmap = gdk_pixmap_new( parent, rect.width, rect.height, 1 ); | |
422 | ||
423 | GdkGC *gc = gdk_gc_new( mask->m_bitmap ); | |
424 | gdk_draw_bitmap( mask->m_bitmap, gc, M_BMPDATA->m_mask->m_bitmap, 0, 0, rect.x, rect.y, rect.width, rect.height ); | |
425 | gdk_gc_destroy( gc ); | |
426 | ||
427 | ret.SetMask( mask ); | |
428 | } | |
429 | ||
430 | return ret; | |
431 | } | |
432 | ||
433 | bool wxBitmap::SaveFile( const wxString &name, int type, wxPalette *WXUNUSED(palette) ) | |
434 | { | |
435 | wxCHECK_MSG( Ok(), FALSE, wxT("invalid bitmap") ); | |
436 | ||
437 | // Try to save the bitmap via wxImage handlers: | |
438 | { | |
439 | wxImage image( *this ); | |
440 | if (image.Ok()) return image.SaveFile( name, type ); | |
441 | } | |
442 | ||
443 | return FALSE; | |
444 | } | |
445 | ||
446 | bool wxBitmap::LoadFile( const wxString &name, int type ) | |
447 | { | |
448 | UnRef(); | |
449 | ||
450 | if (!wxFileExists(name)) return FALSE; | |
451 | ||
452 | if (type == wxBITMAP_TYPE_XPM) | |
453 | { | |
454 | m_refData = new wxBitmapRefData(); | |
455 | ||
456 | GdkBitmap *mask = (GdkBitmap*) NULL; | |
457 | GdkWindow *parent = (GdkWindow*) &gdk_root_parent; | |
458 | ||
459 | M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm( parent, &mask, NULL, name.fn_str() ); | |
460 | ||
461 | if (mask) | |
462 | { | |
463 | M_BMPDATA->m_mask = new wxMask(); | |
464 | M_BMPDATA->m_mask->m_bitmap = mask; | |
465 | } | |
466 | ||
467 | gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) ); | |
468 | M_BMPDATA->m_bpp = gdk_window_get_visual( parent )->depth; | |
469 | } | |
470 | else // try if wxImage can load it | |
471 | { | |
472 | wxImage image; | |
473 | if (!image.LoadFile( name, type )) return FALSE; | |
474 | if (image.Ok()) *this = image.ConvertToBitmap(); | |
475 | else return FALSE; | |
476 | } | |
477 | ||
478 | return TRUE; | |
479 | } | |
480 | ||
481 | wxPalette *wxBitmap::GetPalette() const | |
482 | { | |
483 | if (!Ok()) return (wxPalette *) NULL; | |
484 | ||
485 | return M_BMPDATA->m_palette; | |
486 | } | |
487 | ||
488 | void wxBitmap::SetHeight( int height ) | |
489 | { | |
490 | if (!m_refData) m_refData = new wxBitmapRefData(); | |
491 | ||
492 | M_BMPDATA->m_height = height; | |
493 | } | |
494 | ||
495 | void wxBitmap::SetWidth( int width ) | |
496 | { | |
497 | if (!m_refData) m_refData = new wxBitmapRefData(); | |
498 | ||
499 | M_BMPDATA->m_width = width; | |
500 | } | |
501 | ||
502 | void wxBitmap::SetDepth( int depth ) | |
503 | { | |
504 | if (!m_refData) m_refData = new wxBitmapRefData(); | |
505 | ||
506 | M_BMPDATA->m_bpp = depth; | |
507 | } | |
508 | ||
509 | void wxBitmap::SetPixmap( GdkPixmap *pixmap ) | |
510 | { | |
511 | if (!m_refData) m_refData = new wxBitmapRefData(); | |
512 | ||
513 | M_BMPDATA->m_pixmap = pixmap; | |
514 | } | |
515 | ||
516 | void wxBitmap::SetBitmap( GdkPixmap *bitmap ) | |
517 | { | |
518 | if (!m_refData) m_refData = new wxBitmapRefData(); | |
519 | ||
520 | M_BMPDATA->m_bitmap = bitmap; | |
521 | } | |
522 | ||
523 | GdkPixmap *wxBitmap::GetPixmap() const | |
524 | { | |
525 | wxCHECK_MSG( Ok(), (GdkPixmap *) NULL, wxT("invalid bitmap") ); | |
526 | ||
527 | return M_BMPDATA->m_pixmap; | |
528 | } | |
529 | ||
530 | GdkBitmap *wxBitmap::GetBitmap() const | |
531 | { | |
532 | wxCHECK_MSG( Ok(), (GdkBitmap *) NULL, wxT("invalid bitmap") ); | |
533 | ||
534 | return M_BMPDATA->m_bitmap; | |
535 | } |