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)
31 wxIcon::wxIcon( const char bits
[], int width
, int height
)
33 wxBitmap
bmp( bits
, width
, height
) ;
34 CopyFromBitmap( bmp
) ;
37 wxIcon::wxIcon( const char **bits
)
39 wxBitmap
bmp( bits
) ;
40 CopyFromBitmap( bmp
) ;
43 wxIcon::wxIcon( char **bits
)
45 wxBitmap
bmp( bits
) ;
46 CopyFromBitmap( bmp
) ;
50 const wxString
& icon_file
, int flags
,
51 int desiredWidth
, int desiredHeight
)
53 LoadFile( icon_file
, (wxBitmapType
) flags
, desiredWidth
, desiredHeight
);
56 wxIcon::wxIcon(WXHICON icon
, const wxSize
& size
)
59 // as the icon owns that ref, we have to acquire it as well
61 AcquireIconRef( (IconRef
) icon
) ;
63 m_refData
= new wxIconRefData( icon
) ;
64 M_ICONDATA
->SetWidth( size
.x
) ;
65 M_ICONDATA
->SetHeight( size
.y
) ;
72 WXHICON
wxIcon::GetHICON() const
76 return (WXHICON
) ((wxIconRefData
*)m_refData
)->GetHICON() ;
79 int wxIcon::GetWidth() const
81 wxCHECK_MSG( Ok(), -1, wxT("invalid icon") );
83 return M_ICONDATA
->GetWidth();
86 int wxIcon::GetHeight() const
88 wxCHECK_MSG( Ok(), -1, wxT("invalid icon") );
90 return M_ICONDATA
->GetHeight();
93 int wxIcon::GetDepth() const
98 void wxIcon::SetDepth( int WXUNUSED(depth
) )
102 void wxIcon::SetWidth( int WXUNUSED(width
) )
106 void wxIcon::SetHeight( int WXUNUSED(height
) )
110 bool wxIcon::IsOk() const
112 return m_refData
!= NULL
;
115 bool wxIcon::LoadFile(
116 const wxString
& filename
, wxBitmapType type
,
117 int desiredWidth
, int desiredHeight
)
121 if ( type
== wxBITMAP_TYPE_ICON_RESOURCE
)
125 if ( filename
== wxT("wxICON_INFORMATION") )
127 theId
= kAlertNoteIcon
;
129 else if ( filename
== wxT("wxICON_QUESTION") )
131 theId
= kAlertCautionIcon
;
133 else if ( filename
== wxT("wxICON_WARNING") )
135 theId
= kAlertCautionIcon
;
137 else if ( filename
== wxT("wxICON_ERROR") )
139 theId
= kAlertStopIcon
;
141 else if ( filename
== wxT("wxICON_FOLDER") )
143 theId
= kGenericFolderIcon
;
145 else if ( filename
== wxT("wxICON_FOLDER_OPEN") )
147 theId
= kOpenFolderIcon
;
149 else if ( filename
== wxT("wxICON_NORMAL_FILE") )
151 theId
= kGenericDocumentIcon
;
155 IconRef iconRef
= NULL
;
157 // first look in the resource fork
158 if ( iconRef
== NULL
)
162 wxMacStringToPascal( filename
, theName
) ;
163 Handle resHandle
= GetNamedResource( 'icns' , theName
) ;
164 if ( resHandle
!= 0L )
166 IconFamilyHandle iconFamily
= (IconFamilyHandle
) resHandle
;
167 HLock((Handle
) iconFamily
);
168 OSStatus err
= GetIconRefFromIconFamilyPtr( *iconFamily
, GetHandleSize((Handle
) iconFamily
), &iconRef
);
169 HUnlock((Handle
) iconFamily
);
170 wxASSERT_MSG( err
== noErr
, wxT("Error when constructing icon ref") );
171 ReleaseResource( resHandle
) ;
174 if ( iconRef
== NULL
)
176 // TODO add other attempts to load it from files etc here
180 m_refData
= new wxIconRefData( (WXHICON
) iconRef
) ;
187 IconRef iconRef
= NULL
;
188 verify_noerr( GetIconRef( kOnSystemDisk
, kSystemIconsCreator
, theId
, &iconRef
) ) ;
191 m_refData
= new wxIconRefData( (WXHICON
) iconRef
) ;
201 wxBitmapHandler
*handler
= wxBitmap::FindHandler( type
);
206 if ( handler
->LoadFile( &bmp
, filename
, type
, desiredWidth
, desiredHeight
))
208 CopyFromBitmap( bmp
) ;
218 wxImage
loadimage( filename
, type
);
221 if ( desiredWidth
== -1 )
222 desiredWidth
= loadimage
.GetWidth() ;
223 if ( desiredHeight
== -1 )
224 desiredHeight
= loadimage
.GetHeight() ;
225 if ( desiredWidth
!= loadimage
.GetWidth() || desiredHeight
!= loadimage
.GetHeight() )
226 loadimage
.Rescale( desiredWidth
, desiredHeight
) ;
228 wxBitmap
bmp( loadimage
);
229 CopyFromBitmap( bmp
) ;
239 void wxIcon::CopyFromBitmap( const wxBitmap
& bmp
)
243 // as the bitmap owns that ref, we have to acquire it as well
244 IconRef iconRef
= bmp
.GetBitmapData()->GetIconRef() ;
245 AcquireIconRef( iconRef
) ;
247 m_refData
= new wxIconRefData( (WXHICON
) iconRef
) ;
248 M_ICONDATA
->SetWidth( bmp
.GetWidth() ) ;
249 M_ICONDATA
->SetHeight( bmp
.GetHeight() ) ;
252 wxIconRefData::wxIconRefData( WXHICON icon
)
254 m_iconRef
= MAC_WXHICON( icon
) ;
261 void wxIconRefData::Init()
266 void wxIconRefData::Free()
270 ReleaseIconRef( m_iconRef
) ;
275 IMPLEMENT_DYNAMIC_CLASS(wxICONResourceHandler
, wxBitmapHandler
)
277 bool wxICONResourceHandler::LoadFile(
278 wxBitmap
*bitmap
, const wxString
& name
, long WXUNUSED(flags
),
279 int desiredWidth
, int desiredHeight
)
282 icon
.LoadFile( name
, wxBITMAP_TYPE_ICON_RESOURCE
, desiredWidth
, desiredHeight
) ;
283 bitmap
->CopyFromIcon( icon
) ;
285 return bitmap
->Ok() ;