]>
Commit | Line | Data |
---|---|---|
da0634c1 | 1 | ///////////////////////////////////////////////////////////////////////////// |
ab1ce969 | 2 | // Name: src/cocoa/checkbox.mm |
da0634c1 DE |
3 | // Purpose: wxCheckBox |
4 | // Author: David Elliott | |
5 | // Modified by: | |
6 | // Created: 2003/03/16 | |
da0634c1 | 7 | // Copyright: (c) 2003 David Elliott |
526954c5 | 8 | // Licence: wxWindows licence |
da0634c1 DE |
9 | ///////////////////////////////////////////////////////////////////////////// |
10 | ||
449c5673 | 11 | #include "wx/wxprec.h" |
16c81607 RN |
12 | |
13 | #if wxUSE_CHECKBOX | |
14 | ||
ab1ce969 WS |
15 | #include "wx/checkbox.h" |
16 | ||
449c5673 DE |
17 | #ifndef WX_PRECOMP |
18 | #include "wx/log.h" | |
19 | #include "wx/app.h" | |
449c5673 | 20 | #endif //WX_PRECOMP |
da0634c1 | 21 | |
7fc77f30 | 22 | #include "wx/cocoa/autorelease.h" |
677d7f24 | 23 | #include "wx/cocoa/string.h" |
7fc77f30 | 24 | |
da0634c1 DE |
25 | #import <AppKit/NSButton.h> |
26 | #import <Foundation/NSString.h> | |
27 | ||
da0634c1 DE |
28 | BEGIN_EVENT_TABLE(wxCheckBox, wxCheckBoxBase) |
29 | END_EVENT_TABLE() | |
30 | WX_IMPLEMENT_COCOA_OWNER(wxCheckBox,NSButton,NSControl,NSView) | |
31 | ||
32 | bool wxCheckBox::Create(wxWindow *parent, wxWindowID winid, | |
33 | const wxString& label, | |
34 | const wxPoint& pos, | |
35 | const wxSize& size, | |
36 | long style, | |
37 | const wxValidator& validator, | |
38 | const wxString& name) | |
39 | { | |
7fc77f30 | 40 | wxAutoNSAutoreleasePool pool; |
da0634c1 DE |
41 | if(!CreateControl(parent,winid,pos,size,style,validator,name)) |
42 | return false; | |
43 | m_cocoaNSView = NULL; | |
8d656ea9 | 44 | SetNSButton([[NSButton alloc] initWithFrame: MakeDefaultNSRect(size)]); |
da0634c1 DE |
45 | [m_cocoaNSView release]; |
46 | [GetNSButton() setButtonType: NSSwitchButton]; | |
7f7b69e2 | 47 | [GetNSButton() setAllowsMixedState: Is3State()]; |
525007cf | 48 | CocoaSetLabelForObject(label, GetNSButton()); |
da0634c1 DE |
49 | [GetNSControl() sizeToFit]; |
50 | ||
51 | if(m_parent) | |
52 | m_parent->CocoaAddChild(this); | |
8d656ea9 DE |
53 | SetInitialFrameRect(pos,size); |
54 | ||
da0634c1 DE |
55 | return true; |
56 | } | |
57 | ||
58 | wxCheckBox::~wxCheckBox() | |
59 | { | |
911e17c6 | 60 | DisassociateNSButton(GetNSButton()); |
da0634c1 DE |
61 | } |
62 | ||
a0c6a355 | 63 | void wxCheckBox::SetValue(bool value) |
da0634c1 | 64 | { |
7f7b69e2 DE |
65 | [GetNSButton() setState: value?NSOnState:NSOffState]; |
66 | } | |
67 | ||
68 | void wxCheckBox::DoSet3StateValue(wxCheckBoxState state) | |
69 | { | |
70 | int cocoaState; | |
71 | switch(state) | |
72 | { | |
73 | case wxCHK_UNCHECKED: | |
74 | cocoaState = NSOffState; | |
75 | break; | |
76 | case wxCHK_CHECKED: | |
77 | cocoaState = NSOnState; | |
78 | break; | |
79 | case wxCHK_UNDETERMINED: | |
079cc3b6 DE |
80 | // Base class would have already set state to wxCHK_UNCHECKED |
81 | // wxASSERT_MSG(Is3State(),"Use the wxCHK_3STATE style flag"); | |
7f7b69e2 DE |
82 | cocoaState = NSMixedState; |
83 | break; | |
84 | default: | |
85 | wxFAIL_MSG(wxT("Invalid state in wxCheckBox::DoSet3StateValue")); | |
86 | return; | |
87 | } | |
88 | [GetNSButton() setState:cocoaState]; | |
da0634c1 DE |
89 | } |
90 | ||
91 | bool wxCheckBox::GetValue() const | |
92 | { | |
a0c6a355 | 93 | int state = [GetNSButton() state]; |
7f7b69e2 DE |
94 | wxASSERT_MSG(state!=NSMixedState || Is3State(), |
95 | wxT("NSMixedState returned from a 2-state checkbox")); | |
079cc3b6 | 96 | return state!=NSOffState; |
da0634c1 DE |
97 | } |
98 | ||
7f7b69e2 DE |
99 | wxCheckBoxState wxCheckBox::DoGet3StateValue() const |
100 | { | |
101 | switch([GetNSButton() state]) | |
102 | { | |
103 | case NSOffState: | |
104 | return wxCHK_UNCHECKED; | |
105 | case NSOnState: | |
106 | return wxCHK_CHECKED; | |
107 | default: | |
108 | wxFAIL_MSG(wxT("[NSButton -state] returned an invalid state!")); | |
109 | case NSMixedState: | |
079cc3b6 DE |
110 | // Base class handles this assertion for us |
111 | // wxASSERT_MSG(Is3State(),wxT("NSMixedState returned from a 2-state checkbox")); | |
7f7b69e2 DE |
112 | return wxCHK_UNDETERMINED; |
113 | } | |
114 | } | |
115 | ||
da0634c1 DE |
116 | void wxCheckBox::Cocoa_wxNSButtonAction(void) |
117 | { | |
48580976 | 118 | wxLogTrace(wxTRACE_COCOA,wxT("Checkbox")); |
7f7b69e2 DE |
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 | |
122 | ||
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 ) | |
127 | { | |
128 | // Cocoa's sequence is on/off/mixed | |
129 | // skip mixed, go right back to on | |
130 | [GetNSButton() setState: NSOnState]; | |
131 | } | |
ce7fe42e | 132 | wxCommandEvent event(wxEVT_CHECKBOX, GetId()); |
a0c6a355 | 133 | InitCommandEvent(event); // event.SetEventObject(this); |
7f7b69e2 | 134 | event.SetInt(Get3StateValue()); |
a0c6a355 | 135 | Command(event); |
da0634c1 DE |
136 | } |
137 | ||
d803176a RN |
138 | void wxCheckBox::SetLabel(const wxString& s) |
139 | { | |
140 | wxAutoNSAutoreleasePool pool; | |
525007cf | 141 | CocoaSetLabelForObject(s, GetNSButton()); |
d803176a | 142 | } |
d174f457 VZ |
143 | |
144 | wxString wxCheckBox::GetLabel() const | |
145 | { | |
146 | wxAutoNSAutoreleasePool pool; | |
147 | return wxStringWithNSString([GetNSButton() title]); | |
148 | ||
149 | } | |
150 | ||
151 | #endif // wxUSE_CHECKBOX |