]> git.saurik.com Git - wxWidgets.git/blob - src/osx/carbon/icon.cpp
704608f33ed209b70c0848f2788a04907daa9892
[wxWidgets.git] / src / osx / carbon / icon.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/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/osx/private.h"
21
22 IMPLEMENT_DYNAMIC_CLASS(wxIcon, wxGDIObject)
23
24 #define M_ICONDATA ((wxIconRefData *)m_refData)
25
26 class WXDLLEXPORT wxIconRefData : public wxGDIRefData
27 {
28 public:
29 wxIconRefData() { Init(); }
30 wxIconRefData( WXHICON iconref, int desiredWidth, int desiredHeight );
31 virtual ~wxIconRefData() { Free(); }
32
33 virtual bool IsOk() const { return m_iconRef != NULL; }
34
35 virtual void Free();
36
37 void SetWidth( int width ) { m_width = width; }
38 void SetHeight( int height ) { m_height = height; }
39
40 int GetWidth() const { return m_width; }
41 int GetHeight() const { return m_height; }
42
43 WXHICON GetHICON() const { return (WXHICON) m_iconRef; }
44
45 private:
46 void Init();
47
48 IconRef m_iconRef;
49 int m_width;
50 int m_height;
51 };
52
53
54 wxIconRefData::wxIconRefData( WXHICON icon, int desiredWidth, int desiredHeight )
55 {
56 m_iconRef = (IconRef)( icon ) ;
57
58 // Standard sizes
59 SetWidth( desiredWidth == -1 ? 32 : desiredWidth ) ;
60 SetHeight( desiredHeight == -1 ? 32 : desiredHeight ) ;
61 }
62
63 void wxIconRefData::Init()
64 {
65 m_iconRef = NULL ;
66 m_width =
67 m_height = 0;
68 }
69
70 void wxIconRefData::Free()
71 {
72 if ( m_iconRef )
73 {
74 ReleaseIconRef( m_iconRef ) ;
75 m_iconRef = NULL ;
76 }
77 }
78
79 //
80 //
81 //
82
83 wxIcon::wxIcon()
84 {
85 }
86
87 wxIcon::wxIcon( const char bits[], int width, int height )
88 {
89 wxBitmap bmp( bits, width, height ) ;
90 CopyFromBitmap( bmp ) ;
91 }
92
93 wxIcon::wxIcon(const char* const* bits)
94 {
95 wxBitmap bmp( bits ) ;
96 CopyFromBitmap( bmp ) ;
97 }
98
99 wxIcon::wxIcon(
100 const wxString& icon_file, wxBitmapType flags,
101 int desiredWidth, int desiredHeight )
102 {
103 LoadFile( icon_file, flags, desiredWidth, desiredHeight );
104 }
105
106 wxIcon::wxIcon(WXHICON icon, const wxSize& size)
107 : wxGDIObject()
108 {
109 // as the icon owns that ref, we have to acquire it as well
110 if (icon)
111 AcquireIconRef( (IconRef) icon ) ;
112
113 m_refData = new wxIconRefData( icon, size.x, size.y ) ;
114 }
115
116 wxIcon::~wxIcon()
117 {
118 }
119
120 wxGDIRefData *wxIcon::CreateGDIRefData() const
121 {
122 return new wxIconRefData;
123 }
124
125 wxGDIRefData *wxIcon::CloneGDIRefData(const wxGDIRefData *data) const
126 {
127 return new wxIconRefData(*static_cast<const wxIconRefData *>(data));
128 }
129
130 WXHICON wxIcon::GetHICON() const
131 {
132 wxASSERT( Ok() ) ;
133
134 return (WXHICON) ((wxIconRefData*)m_refData)->GetHICON() ;
135 }
136
137 int wxIcon::GetWidth() const
138 {
139 wxCHECK_MSG( Ok(), -1, wxT("invalid icon") );
140
141 return M_ICONDATA->GetWidth();
142 }
143
144 int wxIcon::GetHeight() const
145 {
146 wxCHECK_MSG( Ok(), -1, wxT("invalid icon") );
147
148 return M_ICONDATA->GetHeight();
149 }
150
151 int wxIcon::GetDepth() const
152 {
153 return 32;
154 }
155
156 void wxIcon::SetDepth( int WXUNUSED(depth) )
157 {
158 }
159
160 void wxIcon::SetWidth( int WXUNUSED(width) )
161 {
162 }
163
164 void wxIcon::SetHeight( int WXUNUSED(height) )
165 {
166 }
167
168 bool wxIcon::LoadFile(
169 const wxString& filename, wxBitmapType type,
170 int desiredWidth, int desiredHeight )
171 {
172 UnRef();
173
174 if ( type == wxBITMAP_TYPE_ICON_RESOURCE )
175 {
176 OSType theId = 0 ;
177
178 if ( filename == wxT("wxICON_INFORMATION") )
179 {
180 theId = kAlertNoteIcon ;
181 }
182 else if ( filename == wxT("wxICON_QUESTION") )
183 {
184 theId = kAlertCautionIcon ;
185 }
186 else if ( filename == wxT("wxICON_WARNING") )
187 {
188 theId = kAlertCautionIcon ;
189 }
190 else if ( filename == wxT("wxICON_ERROR") )
191 {
192 theId = kAlertStopIcon ;
193 }
194 else if ( filename == wxT("wxICON_FOLDER") )
195 {
196 theId = kGenericFolderIcon ;
197 }
198 else if ( filename == wxT("wxICON_FOLDER_OPEN") )
199 {
200 theId = kOpenFolderIcon ;
201 }
202 else if ( filename == wxT("wxICON_NORMAL_FILE") )
203 {
204 theId = kGenericDocumentIcon ;
205 }
206 else if ( filename == wxT("wxICON_CDROM") )
207 {
208 theId = kGenericCDROMIcon ;
209 }
210 else if ( filename == wxT("wxICON_FLOPPY") )
211 {
212 theId = kGenericFloppyIcon ;
213 }
214 else if ( filename == wxT("wxICON_HARDDISK") )
215 {
216 theId = kGenericHardDiskIcon ;
217 }
218 else if ( filename == wxT("wxICON_REMOVABLE") )
219 {
220 theId = kGenericRemovableMediaIcon ;
221 }
222 else if ( filename == wxT("wxICON_DELETE") )
223 {
224 theId = kTrashIcon ;
225 }
226 else if ( filename == wxT("wxICON_GO_BACK") )
227 {
228 theId = kBackwardArrowIcon ;
229 }
230 else if ( filename == wxT("wxICON_GO_FORWARD") )
231 {
232 theId = kForwardArrowIcon ;
233 }
234 else if ( filename == wxT("wxICON_GO_HOME") )
235 {
236 theId = kUserFolderIcon ;
237 }
238 else if ( filename == wxT("wxICON_HELP_FOLDER") )
239 {
240 theId = kHelpFolderIcon ;
241 }
242 else if ( filename == wxT("wxICON_HELP_SETTINGS") )
243 {
244 theId = kGenericFontIcon ;
245 }
246 else if ( filename == wxT("wxICON_HELP_PAGE") )
247 {
248 theId = kGenericDocumentIcon ;
249 }
250 else if ( filename == wxT("wxICON_PRINT") )
251 {
252 theId = kPrintMonitorFolderIcon ;
253 }
254 else
255 {
256 IconRef iconRef = NULL ;
257
258 // first look in the resource fork
259 if ( iconRef == NULL )
260 {
261 Str255 theName ;
262
263 wxMacStringToPascal( filename , theName ) ;
264 Handle resHandle = GetNamedResource( 'icns' , theName ) ;
265 if ( resHandle != 0L )
266 {
267 IconFamilyHandle iconFamily = (IconFamilyHandle) resHandle ;
268 HLock((Handle) iconFamily);
269 OSStatus err = GetIconRefFromIconFamilyPtr( *iconFamily, GetHandleSize((Handle) iconFamily), &iconRef );
270 HUnlock((Handle) iconFamily);
271 if ( err != noErr )
272 {
273 wxFAIL_MSG("Error when constructing icon ref");
274 }
275
276 ReleaseResource( resHandle ) ;
277 }
278 }
279 if ( iconRef == NULL )
280 {
281 // TODO add other attempts to load it from files etc here
282 }
283 if ( iconRef )
284 {
285 m_refData = new wxIconRefData( (WXHICON) iconRef, desiredWidth, desiredHeight ) ;
286 return true ;
287 }
288 }
289
290 if ( theId != 0 )
291 {
292 IconRef iconRef = NULL ;
293 verify_noerr( GetIconRef( kOnSystemDisk, kSystemIconsCreator, theId, &iconRef ) ) ;
294 if ( iconRef )
295 {
296 m_refData = new wxIconRefData( (WXHICON) iconRef, desiredWidth, desiredHeight ) ;
297
298 return true ;
299 }
300 }
301
302 return false ;
303 }
304 else
305 {
306 wxBitmapHandler *handler = wxBitmap::FindHandler( type );
307
308 if ( handler )
309 {
310 wxBitmap bmp ;
311 if ( handler->LoadFile( &bmp , filename, type, desiredWidth, desiredHeight ))
312 {
313 CopyFromBitmap( bmp ) ;
314
315 return true ;
316 }
317
318 return false ;
319 }
320 else
321 {
322 #if wxUSE_IMAGE
323 wxImage loadimage( filename, type );
324 if (loadimage.Ok())
325 {
326 if ( desiredWidth == -1 )
327 desiredWidth = loadimage.GetWidth() ;
328 if ( desiredHeight == -1 )
329 desiredHeight = loadimage.GetHeight() ;
330 if ( desiredWidth != loadimage.GetWidth() || desiredHeight != loadimage.GetHeight() )
331 loadimage.Rescale( desiredWidth , desiredHeight ) ;
332
333 wxBitmap bmp( loadimage );
334 CopyFromBitmap( bmp ) ;
335
336 return true;
337 }
338 #endif
339 }
340 }
341 return true ;
342 }
343
344 void wxIcon::CopyFromBitmap( const wxBitmap& bmp )
345 {
346 UnRef() ;
347
348 // as the bitmap owns that ref, we have to acquire it as well
349 IconRef iconRef = bmp.CreateIconRef() ;
350 m_refData = new wxIconRefData( (WXHICON) iconRef, bmp.GetWidth(), bmp.GetHeight() ) ;
351 }
352
353 IMPLEMENT_DYNAMIC_CLASS(wxICONResourceHandler, wxBitmapHandler)
354
355 bool wxICONResourceHandler::LoadFile(
356 wxBitmap *bitmap, const wxString& name, wxBitmapType WXUNUSED(flags),
357 int desiredWidth, int desiredHeight )
358 {
359 wxIcon icon ;
360 icon.LoadFile( name , wxBITMAP_TYPE_ICON_RESOURCE , desiredWidth , desiredHeight ) ;
361 bitmap->CopyFromIcon( icon ) ;
362
363 return bitmap->Ok() ;
364 }
365