]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/radiobut.mm
Increase the number of visible OS X combo items.
[wxWidgets.git] / src / cocoa / radiobut.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/radiobut.mm
3 // Purpose: wxRadioButton
4 // Author: David Elliott
5 // Modified by:
6 // Created: 2003/03/16
7 // Copyright: (c) 2003 David Elliott
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #include "wx/wxprec.h"
12
13 #if wxUSE_RADIOBTN
14
15 #include "wx/radiobut.h"
16
17 #ifndef WX_PRECOMP
18 #include "wx/log.h"
19 #include "wx/app.h"
20 #endif //WX_PRECOMP
21
22 #import <AppKit/NSButton.h>
23 #include "wx/cocoa/string.h"
24 #include "wx/cocoa/autorelease.h"
25
26 #include "wx/listimpl.cpp"
27
28 WX_DEFINE_LIST(wxRadioButtonList);
29
30 // wxRadioButtonBase == wxControl
31 BEGIN_EVENT_TABLE(wxRadioButton, wxControl)
32 END_EVENT_TABLE()
33 WX_IMPLEMENT_COCOA_OWNER(wxRadioButton,NSButton,NSControl,NSView)
34
35 bool wxRadioButton::Create(wxWindow *parent, wxWindowID winid,
36 const wxString& label,
37 const wxPoint& pos,
38 const wxSize& size,
39 long style,
40 const wxValidator& validator,
41 const wxString& name)
42 {
43 wxAutoNSAutoreleasePool pool;
44 m_radioMaster = NULL;
45 if(!CreateControl(parent,winid,pos,size,style,validator,name))
46 return false;
47 if(style&wxRB_GROUP)
48 {
49 m_radioMaster = this;
50 m_radioSlaves.Append(this);
51 }
52 else if(style&wxRB_SINGLE)
53 m_radioMaster = NULL;
54 else
55 {
56 for(wxWindowList::compatibility_iterator siblingNode= GetParent()->GetChildren().GetLast();
57 siblingNode;
58 siblingNode = siblingNode->GetPrevious())
59 {
60 wxRadioButton *radioButton = wxDynamicCast(siblingNode->GetData(),wxRadioButton);
61 if(radioButton && radioButton!=this)
62 {
63 m_radioMaster = radioButton->m_radioMaster;
64 wxASSERT_MSG(m_radioMaster,
65 wxT("Previous radio button should be part of a group"));
66 // Don't crash, assume user meant wxRB_SINGLE
67 if(m_radioMaster)
68 m_radioMaster->m_radioSlaves.Append(this);
69 break;
70 }
71 }
72 }
73
74 m_cocoaNSView = NULL;
75 SetNSButton([[NSButton alloc] initWithFrame: MakeDefaultNSRect(size)]);
76 [m_cocoaNSView release];
77 [GetNSButton() setButtonType: NSRadioButton];
78 CocoaSetLabelForObject(label, GetNSButton());
79 // If it's the first in a group, it should be selected
80 if(style&wxRB_GROUP)
81 [GetNSButton() setState: NSOnState];
82 [GetNSControl() sizeToFit];
83
84 if(m_parent)
85 m_parent->CocoaAddChild(this);
86 SetInitialFrameRect(pos,size);
87
88 return true;
89 }
90
91 wxRadioButton::~wxRadioButton()
92 {
93 if(m_radioMaster==this)
94 {
95 // First get rid of ourselves (we should ALWAYS be at the head)
96 wxRadioButtonList::compatibility_iterator slaveNode =
97 m_radioSlaves.GetFirst();
98 wxASSERT(slaveNode);
99 wxASSERT(slaveNode->GetData() == this);
100 m_radioSlaves.Erase(slaveNode);
101
102 // Now find the new master
103 wxRadioButton *newMaster = NULL;
104 slaveNode = m_radioSlaves.GetFirst();
105 if(slaveNode)
106 newMaster = slaveNode->GetData();
107
108 // For each node (including the new master) set the master, remove
109 // it from this list, and add it to the new master's list
110 for(; slaveNode; slaveNode = m_radioSlaves.GetFirst())
111 {
112 wxRadioButton *radioButton = slaveNode->GetData();
113 wxASSERT(radioButton->m_radioMaster == this);
114 radioButton->m_radioMaster = newMaster;
115 newMaster->m_radioSlaves.Append(radioButton);
116 m_radioSlaves.Erase(slaveNode);
117 }
118 }
119 else if(m_radioMaster)
120 {
121 m_radioMaster->m_radioSlaves.DeleteObject(this);
122 m_radioMaster = NULL;
123 }
124 // normal stuff
125 DisassociateNSButton(GetNSButton());
126 }
127
128 void wxRadioButton::SetValue(bool value)
129 {
130 if(value)
131 {
132 [GetNSButton() setState: NSOnState];
133 Cocoa_DeselectOtherButtonsInTheGroup();
134 }
135 else
136 [GetNSButton() setState: NSOffState];
137 }
138
139 bool wxRadioButton::GetValue() const
140 {
141 int state = [GetNSButton() state];
142 wxASSERT(state!=NSMixedState);
143 return state==NSOnState;
144 }
145
146 void wxRadioButton::SetLabel(const wxString& label)
147 {
148 wxAutoNSAutoreleasePool pool;
149 CocoaSetLabelForObject(label, GetNSButton());
150 }
151
152 wxString wxRadioButton::GetLabel() const
153 {
154 return wxStringWithNSString([GetNSButton() title]);
155 }
156
157 /**
158 * If this radio button is part of a group, this method turns off every other
159 * button in the group. If this radio button is not part of a group, this
160 * method does absolutely nothing.
161 */
162 void wxRadioButton::Cocoa_DeselectOtherButtonsInTheGroup(void)
163 {
164 if(m_radioMaster)
165 {
166 for(wxRadioButtonList::compatibility_iterator slaveNode =
167 m_radioMaster->m_radioSlaves.GetFirst();
168 slaveNode; slaveNode = slaveNode->GetNext())
169 {
170 wxRadioButton *radioButton = slaveNode->GetData();
171 if(radioButton!=this)
172 radioButton->SetValue(false);
173 }
174 }
175 }
176
177 void wxRadioButton::Cocoa_wxNSButtonAction(void)
178 {
179 wxLogTrace(wxTRACE_COCOA,wxT("wxRadioButton"));
180 if([GetNSButton() state] == NSOnState)
181 {
182 Cocoa_DeselectOtherButtonsInTheGroup();
183 }
184 wxCommandEvent event(wxEVT_RADIOBUTTON, GetId());
185 InitCommandEvent(event); // event.SetEventObject(this);
186 event.SetInt(GetValue());
187 Command(event);
188 }
189
190 #endif