]> git.saurik.com Git - wxWidgets.git/blob - src/x11/bitmap.cpp
Add various selection menu items to the sample.
[wxWidgets.git] / src / x11 / bitmap.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/x11/bitmap.cpp
3 // Purpose: wxBitmap
4 // Author: Julian Smart, Robert Roebling
5 // Modified by:
6 // Created: 17/09/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart, Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // for compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #include "wx/bitmap.h"
16
17 #ifndef WX_PRECOMP
18 #include "wx/log.h"
19 #include "wx/app.h"
20 #include "wx/dcmemory.h"
21 #include "wx/icon.h"
22 #include "wx/math.h"
23 #include "wx/image.h"
24 #endif
25
26 #include "wx/x11/private.h"
27
28 /* No point in using libXPM for NanoX */
29 #if wxUSE_NANOX
30 #undef wxHAVE_LIB_XPM
31 #define wxHAVE_LIB_XPM 0
32
33 // Copy from the drawable to the wxImage
34 bool wxGetImageFromDrawable(GR_DRAW_ID drawable, int srcX, int srcY, int width, int height, wxImage& image);
35 #endif
36
37 static WXPixmap wxGetSubPixmap( WXDisplay* xdisplay, WXPixmap xpixmap,
38 int x, int y, int width, int height,
39 int depth );
40
41 #if wxUSE_XPM
42 #if wxHAVE_LIB_XPM
43 #include <X11/xpm.h>
44 #else
45 #include "wx/xpmdecod.h"
46 #include "wx/wfstream.h"
47 #endif
48 #endif
49
50 //-----------------------------------------------------------------------------
51 // wxMask
52 //-----------------------------------------------------------------------------
53
54 IMPLEMENT_DYNAMIC_CLASS(wxMask,wxObject)
55
56 wxMask::wxMask()
57 {
58 m_bitmap = NULL;
59 m_display = NULL;
60 }
61
62 wxMask::wxMask(const wxMask& mask)
63 {
64 m_display = mask.m_display;
65 if ( !mask.m_bitmap )
66 {
67 m_bitmap = NULL;
68 return;
69 }
70
71 m_size = mask.m_size;
72
73 // Duplicate the mask bitmap using the existing wxGetSubPixmap() function.
74 // There are probably/surely better ways to do it.
75 m_bitmap = wxGetSubPixmap(m_display, mask.m_bitmap,
76 0, 0, m_size.x, m_size.y,
77 1);
78 }
79
80 wxMask::wxMask( const wxBitmap& bitmap, const wxColour& colour )
81 {
82 m_bitmap = NULL;
83 Create( bitmap, colour );
84 }
85
86 wxMask::wxMask( const wxBitmap& bitmap, int paletteIndex )
87 {
88 m_bitmap = NULL;
89 Create( bitmap, paletteIndex );
90 }
91
92 wxMask::wxMask( const wxBitmap& bitmap )
93 {
94 m_bitmap = NULL;
95 Create( bitmap );
96 }
97
98 wxMask::~wxMask()
99 {
100 if (m_bitmap)
101 XFreePixmap( (Display*) m_display, (Pixmap) m_bitmap );
102 }
103
104 bool wxMask::Create( const wxBitmap& bitmap,
105 const wxColour& colour )
106 {
107 m_size = bitmap.GetSize();
108
109 #if !wxUSE_NANOX
110 if (m_bitmap)
111 {
112 XFreePixmap( (Display*) m_display, (Pixmap) m_bitmap );
113 m_bitmap = NULL;
114 }
115
116 m_display = bitmap.GetDisplay();
117
118 wxImage image = bitmap.ConvertToImage();
119 if (!image.IsOk()) return false;
120
121 m_display = bitmap.GetDisplay();
122
123 Display *xdisplay = (Display*) m_display;
124 int xscreen = DefaultScreen( xdisplay );
125 Window xroot = RootWindow( xdisplay, xscreen );
126
127 m_bitmap = (WXPixmap) XCreatePixmap( xdisplay, xroot, image.GetWidth(), image.GetHeight(), 1 );
128 GC gc = XCreateGC( xdisplay, (Pixmap) m_bitmap, 0, NULL );
129
130 XSetForeground( xdisplay, gc, WhitePixel(xdisplay,xscreen) );
131 XSetFillStyle( xdisplay, gc, FillSolid );
132 XFillRectangle( xdisplay, (Pixmap) m_bitmap, gc, 0, 0, image.GetWidth(), image.GetHeight() );
133
134 unsigned char *data = image.GetData();
135 int index = 0;
136
137 unsigned char red = colour.Red();
138 unsigned char green = colour.Green();
139 unsigned char blue = colour.Blue();
140
141 int bpp = wxTheApp->GetVisualInfo(m_display)->m_visualDepth;
142
143 if (bpp == 15)
144 {
145 red &= 0xf8;
146 green &= 0xf8;
147 blue &= 0xf8;
148 } else
149 if (bpp == 16)
150 {
151 red &= 0xf8;
152 green &= 0xfc;
153 blue &= 0xf8;
154 } else
155 if (bpp == 12)
156 {
157 red &= 0xf0;
158 green &= 0xf0;
159 blue &= 0xf0;
160 }
161
162 XSetForeground( xdisplay, gc, BlackPixel(xdisplay,xscreen) );
163
164 int width = image.GetWidth();
165 int height = image.GetHeight();
166 for (int j = 0; j < height; j++)
167 {
168 int start_x = -1;
169 int i;
170 for (i = 0; i < width; i++)
171 {
172 if ((data[index] == red) &&
173 (data[index+1] == green) &&
174 (data[index+2] == blue))
175 {
176 if (start_x == -1)
177 start_x = i;
178 }
179 else
180 {
181 if (start_x != -1)
182 {
183 XDrawLine( xdisplay, (Pixmap) m_bitmap, gc, start_x, j, i-1, j );
184 start_x = -1;
185 }
186 }
187 index += 3;
188 }
189 if (start_x != -1)
190 XDrawLine( xdisplay, (Pixmap) m_bitmap, gc, start_x, j, i, j );
191 }
192
193 XFreeGC( xdisplay, gc );
194
195 return true;
196 #else
197 return false;
198 #endif
199 // wxUSE_NANOX
200 }
201
202 bool wxMask::Create( const wxBitmap& bitmap, int paletteIndex )
203 {
204 unsigned char r,g,b;
205 wxPalette *pal = bitmap.GetPalette();
206
207 wxCHECK_MSG( pal, false, wxT("Cannot create mask from bitmap without palette") );
208
209 pal->GetRGB(paletteIndex, &r, &g, &b);
210
211 return Create(bitmap, wxColour(r, g, b));
212 }
213
214 bool wxMask::Create( const wxBitmap& bitmap )
215 {
216 #if !wxUSE_NANOX
217 if (m_bitmap)
218 {
219 XFreePixmap( (Display*) m_display, (Pixmap) m_bitmap );
220 m_bitmap = NULL;
221 }
222
223 if (!bitmap.IsOk()) return false;
224
225 wxCHECK_MSG( bitmap.GetBitmap(), false, wxT("Cannot create mask from colour bitmap") );
226
227 m_display = bitmap.GetDisplay();
228
229 int xscreen = DefaultScreen( (Display*) m_display );
230 Window xroot = RootWindow( (Display*) m_display, xscreen );
231
232 m_bitmap = (WXPixmap) XCreatePixmap( (Display*) m_display, xroot, bitmap.GetWidth(), bitmap.GetHeight(), 1 );
233
234 if (!m_bitmap) return false;
235
236 GC gc = XCreateGC( (Display*) m_display, (Pixmap) m_bitmap, 0, NULL );
237
238 XCopyPlane( (Display*) m_display, (Pixmap) bitmap.GetBitmap(), (Pixmap) m_bitmap,
239 gc, 0, 0, bitmap.GetWidth(), bitmap.GetHeight(), 0, 0, 1 );
240
241 XFreeGC( (Display*) m_display, gc );
242
243 return true;
244 #else
245 return false;
246 #endif
247 // wxUSE_NANOX
248 }
249
250 //-----------------------------------------------------------------------------
251 // wxBitmapRefData
252 //-----------------------------------------------------------------------------
253
254 class wxBitmapRefData : public wxGDIRefData
255 {
256 public:
257 wxBitmapRefData();
258 wxBitmapRefData(const wxBitmapRefData& data);
259 virtual ~wxBitmapRefData();
260
261 // shouldn't be called more than once as it doesn't free the existing data
262 bool Create(int width, int height, int depth);
263
264 virtual bool IsOk() const { return m_pixmap || m_bitmap; }
265
266 Pixmap m_pixmap;
267 Pixmap m_bitmap;
268 Display *m_display;
269 wxMask *m_mask;
270 int m_width;
271 int m_height;
272 int m_bpp;
273 wxPalette *m_palette;
274 };
275
276 wxBitmapRefData::wxBitmapRefData()
277 {
278 m_pixmap = 0;
279 m_bitmap = 0;
280 m_display = NULL;
281 m_mask = NULL;
282 m_width = 0;
283 m_height = 0;
284 m_bpp = 0;
285 m_palette = NULL;
286 }
287
288 wxBitmapRefData::wxBitmapRefData(const wxBitmapRefData& data)
289 {
290 m_pixmap = 0;
291 m_bitmap = 0;
292 m_display = data.m_display;
293 m_mask = NULL; // FIXME: should copy
294 m_palette = NULL; // FIXME: should copy
295
296 Create(data.m_width, data.m_height, data.m_bpp);
297 }
298
299 bool wxBitmapRefData::Create(int width, int height, int depth)
300 {
301 m_width = width;
302 m_height = height;
303 m_bpp = depth;
304
305 m_display = wxGlobalDisplay();
306
307 wxCHECK_MSG( m_display, false, wxT("No display") );
308
309 int xscreen = DefaultScreen(m_display);
310 int bpp = DefaultDepth(m_display, xscreen);
311 if ( depth == -1 )
312 depth = bpp;
313
314 wxCHECK_MSG( (depth == bpp) || (depth == 1), false,
315 wxT("invalid bitmap depth") );
316
317 #if wxUSE_NANOX
318 m_pixmap = (WXPixmap) GrNewPixmap(width, height, NULL);
319 #else // !wxUSE_NANOX
320 Window xroot = RootWindow(m_display, xscreen);
321
322 *(depth == 1 ? &m_bitmap : &m_pixmap) =
323 XCreatePixmap(m_display, xroot, width, height, depth);
324 #endif // wxUSE_NANOX/!wxUSE_NANOX
325
326 wxCHECK_MSG( m_pixmap || m_bitmap, false, wxT("Bitmap creation failed") );
327
328 return true;
329 }
330
331 wxBitmapRefData::~wxBitmapRefData()
332 {
333 if (m_pixmap)
334 XFreePixmap(m_display, m_pixmap);
335 if (m_bitmap)
336 XFreePixmap(m_display, m_bitmap);
337 delete m_mask;
338 delete m_palette;
339 }
340
341 //-----------------------------------------------------------------------------
342
343 // helper function
344
345 static WXPixmap wxGetSubPixmap( WXDisplay* xdisplay, WXPixmap xpixmap,
346 int x, int y, int width, int height,
347 int depth )
348 {
349 Display * const dpy = (Display *)xdisplay;
350
351 int xscreen = DefaultScreen( dpy );
352 Window xroot = RootWindow( dpy, xscreen );
353 Visual* xvisual = DefaultVisual( dpy, xscreen );
354
355 XImage* ximage = XCreateImage( dpy, xvisual, depth,
356 ZPixmap, 0, 0, width, height, 32, 0 );
357 ximage->data = (char*)malloc( ximage->bytes_per_line * ximage->height );
358 ximage = XGetSubImage( dpy, (Pixmap)xpixmap,
359 x, y, width, height,
360 AllPlanes, ZPixmap, ximage, 0, 0 );
361
362 GC gc = XCreateGC( dpy, (Pixmap)xpixmap, 0, NULL );
363 Pixmap ret = XCreatePixmap( dpy, xroot,
364 width, height, depth );
365
366 XPutImage( dpy, ret, gc, ximage,
367 0, 0, 0, 0, width, height );
368 XDestroyImage( ximage );
369 XFreeGC( dpy, gc );
370
371 return (WXPixmap)ret;
372 }
373
374
375 //-----------------------------------------------------------------------------
376 // wxBitmap
377 //-----------------------------------------------------------------------------
378
379 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
380
381 IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxGDIObject)
382
383 bool wxBitmap::Create( int width, int height, int depth )
384 {
385 UnRef();
386
387 wxCHECK_MSG( (width > 0) && (height > 0), false, wxT("invalid bitmap size") );
388
389 m_refData = new wxBitmapRefData();
390
391 return M_BMPDATA->Create(width, height, depth);
392 }
393
394 bool wxBitmap::Create(const void* data, wxBitmapType type,
395 int width, int height, int depth)
396 {
397 UnRef();
398
399 wxBitmapHandler *handler = FindHandler(type);
400
401 if ( handler == NULL ) {
402 wxLogWarning(wxT("no data bitmap handler for type %ld defined."),
403 (long)type);
404
405 return false;
406 }
407
408 return handler->Create(this, data, type, width, height, depth);
409 }
410
411 bool wxBitmap::Create(WXPixmap pixmap)
412 {
413 UnRef();
414 Pixmap xpixmap = (Pixmap)pixmap;
415 Display* xdisplay = wxGlobalDisplay();
416 int xscreen = DefaultScreen( xdisplay );
417 Window xroot = RootWindow( xdisplay, xscreen );
418
419 // make a copy of the Pixmap
420 Window root;
421 int x, y;
422 unsigned width, height, border, depth;
423
424 XGetGeometry( xdisplay, (Drawable)xpixmap, &root, &x, &y,
425 &width, &height, &border, &depth );
426 Pixmap copy = XCreatePixmap( xdisplay, xroot, width, height, depth );
427
428 GC gc = XCreateGC( xdisplay, copy, 0, NULL );
429 XCopyArea( xdisplay, xpixmap, copy, gc, 0, 0, width, height, 0, 0 );
430 XFreeGC( xdisplay, gc );
431
432 // fill in ref data
433 wxBitmapRefData* ref = new wxBitmapRefData();
434
435 if( depth == 1 )
436 ref->m_bitmap = copy;
437 else
438 ref->m_pixmap = copy;
439
440 ref->m_display = xdisplay;
441 ref->m_width = width;
442 ref->m_height = height;
443 ref->m_bpp = depth;
444
445 m_refData = ref;
446
447 return true;
448 }
449
450 wxBitmap::wxBitmap(const char* const* bits)
451 {
452 Create(bits, wxBITMAP_TYPE_XPM_DATA, 0, 0, 0);
453 }
454
455 wxGDIRefData *wxBitmap::CreateGDIRefData() const
456 {
457 return new wxBitmapRefData;
458 }
459
460 wxGDIRefData *wxBitmap::CloneGDIRefData(const wxGDIRefData *data) const
461 {
462 return new wxBitmapRefData(*static_cast<const wxBitmapRefData *>(data));
463 }
464
465 bool wxBitmap::CreateFromImage( const wxImage& image, int depth )
466 {
467 #if wxUSE_NANOX
468 if (!image.IsOk())
469 {
470 wxASSERT_MSG(image.IsOk(), wxT("Invalid wxImage passed to wxBitmap::CreateFromImage."));
471 return false;
472 }
473
474 int w = image.GetWidth();
475 int h = image.GetHeight();
476
477 if (!Create(w, h, depth))
478 return false;
479
480 // Unfortunately the mask has to be screen-depth since
481 // 1-bpp bitmaps don't seem to be supported
482 // TODO: implement transparent drawing, presumably
483 // by doing several blits as per the Windows
484 // implementation because Nano-X doesn't support
485 // XSetClipMask.
486 // TODO: could perhaps speed this function up
487 // by making a buffer of pixel values,
488 // and then calling GrArea to write that to the
489 // pixmap. See demos/nxroach.c.
490
491 bool hasMask = image.HasMask();
492
493 GC pixmapGC = GrNewGC();
494 Pixmap pixmap = (Pixmap) GetPixmap();
495
496 GC maskGC = 0;
497 Pixmap maskPixmap = 0;
498
499 unsigned char maskR = 0;
500 unsigned char maskG = 0;
501 unsigned char maskB = 0;
502
503 if (hasMask)
504 {
505 maskR = image.GetMaskRed();
506 maskG = image.GetMaskGreen();
507 maskB = image.GetMaskBlue();
508
509 maskGC = GrNewGC();
510 maskPixmap = GrNewPixmap(w, h, 0);
511 if (!maskPixmap)
512 hasMask = false;
513 else
514 {
515 wxMask* mask = new wxMask;
516 mask->SetBitmap((WXPixmap) maskPixmap);
517 SetMask(mask);
518 }
519 }
520
521 GR_COLOR lastPixmapColour = 0;
522 GR_COLOR lastMaskColour = 0;
523
524 int i, j;
525 for (i = 0; i < w; i++)
526 {
527 for (j = 0; j < h; j++)
528 {
529 unsigned char red = image.GetRed(i, j);
530 unsigned char green = image.GetGreen(i, j);
531 unsigned char blue = image.GetBlue(i, j);
532
533 GR_COLOR colour = GR_RGB(red, green, blue);
534
535 // Efficiency measure
536 if (colour != lastPixmapColour || (i == 0 && j == 0))
537 {
538 GrSetGCForeground(pixmapGC, colour);
539 lastPixmapColour = colour;
540 }
541
542 GrPoint(pixmap, pixmapGC, i, j);
543
544 if (hasMask)
545 {
546 // scan the bitmap for the transparent colour and set the corresponding
547 // pixels in the mask to BLACK and the rest to WHITE
548 if (maskR == red && maskG == green && maskB == blue)
549 {
550 colour = GR_RGB(0, 0, 0);
551 }
552 else
553 {
554 colour = GR_RGB(255, 255, 255);
555 }
556 if (colour != lastMaskColour || (i == 0 && j == 0))
557 {
558 GrSetGCForeground(maskGC, colour);
559 lastMaskColour = colour;
560 }
561 GrPoint(maskPixmap, maskGC, i, j);
562 }
563 }
564 }
565
566 GrDestroyGC(pixmapGC);
567 if (hasMask)
568 GrDestroyGC(maskGC);
569
570 return true;
571 #else
572 // !wxUSE_NANOX
573
574 UnRef();
575
576 wxCHECK_MSG( image.IsOk(), false, wxT("invalid image") );
577 wxCHECK_MSG( depth == -1, false, wxT("invalid bitmap depth") );
578
579 m_refData = new wxBitmapRefData();
580
581 M_BMPDATA->m_display = wxGlobalDisplay();
582
583 Display *xdisplay = (Display*) M_BMPDATA->m_display;
584
585 int xscreen = DefaultScreen( xdisplay );
586 Window xroot = RootWindow( xdisplay, xscreen );
587 Visual* xvisual = DefaultVisual( xdisplay, xscreen );
588
589 int bpp = wxTheApp->GetVisualInfo(M_BMPDATA->m_display)->m_visualDepth;
590
591 int width = image.GetWidth();
592 int height = image.GetHeight();
593 M_BMPDATA->m_width = width;
594 M_BMPDATA->m_height = height;
595
596 if (depth != 1) depth = bpp;
597 M_BMPDATA->m_bpp = depth;
598
599 if (depth == 1)
600 {
601 wxFAIL_MSG( wxT("mono images later") );
602 }
603 else
604 {
605 // Create image
606
607 XImage *data_image = XCreateImage( xdisplay, xvisual, bpp, ZPixmap, 0, 0, width, height, 32, 0 );
608 data_image->data = (char*) malloc( data_image->bytes_per_line * data_image->height );
609
610 if (data_image->data == NULL)
611 {
612 wxLogError( wxT("Out of memory.") ); // TODO clean
613 return false;
614 }
615
616 M_BMPDATA->m_pixmap = XCreatePixmap( xdisplay, xroot, width, height, depth );
617
618 // Create mask if necessary
619 const bool hasMask = image.HasMask();
620
621 XImage *mask_image = NULL;
622 if ( hasMask )
623 {
624 mask_image = XCreateImage( xdisplay, xvisual, 1, ZPixmap, 0, 0, width, height, 32, 0 );
625 mask_image->data = (char*) malloc( mask_image->bytes_per_line * mask_image->height );
626
627 if (mask_image->data == NULL)
628 {
629 wxLogError( wxT("Out of memory.") ); // TODO clean
630 return false;
631 }
632
633 wxMask *mask = new wxMask();
634 mask->SetDisplay( xdisplay );
635 mask->SetBitmap( (WXPixmap) XCreatePixmap( xdisplay, xroot, width, height, 1 ) );
636
637 SetMask( mask );
638 }
639
640 if (bpp < 8) bpp = 8;
641
642 // Render
643
644 enum byte_order { RGB, RBG, BRG, BGR, GRB, GBR };
645 byte_order b_o = RGB;
646
647 wxXVisualInfo* vi = wxTheApp->GetVisualInfo(M_BMPDATA->m_display);
648 unsigned long greenMask = vi->m_visualGreenMask,
649 redMask = vi->m_visualRedMask,
650 blueMask = vi->m_visualBlueMask;
651
652 if (bpp > 8)
653 {
654 if ((redMask > greenMask) && (greenMask > blueMask)) b_o = RGB;
655 else if ((redMask > blueMask) && (blueMask > greenMask)) b_o = RBG;
656 else if ((blueMask > redMask) && (redMask > greenMask)) b_o = BRG;
657 else if ((blueMask > greenMask) && (greenMask > redMask))b_o = BGR;
658 else if ((greenMask > redMask) && (redMask > blueMask)) b_o = GRB;
659 else if ((greenMask > blueMask) && (blueMask > redMask)) b_o = GBR;
660 }
661
662 int r_mask = image.GetMaskRed();
663 int g_mask = image.GetMaskGreen();
664 int b_mask = image.GetMaskBlue();
665
666 unsigned char* data = image.GetData();
667 wxASSERT_MSG( data, wxT("No image data") );
668
669 unsigned char *colorCube =
670 wxTheApp->GetVisualInfo(M_BMPDATA->m_display)->m_colorCube;
671
672 int index = 0;
673 for (int y = 0; y < height; y++)
674 {
675 for (int x = 0; x < width; x++)
676 {
677 int r = data[index];
678 index++;
679 int g = data[index];
680 index++;
681 int b = data[index];
682 index++;
683
684 if (hasMask)
685 {
686 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
687 XPutPixel( mask_image, x, y, 0 );
688 else
689 XPutPixel( mask_image, x, y, 1 );
690 }
691
692 switch (bpp)
693 {
694 case 8:
695 {
696 int pixel = 0;
697 pixel = colorCube[ ((r & 0xf8) << 7) + ((g & 0xf8) << 2) + ((b & 0xf8) >> 3) ];
698 XPutPixel( data_image, x, y, pixel );
699 break;
700 }
701 case 12: // SGI only
702 {
703 int pixel = 0;
704 switch (b_o)
705 {
706 case RGB: pixel = ((r & 0xf0) << 4) | (g & 0xf0) | ((b & 0xf0) >> 4); break;
707 case RBG: pixel = ((r & 0xf0) << 4) | (b & 0xf0) | ((g & 0xf0) >> 4); break;
708 case GRB: pixel = ((g & 0xf0) << 4) | (r & 0xf0) | ((b & 0xf0) >> 4); break;
709 case GBR: pixel = ((g & 0xf0) << 4) | (b & 0xf0) | ((r & 0xf0) >> 4); break;
710 case BRG: pixel = ((b & 0xf0) << 4) | (r & 0xf0) | ((g & 0xf0) >> 4); break;
711 case BGR: pixel = ((b & 0xf0) << 4) | (g & 0xf0) | ((r & 0xf0) >> 4); break;
712 }
713 XPutPixel( data_image, x, y, pixel );
714 break;
715 }
716 case 15:
717 {
718 int pixel = 0;
719 switch (b_o)
720 {
721 case RGB: pixel = ((r & 0xf8) << 7) | ((g & 0xf8) << 2) | ((b & 0xf8) >> 3); break;
722 case RBG: pixel = ((r & 0xf8) << 7) | ((b & 0xf8) << 2) | ((g & 0xf8) >> 3); break;
723 case GRB: pixel = ((g & 0xf8) << 7) | ((r & 0xf8) << 2) | ((b & 0xf8) >> 3); break;
724 case GBR: pixel = ((g & 0xf8) << 7) | ((b & 0xf8) << 2) | ((r & 0xf8) >> 3); break;
725 case BRG: pixel = ((b & 0xf8) << 7) | ((r & 0xf8) << 2) | ((g & 0xf8) >> 3); break;
726 case BGR: pixel = ((b & 0xf8) << 7) | ((g & 0xf8) << 2) | ((r & 0xf8) >> 3); break;
727 }
728 XPutPixel( data_image, x, y, pixel );
729 break;
730 }
731 case 16:
732 {
733 // I actually don't know if for 16-bit displays, it is alway the green
734 // component or the second component which has 6 bits.
735 int pixel = 0;
736 switch (b_o)
737 {
738 case RGB: pixel = ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | ((b & 0xf8) >> 3); break;
739 case RBG: pixel = ((r & 0xf8) << 8) | ((b & 0xfc) << 3) | ((g & 0xf8) >> 3); break;
740 case GRB: pixel = ((g & 0xf8) << 8) | ((r & 0xfc) << 3) | ((b & 0xf8) >> 3); break;
741 case GBR: pixel = ((g & 0xf8) << 8) | ((b & 0xfc) << 3) | ((r & 0xf8) >> 3); break;
742 case BRG: pixel = ((b & 0xf8) << 8) | ((r & 0xfc) << 3) | ((g & 0xf8) >> 3); break;
743 case BGR: pixel = ((b & 0xf8) << 8) | ((g & 0xfc) << 3) | ((r & 0xf8) >> 3); break;
744 }
745 XPutPixel( data_image, x, y, pixel );
746 break;
747 }
748 case 32:
749 case 24:
750 {
751 int pixel = 0;
752 switch (b_o)
753 {
754 case RGB: pixel = (r << 16) | (g << 8) | b; break;
755 case RBG: pixel = (r << 16) | (b << 8) | g; break;
756 case BRG: pixel = (b << 16) | (r << 8) | g; break;
757 case BGR: pixel = (b << 16) | (g << 8) | r; break;
758 case GRB: pixel = (g << 16) | (r << 8) | b; break;
759 case GBR: pixel = (g << 16) | (b << 8) | r; break;
760 }
761 XPutPixel( data_image, x, y, pixel );
762 }
763 default: break;
764 }
765 } // for
766 } // for
767
768 // Blit picture
769
770 GC gc = XCreateGC( xdisplay, (Pixmap) M_BMPDATA->m_pixmap, 0, NULL );
771 XPutImage( xdisplay, (Pixmap) M_BMPDATA->m_pixmap, gc, data_image, 0, 0, 0, 0, width, height );
772 XDestroyImage( data_image );
773 XFreeGC( xdisplay, gc );
774
775 // Blit mask
776
777 if (image.HasMask())
778 {
779 GC gc = XCreateGC( xdisplay, (Pixmap) GetMask()->GetBitmap(), 0, NULL );
780 XPutImage( xdisplay, (Pixmap) GetMask()->GetBitmap(), gc, mask_image, 0, 0, 0, 0, width, height );
781
782 XDestroyImage( mask_image );
783 XFreeGC( xdisplay, gc );
784 }
785 }
786
787 return true;
788 #endif
789 // wxUSE_NANOX
790 }
791
792 wxImage wxBitmap::ConvertToImage() const
793 {
794 wxImage image;
795
796 wxCHECK_MSG( IsOk(), wxNullImage, wxT("invalid bitmap") );
797
798 Display *xdisplay = (Display*) M_BMPDATA->m_display;
799 wxASSERT_MSG( xdisplay, wxT("No display") );
800
801 #if wxUSE_NANOX
802 //int bpp = DefaultDepth(xdisplay, xscreen);
803 wxGetImageFromDrawable((Pixmap) GetPixmap(), 0, 0, GetWidth(), GetHeight(), image);
804 return image;
805 #else
806 // !wxUSE_NANOX
807 int bpp = wxTheApp->GetVisualInfo(M_BMPDATA->m_display)->m_visualDepth;
808 XImage *x_image = NULL;
809 if (GetPixmap())
810 {
811 x_image = XGetImage( xdisplay, (Pixmap) GetPixmap(),
812 0, 0,
813 GetWidth(), GetHeight(),
814 AllPlanes, ZPixmap );
815 } else
816 if (GetBitmap())
817 {
818 x_image = XGetImage( xdisplay, (Pixmap) GetBitmap(),
819 0, 0,
820 GetWidth(), GetHeight(),
821 AllPlanes, ZPixmap );
822 } else
823 {
824 wxFAIL_MSG( wxT("Ill-formed bitmap") );
825 }
826
827 wxCHECK_MSG( x_image, wxNullImage, wxT("couldn't create image") );
828
829 image.Create( GetWidth(), GetHeight() );
830 char unsigned *data = image.GetData();
831
832 if (!data)
833 {
834 XDestroyImage( x_image );
835 wxFAIL_MSG( wxT("couldn't create image") );
836 return wxNullImage;
837 }
838
839 XImage *x_image_mask = NULL;
840 if (GetMask())
841 {
842 x_image_mask = XGetImage( xdisplay, (Pixmap) GetMask()->GetBitmap(),
843 0, 0,
844 GetWidth(), GetHeight(),
845 AllPlanes, ZPixmap );
846
847 image.SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
848 }
849
850 int red_shift_right = 0;
851 int green_shift_right = 0;
852 int blue_shift_right = 0;
853 int red_shift_left = 0;
854 int green_shift_left = 0;
855 int blue_shift_left = 0;
856 bool use_shift = false;
857
858 if (GetPixmap())
859 {
860 wxXVisualInfo* vi = wxTheApp->GetVisualInfo(M_BMPDATA->m_display);
861
862 red_shift_right = vi->m_visualRedShift;
863 red_shift_left = 8 - vi->m_visualRedPrec;
864 green_shift_right = vi->m_visualGreenShift;
865 green_shift_left = 8 - vi->m_visualGreenPrec;
866 blue_shift_right = vi->m_visualBlueShift;
867 blue_shift_left = 8 - vi->m_visualBluePrec;
868
869 use_shift = (vi->m_visualType == GrayScale) ||
870 (vi->m_visualType != PseudoColor);
871 }
872
873 if (GetBitmap())
874 {
875 bpp = 1;
876 }
877
878 XColor *colors = (XColor*)wxTheApp->
879 GetVisualInfo(M_BMPDATA->m_display)->m_visualColormap;
880
881 int width = GetWidth();
882 int height = GetHeight();
883 long pos = 0;
884 for (int j = 0; j < height; j++)
885 {
886 for (int i = 0; i < width; i++)
887 {
888 unsigned long pixel = XGetPixel( x_image, i, j );
889 if (bpp == 1)
890 {
891 if (pixel == 0)
892 {
893 data[pos] = 0;
894 data[pos+1] = 0;
895 data[pos+2] = 0;
896 }
897 else
898 {
899 data[pos] = 255;
900 data[pos+1] = 255;
901 data[pos+2] = 255;
902 }
903 }
904 else if (use_shift)
905 {
906 data[pos] = (unsigned char)((pixel >> red_shift_right) << red_shift_left);
907 data[pos+1] = (unsigned char)((pixel >> green_shift_right) << green_shift_left);
908 data[pos+2] = (unsigned char)((pixel >> blue_shift_right) << blue_shift_left);
909 }
910 else if (colors)
911 {
912 data[pos] = (unsigned char)(colors[pixel].red >> 8);
913 data[pos+1] = (unsigned char)(colors[pixel].green >> 8);
914 data[pos+2] = (unsigned char)(colors[pixel].blue >> 8);
915 }
916 else
917 {
918 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
919 }
920
921 if (x_image_mask)
922 {
923 int mask_pixel = XGetPixel( x_image_mask, i, j );
924 if (mask_pixel == 0)
925 {
926 data[pos] = 16;
927 data[pos+1] = 16;
928 data[pos+2] = 16;
929 }
930 }
931
932 pos += 3;
933 }
934 }
935
936 XDestroyImage( x_image );
937 if (x_image_mask) XDestroyImage( x_image_mask );
938 return image;
939 #endif
940 // wxUSE_NANOX
941 }
942
943 wxBitmap::wxBitmap( const wxString &filename, wxBitmapType type )
944 {
945 LoadFile( filename, type );
946 }
947
948 wxBitmap::wxBitmap( const char bits[], int width, int height, int depth )
949 {
950 m_refData = new wxBitmapRefData;
951
952 (void) Create(bits, wxBITMAP_TYPE_XBM_DATA, width, height, depth);
953 }
954
955 wxBitmap::~wxBitmap()
956 {
957 }
958
959 int wxBitmap::GetHeight() const
960 {
961 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
962
963 return M_BMPDATA->m_height;
964 }
965
966 int wxBitmap::GetWidth() const
967 {
968 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
969
970 return M_BMPDATA->m_width;
971 }
972
973 int wxBitmap::GetDepth() const
974 {
975 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
976
977 return M_BMPDATA->m_bpp;
978 }
979
980 wxMask *wxBitmap::GetMask() const
981 {
982 wxCHECK_MSG( IsOk(), NULL, wxT("invalid bitmap") );
983
984 return M_BMPDATA->m_mask;
985 }
986
987 void wxBitmap::SetMask( wxMask *mask )
988 {
989 wxCHECK_RET( IsOk(), wxT("invalid bitmap") );
990
991 AllocExclusive();
992 if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask;
993
994 M_BMPDATA->m_mask = mask;
995 }
996
997 bool wxBitmap::CopyFromIcon(const wxIcon& icon)
998 {
999 *this = icon;
1000 return true;
1001 }
1002
1003 wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const
1004 {
1005 wxCHECK_MSG( IsOk() &&
1006 (rect.x >= 0) && (rect.y >= 0) &&
1007 (rect.x+rect.width <= M_BMPDATA->m_width ) &&
1008 (rect.y+rect.height <= M_BMPDATA->m_height),
1009 wxNullBitmap, wxT("invalid bitmap or bitmap region") );
1010
1011 wxBitmap ret( rect.width, rect.height, M_BMPDATA->m_bpp );
1012 wxASSERT_MSG( ret.IsOk(), wxT("GetSubBitmap error") );
1013
1014 if( GetMask() )
1015 {
1016 wxMask* mask = new wxMask();
1017 mask->SetDisplay( GetMask()->GetDisplay() );
1018 mask->SetBitmap( wxGetSubPixmap( GetMask()->GetDisplay(),
1019 GetMask()->GetBitmap(),
1020 rect.x, rect.y,
1021 rect.width, rect.height,
1022 1 ) );
1023
1024 ret.SetMask( mask );
1025 }
1026
1027 if( GetPixmap() )
1028 {
1029 ret.SetPixmap( wxGetSubPixmap( GetDisplay(),
1030 GetPixmap(),
1031 rect.x, rect.y,
1032 rect.width, rect.height,
1033 M_BMPDATA->m_bpp ) );
1034 }
1035
1036 if( GetBitmap() )
1037 {
1038 ret.SetBitmap( wxGetSubPixmap( GetDisplay(),
1039 GetBitmap(),
1040 rect.x, rect.y,
1041 rect.width, rect.height,
1042 1 ) );
1043 }
1044
1045 return ret;
1046 }
1047
1048 bool wxBitmap::SaveFile( const wxString &name, wxBitmapType type,
1049 const wxPalette *palette ) const
1050 {
1051 wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") );
1052
1053 wxBitmapHandler *handler = FindHandler(type);
1054
1055 // Try to save the bitmap via wxImage handlers:
1056 if (handler == NULL)
1057 {
1058 wxImage image(this->ConvertToImage());
1059 if (image.IsOk()) return image.SaveFile( name, type );
1060
1061 return false;
1062 }
1063
1064 return handler->SaveFile(this, name, type, palette);
1065 }
1066
1067 bool wxBitmap::LoadFile( const wxString &name, wxBitmapType type )
1068 {
1069 UnRef();
1070
1071 if (!wxFileExists(name)) return false;
1072
1073 wxBitmapHandler *handler = FindHandler(type);
1074
1075 if (handler == NULL)
1076 {
1077 wxImage image;
1078 if (!image.LoadFile( name, type ))
1079 return false;
1080
1081 if (image.IsOk())
1082 {
1083 *this = wxBitmap(image);
1084 return true;
1085 }
1086 else return false;
1087 }
1088
1089 return handler->LoadFile(this, name, type, -1, -1);
1090 }
1091
1092 void wxBitmap::SetPalette(const wxPalette& palette)
1093 {
1094 wxCHECK_RET(IsOk(), wxT("invalid bitmap"));
1095 wxCHECK_RET(GetDepth() > 1 && GetDepth() <= 8,
1096 wxT("cannot set palette for bitmap of this depth"));
1097
1098 AllocExclusive();
1099 wxDELETE(M_BMPDATA->m_palette);
1100
1101 if (!palette.IsOk()) return;
1102
1103 M_BMPDATA->m_palette = new wxPalette(palette);
1104 }
1105
1106 wxPalette *wxBitmap::GetPalette() const
1107 {
1108 if (!IsOk()) return NULL;
1109
1110 return M_BMPDATA->m_palette;
1111 }
1112
1113 void wxBitmap::SetHeight( int height )
1114 {
1115 AllocExclusive();
1116
1117 M_BMPDATA->m_height = height;
1118 }
1119
1120 void wxBitmap::SetWidth( int width )
1121 {
1122 AllocExclusive();
1123
1124 M_BMPDATA->m_width = width;
1125 }
1126
1127 void wxBitmap::SetDepth( int depth )
1128 {
1129 AllocExclusive();
1130
1131 M_BMPDATA->m_bpp = depth;
1132 }
1133
1134 void wxBitmap::SetPixmap( WXPixmap pixmap )
1135 {
1136 if (!m_refData) m_refData = new wxBitmapRefData();
1137
1138 M_BMPDATA->m_pixmap = (Pixmap)pixmap;
1139 }
1140
1141 void wxBitmap::SetBitmap( WXPixmap bitmap )
1142 {
1143 if (!m_refData) m_refData = new wxBitmapRefData();
1144
1145 M_BMPDATA->m_bitmap = (Pixmap)bitmap;
1146 }
1147
1148 WXPixmap wxBitmap::GetPixmap() const
1149 {
1150 wxCHECK_MSG( IsOk(), (WXPixmap) NULL, wxT("invalid bitmap") );
1151
1152 return (WXPixmap)M_BMPDATA->m_pixmap;
1153 }
1154
1155 WXPixmap wxBitmap::GetBitmap() const
1156 {
1157 wxCHECK_MSG( IsOk(), (WXPixmap) NULL, wxT("invalid bitmap") );
1158
1159 return (WXPixmap)M_BMPDATA->m_bitmap;
1160 }
1161
1162 WXPixmap wxBitmap::GetDrawable() const
1163 {
1164 wxCHECK_MSG( IsOk(), (WXPixmap) NULL, wxT("invalid bitmap") );
1165
1166 return (WXPixmap)(M_BMPDATA->m_bpp == 1 ? M_BMPDATA->m_bitmap
1167 : M_BMPDATA->m_pixmap);
1168 }
1169
1170 WXDisplay *wxBitmap::GetDisplay() const
1171 {
1172 wxCHECK_MSG( IsOk(), NULL, wxT("invalid bitmap") );
1173
1174 return M_BMPDATA->m_display;
1175 }
1176
1177 #if wxUSE_NANOX
1178 // Copy from the drawable to the wxImage
1179 bool wxGetImageFromDrawable(GR_DRAW_ID drawable, int srcX, int srcY, int width, int height, wxImage& image)
1180 {
1181 GR_SCREEN_INFO sinfo;
1182 int x, y;
1183 GR_PIXELVAL *pixels;
1184 GR_PALETTE* palette = NULL;
1185 unsigned char rgb[3], *pp;
1186
1187 GrGetScreenInfo(&sinfo);
1188
1189 if (sinfo.pixtype == MWPF_PALETTE) {
1190 if(!(palette = (GR_PALETTE*) malloc(sizeof(GR_PALETTE)))) {
1191 return false;
1192 }
1193 GrGetSystemPalette(palette);
1194 }
1195
1196 if(!(pixels = (GR_PIXELVAL*) malloc(sizeof(GR_PIXELVAL) * width * height)))
1197 {
1198 return false;
1199 }
1200
1201 image.Create(width, height);
1202
1203 GrReadArea(drawable, srcX, srcY, width, height,
1204 pixels);
1205
1206
1207 for(x = 0; x < sinfo.cols; x++) {
1208
1209 pp = (unsigned char *)pixels +
1210 ((x + (y * sinfo.cols)) *
1211 sizeof(GR_PIXELVAL));
1212
1213 switch(sinfo.pixtype) {
1214 /* FIXME: These may need modifying on big endian. */
1215 case MWPF_TRUECOLOR0888:
1216 case MWPF_TRUECOLOR888:
1217 rgb[0] = pp[2];
1218 rgb[1] = pp[1];
1219 rgb[2] = pp[0];
1220 break;
1221 case MWPF_PALETTE:
1222 rgb[0] = palette->palette[pp[0]].r;
1223 rgb[1] = palette->palette[pp[0]].g;
1224 rgb[2] = palette->palette[pp[0]].b;
1225 break;
1226 case MWPF_TRUECOLOR565:
1227 rgb[0] = pp[1] & 0xf8;
1228 rgb[1] = ((pp[1] & 0x07) << 5) |
1229 ((pp[0] & 0xe0) >> 3);
1230 rgb[2] = (pp[0] & 0x1f) << 3;
1231 break;
1232 case MWPF_TRUECOLOR555:
1233 rgb[0] = (pp[1] & 0x7c) << 1;
1234 rgb[1] = ((pp[1] & 0x03) << 6) |
1235 ((pp[0] & 0xe0) >> 2);
1236 rgb[2] = (pp[0] & 0x1f) << 3;
1237 break;
1238 case MWPF_TRUECOLOR332:
1239 rgb[0] = pp[0] & 0xe0;
1240 rgb[1] = (pp[0] & 0x1c) << 3;
1241 rgb[2] = (pp[0] & 0x03) << 6;
1242 break;
1243 default:
1244 fprintf(stderr, "Unsupported pixel "
1245 "format\n");
1246 return 1;
1247 }
1248
1249 image.SetRGB(x, y, rgb[0], rgb[1], rgb[2]);
1250
1251 }
1252
1253 free(pixels);
1254 if(palette) free(palette);
1255
1256 return true;
1257 }
1258
1259 #if 0
1260 int GrGetPixelColor(GR_SCREEN_INFO* sinfo, GR_PALETTE* palette, GR_PIXELVAL pixel,
1261 unsigned char* red, unsigned char* green, unsigned char* blue)
1262 {
1263 unsigned char rgb[3], *pp;
1264
1265 pp = (unsigned char*) & pixel ;
1266
1267 switch (sinfo.pixtype)
1268 {
1269 /* FIXME: These may need modifying on big endian. */
1270 case MWPF_TRUECOLOR0888:
1271 case MWPF_TRUECOLOR888:
1272 rgb[0] = pp[2];
1273 rgb[1] = pp[1];
1274 rgb[2] = pp[0];
1275 break;
1276 case MWPF_PALETTE:
1277 rgb[0] = palette->palette[pp[0]].r;
1278 rgb[1] = palette->palette[pp[0]].g;
1279 rgb[2] = palette->palette[pp[0]].b;
1280 break;
1281 case MWPF_TRUECOLOR565:
1282 rgb[0] = pp[1] & 0xf8;
1283 rgb[1] = ((pp[1] & 0x07) << 5) |
1284 ((pp[0] & 0xe0) >> 3);
1285 rgb[2] = (pp[0] & 0x1f) << 3;
1286 break;
1287 case MWPF_TRUECOLOR555:
1288 rgb[0] = (pp[1] & 0x7c) << 1;
1289 rgb[1] = ((pp[1] & 0x03) << 6) |
1290 ((pp[0] & 0xe0) >> 2);
1291 rgb[2] = (pp[0] & 0x1f) << 3;
1292 break;
1293 case MWPF_TRUECOLOR332:
1294 rgb[0] = pp[0] & 0xe0;
1295 rgb[1] = (pp[0] & 0x1c) << 3;
1296 rgb[2] = (pp[0] & 0x03) << 6;
1297 break;
1298 default:
1299 fprintf(stderr, "Unsupported pixel format\n");
1300 return 0;
1301 }
1302
1303
1304 *(red) = rgb[0];
1305 *(green) = rgb[1];
1306 *(blue) = rgb[2];
1307 return 1;
1308 }
1309 #endif
1310
1311 #endif
1312 // wxUSE_NANOX
1313
1314 // ============================================================================
1315 // Bitmap handlers
1316 // ============================================================================
1317
1318 #define M_BMPHANDLERDATA ((wxBitmapRefData *)bitmap->GetRefData())
1319
1320 #if wxUSE_XPM
1321
1322 #if wxHAVE_LIB_XPM || wxUSE_STREAMS
1323
1324 // ----------------------------------------------------------------------------
1325 // wxXPMFileHandler
1326 // ----------------------------------------------------------------------------
1327
1328 class wxXPMFileHandler : public wxBitmapHandler
1329 {
1330 public:
1331 wxXPMFileHandler()
1332 {
1333 SetName( wxT("XPM file") );
1334 SetExtension( wxT("xpm") );
1335 SetType( wxBITMAP_TYPE_XPM );
1336 };
1337
1338 virtual bool LoadFile(wxBitmap *bitmap, const wxString& name,
1339 wxBitmapType flags,
1340 int desiredWidth, int desiredHeight);
1341
1342 virtual bool SaveFile(const wxBitmap *bitmap, const wxString& name,
1343 wxBitmapType type,
1344 const wxPalette *palette = NULL) const;
1345
1346 virtual bool Create(wxBitmap *WXUNUSED(bitmap),
1347 const void* WXUNUSED(data),
1348 wxBitmapType WXUNUSED(flags),
1349 int WXUNUSED(width),
1350 int WXUNUSED(height),
1351 int WXUNUSED(depth) = 1)
1352 { return false; }
1353
1354 DECLARE_DYNAMIC_CLASS(wxXPMFileHandler)
1355 };
1356
1357 IMPLEMENT_DYNAMIC_CLASS(wxXPMFileHandler, wxBitmapHandler)
1358
1359 bool wxXPMFileHandler::LoadFile(wxBitmap *bitmap,
1360 const wxString& name,
1361 wxBitmapType WXUNUSED(flags),
1362 int WXUNUSED(desiredWidth),
1363 int WXUNUSED(desiredHeight))
1364 {
1365 #if wxHAVE_LIB_XPM
1366 if (!bitmap->GetRefData())
1367 bitmap->SetRefData( new wxBitmapRefData() );
1368
1369 M_BMPHANDLERDATA->m_display = wxGlobalDisplay();
1370
1371 Display *xdisplay = (Display*) M_BMPHANDLERDATA->m_display;
1372
1373 int xscreen = DefaultScreen( xdisplay );
1374 Window xroot = RootWindow( xdisplay, xscreen );
1375
1376 int bpp = DefaultDepth( xdisplay, xscreen );
1377
1378 XpmAttributes xpmAttr;
1379 xpmAttr.valuemask = XpmReturnInfos; // nothing yet, but get infos back
1380
1381 Pixmap pixmap;
1382 Pixmap mask = 0;
1383
1384 int ErrorStatus = XpmReadFileToPixmap( xdisplay, xroot,
1385 (char*) ((const char*) name.c_str()),
1386 &pixmap, &mask, &xpmAttr);
1387
1388 if (ErrorStatus == XpmSuccess)
1389 {
1390 M_BMPHANDLERDATA->m_width = xpmAttr.width;
1391 M_BMPHANDLERDATA->m_height = xpmAttr.height;
1392
1393 M_BMPHANDLERDATA->m_bpp = bpp; // mono as well?
1394
1395 XpmFreeAttributes(&xpmAttr);
1396
1397 M_BMPHANDLERDATA->m_bitmap = (Pixmap) pixmap;
1398
1399 if (mask)
1400 {
1401 M_BMPHANDLERDATA->m_mask = new wxMask;
1402 M_BMPHANDLERDATA->m_mask->SetBitmap( (WXPixmap) mask );
1403 M_BMPHANDLERDATA->m_mask->SetDisplay( xdisplay );
1404 }
1405 }
1406 else
1407 {
1408 UnRef();
1409
1410 return false;
1411 }
1412
1413 return true;
1414 #elif wxUSE_STREAMS
1415 wxXPMDecoder decoder;
1416 wxFileInputStream stream(name);
1417 if (stream.IsOk())
1418 {
1419 wxImage image(decoder.ReadFile(stream));
1420 return image.IsOk() && bitmap->CreateFromImage(image);
1421 }
1422
1423 return false;
1424 #else // !wxHAVE_LIB_XPM && !wxUSE_STREAMS
1425 return false;
1426 #endif // wxHAVE_LIB_XPM / wxUSE_STREAMS
1427 }
1428
1429 bool wxXPMFileHandler::SaveFile(const wxBitmap *bitmap,
1430 const wxString& name,
1431 wxBitmapType type,
1432 const wxPalette *WXUNUSED(palette)) const
1433 {
1434 wxImage image(bitmap->ConvertToImage());
1435 if (image.IsOk())
1436 return image.SaveFile( name, type );
1437
1438 return false;
1439 }
1440
1441 #endif // wxHAVE_LIB_XPM || wxUSE_STREAMS
1442
1443 // ----------------------------------------------------------------------------
1444 // wxXPMDataHandler
1445 // ----------------------------------------------------------------------------
1446
1447 class wxXPMDataHandler : public wxBitmapHandler
1448 {
1449 DECLARE_DYNAMIC_CLASS(wxXPMDataHandler)
1450 public:
1451 wxXPMDataHandler()
1452 {
1453 SetName( wxT("XPM data") );
1454 SetExtension( wxT("xpm") );
1455 SetType( wxBITMAP_TYPE_XPM_DATA );
1456 };
1457
1458 virtual bool LoadFile(wxBitmap *WXUNUSED(bitmap),
1459 const wxString& WXUNUSED(name),
1460 wxBitmapType WXUNUSED(flags),
1461 int WXUNUSED(desiredWidth),
1462 int WXUNUSED(desiredHeight))
1463 { return false; }
1464
1465 virtual bool SaveFile(const wxBitmap *WXUNUSED(bitmap),
1466 const wxString& WXUNUSED(name),
1467 wxBitmapType WXUNUSED(type),
1468 const wxPalette *WXUNUSED(palette) = NULL) const
1469 { return false; }
1470
1471 virtual bool Create(wxBitmap *bitmap, const void* data, wxBitmapType flags,
1472 int width, int height, int depth = 1);
1473 };
1474
1475 IMPLEMENT_DYNAMIC_CLASS(wxXPMDataHandler, wxBitmapHandler)
1476
1477 bool wxXPMDataHandler::Create(wxBitmap *bitmap, const void* bits,
1478 wxBitmapType WXUNUSED(flags),
1479 int WXUNUSED(width), int WXUNUSED(height), int WXUNUSED(depth))
1480 {
1481 #if wxHAVE_LIB_XPM
1482 wxCHECK_MSG( bits != NULL, false, wxT("invalid bitmap data") );
1483
1484 if (!bitmap->GetRefData())
1485 bitmap->SetRefData( new wxBitmapRefData() );
1486
1487 M_BMPHANDLERDATA->m_display = wxGlobalDisplay();
1488
1489 Display *xdisplay = (Display*) M_BMPHANDLERDATA->m_display;
1490
1491 int xscreen = DefaultScreen( xdisplay );
1492 Window xroot = RootWindow( xdisplay, xscreen );
1493
1494 int bpp = DefaultDepth( xdisplay, xscreen );
1495
1496 XpmAttributes xpmAttr;
1497 xpmAttr.valuemask = XpmReturnInfos; // nothing yet, but get infos back
1498
1499 Pixmap pixmap = 0;
1500 Pixmap mask = 0;
1501
1502 int ErrorStatus = XpmCreatePixmapFromData( xdisplay, xroot, (char**) bits,
1503 &pixmap, &mask, &xpmAttr );
1504
1505 if (ErrorStatus == XpmSuccess)
1506 {
1507 M_BMPHANDLERDATA->m_width = xpmAttr.width;
1508 M_BMPHANDLERDATA->m_height = xpmAttr.height;
1509
1510 M_BMPHANDLERDATA->m_bpp = bpp; // mono as well?
1511
1512 #if wxDEBUG_LEVEL
1513 unsigned int depthRet;
1514 int xRet, yRet;
1515 unsigned int widthRet, heightRet, borderWidthRet;
1516 XGetGeometry( xdisplay, pixmap, &xroot, &xRet, &yRet,
1517 &widthRet, &heightRet, &borderWidthRet, &depthRet);
1518
1519 wxASSERT_MSG( bpp == (int)depthRet, wxT("colour depth mismatch") );
1520 #endif // wxDEBUG_LEVEL
1521
1522 XpmFreeAttributes(&xpmAttr);
1523
1524 M_BMPHANDLERDATA->m_pixmap = (Pixmap) pixmap;
1525
1526 if (mask)
1527 {
1528 M_BMPHANDLERDATA->m_mask = new wxMask;
1529 M_BMPHANDLERDATA->m_mask->SetBitmap( (WXPixmap) mask );
1530 M_BMPHANDLERDATA->m_mask->SetDisplay( xdisplay );
1531 }
1532 return true;
1533 }
1534 else
1535 {
1536 bitmap->UnRef();
1537
1538 return false;
1539 }
1540 #else // !wxHAVE_LIB_XPM
1541 wxXPMDecoder decoder;
1542 wxImage image(decoder.ReadData((const char **)bits));
1543 return image.IsOk() && bitmap->CreateFromImage(image);
1544 #endif // wxHAVE_LIB_XPM/!wxHAVE_LIB_XPM
1545 }
1546
1547 #endif // wxUSE_XPM
1548
1549 // ----------------------------------------------------------------------------
1550 // wxXBMDataHandler
1551 // ----------------------------------------------------------------------------
1552
1553 class WXDLLEXPORT wxXBMDataHandler: public wxBitmapHandler
1554 {
1555 DECLARE_DYNAMIC_CLASS(wxXBMDataHandler)
1556 public:
1557 inline wxXBMDataHandler()
1558 {
1559 SetName( wxT("XBM data") );
1560 SetExtension( wxT("xbm") );
1561 SetType( wxBITMAP_TYPE_XBM_DATA );
1562 };
1563
1564 virtual bool LoadFile(wxBitmap *WXUNUSED(bitmap),
1565 const wxString& WXUNUSED(name),
1566 wxBitmapType WXUNUSED(flags),
1567 int WXUNUSED(desiredWidth),
1568 int WXUNUSED(desiredHeight))
1569 { return false; }
1570
1571 virtual bool SaveFile(const wxBitmap *WXUNUSED(bitmap),
1572 const wxString& WXUNUSED(name),
1573 wxBitmapType WXUNUSED(type),
1574 const wxPalette *WXUNUSED(palette) = NULL) const
1575 { return false; }
1576
1577 virtual bool Create(wxBitmap *bitmap, const void* data, wxBitmapType type,
1578 int width, int height, int depth = 1);
1579 };
1580
1581 IMPLEMENT_DYNAMIC_CLASS(wxXBMDataHandler, wxBitmapHandler)
1582
1583 bool wxXBMDataHandler::Create( wxBitmap *bitmap, const void* bits,
1584 wxBitmapType WXUNUSED(type),
1585 int width, int height, int WXUNUSED(depth))
1586 {
1587 #if !wxUSE_NANOX
1588 if (!bitmap->GetRefData())
1589 bitmap->SetRefData( new wxBitmapRefData() );
1590
1591 M_BMPHANDLERDATA->m_display = wxGlobalDisplay();
1592
1593 Display *xdisplay = (Display*) M_BMPHANDLERDATA->m_display;
1594
1595 int xscreen = DefaultScreen( xdisplay );
1596 Window xroot = RootWindow( xdisplay, xscreen );
1597
1598 M_BMPHANDLERDATA->m_mask = NULL;
1599 M_BMPHANDLERDATA->m_bitmap =
1600 XCreateBitmapFromData(xdisplay, xroot,
1601 (char *) bits, width, height );
1602 M_BMPHANDLERDATA->m_width = width;
1603 M_BMPHANDLERDATA->m_height = height;
1604 M_BMPHANDLERDATA->m_bpp = 1;
1605
1606 return true;
1607 #else
1608 wxCHECK_MSG( M_BMPHANDLERDATA->m_bitmap, false,
1609 wxT("couldn't create bitmap") );
1610 #endif
1611 }
1612
1613 void wxBitmap::InitStandardHandlers()
1614 {
1615 AddHandler(new wxXBMDataHandler);
1616 #if wxUSE_XPM
1617 #if wxHAVE_LIB_XPM || wxUSE_STREAMS
1618 AddHandler(new wxXPMFileHandler);
1619 #endif
1620 AddHandler(new wxXPMDataHandler);
1621 #endif
1622 }