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