]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/checkbox.mm
use margin width after updating it in OnMeasureItem() (see #10452)
[wxWidgets.git] / src / cocoa / checkbox.mm
CommitLineData
da0634c1 1/////////////////////////////////////////////////////////////////////////////
ab1ce969 2// Name: src/cocoa/checkbox.mm
da0634c1
DE
3// Purpose: wxCheckBox
4// Author: David Elliott
5// Modified by:
6// Created: 2003/03/16
ab1ce969 7// RCS-ID: $Id$
da0634c1 8// Copyright: (c) 2003 David Elliott
ab1ce969 9// Licence: wxWidgets licence
da0634c1
DE
10/////////////////////////////////////////////////////////////////////////////
11
449c5673 12#include "wx/wxprec.h"
16c81607
RN
13
14#if wxUSE_CHECKBOX
15
ab1ce969
WS
16#include "wx/checkbox.h"
17
449c5673
DE
18#ifndef WX_PRECOMP
19 #include "wx/log.h"
20 #include "wx/app.h"
449c5673 21#endif //WX_PRECOMP
da0634c1 22
7fc77f30 23#include "wx/cocoa/autorelease.h"
677d7f24 24#include "wx/cocoa/string.h"
7fc77f30 25
da0634c1
DE
26#import <AppKit/NSButton.h>
27#import <Foundation/NSString.h>
28
29IMPLEMENT_DYNAMIC_CLASS(wxCheckBox, wxControl)
30BEGIN_EVENT_TABLE(wxCheckBox, wxCheckBoxBase)
31END_EVENT_TABLE()
32WX_IMPLEMENT_COCOA_OWNER(wxCheckBox,NSButton,NSControl,NSView)
33
34bool 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{
7fc77f30 42 wxAutoNSAutoreleasePool pool;
da0634c1
DE
43 if(!CreateControl(parent,winid,pos,size,style,validator,name))
44 return false;
45 m_cocoaNSView = NULL;
8d656ea9 46 SetNSButton([[NSButton alloc] initWithFrame: MakeDefaultNSRect(size)]);
da0634c1
DE
47 [m_cocoaNSView release];
48 [GetNSButton() setButtonType: NSSwitchButton];
7f7b69e2 49 [GetNSButton() setAllowsMixedState: Is3State()];
525007cf 50 CocoaSetLabelForObject(label, GetNSButton());
da0634c1
DE
51 [GetNSControl() sizeToFit];
52
53 if(m_parent)
54 m_parent->CocoaAddChild(this);
8d656ea9
DE
55 SetInitialFrameRect(pos,size);
56
da0634c1
DE
57 return true;
58}
59
60wxCheckBox::~wxCheckBox()
61{
911e17c6 62 DisassociateNSButton(GetNSButton());
da0634c1
DE
63}
64
a0c6a355 65void wxCheckBox::SetValue(bool value)
da0634c1 66{
7f7b69e2
DE
67 [GetNSButton() setState: value?NSOnState:NSOffState];
68}
69
70void 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:
079cc3b6
DE
82 // Base class would have already set state to wxCHK_UNCHECKED
83// wxASSERT_MSG(Is3State(),"Use the wxCHK_3STATE style flag");
7f7b69e2
DE
84 cocoaState = NSMixedState;
85 break;
86 default:
87 wxFAIL_MSG(wxT("Invalid state in wxCheckBox::DoSet3StateValue"));
88 return;
89 }
90 [GetNSButton() setState:cocoaState];
da0634c1
DE
91}
92
93bool wxCheckBox::GetValue() const
94{
a0c6a355 95 int state = [GetNSButton() state];
7f7b69e2
DE
96 wxASSERT_MSG(state!=NSMixedState || Is3State(),
97 wxT("NSMixedState returned from a 2-state checkbox"));
079cc3b6 98 return state!=NSOffState;
da0634c1
DE
99}
100
7f7b69e2
DE
101wxCheckBoxState 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:
079cc3b6
DE
112 // Base class handles this assertion for us
113// wxASSERT_MSG(Is3State(),wxT("NSMixedState returned from a 2-state checkbox"));
7f7b69e2
DE
114 return wxCHK_UNDETERMINED;
115 }
116}
117
da0634c1
DE
118void wxCheckBox::Cocoa_wxNSButtonAction(void)
119{
48580976 120 wxLogTrace(wxTRACE_COCOA,wxT("Checkbox"));
7f7b69e2
DE
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 }
a0c6a355
DE
134 wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, GetId());
135 InitCommandEvent(event); // event.SetEventObject(this);
7f7b69e2 136 event.SetInt(Get3StateValue());
a0c6a355 137 Command(event);
da0634c1
DE
138}
139
d803176a
RN
140void wxCheckBox::SetLabel(const wxString& s)
141{
142 wxAutoNSAutoreleasePool pool;
525007cf 143 CocoaSetLabelForObject(s, GetNSButton());
d803176a 144}
d174f457
VZ
145
146wxString wxCheckBox::GetLabel() const
147{
148 wxAutoNSAutoreleasePool pool;
149 return wxStringWithNSString([GetNSButton() title]);
150
151}
152
153#endif // wxUSE_CHECKBOX