1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "views/controls/button/checkbox.h"
#include "base/logging.h"
#include "ui/base/accessibility/accessible_view_state.h"
#include "ui/gfx/canvas.h"
#include "views/controls/label.h"
namespace views {
// static
const char NativeCheckbox::kViewClassName[] = "views/NativeCheckbox";
// static
const char Checkbox::kViewClassName[] = "views/Checkbox";
static const int kCheckboxLabelSpacing = 4;
static const int kLabelFocusPaddingHorizontal = 2;
static const int kLabelFocusPaddingVertical = 1;
////////////////////////////////////////////////////////////////////////////////
// NativeCheckbox, public:
NativeCheckbox::NativeCheckbox() : NativeButtonBase(NULL), checked_(false) {
Init(std::wstring());
}
NativeCheckbox::NativeCheckbox(const std::wstring& label)
: NativeButtonBase(NULL, label),
checked_(false) {
Init(label);
}
NativeCheckbox::~NativeCheckbox() {
}
// static
int NativeCheckbox::GetTextIndent() {
return NativeButtonWrapper::GetFixedWidth() + kCheckboxLabelSpacing;
}
void NativeCheckbox::SetMultiLine(bool multiline) {
label_->SetMultiLine(multiline);
PreferredSizeChanged();
}
void NativeCheckbox::SetChecked(bool checked) {
if (checked_ == checked)
return;
checked_ = checked;
if (native_wrapper_)
native_wrapper_->UpdateChecked();
}
////////////////////////////////////////////////////////////////////////////////
// NativeCheckbox, View overrides:
gfx::Size NativeCheckbox::GetPreferredSize() {
if (!native_wrapper_)
return gfx::Size();
gfx::Size prefsize = native_wrapper_->GetView()->GetPreferredSize();
if (native_wrapper_->UsesNativeLabel())
return prefsize;
prefsize.set_width(
prefsize.width() + kCheckboxLabelSpacing +
kLabelFocusPaddingHorizontal * 2);
gfx::Size label_prefsize = label_->GetPreferredSize();
prefsize.set_width(prefsize.width() + label_prefsize.width());
prefsize.set_height(
std::max(prefsize.height(),
label_prefsize.height() + kLabelFocusPaddingVertical * 2));
return prefsize;
}
int NativeCheckbox::GetHeightForWidth(int w) {
if (!native_wrapper_)
return 0;
gfx::Size prefsize = native_wrapper_->GetView()->GetPreferredSize();
if (native_wrapper_->UsesNativeLabel())
return prefsize.height();
int width = prefsize.width() + kCheckboxLabelSpacing +
kLabelFocusPaddingHorizontal * 2;
return label_->GetHeightForWidth(std::max(prefsize.height(), w - width));
}
void NativeCheckbox::OnEnabledChanged() {
NativeButtonBase::OnEnabledChanged();
if (label_)
label_->SetEnabled(IsEnabled());
}
void NativeCheckbox::Layout() {
if (!native_wrapper_)
return;
if (native_wrapper_->UsesNativeLabel()) {
label_->SetBounds(0, 0, 0, 0);
label_->SetVisible(false);
native_wrapper_->GetView()->SetBounds(0, 0, width(), height());
} else {
gfx::Size checkmark_prefsize =
native_wrapper_->GetView()->GetPreferredSize();
int label_x = checkmark_prefsize.width() + kCheckboxLabelSpacing +
kLabelFocusPaddingHorizontal;
label_->SetBounds(
label_x, 0, std::max(0, width() - label_x -
kLabelFocusPaddingHorizontal),
height());
int first_line_height = label_->font().GetHeight();
native_wrapper_->GetView()->SetBounds(
0, ((first_line_height - checkmark_prefsize.height()) / 2),
checkmark_prefsize.width(), checkmark_prefsize.height());
}
native_wrapper_->GetView()->Layout();
}
std::string NativeCheckbox::GetClassName() const {
return kViewClassName;
}
bool NativeCheckbox::OnMousePressed(const MouseEvent& event) {
native_wrapper_->SetPushed(HitTestLabel(event));
return true;
}
bool NativeCheckbox::OnMouseDragged(const MouseEvent& event) {
return false;
}
void NativeCheckbox::OnMouseReleased(const MouseEvent& event) {
OnMouseCaptureLost();
if (HitTestLabel(event)) {
SetChecked(!checked());
ButtonPressed();
}
}
void NativeCheckbox::OnMouseCaptureLost() {
native_wrapper_->SetPushed(false);
}
void NativeCheckbox::OnMouseMoved(const MouseEvent& event) {
native_wrapper_->SetPushed(HitTestLabel(event));
}
void NativeCheckbox::OnMouseEntered(const MouseEvent& event) {
native_wrapper_->SetPushed(HitTestLabel(event));
}
void NativeCheckbox::OnMouseExited(const MouseEvent& event) {
native_wrapper_->SetPushed(false);
}
void NativeCheckbox::GetAccessibleState(ui::AccessibleViewState* state) {
Button::GetAccessibleState(state);
state->role = ui::AccessibilityTypes::ROLE_CHECKBUTTON;
state->state = checked() ? ui::AccessibilityTypes::STATE_CHECKED : 0;
}
////////////////////////////////////////////////////////////////////////////////
// NativeCheckbox, NativeButton overrides:
void NativeCheckbox::SetLabel(const std::wstring& label) {
NativeButtonBase::SetLabel(label);
if (!native_wrapper_->UsesNativeLabel())
label_->SetText(label);
}
////////////////////////////////////////////////////////////////////////////////
// NativeCheckbox, protected:
bool NativeCheckbox::HitTestLabel(const MouseEvent& event) {
gfx::Point tmp(event.location());
ConvertPointToView(this, label_, &tmp);
return label_->HitTest(tmp);
}
////////////////////////////////////////////////////////////////////////////////
// NativeCheckbox, View overrides, protected:
void NativeCheckbox::OnPaintFocusBorder(gfx::Canvas* canvas) {
// Our focus border is rendered by the label, so we don't do anything here.
}
void NativeCheckbox::OnFocus() {
NativeButtonBase::OnFocus();
label_->set_paint_as_focused(true);
}
void NativeCheckbox::OnBlur() {
label_->set_paint_as_focused(false);
}
////////////////////////////////////////////////////////////////////////////////
// NativeCheckbox, NativeButton overrides, protected:
NativeButtonWrapper* NativeCheckbox::CreateWrapper() {
return NativeButtonWrapper::CreateCheckboxWrapper(this);
}
void NativeCheckbox::InitBorder() {
// No border, so we do nothing.
}
////////////////////////////////////////////////////////////////////////////////
// NativeCheckbox, private:
void NativeCheckbox::Init(const std::wstring& label_text) {
// Checkboxs don't need to enforce a minimum size.
set_ignore_minimum_size(true);
label_ = new Label(label_text);
label_->SetHasFocusBorder(true);
label_->SetHorizontalAlignment(Label::ALIGN_LEFT);
AddChildView(label_);
}
////////////////////////////////////////////////////////////////////////////////
// Checkbox, public:
Checkbox::Checkbox(const std::wstring& label)
: TextButtonBase(NULL, label),
checked_(false) {
set_border(new TextButtonNativeThemeBorder(this));
}
Checkbox::~Checkbox() {
}
void Checkbox::SetChecked(bool checked) {
checked_ = checked;
SchedulePaint();
}
gfx::Size Checkbox::GetPreferredSize() {
gfx::Size prefsize(TextButtonBase::GetPreferredSize());
gfx::NativeTheme::ExtraParams extra;
gfx::NativeTheme::State state = GetThemeState(&extra);
gfx::Size size = gfx::NativeTheme::instance()->GetPartSize(GetThemePart(),
state,
extra);
prefsize.Enlarge(size.width(), 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;
}
std::string Checkbox::GetClassName() const {
return kViewClassName;
}
void Checkbox::GetAccessibleState(ui::AccessibleViewState* state) {
TextButtonBase::GetAccessibleState(state);
state->role = ui::AccessibilityTypes::ROLE_CHECKBUTTON;
state->state = checked() ? ui::AccessibilityTypes::STATE_CHECKED : 0;
}
void Checkbox::OnPaintFocusBorder(gfx::Canvas* canvas) {
if (HasFocus() && (IsFocusable() || IsAccessibilityFocusableInRootView())) {
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(-1, -1, -1, -1);
canvas->DrawFocusRect(bounds.x(), bounds.y(), bounds.width(),
bounds.height());
}
}
void Checkbox::NotifyClick(const views::Event& event) {
SetChecked(!checked());
RequestFocus();
TextButtonBase::NotifyClick(event);
}
gfx::NativeTheme::Part Checkbox::GetThemePart() const {
return gfx::NativeTheme::kCheckbox;
}
gfx::Rect Checkbox::GetThemePaintRect() const {
gfx::NativeTheme::ExtraParams extra;
gfx::NativeTheme::State state = GetThemeState(&extra);
gfx::Size size(gfx::NativeTheme::instance()->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(gfx::NativeTheme::ExtraParams* params) const {
TextButtonBase::GetExtraParams(params);
params->button.is_default = false;
params->button.checked = checked_;
}
gfx::Rect Checkbox::GetTextBounds() const {
gfx::Rect bounds(TextButtonBase::GetTextBounds());
gfx::NativeTheme::ExtraParams extra;
gfx::NativeTheme::State state = GetThemeState(&extra);
gfx::Size size(gfx::NativeTheme::instance()->GetPartSize(GetThemePart(),
state,
extra));
bounds.Offset(size.width() + kCheckboxLabelSpacing, 0);
return bounds;
}
} // namespace views
|