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