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/checkbox.h"
7 #include "ui/accessibility/ax_view_state.h"
8 #include "ui/base/resource/resource_bundle.h"
9 #include "ui/resources/grit/ui_resources.h"
10 #include "ui/views/controls/button/label_button_border.h"
11 #include "ui/views/painter.h"
12 #include "ui/views/resources/grit/views_resources.h"
17 const char Checkbox::kViewClassName
[] = "Checkbox";
19 Checkbox::Checkbox(const base::string16
& label
)
20 : LabelButton(NULL
, label
),
22 SetHorizontalAlignment(gfx::ALIGN_LEFT
);
23 scoped_ptr
<LabelButtonBorder
> button_border(new LabelButtonBorder());
24 // Inset the trailing side by a couple pixels for the focus border.
25 button_border
->set_insets(gfx::Insets(0, 0, 0, 2));
26 SetBorder(button_border
.Pass());
29 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
31 // Unchecked/Unfocused images.
32 SetCustomImage(false, false, STATE_NORMAL
,
33 *rb
.GetImageSkiaNamed(IDR_CHECKBOX
));
34 SetCustomImage(false, false, STATE_HOVERED
,
35 *rb
.GetImageSkiaNamed(IDR_CHECKBOX_HOVER
));
36 SetCustomImage(false, false, STATE_PRESSED
,
37 *rb
.GetImageSkiaNamed(IDR_CHECKBOX_PRESSED
));
38 SetCustomImage(false, false, STATE_DISABLED
,
39 *rb
.GetImageSkiaNamed(IDR_CHECKBOX_DISABLED
));
41 // Checked/Unfocused images.
42 SetCustomImage(true, false, STATE_NORMAL
,
43 *rb
.GetImageSkiaNamed(IDR_CHECKBOX_CHECKED
));
44 SetCustomImage(true, false, STATE_HOVERED
,
45 *rb
.GetImageSkiaNamed(IDR_CHECKBOX_CHECKED_HOVER
));
46 SetCustomImage(true, false, STATE_PRESSED
,
47 *rb
.GetImageSkiaNamed(IDR_CHECKBOX_CHECKED_PRESSED
));
48 SetCustomImage(true, false, STATE_DISABLED
,
49 *rb
.GetImageSkiaNamed(IDR_CHECKBOX_CHECKED_DISABLED
));
51 // Unchecked/Focused images.
52 SetCustomImage(false, true, STATE_NORMAL
,
53 *rb
.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED
));
54 SetCustomImage(false, true, STATE_HOVERED
,
55 *rb
.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED_HOVER
));
56 SetCustomImage(false, true, STATE_PRESSED
,
57 *rb
.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED_PRESSED
));
59 // Checked/Focused images.
60 SetCustomImage(true, true, STATE_NORMAL
,
61 *rb
.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED_CHECKED
));
62 SetCustomImage(true, true, STATE_HOVERED
,
63 *rb
.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED_CHECKED_HOVER
));
64 SetCustomImage(true, true, STATE_PRESSED
,
65 *rb
.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED_CHECKED_PRESSED
));
67 // Limit the checkbox height to match the legacy appearance.
68 const gfx::Size
preferred_size(LabelButton::GetPreferredSize());
69 SetMinSize(gfx::Size(0, preferred_size
.height() + 4));
72 Checkbox::~Checkbox() {
75 void Checkbox::SetChecked(bool checked
) {
80 void Checkbox::Layout() {
81 LabelButton::Layout();
83 // Construct a focus painter that only surrounds the label area.
84 gfx::Rect rect
= label()->GetMirroredBounds();
86 SetFocusPainter(Painter::CreateDashedFocusPainterWithInsets(
87 gfx::Insets(rect
.y(), rect
.x(),
88 height() - rect
.bottom(),
89 width() - rect
.right())));
92 const char* Checkbox::GetClassName() const {
93 return kViewClassName
;
96 void Checkbox::GetAccessibleState(ui::AXViewState
* state
) {
97 LabelButton::GetAccessibleState(state
);
98 state
->role
= ui::AX_ROLE_CHECK_BOX
;
100 state
->AddStateFlag(ui::AX_STATE_CHECKED
);
103 void Checkbox::OnFocus() {
104 LabelButton::OnFocus();
108 void Checkbox::OnBlur() {
109 LabelButton::OnBlur();
113 const gfx::ImageSkia
& Checkbox::GetImage(ButtonState for_state
) {
114 const size_t checked_index
= checked_
? 1 : 0;
115 const size_t focused_index
= HasFocus() ? 1 : 0;
116 if (for_state
!= STATE_NORMAL
&&
117 images_
[checked_index
][focused_index
][for_state
].isNull())
118 return images_
[checked_index
][focused_index
][STATE_NORMAL
];
119 return images_
[checked_index
][focused_index
][for_state
];
122 void Checkbox::SetCustomImage(bool checked
,
124 ButtonState for_state
,
125 const gfx::ImageSkia
& image
) {
126 const size_t checked_index
= checked
? 1 : 0;
127 const size_t focused_index
= focused
? 1 : 0;
128 images_
[checked_index
][focused_index
][for_state
] = image
;
132 void Checkbox::NotifyClick(const ui::Event
& event
) {
133 SetChecked(!checked());
134 LabelButton::NotifyClick(event
);
137 ui::NativeTheme::Part
Checkbox::GetThemePart() const {
138 return ui::NativeTheme::kCheckbox
;
141 void Checkbox::GetExtraParams(ui::NativeTheme::ExtraParams
* params
) const {
142 LabelButton::GetExtraParams(params
);
143 params
->button
.checked
= checked_
;