1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/checkbox.mm
4 // Author: David Elliott
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWidgets licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
16 #include "wx/checkbox.h"
23 #include "wx/cocoa/autorelease.h"
24 #include "wx/cocoa/string.h"
26 #import <AppKit/NSButton.h>
27 #import <Foundation/NSString.h>
29 IMPLEMENT_DYNAMIC_CLASS(wxCheckBox, wxControl)
30 BEGIN_EVENT_TABLE(wxCheckBox, wxCheckBoxBase)
32 WX_IMPLEMENT_COCOA_OWNER(wxCheckBox,NSButton,NSControl,NSView)
34 bool wxCheckBox::Create(wxWindow *parent, wxWindowID winid,
35 const wxString& label,
39 const wxValidator& validator,
42 wxAutoNSAutoreleasePool pool;
43 if(!CreateControl(parent,winid,pos,size,style,validator,name))
46 SetNSButton([[NSButton alloc] initWithFrame: MakeDefaultNSRect(size)]);
47 [m_cocoaNSView release];
48 [GetNSButton() setButtonType: NSSwitchButton];
49 [GetNSButton() setAllowsMixedState: Is3State()];
50 [GetNSButton() setTitle:wxNSStringWithWxString(GetLabelText(label))];
51 [GetNSControl() sizeToFit];
54 m_parent->CocoaAddChild(this);
55 SetInitialFrameRect(pos,size);
60 wxCheckBox::~wxCheckBox()
62 DisassociateNSButton(GetNSButton());
65 void wxCheckBox::SetValue(bool value)
67 [GetNSButton() setState: value?NSOnState:NSOffState];
70 void wxCheckBox::DoSet3StateValue(wxCheckBoxState state)
76 cocoaState = NSOffState;
79 cocoaState = NSOnState;
81 case wxCHK_UNDETERMINED:
82 // Base class would have already set state to wxCHK_UNCHECKED
83 // wxASSERT_MSG(Is3State(),"Use the wxCHK_3STATE style flag");
84 cocoaState = NSMixedState;
87 wxFAIL_MSG(wxT("Invalid state in wxCheckBox::DoSet3StateValue"));
90 [GetNSButton() setState:cocoaState];
93 bool wxCheckBox::GetValue() const
95 int state = [GetNSButton() state];
96 wxASSERT_MSG(state!=NSMixedState || Is3State(),
97 wxT("NSMixedState returned from a 2-state checkbox"));
98 return state!=NSOffState;
101 wxCheckBoxState wxCheckBox::DoGet3StateValue() const
103 switch([GetNSButton() state])
106 return wxCHK_UNCHECKED;
108 return wxCHK_CHECKED;
110 wxFAIL_MSG(wxT("[NSButton -state] returned an invalid state!"));
112 // Base class handles this assertion for us
113 // wxASSERT_MSG(Is3State(),wxT("NSMixedState returned from a 2-state checkbox"));
114 return wxCHK_UNDETERMINED;
118 void wxCheckBox::Cocoa_wxNSButtonAction(void)
120 wxLogTrace(wxTRACE_COCOA,wxT("Checkbox"));
121 // What we really want to do is override [NSCell -nextState] to return
122 // NSOnState in lieu of NSMixedState but this works (aside from the
123 // very slightly noticeable drawing of - and then a check) -DE
125 // Cocoa always allows a 3-state button to transition into
126 // the mixed/undetermined state by clicking, we don't
127 if ( !Is3rdStateAllowedForUser()
128 && [GetNSButton() state] == NSMixedState )
130 // Cocoa's sequence is on/off/mixed
131 // skip mixed, go right back to on
132 [GetNSButton() setState: NSOnState];
134 wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, GetId());
135 InitCommandEvent(event); // event.SetEventObject(this);
136 event.SetInt(Get3StateValue());
140 void wxCheckBox::SetLabel(const wxString& s)
142 wxAutoNSAutoreleasePool pool;
143 [GetNSButton() setTitle:wxNSStringWithWxString(s)];