1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/icon.cpp
3 // Purpose: wxIcon class
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
20 #include "wx/mac/private.h"
22 IMPLEMENT_DYNAMIC_CLASS(wxIcon
, wxBitmap
)
24 #define M_ICONDATA ((wxIconRefData *)m_refData)
31 wxIcon::wxIcon( const char bits
[], int width
, int height
)
33 wxBitmap
bmp( bits
, width
, height
) ;
34 CopyFromBitmap( bmp
) ;
37 wxIcon::wxIcon( const char **bits
)
39 wxBitmap
bmp( bits
) ;
40 CopyFromBitmap( bmp
) ;
43 wxIcon::wxIcon( char **bits
)
45 wxBitmap
bmp( bits
) ;
46 CopyFromBitmap( bmp
) ;
50 const wxString
& icon_file
, int flags
,
51 int desiredWidth
, int desiredHeight
)
53 LoadFile( icon_file
, (wxBitmapType
) flags
, desiredWidth
, desiredHeight
);
60 WXHICON
wxIcon::GetHICON() const
64 return (WXHICON
) ((wxIconRefData
*)m_refData
)->GetHICON() ;
67 int wxIcon::GetWidth() const
69 wxCHECK_MSG( Ok(), -1, wxT("invalid icon") );
71 return M_ICONDATA
->GetWidth();
74 int wxIcon::GetHeight() const
76 wxCHECK_MSG( Ok(), -1, wxT("invalid icon") );
78 return M_ICONDATA
->GetHeight();
81 int wxIcon::GetDepth() const
86 void wxIcon::SetDepth( int depth
)
90 void wxIcon::SetWidth( int width
)
94 void wxIcon::SetHeight( int height
)
98 bool wxIcon::IsOk() const
100 return m_refData
!= NULL
;
103 bool wxIcon::LoadFile(
104 const wxString
& filename
, wxBitmapType type
,
105 int desiredWidth
, int desiredHeight
)
109 if ( type
== wxBITMAP_TYPE_ICON_RESOURCE
)
113 if ( filename
== wxT("wxICON_INFORMATION") )
115 theId
= kAlertNoteIcon
;
117 else if ( filename
== wxT("wxICON_QUESTION") )
119 theId
= kAlertCautionIcon
;
121 else if ( filename
== wxT("wxICON_WARNING") )
123 theId
= kAlertCautionIcon
;
125 else if ( filename
== wxT("wxICON_ERROR") )
127 theId
= kAlertStopIcon
;
134 wxMacStringToPascal( name
, theName
) ;
136 Handle resHandle
= GetNamedResource( 'cicn' , theName
) ;
137 if ( resHandle
!= 0L )
139 GetResInfo( resHandle
, &theId
, &theType
, theName
) ;
140 ReleaseResource( resHandle
) ;
147 IconRef iconRef
= NULL
;
148 verify_noerr( GetIconRef( kOnSystemDisk
, kSystemIconsCreator
, theId
, &iconRef
) ) ;
151 m_refData
= new wxIconRefData( (WXHICON
) iconRef
) ;
161 wxBitmapHandler
*handler
= wxBitmap::FindHandler( type
);
166 if ( handler
->LoadFile( &bmp
, filename
, type
, desiredWidth
, desiredHeight
))
168 CopyFromBitmap( bmp
) ;
178 wxImage
loadimage( filename
, type
);
181 if ( desiredWidth
== -1 )
182 desiredWidth
= loadimage
.GetWidth() ;
183 if ( desiredHeight
== -1 )
184 desiredHeight
= loadimage
.GetHeight() ;
185 if ( desiredWidth
!= loadimage
.GetWidth() || desiredHeight
!= loadimage
.GetHeight() )
186 loadimage
.Rescale( desiredWidth
, desiredHeight
) ;
188 wxBitmap
bmp( loadimage
);
189 CopyFromBitmap( bmp
) ;
199 void wxIcon::CopyFromBitmap( const wxBitmap
& bmp
)
203 // as the bitmap owns that ref, we have to acquire it as well
204 IconRef iconRef
= bmp
.GetBitmapData()->GetIconRef() ;
205 AcquireIconRef( iconRef
) ;
207 m_refData
= new wxIconRefData( (WXHICON
) iconRef
) ;
208 M_ICONDATA
->SetWidth( bmp
.GetWidth() ) ;
209 M_ICONDATA
->SetHeight( bmp
.GetHeight() ) ;
212 wxIconRefData::wxIconRefData( WXHICON icon
)
214 m_iconRef
= MAC_WXHICON( icon
) ;
221 void wxIconRefData::Init()
226 void wxIconRefData::Free()
230 ReleaseIconRef( m_iconRef
) ;
235 IMPLEMENT_DYNAMIC_CLASS(wxICONResourceHandler
, wxBitmapHandler
)
237 bool wxICONResourceHandler::LoadFile(
238 wxBitmap
*bitmap
, const wxString
& name
, long flags
,
239 int desiredWidth
, int desiredHeight
)
242 icon
.LoadFile( name
, wxBITMAP_TYPE_ICON_RESOURCE
, desiredWidth
, desiredHeight
) ;
243 bitmap
->CopyFromIcon( icon
) ;
245 return bitmap
->Ok() ;