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