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