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