]> git.saurik.com Git - wxWidgets.git/blob - src/osx/carbon/icon.cpp
50bd2b8c5a17e9b70eb769b055b31a7809c68b57
[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 // We can (easily) copy m_iconRef so we don't implement the copy ctor.
55 wxDECLARE_NO_COPY_CLASS(wxIconRefData);
56 };
57
58
59 wxIconRefData::wxIconRefData( WXHICON icon, int desiredWidth, int desiredHeight )
60 {
61 m_iconRef = (IconRef)( icon ) ;
62
63 // Standard sizes
64 SetWidth( desiredWidth == -1 ? 32 : desiredWidth ) ;
65 SetHeight( desiredHeight == -1 ? 32 : desiredHeight ) ;
66 }
67
68 void wxIconRefData::Init()
69 {
70 m_iconRef = NULL ;
71 m_width =
72 m_height = 0;
73 }
74
75 void wxIconRefData::Free()
76 {
77 if ( m_iconRef )
78 {
79 ReleaseIconRef( m_iconRef ) ;
80 m_iconRef = NULL ;
81 }
82 }
83
84 //
85 //
86 //
87
88 wxIcon::wxIcon()
89 {
90 }
91
92 wxIcon::wxIcon( const char bits[], int width, int height )
93 {
94 wxBitmap bmp( bits, width, height ) ;
95 CopyFromBitmap( bmp ) ;
96 }
97
98 wxIcon::wxIcon(const char* const* bits)
99 {
100 wxBitmap bmp( bits ) ;
101 CopyFromBitmap( bmp ) ;
102 }
103
104 wxIcon::wxIcon(
105 const wxString& icon_file, wxBitmapType flags,
106 int desiredWidth, int desiredHeight )
107 {
108 LoadFile( icon_file, flags, desiredWidth, desiredHeight );
109 }
110
111 wxIcon::wxIcon(WXHICON icon, const wxSize& size)
112 : wxGDIObject()
113 {
114 // as the icon owns that ref, we have to acquire it as well
115 if (icon)
116 AcquireIconRef( (IconRef) icon ) ;
117
118 m_refData = new wxIconRefData( icon, size.x, size.y ) ;
119 }
120
121 wxIcon::~wxIcon()
122 {
123 }
124
125 wxGDIRefData *wxIcon::CreateGDIRefData() const
126 {
127 return new wxIconRefData;
128 }
129
130 wxGDIRefData *
131 wxIcon::CloneGDIRefData(const wxGDIRefData * WXUNUSED(data)) const
132 {
133 wxFAIL_MSG( wxS("Cloning icons is not implemented in wxCarbon.") );
134
135 return new wxIconRefData;
136 }
137
138 WXHICON wxIcon::GetHICON() const
139 {
140 wxASSERT( Ok() ) ;
141
142 return (WXHICON) ((wxIconRefData*)m_refData)->GetHICON() ;
143 }
144
145 int wxIcon::GetWidth() const
146 {
147 wxCHECK_MSG( Ok(), -1, wxT("invalid icon") );
148
149 return M_ICONDATA->GetWidth();
150 }
151
152 int wxIcon::GetHeight() const
153 {
154 wxCHECK_MSG( Ok(), -1, wxT("invalid icon") );
155
156 return M_ICONDATA->GetHeight();
157 }
158
159 int wxIcon::GetDepth() const
160 {
161 return 32;
162 }
163
164 void wxIcon::SetDepth( int WXUNUSED(depth) )
165 {
166 }
167
168 void wxIcon::SetWidth( int WXUNUSED(width) )
169 {
170 }
171
172 void wxIcon::SetHeight( int WXUNUSED(height) )
173 {
174 }
175
176 // Load an icon based on resource name or filel name
177 // Return true on success, false otherwise
178 bool wxIcon::LoadFile(
179 const wxString& filename, wxBitmapType type,
180 int desiredWidth, int desiredHeight )
181 {
182 if( type == wxBITMAP_TYPE_ICON_RESOURCE )
183 {
184 if( LoadIconFromSystemResource( filename, desiredWidth, desiredHeight ) )
185 return true;
186 else
187 return LoadIconFromBundleResource( filename, desiredWidth, desiredHeight );
188 }
189 else if( type == wxBITMAP_TYPE_ICON )
190 {
191 return LoadIconFromFile( filename, desiredWidth, desiredHeight );
192 }
193 else
194 {
195 return LoadIconAsBitmap( filename, type, desiredWidth, desiredHeight );
196 }
197 }
198
199 // Load a well known system icon by its wxWidgets identifier
200 // Returns true on success, false otherwise
201 bool wxIcon::LoadIconFromSystemResource(const wxString& resourceName, int desiredWidth, int desiredHeight)
202 {
203 UnRef();
204
205 OSType theId = 0 ;
206
207 if ( resourceName == wxT("wxICON_INFORMATION") )
208 {
209 theId = kAlertNoteIcon ;
210 }
211 else if ( resourceName == wxT("wxICON_QUESTION") )
212 {
213 theId = kAlertCautionIcon ;
214 }
215 else if ( resourceName == wxT("wxICON_WARNING") )
216 {
217 theId = kAlertCautionIcon ;
218 }
219 else if ( resourceName == wxT("wxICON_ERROR") )
220 {
221 theId = kAlertStopIcon ;
222 }
223 else if ( resourceName == wxT("wxICON_FOLDER") )
224 {
225 theId = kGenericFolderIcon ;
226 }
227 else if ( resourceName == wxT("wxICON_FOLDER_OPEN") )
228 {
229 theId = kOpenFolderIcon ;
230 }
231 else if ( resourceName == wxT("wxICON_NORMAL_FILE") )
232 {
233 theId = kGenericDocumentIcon ;
234 }
235 else if ( resourceName == wxT("wxICON_EXECUTABLE_FILE") )
236 {
237 theId = kGenericApplicationIcon ;
238 }
239 else if ( resourceName == wxT("wxICON_CDROM") )
240 {
241 theId = kGenericCDROMIcon ;
242 }
243 else if ( resourceName == wxT("wxICON_FLOPPY") )
244 {
245 theId = kGenericFloppyIcon ;
246 }
247 else if ( resourceName == wxT("wxICON_HARDDISK") )
248 {
249 theId = kGenericHardDiskIcon ;
250 }
251 else if ( resourceName == wxT("wxICON_REMOVABLE") )
252 {
253 theId = kGenericRemovableMediaIcon ;
254 }
255 else if ( resourceName == wxT("wxICON_DELETE") )
256 {
257 theId = kToolbarDeleteIcon ;
258 }
259 else if ( resourceName == wxT("wxICON_GO_BACK") )
260 {
261 theId = kBackwardArrowIcon ;
262 }
263 else if ( resourceName == wxT("wxICON_GO_FORWARD") )
264 {
265 theId = kForwardArrowIcon ;
266 }
267 else if ( resourceName == wxT("wxICON_GO_HOME") )
268 {
269 theId = kToolbarHomeIcon ;
270 }
271 else if ( resourceName == wxT("wxICON_HELP_SETTINGS") )
272 {
273 theId = kGenericFontIcon ;
274 }
275 else if ( resourceName == wxT("wxICON_HELP_PAGE") )
276 {
277 theId = kGenericDocumentIcon ;
278 }
279
280 if ( theId != 0 )
281 {
282 IconRef iconRef = NULL ;
283 verify_noerr( GetIconRef( kOnSystemDisk, kSystemIconsCreator, theId, &iconRef ) ) ;
284 if ( iconRef )
285 {
286 m_refData = new wxIconRefData( (WXHICON) iconRef, desiredWidth, desiredHeight ) ;
287 return true ;
288 }
289 }
290
291 return false;
292 }
293
294 // Load an icon of type 'icns' by resource by name
295 // The resource must exist in one of the currently accessible bundles
296 // (usually this means the application bundle for the current application)
297 // Return true on success, false otherwise
298 bool wxIcon::LoadIconFromBundleResource(const wxString& resourceName, int desiredWidth, int desiredHeight)
299 {
300 UnRef();
301
302 IconRef iconRef = NULL ;
303
304 // first look in the resource fork
305 if ( iconRef == NULL )
306 {
307 Str255 theName ;
308
309 wxMacStringToPascal( resourceName , theName ) ;
310 Handle resHandle = GetNamedResource( 'icns' , theName ) ;
311 if ( resHandle != 0L )
312 {
313 IconFamilyHandle iconFamily = (IconFamilyHandle) resHandle ;
314 OSStatus err = GetIconRefFromIconFamilyPtr( *iconFamily, GetHandleSize((Handle) iconFamily), &iconRef );
315
316 if ( err != noErr )
317 {
318 wxFAIL_MSG("Error when constructing icon ref");
319 }
320
321 ReleaseResource( resHandle ) ;
322 }
323 }
324 if ( iconRef == NULL )
325 {
326 wxCFStringRef name(resourceName);
327 FSRef iconFSRef;
328
329 wxCFRef<CFURLRef> iconURL(CFBundleCopyResourceURL(CFBundleGetMainBundle(), name, CFSTR("icns"), NULL));
330
331 if (CFURLGetFSRef(iconURL, &iconFSRef))
332 {
333 // Get a handle on the icon family
334 IconFamilyHandle iconFamily;
335 OSStatus err = ReadIconFromFSRef( &iconFSRef, &iconFamily );
336
337 if ( err == noErr )
338 {
339 err = GetIconRefFromIconFamilyPtr( *iconFamily, GetHandleSize((Handle) iconFamily), &iconRef );
340 }
341 }
342 }
343
344 if ( iconRef )
345 {
346 m_refData = new wxIconRefData( (WXHICON) iconRef, desiredWidth, desiredHeight );
347 return true;
348 }
349
350 return false;
351 }
352
353 // Load an icon from an icon file using the underlying OS X API
354 // The icon file must be in a format understood by the OS
355 // Return true for success, false otherwise
356 bool wxIcon::LoadIconFromFile(const wxString& filename, int desiredWidth, int desiredHeight)
357 {
358 UnRef();
359
360 OSStatus err;
361 bool result = false;
362
363 // Get a file system reference
364 FSRef fsRef;
365 err = FSPathMakeRef( (const wxUint8*)filename.utf8_str().data(), &fsRef, NULL );
366
367 if( err != noErr )
368 return false;
369
370 // Get a handle on the icon family
371 IconFamilyHandle iconFamily;
372 err = ReadIconFromFSRef( &fsRef, &iconFamily );
373
374 if( err != noErr )
375 return false;
376
377 // Get the icon reference itself
378 IconRef iconRef;
379 err = GetIconRefFromIconFamilyPtr( *iconFamily, GetHandleSize((Handle) iconFamily), &iconRef );
380
381 if( err == noErr )
382 {
383 // If everthing is OK, assign m_refData
384 m_refData = new wxIconRefData( (WXHICON) iconRef, desiredWidth, desiredHeight );
385 result = true;
386 }
387
388 // Release the iconFamily before returning
389 ReleaseResource( (Handle) iconFamily );
390 return result;
391 }
392
393 // Load an icon from a file using functionality from wxWidgets
394 // A suitable bitmap handler (or image handler) must be available
395 // Return true on success, false otherwise
396 bool wxIcon::LoadIconAsBitmap(const wxString& filename, wxBitmapType type, int desiredWidth, int desiredHeight)
397 {
398 UnRef();
399
400 wxBitmapHandler *handler = wxBitmap::FindHandler( type );
401
402 if ( handler )
403 {
404 wxBitmap bmp ;
405 if ( handler->LoadFile( &bmp , filename, type, desiredWidth, desiredHeight ))
406 {
407 CopyFromBitmap( bmp ) ;
408 return true ;
409 }
410 }
411
412 #if wxUSE_IMAGE
413 else
414 {
415 wxImage loadimage( filename, type );
416 if (loadimage.Ok())
417 {
418 if ( desiredWidth == -1 )
419 desiredWidth = loadimage.GetWidth() ;
420 if ( desiredHeight == -1 )
421 desiredHeight = loadimage.GetHeight() ;
422 if ( desiredWidth != loadimage.GetWidth() || desiredHeight != loadimage.GetHeight() )
423 loadimage.Rescale( desiredWidth , desiredHeight ) ;
424
425 wxBitmap bmp( loadimage );
426 CopyFromBitmap( bmp ) ;
427
428 return true;
429 }
430 }
431 #endif
432
433 return false;
434 }
435
436
437 void wxIcon::CopyFromBitmap( const wxBitmap& bmp )
438 {
439 UnRef() ;
440
441 // as the bitmap owns that ref, we have to acquire it as well
442 IconRef iconRef = bmp.CreateIconRef() ;
443 m_refData = new wxIconRefData( (WXHICON) iconRef, bmp.GetWidth(), bmp.GetHeight() ) ;
444 }
445
446 IMPLEMENT_DYNAMIC_CLASS(wxICONResourceHandler, wxBitmapHandler)
447
448 bool wxICONResourceHandler::LoadFile(
449 wxBitmap *bitmap, const wxString& name, wxBitmapType WXUNUSED(flags),
450 int desiredWidth, int desiredHeight )
451 {
452 wxIcon icon ;
453 if ( icon.LoadFile( name , wxBITMAP_TYPE_ICON_RESOURCE , desiredWidth , desiredHeight ) )
454 {
455 bitmap->CopyFromIcon( icon ) ;
456 return bitmap->Ok() ;
457 }
458 return false;
459 }
460
461 #endif
462