add IsOk() to all classes having Ok() method (patch 1570985)
[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:     wxWidgets licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #include "wx/icon.h"
15
16 #ifndef WX_PRECOMP
17     #include "wx/bitmap.h"
18 #endif //WX_PRECOMP
19
20 #include "wx/cocoa/autorelease.h"
21
22 #import <AppKit/NSImage.h>
23
24 // ========================================================================
25 // wxIconRefData
26 // ========================================================================
27 class wxIconRefData: public wxGDIRefData
28 {
29     friend class wxIcon;
30 public:
31     wxIconRefData();
32     wxIconRefData( const wxIconRefData& data );
33     virtual ~wxIconRefData();
34
35 protected:
36     int                 m_width;
37     int                 m_height;
38     int                 m_depth;
39     bool                m_ok;
40     int                 m_numColors;
41     int                 m_quality;
42     WX_NSImage          m_cocoaNSImage;
43 };
44
45 #define M_ICONDATA ((wxIconRefData *)m_refData)
46
47 wxIconRefData::wxIconRefData()
48 {
49     m_ok = false;
50     m_width = 0;
51     m_height = 0;
52     m_depth = 0;
53     m_quality = 0;
54     m_numColors = 0;
55     m_cocoaNSImage = nil;
56 }
57
58 wxIconRefData::wxIconRefData( const wxIconRefData& data)
59 {
60     m_width = data.m_width;
61     m_height = data.m_height;
62     m_depth = data.m_depth;
63     m_ok = data.m_ok;
64     m_numColors = data.m_numColors;
65     m_quality = data.m_quality;
66     m_cocoaNSImage = [data.m_cocoaNSImage copyWithZone:nil];
67 }
68
69 wxIconRefData::~wxIconRefData()
70 {
71     wxAutoNSAutoreleasePool pool;
72     [m_cocoaNSImage release];
73     m_cocoaNSImage = NULL;
74 }
75
76
77 // ========================================================================
78 // wxIcon
79 // ========================================================================
80 IMPLEMENT_DYNAMIC_CLASS(wxIcon, wxGDIObject)
81
82 wxIcon::wxIcon()
83 {
84 }
85
86 wxIcon::wxIcon(const char WXUNUSED(bits)[], int WXUNUSED(width), int WXUNUSED(height))
87 {
88 }
89
90 wxIcon::wxIcon(const wxString& icon_file, int flags,
91     int desiredWidth, int desiredHeight)
92
93 {
94     LoadFile(icon_file, (wxBitmapType)flags, desiredWidth, desiredHeight);
95 }
96
97 wxIcon::~wxIcon()
98 {
99 }
100
101 bool wxIcon::CreateFromXpm(const char **xpm)
102 {
103     wxBitmap bitmap(xpm);
104     CopyFromBitmap(bitmap);
105     return Ok();
106 }
107
108 bool wxIcon::LoadFile(const wxString& filename, wxBitmapType type,
109     int desiredWidth, int desiredHeight)
110 {
111     UnRef();
112
113     m_refData = new wxIconRefData;
114     M_ICONDATA->m_width = 5;
115     M_ICONDATA->m_height = 5;
116     M_ICONDATA->m_cocoaNSImage = [[NSImage alloc] initWithSize:NSMakeSize(5,5)];
117     M_ICONDATA->m_ok = true;
118     M_ICONDATA->m_numColors = 0;
119     M_ICONDATA->m_quality = 0;
120
121     return false;
122 }
123
124 void wxIcon::CopyFromBitmap(const wxBitmap& bitmap)
125 {
126     UnRef();
127     m_refData = new wxIconRefData;
128     M_ICONDATA->m_width = bitmap.GetWidth();
129     M_ICONDATA->m_height = bitmap.GetHeight();
130     wxAutoNSAutoreleasePool pool;
131     M_ICONDATA->m_cocoaNSImage = [bitmap.GetNSImage(true) retain];
132     M_ICONDATA->m_ok = bitmap.Ok();
133     M_ICONDATA->m_numColors = 0;
134     M_ICONDATA->m_quality = 0;
135 }
136
137 bool wxIcon::IsOk() const
138 {
139     return m_refData && M_ICONDATA->m_ok;
140 }
141
142 int wxIcon::GetWidth() const
143 {
144     if(!m_refData)
145         return 0;
146     return M_ICONDATA->m_width;
147 }
148
149 int wxIcon::GetHeight() const
150 {
151     if(!m_refData)
152         return 0;
153     return M_ICONDATA->m_height;
154 }
155
156 WX_NSImage wxIcon::GetNSImage() const
157 {
158     if(!M_ICONDATA)
159         return nil;
160     return M_ICONDATA->m_cocoaNSImage;
161 }