Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6)

Side by Side Diff: ui/views/controls/button/checkbox.cc

Issue 15061006: views: Switch Checkbox over to LabelButton. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: changes per review Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 // Unchecked/Unfocused images.
30 return custom_insets_; 24 SetCustomImage(false, false, STATE_NORMAL,
25 *rb.GetImageSkiaNamed(IDR_CHECKBOX));
26 SetCustomImage(false, false, STATE_HOVERED,
27 *rb.GetImageSkiaNamed(IDR_CHECKBOX_HOVER));
28 SetCustomImage(false, false, STATE_PRESSED,
29 *rb.GetImageSkiaNamed(IDR_CHECKBOX_PRESSED));
31 30
32 const gfx::Insets& insets = TextButtonNativeThemeBorder::GetInsets(); 31 // Checked/Unfocused images.
33 return gfx::Insets(insets.top(), 0, insets.bottom(), 0); 32 SetCustomImage(true, false, STATE_NORMAL,
34 } 33 *rb.GetImageSkiaNamed(IDR_CHECKBOX_CHECKED));
34 SetCustomImage(true, false, STATE_HOVERED,
35 *rb.GetImageSkiaNamed(IDR_CHECKBOX_CHECKED_HOVER));
36 SetCustomImage(true, false, STATE_PRESSED,
37 *rb.GetImageSkiaNamed(IDR_CHECKBOX_CHECKED_PRESSED));
35 38
36 void CheckboxNativeThemeBorder::SetCustomInsets( 39 // Unchecked/Focused images.
37 const gfx::Insets& custom_insets) { 40 SetCustomImage(false, true, STATE_NORMAL,
38 custom_insets_ = custom_insets; 41 *rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED));
39 use_custom_insets_ = true; 42 SetCustomImage(false, true, STATE_HOVERED,
40 } 43 *rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED_HOVER));
44 SetCustomImage(false, true, STATE_PRESSED,
45 *rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED_PRESSED));
41 46
42 void CheckboxNativeThemeBorder::UseDefaultInsets() { 47 // Checked/Focused images.
43 use_custom_insets_ = false; 48 SetCustomImage(true, true, STATE_NORMAL,
44 } 49 *rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED_CHECKED));
45 50 SetCustomImage(true, true, STATE_HOVERED,
46 //////////////////////////////////////////////////////////////////////////////// 51 *rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED_CHECKED_HOVER));
47 // Checkbox, public: 52 SetCustomImage(true, true, STATE_PRESSED,
48 53 *rb.GetImageSkiaNamed(IDR_CHECKBOX_FOCUSED_CHECKED_PRESSED));
49 Checkbox::Checkbox(const string16& label)
50 : TextButtonBase(NULL, label),
51 checked_(false) {
52 set_border(new CheckboxNativeThemeBorder(this));
53 set_focusable(true);
54 } 54 }
55 55
56 Checkbox::~Checkbox() { 56 Checkbox::~Checkbox() {
57 } 57 }
58 58
59 void Checkbox::SetChecked(bool checked) { 59 void Checkbox::SetChecked(bool checked) {
60 checked_ = checked; 60 checked_ = checked;
61 SchedulePaint(); 61 SchedulePaint();
62 } 62 }
63 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 }
77
78 const char* Checkbox::GetClassName() const { 64 const char* Checkbox::GetClassName() const {
79 return kViewClassName; 65 return kViewClassName;
80 } 66 }
81 67
82 void Checkbox::GetAccessibleState(ui::AccessibleViewState* state) { 68 void Checkbox::GetAccessibleState(ui::AccessibleViewState* state) {
83 TextButtonBase::GetAccessibleState(state); 69 LabelButton::GetAccessibleState(state);
84 state->role = ui::AccessibilityTypes::ROLE_CHECKBUTTON; 70 state->role = ui::AccessibilityTypes::ROLE_CHECKBUTTON;
85 state->state = checked() ? ui::AccessibilityTypes::STATE_CHECKED : 0; 71 state->state = checked() ? ui::AccessibilityTypes::STATE_CHECKED : 0;
86 } 72 }
87 73
88 void Checkbox::OnPaintFocusBorder(gfx::Canvas* canvas) { 74 const gfx::ImageSkia& Checkbox::GetImage(ButtonState for_state) {
89 if (HasFocus() && (focusable() || IsAccessibilityFocusable())) { 75 const size_t checked_index = checked_ ? 1 : 0;
90 gfx::Rect bounds(GetTextBounds()); 76 const size_t focused_index = HasFocus() ? 1 : 0;
91 // Increate the bounding box by one on each side so that that focus border 77 if (for_state != STATE_NORMAL &&
92 // does not draw on top of the letters. 78 images_[checked_index][focused_index][for_state].isNull())
93 bounds.Inset(-kFocusBorderWidth, 79 return images_[checked_index][focused_index][STATE_NORMAL];
94 -kFocusBorderWidth, 80 return images_[checked_index][focused_index][for_state];
95 -kFocusBorderWidth, 81 }
96 -kFocusBorderWidth); 82
97 canvas->DrawFocusRect(bounds); 83 void Checkbox::SetCustomImage(bool checked,
98 } 84 bool focused,
85 ButtonState for_state,
86 const gfx::ImageSkia& image) {
87 const size_t checked_index = checked ? 1 : 0;
88 const size_t focused_index = focused ? 1 : 0;
89 images_[checked_index][focused_index][for_state] = image;
90 image_->SetImage(GetImage(state()));
99 } 91 }
100 92
101 void Checkbox::NotifyClick(const ui::Event& event) { 93 void Checkbox::NotifyClick(const ui::Event& event) {
102 SetChecked(!checked()); 94 SetChecked(!checked());
103 RequestFocus(); 95 LabelButton::NotifyClick(event);
104 TextButtonBase::NotifyClick(event);
105 } 96 }
106 97
107 ui::NativeTheme::Part Checkbox::GetThemePart() const { 98 ui::NativeTheme::Part Checkbox::GetThemePart() const {
108 return ui::NativeTheme::kCheckbox; 99 return ui::NativeTheme::kCheckbox;
109 } 100 }
110 101
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 { 102 void Checkbox::GetExtraParams(ui::NativeTheme::ExtraParams* params) const {
123 TextButtonBase::GetExtraParams(params); 103 LabelButton::GetExtraParams(params);
124 params->button.checked = checked_; 104 params->button.checked = checked_;
125 } 105 }
126 106
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 107 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698