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