]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/icon.cpp
must be static of course, otherwise items just get appended for every menu-build
[wxWidgets.git] / src / mac / carbon / icon.cpp
CommitLineData
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 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
e9576ca5
SC
13#pragma implementation "icon.h"
14#endif
15
3d1a4878
SC
16#include "wx/wxprec.h"
17
e9576ca5
SC
18#include "wx/icon.h"
19
2f1ae414 20#if !USE_SHARED_LIBRARIES
e9576ca5 21IMPLEMENT_DYNAMIC_CLASS(wxIcon, wxBitmap)
2f1ae414 22#endif
e9576ca5 23
1996c23f 24#include "wx/image.h"
76a5e5d2
SC
25#include "wx/mac/private.h"
26
ea68b706
SC
27#define M_ICONDATA ((wxIconRefData *)m_refData)
28
76a5e5d2 29
e9576ca5
SC
30/*
31 * Icons
32 */
33
e9576ca5
SC
34wxIcon::wxIcon()
35{
36}
37
20b69855 38wxIcon::wxIcon(const char bits[], int width, int height)
e9576ca5 39{
20b69855
SC
40 wxBitmap bmp(bits,width,height) ;
41 CopyFromBitmap( bmp ) ;
e9576ca5
SC
42}
43
20b69855 44wxIcon::wxIcon( const char **bits )
2f1ae414 45{
20b69855
SC
46 wxBitmap bmp(bits) ;
47 CopyFromBitmap( bmp ) ;
2f1ae414
SC
48}
49
20b69855 50wxIcon::wxIcon( char **bits )
2f1ae414 51{
20b69855
SC
52 wxBitmap bmp(bits) ;
53 CopyFromBitmap( bmp ) ;
2f1ae414
SC
54}
55
76a5e5d2 56wxIcon::wxIcon(const wxString& icon_file, int flags,
e9576ca5 57 int desiredWidth, int desiredHeight)
e9576ca5 58{
76a5e5d2 59 LoadFile(icon_file, (wxBitmapType) flags, desiredWidth, desiredHeight);
e9576ca5
SC
60}
61
62wxIcon::~wxIcon()
63{
64}
65
20b69855
SC
66WXHICON wxIcon::GetHICON() const
67{
68 wxASSERT( Ok() ) ;
69 return (WXHICON) ((wxIconRefData*)m_refData)->GetHICON() ;
70}
71
72int wxIcon::GetWidth() const
73{
ea68b706
SC
74 wxCHECK_MSG( Ok(), -1, wxT("invalid icon") );
75
76 return M_ICONDATA->GetWidth();
20b69855
SC
77}
78
79int wxIcon::GetHeight() const
80{
ea68b706
SC
81 wxCHECK_MSG( Ok(), -1, wxT("invalid icon") );
82
83 return M_ICONDATA->GetHeight();
20b69855
SC
84}
85
c605b871
KO
86int wxIcon::GetDepth() const{
87 return 32;
88}
89
90void wxIcon::SetDepth(int depth){
91
92}
93
94void wxIcon::SetWidth(int width){
95
96}
97
98void wxIcon::SetHeight(int height){
99
100}
101
20b69855
SC
102bool wxIcon::Ok() const
103{
104 return m_refData != NULL ;
105}
106
76a5e5d2 107bool wxIcon::LoadFile(const wxString& filename, wxBitmapType type,
e9576ca5
SC
108 int desiredWidth, int desiredHeight)
109{
e40298d5
JS
110 UnRef();
111
20b69855 112 if ( type == wxBITMAP_TYPE_ICON_RESOURCE )
1996c23f 113 {
20b69855
SC
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 ;
1996c23f 156 }
e40298d5 157 else
1996c23f 158 {
20b69855
SC
159 wxBitmapHandler *handler = wxBitmap::FindHandler(type);
160
161 if ( handler )
1996c23f 162 {
20b69855
SC
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 {
da76c2ce 173#if wxUSE_IMAGE
20b69855
SC
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 }
da76c2ce 187#endif
1996c23f
SC
188 }
189 }
20b69855 190 return true ;
e9576ca5
SC
191}
192
d062e17f
GD
193void wxIcon::CopyFromBitmap(const wxBitmap& bmp)
194{
20b69855
SC
195 UnRef() ;
196
796a6ef0
SC
197 // as the bitmap owns that ref, we have to acquire it as well
198 IconRef iconRef = bmp.GetBitmapData()->GetIconRef() ;
199 AcquireIconRef( iconRef ) ;
200 m_refData = new wxIconRefData( (WXHICON) iconRef ) ;
ea68b706
SC
201 M_ICONDATA->SetWidth( bmp.GetWidth() ) ;
202 M_ICONDATA->SetHeight( bmp.GetHeight() ) ;
20b69855
SC
203}
204
205wxIconRefData::wxIconRefData( WXHICON icon )
206{
207 m_iconRef = MAC_WXHICON( icon ) ;
ea68b706
SC
208 // Std sizes
209 SetWidth( 32 ) ;
210 SetHeight( 32 ) ;
20b69855
SC
211}
212
213void wxIconRefData::Init()
214{
215 m_iconRef = NULL ;
216}
217
218void wxIconRefData::Free()
219{
220 if ( m_iconRef )
221 {
222 ReleaseIconRef( m_iconRef ) ;
223 m_iconRef = NULL ;
224 }
d062e17f
GD
225}
226
519cb848
SC
227IMPLEMENT_DYNAMIC_CLASS(wxICONResourceHandler, wxBitmapHandler)
228
229bool wxICONResourceHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
e40298d5 230 int desiredWidth, int desiredHeight)
519cb848 231{
20b69855
SC
232 wxIcon icon ;
233 icon.LoadFile( name , wxBITMAP_TYPE_ICON_RESOURCE , desiredWidth , desiredHeight ) ;
234 bitmap->CopyFromIcon( icon ) ;
235 return bitmap->Ok() ;
03e11df5 236}