]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/checkbox.mm
PutChar doc
[wxWidgets.git] / src / cocoa / checkbox.mm
CommitLineData
da0634c1
DE
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
065e208e 9// Licence: wxWidgets licence
da0634c1
DE
10/////////////////////////////////////////////////////////////////////////////
11
449c5673
DE
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
da0634c1 18
7fc77f30 19#include "wx/cocoa/autorelease.h"
677d7f24 20#include "wx/cocoa/string.h"
7fc77f30 21
da0634c1
DE
22#import <AppKit/NSButton.h>
23#import <Foundation/NSString.h>
24
25IMPLEMENT_DYNAMIC_CLASS(wxCheckBox, wxControl)
26BEGIN_EVENT_TABLE(wxCheckBox, wxCheckBoxBase)
27END_EVENT_TABLE()
28WX_IMPLEMENT_COCOA_OWNER(wxCheckBox,NSButton,NSControl,NSView)
29
30bool 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{
7fc77f30 38 wxAutoNSAutoreleasePool pool;
da0634c1
DE
39 if(!CreateControl(parent,winid,pos,size,style,validator,name))
40 return false;
41 m_cocoaNSView = NULL;
8d656ea9 42 SetNSButton([[NSButton alloc] initWithFrame: MakeDefaultNSRect(size)]);
da0634c1
DE
43 [m_cocoaNSView release];
44 [GetNSButton() setButtonType: NSSwitchButton];
7f7b69e2 45 [GetNSButton() setAllowsMixedState: Is3State()];
677d7f24 46 [GetNSButton() setTitle:wxNSStringWithWxString(wxStripMenuCodes(label))];
da0634c1
DE
47 [GetNSControl() sizeToFit];
48
49 if(m_parent)
50 m_parent->CocoaAddChild(this);
8d656ea9
DE
51 SetInitialFrameRect(pos,size);
52
da0634c1
DE
53 return true;
54}
55
56wxCheckBox::~wxCheckBox()
57{
911e17c6 58 DisassociateNSButton(GetNSButton());
da0634c1
DE
59}
60
a0c6a355 61void wxCheckBox::SetValue(bool value)
da0634c1 62{
7f7b69e2
DE
63 [GetNSButton() setState: value?NSOnState:NSOffState];
64}
65
66void 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:
079cc3b6
DE
78 // Base class would have already set state to wxCHK_UNCHECKED
79// wxASSERT_MSG(Is3State(),"Use the wxCHK_3STATE style flag");
7f7b69e2
DE
80 cocoaState = NSMixedState;
81 break;
82 default:
83 wxFAIL_MSG(wxT("Invalid state in wxCheckBox::DoSet3StateValue"));
84 return;
85 }
86 [GetNSButton() setState:cocoaState];
da0634c1
DE
87}
88
89bool wxCheckBox::GetValue() const
90{
a0c6a355 91 int state = [GetNSButton() state];
7f7b69e2
DE
92 wxASSERT_MSG(state!=NSMixedState || Is3State(),
93 wxT("NSMixedState returned from a 2-state checkbox"));
079cc3b6 94 return state!=NSOffState;
da0634c1
DE
95}
96
7f7b69e2
DE
97wxCheckBoxState 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:
079cc3b6
DE
108 // Base class handles this assertion for us
109// wxASSERT_MSG(Is3State(),wxT("NSMixedState returned from a 2-state checkbox"));
7f7b69e2
DE
110 return wxCHK_UNDETERMINED;
111 }
112}
113
da0634c1
DE
114void wxCheckBox::Cocoa_wxNSButtonAction(void)
115{
48580976 116 wxLogTrace(wxTRACE_COCOA,wxT("Checkbox"));
7f7b69e2
DE
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 }
a0c6a355
DE
130 wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, GetId());
131 InitCommandEvent(event); // event.SetEventObject(this);
7f7b69e2 132 event.SetInt(Get3StateValue());
a0c6a355 133 Command(event);
da0634c1
DE
134}
135