]>
Commit | Line | Data |
---|---|---|
83df96d6 JS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: bitmap.cpp | |
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 |
83df96d6 JS |
9 | // Licence: wxWindows licence |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "bitmap.h" | |
14 | #endif | |
15 | ||
83df96d6 JS |
16 | #include "wx/bitmap.h" |
17 | #include "wx/icon.h" | |
18 | #include "wx/log.h" | |
83df96d6 JS |
19 | #include "wx/image.h" |
20 | #include "wx/app.h" | |
c79a329d JS |
21 | #if wxUSE_NANOX |
22 | #include "wx/dcmemory.h" | |
23 | #endif | |
83df96d6 | 24 | |
bc797f4c | 25 | #include "wx/x11/private.h" |
83df96d6 | 26 | |
c79a329d JS |
27 | /* No point in using libXPM for NanoX */ |
28 | #if wxUSE_NANOX | |
29 | #undef wxHAVE_LIB_XPM | |
30 | #define wxHAVE_LIB_XPM 0 | |
31 | #endif | |
32 | ||
707440dc | 33 | #if wxUSE_XPM |
83df96d6 | 34 | #if wxHAVE_LIB_XPM |
707440dc JS |
35 | #include <X11/xpm.h> |
36 | #else | |
37 | #include "wx/xpmdecod.h" | |
38 | #include "wx/wfstream.h" | |
39 | #endif | |
83df96d6 JS |
40 | #endif |
41 | #include <math.h> | |
42 | ||
a11672a4 RR |
43 | //----------------------------------------------------------------------------- |
44 | // wxMask | |
45 | //----------------------------------------------------------------------------- | |
83df96d6 | 46 | |
a11672a4 | 47 | IMPLEMENT_DYNAMIC_CLASS(wxMask,wxObject) |
83df96d6 | 48 | |
a11672a4 | 49 | wxMask::wxMask() |
83df96d6 | 50 | { |
a11672a4 RR |
51 | m_bitmap = NULL; |
52 | m_display = NULL; | |
83df96d6 JS |
53 | } |
54 | ||
a11672a4 | 55 | wxMask::wxMask( const wxBitmap& bitmap, const wxColour& colour ) |
83df96d6 | 56 | { |
a11672a4 RR |
57 | m_bitmap = NULL; |
58 | Create( bitmap, colour ); | |
83df96d6 JS |
59 | } |
60 | ||
a11672a4 | 61 | wxMask::wxMask( const wxBitmap& bitmap, int paletteIndex ) |
83df96d6 | 62 | { |
a11672a4 RR |
63 | m_bitmap = NULL; |
64 | Create( bitmap, paletteIndex ); | |
83df96d6 JS |
65 | } |
66 | ||
a11672a4 | 67 | wxMask::wxMask( const wxBitmap& bitmap ) |
83df96d6 | 68 | { |
a11672a4 RR |
69 | m_bitmap = NULL; |
70 | Create( bitmap ); | |
83df96d6 JS |
71 | } |
72 | ||
a11672a4 | 73 | wxMask::~wxMask() |
83df96d6 | 74 | { |
a11672a4 RR |
75 | if (m_bitmap) |
76 | XFreePixmap( (Display*) m_display, (Pixmap) m_bitmap ); | |
83df96d6 JS |
77 | } |
78 | ||
a11672a4 RR |
79 | bool wxMask::Create( const wxBitmap& bitmap, |
80 | const wxColour& colour ) | |
83df96d6 | 81 | { |
c79a329d | 82 | #if !wxUSE_NANOX |
a11672a4 RR |
83 | if (m_bitmap) |
84 | { | |
85 | XFreePixmap( (Display*) m_display, (Pixmap) m_bitmap ); | |
86 | m_bitmap = NULL; | |
87 | } | |
83df96d6 | 88 | |
a11672a4 | 89 | m_display = bitmap.GetDisplay(); |
83df96d6 | 90 | |
a11672a4 RR |
91 | wxImage image( bitmap ); |
92 | if (!image.Ok()) return FALSE; | |
93 | ||
94 | m_display = bitmap.GetDisplay(); | |
95 | ||
96 | Display *xdisplay = (Display*) m_display; | |
83df96d6 | 97 | |
a11672a4 RR |
98 | int xscreen = DefaultScreen( xdisplay ); |
99 | Window xroot = RootWindow( xdisplay, xscreen ); | |
100 | Visual* xvisual = DefaultVisual( xdisplay, xscreen ); | |
101 | int bpp = DefaultDepth( xdisplay, xscreen ); | |
102 | ||
103 | m_bitmap = (WXPixmap) XCreatePixmap( xdisplay, xroot, image.GetWidth(), image.GetHeight(), 1 ); | |
104 | GC gc = XCreateGC( xdisplay, (Pixmap) m_bitmap, 0, NULL ); | |
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 | |
a11672a4 RR |
117 | XVisualInfo vinfo_template; |
118 | XVisualInfo *vi; | |
83df96d6 | 119 | |
a11672a4 RR |
120 | vinfo_template.visual = xvisual; |
121 | vinfo_template.visualid = XVisualIDFromVisual( xvisual ); | |
122 | vinfo_template.depth = bpp; | |
123 | int nitem = 0; | |
83df96d6 | 124 | |
a11672a4 RR |
125 | vi = XGetVisualInfo( xdisplay, VisualIDMask|VisualDepthMask, &vinfo_template, &nitem ); |
126 | wxASSERT_MSG( vi, wxT("No visual info") ); | |
127 | ||
128 | if ((bpp == 16) && (vi->red_mask != 0xf800)) bpp = 15; | |
129 | if (bpp == 15) | |
130 | { | |
131 | red = red & 0xf8; | |
132 | green = green & 0xf8; | |
133 | blue = blue & 0xf8; | |
134 | } else | |
135 | if (bpp == 16) | |
136 | { | |
137 | red = red & 0xf8; | |
138 | green = green & 0xfc; | |
139 | blue = blue & 0xf8; | |
140 | } else | |
141 | if (bpp == 12) | |
142 | { | |
143 | red = red & 0xf0; | |
144 | green = green & 0xf0; | |
145 | blue = blue & 0xf0; | |
146 | } | |
83df96d6 | 147 | |
a11672a4 | 148 | XSetForeground( xdisplay, gc, BlackPixel(xdisplay,xscreen) ); |
83df96d6 | 149 | |
a11672a4 RR |
150 | for (int j = 0; j < image.GetHeight(); j++) |
151 | { | |
152 | int start_x = -1; | |
153 | int i; | |
154 | for (i = 0; i < image.GetWidth(); i++) | |
83df96d6 | 155 | { |
a11672a4 RR |
156 | if ((data[index] == red) && |
157 | (data[index+1] == green) && | |
158 | (data[index+2] == blue)) | |
159 | { | |
160 | if (start_x == -1) | |
161 | start_x = i; | |
162 | } | |
163 | else | |
164 | { | |
165 | if (start_x != -1) | |
166 | { | |
167 | XDrawLine( xdisplay, (Pixmap) m_bitmap, gc, start_x, j, i-1, j ); | |
168 | start_x = -1; | |
169 | } | |
170 | } | |
171 | index += 3; | |
83df96d6 | 172 | } |
a11672a4 RR |
173 | if (start_x != -1) |
174 | XDrawLine( xdisplay, (Pixmap) m_bitmap, gc, start_x, j, i, j ); | |
83df96d6 | 175 | } |
a11672a4 RR |
176 | |
177 | XFreeGC( xdisplay, gc ); | |
83df96d6 | 178 | |
a11672a4 | 179 | return TRUE; |
c79a329d JS |
180 | #else |
181 | return FALSE; | |
182 | #endif | |
183 | // wxUSE_NANOX | |
83df96d6 JS |
184 | } |
185 | ||
a11672a4 | 186 | bool wxMask::Create( const wxBitmap& bitmap, int paletteIndex ) |
83df96d6 | 187 | { |
a11672a4 RR |
188 | unsigned char r,g,b; |
189 | wxPalette *pal = bitmap.GetPalette(); | |
83df96d6 | 190 | |
a11672a4 | 191 | wxCHECK_MSG( pal, FALSE, wxT("Cannot create mask from bitmap without palette") ); |
83df96d6 | 192 | |
a11672a4 | 193 | pal->GetRGB(paletteIndex, &r, &g, &b); |
83df96d6 | 194 | |
a11672a4 | 195 | return Create(bitmap, wxColour(r, g, b)); |
83df96d6 JS |
196 | } |
197 | ||
a11672a4 | 198 | bool wxMask::Create( const wxBitmap& bitmap ) |
83df96d6 | 199 | { |
c79a329d | 200 | #if !wxUSE_NANOX |
a11672a4 RR |
201 | if (m_bitmap) |
202 | { | |
203 | XFreePixmap( (Display*) m_display, (Pixmap) m_bitmap ); | |
204 | m_bitmap = NULL; | |
83df96d6 JS |
205 | } |
206 | ||
a11672a4 | 207 | if (!bitmap.Ok()) return FALSE; |
83df96d6 | 208 | |
a11672a4 RR |
209 | wxCHECK_MSG( bitmap.GetBitmap(), FALSE, wxT("Cannot create mask from colour bitmap") ); |
210 | ||
211 | m_display = bitmap.GetDisplay(); | |
83df96d6 | 212 | |
a11672a4 RR |
213 | int xscreen = DefaultScreen( (Display*) m_display ); |
214 | Window xroot = RootWindow( (Display*) m_display, xscreen ); | |
215 | ||
216 | m_bitmap = (WXPixmap) XCreatePixmap( (Display*) m_display, xroot, bitmap.GetWidth(), bitmap.GetHeight(), 1 ); | |
83df96d6 | 217 | |
a11672a4 RR |
218 | if (!m_bitmap) return FALSE; |
219 | ||
220 | GC gc = XCreateGC( (Display*) m_display, (Pixmap) m_bitmap, 0, NULL ); | |
83df96d6 | 221 | |
a11672a4 RR |
222 | XCopyPlane( (Display*) m_display, (Pixmap) bitmap.GetBitmap(), (Pixmap) m_bitmap, |
223 | gc, 0, 0, bitmap.GetWidth(), bitmap.GetHeight(), 0, 0, 1 ); | |
224 | ||
225 | XFreeGC( (Display*) m_display, gc ); | |
83df96d6 | 226 | |
a11672a4 | 227 | return TRUE; |
c79a329d JS |
228 | #else |
229 | return FALSE; | |
230 | #endif | |
231 | // wxUSE_NANOX | |
83df96d6 JS |
232 | } |
233 | ||
a11672a4 RR |
234 | //----------------------------------------------------------------------------- |
235 | // wxBitmap | |
236 | //----------------------------------------------------------------------------- | |
83df96d6 | 237 | |
a11672a4 | 238 | class wxBitmapRefData: public wxObjectRefData |
83df96d6 | 239 | { |
a11672a4 RR |
240 | public: |
241 | wxBitmapRefData(); | |
242 | ~wxBitmapRefData(); | |
243 | ||
244 | WXPixmap m_pixmap; | |
245 | WXPixmap m_bitmap; | |
246 | WXDisplay *m_display; | |
247 | wxMask *m_mask; | |
248 | int m_width; | |
249 | int m_height; | |
250 | int m_bpp; | |
251 | wxPalette *m_palette; | |
252 | }; | |
83df96d6 | 253 | |
a11672a4 | 254 | wxBitmapRefData::wxBitmapRefData() |
83df96d6 | 255 | { |
a11672a4 RR |
256 | m_pixmap = NULL; |
257 | m_bitmap = NULL; | |
258 | m_display = NULL; | |
259 | m_mask = (wxMask *) NULL; | |
260 | m_width = 0; | |
261 | m_height = 0; | |
262 | m_bpp = 0; | |
263 | m_palette = (wxPalette *) NULL; | |
83df96d6 JS |
264 | } |
265 | ||
a11672a4 | 266 | wxBitmapRefData::~wxBitmapRefData() |
83df96d6 | 267 | { |
a11672a4 RR |
268 | if (m_pixmap) XFreePixmap( (Display*) m_display, (Pixmap) m_pixmap ); |
269 | if (m_bitmap) XFreePixmap( (Display*) m_display, (Pixmap) m_bitmap ); | |
270 | if (m_mask) delete m_mask; | |
271 | if (m_palette) delete m_palette; | |
83df96d6 JS |
272 | } |
273 | ||
a11672a4 | 274 | //----------------------------------------------------------------------------- |
83df96d6 | 275 | |
a11672a4 | 276 | #define M_BMPDATA ((wxBitmapRefData *)m_refData) |
83df96d6 | 277 | |
a11672a4 | 278 | IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxGDIObject) |
83df96d6 | 279 | |
a11672a4 | 280 | wxBitmap::wxBitmap() |
83df96d6 | 281 | { |
83df96d6 JS |
282 | } |
283 | ||
a11672a4 | 284 | wxBitmap::wxBitmap( int width, int height, int depth ) |
83df96d6 | 285 | { |
a11672a4 | 286 | Create( width, height, depth ); |
83df96d6 JS |
287 | } |
288 | ||
a11672a4 | 289 | bool wxBitmap::Create( int width, int height, int depth ) |
83df96d6 | 290 | { |
a11672a4 RR |
291 | UnRef(); |
292 | ||
293 | wxCHECK_MSG( (width > 0) && (height > 0), FALSE, wxT("invalid bitmap size") ) | |
294 | ||
295 | m_refData = new wxBitmapRefData(); | |
296 | ||
297 | M_BMPDATA->m_display = wxGlobalDisplay(); | |
298 | ||
299 | wxASSERT_MSG( M_BMPDATA->m_display, wxT("No display") ); | |
300 | ||
301 | int xscreen = DefaultScreen( (Display*) M_BMPDATA->m_display ); | |
302 | Window xroot = RootWindow( (Display*) M_BMPDATA->m_display, xscreen ); | |
303 | ||
304 | int bpp = DefaultDepth( (Display*) M_BMPDATA->m_display, xscreen ); | |
305 | if (depth == -1) depth = bpp; | |
306 | ||
307 | wxCHECK_MSG( (depth == bpp) || | |
308 | (depth == 1), FALSE, wxT("invalid bitmap depth") ) | |
309 | ||
310 | M_BMPDATA->m_mask = (wxMask *) NULL; | |
311 | M_BMPDATA->m_width = width; | |
312 | M_BMPDATA->m_height = height; | |
c79a329d JS |
313 | |
314 | #if wxUSE_NANOX | |
315 | M_BMPDATA->m_bitmap = (WXPixmap) GrNewPixmap(width, height, NULL); | |
316 | M_BMPDATA->m_bpp = bpp; | |
317 | ||
318 | wxASSERT_MSG( M_BMPDATA->m_bitmap, wxT("Bitmap creation failed") ); | |
319 | #else | |
a11672a4 | 320 | if (depth == 1) |
83df96d6 | 321 | { |
a11672a4 RR |
322 | M_BMPDATA->m_bitmap = (WXPixmap) XCreatePixmap( (Display*) M_BMPDATA->m_display, xroot, width, height, 1 ); |
323 | ||
324 | wxASSERT_MSG( M_BMPDATA->m_bitmap, wxT("Bitmap creation failed") ); | |
325 | ||
326 | M_BMPDATA->m_bpp = 1; | |
83df96d6 JS |
327 | } |
328 | else | |
83df96d6 | 329 | { |
a11672a4 RR |
330 | M_BMPDATA->m_pixmap = (WXPixmap) XCreatePixmap( (Display*) M_BMPDATA->m_display, xroot, width, height, depth ); |
331 | ||
332 | wxASSERT_MSG( M_BMPDATA->m_pixmap, wxT("Pixmap creation failed") ); | |
333 | ||
334 | M_BMPDATA->m_bpp = depth; | |
83df96d6 | 335 | } |
c79a329d | 336 | #endif |
a11672a4 | 337 | return Ok(); |
83df96d6 JS |
338 | } |
339 | ||
a11672a4 | 340 | bool wxBitmap::CreateFromXpm( const char **bits ) |
83df96d6 | 341 | { |
707440dc JS |
342 | #if wxUSE_XPM |
343 | #if wxHAVE_LIB_XPM | |
a11672a4 RR |
344 | UnRef(); |
345 | ||
346 | wxCHECK_MSG( bits != NULL, FALSE, wxT("invalid bitmap data") ) | |
347 | ||
348 | m_refData = new wxBitmapRefData(); | |
349 | ||
350 | M_BMPDATA->m_display = wxGlobalDisplay(); | |
351 | ||
352 | Display *xdisplay = (Display*) M_BMPDATA->m_display; | |
353 | ||
354 | int xscreen = DefaultScreen( xdisplay ); | |
355 | Window xroot = RootWindow( xdisplay, xscreen ); | |
356 | ||
357 | int bpp = DefaultDepth( xdisplay, xscreen ); | |
358 | ||
359 | XpmAttributes xpmAttr; | |
360 | xpmAttr.valuemask = XpmReturnInfos; // nothing yet, but get infos back | |
361 | ||
362 | Pixmap pixmap; | |
363 | Pixmap mask = 0; | |
364 | ||
365 | int ErrorStatus = XpmCreatePixmapFromData( xdisplay, xroot, (char**) bits, &pixmap, &mask, &xpmAttr ); | |
366 | ||
367 | if (ErrorStatus == XpmSuccess) | |
83df96d6 | 368 | { |
a11672a4 RR |
369 | M_BMPDATA->m_width = xpmAttr.width; |
370 | M_BMPDATA->m_height = xpmAttr.height; | |
83df96d6 | 371 | |
a11672a4 | 372 | M_BMPDATA->m_bpp = bpp; // mono as well? |
83df96d6 | 373 | |
a11672a4 RR |
374 | #if __WXDEBUG__ |
375 | unsigned int depthRet; | |
376 | int xRet, yRet; | |
377 | unsigned int widthRet, heightRet, borderWidthRet; | |
378 | XGetGeometry( xdisplay, pixmap, &xroot, &xRet, &yRet, | |
379 | &widthRet, &heightRet, &borderWidthRet, &depthRet); | |
83df96d6 | 380 | |
a11672a4 RR |
381 | wxASSERT_MSG( bpp == (int)depthRet, wxT("colour depth mismatch") ) |
382 | #endif | |
383 | ||
384 | XpmFreeAttributes(&xpmAttr); | |
385 | ||
386 | M_BMPDATA->m_pixmap = (WXPixmap) pixmap; | |
387 | ||
388 | if (mask) | |
389 | { | |
390 | M_BMPDATA->m_mask = new wxMask; | |
391 | M_BMPDATA->m_mask->SetBitmap( (WXPixmap) mask ); | |
392 | M_BMPDATA->m_mask->SetDisplay( xdisplay ); | |
393 | } | |
707440dc | 394 | return TRUE; |
a11672a4 RR |
395 | } |
396 | else | |
397 | { | |
398 | UnRef(); | |
399 | ||
400 | return FALSE; | |
401 | } | |
707440dc JS |
402 | #else |
403 | wxXPMDecoder decoder; | |
404 | wxImage image(decoder.ReadData(bits)); | |
405 | if (image.Ok()) | |
406 | return CreateFromImage(image); | |
407 | else | |
408 | return FALSE; | |
409 | #endif | |
410 | #endif | |
411 | return FALSE; | |
83df96d6 JS |
412 | } |
413 | ||
a11672a4 | 414 | bool wxBitmap::CreateFromImage( const wxImage& image, int depth ) |
83df96d6 | 415 | { |
c79a329d JS |
416 | #if wxUSE_NANOX |
417 | if (!image.Ok()) | |
418 | { | |
419 | wxASSERT_MSG(image.Ok(), "Invalid wxImage passed to wxBitmap::CreateFromImage."); | |
420 | return FALSE; | |
421 | } | |
422 | ||
423 | int w = image.GetWidth(); | |
424 | int h = image.GetHeight(); | |
425 | ||
426 | if (!Create(w, h, depth)) | |
427 | return FALSE; | |
428 | ||
429 | wxMemoryDC memDC; | |
430 | memDC.SelectObject(*this); | |
431 | ||
432 | // Warning: this is very inefficient. | |
433 | wxPen pen; | |
434 | pen.SetStyle(wxSOLID); | |
435 | pen.SetWidth(1); | |
436 | ||
437 | int i, j; | |
438 | for (i = 0; i < w; i++) | |
439 | { | |
440 | for (j = 0; j < h; j++) | |
441 | { | |
442 | unsigned char red = image.GetRed(i, j); | |
443 | unsigned char green = image.GetGreen(i, j); | |
444 | unsigned char blue = image.GetBlue(i, j); | |
445 | wxColour colour(red, green, blue); | |
446 | ||
447 | pen.SetColour(colour); | |
448 | memDC.SetPen(pen); | |
449 | memDC.DrawPoint(i, j); | |
450 | ||
451 | #if 0 | |
452 | if (hasMask) | |
453 | { | |
454 | // scan the bitmap for the transparent colour and set the corresponding | |
455 | // pixels in the mask to BLACK and the rest to WHITE | |
456 | if (maskR == red && maskG == green && maskB == blue) | |
457 | ::SetPixel(hMaskDC, i, j, PALETTERGB(0, 0, 0)); | |
458 | else | |
459 | ::SetPixel(hMaskDC, i, j, PALETTERGB(255, 255, 255)); | |
460 | } | |
461 | #endif | |
462 | } | |
463 | } | |
464 | memDC.SelectObject(wxNullBitmap); | |
465 | ||
466 | return TRUE; | |
467 | #else | |
468 | // !wxUSE_NANOX | |
469 | ||
a11672a4 | 470 | UnRef(); |
83df96d6 | 471 | |
a11672a4 RR |
472 | wxCHECK_MSG( image.Ok(), FALSE, wxT("invalid image") ) |
473 | wxCHECK_MSG( depth == -1, FALSE, wxT("invalid bitmap depth") ) | |
83df96d6 | 474 | |
a11672a4 | 475 | m_refData = new wxBitmapRefData(); |
83df96d6 | 476 | |
a11672a4 RR |
477 | M_BMPDATA->m_display = wxGlobalDisplay(); |
478 | ||
479 | Display *xdisplay = (Display*) M_BMPDATA->m_display; | |
480 | ||
481 | int xscreen = DefaultScreen( xdisplay ); | |
482 | Window xroot = RootWindow( xdisplay, xscreen ); | |
483 | Visual* xvisual = DefaultVisual( xdisplay, xscreen ); | |
484 | ||
485 | int bpp = DefaultDepth( xdisplay, xscreen ); | |
486 | ||
487 | int width = image.GetWidth(); | |
488 | int height = image.GetHeight(); | |
489 | M_BMPDATA->m_width = width; | |
490 | M_BMPDATA->m_height = height; | |
83df96d6 | 491 | |
a11672a4 RR |
492 | if (depth != 1) depth = bpp; |
493 | M_BMPDATA->m_bpp = depth; | |
494 | ||
495 | if (depth == 1) | |
496 | { | |
497 | wxFAIL_MSG( "mono images later" ); | |
498 | } | |
499 | else | |
500 | { | |
501 | // Create image | |
502 | ||
503 | XImage *data_image = XCreateImage( xdisplay, xvisual, bpp, ZPixmap, 0, 0, width, height, 32, 0 ); | |
504 | data_image->data = (char*) malloc( data_image->bytes_per_line * data_image->height ); | |
505 | ||
506 | if (data_image->data == NULL) | |
507 | { | |
508 | wxLogError( wxT("Out of memory.") ); // TODO clean | |
509 | return FALSE; | |
510 | } | |
83df96d6 | 511 | |
a11672a4 | 512 | M_BMPDATA->m_pixmap = (WXPixmap) XCreatePixmap( xdisplay, xroot, width, height, depth ); |
83df96d6 | 513 | |
a11672a4 | 514 | // Create mask |
83df96d6 | 515 | |
a11672a4 RR |
516 | XImage *mask_image = (XImage*) NULL; |
517 | if (image.HasMask()) | |
518 | { | |
519 | mask_image = XCreateImage( xdisplay, xvisual, 1, ZPixmap, 0, 0, width, height, 32, 0 ); | |
520 | mask_image->data = (char*) malloc( mask_image->bytes_per_line * mask_image->height ); | |
521 | ||
522 | if (mask_image->data == NULL) | |
523 | { | |
524 | wxLogError( wxT("Out of memory.") ); // TODO clean | |
525 | return FALSE; | |
526 | } | |
527 | ||
528 | wxMask *mask = new wxMask(); | |
529 | mask->SetDisplay( xdisplay ); | |
530 | mask->SetBitmap( (WXPixmap) XCreatePixmap( xdisplay, xroot, width, height, 1 ) ); | |
83df96d6 | 531 | |
a11672a4 RR |
532 | SetMask( mask ); |
533 | } | |
83df96d6 | 534 | |
a11672a4 | 535 | // Retrieve info |
83df96d6 | 536 | |
a11672a4 RR |
537 | XVisualInfo vinfo_template; |
538 | XVisualInfo *vi; | |
83df96d6 | 539 | |
a11672a4 RR |
540 | vinfo_template.visual = xvisual; |
541 | vinfo_template.visualid = XVisualIDFromVisual( xvisual ); | |
542 | vinfo_template.depth = bpp; | |
543 | int nitem = 0; | |
83df96d6 | 544 | |
a11672a4 RR |
545 | vi = XGetVisualInfo( xdisplay, VisualIDMask|VisualDepthMask, &vinfo_template, &nitem ); |
546 | wxASSERT_MSG( vi, wxT("No visual info") ); | |
83df96d6 | 547 | |
a11672a4 RR |
548 | if ((bpp == 16) && (vi->red_mask != 0xf800)) bpp = 15; |
549 | if (bpp < 8) bpp = 8; | |
83df96d6 | 550 | |
a11672a4 | 551 | // Render |
83df96d6 | 552 | |
a11672a4 RR |
553 | enum byte_order { RGB, RBG, BRG, BGR, GRB, GBR }; |
554 | byte_order b_o = RGB; | |
83df96d6 | 555 | |
a11672a4 RR |
556 | if (bpp > 8) |
557 | { | |
558 | if ((vi->red_mask > vi->green_mask) && (vi->green_mask > vi->blue_mask)) b_o = RGB; | |
559 | else if ((vi->red_mask > vi->blue_mask) && (vi->blue_mask > vi->green_mask)) b_o = RBG; | |
560 | else if ((vi->blue_mask > vi->red_mask) && (vi->red_mask > vi->green_mask)) b_o = BRG; | |
561 | else if ((vi->blue_mask > vi->green_mask) && (vi->green_mask > vi->red_mask)) b_o = BGR; | |
562 | else if ((vi->green_mask > vi->red_mask) && (vi->red_mask > vi->blue_mask)) b_o = GRB; | |
563 | else if ((vi->green_mask > vi->blue_mask) && (vi->blue_mask > vi->red_mask)) b_o = GBR; | |
564 | } | |
83df96d6 | 565 | |
a11672a4 | 566 | XFree( vi ); |
83df96d6 | 567 | |
a11672a4 RR |
568 | int r_mask = image.GetMaskRed(); |
569 | int g_mask = image.GetMaskGreen(); | |
570 | int b_mask = image.GetMaskBlue(); | |
83df96d6 | 571 | |
a11672a4 RR |
572 | unsigned char* data = image.GetData(); |
573 | wxASSERT_MSG( data, "No image data" ); | |
83df96d6 | 574 | |
a11672a4 | 575 | bool hasMask = image.HasMask(); |
83df96d6 | 576 | |
a11672a4 RR |
577 | int index = 0; |
578 | for (int y = 0; y < height; y++) | |
579 | { | |
580 | for (int x = 0; x < width; x++) | |
581 | { | |
582 | int r = data[index]; | |
583 | index++; | |
584 | int g = data[index]; | |
585 | index++; | |
586 | int b = data[index]; | |
587 | index++; | |
588 | ||
589 | if (hasMask) | |
590 | { | |
591 | if ((r == r_mask) && (b == b_mask) && (g == g_mask)) | |
592 | XPutPixel( mask_image, x, y, 0 ); | |
593 | else | |
594 | XPutPixel( mask_image, x, y, 1 ); | |
595 | } | |
83df96d6 | 596 | |
a11672a4 RR |
597 | switch (bpp) |
598 | { | |
599 | case 8: | |
600 | { | |
601 | int pixel = 0; | |
602 | #if 0 | |
603 | if (wxTheApp->m_colorCube) | |
604 | { | |
605 | pixel = wxTheApp->m_colorCube[ ((r & 0xf8) << 7) + ((g & 0xf8) << 2) + ((b & 0xf8) >> 3) ]; | |
606 | } | |
607 | else | |
608 | { | |
609 | GdkColormap *cmap = gtk_widget_get_default_colormap(); | |
610 | GdkColor *colors = cmap->colors; | |
611 | int max = 3 * (65536); | |
612 | ||
613 | for (int i = 0; i < cmap->size; i++) | |
614 | { | |
615 | int rdiff = (r << 8) - colors[i].red; | |
616 | int gdiff = (g << 8) - colors[i].green; | |
617 | int bdiff = (b << 8) - colors[i].blue; | |
618 | int sum = ABS (rdiff) + ABS (gdiff) + ABS (bdiff); | |
619 | if (sum < max) { pixel = i; max = sum; } | |
620 | } | |
621 | } | |
622 | #endif | |
623 | XPutPixel( data_image, x, y, pixel ); | |
624 | break; | |
625 | } | |
626 | case 12: // SGI only | |
627 | { | |
628 | int pixel = 0; | |
629 | switch (b_o) | |
630 | { | |
631 | case RGB: pixel = ((r & 0xf0) << 4) | (g & 0xf0) | ((b & 0xf0) >> 4); break; | |
632 | case RBG: pixel = ((r & 0xf0) << 4) | (b & 0xf0) | ((g & 0xf0) >> 4); break; | |
633 | case GRB: pixel = ((g & 0xf0) << 4) | (r & 0xf0) | ((b & 0xf0) >> 4); break; | |
634 | case GBR: pixel = ((g & 0xf0) << 4) | (b & 0xf0) | ((r & 0xf0) >> 4); break; | |
635 | case BRG: pixel = ((b & 0xf0) << 4) | (r & 0xf0) | ((g & 0xf0) >> 4); break; | |
636 | case BGR: pixel = ((b & 0xf0) << 4) | (g & 0xf0) | ((r & 0xf0) >> 4); break; | |
637 | } | |
638 | XPutPixel( data_image, x, y, pixel ); | |
639 | break; | |
640 | } | |
641 | case 15: | |
642 | { | |
643 | int pixel = 0; | |
644 | switch (b_o) | |
645 | { | |
646 | case RGB: pixel = ((r & 0xf8) << 7) | ((g & 0xf8) << 2) | ((b & 0xf8) >> 3); break; | |
647 | case RBG: pixel = ((r & 0xf8) << 7) | ((b & 0xf8) << 2) | ((g & 0xf8) >> 3); break; | |
648 | case GRB: pixel = ((g & 0xf8) << 7) | ((r & 0xf8) << 2) | ((b & 0xf8) >> 3); break; | |
649 | case GBR: pixel = ((g & 0xf8) << 7) | ((b & 0xf8) << 2) | ((r & 0xf8) >> 3); break; | |
650 | case BRG: pixel = ((b & 0xf8) << 7) | ((r & 0xf8) << 2) | ((g & 0xf8) >> 3); break; | |
651 | case BGR: pixel = ((b & 0xf8) << 7) | ((g & 0xf8) << 2) | ((r & 0xf8) >> 3); break; | |
652 | } | |
653 | XPutPixel( data_image, x, y, pixel ); | |
654 | break; | |
655 | } | |
656 | case 16: | |
657 | { | |
658 | // I actually don't know if for 16-bit displays, it is alway the green | |
659 | // component or the second component which has 6 bits. | |
660 | int pixel = 0; | |
661 | switch (b_o) | |
662 | { | |
663 | case RGB: pixel = ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | ((b & 0xf8) >> 3); break; | |
664 | case RBG: pixel = ((r & 0xf8) << 8) | ((b & 0xfc) << 3) | ((g & 0xf8) >> 3); break; | |
665 | case GRB: pixel = ((g & 0xf8) << 8) | ((r & 0xfc) << 3) | ((b & 0xf8) >> 3); break; | |
666 | case GBR: pixel = ((g & 0xf8) << 8) | ((b & 0xfc) << 3) | ((r & 0xf8) >> 3); break; | |
667 | case BRG: pixel = ((b & 0xf8) << 8) | ((r & 0xfc) << 3) | ((g & 0xf8) >> 3); break; | |
668 | case BGR: pixel = ((b & 0xf8) << 8) | ((g & 0xfc) << 3) | ((r & 0xf8) >> 3); break; | |
669 | } | |
670 | XPutPixel( data_image, x, y, pixel ); | |
671 | break; | |
672 | } | |
673 | case 32: | |
674 | case 24: | |
675 | { | |
676 | int pixel = 0; | |
677 | switch (b_o) | |
678 | { | |
679 | case RGB: pixel = (r << 16) | (g << 8) | b; break; | |
680 | case RBG: pixel = (r << 16) | (b << 8) | g; break; | |
681 | case BRG: pixel = (b << 16) | (r << 8) | g; break; | |
682 | case BGR: pixel = (b << 16) | (g << 8) | r; break; | |
683 | case GRB: pixel = (g << 16) | (r << 8) | b; break; | |
684 | case GBR: pixel = (g << 16) | (b << 8) | r; break; | |
685 | } | |
686 | XPutPixel( data_image, x, y, pixel ); | |
687 | } | |
688 | default: break; | |
689 | } | |
690 | } // for | |
691 | } // for | |
83df96d6 | 692 | |
a11672a4 | 693 | // Blit picture |
83df96d6 | 694 | |
a11672a4 RR |
695 | GC gc = XCreateGC( xdisplay, (Pixmap) M_BMPDATA->m_pixmap, 0, NULL ); |
696 | XPutImage( xdisplay, (Pixmap) M_BMPDATA->m_pixmap, gc, data_image, 0, 0, 0, 0, width, height ); | |
e334d0ea JS |
697 | #ifdef __WXDEBUG__ |
698 | XSync(wxGlobalDisplay(), False); | |
699 | #endif | |
83df96d6 | 700 | |
a11672a4 RR |
701 | XDestroyImage( data_image ); |
702 | XFreeGC( xdisplay, gc ); | |
83df96d6 | 703 | |
a11672a4 RR |
704 | // Blit mask |
705 | ||
706 | if (image.HasMask()) | |
707 | { | |
708 | GC gc = XCreateGC( xdisplay, (Pixmap) GetMask()->GetBitmap(), 0, NULL ); | |
709 | XPutImage( xdisplay, (Pixmap) GetMask()->GetBitmap(), gc, data_image, 0, 0, 0, 0, width, height ); | |
710 | ||
711 | XDestroyImage( mask_image ); | |
712 | XFreeGC( xdisplay, gc ); | |
713 | } | |
714 | } | |
83df96d6 | 715 | |
83df96d6 | 716 | return TRUE; |
c79a329d JS |
717 | #endif |
718 | // wxUSE_NANOX | |
83df96d6 JS |
719 | } |
720 | ||
a11672a4 | 721 | static void wxCalcPrecAndShift( unsigned long mask, int *shift, int *prec ) |
83df96d6 | 722 | { |
a11672a4 RR |
723 | *shift = 0; |
724 | *prec = 0; | |
725 | ||
726 | while (!(mask & 0x1)) | |
83df96d6 | 727 | { |
a11672a4 RR |
728 | (*shift)++; |
729 | mask >>= 1; | |
83df96d6 | 730 | } |
83df96d6 | 731 | |
a11672a4 RR |
732 | while (mask & 0x1) |
733 | { | |
734 | (*prec)++; | |
735 | mask >>= 1; | |
736 | } | |
83df96d6 JS |
737 | } |
738 | ||
a11672a4 | 739 | wxImage wxBitmap::ConvertToImage() const |
83df96d6 | 740 | { |
a11672a4 | 741 | wxImage image; |
83df96d6 | 742 | |
a11672a4 | 743 | wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") ); |
83df96d6 | 744 | |
a11672a4 RR |
745 | Display *xdisplay = (Display*) M_BMPDATA->m_display; |
746 | wxASSERT_MSG( xdisplay, wxT("No display") ); | |
747 | ||
748 | int xscreen = DefaultScreen( xdisplay ); | |
749 | Visual* xvisual = DefaultVisual( xdisplay, xscreen ); | |
750 | ||
751 | int bpp = DefaultDepth( xdisplay, xscreen ); | |
c79a329d JS |
752 | |
753 | #if wxUSE_NANOX | |
754 | int w = image.GetWidth(); | |
755 | int h = image.GetHeight(); | |
a11672a4 | 756 | |
c79a329d JS |
757 | wxMemoryDC memDC; |
758 | memDC.SelectObject(*this); | |
759 | ||
760 | wxColour pixelCol; | |
761 | ||
762 | // Warning: this is very inefficient. | |
763 | int i, j; | |
764 | for (i = 0; i < w; i++) | |
765 | { | |
766 | for (j = 0; j < h; j++) | |
767 | { | |
768 | memDC.GetPixel(i, j, & pixelCol); | |
769 | ||
770 | // TODO: make wxColour accessors more efficient | |
771 | // by inlining, if possible | |
772 | image.SetRGB(i, j, | |
773 | pixelCol.Red(), pixelCol.Green(), | |
774 | pixelCol.Blue()); | |
775 | } | |
776 | } | |
777 | memDC.SelectObject(wxNullBitmap); | |
778 | ||
779 | return image; | |
780 | ||
781 | #else | |
782 | // !wxUSE_NANOX | |
a11672a4 RR |
783 | XImage *x_image = NULL; |
784 | if (GetPixmap()) | |
785 | { | |
786 | x_image = XGetImage( xdisplay, (Pixmap) GetPixmap(), | |
787 | 0, 0, | |
788 | GetWidth(), GetHeight(), | |
789 | AllPlanes, ZPixmap ); | |
790 | } else | |
791 | if (GetBitmap()) | |
792 | { | |
793 | x_image = XGetImage( xdisplay, (Pixmap) GetBitmap(), | |
794 | 0, 0, | |
795 | GetWidth(), GetHeight(), | |
796 | AllPlanes, ZPixmap ); | |
797 | } else | |
798 | { | |
799 | wxFAIL_MSG( wxT("Ill-formed bitmap") ); | |
800 | } | |
83df96d6 | 801 | |
a11672a4 | 802 | wxCHECK_MSG( x_image, wxNullImage, wxT("couldn't create image") ); |
83df96d6 | 803 | |
a11672a4 RR |
804 | image.Create( GetWidth(), GetHeight() ); |
805 | char unsigned *data = image.GetData(); | |
83df96d6 | 806 | |
a11672a4 RR |
807 | if (!data) |
808 | { | |
809 | XDestroyImage( x_image ); | |
810 | wxFAIL_MSG( wxT("couldn't create image") ); | |
811 | return wxNullImage; | |
812 | } | |
83df96d6 | 813 | |
a11672a4 RR |
814 | XImage *x_image_mask = NULL; |
815 | if (GetMask()) | |
816 | { | |
817 | x_image_mask = XGetImage( xdisplay, (Pixmap) GetMask()->GetBitmap(), | |
818 | 0, 0, | |
819 | GetWidth(), GetHeight(), | |
820 | AllPlanes, ZPixmap ); | |
83df96d6 | 821 | |
a11672a4 RR |
822 | image.SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable |
823 | } | |
83df96d6 | 824 | |
a11672a4 RR |
825 | int red_shift_right = 0; |
826 | int green_shift_right = 0; | |
827 | int blue_shift_right = 0; | |
828 | int red_shift_left = 0; | |
829 | int green_shift_left = 0; | |
830 | int blue_shift_left = 0; | |
831 | bool use_shift = FALSE; | |
83df96d6 | 832 | |
a11672a4 RR |
833 | if (GetPixmap()) |
834 | { | |
835 | // Retrieve info | |
836 | ||
837 | XVisualInfo vinfo_template; | |
838 | XVisualInfo *vi; | |
839 | ||
840 | vinfo_template.visual = xvisual; | |
841 | vinfo_template.visualid = XVisualIDFromVisual( xvisual ); | |
842 | vinfo_template.depth = bpp; | |
843 | int nitem = 0; | |
844 | ||
845 | vi = XGetVisualInfo( xdisplay, VisualIDMask|VisualDepthMask, &vinfo_template, &nitem ); | |
846 | wxASSERT_MSG( vi, wxT("No visual info") ); | |
847 | ||
848 | int red_prec,green_prec,blue_prec; | |
849 | int red_shift,green_shift,blue_shift; | |
850 | wxCalcPrecAndShift( vi->red_mask, &red_shift, &red_prec ); | |
851 | wxCalcPrecAndShift( vi->green_mask, &green_shift, &green_prec ); | |
852 | wxCalcPrecAndShift( vi->blue_mask, &blue_shift, &blue_prec ); | |
853 | if (bpp == 16) bpp = red_prec + green_prec + blue_prec; | |
854 | ||
855 | red_shift_right = red_shift; | |
856 | red_shift_left = 8-red_prec; | |
857 | green_shift_right = green_shift; | |
858 | green_shift_left = 8-green_prec; | |
859 | blue_shift_right = blue_shift; | |
860 | blue_shift_left = 8-blue_prec; | |
861 | ||
862 | #if 0 | |
863 | use_shift = (vi->visual->c_class == TrueColor) || (vi->visual->c_class == DirectColor); | |
864 | #else | |
865 | use_shift = TRUE; | |
866 | #endif | |
867 | ||
868 | XFree( vi ); | |
869 | } | |
870 | if (GetBitmap()) | |
871 | { | |
872 | bpp = 1; | |
873 | } | |
83df96d6 | 874 | |
83df96d6 | 875 | |
a11672a4 | 876 | // GdkColormap *cmap = gtk_widget_get_default_colormap(); |
83df96d6 | 877 | |
a11672a4 RR |
878 | long pos = 0; |
879 | for (int j = 0; j < GetHeight(); j++) | |
880 | { | |
881 | for (int i = 0; i < GetWidth(); i++) | |
882 | { | |
883 | unsigned long pixel = XGetPixel( x_image, i, j ); | |
884 | if (bpp == 1) | |
885 | { | |
886 | if (pixel == 0) | |
887 | { | |
888 | data[pos] = 0; | |
889 | data[pos+1] = 0; | |
890 | data[pos+2] = 0; | |
891 | } | |
892 | else | |
893 | { | |
894 | data[pos] = 255; | |
895 | data[pos+1] = 255; | |
896 | data[pos+2] = 255; | |
897 | } | |
898 | } | |
899 | else if (use_shift) | |
900 | { | |
901 | data[pos] = (pixel >> red_shift_right) << red_shift_left; | |
902 | data[pos+1] = (pixel >> green_shift_right) << green_shift_left; | |
903 | data[pos+2] = (pixel >> blue_shift_right) << blue_shift_left; | |
904 | } | |
905 | #if 0 | |
906 | else if (cmap->colors) | |
907 | { | |
908 | data[pos] = cmap->colors[pixel].red >> 8; | |
909 | data[pos+1] = cmap->colors[pixel].green >> 8; | |
910 | data[pos+2] = cmap->colors[pixel].blue >> 8; | |
911 | } | |
912 | #endif | |
913 | else | |
914 | { | |
915 | wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") ); | |
916 | } | |
83df96d6 | 917 | |
a11672a4 RR |
918 | if (x_image_mask) |
919 | { | |
920 | int mask_pixel = XGetPixel( x_image_mask, i, j ); | |
921 | if (mask_pixel == 0) | |
922 | { | |
923 | data[pos] = 16; | |
924 | data[pos+1] = 16; | |
925 | data[pos+2] = 16; | |
926 | } | |
927 | } | |
83df96d6 | 928 | |
a11672a4 RR |
929 | pos += 3; |
930 | } | |
931 | } | |
83df96d6 | 932 | |
a11672a4 RR |
933 | XDestroyImage( x_image ); |
934 | if (x_image_mask) XDestroyImage( x_image_mask ); | |
a11672a4 | 935 | return image; |
c79a329d JS |
936 | #endif |
937 | // wxUSE_NANOX | |
83df96d6 JS |
938 | } |
939 | ||
a11672a4 | 940 | wxBitmap::wxBitmap( const wxBitmap& bmp ) |
83df96d6 | 941 | { |
a11672a4 | 942 | Ref( bmp ); |
83df96d6 JS |
943 | } |
944 | ||
a11672a4 | 945 | wxBitmap::wxBitmap( const wxString &filename, int type ) |
83df96d6 | 946 | { |
a11672a4 | 947 | LoadFile( filename, type ); |
83df96d6 JS |
948 | } |
949 | ||
a11672a4 | 950 | wxBitmap::wxBitmap( const char bits[], int width, int height, int WXUNUSED(depth) ) |
83df96d6 | 951 | { |
c79a329d | 952 | #if !wxUSE_NANOX |
a11672a4 | 953 | m_refData = new wxBitmapRefData(); |
83df96d6 | 954 | |
a11672a4 | 955 | M_BMPDATA->m_display = wxGlobalDisplay(); |
83df96d6 | 956 | |
a11672a4 RR |
957 | Display *xdisplay = (Display*) M_BMPDATA->m_display; |
958 | ||
959 | int xscreen = DefaultScreen( xdisplay ); | |
960 | Window xroot = RootWindow( xdisplay, xscreen ); | |
961 | ||
962 | M_BMPDATA->m_mask = (wxMask *) NULL; | |
963 | M_BMPDATA->m_bitmap = (WXPixmap) XCreateBitmapFromData( xdisplay, xroot, (char *) bits, width, height ); | |
964 | M_BMPDATA->m_width = width; | |
965 | M_BMPDATA->m_height = height; | |
966 | M_BMPDATA->m_bpp = 1; | |
c79a329d | 967 | #endif |
a11672a4 | 968 | wxCHECK_RET( M_BMPDATA->m_bitmap, wxT("couldn't create bitmap") ); |
83df96d6 JS |
969 | } |
970 | ||
a11672a4 | 971 | wxBitmap::~wxBitmap() |
83df96d6 | 972 | { |
83df96d6 JS |
973 | } |
974 | ||
a11672a4 | 975 | wxBitmap& wxBitmap::operator = ( const wxBitmap& bmp ) |
83df96d6 | 976 | { |
a11672a4 RR |
977 | if ( m_refData != bmp.m_refData ) |
978 | Ref( bmp ); | |
83df96d6 | 979 | |
a11672a4 RR |
980 | return *this; |
981 | } | |
83df96d6 | 982 | |
a11672a4 RR |
983 | bool wxBitmap::operator == ( const wxBitmap& bmp ) const |
984 | { | |
985 | return m_refData == bmp.m_refData; | |
986 | } | |
83df96d6 | 987 | |
a11672a4 RR |
988 | bool wxBitmap::operator != ( const wxBitmap& bmp ) const |
989 | { | |
990 | return m_refData != bmp.m_refData; | |
991 | } | |
83df96d6 | 992 | |
a11672a4 RR |
993 | bool wxBitmap::Ok() const |
994 | { | |
995 | return (m_refData != NULL); | |
996 | } | |
83df96d6 | 997 | |
a11672a4 RR |
998 | int wxBitmap::GetHeight() const |
999 | { | |
1000 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); | |
83df96d6 | 1001 | |
a11672a4 RR |
1002 | return M_BMPDATA->m_height; |
1003 | } | |
83df96d6 | 1004 | |
a11672a4 RR |
1005 | int wxBitmap::GetWidth() const |
1006 | { | |
1007 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); | |
83df96d6 | 1008 | |
a11672a4 RR |
1009 | return M_BMPDATA->m_width; |
1010 | } | |
83df96d6 | 1011 | |
a11672a4 RR |
1012 | int wxBitmap::GetDepth() const |
1013 | { | |
1014 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); | |
83df96d6 | 1015 | |
a11672a4 RR |
1016 | return M_BMPDATA->m_bpp; |
1017 | } | |
83df96d6 | 1018 | |
a11672a4 RR |
1019 | wxMask *wxBitmap::GetMask() const |
1020 | { | |
1021 | wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") ); | |
83df96d6 | 1022 | |
a11672a4 RR |
1023 | return M_BMPDATA->m_mask; |
1024 | } | |
83df96d6 | 1025 | |
a11672a4 RR |
1026 | void wxBitmap::SetMask( wxMask *mask ) |
1027 | { | |
1028 | wxCHECK_RET( Ok(), wxT("invalid bitmap") ); | |
83df96d6 | 1029 | |
a11672a4 | 1030 | if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask; |
83df96d6 | 1031 | |
a11672a4 RR |
1032 | M_BMPDATA->m_mask = mask; |
1033 | } | |
83df96d6 | 1034 | |
a11672a4 RR |
1035 | bool wxBitmap::CopyFromIcon(const wxIcon& icon) |
1036 | { | |
1037 | *this = icon; | |
1038 | return TRUE; | |
1039 | } | |
83df96d6 | 1040 | |
a11672a4 RR |
1041 | wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const |
1042 | { | |
1043 | wxCHECK_MSG( Ok() && | |
1044 | (rect.x >= 0) && (rect.y >= 0) && | |
1045 | (rect.x+rect.width <= M_BMPDATA->m_width) && (rect.y+rect.height <= M_BMPDATA->m_height), | |
1046 | wxNullBitmap, wxT("invalid bitmap or bitmap region") ); | |
83df96d6 | 1047 | |
a11672a4 RR |
1048 | wxBitmap ret( rect.width, rect.height, M_BMPDATA->m_bpp ); |
1049 | wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") ); | |
83df96d6 | 1050 | |
a11672a4 RR |
1051 | |
1052 | wxFAIL_MSG( "wxBitmap::GetSubBitmap not yet implemented" ); | |
1053 | ||
1054 | #if 0 | |
1055 | if (ret.GetPixmap()) | |
83df96d6 | 1056 | { |
a11672a4 RR |
1057 | GdkGC *gc = gdk_gc_new( ret.GetPixmap() ); |
1058 | gdk_draw_pixmap( ret.GetPixmap(), gc, GetPixmap(), rect.x, rect.y, 0, 0, rect.width, rect.height ); | |
1059 | gdk_gc_destroy( gc ); | |
1060 | } | |
1061 | else | |
1062 | { | |
1063 | GdkGC *gc = gdk_gc_new( ret.GetBitmap() ); | |
1064 | gdk_wx_draw_bitmap( ret.GetBitmap(), gc, GetBitmap(), rect.x, rect.y, 0, 0, rect.width, rect.height ); | |
1065 | gdk_gc_destroy( gc ); | |
83df96d6 JS |
1066 | } |
1067 | ||
a11672a4 | 1068 | if (GetMask()) |
83df96d6 | 1069 | { |
a11672a4 RR |
1070 | wxMask *mask = new wxMask; |
1071 | mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, rect.width, rect.height, 1 ); | |
1072 | ||
1073 | GdkGC *gc = gdk_gc_new( mask->m_bitmap ); | |
1074 | gdk_wx_draw_bitmap( mask->m_bitmap, gc, M_BMPDATA->m_mask->m_bitmap, 0, 0, rect.x, rect.y, rect.width, rect.height ); | |
1075 | gdk_gc_destroy( gc ); | |
83df96d6 | 1076 | |
a11672a4 | 1077 | ret.SetMask( mask ); |
83df96d6 | 1078 | } |
a11672a4 | 1079 | #endif |
83df96d6 | 1080 | |
a11672a4 RR |
1081 | return ret; |
1082 | } | |
83df96d6 | 1083 | |
a11672a4 RR |
1084 | bool wxBitmap::SaveFile( const wxString &name, int type, wxPalette *WXUNUSED(palette) ) |
1085 | { | |
1086 | wxCHECK_MSG( Ok(), FALSE, wxT("invalid bitmap") ); | |
83df96d6 | 1087 | |
a11672a4 | 1088 | // Try to save the bitmap via wxImage handlers: |
83df96d6 | 1089 | { |
a11672a4 RR |
1090 | wxImage image( *this ); |
1091 | if (image.Ok()) return image.SaveFile( name, type ); | |
1092 | } | |
83df96d6 | 1093 | |
a11672a4 RR |
1094 | return FALSE; |
1095 | } | |
83df96d6 | 1096 | |
a11672a4 RR |
1097 | bool wxBitmap::LoadFile( const wxString &name, int type ) |
1098 | { | |
1099 | UnRef(); | |
83df96d6 | 1100 | |
a11672a4 | 1101 | if (!wxFileExists(name)) return FALSE; |
83df96d6 | 1102 | |
83df96d6 | 1103 | |
a11672a4 | 1104 | if (type == wxBITMAP_TYPE_XPM) |
83df96d6 | 1105 | { |
707440dc JS |
1106 | #if wxUSE_XPM |
1107 | #if wxHAVE_LIB_XPM | |
a11672a4 | 1108 | m_refData = new wxBitmapRefData(); |
83df96d6 | 1109 | |
a11672a4 RR |
1110 | M_BMPDATA->m_display = wxGlobalDisplay(); |
1111 | ||
1112 | Display *xdisplay = (Display*) M_BMPDATA->m_display; | |
1113 | ||
1114 | int xscreen = DefaultScreen( xdisplay ); | |
1115 | Window xroot = RootWindow( xdisplay, xscreen ); | |
1116 | ||
1117 | int bpp = DefaultDepth( xdisplay, xscreen ); | |
83df96d6 | 1118 | |
a11672a4 RR |
1119 | XpmAttributes xpmAttr; |
1120 | xpmAttr.valuemask = XpmReturnInfos; // nothing yet, but get infos back | |
83df96d6 | 1121 | |
a11672a4 RR |
1122 | Pixmap pixmap; |
1123 | Pixmap mask = 0; | |
1124 | ||
1125 | int ErrorStatus = XpmReadFileToPixmap( xdisplay, xroot, (char*) name.c_str(), &pixmap, &mask, &xpmAttr); | |
1126 | ||
1127 | if (ErrorStatus == XpmSuccess) | |
1128 | { | |
1129 | M_BMPDATA->m_width = xpmAttr.width; | |
1130 | M_BMPDATA->m_height = xpmAttr.height; | |
83df96d6 | 1131 | |
a11672a4 | 1132 | M_BMPDATA->m_bpp = bpp; // mono as well? |
83df96d6 | 1133 | |
a11672a4 RR |
1134 | XpmFreeAttributes(&xpmAttr); |
1135 | ||
1136 | M_BMPDATA->m_bitmap = (WXPixmap) pixmap; | |
1137 | ||
1138 | if (mask) | |
1139 | { | |
1140 | M_BMPDATA->m_mask = new wxMask; | |
1141 | M_BMPDATA->m_mask->SetBitmap( (WXPixmap) mask ); | |
1142 | M_BMPDATA->m_mask->SetDisplay( xdisplay ); | |
1143 | } | |
1144 | } | |
1145 | else | |
1146 | { | |
1147 | UnRef(); | |
1148 | ||
1149 | return FALSE; | |
1150 | } | |
707440dc JS |
1151 | #else |
1152 | #if wxUSE_STREAMS | |
1153 | wxXPMDecoder decoder; | |
1154 | wxFileInputStream stream(name); | |
1155 | if (stream.Ok()) | |
1156 | { | |
c79a329d | 1157 | wxImage image(decoder.ReadFile(stream)); |
707440dc JS |
1158 | if (image.Ok()) |
1159 | return CreateFromImage(image); | |
1160 | else | |
1161 | return FALSE; | |
1162 | } | |
1163 | else | |
1164 | return FALSE; | |
1165 | #else | |
1166 | return FALSE; | |
1167 | #endif | |
1168 | // wxUSE_STREAMS | |
1169 | #endif | |
1170 | // wxHAVE_LIB_XPM | |
1171 | #endif | |
1172 | // wxUSE_XPM | |
1173 | return FALSE; | |
a11672a4 RR |
1174 | } |
1175 | else // try if wxImage can load it | |
1176 | { | |
1177 | wxImage image; | |
1178 | if (!image.LoadFile( name, type )) return FALSE; | |
1179 | if (image.Ok()) *this = image.ConvertToBitmap(); | |
1180 | else return FALSE; | |
83df96d6 | 1181 | } |
83df96d6 JS |
1182 | |
1183 | return TRUE; | |
1184 | } | |
1185 | ||
a11672a4 | 1186 | wxPalette *wxBitmap::GetPalette() const |
83df96d6 | 1187 | { |
a11672a4 | 1188 | if (!Ok()) return (wxPalette *) NULL; |
83df96d6 | 1189 | |
a11672a4 RR |
1190 | return M_BMPDATA->m_palette; |
1191 | } | |
83df96d6 | 1192 | |
a11672a4 RR |
1193 | void wxBitmap::SetHeight( int height ) |
1194 | { | |
1195 | if (!m_refData) m_refData = new wxBitmapRefData(); | |
83df96d6 | 1196 | |
a11672a4 RR |
1197 | M_BMPDATA->m_height = height; |
1198 | } | |
83df96d6 | 1199 | |
a11672a4 RR |
1200 | void wxBitmap::SetWidth( int width ) |
1201 | { | |
1202 | if (!m_refData) m_refData = new wxBitmapRefData(); | |
83df96d6 | 1203 | |
a11672a4 RR |
1204 | M_BMPDATA->m_width = width; |
1205 | } | |
83df96d6 | 1206 | |
a11672a4 RR |
1207 | void wxBitmap::SetDepth( int depth ) |
1208 | { | |
1209 | if (!m_refData) m_refData = new wxBitmapRefData(); | |
83df96d6 | 1210 | |
a11672a4 RR |
1211 | M_BMPDATA->m_bpp = depth; |
1212 | } | |
83df96d6 | 1213 | |
a11672a4 RR |
1214 | void wxBitmap::SetPixmap( WXPixmap pixmap ) |
1215 | { | |
1216 | if (!m_refData) m_refData = new wxBitmapRefData(); | |
83df96d6 | 1217 | |
a11672a4 RR |
1218 | M_BMPDATA->m_pixmap = pixmap; |
1219 | } | |
83df96d6 | 1220 | |
a11672a4 RR |
1221 | void wxBitmap::SetBitmap( WXPixmap bitmap ) |
1222 | { | |
1223 | if (!m_refData) m_refData = new wxBitmapRefData(); | |
83df96d6 | 1224 | |
a11672a4 RR |
1225 | M_BMPDATA->m_bitmap = bitmap; |
1226 | } | |
83df96d6 | 1227 | |
a11672a4 RR |
1228 | WXPixmap wxBitmap::GetPixmap() const |
1229 | { | |
1230 | wxCHECK_MSG( Ok(), (WXPixmap) NULL, wxT("invalid bitmap") ); | |
83df96d6 | 1231 | |
a11672a4 RR |
1232 | return M_BMPDATA->m_pixmap; |
1233 | } | |
83df96d6 | 1234 | |
a11672a4 RR |
1235 | WXPixmap wxBitmap::GetBitmap() const |
1236 | { | |
1237 | wxCHECK_MSG( Ok(), (WXPixmap) NULL, wxT("invalid bitmap") ); | |
83df96d6 | 1238 | |
a11672a4 | 1239 | return M_BMPDATA->m_bitmap; |
83df96d6 | 1240 | } |
7266b672 | 1241 | |
a11672a4 | 1242 | WXDisplay *wxBitmap::GetDisplay() const |
7266b672 | 1243 | { |
a11672a4 | 1244 | wxCHECK_MSG( Ok(), (WXDisplay*) NULL, wxT("invalid bitmap") ); |
b6ed4565 | 1245 | |
a11672a4 | 1246 | return M_BMPDATA->m_display; |
7266b672 | 1247 | } |
a11672a4 | 1248 |