1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/icon.mm
3 // Purpose: wxIcon class
4 // Author: David Elliott
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWidgets licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
19 #include "wx/bitmap.h"
21 #include "wx/cocoa/autorelease.h"
23 #import <AppKit/NSImage.h>
25 // ========================================================================
27 // ========================================================================
28 class wxIconRefData: public wxGDIRefData
33 wxIconRefData( const wxIconRefData& data );
34 virtual ~wxIconRefData();
43 WX_NSImage m_cocoaNSImage;
46 #define M_ICONDATA ((wxIconRefData *)m_refData)
48 wxIconRefData::wxIconRefData()
59 wxIconRefData::wxIconRefData( const wxIconRefData& data)
61 m_width = data.m_width;
62 m_height = data.m_height;
63 m_depth = data.m_depth;
65 m_numColors = data.m_numColors;
66 m_quality = data.m_quality;
67 m_cocoaNSImage = [data.m_cocoaNSImage copyWithZone:nil];
70 wxIconRefData::~wxIconRefData()
72 wxAutoNSAutoreleasePool pool;
73 [m_cocoaNSImage release];
74 m_cocoaNSImage = NULL;
78 // ========================================================================
80 // ========================================================================
81 IMPLEMENT_DYNAMIC_CLASS(wxIcon, wxGDIObject)
87 wxIcon::wxIcon(const char WXUNUSED(bits)[], int WXUNUSED(width), int WXUNUSED(height))
91 wxIcon::wxIcon(const wxString& icon_file, int flags,
92 int desiredWidth, int desiredHeight)
95 LoadFile(icon_file, (wxBitmapType)flags, desiredWidth, desiredHeight);
102 bool wxIcon::CreateFromXpm(const char **xpm)
104 wxBitmap bitmap(xpm);
105 CopyFromBitmap(bitmap);
109 bool wxIcon::LoadFile(const wxString& filename, wxBitmapType type,
110 int desiredWidth, int desiredHeight)
114 m_refData = new wxIconRefData;
115 M_ICONDATA->m_width = 5;
116 M_ICONDATA->m_height = 5;
117 M_ICONDATA->m_cocoaNSImage = [[NSImage alloc] initWithSize:NSMakeSize(5,5)];
118 M_ICONDATA->m_ok = true;
119 M_ICONDATA->m_numColors = 0;
120 M_ICONDATA->m_quality = 0;
125 void wxIcon::CopyFromBitmap(const wxBitmap& bitmap)
128 m_refData = new wxIconRefData;
129 M_ICONDATA->m_width = bitmap.GetWidth();
130 M_ICONDATA->m_height = bitmap.GetHeight();
131 wxAutoNSAutoreleasePool pool;
132 M_ICONDATA->m_cocoaNSImage = [bitmap.GetNSImage(true) retain];
133 M_ICONDATA->m_ok = bitmap.Ok();
134 M_ICONDATA->m_numColors = 0;
135 M_ICONDATA->m_quality = 0;
138 bool wxIcon::Ok() const
140 return m_refData && M_ICONDATA->m_ok;
143 int wxIcon::GetWidth() const
147 return M_ICONDATA->m_width;
150 int wxIcon::GetHeight() const
154 return M_ICONDATA->m_height;
157 WX_NSImage wxIcon::GetNSImage() const
161 return M_ICONDATA->m_cocoaNSImage;