]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/icon.cpp
avoid GCC warning about type-punned pointer breaking strict aliasing rules
[wxWidgets.git] / src / mac / carbon / icon.cpp
CommitLineData
e9576ca5 1/////////////////////////////////////////////////////////////////////////////
155ecd4c 2// Name: src/mac/carbon/icon.cpp
e9576ca5 3// Purpose: wxIcon class
a31a5f85 4// Author: Stefan Csomor
e9576ca5 5// Modified by:
a31a5f85 6// Created: 1998-01-01
e9576ca5 7// RCS-ID: $Id$
a31a5f85 8// Copyright: (c) Stefan Csomor
155ecd4c 9// Licence: wxWindows licence
e9576ca5
SC
10/////////////////////////////////////////////////////////////////////////////
11
3d1a4878
SC
12#include "wx/wxprec.h"
13
e9576ca5 14#include "wx/icon.h"
155ecd4c
WS
15
16#ifndef WX_PRECOMP
17 #include "wx/image.h"
18#endif
19
76a5e5d2
SC
20#include "wx/mac/private.h"
21
1046cbe1 22IMPLEMENT_DYNAMIC_CLASS(wxIcon, wxGDIObject)
ea68b706 23
5a7d70fe 24#define M_ICONDATA ((wxIconRefData *)m_refData)
76a5e5d2 25
968c951f
SC
26class WXDLLEXPORT wxIconRefData : public wxGDIRefData
27{
28public:
8f884a0d 29 wxIconRefData() { Init(); }
968c951f
SC
30 wxIconRefData( WXHICON iconref, int desiredWidth, int desiredHeight );
31 virtual ~wxIconRefData() { Free(); }
8f884a0d
VZ
32
33 virtual bool IsOk() const { return m_iconRef != NULL; }
34
968c951f 35 virtual void Free();
8f884a0d 36
968c951f
SC
37 void SetWidth( int width ) { m_width = width; }
38 void SetHeight( int height ) { m_height = height; }
8f884a0d 39
968c951f
SC
40 int GetWidth() const { return m_width; }
41 int GetHeight() const { return m_height; }
8f884a0d 42
968c951f 43 WXHICON GetHICON() const { return (WXHICON) m_iconRef; }
8f884a0d
VZ
44
45private:
46 void Init();
47
968c951f
SC
48 IconRef m_iconRef;
49 int m_width;
50 int m_height;
51};
52
53
54wxIconRefData::wxIconRefData( WXHICON icon, int desiredWidth, int desiredHeight )
55{
56 m_iconRef = MAC_WXHICON( icon ) ;
8f884a0d 57
968c951f
SC
58 // Standard sizes
59 SetWidth( desiredWidth == -1 ? 32 : desiredWidth ) ;
60 SetHeight( desiredHeight == -1 ? 32 : desiredHeight ) ;
61}
62
63void wxIconRefData::Init()
64{
65 m_iconRef = NULL ;
8f884a0d
VZ
66 m_width =
67 m_height = 0;
968c951f
SC
68}
69
70void wxIconRefData::Free()
71{
72 if ( m_iconRef )
73 {
74 ReleaseIconRef( m_iconRef ) ;
75 m_iconRef = NULL ;
76 }
77}
78
79//
80//
81//
e9576ca5 82
e9576ca5
SC
83wxIcon::wxIcon()
84{
85}
86
5a7d70fe 87wxIcon::wxIcon( const char bits[], int width, int height )
e9576ca5 88{
5a7d70fe 89 wxBitmap bmp( bits, width, height ) ;
20b69855 90 CopyFromBitmap( bmp ) ;
e9576ca5
SC
91}
92
5a7d70fe 93wxIcon::wxIcon( const char **bits )
2f1ae414 94{
5a7d70fe 95 wxBitmap bmp( bits ) ;
20b69855 96 CopyFromBitmap( bmp ) ;
2f1ae414
SC
97}
98
5a7d70fe 99wxIcon::wxIcon( char **bits )
2f1ae414 100{
5a7d70fe 101 wxBitmap bmp( bits ) ;
20b69855 102 CopyFromBitmap( bmp ) ;
2f1ae414
SC
103}
104
5a7d70fe
DS
105wxIcon::wxIcon(
106 const wxString& icon_file, int flags,
107 int desiredWidth, int desiredHeight )
e9576ca5 108{
5a7d70fe 109 LoadFile( icon_file, (wxBitmapType) flags, desiredWidth, desiredHeight );
e9576ca5
SC
110}
111
52734360
VZ
112wxIcon::wxIcon(WXHICON icon, const wxSize& size)
113 : wxGDIObject()
114{
115 // as the icon owns that ref, we have to acquire it as well
116 if (icon)
117 AcquireIconRef( (IconRef) icon ) ;
118
968c951f 119 m_refData = new wxIconRefData( icon, size.x, size.y ) ;
52734360
VZ
120}
121
e9576ca5
SC
122wxIcon::~wxIcon()
123{
124}
125
8f884a0d
VZ
126wxGDIRefData *wxIcon::CreateGDIRefData() const
127{
128 return new wxIconRefData;
129}
130
131wxGDIRefData *wxIcon::CloneGDIRefData(const wxGDIRefData *data) const
132{
133 return new wxIconRefData(*wx_static_cast(const wxIconRefData *, data));
134}
135
5a7d70fe 136WXHICON wxIcon::GetHICON() const
20b69855
SC
137{
138 wxASSERT( Ok() ) ;
5a7d70fe 139
20b69855
SC
140 return (WXHICON) ((wxIconRefData*)m_refData)->GetHICON() ;
141}
142
143int wxIcon::GetWidth() const
144{
ea68b706
SC
145 wxCHECK_MSG( Ok(), -1, wxT("invalid icon") );
146
147 return M_ICONDATA->GetWidth();
20b69855
SC
148}
149
150int wxIcon::GetHeight() const
151{
ea68b706
SC
152 wxCHECK_MSG( Ok(), -1, wxT("invalid icon") );
153
154 return M_ICONDATA->GetHeight();
20b69855
SC
155}
156
5a7d70fe
DS
157int wxIcon::GetDepth() const
158{
159 return 32;
c605b871
KO
160}
161
89954433 162void wxIcon::SetDepth( int WXUNUSED(depth) )
5a7d70fe 163{
c605b871
KO
164}
165
89954433 166void wxIcon::SetWidth( int WXUNUSED(width) )
5a7d70fe 167{
c605b871
KO
168}
169
89954433 170void wxIcon::SetHeight( int WXUNUSED(height) )
5a7d70fe 171{
c605b871
KO
172}
173
5a7d70fe
DS
174bool wxIcon::LoadFile(
175 const wxString& filename, wxBitmapType type,
176 int desiredWidth, int desiredHeight )
e9576ca5 177{
e40298d5 178 UnRef();
5a7d70fe 179
20b69855 180 if ( type == wxBITMAP_TYPE_ICON_RESOURCE )
1996c23f 181 {
20b69855 182 OSType theId = 0 ;
5a7d70fe 183
20b69855
SC
184 if ( filename == wxT("wxICON_INFORMATION") )
185 {
186 theId = kAlertNoteIcon ;
187 }
188 else if ( filename == wxT("wxICON_QUESTION") )
189 {
190 theId = kAlertCautionIcon ;
191 }
192 else if ( filename == wxT("wxICON_WARNING") )
193 {
194 theId = kAlertCautionIcon ;
195 }
196 else if ( filename == wxT("wxICON_ERROR") )
197 {
198 theId = kAlertStopIcon ;
199 }
52734360
VZ
200 else if ( filename == wxT("wxICON_FOLDER") )
201 {
202 theId = kGenericFolderIcon ;
203 }
204 else if ( filename == wxT("wxICON_FOLDER_OPEN") )
205 {
206 theId = kOpenFolderIcon ;
207 }
208 else if ( filename == wxT("wxICON_NORMAL_FILE") )
209 {
210 theId = kGenericDocumentIcon ;
211 }
20b69855 212 else
5a7d70fe 213 {
8f884a0d
VZ
214 IconRef iconRef = NULL ;
215
216 // first look in the resource fork
217 if ( iconRef == NULL )
218 {
219 Str255 theName ;
220
221 wxMacStringToPascal( filename , theName ) ;
222 Handle resHandle = GetNamedResource( 'icns' , theName ) ;
223 if ( resHandle != 0L )
224 {
225 IconFamilyHandle iconFamily = (IconFamilyHandle) resHandle ;
6239ee05
SC
226 HLock((Handle) iconFamily);
227 OSStatus err = GetIconRefFromIconFamilyPtr( *iconFamily, GetHandleSize((Handle) iconFamily), &iconRef );
228 HUnlock((Handle) iconFamily);
229 wxASSERT_MSG( err == noErr , wxT("Error when constructing icon ref") );
230 ReleaseResource( resHandle ) ;
8f884a0d
VZ
231 }
232 }
6239ee05 233 if ( iconRef == NULL )
20b69855 234 {
6239ee05 235 // TODO add other attempts to load it from files etc here
20b69855 236 }
8f884a0d
VZ
237 if ( iconRef )
238 {
239 m_refData = new wxIconRefData( (WXHICON) iconRef, desiredWidth, desiredHeight ) ;
240 return true ;
241 }
20b69855 242 }
5a7d70fe 243
20b69855
SC
244 if ( theId != 0 )
245 {
246 IconRef iconRef = NULL ;
5a7d70fe 247 verify_noerr( GetIconRef( kOnSystemDisk, kSystemIconsCreator, theId, &iconRef ) ) ;
20b69855
SC
248 if ( iconRef )
249 {
968c951f 250 m_refData = new wxIconRefData( (WXHICON) iconRef, desiredWidth, desiredHeight ) ;
5a7d70fe
DS
251
252 return true ;
20b69855
SC
253 }
254 }
5a7d70fe
DS
255
256 return false ;
1996c23f 257 }
e40298d5 258 else
1996c23f 259 {
5a7d70fe 260 wxBitmapHandler *handler = wxBitmap::FindHandler( type );
20b69855
SC
261
262 if ( handler )
1996c23f 263 {
20b69855 264 wxBitmap bmp ;
5a7d70fe 265 if ( handler->LoadFile( &bmp , filename, type, desiredWidth, desiredHeight ))
20b69855
SC
266 {
267 CopyFromBitmap( bmp ) ;
5a7d70fe 268
20b69855
SC
269 return true ;
270 }
5a7d70fe 271
20b69855
SC
272 return false ;
273 }
274 else
275 {
da76c2ce 276#if wxUSE_IMAGE
5a7d70fe
DS
277 wxImage loadimage( filename, type );
278 if (loadimage.Ok())
20b69855
SC
279 {
280 if ( desiredWidth == -1 )
281 desiredWidth = loadimage.GetWidth() ;
282 if ( desiredHeight == -1 )
283 desiredHeight = loadimage.GetHeight() ;
284 if ( desiredWidth != loadimage.GetWidth() || desiredHeight != loadimage.GetHeight() )
285 loadimage.Rescale( desiredWidth , desiredHeight ) ;
155ecd4c 286
20b69855
SC
287 wxBitmap bmp( loadimage );
288 CopyFromBitmap( bmp ) ;
5a7d70fe 289
20b69855
SC
290 return true;
291 }
da76c2ce 292#endif
1996c23f
SC
293 }
294 }
20b69855 295 return true ;
e9576ca5
SC
296}
297
5a7d70fe 298void wxIcon::CopyFromBitmap( const wxBitmap& bmp )
d062e17f 299{
20b69855 300 UnRef() ;
5a7d70fe 301
796a6ef0 302 // as the bitmap owns that ref, we have to acquire it as well
968c951f
SC
303 IconRef iconRef = bmp.CreateIconRef() ;
304 m_refData = new wxIconRefData( (WXHICON) iconRef, bmp.GetWidth(), bmp.GetHeight() ) ;
d062e17f
GD
305}
306
519cb848
SC
307IMPLEMENT_DYNAMIC_CLASS(wxICONResourceHandler, wxBitmapHandler)
308
5a7d70fe 309bool wxICONResourceHandler::LoadFile(
89954433 310 wxBitmap *bitmap, const wxString& name, long WXUNUSED(flags),
5a7d70fe 311 int desiredWidth, int desiredHeight )
519cb848 312{
20b69855
SC
313 wxIcon icon ;
314 icon.LoadFile( name , wxBITMAP_TYPE_ICON_RESOURCE , desiredWidth , desiredHeight ) ;
315 bitmap->CopyFromIcon( icon ) ;
5a7d70fe 316
20b69855 317 return bitmap->Ok() ;
03e11df5 318}
5a7d70fe 319