Chromium Code Reviews| 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 LabelButtonBorder* button_border = static_cast<LabelButtonBorder*>(border()); | |
| 21 button_border->set_insets(gfx::Insets(7, 6, 5, 7)); | |
| 22 button_border->SetPainter(false, STATE_HOVERED, NULL); | |
| 23 button_border->SetPainter(false, STATE_PRESSED, NULL); | |
| 24 set_focus_border(NULL); | |
| 25 set_focusable(true); | |
| 27 | 26 |
| 28 gfx::Insets CheckboxNativeThemeBorder::GetInsets() const { | 27 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 29 if (use_custom_insets_) | |
| 30 return custom_insets_; | |
| 31 | 28 |
| 32 const gfx::Insets& insets = TextButtonNativeThemeBorder::GetInsets(); | 29 // Disabled image. |
| 33 return gfx::Insets(insets.top(), 0, insets.bottom(), 0); | 30 SetCustomImage(false, false, STATE_DISABLED, |
|
msw
2013/05/22 00:36:10
nit: remove the disabled comment and group this wi
tfarina
2013/05/22 03:03:59
Done.
| |
| 34 } | 31 *rb.GetImageSkiaNamed(IDR_CHECKBOX_DISABLED)); |
| 35 | 32 |
| 36 void CheckboxNativeThemeBorder::SetCustomInsets( | 33 // Unchecked/Unfocused images. |
| 37 const gfx::Insets& custom_insets) { | 34 SetCustomImage(false, false, STATE_NORMAL, |
| 38 custom_insets_ = custom_insets; | 35 *rb.GetImageSkiaNamed(IDR_CHECKBOX)); |
| 39 use_custom_insets_ = true; | 36 SetCustomImage(false, false, STATE_HOVERED, |
| 40 } | 37 *rb.GetImageSkiaNamed(IDR_CHECKBOX_HOVER)); |
| 38 SetCustomImage(false, false, STATE_PRESSED, | |
| 39 *rb.GetImageSkiaNamed(IDR_CHECKBOX_PRESSED)); | |
| 41 | 40 |
| 42 void CheckboxNativeThemeBorder::UseDefaultInsets() { | 41 // Checked/Unfocused images. |
| 43 use_custom_insets_ = false; | 42 SetCustomImage(true, false, STATE_NORMAL, |
| 44 } | 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)); | |
| 45 | 48 |
| 46 //////////////////////////////////////////////////////////////////////////////// | 49 // Unchecked/Focused images. |
| 47 // Checkbox, public: | 50 SetCustomImage(false, true, STATE_NORMAL, |
| 51 *rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED)); | |
| 52 SetCustomImage(false, true, STATE_HOVERED, | |
| 53 *rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED_HOVER)); | |
| 54 SetCustomImage(false, true, STATE_PRESSED, | |
| 55 *rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED_PRESSED)); | |
| 48 | 56 |
| 49 Checkbox::Checkbox(const string16& label) | 57 // Checked/Focused images. |
| 50 : TextButtonBase(NULL, label), | 58 SetCustomImage(true, true, STATE_NORMAL, |
| 51 checked_(false) { | 59 *rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED_CHECKED)); |
| 52 set_border(new CheckboxNativeThemeBorder(this)); | 60 SetCustomImage(true, true, STATE_HOVERED, |
| 53 set_focusable(true); | 61 *rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED_CHECKED_HOVER)); |
| 62 SetCustomImage(true, true, STATE_PRESSED, | |
| 63 *rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED_CHECKED_PRESSED)); | |
| 54 } | 64 } |
| 55 | 65 |
| 56 Checkbox::~Checkbox() { | 66 Checkbox::~Checkbox() { |
| 57 } | 67 } |
| 58 | 68 |
| 59 void Checkbox::SetChecked(bool checked) { | 69 void Checkbox::SetChecked(bool checked) { |
| 60 checked_ = checked; | 70 checked_ = checked; |
| 61 SchedulePaint(); | 71 UpdateImage(); |
| 62 } | |
| 63 | |
| 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 } | 72 } |
| 77 | 73 |
| 78 const char* Checkbox::GetClassName() const { | 74 const char* Checkbox::GetClassName() const { |
| 79 return kViewClassName; | 75 return kViewClassName; |
| 80 } | 76 } |
| 81 | 77 |
| 82 void Checkbox::GetAccessibleState(ui::AccessibleViewState* state) { | 78 void Checkbox::GetAccessibleState(ui::AccessibleViewState* state) { |
| 83 TextButtonBase::GetAccessibleState(state); | 79 LabelButton::GetAccessibleState(state); |
| 84 state->role = ui::AccessibilityTypes::ROLE_CHECKBUTTON; | 80 state->role = ui::AccessibilityTypes::ROLE_CHECKBUTTON; |
| 85 state->state = checked() ? ui::AccessibilityTypes::STATE_CHECKED : 0; | 81 state->state = checked() ? ui::AccessibilityTypes::STATE_CHECKED : 0; |
| 86 } | 82 } |
| 87 | 83 |
| 88 void Checkbox::OnPaintFocusBorder(gfx::Canvas* canvas) { | 84 void Checkbox::OnFocus() { |
| 89 if (HasFocus() && (focusable() || IsAccessibilityFocusable())) { | 85 UpdateImage(); |
| 90 gfx::Rect bounds(GetTextBounds()); | 86 } |
| 91 // Increate the bounding box by one on each side so that that focus border | 87 |
| 92 // does not draw on top of the letters. | 88 void Checkbox::OnBlur() { |
| 93 bounds.Inset(-kFocusBorderWidth, | 89 UpdateImage(); |
| 94 -kFocusBorderWidth, | 90 } |
| 95 -kFocusBorderWidth, | 91 |
| 96 -kFocusBorderWidth); | 92 const gfx::ImageSkia& Checkbox::GetImage(ButtonState for_state) { |
| 97 canvas->DrawFocusRect(bounds); | 93 const size_t checked_index = checked_ ? 1 : 0; |
| 98 } | 94 const size_t focused_index = HasFocus() ? 1 : 0; |
| 95 if (for_state != STATE_NORMAL && | |
| 96 images_[checked_index][focused_index][for_state].isNull()) | |
| 97 return images_[checked_index][focused_index][STATE_NORMAL]; | |
| 98 return images_[checked_index][focused_index][for_state]; | |
| 99 } | |
| 100 | |
| 101 void Checkbox::SetCustomImage(bool checked, | |
| 102 bool focused, | |
| 103 ButtonState for_state, | |
| 104 const gfx::ImageSkia& image) { | |
| 105 const size_t checked_index = checked ? 1 : 0; | |
| 106 const size_t focused_index = focused ? 1 : 0; | |
| 107 images_[checked_index][focused_index][for_state] = image; | |
| 108 UpdateImage(); | |
| 99 } | 109 } |
| 100 | 110 |
| 101 void Checkbox::NotifyClick(const ui::Event& event) { | 111 void Checkbox::NotifyClick(const ui::Event& event) { |
| 102 SetChecked(!checked()); | 112 SetChecked(!checked()); |
| 103 RequestFocus(); | 113 LabelButton::NotifyClick(event); |
| 104 TextButtonBase::NotifyClick(event); | |
| 105 } | 114 } |
| 106 | 115 |
| 107 ui::NativeTheme::Part Checkbox::GetThemePart() const { | 116 ui::NativeTheme::Part Checkbox::GetThemePart() const { |
| 108 return ui::NativeTheme::kCheckbox; | 117 return ui::NativeTheme::kCheckbox; |
| 109 } | 118 } |
| 110 | 119 |
| 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 { | 120 void Checkbox::GetExtraParams(ui::NativeTheme::ExtraParams* params) const { |
| 123 TextButtonBase::GetExtraParams(params); | 121 LabelButton::GetExtraParams(params); |
| 124 params->button.checked = checked_; | 122 params->button.checked = checked_; |
| 125 } | 123 } |
| 126 | 124 |
| 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 | 125 } // namespace views |
| OLD | NEW |