1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxIcon class
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
16 #include "wx/mac/private.h"
18 IMPLEMENT_DYNAMIC_CLASS(wxIcon
, wxBitmap
)
20 #define M_ICONDATA ((wxIconRefData *)m_refData)
27 wxIcon::wxIcon( const char bits
[], int width
, int height
)
29 wxBitmap
bmp( bits
, width
, height
) ;
30 CopyFromBitmap( bmp
) ;
33 wxIcon::wxIcon( const char **bits
)
35 wxBitmap
bmp( bits
) ;
36 CopyFromBitmap( bmp
) ;
39 wxIcon::wxIcon( char **bits
)
41 wxBitmap
bmp( bits
) ;
42 CopyFromBitmap( bmp
) ;
46 const wxString
& icon_file
, int flags
,
47 int desiredWidth
, int desiredHeight
)
49 LoadFile( icon_file
, (wxBitmapType
) flags
, desiredWidth
, desiredHeight
);
56 WXHICON
wxIcon::GetHICON() const
60 return (WXHICON
) ((wxIconRefData
*)m_refData
)->GetHICON() ;
63 int wxIcon::GetWidth() const
65 wxCHECK_MSG( Ok(), -1, wxT("invalid icon") );
67 return M_ICONDATA
->GetWidth();
70 int wxIcon::GetHeight() const
72 wxCHECK_MSG( Ok(), -1, wxT("invalid icon") );
74 return M_ICONDATA
->GetHeight();
77 int wxIcon::GetDepth() const
82 void wxIcon::SetDepth( int depth
)
86 void wxIcon::SetWidth( int width
)
90 void wxIcon::SetHeight( int height
)
94 bool wxIcon::Ok() const
96 return m_refData
!= NULL
;
99 bool wxIcon::LoadFile(
100 const wxString
& filename
, wxBitmapType type
,
101 int desiredWidth
, int desiredHeight
)
105 if ( type
== wxBITMAP_TYPE_ICON_RESOURCE
)
109 if ( filename
== wxT("wxICON_INFORMATION") )
111 theId
= kAlertNoteIcon
;
113 else if ( filename
== wxT("wxICON_QUESTION") )
115 theId
= kAlertCautionIcon
;
117 else if ( filename
== wxT("wxICON_WARNING") )
119 theId
= kAlertCautionIcon
;
121 else if ( filename
== wxT("wxICON_ERROR") )
123 theId
= kAlertStopIcon
;
130 wxMacStringToPascal( name
, theName
) ;
132 Handle resHandle
= GetNamedResource( 'cicn' , theName
) ;
133 if ( resHandle
!= 0L )
135 GetResInfo( resHandle
, &theId
, &theType
, theName
) ;
136 ReleaseResource( resHandle
) ;
143 IconRef iconRef
= NULL
;
144 verify_noerr( GetIconRef( kOnSystemDisk
, kSystemIconsCreator
, theId
, &iconRef
) ) ;
147 m_refData
= new wxIconRefData( (WXHICON
) iconRef
) ;
157 wxBitmapHandler
*handler
= wxBitmap::FindHandler( type
);
162 if ( handler
->LoadFile( &bmp
, filename
, type
, desiredWidth
, desiredHeight
))
164 CopyFromBitmap( bmp
) ;
174 wxImage
loadimage( filename
, type
);
177 if ( desiredWidth
== -1 )
178 desiredWidth
= loadimage
.GetWidth() ;
179 if ( desiredHeight
== -1 )
180 desiredHeight
= loadimage
.GetHeight() ;
181 if ( desiredWidth
!= loadimage
.GetWidth() || desiredHeight
!= loadimage
.GetHeight() )
182 loadimage
.Rescale( desiredWidth
, desiredHeight
) ;
184 wxBitmap
bmp( loadimage
);
185 CopyFromBitmap( bmp
) ;
195 void wxIcon::CopyFromBitmap( const wxBitmap
& bmp
)
199 // as the bitmap owns that ref, we have to acquire it as well
200 IconRef iconRef
= bmp
.GetBitmapData()->GetIconRef() ;
201 AcquireIconRef( iconRef
) ;
203 m_refData
= new wxIconRefData( (WXHICON
) iconRef
) ;
204 M_ICONDATA
->SetWidth( bmp
.GetWidth() ) ;
205 M_ICONDATA
->SetHeight( bmp
.GetHeight() ) ;
208 wxIconRefData::wxIconRefData( WXHICON icon
)
210 m_iconRef
= MAC_WXHICON( icon
) ;
217 void wxIconRefData::Init()
222 void wxIconRefData::Free()
226 ReleaseIconRef( m_iconRef
) ;
231 IMPLEMENT_DYNAMIC_CLASS(wxICONResourceHandler
, wxBitmapHandler
)
233 bool wxICONResourceHandler::LoadFile(
234 wxBitmap
*bitmap
, const wxString
& name
, long flags
,
235 int desiredWidth
, int desiredHeight
)
238 icon
.LoadFile( name
, wxBITMAP_TYPE_ICON_RESOURCE
, desiredWidth
, desiredHeight
) ;
239 bitmap
->CopyFromIcon( icon
) ;
241 return bitmap
->Ok() ;