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

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: almost working - :p 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 "base/logging.h"
msw 2013/05/13 01:37:22 nit: remove
tfarina 2013/05/13 01:47:12 Done.
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/gfx/canvas.h"
msw 2013/05/13 01:37:22 nit: remove
tfarina 2013/05/13 01:47:12 Done.
10 #include "ui/views/controls/label.h" 10 #include "ui/views/controls/label.h"
msw 2013/05/13 01:37:22 nit: remove
tfarina 2013/05/13 01:47:12 Done.
11 #include "ui/base/resource/resource_bundle.h"
12 #include "grit/ui_resources.h"
11 13
12 namespace views { 14 namespace views {
13 15
14 namespace { 16 namespace {
15 17
16 const int kCheckboxLabelSpacing = 4; 18 const int kCheckboxLabelSpacing = 4;
msw 2013/05/13 01:37:22 Remove this, just use the LabelButton default Imag
tfarina 2013/05/13 01:47:12 Done.
17 19
18 const int kFocusBorderWidth = 1; 20 const int kFocusBorderWidth = 1;
msw 2013/05/13 01:37:22 Let's try to remove this and use default LabelButt
tfarina 2013/05/13 01:47:12 Done.
19 21
20 } // namespace 22 } // namespace
21 23
22 // static 24 // static
23 const char Checkbox::kViewClassName[] = "views/Checkbox"; 25 const char Checkbox::kViewClassName[] = "views/Checkbox";
24 26
25 //////////////////////////////////////////////////////////////////////////////// 27 ////////////////////////////////////////////////////////////////////////////////
26 // CheckboxNativeThemeBorder, public: 28 // CheckboxNativeThemeBorder, public:
27 29
28 gfx::Insets CheckboxNativeThemeBorder::GetInsets() const { 30 gfx::Insets CheckboxNativeThemeBorder::GetInsets() const {
(...skipping 11 matching lines...) Expand all
40 } 42 }
41 43
42 void CheckboxNativeThemeBorder::UseDefaultInsets() { 44 void CheckboxNativeThemeBorder::UseDefaultInsets() {
43 use_custom_insets_ = false; 45 use_custom_insets_ = false;
44 } 46 }
45 47
46 //////////////////////////////////////////////////////////////////////////////// 48 ////////////////////////////////////////////////////////////////////////////////
47 // Checkbox, public: 49 // Checkbox, public:
48 50
49 Checkbox::Checkbox(const string16& label) 51 Checkbox::Checkbox(const string16& label)
50 : TextButtonBase(NULL, label), 52 : LabelButton(NULL, label),
51 checked_(false) { 53 checked_(false) {
52 set_border(new CheckboxNativeThemeBorder(this)); 54 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
55 SetStyle(STYLE_CHECKBOX);
56 SetImage(STATE_NORMAL, *rb.GetImageSkiaNamed(IDR_CHECKBOX));
57 SetImage(STATE_HOVERED, *rb.GetImageSkiaNamed(IDR_CHECKBOX_HOVER));
58 SetImage(STATE_PRESSED, *rb.GetImageSkiaNamed(IDR_CHECKBOX_PRESSED));
53 set_focusable(true); 59 set_focusable(true);
54 } 60 }
55 61
56 Checkbox::~Checkbox() { 62 Checkbox::~Checkbox() {
57 } 63 }
58 64
59 void Checkbox::SetChecked(bool checked) { 65 void Checkbox::SetChecked(bool checked) {
60 checked_ = checked; 66 checked_ = checked;
61 SchedulePaint(); 67 SchedulePaint();
62 } 68 }
63 69
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 { 70 const char* Checkbox::GetClassName() const {
79 return kViewClassName; 71 return kViewClassName;
80 } 72 }
81 73
82 void Checkbox::GetAccessibleState(ui::AccessibleViewState* state) { 74 void Checkbox::GetAccessibleState(ui::AccessibleViewState* state) {
83 TextButtonBase::GetAccessibleState(state); 75 LabelButton::GetAccessibleState(state);
84 state->role = ui::AccessibilityTypes::ROLE_CHECKBUTTON; 76 state->role = ui::AccessibilityTypes::ROLE_CHECKBUTTON;
85 state->state = checked() ? ui::AccessibilityTypes::STATE_CHECKED : 0; 77 state->state = checked() ? ui::AccessibilityTypes::STATE_CHECKED : 0;
86 } 78 }
87 79
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) { 80 void Checkbox::NotifyClick(const ui::Event& event) {
102 SetChecked(!checked()); 81 SetChecked(!checked());
103 RequestFocus(); 82 RequestFocus();
msw 2013/05/13 01:37:22 nit: try to remove this.
tfarina 2013/05/13 01:47:12 Done.
104 TextButtonBase::NotifyClick(event); 83 LabelButton::NotifyClick(event);
105 }
106
107 ui::NativeTheme::Part Checkbox::GetThemePart() const {
108 return ui::NativeTheme::kCheckbox;
109 }
110
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 {
123 TextButtonBase::GetExtraParams(params);
124 params->button.checked = checked_;
125 }
126
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 } 84 }
135 85
136 } // namespace views 86 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698