1 /////////////////////////////////////////////////////////////////////////////
2 // Name: cocoa/radiobut.mm
3 // Purpose: wxRadioButton
4 // Author: David Elliott
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWidgets licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
16 #include "wx/radiobut.h"
19 #import <AppKit/NSButton.h>
20 #include "wx/cocoa/string.h"
21 #include "wx/cocoa/autorelease.h"
23 #include "wx/listimpl.cpp"
25 WX_DEFINE_LIST(wxRadioButtonList);
27 IMPLEMENT_DYNAMIC_CLASS(wxRadioButton, wxControl)
28 // wxRadioButtonBase == wxControl
29 BEGIN_EVENT_TABLE(wxRadioButton, wxControl)
31 WX_IMPLEMENT_COCOA_OWNER(wxRadioButton,NSButton,NSControl,NSView)
33 bool wxRadioButton::Create(wxWindow *parent, wxWindowID winid,
34 const wxString& label,
38 const wxValidator& validator,
41 wxAutoNSAutoreleasePool pool;
43 if(!CreateControl(parent,winid,pos,size,style,validator,name))
48 m_radioSlaves.Append(this);
50 else if(style&wxRB_SINGLE)
54 for(wxWindowList::compatibility_iterator siblingNode= GetParent()->GetChildren().GetLast();
56 siblingNode = siblingNode->GetPrevious())
58 wxRadioButton *radioButton = wxDynamicCast(siblingNode->GetData(),wxRadioButton);
59 if(radioButton && radioButton!=this)
61 m_radioMaster = radioButton->m_radioMaster;
62 wxASSERT_MSG(m_radioMaster,
63 wxT("Previous radio button should be part of a group"));
64 // Don't crash, assume user meant wxRB_SINGLE
66 m_radioMaster->m_radioSlaves.Append(this);
73 SetNSButton([[NSButton alloc] initWithFrame: MakeDefaultNSRect(size)]);
74 [m_cocoaNSView release];
75 [GetNSButton() setButtonType: NSRadioButton];
76 [GetNSButton() setTitle:wxNSStringWithWxString(label)];
77 // If it's the first in a group, it should be selected
79 [GetNSButton() setState: NSOnState];
80 [GetNSControl() sizeToFit];
83 m_parent->CocoaAddChild(this);
84 SetInitialFrameRect(pos,size);
89 wxRadioButton::~wxRadioButton()
91 if(m_radioMaster==this)
93 // First get rid of ourselves (we should ALWAYS be at the head)
94 wxRadioButtonList::compatibility_iterator slaveNode =
95 m_radioSlaves.GetFirst();
97 wxASSERT(slaveNode->GetData() == this);
98 m_radioSlaves.Erase(slaveNode);
100 // Now find the new master
101 wxRadioButton *newMaster = NULL;
102 slaveNode = m_radioSlaves.GetFirst();
104 newMaster = slaveNode->GetData();
106 // For each node (including the new master) set the master, remove
107 // it from this list, and add it to the new master's list
108 for(; slaveNode; slaveNode = m_radioSlaves.GetFirst())
110 wxRadioButton *radioButton = slaveNode->GetData();
111 wxASSERT(radioButton->m_radioMaster == this);
112 radioButton->m_radioMaster = newMaster;
113 newMaster->m_radioSlaves.Append(radioButton);
114 m_radioSlaves.Erase(slaveNode);
117 else if(m_radioMaster)
119 m_radioMaster->m_radioSlaves.DeleteObject(this);
120 m_radioMaster = NULL;
123 DisassociateNSButton(GetNSButton());
126 void wxRadioButton::SetValue(bool value)
129 [GetNSButton() setState: NSOnState];
131 [GetNSButton() setState: NSOffState];
134 bool wxRadioButton::GetValue() const
136 int state = [GetNSButton() state];
137 wxASSERT(state!=NSMixedState);
138 return state==NSOnState;
141 void wxRadioButton::Cocoa_wxNSButtonAction(void)
143 wxLogTrace(wxTRACE_COCOA,wxT("wxRadioButton"));
144 if(m_radioMaster && ([GetNSButton() state] == NSOnState))
146 for(wxRadioButtonList::compatibility_iterator slaveNode =
147 m_radioMaster->m_radioSlaves.GetFirst();
148 slaveNode; slaveNode = slaveNode->GetNext())
150 wxRadioButton *radioButton = slaveNode->GetData();
151 if(radioButton!=this)
152 radioButton->SetValue(false);
155 wxCommandEvent event(wxEVT_COMMAND_RADIOBUTTON_SELECTED, GetId());
156 InitCommandEvent(event); // event.SetEventObject(this);
157 event.SetInt(GetValue());