]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/icon.cpp
enter wxPendingEventLocker critical section in ProcessPendingEvents() (fixes 1720352)
[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 depth )
99 {
100 }
101
102 void wxIcon::SetWidth( int width )
103 {
104 }
105
106 void wxIcon::SetHeight( int 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 #if 0
156 Str255 theName ;
157 OSType theType ;
158 wxMacStringToPascal( name , theName ) ;
159
160 Handle resHandle = GetNamedResource( 'cicn' , theName ) ;
161 if ( resHandle != 0L )
162 {
163 GetResInfo( resHandle , &theId , &theType , theName ) ;
164 ReleaseResource( resHandle ) ;
165 }
166 #endif
167 }
168
169 if ( theId != 0 )
170 {
171 IconRef iconRef = NULL ;
172 verify_noerr( GetIconRef( kOnSystemDisk, kSystemIconsCreator, theId, &iconRef ) ) ;
173 if ( iconRef )
174 {
175 m_refData = new wxIconRefData( (WXHICON) iconRef ) ;
176
177 return true ;
178 }
179 }
180
181 return false ;
182 }
183 else
184 {
185 wxBitmapHandler *handler = wxBitmap::FindHandler( type );
186
187 if ( handler )
188 {
189 wxBitmap bmp ;
190 if ( handler->LoadFile( &bmp , filename, type, desiredWidth, desiredHeight ))
191 {
192 CopyFromBitmap( bmp ) ;
193
194 return true ;
195 }
196
197 return false ;
198 }
199 else
200 {
201 #if wxUSE_IMAGE
202 wxImage loadimage( filename, type );
203 if (loadimage.Ok())
204 {
205 if ( desiredWidth == -1 )
206 desiredWidth = loadimage.GetWidth() ;
207 if ( desiredHeight == -1 )
208 desiredHeight = loadimage.GetHeight() ;
209 if ( desiredWidth != loadimage.GetWidth() || desiredHeight != loadimage.GetHeight() )
210 loadimage.Rescale( desiredWidth , desiredHeight ) ;
211
212 wxBitmap bmp( loadimage );
213 CopyFromBitmap( bmp ) ;
214
215 return true;
216 }
217 #endif
218 }
219 }
220 return true ;
221 }
222
223 void wxIcon::CopyFromBitmap( const wxBitmap& bmp )
224 {
225 UnRef() ;
226
227 // as the bitmap owns that ref, we have to acquire it as well
228 IconRef iconRef = bmp.GetBitmapData()->GetIconRef() ;
229 AcquireIconRef( iconRef ) ;
230
231 m_refData = new wxIconRefData( (WXHICON) iconRef ) ;
232 M_ICONDATA->SetWidth( bmp.GetWidth() ) ;
233 M_ICONDATA->SetHeight( bmp.GetHeight() ) ;
234 }
235
236 wxIconRefData::wxIconRefData( WXHICON icon )
237 {
238 m_iconRef = MAC_WXHICON( icon ) ;
239
240 // Standard sizes
241 SetWidth( 32 ) ;
242 SetHeight( 32 ) ;
243 }
244
245 void wxIconRefData::Init()
246 {
247 m_iconRef = NULL ;
248 }
249
250 void wxIconRefData::Free()
251 {
252 if ( m_iconRef )
253 {
254 ReleaseIconRef( m_iconRef ) ;
255 m_iconRef = NULL ;
256 }
257 }
258
259 IMPLEMENT_DYNAMIC_CLASS(wxICONResourceHandler, wxBitmapHandler)
260
261 bool wxICONResourceHandler::LoadFile(
262 wxBitmap *bitmap, const wxString& name, long flags,
263 int desiredWidth, int desiredHeight )
264 {
265 wxIcon icon ;
266 icon.LoadFile( name , wxBITMAP_TYPE_ICON_RESOURCE , desiredWidth , desiredHeight ) ;
267 bitmap->CopyFromIcon( icon ) ;
268
269 return bitmap->Ok() ;
270 }
271