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