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"
16 #include "wx/bitmap.h"
18 #include "wx/cocoa/autorelease.h"
20 #import <AppKit/NSImage.h>
22 // ========================================================================
24 // ========================================================================
25 class wxIconRefData: public wxGDIRefData
30 wxIconRefData( const wxIconRefData& data );
31 virtual ~wxIconRefData();
40 WX_NSImage m_cocoaNSImage;
43 #define M_ICONDATA ((wxIconRefData *)m_refData)
45 wxIconRefData::wxIconRefData()
56 wxIconRefData::wxIconRefData( const wxIconRefData& data)
58 m_width = data.m_width;
59 m_height = data.m_height;
60 m_depth = data.m_depth;
62 m_numColors = data.m_numColors;
63 m_quality = data.m_quality;
64 m_cocoaNSImage = [data.m_cocoaNSImage copyWithZone:nil];
67 wxIconRefData::~wxIconRefData()
69 [m_cocoaNSImage release];
70 m_cocoaNSImage = NULL;
74 // ========================================================================
76 // ========================================================================
77 IMPLEMENT_DYNAMIC_CLASS(wxIcon, wxGDIObject)
83 wxIcon::wxIcon(const char WXUNUSED(bits)[], int WXUNUSED(width), int WXUNUSED(height))
87 wxIcon::wxIcon(const wxString& icon_file, int flags,
88 int desiredWidth, int desiredHeight)
91 LoadFile(icon_file, (wxBitmapType)flags, desiredWidth, desiredHeight);
98 bool wxIcon::CreateFromXpm(const char **xpm)
100 wxBitmap bitmap(xpm);
101 CopyFromBitmap(bitmap);
105 bool wxIcon::LoadFile(const wxString& filename, wxBitmapType type,
106 int desiredWidth, int desiredHeight)
110 m_refData = new wxIconRefData;
111 M_ICONDATA->m_width = 5;
112 M_ICONDATA->m_height = 5;
113 M_ICONDATA->m_cocoaNSImage = [[NSImage alloc] initWithSize:NSMakeSize(5,5)];
114 M_ICONDATA->m_ok = true;
115 M_ICONDATA->m_numColors = 0;
116 M_ICONDATA->m_quality = 0;
121 void wxIcon::CopyFromBitmap(const wxBitmap& bitmap)
124 m_refData = new wxIconRefData;
125 M_ICONDATA->m_width = bitmap.GetWidth();
126 M_ICONDATA->m_height = bitmap.GetHeight();
127 wxAutoNSAutoreleasePool pool;
128 M_ICONDATA->m_cocoaNSImage = [bitmap.GetNSImage(true) retain];
129 M_ICONDATA->m_ok = bitmap.Ok();
130 M_ICONDATA->m_numColors = 0;
131 M_ICONDATA->m_quality = 0;
134 bool wxIcon::Ok() const
136 return m_refData && M_ICONDATA->m_ok;
139 int wxIcon::GetWidth() const
143 return M_ICONDATA->m_width;
146 int wxIcon::GetHeight() const
150 return M_ICONDATA->m_height;
153 WX_NSImage wxIcon::GetNSImage() const
157 return M_ICONDATA->m_cocoaNSImage;