]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/icon.cpp
cleanup
[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(WXHICON icon, const wxSize& size)
57 : wxGDIObject()
58 {
59 // as the icon owns that ref, we have to acquire it as well
60 if (icon)
61 AcquireIconRef( (IconRef) icon ) ;
62
63 m_refData = new wxIconRefData( icon ) ;
64 if ( (size.x != -1) && (size.y != -1) )
65 {
66 M_ICONDATA->SetWidth( size.x ) ;
67 M_ICONDATA->SetHeight( size.y ) ;
68 }
69 }
70
71 wxIcon::~wxIcon()
72 {
73 }
74
75 WXHICON wxIcon::GetHICON() const
76 {
77 wxASSERT( Ok() ) ;
78
79 return (WXHICON) ((wxIconRefData*)m_refData)->GetHICON() ;
80 }
81
82 int wxIcon::GetWidth() const
83 {
84 wxCHECK_MSG( Ok(), -1, wxT("invalid icon") );
85
86 return M_ICONDATA->GetWidth();
87 }
88
89 int wxIcon::GetHeight() const
90 {
91 wxCHECK_MSG( Ok(), -1, wxT("invalid icon") );
92
93 return M_ICONDATA->GetHeight();
94 }
95
96 int wxIcon::GetDepth() const
97 {
98 return 32;
99 }
100
101 void wxIcon::SetDepth( int WXUNUSED(depth) )
102 {
103 }
104
105 void wxIcon::SetWidth( int WXUNUSED(width) )
106 {
107 }
108
109 void wxIcon::SetHeight( int WXUNUSED(height) )
110 {
111 }
112
113 bool wxIcon::IsOk() const
114 {
115 return m_refData != NULL ;
116 }
117
118 bool wxIcon::LoadFile(
119 const wxString& filename, wxBitmapType type,
120 int desiredWidth, int desiredHeight )
121 {
122 UnRef();
123
124 if ( type == wxBITMAP_TYPE_ICON_RESOURCE )
125 {
126 OSType theId = 0 ;
127
128 if ( filename == wxT("wxICON_INFORMATION") )
129 {
130 theId = kAlertNoteIcon ;
131 }
132 else if ( filename == wxT("wxICON_QUESTION") )
133 {
134 theId = kAlertCautionIcon ;
135 }
136 else if ( filename == wxT("wxICON_WARNING") )
137 {
138 theId = kAlertCautionIcon ;
139 }
140 else if ( filename == wxT("wxICON_ERROR") )
141 {
142 theId = kAlertStopIcon ;
143 }
144 else if ( filename == wxT("wxICON_FOLDER") )
145 {
146 theId = kGenericFolderIcon ;
147 }
148 else if ( filename == wxT("wxICON_FOLDER_OPEN") )
149 {
150 theId = kOpenFolderIcon ;
151 }
152 else if ( filename == wxT("wxICON_NORMAL_FILE") )
153 {
154 theId = kGenericDocumentIcon ;
155 }
156 else
157 {
158 IconRef iconRef = NULL ;
159
160 // first look in the resource fork
161 if ( iconRef == NULL )
162 {
163 Str255 theName ;
164
165 wxMacStringToPascal( filename , theName ) ;
166 Handle resHandle = GetNamedResource( 'icns' , theName ) ;
167 if ( resHandle != 0L )
168 {
169 IconFamilyHandle iconFamily = (IconFamilyHandle) resHandle ;
170 HLock((Handle) iconFamily);
171 OSStatus err = GetIconRefFromIconFamilyPtr( *iconFamily, GetHandleSize((Handle) iconFamily), &iconRef );
172 HUnlock((Handle) iconFamily);
173 wxASSERT_MSG( err == noErr , wxT("Error when constructing icon ref") );
174 ReleaseResource( resHandle ) ;
175 }
176 }
177 if ( iconRef == NULL )
178 {
179 // TODO add other attempts to load it from files etc here
180 }
181 if ( iconRef )
182 {
183 m_refData = new wxIconRefData( (WXHICON) iconRef ) ;
184 return true ;
185 }
186 }
187
188 if ( theId != 0 )
189 {
190 IconRef iconRef = NULL ;
191 verify_noerr( GetIconRef( kOnSystemDisk, kSystemIconsCreator, theId, &iconRef ) ) ;
192 if ( iconRef )
193 {
194 m_refData = new wxIconRefData( (WXHICON) iconRef ) ;
195
196 return true ;
197 }
198 }
199
200 return false ;
201 }
202 else
203 {
204 wxBitmapHandler *handler = wxBitmap::FindHandler( type );
205
206 if ( handler )
207 {
208 wxBitmap bmp ;
209 if ( handler->LoadFile( &bmp , filename, type, desiredWidth, desiredHeight ))
210 {
211 CopyFromBitmap( bmp ) ;
212
213 return true ;
214 }
215
216 return false ;
217 }
218 else
219 {
220 #if wxUSE_IMAGE
221 wxImage loadimage( filename, type );
222 if (loadimage.Ok())
223 {
224 if ( desiredWidth == -1 )
225 desiredWidth = loadimage.GetWidth() ;
226 if ( desiredHeight == -1 )
227 desiredHeight = loadimage.GetHeight() ;
228 if ( desiredWidth != loadimage.GetWidth() || desiredHeight != loadimage.GetHeight() )
229 loadimage.Rescale( desiredWidth , desiredHeight ) ;
230
231 wxBitmap bmp( loadimage );
232 CopyFromBitmap( bmp ) ;
233
234 return true;
235 }
236 #endif
237 }
238 }
239 return true ;
240 }
241
242 void wxIcon::CopyFromBitmap( const wxBitmap& bmp )
243 {
244 UnRef() ;
245
246 // as the bitmap owns that ref, we have to acquire it as well
247 IconRef iconRef = bmp.GetBitmapData()->GetIconRef() ;
248 AcquireIconRef( iconRef ) ;
249
250 m_refData = new wxIconRefData( (WXHICON) iconRef ) ;
251 M_ICONDATA->SetWidth( bmp.GetWidth() ) ;
252 M_ICONDATA->SetHeight( bmp.GetHeight() ) ;
253 }
254
255 wxIconRefData::wxIconRefData( WXHICON icon )
256 {
257 m_iconRef = MAC_WXHICON( icon ) ;
258
259 // Standard sizes
260 SetWidth( 32 ) ;
261 SetHeight( 32 ) ;
262 }
263
264 void wxIconRefData::Init()
265 {
266 m_iconRef = NULL ;
267 }
268
269 void wxIconRefData::Free()
270 {
271 if ( m_iconRef )
272 {
273 ReleaseIconRef( m_iconRef ) ;
274 m_iconRef = NULL ;
275 }
276 }
277
278 IMPLEMENT_DYNAMIC_CLASS(wxICONResourceHandler, wxBitmapHandler)
279
280 bool wxICONResourceHandler::LoadFile(
281 wxBitmap *bitmap, const wxString& name, long WXUNUSED(flags),
282 int desiredWidth, int desiredHeight )
283 {
284 wxIcon icon ;
285 icon.LoadFile( name , wxBITMAP_TYPE_ICON_RESOURCE , desiredWidth , desiredHeight ) ;
286 bitmap->CopyFromIcon( icon ) ;
287
288 return bitmap->Ok() ;
289 }
290