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"
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 wxAutoNSAutoreleasePool pool;
70 [m_cocoaNSImage release];
71 m_cocoaNSImage = NULL;
75 // ========================================================================
77 // ========================================================================
78 IMPLEMENT_DYNAMIC_CLASS(wxIcon, wxGDIObject)
84 wxIcon::wxIcon(const char WXUNUSED(bits)[], int WXUNUSED(width), int WXUNUSED(height))
88 wxIcon::wxIcon(const wxString& icon_file, int flags,
89 int desiredWidth, int desiredHeight)
92 LoadFile(icon_file, (wxBitmapType)flags, desiredWidth, desiredHeight);
99 bool wxIcon::CreateFromXpm(const char **xpm)
101 wxBitmap bitmap(xpm);
102 CopyFromBitmap(bitmap);
106 bool wxIcon::LoadFile(const wxString& filename, wxBitmapType type,
107 int desiredWidth, int desiredHeight)
111 m_refData = new wxIconRefData;
112 M_ICONDATA->m_width = 5;
113 M_ICONDATA->m_height = 5;
114 M_ICONDATA->m_cocoaNSImage = [[NSImage alloc] initWithSize:NSMakeSize(5,5)];
115 M_ICONDATA->m_ok = true;
116 M_ICONDATA->m_numColors = 0;
117 M_ICONDATA->m_quality = 0;
122 void wxIcon::CopyFromBitmap(const wxBitmap& bitmap)
125 m_refData = new wxIconRefData;
126 M_ICONDATA->m_width = bitmap.GetWidth();
127 M_ICONDATA->m_height = bitmap.GetHeight();
128 wxAutoNSAutoreleasePool pool;
129 M_ICONDATA->m_cocoaNSImage = [bitmap.GetNSImage(true) retain];
130 M_ICONDATA->m_ok = bitmap.Ok();
131 M_ICONDATA->m_numColors = 0;
132 M_ICONDATA->m_quality = 0;
135 bool wxIcon::Ok() const
137 return m_refData && M_ICONDATA->m_ok;
140 int wxIcon::GetWidth() const
144 return M_ICONDATA->m_width;
147 int wxIcon::GetHeight() const
151 return M_ICONDATA->m_height;
154 WX_NSImage wxIcon::GetNSImage() const
158 return M_ICONDATA->m_cocoaNSImage;