]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/bitmap.mm
1) some compilers (Watcom) don't like it if wxBase library PCH file contains
[wxWidgets.git] / src / cocoa / bitmap.mm
CommitLineData
a24aff65 1/////////////////////////////////////////////////////////////////////////////
d135b1a5 2// Name: src/cocoa/bitmap.cpp
a24aff65 3// Purpose: wxBitmap
d135b1a5 4// Author: David Elliott
a24aff65 5// Modified by:
d135b1a5 6// Created: 2003/07/19
a24aff65 7// RCS-ID: $Id$
d135b1a5 8// Copyright: (c) 2003 David Elliott
a24aff65
DE
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
a24aff65
DE
12#include "wx/setup.h"
13#include "wx/utils.h"
14#include "wx/palette.h"
15#include "wx/bitmap.h"
16#include "wx/icon.h"
17#include "wx/log.h"
18#include "wx/image.h"
d135b1a5 19#include "wx/xpmdecod.h"
a24aff65 20
d135b1a5
DE
21#include "wx/cocoa/autorelease.h"
22#include "wx/cocoa/string.h"
23
24#import <AppKit/NSBitmapImageRep.h>
25#import <AppKit/NSGraphics.h>
26
27// ========================================================================
28// wxBitmapRefData
29// ========================================================================
30class wxBitmapRefData: public wxGDIRefData
31{
32 friend class wxBitmap;
33public:
34 wxBitmapRefData();
35 wxBitmapRefData( const wxBitmapRefData& data );
36 virtual ~wxBitmapRefData();
37
38protected:
39 int m_width;
40 int m_height;
41 int m_depth;
42 bool m_ok;
43 int m_numColors;
44 wxPalette m_bitmapPalette;
45 int m_quality;
46 WX_NSBitmapImageRep m_cocoaNSBitmapImageRep;
47 wxMask *m_bitmapMask; // Optional mask
48};
49
50#define M_BITMAPDATA ((wxBitmapRefData *)m_refData)
a24aff65
DE
51
52wxBitmapRefData::wxBitmapRefData()
53{
54 m_ok = FALSE;
55 m_width = 0;
56 m_height = 0;
57 m_depth = 0;
58 m_quality = 0;
59 m_numColors = 0;
d135b1a5 60 m_cocoaNSBitmapImageRep = nil;
a24aff65
DE
61 m_bitmapMask = NULL;
62}
63
d135b1a5
DE
64wxBitmapRefData::wxBitmapRefData( const wxBitmapRefData& data)
65{
66 m_width = data.m_width;
67 m_height = data.m_height;
68 m_depth = data.m_depth;
69 m_ok = data.m_ok;
70 m_numColors = data.m_numColors;
71 m_bitmapPalette = data.m_bitmapPalette;
72 m_quality = data.m_quality;
73 m_cocoaNSBitmapImageRep = data.m_cocoaNSBitmapImageRep;
74 [m_cocoaNSBitmapImageRep retain];
75 m_bitmapMask = data.m_bitmapMask?new wxMask(*data.m_bitmapMask):NULL;
76}
77
a24aff65
DE
78wxBitmapRefData::~wxBitmapRefData()
79{
d135b1a5
DE
80 [m_cocoaNSBitmapImageRep release];
81 m_cocoaNSBitmapImageRep = NULL;
a24aff65 82
d135b1a5 83 delete m_bitmapMask;
a24aff65
DE
84 m_bitmapMask = NULL;
85}
86
d135b1a5
DE
87// ========================================================================
88// wxBitmap
89// ========================================================================
90IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject)
91
a24aff65
DE
92wxBitmap::wxBitmap()
93{
94 m_refData = NULL;
a24aff65
DE
95}
96
97wxBitmap::~wxBitmap()
98{
a24aff65
DE
99}
100
101wxBitmap::wxBitmap(const char bits[], int the_width, int the_height, int no_bits)
102{
103 m_refData = new wxBitmapRefData;
104
105 M_BITMAPDATA->m_width = the_width ;
106 M_BITMAPDATA->m_height = the_height ;
107 M_BITMAPDATA->m_depth = no_bits ;
108 M_BITMAPDATA->m_numColors = 0;
109
110 /* TODO: create the bitmap from data */
a24aff65
DE
111}
112
113wxBitmap::wxBitmap(int w, int h, int d)
114{
115 (void)Create(w, h, d);
a24aff65
DE
116}
117
118wxBitmap::wxBitmap(void *data, wxBitmapType type, int width, int height, int depth)
119{
120 (void) Create(data, type, width, height, depth);
a24aff65
DE
121}
122
123wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type)
124{
125 LoadFile(filename, type);
a24aff65
DE
126}
127
d135b1a5 128wxObjectRefData *wxBitmap::CreateRefData() const
a24aff65 129{
d135b1a5 130 return new wxBitmapRefData;
a24aff65
DE
131}
132
d135b1a5 133wxObjectRefData *wxBitmap::CloneRefData(const wxObjectRefData *data) const
a24aff65 134{
d135b1a5 135 return new wxBitmapRefData(*(wxBitmapRefData*)data);
a24aff65
DE
136}
137
d135b1a5 138WX_NSBitmapImageRep wxBitmap::GetNSBitmapImageRep()
a24aff65 139{
d135b1a5
DE
140 if(!M_BITMAPDATA)
141 return NULL;
142 return M_BITMAPDATA->m_cocoaNSBitmapImageRep;
a24aff65
DE
143}
144
145void wxBitmap::SetWidth(int w)
146{
147 if (!M_BITMAPDATA)
148 m_refData = new wxBitmapRefData;
149
150 M_BITMAPDATA->m_width = w;
151}
152
153void wxBitmap::SetHeight(int h)
154{
155 if (!M_BITMAPDATA)
156 m_refData = new wxBitmapRefData;
157
158 M_BITMAPDATA->m_height = h;
159}
160
161void wxBitmap::SetDepth(int d)
162{
163 if (!M_BITMAPDATA)
164 m_refData = new wxBitmapRefData;
165
166 M_BITMAPDATA->m_depth = d;
167}
168
169void wxBitmap::SetQuality(int q)
170{
171 if (!M_BITMAPDATA)
172 m_refData = new wxBitmapRefData;
173
174 M_BITMAPDATA->m_quality = q;
175}
176
177void wxBitmap::SetOk(bool isOk)
178{
179 if (!M_BITMAPDATA)
180 m_refData = new wxBitmapRefData;
181
182 M_BITMAPDATA->m_ok = isOk;
183}
184
185void wxBitmap::SetPalette(const wxPalette& palette)
186{
187 if (!M_BITMAPDATA)
188 m_refData = new wxBitmapRefData;
189
190 M_BITMAPDATA->m_bitmapPalette = palette ;
191}
192
193void wxBitmap::SetMask(wxMask *mask)
194{
195 if (!M_BITMAPDATA)
196 m_refData = new wxBitmapRefData;
197
198 M_BITMAPDATA->m_bitmapMask = mask ;
199}
200
d135b1a5 201bool wxBitmap::Ok() const
a24aff65 202{
d135b1a5 203 return m_refData && M_BITMAPDATA->m_ok;
a24aff65
DE
204}
205
d135b1a5 206wxPalette* wxBitmap::GetPalette() const
a24aff65 207{
d135b1a5
DE
208 if(!m_refData)
209 return NULL;
210 return &M_BITMAPDATA->m_bitmapPalette;
a24aff65
DE
211}
212
d135b1a5 213wxMask* wxBitmap::GetMask() const
a24aff65 214{
d135b1a5
DE
215 if(!m_refData)
216 return NULL;
217 return M_BITMAPDATA->m_bitmapMask;
a24aff65
DE
218}
219
d135b1a5 220int wxBitmap::GetDepth() const
a24aff65 221{
d135b1a5
DE
222 if(!m_refData)
223 return 0;
224 return M_BITMAPDATA->m_depth;
a24aff65
DE
225}
226
d135b1a5 227int wxBitmap::GetWidth() const
a24aff65 228{
d135b1a5
DE
229 if(!m_refData)
230 return 0;
231 return M_BITMAPDATA->m_width;
a24aff65
DE
232}
233
d135b1a5 234int wxBitmap::GetHeight() const
a24aff65 235{
d135b1a5
DE
236 if(!m_refData)
237 return 0;
238 return M_BITMAPDATA->m_height;
a24aff65
DE
239}
240
d135b1a5 241bool wxBitmap::Create(int w, int h, int d)
a24aff65 242{
d135b1a5
DE
243 UnRef();
244
245 m_refData = new wxBitmapRefData;
246
247 M_BITMAPDATA->m_width = w;
248 M_BITMAPDATA->m_height = h;
249 M_BITMAPDATA->m_depth = d;
250
251 /* TODO: create new bitmap */
252
253 return M_BITMAPDATA->m_ok;
a24aff65
DE
254}
255
d135b1a5 256bool wxBitmap::LoadFile(const wxString& filename, wxBitmapType type)
a24aff65 257{
d135b1a5
DE
258 wxAutoNSAutoreleasePool pool;
259 UnRef();
a24aff65 260
d135b1a5 261 m_refData = new wxBitmapRefData;
a24aff65 262
d135b1a5
DE
263 NSBitmapImageRep *imageRep = [NSBitmapImageRep
264 imageRepWithContentsOfFile:wxNSStringWithWxString(filename)];
a24aff65 265
d135b1a5
DE
266 if(imageRep)
267 {
268 M_BITMAPDATA->m_width = [imageRep pixelsWide];
269 M_BITMAPDATA->m_height = [imageRep pixelsHigh];
270 M_BITMAPDATA->m_depth = 24; // FIXME
271 M_BITMAPDATA->m_ok = true;
272 M_BITMAPDATA->m_numColors = 0;
273 M_BITMAPDATA->m_quality = 0;
274 M_BITMAPDATA->m_cocoaNSBitmapImageRep = [imageRep retain];
275 M_BITMAPDATA->m_bitmapMask = NULL;
276 return true;
277 }
278 wxImage image;
279 if(!image.LoadFile(filename,type))
280 return false;
281 if(!image.Ok())
282 return false;
283 *this = wxBitmap(image);
284 return true;
a24aff65
DE
285}
286
d135b1a5 287bool wxBitmap::Create(void *data, wxBitmapType type, int width, int height, int depth)
a24aff65 288{
d135b1a5
DE
289 UnRef();
290
291 m_refData = new wxBitmapRefData;
292
293 return false;
a24aff65
DE
294}
295
d135b1a5 296bool wxBitmap::SaveFile(const wxString& filename, wxBitmapType type, const wxPalette *palette) const
a24aff65 297{
d135b1a5 298 return false;
a24aff65
DE
299}
300
d135b1a5 301bool wxBitmap::CopyFromIcon(const wxIcon& icno)
a24aff65 302{
d135b1a5 303 return false;
a24aff65
DE
304}
305
d135b1a5 306wxBitmap wxBitmap::GetSubBitmap(wxRect const&) const
a24aff65 307{
d135b1a5
DE
308 return wxNullBitmap;
309}
a24aff65 310
d135b1a5 311wxImage wxBitmap::ConvertToImage() const
a24aff65 312{
d135b1a5
DE
313 if(!M_BITMAPDATA->m_ok)
314 return wxImage(5,5)/*wxNullImage*/;
315 return wxImage(M_BITMAPDATA->m_width,M_BITMAPDATA->m_height);
a24aff65
DE
316}
317
d135b1a5 318bool wxBitmap::CreateFromXpm(const char **xpm)
a24aff65 319{
d135b1a5
DE
320#if wxUSE_IMAGE && wxUSE_XPM
321 UnRef();
322
323 wxCHECK_MSG( xpm, false, wxT("invalid XPM data") )
324
325 wxXPMDecoder decoder;
326 wxImage img = decoder.ReadData(xpm);
327 wxCHECK_MSG( img.Ok(), false, wxT("invalid XPM data") )
328
329 *this = wxBitmap(img);
330 return true;
331#else
a24aff65 332 return false;
d135b1a5 333#endif
a24aff65
DE
334}
335
d135b1a5 336bool wxBitmap::CreateFromImage(const wxImage& image, int depth)
a24aff65 337{
d135b1a5
DE
338 UnRef();
339
340 wxCHECK_MSG(image.Ok(), false, wxT("invalid image"));
341 wxCHECK_MSG(depth == -1 || depth == 1, false, wxT("invalid bitmap depth"));
342
343 m_refData = new wxBitmapRefData();
344
345 M_BITMAPDATA->m_width = image.GetWidth();
346 M_BITMAPDATA->m_height = image.GetHeight();
347 NSBitmapImageRep *bitmapImage = [[NSBitmapImageRep alloc]
348 initWithBitmapDataPlanes: NULL
349 pixelsWide: image.GetWidth()
350 pixelsHigh: image.GetHeight()
351 bitsPerSample: 8
352 samplesPerPixel: 3
353 hasAlpha: NO
354 isPlanar: NO
355 colorSpaceName: NSCalibratedRGBColorSpace
356 bytesPerRow: 0
357 bitsPerPixel: 0];
358
359 const int numBytes = image.GetWidth()*image.GetHeight()*3;
360 memcpy([bitmapImage bitmapData], image.GetData(), numBytes);
361 // TODO: Alpha and convert to desired depth
362 M_BITMAPDATA->m_depth = 24;
363 M_BITMAPDATA->m_ok = true;
364 M_BITMAPDATA->m_numColors = 0;
365 M_BITMAPDATA->m_quality = 0;
366 M_BITMAPDATA->m_cocoaNSBitmapImageRep = bitmapImage;
367 M_BITMAPDATA->m_bitmapMask = NULL;
368 return true;
a24aff65
DE
369}
370
d135b1a5
DE
371// ========================================================================
372// wxMask
373// ========================================================================
374
375IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject)
376
377wxMask::wxMask()
a24aff65 378{
d135b1a5
DE
379/* TODO
380 m_maskBitmap = 0;
381*/
a24aff65
DE
382}
383
d135b1a5
DE
384// Construct a mask from a bitmap and a colour indicating
385// the transparent area
386wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
a24aff65 387{
d135b1a5
DE
388/* TODO
389 m_maskBitmap = 0;
390*/
391 Create(bitmap, colour);
a24aff65
DE
392}
393
d135b1a5
DE
394// Construct a mask from a bitmap and a palette index indicating
395// the transparent area
396wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex)
a24aff65 397{
d135b1a5
DE
398/* TODO
399 m_maskBitmap = 0;
400*/
401
402 Create(bitmap, paletteIndex);
a24aff65
DE
403}
404
d135b1a5
DE
405// Construct a mask from a mono bitmap (copies the bitmap).
406wxMask::wxMask(const wxBitmap& bitmap)
a24aff65 407{
d135b1a5
DE
408/* TODO
409 m_maskBitmap = 0;
410*/
411
412 Create(bitmap);
a24aff65
DE
413}
414
d135b1a5 415wxMask::~wxMask()
a24aff65 416{
d135b1a5 417// TODO: delete mask bitmap
a24aff65
DE
418}
419
d135b1a5
DE
420// Create a mask from a mono bitmap (copies the bitmap).
421bool wxMask::Create(const wxBitmap& bitmap)
a24aff65 422{
d135b1a5
DE
423// TODO
424 return FALSE;
a24aff65
DE
425}
426
d135b1a5
DE
427// Create a mask from a bitmap and a palette index indicating
428// the transparent area
429bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex)
a24aff65 430{
d135b1a5
DE
431// TODO
432 return FALSE;
a24aff65
DE
433}
434
d135b1a5
DE
435// Create a mask from a bitmap and a colour indicating
436// the transparent area
437bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
a24aff65 438{
d135b1a5
DE
439// TODO
440 return FALSE;
a24aff65
DE
441}
442