X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/1f0c8f31f407ecfce909060464c0ea655221cdab..c072b9ec8a5a9f65743ecbd16b0b0bb9dbc8b46b:/src/osx/carbon/icon.cpp diff --git a/src/osx/carbon/icon.cpp b/src/osx/carbon/icon.cpp index 38e86abec3..e156e55a18 100644 --- a/src/osx/carbon/icon.cpp +++ b/src/osx/carbon/icon.cpp @@ -1,5 +1,5 @@ ///////////////////////////////////////////////////////////////////////////// -// Name: src/mac/carbon/icon.cpp +// Name: src/osx/carbon/icon.cpp // Purpose: wxIcon class // Author: Stefan Csomor // Modified by: @@ -11,6 +11,8 @@ #include "wx/wxprec.h" +#if wxOSX_USE_COCOA_OR_CARBON + #include "wx/icon.h" #ifndef WX_PRECOMP @@ -53,7 +55,7 @@ private: wxIconRefData::wxIconRefData( WXHICON icon, int desiredWidth, int desiredHeight ) { - m_iconRef = MAC_WXHICON( icon ) ; + m_iconRef = (IconRef)( icon ) ; // Standard sizes SetWidth( desiredWidth == -1 ? 32 : desiredWidth ) ; @@ -97,10 +99,10 @@ wxIcon::wxIcon(const char* const* bits) } wxIcon::wxIcon( - const wxString& icon_file, int flags, + const wxString& icon_file, wxBitmapType flags, int desiredWidth, int desiredHeight ) { - LoadFile( icon_file, (wxBitmapType) flags, desiredWidth, desiredHeight ); + LoadFile( icon_file, flags, desiredWidth, desiredHeight ); } wxIcon::wxIcon(WXHICON icon, const wxSize& size) @@ -124,7 +126,7 @@ wxGDIRefData *wxIcon::CreateGDIRefData() const wxGDIRefData *wxIcon::CloneGDIRefData(const wxGDIRefData *data) const { - return new wxIconRefData(*wx_static_cast(const wxIconRefData *, data)); + return new wxIconRefData(*static_cast(data)); } WXHICON wxIcon::GetHICON() const @@ -165,134 +167,248 @@ void wxIcon::SetHeight( int WXUNUSED(height) ) { } +// Load an icon based on resource name or filel name +// Return true on success, false otherwise bool wxIcon::LoadFile( const wxString& filename, wxBitmapType type, int desiredWidth, int desiredHeight ) +{ + if( type == wxBITMAP_TYPE_ICON_RESOURCE ) + { + if( LoadIconFromSystemResource( filename, desiredWidth, desiredHeight ) ) + return true; + else + return LoadIconFromBundleResource( filename, desiredWidth, desiredHeight ); + } + else if( type == wxBITMAP_TYPE_ICON ) + { + return LoadIconFromFile( filename, desiredWidth, desiredHeight ); + } + else + { + return LoadIconAsBitmap( filename, type, desiredWidth, desiredHeight ); + } +} + +// Load a well known system icon by its wxWidgets identifier +// Returns true on success, false otherwise +bool wxIcon::LoadIconFromSystemResource(const wxString& resourceName, int desiredWidth, int desiredHeight) { UnRef(); - if ( type == wxBITMAP_TYPE_ICON_RESOURCE ) + OSType theId = 0 ; + + if ( resourceName == wxT("wxICON_INFORMATION") ) + { + theId = kAlertNoteIcon ; + } + else if ( resourceName == wxT("wxICON_QUESTION") ) + { + theId = kAlertCautionIcon ; + } + else if ( resourceName == wxT("wxICON_WARNING") ) + { + theId = kAlertCautionIcon ; + } + else if ( resourceName == wxT("wxICON_ERROR") ) + { + theId = kAlertStopIcon ; + } + else if ( resourceName == wxT("wxICON_FOLDER") ) + { + theId = kGenericFolderIcon ; + } + else if ( resourceName == wxT("wxICON_FOLDER_OPEN") ) + { + theId = kOpenFolderIcon ; + } + else if ( resourceName == wxT("wxICON_NORMAL_FILE") ) + { + theId = kGenericDocumentIcon ; + } + else if ( resourceName == wxT("wxICON_EXECUTABLE_FILE") ) + { + theId = kGenericApplicationIcon ; + } + else if ( resourceName == wxT("wxICON_CDROM") ) + { + theId = kGenericCDROMIcon ; + } + else if ( resourceName == wxT("wxICON_FLOPPY") ) { - OSType theId = 0 ; + theId = kGenericFloppyIcon ; + } + else if ( resourceName == wxT("wxICON_HARDDISK") ) + { + theId = kGenericHardDiskIcon ; + } + else if ( resourceName == wxT("wxICON_REMOVABLE") ) + { + theId = kGenericRemovableMediaIcon ; + } + else if ( resourceName == wxT("wxICON_DELETE") ) + { + theId = kToolbarDeleteIcon ; + } + else if ( resourceName == wxT("wxICON_GO_BACK") ) + { + theId = kBackwardArrowIcon ; + } + else if ( resourceName == wxT("wxICON_GO_FORWARD") ) + { + theId = kForwardArrowIcon ; + } + else if ( resourceName == wxT("wxICON_GO_HOME") ) + { + theId = kToolbarHomeIcon ; + } + else if ( resourceName == wxT("wxICON_HELP_SETTINGS") ) + { + theId = kGenericFontIcon ; + } + else if ( resourceName == wxT("wxICON_HELP_PAGE") ) + { + theId = kGenericDocumentIcon ; + } - if ( filename == wxT("wxICON_INFORMATION") ) - { - theId = kAlertNoteIcon ; - } - else if ( filename == wxT("wxICON_QUESTION") ) - { - theId = kAlertCautionIcon ; - } - else if ( filename == wxT("wxICON_WARNING") ) - { - theId = kAlertCautionIcon ; - } - else if ( filename == wxT("wxICON_ERROR") ) - { - theId = kAlertStopIcon ; - } - else if ( filename == wxT("wxICON_FOLDER") ) - { - theId = kGenericFolderIcon ; - } - else if ( filename == wxT("wxICON_FOLDER_OPEN") ) - { - theId = kOpenFolderIcon ; - } - else if ( filename == wxT("wxICON_NORMAL_FILE") ) + if ( theId != 0 ) + { + IconRef iconRef = NULL ; + verify_noerr( GetIconRef( kOnSystemDisk, kSystemIconsCreator, theId, &iconRef ) ) ; + if ( iconRef ) { - theId = kGenericDocumentIcon ; + m_refData = new wxIconRefData( (WXHICON) iconRef, desiredWidth, desiredHeight ) ; + return true ; } - else - { - IconRef iconRef = NULL ; + } - // first look in the resource fork - if ( iconRef == NULL ) - { - Str255 theName ; - - wxMacStringToPascal( filename , theName ) ; - Handle resHandle = GetNamedResource( 'icns' , theName ) ; - if ( resHandle != 0L ) - { - IconFamilyHandle iconFamily = (IconFamilyHandle) resHandle ; - HLock((Handle) iconFamily); - OSStatus err = GetIconRefFromIconFamilyPtr( *iconFamily, GetHandleSize((Handle) iconFamily), &iconRef ); - HUnlock((Handle) iconFamily); - if ( err != noErr ) - { - wxFAIL_MSG("Error when constructing icon ref"); - } - - ReleaseResource( resHandle ) ; - } - } - if ( iconRef == NULL ) - { - // TODO add other attempts to load it from files etc here - } - if ( iconRef ) - { - m_refData = new wxIconRefData( (WXHICON) iconRef, desiredWidth, desiredHeight ) ; - return true ; - } - } + return false; +} - if ( theId != 0 ) +// Load an icon of type 'icns' by resource by name +// The resource must exist in one of the currently accessible bundles +// (usually this means the application bundle for the current application) +// Return true on success, false otherwise +bool wxIcon::LoadIconFromBundleResource(const wxString& resourceName, int desiredWidth, int desiredHeight) +{ + UnRef(); + + IconRef iconRef = NULL ; + + // first look in the resource fork + if ( iconRef == NULL ) + { + Str255 theName ; + + wxMacStringToPascal( resourceName , theName ) ; + Handle resHandle = GetNamedResource( 'icns' , theName ) ; + if ( resHandle != 0L ) { - IconRef iconRef = NULL ; - verify_noerr( GetIconRef( kOnSystemDisk, kSystemIconsCreator, theId, &iconRef ) ) ; - if ( iconRef ) - { - m_refData = new wxIconRefData( (WXHICON) iconRef, desiredWidth, desiredHeight ) ; + IconFamilyHandle iconFamily = (IconFamilyHandle) resHandle ; + OSStatus err = GetIconRefFromIconFamilyPtr( *iconFamily, GetHandleSize((Handle) iconFamily), &iconRef ); - return true ; + if ( err != noErr ) + { + wxFAIL_MSG("Error when constructing icon ref"); } + + ReleaseResource( resHandle ) ; } + } - return false ; + if ( iconRef ) + { + m_refData = new wxIconRefData( (WXHICON) iconRef, desiredWidth, desiredHeight ); + return true; } - else + + return false; +} + +// Load an icon from an icon file using the underlying OS X API +// The icon file must be in a format understood by the OS +// Return true for success, false otherwise +bool wxIcon::LoadIconFromFile(const wxString& filename, int desiredWidth, int desiredHeight) +{ + UnRef(); + + OSStatus err; + bool result = false; + + // Get a file system reference + FSRef fsRef; + err = FSPathMakeRef( (const wxUint8*)filename.utf8_str().data(), &fsRef, NULL ); + + if( err != noErr ) + return false; + + // Get a handle on the icon family + IconFamilyHandle iconFamily; + err = ReadIconFromFSRef( &fsRef, &iconFamily ); + + if( err != noErr ) + return false; + + // Get the icon reference itself + IconRef iconRef; + err = GetIconRefFromIconFamilyPtr( *iconFamily, GetHandleSize((Handle) iconFamily), &iconRef ); + + if( err == noErr ) { - wxBitmapHandler *handler = wxBitmap::FindHandler( type ); + // If everthing is OK, assign m_refData + m_refData = new wxIconRefData( (WXHICON) iconRef, desiredWidth, desiredHeight ); + result = true; + } - if ( handler ) - { - wxBitmap bmp ; - if ( handler->LoadFile( &bmp , filename, type, desiredWidth, desiredHeight )) - { - CopyFromBitmap( bmp ) ; + // Release the iconFamily before returning + ReleaseResource( (Handle) iconFamily ); + return result; +} - return true ; - } +// Load an icon from a file using functionality from wxWidgets +// A suitable bitmap handler (or image handler) must be available +// Return true on success, false otherwise +bool wxIcon::LoadIconAsBitmap(const wxString& filename, wxBitmapType type, int desiredWidth, int desiredHeight) +{ + UnRef(); - return false ; - } - else + wxBitmapHandler *handler = wxBitmap::FindHandler( type ); + + if ( handler ) + { + wxBitmap bmp ; + if ( handler->LoadFile( &bmp , filename, type, desiredWidth, desiredHeight )) { + CopyFromBitmap( bmp ) ; + return true ; + } + } + #if wxUSE_IMAGE - wxImage loadimage( filename, type ); - if (loadimage.Ok()) - { - if ( desiredWidth == -1 ) - desiredWidth = loadimage.GetWidth() ; - if ( desiredHeight == -1 ) - desiredHeight = loadimage.GetHeight() ; - if ( desiredWidth != loadimage.GetWidth() || desiredHeight != loadimage.GetHeight() ) - loadimage.Rescale( desiredWidth , desiredHeight ) ; + else + { + wxImage loadimage( filename, type ); + if (loadimage.Ok()) + { + if ( desiredWidth == -1 ) + desiredWidth = loadimage.GetWidth() ; + if ( desiredHeight == -1 ) + desiredHeight = loadimage.GetHeight() ; + if ( desiredWidth != loadimage.GetWidth() || desiredHeight != loadimage.GetHeight() ) + loadimage.Rescale( desiredWidth , desiredHeight ) ; - wxBitmap bmp( loadimage ); - CopyFromBitmap( bmp ) ; + wxBitmap bmp( loadimage ); + CopyFromBitmap( bmp ) ; - return true; - } -#endif + return true; } } - return true ; +#endif + + return false; } + void wxIcon::CopyFromBitmap( const wxBitmap& bmp ) { UnRef() ; @@ -305,13 +421,17 @@ void wxIcon::CopyFromBitmap( const wxBitmap& bmp ) IMPLEMENT_DYNAMIC_CLASS(wxICONResourceHandler, wxBitmapHandler) bool wxICONResourceHandler::LoadFile( - wxBitmap *bitmap, const wxString& name, long WXUNUSED(flags), + wxBitmap *bitmap, const wxString& name, wxBitmapType WXUNUSED(flags), int desiredWidth, int desiredHeight ) { wxIcon icon ; - icon.LoadFile( name , wxBITMAP_TYPE_ICON_RESOURCE , desiredWidth , desiredHeight ) ; - bitmap->CopyFromIcon( icon ) ; - - return bitmap->Ok() ; + if ( icon.LoadFile( name , wxBITMAP_TYPE_ICON_RESOURCE , desiredWidth , desiredHeight ) ) + { + bitmap->CopyFromIcon( icon ) ; + return bitmap->Ok() ; + } + return false; } +#endif +