Chromium Code Reviews| Index: ui/views/controls/button/checkbox.cc |
| diff --git a/ui/views/controls/button/checkbox.cc b/ui/views/controls/button/checkbox.cc |
| index 0bf11ae0b3f6ba1e088aac4cf2e92d0bd3c7c1b3..1a89e9b9443bc0bc10ae9814b50752c396e4cb4e 100644 |
| --- a/ui/views/controls/button/checkbox.cc |
| +++ b/ui/views/controls/button/checkbox.cc |
| @@ -4,53 +4,57 @@ |
| #include "ui/views/controls/button/checkbox.h" |
| -#include "base/logging.h" |
| +#include "grit/ui_resources.h" |
| #include "ui/base/accessibility/accessible_view_state.h" |
| -#include "ui/gfx/canvas.h" |
| -#include "ui/views/controls/label.h" |
| +#include "ui/base/resource/resource_bundle.h" |
| namespace views { |
| -namespace { |
| - |
| -const int kCheckboxLabelSpacing = 4; |
| - |
| -const int kFocusBorderWidth = 1; |
| - |
| -} // namespace |
| - |
| // static |
| const char Checkbox::kViewClassName[] = "views/Checkbox"; |
| -//////////////////////////////////////////////////////////////////////////////// |
| -// CheckboxNativeThemeBorder, public: |
| - |
| -gfx::Insets CheckboxNativeThemeBorder::GetInsets() const { |
| - if (use_custom_insets_) |
| - return custom_insets_; |
| - |
| - const gfx::Insets& insets = TextButtonNativeThemeBorder::GetInsets(); |
| - return gfx::Insets(insets.top(), 0, insets.bottom(), 0); |
| -} |
| - |
| -void CheckboxNativeThemeBorder::SetCustomInsets( |
| - const gfx::Insets& custom_insets) { |
| - custom_insets_ = custom_insets; |
| - use_custom_insets_ = true; |
| -} |
| - |
| -void CheckboxNativeThemeBorder::UseDefaultInsets() { |
| - use_custom_insets_ = false; |
| -} |
| - |
| -//////////////////////////////////////////////////////////////////////////////// |
| -// Checkbox, public: |
| - |
| Checkbox::Checkbox(const string16& label) |
| - : TextButtonBase(NULL, label), |
| + : LabelButton(NULL, label), |
| checked_(false) { |
| - set_border(new CheckboxNativeThemeBorder(this)); |
| + SetStyle(STYLE_CHECKBOX); |
| set_focusable(true); |
| + |
| + ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| + 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.
|
| + SetCustomImage( |
| + 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.
|
| + 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.
|
| + false, |
| + STATE_NORMAL, |
| + *rb.GetImageSkiaNamed(IDR_CHECKBOX_CHECKED_HOVER)); |
| + SetCustomImage(true, |
| + false, |
| + STATE_NORMAL, |
| + *rb.GetImageSkiaNamed(IDR_CHECKBOX_CHECKED_PRESSED)); |
| + SetCustomImage( |
| + false, true, STATE_NORMAL, *rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED)); |
| + SetCustomImage(true, |
| + true, |
| + STATE_NORMAL, |
| + *rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED_CHECKED)); |
| + SetCustomImage(true, |
| + true, |
| + STATE_HOVERED, |
| + *rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED_CHECKED_HOVER)); |
| + SetCustomImage(true, |
| + true, |
| + STATE_PRESSED, |
| + *rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED_CHECKED_PRESSED)); |
| + SetCustomImage(false, |
| + true, |
| + STATE_HOVERED, |
| + *rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED_HOVER)); |
| + SetCustomImage(false, |
| + true, |
| + STATE_PRESSED, |
| + *rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED_PRESSED)); |
| + 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.
|
| + SetImage(STATE_PRESSED, *rb.GetImageSkiaNamed(IDR_CHECKBOX_PRESSED)); |
| } |
| Checkbox::~Checkbox() { |
| @@ -61,76 +65,50 @@ void Checkbox::SetChecked(bool checked) { |
| SchedulePaint(); |
| } |
| -gfx::Size Checkbox::GetPreferredSize() { |
| - gfx::Size prefsize(TextButtonBase::GetPreferredSize()); |
| - ui::NativeTheme::ExtraParams extra; |
| - ui::NativeTheme::State state = GetThemeState(&extra); |
| - gfx::Size size = GetNativeTheme()->GetPartSize(GetThemePart(), state, extra); |
| - prefsize.Enlarge(size.width() + kCheckboxLabelSpacing + kFocusBorderWidth, 0); |
| - prefsize.set_height(std::max(prefsize.height(), size.height())); |
| - |
| - if (max_width_ > 0) |
| - prefsize.set_width(std::min(max_width_, prefsize.width())); |
| - |
| - return prefsize; |
| -} |
| - |
| const char* Checkbox::GetClassName() const { |
| return kViewClassName; |
| } |
| void Checkbox::GetAccessibleState(ui::AccessibleViewState* state) { |
| - TextButtonBase::GetAccessibleState(state); |
| + LabelButton::GetAccessibleState(state); |
| state->role = ui::AccessibilityTypes::ROLE_CHECKBUTTON; |
| state->state = checked() ? ui::AccessibilityTypes::STATE_CHECKED : 0; |
| } |
| -void Checkbox::OnPaintFocusBorder(gfx::Canvas* canvas) { |
| - if (HasFocus() && (focusable() || IsAccessibilityFocusable())) { |
| - gfx::Rect bounds(GetTextBounds()); |
| - // Increate the bounding box by one on each side so that that focus border |
| - // does not draw on top of the letters. |
| - bounds.Inset(-kFocusBorderWidth, |
| - -kFocusBorderWidth, |
| - -kFocusBorderWidth, |
| - -kFocusBorderWidth); |
| - canvas->DrawFocusRect(bounds); |
| - } |
| +const gfx::ImageSkia& Checkbox::GetImage(ButtonState for_state) { |
| + 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.
|
| + return LabelButton::GetImage(for_state); |
| + |
| + const size_t checked_index = checked_ ? 1 : 0; |
| + const size_t focused_index = HasFocus() ? 1 : 0; |
| + if (for_state != STATE_NORMAL && |
| + images_[checked_index][focused_index][for_state].isNull()) |
| + return images_[checked_index][focused_index][STATE_NORMAL]; |
| + return images_[checked_index][focused_index][for_state]; |
| +} |
| + |
| +void Checkbox::SetCustomImage(bool checked, |
| + bool focused, |
| + ButtonState for_state, |
| + const gfx::ImageSkia& image) { |
| + const size_t checked_index = checked ? 1 : 0; |
| + const size_t focused_index = focused ? 1 : 0; |
| + images_[checked_index][focused_index][for_state] = image; |
| + image_->SetImage(GetImage(state())); |
| } |
| void Checkbox::NotifyClick(const ui::Event& event) { |
| SetChecked(!checked()); |
| - RequestFocus(); |
| - TextButtonBase::NotifyClick(event); |
| + LabelButton::NotifyClick(event); |
| } |
| ui::NativeTheme::Part Checkbox::GetThemePart() const { |
| return ui::NativeTheme::kCheckbox; |
| } |
| -gfx::Rect Checkbox::GetThemePaintRect() const { |
| - ui::NativeTheme::ExtraParams extra; |
| - ui::NativeTheme::State state = GetThemeState(&extra); |
| - gfx::Size size(GetNativeTheme()->GetPartSize(GetThemePart(), state, extra)); |
| - gfx::Insets insets = GetInsets(); |
| - int y_offset = (height() - size.height()) / 2; |
| - gfx::Rect rect(insets.left(), y_offset, size.width(), size.height()); |
| - rect.set_x(GetMirroredXForRect(rect)); |
| - return rect; |
| -} |
| - |
| void Checkbox::GetExtraParams(ui::NativeTheme::ExtraParams* params) const { |
| - TextButtonBase::GetExtraParams(params); |
| + LabelButton::GetExtraParams(params); |
| params->button.checked = checked_; |
| } |
| -gfx::Rect Checkbox::GetTextBounds() const { |
| - gfx::Rect bounds(TextButtonBase::GetTextBounds()); |
| - ui::NativeTheme::ExtraParams extra; |
| - ui::NativeTheme::State state = GetThemeState(&extra); |
| - gfx::Size size(GetNativeTheme()->GetPartSize(GetThemePart(), state, extra)); |
| - bounds.Offset(size.width() + kCheckboxLabelSpacing, 0); |
| - return bounds; |
| -} |
| - |
| } // namespace views |