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