Implement wxIcon::LoadFile by defering to wxBitmap implementation instead of creating...
[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     wxBitmap bitmap(filename, type);
112     CopyFromBitmap(bitmap);
113     return bitmap.Ok();
114 }
115
116 void wxIcon::CopyFromBitmap(const wxBitmap& bitmap)
117 {
118     UnRef();
119     m_refData = new wxIconRefData;
120     M_ICONDATA->m_width = bitmap.GetWidth();
121     M_ICONDATA->m_height = bitmap.GetHeight();
122     wxAutoNSAutoreleasePool pool;
123     M_ICONDATA->m_cocoaNSImage = [bitmap.GetNSImage(true) retain];
124     M_ICONDATA->m_ok = bitmap.Ok();
125     M_ICONDATA->m_numColors = 0;
126     M_ICONDATA->m_quality = 0;
127 }
128
129 bool wxIcon::IsOk() const
130 {
131     return m_refData && M_ICONDATA->m_ok;
132 }
133
134 int wxIcon::GetWidth() const
135 {
136     if(!m_refData)
137         return 0;
138     return M_ICONDATA->m_width;
139 }
140
141 int wxIcon::GetHeight() const
142 {
143     if(!m_refData)
144         return 0;
145     return M_ICONDATA->m_height;
146 }
147
148 WX_NSImage wxIcon::GetNSImage() const
149 {
150     if(!M_ICONDATA)
151         return nil;
152     return M_ICONDATA->m_cocoaNSImage;
153 }