]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/icon.cpp
porting forward scrolling fix
[wxWidgets.git] / src / mac / carbon / icon.cpp
CommitLineData
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 22IMPLEMENT_DYNAMIC_CLASS(wxIcon, wxBitmap)
ea68b706 23
5a7d70fe 24#define M_ICONDATA ((wxIconRefData *)m_refData)
76a5e5d2 25
968c951f
SC
26class WXDLLEXPORT wxIconRefData : public wxGDIRefData
27{
28public:
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
50wxIconRefData::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
59void wxIconRefData::Init()
60{
61 m_iconRef = NULL ;
62}
63
64void wxIconRefData::Free()
65{
66 if ( m_iconRef )
67 {
68 ReleaseIconRef( m_iconRef ) ;
69 m_iconRef = NULL ;
70 }
71}
72
73//
74//
75//
e9576ca5 76
e9576ca5
SC
77wxIcon::wxIcon()
78{
79}
80
5a7d70fe 81wxIcon::wxIcon( const char bits[], int width, int height )
e9576ca5 82{
5a7d70fe 83 wxBitmap bmp( bits, width, height ) ;
20b69855 84 CopyFromBitmap( bmp ) ;
e9576ca5
SC
85}
86
5a7d70fe 87wxIcon::wxIcon( const char **bits )
2f1ae414 88{
5a7d70fe 89 wxBitmap bmp( bits ) ;
20b69855 90 CopyFromBitmap( bmp ) ;
2f1ae414
SC
91}
92
5a7d70fe 93wxIcon::wxIcon( char **bits )
2f1ae414 94{
5a7d70fe 95 wxBitmap bmp( bits ) ;
20b69855 96 CopyFromBitmap( bmp ) ;
2f1ae414
SC
97}
98
5a7d70fe
DS
99wxIcon::wxIcon(
100 const wxString& icon_file, int flags,
101 int desiredWidth, int desiredHeight )
e9576ca5 102{
5a7d70fe 103 LoadFile( icon_file, (wxBitmapType) flags, desiredWidth, desiredHeight );
e9576ca5
SC
104}
105
52734360
VZ
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
968c951f 113 m_refData = new wxIconRefData( icon, size.x, size.y ) ;
52734360
VZ
114}
115
e9576ca5
SC
116wxIcon::~wxIcon()
117{
118}
119
5a7d70fe 120WXHICON wxIcon::GetHICON() const
20b69855
SC
121{
122 wxASSERT( Ok() ) ;
5a7d70fe 123
20b69855
SC
124 return (WXHICON) ((wxIconRefData*)m_refData)->GetHICON() ;
125}
126
127int wxIcon::GetWidth() const
128{
ea68b706
SC
129 wxCHECK_MSG( Ok(), -1, wxT("invalid icon") );
130
131 return M_ICONDATA->GetWidth();
20b69855
SC
132}
133
134int wxIcon::GetHeight() const
135{
ea68b706
SC
136 wxCHECK_MSG( Ok(), -1, wxT("invalid icon") );
137
138 return M_ICONDATA->GetHeight();
20b69855
SC
139}
140
5a7d70fe
DS
141int wxIcon::GetDepth() const
142{
143 return 32;
c605b871
KO
144}
145
89954433 146void wxIcon::SetDepth( int WXUNUSED(depth) )
5a7d70fe 147{
c605b871
KO
148}
149
89954433 150void wxIcon::SetWidth( int WXUNUSED(width) )
5a7d70fe 151{
c605b871
KO
152}
153
89954433 154void wxIcon::SetHeight( int WXUNUSED(height) )
5a7d70fe 155{
c605b871
KO
156}
157
b7cacb43 158bool wxIcon::IsOk() const
20b69855
SC
159{
160 return m_refData != NULL ;
161}
162
5a7d70fe
DS
163bool wxIcon::LoadFile(
164 const wxString& filename, wxBitmapType type,
165 int desiredWidth, int desiredHeight )
e9576ca5 166{
e40298d5 167 UnRef();
5a7d70fe 168
20b69855 169 if ( type == wxBITMAP_TYPE_ICON_RESOURCE )
1996c23f 170 {
20b69855 171 OSType theId = 0 ;
5a7d70fe 172
20b69855
SC
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 }
52734360
VZ
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 }
20b69855 201 else
5a7d70fe 202 {
6239ee05
SC
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 )
20b69855 223 {
6239ee05 224 // TODO add other attempts to load it from files etc here
20b69855 225 }
6239ee05
SC
226 if ( iconRef )
227 {
968c951f 228 m_refData = new wxIconRefData( (WXHICON) iconRef, desiredWidth, desiredHeight ) ;
6239ee05
SC
229 return true ;
230 }
20b69855 231 }
5a7d70fe 232
20b69855
SC
233 if ( theId != 0 )
234 {
235 IconRef iconRef = NULL ;
5a7d70fe 236 verify_noerr( GetIconRef( kOnSystemDisk, kSystemIconsCreator, theId, &iconRef ) ) ;
20b69855
SC
237 if ( iconRef )
238 {
968c951f 239 m_refData = new wxIconRefData( (WXHICON) iconRef, desiredWidth, desiredHeight ) ;
5a7d70fe
DS
240
241 return true ;
20b69855
SC
242 }
243 }
5a7d70fe
DS
244
245 return false ;
1996c23f 246 }
e40298d5 247 else
1996c23f 248 {
5a7d70fe 249 wxBitmapHandler *handler = wxBitmap::FindHandler( type );
20b69855
SC
250
251 if ( handler )
1996c23f 252 {
20b69855 253 wxBitmap bmp ;
5a7d70fe 254 if ( handler->LoadFile( &bmp , filename, type, desiredWidth, desiredHeight ))
20b69855
SC
255 {
256 CopyFromBitmap( bmp ) ;
5a7d70fe 257
20b69855
SC
258 return true ;
259 }
5a7d70fe 260
20b69855
SC
261 return false ;
262 }
263 else
264 {
da76c2ce 265#if wxUSE_IMAGE
5a7d70fe
DS
266 wxImage loadimage( filename, type );
267 if (loadimage.Ok())
20b69855
SC
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 ) ;
155ecd4c 275
20b69855
SC
276 wxBitmap bmp( loadimage );
277 CopyFromBitmap( bmp ) ;
5a7d70fe 278
20b69855
SC
279 return true;
280 }
da76c2ce 281#endif
1996c23f
SC
282 }
283 }
20b69855 284 return true ;
e9576ca5
SC
285}
286
5a7d70fe 287void wxIcon::CopyFromBitmap( const wxBitmap& bmp )
d062e17f 288{
20b69855 289 UnRef() ;
5a7d70fe 290
796a6ef0 291 // as the bitmap owns that ref, we have to acquire it as well
968c951f
SC
292 IconRef iconRef = bmp.CreateIconRef() ;
293 m_refData = new wxIconRefData( (WXHICON) iconRef, bmp.GetWidth(), bmp.GetHeight() ) ;
d062e17f
GD
294}
295
519cb848
SC
296IMPLEMENT_DYNAMIC_CLASS(wxICONResourceHandler, wxBitmapHandler)
297
5a7d70fe 298bool wxICONResourceHandler::LoadFile(
89954433 299 wxBitmap *bitmap, const wxString& name, long WXUNUSED(flags),
5a7d70fe 300 int desiredWidth, int desiredHeight )
519cb848 301{
20b69855
SC
302 wxIcon icon ;
303 icon.LoadFile( name , wxBITMAP_TYPE_ICON_RESOURCE , desiredWidth , desiredHeight ) ;
304 bitmap->CopyFromIcon( icon ) ;
5a7d70fe 305
20b69855 306 return bitmap->Ok() ;
03e11df5 307}
5a7d70fe 308