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
;
158 wxMacStringToPascal( name
, theName
) ;
160 Handle resHandle
= GetNamedResource( 'cicn' , theName
) ;
161 if ( resHandle
!= 0L )
163 GetResInfo( resHandle
, &theId
, &theType
, theName
) ;
164 ReleaseResource( resHandle
) ;
171 IconRef iconRef
= NULL
;
172 verify_noerr( GetIconRef( kOnSystemDisk
, kSystemIconsCreator
, theId
, &iconRef
) ) ;
175 m_refData
= new wxIconRefData( (WXHICON
) iconRef
) ;
185 wxBitmapHandler
*handler
= wxBitmap::FindHandler( type
);
190 if ( handler
->LoadFile( &bmp
, filename
, type
, desiredWidth
, desiredHeight
))
192 CopyFromBitmap( bmp
) ;
202 wxImage
loadimage( filename
, type
);
205 if ( desiredWidth
== -1 )
206 desiredWidth
= loadimage
.GetWidth() ;
207 if ( desiredHeight
== -1 )
208 desiredHeight
= loadimage
.GetHeight() ;
209 if ( desiredWidth
!= loadimage
.GetWidth() || desiredHeight
!= loadimage
.GetHeight() )
210 loadimage
.Rescale( desiredWidth
, desiredHeight
) ;
212 wxBitmap
bmp( loadimage
);
213 CopyFromBitmap( bmp
) ;
223 void wxIcon::CopyFromBitmap( const wxBitmap
& bmp
)
227 // as the bitmap owns that ref, we have to acquire it as well
228 IconRef iconRef
= bmp
.GetBitmapData()->GetIconRef() ;
229 AcquireIconRef( iconRef
) ;
231 m_refData
= new wxIconRefData( (WXHICON
) iconRef
) ;
232 M_ICONDATA
->SetWidth( bmp
.GetWidth() ) ;
233 M_ICONDATA
->SetHeight( bmp
.GetHeight() ) ;
236 wxIconRefData::wxIconRefData( WXHICON icon
)
238 m_iconRef
= MAC_WXHICON( icon
) ;
245 void wxIconRefData::Init()
250 void wxIconRefData::Free()
254 ReleaseIconRef( m_iconRef
) ;
259 IMPLEMENT_DYNAMIC_CLASS(wxICONResourceHandler
, wxBitmapHandler
)
261 bool wxICONResourceHandler::LoadFile(
262 wxBitmap
*bitmap
, const wxString
& name
, long WXUNUSED(flags
),
263 int desiredWidth
, int desiredHeight
)
266 icon
.LoadFile( name
, wxBITMAP_TYPE_ICON_RESOURCE
, desiredWidth
, desiredHeight
) ;
267 bitmap
->CopyFromIcon( icon
) ;
269 return bitmap
->Ok() ;