1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/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/osx/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
= (IconRef
)( 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
, wxBitmapType flags
,
101 int desiredWidth
, int desiredHeight
)
103 LoadFile( icon_file
, 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(*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
);
225 wxFAIL_MSG("Error when constructing icon ref");
228 ReleaseResource( resHandle
) ;
231 if ( iconRef
== NULL
)
233 // TODO add other attempts to load it from files etc here
237 m_refData
= new wxIconRefData( (WXHICON
) iconRef
, desiredWidth
, desiredHeight
) ;
244 IconRef iconRef
= NULL
;
245 verify_noerr( GetIconRef( kOnSystemDisk
, kSystemIconsCreator
, theId
, &iconRef
) ) ;
248 m_refData
= new wxIconRefData( (WXHICON
) iconRef
, desiredWidth
, desiredHeight
) ;
258 wxBitmapHandler
*handler
= wxBitmap::FindHandler( type
);
263 if ( handler
->LoadFile( &bmp
, filename
, type
, desiredWidth
, desiredHeight
))
265 CopyFromBitmap( bmp
) ;
275 wxImage
loadimage( filename
, type
);
278 if ( desiredWidth
== -1 )
279 desiredWidth
= loadimage
.GetWidth() ;
280 if ( desiredHeight
== -1 )
281 desiredHeight
= loadimage
.GetHeight() ;
282 if ( desiredWidth
!= loadimage
.GetWidth() || desiredHeight
!= loadimage
.GetHeight() )
283 loadimage
.Rescale( desiredWidth
, desiredHeight
) ;
285 wxBitmap
bmp( loadimage
);
286 CopyFromBitmap( bmp
) ;
296 void wxIcon::CopyFromBitmap( const wxBitmap
& bmp
)
300 // as the bitmap owns that ref, we have to acquire it as well
301 IconRef iconRef
= bmp
.CreateIconRef() ;
302 m_refData
= new wxIconRefData( (WXHICON
) iconRef
, bmp
.GetWidth(), bmp
.GetHeight() ) ;
305 IMPLEMENT_DYNAMIC_CLASS(wxICONResourceHandler
, wxBitmapHandler
)
307 bool wxICONResourceHandler::LoadFile(
308 wxBitmap
*bitmap
, const wxString
& name
, wxBitmapType
WXUNUSED(flags
),
309 int desiredWidth
, int desiredHeight
)
312 icon
.LoadFile( name
, wxBITMAP_TYPE_ICON_RESOURCE
, desiredWidth
, desiredHeight
) ;
313 bitmap
->CopyFromIcon( icon
) ;
315 return bitmap
->Ok() ;