]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/checkbox.mm
Make wxToolBar styles consistent and add wxTB_DEFAULT_STYLE.
[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 // RCS-ID: $Id$
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #if wxUSE_CHECKBOX
15
16 #include "wx/checkbox.h"
17
18 #ifndef WX_PRECOMP
19 #include "wx/log.h"
20 #include "wx/app.h"
21 #endif //WX_PRECOMP
22
23 #include "wx/cocoa/autorelease.h"
24 #include "wx/cocoa/string.h"
25
26 #import <AppKit/NSButton.h>
27 #import <Foundation/NSString.h>
28
29 BEGIN_EVENT_TABLE(wxCheckBox, wxCheckBoxBase)
30 END_EVENT_TABLE()
31 WX_IMPLEMENT_COCOA_OWNER(wxCheckBox,NSButton,NSControl,NSView)
32
33 bool wxCheckBox::Create(wxWindow *parent, wxWindowID winid,
34 const wxString& label,
35 const wxPoint& pos,
36 const wxSize& size,
37 long style,
38 const wxValidator& validator,
39 const wxString& name)
40 {
41 wxAutoNSAutoreleasePool pool;
42 if(!CreateControl(parent,winid,pos,size,style,validator,name))
43 return false;
44 m_cocoaNSView = NULL;
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];
51
52 if(m_parent)
53 m_parent->CocoaAddChild(this);
54 SetInitialFrameRect(pos,size);
55
56 return true;
57 }
58
59 wxCheckBox::~wxCheckBox()
60 {
61 DisassociateNSButton(GetNSButton());
62 }
63
64 void wxCheckBox::SetValue(bool value)
65 {
66 [GetNSButton() setState: value?NSOnState:NSOffState];
67 }
68
69 void wxCheckBox::DoSet3StateValue(wxCheckBoxState state)
70 {
71 int cocoaState;
72 switch(state)
73 {
74 case wxCHK_UNCHECKED:
75 cocoaState = NSOffState;
76 break;
77 case wxCHK_CHECKED:
78 cocoaState = NSOnState;
79 break;
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;
84 break;
85 default:
86 wxFAIL_MSG(wxT("Invalid state in wxCheckBox::DoSet3StateValue"));
87 return;
88 }
89 [GetNSButton() setState:cocoaState];
90 }
91
92 bool wxCheckBox::GetValue() const
93 {
94 int state = [GetNSButton() state];
95 wxASSERT_MSG(state!=NSMixedState || Is3State(),
96 wxT("NSMixedState returned from a 2-state checkbox"));
97 return state!=NSOffState;
98 }
99
100 wxCheckBoxState wxCheckBox::DoGet3StateValue() const
101 {
102 switch([GetNSButton() state])
103 {
104 case NSOffState:
105 return wxCHK_UNCHECKED;
106 case NSOnState:
107 return wxCHK_CHECKED;
108 default:
109 wxFAIL_MSG(wxT("[NSButton -state] returned an invalid state!"));
110 case NSMixedState:
111 // Base class handles this assertion for us
112 // wxASSERT_MSG(Is3State(),wxT("NSMixedState returned from a 2-state checkbox"));
113 return wxCHK_UNDETERMINED;
114 }
115 }
116
117 void wxCheckBox::Cocoa_wxNSButtonAction(void)
118 {
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
123
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 )
128 {
129 // Cocoa's sequence is on/off/mixed
130 // skip mixed, go right back to on
131 [GetNSButton() setState: NSOnState];
132 }
133 wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, GetId());
134 InitCommandEvent(event); // event.SetEventObject(this);
135 event.SetInt(Get3StateValue());
136 Command(event);
137 }
138
139 void wxCheckBox::SetLabel(const wxString& s)
140 {
141 wxAutoNSAutoreleasePool pool;
142 CocoaSetLabelForObject(s, GetNSButton());
143 }
144
145 wxString wxCheckBox::GetLabel() const
146 {
147 wxAutoNSAutoreleasePool pool;
148 return wxStringWithNSString([GetNSButton() title]);
149
150 }
151
152 #endif // wxUSE_CHECKBOX