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
);
56 wxIcon::wxIcon(WXHICON icon
, const wxSize
& size
)
59 // as the icon owns that ref, we have to acquire it as well
61 AcquireIconRef( (IconRef
) icon
) ;
63 m_refData
= new wxIconRefData( icon
) ;
64 if ( (size
.x
!= -1) && (size
.y
!= -1) )
66 M_ICONDATA
->SetWidth( size
.x
) ;
67 M_ICONDATA
->SetHeight( size
.y
) ;
75 WXHICON
wxIcon::GetHICON() const
79 return (WXHICON
) ((wxIconRefData
*)m_refData
)->GetHICON() ;
82 int wxIcon::GetWidth() const
84 wxCHECK_MSG( Ok(), -1, wxT("invalid icon") );
86 return M_ICONDATA
->GetWidth();
89 int wxIcon::GetHeight() const
91 wxCHECK_MSG( Ok(), -1, wxT("invalid icon") );
93 return M_ICONDATA
->GetHeight();
96 int wxIcon::GetDepth() const
101 void wxIcon::SetDepth( int WXUNUSED(depth
) )
105 void wxIcon::SetWidth( int WXUNUSED(width
) )
109 void wxIcon::SetHeight( int WXUNUSED(height
) )
113 bool wxIcon::IsOk() const
115 return m_refData
!= NULL
;
118 bool wxIcon::LoadFile(
119 const wxString
& filename
, wxBitmapType type
,
120 int desiredWidth
, int desiredHeight
)
124 if ( type
== wxBITMAP_TYPE_ICON_RESOURCE
)
128 if ( filename
== wxT("wxICON_INFORMATION") )
130 theId
= kAlertNoteIcon
;
132 else if ( filename
== wxT("wxICON_QUESTION") )
134 theId
= kAlertCautionIcon
;
136 else if ( filename
== wxT("wxICON_WARNING") )
138 theId
= kAlertCautionIcon
;
140 else if ( filename
== wxT("wxICON_ERROR") )
142 theId
= kAlertStopIcon
;
144 else if ( filename
== wxT("wxICON_FOLDER") )
146 theId
= kGenericFolderIcon
;
148 else if ( filename
== wxT("wxICON_FOLDER_OPEN") )
150 theId
= kOpenFolderIcon
;
152 else if ( filename
== wxT("wxICON_NORMAL_FILE") )
154 theId
= kGenericDocumentIcon
;
158 IconRef iconRef
= NULL
;
160 // first look in the resource fork
161 if ( iconRef
== NULL
)
165 wxMacStringToPascal( filename
, theName
) ;
166 Handle resHandle
= GetNamedResource( 'icns' , theName
) ;
167 if ( resHandle
!= 0L )
169 IconFamilyHandle iconFamily
= (IconFamilyHandle
) resHandle
;
170 HLock((Handle
) iconFamily
);
171 OSStatus err
= GetIconRefFromIconFamilyPtr( *iconFamily
, GetHandleSize((Handle
) iconFamily
), &iconRef
);
172 HUnlock((Handle
) iconFamily
);
173 wxASSERT_MSG( err
== noErr
, wxT("Error when constructing icon ref") );
174 ReleaseResource( resHandle
) ;
177 if ( iconRef
== NULL
)
179 // TODO add other attempts to load it from files etc here
183 m_refData
= new wxIconRefData( (WXHICON
) iconRef
) ;
190 IconRef iconRef
= NULL
;
191 verify_noerr( GetIconRef( kOnSystemDisk
, kSystemIconsCreator
, theId
, &iconRef
) ) ;
194 m_refData
= new wxIconRefData( (WXHICON
) iconRef
) ;
204 wxBitmapHandler
*handler
= wxBitmap::FindHandler( type
);
209 if ( handler
->LoadFile( &bmp
, filename
, type
, desiredWidth
, desiredHeight
))
211 CopyFromBitmap( bmp
) ;
221 wxImage
loadimage( filename
, type
);
224 if ( desiredWidth
== -1 )
225 desiredWidth
= loadimage
.GetWidth() ;
226 if ( desiredHeight
== -1 )
227 desiredHeight
= loadimage
.GetHeight() ;
228 if ( desiredWidth
!= loadimage
.GetWidth() || desiredHeight
!= loadimage
.GetHeight() )
229 loadimage
.Rescale( desiredWidth
, desiredHeight
) ;
231 wxBitmap
bmp( loadimage
);
232 CopyFromBitmap( bmp
) ;
242 void wxIcon::CopyFromBitmap( const wxBitmap
& bmp
)
246 // as the bitmap owns that ref, we have to acquire it as well
247 IconRef iconRef
= bmp
.GetBitmapData()->GetIconRef() ;
248 AcquireIconRef( iconRef
) ;
250 m_refData
= new wxIconRefData( (WXHICON
) iconRef
) ;
251 M_ICONDATA
->SetWidth( bmp
.GetWidth() ) ;
252 M_ICONDATA
->SetHeight( bmp
.GetHeight() ) ;
255 wxIconRefData::wxIconRefData( WXHICON icon
)
257 m_iconRef
= MAC_WXHICON( icon
) ;
264 void wxIconRefData::Init()
269 void wxIconRefData::Free()
273 ReleaseIconRef( m_iconRef
) ;
278 IMPLEMENT_DYNAMIC_CLASS(wxICONResourceHandler
, wxBitmapHandler
)
280 bool wxICONResourceHandler::LoadFile(
281 wxBitmap
*bitmap
, const wxString
& name
, long WXUNUSED(flags
),
282 int desiredWidth
, int desiredHeight
)
285 icon
.LoadFile( name
, wxBITMAP_TYPE_ICON_RESOURCE
, desiredWidth
, desiredHeight
) ;
286 bitmap
->CopyFromIcon( icon
) ;
288 return bitmap
->Ok() ;