]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/icon.cpp
Patch by David Brinegar to fix menubar updating after modal dialog shown
[wxWidgets.git] / src / mac / carbon / icon.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: icon.cpp
3 // Purpose: wxIcon class
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "icon.h"
14 #endif
15
16 #include "wx/wxprec.h"
17
18 #include "wx/icon.h"
19
20 #if !USE_SHARED_LIBRARIES
21 IMPLEMENT_DYNAMIC_CLASS(wxIcon, wxBitmap)
22 #endif
23
24 #include "wx/image.h"
25 #include "wx/mac/private.h"
26
27
28 /*
29 * Icons
30 */
31
32 wxIcon::wxIcon()
33 {
34 }
35
36 wxIcon::wxIcon(const char bits[], int width, int height)
37 {
38 wxBitmap bmp(bits,width,height) ;
39 CopyFromBitmap( bmp ) ;
40 }
41
42 wxIcon::wxIcon( const char **bits )
43 {
44 wxBitmap bmp(bits) ;
45 CopyFromBitmap( bmp ) ;
46 }
47
48 wxIcon::wxIcon( char **bits )
49 {
50 wxBitmap bmp(bits) ;
51 CopyFromBitmap( bmp ) ;
52 }
53
54 wxIcon::wxIcon(const wxString& icon_file, int flags,
55 int desiredWidth, int desiredHeight)
56 {
57 LoadFile(icon_file, (wxBitmapType) flags, desiredWidth, desiredHeight);
58 }
59
60 wxIcon::~wxIcon()
61 {
62 }
63
64 WXHICON wxIcon::GetHICON() const
65 {
66 wxASSERT( Ok() ) ;
67 return (WXHICON) ((wxIconRefData*)m_refData)->GetHICON() ;
68 }
69
70 int wxIcon::GetWidth() const
71 {
72 return 32 ;
73 }
74
75 int wxIcon::GetHeight() const
76 {
77 return 32 ;
78 }
79
80 bool wxIcon::Ok() const
81 {
82 return m_refData != NULL ;
83 }
84
85 bool wxIcon::LoadFile(const wxString& filename, wxBitmapType type,
86 int desiredWidth, int desiredHeight)
87 {
88 UnRef();
89
90 if ( type == wxBITMAP_TYPE_ICON_RESOURCE )
91 {
92 OSType theId = 0 ;
93 if ( filename == wxT("wxICON_INFORMATION") )
94 {
95 theId = kAlertNoteIcon ;
96 }
97 else if ( filename == wxT("wxICON_QUESTION") )
98 {
99 theId = kAlertCautionIcon ;
100 }
101 else if ( filename == wxT("wxICON_WARNING") )
102 {
103 theId = kAlertCautionIcon ;
104 }
105 else if ( filename == wxT("wxICON_ERROR") )
106 {
107 theId = kAlertStopIcon ;
108 }
109 else
110 {/*
111 Str255 theName ;
112 OSType theType ;
113 wxMacStringToPascal( name , theName ) ;
114
115 Handle resHandle = GetNamedResource( 'cicn' , theName ) ;
116 if ( resHandle != 0L )
117 {
118 GetResInfo( resHandle , &theId , &theType , theName ) ;
119 ReleaseResource( resHandle ) ;
120 }
121 */
122 }
123 if ( theId != 0 )
124 {
125 IconRef iconRef = NULL ;
126 verify_noerr(GetIconRef(kOnSystemDisk,kSystemIconsCreator,theId, &iconRef)) ;
127 if ( iconRef )
128 {
129 m_refData = new wxIconRefData( (WXHICON) iconRef ) ;
130 return TRUE ;
131 }
132 }
133 return FALSE ;
134 }
135 else
136 {
137 wxBitmapHandler *handler = wxBitmap::FindHandler(type);
138
139 if ( handler )
140 {
141 wxBitmap bmp ;
142 if ( handler->LoadFile(&bmp , filename, type, desiredWidth, desiredHeight ))
143 {
144 CopyFromBitmap( bmp ) ;
145 return true ;
146 }
147 return false ;
148 }
149 else
150 {
151 wxImage loadimage(filename, type);
152 if (loadimage.Ok())
153 {
154 if ( desiredWidth == -1 )
155 desiredWidth = loadimage.GetWidth() ;
156 if ( desiredHeight == -1 )
157 desiredHeight = loadimage.GetHeight() ;
158 if ( desiredWidth != loadimage.GetWidth() || desiredHeight != loadimage.GetHeight() )
159 loadimage.Rescale( desiredWidth , desiredHeight ) ;
160 wxBitmap bmp( loadimage );
161 CopyFromBitmap( bmp ) ;
162 return true;
163 }
164 }
165 }
166 return true ;
167 }
168
169 void wxIcon::CopyFromBitmap(const wxBitmap& bmp)
170 {
171 UnRef() ;
172
173 m_refData = new wxIconRefData( (WXHICON) wxMacCreateIconRef( bmp ) ) ;
174 }
175
176 wxIconRefData::wxIconRefData( WXHICON icon )
177 {
178 m_iconRef = MAC_WXHICON( icon ) ;
179 }
180
181 void wxIconRefData::Init()
182 {
183 m_iconRef = NULL ;
184 }
185
186 void wxIconRefData::Free()
187 {
188 if ( m_iconRef )
189 {
190 ReleaseIconRef( m_iconRef ) ;
191 m_iconRef = NULL ;
192 }
193 }
194
195 IMPLEMENT_DYNAMIC_CLASS(wxICONResourceHandler, wxBitmapHandler)
196
197 bool wxICONResourceHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
198 int desiredWidth, int desiredHeight)
199 {
200 wxIcon icon ;
201 icon.LoadFile( name , wxBITMAP_TYPE_ICON_RESOURCE , desiredWidth , desiredHeight ) ;
202 bitmap->CopyFromIcon( icon ) ;
203 return bitmap->Ok() ;
204 }