]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/icon.cpp
icon - bitmap conversions corrections (size is not always the 'standard' 32 pixels)
[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 bool wxIcon::Ok() const
87 {
88 return m_refData != NULL ;
89 }
90
91 bool wxIcon::LoadFile(const wxString& filename, wxBitmapType type,
92 int desiredWidth, int desiredHeight)
93 {
94 UnRef();
95
96 if ( type == wxBITMAP_TYPE_ICON_RESOURCE )
97 {
98 OSType theId = 0 ;
99 if ( filename == wxT("wxICON_INFORMATION") )
100 {
101 theId = kAlertNoteIcon ;
102 }
103 else if ( filename == wxT("wxICON_QUESTION") )
104 {
105 theId = kAlertCautionIcon ;
106 }
107 else if ( filename == wxT("wxICON_WARNING") )
108 {
109 theId = kAlertCautionIcon ;
110 }
111 else if ( filename == wxT("wxICON_ERROR") )
112 {
113 theId = kAlertStopIcon ;
114 }
115 else
116 {/*
117 Str255 theName ;
118 OSType theType ;
119 wxMacStringToPascal( name , theName ) ;
120
121 Handle resHandle = GetNamedResource( 'cicn' , theName ) ;
122 if ( resHandle != 0L )
123 {
124 GetResInfo( resHandle , &theId , &theType , theName ) ;
125 ReleaseResource( resHandle ) ;
126 }
127 */
128 }
129 if ( theId != 0 )
130 {
131 IconRef iconRef = NULL ;
132 verify_noerr(GetIconRef(kOnSystemDisk,kSystemIconsCreator,theId, &iconRef)) ;
133 if ( iconRef )
134 {
135 m_refData = new wxIconRefData( (WXHICON) iconRef ) ;
136 return TRUE ;
137 }
138 }
139 return FALSE ;
140 }
141 else
142 {
143 wxBitmapHandler *handler = wxBitmap::FindHandler(type);
144
145 if ( handler )
146 {
147 wxBitmap bmp ;
148 if ( handler->LoadFile(&bmp , filename, type, desiredWidth, desiredHeight ))
149 {
150 CopyFromBitmap( bmp ) ;
151 return true ;
152 }
153 return false ;
154 }
155 else
156 {
157 wxImage loadimage(filename, type);
158 if (loadimage.Ok())
159 {
160 if ( desiredWidth == -1 )
161 desiredWidth = loadimage.GetWidth() ;
162 if ( desiredHeight == -1 )
163 desiredHeight = loadimage.GetHeight() ;
164 if ( desiredWidth != loadimage.GetWidth() || desiredHeight != loadimage.GetHeight() )
165 loadimage.Rescale( desiredWidth , desiredHeight ) ;
166 wxBitmap bmp( loadimage );
167 CopyFromBitmap( bmp ) ;
168 return true;
169 }
170 }
171 }
172 return true ;
173 }
174
175 void wxIcon::CopyFromBitmap(const wxBitmap& bmp)
176 {
177 UnRef() ;
178
179 m_refData = new wxIconRefData( (WXHICON) wxMacCreateIconRef( bmp ) ) ;
180 M_ICONDATA->SetWidth( bmp.GetWidth() ) ;
181 M_ICONDATA->SetHeight( bmp.GetHeight() ) ;
182 }
183
184 wxIconRefData::wxIconRefData( WXHICON icon )
185 {
186 m_iconRef = MAC_WXHICON( icon ) ;
187 // Std sizes
188 SetWidth( 32 ) ;
189 SetHeight( 32 ) ;
190 }
191
192 void wxIconRefData::Init()
193 {
194 m_iconRef = NULL ;
195 }
196
197 void wxIconRefData::Free()
198 {
199 if ( m_iconRef )
200 {
201 ReleaseIconRef( m_iconRef ) ;
202 m_iconRef = NULL ;
203 }
204 }
205
206 IMPLEMENT_DYNAMIC_CLASS(wxICONResourceHandler, wxBitmapHandler)
207
208 bool wxICONResourceHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
209 int desiredWidth, int desiredHeight)
210 {
211 wxIcon icon ;
212 icon.LoadFile( name , wxBITMAP_TYPE_ICON_RESOURCE , desiredWidth , desiredHeight ) ;
213 bitmap->CopyFromIcon( icon ) ;
214 return bitmap->Ok() ;
215 }