]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/checkbox.mm
don't use negative sizes for row/column labels subwindows to avoid GTK warnings ...
[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: wxWidgets 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 IMPLEMENT_DYNAMIC_CLASS(wxCheckBox, wxControl)
30 BEGIN_EVENT_TABLE(wxCheckBox, wxCheckBoxBase)
31 END_EVENT_TABLE()
32 WX_IMPLEMENT_COCOA_OWNER(wxCheckBox,NSButton,NSControl,NSView)
33
34 bool wxCheckBox::Create(wxWindow *parent, wxWindowID winid,
35 const wxString& label,
36 const wxPoint& pos,
37 const wxSize& size,
38 long style,
39 const wxValidator& validator,
40 const wxString& name)
41 {
42 wxAutoNSAutoreleasePool pool;
43 if(!CreateControl(parent,winid,pos,size,style,validator,name))
44 return false;
45 m_cocoaNSView = NULL;
46 SetNSButton([[NSButton alloc] initWithFrame: MakeDefaultNSRect(size)]);
47 [m_cocoaNSView release];
48 [GetNSButton() setButtonType: NSSwitchButton];
49 [GetNSButton() setAllowsMixedState: Is3State()];
50 CocoaSetLabelForObject(label, GetNSButton());
51 [GetNSControl() sizeToFit];
52
53 if(m_parent)
54 m_parent->CocoaAddChild(this);
55 SetInitialFrameRect(pos,size);
56
57 return true;
58 }
59
60 wxCheckBox::~wxCheckBox()
61 {
62 DisassociateNSButton(GetNSButton());
63 }
64
65 void wxCheckBox::SetValue(bool value)
66 {
67 [GetNSButton() setState: value?NSOnState:NSOffState];
68 }
69
70 void wxCheckBox::DoSet3StateValue(wxCheckBoxState state)
71 {
72 int cocoaState;
73 switch(state)
74 {
75 case wxCHK_UNCHECKED:
76 cocoaState = NSOffState;
77 break;
78 case wxCHK_CHECKED:
79 cocoaState = NSOnState;
80 break;
81 case wxCHK_UNDETERMINED:
82 // Base class would have already set state to wxCHK_UNCHECKED
83 // wxASSERT_MSG(Is3State(),"Use the wxCHK_3STATE style flag");
84 cocoaState = NSMixedState;
85 break;
86 default:
87 wxFAIL_MSG(wxT("Invalid state in wxCheckBox::DoSet3StateValue"));
88 return;
89 }
90 [GetNSButton() setState:cocoaState];
91 }
92
93 bool wxCheckBox::GetValue() const
94 {
95 int state = [GetNSButton() state];
96 wxASSERT_MSG(state!=NSMixedState || Is3State(),
97 wxT("NSMixedState returned from a 2-state checkbox"));
98 return state!=NSOffState;
99 }
100
101 wxCheckBoxState wxCheckBox::DoGet3StateValue() const
102 {
103 switch([GetNSButton() state])
104 {
105 case NSOffState:
106 return wxCHK_UNCHECKED;
107 case NSOnState:
108 return wxCHK_CHECKED;
109 default:
110 wxFAIL_MSG(wxT("[NSButton -state] returned an invalid state!"));
111 case NSMixedState:
112 // Base class handles this assertion for us
113 // wxASSERT_MSG(Is3State(),wxT("NSMixedState returned from a 2-state checkbox"));
114 return wxCHK_UNDETERMINED;
115 }
116 }
117
118 void wxCheckBox::Cocoa_wxNSButtonAction(void)
119 {
120 wxLogTrace(wxTRACE_COCOA,wxT("Checkbox"));
121 // What we really want to do is override [NSCell -nextState] to return
122 // NSOnState in lieu of NSMixedState but this works (aside from the
123 // very slightly noticeable drawing of - and then a check) -DE
124
125 // Cocoa always allows a 3-state button to transition into
126 // the mixed/undetermined state by clicking, we don't
127 if ( !Is3rdStateAllowedForUser()
128 && [GetNSButton() state] == NSMixedState )
129 {
130 // Cocoa's sequence is on/off/mixed
131 // skip mixed, go right back to on
132 [GetNSButton() setState: NSOnState];
133 }
134 wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, GetId());
135 InitCommandEvent(event); // event.SetEventObject(this);
136 event.SetInt(Get3StateValue());
137 Command(event);
138 }
139
140 void wxCheckBox::SetLabel(const wxString& s)
141 {
142 wxAutoNSAutoreleasePool pool;
143 CocoaSetLabelForObject(s, GetNSButton());
144 }
145
146 wxString wxCheckBox::GetLabel() const
147 {
148 wxAutoNSAutoreleasePool pool;
149 return wxStringWithNSString([GetNSButton() title]);
150
151 }
152
153 #endif // wxUSE_CHECKBOX