1 /////////////////////////////////////////////////////////////////////////////
2 // Name: cocoa/button.mm
4 // Author: David Elliott
8 // Copyright: (c) 2002 David Elliott
9 // Licence: wxWidgets licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
15 #include "wx/button.h"
19 #include "wx/stockitem.h"
20 #include "wx/cocoa/autorelease.h"
21 #include "wx/cocoa/string.h"
23 #import <AppKit/NSButton.h>
26 IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl)
27 BEGIN_EVENT_TABLE(wxButton, wxButtonBase)
29 WX_IMPLEMENT_COCOA_OWNER(wxButton,NSButton,NSControl,NSView)
31 bool wxButton::Create(wxWindow *parent, wxWindowID winid,
32 const wxString& lbl, const wxPoint& pos,
33 const wxSize& size, long style,
34 const wxValidator& validator, const wxString& name)
36 wxString label((lbl.empty() && wxIsStockID(winid))?wxGetStockLabel(winid):lbl);
38 wxAutoNSAutoreleasePool pool;
39 wxLogTrace(wxTRACE_COCOA,wxT("Creating control with id=%d"),winid);
40 if(!CreateControl(parent,winid,pos,size,style,validator,name))
42 wxLogTrace(wxTRACE_COCOA,wxT("Created control with id=%d"),GetId());
44 SetNSButton([[NSButton alloc] initWithFrame: MakeDefaultNSRect(size)]);
45 // NOTE: YES we want to release this (to match the alloc).
46 // DoAddChild(this) will retain us again since addSubView doesn't.
47 [m_cocoaNSView release];
49 [GetNSButton() setBezelStyle:NSRoundedBezelStyle];
50 [GetNSButton() setTitle:wxNSStringWithWxString(wxStripMenuCodes(label))];
51 [GetNSControl() sizeToFit];
54 m_parent->CocoaAddChild(this);
55 SetInitialFrameRect(pos,size);
62 DisassociateNSButton(GetNSButton());
65 void wxButton::Cocoa_wxNSButtonAction(void)
67 wxLogTrace(wxTRACE_COCOA,wxT("YAY!"));
68 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetId());
69 InitCommandEvent(event); // event.SetEventObject(this);
73 wxString wxButton::GetLabel() const
75 return wxStringWithNSString([GetNSButton() title]);
78 void wxButton::SetLabel(const wxString& label)
80 [GetNSButton() setTitle:wxNSStringWithWxString(wxStripMenuCodes(label))];
83 wxSize wxButton::DoGetBestSize() const
85 wxSize size = wxButtonBase::DoGetBestSize();
86 if(!HasFlag(wxBU_EXACTFIT))
94 static NSRect MakeNSButtonDefaultRect()
96 // create at (10.0,10.0) with size 20.0x20.0 (just bogus values)
97 wxObjcAutoRefFromAlloc<NSButton*> defaultButton = [[NSButton alloc]
98 initWithFrame:NSMakeRect(10.0,10.0,20.0,20.0)];
99 [static_cast<NSButton*>(defaultButton) setBezelStyle:NSRoundedBezelStyle];
100 [static_cast<NSButton*>(defaultButton) setTitle:@""];
101 [static_cast<NSButton*>(defaultButton) sizeToFit];
102 return [static_cast<NSButton*>(defaultButton) frame];
105 wxSize wxButtonBase::GetDefaultSize()
107 static NSRect cocoaRect = MakeNSButtonDefaultRect();
108 // Apple HIG says OK/Cancel buttons have default width of 68.
109 return wxSize(68,(int)ceilf(cocoaRect.size.height));