1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/button.mm
4 // Author: David Elliott
7 // Copyright: (c) 2002 David Elliott
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #include "wx/wxprec.h"
13 #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 CocoaSetLabelForObject(label, GetNSButton());
55 if ((style & wxBU_LEFT) && !(style & wxBU_RIGHT))
56 mode = NSLeftTextAlignment;
57 else if ((style & wxBU_RIGHT) && !(style & wxBU_LEFT))
58 mode = NSRightTextAlignment;
61 [GetNSControl() setAlignment:mode];
64 [GetNSControl() sizeToFit];
67 m_parent->CocoaAddChild(this);
68 SetInitialFrameRect(pos,size);
75 DisassociateNSButton(GetNSButton());
78 void wxButton::Cocoa_wxNSButtonAction(void)
80 wxLogTrace(wxTRACE_COCOA,wxT("YAY!"));
81 wxCommandEvent event(wxEVT_BUTTON, GetId());
82 InitCommandEvent(event); // event.SetEventObject(this);
86 wxString wxButton::GetLabel() const
88 return wxStringWithNSString([GetNSButton() title]);
91 void wxButton::SetLabel(const wxString& label)
93 CocoaSetLabelForObject(label, GetNSButton());
96 wxSize wxButton::DoGetBestSize() const
98 wxSize size = wxButtonBase::DoGetBestSize();
99 if(!HasFlag(wxBU_EXACTFIT))
107 static NSRect MakeNSButtonDefaultRect()
109 // create at (10.0,10.0) with size 20.0x20.0 (just bogus values)
110 wxObjcAutoRefFromAlloc<NSButton*> defaultButton = [[NSButton alloc]
111 initWithFrame:NSMakeRect(10.0,10.0,20.0,20.0)];
112 [static_cast<NSButton*>(defaultButton) setBezelStyle:NSRoundedBezelStyle];
113 [static_cast<NSButton*>(defaultButton) setTitle:@""];
114 [static_cast<NSButton*>(defaultButton) sizeToFit];
115 return [static_cast<NSButton*>(defaultButton) frame];
118 wxSize wxButtonBase::GetDefaultSize()
120 static NSRect cocoaRect = MakeNSButtonDefaultRect();
121 // Apple HIG says OK/Cancel buttons have default width of 68.
122 return wxSize(68,(int)ceil(cocoaRect.size.height));