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