X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/5a7d70fe8922a56407a5b02b95a53fb16e787f70..947f3b358cae28b63c6f69af21c493cab355fb7e:/src/mac/carbon/icon.cpp diff --git a/src/mac/carbon/icon.cpp b/src/mac/carbon/icon.cpp index 3473bf72ba..c90a0b37c7 100644 --- a/src/mac/carbon/icon.cpp +++ b/src/mac/carbon/icon.cpp @@ -1,42 +1,96 @@ ///////////////////////////////////////////////////////////////////////////// -// Name: icon.cpp +// Name: src/mac/carbon/icon.cpp // Purpose: wxIcon class // Author: Stefan Csomor // Modified by: // Created: 1998-01-01 // RCS-ID: $Id$ // Copyright: (c) Stefan Csomor -// Licence: wxWindows licence +// Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// #include "wx/wxprec.h" #include "wx/icon.h" -#include "wx/image.h" + +#ifndef WX_PRECOMP + #include "wx/image.h" +#endif + #include "wx/mac/private.h" -IMPLEMENT_DYNAMIC_CLASS(wxIcon, wxBitmap) +IMPLEMENT_DYNAMIC_CLASS(wxIcon, wxGDIObject) #define M_ICONDATA ((wxIconRefData *)m_refData) +class WXDLLEXPORT wxIconRefData : public wxGDIRefData +{ +public: + wxIconRefData() { Init(); } + wxIconRefData( WXHICON iconref, int desiredWidth, int desiredHeight ); + virtual ~wxIconRefData() { Free(); } -wxIcon::wxIcon() + virtual bool IsOk() const { return m_iconRef != NULL; } + + virtual void Free(); + + void SetWidth( int width ) { m_width = width; } + void SetHeight( int height ) { m_height = height; } + + int GetWidth() const { return m_width; } + int GetHeight() const { return m_height; } + + WXHICON GetHICON() const { return (WXHICON) m_iconRef; } + +private: + void Init(); + + IconRef m_iconRef; + int m_width; + int m_height; +}; + + +wxIconRefData::wxIconRefData( WXHICON icon, int desiredWidth, int desiredHeight ) { + m_iconRef = MAC_WXHICON( icon ) ; + + // Standard sizes + SetWidth( desiredWidth == -1 ? 32 : desiredWidth ) ; + SetHeight( desiredHeight == -1 ? 32 : desiredHeight ) ; } -wxIcon::wxIcon( const char bits[], int width, int height ) +void wxIconRefData::Init() { - wxBitmap bmp( bits, width, height ) ; - CopyFromBitmap( bmp ) ; + m_iconRef = NULL ; + m_width = + m_height = 0; } -wxIcon::wxIcon( const char **bits ) +void wxIconRefData::Free() { - wxBitmap bmp( bits ) ; + if ( m_iconRef ) + { + ReleaseIconRef( m_iconRef ) ; + m_iconRef = NULL ; + } +} + +// +// +// + +wxIcon::wxIcon() +{ +} + +wxIcon::wxIcon( const char bits[], int width, int height ) +{ + wxBitmap bmp( bits, width, height ) ; CopyFromBitmap( bmp ) ; } -wxIcon::wxIcon( char **bits ) +wxIcon::wxIcon(const char* const* bits) { wxBitmap bmp( bits ) ; CopyFromBitmap( bmp ) ; @@ -49,10 +103,30 @@ wxIcon::wxIcon( LoadFile( icon_file, (wxBitmapType) flags, desiredWidth, desiredHeight ); } +wxIcon::wxIcon(WXHICON icon, const wxSize& size) + : wxGDIObject() +{ + // as the icon owns that ref, we have to acquire it as well + if (icon) + AcquireIconRef( (IconRef) icon ) ; + + m_refData = new wxIconRefData( icon, size.x, size.y ) ; +} + wxIcon::~wxIcon() { } +wxGDIRefData *wxIcon::CreateGDIRefData() const +{ + return new wxIconRefData; +} + +wxGDIRefData *wxIcon::CloneGDIRefData(const wxGDIRefData *data) const +{ + return new wxIconRefData(*wx_static_cast(const wxIconRefData *, data)); +} + WXHICON wxIcon::GetHICON() const { wxASSERT( Ok() ) ; @@ -79,23 +153,18 @@ int wxIcon::GetDepth() const return 32; } -void wxIcon::SetDepth( int depth ) +void wxIcon::SetDepth( int WXUNUSED(depth) ) { } -void wxIcon::SetWidth( int width ) +void wxIcon::SetWidth( int WXUNUSED(width) ) { } -void wxIcon::SetHeight( int height ) +void wxIcon::SetHeight( int WXUNUSED(height) ) { } -bool wxIcon::Ok() const -{ - return m_refData != NULL ; -} - bool wxIcon::LoadFile( const wxString& filename, wxBitmapType type, int desiredWidth, int desiredHeight ) @@ -122,20 +191,52 @@ bool wxIcon::LoadFile( { 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") ) + { + theId = kGenericDocumentIcon ; + } else { -#if 0 - Str255 theName ; - OSType theType ; - wxMacStringToPascal( name , theName ) ; + IconRef iconRef = NULL ; - Handle resHandle = GetNamedResource( 'cicn' , theName ) ; - if ( resHandle != 0L ) + // first look in the resource fork + if ( iconRef == NULL ) { - GetResInfo( resHandle , &theId , &theType , theName ) ; - ReleaseResource( resHandle ) ; + 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 } -#endif + if ( iconRef ) + { + m_refData = new wxIconRefData( (WXHICON) iconRef, desiredWidth, desiredHeight ) ; + return true ; + } } if ( theId != 0 ) @@ -144,7 +245,7 @@ bool wxIcon::LoadFile( verify_noerr( GetIconRef( kOnSystemDisk, kSystemIconsCreator, theId, &iconRef ) ) ; if ( iconRef ) { - m_refData = new wxIconRefData( (WXHICON) iconRef ) ; + m_refData = new wxIconRefData( (WXHICON) iconRef, desiredWidth, desiredHeight ) ; return true ; } @@ -180,7 +281,7 @@ bool wxIcon::LoadFile( desiredHeight = loadimage.GetHeight() ; if ( desiredWidth != loadimage.GetWidth() || desiredHeight != loadimage.GetHeight() ) loadimage.Rescale( desiredWidth , desiredHeight ) ; - + wxBitmap bmp( loadimage ); CopyFromBitmap( bmp ) ; @@ -197,41 +298,14 @@ void wxIcon::CopyFromBitmap( const wxBitmap& bmp ) UnRef() ; // as the bitmap owns that ref, we have to acquire it as well - IconRef iconRef = bmp.GetBitmapData()->GetIconRef() ; - AcquireIconRef( iconRef ) ; - - m_refData = new wxIconRefData( (WXHICON) iconRef ) ; - M_ICONDATA->SetWidth( bmp.GetWidth() ) ; - M_ICONDATA->SetHeight( bmp.GetHeight() ) ; -} - -wxIconRefData::wxIconRefData( WXHICON icon ) -{ - m_iconRef = MAC_WXHICON( icon ) ; - - // Standard sizes - SetWidth( 32 ) ; - SetHeight( 32 ) ; -} - -void wxIconRefData::Init() -{ - m_iconRef = NULL ; -} - -void wxIconRefData::Free() -{ - if ( m_iconRef ) - { - ReleaseIconRef( m_iconRef ) ; - m_iconRef = NULL ; - } + IconRef iconRef = bmp.CreateIconRef() ; + m_refData = new wxIconRefData( (WXHICON) iconRef, bmp.GetWidth(), bmp.GetHeight() ) ; } IMPLEMENT_DYNAMIC_CLASS(wxICONResourceHandler, wxBitmapHandler) bool wxICONResourceHandler::LoadFile( - wxBitmap *bitmap, const wxString& name, long flags, + wxBitmap *bitmap, const wxString& name, long WXUNUSED(flags), int desiredWidth, int desiredHeight ) { wxIcon icon ;