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_EXECUTABLE_FILE") )
208 theId
= kGenericApplicationIcon
;
210 else if ( filename
== wxT("wxICON_CDROM") )
212 theId
= kGenericCDROMIcon
;
214 else if ( filename
== wxT("wxICON_FLOPPY") )
216 theId
= kGenericFloppyIcon
;
218 else if ( filename
== wxT("wxICON_HARDDISK") )
220 theId
= kGenericHardDiskIcon
;
222 else if ( filename
== wxT("wxICON_REMOVABLE") )
224 theId
= kGenericRemovableMediaIcon
;
226 else if ( filename
== wxT("wxICON_DELETE") )
228 theId
= kToolbarDeleteIcon
;
230 else if ( filename
== wxT("wxICON_GO_BACK") )
232 theId
= kBackwardArrowIcon
;
234 else if ( filename
== wxT("wxICON_GO_FORWARD") )
236 theId
= kForwardArrowIcon
;
238 else if ( filename
== wxT("wxICON_GO_HOME") )
240 theId
= kToolbarHomeIcon
;
242 else if ( filename
== wxT("wxICON_HELP_SETTINGS") )
244 theId
= kGenericFontIcon
;
246 else if ( filename
== wxT("wxICON_HELP_PAGE") )
248 theId
= kGenericDocumentIcon
;
252 IconRef iconRef
= NULL
;
254 // first look in the resource fork
255 if ( iconRef
== NULL
)
259 wxMacStringToPascal( filename
, theName
) ;
260 Handle resHandle
= GetNamedResource( 'icns' , theName
) ;
261 if ( resHandle
!= 0L )
263 IconFamilyHandle iconFamily
= (IconFamilyHandle
) resHandle
;
264 HLock((Handle
) iconFamily
);
265 OSStatus err
= GetIconRefFromIconFamilyPtr( *iconFamily
, GetHandleSize((Handle
) iconFamily
), &iconRef
);
266 HUnlock((Handle
) iconFamily
);
269 wxFAIL_MSG("Error when constructing icon ref");
272 ReleaseResource( resHandle
) ;
275 if ( iconRef
== NULL
)
277 // TODO add other attempts to load it from files etc here
281 m_refData
= new wxIconRefData( (WXHICON
) iconRef
, desiredWidth
, desiredHeight
) ;
288 IconRef iconRef
= NULL
;
289 verify_noerr( GetIconRef( kOnSystemDisk
, kSystemIconsCreator
, theId
, &iconRef
) ) ;
292 m_refData
= new wxIconRefData( (WXHICON
) iconRef
, desiredWidth
, desiredHeight
) ;
302 wxBitmapHandler
*handler
= wxBitmap::FindHandler( type
);
307 if ( handler
->LoadFile( &bmp
, filename
, type
, desiredWidth
, desiredHeight
))
309 CopyFromBitmap( bmp
) ;
319 wxImage
loadimage( filename
, type
);
322 if ( desiredWidth
== -1 )
323 desiredWidth
= loadimage
.GetWidth() ;
324 if ( desiredHeight
== -1 )
325 desiredHeight
= loadimage
.GetHeight() ;
326 if ( desiredWidth
!= loadimage
.GetWidth() || desiredHeight
!= loadimage
.GetHeight() )
327 loadimage
.Rescale( desiredWidth
, desiredHeight
) ;
329 wxBitmap
bmp( loadimage
);
330 CopyFromBitmap( bmp
) ;
340 void wxIcon::CopyFromBitmap( const wxBitmap
& bmp
)
344 // as the bitmap owns that ref, we have to acquire it as well
345 IconRef iconRef
= bmp
.CreateIconRef() ;
346 m_refData
= new wxIconRefData( (WXHICON
) iconRef
, bmp
.GetWidth(), bmp
.GetHeight() ) ;
349 IMPLEMENT_DYNAMIC_CLASS(wxICONResourceHandler
, wxBitmapHandler
)
351 bool wxICONResourceHandler::LoadFile(
352 wxBitmap
*bitmap
, const wxString
& name
, wxBitmapType
WXUNUSED(flags
),
353 int desiredWidth
, int desiredHeight
)
356 icon
.LoadFile( name
, wxBITMAP_TYPE_ICON_RESOURCE
, desiredWidth
, desiredHeight
) ;
357 bitmap
->CopyFromIcon( icon
) ;
359 return bitmap
->Ok() ;