1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/checkbox.mm
4 // Author: David Elliott
7 // Copyright: (c) 2003 David Elliott
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #include "wx/wxprec.h"
15 #include "wx/checkbox.h"
22 #include "wx/cocoa/autorelease.h"
23 #include "wx/cocoa/string.h"
25 #import <AppKit/NSButton.h>
26 #import <Foundation/NSString.h>
28 BEGIN_EVENT_TABLE(wxCheckBox, wxCheckBoxBase)
30 WX_IMPLEMENT_COCOA_OWNER(wxCheckBox,NSButton,NSControl,NSView)
32 bool wxCheckBox::Create(wxWindow *parent, wxWindowID winid,
33 const wxString& label,
37 const wxValidator& validator,
40 wxAutoNSAutoreleasePool pool;
41 if(!CreateControl(parent,winid,pos,size,style,validator,name))
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];
52 m_parent->CocoaAddChild(this);
53 SetInitialFrameRect(pos,size);
58 wxCheckBox::~wxCheckBox()
60 DisassociateNSButton(GetNSButton());
63 void wxCheckBox::SetValue(bool value)
65 [GetNSButton() setState: value?NSOnState:NSOffState];
68 void wxCheckBox::DoSet3StateValue(wxCheckBoxState state)
74 cocoaState = NSOffState;
77 cocoaState = NSOnState;
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;
85 wxFAIL_MSG(wxT("Invalid state in wxCheckBox::DoSet3StateValue"));
88 [GetNSButton() setState:cocoaState];
91 bool wxCheckBox::GetValue() const
93 int state = [GetNSButton() state];
94 wxASSERT_MSG(state!=NSMixedState || Is3State(),
95 wxT("NSMixedState returned from a 2-state checkbox"));
96 return state!=NSOffState;
99 wxCheckBoxState wxCheckBox::DoGet3StateValue() const
101 switch([GetNSButton() state])
104 return wxCHK_UNCHECKED;
106 return wxCHK_CHECKED;
108 wxFAIL_MSG(wxT("[NSButton -state] returned an invalid state!"));
110 // Base class handles this assertion for us
111 // wxASSERT_MSG(Is3State(),wxT("NSMixedState returned from a 2-state checkbox"));
112 return wxCHK_UNDETERMINED;
116 void wxCheckBox::Cocoa_wxNSButtonAction(void)
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
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 )
128 // Cocoa's sequence is on/off/mixed
129 // skip mixed, go right back to on
130 [GetNSButton() setState: NSOnState];
132 wxCommandEvent event(wxEVT_CHECKBOX, GetId());
133 InitCommandEvent(event); // event.SetEventObject(this);
134 event.SetInt(Get3StateValue());
138 void wxCheckBox::SetLabel(const wxString& s)
140 wxAutoNSAutoreleasePool pool;
141 CocoaSetLabelForObject(s, GetNSButton());
144 wxString wxCheckBox::GetLabel() const
146 wxAutoNSAutoreleasePool pool;
147 return wxStringWithNSString([GetNSButton() title]);
151 #endif // wxUSE_CHECKBOX