]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/icon.cpp
applied patch 1466370
[wxWidgets.git] / src / mac / carbon / icon.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #include "wx/image.h"
16 #include "wx/mac/private.h"
17
18 IMPLEMENT_DYNAMIC_CLASS(wxIcon, wxBitmap)
19
20 #define M_ICONDATA ((wxIconRefData *)m_refData)
21
22
23 wxIcon::wxIcon()
24 {
25 }
26
27 wxIcon::wxIcon( const char bits[], int width, int height )
28 {
29 wxBitmap bmp( bits, width, height ) ;
30 CopyFromBitmap( bmp ) ;
31 }
32
33 wxIcon::wxIcon( const char **bits )
34 {
35 wxBitmap bmp( bits ) ;
36 CopyFromBitmap( bmp ) ;
37 }
38
39 wxIcon::wxIcon( char **bits )
40 {
41 wxBitmap bmp( bits ) ;
42 CopyFromBitmap( bmp ) ;
43 }
44
45 wxIcon::wxIcon(
46 const wxString& icon_file, int flags,
47 int desiredWidth, int desiredHeight )
48 {
49 LoadFile( icon_file, (wxBitmapType) flags, desiredWidth, desiredHeight );
50 }
51
52 wxIcon::~wxIcon()
53 {
54 }
55
56 WXHICON wxIcon::GetHICON() const
57 {
58 wxASSERT( Ok() ) ;
59
60 return (WXHICON) ((wxIconRefData*)m_refData)->GetHICON() ;
61 }
62
63 int wxIcon::GetWidth() const
64 {
65 wxCHECK_MSG( Ok(), -1, wxT("invalid icon") );
66
67 return M_ICONDATA->GetWidth();
68 }
69
70 int wxIcon::GetHeight() const
71 {
72 wxCHECK_MSG( Ok(), -1, wxT("invalid icon") );
73
74 return M_ICONDATA->GetHeight();
75 }
76
77 int wxIcon::GetDepth() const
78 {
79 return 32;
80 }
81
82 void wxIcon::SetDepth( int depth )
83 {
84 }
85
86 void wxIcon::SetWidth( int width )
87 {
88 }
89
90 void wxIcon::SetHeight( int height )
91 {
92 }
93
94 bool wxIcon::Ok() const
95 {
96 return m_refData != NULL ;
97 }
98
99 bool wxIcon::LoadFile(
100 const wxString& filename, wxBitmapType type,
101 int desiredWidth, int desiredHeight )
102 {
103 UnRef();
104
105 if ( type == wxBITMAP_TYPE_ICON_RESOURCE )
106 {
107 OSType theId = 0 ;
108
109 if ( filename == wxT("wxICON_INFORMATION") )
110 {
111 theId = kAlertNoteIcon ;
112 }
113 else if ( filename == wxT("wxICON_QUESTION") )
114 {
115 theId = kAlertCautionIcon ;
116 }
117 else if ( filename == wxT("wxICON_WARNING") )
118 {
119 theId = kAlertCautionIcon ;
120 }
121 else if ( filename == wxT("wxICON_ERROR") )
122 {
123 theId = kAlertStopIcon ;
124 }
125 else
126 {
127 #if 0
128 Str255 theName ;
129 OSType theType ;
130 wxMacStringToPascal( name , theName ) ;
131
132 Handle resHandle = GetNamedResource( 'cicn' , theName ) ;
133 if ( resHandle != 0L )
134 {
135 GetResInfo( resHandle , &theId , &theType , theName ) ;
136 ReleaseResource( resHandle ) ;
137 }
138 #endif
139 }
140
141 if ( theId != 0 )
142 {
143 IconRef iconRef = NULL ;
144 verify_noerr( GetIconRef( kOnSystemDisk, kSystemIconsCreator, theId, &iconRef ) ) ;
145 if ( iconRef )
146 {
147 m_refData = new wxIconRefData( (WXHICON) iconRef ) ;
148
149 return true ;
150 }
151 }
152
153 return false ;
154 }
155 else
156 {
157 wxBitmapHandler *handler = wxBitmap::FindHandler( type );
158
159 if ( handler )
160 {
161 wxBitmap bmp ;
162 if ( handler->LoadFile( &bmp , filename, type, desiredWidth, desiredHeight ))
163 {
164 CopyFromBitmap( bmp ) ;
165
166 return true ;
167 }
168
169 return false ;
170 }
171 else
172 {
173 #if wxUSE_IMAGE
174 wxImage loadimage( filename, type );
175 if (loadimage.Ok())
176 {
177 if ( desiredWidth == -1 )
178 desiredWidth = loadimage.GetWidth() ;
179 if ( desiredHeight == -1 )
180 desiredHeight = loadimage.GetHeight() ;
181 if ( desiredWidth != loadimage.GetWidth() || desiredHeight != loadimage.GetHeight() )
182 loadimage.Rescale( desiredWidth , desiredHeight ) ;
183
184 wxBitmap bmp( loadimage );
185 CopyFromBitmap( bmp ) ;
186
187 return true;
188 }
189 #endif
190 }
191 }
192 return true ;
193 }
194
195 void wxIcon::CopyFromBitmap( const wxBitmap& bmp )
196 {
197 UnRef() ;
198
199 // as the bitmap owns that ref, we have to acquire it as well
200 IconRef iconRef = bmp.GetBitmapData()->GetIconRef() ;
201 AcquireIconRef( iconRef ) ;
202
203 m_refData = new wxIconRefData( (WXHICON) iconRef ) ;
204 M_ICONDATA->SetWidth( bmp.GetWidth() ) ;
205 M_ICONDATA->SetHeight( bmp.GetHeight() ) ;
206 }
207
208 wxIconRefData::wxIconRefData( WXHICON icon )
209 {
210 m_iconRef = MAC_WXHICON( icon ) ;
211
212 // Standard sizes
213 SetWidth( 32 ) ;
214 SetHeight( 32 ) ;
215 }
216
217 void wxIconRefData::Init()
218 {
219 m_iconRef = NULL ;
220 }
221
222 void wxIconRefData::Free()
223 {
224 if ( m_iconRef )
225 {
226 ReleaseIconRef( m_iconRef ) ;
227 m_iconRef = NULL ;
228 }
229 }
230
231 IMPLEMENT_DYNAMIC_CLASS(wxICONResourceHandler, wxBitmapHandler)
232
233 bool wxICONResourceHandler::LoadFile(
234 wxBitmap *bitmap, const wxString& name, long flags,
235 int desiredWidth, int desiredHeight )
236 {
237 wxIcon icon ;
238 icon.LoadFile( name , wxBITMAP_TYPE_ICON_RESOURCE , desiredWidth , desiredHeight ) ;
239 bitmap->CopyFromIcon( icon ) ;
240
241 return bitmap->Ok() ;
242 }
243