X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/3dec57adfdb2469b7679930092f0bd9c8569d62c..947f3b358cae28b63c6f69af21c493cab355fb7e:/src/mac/carbon/icon.cpp diff --git a/src/mac/carbon/icon.cpp b/src/mac/carbon/icon.cpp index 7bfb1d8309..c90a0b37c7 100644 --- a/src/mac/carbon/icon.cpp +++ b/src/mac/carbon/icon.cpp @@ -1,130 +1,317 @@ ///////////////////////////////////////////////////////////////////////////// -// Name: icon.cpp +// Name: src/mac/carbon/icon.cpp // Purpose: wxIcon class -// Author: AUTHOR +// Author: Stefan Csomor // Modified by: -// Created: ??/??/98 +// Created: 1998-01-01 // RCS-ID: $Id$ -// Copyright: (c) AUTHOR -// Licence: wxWindows licence +// Copyright: (c) Stefan Csomor +// 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 -/* - * Icons - */ +#include "wx/mac/private.h" -wxIcon::wxIcon() +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(); } + + 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) : - wxBitmap(bits,width,height ) +void wxIconRefData::Init() { - + m_iconRef = NULL ; + m_width = + m_height = 0; } -wxIcon::wxIcon( const char **bits ) : - wxBitmap(bits ) +void wxIconRefData::Free() { + if ( m_iconRef ) + { + ReleaseIconRef( m_iconRef ) ; + m_iconRef = NULL ; + } } -wxIcon::wxIcon( char **bits ) : - wxBitmap(bits ) +// +// +// + +wxIcon::wxIcon() { } -wxIcon::wxIcon(const wxString& icon_file, long flags, - int desiredWidth, int desiredHeight) +wxIcon::wxIcon( const char bits[], int width, int height ) +{ + wxBitmap bmp( bits, width, height ) ; + CopyFromBitmap( bmp ) ; +} +wxIcon::wxIcon(const char* const* bits) { - LoadFile(icon_file, flags, desiredWidth, desiredHeight); + wxBitmap bmp( bits ) ; + CopyFromBitmap( bmp ) ; +} + +wxIcon::wxIcon( + const wxString& icon_file, int flags, + int desiredWidth, int desiredHeight ) +{ + 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() { } -bool wxIcon::LoadFile(const wxString& filename, long type, - int desiredWidth, int desiredHeight) +wxGDIRefData *wxIcon::CreateGDIRefData() const { - UnRef(); + return new wxIconRefData; +} - m_refData = new wxBitmapRefData; +wxGDIRefData *wxIcon::CloneGDIRefData(const wxGDIRefData *data) const +{ + return new wxIconRefData(*wx_static_cast(const wxIconRefData *, data)); +} - wxBitmapHandler *handler = FindHandler(type); +WXHICON wxIcon::GetHICON() const +{ + wxASSERT( Ok() ) ; - if ( handler ) - return handler->LoadFile(this, filename, type, desiredWidth, desiredHeight); - else - return FALSE; + return (WXHICON) ((wxIconRefData*)m_refData)->GetHICON() ; } -IMPLEMENT_DYNAMIC_CLASS(wxICONResourceHandler, wxBitmapHandler) +int wxIcon::GetWidth() const +{ + wxCHECK_MSG( Ok(), -1, wxT("invalid icon") ); + + return M_ICONDATA->GetWidth(); +} -bool wxICONResourceHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags, - int desiredWidth, int desiredHeight) +int wxIcon::GetHeight() const { - short theId = -1 ; - if ( name == "wxICON_INFO" ) - { - theId = kNoteIcon ; - } - else if ( name == "wxICON_QUESTION" ) - { - theId = kCautionIcon ; - } - else if ( name == "wxICON_WARNING" ) - { - theId = kCautionIcon ; - } - else if ( name == "wxICON_ERROR" ) + wxCHECK_MSG( Ok(), -1, wxT("invalid icon") ); + + return M_ICONDATA->GetHeight(); +} + +int wxIcon::GetDepth() const +{ + return 32; +} + +void wxIcon::SetDepth( int WXUNUSED(depth) ) +{ +} + +void wxIcon::SetWidth( int WXUNUSED(width) ) +{ +} + +void wxIcon::SetHeight( int WXUNUSED(height) ) +{ +} + +bool wxIcon::LoadFile( + const wxString& filename, wxBitmapType type, + int desiredWidth, int desiredHeight ) +{ + UnRef(); + + if ( type == wxBITMAP_TYPE_ICON_RESOURCE ) { - theId = kStopIcon ; + 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 ( 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 + { + 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 ; + } + } + + if ( theId != 0 ) + { + IconRef iconRef = NULL ; + verify_noerr( GetIconRef( kOnSystemDisk, kSystemIconsCreator, theId, &iconRef ) ) ; + if ( iconRef ) + { + m_refData = new wxIconRefData( (WXHICON) iconRef, desiredWidth, desiredHeight ) ; + + return true ; + } + } + + return false ; } else { - Str255 theName ; - OSType theType ; - - #if TARGET_CARBON - c2pstrcpy( (StringPtr) theName , name ) ; - #else - strcpy( (char *) theName , name ) ; - c2pstr( (char *) theName ) ; - #endif - - Handle resHandle = GetNamedResource( 'cicn' , theName ) ; - if ( resHandle != 0L ) - { - GetResInfo( resHandle , &theId , &theType , theName ) ; - ReleaseResource( resHandle ) ; - } + wxBitmapHandler *handler = wxBitmap::FindHandler( type ); + + if ( handler ) + { + 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 + } } - if ( theId != -1 ) - { - 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 ; - } - } - return FALSE ; + return true ; +} + +void wxIcon::CopyFromBitmap( const wxBitmap& bmp ) +{ + UnRef() ; + + // as the bitmap owns that ref, we have to acquire it as well + 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 WXUNUSED(flags), + int desiredWidth, int desiredHeight ) +{ + wxIcon icon ; + icon.LoadFile( name , wxBITMAP_TYPE_ICON_RESOURCE , desiredWidth , desiredHeight ) ; + bitmap->CopyFromIcon( icon ) ; + + return bitmap->Ok() ; } +