]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/radiobut.mm
Workaround for #15404: wxRichTextCtrl: caret does not disappear when focus is lost...
[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
da0634c1 7// Copyright: (c) 2003 David Elliott
526954c5 8// Licence: wxWindows licence
da0634c1
DE
9/////////////////////////////////////////////////////////////////////////////
10
449c5673 11#include "wx/wxprec.h"
16c81607
RN
12
13#if wxUSE_RADIOBTN
14
b0b5881a
WS
15#include "wx/radiobut.h"
16
449c5673
DE
17#ifndef WX_PRECOMP
18 #include "wx/log.h"
19 #include "wx/app.h"
449c5673 20#endif //WX_PRECOMP
da0634c1
DE
21
22#import <AppKit/NSButton.h>
c8cae94c 23#include "wx/cocoa/string.h"
e51c36de 24#include "wx/cocoa/autorelease.h"
da0634c1 25
d0a5e34a
DE
26#include "wx/listimpl.cpp"
27
28WX_DEFINE_LIST(wxRadioButtonList);
29
da0634c1
DE
30// wxRadioButtonBase == wxControl
31BEGIN_EVENT_TABLE(wxRadioButton, wxControl)
32END_EVENT_TABLE()
33WX_IMPLEMENT_COCOA_OWNER(wxRadioButton,NSButton,NSControl,NSView)
34
35bool 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{
e51c36de 43 wxAutoNSAutoreleasePool pool;
d0a5e34a 44 m_radioMaster = NULL;
da0634c1
DE
45 if(!CreateControl(parent,winid,pos,size,style,validator,name))
46 return false;
d0a5e34a
DE
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,
2b030203 65 wxT("Previous radio button should be part of a group"));
d0a5e34a
DE
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
da0634c1 74 m_cocoaNSView = NULL;
8d656ea9 75 SetNSButton([[NSButton alloc] initWithFrame: MakeDefaultNSRect(size)]);
da0634c1
DE
76 [m_cocoaNSView release];
77 [GetNSButton() setButtonType: NSRadioButton];
525007cf 78 CocoaSetLabelForObject(label, GetNSButton());
bd8e6e37
DE
79 // If it's the first in a group, it should be selected
80 if(style&wxRB_GROUP)
81 [GetNSButton() setState: NSOnState];
da0634c1
DE
82 [GetNSControl() sizeToFit];
83
84 if(m_parent)
85 m_parent->CocoaAddChild(this);
8d656ea9
DE
86 SetInitialFrameRect(pos,size);
87
da0634c1
DE
88 return true;
89}
90
91wxRadioButton::~wxRadioButton()
92{
d0a5e34a
DE
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);
96c0183f 100 m_radioSlaves.Erase(slaveNode);
b0b5881a 101
d0a5e34a
DE
102 // Now find the new master
103 wxRadioButton *newMaster = NULL;
104 slaveNode = m_radioSlaves.GetFirst();
105 if(slaveNode)
106 newMaster = slaveNode->GetData();
b0b5881a 107
d0a5e34a
DE
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);
96c0183f 116 m_radioSlaves.Erase(slaveNode);
d0a5e34a
DE
117 }
118 }
119 else if(m_radioMaster)
120 {
121 m_radioMaster->m_radioSlaves.DeleteObject(this);
122 m_radioMaster = NULL;
123 }
124 // normal stuff
911e17c6 125 DisassociateNSButton(GetNSButton());
da0634c1
DE
126}
127
d0a5e34a 128void wxRadioButton::SetValue(bool value)
da0634c1 129{
d0a5e34a 130 if(value)
18359b19 131 {
d0a5e34a 132 [GetNSButton() setState: NSOnState];
18359b19
DE
133 Cocoa_DeselectOtherButtonsInTheGroup();
134 }
d0a5e34a
DE
135 else
136 [GetNSButton() setState: NSOffState];
da0634c1
DE
137}
138
139bool wxRadioButton::GetValue() const
140{
d0a5e34a
DE
141 int state = [GetNSButton() state];
142 wxASSERT(state!=NSMixedState);
143 return state==NSOnState;
da0634c1
DE
144}
145
3c0f8ed2
DE
146void wxRadioButton::SetLabel(const wxString& label)
147{
148 wxAutoNSAutoreleasePool pool;
149 CocoaSetLabelForObject(label, GetNSButton());
150}
151
152wxString wxRadioButton::GetLabel() const
153{
154 return wxStringWithNSString([GetNSButton() title]);
155}
156
18359b19
DE
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 */
162void wxRadioButton::Cocoa_DeselectOtherButtonsInTheGroup(void)
da0634c1 163{
18359b19 164 if(m_radioMaster)
d0a5e34a
DE
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 }
18359b19
DE
175}
176
177void wxRadioButton::Cocoa_wxNSButtonAction(void)
178{
179 wxLogTrace(wxTRACE_COCOA,wxT("wxRadioButton"));
180 if([GetNSButton() state] == NSOnState)
181 {
182 Cocoa_DeselectOtherButtonsInTheGroup();
183 }
ce7fe42e 184 wxCommandEvent event(wxEVT_RADIOBUTTON, GetId());
d0a5e34a
DE
185 InitCommandEvent(event); // event.SetEventObject(this);
186 event.SetInt(GetValue());
187 Command(event);
da0634c1
DE
188}
189
16c81607 190#endif