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