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" | |
| 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) { |
|
msw
2013/05/18 01:04:56
Restore the checked_(false) init.
tfarina
2013/05/18 02:30:49
Done.
| |
| 51 checked_(false) { | 18 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
|
msw
2013/05/18 01:04:56
nit: move this below SetStyle (just before its use
tfarina
2013/05/18 02:30:49
Done.
tfarina
2013/05/18 02:30:49
Done.
| |
| 52 set_border(new CheckboxNativeThemeBorder(this)); | 19 SetStyle(STYLE_CHECKBOX); |
| 20 SetImage(STATE_NORMAL, *rb.GetImageSkiaNamed(IDR_CHECKBOX)); | |
| 21 SetImage(STATE_HOVERED, *rb.GetImageSkiaNamed(IDR_CHECKBOX_HOVER)); | |
| 22 SetImage(STATE_PRESSED, *rb.GetImageSkiaNamed(IDR_CHECKBOX_PRESSED)); | |
| 53 set_focusable(true); | 23 set_focusable(true); |
| 54 } | 24 } |
| 55 | 25 |
| 56 Checkbox::~Checkbox() { | 26 Checkbox::~Checkbox() { |
| 57 } | 27 } |
| 58 | 28 |
| 59 void Checkbox::SetChecked(bool checked) { | 29 void Checkbox::SetChecked(bool checked) { |
| 60 checked_ = checked; | 30 checked_ = checked; |
| 61 SchedulePaint(); | 31 SchedulePaint(); |
| 62 } | 32 } |
| 63 | 33 |
| 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 { | 34 const char* Checkbox::GetClassName() const { |
| 79 return kViewClassName; | 35 return kViewClassName; |
| 80 } | 36 } |
| 81 | 37 |
| 82 void Checkbox::GetAccessibleState(ui::AccessibleViewState* state) { | 38 void Checkbox::GetAccessibleState(ui::AccessibleViewState* state) { |
| 83 TextButtonBase::GetAccessibleState(state); | 39 LabelButton::GetAccessibleState(state); |
| 84 state->role = ui::AccessibilityTypes::ROLE_CHECKBUTTON; | 40 state->role = ui::AccessibilityTypes::ROLE_CHECKBUTTON; |
| 85 state->state = checked() ? ui::AccessibilityTypes::STATE_CHECKED : 0; | 41 state->state = checked() ? ui::AccessibilityTypes::STATE_CHECKED : 0; |
| 86 } | 42 } |
| 87 | 43 |
| 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) { | 44 void Checkbox::NotifyClick(const ui::Event& event) { |
| 102 SetChecked(!checked()); | 45 SetChecked(!checked()); |
| 103 RequestFocus(); | 46 LabelButton::NotifyClick(event); |
| 104 TextButtonBase::NotifyClick(event); | |
| 105 } | 47 } |
| 106 | 48 |
| 107 ui::NativeTheme::Part Checkbox::GetThemePart() const { | 49 ui::NativeTheme::Part Checkbox::GetThemePart() const { |
| 108 return ui::NativeTheme::kCheckbox; | 50 return ui::NativeTheme::kCheckbox; |
| 109 } | 51 } |
| 110 | 52 |
| 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 { | 53 void Checkbox::GetExtraParams(ui::NativeTheme::ExtraParams* params) const { |
| 123 TextButtonBase::GetExtraParams(params); | 54 LabelButton::GetExtraParams(params); |
| 124 params->button.checked = checked_; | 55 params->button.checked = checked_; |
| 125 } | 56 } |
| 126 | 57 |
| 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 | 58 } // namespace views |
| OLD | NEW |