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
, wxGDIObject
)
24 #define M_ICONDATA ((wxIconRefData *)m_refData)
26 class WXDLLEXPORT wxIconRefData
: public wxGDIRefData
29 wxIconRefData() { Init(); }
30 wxIconRefData( WXHICON iconref
, int desiredWidth
, int desiredHeight
);
31 virtual ~wxIconRefData() { Free(); }
33 virtual bool IsOk() const { return m_iconRef
!= NULL
; }
37 void SetWidth( int width
) { m_width
= width
; }
38 void SetHeight( int height
) { m_height
= height
; }
40 int GetWidth() const { return m_width
; }
41 int GetHeight() const { return m_height
; }
43 WXHICON
GetHICON() const { return (WXHICON
) m_iconRef
; }
54 wxIconRefData::wxIconRefData( WXHICON icon
, int desiredWidth
, int desiredHeight
)
56 m_iconRef
= MAC_WXHICON( icon
) ;
59 SetWidth( desiredWidth
== -1 ? 32 : desiredWidth
) ;
60 SetHeight( desiredHeight
== -1 ? 32 : desiredHeight
) ;
63 void wxIconRefData::Init()
70 void wxIconRefData::Free()
74 ReleaseIconRef( m_iconRef
) ;
87 wxIcon::wxIcon( const char bits
[], int width
, int height
)
89 wxBitmap
bmp( bits
, width
, height
) ;
90 CopyFromBitmap( bmp
) ;
93 wxIcon::wxIcon(const char* const* bits
)
95 wxBitmap
bmp( bits
) ;
96 CopyFromBitmap( bmp
) ;
100 const wxString
& icon_file
, int flags
,
101 int desiredWidth
, int desiredHeight
)
103 LoadFile( icon_file
, (wxBitmapType
) flags
, desiredWidth
, desiredHeight
);
106 wxIcon::wxIcon(WXHICON icon
, const wxSize
& size
)
109 // as the icon owns that ref, we have to acquire it as well
111 AcquireIconRef( (IconRef
) icon
) ;
113 m_refData
= new wxIconRefData( icon
, size
.x
, size
.y
) ;
120 wxGDIRefData
*wxIcon::CreateGDIRefData() const
122 return new wxIconRefData
;
125 wxGDIRefData
*wxIcon::CloneGDIRefData(const wxGDIRefData
*data
) const
127 return new wxIconRefData(*wx_static_cast(const wxIconRefData
*, data
));
130 WXHICON
wxIcon::GetHICON() const
134 return (WXHICON
) ((wxIconRefData
*)m_refData
)->GetHICON() ;
137 int wxIcon::GetWidth() const
139 wxCHECK_MSG( Ok(), -1, wxT("invalid icon") );
141 return M_ICONDATA
->GetWidth();
144 int wxIcon::GetHeight() const
146 wxCHECK_MSG( Ok(), -1, wxT("invalid icon") );
148 return M_ICONDATA
->GetHeight();
151 int wxIcon::GetDepth() const
156 void wxIcon::SetDepth( int WXUNUSED(depth
) )
160 void wxIcon::SetWidth( int WXUNUSED(width
) )
164 void wxIcon::SetHeight( int WXUNUSED(height
) )
168 bool wxIcon::LoadFile(
169 const wxString
& filename
, wxBitmapType type
,
170 int desiredWidth
, int desiredHeight
)
174 if ( type
== wxBITMAP_TYPE_ICON_RESOURCE
)
178 if ( filename
== wxT("wxICON_INFORMATION") )
180 theId
= kAlertNoteIcon
;
182 else if ( filename
== wxT("wxICON_QUESTION") )
184 theId
= kAlertCautionIcon
;
186 else if ( filename
== wxT("wxICON_WARNING") )
188 theId
= kAlertCautionIcon
;
190 else if ( filename
== wxT("wxICON_ERROR") )
192 theId
= kAlertStopIcon
;
194 else if ( filename
== wxT("wxICON_FOLDER") )
196 theId
= kGenericFolderIcon
;
198 else if ( filename
== wxT("wxICON_FOLDER_OPEN") )
200 theId
= kOpenFolderIcon
;
202 else if ( filename
== wxT("wxICON_NORMAL_FILE") )
204 theId
= kGenericDocumentIcon
;
208 IconRef iconRef
= NULL
;
210 // first look in the resource fork
211 if ( iconRef
== NULL
)
215 wxMacStringToPascal( filename
, theName
) ;
216 Handle resHandle
= GetNamedResource( 'icns' , theName
) ;
217 if ( resHandle
!= 0L )
219 IconFamilyHandle iconFamily
= (IconFamilyHandle
) resHandle
;
220 HLock((Handle
) iconFamily
);
221 OSStatus err
= GetIconRefFromIconFamilyPtr( *iconFamily
, GetHandleSize((Handle
) iconFamily
), &iconRef
);
222 HUnlock((Handle
) iconFamily
);
223 wxASSERT_MSG( err
== noErr
, wxT("Error when constructing icon ref") );
224 ReleaseResource( resHandle
) ;
227 if ( iconRef
== NULL
)
229 // TODO add other attempts to load it from files etc here
233 m_refData
= new wxIconRefData( (WXHICON
) iconRef
, desiredWidth
, desiredHeight
) ;
240 IconRef iconRef
= NULL
;
241 verify_noerr( GetIconRef( kOnSystemDisk
, kSystemIconsCreator
, theId
, &iconRef
) ) ;
244 m_refData
= new wxIconRefData( (WXHICON
) iconRef
, desiredWidth
, desiredHeight
) ;
254 wxBitmapHandler
*handler
= wxBitmap::FindHandler( type
);
259 if ( handler
->LoadFile( &bmp
, filename
, type
, desiredWidth
, desiredHeight
))
261 CopyFromBitmap( bmp
) ;
271 wxImage
loadimage( filename
, type
);
274 if ( desiredWidth
== -1 )
275 desiredWidth
= loadimage
.GetWidth() ;
276 if ( desiredHeight
== -1 )
277 desiredHeight
= loadimage
.GetHeight() ;
278 if ( desiredWidth
!= loadimage
.GetWidth() || desiredHeight
!= loadimage
.GetHeight() )
279 loadimage
.Rescale( desiredWidth
, desiredHeight
) ;
281 wxBitmap
bmp( loadimage
);
282 CopyFromBitmap( bmp
) ;
292 void wxIcon::CopyFromBitmap( const wxBitmap
& bmp
)
296 // as the bitmap owns that ref, we have to acquire it as well
297 IconRef iconRef
= bmp
.CreateIconRef() ;
298 m_refData
= new wxIconRefData( (WXHICON
) iconRef
, bmp
.GetWidth(), bmp
.GetHeight() ) ;
301 IMPLEMENT_DYNAMIC_CLASS(wxICONResourceHandler
, wxBitmapHandler
)
303 bool wxICONResourceHandler::LoadFile(
304 wxBitmap
*bitmap
, const wxString
& name
, long WXUNUSED(flags
),
305 int desiredWidth
, int desiredHeight
)
308 icon
.LoadFile( name
, wxBITMAP_TYPE_ICON_RESOURCE
, desiredWidth
, desiredHeight
) ;
309 bitmap
->CopyFromIcon( icon
) ;
311 return bitmap
->Ok() ;