]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/checkbox.mm
Fix install_name_tool calls in OS X "make install".
[wxWidgets.git] / src / cocoa / checkbox.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/checkbox.mm
3 // Purpose: wxCheckBox
4 // Author: David Elliott
5 // Modified by:
6 // Created: 2003/03/16
7 // Copyright: (c) 2003 David Elliott
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #include "wx/wxprec.h"
12
13 #if wxUSE_CHECKBOX
14
15 #include "wx/checkbox.h"
16
17 #ifndef WX_PRECOMP
18 #include "wx/log.h"
19 #include "wx/app.h"
20 #endif //WX_PRECOMP
21
22 #include "wx/cocoa/autorelease.h"
23 #include "wx/cocoa/string.h"
24
25 #import <AppKit/NSButton.h>
26 #import <Foundation/NSString.h>
27
28 BEGIN_EVENT_TABLE(wxCheckBox, wxCheckBoxBase)
29 END_EVENT_TABLE()
30 WX_IMPLEMENT_COCOA_OWNER(wxCheckBox,NSButton,NSControl,NSView)
31
32 bool wxCheckBox::Create(wxWindow *parent, wxWindowID winid,
33 const wxString& label,
34 const wxPoint& pos,
35 const wxSize& size,
36 long style,
37 const wxValidator& validator,
38 const wxString& name)
39 {
40 wxAutoNSAutoreleasePool pool;
41 if(!CreateControl(parent,winid,pos,size,style,validator,name))
42 return false;
43 m_cocoaNSView = NULL;
44 SetNSButton([[NSButton alloc] initWithFrame: MakeDefaultNSRect(size)]);
45 [m_cocoaNSView release];
46 [GetNSButton() setButtonType: NSSwitchButton];
47 [GetNSButton() setAllowsMixedState: Is3State()];
48 CocoaSetLabelForObject(label, GetNSButton());
49 [GetNSControl() sizeToFit];
50
51 if(m_parent)
52 m_parent->CocoaAddChild(this);
53 SetInitialFrameRect(pos,size);
54
55 return true;
56 }
57
58 wxCheckBox::~wxCheckBox()
59 {
60 DisassociateNSButton(GetNSButton());
61 }
62
63 void wxCheckBox::SetValue(bool value)
64 {
65 [GetNSButton() setState: value?NSOnState:NSOffState];
66 }
67
68 void wxCheckBox::DoSet3StateValue(wxCheckBoxState state)
69 {
70 int cocoaState;
71 switch(state)
72 {
73 case wxCHK_UNCHECKED:
74 cocoaState = NSOffState;
75 break;
76 case wxCHK_CHECKED:
77 cocoaState = NSOnState;
78 break;
79 case wxCHK_UNDETERMINED:
80 // Base class would have already set state to wxCHK_UNCHECKED
81 // wxASSERT_MSG(Is3State(),"Use the wxCHK_3STATE style flag");
82 cocoaState = NSMixedState;
83 break;
84 default:
85 wxFAIL_MSG(wxT("Invalid state in wxCheckBox::DoSet3StateValue"));
86 return;
87 }
88 [GetNSButton() setState:cocoaState];
89 }
90
91 bool wxCheckBox::GetValue() const
92 {
93 int state = [GetNSButton() state];
94 wxASSERT_MSG(state!=NSMixedState || Is3State(),
95 wxT("NSMixedState returned from a 2-state checkbox"));
96 return state!=NSOffState;
97 }
98
99 wxCheckBoxState wxCheckBox::DoGet3StateValue() const
100 {
101 switch([GetNSButton() state])
102 {
103 case NSOffState:
104 return wxCHK_UNCHECKED;
105 case NSOnState:
106 return wxCHK_CHECKED;
107 default:
108 wxFAIL_MSG(wxT("[NSButton -state] returned an invalid state!"));
109 case NSMixedState:
110 // Base class handles this assertion for us
111 // wxASSERT_MSG(Is3State(),wxT("NSMixedState returned from a 2-state checkbox"));
112 return wxCHK_UNDETERMINED;
113 }
114 }
115
116 void wxCheckBox::Cocoa_wxNSButtonAction(void)
117 {
118 wxLogTrace(wxTRACE_COCOA,wxT("Checkbox"));
119 // What we really want to do is override [NSCell -nextState] to return
120 // NSOnState in lieu of NSMixedState but this works (aside from the
121 // very slightly noticeable drawing of - and then a check) -DE
122
123 // Cocoa always allows a 3-state button to transition into
124 // the mixed/undetermined state by clicking, we don't
125 if ( !Is3rdStateAllowedForUser()
126 && [GetNSButton() state] == NSMixedState )
127 {
128 // Cocoa's sequence is on/off/mixed
129 // skip mixed, go right back to on
130 [GetNSButton() setState: NSOnState];
131 }
132 wxCommandEvent event(wxEVT_CHECKBOX, GetId());
133 InitCommandEvent(event); // event.SetEventObject(this);
134 event.SetInt(Get3StateValue());
135 Command(event);
136 }
137
138 void wxCheckBox::SetLabel(const wxString& s)
139 {
140 wxAutoNSAutoreleasePool pool;
141 CocoaSetLabelForObject(s, GetNSButton());
142 }
143
144 wxString wxCheckBox::GetLabel() const
145 {
146 wxAutoNSAutoreleasePool pool;
147 return wxStringWithNSString([GetNSButton() title]);
148
149 }
150
151 #endif // wxUSE_CHECKBOX