X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/1996c23f8c7255c51aacc8f1a2f8f904d440ea4f..4b6930c81303eace9164234a5b5589e1904626eb:/src/mac/carbon/icon.cpp diff --git a/src/mac/carbon/icon.cpp b/src/mac/carbon/icon.cpp index ab1dffe712..7084b59cfc 100644 --- a/src/mac/carbon/icon.cpp +++ b/src/mac/carbon/icon.cpp @@ -1,150 +1,247 @@ ///////////////////////////////////////////////////////////////////////////// -// 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 ///////////////////////////////////////////////////////////////////////////// -#ifdef __GNUG__ -#pragma implementation "icon.h" -#endif +#include "wx/wxprec.h" #include "wx/icon.h" -#if !USE_SHARED_LIBRARIES -IMPLEMENT_DYNAMIC_CLASS(wxIcon, wxBitmap) +#ifndef WX_PRECOMP + #include "wx/image.h" #endif -#include "wx/image.h" #include "wx/mac/private.h" +IMPLEMENT_DYNAMIC_CLASS(wxIcon, wxBitmap) + +#define M_ICONDATA ((wxIconRefData *)m_refData) -/* - * Icons - */ wxIcon::wxIcon() { } -wxIcon::wxIcon(const char bits[], int width, int height) : - wxBitmap(bits, width, height) +wxIcon::wxIcon( const char bits[], int width, int height ) { - + wxBitmap bmp( bits, width, height ) ; + CopyFromBitmap( bmp ) ; } -wxIcon::wxIcon( const char **bits ) : - wxBitmap(bits) +wxIcon::wxIcon( const char **bits ) { + wxBitmap bmp( bits ) ; + CopyFromBitmap( bmp ) ; } -wxIcon::wxIcon( char **bits ) : - wxBitmap(bits) +wxIcon::wxIcon( char **bits ) { + wxBitmap bmp( bits ) ; + CopyFromBitmap( bmp ) ; } -wxIcon::wxIcon(const wxString& icon_file, int flags, - int desiredWidth, int desiredHeight) +wxIcon::wxIcon( + const wxString& icon_file, int flags, + int desiredWidth, int desiredHeight ) { - LoadFile(icon_file, (wxBitmapType) flags, desiredWidth, desiredHeight); + LoadFile( icon_file, (wxBitmapType) flags, desiredWidth, desiredHeight ); } wxIcon::~wxIcon() { } -bool wxIcon::LoadFile(const wxString& filename, wxBitmapType type, - int desiredWidth, int desiredHeight) +WXHICON wxIcon::GetHICON() const +{ + wxASSERT( Ok() ) ; + + return (WXHICON) ((wxIconRefData*)m_refData)->GetHICON() ; +} + +int wxIcon::GetWidth() const +{ + wxCHECK_MSG( Ok(), -1, wxT("invalid icon") ); + + return M_ICONDATA->GetWidth(); +} + +int wxIcon::GetHeight() const +{ + wxCHECK_MSG( Ok(), -1, wxT("invalid icon") ); + + return M_ICONDATA->GetHeight(); +} + +int wxIcon::GetDepth() const +{ + return 32; +} + +void wxIcon::SetDepth( int depth ) +{ +} + +void wxIcon::SetWidth( int width ) +{ +} + +void wxIcon::SetHeight( int height ) +{ +} + +bool wxIcon::IsOk() const +{ + return m_refData != NULL ; +} + +bool wxIcon::LoadFile( + const wxString& filename, wxBitmapType type, + int desiredWidth, int desiredHeight ) { UnRef(); - - wxBitmapHandler *handler = FindHandler(type); - if ( handler ) + if ( type == wxBITMAP_TYPE_ICON_RESOURCE ) { - m_refData = new wxBitmapRefData; - return handler->LoadFile(this, filename, type, desiredWidth, desiredHeight ); + OSType theId = 0 ; + + 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 0 + Str255 theName ; + OSType theType ; + wxMacStringToPascal( name , theName ) ; + + Handle resHandle = GetNamedResource( 'cicn' , theName ) ; + if ( resHandle != 0L ) + { + GetResInfo( resHandle , &theId , &theType , theName ) ; + ReleaseResource( resHandle ) ; + } +#endif + } + + if ( theId != 0 ) + { + IconRef iconRef = NULL ; + verify_noerr( GetIconRef( kOnSystemDisk, kSystemIconsCreator, theId, &iconRef ) ) ; + if ( iconRef ) + { + m_refData = new wxIconRefData( (WXHICON) iconRef ) ; + + return true ; + } + } + + return false ; } else { - wxImage loadimage(filename, type); - if (loadimage.Ok()) + wxBitmapHandler *handler = wxBitmap::FindHandler( type ); + + if ( handler ) { - 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 ); - wxIcon *icon = (wxIcon*)(&bmp); - *this = *icon; - return true; + wxBitmap bmp ; + if ( handler->LoadFile( &bmp , filename, type, desiredWidth, desiredHeight )) + { + CopyFromBitmap( bmp ) ; + + return true ; + } + + return false ; + } + else + { +#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 ) ; + + wxBitmap bmp( loadimage ); + CopyFromBitmap( bmp ) ; + + return true; + } +#endif } } - return false ; + return true ; } -void wxIcon::CopyFromBitmap(const wxBitmap& bmp) +void wxIcon::CopyFromBitmap( const wxBitmap& bmp ) { - wxIcon *icon = (wxIcon*)(&bmp); - *this = *icon; + 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() ) ; } -IMPLEMENT_DYNAMIC_CLASS(wxICONResourceHandler, wxBitmapHandler) +wxIconRefData::wxIconRefData( WXHICON icon ) +{ + m_iconRef = MAC_WXHICON( icon ) ; + + // Standard sizes + SetWidth( 32 ) ; + SetHeight( 32 ) ; +} -bool wxICONResourceHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags, - int desiredWidth, int desiredHeight) +void wxIconRefData::Init() { - short theId = -1 ; - if ( name == wxT("wxICON_INFORMATION") ) - { - theId = kNoteIcon ; - } - else if ( name == wxT("wxICON_QUESTION") ) - { - theId = kCautionIcon ; - } - else if ( name == wxT("wxICON_WARNING") ) - { - theId = kCautionIcon ; - } - else if ( name == wxT("wxICON_ERROR") ) - { - theId = kStopIcon ; - } - else - { - Str255 theName ; - OSType theType ; - wxMacStringToPascal( name , theName ) ; - - Handle resHandle = GetNamedResource( 'cicn' , theName ) ; - if ( resHandle != 0L ) - { - GetResInfo( resHandle , &theId , &theType , theName ) ; - ReleaseResource( resHandle ) ; - } - } - if ( theId != -1 ) + m_iconRef = NULL ; +} + +void wxIconRefData::Free() +{ + if ( m_iconRef ) { - CIconHandle theIcon = (CIconHandle ) GetCIcon( theId ) ; - if ( theIcon ) - { - M_BITMAPHANDLERDATA->m_hIcon = theIcon ; - M_BITMAPHANDLERDATA->m_width = 32 ; - M_BITMAPHANDLERDATA->m_height = 32 ; - - M_BITMAPHANDLERDATA->m_depth = 8 ; - M_BITMAPHANDLERDATA->m_ok = true ; - M_BITMAPHANDLERDATA->m_numColors = 256 ; - M_BITMAPHANDLERDATA->m_bitmapType = kMacBitmapTypeIcon ; - return TRUE ; - } + ReleaseIconRef( m_iconRef ) ; + m_iconRef = NULL ; } - return FALSE ; } + +IMPLEMENT_DYNAMIC_CLASS(wxICONResourceHandler, wxBitmapHandler) + +bool wxICONResourceHandler::LoadFile( + wxBitmap *bitmap, const wxString& name, long flags, + int desiredWidth, int desiredHeight ) +{ + wxIcon icon ; + icon.LoadFile( name , wxBITMAP_TYPE_ICON_RESOURCE , desiredWidth , desiredHeight ) ; + bitmap->CopyFromIcon( icon ) ; + + return bitmap->Ok() ; +} +