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