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 //////////////////////////////////////////////////////////////////////////////// | |
26 // CheckboxNativeThemeBorder, public: | |
27 | |
28 gfx::Insets CheckboxNativeThemeBorder::GetInsets() const { | |
29 if (use_custom_insets_) | |
30 return custom_insets_; | |
31 | |
32 const gfx::Insets& insets = TextButtonNativeThemeBorder::GetInsets(); | |
33 return gfx::Insets(insets.top(), 0, insets.bottom(), 0); | |
34 } | |
35 | |
36 void CheckboxNativeThemeBorder::SetCustomInsets( | |
37 const gfx::Insets& custom_insets) { | |
38 custom_insets_ = custom_insets; | |
39 use_custom_insets_ = true; | |
40 } | |
41 | |
42 void CheckboxNativeThemeBorder::UseDefaultInsets() { | |
43 use_custom_insets_ = false; | |
44 } | |
45 | |
46 //////////////////////////////////////////////////////////////////////////////// | |
47 // Checkbox, public: | |
48 | |
49 Checkbox::Checkbox(const string16& label) | 16 Checkbox::Checkbox(const string16& label) |
50 : TextButtonBase(NULL, label), | 17 : LabelButton(NULL, label) { |
51 checked_(false) { | 18 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
52 set_border(new CheckboxNativeThemeBorder(this)); | 19 SetStyle(STYLE_CHECKBOX); |
| 20 SetImage(STATE_NORMAL, *rb.GetImageSkiaNamed(IDR_CHECKBOX)); |
| 21 SetImage(STATE_TOGGLED, *rb.GetImageSkiaNamed(IDR_CHECKBOX_CHECKED)); |
| 22 SetImage(STATE_HOVERED, *rb.GetImageSkiaNamed(IDR_CHECKBOX_HOVER)); |
| 23 SetImage(STATE_PRESSED, *rb.GetImageSkiaNamed(IDR_CHECKBOX_PRESSED)); |
53 set_focusable(true); | 24 set_focusable(true); |
54 } | 25 } |
55 | 26 |
56 Checkbox::~Checkbox() { | 27 Checkbox::~Checkbox() { |
57 } | 28 } |
58 | 29 |
59 void Checkbox::SetChecked(bool checked) { | 30 void Checkbox::SetChecked(bool checked) { |
60 checked_ = checked; | 31 SetState(checked ? STATE_TOGGLED : STATE_NORMAL); |
61 SchedulePaint(); | 32 SchedulePaint(); |
62 } | 33 } |
63 | 34 |
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 { | 35 const char* Checkbox::GetClassName() const { |
79 return kViewClassName; | 36 return kViewClassName; |
80 } | 37 } |
81 | 38 |
82 void Checkbox::GetAccessibleState(ui::AccessibleViewState* state) { | 39 void Checkbox::GetAccessibleState(ui::AccessibleViewState* state) { |
83 TextButtonBase::GetAccessibleState(state); | 40 LabelButton::GetAccessibleState(state); |
84 state->role = ui::AccessibilityTypes::ROLE_CHECKBUTTON; | 41 state->role = ui::AccessibilityTypes::ROLE_CHECKBUTTON; |
85 state->state = checked() ? ui::AccessibilityTypes::STATE_CHECKED : 0; | 42 state->state = checked() ? ui::AccessibilityTypes::STATE_CHECKED : 0; |
86 } | 43 } |
87 | 44 |
88 void Checkbox::OnPaintFocusBorder(gfx::Canvas* canvas) { | |
89 if (HasFocus() && (focusable() || IsAccessibilityFocusable())) { | |
90 gfx::Rect bounds(GetTextBounds()); | |
91 // Increate the bounding box by one on each side so that that focus border | |
92 // does not draw on top of the letters. | |
93 bounds.Inset(-kFocusBorderWidth, | |
94 -kFocusBorderWidth, | |
95 -kFocusBorderWidth, | |
96 -kFocusBorderWidth); | |
97 canvas->DrawFocusRect(bounds); | |
98 } | |
99 } | |
100 | |
101 void Checkbox::NotifyClick(const ui::Event& event) { | 45 void Checkbox::NotifyClick(const ui::Event& event) { |
102 SetChecked(!checked()); | 46 SetChecked(!checked()); |
103 RequestFocus(); | 47 LabelButton::NotifyClick(event); |
104 TextButtonBase::NotifyClick(event); | |
105 } | 48 } |
106 | 49 |
107 ui::NativeTheme::Part Checkbox::GetThemePart() const { | 50 ui::NativeTheme::Part Checkbox::GetThemePart() const { |
108 return ui::NativeTheme::kCheckbox; | 51 return ui::NativeTheme::kCheckbox; |
109 } | 52 } |
110 | 53 |
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 { | 54 void Checkbox::GetExtraParams(ui::NativeTheme::ExtraParams* params) const { |
123 TextButtonBase::GetExtraParams(params); | 55 LabelButton::GetExtraParams(params); |
124 params->button.checked = checked_; | 56 params->button.checked = checked(); |
125 } | |
126 | |
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 } | 57 } |
135 | 58 |
136 } // namespace views | 59 } // namespace views |
OLD | NEW |