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/cocoa/autorelease.h"
21 #import <AppKit/NSButton.h>
22 #include "wx/cocoa/string.h"
24 IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl)
25 BEGIN_EVENT_TABLE(wxButton, wxButtonBase)
27 WX_IMPLEMENT_COCOA_OWNER(wxButton,NSButton,NSControl,NSView)
29 bool wxButton::Create(wxWindow *parent, wxWindowID winid,
30 const wxString& label, const wxPoint& pos,
31 const wxSize& size, long style,
32 const wxValidator& validator, const wxString& name)
34 wxAutoNSAutoreleasePool pool;
35 wxLogTrace(wxTRACE_COCOA,wxT("Creating control with id=%d"),winid);
36 if(!CreateControl(parent,winid,pos,size,style,validator,name))
38 wxLogTrace(wxTRACE_COCOA,wxT("Created control with id=%d"),GetId());
40 SetNSButton([[NSButton alloc] initWithFrame: MakeDefaultNSRect(size)]);
41 // NOTE: YES we want to release this (to match the alloc).
42 // DoAddChild(this) will retain us again since addSubView doesn't.
43 [m_cocoaNSView release];
45 [GetNSButton() setBezelStyle:NSRoundedBezelStyle];
46 [GetNSButton() setTitle:wxNSStringWithWxString(wxStripMenuCodes(label))];
47 [GetNSControl() sizeToFit];
50 m_parent->CocoaAddChild(this);
51 SetInitialFrameRect(pos,size);
58 DisassociateNSButton(GetNSButton());
61 void wxButton::Cocoa_wxNSButtonAction(void)
63 wxLogTrace(wxTRACE_COCOA,wxT("YAY!"));
64 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetId());
65 InitCommandEvent(event); // event.SetEventObject(this);
69 wxString wxButton::GetLabel() const
71 return wxStringWithNSString([GetNSButton() title]);
74 void wxButton::SetLabel(const wxString& label)
76 [GetNSButton() setTitle:wxNSStringWithWxString(wxStripMenuCodes(label))];
79 wxSize wxButtonBase::GetDefaultSize()