1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/button.mm
4 // Author: David Elliott
8 // Copyright: (c) 2002 David Elliott
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
14 #include "wx/button.h"
20 #include "wx/stockitem.h"
21 #include "wx/cocoa/autorelease.h"
22 #include "wx/cocoa/string.h"
24 #import <AppKit/NSButton.h>
27 IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl)
28 BEGIN_EVENT_TABLE(wxButton, wxButtonBase)
30 WX_IMPLEMENT_COCOA_OWNER(wxButton,NSButton,NSControl,NSView)
32 bool wxButton::Create(wxWindow *parent, wxWindowID winid,
33 const wxString& lbl, const wxPoint& pos,
34 const wxSize& size, long style,
35 const wxValidator& validator, const wxString& name)
37 wxString label((lbl.empty() && wxIsStockID(winid))?wxGetStockLabel(winid):lbl);
39 wxAutoNSAutoreleasePool pool;
40 wxLogTrace(wxTRACE_COCOA,wxT("Creating control with id=%d"),winid);
41 if(!CreateControl(parent,winid,pos,size,style,validator,name))
43 wxLogTrace(wxTRACE_COCOA,wxT("Created control with id=%d"),GetId());
45 SetNSButton([[NSButton alloc] initWithFrame: MakeDefaultNSRect(size)]);
46 // NOTE: YES we want to release this (to match the alloc).
47 // DoAddChild(this) will retain us again since addSubView doesn't.
48 [m_cocoaNSView release];
50 [GetNSButton() setBezelStyle:NSRoundedBezelStyle];
51 CocoaSetLabelForObject(label, GetNSButton());
56 if ((style & wxBU_LEFT) && !(style & wxBU_RIGHT))
57 mode = NSLeftTextAlignment;
58 else if ((style & wxBU_RIGHT) && !(style & wxBU_LEFT))
59 mode = NSRightTextAlignment;
62 [GetNSControl() setAlignment:mode];
65 [GetNSControl() sizeToFit];
68 m_parent->CocoaAddChild(this);
69 SetInitialFrameRect(pos,size);
76 DisassociateNSButton(GetNSButton());
79 void wxButton::Cocoa_wxNSButtonAction(void)
81 wxLogTrace(wxTRACE_COCOA,wxT("YAY!"));
82 wxCommandEvent event(wxEVT_BUTTON, GetId());
83 InitCommandEvent(event); // event.SetEventObject(this);
87 wxString wxButton::GetLabel() const
89 return wxStringWithNSString([GetNSButton() title]);
92 void wxButton::SetLabel(const wxString& label)
94 CocoaSetLabelForObject(label, GetNSButton());
97 wxSize wxButton::DoGetBestSize() const
99 wxSize size = wxButtonBase::DoGetBestSize();
100 if(!HasFlag(wxBU_EXACTFIT))
108 static NSRect MakeNSButtonDefaultRect()
110 // create at (10.0,10.0) with size 20.0x20.0 (just bogus values)
111 wxObjcAutoRefFromAlloc<NSButton*> defaultButton = [[NSButton alloc]
112 initWithFrame:NSMakeRect(10.0,10.0,20.0,20.0)];
113 [static_cast<NSButton*>(defaultButton) setBezelStyle:NSRoundedBezelStyle];
114 [static_cast<NSButton*>(defaultButton) setTitle:@""];
115 [static_cast<NSButton*>(defaultButton) sizeToFit];
116 return [static_cast<NSButton*>(defaultButton) frame];
119 wxSize wxButtonBase::GetDefaultSize()
121 static NSRect cocoaRect = MakeNSButtonDefaultRect();
122 // Apple HIG says OK/Cancel buttons have default width of 68.
123 return wxSize(68,(int)ceil(cocoaRect.size.height));