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"
22 #import <AppKit/NSButton.h>
23 #include "wx/cocoa/string.h"
25 IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl)
26 BEGIN_EVENT_TABLE(wxButton, wxButtonBase)
28 WX_IMPLEMENT_COCOA_OWNER(wxButton,NSButton,NSControl,NSView)
30 bool wxButton::Create(wxWindow *parent, wxWindowID winid,
31 const wxString& lbl, const wxPoint& pos,
32 const wxSize& size, long style,
33 const wxValidator& validator, const wxString& name)
35 wxString label((lbl.empty() && wxIsStockID(winid))?wxGetStockLabel(winid):lbl);
37 wxAutoNSAutoreleasePool pool;
38 wxLogTrace(wxTRACE_COCOA,wxT("Creating control with id=%d"),winid);
39 if(!CreateControl(parent,winid,pos,size,style,validator,name))
41 wxLogTrace(wxTRACE_COCOA,wxT("Created control with id=%d"),GetId());
43 SetNSButton([[NSButton alloc] initWithFrame: MakeDefaultNSRect(size)]);
44 // NOTE: YES we want to release this (to match the alloc).
45 // DoAddChild(this) will retain us again since addSubView doesn't.
46 [m_cocoaNSView release];
48 [GetNSButton() setBezelStyle:NSRoundedBezelStyle];
49 [GetNSButton() setTitle:wxNSStringWithWxString(wxStripMenuCodes(label))];
50 [GetNSControl() sizeToFit];
53 m_parent->CocoaAddChild(this);
54 SetInitialFrameRect(pos,size);
61 DisassociateNSButton(GetNSButton());
64 void wxButton::Cocoa_wxNSButtonAction(void)
66 wxLogTrace(wxTRACE_COCOA,wxT("YAY!"));
67 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetId());
68 InitCommandEvent(event); // event.SetEventObject(this);
72 wxString wxButton::GetLabel() const
74 return wxStringWithNSString([GetNSButton() title]);
77 void wxButton::SetLabel(const wxString& label)
79 [GetNSButton() setTitle:wxNSStringWithWxString(wxStripMenuCodes(label))];
82 wxSize wxButtonBase::GetDefaultSize()