]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/checkbox.mm
b43f179077d8d1340c5863c4959c4291d54e537e
[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
21 #import <AppKit/NSButton.h>
22 #import <Foundation/NSString.h>
23
24 IMPLEMENT_DYNAMIC_CLASS(wxCheckBox, wxControl)
25 BEGIN_EVENT_TABLE(wxCheckBox, wxCheckBoxBase)
26 END_EVENT_TABLE()
27 WX_IMPLEMENT_COCOA_OWNER(wxCheckBox,NSButton,NSControl,NSView)
28
29 bool wxCheckBox::Create(wxWindow *parent, wxWindowID winid,
30 const wxString& label,
31 const wxPoint& pos,
32 const wxSize& size,
33 long style,
34 const wxValidator& validator,
35 const wxString& name)
36 {
37 wxAutoNSAutoreleasePool pool;
38 if(!CreateControl(parent,winid,pos,size,style,validator,name))
39 return false;
40 m_cocoaNSView = NULL;
41 SetNSButton([[NSButton alloc] initWithFrame: MakeDefaultNSRect(size)]);
42 [m_cocoaNSView release];
43 [GetNSButton() setButtonType: NSSwitchButton];
44 [GetNSButton() setTitle:[NSString stringWithCString: label.c_str()]];
45 [GetNSControl() sizeToFit];
46
47 if(m_parent)
48 m_parent->CocoaAddChild(this);
49 SetInitialFrameRect(pos,size);
50
51 return true;
52 }
53
54 wxCheckBox::~wxCheckBox()
55 {
56 DisassociateNSButton(GetNSButton());
57 }
58
59 void wxCheckBox::SetValue(bool value)
60 {
61 if(value)
62 [GetNSButton() setState: NSOnState];
63 else
64 [GetNSButton() setState: NSOffState];
65 }
66
67 bool wxCheckBox::GetValue() const
68 {
69 int state = [GetNSButton() state];
70 wxASSERT(state!=NSMixedState);
71 return state==NSOnState;
72 }
73
74 void wxCheckBox::Cocoa_wxNSButtonAction(void)
75 {
76 wxLogDebug("Checkbox");
77 wxCommandEvent event(wxEVT_COMMAND_CHECKBOX_CLICKED, GetId());
78 InitCommandEvent(event); // event.SetEventObject(this);
79 event.SetInt(GetValue());
80 Command(event);
81 }
82