Use wxAutoNSAutoreleasePool when destroying wxIconRefData
[wxWidgets.git] / src / cocoa / icon.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        src/cocoa/icon.mm
3 // Purpose:     wxIcon class
4 // Author:      David Elliott
5 // Modified by:
6 // Created:     2003/08/11
7 // RCS-ID:      $Id$
8 // Copyright:   (c) 2003 David Elliott
9 // Licence:     wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13 #ifndef WX_PRECOMP
14     #include "wx/icon.h"
15 #endif //WX_PRECOMP
16 #include "wx/bitmap.h"
17
18 #include "wx/cocoa/autorelease.h"
19
20 #import <AppKit/NSImage.h>
21
22 // ========================================================================
23 // wxIconRefData
24 // ========================================================================
25 class wxIconRefData: public wxGDIRefData
26 {
27     friend class wxIcon;
28 public:
29     wxIconRefData();
30     wxIconRefData( const wxIconRefData& data );
31     virtual ~wxIconRefData();
32
33 protected:
34     int                 m_width;
35     int                 m_height;
36     int                 m_depth;
37     bool                m_ok;
38     int                 m_numColors;
39     int                 m_quality;
40     WX_NSImage          m_cocoaNSImage;
41 };
42
43 #define M_ICONDATA ((wxIconRefData *)m_refData)
44
45 wxIconRefData::wxIconRefData()
46 {
47     m_ok = FALSE;
48     m_width = 0;
49     m_height = 0;
50     m_depth = 0;
51     m_quality = 0;
52     m_numColors = 0;
53     m_cocoaNSImage = nil;
54 }
55
56 wxIconRefData::wxIconRefData( const wxIconRefData& data)
57 {
58     m_width = data.m_width;
59     m_height = data.m_height;
60     m_depth = data.m_depth;
61     m_ok = data.m_ok;
62     m_numColors = data.m_numColors;
63     m_quality = data.m_quality;
64     m_cocoaNSImage = [data.m_cocoaNSImage copyWithZone:nil];
65 }
66
67 wxIconRefData::~wxIconRefData()
68 {
69     wxAutoNSAutoreleasePool pool;
70     [m_cocoaNSImage release];
71     m_cocoaNSImage = NULL;
72 }
73
74
75 // ========================================================================
76 // wxIcon
77 // ========================================================================
78 IMPLEMENT_DYNAMIC_CLASS(wxIcon, wxGDIObject)
79
80 wxIcon::wxIcon()
81 {
82 }
83
84 wxIcon::wxIcon(const char WXUNUSED(bits)[], int WXUNUSED(width), int WXUNUSED(height))
85 {
86 }
87
88 wxIcon::wxIcon(const wxString& icon_file, int flags,
89     int desiredWidth, int desiredHeight)
90
91 {
92     LoadFile(icon_file, (wxBitmapType)flags, desiredWidth, desiredHeight);
93 }
94
95 wxIcon::~wxIcon()
96 {
97 }
98
99 bool wxIcon::CreateFromXpm(const char **xpm)
100 {
101     wxBitmap bitmap(xpm);
102     CopyFromBitmap(bitmap);
103     return Ok();
104 }
105
106 bool wxIcon::LoadFile(const wxString& filename, wxBitmapType type,
107     int desiredWidth, int desiredHeight)
108 {
109     UnRef();
110
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;
118
119     return FALSE;
120 }
121
122 void wxIcon::CopyFromBitmap(const wxBitmap& bitmap)
123 {
124     UnRef();
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;
133 }
134
135 bool wxIcon::Ok() const
136 {
137     return m_refData && M_ICONDATA->m_ok;
138 }
139
140 int wxIcon::GetWidth() const
141 {
142     if(!m_refData)
143         return 0;
144     return M_ICONDATA->m_width;
145 }
146
147 int wxIcon::GetHeight() const
148 {
149     if(!m_refData)
150         return 0;
151     return M_ICONDATA->m_height;
152 }
153
154 WX_NSImage wxIcon::GetNSImage() const
155 {
156     if(!M_ICONDATA)
157         return nil;
158     return M_ICONDATA->m_cocoaNSImage;
159 }
160