Applied modified 3-state checkbox patch from Mathew Cucuzella
[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:     wxWindows license
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         wxASSERT_MSG(Is3State(),"Use the wxCHK_3STATE style flag");
79         cocoaState = NSMixedState;
80         break;
81     default:
82         wxFAIL_MSG(wxT("Invalid state in wxCheckBox::DoSet3StateValue"));
83         return;
84     }
85     [GetNSButton() setState:cocoaState];
86 }
87
88 bool wxCheckBox::GetValue() const
89 {
90     int state = [GetNSButton() state];
91     wxASSERT_MSG(state!=NSMixedState || Is3State(),
92         wxT("NSMixedState returned from a 2-state checkbox"));
93     return state==NSOnState;
94 }
95
96 wxCheckBoxState wxCheckBox::DoGet3StateValue() const
97 {
98     switch([GetNSButton() state])
99     {
100     case NSOffState:
101         return wxCHK_UNCHECKED;
102     case NSOnState:
103         return wxCHK_CHECKED;
104     default:
105         wxFAIL_MSG(wxT("[NSButton -state] returned an invalid state!"));
106     case NSMixedState:
107         wxASSERT_MSG(Is3State(),wxT("NSMixedState returned from a 2-state checkbox"));
108         return wxCHK_UNDETERMINED;
109     }
110 }
111
112 void wxCheckBox::Cocoa_wxNSButtonAction(void)
113 {
114     wxLogDebug(wxT("Checkbox"));
115     // What we really want to do is override [NSCell -nextState] to return
116     // NSOnState in lieu of NSMixedState but this works (aside from the
117     // very slightly noticeable drawing of - and then a check) -DE
118
119     // Cocoa always allows a 3-state button to transition into
120     // the mixed/undetermined state by clicking, we don't
121     if ( !Is3rdStateAllowedForUser()
122          && [GetNSButton() state] == NSMixedState )
123     {
124         // Cocoa's sequence is on/off/mixed
125         // skip mixed, go right back to on
126         [GetNSButton() setState: NSOnState];
127     }
128     wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, GetId());
129     InitCommandEvent(event); //    event.SetEventObject(this);
130     event.SetInt(Get3StateValue());
131     Command(event);
132 }
133