The default state for the first radio button in a group should be selected
[wxWidgets.git] / src / cocoa / radiobut.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        cocoa/radiobut.mm
3 // Purpose:     wxRadioButton
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/app.h"
13 #include "wx/radiobut.h"
14 #include "wx/log.h"
15
16 #import <AppKit/NSButton.h>
17 #include "wx/cocoa/string.h"
18 #include "wx/cocoa/autorelease.h"
19
20 #include "wx/listimpl.cpp"
21
22 WX_DEFINE_LIST(wxRadioButtonList);
23
24 IMPLEMENT_DYNAMIC_CLASS(wxRadioButton, wxControl)
25 // wxRadioButtonBase == wxControl
26 BEGIN_EVENT_TABLE(wxRadioButton, wxControl)
27 END_EVENT_TABLE()
28 WX_IMPLEMENT_COCOA_OWNER(wxRadioButton,NSButton,NSControl,NSView)
29
30 bool wxRadioButton::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     m_radioMaster = NULL;
40     if(!CreateControl(parent,winid,pos,size,style,validator,name))
41         return false;
42     if(style&wxRB_GROUP)
43     {
44         m_radioMaster = this;
45         m_radioSlaves.Append(this);
46     }
47     else if(style&wxRB_SINGLE)
48         m_radioMaster = NULL;
49     else
50     {
51         for(wxWindowList::compatibility_iterator siblingNode= GetParent()->GetChildren().GetLast();
52             siblingNode;
53             siblingNode = siblingNode->GetPrevious())
54         {
55             wxRadioButton *radioButton  = wxDynamicCast(siblingNode->GetData(),wxRadioButton);
56             if(radioButton && radioButton!=this)
57             {
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
62                 if(m_radioMaster)
63                     m_radioMaster->m_radioSlaves.Append(this);
64                 break;
65             }
66         }
67     }
68
69     m_cocoaNSView = NULL;
70     SetNSButton([[NSButton alloc] initWithFrame: MakeDefaultNSRect(size)]);
71     [m_cocoaNSView release];
72     [GetNSButton() setButtonType: NSRadioButton];
73     [GetNSButton() setTitle:wxNSStringWithWxString(label)];
74     // If it's the first in a group, it should be selected
75     if(style&wxRB_GROUP)
76         [GetNSButton() setState: NSOnState];
77     [GetNSControl() sizeToFit];
78
79     if(m_parent)
80         m_parent->CocoaAddChild(this);
81     SetInitialFrameRect(pos,size);
82
83     return true;
84 }
85
86 wxRadioButton::~wxRadioButton()
87 {
88     if(m_radioMaster==this)
89     {
90         // First get rid of ourselves (we should ALWAYS be at the head)
91         wxRadioButtonList::compatibility_iterator slaveNode =
92             m_radioSlaves.GetFirst();
93         wxASSERT(slaveNode);
94         wxASSERT(slaveNode->GetData() == this);
95         m_radioSlaves.DeleteNode(slaveNode);
96     
97         // Now find the new master
98         wxRadioButton *newMaster = NULL;
99         slaveNode = m_radioSlaves.GetFirst();
100         if(slaveNode)
101             newMaster = slaveNode->GetData();
102     
103         // For each node (including the new master) set the master, remove
104         // it from this list, and add it to the new master's list
105         for(; slaveNode; slaveNode = m_radioSlaves.GetFirst())
106         {
107             wxRadioButton *radioButton = slaveNode->GetData();
108             wxASSERT(radioButton->m_radioMaster == this);
109             radioButton->m_radioMaster = newMaster;
110             newMaster->m_radioSlaves.Append(radioButton);
111             m_radioSlaves.DeleteNode(slaveNode);
112         }
113     }
114     else if(m_radioMaster)
115     {
116         m_radioMaster->m_radioSlaves.DeleteObject(this);
117         m_radioMaster = NULL;
118     }
119     // normal stuff
120     DisassociateNSButton(m_cocoaNSView);
121 }
122
123 void wxRadioButton::SetValue(bool value)
124 {
125     if(value)
126         [GetNSButton() setState: NSOnState];
127     else
128         [GetNSButton() setState: NSOffState];
129 }
130
131 bool wxRadioButton::GetValue() const
132 {
133     int state = [GetNSButton() state];
134     wxASSERT(state!=NSMixedState);
135     return state==NSOnState;
136 }
137
138 void wxRadioButton::Cocoa_wxNSButtonAction(void)
139 {
140     wxLogDebug("wxRadioButton");
141     if(m_radioMaster && ([GetNSButton() state] == NSOnState))
142     {
143         for(wxRadioButtonList::compatibility_iterator slaveNode =
144                 m_radioMaster->m_radioSlaves.GetFirst();
145             slaveNode; slaveNode = slaveNode->GetNext())
146         {
147             wxRadioButton *radioButton = slaveNode->GetData();
148             if(radioButton!=this)
149                 radioButton->SetValue(false);
150         }
151     }
152     wxCommandEvent event(wxEVT_COMMAND_RADIOBUTTON_SELECTED, GetId());
153     InitCommandEvent(event); //    event.SetEventObject(this);
154     event.SetInt(GetValue());
155     Command(event);
156 }
157