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