1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/checkbox.mm
4 // Author: David Elliott
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWindows 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 BEGIN_EVENT_TABLE(wxCheckBox, wxCheckBoxBase)
31 WX_IMPLEMENT_COCOA_OWNER(wxCheckBox,NSButton,NSControl,NSView)
33 bool wxCheckBox::Create(wxWindow *parent, wxWindowID winid,
34 const wxString& label,
38 const wxValidator& validator,
41 wxAutoNSAutoreleasePool pool;
42 if(!CreateControl(parent,winid,pos,size,style,validator,name))
45 SetNSButton([[NSButton alloc] initWithFrame: MakeDefaultNSRect(size)]);
46 [m_cocoaNSView release];
47 [GetNSButton() setButtonType: NSSwitchButton];
48 [GetNSButton() setAllowsMixedState: Is3State()];
49 CocoaSetLabelForObject(label, GetNSButton());
50 [GetNSControl() sizeToFit];
53 m_parent->CocoaAddChild(this);
54 SetInitialFrameRect(pos,size);
59 wxCheckBox::~wxCheckBox()
61 DisassociateNSButton(GetNSButton());
64 void wxCheckBox::SetValue(bool value)
66 [GetNSButton() setState: value?NSOnState:NSOffState];
69 void wxCheckBox::DoSet3StateValue(wxCheckBoxState state)
75 cocoaState = NSOffState;
78 cocoaState = NSOnState;
80 case wxCHK_UNDETERMINED:
81 // Base class would have already set state to wxCHK_UNCHECKED
82 // wxASSERT_MSG(Is3State(),"Use the wxCHK_3STATE style flag");
83 cocoaState = NSMixedState;
86 wxFAIL_MSG(wxT("Invalid state in wxCheckBox::DoSet3StateValue"));
89 [GetNSButton() setState:cocoaState];
92 bool wxCheckBox::GetValue() const
94 int state = [GetNSButton() state];
95 wxASSERT_MSG(state!=NSMixedState || Is3State(),
96 wxT("NSMixedState returned from a 2-state checkbox"));
97 return state!=NSOffState;
100 wxCheckBoxState wxCheckBox::DoGet3StateValue() const
102 switch([GetNSButton() state])
105 return wxCHK_UNCHECKED;
107 return wxCHK_CHECKED;
109 wxFAIL_MSG(wxT("[NSButton -state] returned an invalid state!"));
111 // Base class handles this assertion for us
112 // wxASSERT_MSG(Is3State(),wxT("NSMixedState returned from a 2-state checkbox"));
113 return wxCHK_UNDETERMINED;
117 void wxCheckBox::Cocoa_wxNSButtonAction(void)
119 wxLogTrace(wxTRACE_COCOA,wxT("Checkbox"));
120 // What we really want to do is override [NSCell -nextState] to return
121 // NSOnState in lieu of NSMixedState but this works (aside from the
122 // very slightly noticeable drawing of - and then a check) -DE
124 // Cocoa always allows a 3-state button to transition into
125 // the mixed/undetermined state by clicking, we don't
126 if ( !Is3rdStateAllowedForUser()
127 && [GetNSButton() state] == NSMixedState )
129 // Cocoa's sequence is on/off/mixed
130 // skip mixed, go right back to on
131 [GetNSButton() setState: NSOnState];
133 wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, GetId());
134 InitCommandEvent(event); // event.SetEventObject(this);
135 event.SetInt(Get3StateValue());
139 void wxCheckBox::SetLabel(const wxString& s)
141 wxAutoNSAutoreleasePool pool;
142 CocoaSetLabelForObject(s, GetNSButton());
145 wxString wxCheckBox::GetLabel() const
147 wxAutoNSAutoreleasePool pool;
148 return wxStringWithNSString([GetNSButton() title]);
152 #endif // wxUSE_CHECKBOX