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"
14 #if wxOSX_USE_COCOA_OR_CARBON
22 #include "wx/osx/private.h"
24 IMPLEMENT_DYNAMIC_CLASS(wxIcon
, wxGDIObject
)
26 #define M_ICONDATA ((wxIconRefData *)m_refData)
28 class WXDLLEXPORT wxIconRefData
: public wxGDIRefData
31 wxIconRefData() { Init(); }
32 wxIconRefData( WXHICON iconref
, int desiredWidth
, int desiredHeight
);
33 virtual ~wxIconRefData() { Free(); }
35 virtual bool IsOk() const { return m_iconRef
!= NULL
; }
39 void SetWidth( int width
) { m_width
= width
; }
40 void SetHeight( int height
) { m_height
= height
; }
42 int GetWidth() const { return m_width
; }
43 int GetHeight() const { return m_height
; }
45 WXHICON
GetHICON() const { return (WXHICON
) m_iconRef
; }
56 wxIconRefData::wxIconRefData( WXHICON icon
, int desiredWidth
, int desiredHeight
)
58 m_iconRef
= (IconRef
)( icon
) ;
61 SetWidth( desiredWidth
== -1 ? 32 : desiredWidth
) ;
62 SetHeight( desiredHeight
== -1 ? 32 : desiredHeight
) ;
65 void wxIconRefData::Init()
72 void wxIconRefData::Free()
76 ReleaseIconRef( m_iconRef
) ;
89 wxIcon::wxIcon( const char bits
[], int width
, int height
)
91 wxBitmap
bmp( bits
, width
, height
) ;
92 CopyFromBitmap( bmp
) ;
95 wxIcon::wxIcon(const char* const* bits
)
97 wxBitmap
bmp( bits
) ;
98 CopyFromBitmap( bmp
) ;
102 const wxString
& icon_file
, wxBitmapType flags
,
103 int desiredWidth
, int desiredHeight
)
105 LoadFile( icon_file
, flags
, desiredWidth
, desiredHeight
);
108 wxIcon::wxIcon(WXHICON icon
, const wxSize
& size
)
111 // as the icon owns that ref, we have to acquire it as well
113 AcquireIconRef( (IconRef
) icon
) ;
115 m_refData
= new wxIconRefData( icon
, size
.x
, size
.y
) ;
122 wxGDIRefData
*wxIcon::CreateGDIRefData() const
124 return new wxIconRefData
;
127 wxGDIRefData
*wxIcon::CloneGDIRefData(const wxGDIRefData
*data
) const
129 return new wxIconRefData(*static_cast<const wxIconRefData
*>(data
));
132 WXHICON
wxIcon::GetHICON() const
136 return (WXHICON
) ((wxIconRefData
*)m_refData
)->GetHICON() ;
139 int wxIcon::GetWidth() const
141 wxCHECK_MSG( Ok(), -1, wxT("invalid icon") );
143 return M_ICONDATA
->GetWidth();
146 int wxIcon::GetHeight() const
148 wxCHECK_MSG( Ok(), -1, wxT("invalid icon") );
150 return M_ICONDATA
->GetHeight();
153 int wxIcon::GetDepth() const
158 void wxIcon::SetDepth( int WXUNUSED(depth
) )
162 void wxIcon::SetWidth( int WXUNUSED(width
) )
166 void wxIcon::SetHeight( int WXUNUSED(height
) )
170 bool wxIcon::LoadFile(
171 const wxString
& filename
, wxBitmapType type
,
172 int desiredWidth
, int desiredHeight
)
176 if ( type
== wxBITMAP_TYPE_ICON_RESOURCE
)
180 if ( filename
== wxT("wxICON_INFORMATION") )
182 theId
= kAlertNoteIcon
;
184 else if ( filename
== wxT("wxICON_QUESTION") )
186 theId
= kAlertCautionIcon
;
188 else if ( filename
== wxT("wxICON_WARNING") )
190 theId
= kAlertCautionIcon
;
192 else if ( filename
== wxT("wxICON_ERROR") )
194 theId
= kAlertStopIcon
;
196 else if ( filename
== wxT("wxICON_FOLDER") )
198 theId
= kGenericFolderIcon
;
200 else if ( filename
== wxT("wxICON_FOLDER_OPEN") )
202 theId
= kOpenFolderIcon
;
204 else if ( filename
== wxT("wxICON_NORMAL_FILE") )
206 theId
= kGenericDocumentIcon
;
208 else if ( filename
== wxT("wxICON_EXECUTABLE_FILE") )
210 theId
= kGenericApplicationIcon
;
212 else if ( filename
== wxT("wxICON_CDROM") )
214 theId
= kGenericCDROMIcon
;
216 else if ( filename
== wxT("wxICON_FLOPPY") )
218 theId
= kGenericFloppyIcon
;
220 else if ( filename
== wxT("wxICON_HARDDISK") )
222 theId
= kGenericHardDiskIcon
;
224 else if ( filename
== wxT("wxICON_REMOVABLE") )
226 theId
= kGenericRemovableMediaIcon
;
228 else if ( filename
== wxT("wxICON_DELETE") )
230 theId
= kToolbarDeleteIcon
;
232 else if ( filename
== wxT("wxICON_GO_BACK") )
234 theId
= kBackwardArrowIcon
;
236 else if ( filename
== wxT("wxICON_GO_FORWARD") )
238 theId
= kForwardArrowIcon
;
240 else if ( filename
== wxT("wxICON_GO_HOME") )
242 theId
= kToolbarHomeIcon
;
244 else if ( filename
== wxT("wxICON_HELP_SETTINGS") )
246 theId
= kGenericFontIcon
;
248 else if ( filename
== wxT("wxICON_HELP_PAGE") )
250 theId
= kGenericDocumentIcon
;
254 IconRef iconRef
= NULL
;
256 // first look in the resource fork
257 if ( iconRef
== NULL
)
261 wxMacStringToPascal( filename
, theName
) ;
262 Handle resHandle
= GetNamedResource( 'icns' , theName
) ;
263 if ( resHandle
!= 0L )
265 IconFamilyHandle iconFamily
= (IconFamilyHandle
) resHandle
;
266 HLock((Handle
) iconFamily
);
267 OSStatus err
= GetIconRefFromIconFamilyPtr( *iconFamily
, GetHandleSize((Handle
) iconFamily
), &iconRef
);
268 HUnlock((Handle
) iconFamily
);
271 wxFAIL_MSG("Error when constructing icon ref");
274 ReleaseResource( resHandle
) ;
277 if ( iconRef
== NULL
)
279 // TODO add other attempts to load it from files etc here
283 m_refData
= new wxIconRefData( (WXHICON
) iconRef
, desiredWidth
, desiredHeight
) ;
290 IconRef iconRef
= NULL
;
291 verify_noerr( GetIconRef( kOnSystemDisk
, kSystemIconsCreator
, theId
, &iconRef
) ) ;
294 m_refData
= new wxIconRefData( (WXHICON
) iconRef
, desiredWidth
, desiredHeight
) ;
304 wxBitmapHandler
*handler
= wxBitmap::FindHandler( type
);
309 if ( handler
->LoadFile( &bmp
, filename
, type
, desiredWidth
, desiredHeight
))
311 CopyFromBitmap( bmp
) ;
321 wxImage
loadimage( filename
, type
);
324 if ( desiredWidth
== -1 )
325 desiredWidth
= loadimage
.GetWidth() ;
326 if ( desiredHeight
== -1 )
327 desiredHeight
= loadimage
.GetHeight() ;
328 if ( desiredWidth
!= loadimage
.GetWidth() || desiredHeight
!= loadimage
.GetHeight() )
329 loadimage
.Rescale( desiredWidth
, desiredHeight
) ;
331 wxBitmap
bmp( loadimage
);
332 CopyFromBitmap( bmp
) ;
342 void wxIcon::CopyFromBitmap( const wxBitmap
& bmp
)
346 // as the bitmap owns that ref, we have to acquire it as well
347 IconRef iconRef
= bmp
.CreateIconRef() ;
348 m_refData
= new wxIconRefData( (WXHICON
) iconRef
, bmp
.GetWidth(), bmp
.GetHeight() ) ;
351 IMPLEMENT_DYNAMIC_CLASS(wxICONResourceHandler
, wxBitmapHandler
)
353 bool wxICONResourceHandler::LoadFile(
354 wxBitmap
*bitmap
, const wxString
& name
, wxBitmapType
WXUNUSED(flags
),
355 int desiredWidth
, int desiredHeight
)
358 if ( icon
.LoadFile( name
, wxBITMAP_TYPE_ICON_RESOURCE
, desiredWidth
, desiredHeight
) )
360 bitmap
->CopyFromIcon( icon
) ;
361 return bitmap
->Ok() ;