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:
54 m_peer->GetBestRect(&r);
56 if ( r.GetWidth() == 0 && r.GetHeight() == 0 )
64 if ((wBtn > sz.x) || ( GetWindowStyle() & wxBU_EXACTFIT))
68 Rect bestsize = { 0 , 0 , 0 , 0 } ;
69 m_peer->GetBestRect( &bestsize ) ;
72 if ( EmptyRect( &bestsize ) || ( GetWindowStyle() & wxBU_EXACTFIT) )
76 ControlFontStyleRec controlFont;
77 OSStatus err = m_peer->GetData<ControlFontStyleRec>( kControlEntireControl, kControlFontStyleTag, &controlFont );
80 wxCFStringRef str( m_label, GetFont().GetEncoding() );
82 #if wxOSX_USE_ATSU_TEXT
84 if ( m_font.MacGetThemeFontID() != kThemeCurrentPortFont )
86 err = GetThemeTextDimensions(
87 (!m_label.empty() ? (CFStringRef)str : CFSTR(" ")),
88 m_font.MacGetThemeFontID(), kThemeStateActive, false, &bounds, &baseline );
94 wxClientDC dc(const_cast<wxButton*>(this));
95 wxCoord width, height ;
96 dc.GetTextExtent( m_label , &width, &height);
101 wBtn = bounds.h + sz.y;
105 wBtn = bestsize.right - bestsize.left ;
106 // non 'normal' window variants don't return the correct height
107 // sz.y = bestsize.bottom - bestsize.top ;
109 if ((wBtn > sz.x) || ( GetWindowStyle() & wxBU_EXACTFIT))
116 wxSize wxButton::GetDefaultSize()
121 return wxSize(wBtn, hBtn);
124 @implementation wxNSButton
126 - (id)initWithFrame:(NSRect)frame
128 [super initWithFrame:frame];
130 [self setTarget: self];
131 [self setAction: @selector(clickedAction:)];
135 - (void) clickedAction: (id) sender
139 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
141 wxpeer->HandleClicked(0);
145 - (void)setImplementation: (wxWidgetImpl *) theImplementation
147 impl = theImplementation;
150 - (wxWidgetImpl*) implementation
162 switch ( [self state] )
173 - (void) setIntValue: (int) v
178 [self setState:NSMixedState];
181 [self setState:NSOnState];
184 [self setState:NSOffState];
192 wxWidgetImplType* wxWidgetImpl::CreateButton( wxWindowMac* wxpeer,
195 const wxString& label,
201 NSView* sv = (wxpeer->GetParent()->GetHandle() );
203 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
204 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
206 if ( id == wxID_HELP )
208 [v setBezelStyle:NSHelpButtonBezelStyle];
212 [v setBezelStyle:NSRoundedBezelStyle];
215 [v setButtonType:NSMomentaryPushInButton];
217 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v );
218 [v setImplementation:c];
222 Rect bounds = wxMacGetBoundsForControl( wxpeer , pos , size ) ;
223 wxMacControl* peer = new wxMacControl(wxpeer) ;
224 if ( id == wxID_HELP )
226 ControlButtonContentInfo info ;
227 info.contentType = kControlContentIconRef ;
228 GetIconRef(kOnSystemDisk, kSystemIconsCreator, kHelpIcon, &info.u.iconRef);
229 err = CreateRoundButtonControl(
230 MAC_WXHWND(parent->MacGetTopLevelWindowRef()),
231 &bounds, kControlRoundButtonNormalSize,
232 &info, peer->GetControlRefAddr() );
234 else if ( label.Find('\n' ) == wxNOT_FOUND && label.Find('\r' ) == wxNOT_FOUND)
236 // Button height is static in Mac, can't be changed, so we need to force it here
238 switch (wxpeer->GetWindowVariant() )
240 case wxWINDOW_VARIANT_NORMAL:
241 case wxWINDOW_VARIANT_LARGE:
244 case wxWINDOW_VARIANT_SMALL:
246 case wxWINDOW_VARIANT_MINI:
251 bounds.bottom = bounds.top + maxHeight ;
252 wxpeer->SetMaxSize( wxSize( wxpeer->GetMaxWidth() , maxHeight ));
253 err = CreatePushButtonControl(
254 MAC_WXHWND(parent->MacGetTopLevelWindowRef()),
255 &bounds, CFSTR(""), peer->GetControlRefAddr() );
259 ControlButtonContentInfo info ;
260 info.contentType = kControlNoContent ;
261 err = CreateBevelButtonControl(
262 MAC_WXHWND(parent->MacGetTopLevelWindowRef()) , &bounds, CFSTR(""),
263 kControlBevelButtonLargeBevel, kControlBehaviorPushbutton,
264 &info, 0, 0, 0, peer->GetControlRefAddr() );
271 void wxWidgetCocoaImpl::SetDefaultButton( bool isDefault )
273 if ( [m_osxView isKindOfClass:[NSButton class]] )
274 [(NSButton*)m_osxView setKeyEquivalent: isDefault ? @"\r" : nil ];
277 void wxWidgetCocoaImpl::PerformClick()
281 wxWidgetImplType* wxWidgetImpl::CreateDisclosureTriangle( wxWindowMac* wxpeer,
284 const wxString& label,
290 NSView* sv = (wxpeer->GetParent()->GetHandle() );
292 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
293 wxNSButton* v = [[wxNSButton alloc] initWithFrame:r];
294 [v setBezelStyle:NSDisclosureBezelStyle];
295 [v setButtonType:NSOnOffButton];
296 [v setTitle:wxCFStringRef( label).AsNSString()];
297 [v setImagePosition:NSImageRight];
299 wxWidgetCocoaImpl* c = new wxWidgetCocoaImpl( wxpeer, v );
300 [v setImplementation:c];