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 **bits
)
95 wxBitmap
bmp( bits
) ;
96 CopyFromBitmap( bmp
) ;
99 wxIcon::wxIcon( char **bits
)
101 wxBitmap
bmp( bits
) ;
102 CopyFromBitmap( bmp
) ;
106 const wxString
& icon_file
, int flags
,
107 int desiredWidth
, int desiredHeight
)
109 LoadFile( icon_file
, (wxBitmapType
) flags
, desiredWidth
, desiredHeight
);
112 wxIcon::wxIcon(WXHICON icon
, const wxSize
& size
)
115 // as the icon owns that ref, we have to acquire it as well
117 AcquireIconRef( (IconRef
) icon
) ;
119 m_refData
= new wxIconRefData( icon
, size
.x
, size
.y
) ;
126 wxGDIRefData
*wxIcon::CreateGDIRefData() const
128 return new wxIconRefData
;
131 wxGDIRefData
*wxIcon::CloneGDIRefData(const wxGDIRefData
*data
) const
133 return new wxIconRefData(*wx_static_cast(const wxIconRefData
*, data
));
136 WXHICON
wxIcon::GetHICON() const
140 return (WXHICON
) ((wxIconRefData
*)m_refData
)->GetHICON() ;
143 int wxIcon::GetWidth() const
145 wxCHECK_MSG( Ok(), -1, wxT("invalid icon") );
147 return M_ICONDATA
->GetWidth();
150 int wxIcon::GetHeight() const
152 wxCHECK_MSG( Ok(), -1, wxT("invalid icon") );
154 return M_ICONDATA
->GetHeight();
157 int wxIcon::GetDepth() const
162 void wxIcon::SetDepth( int WXUNUSED(depth
) )
166 void wxIcon::SetWidth( int WXUNUSED(width
) )
170 void wxIcon::SetHeight( int WXUNUSED(height
) )
174 bool wxIcon::LoadFile(
175 const wxString
& filename
, wxBitmapType type
,
176 int desiredWidth
, int desiredHeight
)
180 if ( type
== wxBITMAP_TYPE_ICON_RESOURCE
)
184 if ( filename
== wxT("wxICON_INFORMATION") )
186 theId
= kAlertNoteIcon
;
188 else if ( filename
== wxT("wxICON_QUESTION") )
190 theId
= kAlertCautionIcon
;
192 else if ( filename
== wxT("wxICON_WARNING") )
194 theId
= kAlertCautionIcon
;
196 else if ( filename
== wxT("wxICON_ERROR") )
198 theId
= kAlertStopIcon
;
200 else if ( filename
== wxT("wxICON_FOLDER") )
202 theId
= kGenericFolderIcon
;
204 else if ( filename
== wxT("wxICON_FOLDER_OPEN") )
206 theId
= kOpenFolderIcon
;
208 else if ( filename
== wxT("wxICON_NORMAL_FILE") )
210 theId
= kGenericDocumentIcon
;
214 IconRef iconRef
= NULL
;
216 // first look in the resource fork
217 if ( iconRef
== NULL
)
221 wxMacStringToPascal( filename
, theName
) ;
222 Handle resHandle
= GetNamedResource( 'icns' , theName
) ;
223 if ( resHandle
!= 0L )
225 IconFamilyHandle iconFamily
= (IconFamilyHandle
) resHandle
;
226 HLock((Handle
) iconFamily
);
227 OSStatus err
= GetIconRefFromIconFamilyPtr( *iconFamily
, GetHandleSize((Handle
) iconFamily
), &iconRef
);
228 HUnlock((Handle
) iconFamily
);
229 wxASSERT_MSG( err
== noErr
, wxT("Error when constructing icon ref") );
230 ReleaseResource( resHandle
) ;
233 if ( iconRef
== NULL
)
235 // TODO add other attempts to load it from files etc here
239 m_refData
= new wxIconRefData( (WXHICON
) iconRef
, desiredWidth
, desiredHeight
) ;
246 IconRef iconRef
= NULL
;
247 verify_noerr( GetIconRef( kOnSystemDisk
, kSystemIconsCreator
, theId
, &iconRef
) ) ;
250 m_refData
= new wxIconRefData( (WXHICON
) iconRef
, desiredWidth
, desiredHeight
) ;
260 wxBitmapHandler
*handler
= wxBitmap::FindHandler( type
);
265 if ( handler
->LoadFile( &bmp
, filename
, type
, desiredWidth
, desiredHeight
))
267 CopyFromBitmap( bmp
) ;
277 wxImage
loadimage( filename
, type
);
280 if ( desiredWidth
== -1 )
281 desiredWidth
= loadimage
.GetWidth() ;
282 if ( desiredHeight
== -1 )
283 desiredHeight
= loadimage
.GetHeight() ;
284 if ( desiredWidth
!= loadimage
.GetWidth() || desiredHeight
!= loadimage
.GetHeight() )
285 loadimage
.Rescale( desiredWidth
, desiredHeight
) ;
287 wxBitmap
bmp( loadimage
);
288 CopyFromBitmap( bmp
) ;
298 void wxIcon::CopyFromBitmap( const wxBitmap
& bmp
)
302 // as the bitmap owns that ref, we have to acquire it as well
303 IconRef iconRef
= bmp
.CreateIconRef() ;
304 m_refData
= new wxIconRefData( (WXHICON
) iconRef
, bmp
.GetWidth(), bmp
.GetHeight() ) ;
307 IMPLEMENT_DYNAMIC_CLASS(wxICONResourceHandler
, wxBitmapHandler
)
309 bool wxICONResourceHandler::LoadFile(
310 wxBitmap
*bitmap
, const wxString
& name
, long WXUNUSED(flags
),
311 int desiredWidth
, int desiredHeight
)
314 icon
.LoadFile( name
, wxBITMAP_TYPE_ICON_RESOURCE
, desiredWidth
, desiredHeight
) ;
315 bitmap
->CopyFromIcon( icon
) ;
317 return bitmap
->Ok() ;