1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/radiobut.mm
3 // Purpose: wxRadioButton
4 // Author: David Elliott
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
16 #include "wx/radiobut.h"
23 #import <AppKit/NSButton.h>
24 #include "wx/cocoa/string.h"
25 #include "wx/cocoa/autorelease.h"
27 #include "wx/listimpl.cpp"
29 WX_DEFINE_LIST(wxRadioButtonList);
31 IMPLEMENT_DYNAMIC_CLASS(wxRadioButton, wxControl)
32 // wxRadioButtonBase == wxControl
33 BEGIN_EVENT_TABLE(wxRadioButton, wxControl)
35 WX_IMPLEMENT_COCOA_OWNER(wxRadioButton,NSButton,NSControl,NSView)
37 bool wxRadioButton::Create(wxWindow *parent, wxWindowID winid,
38 const wxString& label,
42 const wxValidator& validator,
45 wxAutoNSAutoreleasePool pool;
47 if(!CreateControl(parent,winid,pos,size,style,validator,name))
52 m_radioSlaves.Append(this);
54 else if(style&wxRB_SINGLE)
58 for(wxWindowList::compatibility_iterator siblingNode= GetParent()->GetChildren().GetLast();
60 siblingNode = siblingNode->GetPrevious())
62 wxRadioButton *radioButton = wxDynamicCast(siblingNode->GetData(),wxRadioButton);
63 if(radioButton && radioButton!=this)
65 m_radioMaster = radioButton->m_radioMaster;
66 wxASSERT_MSG(m_radioMaster,
67 wxT("Previous radio button should be part of a group"));
68 // Don't crash, assume user meant wxRB_SINGLE
70 m_radioMaster->m_radioSlaves.Append(this);
77 SetNSButton([[NSButton alloc] initWithFrame: MakeDefaultNSRect(size)]);
78 [m_cocoaNSView release];
79 [GetNSButton() setButtonType: NSRadioButton];
80 CocoaSetLabelForObject(label, GetNSButton());
81 // If it's the first in a group, it should be selected
83 [GetNSButton() setState: NSOnState];
84 [GetNSControl() sizeToFit];
87 m_parent->CocoaAddChild(this);
88 SetInitialFrameRect(pos,size);
93 wxRadioButton::~wxRadioButton()
95 if(m_radioMaster==this)
97 // First get rid of ourselves (we should ALWAYS be at the head)
98 wxRadioButtonList::compatibility_iterator slaveNode =
99 m_radioSlaves.GetFirst();
101 wxASSERT(slaveNode->GetData() == this);
102 m_radioSlaves.Erase(slaveNode);
104 // Now find the new master
105 wxRadioButton *newMaster = NULL;
106 slaveNode = m_radioSlaves.GetFirst();
108 newMaster = slaveNode->GetData();
110 // For each node (including the new master) set the master, remove
111 // it from this list, and add it to the new master's list
112 for(; slaveNode; slaveNode = m_radioSlaves.GetFirst())
114 wxRadioButton *radioButton = slaveNode->GetData();
115 wxASSERT(radioButton->m_radioMaster == this);
116 radioButton->m_radioMaster = newMaster;
117 newMaster->m_radioSlaves.Append(radioButton);
118 m_radioSlaves.Erase(slaveNode);
121 else if(m_radioMaster)
123 m_radioMaster->m_radioSlaves.DeleteObject(this);
124 m_radioMaster = NULL;
127 DisassociateNSButton(GetNSButton());
130 void wxRadioButton::SetValue(bool value)
134 [GetNSButton() setState: NSOnState];
135 Cocoa_DeselectOtherButtonsInTheGroup();
138 [GetNSButton() setState: NSOffState];
141 bool wxRadioButton::GetValue() const
143 int state = [GetNSButton() state];
144 wxASSERT(state!=NSMixedState);
145 return state==NSOnState;
148 void wxRadioButton::SetLabel(const wxString& label)
150 wxAutoNSAutoreleasePool pool;
151 CocoaSetLabelForObject(label, GetNSButton());
154 wxString wxRadioButton::GetLabel() const
156 return wxStringWithNSString([GetNSButton() title]);
160 * If this radio button is part of a group, this method turns off every other
161 * button in the group. If this radio button is not part of a group, this
162 * method does absolutely nothing.
164 void wxRadioButton::Cocoa_DeselectOtherButtonsInTheGroup(void)
168 for(wxRadioButtonList::compatibility_iterator slaveNode =
169 m_radioMaster->m_radioSlaves.GetFirst();
170 slaveNode; slaveNode = slaveNode->GetNext())
172 wxRadioButton *radioButton = slaveNode->GetData();
173 if(radioButton!=this)
174 radioButton->SetValue(false);
179 void wxRadioButton::Cocoa_wxNSButtonAction(void)
181 wxLogTrace(wxTRACE_COCOA,wxT("wxRadioButton"));
182 if([GetNSButton() state] == NSOnState)
184 Cocoa_DeselectOtherButtonsInTheGroup();
186 wxCommandEvent event(wxEVT_COMMAND_RADIOBUTTON_SELECTED, GetId());
187 InitCommandEvent(event); // event.SetEventObject(this);
188 event.SetInt(GetValue());