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