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