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" | |
11 | 10 |
12 namespace views { | 11 namespace views { |
13 | 12 |
14 namespace { | |
15 | |
16 const int kCheckboxLabelSpacing = 4; | |
17 | |
18 const int kFocusBorderWidth = 1; | |
19 | |
20 } // namespace | |
21 | |
22 // static | 13 // static |
23 const char Checkbox::kViewClassName[] = "views/Checkbox"; | 14 const char Checkbox::kViewClassName[] = "views/Checkbox"; |
24 | 15 |
25 //////////////////////////////////////////////////////////////////////////////// | 16 Checkbox::Checkbox(const string16& label) |
26 // CheckboxNativeThemeBorder, public: | 17 : LabelButton(NULL, label), |
18 checked_(false) { | |
19 SetStyle(STYLE_CHECKBOX); | |
20 set_focusable(true); | |
27 | 21 |
28 gfx::Insets CheckboxNativeThemeBorder::GetInsets() const { | 22 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
29 if (use_custom_insets_) | 23 SetImage(STATE_NORMAL, *rb.GetImageSkiaNamed(IDR_CHECKBOX)); |
msw
2013/05/21 00:35:17
nit: I'm not sure how you ordered these, I'd do:
(
tfarina
2013/05/21 01:56:52
Done.
| |
30 return custom_insets_; | 24 SetCustomImage( |
31 | 25 true, false, STATE_NORMAL, *rb.GetImageSkiaNamed(IDR_CHECKBOX_CHECKED)); |
msw
2013/05/21 00:35:17
nit: put 3 state args on the line above, do the sa
tfarina
2013/05/21 01:56:52
Done.
| |
32 const gfx::Insets& insets = TextButtonNativeThemeBorder::GetInsets(); | 26 SetCustomImage(true, |
msw
2013/05/21 00:35:17
nit: combine 3 state args onto one line here and b
tfarina
2013/05/21 01:56:52
Done.
| |
33 return gfx::Insets(insets.top(), 0, insets.bottom(), 0); | 27 false, |
34 } | 28 STATE_NORMAL, |
35 | 29 *rb.GetImageSkiaNamed(IDR_CHECKBOX_CHECKED_HOVER)); |
36 void CheckboxNativeThemeBorder::SetCustomInsets( | 30 SetCustomImage(true, |
37 const gfx::Insets& custom_insets) { | 31 false, |
38 custom_insets_ = custom_insets; | 32 STATE_NORMAL, |
39 use_custom_insets_ = true; | 33 *rb.GetImageSkiaNamed(IDR_CHECKBOX_CHECKED_PRESSED)); |
40 } | 34 SetCustomImage( |
41 | 35 false, true, STATE_NORMAL, *rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED)); |
42 void CheckboxNativeThemeBorder::UseDefaultInsets() { | 36 SetCustomImage(true, |
43 use_custom_insets_ = false; | 37 true, |
44 } | 38 STATE_NORMAL, |
45 | 39 *rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED_CHECKED)); |
46 //////////////////////////////////////////////////////////////////////////////// | 40 SetCustomImage(true, |
47 // Checkbox, public: | 41 true, |
48 | 42 STATE_HOVERED, |
49 Checkbox::Checkbox(const string16& label) | 43 *rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED_CHECKED_HOVER)); |
50 : TextButtonBase(NULL, label), | 44 SetCustomImage(true, |
51 checked_(false) { | 45 true, |
52 set_border(new CheckboxNativeThemeBorder(this)); | 46 STATE_PRESSED, |
53 set_focusable(true); | 47 *rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED_CHECKED_PRESSED)); |
48 SetCustomImage(false, | |
49 true, | |
50 STATE_HOVERED, | |
51 *rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED_HOVER)); | |
52 SetCustomImage(false, | |
53 true, | |
54 STATE_PRESSED, | |
55 *rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED_PRESSED)); | |
56 SetImage(STATE_HOVERED, *rb.GetImageSkiaNamed(IDR_CHECKBOX_HOVER)); | |
msw
2013/05/21 00:35:17
Use SetCustomImage for everything, same for radio.
tfarina
2013/05/21 01:56:52
Done.
| |
57 SetImage(STATE_PRESSED, *rb.GetImageSkiaNamed(IDR_CHECKBOX_PRESSED)); | |
54 } | 58 } |
55 | 59 |
56 Checkbox::~Checkbox() { | 60 Checkbox::~Checkbox() { |
57 } | 61 } |
58 | 62 |
59 void Checkbox::SetChecked(bool checked) { | 63 void Checkbox::SetChecked(bool checked) { |
60 checked_ = checked; | 64 checked_ = checked; |
61 SchedulePaint(); | 65 SchedulePaint(); |
62 } | 66 } |
63 | 67 |
64 gfx::Size Checkbox::GetPreferredSize() { | |
65 gfx::Size prefsize(TextButtonBase::GetPreferredSize()); | |
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 | |
72 if (max_width_ > 0) | |
73 prefsize.set_width(std::min(max_width_, prefsize.width())); | |
74 | |
75 return prefsize; | |
76 } | |
77 | |
78 const char* Checkbox::GetClassName() const { | 68 const char* Checkbox::GetClassName() const { |
79 return kViewClassName; | 69 return kViewClassName; |
80 } | 70 } |
81 | 71 |
82 void Checkbox::GetAccessibleState(ui::AccessibleViewState* state) { | 72 void Checkbox::GetAccessibleState(ui::AccessibleViewState* state) { |
83 TextButtonBase::GetAccessibleState(state); | 73 LabelButton::GetAccessibleState(state); |
84 state->role = ui::AccessibilityTypes::ROLE_CHECKBUTTON; | 74 state->role = ui::AccessibilityTypes::ROLE_CHECKBUTTON; |
85 state->state = checked() ? ui::AccessibilityTypes::STATE_CHECKED : 0; | 75 state->state = checked() ? ui::AccessibilityTypes::STATE_CHECKED : 0; |
86 } | 76 } |
87 | 77 |
88 void Checkbox::OnPaintFocusBorder(gfx::Canvas* canvas) { | 78 const gfx::ImageSkia& Checkbox::GetImage(ButtonState for_state) { |
89 if (HasFocus() && (focusable() || IsAccessibilityFocusable())) { | 79 if (!checked_) |
msw
2013/05/21 00:35:17
I don't think we want this early return at all any
tfarina
2013/05/21 01:56:52
Done.
| |
90 gfx::Rect bounds(GetTextBounds()); | 80 return LabelButton::GetImage(for_state); |
91 // Increate the bounding box by one on each side so that that focus border | 81 |
92 // does not draw on top of the letters. | 82 const size_t checked_index = checked_ ? 1 : 0; |
93 bounds.Inset(-kFocusBorderWidth, | 83 const size_t focused_index = HasFocus() ? 1 : 0; |
94 -kFocusBorderWidth, | 84 if (for_state != STATE_NORMAL && |
95 -kFocusBorderWidth, | 85 images_[checked_index][focused_index][for_state].isNull()) |
96 -kFocusBorderWidth); | 86 return images_[checked_index][focused_index][STATE_NORMAL]; |
97 canvas->DrawFocusRect(bounds); | 87 return images_[checked_index][focused_index][for_state]; |
98 } | 88 } |
89 | |
90 void Checkbox::SetCustomImage(bool checked, | |
91 bool focused, | |
92 ButtonState for_state, | |
93 const gfx::ImageSkia& image) { | |
94 const size_t checked_index = checked ? 1 : 0; | |
95 const size_t focused_index = focused ? 1 : 0; | |
96 images_[checked_index][focused_index][for_state] = image; | |
97 image_->SetImage(GetImage(state())); | |
99 } | 98 } |
100 | 99 |
101 void Checkbox::NotifyClick(const ui::Event& event) { | 100 void Checkbox::NotifyClick(const ui::Event& event) { |
102 SetChecked(!checked()); | 101 SetChecked(!checked()); |
103 RequestFocus(); | 102 LabelButton::NotifyClick(event); |
104 TextButtonBase::NotifyClick(event); | |
105 } | 103 } |
106 | 104 |
107 ui::NativeTheme::Part Checkbox::GetThemePart() const { | 105 ui::NativeTheme::Part Checkbox::GetThemePart() const { |
108 return ui::NativeTheme::kCheckbox; | 106 return ui::NativeTheme::kCheckbox; |
109 } | 107 } |
110 | 108 |
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 { | 109 void Checkbox::GetExtraParams(ui::NativeTheme::ExtraParams* params) const { |
123 TextButtonBase::GetExtraParams(params); | 110 LabelButton::GetExtraParams(params); |
124 params->button.checked = checked_; | 111 params->button.checked = checked_; |
125 } | 112 } |
126 | 113 |
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 | 114 } // namespace views |
OLD | NEW |