Derive wxBitmap from wxBitmapBase.
[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->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->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 if (bpp > 8)
567 {
568 if ((wxTheApp->m_visualRedMask > wxTheApp->m_visualGreenMask) && (wxTheApp->m_visualGreenMask > wxTheApp->m_visualBlueMask)) b_o = RGB;
569 else if ((wxTheApp->m_visualRedMask > wxTheApp->m_visualBlueMask) && (wxTheApp->m_visualBlueMask > wxTheApp->m_visualGreenMask)) b_o = RBG;
570 else if ((wxTheApp->m_visualBlueMask > wxTheApp->m_visualRedMask) && (wxTheApp->m_visualRedMask > wxTheApp->m_visualGreenMask)) b_o = BRG;
571 else if ((wxTheApp->m_visualBlueMask > wxTheApp->m_visualGreenMask) && (wxTheApp->m_visualGreenMask > wxTheApp->m_visualRedMask)) b_o = BGR;
572 else if ((wxTheApp->m_visualGreenMask > wxTheApp->m_visualRedMask) && (wxTheApp->m_visualRedMask > wxTheApp->m_visualBlueMask)) b_o = GRB;
573 else if ((wxTheApp->m_visualGreenMask > wxTheApp->m_visualBlueMask) && (wxTheApp->m_visualBlueMask > wxTheApp->m_visualRedMask)) b_o = GBR;
574 }
575
576 int r_mask = image.GetMaskRed();
577 int g_mask = image.GetMaskGreen();
578 int b_mask = image.GetMaskBlue();
579
580 unsigned char* data = image.GetData();
581 wxASSERT_MSG( data, "No image data" );
582
583 unsigned char *colorCube = wxTheApp->m_colorCube;
584
585 bool hasMask = image.HasMask();
586
587 int index = 0;
588 for (int y = 0; y < height; y++)
589 {
590 for (int x = 0; x < width; x++)
591 {
592 int r = data[index];
593 index++;
594 int g = data[index];
595 index++;
596 int b = data[index];
597 index++;
598
599 if (hasMask)
600 {
601 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
602 XPutPixel( mask_image, x, y, 0 );
603 else
604 XPutPixel( mask_image, x, y, 1 );
605 }
606
607 switch (bpp)
608 {
609 case 8:
610 {
611 int pixel = 0;
612 pixel = colorCube[ ((r & 0xf8) << 7) + ((g & 0xf8) << 2) + ((b & 0xf8) >> 3) ];
613 XPutPixel( data_image, x, y, pixel );
614 break;
615 }
616 case 12: // SGI only
617 {
618 int pixel = 0;
619 switch (b_o)
620 {
621 case RGB: pixel = ((r & 0xf0) << 4) | (g & 0xf0) | ((b & 0xf0) >> 4); break;
622 case RBG: pixel = ((r & 0xf0) << 4) | (b & 0xf0) | ((g & 0xf0) >> 4); break;
623 case GRB: pixel = ((g & 0xf0) << 4) | (r & 0xf0) | ((b & 0xf0) >> 4); break;
624 case GBR: pixel = ((g & 0xf0) << 4) | (b & 0xf0) | ((r & 0xf0) >> 4); break;
625 case BRG: pixel = ((b & 0xf0) << 4) | (r & 0xf0) | ((g & 0xf0) >> 4); break;
626 case BGR: pixel = ((b & 0xf0) << 4) | (g & 0xf0) | ((r & 0xf0) >> 4); break;
627 }
628 XPutPixel( data_image, x, y, pixel );
629 break;
630 }
631 case 15:
632 {
633 int pixel = 0;
634 switch (b_o)
635 {
636 case RGB: pixel = ((r & 0xf8) << 7) | ((g & 0xf8) << 2) | ((b & 0xf8) >> 3); break;
637 case RBG: pixel = ((r & 0xf8) << 7) | ((b & 0xf8) << 2) | ((g & 0xf8) >> 3); break;
638 case GRB: pixel = ((g & 0xf8) << 7) | ((r & 0xf8) << 2) | ((b & 0xf8) >> 3); break;
639 case GBR: pixel = ((g & 0xf8) << 7) | ((b & 0xf8) << 2) | ((r & 0xf8) >> 3); break;
640 case BRG: pixel = ((b & 0xf8) << 7) | ((r & 0xf8) << 2) | ((g & 0xf8) >> 3); break;
641 case BGR: pixel = ((b & 0xf8) << 7) | ((g & 0xf8) << 2) | ((r & 0xf8) >> 3); break;
642 }
643 XPutPixel( data_image, x, y, pixel );
644 break;
645 }
646 case 16:
647 {
648 // I actually don't know if for 16-bit displays, it is alway the green
649 // component or the second component which has 6 bits.
650 int pixel = 0;
651 switch (b_o)
652 {
653 case RGB: pixel = ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | ((b & 0xf8) >> 3); break;
654 case RBG: pixel = ((r & 0xf8) << 8) | ((b & 0xfc) << 3) | ((g & 0xf8) >> 3); break;
655 case GRB: pixel = ((g & 0xf8) << 8) | ((r & 0xfc) << 3) | ((b & 0xf8) >> 3); break;
656 case GBR: pixel = ((g & 0xf8) << 8) | ((b & 0xfc) << 3) | ((r & 0xf8) >> 3); break;
657 case BRG: pixel = ((b & 0xf8) << 8) | ((r & 0xfc) << 3) | ((g & 0xf8) >> 3); break;
658 case BGR: pixel = ((b & 0xf8) << 8) | ((g & 0xfc) << 3) | ((r & 0xf8) >> 3); break;
659 }
660 XPutPixel( data_image, x, y, pixel );
661 break;
662 }
663 case 32:
664 case 24:
665 {
666 int pixel = 0;
667 switch (b_o)
668 {
669 case RGB: pixel = (r << 16) | (g << 8) | b; break;
670 case RBG: pixel = (r << 16) | (b << 8) | g; break;
671 case BRG: pixel = (b << 16) | (r << 8) | g; break;
672 case BGR: pixel = (b << 16) | (g << 8) | r; break;
673 case GRB: pixel = (g << 16) | (r << 8) | b; break;
674 case GBR: pixel = (g << 16) | (b << 8) | r; break;
675 }
676 XPutPixel( data_image, x, y, pixel );
677 }
678 default: break;
679 }
680 } // for
681 } // for
682
683 // Blit picture
684
685 GC gc = XCreateGC( xdisplay, (Pixmap) M_BMPDATA->m_pixmap, 0, NULL );
686 XPutImage( xdisplay, (Pixmap) M_BMPDATA->m_pixmap, gc, data_image, 0, 0, 0, 0, width, height );
687 XDestroyImage( data_image );
688 XFreeGC( xdisplay, gc );
689
690 // Blit mask
691
692 if (image.HasMask())
693 {
694 GC gc = XCreateGC( xdisplay, (Pixmap) GetMask()->GetBitmap(), 0, NULL );
695 XPutImage( xdisplay, (Pixmap) GetMask()->GetBitmap(), gc, mask_image, 0, 0, 0, 0, width, height );
696
697 XDestroyImage( mask_image );
698 XFreeGC( xdisplay, gc );
699 }
700 }
701
702 return TRUE;
703 #endif
704 // wxUSE_NANOX
705 }
706
707 wxImage wxBitmap::ConvertToImage() const
708 {
709 wxImage image;
710
711 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
712
713 Display *xdisplay = (Display*) M_BMPDATA->m_display;
714 wxASSERT_MSG( xdisplay, wxT("No display") );
715
716 #if wxUSE_NANOX
717 //int bpp = DefaultDepth(xdisplay, xscreen);
718 wxGetImageFromDrawable((Pixmap) GetPixmap(), 0, 0, GetWidth(), GetHeight(), image);
719 return image;
720 #else
721 // !wxUSE_NANOX
722 int bpp = wxTheApp->m_visualDepth;
723 XImage *x_image = NULL;
724 if (GetPixmap())
725 {
726 x_image = XGetImage( xdisplay, (Pixmap) GetPixmap(),
727 0, 0,
728 GetWidth(), GetHeight(),
729 AllPlanes, ZPixmap );
730 } else
731 if (GetBitmap())
732 {
733 x_image = XGetImage( xdisplay, (Pixmap) GetBitmap(),
734 0, 0,
735 GetWidth(), GetHeight(),
736 AllPlanes, ZPixmap );
737 } else
738 {
739 wxFAIL_MSG( wxT("Ill-formed bitmap") );
740 }
741
742 wxCHECK_MSG( x_image, wxNullImage, wxT("couldn't create image") );
743
744 image.Create( GetWidth(), GetHeight() );
745 char unsigned *data = image.GetData();
746
747 if (!data)
748 {
749 XDestroyImage( x_image );
750 wxFAIL_MSG( wxT("couldn't create image") );
751 return wxNullImage;
752 }
753
754 XImage *x_image_mask = NULL;
755 if (GetMask())
756 {
757 x_image_mask = XGetImage( xdisplay, (Pixmap) GetMask()->GetBitmap(),
758 0, 0,
759 GetWidth(), GetHeight(),
760 AllPlanes, ZPixmap );
761
762 image.SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
763 }
764
765 int red_shift_right = 0;
766 int green_shift_right = 0;
767 int blue_shift_right = 0;
768 int red_shift_left = 0;
769 int green_shift_left = 0;
770 int blue_shift_left = 0;
771 bool use_shift = FALSE;
772
773 if (GetPixmap())
774 {
775 red_shift_right = wxTheApp->m_visualRedShift;
776 red_shift_left = 8-wxTheApp->m_visualRedPrec;
777 green_shift_right = wxTheApp->m_visualGreenShift;
778 green_shift_left = 8-wxTheApp->m_visualGreenPrec;
779 blue_shift_right = wxTheApp->m_visualBlueShift;
780 blue_shift_left = 8-wxTheApp->m_visualBluePrec;
781
782 use_shift = (wxTheApp->m_visualType == GrayScale) || (wxTheApp->m_visualType != PseudoColor);
783 }
784
785 if (GetBitmap())
786 {
787 bpp = 1;
788 }
789
790 XColor *colors = (XColor*) wxTheApp->m_visualColormap;
791
792 int width = GetWidth();
793 int height = GetHeight();
794 long pos = 0;
795 for (int j = 0; j < height; j++)
796 {
797 for (int i = 0; i < width; i++)
798 {
799 unsigned long pixel = XGetPixel( x_image, i, j );
800 if (bpp == 1)
801 {
802 if (pixel == 0)
803 {
804 data[pos] = 0;
805 data[pos+1] = 0;
806 data[pos+2] = 0;
807 }
808 else
809 {
810 data[pos] = 255;
811 data[pos+1] = 255;
812 data[pos+2] = 255;
813 }
814 }
815 else if (use_shift)
816 {
817 data[pos] = (pixel >> red_shift_right) << red_shift_left;
818 data[pos+1] = (pixel >> green_shift_right) << green_shift_left;
819 data[pos+2] = (pixel >> blue_shift_right) << blue_shift_left;
820 }
821 else if (colors)
822 {
823 data[pos] = colors[pixel].red >> 8;
824 data[pos+1] = colors[pixel].green >> 8;
825 data[pos+2] = colors[pixel].blue >> 8;
826 }
827 else
828 {
829 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
830 }
831
832 if (x_image_mask)
833 {
834 int mask_pixel = XGetPixel( x_image_mask, i, j );
835 if (mask_pixel == 0)
836 {
837 data[pos] = 16;
838 data[pos+1] = 16;
839 data[pos+2] = 16;
840 }
841 }
842
843 pos += 3;
844 }
845 }
846
847 XDestroyImage( x_image );
848 if (x_image_mask) XDestroyImage( x_image_mask );
849 return image;
850 #endif
851 // wxUSE_NANOX
852 }
853
854 wxBitmap::wxBitmap( const wxBitmap& bmp )
855 {
856 Ref( bmp );
857 }
858
859 wxBitmap::wxBitmap( const wxString &filename, wxBitmapType type )
860 {
861 LoadFile( filename, type );
862 }
863
864 wxBitmap::wxBitmap( const char bits[], int width, int height, int depth )
865 {
866 m_refData = new wxBitmapRefData;
867
868 (void) Create((void*) bits, wxBITMAP_TYPE_XBM_DATA, width, height, depth);
869 }
870
871 wxBitmap::~wxBitmap()
872 {
873 }
874
875 wxBitmap& wxBitmap::operator = ( const wxBitmap& bmp )
876 {
877 if ( m_refData != bmp.m_refData )
878 Ref( bmp );
879
880 return *this;
881 }
882
883 bool wxBitmap::operator == ( const wxBitmap& bmp ) const
884 {
885 return m_refData == bmp.m_refData;
886 }
887
888 bool wxBitmap::operator != ( const wxBitmap& bmp ) const
889 {
890 return m_refData != bmp.m_refData;
891 }
892
893 bool wxBitmap::Ok() const
894 {
895 return (m_refData != NULL);
896 }
897
898 int wxBitmap::GetHeight() const
899 {
900 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
901
902 return M_BMPDATA->m_height;
903 }
904
905 int wxBitmap::GetWidth() const
906 {
907 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
908
909 return M_BMPDATA->m_width;
910 }
911
912 int wxBitmap::GetDepth() const
913 {
914 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
915
916 return M_BMPDATA->m_bpp;
917 }
918
919 wxMask *wxBitmap::GetMask() const
920 {
921 wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") );
922
923 return M_BMPDATA->m_mask;
924 }
925
926 void wxBitmap::SetMask( wxMask *mask )
927 {
928 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
929
930 if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask;
931
932 M_BMPDATA->m_mask = mask;
933 }
934
935 bool wxBitmap::CopyFromIcon(const wxIcon& icon)
936 {
937 *this = icon;
938 return TRUE;
939 }
940
941 wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const
942 {
943 wxCHECK_MSG( Ok() &&
944 (rect.x >= 0) && (rect.y >= 0) &&
945 (rect.x+rect.width <= M_BMPDATA->m_width ) &&
946 (rect.y+rect.height <= M_BMPDATA->m_height),
947 wxNullBitmap, wxT("invalid bitmap or bitmap region") );
948
949 wxBitmap ret( rect.width, rect.height, M_BMPDATA->m_bpp );
950 wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") );
951
952 if( GetMask() )
953 {
954 wxMask* mask = new wxMask();
955 mask->SetDisplay( GetMask()->GetDisplay() );
956 mask->SetBitmap( wxGetSubPixmap( GetMask()->GetDisplay(),
957 GetMask()->GetBitmap(),
958 rect.x, rect.y,
959 rect.width, rect.height,
960 1 ) );
961
962 ret.SetMask( mask );
963 }
964
965 if( GetPixmap() )
966 {
967 ret.SetPixmap( wxGetSubPixmap( GetDisplay(),
968 GetPixmap(),
969 rect.x, rect.y,
970 rect.width, rect.height,
971 M_BMPDATA->m_bpp ) );
972 }
973
974 if( GetBitmap() )
975 {
976 ret.SetBitmap( wxGetSubPixmap( GetDisplay(),
977 GetBitmap(),
978 rect.x, rect.y,
979 rect.width, rect.height,
980 1 ) );
981 }
982
983 return ret;
984 }
985
986 bool wxBitmap::SaveFile( const wxString &name, wxBitmapType type,
987 const wxPalette *palette ) const
988 {
989 wxCHECK_MSG( Ok(), FALSE, wxT("invalid bitmap") );
990
991 wxBitmapHandler *handler = FindHandler(type);
992
993 // Try to save the bitmap via wxImage handlers:
994 if (handler == NULL)
995 {
996 wxImage image(this->ConvertToImage());
997 if (image.Ok()) return image.SaveFile( name, type );
998
999 return FALSE;
1000 }
1001
1002 return handler->SaveFile(this, name, type, palette);
1003 }
1004
1005 bool wxBitmap::LoadFile( const wxString &name, wxBitmapType type )
1006 {
1007 UnRef();
1008
1009 if (!wxFileExists(name)) return FALSE;
1010
1011 wxBitmapHandler *handler = FindHandler(type);
1012
1013 if (handler == NULL)
1014 {
1015 wxImage image;
1016 if (!image.LoadFile( name, type ))
1017 return FALSE;
1018
1019 if (image.Ok())
1020 {
1021 *this = wxBitmap(image);
1022 return TRUE;
1023 }
1024 else return FALSE;
1025 }
1026
1027 return handler->LoadFile(this, name, type, -1, -1);
1028 }
1029
1030 void wxBitmap::SetPalette(const wxPalette& palette)
1031 {
1032 wxCHECK_RET(Ok(), wxT("invalid bitmap"));
1033 wxCHECK_RET(GetDepth() > 1 && GetDepth() <= 8,
1034 wxT("cannot set palette for bitmap of this depth"));
1035
1036 delete M_BMPDATA->m_palette;
1037 M_BMPDATA->m_palette = NULL;
1038
1039 if (!palette.Ok()) return;
1040
1041 M_BMPDATA->m_palette = new wxPalette(palette);
1042 }
1043
1044 wxPalette *wxBitmap::GetPalette() const
1045 {
1046 if (!Ok()) return (wxPalette *) NULL;
1047
1048 return M_BMPDATA->m_palette;
1049 }
1050
1051 void wxBitmap::SetHeight( int height )
1052 {
1053 if (!m_refData) m_refData = new wxBitmapRefData();
1054
1055 M_BMPDATA->m_height = height;
1056 }
1057
1058 void wxBitmap::SetWidth( int width )
1059 {
1060 if (!m_refData) m_refData = new wxBitmapRefData();
1061
1062 M_BMPDATA->m_width = width;
1063 }
1064
1065 void wxBitmap::SetDepth( int depth )
1066 {
1067 if (!m_refData) m_refData = new wxBitmapRefData();
1068
1069 M_BMPDATA->m_bpp = depth;
1070 }
1071
1072 void wxBitmap::SetPixmap( WXPixmap pixmap )
1073 {
1074 if (!m_refData) m_refData = new wxBitmapRefData();
1075
1076 M_BMPDATA->m_pixmap = pixmap;
1077 }
1078
1079 void wxBitmap::SetBitmap( WXPixmap bitmap )
1080 {
1081 if (!m_refData) m_refData = new wxBitmapRefData();
1082
1083 M_BMPDATA->m_bitmap = bitmap;
1084 }
1085
1086 WXPixmap wxBitmap::GetPixmap() const
1087 {
1088 wxCHECK_MSG( Ok(), (WXPixmap) NULL, wxT("invalid bitmap") );
1089
1090 return M_BMPDATA->m_pixmap;
1091 }
1092
1093 WXPixmap wxBitmap::GetBitmap() const
1094 {
1095 wxCHECK_MSG( Ok(), (WXPixmap) NULL, wxT("invalid bitmap") );
1096
1097 return M_BMPDATA->m_bitmap;
1098 }
1099
1100 WXDisplay *wxBitmap::GetDisplay() const
1101 {
1102 wxCHECK_MSG( Ok(), (WXDisplay*) NULL, wxT("invalid bitmap") );
1103
1104 return M_BMPDATA->m_display;
1105 }
1106
1107 #if wxUSE_NANOX
1108 // Copy from the drawable to the wxImage
1109 bool wxGetImageFromDrawable(GR_DRAW_ID drawable, int srcX, int srcY, int width, int height, wxImage& image)
1110 {
1111 GR_SCREEN_INFO sinfo;
1112 int x, y;
1113 GR_PIXELVAL *pixels;
1114 GR_PALETTE* palette = NULL;
1115 unsigned char rgb[3], *pp;
1116
1117 GrGetScreenInfo(&sinfo);
1118
1119 if (sinfo.pixtype == MWPF_PALETTE) {
1120 if(!(palette = (GR_PALETTE*) malloc(sizeof(GR_PALETTE)))) {
1121 return FALSE;
1122 }
1123 GrGetSystemPalette(palette);
1124 }
1125
1126 if(!(pixels = (GR_PIXELVAL*) malloc(sizeof(GR_PIXELVAL) * width * height)))
1127 {
1128 return FALSE;
1129 }
1130
1131 image.Create(width, height);
1132
1133 GrReadArea(drawable, srcX, srcY, width, height,
1134 pixels);
1135
1136
1137 for(x = 0; x < sinfo.cols; x++) {
1138
1139 pp = (unsigned char *)pixels +
1140 ((x + (y * sinfo.cols)) *
1141 sizeof(GR_PIXELVAL));
1142
1143 switch(sinfo.pixtype) {
1144 /* FIXME: These may need modifying on big endian. */
1145 case MWPF_TRUECOLOR0888:
1146 case MWPF_TRUECOLOR888:
1147 rgb[0] = pp[2];
1148 rgb[1] = pp[1];
1149 rgb[2] = pp[0];
1150 break;
1151 case MWPF_PALETTE:
1152 rgb[0] = palette->palette[pp[0]].r;
1153 rgb[1] = palette->palette[pp[0]].g;
1154 rgb[2] = palette->palette[pp[0]].b;
1155 break;
1156 case MWPF_TRUECOLOR565:
1157 rgb[0] = pp[1] & 0xf8;
1158 rgb[1] = ((pp[1] & 0x07) << 5) |
1159 ((pp[0] & 0xe0) >> 3);
1160 rgb[2] = (pp[0] & 0x1f) << 3;
1161 break;
1162 case MWPF_TRUECOLOR555:
1163 rgb[0] = (pp[1] & 0x7c) << 1;
1164 rgb[1] = ((pp[1] & 0x03) << 6) |
1165 ((pp[0] & 0xe0) >> 2);
1166 rgb[2] = (pp[0] & 0x1f) << 3;
1167 break;
1168 case MWPF_TRUECOLOR332:
1169 rgb[0] = pp[0] & 0xe0;
1170 rgb[1] = (pp[0] & 0x1c) << 3;
1171 rgb[2] = (pp[0] & 0x03) << 6;
1172 break;
1173 default:
1174 fprintf(stderr, "Unsupported pixel "
1175 "format\n");
1176 return 1;
1177 }
1178
1179 image.SetRGB(x, y, rgb[0], rgb[1], rgb[2]);
1180
1181 }
1182
1183 free(pixels);
1184 if(palette) free(palette);
1185
1186 return TRUE;
1187 }
1188
1189 #if 0
1190 int GrGetPixelColor(GR_SCREEN_INFO* sinfo, GR_PALETTE* palette, GR_PIXELVAL pixel,
1191 unsigned char* red, unsigned char* green, unsigned char* blue)
1192 {
1193 unsigned char rgb[3], *pp;
1194
1195 pp = (unsigned char*) & pixel ;
1196
1197 switch (sinfo.pixtype)
1198 {
1199 /* FIXME: These may need modifying on big endian. */
1200 case MWPF_TRUECOLOR0888:
1201 case MWPF_TRUECOLOR888:
1202 rgb[0] = pp[2];
1203 rgb[1] = pp[1];
1204 rgb[2] = pp[0];
1205 break;
1206 case MWPF_PALETTE:
1207 rgb[0] = palette->palette[pp[0]].r;
1208 rgb[1] = palette->palette[pp[0]].g;
1209 rgb[2] = palette->palette[pp[0]].b;
1210 break;
1211 case MWPF_TRUECOLOR565:
1212 rgb[0] = pp[1] & 0xf8;
1213 rgb[1] = ((pp[1] & 0x07) << 5) |
1214 ((pp[0] & 0xe0) >> 3);
1215 rgb[2] = (pp[0] & 0x1f) << 3;
1216 break;
1217 case MWPF_TRUECOLOR555:
1218 rgb[0] = (pp[1] & 0x7c) << 1;
1219 rgb[1] = ((pp[1] & 0x03) << 6) |
1220 ((pp[0] & 0xe0) >> 2);
1221 rgb[2] = (pp[0] & 0x1f) << 3;
1222 break;
1223 case MWPF_TRUECOLOR332:
1224 rgb[0] = pp[0] & 0xe0;
1225 rgb[1] = (pp[0] & 0x1c) << 3;
1226 rgb[2] = (pp[0] & 0x03) << 6;
1227 break;
1228 default:
1229 fprintf(stderr, "Unsupported pixel format\n");
1230 return 0;
1231 }
1232
1233
1234 *(red) = rgb[0];
1235 *(green) = rgb[1];
1236 *(blue) = rgb[2];
1237 return 1;
1238 }
1239 #endif
1240
1241 #endif
1242 // wxUSE_NANOX
1243
1244 // ============================================================================
1245 // Bitmap handlers
1246 // ============================================================================
1247
1248 IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler, wxBitmapHandlerBase);
1249
1250 #define M_BMPHANDLERDATA ((wxBitmapRefData *)bitmap->GetRefData())
1251
1252 #if wxUSE_XPM
1253
1254 #if wxHAVE_LIB_XPM || wxUSE_STREAMS
1255
1256 // ----------------------------------------------------------------------------
1257 // wxXPMFileHandler
1258 // ----------------------------------------------------------------------------
1259
1260 class wxXPMFileHandler : public wxBitmapHandler
1261 {
1262 DECLARE_DYNAMIC_CLASS(wxXPMFileHandler);
1263 public:
1264 wxXPMFileHandler()
1265 {
1266 m_name = "XPM file";
1267 m_extension = "xpm";
1268 m_type = wxBITMAP_TYPE_XPM;
1269 };
1270
1271 virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
1272 int desiredWidth, int desiredHeight);
1273
1274 virtual bool SaveFile(const wxBitmap *bitmap, const wxString& name,
1275 int type, const wxPalette *palette = NULL);
1276
1277 virtual bool Create(wxBitmap *bitmap, void *data, long flags,
1278 int width, int height, int depth = 1)
1279 { return FALSE; }
1280 };
1281
1282 IMPLEMENT_DYNAMIC_CLASS(wxXPMFileHandler, wxBitmapHandler);
1283
1284 bool wxXPMFileHandler::LoadFile(wxBitmap *bitmap, const wxString& name,
1285 long flags, int desiredWidth,
1286 int desiredHeight)
1287 {
1288 #if wxHAVE_LIB_XPM
1289 if (!bitmap->GetRefData())
1290 bitmap->SetRefData( new wxBitmapRefData() );
1291
1292 M_BMPHANDLERDATA->m_display = wxGlobalDisplay();
1293
1294 Display *xdisplay = (Display*) M_BMPHANDLERDATA->m_display;
1295
1296 int xscreen = DefaultScreen( xdisplay );
1297 Window xroot = RootWindow( xdisplay, xscreen );
1298
1299 int bpp = DefaultDepth( xdisplay, xscreen );
1300
1301 XpmAttributes xpmAttr;
1302 xpmAttr.valuemask = XpmReturnInfos; // nothing yet, but get infos back
1303
1304 Pixmap pixmap;
1305 Pixmap mask = 0;
1306
1307 int ErrorStatus = XpmReadFileToPixmap( xdisplay, xroot,
1308 (char*) name.c_str(),
1309 &pixmap, &mask, &xpmAttr);
1310
1311 if (ErrorStatus == XpmSuccess)
1312 {
1313 M_BMPHANDLERDATA->m_width = xpmAttr.width;
1314 M_BMPHANDLERDATA->m_height = xpmAttr.height;
1315
1316 M_BMPHANDLERDATA->m_bpp = bpp; // mono as well?
1317
1318 XpmFreeAttributes(&xpmAttr);
1319
1320 M_BMPHANDLERDATA->m_bitmap = (WXPixmap) pixmap;
1321
1322 if (mask)
1323 {
1324 M_BMPHANDLERDATA->m_mask = new wxMask;
1325 M_BMPHANDLERDATA->m_mask->SetBitmap( (WXPixmap) mask );
1326 M_BMPHANDLERDATA->m_mask->SetDisplay( xdisplay );
1327 }
1328 }
1329 else
1330 {
1331 UnRef();
1332
1333 return FALSE;
1334 }
1335
1336 return TRUE;
1337 #elif wxUSE_STREAMS
1338 wxXPMDecoder decoder;
1339 wxFileInputStream stream(name);
1340 if (stream.Ok())
1341 {
1342 wxImage image(decoder.ReadFile(stream));
1343 if (image.Ok())
1344 return CreateFromImage(image);
1345 else
1346 return FALSE;
1347 }
1348 else
1349 return FALSE;
1350 #endif // wxHAVE_LIB_XPM / wxUSE_STREAMS
1351 }
1352
1353 bool wxXPMFileHandler::SaveFile(const wxBitmap *bitmap, const wxString& name,
1354 int type,
1355 const wxPalette *WXUNUSED(palette))
1356 {
1357 wxImage image(bitmap->ConvertToImage());
1358 if (image.Ok()) return image.SaveFile( name, (wxBitmapType)type );
1359
1360 return FALSE;
1361 }
1362
1363 #endif // wxHAVE_LIB_XPM || wxUSE_STREAMS
1364
1365 // ----------------------------------------------------------------------------
1366 // wxXPMDataHandler
1367 // ----------------------------------------------------------------------------
1368
1369 class wxXPMDataHandler : public wxBitmapHandler
1370 {
1371 DECLARE_DYNAMIC_CLASS(wxXPMDataHandler);
1372 public:
1373 wxXPMDataHandler()
1374 {
1375 m_name = "XPM data";
1376 m_extension = "xpm";
1377 m_type = wxBITMAP_TYPE_XPM_DATA;
1378 };
1379
1380 virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
1381 int desiredWidth, int desiredHeight)
1382 { return FALSE; }
1383
1384 virtual bool SaveFile(const wxBitmap *bitmap, const wxString& name,
1385 int type, const wxPalette *palette = NULL)
1386 { return FALSE; }
1387
1388 virtual bool Create(wxBitmap *bitmap, void *data, long flags,
1389 int width, int height, int depth = 1);
1390 };
1391
1392 IMPLEMENT_DYNAMIC_CLASS(wxXPMDataHandler, wxBitmapHandler);
1393
1394 bool wxXPMDataHandler::Create(wxBitmap *bitmap, void *bits,
1395 long WXUNUSED(flags),
1396 int width, int height, int WXUNUSED(depth))
1397 {
1398 #if wxHAVE_LIB_XPM
1399 wxCHECK_MSG( bits != NULL, FALSE, wxT("invalid bitmap data") )
1400
1401 if (!bitmap->GetRefData())
1402 bitmap->SetRefData( new wxBitmapRefData() );
1403
1404 M_BMPHANDLERDATA->m_display = wxGlobalDisplay();
1405
1406 Display *xdisplay = (Display*) M_BMPHANDLERDATA->m_display;
1407
1408 int xscreen = DefaultScreen( xdisplay );
1409 Window xroot = RootWindow( xdisplay, xscreen );
1410
1411 int bpp = DefaultDepth( xdisplay, xscreen );
1412
1413 XpmAttributes xpmAttr;
1414 xpmAttr.valuemask = XpmReturnInfos; // nothing yet, but get infos back
1415
1416 Pixmap pixmap = 0;
1417 Pixmap mask = 0;
1418
1419 int ErrorStatus = XpmCreatePixmapFromData( xdisplay, xroot, (char**) bits,
1420 &pixmap, &mask, &xpmAttr );
1421
1422 if (ErrorStatus == XpmSuccess)
1423 {
1424 M_BMPHANDLERDATA->m_width = xpmAttr.width;
1425 M_BMPHANDLERDATA->m_height = xpmAttr.height;
1426
1427 M_BMPHANDLERDATA->m_bpp = bpp; // mono as well?
1428
1429 #if __WXDEBUG__
1430 unsigned int depthRet;
1431 int xRet, yRet;
1432 unsigned int widthRet, heightRet, borderWidthRet;
1433 XGetGeometry( xdisplay, pixmap, &xroot, &xRet, &yRet,
1434 &widthRet, &heightRet, &borderWidthRet, &depthRet);
1435
1436 wxASSERT_MSG( bpp == (int)depthRet, wxT("colour depth mismatch") );
1437 #endif
1438
1439 XpmFreeAttributes(&xpmAttr);
1440
1441 M_BMPHANDLERDATA->m_pixmap = (WXPixmap) pixmap;
1442
1443 if (mask)
1444 {
1445 M_BMPHANDLERDATA->m_mask = new wxMask;
1446 M_BMPHANDLERDATA->m_mask->SetBitmap( (WXPixmap) mask );
1447 M_BMPHANDLERDATA->m_mask->SetDisplay( xdisplay );
1448 }
1449 return TRUE;
1450 }
1451 else
1452 {
1453 bitmap->UnRef();
1454
1455 return FALSE;
1456 }
1457 #else
1458 wxXPMDecoder decoder;
1459 wxImage image(decoder.ReadData(bits));
1460 if (image.Ok())
1461 return bitmap->CreateFromImage(image);
1462 else
1463 return FALSE;
1464 #endif
1465 }
1466
1467 #endif // wxUSE_XPM
1468
1469 // ----------------------------------------------------------------------------
1470 // wxXBMDataHandler
1471 // ----------------------------------------------------------------------------
1472
1473 class WXDLLEXPORT wxXBMDataHandler: public wxBitmapHandler
1474 {
1475 DECLARE_DYNAMIC_CLASS(wxXBMDataHandler)
1476 public:
1477 inline wxXBMDataHandler()
1478 {
1479 m_name = "XBM data";
1480 m_extension = "xbm";
1481 m_type = wxBITMAP_TYPE_XBM_DATA;
1482 };
1483
1484 virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
1485 int desiredWidth, int desiredHeight)
1486 { return FALSE; }
1487
1488 virtual bool SaveFile(const wxBitmap *bitmap, const wxString& name,
1489 int type, const wxPalette *palette = NULL)
1490 { return FALSE; }
1491
1492 virtual bool Create(wxBitmap *bitmap, void *data, long flags,
1493 int width, int height, int depth = 1);
1494 };
1495
1496 IMPLEMENT_DYNAMIC_CLASS(wxXBMDataHandler, wxBitmapHandler);
1497
1498 bool wxXBMDataHandler::Create( wxBitmap *bitmap, void *bits,
1499 long WXUNUSED(flags),
1500 int width, int height, int depth)
1501 {
1502 #if !wxUSE_NANOX
1503 if (!bitmap->GetRefData())
1504 bitmap->SetRefData( new wxBitmapRefData() );
1505
1506 M_BMPHANDLERDATA->m_display = wxGlobalDisplay();
1507
1508 Display *xdisplay = (Display*) M_BMPHANDLERDATA->m_display;
1509
1510 int xscreen = DefaultScreen( xdisplay );
1511 Window xroot = RootWindow( xdisplay, xscreen );
1512
1513 M_BMPHANDLERDATA->m_mask = (wxMask *) NULL;
1514 M_BMPHANDLERDATA->m_bitmap =
1515 (WXPixmap) XCreateBitmapFromData( xdisplay, xroot,
1516 (char *) bits, width, height );
1517 M_BMPHANDLERDATA->m_width = width;
1518 M_BMPHANDLERDATA->m_height = height;
1519 M_BMPHANDLERDATA->m_bpp = 1;
1520
1521 return TRUE;
1522 #endif
1523 wxCHECK_MSG( M_BMPHANDLERDATA->m_bitmap, FALSE,
1524 wxT("couldn't create bitmap") );
1525 }
1526
1527 void wxBitmap::InitStandardHandlers()
1528 {
1529 AddHandler(new wxXBMDataHandler);
1530 #if wxUSE_XPM
1531 #if wxHAVE_LIB_XPM || wxUSE_STREAMS
1532 AddHandler(new wxXPMFileHandler);
1533 #endif
1534 AddHandler(new wxXPMDataHandler);
1535 #endif
1536 }