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
;
206 else if ( filename
== wxT("wxICON_CDROM") )
208 theId
= kGenericCDROMIcon
;
210 else if ( filename
== wxT("wxICON_FLOPPY") )
212 theId
= kGenericFloppyIcon
;
214 else if ( filename
== wxT("wxICON_HARDDISK") )
216 theId
= kGenericHardDiskIcon
;
218 else if ( filename
== wxT("wxICON_REMOVABLE") )
220 theId
= kGenericRemovableMediaIcon
;
222 else if ( filename
== wxT("wxICON_DELETE") )
224 theId
= kToolbarDeleteIcon
;
226 else if ( filename
== wxT("wxICON_GO_BACK") )
228 theId
= kBackwardArrowIcon
;
230 else if ( filename
== wxT("wxICON_GO_FORWARD") )
232 theId
= kForwardArrowIcon
;
234 else if ( filename
== wxT("wxICON_GO_HOME") )
236 theId
= kToolbarHomeIcon
;
238 else if ( filename
== wxT("wxICON_HELP_SETTINGS") )
240 theId
= kGenericFontIcon
;
242 else if ( filename
== wxT("wxICON_HELP_PAGE") )
244 theId
= kGenericDocumentIcon
;
248 IconRef iconRef
= NULL
;
250 // first look in the resource fork
251 if ( iconRef
== NULL
)
255 wxMacStringToPascal( filename
, theName
) ;
256 Handle resHandle
= GetNamedResource( 'icns' , theName
) ;
257 if ( resHandle
!= 0L )
259 IconFamilyHandle iconFamily
= (IconFamilyHandle
) resHandle
;
260 HLock((Handle
) iconFamily
);
261 OSStatus err
= GetIconRefFromIconFamilyPtr( *iconFamily
, GetHandleSize((Handle
) iconFamily
), &iconRef
);
262 HUnlock((Handle
) iconFamily
);
265 wxFAIL_MSG("Error when constructing icon ref");
268 ReleaseResource( resHandle
) ;
271 if ( iconRef
== NULL
)
273 // TODO add other attempts to load it from files etc here
277 m_refData
= new wxIconRefData( (WXHICON
) iconRef
, desiredWidth
, desiredHeight
) ;
284 IconRef iconRef
= NULL
;
285 verify_noerr( GetIconRef( kOnSystemDisk
, kSystemIconsCreator
, theId
, &iconRef
) ) ;
288 m_refData
= new wxIconRefData( (WXHICON
) iconRef
, desiredWidth
, desiredHeight
) ;
298 wxBitmapHandler
*handler
= wxBitmap::FindHandler( type
);
303 if ( handler
->LoadFile( &bmp
, filename
, type
, desiredWidth
, desiredHeight
))
305 CopyFromBitmap( bmp
) ;
315 wxImage
loadimage( filename
, type
);
318 if ( desiredWidth
== -1 )
319 desiredWidth
= loadimage
.GetWidth() ;
320 if ( desiredHeight
== -1 )
321 desiredHeight
= loadimage
.GetHeight() ;
322 if ( desiredWidth
!= loadimage
.GetWidth() || desiredHeight
!= loadimage
.GetHeight() )
323 loadimage
.Rescale( desiredWidth
, desiredHeight
) ;
325 wxBitmap
bmp( loadimage
);
326 CopyFromBitmap( bmp
) ;
336 void wxIcon::CopyFromBitmap( const wxBitmap
& bmp
)
340 // as the bitmap owns that ref, we have to acquire it as well
341 IconRef iconRef
= bmp
.CreateIconRef() ;
342 m_refData
= new wxIconRefData( (WXHICON
) iconRef
, bmp
.GetWidth(), bmp
.GetHeight() ) ;
345 IMPLEMENT_DYNAMIC_CLASS(wxICONResourceHandler
, wxBitmapHandler
)
347 bool wxICONResourceHandler::LoadFile(
348 wxBitmap
*bitmap
, const wxString
& name
, wxBitmapType
WXUNUSED(flags
),
349 int desiredWidth
, int desiredHeight
)
352 icon
.LoadFile( name
, wxBITMAP_TYPE_ICON_RESOURCE
, desiredWidth
, desiredHeight
) ;
353 bitmap
->CopyFromIcon( icon
) ;
355 return bitmap
->Ok() ;