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