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