]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/bitmap.mm
Moved wxToolBarSimple to deprecated library
[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;
ab13160e 73 m_cocoaNSBitmapImageRep = [data.m_cocoaNSBitmapImageRep copyWithZone:nil];
d135b1a5
DE
74 m_bitmapMask = data.m_bitmapMask?new wxMask(*data.m_bitmapMask):NULL;
75}
76
a24aff65
DE
77wxBitmapRefData::~wxBitmapRefData()
78{
d135b1a5
DE
79 [m_cocoaNSBitmapImageRep release];
80 m_cocoaNSBitmapImageRep = NULL;
a24aff65 81
d135b1a5 82 delete m_bitmapMask;
a24aff65
DE
83 m_bitmapMask = NULL;
84}
85
d135b1a5
DE
86// ========================================================================
87// wxBitmap
88// ========================================================================
89IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject)
90
a24aff65
DE
91wxBitmap::wxBitmap()
92{
93 m_refData = NULL;
a24aff65
DE
94}
95
96wxBitmap::~wxBitmap()
97{
a24aff65
DE
98}
99
100wxBitmap::wxBitmap(const char bits[], int the_width, int the_height, int no_bits)
101{
102 m_refData = new wxBitmapRefData;
103
104 M_BITMAPDATA->m_width = the_width ;
105 M_BITMAPDATA->m_height = the_height ;
106 M_BITMAPDATA->m_depth = no_bits ;
107 M_BITMAPDATA->m_numColors = 0;
108
109 /* TODO: create the bitmap from data */
a24aff65
DE
110}
111
112wxBitmap::wxBitmap(int w, int h, int d)
113{
114 (void)Create(w, h, d);
a24aff65
DE
115}
116
117wxBitmap::wxBitmap(void *data, wxBitmapType type, int width, int height, int depth)
118{
119 (void) Create(data, type, width, height, depth);
a24aff65
DE
120}
121
122wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type)
123{
124 LoadFile(filename, type);
a24aff65
DE
125}
126
d135b1a5 127wxObjectRefData *wxBitmap::CreateRefData() const
a24aff65 128{
d135b1a5 129 return new wxBitmapRefData;
a24aff65
DE
130}
131
d135b1a5 132wxObjectRefData *wxBitmap::CloneRefData(const wxObjectRefData *data) const
a24aff65 133{
d135b1a5 134 return new wxBitmapRefData(*(wxBitmapRefData*)data);
a24aff65
DE
135}
136
d135b1a5 137WX_NSBitmapImageRep wxBitmap::GetNSBitmapImageRep()
a24aff65 138{
d135b1a5
DE
139 if(!M_BITMAPDATA)
140 return NULL;
141 return M_BITMAPDATA->m_cocoaNSBitmapImageRep;
a24aff65
DE
142}
143
ab13160e
DE
144void wxBitmap::SetNSBitmapImageRep(WX_NSBitmapImageRep bitmapImageRep)
145{
146 if(!M_BITMAPDATA)
147 return;
148 // NOTE: No checking is done to make sure width/height agree
149 [bitmapImageRep retain];
150 [M_BITMAPDATA->m_cocoaNSBitmapImageRep release];
151 M_BITMAPDATA->m_cocoaNSBitmapImageRep = bitmapImageRep;
152}
153
a24aff65
DE
154void wxBitmap::SetWidth(int w)
155{
156 if (!M_BITMAPDATA)
157 m_refData = new wxBitmapRefData;
158
159 M_BITMAPDATA->m_width = w;
160}
161
162void wxBitmap::SetHeight(int h)
163{
164 if (!M_BITMAPDATA)
165 m_refData = new wxBitmapRefData;
166
167 M_BITMAPDATA->m_height = h;
168}
169
170void wxBitmap::SetDepth(int d)
171{
172 if (!M_BITMAPDATA)
173 m_refData = new wxBitmapRefData;
174
175 M_BITMAPDATA->m_depth = d;
176}
177
178void wxBitmap::SetQuality(int q)
179{
180 if (!M_BITMAPDATA)
181 m_refData = new wxBitmapRefData;
182
183 M_BITMAPDATA->m_quality = q;
184}
185
186void wxBitmap::SetOk(bool isOk)
187{
188 if (!M_BITMAPDATA)
189 m_refData = new wxBitmapRefData;
190
191 M_BITMAPDATA->m_ok = isOk;
192}
193
194void wxBitmap::SetPalette(const wxPalette& palette)
195{
196 if (!M_BITMAPDATA)
197 m_refData = new wxBitmapRefData;
198
199 M_BITMAPDATA->m_bitmapPalette = palette ;
200}
201
202void wxBitmap::SetMask(wxMask *mask)
203{
204 if (!M_BITMAPDATA)
205 m_refData = new wxBitmapRefData;
206
207 M_BITMAPDATA->m_bitmapMask = mask ;
208}
209
d135b1a5 210bool wxBitmap::Ok() const
a24aff65 211{
d135b1a5 212 return m_refData && M_BITMAPDATA->m_ok;
a24aff65
DE
213}
214
d135b1a5 215wxPalette* wxBitmap::GetPalette() const
a24aff65 216{
d135b1a5
DE
217 if(!m_refData)
218 return NULL;
219 return &M_BITMAPDATA->m_bitmapPalette;
a24aff65
DE
220}
221
d135b1a5 222wxMask* wxBitmap::GetMask() const
a24aff65 223{
d135b1a5
DE
224 if(!m_refData)
225 return NULL;
226 return M_BITMAPDATA->m_bitmapMask;
a24aff65
DE
227}
228
d135b1a5 229int wxBitmap::GetDepth() const
a24aff65 230{
d135b1a5
DE
231 if(!m_refData)
232 return 0;
233 return M_BITMAPDATA->m_depth;
a24aff65
DE
234}
235
d135b1a5 236int wxBitmap::GetWidth() const
a24aff65 237{
d135b1a5
DE
238 if(!m_refData)
239 return 0;
240 return M_BITMAPDATA->m_width;
a24aff65
DE
241}
242
d135b1a5 243int wxBitmap::GetHeight() const
a24aff65 244{
d135b1a5
DE
245 if(!m_refData)
246 return 0;
247 return M_BITMAPDATA->m_height;
a24aff65
DE
248}
249
d135b1a5 250bool wxBitmap::Create(int w, int h, int d)
a24aff65 251{
d135b1a5
DE
252 UnRef();
253
254 m_refData = new wxBitmapRefData;
255
256 M_BITMAPDATA->m_width = w;
257 M_BITMAPDATA->m_height = h;
258 M_BITMAPDATA->m_depth = d;
259
260 /* TODO: create new bitmap */
ab13160e
DE
261 M_BITMAPDATA->m_cocoaNSBitmapImageRep = [[NSBitmapImageRep alloc]
262 initWithBitmapDataPlanes: NULL
263 pixelsWide: w
264 pixelsHigh: h
265 bitsPerSample: 8
266 samplesPerPixel: 3
267 hasAlpha: NO
268 isPlanar: NO
269 colorSpaceName: NSCalibratedRGBColorSpace
270 bytesPerRow: 0
271 bitsPerPixel: 0];
272
273 wxLogDebug("M_BITMAPDATA=%p NSBitmapImageRep bitmapData=%p", M_BITMAPDATA, [M_BITMAPDATA->m_cocoaNSBitmapImageRep bitmapData]);
274 M_BITMAPDATA->m_ok = true;
275 M_BITMAPDATA->m_numColors = 0;
276 M_BITMAPDATA->m_quality = 0;
277 M_BITMAPDATA->m_bitmapMask = NULL;
d135b1a5
DE
278
279 return M_BITMAPDATA->m_ok;
a24aff65
DE
280}
281
d135b1a5 282bool wxBitmap::LoadFile(const wxString& filename, wxBitmapType type)
a24aff65 283{
d135b1a5
DE
284 wxAutoNSAutoreleasePool pool;
285 UnRef();
a24aff65 286
d135b1a5 287 m_refData = new wxBitmapRefData;
a24aff65 288
d135b1a5
DE
289 NSBitmapImageRep *imageRep = [NSBitmapImageRep
290 imageRepWithContentsOfFile:wxNSStringWithWxString(filename)];
a24aff65 291
d135b1a5
DE
292 if(imageRep)
293 {
294 M_BITMAPDATA->m_width = [imageRep pixelsWide];
295 M_BITMAPDATA->m_height = [imageRep pixelsHigh];
296 M_BITMAPDATA->m_depth = 24; // FIXME
297 M_BITMAPDATA->m_ok = true;
298 M_BITMAPDATA->m_numColors = 0;
299 M_BITMAPDATA->m_quality = 0;
300 M_BITMAPDATA->m_cocoaNSBitmapImageRep = [imageRep retain];
301 M_BITMAPDATA->m_bitmapMask = NULL;
302 return true;
303 }
304 wxImage image;
305 if(!image.LoadFile(filename,type))
306 return false;
307 if(!image.Ok())
308 return false;
309 *this = wxBitmap(image);
310 return true;
a24aff65
DE
311}
312
d135b1a5 313bool wxBitmap::Create(void *data, wxBitmapType type, int width, int height, int depth)
a24aff65 314{
d135b1a5
DE
315 UnRef();
316
317 m_refData = new wxBitmapRefData;
318
319 return false;
a24aff65
DE
320}
321
d135b1a5 322bool wxBitmap::SaveFile(const wxString& filename, wxBitmapType type, const wxPalette *palette) const
a24aff65 323{
d135b1a5 324 return false;
a24aff65
DE
325}
326
d135b1a5 327bool wxBitmap::CopyFromIcon(const wxIcon& icno)
a24aff65 328{
d135b1a5 329 return false;
a24aff65
DE
330}
331
d135b1a5 332wxBitmap wxBitmap::GetSubBitmap(wxRect const&) const
a24aff65 333{
d135b1a5
DE
334 return wxNullBitmap;
335}
a24aff65 336
d135b1a5 337wxImage wxBitmap::ConvertToImage() const
a24aff65 338{
d135b1a5
DE
339 if(!M_BITMAPDATA->m_ok)
340 return wxImage(5,5)/*wxNullImage*/;
341 return wxImage(M_BITMAPDATA->m_width,M_BITMAPDATA->m_height);
a24aff65
DE
342}
343
d135b1a5 344bool wxBitmap::CreateFromXpm(const char **xpm)
a24aff65 345{
d135b1a5
DE
346#if wxUSE_IMAGE && wxUSE_XPM
347 UnRef();
348
349 wxCHECK_MSG( xpm, false, wxT("invalid XPM data") )
350
351 wxXPMDecoder decoder;
352 wxImage img = decoder.ReadData(xpm);
353 wxCHECK_MSG( img.Ok(), false, wxT("invalid XPM data") )
354
355 *this = wxBitmap(img);
356 return true;
357#else
a24aff65 358 return false;
d135b1a5 359#endif
a24aff65
DE
360}
361
d135b1a5 362bool wxBitmap::CreateFromImage(const wxImage& image, int depth)
a24aff65 363{
d135b1a5
DE
364 UnRef();
365
366 wxCHECK_MSG(image.Ok(), false, wxT("invalid image"));
367 wxCHECK_MSG(depth == -1 || depth == 1, false, wxT("invalid bitmap depth"));
368
369 m_refData = new wxBitmapRefData();
370
371 M_BITMAPDATA->m_width = image.GetWidth();
372 M_BITMAPDATA->m_height = image.GetHeight();
373 NSBitmapImageRep *bitmapImage = [[NSBitmapImageRep alloc]
374 initWithBitmapDataPlanes: NULL
375 pixelsWide: image.GetWidth()
376 pixelsHigh: image.GetHeight()
377 bitsPerSample: 8
378 samplesPerPixel: 3
379 hasAlpha: NO
380 isPlanar: NO
381 colorSpaceName: NSCalibratedRGBColorSpace
382 bytesPerRow: 0
383 bitsPerPixel: 0];
384
385 const int numBytes = image.GetWidth()*image.GetHeight()*3;
386 memcpy([bitmapImage bitmapData], image.GetData(), numBytes);
387 // TODO: Alpha and convert to desired depth
388 M_BITMAPDATA->m_depth = 24;
389 M_BITMAPDATA->m_ok = true;
390 M_BITMAPDATA->m_numColors = 0;
391 M_BITMAPDATA->m_quality = 0;
392 M_BITMAPDATA->m_cocoaNSBitmapImageRep = bitmapImage;
393 M_BITMAPDATA->m_bitmapMask = NULL;
394 return true;
a24aff65
DE
395}
396
d135b1a5
DE
397// ========================================================================
398// wxMask
399// ========================================================================
400
401IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject)
402
403wxMask::wxMask()
a24aff65 404{
d135b1a5
DE
405/* TODO
406 m_maskBitmap = 0;
407*/
a24aff65
DE
408}
409
d135b1a5
DE
410// Construct a mask from a bitmap and a colour indicating
411// the transparent area
412wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
a24aff65 413{
d135b1a5
DE
414/* TODO
415 m_maskBitmap = 0;
416*/
417 Create(bitmap, colour);
a24aff65
DE
418}
419
d135b1a5
DE
420// Construct a mask from a bitmap and a palette index indicating
421// the transparent area
422wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex)
a24aff65 423{
d135b1a5
DE
424/* TODO
425 m_maskBitmap = 0;
426*/
427
428 Create(bitmap, paletteIndex);
a24aff65
DE
429}
430
d135b1a5
DE
431// Construct a mask from a mono bitmap (copies the bitmap).
432wxMask::wxMask(const wxBitmap& bitmap)
a24aff65 433{
d135b1a5
DE
434/* TODO
435 m_maskBitmap = 0;
436*/
437
438 Create(bitmap);
a24aff65
DE
439}
440
d135b1a5 441wxMask::~wxMask()
a24aff65 442{
d135b1a5 443// TODO: delete mask bitmap
a24aff65
DE
444}
445
d135b1a5
DE
446// Create a mask from a mono bitmap (copies the bitmap).
447bool wxMask::Create(const wxBitmap& bitmap)
a24aff65 448{
d135b1a5
DE
449// TODO
450 return FALSE;
a24aff65
DE
451}
452
d135b1a5
DE
453// Create a mask from a bitmap and a palette index indicating
454// the transparent area
455bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex)
a24aff65 456{
d135b1a5
DE
457// TODO
458 return FALSE;
a24aff65
DE
459}
460
d135b1a5
DE
461// Create a mask from a bitmap and a colour indicating
462// the transparent area
463bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
a24aff65 464{
d135b1a5
DE
465// TODO
466 return FALSE;
a24aff65
DE
467}
468