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