]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/button.mm
update button style to include BS_MULTILINE when a multiline label is set
[wxWidgets.git] / src / cocoa / button.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/button.mm
3 // Purpose: wxButton
4 // Author: David Elliott
5 // Modified by:
6 // Created: 2002/12/30
7 // RCS-ID: $Id$
8 // Copyright: (c) 2002 David Elliott
9 // Licence: wxWidgets licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #include "wx/button.h"
15
16 #ifndef WX_PRECOMP
17 #include "wx/log.h"
18 #endif
19
20 #include "wx/stockitem.h"
21 #include "wx/cocoa/autorelease.h"
22 #include "wx/cocoa/string.h"
23
24 #import <AppKit/NSButton.h>
25 #import <math.h>
26
27 IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl)
28 BEGIN_EVENT_TABLE(wxButton, wxButtonBase)
29 END_EVENT_TABLE()
30 WX_IMPLEMENT_COCOA_OWNER(wxButton,NSButton,NSControl,NSView)
31
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)
36 {
37 wxString label((lbl.empty() && wxIsStockID(winid))?wxGetStockLabel(winid):lbl);
38
39 wxAutoNSAutoreleasePool pool;
40 wxLogTrace(wxTRACE_COCOA,wxT("Creating control with id=%d"),winid);
41 if(!CreateControl(parent,winid,pos,size,style,validator,name))
42 return false;
43 wxLogTrace(wxTRACE_COCOA,wxT("Created control with id=%d"),GetId());
44 m_cocoaNSView = NULL;
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];
49
50 [GetNSButton() setBezelStyle:NSRoundedBezelStyle];
51 [GetNSButton() setTitle:wxNSStringWithWxString(GetLabelText(label))];
52 [GetNSControl() sizeToFit];
53
54 if(m_parent)
55 m_parent->CocoaAddChild(this);
56 SetInitialFrameRect(pos,size);
57
58 return true;
59 }
60
61 wxButton::~wxButton()
62 {
63 DisassociateNSButton(GetNSButton());
64 }
65
66 void wxButton::Cocoa_wxNSButtonAction(void)
67 {
68 wxLogTrace(wxTRACE_COCOA,wxT("YAY!"));
69 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetId());
70 InitCommandEvent(event); // event.SetEventObject(this);
71 Command(event);
72 }
73
74 wxString wxButton::GetLabel() const
75 {
76 return wxStringWithNSString([GetNSButton() title]);
77 }
78
79 void wxButton::SetLabel(const wxString& label)
80 {
81 [GetNSButton() setTitle:wxNSStringWithWxString(GetLabelText(label))];
82 }
83
84 wxSize wxButton::DoGetBestSize() const
85 {
86 wxSize size = wxButtonBase::DoGetBestSize();
87 if(!HasFlag(wxBU_EXACTFIT))
88 {
89 if(size.x<68)
90 size.x = 68;
91 }
92 return size;
93 }
94
95 static NSRect MakeNSButtonDefaultRect()
96 {
97 // create at (10.0,10.0) with size 20.0x20.0 (just bogus values)
98 wxObjcAutoRefFromAlloc<NSButton*> defaultButton = [[NSButton alloc]
99 initWithFrame:NSMakeRect(10.0,10.0,20.0,20.0)];
100 [static_cast<NSButton*>(defaultButton) setBezelStyle:NSRoundedBezelStyle];
101 [static_cast<NSButton*>(defaultButton) setTitle:@""];
102 [static_cast<NSButton*>(defaultButton) sizeToFit];
103 return [static_cast<NSButton*>(defaultButton) frame];
104 }
105
106 wxSize wxButtonBase::GetDefaultSize()
107 {
108 static NSRect cocoaRect = MakeNSButtonDefaultRect();
109 // Apple HIG says OK/Cancel buttons have default width of 68.
110 return wxSize(68,(int)ceil(cocoaRect.size.height));
111 }