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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
|
// Copyright (c) 2012 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 "ui/views/controls/combobox/combobox.h"
#include "base/logging.h"
#include "base/strings/utf_string_conversions.h"
#include "grit/ui_resources.h"
#include "ui/base/accessibility/accessible_view_state.h"
#include "ui/base/events/event.h"
#include "ui/base/keycodes/keyboard_codes.h"
#include "ui/base/models/combobox_model.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/canvas.h"
#include "ui/native_theme/native_theme.h"
#include "ui/views/color_constants.h"
#include "ui/views/controls/combobox/combobox_listener.h"
#include "ui/views/controls/focusable_border.h"
#include "ui/views/controls/menu/menu_runner.h"
#include "ui/views/controls/menu/submenu_view.h"
#include "ui/views/controls/prefix_selector.h"
#include "ui/views/ime/input_method.h"
#include "ui/views/mouse_constants.h"
#include "ui/views/widget/widget.h"
namespace views {
namespace {
// Menu border widths
const int kMenuBorderWidthLeft = 1;
const int kMenuBorderWidthTop = 1;
const int kMenuBorderWidthRight = 1;
const int kMenuBorderWidthBottom = 2;
// Limit how small a combobox can be.
const int kMinComboboxWidth = 25;
// Size of the combobox arrow margins
const int kDisclosureArrowLeftPadding = 7;
const int kDisclosureArrowRightPadding = 7;
// Define the id of the first item in the menu (since it needs to be > 0)
const int kFirstMenuItemId = 1000;
const SkColor kInvalidTextColor = SK_ColorWHITE;
// Used to indicate that no item is currently selected by the user.
const int kNoSelection = -1;
// The background to use for invalid comboboxes.
class InvalidBackground : public Background {
public:
InvalidBackground() {}
virtual ~InvalidBackground() {}
// Overridden from Background:
virtual void Paint(gfx::Canvas* canvas, View* view) const OVERRIDE {
gfx::Rect bounds(view->GetLocalBounds());
// Inset by 2 to leave 1 empty pixel between background and border.
bounds.Inset(2, 2, 2, 2);
canvas->FillRect(bounds, kWarningColor);
}
private:
DISALLOW_COPY_AND_ASSIGN(InvalidBackground);
};
// Returns the next or previous valid index (depending on |increment|'s value).
// Skips separator indices. Returns -1 if there is no valid adjacent index.
int GetAdjacentIndex(ui::ComboboxModel* model, int increment, int index) {
DCHECK(increment == -1 || increment == 1);
index += increment;
while (index >= 0 && index < model->GetItemCount()) {
if (!model->IsItemSeparatorAt(index))
return index;
index += increment;
}
return kNoSelection;
}
} // namespace
// static
const char Combobox::kViewClassName[] = "views/Combobox";
////////////////////////////////////////////////////////////////////////////////
// Combobox, public:
Combobox::Combobox(ui::ComboboxModel* model)
: model_(model),
listener_(NULL),
selected_index_(model_->GetDefaultIndex()),
invalid_(false),
text_border_(new FocusableBorder()),
disclosure_arrow_(ui::ResourceBundle::GetSharedInstance().GetImageNamed(
IDR_MENU_DROPARROW).ToImageSkia()),
dropdown_open_(false) {
UpdateFromModel();
set_focusable(true);
set_border(text_border_);
}
Combobox::~Combobox() {
}
// static
const gfx::Font& Combobox::GetFont() {
ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
return rb.GetFont(ui::ResourceBundle::BaseFont);
}
void Combobox::ModelChanged() {
selected_index_ = std::min(0, model_->GetItemCount());
UpdateFromModel();
PreferredSizeChanged();
}
void Combobox::SetSelectedIndex(int index) {
selected_index_ = index;
SchedulePaint();
}
void Combobox::SetAccessibleName(const string16& name) {
accessible_name_ = name;
}
void Combobox::SetInvalid(bool invalid) {
invalid_ = invalid;
if (invalid) {
text_border_->SetColor(kWarningColor);
set_background(new InvalidBackground());
} else {
text_border_->UseDefaultColor();
set_background(NULL);
}
}
ui::TextInputClient* Combobox::GetTextInputClient() {
if (!selector_)
selector_.reset(new PrefixSelector(this));
return selector_.get();
}
bool Combobox::IsItemChecked(int id) const {
return false;
}
bool Combobox::IsCommandEnabled(int id) const {
return true;
}
void Combobox::ExecuteCommand(int id) {
// (note that the id received is offset by kFirstMenuItemId)
// Revert menu ID offset to map back to combobox model.
id -= kFirstMenuItemId;
DCHECK_LT(id, model()->GetItemCount());
selected_index_ = id;
OnSelectionChanged();
}
bool Combobox::GetAccelerator(int id, ui::Accelerator* accel) {
return false;
}
int Combobox::GetRowCount() {
return model()->GetItemCount();
}
int Combobox::GetSelectedRow() {
return selected_index_;
}
void Combobox::SetSelectedRow(int row) {
SetSelectedIndex(row);
}
string16 Combobox::GetTextForRow(int row) {
return model()->GetItemAt(row);
}
////////////////////////////////////////////////////////////////////////////////
// Combobox, View overrides:
gfx::Size Combobox::GetPreferredSize() {
if (content_size_.IsEmpty())
UpdateFromModel();
// The preferred size will drive the local bounds which in turn is used to set
// the minimum width for the dropdown list.
gfx::Insets insets = GetInsets();
int total_width = std::max(kMinComboboxWidth, content_size_.width()) +
insets.width() + kDisclosureArrowLeftPadding +
disclosure_arrow_->width() + kDisclosureArrowRightPadding;
return gfx::Size(total_width, content_size_.height() + insets.height());
}
const char* Combobox::GetClassName() const {
return kViewClassName;
}
bool Combobox::SkipDefaultKeyEventProcessing(const ui::KeyEvent& e) {
// Escape should close the drop down list when it is active, not host UI.
if (e.key_code() != ui::VKEY_ESCAPE ||
e.IsShiftDown() || e.IsControlDown() || e.IsAltDown()) {
return false;
}
return dropdown_open_;
}
bool Combobox::OnMousePressed(const ui::MouseEvent& mouse_event) {
RequestFocus();
const base::TimeDelta delta = base::Time::Now() - closed_time_;
if (mouse_event.IsLeftMouseButton() &&
(delta.InMilliseconds() > kMinimumMsBetweenButtonClicks)) {
UpdateFromModel();
ShowDropDownMenu(ui::MENU_SOURCE_MOUSE);
}
return true;
}
bool Combobox::OnMouseDragged(const ui::MouseEvent& mouse_event) {
return true;
}
bool Combobox::OnKeyPressed(const ui::KeyEvent& e) {
// TODO(oshima): handle IME.
DCHECK_EQ(e.type(), ui::ET_KEY_PRESSED);
DCHECK_GE(selected_index_, 0);
DCHECK_LT(selected_index_, model()->GetItemCount());
if (selected_index_ < 0 || selected_index_ > model()->GetItemCount())
selected_index_ = 0;
bool show_menu = false;
int new_index = kNoSelection;
switch (e.key_code()) {
// Show the menu on Space.
case ui::VKEY_SPACE:
show_menu = true;
break;
// Show the menu on Alt+Down (like Windows) or move to the next item if any.
case ui::VKEY_DOWN:
if (e.IsAltDown())
show_menu = true;
else
new_index = GetAdjacentIndex(model(), 1, selected_index_);
break;
// Move to the end of the list.
case ui::VKEY_END:
case ui::VKEY_NEXT: // Page down.
new_index = GetAdjacentIndex(model(), -1, model()->GetItemCount());
break;
// Move to the beginning of the list.
case ui::VKEY_HOME:
case ui::VKEY_PRIOR: // Page up.
new_index = GetAdjacentIndex(model(), 1, -1);
break;
// Move to the previous item if any.
case ui::VKEY_UP:
new_index = GetAdjacentIndex(model(), -1, selected_index_);
break;
default:
return false;
}
if (show_menu) {
UpdateFromModel();
ShowDropDownMenu(ui::MENU_SOURCE_KEYBOARD);
} else if (new_index != selected_index_ && new_index != kNoSelection) {
DCHECK(!model()->IsItemSeparatorAt(new_index));
selected_index_ = new_index;
OnSelectionChanged();
}
return true;
}
bool Combobox::OnKeyReleased(const ui::KeyEvent& e) {
return false; // crbug.com/127520
}
void Combobox::OnGestureEvent(ui::GestureEvent* gesture) {
if (gesture->type() == ui::ET_GESTURE_TAP) {
UpdateFromModel();
ShowDropDownMenu(ui::MENU_SOURCE_TOUCH);
gesture->StopPropagation();
return;
}
View::OnGestureEvent(gesture);
}
void Combobox::OnPaint(gfx::Canvas* canvas) {
OnPaintBackground(canvas);
PaintText(canvas);
OnPaintBorder(canvas);
}
void Combobox::OnFocus() {
GetInputMethod()->OnFocus();
text_border_->set_has_focus(true);
View::OnFocus();
}
void Combobox::OnBlur() {
GetInputMethod()->OnBlur();
if (selector_)
selector_->OnViewBlur();
text_border_->set_has_focus(false);
}
void Combobox::GetAccessibleState(ui::AccessibleViewState* state) {
state->role = ui::AccessibilityTypes::ROLE_COMBOBOX;
state->name = accessible_name_;
state->value = model_->GetItemAt(selected_index_);
state->index = selected_index_;
state->count = model_->GetItemCount();
}
void Combobox::UpdateFromModel() {
int max_width = 0;
const gfx::Font& font = Combobox::GetFont();
MenuItemView* menu = new MenuItemView(this);
// MenuRunner owns |menu|.
dropdown_list_menu_runner_.reset(new MenuRunner(menu));
int num_items = model()->GetItemCount();
for (int i = 0; i < num_items; ++i) {
if (model()->IsItemSeparatorAt(i)) {
menu->AppendSeparator();
continue;
}
string16 text = model()->GetItemAt(i);
// Inserting the Unicode formatting characters if necessary so that the
// text is displayed correctly in right-to-left UIs.
base::i18n::AdjustStringForLocaleDirection(&text);
menu->AppendMenuItem(i + kFirstMenuItemId, text, MenuItemView::NORMAL);
max_width = std::max(max_width, font.GetStringWidth(text));
}
content_size_.SetSize(max_width, font.GetHeight());
}
void Combobox::AdjustBoundsForRTLUI(gfx::Rect* rect) const {
rect->set_x(GetMirroredXForRect(*rect));
}
void Combobox::PaintText(gfx::Canvas* canvas) {
gfx::Insets insets = GetInsets();
canvas->Save();
canvas->ClipRect(GetContentsBounds());
int x = insets.left();
int y = insets.top();
int text_height = height() - insets.height();
SkColor text_color = invalid() ? kInvalidTextColor :
GetNativeTheme()->GetSystemColor(
ui::NativeTheme::kColorId_LabelEnabledColor);
DCHECK_GE(selected_index_, 0);
DCHECK_LT(selected_index_, model()->GetItemCount());
if (selected_index_ < 0 || selected_index_ > model()->GetItemCount())
selected_index_ = 0;
string16 text = model()->GetItemAt(selected_index_);
int disclosure_arrow_offset = width() - disclosure_arrow_->width()
- kDisclosureArrowLeftPadding - kDisclosureArrowRightPadding;
const gfx::Font& font = Combobox::GetFont();
int text_width = font.GetStringWidth(text);
if ((text_width + insets.width()) > disclosure_arrow_offset)
text_width = disclosure_arrow_offset - insets.width();
gfx::Rect text_bounds(x, y, text_width, text_height);
AdjustBoundsForRTLUI(&text_bounds);
canvas->DrawStringInt(text, font, text_color, text_bounds);
gfx::Rect arrow_bounds(disclosure_arrow_offset + kDisclosureArrowLeftPadding,
height() / 2 - disclosure_arrow_->height() / 2,
disclosure_arrow_->width(),
disclosure_arrow_->height());
AdjustBoundsForRTLUI(&arrow_bounds);
SkPaint paint;
// This makes the arrow subtractive.
if (invalid())
paint.setXfermodeMode(SkXfermode::kDstOut_Mode);
canvas->DrawImageInt(*disclosure_arrow_, arrow_bounds.x(), arrow_bounds.y(),
paint);
canvas->Restore();
}
void Combobox::ShowDropDownMenu(ui::MenuSourceType source_type) {
if (!dropdown_list_menu_runner_.get())
UpdateFromModel();
// Extend the menu to the width of the combobox.
MenuItemView* menu = dropdown_list_menu_runner_->GetMenu();
SubmenuView* submenu = menu->CreateSubmenu();
submenu->set_minimum_preferred_width(size().width() -
(kMenuBorderWidthLeft + kMenuBorderWidthRight));
gfx::Rect lb = GetLocalBounds();
gfx::Point menu_position(lb.origin());
// Inset the menu's requested position so the border of the menu lines up
// with the border of the combobox.
menu_position.set_x(menu_position.x() + kMenuBorderWidthLeft);
menu_position.set_y(menu_position.y() + kMenuBorderWidthTop);
lb.set_width(lb.width() - (kMenuBorderWidthLeft + kMenuBorderWidthRight));
View::ConvertPointToScreen(this, &menu_position);
if (menu_position.x() < 0)
menu_position.set_x(0);
gfx::Rect bounds(menu_position, lb.size());
dropdown_open_ = true;
if (dropdown_list_menu_runner_->RunMenuAt(
GetWidget(), NULL, bounds, MenuItemView::TOPLEFT, source_type, 0) ==
MenuRunner::MENU_DELETED)
return;
dropdown_open_ = false;
closed_time_ = base::Time::Now();
// Need to explicitly clear mouse handler so that events get sent
// properly after the menu finishes running. If we don't do this, then
// the first click to other parts of the UI is eaten.
SetMouseHandler(NULL);
}
void Combobox::OnSelectionChanged() {
if (listener_)
listener_->OnSelectedIndexChanged(this);
NotifyAccessibilityEvent(ui::AccessibilityTypes::EVENT_VALUE_CHANGED, false);
SchedulePaint();
}
} // namespace views
|