]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/icon.cpp
remove the drag-and-drop compatibility hack for extracting the embedded size of the...
[wxWidgets.git] / src / mac / carbon / icon.cpp
CommitLineData
e9576ca5 1/////////////////////////////////////////////////////////////////////////////
155ecd4c 2// Name: src/mac/carbon/icon.cpp
e9576ca5 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
155ecd4c 9// Licence: wxWindows licence
e9576ca5
SC
10/////////////////////////////////////////////////////////////////////////////
11
3d1a4878
SC
12#include "wx/wxprec.h"
13
e9576ca5 14#include "wx/icon.h"
155ecd4c
WS
15
16#ifndef WX_PRECOMP
17 #include "wx/image.h"
18#endif
19
76a5e5d2
SC
20#include "wx/mac/private.h"
21
5a7d70fe 22IMPLEMENT_DYNAMIC_CLASS(wxIcon, wxBitmap)
ea68b706 23
5a7d70fe 24#define M_ICONDATA ((wxIconRefData *)m_refData)
76a5e5d2 25
e9576ca5 26
e9576ca5
SC
27wxIcon::wxIcon()
28{
29}
30
5a7d70fe 31wxIcon::wxIcon( const char bits[], int width, int height )
e9576ca5 32{
5a7d70fe 33 wxBitmap bmp( bits, width, height ) ;
20b69855 34 CopyFromBitmap( bmp ) ;
e9576ca5
SC
35}
36
5a7d70fe 37wxIcon::wxIcon( const char **bits )
2f1ae414 38{
5a7d70fe 39 wxBitmap bmp( bits ) ;
20b69855 40 CopyFromBitmap( bmp ) ;
2f1ae414
SC
41}
42
5a7d70fe 43wxIcon::wxIcon( char **bits )
2f1ae414 44{
5a7d70fe 45 wxBitmap bmp( bits ) ;
20b69855 46 CopyFromBitmap( bmp ) ;
2f1ae414
SC
47}
48
5a7d70fe
DS
49wxIcon::wxIcon(
50 const wxString& icon_file, int flags,
51 int desiredWidth, int desiredHeight )
e9576ca5 52{
5a7d70fe 53 LoadFile( icon_file, (wxBitmapType) flags, desiredWidth, desiredHeight );
e9576ca5
SC
54}
55
52734360
VZ
56wxIcon::wxIcon(WXHICON icon, const wxSize& size)
57 : wxGDIObject()
58{
59 // as the icon owns that ref, we have to acquire it as well
60 if (icon)
61 AcquireIconRef( (IconRef) icon ) ;
62
63 m_refData = new wxIconRefData( icon ) ;
64 M_ICONDATA->SetWidth( size.x ) ;
65 M_ICONDATA->SetHeight( size.y ) ;
66}
67
e9576ca5
SC
68wxIcon::~wxIcon()
69{
70}
71
5a7d70fe 72WXHICON wxIcon::GetHICON() const
20b69855
SC
73{
74 wxASSERT( Ok() ) ;
5a7d70fe 75
20b69855
SC
76 return (WXHICON) ((wxIconRefData*)m_refData)->GetHICON() ;
77}
78
79int wxIcon::GetWidth() const
80{
ea68b706
SC
81 wxCHECK_MSG( Ok(), -1, wxT("invalid icon") );
82
83 return M_ICONDATA->GetWidth();
20b69855
SC
84}
85
86int wxIcon::GetHeight() const
87{
ea68b706
SC
88 wxCHECK_MSG( Ok(), -1, wxT("invalid icon") );
89
90 return M_ICONDATA->GetHeight();
20b69855
SC
91}
92
5a7d70fe
DS
93int wxIcon::GetDepth() const
94{
95 return 32;
c605b871
KO
96}
97
5a7d70fe
DS
98void wxIcon::SetDepth( int depth )
99{
c605b871
KO
100}
101
5a7d70fe
DS
102void wxIcon::SetWidth( int width )
103{
c605b871
KO
104}
105
5a7d70fe
DS
106void wxIcon::SetHeight( int height )
107{
c605b871
KO
108}
109
b7cacb43 110bool wxIcon::IsOk() const
20b69855
SC
111{
112 return m_refData != NULL ;
113}
114
5a7d70fe
DS
115bool wxIcon::LoadFile(
116 const wxString& filename, wxBitmapType type,
117 int desiredWidth, int desiredHeight )
e9576ca5 118{
e40298d5 119 UnRef();
5a7d70fe 120
20b69855 121 if ( type == wxBITMAP_TYPE_ICON_RESOURCE )
1996c23f 122 {
20b69855 123 OSType theId = 0 ;
5a7d70fe 124
20b69855
SC
125 if ( filename == wxT("wxICON_INFORMATION") )
126 {
127 theId = kAlertNoteIcon ;
128 }
129 else if ( filename == wxT("wxICON_QUESTION") )
130 {
131 theId = kAlertCautionIcon ;
132 }
133 else if ( filename == wxT("wxICON_WARNING") )
134 {
135 theId = kAlertCautionIcon ;
136 }
137 else if ( filename == wxT("wxICON_ERROR") )
138 {
139 theId = kAlertStopIcon ;
140 }
52734360
VZ
141 else if ( filename == wxT("wxICON_FOLDER") )
142 {
143 theId = kGenericFolderIcon ;
144 }
145 else if ( filename == wxT("wxICON_FOLDER_OPEN") )
146 {
147 theId = kOpenFolderIcon ;
148 }
149 else if ( filename == wxT("wxICON_NORMAL_FILE") )
150 {
151 theId = kGenericDocumentIcon ;
152 }
20b69855 153 else
5a7d70fe
DS
154 {
155#if 0
20b69855
SC
156 Str255 theName ;
157 OSType theType ;
158 wxMacStringToPascal( name , theName ) ;
5a7d70fe 159
20b69855
SC
160 Handle resHandle = GetNamedResource( 'cicn' , theName ) ;
161 if ( resHandle != 0L )
162 {
163 GetResInfo( resHandle , &theId , &theType , theName ) ;
164 ReleaseResource( resHandle ) ;
165 }
5a7d70fe 166#endif
20b69855 167 }
5a7d70fe 168
20b69855
SC
169 if ( theId != 0 )
170 {
171 IconRef iconRef = NULL ;
5a7d70fe 172 verify_noerr( GetIconRef( kOnSystemDisk, kSystemIconsCreator, theId, &iconRef ) ) ;
20b69855
SC
173 if ( iconRef )
174 {
175 m_refData = new wxIconRefData( (WXHICON) iconRef ) ;
5a7d70fe
DS
176
177 return true ;
20b69855
SC
178 }
179 }
5a7d70fe
DS
180
181 return false ;
1996c23f 182 }
e40298d5 183 else
1996c23f 184 {
5a7d70fe 185 wxBitmapHandler *handler = wxBitmap::FindHandler( type );
20b69855
SC
186
187 if ( handler )
1996c23f 188 {
20b69855 189 wxBitmap bmp ;
5a7d70fe 190 if ( handler->LoadFile( &bmp , filename, type, desiredWidth, desiredHeight ))
20b69855
SC
191 {
192 CopyFromBitmap( bmp ) ;
5a7d70fe 193
20b69855
SC
194 return true ;
195 }
5a7d70fe 196
20b69855
SC
197 return false ;
198 }
199 else
200 {
da76c2ce 201#if wxUSE_IMAGE
5a7d70fe
DS
202 wxImage loadimage( filename, type );
203 if (loadimage.Ok())
20b69855
SC
204 {
205 if ( desiredWidth == -1 )
206 desiredWidth = loadimage.GetWidth() ;
207 if ( desiredHeight == -1 )
208 desiredHeight = loadimage.GetHeight() ;
209 if ( desiredWidth != loadimage.GetWidth() || desiredHeight != loadimage.GetHeight() )
210 loadimage.Rescale( desiredWidth , desiredHeight ) ;
155ecd4c 211
20b69855
SC
212 wxBitmap bmp( loadimage );
213 CopyFromBitmap( bmp ) ;
5a7d70fe 214
20b69855
SC
215 return true;
216 }
da76c2ce 217#endif
1996c23f
SC
218 }
219 }
20b69855 220 return true ;
e9576ca5
SC
221}
222
5a7d70fe 223void wxIcon::CopyFromBitmap( const wxBitmap& bmp )
d062e17f 224{
20b69855 225 UnRef() ;
5a7d70fe 226
796a6ef0
SC
227 // as the bitmap owns that ref, we have to acquire it as well
228 IconRef iconRef = bmp.GetBitmapData()->GetIconRef() ;
229 AcquireIconRef( iconRef ) ;
5a7d70fe 230
796a6ef0 231 m_refData = new wxIconRefData( (WXHICON) iconRef ) ;
ea68b706
SC
232 M_ICONDATA->SetWidth( bmp.GetWidth() ) ;
233 M_ICONDATA->SetHeight( bmp.GetHeight() ) ;
20b69855
SC
234}
235
236wxIconRefData::wxIconRefData( WXHICON icon )
237{
238 m_iconRef = MAC_WXHICON( icon ) ;
5a7d70fe
DS
239
240 // Standard sizes
ea68b706
SC
241 SetWidth( 32 ) ;
242 SetHeight( 32 ) ;
20b69855
SC
243}
244
5a7d70fe 245void wxIconRefData::Init()
20b69855
SC
246{
247 m_iconRef = NULL ;
248}
249
250void wxIconRefData::Free()
251{
252 if ( m_iconRef )
253 {
254 ReleaseIconRef( m_iconRef ) ;
255 m_iconRef = NULL ;
256 }
d062e17f
GD
257}
258
519cb848
SC
259IMPLEMENT_DYNAMIC_CLASS(wxICONResourceHandler, wxBitmapHandler)
260
5a7d70fe
DS
261bool wxICONResourceHandler::LoadFile(
262 wxBitmap *bitmap, const wxString& name, long flags,
263 int desiredWidth, int desiredHeight )
519cb848 264{
20b69855
SC
265 wxIcon icon ;
266 icon.LoadFile( name , wxBITMAP_TYPE_ICON_RESOURCE , desiredWidth , desiredHeight ) ;
267 bitmap->CopyFromIcon( icon ) ;
5a7d70fe 268
20b69855 269 return bitmap->Ok() ;
03e11df5 270}
5a7d70fe 271