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