1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/icon.cpp
3 // Purpose: wxIcon class
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
14 #if wxOSX_USE_COCOA_OR_CARBON
22 #include "wx/osx/private.h"
24 IMPLEMENT_DYNAMIC_CLASS(wxIcon
, wxGDIObject
)
26 #define M_ICONDATA ((wxIconRefData *)m_refData)
28 class WXDLLEXPORT wxIconRefData
: public wxGDIRefData
31 wxIconRefData() { Init(); }
32 wxIconRefData( WXHICON iconref
, int desiredWidth
, int desiredHeight
);
33 virtual ~wxIconRefData() { Free(); }
35 virtual bool IsOk() const { return m_iconRef
!= NULL
; }
39 void SetWidth( int width
) { m_width
= width
; }
40 void SetHeight( int height
) { m_height
= height
; }
42 int GetWidth() const { return m_width
; }
43 int GetHeight() const { return m_height
; }
45 WXHICON
GetHICON() const { return (WXHICON
) m_iconRef
; }
56 wxIconRefData::wxIconRefData( WXHICON icon
, int desiredWidth
, int desiredHeight
)
58 m_iconRef
= (IconRef
)( icon
) ;
61 SetWidth( desiredWidth
== -1 ? 32 : desiredWidth
) ;
62 SetHeight( desiredHeight
== -1 ? 32 : desiredHeight
) ;
65 void wxIconRefData::Init()
72 void wxIconRefData::Free()
76 ReleaseIconRef( m_iconRef
) ;
89 wxIcon::wxIcon( const char bits
[], int width
, int height
)
91 wxBitmap
bmp( bits
, width
, height
) ;
92 CopyFromBitmap( bmp
) ;
95 wxIcon::wxIcon(const char* const* bits
)
97 wxBitmap
bmp( bits
) ;
98 CopyFromBitmap( bmp
) ;
102 const wxString
& icon_file
, wxBitmapType flags
,
103 int desiredWidth
, int desiredHeight
)
105 LoadFile( icon_file
, flags
, desiredWidth
, desiredHeight
);
108 wxIcon::wxIcon(WXHICON icon
, const wxSize
& size
)
111 // as the icon owns that ref, we have to acquire it as well
113 AcquireIconRef( (IconRef
) icon
) ;
115 m_refData
= new wxIconRefData( icon
, size
.x
, size
.y
) ;
122 wxGDIRefData
*wxIcon::CreateGDIRefData() const
124 return new wxIconRefData
;
127 wxGDIRefData
*wxIcon::CloneGDIRefData(const wxGDIRefData
*data
) const
129 return new wxIconRefData(*static_cast<const wxIconRefData
*>(data
));
132 WXHICON
wxIcon::GetHICON() const
136 return (WXHICON
) ((wxIconRefData
*)m_refData
)->GetHICON() ;
139 int wxIcon::GetWidth() const
141 wxCHECK_MSG( Ok(), -1, wxT("invalid icon") );
143 return M_ICONDATA
->GetWidth();
146 int wxIcon::GetHeight() const
148 wxCHECK_MSG( Ok(), -1, wxT("invalid icon") );
150 return M_ICONDATA
->GetHeight();
153 int wxIcon::GetDepth() const
158 void wxIcon::SetDepth( int WXUNUSED(depth
) )
162 void wxIcon::SetWidth( int WXUNUSED(width
) )
166 void wxIcon::SetHeight( int WXUNUSED(height
) )
170 // Load an icon based on resource name or filel name
171 // Return true on success, false otherwise
172 bool wxIcon::LoadFile(
173 const wxString
& filename
, wxBitmapType type
,
174 int desiredWidth
, int desiredHeight
)
176 if( type
== wxBITMAP_TYPE_ICON_RESOURCE
)
178 if( LoadIconFromSystemResource( filename
, desiredWidth
, desiredHeight
) )
181 return LoadIconFromBundleResource( filename
, desiredWidth
, desiredHeight
);
183 else if( type
== wxBITMAP_TYPE_ICON
)
185 return LoadIconFromFile( filename
, desiredWidth
, desiredHeight
);
189 return LoadIconAsBitmap( filename
, type
, desiredWidth
, desiredHeight
);
193 // Load a well known system icon by its wxWidgets identifier
194 // Returns true on success, false otherwise
195 bool wxIcon::LoadIconFromSystemResource(const wxString
& resourceName
, int desiredWidth
, int desiredHeight
)
201 if ( resourceName
== wxT("wxICON_INFORMATION") )
203 theId
= kAlertNoteIcon
;
205 else if ( resourceName
== wxT("wxICON_QUESTION") )
207 theId
= kAlertCautionIcon
;
209 else if ( resourceName
== wxT("wxICON_WARNING") )
211 theId
= kAlertCautionIcon
;
213 else if ( resourceName
== wxT("wxICON_ERROR") )
215 theId
= kAlertStopIcon
;
217 else if ( resourceName
== wxT("wxICON_FOLDER") )
219 theId
= kGenericFolderIcon
;
221 else if ( resourceName
== wxT("wxICON_FOLDER_OPEN") )
223 theId
= kOpenFolderIcon
;
225 else if ( resourceName
== wxT("wxICON_NORMAL_FILE") )
227 theId
= kGenericDocumentIcon
;
229 else if ( resourceName
== wxT("wxICON_EXECUTABLE_FILE") )
231 theId
= kGenericApplicationIcon
;
233 else if ( resourceName
== wxT("wxICON_CDROM") )
235 theId
= kGenericCDROMIcon
;
237 else if ( resourceName
== wxT("wxICON_FLOPPY") )
239 theId
= kGenericFloppyIcon
;
241 else if ( resourceName
== wxT("wxICON_HARDDISK") )
243 theId
= kGenericHardDiskIcon
;
245 else if ( resourceName
== wxT("wxICON_REMOVABLE") )
247 theId
= kGenericRemovableMediaIcon
;
249 else if ( resourceName
== wxT("wxICON_DELETE") )
251 theId
= kToolbarDeleteIcon
;
253 else if ( resourceName
== wxT("wxICON_GO_BACK") )
255 theId
= kBackwardArrowIcon
;
257 else if ( resourceName
== wxT("wxICON_GO_FORWARD") )
259 theId
= kForwardArrowIcon
;
261 else if ( resourceName
== wxT("wxICON_GO_HOME") )
263 theId
= kToolbarHomeIcon
;
265 else if ( resourceName
== wxT("wxICON_HELP_SETTINGS") )
267 theId
= kGenericFontIcon
;
269 else if ( resourceName
== wxT("wxICON_HELP_PAGE") )
271 theId
= kGenericDocumentIcon
;
276 IconRef iconRef
= NULL
;
277 verify_noerr( GetIconRef( kOnSystemDisk
, kSystemIconsCreator
, theId
, &iconRef
) ) ;
280 m_refData
= new wxIconRefData( (WXHICON
) iconRef
, desiredWidth
, desiredHeight
) ;
288 // Load an icon of type 'icns' by resource by name
289 // The resource must exist in one of the currently accessible bundles
290 // (usually this means the application bundle for the current application)
291 // Return true on success, false otherwise
292 bool wxIcon::LoadIconFromBundleResource(const wxString
& resourceName
, int desiredWidth
, int desiredHeight
)
296 IconRef iconRef
= NULL
;
298 // first look in the resource fork
299 if ( iconRef
== NULL
)
303 wxMacStringToPascal( resourceName
, theName
) ;
304 Handle resHandle
= GetNamedResource( 'icns' , theName
) ;
305 if ( resHandle
!= 0L )
307 IconFamilyHandle iconFamily
= (IconFamilyHandle
) resHandle
;
308 OSStatus err
= GetIconRefFromIconFamilyPtr( *iconFamily
, GetHandleSize((Handle
) iconFamily
), &iconRef
);
312 wxFAIL_MSG("Error when constructing icon ref");
315 ReleaseResource( resHandle
) ;
321 m_refData
= new wxIconRefData( (WXHICON
) iconRef
, desiredWidth
, desiredHeight
);
328 // Load an icon from an icon file using the underlying OS X API
329 // The icon file must be in a format understood by the OS
330 // Return true for success, false otherwise
331 bool wxIcon::LoadIconFromFile(const wxString
& filename
, int desiredWidth
, int desiredHeight
)
338 // Get a file system reference
340 err
= FSPathMakeRef( (const wxUint8
*)filename
.utf8_str().data(), &fsRef
, NULL
);
345 // Get a handle on the icon family
346 IconFamilyHandle iconFamily
;
347 err
= ReadIconFromFSRef( &fsRef
, &iconFamily
);
352 // Get the icon reference itself
354 err
= GetIconRefFromIconFamilyPtr( *iconFamily
, GetHandleSize((Handle
) iconFamily
), &iconRef
);
358 // If everthing is OK, assign m_refData
359 m_refData
= new wxIconRefData( (WXHICON
) iconRef
, desiredWidth
, desiredHeight
);
363 // Release the iconFamily before returning
364 ReleaseResource( (Handle
) iconFamily
);
368 // Load an icon from a file using functionality from wxWidgets
369 // A suitable bitmap handler (or image handler) must be available
370 // Return true on success, false otherwise
371 bool wxIcon::LoadIconAsBitmap(const wxString
& filename
, wxBitmapType type
, int desiredWidth
, int desiredHeight
)
375 wxBitmapHandler
*handler
= wxBitmap::FindHandler( type
);
380 if ( handler
->LoadFile( &bmp
, filename
, type
, desiredWidth
, desiredHeight
))
382 CopyFromBitmap( bmp
) ;
390 wxImage
loadimage( filename
, type
);
393 if ( desiredWidth
== -1 )
394 desiredWidth
= loadimage
.GetWidth() ;
395 if ( desiredHeight
== -1 )
396 desiredHeight
= loadimage
.GetHeight() ;
397 if ( desiredWidth
!= loadimage
.GetWidth() || desiredHeight
!= loadimage
.GetHeight() )
398 loadimage
.Rescale( desiredWidth
, desiredHeight
) ;
400 wxBitmap
bmp( loadimage
);
401 CopyFromBitmap( bmp
) ;
412 void wxIcon::CopyFromBitmap( const wxBitmap
& bmp
)
416 // as the bitmap owns that ref, we have to acquire it as well
417 IconRef iconRef
= bmp
.CreateIconRef() ;
418 m_refData
= new wxIconRefData( (WXHICON
) iconRef
, bmp
.GetWidth(), bmp
.GetHeight() ) ;
421 IMPLEMENT_DYNAMIC_CLASS(wxICONResourceHandler
, wxBitmapHandler
)
423 bool wxICONResourceHandler::LoadFile(
424 wxBitmap
*bitmap
, const wxString
& name
, wxBitmapType
WXUNUSED(flags
),
425 int desiredWidth
, int desiredHeight
)
428 if ( icon
.LoadFile( name
, wxBITMAP_TYPE_ICON_RESOURCE
, desiredWidth
, desiredHeight
) )
430 bitmap
->CopyFromIcon( icon
) ;
431 return bitmap
->Ok() ;