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