1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/icon.mm
3 // Purpose: wxIcon class
4 // Author: David Elliott
7 // Copyright: (c) 2003 David Elliott
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #include "wx/wxprec.h"
16 #include "wx/bitmap.h"
19 #include "wx/cocoa/autorelease.h"
21 #import <AppKit/NSImage.h>
23 // ========================================================================
25 // ========================================================================
26 class wxIconRefData: public wxGDIRefData
31 wxIconRefData( const wxIconRefData& data );
32 virtual ~wxIconRefData();
34 virtual bool IsOk() const { return m_ok; }
43 WX_NSImage m_cocoaNSImage;
46 #define M_ICONDATA ((wxIconRefData *)m_refData)
48 wxIconRefData::wxIconRefData()
59 wxIconRefData::wxIconRefData( const wxIconRefData& data)
61 m_width = data.m_width;
62 m_height = data.m_height;
63 m_depth = data.m_depth;
65 m_numColors = data.m_numColors;
66 m_quality = data.m_quality;
67 m_cocoaNSImage = [data.m_cocoaNSImage copyWithZone:nil];
70 wxIconRefData::~wxIconRefData()
72 wxAutoNSAutoreleasePool pool;
73 [m_cocoaNSImage release];
74 m_cocoaNSImage = NULL;
78 // ========================================================================
80 // ========================================================================
81 IMPLEMENT_DYNAMIC_CLASS(wxIcon, wxGDIObject)
87 wxIcon::wxIcon(const char WXUNUSED(bits)[], int WXUNUSED(width), int WXUNUSED(height))
91 wxIcon::wxIcon(const wxString& icon_file, int flags,
92 int desiredWidth, int desiredHeight)
95 LoadFile(icon_file, (wxBitmapType)flags, desiredWidth, desiredHeight);
102 wxGDIRefData *wxIcon::CreateGDIRefData() const
104 return new wxIconRefData;
107 wxGDIRefData *wxIcon::CloneGDIRefData(const wxGDIRefData *data) const
109 return new wxIconRefData(*static_cast<const wxIconRefData *>(data));
112 bool wxIcon::CreateFromXpm(const char* const* xpm)
114 wxBitmap bitmap(xpm);
115 CopyFromBitmap(bitmap);
119 bool wxIcon::LoadFile(const wxString& filename, wxBitmapType type,
120 int desiredWidth, int desiredHeight)
122 wxBitmap bitmap(filename, type);
123 CopyFromBitmap(bitmap);
124 return bitmap.IsOk();
127 void wxIcon::CopyFromBitmap(const wxBitmap& bitmap)
130 m_refData = new wxIconRefData;
131 M_ICONDATA->m_width = bitmap.GetWidth();
132 M_ICONDATA->m_height = bitmap.GetHeight();
133 wxAutoNSAutoreleasePool pool;
134 M_ICONDATA->m_cocoaNSImage = [bitmap.GetNSImage(true) retain];
135 M_ICONDATA->m_ok = bitmap.IsOk();
136 M_ICONDATA->m_numColors = 0;
137 M_ICONDATA->m_quality = 0;
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;