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)
111 wxBitmap bitmap(filename, type);
112 CopyFromBitmap(bitmap);
116 void wxIcon::CopyFromBitmap(const wxBitmap& bitmap)
119 m_refData = new wxIconRefData;
120 M_ICONDATA->m_width = bitmap.GetWidth();
121 M_ICONDATA->m_height = bitmap.GetHeight();
122 wxAutoNSAutoreleasePool pool;
123 M_ICONDATA->m_cocoaNSImage = [bitmap.GetNSImage(true) retain];
124 M_ICONDATA->m_ok = bitmap.Ok();
125 M_ICONDATA->m_numColors = 0;
126 M_ICONDATA->m_quality = 0;
129 bool wxIcon::IsOk() const
131 return m_refData && M_ICONDATA->m_ok;
134 int wxIcon::GetWidth() const
138 return M_ICONDATA->m_width;
141 int wxIcon::GetHeight() const
145 return M_ICONDATA->m_height;
148 WX_NSImage wxIcon::GetNSImage() const
152 return M_ICONDATA->m_cocoaNSImage;