]> git.saurik.com Git - wxWidgets.git/blob - src/osx/carbon/icon.cpp
Use wxDELETE() and wxDELETEA() when possible.
[wxWidgets.git] / src / osx / carbon / icon.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/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 #if wxOSX_USE_COCOA_OR_CARBON
15
16 #include "wx/icon.h"
17
18 #ifndef WX_PRECOMP
19 #include "wx/image.h"
20 #endif
21
22 #include "wx/osx/private.h"
23
24 IMPLEMENT_DYNAMIC_CLASS(wxIcon, wxGDIObject)
25
26 #define M_ICONDATA ((wxIconRefData *)m_refData)
27
28 class WXDLLEXPORT wxIconRefData : public wxGDIRefData
29 {
30 public:
31 wxIconRefData() { Init(); }
32 wxIconRefData( WXHICON iconref, int desiredWidth, int desiredHeight );
33 virtual ~wxIconRefData() { Free(); }
34
35 virtual bool IsOk() const { return m_iconRef != NULL; }
36
37 virtual void Free();
38
39 void SetWidth( int width ) { m_width = width; }
40 void SetHeight( int height ) { m_height = height; }
41
42 int GetWidth() const { return m_width; }
43 int GetHeight() const { return m_height; }
44
45 WXHICON GetHICON() const { return (WXHICON) m_iconRef; }
46
47 private:
48 void Init();
49
50 IconRef m_iconRef;
51 int m_width;
52 int m_height;
53 };
54
55
56 wxIconRefData::wxIconRefData( WXHICON icon, int desiredWidth, int desiredHeight )
57 {
58 m_iconRef = (IconRef)( icon ) ;
59
60 // Standard sizes
61 SetWidth( desiredWidth == -1 ? 32 : desiredWidth ) ;
62 SetHeight( desiredHeight == -1 ? 32 : desiredHeight ) ;
63 }
64
65 void wxIconRefData::Init()
66 {
67 m_iconRef = NULL ;
68 m_width =
69 m_height = 0;
70 }
71
72 void wxIconRefData::Free()
73 {
74 if ( m_iconRef )
75 {
76 ReleaseIconRef( m_iconRef ) ;
77 m_iconRef = NULL ;
78 }
79 }
80
81 //
82 //
83 //
84
85 wxIcon::wxIcon()
86 {
87 }
88
89 wxIcon::wxIcon( const char bits[], int width, int height )
90 {
91 wxBitmap bmp( bits, width, height ) ;
92 CopyFromBitmap( bmp ) ;
93 }
94
95 wxIcon::wxIcon(const char* const* bits)
96 {
97 wxBitmap bmp( bits ) ;
98 CopyFromBitmap( bmp ) ;
99 }
100
101 wxIcon::wxIcon(
102 const wxString& icon_file, wxBitmapType flags,
103 int desiredWidth, int desiredHeight )
104 {
105 LoadFile( icon_file, flags, desiredWidth, desiredHeight );
106 }
107
108 wxIcon::wxIcon(WXHICON icon, const wxSize& size)
109 : wxGDIObject()
110 {
111 // as the icon owns that ref, we have to acquire it as well
112 if (icon)
113 AcquireIconRef( (IconRef) icon ) ;
114
115 m_refData = new wxIconRefData( icon, size.x, size.y ) ;
116 }
117
118 wxIcon::~wxIcon()
119 {
120 }
121
122 wxGDIRefData *wxIcon::CreateGDIRefData() const
123 {
124 return new wxIconRefData;
125 }
126
127 wxGDIRefData *wxIcon::CloneGDIRefData(const wxGDIRefData *data) const
128 {
129 return new wxIconRefData(*static_cast<const wxIconRefData *>(data));
130 }
131
132 WXHICON wxIcon::GetHICON() const
133 {
134 wxASSERT( Ok() ) ;
135
136 return (WXHICON) ((wxIconRefData*)m_refData)->GetHICON() ;
137 }
138
139 int wxIcon::GetWidth() const
140 {
141 wxCHECK_MSG( Ok(), -1, wxT("invalid icon") );
142
143 return M_ICONDATA->GetWidth();
144 }
145
146 int wxIcon::GetHeight() const
147 {
148 wxCHECK_MSG( Ok(), -1, wxT("invalid icon") );
149
150 return M_ICONDATA->GetHeight();
151 }
152
153 int wxIcon::GetDepth() const
154 {
155 return 32;
156 }
157
158 void wxIcon::SetDepth( int WXUNUSED(depth) )
159 {
160 }
161
162 void wxIcon::SetWidth( int WXUNUSED(width) )
163 {
164 }
165
166 void wxIcon::SetHeight( int WXUNUSED(height) )
167 {
168 }
169
170 bool wxIcon::LoadFile(
171 const wxString& filename, wxBitmapType type,
172 int desiredWidth, int desiredHeight )
173 {
174 UnRef();
175
176 if ( type == wxBITMAP_TYPE_ICON_RESOURCE )
177 {
178 OSType theId = 0 ;
179
180 if ( filename == wxT("wxICON_INFORMATION") )
181 {
182 theId = kAlertNoteIcon ;
183 }
184 else if ( filename == wxT("wxICON_QUESTION") )
185 {
186 theId = kAlertCautionIcon ;
187 }
188 else if ( filename == wxT("wxICON_WARNING") )
189 {
190 theId = kAlertCautionIcon ;
191 }
192 else if ( filename == wxT("wxICON_ERROR") )
193 {
194 theId = kAlertStopIcon ;
195 }
196 else if ( filename == wxT("wxICON_FOLDER") )
197 {
198 theId = kGenericFolderIcon ;
199 }
200 else if ( filename == wxT("wxICON_FOLDER_OPEN") )
201 {
202 theId = kOpenFolderIcon ;
203 }
204 else if ( filename == wxT("wxICON_NORMAL_FILE") )
205 {
206 theId = kGenericDocumentIcon ;
207 }
208 else if ( filename == wxT("wxICON_EXECUTABLE_FILE") )
209 {
210 theId = kGenericApplicationIcon ;
211 }
212 else if ( filename == wxT("wxICON_CDROM") )
213 {
214 theId = kGenericCDROMIcon ;
215 }
216 else if ( filename == wxT("wxICON_FLOPPY") )
217 {
218 theId = kGenericFloppyIcon ;
219 }
220 else if ( filename == wxT("wxICON_HARDDISK") )
221 {
222 theId = kGenericHardDiskIcon ;
223 }
224 else if ( filename == wxT("wxICON_REMOVABLE") )
225 {
226 theId = kGenericRemovableMediaIcon ;
227 }
228 else if ( filename == wxT("wxICON_DELETE") )
229 {
230 theId = kToolbarDeleteIcon ;
231 }
232 else if ( filename == wxT("wxICON_GO_BACK") )
233 {
234 theId = kBackwardArrowIcon ;
235 }
236 else if ( filename == wxT("wxICON_GO_FORWARD") )
237 {
238 theId = kForwardArrowIcon ;
239 }
240 else if ( filename == wxT("wxICON_GO_HOME") )
241 {
242 theId = kToolbarHomeIcon ;
243 }
244 else if ( filename == wxT("wxICON_HELP_SETTINGS") )
245 {
246 theId = kGenericFontIcon ;
247 }
248 else if ( filename == wxT("wxICON_HELP_PAGE") )
249 {
250 theId = kGenericDocumentIcon ;
251 }
252 else
253 {
254 IconRef iconRef = NULL ;
255
256 // first look in the resource fork
257 if ( iconRef == NULL )
258 {
259 Str255 theName ;
260
261 wxMacStringToPascal( filename , theName ) ;
262 Handle resHandle = GetNamedResource( 'icns' , theName ) ;
263 if ( resHandle != 0L )
264 {
265 IconFamilyHandle iconFamily = (IconFamilyHandle) resHandle ;
266 HLock((Handle) iconFamily);
267 OSStatus err = GetIconRefFromIconFamilyPtr( *iconFamily, GetHandleSize((Handle) iconFamily), &iconRef );
268 HUnlock((Handle) iconFamily);
269 if ( err != noErr )
270 {
271 wxFAIL_MSG("Error when constructing icon ref");
272 }
273
274 ReleaseResource( resHandle ) ;
275 }
276 }
277 if ( iconRef == NULL )
278 {
279 // TODO add other attempts to load it from files etc here
280 }
281 if ( iconRef )
282 {
283 m_refData = new wxIconRefData( (WXHICON) iconRef, desiredWidth, desiredHeight ) ;
284 return true ;
285 }
286 }
287
288 if ( theId != 0 )
289 {
290 IconRef iconRef = NULL ;
291 verify_noerr( GetIconRef( kOnSystemDisk, kSystemIconsCreator, theId, &iconRef ) ) ;
292 if ( iconRef )
293 {
294 m_refData = new wxIconRefData( (WXHICON) iconRef, desiredWidth, desiredHeight ) ;
295
296 return true ;
297 }
298 }
299
300 return false ;
301 }
302 else
303 {
304 wxBitmapHandler *handler = wxBitmap::FindHandler( type );
305
306 if ( handler )
307 {
308 wxBitmap bmp ;
309 if ( handler->LoadFile( &bmp , filename, type, desiredWidth, desiredHeight ))
310 {
311 CopyFromBitmap( bmp ) ;
312
313 return true ;
314 }
315
316 return false ;
317 }
318 else
319 {
320 #if wxUSE_IMAGE
321 wxImage loadimage( filename, type );
322 if (loadimage.Ok())
323 {
324 if ( desiredWidth == -1 )
325 desiredWidth = loadimage.GetWidth() ;
326 if ( desiredHeight == -1 )
327 desiredHeight = loadimage.GetHeight() ;
328 if ( desiredWidth != loadimage.GetWidth() || desiredHeight != loadimage.GetHeight() )
329 loadimage.Rescale( desiredWidth , desiredHeight ) ;
330
331 wxBitmap bmp( loadimage );
332 CopyFromBitmap( bmp ) ;
333
334 return true;
335 }
336 #endif
337 }
338 }
339 return false;
340 }
341
342 void wxIcon::CopyFromBitmap( const wxBitmap& bmp )
343 {
344 UnRef() ;
345
346 // as the bitmap owns that ref, we have to acquire it as well
347 IconRef iconRef = bmp.CreateIconRef() ;
348 m_refData = new wxIconRefData( (WXHICON) iconRef, bmp.GetWidth(), bmp.GetHeight() ) ;
349 }
350
351 IMPLEMENT_DYNAMIC_CLASS(wxICONResourceHandler, wxBitmapHandler)
352
353 bool wxICONResourceHandler::LoadFile(
354 wxBitmap *bitmap, const wxString& name, wxBitmapType WXUNUSED(flags),
355 int desiredWidth, int desiredHeight )
356 {
357 wxIcon icon ;
358 if ( icon.LoadFile( name , wxBITMAP_TYPE_ICON_RESOURCE , desiredWidth , desiredHeight ) )
359 {
360 bitmap->CopyFromIcon( icon ) ;
361 return bitmap->Ok() ;
362 }
363 return false;
364 }
365
366 #endif
367