/////////////////////////////////////////////////////////////////////////////
// Name: 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__
+#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
#pragma implementation "icon.h"
#endif
+#include "wx/wxprec.h"
+
#include "wx/icon.h"
#if !USE_SHARED_LIBRARIES
IMPLEMENT_DYNAMIC_CLASS(wxIcon, wxBitmap)
#endif
+#include "wx/image.h"
+#include "wx/mac/private.h"
+
+#define M_ICONDATA ((wxIconRefData *)m_refData)
+
+
/*
* Icons
*/
+wxIcon::wxIcon()
+{
+}
-wxIconRefData::wxIconRefData()
+wxIcon::wxIcon(const char bits[], int width, int height)
{
- m_ok = FALSE;
- m_width = 0;
- m_height = 0;
- m_depth = 0;
- m_quality = 0;
- m_numColors = 0;
- m_bitmapMask = NULL;
- m_hBitmap = NULL ;
- m_hIcon = NULL ;
+ wxBitmap bmp(bits,width,height) ;
+ CopyFromBitmap( bmp ) ;
}
-wxIconRefData::~wxIconRefData()
+wxIcon::wxIcon( const char **bits )
{
- if ( m_hIcon )
- {
- DisposeCIcon( m_hIcon ) ;
- m_hIcon = NULL ;
- }
-
- if (m_bitmapMask)
- {
- delete m_bitmapMask;
- m_bitmapMask = NULL;
- }
+ wxBitmap bmp(bits) ;
+ CopyFromBitmap( bmp ) ;
}
-wxIcon::wxIcon()
+wxIcon::wxIcon( char **bits )
{
+ wxBitmap bmp(bits) ;
+ CopyFromBitmap( bmp ) ;
}
-wxIcon::wxIcon(const char WXUNUSED(bits)[], int WXUNUSED(width), int WXUNUSED(height))
+wxIcon::wxIcon(const wxString& icon_file, int flags,
+ int desiredWidth, int desiredHeight)
{
+ LoadFile(icon_file, (wxBitmapType) flags, desiredWidth, desiredHeight);
}
-wxIcon::wxIcon( const char **bits, int width, int height )
+wxIcon::~wxIcon()
{
}
-wxIcon::wxIcon( char **bits, int width, int height )
+WXHICON wxIcon::GetHICON() const
{
+ wxASSERT( Ok() ) ;
+ return (WXHICON) ((wxIconRefData*)m_refData)->GetHICON() ;
}
-wxIcon::wxIcon(const wxString& icon_file, long flags,
- int desiredWidth, int desiredHeight)
+int wxIcon::GetWidth() const
+{
+ wxCHECK_MSG( Ok(), -1, wxT("invalid icon") );
+ return M_ICONDATA->GetWidth();
+}
+
+int wxIcon::GetHeight() const
{
- LoadFile(icon_file, flags, desiredWidth, desiredHeight);
+ wxCHECK_MSG( Ok(), -1, wxT("invalid icon") );
+
+ return M_ICONDATA->GetHeight();
}
-wxIcon::~wxIcon()
+int wxIcon::GetDepth() const{
+ return 32;
+}
+
+void wxIcon::SetDepth(int depth){
+
+}
+
+void wxIcon::SetWidth(int width){
+
+}
+
+void wxIcon::SetHeight(int height){
+
+}
+
+bool wxIcon::Ok() const
{
+ return m_refData != NULL ;
}
-bool wxIcon::LoadFile(const wxString& filename, long type,
+bool wxIcon::LoadFile(const wxString& filename, wxBitmapType type,
int desiredWidth, int desiredHeight)
{
- UnRef();
+ UnRef();
+
+ if ( type == wxBITMAP_TYPE_ICON_RESOURCE )
+ {
+ 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
+ {/*
+ Str255 theName ;
+ OSType theType ;
+ wxMacStringToPascal( name , theName ) ;
+
+ Handle resHandle = GetNamedResource( 'cicn' , theName ) ;
+ if ( resHandle != 0L )
+ {
+ GetResInfo( resHandle , &theId , &theType , theName ) ;
+ ReleaseResource( resHandle ) ;
+ }
+ */
+ }
+ 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
+ {
+ wxBitmapHandler *handler = wxBitmap::FindHandler(type);
+
+ if ( handler )
+ {
+ wxBitmap bmp ;
+ if ( handler->LoadFile(&bmp , filename, type, desiredWidth, desiredHeight ))
+ {
+ CopyFromBitmap( bmp ) ;
+ return true ;
+ }
+ return false ;
+ }
+ 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 ) ;
+ return true;
+ }
+ }
+ }
+ return true ;
+}
- m_refData = new wxIconRefData;
+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() ) ;
+}
- wxBitmapHandler *handler = FindHandler(type);
+wxIconRefData::wxIconRefData( WXHICON icon )
+{
+ m_iconRef = MAC_WXHICON( icon ) ;
+ // Std sizes
+ SetWidth( 32 ) ;
+ SetHeight( 32 ) ;
+}
- if ( handler )
- return handler->LoadFile(this, filename, type, desiredWidth, desiredHeight);
- else
- return FALSE;
+void wxIconRefData::Init()
+{
+ m_iconRef = NULL ;
+}
+
+void wxIconRefData::Free()
+{
+ if ( m_iconRef )
+ {
+ ReleaseIconRef( m_iconRef ) ;
+ m_iconRef = NULL ;
+ }
}
IMPLEMENT_DYNAMIC_CLASS(wxICONResourceHandler, wxBitmapHandler)
bool wxICONResourceHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
- int desiredWidth, int desiredHeight)
+ int desiredWidth, int desiredHeight)
{
- Str255 theName ;
- short theId ;
- 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 ) ;
-
- CIconHandle theIcon = (CIconHandle ) GetCIcon( theId ) ;
- if ( theIcon )
- {
- M_ICONHANDLERDATA->m_hIcon = theIcon ;
- M_ICONHANDLERDATA->m_width = 32 ;
- M_ICONHANDLERDATA->m_height = 32 ;
-
- M_ICONHANDLERDATA->m_depth = 8 ;
- M_ICONHANDLERDATA->m_ok = true ;
- M_ICONHANDLERDATA->m_numColors = 256 ;
- return TRUE ;
- }
- }
- return FALSE ;
+ wxIcon icon ;
+ icon.LoadFile( name , wxBITMAP_TYPE_ICON_RESOURCE , desiredWidth , desiredHeight ) ;
+ bitmap->CopyFromIcon( icon ) ;
+ return bitmap->Ok() ;
}