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