]>
Commit | Line | Data |
---|---|---|
e9576ca5 SC |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: icon.cpp | |
3 | // Purpose: wxIcon class | |
a31a5f85 | 4 | // Author: Stefan Csomor |
e9576ca5 | 5 | // Modified by: |
a31a5f85 | 6 | // Created: 1998-01-01 |
e9576ca5 | 7 | // RCS-ID: $Id$ |
a31a5f85 | 8 | // Copyright: (c) Stefan Csomor |
65571936 | 9 | // Licence: wxWindows licence |
e9576ca5 SC |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
3d1a4878 SC |
12 | #include "wx/wxprec.h" |
13 | ||
e9576ca5 SC |
14 | #include "wx/icon.h" |
15 | ||
e9576ca5 | 16 | IMPLEMENT_DYNAMIC_CLASS(wxIcon, wxBitmap) |
e9576ca5 | 17 | |
1996c23f | 18 | #include "wx/image.h" |
76a5e5d2 SC |
19 | #include "wx/mac/private.h" |
20 | ||
ea68b706 SC |
21 | #define M_ICONDATA ((wxIconRefData *)m_refData) |
22 | ||
76a5e5d2 | 23 | |
e9576ca5 SC |
24 | /* |
25 | * Icons | |
26 | */ | |
27 | ||
e9576ca5 SC |
28 | wxIcon::wxIcon() |
29 | { | |
30 | } | |
31 | ||
20b69855 | 32 | wxIcon::wxIcon(const char bits[], int width, int height) |
e9576ca5 | 33 | { |
20b69855 SC |
34 | wxBitmap bmp(bits,width,height) ; |
35 | CopyFromBitmap( bmp ) ; | |
e9576ca5 SC |
36 | } |
37 | ||
20b69855 | 38 | wxIcon::wxIcon( const char **bits ) |
2f1ae414 | 39 | { |
20b69855 SC |
40 | wxBitmap bmp(bits) ; |
41 | CopyFromBitmap( bmp ) ; | |
2f1ae414 SC |
42 | } |
43 | ||
20b69855 | 44 | wxIcon::wxIcon( char **bits ) |
2f1ae414 | 45 | { |
20b69855 SC |
46 | wxBitmap bmp(bits) ; |
47 | CopyFromBitmap( bmp ) ; | |
2f1ae414 SC |
48 | } |
49 | ||
76a5e5d2 | 50 | wxIcon::wxIcon(const wxString& icon_file, int flags, |
e9576ca5 | 51 | int desiredWidth, int desiredHeight) |
e9576ca5 | 52 | { |
76a5e5d2 | 53 | LoadFile(icon_file, (wxBitmapType) flags, desiredWidth, desiredHeight); |
e9576ca5 SC |
54 | } |
55 | ||
56 | wxIcon::~wxIcon() | |
57 | { | |
58 | } | |
59 | ||
20b69855 SC |
60 | WXHICON wxIcon::GetHICON() const |
61 | { | |
62 | wxASSERT( Ok() ) ; | |
63 | return (WXHICON) ((wxIconRefData*)m_refData)->GetHICON() ; | |
64 | } | |
65 | ||
66 | int wxIcon::GetWidth() const | |
67 | { | |
ea68b706 SC |
68 | wxCHECK_MSG( Ok(), -1, wxT("invalid icon") ); |
69 | ||
70 | return M_ICONDATA->GetWidth(); | |
20b69855 SC |
71 | } |
72 | ||
73 | int wxIcon::GetHeight() const | |
74 | { | |
ea68b706 SC |
75 | wxCHECK_MSG( Ok(), -1, wxT("invalid icon") ); |
76 | ||
77 | return M_ICONDATA->GetHeight(); | |
20b69855 SC |
78 | } |
79 | ||
c605b871 KO |
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 | ||
20b69855 SC |
96 | bool wxIcon::Ok() const |
97 | { | |
98 | return m_refData != NULL ; | |
99 | } | |
100 | ||
76a5e5d2 | 101 | bool wxIcon::LoadFile(const wxString& filename, wxBitmapType type, |
e9576ca5 SC |
102 | int desiredWidth, int desiredHeight) |
103 | { | |
e40298d5 JS |
104 | UnRef(); |
105 | ||
20b69855 | 106 | if ( type == wxBITMAP_TYPE_ICON_RESOURCE ) |
1996c23f | 107 | { |
20b69855 SC |
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 ; | |
1996c23f | 150 | } |
e40298d5 | 151 | else |
1996c23f | 152 | { |
20b69855 SC |
153 | wxBitmapHandler *handler = wxBitmap::FindHandler(type); |
154 | ||
155 | if ( handler ) | |
1996c23f | 156 | { |
20b69855 SC |
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 | { | |
da76c2ce | 167 | #if wxUSE_IMAGE |
20b69855 SC |
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 | } | |
da76c2ce | 181 | #endif |
1996c23f SC |
182 | } |
183 | } | |
20b69855 | 184 | return true ; |
e9576ca5 SC |
185 | } |
186 | ||
d062e17f GD |
187 | void wxIcon::CopyFromBitmap(const wxBitmap& bmp) |
188 | { | |
20b69855 SC |
189 | UnRef() ; |
190 | ||
796a6ef0 SC |
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 ) ; | |
ea68b706 SC |
195 | M_ICONDATA->SetWidth( bmp.GetWidth() ) ; |
196 | M_ICONDATA->SetHeight( bmp.GetHeight() ) ; | |
20b69855 SC |
197 | } |
198 | ||
199 | wxIconRefData::wxIconRefData( WXHICON icon ) | |
200 | { | |
201 | m_iconRef = MAC_WXHICON( icon ) ; | |
ea68b706 SC |
202 | // Std sizes |
203 | SetWidth( 32 ) ; | |
204 | SetHeight( 32 ) ; | |
20b69855 SC |
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 | } | |
d062e17f GD |
219 | } |
220 | ||
519cb848 SC |
221 | IMPLEMENT_DYNAMIC_CLASS(wxICONResourceHandler, wxBitmapHandler) |
222 | ||
223 | bool wxICONResourceHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags, | |
e40298d5 | 224 | int desiredWidth, int desiredHeight) |
519cb848 | 225 | { |
20b69855 SC |
226 | wxIcon icon ; |
227 | icon.LoadFile( name , wxBITMAP_TYPE_ICON_RESOURCE , desiredWidth , desiredHeight ) ; | |
228 | bitmap->CopyFromIcon( icon ) ; | |
229 | return bitmap->Ok() ; | |
03e11df5 | 230 | } |