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"
17 #include "wx/bitmap.h"
20 #include "wx/cocoa/autorelease.h"
22 #import <AppKit/NSImage.h>
24 // ========================================================================
26 // ========================================================================
27 class wxIconRefData: public wxGDIRefData
32 wxIconRefData( const wxIconRefData& data );
33 virtual ~wxIconRefData();
42 WX_NSImage m_cocoaNSImage;
45 #define M_ICONDATA ((wxIconRefData *)m_refData)
47 wxIconRefData::wxIconRefData()
58 wxIconRefData::wxIconRefData( const wxIconRefData& data)
60 m_width = data.m_width;
61 m_height = data.m_height;
62 m_depth = data.m_depth;
64 m_numColors = data.m_numColors;
65 m_quality = data.m_quality;
66 m_cocoaNSImage = [data.m_cocoaNSImage copyWithZone:nil];
69 wxIconRefData::~wxIconRefData()
71 wxAutoNSAutoreleasePool pool;
72 [m_cocoaNSImage release];
73 m_cocoaNSImage = NULL;
77 // ========================================================================
79 // ========================================================================
80 IMPLEMENT_DYNAMIC_CLASS(wxIcon, wxGDIObject)
86 wxIcon::wxIcon(const char WXUNUSED(bits)[], int WXUNUSED(width), int WXUNUSED(height))
90 wxIcon::wxIcon(const wxString& icon_file, int flags,
91 int desiredWidth, int desiredHeight)
94 LoadFile(icon_file, (wxBitmapType)flags, desiredWidth, desiredHeight);
101 bool wxIcon::CreateFromXpm(const char **xpm)
103 wxBitmap bitmap(xpm);
104 CopyFromBitmap(bitmap);
108 bool wxIcon::LoadFile(const wxString& filename, wxBitmapType type,
109 int desiredWidth, int desiredHeight)
113 m_refData = new wxIconRefData;
114 M_ICONDATA->m_width = 5;
115 M_ICONDATA->m_height = 5;
116 M_ICONDATA->m_cocoaNSImage = [[NSImage alloc] initWithSize:NSMakeSize(5,5)];
117 M_ICONDATA->m_ok = true;
118 M_ICONDATA->m_numColors = 0;
119 M_ICONDATA->m_quality = 0;
124 void wxIcon::CopyFromBitmap(const wxBitmap& bitmap)
127 m_refData = new wxIconRefData;
128 M_ICONDATA->m_width = bitmap.GetWidth();
129 M_ICONDATA->m_height = bitmap.GetHeight();
130 wxAutoNSAutoreleasePool pool;
131 M_ICONDATA->m_cocoaNSImage = [bitmap.GetNSImage(true) retain];
132 M_ICONDATA->m_ok = bitmap.Ok();
133 M_ICONDATA->m_numColors = 0;
134 M_ICONDATA->m_quality = 0;
137 bool wxIcon::Ok() const
139 return m_refData && M_ICONDATA->m_ok;
142 int wxIcon::GetWidth() const
146 return M_ICONDATA->m_width;
149 int wxIcon::GetHeight() const
153 return M_ICONDATA->m_height;
156 WX_NSImage wxIcon::GetNSImage() const
160 return M_ICONDATA->m_cocoaNSImage;