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