]>
Commit | Line | Data |
---|---|---|
e9576ca5 | 1 | ///////////////////////////////////////////////////////////////////////////// |
155ecd4c | 2 | // Name: src/mac/carbon/icon.cpp |
e9576ca5 | 3 | // Purpose: wxIcon class |
a31a5f85 | 4 | // Author: Stefan Csomor |
e9576ca5 | 5 | // Modified by: |
a31a5f85 | 6 | // Created: 1998-01-01 |
e9576ca5 | 7 | // RCS-ID: $Id$ |
a31a5f85 | 8 | // Copyright: (c) Stefan Csomor |
155ecd4c | 9 | // Licence: wxWindows licence |
e9576ca5 SC |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
3d1a4878 SC |
12 | #include "wx/wxprec.h" |
13 | ||
e9576ca5 | 14 | #include "wx/icon.h" |
155ecd4c WS |
15 | |
16 | #ifndef WX_PRECOMP | |
17 | #include "wx/image.h" | |
18 | #endif | |
19 | ||
76a5e5d2 SC |
20 | #include "wx/mac/private.h" |
21 | ||
1046cbe1 | 22 | IMPLEMENT_DYNAMIC_CLASS(wxIcon, wxGDIObject) |
ea68b706 | 23 | |
5a7d70fe | 24 | #define M_ICONDATA ((wxIconRefData *)m_refData) |
76a5e5d2 | 25 | |
968c951f SC |
26 | class WXDLLEXPORT wxIconRefData : public wxGDIRefData |
27 | { | |
28 | public: | |
8f884a0d | 29 | wxIconRefData() { Init(); } |
968c951f SC |
30 | wxIconRefData( WXHICON iconref, int desiredWidth, int desiredHeight ); |
31 | virtual ~wxIconRefData() { Free(); } | |
8f884a0d VZ |
32 | |
33 | virtual bool IsOk() const { return m_iconRef != NULL; } | |
34 | ||
968c951f | 35 | virtual void Free(); |
8f884a0d | 36 | |
968c951f SC |
37 | void SetWidth( int width ) { m_width = width; } |
38 | void SetHeight( int height ) { m_height = height; } | |
8f884a0d | 39 | |
968c951f SC |
40 | int GetWidth() const { return m_width; } |
41 | int GetHeight() const { return m_height; } | |
8f884a0d | 42 | |
968c951f | 43 | WXHICON GetHICON() const { return (WXHICON) m_iconRef; } |
8f884a0d VZ |
44 | |
45 | private: | |
46 | void Init(); | |
47 | ||
968c951f SC |
48 | IconRef m_iconRef; |
49 | int m_width; | |
50 | int m_height; | |
51 | }; | |
52 | ||
53 | ||
54 | wxIconRefData::wxIconRefData( WXHICON icon, int desiredWidth, int desiredHeight ) | |
55 | { | |
56 | m_iconRef = MAC_WXHICON( icon ) ; | |
8f884a0d | 57 | |
968c951f SC |
58 | // Standard sizes |
59 | SetWidth( desiredWidth == -1 ? 32 : desiredWidth ) ; | |
60 | SetHeight( desiredHeight == -1 ? 32 : desiredHeight ) ; | |
61 | } | |
62 | ||
63 | void wxIconRefData::Init() | |
64 | { | |
65 | m_iconRef = NULL ; | |
8f884a0d VZ |
66 | m_width = |
67 | m_height = 0; | |
968c951f SC |
68 | } |
69 | ||
70 | void wxIconRefData::Free() | |
71 | { | |
72 | if ( m_iconRef ) | |
73 | { | |
74 | ReleaseIconRef( m_iconRef ) ; | |
75 | m_iconRef = NULL ; | |
76 | } | |
77 | } | |
78 | ||
79 | // | |
80 | // | |
81 | // | |
e9576ca5 | 82 | |
e9576ca5 SC |
83 | wxIcon::wxIcon() |
84 | { | |
85 | } | |
86 | ||
5a7d70fe | 87 | wxIcon::wxIcon( const char bits[], int width, int height ) |
e9576ca5 | 88 | { |
5a7d70fe | 89 | wxBitmap bmp( bits, width, height ) ; |
20b69855 | 90 | CopyFromBitmap( bmp ) ; |
e9576ca5 SC |
91 | } |
92 | ||
e45080c1 | 93 | wxIcon::wxIcon(const char* const* bits) |
2f1ae414 | 94 | { |
5a7d70fe | 95 | wxBitmap bmp( bits ) ; |
20b69855 | 96 | CopyFromBitmap( bmp ) ; |
2f1ae414 SC |
97 | } |
98 | ||
5a7d70fe DS |
99 | wxIcon::wxIcon( |
100 | const wxString& icon_file, int flags, | |
101 | int desiredWidth, int desiredHeight ) | |
e9576ca5 | 102 | { |
5a7d70fe | 103 | LoadFile( icon_file, (wxBitmapType) flags, desiredWidth, desiredHeight ); |
e9576ca5 SC |
104 | } |
105 | ||
52734360 VZ |
106 | wxIcon::wxIcon(WXHICON icon, const wxSize& size) |
107 | : wxGDIObject() | |
108 | { | |
109 | // as the icon owns that ref, we have to acquire it as well | |
110 | if (icon) | |
111 | AcquireIconRef( (IconRef) icon ) ; | |
112 | ||
968c951f | 113 | m_refData = new wxIconRefData( icon, size.x, size.y ) ; |
52734360 VZ |
114 | } |
115 | ||
e9576ca5 SC |
116 | wxIcon::~wxIcon() |
117 | { | |
118 | } | |
119 | ||
8f884a0d VZ |
120 | wxGDIRefData *wxIcon::CreateGDIRefData() const |
121 | { | |
122 | return new wxIconRefData; | |
123 | } | |
124 | ||
125 | wxGDIRefData *wxIcon::CloneGDIRefData(const wxGDIRefData *data) const | |
126 | { | |
127 | return new wxIconRefData(*wx_static_cast(const wxIconRefData *, data)); | |
128 | } | |
129 | ||
5a7d70fe | 130 | WXHICON wxIcon::GetHICON() const |
20b69855 SC |
131 | { |
132 | wxASSERT( Ok() ) ; | |
5a7d70fe | 133 | |
20b69855 SC |
134 | return (WXHICON) ((wxIconRefData*)m_refData)->GetHICON() ; |
135 | } | |
136 | ||
137 | int wxIcon::GetWidth() const | |
138 | { | |
ea68b706 SC |
139 | wxCHECK_MSG( Ok(), -1, wxT("invalid icon") ); |
140 | ||
141 | return M_ICONDATA->GetWidth(); | |
20b69855 SC |
142 | } |
143 | ||
144 | int wxIcon::GetHeight() const | |
145 | { | |
ea68b706 SC |
146 | wxCHECK_MSG( Ok(), -1, wxT("invalid icon") ); |
147 | ||
148 | return M_ICONDATA->GetHeight(); | |
20b69855 SC |
149 | } |
150 | ||
5a7d70fe DS |
151 | int wxIcon::GetDepth() const |
152 | { | |
153 | return 32; | |
c605b871 KO |
154 | } |
155 | ||
89954433 | 156 | void wxIcon::SetDepth( int WXUNUSED(depth) ) |
5a7d70fe | 157 | { |
c605b871 KO |
158 | } |
159 | ||
89954433 | 160 | void wxIcon::SetWidth( int WXUNUSED(width) ) |
5a7d70fe | 161 | { |
c605b871 KO |
162 | } |
163 | ||
89954433 | 164 | void wxIcon::SetHeight( int WXUNUSED(height) ) |
5a7d70fe | 165 | { |
c605b871 KO |
166 | } |
167 | ||
5a7d70fe DS |
168 | bool wxIcon::LoadFile( |
169 | const wxString& filename, wxBitmapType type, | |
170 | int desiredWidth, int desiredHeight ) | |
e9576ca5 | 171 | { |
e40298d5 | 172 | UnRef(); |
5a7d70fe | 173 | |
20b69855 | 174 | if ( type == wxBITMAP_TYPE_ICON_RESOURCE ) |
1996c23f | 175 | { |
20b69855 | 176 | OSType theId = 0 ; |
5a7d70fe | 177 | |
20b69855 SC |
178 | if ( filename == wxT("wxICON_INFORMATION") ) |
179 | { | |
180 | theId = kAlertNoteIcon ; | |
181 | } | |
182 | else if ( filename == wxT("wxICON_QUESTION") ) | |
183 | { | |
184 | theId = kAlertCautionIcon ; | |
185 | } | |
186 | else if ( filename == wxT("wxICON_WARNING") ) | |
187 | { | |
188 | theId = kAlertCautionIcon ; | |
189 | } | |
190 | else if ( filename == wxT("wxICON_ERROR") ) | |
191 | { | |
192 | theId = kAlertStopIcon ; | |
193 | } | |
52734360 VZ |
194 | else if ( filename == wxT("wxICON_FOLDER") ) |
195 | { | |
196 | theId = kGenericFolderIcon ; | |
197 | } | |
198 | else if ( filename == wxT("wxICON_FOLDER_OPEN") ) | |
199 | { | |
200 | theId = kOpenFolderIcon ; | |
201 | } | |
202 | else if ( filename == wxT("wxICON_NORMAL_FILE") ) | |
203 | { | |
204 | theId = kGenericDocumentIcon ; | |
205 | } | |
20b69855 | 206 | else |
5a7d70fe | 207 | { |
8f884a0d VZ |
208 | IconRef iconRef = NULL ; |
209 | ||
210 | // first look in the resource fork | |
211 | if ( iconRef == NULL ) | |
212 | { | |
213 | Str255 theName ; | |
214 | ||
215 | wxMacStringToPascal( filename , theName ) ; | |
216 | Handle resHandle = GetNamedResource( 'icns' , theName ) ; | |
217 | if ( resHandle != 0L ) | |
218 | { | |
219 | IconFamilyHandle iconFamily = (IconFamilyHandle) resHandle ; | |
6239ee05 SC |
220 | HLock((Handle) iconFamily); |
221 | OSStatus err = GetIconRefFromIconFamilyPtr( *iconFamily, GetHandleSize((Handle) iconFamily), &iconRef ); | |
222 | HUnlock((Handle) iconFamily); | |
947f3b35 VZ |
223 | if ( err != noErr ) |
224 | { | |
225 | wxFAIL_MSG("Error when constructing icon ref"); | |
226 | } | |
227 | ||
6239ee05 | 228 | ReleaseResource( resHandle ) ; |
8f884a0d VZ |
229 | } |
230 | } | |
6239ee05 | 231 | if ( iconRef == NULL ) |
20b69855 | 232 | { |
6239ee05 | 233 | // TODO add other attempts to load it from files etc here |
20b69855 | 234 | } |
8f884a0d VZ |
235 | if ( iconRef ) |
236 | { | |
237 | m_refData = new wxIconRefData( (WXHICON) iconRef, desiredWidth, desiredHeight ) ; | |
238 | return true ; | |
239 | } | |
20b69855 | 240 | } |
5a7d70fe | 241 | |
20b69855 SC |
242 | if ( theId != 0 ) |
243 | { | |
244 | IconRef iconRef = NULL ; | |
5a7d70fe | 245 | verify_noerr( GetIconRef( kOnSystemDisk, kSystemIconsCreator, theId, &iconRef ) ) ; |
20b69855 SC |
246 | if ( iconRef ) |
247 | { | |
968c951f | 248 | m_refData = new wxIconRefData( (WXHICON) iconRef, desiredWidth, desiredHeight ) ; |
5a7d70fe DS |
249 | |
250 | return true ; | |
20b69855 SC |
251 | } |
252 | } | |
5a7d70fe DS |
253 | |
254 | return false ; | |
1996c23f | 255 | } |
e40298d5 | 256 | else |
1996c23f | 257 | { |
5a7d70fe | 258 | wxBitmapHandler *handler = wxBitmap::FindHandler( type ); |
20b69855 SC |
259 | |
260 | if ( handler ) | |
1996c23f | 261 | { |
20b69855 | 262 | wxBitmap bmp ; |
5a7d70fe | 263 | if ( handler->LoadFile( &bmp , filename, type, desiredWidth, desiredHeight )) |
20b69855 SC |
264 | { |
265 | CopyFromBitmap( bmp ) ; | |
5a7d70fe | 266 | |
20b69855 SC |
267 | return true ; |
268 | } | |
5a7d70fe | 269 | |
20b69855 SC |
270 | return false ; |
271 | } | |
272 | else | |
273 | { | |
da76c2ce | 274 | #if wxUSE_IMAGE |
5a7d70fe DS |
275 | wxImage loadimage( filename, type ); |
276 | if (loadimage.Ok()) | |
20b69855 SC |
277 | { |
278 | if ( desiredWidth == -1 ) | |
279 | desiredWidth = loadimage.GetWidth() ; | |
280 | if ( desiredHeight == -1 ) | |
281 | desiredHeight = loadimage.GetHeight() ; | |
282 | if ( desiredWidth != loadimage.GetWidth() || desiredHeight != loadimage.GetHeight() ) | |
283 | loadimage.Rescale( desiredWidth , desiredHeight ) ; | |
155ecd4c | 284 | |
20b69855 SC |
285 | wxBitmap bmp( loadimage ); |
286 | CopyFromBitmap( bmp ) ; | |
5a7d70fe | 287 | |
20b69855 SC |
288 | return true; |
289 | } | |
da76c2ce | 290 | #endif |
1996c23f SC |
291 | } |
292 | } | |
20b69855 | 293 | return true ; |
e9576ca5 SC |
294 | } |
295 | ||
5a7d70fe | 296 | void wxIcon::CopyFromBitmap( const wxBitmap& bmp ) |
d062e17f | 297 | { |
20b69855 | 298 | UnRef() ; |
5a7d70fe | 299 | |
796a6ef0 | 300 | // as the bitmap owns that ref, we have to acquire it as well |
968c951f SC |
301 | IconRef iconRef = bmp.CreateIconRef() ; |
302 | m_refData = new wxIconRefData( (WXHICON) iconRef, bmp.GetWidth(), bmp.GetHeight() ) ; | |
d062e17f GD |
303 | } |
304 | ||
519cb848 SC |
305 | IMPLEMENT_DYNAMIC_CLASS(wxICONResourceHandler, wxBitmapHandler) |
306 | ||
5a7d70fe | 307 | bool wxICONResourceHandler::LoadFile( |
89954433 | 308 | wxBitmap *bitmap, const wxString& name, long WXUNUSED(flags), |
5a7d70fe | 309 | int desiredWidth, int desiredHeight ) |
519cb848 | 310 | { |
20b69855 SC |
311 | wxIcon icon ; |
312 | icon.LoadFile( name , wxBITMAP_TYPE_ICON_RESOURCE , desiredWidth , desiredHeight ) ; | |
313 | bitmap->CopyFromIcon( icon ) ; | |
5a7d70fe | 314 | |
20b69855 | 315 | return bitmap->Ok() ; |
03e11df5 | 316 | } |
5a7d70fe | 317 |