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