]> git.saurik.com Git - wxWidgets.git/blame - src/osx/carbon/icon.cpp
move notebook event definition to common code
[wxWidgets.git] / src / osx / carbon / icon.cpp
CommitLineData
489468fe
SC
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
1f0c8f31 20#include "wx/osx/private.h"
489468fe
SC
21
22IMPLEMENT_DYNAMIC_CLASS(wxIcon, wxGDIObject)
23
24#define M_ICONDATA ((wxIconRefData *)m_refData)
25
26class WXDLLEXPORT wxIconRefData : public wxGDIRefData
27{
28public:
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
45private:
46 void Init();
47
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 ) ;
57
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 ;
66 m_width =
67 m_height = 0;
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//
82
83wxIcon::wxIcon()
84{
85}
86
87wxIcon::wxIcon( const char bits[], int width, int height )
88{
89 wxBitmap bmp( bits, width, height ) ;
90 CopyFromBitmap( bmp ) ;
91}
92
93wxIcon::wxIcon(const char* const* bits)
94{
95 wxBitmap bmp( bits ) ;
96 CopyFromBitmap( bmp ) ;
97}
98
99wxIcon::wxIcon(
100 const wxString& icon_file, int flags,
101 int desiredWidth, int desiredHeight )
102{
103 LoadFile( icon_file, (wxBitmapType) flags, desiredWidth, desiredHeight );
104}
105
106wxIcon::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
116wxIcon::~wxIcon()
117{
118}
119
120wxGDIRefData *wxIcon::CreateGDIRefData() const
121{
122 return new wxIconRefData;
123}
124
125wxGDIRefData *wxIcon::CloneGDIRefData(const wxGDIRefData *data) const
126{
127 return new wxIconRefData(*wx_static_cast(const wxIconRefData *, data));
128}
129
130WXHICON wxIcon::GetHICON() const
131{
132 wxASSERT( Ok() ) ;
133
134 return (WXHICON) ((wxIconRefData*)m_refData)->GetHICON() ;
135}
136
137int wxIcon::GetWidth() const
138{
139 wxCHECK_MSG( Ok(), -1, wxT("invalid icon") );
140
141 return M_ICONDATA->GetWidth();
142}
143
144int wxIcon::GetHeight() const
145{
146 wxCHECK_MSG( Ok(), -1, wxT("invalid icon") );
147
148 return M_ICONDATA->GetHeight();
149}
150
151int wxIcon::GetDepth() const
152{
153 return 32;
154}
155
156void wxIcon::SetDepth( int WXUNUSED(depth) )
157{
158}
159
160void wxIcon::SetWidth( int WXUNUSED(width) )
161{
162}
163
164void wxIcon::SetHeight( int WXUNUSED(height) )
165{
166}
167
168bool 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
207 {
208 IconRef iconRef = NULL ;
209
210 // first look in the resource fork
211 if ( iconRef == NULL )
212 {
213 Str255 theName ;
214
215 wxMacStringToPascal( filename , theName ) ;
216 Handle resHandle = GetNamedResource( 'icns' , theName ) ;
217 if ( resHandle != 0L )
218 {
219 IconFamilyHandle iconFamily = (IconFamilyHandle) resHandle ;
220 HLock((Handle) iconFamily);
221 OSStatus err = GetIconRefFromIconFamilyPtr( *iconFamily, GetHandleSize((Handle) iconFamily), &iconRef );
222 HUnlock((Handle) iconFamily);
223 if ( err != noErr )
224 {
225 wxFAIL_MSG("Error when constructing icon ref");
226 }
227
228 ReleaseResource( resHandle ) ;
229 }
230 }
231 if ( iconRef == NULL )
232 {
233 // TODO add other attempts to load it from files etc here
234 }
235 if ( iconRef )
236 {
237 m_refData = new wxIconRefData( (WXHICON) iconRef, desiredWidth, desiredHeight ) ;
238 return true ;
239 }
240 }
241
242 if ( theId != 0 )
243 {
244 IconRef iconRef = NULL ;
245 verify_noerr( GetIconRef( kOnSystemDisk, kSystemIconsCreator, theId, &iconRef ) ) ;
246 if ( iconRef )
247 {
248 m_refData = new wxIconRefData( (WXHICON) iconRef, desiredWidth, desiredHeight ) ;
249
250 return true ;
251 }
252 }
253
254 return false ;
255 }
256 else
257 {
258 wxBitmapHandler *handler = wxBitmap::FindHandler( type );
259
260 if ( handler )
261 {
262 wxBitmap bmp ;
263 if ( handler->LoadFile( &bmp , filename, type, desiredWidth, desiredHeight ))
264 {
265 CopyFromBitmap( bmp ) ;
266
267 return true ;
268 }
269
270 return false ;
271 }
272 else
273 {
274#if wxUSE_IMAGE
275 wxImage loadimage( filename, type );
276 if (loadimage.Ok())
277 {
278 if ( desiredWidth == -1 )
279 desiredWidth = loadimage.GetWidth() ;
280 if ( desiredHeight == -1 )
281 desiredHeight = loadimage.GetHeight() ;
282 if ( desiredWidth != loadimage.GetWidth() || desiredHeight != loadimage.GetHeight() )
283 loadimage.Rescale( desiredWidth , desiredHeight ) ;
284
285 wxBitmap bmp( loadimage );
286 CopyFromBitmap( bmp ) ;
287
288 return true;
289 }
290#endif
291 }
292 }
293 return true ;
294}
295
296void wxIcon::CopyFromBitmap( const wxBitmap& bmp )
297{
298 UnRef() ;
299
300 // as the bitmap owns that ref, we have to acquire it as well
301 IconRef iconRef = bmp.CreateIconRef() ;
302 m_refData = new wxIconRefData( (WXHICON) iconRef, bmp.GetWidth(), bmp.GetHeight() ) ;
303}
304
305IMPLEMENT_DYNAMIC_CLASS(wxICONResourceHandler, wxBitmapHandler)
306
307bool wxICONResourceHandler::LoadFile(
308 wxBitmap *bitmap, const wxString& name, long WXUNUSED(flags),
309 int desiredWidth, int desiredHeight )
310{
311 wxIcon icon ;
312 icon.LoadFile( name , wxBITMAP_TYPE_ICON_RESOURCE , desiredWidth , desiredHeight ) ;
313 bitmap->CopyFromIcon( icon ) ;
314
315 return bitmap->Ok() ;
316}
317