1 /////////////////////////////////////////////////////////////////////////////
2 // Name: cocoa/radiobut.mm
3 // Purpose: wxRadioButton
4 // Author: David Elliott
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #include "wx/radiobut.h"
16 #import <AppKit/NSButton.h>
17 #include "wx/cocoa/string.h"
18 #include "wx/cocoa/autorelease.h"
20 #include "wx/listimpl.cpp"
22 WX_DEFINE_LIST(wxRadioButtonList);
24 IMPLEMENT_DYNAMIC_CLASS(wxRadioButton, wxControl)
25 // wxRadioButtonBase == wxControl
26 BEGIN_EVENT_TABLE(wxRadioButton, wxControl)
28 WX_IMPLEMENT_COCOA_OWNER(wxRadioButton,NSButton,NSControl,NSView)
30 bool wxRadioButton::Create(wxWindow *parent, wxWindowID winid,
31 const wxString& label,
35 const wxValidator& validator,
38 wxAutoNSAutoreleasePool pool;
40 if(!CreateControl(parent,winid,pos,size,style,validator,name))
45 m_radioSlaves.Append(this);
47 else if(style&wxRB_SINGLE)
51 for(wxWindowList::compatibility_iterator siblingNode= GetParent()->GetChildren().GetLast();
53 siblingNode = siblingNode->GetPrevious())
55 wxRadioButton *radioButton = wxDynamicCast(siblingNode->GetData(),wxRadioButton);
56 if(radioButton && radioButton!=this)
58 m_radioMaster = radioButton->m_radioMaster;
59 wxASSERT_MSG(m_radioMaster,
60 "Previous radio button should be part of a group");
61 // Don't crash, assume user meant wxRB_SINGLE
63 m_radioMaster->m_radioSlaves.Append(this);
70 SetNSButton([[NSButton alloc] initWithFrame: MakeDefaultNSRect(size)]);
71 [m_cocoaNSView release];
72 [GetNSButton() setButtonType: NSRadioButton];
73 [GetNSButton() setTitle:wxNSStringWithWxString(label)];
74 [GetNSControl() sizeToFit];
77 m_parent->CocoaAddChild(this);
78 SetInitialFrameRect(pos,size);
83 wxRadioButton::~wxRadioButton()
85 if(m_radioMaster==this)
87 // First get rid of ourselves (we should ALWAYS be at the head)
88 wxRadioButtonList::compatibility_iterator slaveNode =
89 m_radioSlaves.GetFirst();
91 wxASSERT(slaveNode->GetData() == this);
92 m_radioSlaves.DeleteNode(slaveNode);
94 // Now find the new master
95 wxRadioButton *newMaster = NULL;
96 slaveNode = m_radioSlaves.GetFirst();
98 newMaster = slaveNode->GetData();
100 // For each node (including the new master) set the master, remove
101 // it from this list, and add it to the new master's list
102 for(; slaveNode; slaveNode = m_radioSlaves.GetFirst())
104 wxRadioButton *radioButton = slaveNode->GetData();
105 wxASSERT(radioButton->m_radioMaster == this);
106 radioButton->m_radioMaster = newMaster;
107 newMaster->m_radioSlaves.Append(radioButton);
108 m_radioSlaves.DeleteNode(slaveNode);
111 else if(m_radioMaster)
113 m_radioMaster->m_radioSlaves.DeleteObject(this);
114 m_radioMaster = NULL;
117 DisassociateNSButton(m_cocoaNSView);
120 void wxRadioButton::SetValue(bool value)
123 [GetNSButton() setState: NSOnState];
125 [GetNSButton() setState: NSOffState];
128 bool wxRadioButton::GetValue() const
130 int state = [GetNSButton() state];
131 wxASSERT(state!=NSMixedState);
132 return state==NSOnState;
135 void wxRadioButton::Cocoa_wxNSButtonAction(void)
137 wxLogDebug("wxRadioButton");
138 if(m_radioMaster && ([GetNSButton() state] == NSOnState))
140 for(wxRadioButtonList::compatibility_iterator slaveNode =
141 m_radioMaster->m_radioSlaves.GetFirst();
142 slaveNode; slaveNode = slaveNode->GetNext())
144 wxRadioButton *radioButton = slaveNode->GetData();
145 if(radioButton!=this)
146 radioButton->SetValue(false);
149 wxCommandEvent event(wxEVT_COMMAND_RADIOBUTTON_SELECTED, GetId());
150 InitCommandEvent(event); // event.SetEventObject(this);
151 event.SetInt(GetValue());