]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/checkbox.mm
More lib and demo patches to drop the wx prefix (Jeff has been busy!)
[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() setTitle:wxNSStringWithWxString(wxStripMenuCodes(label))];
46 [GetNSControl() sizeToFit];
47
48 if(m_parent)
49 m_parent->CocoaAddChild(this);
50 SetInitialFrameRect(pos,size);
51
52 return true;
53 }
54
55 wxCheckBox::~wxCheckBox()
56 {
57 DisassociateNSButton(GetNSButton());
58 }
59
60 void wxCheckBox::SetValue(bool value)
61 {
62 if(value)
63 [GetNSButton() setState: NSOnState];
64 else
65 [GetNSButton() setState: NSOffState];
66 }
67
68 bool wxCheckBox::GetValue() const
69 {
70 int state = [GetNSButton() state];
71 wxASSERT(state!=NSMixedState);
72 return state==NSOnState;
73 }
74
75 void wxCheckBox::Cocoa_wxNSButtonAction(void)
76 {
77 wxLogDebug("Checkbox");
78 wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, GetId());
79 InitCommandEvent(event); // event.SetEventObject(this);
80 event.SetInt(GetValue());
81 Command(event);
82 }
83