]> git.saurik.com Git - wxWidgets.git/blob - src/osx/carbon/icon.cpp
Don't use deprecated NSTableView selectRow:byExtendingSelection: method.
[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 #if wxOSX_USE_COCOA_OR_CARBON
15
16 #include "wx/icon.h"
17
18 #ifndef WX_PRECOMP
19 #include "wx/image.h"
20 #endif
21
22 #include "wx/osx/private.h"
23
24 IMPLEMENT_DYNAMIC_CLASS(wxIcon, wxGDIObject)
25
26 #define M_ICONDATA ((wxIconRefData *)m_refData)
27
28 class WXDLLEXPORT wxIconRefData : public wxGDIRefData
29 {
30 public:
31 wxIconRefData() { Init(); }
32 wxIconRefData( WXHICON iconref, int desiredWidth, int desiredHeight );
33 virtual ~wxIconRefData() { Free(); }
34
35 virtual bool IsOk() const { return m_iconRef != NULL; }
36
37 virtual void Free();
38
39 void SetWidth( int width ) { m_width = width; }
40 void SetHeight( int height ) { m_height = height; }
41
42 int GetWidth() const { return m_width; }
43 int GetHeight() const { return m_height; }
44
45 WXHICON GetHICON() const { return (WXHICON) m_iconRef; }
46
47 private:
48 void Init();
49
50 IconRef m_iconRef;
51 int m_width;
52 int m_height;
53
54 // We can (easily) copy m_iconRef so we don't implement the copy ctor.
55 wxDECLARE_NO_COPY_CLASS(wxIconRefData);
56 };
57
58
59 wxIconRefData::wxIconRefData( WXHICON icon, int desiredWidth, int desiredHeight )
60 {
61 m_iconRef = (IconRef)( icon ) ;
62
63 // Standard sizes
64 SetWidth( desiredWidth == -1 ? 32 : desiredWidth ) ;
65 SetHeight( desiredHeight == -1 ? 32 : desiredHeight ) ;
66 }
67
68 void wxIconRefData::Init()
69 {
70 m_iconRef = NULL ;
71 m_width =
72 m_height = 0;
73 }
74
75 void wxIconRefData::Free()
76 {
77 if ( m_iconRef )
78 {
79 ReleaseIconRef( m_iconRef ) ;
80 m_iconRef = NULL ;
81 }
82 }
83
84 //
85 //
86 //
87
88 wxIcon::wxIcon()
89 {
90 }
91
92 wxIcon::wxIcon( const char bits[], int width, int height )
93 {
94 wxBitmap bmp( bits, width, height ) ;
95 CopyFromBitmap( bmp ) ;
96 }
97
98 wxIcon::wxIcon(const char* const* bits)
99 {
100 wxBitmap bmp( bits ) ;
101 CopyFromBitmap( bmp ) ;
102 }
103
104 wxIcon::wxIcon(
105 const wxString& icon_file, wxBitmapType flags,
106 int desiredWidth, int desiredHeight )
107 {
108 LoadFile( icon_file, flags, desiredWidth, desiredHeight );
109 }
110
111 wxIcon::wxIcon(WXHICON icon, const wxSize& size)
112 : wxGDIObject()
113 {
114 // as the icon owns that ref, we have to acquire it as well
115 if (icon)
116 AcquireIconRef( (IconRef) icon ) ;
117
118 m_refData = new wxIconRefData( icon, size.x, size.y ) ;
119 }
120
121 wxIcon::~wxIcon()
122 {
123 }
124
125 wxGDIRefData *wxIcon::CreateGDIRefData() const
126 {
127 return new wxIconRefData;
128 }
129
130 wxGDIRefData *
131 wxIcon::CloneGDIRefData(const wxGDIRefData * WXUNUSED(data)) const
132 {
133 wxFAIL_MSG( wxS("Cloning icons is not implemented in wxCarbon.") );
134
135 return new wxIconRefData;
136 }
137
138 WXHICON wxIcon::GetHICON() const
139 {
140 wxASSERT( Ok() ) ;
141
142 return (WXHICON) ((wxIconRefData*)m_refData)->GetHICON() ;
143 }
144
145 int wxIcon::GetWidth() const
146 {
147 wxCHECK_MSG( Ok(), -1, wxT("invalid icon") );
148
149 return M_ICONDATA->GetWidth();
150 }
151
152 int wxIcon::GetHeight() const
153 {
154 wxCHECK_MSG( Ok(), -1, wxT("invalid icon") );
155
156 return M_ICONDATA->GetHeight();
157 }
158
159 int wxIcon::GetDepth() const
160 {
161 return 32;
162 }
163
164 void wxIcon::SetDepth( int WXUNUSED(depth) )
165 {
166 }
167
168 void wxIcon::SetWidth( int WXUNUSED(width) )
169 {
170 }
171
172 void wxIcon::SetHeight( int WXUNUSED(height) )
173 {
174 }
175
176 // Load an icon based on resource name or filel name
177 // Return true on success, false otherwise
178 bool wxIcon::LoadFile(
179 const wxString& filename, wxBitmapType type,
180 int desiredWidth, int desiredHeight )
181 {
182 if( type == wxBITMAP_TYPE_ICON_RESOURCE )
183 {
184 if( LoadIconFromSystemResource( filename, desiredWidth, desiredHeight ) )
185 return true;
186 else
187 return LoadIconFromBundleResource( filename, desiredWidth, desiredHeight );
188 }
189 else if( type == wxBITMAP_TYPE_ICON )
190 {
191 return LoadIconFromFile( filename, desiredWidth, desiredHeight );
192 }
193 else
194 {
195 return LoadIconAsBitmap( filename, type, desiredWidth, desiredHeight );
196 }
197 }
198
199 // Load a well known system icon by its wxWidgets identifier
200 // Returns true on success, false otherwise
201 bool wxIcon::LoadIconFromSystemResource(const wxString& resourceName, int desiredWidth, int desiredHeight)
202 {
203 UnRef();
204
205 OSType theId = 0 ;
206
207 if ( resourceName == wxT("wxICON_INFORMATION") )
208 {
209 theId = kAlertNoteIcon ;
210 }
211 else if ( resourceName == wxT("wxICON_QUESTION") )
212 {
213 theId = kAlertCautionIcon ;
214 }
215 else if ( resourceName == wxT("wxICON_WARNING") )
216 {
217 theId = kAlertCautionIcon ;
218 }
219 else if ( resourceName == wxT("wxICON_ERROR") )
220 {
221 theId = kAlertStopIcon ;
222 }
223 else if ( resourceName == wxT("wxICON_FOLDER") )
224 {
225 theId = kGenericFolderIcon ;
226 }
227 else if ( resourceName == wxT("wxICON_FOLDER_OPEN") )
228 {
229 theId = kOpenFolderIcon ;
230 }
231 else if ( resourceName == wxT("wxICON_NORMAL_FILE") )
232 {
233 theId = kGenericDocumentIcon ;
234 }
235 else if ( resourceName == wxT("wxICON_EXECUTABLE_FILE") )
236 {
237 theId = kGenericApplicationIcon ;
238 }
239 else if ( resourceName == wxT("wxICON_CDROM") )
240 {
241 theId = kGenericCDROMIcon ;
242 }
243 else if ( resourceName == wxT("wxICON_FLOPPY") )
244 {
245 theId = kGenericFloppyIcon ;
246 }
247 else if ( resourceName == wxT("wxICON_HARDDISK") )
248 {
249 theId = kGenericHardDiskIcon ;
250 }
251 else if ( resourceName == wxT("wxICON_REMOVABLE") )
252 {
253 theId = kGenericRemovableMediaIcon ;
254 }
255 else if ( resourceName == wxT("wxICON_DELETE") )
256 {
257 theId = kToolbarDeleteIcon ;
258 }
259 else if ( resourceName == wxT("wxICON_GO_BACK") )
260 {
261 theId = kBackwardArrowIcon ;
262 }
263 else if ( resourceName == wxT("wxICON_GO_FORWARD") )
264 {
265 theId = kForwardArrowIcon ;
266 }
267 else if ( resourceName == wxT("wxICON_GO_HOME") )
268 {
269 theId = kToolbarHomeIcon ;
270 }
271 else if ( resourceName == wxT("wxICON_HELP_SETTINGS") )
272 {
273 theId = kGenericFontIcon ;
274 }
275 else if ( resourceName == wxT("wxICON_HELP_PAGE") )
276 {
277 theId = kGenericDocumentIcon ;
278 }
279
280 if ( theId != 0 )
281 {
282 IconRef iconRef = NULL ;
283 verify_noerr( GetIconRef( kOnSystemDisk, kSystemIconsCreator, theId, &iconRef ) ) ;
284 if ( iconRef )
285 {
286 m_refData = new wxIconRefData( (WXHICON) iconRef, desiredWidth, desiredHeight ) ;
287 return true ;
288 }
289 }
290
291 return false;
292 }
293
294 // Load an icon of type 'icns' by resource by name
295 // The resource must exist in one of the currently accessible bundles
296 // (usually this means the application bundle for the current application)
297 // Return true on success, false otherwise
298 bool wxIcon::LoadIconFromBundleResource(const wxString& resourceName, int desiredWidth, int desiredHeight)
299 {
300 UnRef();
301
302 IconRef iconRef = NULL ;
303
304 // first look in the resource fork
305 if ( iconRef == NULL )
306 {
307 Str255 theName ;
308
309 wxMacStringToPascal( resourceName , theName ) ;
310 Handle resHandle = GetNamedResource( 'icns' , theName ) ;
311 if ( resHandle != 0L )
312 {
313 IconFamilyHandle iconFamily = (IconFamilyHandle) resHandle ;
314 OSStatus err = GetIconRefFromIconFamilyPtr( *iconFamily, GetHandleSize((Handle) iconFamily), &iconRef );
315
316 if ( err != noErr )
317 {
318 wxFAIL_MSG("Error when constructing icon ref");
319 }
320
321 ReleaseResource( resHandle ) ;
322 }
323 }
324
325 if ( iconRef )
326 {
327 m_refData = new wxIconRefData( (WXHICON) iconRef, desiredWidth, desiredHeight );
328 return true;
329 }
330
331 return false;
332 }
333
334 // Load an icon from an icon file using the underlying OS X API
335 // The icon file must be in a format understood by the OS
336 // Return true for success, false otherwise
337 bool wxIcon::LoadIconFromFile(const wxString& filename, int desiredWidth, int desiredHeight)
338 {
339 UnRef();
340
341 OSStatus err;
342 bool result = false;
343
344 // Get a file system reference
345 FSRef fsRef;
346 err = FSPathMakeRef( (const wxUint8*)filename.utf8_str().data(), &fsRef, NULL );
347
348 if( err != noErr )
349 return false;
350
351 // Get a handle on the icon family
352 IconFamilyHandle iconFamily;
353 err = ReadIconFromFSRef( &fsRef, &iconFamily );
354
355 if( err != noErr )
356 return false;
357
358 // Get the icon reference itself
359 IconRef iconRef;
360 err = GetIconRefFromIconFamilyPtr( *iconFamily, GetHandleSize((Handle) iconFamily), &iconRef );
361
362 if( err == noErr )
363 {
364 // If everthing is OK, assign m_refData
365 m_refData = new wxIconRefData( (WXHICON) iconRef, desiredWidth, desiredHeight );
366 result = true;
367 }
368
369 // Release the iconFamily before returning
370 ReleaseResource( (Handle) iconFamily );
371 return result;
372 }
373
374 // Load an icon from a file using functionality from wxWidgets
375 // A suitable bitmap handler (or image handler) must be available
376 // Return true on success, false otherwise
377 bool wxIcon::LoadIconAsBitmap(const wxString& filename, wxBitmapType type, int desiredWidth, int desiredHeight)
378 {
379 UnRef();
380
381 wxBitmapHandler *handler = wxBitmap::FindHandler( type );
382
383 if ( handler )
384 {
385 wxBitmap bmp ;
386 if ( handler->LoadFile( &bmp , filename, type, desiredWidth, desiredHeight ))
387 {
388 CopyFromBitmap( bmp ) ;
389 return true ;
390 }
391 }
392
393 #if wxUSE_IMAGE
394 else
395 {
396 wxImage loadimage( filename, type );
397 if (loadimage.Ok())
398 {
399 if ( desiredWidth == -1 )
400 desiredWidth = loadimage.GetWidth() ;
401 if ( desiredHeight == -1 )
402 desiredHeight = loadimage.GetHeight() ;
403 if ( desiredWidth != loadimage.GetWidth() || desiredHeight != loadimage.GetHeight() )
404 loadimage.Rescale( desiredWidth , desiredHeight ) ;
405
406 wxBitmap bmp( loadimage );
407 CopyFromBitmap( bmp ) ;
408
409 return true;
410 }
411 }
412 #endif
413
414 return false;
415 }
416
417
418 void wxIcon::CopyFromBitmap( const wxBitmap& bmp )
419 {
420 UnRef() ;
421
422 // as the bitmap owns that ref, we have to acquire it as well
423 IconRef iconRef = bmp.CreateIconRef() ;
424 m_refData = new wxIconRefData( (WXHICON) iconRef, bmp.GetWidth(), bmp.GetHeight() ) ;
425 }
426
427 IMPLEMENT_DYNAMIC_CLASS(wxICONResourceHandler, wxBitmapHandler)
428
429 bool wxICONResourceHandler::LoadFile(
430 wxBitmap *bitmap, const wxString& name, wxBitmapType WXUNUSED(flags),
431 int desiredWidth, int desiredHeight )
432 {
433 wxIcon icon ;
434 if ( icon.LoadFile( name , wxBITMAP_TYPE_ICON_RESOURCE , desiredWidth , desiredHeight ) )
435 {
436 bitmap->CopyFromIcon( icon ) ;
437 return bitmap->Ok() ;
438 }
439 return false;
440 }
441
442 #endif
443