1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxIcon class
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "icon.h"
16 #include "wx/wxprec.h"
20 #if !USE_SHARED_LIBRARIES
21 IMPLEMENT_DYNAMIC_CLASS(wxIcon
, wxBitmap
)
25 #include "wx/mac/private.h"
27 #define M_ICONDATA ((wxIconRefData *)m_refData)
38 wxIcon::wxIcon(const char bits
[], int width
, int height
)
40 wxBitmap
bmp(bits
,width
,height
) ;
41 CopyFromBitmap( bmp
) ;
44 wxIcon::wxIcon( const char **bits
)
47 CopyFromBitmap( bmp
) ;
50 wxIcon::wxIcon( char **bits
)
53 CopyFromBitmap( bmp
) ;
56 wxIcon::wxIcon(const wxString
& icon_file
, int flags
,
57 int desiredWidth
, int desiredHeight
)
59 LoadFile(icon_file
, (wxBitmapType
) flags
, desiredWidth
, desiredHeight
);
66 WXHICON
wxIcon::GetHICON() const
69 return (WXHICON
) ((wxIconRefData
*)m_refData
)->GetHICON() ;
72 int wxIcon::GetWidth() const
74 wxCHECK_MSG( Ok(), -1, wxT("invalid icon") );
76 return M_ICONDATA
->GetWidth();
79 int wxIcon::GetHeight() const
81 wxCHECK_MSG( Ok(), -1, wxT("invalid icon") );
83 return M_ICONDATA
->GetHeight();
86 int wxIcon::GetDepth() const{
90 void wxIcon::SetDepth(int depth
){
94 void wxIcon::SetWidth(int width
){
98 void wxIcon::SetHeight(int height
){
102 bool wxIcon::Ok() const
104 return m_refData
!= NULL
;
107 bool wxIcon::LoadFile(const wxString
& filename
, wxBitmapType type
,
108 int desiredWidth
, int desiredHeight
)
112 if ( type
== wxBITMAP_TYPE_ICON_RESOURCE
)
115 if ( filename
== wxT("wxICON_INFORMATION") )
117 theId
= kAlertNoteIcon
;
119 else if ( filename
== wxT("wxICON_QUESTION") )
121 theId
= kAlertCautionIcon
;
123 else if ( filename
== wxT("wxICON_WARNING") )
125 theId
= kAlertCautionIcon
;
127 else if ( filename
== wxT("wxICON_ERROR") )
129 theId
= kAlertStopIcon
;
135 wxMacStringToPascal( name , theName ) ;
137 Handle resHandle = GetNamedResource( 'cicn' , theName ) ;
138 if ( resHandle != 0L )
140 GetResInfo( resHandle , &theId , &theType , theName ) ;
141 ReleaseResource( resHandle ) ;
147 IconRef iconRef
= NULL
;
148 verify_noerr(GetIconRef(kOnSystemDisk
,kSystemIconsCreator
,theId
, &iconRef
)) ;
151 m_refData
= new wxIconRefData( (WXHICON
) iconRef
) ;
159 wxBitmapHandler
*handler
= wxBitmap::FindHandler(type
);
164 if ( handler
->LoadFile(&bmp
, filename
, type
, desiredWidth
, desiredHeight
))
166 CopyFromBitmap( bmp
) ;
174 wxImage
loadimage(filename
, type
);
177 if ( desiredWidth
== -1 )
178 desiredWidth
= loadimage
.GetWidth() ;
179 if ( desiredHeight
== -1 )
180 desiredHeight
= loadimage
.GetHeight() ;
181 if ( desiredWidth
!= loadimage
.GetWidth() || desiredHeight
!= loadimage
.GetHeight() )
182 loadimage
.Rescale( desiredWidth
, desiredHeight
) ;
183 wxBitmap
bmp( loadimage
);
184 CopyFromBitmap( bmp
) ;
193 void wxIcon::CopyFromBitmap(const wxBitmap
& bmp
)
197 // as the bitmap owns that ref, we have to acquire it as well
198 IconRef iconRef
= bmp
.GetBitmapData()->GetIconRef() ;
199 AcquireIconRef( iconRef
) ;
200 m_refData
= new wxIconRefData( (WXHICON
) iconRef
) ;
201 M_ICONDATA
->SetWidth( bmp
.GetWidth() ) ;
202 M_ICONDATA
->SetHeight( bmp
.GetHeight() ) ;
205 wxIconRefData::wxIconRefData( WXHICON icon
)
207 m_iconRef
= MAC_WXHICON( icon
) ;
213 void wxIconRefData::Init()
218 void wxIconRefData::Free()
222 ReleaseIconRef( m_iconRef
) ;
227 IMPLEMENT_DYNAMIC_CLASS(wxICONResourceHandler
, wxBitmapHandler
)
229 bool wxICONResourceHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
230 int desiredWidth
, int desiredHeight
)
233 icon
.LoadFile( name
, wxBITMAP_TYPE_ICON_RESOURCE
, desiredWidth
, desiredHeight
) ;
234 bitmap
->CopyFromIcon( icon
) ;
235 return bitmap
->Ok() ;