OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/views/controls/button/checkbox.h" | 5 #include "ui/views/controls/button/checkbox.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "grit/ui_resources.h" |
8 #include "ui/base/accessibility/accessible_view_state.h" | 8 #include "ui/base/accessibility/accessible_view_state.h" |
9 #include "ui/gfx/canvas.h" | 9 #include "ui/base/resource/resource_bundle.h" |
10 #include "ui/views/controls/label.h" | 10 #include "ui/views/controls/button/label_button_border.h" |
11 | 11 |
12 namespace views { | 12 namespace views { |
13 | 13 |
14 namespace { | |
15 | |
16 const int kCheckboxLabelSpacing = 4; | |
17 | |
18 const int kFocusBorderWidth = 1; | |
19 | |
20 } // namespace | |
21 | |
22 // static | 14 // static |
23 const char Checkbox::kViewClassName[] = "views/Checkbox"; | 15 const char Checkbox::kViewClassName[] = "views/Checkbox"; |
24 | 16 |
25 //////////////////////////////////////////////////////////////////////////////// | 17 Checkbox::Checkbox(const string16& label) |
26 // CheckboxNativeThemeBorder, public: | 18 : LabelButton(NULL, label), |
| 19 checked_(false) { |
| 20 SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 21 LabelButtonBorder* button_border = static_cast<LabelButtonBorder*>(border()); |
| 22 button_border->SetPainter(false, STATE_HOVERED, NULL); |
| 23 button_border->SetPainter(false, STATE_PRESSED, NULL); |
| 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 set_focusable(true); |
27 | 27 |
28 gfx::Insets CheckboxNativeThemeBorder::GetInsets() const { | 28 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
29 if (use_custom_insets_) | |
30 return custom_insets_; | |
31 | 29 |
32 const gfx::Insets& insets = TextButtonNativeThemeBorder::GetInsets(); | 30 // Unchecked/Unfocused images. |
33 return gfx::Insets(insets.top(), 0, insets.bottom(), 0); | 31 SetCustomImage(false, false, STATE_NORMAL, |
34 } | 32 *rb.GetImageSkiaNamed(IDR_CHECKBOX)); |
| 33 SetCustomImage(false, false, STATE_HOVERED, |
| 34 *rb.GetImageSkiaNamed(IDR_CHECKBOX_HOVER)); |
| 35 SetCustomImage(false, false, STATE_PRESSED, |
| 36 *rb.GetImageSkiaNamed(IDR_CHECKBOX_PRESSED)); |
| 37 SetCustomImage(false, false, STATE_DISABLED, |
| 38 *rb.GetImageSkiaNamed(IDR_CHECKBOX_DISABLED)); |
35 | 39 |
36 void CheckboxNativeThemeBorder::SetCustomInsets( | 40 // Checked/Unfocused images. |
37 const gfx::Insets& custom_insets) { | 41 SetCustomImage(true, false, STATE_NORMAL, |
38 custom_insets_ = custom_insets; | 42 *rb.GetImageSkiaNamed(IDR_CHECKBOX_CHECKED)); |
39 use_custom_insets_ = true; | 43 SetCustomImage(true, false, STATE_HOVERED, |
40 } | 44 *rb.GetImageSkiaNamed(IDR_CHECKBOX_CHECKED_HOVER)); |
| 45 SetCustomImage(true, false, STATE_PRESSED, |
| 46 *rb.GetImageSkiaNamed(IDR_CHECKBOX_CHECKED_PRESSED)); |
| 47 SetCustomImage(true, false, STATE_DISABLED, |
| 48 *rb.GetImageSkiaNamed(IDR_CHECKBOX_CHECKED_DISABLED)); |
41 | 49 |
42 void CheckboxNativeThemeBorder::UseDefaultInsets() { | 50 // Unchecked/Focused images. |
43 use_custom_insets_ = false; | 51 SetCustomImage(false, true, STATE_NORMAL, |
44 } | 52 *rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED)); |
| 53 SetCustomImage(false, true, STATE_HOVERED, |
| 54 *rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED_HOVER)); |
| 55 SetCustomImage(false, true, STATE_PRESSED, |
| 56 *rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED_PRESSED)); |
45 | 57 |
46 //////////////////////////////////////////////////////////////////////////////// | 58 // Checked/Focused images. |
47 // Checkbox, public: | 59 SetCustomImage(true, true, STATE_NORMAL, |
| 60 *rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED_CHECKED)); |
| 61 SetCustomImage(true, true, STATE_HOVERED, |
| 62 *rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED_CHECKED_HOVER)); |
| 63 SetCustomImage(true, true, STATE_PRESSED, |
| 64 *rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED_CHECKED_PRESSED)); |
48 | 65 |
49 Checkbox::Checkbox(const string16& label) | 66 // Limit the checkbox height to match the legacy appearance. |
50 : TextButtonBase(NULL, label), | 67 const gfx::Size preferred_size(LabelButton::GetPreferredSize()); |
51 checked_(false) { | 68 set_min_size(gfx::Size(0, preferred_size.height() + 4)); |
52 set_border(new CheckboxNativeThemeBorder(this)); | |
53 set_focusable(true); | |
54 } | 69 } |
55 | 70 |
56 Checkbox::~Checkbox() { | 71 Checkbox::~Checkbox() { |
57 } | 72 } |
58 | 73 |
59 void Checkbox::SetChecked(bool checked) { | 74 void Checkbox::SetChecked(bool checked) { |
60 checked_ = checked; | 75 checked_ = checked; |
61 SchedulePaint(); | 76 UpdateImage(); |
62 } | 77 } |
63 | 78 |
64 gfx::Size Checkbox::GetPreferredSize() { | 79 void Checkbox::Layout() { |
65 gfx::Size prefsize(TextButtonBase::GetPreferredSize()); | 80 LabelButton::Layout(); |
66 ui::NativeTheme::ExtraParams extra; | |
67 ui::NativeTheme::State state = GetThemeState(&extra); | |
68 gfx::Size size = GetNativeTheme()->GetPartSize(GetThemePart(), state, extra); | |
69 prefsize.Enlarge(size.width() + kCheckboxLabelSpacing + kFocusBorderWidth, 0); | |
70 prefsize.set_height(std::max(prefsize.height(), size.height())); | |
71 | 81 |
72 if (max_width_ > 0) | 82 // Construct a focus border that only surrounds the label area. |
73 prefsize.set_width(std::min(max_width_, prefsize.width())); | 83 gfx::Rect rect = label()->GetMirroredBounds(); |
74 | 84 rect.Inset(-2, 3); |
75 return prefsize; | 85 set_focus_border(FocusBorder::CreateDashedFocusBorder( |
| 86 rect.x(), rect.y(), width() - rect.right(), height() - rect.bottom())); |
76 } | 87 } |
77 | 88 |
78 const char* Checkbox::GetClassName() const { | 89 const char* Checkbox::GetClassName() const { |
79 return kViewClassName; | 90 return kViewClassName; |
80 } | 91 } |
81 | 92 |
82 void Checkbox::GetAccessibleState(ui::AccessibleViewState* state) { | 93 void Checkbox::GetAccessibleState(ui::AccessibleViewState* state) { |
83 TextButtonBase::GetAccessibleState(state); | 94 LabelButton::GetAccessibleState(state); |
84 state->role = ui::AccessibilityTypes::ROLE_CHECKBUTTON; | 95 state->role = ui::AccessibilityTypes::ROLE_CHECKBUTTON; |
85 state->state = checked() ? ui::AccessibilityTypes::STATE_CHECKED : 0; | 96 state->state = checked() ? ui::AccessibilityTypes::STATE_CHECKED : 0; |
86 } | 97 } |
87 | 98 |
88 void Checkbox::OnPaintFocusBorder(gfx::Canvas* canvas) { | 99 void Checkbox::OnFocus() { |
89 if (HasFocus() && (focusable() || IsAccessibilityFocusable())) { | 100 UpdateImage(); |
90 gfx::Rect bounds(GetTextBounds()); | 101 } |
91 // Increate the bounding box by one on each side so that that focus border | 102 |
92 // does not draw on top of the letters. | 103 void Checkbox::OnBlur() { |
93 bounds.Inset(-kFocusBorderWidth, | 104 UpdateImage(); |
94 -kFocusBorderWidth, | 105 } |
95 -kFocusBorderWidth, | 106 |
96 -kFocusBorderWidth); | 107 const gfx::ImageSkia& Checkbox::GetImage(ButtonState for_state) { |
97 canvas->DrawFocusRect(bounds); | 108 const size_t checked_index = checked_ ? 1 : 0; |
98 } | 109 const size_t focused_index = HasFocus() ? 1 : 0; |
| 110 if (for_state != STATE_NORMAL && |
| 111 images_[checked_index][focused_index][for_state].isNull()) |
| 112 return images_[checked_index][focused_index][STATE_NORMAL]; |
| 113 return images_[checked_index][focused_index][for_state]; |
| 114 } |
| 115 |
| 116 void Checkbox::SetCustomImage(bool checked, |
| 117 bool focused, |
| 118 ButtonState for_state, |
| 119 const gfx::ImageSkia& image) { |
| 120 const size_t checked_index = checked ? 1 : 0; |
| 121 const size_t focused_index = focused ? 1 : 0; |
| 122 images_[checked_index][focused_index][for_state] = image; |
| 123 UpdateImage(); |
99 } | 124 } |
100 | 125 |
101 void Checkbox::NotifyClick(const ui::Event& event) { | 126 void Checkbox::NotifyClick(const ui::Event& event) { |
102 SetChecked(!checked()); | 127 SetChecked(!checked()); |
103 RequestFocus(); | 128 LabelButton::NotifyClick(event); |
104 TextButtonBase::NotifyClick(event); | |
105 } | 129 } |
106 | 130 |
107 ui::NativeTheme::Part Checkbox::GetThemePart() const { | 131 ui::NativeTheme::Part Checkbox::GetThemePart() const { |
108 return ui::NativeTheme::kCheckbox; | 132 return ui::NativeTheme::kCheckbox; |
109 } | 133 } |
110 | 134 |
111 gfx::Rect Checkbox::GetThemePaintRect() const { | |
112 ui::NativeTheme::ExtraParams extra; | |
113 ui::NativeTheme::State state = GetThemeState(&extra); | |
114 gfx::Size size(GetNativeTheme()->GetPartSize(GetThemePart(), state, extra)); | |
115 gfx::Insets insets = GetInsets(); | |
116 int y_offset = (height() - size.height()) / 2; | |
117 gfx::Rect rect(insets.left(), y_offset, size.width(), size.height()); | |
118 rect.set_x(GetMirroredXForRect(rect)); | |
119 return rect; | |
120 } | |
121 | |
122 void Checkbox::GetExtraParams(ui::NativeTheme::ExtraParams* params) const { | 135 void Checkbox::GetExtraParams(ui::NativeTheme::ExtraParams* params) const { |
123 TextButtonBase::GetExtraParams(params); | 136 LabelButton::GetExtraParams(params); |
124 params->button.checked = checked_; | 137 params->button.checked = checked_; |
125 } | 138 } |
126 | 139 |
127 gfx::Rect Checkbox::GetTextBounds() const { | |
128 gfx::Rect bounds(TextButtonBase::GetTextBounds()); | |
129 ui::NativeTheme::ExtraParams extra; | |
130 ui::NativeTheme::State state = GetThemeState(&extra); | |
131 gfx::Size size(GetNativeTheme()->GetPartSize(GetThemePart(), state, extra)); | |
132 bounds.Offset(size.width() + kCheckboxLabelSpacing, 0); | |
133 return bounds; | |
134 } | |
135 | |
136 } // namespace views | 140 } // namespace views |
OLD | NEW |