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") )
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
= kUserFolderIcon
;
238 else if ( filename
== wxT("wxICON_HELP_FOLDER") )
240 theId
= kHelpFolderIcon
;
242 else if ( filename
== wxT("wxICON_HELP_SETTINGS") )
244 theId
= kGenericFontIcon
;
246 else if ( filename
== wxT("wxICON_HELP_PAGE") )
248 theId
= kGenericDocumentIcon
;
250 else if ( filename
== wxT("wxICON_PRINT") )
252 theId
= kPrintMonitorFolderIcon
;
256 IconRef iconRef
= NULL
;
258 // first look in the resource fork
259 if ( iconRef
== NULL
)
263 wxMacStringToPascal( filename
, theName
) ;
264 Handle resHandle
= GetNamedResource( 'icns' , theName
) ;
265 if ( resHandle
!= 0L )
267 IconFamilyHandle iconFamily
= (IconFamilyHandle
) resHandle
;
268 HLock((Handle
) iconFamily
);
269 OSStatus err
= GetIconRefFromIconFamilyPtr( *iconFamily
, GetHandleSize((Handle
) iconFamily
), &iconRef
);
270 HUnlock((Handle
) iconFamily
);
273 wxFAIL_MSG("Error when constructing icon ref");
276 ReleaseResource( resHandle
) ;
279 if ( iconRef
== NULL
)
281 // TODO add other attempts to load it from files etc here
285 m_refData
= new wxIconRefData( (WXHICON
) iconRef
, desiredWidth
, desiredHeight
) ;
292 IconRef iconRef
= NULL
;
293 verify_noerr( GetIconRef( kOnSystemDisk
, kSystemIconsCreator
, theId
, &iconRef
) ) ;
296 m_refData
= new wxIconRefData( (WXHICON
) iconRef
, desiredWidth
, desiredHeight
) ;
306 wxBitmapHandler
*handler
= wxBitmap::FindHandler( type
);
311 if ( handler
->LoadFile( &bmp
, filename
, type
, desiredWidth
, desiredHeight
))
313 CopyFromBitmap( bmp
) ;
323 wxImage
loadimage( filename
, type
);
326 if ( desiredWidth
== -1 )
327 desiredWidth
= loadimage
.GetWidth() ;
328 if ( desiredHeight
== -1 )
329 desiredHeight
= loadimage
.GetHeight() ;
330 if ( desiredWidth
!= loadimage
.GetWidth() || desiredHeight
!= loadimage
.GetHeight() )
331 loadimage
.Rescale( desiredWidth
, desiredHeight
) ;
333 wxBitmap
bmp( loadimage
);
334 CopyFromBitmap( bmp
) ;
344 void wxIcon::CopyFromBitmap( const wxBitmap
& bmp
)
348 // as the bitmap owns that ref, we have to acquire it as well
349 IconRef iconRef
= bmp
.CreateIconRef() ;
350 m_refData
= new wxIconRefData( (WXHICON
) iconRef
, bmp
.GetWidth(), bmp
.GetHeight() ) ;
353 IMPLEMENT_DYNAMIC_CLASS(wxICONResourceHandler
, wxBitmapHandler
)
355 bool wxICONResourceHandler::LoadFile(
356 wxBitmap
*bitmap
, const wxString
& name
, wxBitmapType
WXUNUSED(flags
),
357 int desiredWidth
, int desiredHeight
)
360 icon
.LoadFile( name
, wxBITMAP_TYPE_ICON_RESOURCE
, desiredWidth
, desiredHeight
) ;
361 bitmap
->CopyFromIcon( icon
) ;
363 return bitmap
->Ok() ;