]>
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 | |
ab1ce969 | 7 | // RCS-ID: $Id$ |
da0634c1 | 8 | // Copyright: (c) 2003 David Elliott |
526954c5 | 9 | // Licence: wxWindows licence |
da0634c1 DE |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
449c5673 | 12 | #include "wx/wxprec.h" |
16c81607 RN |
13 | |
14 | #if wxUSE_CHECKBOX | |
15 | ||
ab1ce969 WS |
16 | #include "wx/checkbox.h" |
17 | ||
449c5673 DE |
18 | #ifndef WX_PRECOMP |
19 | #include "wx/log.h" | |
20 | #include "wx/app.h" | |
449c5673 | 21 | #endif //WX_PRECOMP |
da0634c1 | 22 | |
7fc77f30 | 23 | #include "wx/cocoa/autorelease.h" |
677d7f24 | 24 | #include "wx/cocoa/string.h" |
7fc77f30 | 25 | |
da0634c1 DE |
26 | #import <AppKit/NSButton.h> |
27 | #import <Foundation/NSString.h> | |
28 | ||
da0634c1 DE |
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 | { | |
7fc77f30 | 41 | wxAutoNSAutoreleasePool pool; |
da0634c1 DE |
42 | if(!CreateControl(parent,winid,pos,size,style,validator,name)) |
43 | return false; | |
44 | m_cocoaNSView = NULL; | |
8d656ea9 | 45 | SetNSButton([[NSButton alloc] initWithFrame: MakeDefaultNSRect(size)]); |
da0634c1 DE |
46 | [m_cocoaNSView release]; |
47 | [GetNSButton() setButtonType: NSSwitchButton]; | |
7f7b69e2 | 48 | [GetNSButton() setAllowsMixedState: Is3State()]; |
525007cf | 49 | CocoaSetLabelForObject(label, GetNSButton()); |
da0634c1 DE |
50 | [GetNSControl() sizeToFit]; |
51 | ||
52 | if(m_parent) | |
53 | m_parent->CocoaAddChild(this); | |
8d656ea9 DE |
54 | SetInitialFrameRect(pos,size); |
55 | ||
da0634c1 DE |
56 | return true; |
57 | } | |
58 | ||
59 | wxCheckBox::~wxCheckBox() | |
60 | { | |
911e17c6 | 61 | DisassociateNSButton(GetNSButton()); |
da0634c1 DE |
62 | } |
63 | ||
a0c6a355 | 64 | void wxCheckBox::SetValue(bool value) |
da0634c1 | 65 | { |
7f7b69e2 DE |
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: | |
079cc3b6 DE |
81 | // Base class would have already set state to wxCHK_UNCHECKED |
82 | // wxASSERT_MSG(Is3State(),"Use the wxCHK_3STATE style flag"); | |
7f7b69e2 DE |
83 | cocoaState = NSMixedState; |
84 | break; | |
85 | default: | |
86 | wxFAIL_MSG(wxT("Invalid state in wxCheckBox::DoSet3StateValue")); | |
87 | return; | |
88 | } | |
89 | [GetNSButton() setState:cocoaState]; | |
da0634c1 DE |
90 | } |
91 | ||
92 | bool wxCheckBox::GetValue() const | |
93 | { | |
a0c6a355 | 94 | int state = [GetNSButton() state]; |
7f7b69e2 DE |
95 | wxASSERT_MSG(state!=NSMixedState || Is3State(), |
96 | wxT("NSMixedState returned from a 2-state checkbox")); | |
079cc3b6 | 97 | return state!=NSOffState; |
da0634c1 DE |
98 | } |
99 | ||
7f7b69e2 DE |
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: | |
079cc3b6 DE |
111 | // Base class handles this assertion for us |
112 | // wxASSERT_MSG(Is3State(),wxT("NSMixedState returned from a 2-state checkbox")); | |
7f7b69e2 DE |
113 | return wxCHK_UNDETERMINED; |
114 | } | |
115 | } | |
116 | ||
da0634c1 DE |
117 | void wxCheckBox::Cocoa_wxNSButtonAction(void) |
118 | { | |
48580976 | 119 | wxLogTrace(wxTRACE_COCOA,wxT("Checkbox")); |
7f7b69e2 DE |
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 | } | |
ce7fe42e | 133 | wxCommandEvent event(wxEVT_CHECKBOX, GetId()); |
a0c6a355 | 134 | InitCommandEvent(event); // event.SetEventObject(this); |
7f7b69e2 | 135 | event.SetInt(Get3StateValue()); |
a0c6a355 | 136 | Command(event); |
da0634c1 DE |
137 | } |
138 | ||
d803176a RN |
139 | void wxCheckBox::SetLabel(const wxString& s) |
140 | { | |
141 | wxAutoNSAutoreleasePool pool; | |
525007cf | 142 | CocoaSetLabelForObject(s, GetNSButton()); |
d803176a | 143 | } |
d174f457 VZ |
144 | |
145 | wxString wxCheckBox::GetLabel() const | |
146 | { | |
147 | wxAutoNSAutoreleasePool pool; | |
148 | return wxStringWithNSString([GetNSButton() title]); | |
149 | ||
150 | } | |
151 | ||
152 | #endif // wxUSE_CHECKBOX |