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)
26 class WXDLLEXPORT wxIconRefData
: public wxGDIRefData
30 wxIconRefData( WXHICON iconref
, int desiredWidth
, int desiredHeight
);
31 virtual ~wxIconRefData() { Free(); }
36 void SetWidth( int width
) { m_width
= width
; }
37 void SetHeight( int height
) { m_height
= height
; }
39 int GetWidth() const { return m_width
; }
40 int GetHeight() const { return m_height
; }
42 WXHICON
GetHICON() const { return (WXHICON
) m_iconRef
; }
50 wxIconRefData::wxIconRefData( WXHICON icon
, int desiredWidth
, int desiredHeight
)
52 m_iconRef
= MAC_WXHICON( icon
) ;
55 SetWidth( desiredWidth
== -1 ? 32 : desiredWidth
) ;
56 SetHeight( desiredHeight
== -1 ? 32 : desiredHeight
) ;
59 void wxIconRefData::Init()
64 void wxIconRefData::Free()
68 ReleaseIconRef( m_iconRef
) ;
81 wxIcon::wxIcon( const char bits
[], int width
, int height
)
83 wxBitmap
bmp( bits
, width
, height
) ;
84 CopyFromBitmap( bmp
) ;
87 wxIcon::wxIcon( const char **bits
)
89 wxBitmap
bmp( bits
) ;
90 CopyFromBitmap( bmp
) ;
93 wxIcon::wxIcon( char **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 WXHICON
wxIcon::GetHICON() const
124 return (WXHICON
) ((wxIconRefData
*)m_refData
)->GetHICON() ;
127 int wxIcon::GetWidth() const
129 wxCHECK_MSG( Ok(), -1, wxT("invalid icon") );
131 return M_ICONDATA
->GetWidth();
134 int wxIcon::GetHeight() const
136 wxCHECK_MSG( Ok(), -1, wxT("invalid icon") );
138 return M_ICONDATA
->GetHeight();
141 int wxIcon::GetDepth() const
146 void wxIcon::SetDepth( int WXUNUSED(depth
) )
150 void wxIcon::SetWidth( int WXUNUSED(width
) )
154 void wxIcon::SetHeight( int WXUNUSED(height
) )
158 bool wxIcon::IsOk() const
160 return m_refData
!= NULL
;
163 bool wxIcon::LoadFile(
164 const wxString
& filename
, wxBitmapType type
,
165 int desiredWidth
, int desiredHeight
)
169 if ( type
== wxBITMAP_TYPE_ICON_RESOURCE
)
173 if ( filename
== wxT("wxICON_INFORMATION") )
175 theId
= kAlertNoteIcon
;
177 else if ( filename
== wxT("wxICON_QUESTION") )
179 theId
= kAlertCautionIcon
;
181 else if ( filename
== wxT("wxICON_WARNING") )
183 theId
= kAlertCautionIcon
;
185 else if ( filename
== wxT("wxICON_ERROR") )
187 theId
= kAlertStopIcon
;
189 else if ( filename
== wxT("wxICON_FOLDER") )
191 theId
= kGenericFolderIcon
;
193 else if ( filename
== wxT("wxICON_FOLDER_OPEN") )
195 theId
= kOpenFolderIcon
;
197 else if ( filename
== wxT("wxICON_NORMAL_FILE") )
199 theId
= kGenericDocumentIcon
;
203 IconRef iconRef
= NULL
;
205 // first look in the resource fork
206 if ( iconRef
== NULL
)
210 wxMacStringToPascal( filename
, theName
) ;
211 Handle resHandle
= GetNamedResource( 'icns' , theName
) ;
212 if ( resHandle
!= 0L )
214 IconFamilyHandle iconFamily
= (IconFamilyHandle
) resHandle
;
215 HLock((Handle
) iconFamily
);
216 OSStatus err
= GetIconRefFromIconFamilyPtr( *iconFamily
, GetHandleSize((Handle
) iconFamily
), &iconRef
);
217 HUnlock((Handle
) iconFamily
);
218 wxASSERT_MSG( err
== noErr
, wxT("Error when constructing icon ref") );
219 ReleaseResource( resHandle
) ;
222 if ( iconRef
== NULL
)
224 // TODO add other attempts to load it from files etc here
228 m_refData
= new wxIconRefData( (WXHICON
) iconRef
, desiredWidth
, desiredHeight
) ;
235 IconRef iconRef
= NULL
;
236 verify_noerr( GetIconRef( kOnSystemDisk
, kSystemIconsCreator
, theId
, &iconRef
) ) ;
239 m_refData
= new wxIconRefData( (WXHICON
) iconRef
, desiredWidth
, desiredHeight
) ;
249 wxBitmapHandler
*handler
= wxBitmap::FindHandler( type
);
254 if ( handler
->LoadFile( &bmp
, filename
, type
, desiredWidth
, desiredHeight
))
256 CopyFromBitmap( bmp
) ;
266 wxImage
loadimage( filename
, type
);
269 if ( desiredWidth
== -1 )
270 desiredWidth
= loadimage
.GetWidth() ;
271 if ( desiredHeight
== -1 )
272 desiredHeight
= loadimage
.GetHeight() ;
273 if ( desiredWidth
!= loadimage
.GetWidth() || desiredHeight
!= loadimage
.GetHeight() )
274 loadimage
.Rescale( desiredWidth
, desiredHeight
) ;
276 wxBitmap
bmp( loadimage
);
277 CopyFromBitmap( bmp
) ;
287 void wxIcon::CopyFromBitmap( const wxBitmap
& bmp
)
291 // as the bitmap owns that ref, we have to acquire it as well
292 IconRef iconRef
= bmp
.CreateIconRef() ;
293 m_refData
= new wxIconRefData( (WXHICON
) iconRef
, bmp
.GetWidth(), bmp
.GetHeight() ) ;
296 IMPLEMENT_DYNAMIC_CLASS(wxICONResourceHandler
, wxBitmapHandler
)
298 bool wxICONResourceHandler::LoadFile(
299 wxBitmap
*bitmap
, const wxString
& name
, long WXUNUSED(flags
),
300 int desiredWidth
, int desiredHeight
)
303 icon
.LoadFile( name
, wxBITMAP_TYPE_ICON_RESOURCE
, desiredWidth
, desiredHeight
) ;
304 bitmap
->CopyFromIcon( icon
) ;
306 return bitmap
->Ok() ;