wxIcon::CopyFromBitmap:
[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     [m_cocoaNSImage release];
70     m_cocoaNSImage = NULL;
71 }
72
73
74 // ========================================================================
75 // wxIcon
76 // ========================================================================
77 IMPLEMENT_DYNAMIC_CLASS(wxIcon, wxGDIObject)
78
79 wxIcon::wxIcon()
80 {
81 }
82
83 wxIcon::wxIcon(const char WXUNUSED(bits)[], int WXUNUSED(width), int WXUNUSED(height))
84 {
85 }
86
87 wxIcon::wxIcon(const wxString& icon_file, int flags,
88     int desiredWidth, int desiredHeight)
89
90 {
91     LoadFile(icon_file, (wxBitmapType)flags, desiredWidth, desiredHeight);
92 }
93
94 wxIcon::~wxIcon()
95 {
96 }
97
98 bool wxIcon::CreateFromXpm(const char **xpm)
99 {
100     wxBitmap bitmap(xpm);
101     CopyFromBitmap(bitmap);
102     return Ok();
103 }
104
105 bool wxIcon::LoadFile(const wxString& filename, wxBitmapType type,
106     int desiredWidth, int desiredHeight)
107 {
108     UnRef();
109
110     m_refData = new wxIconRefData;
111     M_ICONDATA->m_width = 5;
112     M_ICONDATA->m_height = 5;
113     M_ICONDATA->m_cocoaNSImage = [[NSImage alloc] initWithSize:NSMakeSize(5,5)];
114     M_ICONDATA->m_ok = true;
115     M_ICONDATA->m_numColors = 0;
116     M_ICONDATA->m_quality = 0;
117
118     return FALSE;
119 }
120
121 void wxIcon::CopyFromBitmap(const wxBitmap& bitmap)
122 {
123     UnRef();
124     m_refData = new wxIconRefData;
125     M_ICONDATA->m_width = bitmap.GetWidth();
126     M_ICONDATA->m_height = bitmap.GetHeight();
127     wxAutoNSAutoreleasePool pool;
128     M_ICONDATA->m_cocoaNSImage = [bitmap.GetNSImage(true) retain];
129     M_ICONDATA->m_ok = bitmap.Ok();
130     M_ICONDATA->m_numColors = 0;
131     M_ICONDATA->m_quality = 0;
132 }
133
134 bool wxIcon::Ok() const
135 {
136     return m_refData && M_ICONDATA->m_ok;
137 }
138
139 int wxIcon::GetWidth() const
140 {
141     if(!m_refData)
142         return 0;
143     return M_ICONDATA->m_width;
144 }
145
146 int wxIcon::GetHeight() const
147 {
148     if(!m_refData)
149         return 0;
150     return M_ICONDATA->m_height;
151 }
152
153 WX_NSImage wxIcon::GetNSImage() const
154 {
155     if(!M_ICONDATA)
156         return nil;
157     return M_ICONDATA->m_cocoaNSImage;
158 }
159