1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/views/controls/button/radio_button.h"
7 #include "base/logging.h"
8 #include "grit/ui_resources.h"
9 #include "ui/accessibility/ax_view_state.h"
10 #include "ui/base/resource/resource_bundle.h"
11 #include "ui/views/widget/widget.h"
16 const char RadioButton::kViewClassName
[] = "RadioButton";
18 RadioButton::RadioButton(const base::string16
& label
, int group_id
)
22 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
24 // Unchecked/Unfocused images.
25 SetCustomImage(false, false, STATE_NORMAL
,
26 *rb
.GetImageSkiaNamed(IDR_RADIO
));
27 SetCustomImage(false, false, STATE_HOVERED
,
28 *rb
.GetImageSkiaNamed(IDR_RADIO_HOVER
));
29 SetCustomImage(false, false, STATE_PRESSED
,
30 *rb
.GetImageSkiaNamed(IDR_RADIO_PRESSED
));
31 SetCustomImage(false, false, STATE_DISABLED
,
32 *rb
.GetImageSkiaNamed(IDR_RADIO_DISABLED
));
34 // Checked/Unfocused images.
35 SetCustomImage(true, false, STATE_NORMAL
,
36 *rb
.GetImageSkiaNamed(IDR_RADIO_CHECKED
));
37 SetCustomImage(true, false, STATE_HOVERED
,
38 *rb
.GetImageSkiaNamed(IDR_RADIO_CHECKED_HOVER
));
39 SetCustomImage(true, false, STATE_PRESSED
,
40 *rb
.GetImageSkiaNamed(IDR_RADIO_CHECKED_PRESSED
));
41 SetCustomImage(true, false, STATE_DISABLED
,
42 *rb
.GetImageSkiaNamed(IDR_RADIO_CHECKED_DISABLED
));
44 // Unchecked/Focused images.
45 SetCustomImage(false, true, STATE_NORMAL
,
46 *rb
.GetImageSkiaNamed(IDR_RADIO_FOCUSED
));
47 SetCustomImage(false, true, STATE_HOVERED
,
48 *rb
.GetImageSkiaNamed(IDR_RADIO_FOCUSED_HOVER
));
49 SetCustomImage(false, true, STATE_PRESSED
,
50 *rb
.GetImageSkiaNamed(IDR_RADIO_FOCUSED_PRESSED
));
52 // Checked/Focused images.
53 SetCustomImage(true, true, STATE_NORMAL
,
54 *rb
.GetImageSkiaNamed(IDR_RADIO_FOCUSED_CHECKED
));
55 SetCustomImage(true, true, STATE_HOVERED
,
56 *rb
.GetImageSkiaNamed(IDR_RADIO_FOCUSED_CHECKED_HOVER
));
57 SetCustomImage(true, true, STATE_PRESSED
,
58 *rb
.GetImageSkiaNamed(IDR_RADIO_FOCUSED_CHECKED_PRESSED
));
61 RadioButton::~RadioButton() {
64 void RadioButton::SetChecked(bool checked
) {
65 if (checked
== RadioButton::checked())
68 // We can't just get the root view here because sometimes the radio
69 // button isn't attached to a root view (e.g., if it's part of a tab page
70 // that is currently not active).
71 View
* container
= parent();
72 while (container
&& container
->parent())
73 container
= container
->parent();
76 container
->GetViewsInGroup(GetGroup(), &other
);
77 for (Views::iterator
i(other
.begin()); i
!= other
.end(); ++i
) {
79 if (strcmp((*i
)->GetClassName(), kViewClassName
)) {
80 NOTREACHED() << "radio-button-nt has same group as other non "
81 "radio-button-nt views.";
84 RadioButton
* peer
= static_cast<RadioButton
*>(*i
);
85 peer
->SetChecked(false);
90 Checkbox::SetChecked(checked
);
93 const char* RadioButton::GetClassName() const {
94 return kViewClassName
;
97 void RadioButton::GetAccessibleState(ui::AXViewState
* state
) {
98 Checkbox::GetAccessibleState(state
);
99 state
->role
= ui::AX_ROLE_RADIO_BUTTON
;
102 View
* RadioButton::GetSelectedViewForGroup(int group
) {
104 GetWidget()->GetRootView()->GetViewsInGroup(group
, &views
);
108 for (Views::const_iterator
i(views
.begin()); i
!= views
.end(); ++i
) {
109 // REVIEW: why don't we check the runtime type like is done above?
110 RadioButton
* radio_button
= static_cast<RadioButton
*>(*i
);
111 if (radio_button
->checked())
117 bool RadioButton::IsGroupFocusTraversable() const {
118 // When focusing a radio button with tab/shift+tab, only the selected button
119 // from the group should be focused.
123 void RadioButton::OnFocus() {
126 ui::MouseEvent
event(ui::ET_MOUSE_PRESSED
, gfx::Point(), gfx::Point(), 0, 0);
127 LabelButton::NotifyClick(event
);
130 void RadioButton::NotifyClick(const ui::Event
& event
) {
131 // Set the checked state to true only if we are unchecked, since we can't
132 // be toggled on and off like a checkbox.
136 LabelButton::NotifyClick(event
);
139 ui::NativeTheme::Part
RadioButton::GetThemePart() const {
140 return ui::NativeTheme::kRadio
;