]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/checkbox.mm
Committed forgotten files
[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 12#include "wx/wxprec.h"
16c81607
RN
13
14#if wxUSE_CHECKBOX
15
449c5673
DE
16#ifndef WX_PRECOMP
17 #include "wx/log.h"
18 #include "wx/app.h"
19 #include "wx/checkbox.h"
20#endif //WX_PRECOMP
da0634c1 21
7fc77f30 22#include "wx/cocoa/autorelease.h"
677d7f24 23#include "wx/cocoa/string.h"
7fc77f30 24
da0634c1
DE
25#import <AppKit/NSButton.h>
26#import <Foundation/NSString.h>
27
28IMPLEMENT_DYNAMIC_CLASS(wxCheckBox, wxControl)
29BEGIN_EVENT_TABLE(wxCheckBox, wxCheckBoxBase)
30END_EVENT_TABLE()
31WX_IMPLEMENT_COCOA_OWNER(wxCheckBox,NSButton,NSControl,NSView)
32
33bool 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()];
677d7f24 49 [GetNSButton() setTitle:wxNSStringWithWxString(wxStripMenuCodes(label))];
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
59wxCheckBox::~wxCheckBox()
60{
911e17c6 61 DisassociateNSButton(GetNSButton());
da0634c1
DE
62}
63
a0c6a355 64void wxCheckBox::SetValue(bool value)
da0634c1 65{
7f7b69e2
DE
66 [GetNSButton() setState: value?NSOnState:NSOffState];
67}
68
69void 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
92bool 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
100wxCheckBoxState 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
117void 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 }
a0c6a355
DE
133 wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, GetId());
134 InitCommandEvent(event); // event.SetEventObject(this);
7f7b69e2 135 event.SetInt(Get3StateValue());
a0c6a355 136 Command(event);
da0634c1
DE
137}
138
d803176a
RN
139void wxCheckBox::SetLabel(const wxString& s)
140{
141 wxAutoNSAutoreleasePool pool;
142 [GetNSButton() setTitle:wxNSStringWithWxString(s)];
143}
cadbf2a8 144#endif