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