1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/icon.mm
3 // Purpose: wxIcon class
4 // Author: David Elliott
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWindows 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();
35 virtual bool IsOk() const { return m_ok; }
44 WX_NSImage m_cocoaNSImage;
47 #define M_ICONDATA ((wxIconRefData *)m_refData)
49 wxIconRefData::wxIconRefData()
60 wxIconRefData::wxIconRefData( const wxIconRefData& data)
62 m_width = data.m_width;
63 m_height = data.m_height;
64 m_depth = data.m_depth;
66 m_numColors = data.m_numColors;
67 m_quality = data.m_quality;
68 m_cocoaNSImage = [data.m_cocoaNSImage copyWithZone:nil];
71 wxIconRefData::~wxIconRefData()
73 wxAutoNSAutoreleasePool pool;
74 [m_cocoaNSImage release];
75 m_cocoaNSImage = NULL;
79 // ========================================================================
81 // ========================================================================
82 IMPLEMENT_DYNAMIC_CLASS(wxIcon, wxGDIObject)
88 wxIcon::wxIcon(const char WXUNUSED(bits)[], int WXUNUSED(width), int WXUNUSED(height))
92 wxIcon::wxIcon(const wxString& icon_file, int flags,
93 int desiredWidth, int desiredHeight)
96 LoadFile(icon_file, (wxBitmapType)flags, desiredWidth, desiredHeight);
103 wxGDIRefData *wxIcon::CreateGDIRefData() const
105 return new wxIconRefData;
108 wxGDIRefData *wxIcon::CloneGDIRefData(const wxGDIRefData *data) const
110 return new wxIconRefData(*static_cast<const wxIconRefData *>(data));
113 bool wxIcon::CreateFromXpm(const char* const* xpm)
115 wxBitmap bitmap(xpm);
116 CopyFromBitmap(bitmap);
120 bool wxIcon::LoadFile(const wxString& filename, wxBitmapType type,
121 int desiredWidth, int desiredHeight)
123 wxBitmap bitmap(filename, type);
124 CopyFromBitmap(bitmap);
128 void wxIcon::CopyFromBitmap(const wxBitmap& bitmap)
131 m_refData = new wxIconRefData;
132 M_ICONDATA->m_width = bitmap.GetWidth();
133 M_ICONDATA->m_height = bitmap.GetHeight();
134 wxAutoNSAutoreleasePool pool;
135 M_ICONDATA->m_cocoaNSImage = [bitmap.GetNSImage(true) retain];
136 M_ICONDATA->m_ok = bitmap.Ok();
137 M_ICONDATA->m_numColors = 0;
138 M_ICONDATA->m_quality = 0;
141 int wxIcon::GetWidth() const
145 return M_ICONDATA->m_width;
148 int wxIcon::GetHeight() const
152 return M_ICONDATA->m_height;
155 WX_NSImage wxIcon::GetNSImage() const
159 return M_ICONDATA->m_cocoaNSImage;