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