1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/button.mm
4 // Author: Stefan Csomor
7 // RCS-ID: $Id: button.cpp 54845 2008-07-30 14:52:41Z SC $
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
14 #include "wx/button.h"
18 #include "wx/toplevel.h"
19 #include "wx/dcclient.h"
22 #include "wx/stockitem.h"
24 #include "wx/osx/private.h"
26 wxSize wxButton::DoGetBestSize() const
28 if ( GetId() == wxID_HELP )
29 return wxSize( 20 , 20 ) ;
31 wxSize sz = GetDefaultSize() ;
33 switch (GetWindowVariant())
35 case wxWINDOW_VARIANT_NORMAL:
36 case wxWINDOW_VARIANT_LARGE:
40 case wxWINDOW_VARIANT_SMALL:
44 case wxWINDOW_VARIANT_MINI:
53 Rect bestsize = { 0 , 0 , 0 , 0 } ;
54 m_peer->GetBestRect( &bestsize ) ;
57 if ( EmptyRect( &bestsize ) || ( GetWindowStyle() & wxBU_EXACTFIT) )
61 ControlFontStyleRec controlFont;
62 OSStatus err = m_peer->GetData<ControlFontStyleRec>( kControlEntireControl, kControlFontStyleTag, &controlFont );
65 wxCFStringRef str( m_label, GetFont().GetEncoding() );
67 #if wxOSX_USE_ATSU_TEXT
69 if ( m_font.MacGetThemeFontID() != kThemeCurrentPortFont )
71 err = GetThemeTextDimensions(
72 (!m_label.empty() ? (CFStringRef)str : CFSTR(" ")),
73 m_font.MacGetThemeFontID(), kThemeStateActive, false, &bounds, &baseline );
79 wxClientDC dc(const_cast<wxButton*>(this));
80 wxCoord width, height ;
81 dc.GetTextExtent( m_label , &width, &height);
86 wBtn = bounds.h + sz.y;
90 wBtn = bestsize.right - bestsize.left ;
91 // non 'normal' window variants don't return the correct height
92 // sz.y = bestsize.bottom - bestsize.top ;
94 if ((wBtn > sz.x) || ( GetWindowStyle() & wxBU_EXACTFIT))
101 wxSize wxButton::GetDefaultSize()
106 return wxSize(wBtn, hBtn);
109 @implementation wxNSButton
111 - (id)initWithFrame:(NSRect)frame
113 [super initWithFrame:frame];
115 [self setTarget: self];
116 [self setAction: @selector(clickedAction:)];
120 - (void) clickedAction: (id) sender
124 wxButton* wxpeer = (wxButton*) m_impl->GetWXPeer();
126 wxpeer->HandleClicked(0);
130 - (void)setImplementation: (wxWidgetImpl *) theImplementation
132 m_impl = theImplementation;
135 - (wxWidgetImpl*) implementation
147 switch ( [self state] )
158 - (void) setIntValue: (int) v
163 [self setState:NSMixedState];
166 [self setState:NSOnState];
169 [self setState:NSOffState];
177 wxWidgetImplType* wxWidgetImpl::CreateButton( wxWindowMac* wxpeer,
180 const wxString& label,
186 NSView* sv = (wxpeer->GetParent()->GetHandle() );
188 NSRect r = wxToNSRect( sv, wxRect( pos, size) );
189 // Rect bounds = wxMacGetBoundsForControl( wxpeer, pos , size ) ;
190 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
192 if ( id == wxID_HELP )
194 [v setBezelStyle:NSHelpButtonBezelStyle];
198 [v setBezelStyle:NSRoundedBezelStyle];
201 [v setButtonType:NSMomentaryPushInButton];
203 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v );
204 [v setImplementation:c];
208 Rect bounds = wxMacGetBoundsForControl( wxpeer , pos , size ) ;
209 wxMacControl* peer = new wxMacControl(wxpeer) ;
210 if ( id == wxID_HELP )
212 ControlButtonContentInfo info ;
213 info.contentType = kControlContentIconRef ;
214 GetIconRef(kOnSystemDisk, kSystemIconsCreator, kHelpIcon, &info.u.iconRef);
215 err = CreateRoundButtonControl(
216 MAC_WXHWND(parent->MacGetTopLevelWindowRef()),
217 &bounds, kControlRoundButtonNormalSize,
218 &info, peer->GetControlRefAddr() );
220 else if ( label.Find('\n' ) == wxNOT_FOUND && label.Find('\r' ) == wxNOT_FOUND)
222 // Button height is static in Mac, can't be changed, so we need to force it here
224 switch (wxpeer->GetWindowVariant() )
226 case wxWINDOW_VARIANT_NORMAL:
227 case wxWINDOW_VARIANT_LARGE:
230 case wxWINDOW_VARIANT_SMALL:
232 case wxWINDOW_VARIANT_MINI:
237 bounds.bottom = bounds.top + maxHeight ;
238 wxpeer->SetMaxSize( wxSize( wxpeer->GetMaxWidth() , maxHeight ));
239 err = CreatePushButtonControl(
240 MAC_WXHWND(parent->MacGetTopLevelWindowRef()),
241 &bounds, CFSTR(""), peer->GetControlRefAddr() );
245 ControlButtonContentInfo info ;
246 info.contentType = kControlNoContent ;
247 err = CreateBevelButtonControl(
248 MAC_WXHWND(parent->MacGetTopLevelWindowRef()) , &bounds, CFSTR(""),
249 kControlBevelButtonLargeBevel, kControlBehaviorPushbutton,
250 &info, 0, 0, 0, peer->GetControlRefAddr() );
257 void wxWidgetCocoaImpl::SetDefaultButton( bool isDefault )
259 [m_osxView setKeyEquivalent: isDefault ? @"\r" : nil ];
260 // SetData(kControlButtonPart , kControlPushButtonDefaultTag , (Boolean) isDefault ) ;
263 void wxWidgetCocoaImpl::PerformClick()
267 // TODO for the disclosure button : NSDisclosureBezelStyle and the button type to NSOnOffButton.