]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/button.mm
minor fixes; replace references to Windows95 with references to wxMSW where possible
[wxWidgets.git] / src / osx / cocoa / button.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/button.mm
3 // Purpose: wxButton
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // RCS-ID: $Id: button.cpp 54845 2008-07-30 14:52:41Z SC $
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #include "wx/button.h"
15
16 #ifndef WX_PRECOMP
17 #include "wx/panel.h"
18 #include "wx/toplevel.h"
19 #include "wx/dcclient.h"
20 #endif
21
22 #include "wx/stockitem.h"
23
24 #include "wx/osx/private.h"
25
26 wxSize wxButton::DoGetBestSize() const
27 {
28 if ( GetId() == wxID_HELP )
29 return wxSize( 20 , 20 ) ;
30
31 wxSize sz = GetDefaultSize() ;
32
33 switch (GetWindowVariant())
34 {
35 case wxWINDOW_VARIANT_NORMAL:
36 case wxWINDOW_VARIANT_LARGE:
37 sz.y = 23 ;
38 break;
39
40 case wxWINDOW_VARIANT_SMALL:
41 sz.y = 17 ;
42 break;
43
44 case wxWINDOW_VARIANT_MINI:
45 sz.y = 15 ;
46 break;
47
48 default:
49 break;
50 }
51
52 wxRect r ;
53
54 m_peer->GetBestRect(&r);
55
56 if ( r.GetWidth() == 0 && r.GetHeight() == 0 )
57 {
58 }
59 sz.x = r.GetWidth();
60 sz.y = r.GetHeight();
61
62 int wBtn = 96;
63
64 if ((wBtn > sz.x) || ( GetWindowStyle() & wxBU_EXACTFIT))
65 sz.x = wBtn;
66
67 #if wxOSX_USE_CARBON
68 Rect bestsize = { 0 , 0 , 0 , 0 } ;
69 m_peer->GetBestRect( &bestsize ) ;
70
71 int wBtn;
72 if ( EmptyRect( &bestsize ) || ( GetWindowStyle() & wxBU_EXACTFIT) )
73 {
74 Point bounds;
75
76 ControlFontStyleRec controlFont;
77 OSStatus err = m_peer->GetData<ControlFontStyleRec>( kControlEntireControl, kControlFontStyleTag, &controlFont );
78 verify_noerr( err );
79
80 wxCFStringRef str( m_label, GetFont().GetEncoding() );
81
82 #if wxOSX_USE_ATSU_TEXT
83 SInt16 baseline;
84 if ( m_font.MacGetThemeFontID() != kThemeCurrentPortFont )
85 {
86 err = GetThemeTextDimensions(
87 (!m_label.empty() ? (CFStringRef)str : CFSTR(" ")),
88 m_font.MacGetThemeFontID(), kThemeStateActive, false, &bounds, &baseline );
89 verify_noerr( err );
90 }
91 else
92 #endif
93 {
94 wxClientDC dc(const_cast<wxButton*>(this));
95 wxCoord width, height ;
96 dc.GetTextExtent( m_label , &width, &height);
97 bounds.h = width;
98 bounds.v = height;
99 }
100
101 wBtn = bounds.h + sz.y;
102 }
103 else
104 {
105 wBtn = bestsize.right - bestsize.left ;
106 // non 'normal' window variants don't return the correct height
107 // sz.y = bestsize.bottom - bestsize.top ;
108 }
109 if ((wBtn > sz.x) || ( GetWindowStyle() & wxBU_EXACTFIT))
110 sz.x = wBtn;
111 #endif
112
113 return sz ;
114 }
115
116 wxSize wxButton::GetDefaultSize()
117 {
118 int wBtn = 70 ;
119 int hBtn = 20 ;
120
121 return wxSize(wBtn, hBtn);
122 }
123
124 @implementation wxNSButton
125
126 - (id)initWithFrame:(NSRect)frame
127 {
128 [super initWithFrame:frame];
129 impl = NULL;
130 [self setTarget: self];
131 [self setAction: @selector(clickedAction:)];
132 return self;
133 }
134
135 - (void) clickedAction: (id) sender
136 {
137 if ( impl )
138 {
139 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
140 if ( wxpeer )
141 wxpeer->HandleClicked(0);
142 }
143 }
144
145 - (void)setImplementation: (wxWidgetImpl *) theImplementation
146 {
147 impl = theImplementation;
148 }
149
150 - (wxWidgetImpl*) implementation
151 {
152 return impl;
153 }
154
155 - (BOOL) isFlipped
156 {
157 return YES;
158 }
159
160 - (int) intValue
161 {
162 switch ( [self state] )
163 {
164 case NSOnState:
165 return 1;
166 case NSMixedState:
167 return 2;
168 default:
169 return 0;
170 }
171 }
172
173 - (void) setIntValue: (int) v
174 {
175 switch( v )
176 {
177 case 2:
178 [self setState:NSMixedState];
179 break;
180 case 1:
181 [self setState:NSOnState];
182 break;
183 default :
184 [self setState:NSOffState];
185 break;
186 }
187 }
188
189 @end
190
191
192 wxWidgetImplType* wxWidgetImpl::CreateButton( wxWindowMac* wxpeer,
193 wxWindowMac* parent,
194 wxWindowID id,
195 const wxString& label,
196 const wxPoint& pos,
197 const wxSize& size,
198 long style,
199 long extraStyle)
200 {
201 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
202 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
203
204 if ( id == wxID_HELP )
205 {
206 [v setBezelStyle:NSHelpButtonBezelStyle];
207 }
208 else
209 {
210 [v setBezelStyle:NSRoundedBezelStyle];
211 }
212
213 [v setButtonType:NSMomentaryPushInButton];
214 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v );
215 [v setImplementation:c];
216 return c;
217 /*
218 OSStatus err;
219 Rect bounds = wxMacGetBoundsForControl( wxpeer , pos , size ) ;
220 wxMacControl* peer = new wxMacControl(wxpeer) ;
221 if ( id == wxID_HELP )
222 {
223 ControlButtonContentInfo info ;
224 info.contentType = kControlContentIconRef ;
225 GetIconRef(kOnSystemDisk, kSystemIconsCreator, kHelpIcon, &info.u.iconRef);
226 err = CreateRoundButtonControl(
227 MAC_WXHWND(parent->MacGetTopLevelWindowRef()),
228 &bounds, kControlRoundButtonNormalSize,
229 &info, peer->GetControlRefAddr() );
230 }
231 else if ( label.Find('\n' ) == wxNOT_FOUND && label.Find('\r' ) == wxNOT_FOUND)
232 {
233 // Button height is static in Mac, can't be changed, so we need to force it here
234 int maxHeight;
235 switch (wxpeer->GetWindowVariant() )
236 {
237 case wxWINDOW_VARIANT_NORMAL:
238 case wxWINDOW_VARIANT_LARGE:
239 maxHeight = 20 ;
240 break;
241 case wxWINDOW_VARIANT_SMALL:
242 maxHeight = 17;
243 case wxWINDOW_VARIANT_MINI:
244 maxHeight = 15;
245 default:
246 break;
247 }
248 bounds.bottom = bounds.top + maxHeight ;
249 wxpeer->SetMaxSize( wxSize( wxpeer->GetMaxWidth() , maxHeight ));
250 err = CreatePushButtonControl(
251 MAC_WXHWND(parent->MacGetTopLevelWindowRef()),
252 &bounds, CFSTR(""), peer->GetControlRefAddr() );
253 }
254 else
255 {
256 ControlButtonContentInfo info ;
257 info.contentType = kControlNoContent ;
258 err = CreateBevelButtonControl(
259 MAC_WXHWND(parent->MacGetTopLevelWindowRef()) , &bounds, CFSTR(""),
260 kControlBevelButtonLargeBevel, kControlBehaviorPushbutton,
261 &info, 0, 0, 0, peer->GetControlRefAddr() );
262 }
263 verify_noerr( err );
264 return peer;
265 */
266 }
267
268 void wxWidgetCocoaImpl::SetDefaultButton( bool isDefault )
269 {
270 if ( [m_osxView isKindOfClass:[NSButton class]] )
271 [(NSButton*)m_osxView setKeyEquivalent: isDefault ? @"\r" : nil ];
272 }
273
274 void wxWidgetCocoaImpl::PerformClick()
275 {
276 }
277
278 wxWidgetImplType* wxWidgetImpl::CreateDisclosureTriangle( wxWindowMac* wxpeer,
279 wxWindowMac* parent,
280 wxWindowID id,
281 const wxString& label,
282 const wxPoint& pos,
283 const wxSize& size,
284 long style,
285 long extraStyle)
286 {
287 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
288 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
289 [v setBezelStyle:NSDisclosureBezelStyle];
290 [v setButtonType:NSOnOffButton];
291 [v setTitle:wxCFStringRef( label).AsNSString()];
292 [v setImagePosition:NSImageRight];
293 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v );
294 [v setImplementation:c];
295 return c;
296 }