]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/icon.cpp
7084b59cfcd6f6e2a6f545c18f3c577fb1cf75a0
[wxWidgets.git] / src / mac / carbon / icon.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/icon.cpp
3 // Purpose: wxIcon class
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #include "wx/icon.h"
15
16 #ifndef WX_PRECOMP
17 #include "wx/image.h"
18 #endif
19
20 #include "wx/mac/private.h"
21
22 IMPLEMENT_DYNAMIC_CLASS(wxIcon, wxBitmap)
23
24 #define M_ICONDATA ((wxIconRefData *)m_refData)
25
26
27 wxIcon::wxIcon()
28 {
29 }
30
31 wxIcon::wxIcon( const char bits[], int width, int height )
32 {
33 wxBitmap bmp( bits, width, height ) ;
34 CopyFromBitmap( bmp ) ;
35 }
36
37 wxIcon::wxIcon( const char **bits )
38 {
39 wxBitmap bmp( bits ) ;
40 CopyFromBitmap( bmp ) ;
41 }
42
43 wxIcon::wxIcon( char **bits )
44 {
45 wxBitmap bmp( bits ) ;
46 CopyFromBitmap( bmp ) ;
47 }
48
49 wxIcon::wxIcon(
50 const wxString& icon_file, int flags,
51 int desiredWidth, int desiredHeight )
52 {
53 LoadFile( icon_file, (wxBitmapType) flags, desiredWidth, desiredHeight );
54 }
55
56 wxIcon::~wxIcon()
57 {
58 }
59
60 WXHICON wxIcon::GetHICON() const
61 {
62 wxASSERT( Ok() ) ;
63
64 return (WXHICON) ((wxIconRefData*)m_refData)->GetHICON() ;
65 }
66
67 int wxIcon::GetWidth() const
68 {
69 wxCHECK_MSG( Ok(), -1, wxT("invalid icon") );
70
71 return M_ICONDATA->GetWidth();
72 }
73
74 int wxIcon::GetHeight() const
75 {
76 wxCHECK_MSG( Ok(), -1, wxT("invalid icon") );
77
78 return M_ICONDATA->GetHeight();
79 }
80
81 int wxIcon::GetDepth() const
82 {
83 return 32;
84 }
85
86 void wxIcon::SetDepth( int depth )
87 {
88 }
89
90 void wxIcon::SetWidth( int width )
91 {
92 }
93
94 void wxIcon::SetHeight( int height )
95 {
96 }
97
98 bool wxIcon::IsOk() const
99 {
100 return m_refData != NULL ;
101 }
102
103 bool wxIcon::LoadFile(
104 const wxString& filename, wxBitmapType type,
105 int desiredWidth, int desiredHeight )
106 {
107 UnRef();
108
109 if ( type == wxBITMAP_TYPE_ICON_RESOURCE )
110 {
111 OSType theId = 0 ;
112
113 if ( filename == wxT("wxICON_INFORMATION") )
114 {
115 theId = kAlertNoteIcon ;
116 }
117 else if ( filename == wxT("wxICON_QUESTION") )
118 {
119 theId = kAlertCautionIcon ;
120 }
121 else if ( filename == wxT("wxICON_WARNING") )
122 {
123 theId = kAlertCautionIcon ;
124 }
125 else if ( filename == wxT("wxICON_ERROR") )
126 {
127 theId = kAlertStopIcon ;
128 }
129 else
130 {
131 #if 0
132 Str255 theName ;
133 OSType theType ;
134 wxMacStringToPascal( name , theName ) ;
135
136 Handle resHandle = GetNamedResource( 'cicn' , theName ) ;
137 if ( resHandle != 0L )
138 {
139 GetResInfo( resHandle , &theId , &theType , theName ) ;
140 ReleaseResource( resHandle ) ;
141 }
142 #endif
143 }
144
145 if ( theId != 0 )
146 {
147 IconRef iconRef = NULL ;
148 verify_noerr( GetIconRef( kOnSystemDisk, kSystemIconsCreator, theId, &iconRef ) ) ;
149 if ( iconRef )
150 {
151 m_refData = new wxIconRefData( (WXHICON) iconRef ) ;
152
153 return true ;
154 }
155 }
156
157 return false ;
158 }
159 else
160 {
161 wxBitmapHandler *handler = wxBitmap::FindHandler( type );
162
163 if ( handler )
164 {
165 wxBitmap bmp ;
166 if ( handler->LoadFile( &bmp , filename, type, desiredWidth, desiredHeight ))
167 {
168 CopyFromBitmap( bmp ) ;
169
170 return true ;
171 }
172
173 return false ;
174 }
175 else
176 {
177 #if wxUSE_IMAGE
178 wxImage loadimage( filename, type );
179 if (loadimage.Ok())
180 {
181 if ( desiredWidth == -1 )
182 desiredWidth = loadimage.GetWidth() ;
183 if ( desiredHeight == -1 )
184 desiredHeight = loadimage.GetHeight() ;
185 if ( desiredWidth != loadimage.GetWidth() || desiredHeight != loadimage.GetHeight() )
186 loadimage.Rescale( desiredWidth , desiredHeight ) ;
187
188 wxBitmap bmp( loadimage );
189 CopyFromBitmap( bmp ) ;
190
191 return true;
192 }
193 #endif
194 }
195 }
196 return true ;
197 }
198
199 void wxIcon::CopyFromBitmap( const wxBitmap& bmp )
200 {
201 UnRef() ;
202
203 // as the bitmap owns that ref, we have to acquire it as well
204 IconRef iconRef = bmp.GetBitmapData()->GetIconRef() ;
205 AcquireIconRef( iconRef ) ;
206
207 m_refData = new wxIconRefData( (WXHICON) iconRef ) ;
208 M_ICONDATA->SetWidth( bmp.GetWidth() ) ;
209 M_ICONDATA->SetHeight( bmp.GetHeight() ) ;
210 }
211
212 wxIconRefData::wxIconRefData( WXHICON icon )
213 {
214 m_iconRef = MAC_WXHICON( icon ) ;
215
216 // Standard sizes
217 SetWidth( 32 ) ;
218 SetHeight( 32 ) ;
219 }
220
221 void wxIconRefData::Init()
222 {
223 m_iconRef = NULL ;
224 }
225
226 void wxIconRefData::Free()
227 {
228 if ( m_iconRef )
229 {
230 ReleaseIconRef( m_iconRef ) ;
231 m_iconRef = NULL ;
232 }
233 }
234
235 IMPLEMENT_DYNAMIC_CLASS(wxICONResourceHandler, wxBitmapHandler)
236
237 bool wxICONResourceHandler::LoadFile(
238 wxBitmap *bitmap, const wxString& name, long flags,
239 int desiredWidth, int desiredHeight )
240 {
241 wxIcon icon ;
242 icon.LoadFile( name , wxBITMAP_TYPE_ICON_RESOURCE , desiredWidth , desiredHeight ) ;
243 bitmap->CopyFromIcon( icon ) ;
244
245 return bitmap->Ok() ;
246 }
247