1 /////////////////////////////////////////////////////////////////////////////
2 // Name: cocoa/checkbox.mm
4 // Author: David Elliott
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
16 #include "wx/checkbox.h"
19 #include "wx/cocoa/autorelease.h"
20 #include "wx/cocoa/string.h"
22 #import <AppKit/NSButton.h>
23 #import <Foundation/NSString.h>
25 IMPLEMENT_DYNAMIC_CLASS(wxCheckBox, wxControl)
26 BEGIN_EVENT_TABLE(wxCheckBox, wxCheckBoxBase)
28 WX_IMPLEMENT_COCOA_OWNER(wxCheckBox,NSButton,NSControl,NSView)
30 bool wxCheckBox::Create(wxWindow *parent, wxWindowID winid,
31 const wxString& label,
35 const wxValidator& validator,
38 wxAutoNSAutoreleasePool pool;
39 if(!CreateControl(parent,winid,pos,size,style,validator,name))
42 SetNSButton([[NSButton alloc] initWithFrame: MakeDefaultNSRect(size)]);
43 [m_cocoaNSView release];
44 [GetNSButton() setButtonType: NSSwitchButton];
45 [GetNSButton() setAllowsMixedState: Is3State()];
46 [GetNSButton() setTitle:wxNSStringWithWxString(wxStripMenuCodes(label))];
47 [GetNSControl() sizeToFit];
50 m_parent->CocoaAddChild(this);
51 SetInitialFrameRect(pos,size);
56 wxCheckBox::~wxCheckBox()
58 DisassociateNSButton(GetNSButton());
61 void wxCheckBox::SetValue(bool value)
63 [GetNSButton() setState: value?NSOnState:NSOffState];
66 void wxCheckBox::DoSet3StateValue(wxCheckBoxState state)
72 cocoaState = NSOffState;
75 cocoaState = NSOnState;
77 case wxCHK_UNDETERMINED:
78 // Base class would have already set state to wxCHK_UNCHECKED
79 // wxASSERT_MSG(Is3State(),"Use the wxCHK_3STATE style flag");
80 cocoaState = NSMixedState;
83 wxFAIL_MSG(wxT("Invalid state in wxCheckBox::DoSet3StateValue"));
86 [GetNSButton() setState:cocoaState];
89 bool wxCheckBox::GetValue() const
91 int state = [GetNSButton() state];
92 wxASSERT_MSG(state!=NSMixedState || Is3State(),
93 wxT("NSMixedState returned from a 2-state checkbox"));
94 return state!=NSOffState;
97 wxCheckBoxState wxCheckBox::DoGet3StateValue() const
99 switch([GetNSButton() state])
102 return wxCHK_UNCHECKED;
104 return wxCHK_CHECKED;
106 wxFAIL_MSG(wxT("[NSButton -state] returned an invalid state!"));
108 // Base class handles this assertion for us
109 // wxASSERT_MSG(Is3State(),wxT("NSMixedState returned from a 2-state checkbox"));
110 return wxCHK_UNDETERMINED;
114 void wxCheckBox::Cocoa_wxNSButtonAction(void)
116 wxLogTrace(wxTRACE_COCOA,wxT("Checkbox"));
117 // What we really want to do is override [NSCell -nextState] to return
118 // NSOnState in lieu of NSMixedState but this works (aside from the
119 // very slightly noticeable drawing of - and then a check) -DE
121 // Cocoa always allows a 3-state button to transition into
122 // the mixed/undetermined state by clicking, we don't
123 if ( !Is3rdStateAllowedForUser()
124 && [GetNSButton() state] == NSMixedState )
126 // Cocoa's sequence is on/off/mixed
127 // skip mixed, go right back to on
128 [GetNSButton() setState: NSOnState];
130 wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, GetId());
131 InitCommandEvent(event); // event.SetEventObject(this);
132 event.SetInt(Get3StateValue());