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