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

Side by Side Diff: ui/views/controls/combobox/combobox.cc

Issue 59383003: Add the button style for combobox (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: sky's review (5) Created 7 years 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
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/combobox/combobox.h" 5 #include "ui/views/controls/combobox/combobox.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/strings/utf_string_conversions.h" 8 #include "base/strings/utf_string_conversions.h"
9 #include "grit/ui_resources.h" 9 #include "grit/ui_resources.h"
10 #include "ui/base/accessibility/accessible_view_state.h" 10 #include "ui/base/accessibility/accessible_view_state.h"
11 #include "ui/base/models/combobox_model.h" 11 #include "ui/base/models/combobox_model.h"
12 #include "ui/base/resource/resource_bundle.h" 12 #include "ui/base/resource/resource_bundle.h"
13 #include "ui/events/event.h" 13 #include "ui/events/event.h"
14 #include "ui/events/keycodes/keyboard_codes.h" 14 #include "ui/events/keycodes/keyboard_codes.h"
15 #include "ui/gfx/animation/throb_animation.h"
15 #include "ui/gfx/canvas.h" 16 #include "ui/gfx/canvas.h"
17 #include "ui/gfx/image/image.h"
18 #include "ui/gfx/scoped_canvas.h"
16 #include "ui/native_theme/native_theme.h" 19 #include "ui/native_theme/native_theme.h"
17 #include "ui/views/color_constants.h" 20 #include "ui/views/color_constants.h"
21 #include "ui/views/controls/button/custom_button.h"
22 #include "ui/views/controls/button/label_button.h"
18 #include "ui/views/controls/combobox/combobox_listener.h" 23 #include "ui/views/controls/combobox/combobox_listener.h"
19 #include "ui/views/controls/focusable_border.h" 24 #include "ui/views/controls/focusable_border.h"
20 #include "ui/views/controls/menu/menu_runner.h" 25 #include "ui/views/controls/menu/menu_runner.h"
26 #include "ui/views/controls/menu/menu_runner_core.h"
21 #include "ui/views/controls/menu/submenu_view.h" 27 #include "ui/views/controls/menu/submenu_view.h"
22 #include "ui/views/controls/prefix_selector.h" 28 #include "ui/views/controls/prefix_selector.h"
23 #include "ui/views/ime/input_method.h" 29 #include "ui/views/ime/input_method.h"
24 #include "ui/views/mouse_constants.h" 30 #include "ui/views/mouse_constants.h"
31 #include "ui/views/painter.h"
25 #include "ui/views/widget/widget.h" 32 #include "ui/views/widget/widget.h"
26 33
27 namespace views { 34 namespace views {
28 35
29 namespace { 36 namespace {
30 37
31 // Menu border widths 38 // Menu border widths
32 const int kMenuBorderWidthLeft = 1; 39 const int kMenuBorderWidthLeft = 1;
33 const int kMenuBorderWidthTop = 1; 40 const int kMenuBorderWidthTop = 1;
34 const int kMenuBorderWidthRight = 1; 41 const int kMenuBorderWidthRight = 1;
35 42
36 // Limit how small a combobox can be. 43 // Limit how small a combobox can be.
37 const int kMinComboboxWidth = 25; 44 const int kMinComboboxWidth = 25;
38 45
39 // Size of the combobox arrow margins 46 // Size of the combobox arrow margins
40 const int kDisclosureArrowLeftPadding = 7; 47 const int kDisclosureArrowLeftPadding = 7;
41 const int kDisclosureArrowRightPadding = 7; 48 const int kDisclosureArrowRightPadding = 7;
49 const int kDisclosureArrowButtonLeftPadding = 11;
50 const int kDisclosureArrowButtonRightPadding = 12;
42 51
43 // Define the id of the first item in the menu (since it needs to be > 0) 52 // Define the id of the first item in the menu (since it needs to be > 0)
44 const int kFirstMenuItemId = 1000; 53 const int kFirstMenuItemId = 1000;
45 54
46 const SkColor kInvalidTextColor = SK_ColorWHITE; 55 const SkColor kInvalidTextColor = SK_ColorWHITE;
47 56
48 // Used to indicate that no item is currently selected by the user. 57 // Used to indicate that no item is currently selected by the user.
49 const int kNoSelection = -1; 58 const int kNoSelection = -1;
50 59
60 const int kBodyButtonImages[] = IMAGE_GRID(IDR_COMBOBOX_BUTTON);
61 const int kHoveredBodyButtonImages[] = IMAGE_GRID(IDR_COMBOBOX_BUTTON_H);
62 const int kPressedBodyButtonImages[] = IMAGE_GRID(IDR_COMBOBOX_BUTTON_P);
63
64 #define MENU_IMAGE_GRID(x) { \
65 x ## _MENU_TOP, x ## _MENU_CENTER, x ## _MENU_BOTTOM, }
66
67 const int kMenuButtonImages[] = MENU_IMAGE_GRID(IDR_COMBOBOX_BUTTON);
68 const int kHoveredMenuButtonImages[] = MENU_IMAGE_GRID(IDR_COMBOBOX_BUTTON_H);
69 const int kPressedMenuButtonImages[] = MENU_IMAGE_GRID(IDR_COMBOBOX_BUTTON_P);
70
71 #undef MENU_IMAGE_GRID
72
51 // The background to use for invalid comboboxes. 73 // The background to use for invalid comboboxes.
52 class InvalidBackground : public Background { 74 class InvalidBackground : public Background {
53 public: 75 public:
54 InvalidBackground() {} 76 InvalidBackground() {}
55 virtual ~InvalidBackground() {} 77 virtual ~InvalidBackground() {}
56 78
57 // Overridden from Background: 79 // Overridden from Background:
58 virtual void Paint(gfx::Canvas* canvas, View* view) const OVERRIDE { 80 virtual void Paint(gfx::Canvas* canvas, View* view) const OVERRIDE {
59 gfx::Rect bounds(view->GetLocalBounds()); 81 gfx::Rect bounds(view->GetLocalBounds());
60 // Inset by 2 to leave 1 empty pixel between background and border. 82 // Inset by 2 to leave 1 empty pixel between background and border.
61 bounds.Inset(2, 2, 2, 2); 83 bounds.Inset(2, 2, 2, 2);
62 canvas->FillRect(bounds, kWarningColor); 84 canvas->FillRect(bounds, kWarningColor);
63 } 85 }
64 86
65 private: 87 private:
66 DISALLOW_COPY_AND_ASSIGN(InvalidBackground); 88 DISALLOW_COPY_AND_ASSIGN(InvalidBackground);
67 }; 89 };
68 90
91 // The transparent button which holds a button state but is not rendered.
92 class TransparentButton : public CustomButton {
93 public:
94 TransparentButton(ButtonListener* listener)
95 : CustomButton(listener) {
96 SetAnimationDuration(LabelButton::kHoverAnimationDurationMs);
97 }
98 virtual ~TransparentButton() {}
99
100 double GetAnimationValue() const {
101 return hover_animation_->GetCurrentValue();
102 }
103
104 private:
105 DISALLOW_COPY_AND_ASSIGN(TransparentButton);
106 };
107
69 // Returns the next or previous valid index (depending on |increment|'s value). 108 // Returns the next or previous valid index (depending on |increment|'s value).
70 // Skips separator or disabled indices. Returns -1 if there is no valid adjacent 109 // Skips separator or disabled indices. Returns -1 if there is no valid adjacent
71 // index. 110 // index.
72 int GetAdjacentIndex(ui::ComboboxModel* model, int increment, int index) { 111 int GetAdjacentIndex(ui::ComboboxModel* model, int increment, int index) {
73 DCHECK(increment == -1 || increment == 1); 112 DCHECK(increment == -1 || increment == 1);
74 113
75 index += increment; 114 index += increment;
76 while (index >= 0 && index < model->GetItemCount()) { 115 while (index >= 0 && index < model->GetItemCount()) {
77 if (!model->IsItemSeparatorAt(index) || !model->IsItemEnabledAt(index)) 116 if (!model->IsItemSeparatorAt(index) || !model->IsItemEnabledAt(index))
78 return index; 117 return index;
79 index += increment; 118 index += increment;
80 } 119 }
81 return kNoSelection; 120 return kNoSelection;
82 } 121 }
83 122
123 // Returns the image resource ids of an array for the body button.
124 //
125 // TODO(hajimehoshi): This function should return the images for the 'disabled'
126 // status. (crbug/270052)
127 //
128 // TODO(hajimehoshi): Currently, |focused| is ignored. This should return the
129 // images for the 'focused' status. (crbug/270052)
130 const int* GetBodyButtonImageIds(bool focused,
131 Button::ButtonState state,
132 size_t* num) {
133 DCHECK(num);
134 *num = 9;
135 switch (state) {
136 case Button::STATE_DISABLED:
137 return kBodyButtonImages;
138 case Button::STATE_NORMAL:
139 return kBodyButtonImages;
140 case Button::STATE_HOVERED:
141 return kHoveredBodyButtonImages;
142 case Button::STATE_PRESSED:
143 return kPressedBodyButtonImages;
144 default:
145 NOTREACHED();
146 }
147 return NULL;
148 }
149
150 // Returns the image resource ids of an array for the menu button.
151 const int* GetMenuButtonImageIds(bool focused,
152 Button::ButtonState state,
153 size_t* num) {
154 DCHECK(num);
155 *num = 3;
156 switch (state) {
157 case Button::STATE_DISABLED:
158 return kMenuButtonImages;
159 case Button::STATE_NORMAL:
160 return kMenuButtonImages;
161 case Button::STATE_HOVERED:
162 return kHoveredMenuButtonImages;
163 case Button::STATE_PRESSED:
164 return kPressedMenuButtonImages;
165 default:
166 NOTREACHED();
167 }
168 return NULL;
169 }
170
171 // Returns the images for the menu buttons.
172 std::vector<const gfx::ImageSkia*> GetMenuButtonImages(
173 bool focused,
174 Button::ButtonState state) {
175 const int* ids;
176 size_t num_ids;
177 ids = GetMenuButtonImageIds(focused, state, &num_ids);
178 std::vector<const gfx::ImageSkia*> images;
179 images.reserve(num_ids);
180 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
181 for (size_t i = 0; i < num_ids; i++)
182 images.push_back(rb.GetImageSkiaNamed(ids[i]));
183 return images;
184 }
185
186 // Paints three images in a column at the given location. The center image is
187 // stretched so as to fit the given height.
188 void PaintImagesVertically(gfx::Canvas* canvas,
189 const gfx::ImageSkia& top_image,
190 const gfx::ImageSkia& center_image,
191 const gfx::ImageSkia& bottom_image,
192 int x, int y, int width, int height) {
193 canvas->DrawImageInt(top_image,
194 0, 0, top_image.width(), top_image.height(),
195 x, y, width, top_image.height(), false);
196 y += top_image.height();
197 int center_height = height - top_image.height() - bottom_image.height();
198 canvas->DrawImageInt(center_image,
199 0, 0, center_image.width(), center_image.height(),
200 x, y, width, center_height, false);
201 y += center_height;
202 canvas->DrawImageInt(bottom_image,
203 0, 0, bottom_image.width(), bottom_image.height(),
204 x, y, width, bottom_image.height(), false);
205 }
206
207 // Paints the arrow button.
208 void PaintArrowButton(
209 gfx::Canvas* canvas,
210 const std::vector<const gfx::ImageSkia*>& arrow_button_images,
211 int x, int height) {
212 PaintImagesVertically(canvas,
213 *arrow_button_images[0],
214 *arrow_button_images[1],
215 *arrow_button_images[2],
216 x, 0, arrow_button_images[0]->width(), height);
217 }
218
84 } // namespace 219 } // namespace
85 220
86 // static 221 // static
87 const char Combobox::kViewClassName[] = "views/Combobox"; 222 const char Combobox::kViewClassName[] = "views/Combobox";
88 223
89 //////////////////////////////////////////////////////////////////////////////// 224 ////////////////////////////////////////////////////////////////////////////////
90 // Combobox, public: 225 // Combobox, public:
91 226
92 Combobox::Combobox(ui::ComboboxModel* model) 227 Combobox::Combobox(ui::ComboboxModel* model)
93 : model_(model), 228 : model_(model),
229 style_(STYLE_SHOW_DROP_DOWN_ON_CLICK),
94 listener_(NULL), 230 listener_(NULL),
95 selected_index_(model_->GetDefaultIndex()), 231 selected_index_(model_->GetDefaultIndex()),
96 invalid_(false), 232 invalid_(false),
97 text_border_(new FocusableBorder()),
98 disclosure_arrow_(ui::ResourceBundle::GetSharedInstance().GetImageNamed( 233 disclosure_arrow_(ui::ResourceBundle::GetSharedInstance().GetImageNamed(
99 IDR_MENU_DROPARROW).ToImageSkia()), 234 IDR_MENU_DROPARROW).ToImageSkia()),
100 dropdown_open_(false) { 235 dropdown_open_(false),
236 text_button_(new TransparentButton(this)),
237 arrow_button_(new TransparentButton(this)) {
101 model_->AddObserver(this); 238 model_->AddObserver(this);
102 UpdateFromModel(); 239 UpdateFromModel();
103 set_focusable(true); 240 set_focusable(true);
104 set_border(text_border_); 241 UpdateBorder();
242
243 // Initialize the button images.
244 Button::ButtonState button_states[] = {
245 Button::STATE_DISABLED,
246 Button::STATE_NORMAL,
247 Button::STATE_HOVERED,
248 Button::STATE_PRESSED,
249 };
250 for (int focused = 0; focused < 2; focused++) {
251 for (size_t state_index = 0; state_index < arraysize(button_states);
252 state_index++) {
253 Button::ButtonState state = button_states[state_index];
254 size_t num;
255 const int* ids = GetBodyButtonImageIds(focused, state, &num);
256 body_button_painters_[focused][state].reset(
257 Painter::CreateImageGridPainter(ids));
258 menu_button_images_[focused][state] = GetMenuButtonImages(focused, state);
259 }
260 }
261
262 text_button_->SetVisible(true);
263 arrow_button_->SetVisible(true);
264 text_button_->set_focusable(false);
265 arrow_button_->set_focusable(false);
266 AddChildView(text_button_);
267 AddChildView(arrow_button_);
105 } 268 }
106 269
107 Combobox::~Combobox() { 270 Combobox::~Combobox() {
108 model_->RemoveObserver(this); 271 model_->RemoveObserver(this);
109 } 272 }
110 273
111 // static 274 // static
112 const gfx::Font& Combobox::GetFont() { 275 const gfx::Font& Combobox::GetFont() {
113 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); 276 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
114 return rb.GetFont(ui::ResourceBundle::BaseFont); 277 return rb.GetFont(ui::ResourceBundle::BaseFont);
115 } 278 }
116 279
280 void Combobox::SetStyle(Style style) {
281 if (style_ == style)
282 return;
283
284 style_ = style;
285
286 UpdateBorder();
287 PreferredSizeChanged();
288 }
289
117 void Combobox::ModelChanged() { 290 void Combobox::ModelChanged() {
118 selected_index_ = std::min(0, model_->GetItemCount()); 291 selected_index_ = std::min(0, model_->GetItemCount());
119 UpdateFromModel(); 292 UpdateFromModel();
120 PreferredSizeChanged(); 293 PreferredSizeChanged();
121 } 294 }
122 295
123 void Combobox::SetSelectedIndex(int index) { 296 void Combobox::SetSelectedIndex(int index) {
124 selected_index_ = index; 297 selected_index_ = index;
125 SchedulePaint(); 298 SchedulePaint();
126 } 299 }
(...skipping 10 matching lines...) Expand all
137 310
138 void Combobox::SetAccessibleName(const string16& name) { 311 void Combobox::SetAccessibleName(const string16& name) {
139 accessible_name_ = name; 312 accessible_name_ = name;
140 } 313 }
141 314
142 void Combobox::SetInvalid(bool invalid) { 315 void Combobox::SetInvalid(bool invalid) {
143 if (invalid == invalid_) 316 if (invalid == invalid_)
144 return; 317 return;
145 318
146 invalid_ = invalid; 319 invalid_ = invalid;
147 if (invalid_) { 320
148 text_border_->SetColor(kWarningColor); 321 set_background(invalid_ ? new InvalidBackground() : NULL);
149 set_background(new InvalidBackground()); 322 UpdateBorder();
150 } else {
151 text_border_->UseDefaultColor();
152 set_background(NULL);
153 }
154 SchedulePaint(); 323 SchedulePaint();
155 } 324 }
156 325
157 ui::TextInputClient* Combobox::GetTextInputClient() { 326 ui::TextInputClient* Combobox::GetTextInputClient() {
158 if (!selector_) 327 if (!selector_)
159 selector_.reset(new PrefixSelector(this)); 328 selector_.reset(new PrefixSelector(this));
160 return selector_.get(); 329 return selector_.get();
161 } 330 }
162 331
332 void Combobox::Layout() {
333 PrefixDelegate::Layout();
334
335 gfx::Insets insets = GetInsets();
336 int text_button_width = 0;
337 int arrow_button_width = 0;
338
339 switch (style_) {
340 case STYLE_SHOW_DROP_DOWN_ON_CLICK: {
341 arrow_button_width = width();
342 break;
343 }
344 case STYLE_NOTIFY_ON_CLICK: {
345 arrow_button_width = GetDisclosureArrowLeftPadding() +
346 disclosure_arrow_->width() + GetDisclosureArrowRightPadding();
347 text_button_width = width() - arrow_button_width;
348 break;
349 }
350 }
351
352 int arrow_button_x = std::max(0, text_button_width);
353 text_button_->SetBounds(0, 0, std::max(0, text_button_width), height());
354 arrow_button_->SetBounds(arrow_button_x, 0, arrow_button_width, height());
355 }
163 356
164 bool Combobox::IsItemChecked(int id) const { 357 bool Combobox::IsItemChecked(int id) const {
165 return false; 358 return false;
166 } 359 }
167 360
168 bool Combobox::IsCommandEnabled(int id) const { 361 bool Combobox::IsCommandEnabled(int id) const {
169 return model()->IsItemEnabledAt(MenuCommandToIndex(id)); 362 return model()->IsItemEnabledAt(MenuCommandToIndex(id));
170 } 363 }
171 364
172 void Combobox::ExecuteCommand(int id) { 365 void Combobox::ExecuteCommand(int id) {
(...skipping 14 matching lines...) Expand all
187 } 380 }
188 381
189 void Combobox::SetSelectedRow(int row) { 382 void Combobox::SetSelectedRow(int row) {
190 SetSelectedIndex(row); 383 SetSelectedIndex(row);
191 } 384 }
192 385
193 string16 Combobox::GetTextForRow(int row) { 386 string16 Combobox::GetTextForRow(int row) {
194 return model()->IsItemSeparatorAt(row) ? string16() : model()->GetItemAt(row); 387 return model()->IsItemSeparatorAt(row) ? string16() : model()->GetItemAt(row);
195 } 388 }
196 389
390 void Combobox::SetMenuRunnerCore(MenuRunnerCore* menu_runner_core) {
391 menu_runner_core_.reset(menu_runner_core);
392 if (dropdown_list_menu_runner_.get())
393 dropdown_list_menu_runner_->set_runner_core(menu_runner_core_.get());
394 }
395
197 //////////////////////////////////////////////////////////////////////////////// 396 ////////////////////////////////////////////////////////////////////////////////
198 // Combobox, View overrides: 397 // Combobox, View overrides:
199 398
200 gfx::Size Combobox::GetPreferredSize() { 399 gfx::Size Combobox::GetPreferredSize() {
201 if (content_size_.IsEmpty()) 400 if (content_size_.IsEmpty())
202 UpdateFromModel(); 401 UpdateFromModel();
203 402
204 // The preferred size will drive the local bounds which in turn is used to set 403 // The preferred size will drive the local bounds which in turn is used to set
205 // the minimum width for the dropdown list. 404 // the minimum width for the dropdown list.
206 gfx::Insets insets = GetInsets(); 405 gfx::Insets insets = GetInsets();
207 int total_width = std::max(kMinComboboxWidth, content_size_.width()) + 406 int total_width = std::max(kMinComboboxWidth, content_size_.width()) +
208 insets.width() + kDisclosureArrowLeftPadding + 407 insets.width() + GetDisclosureArrowLeftPadding() +
209 disclosure_arrow_->width() + kDisclosureArrowRightPadding; 408 disclosure_arrow_->width() + GetDisclosureArrowRightPadding();
210
211 return gfx::Size(total_width, content_size_.height() + insets.height()); 409 return gfx::Size(total_width, content_size_.height() + insets.height());
212 } 410 }
213 411
214 const char* Combobox::GetClassName() const { 412 const char* Combobox::GetClassName() const {
215 return kViewClassName; 413 return kViewClassName;
216 } 414 }
217 415
218 bool Combobox::SkipDefaultKeyEventProcessing(const ui::KeyEvent& e) { 416 bool Combobox::SkipDefaultKeyEventProcessing(const ui::KeyEvent& e) {
219 // Escape should close the drop down list when it is active, not host UI. 417 // Escape should close the drop down list when it is active, not host UI.
220 if (e.key_code() != ui::VKEY_ESCAPE || 418 if (e.key_code() != ui::VKEY_ESCAPE ||
221 e.IsShiftDown() || e.IsControlDown() || e.IsAltDown()) { 419 e.IsShiftDown() || e.IsControlDown() || e.IsAltDown()) {
222 return false; 420 return false;
223 } 421 }
224 return dropdown_open_; 422 return dropdown_open_;
225 } 423 }
226 424
227 bool Combobox::OnMousePressed(const ui::MouseEvent& mouse_event) {
228 RequestFocus();
229 const base::TimeDelta delta = base::Time::Now() - closed_time_;
230 if (mouse_event.IsLeftMouseButton() &&
231 (delta.InMilliseconds() > kMinimumMsBetweenButtonClicks)) {
232 UpdateFromModel();
233 ShowDropDownMenu(ui::MENU_SOURCE_MOUSE);
234 }
235
236 return true;
237 }
238
239 bool Combobox::OnMouseDragged(const ui::MouseEvent& mouse_event) {
240 return true;
241 }
242
243 bool Combobox::OnKeyPressed(const ui::KeyEvent& e) { 425 bool Combobox::OnKeyPressed(const ui::KeyEvent& e) {
244 // TODO(oshima): handle IME. 426 // TODO(oshima): handle IME.
245 DCHECK_EQ(e.type(), ui::ET_KEY_PRESSED); 427 DCHECK_EQ(e.type(), ui::ET_KEY_PRESSED);
246 428
247 DCHECK_GE(selected_index_, 0); 429 DCHECK_GE(selected_index_, 0);
248 DCHECK_LT(selected_index_, model()->GetItemCount()); 430 DCHECK_LT(selected_index_, model()->GetItemCount());
249 if (selected_index_ < 0 || selected_index_ > model()->GetItemCount()) 431 if (selected_index_ < 0 || selected_index_ > model()->GetItemCount())
250 selected_index_ = 0; 432 selected_index_ = 0;
251 433
252 bool show_menu = false; 434 bool show_menu = false;
(...skipping 24 matching lines...) Expand all
277 case ui::VKEY_HOME: 459 case ui::VKEY_HOME:
278 case ui::VKEY_PRIOR: // Page up. 460 case ui::VKEY_PRIOR: // Page up.
279 new_index = GetAdjacentIndex(model(), 1, -1); 461 new_index = GetAdjacentIndex(model(), 1, -1);
280 break; 462 break;
281 463
282 // Move to the previous item if any. 464 // Move to the previous item if any.
283 case ui::VKEY_UP: 465 case ui::VKEY_UP:
284 new_index = GetAdjacentIndex(model(), -1, selected_index_); 466 new_index = GetAdjacentIndex(model(), -1, selected_index_);
285 break; 467 break;
286 468
469 // Click the button only when the button style mode.
470 case ui::VKEY_SPACE:
471 if (style_ == STYLE_NOTIFY_ON_CLICK) {
472 // When pressing space, the click event will be raised after the key is
473 // released.
474 text_button_->SetState(Button::STATE_PRESSED);
475 }
476 break;
477
478 // Click the button only when the button style mode.
479 case ui::VKEY_RETURN:
480 HandleClickEvent();
481 break;
482
287 default: 483 default:
288 return false; 484 return false;
289 } 485 }
290 486
291 if (show_menu) { 487 if (show_menu) {
292 UpdateFromModel(); 488 UpdateFromModel();
293 ShowDropDownMenu(ui::MENU_SOURCE_KEYBOARD); 489 ShowDropDownMenu(ui::MENU_SOURCE_KEYBOARD);
294 } else if (new_index != selected_index_ && new_index != kNoSelection) { 490 } else if (new_index != selected_index_ && new_index != kNoSelection) {
295 DCHECK(!model()->IsItemSeparatorAt(new_index)); 491 DCHECK(!model()->IsItemSeparatorAt(new_index));
296 selected_index_ = new_index; 492 selected_index_ = new_index;
297 OnSelectionChanged(); 493 OnSelectionChanged();
298 } 494 }
299 495
300 return true; 496 return true;
301 } 497 }
302 498
303 bool Combobox::OnKeyReleased(const ui::KeyEvent& e) { 499 bool Combobox::OnKeyReleased(const ui::KeyEvent& e) {
304 return false; // crbug.com/127520 500 if (style_ != STYLE_NOTIFY_ON_CLICK)
305 } 501 return false; // crbug.com/127520
306 502
307 void Combobox::OnGestureEvent(ui::GestureEvent* gesture) { 503 if (e.key_code() == ui::VKEY_SPACE)
308 if (gesture->type() == ui::ET_GESTURE_TAP) { 504 HandleClickEvent();
309 UpdateFromModel(); 505
310 ShowDropDownMenu(ui::MENU_SOURCE_TOUCH); 506 return false;
311 gesture->StopPropagation();
312 return;
313 }
314 View::OnGestureEvent(gesture);
315 } 507 }
316 508
317 void Combobox::OnPaint(gfx::Canvas* canvas) { 509 void Combobox::OnPaint(gfx::Canvas* canvas) {
318 OnPaintBackground(canvas); 510 switch (style_) {
319 PaintText(canvas); 511 case STYLE_SHOW_DROP_DOWN_ON_CLICK: {
320 OnPaintBorder(canvas); 512 OnPaintBackground(canvas);
513 PaintText(canvas);
514 OnPaintBorder(canvas);
515 break;
516 }
517 case STYLE_NOTIFY_ON_CLICK: {
518 PaintButtons(canvas);
519 PaintText(canvas);
520 break;
521 }
522 }
321 } 523 }
322 524
323 void Combobox::OnFocus() { 525 void Combobox::OnFocus() {
324 GetInputMethod()->OnFocus(); 526 GetInputMethod()->OnFocus();
325 View::OnFocus(); 527 View::OnFocus();
326 // Border renders differently when focused. 528 // Border renders differently when focused.
327 SchedulePaint(); 529 SchedulePaint();
328 } 530 }
329 531
330 void Combobox::OnBlur() { 532 void Combobox::OnBlur() {
331 GetInputMethod()->OnBlur(); 533 GetInputMethod()->OnBlur();
332 if (selector_) 534 if (selector_)
333 selector_->OnViewBlur(); 535 selector_->OnViewBlur();
334 // Border renders differently when focused. 536 // Border renders differently when focused.
335 SchedulePaint(); 537 SchedulePaint();
336 } 538 }
337 539
338 void Combobox::GetAccessibleState(ui::AccessibleViewState* state) { 540 void Combobox::GetAccessibleState(ui::AccessibleViewState* state) {
339 state->role = ui::AccessibilityTypes::ROLE_COMBOBOX; 541 state->role = ui::AccessibilityTypes::ROLE_COMBOBOX;
340 state->name = accessible_name_; 542 state->name = accessible_name_;
341 state->value = model_->GetItemAt(selected_index_); 543 state->value = model_->GetItemAt(selected_index_);
342 state->index = selected_index_; 544 state->index = selected_index_;
343 state->count = model_->GetItemCount(); 545 state->count = model_->GetItemCount();
344 } 546 }
345 547
346 void Combobox::OnModelChanged() { 548 void Combobox::OnModelChanged() {
347 ModelChanged(); 549 ModelChanged();
348 } 550 }
349 551
552 void Combobox::ButtonPressed(Button* sender, const ui::Event& event) {
553 RequestFocus();
554
555 if (sender == text_button_) {
556 HandleClickEvent();
557 } else {
558 DCHECK_EQ(arrow_button_, sender);
559 // TODO(hajimehoshi): Fix the problem that the arrow button blinks when
560 // cliking this while the dropdown menu is opened.
561 const base::TimeDelta delta = base::Time::Now() - closed_time_;
562 if (delta.InMilliseconds() <= kMinimumMsBetweenButtonClicks)
563 return;
564
565 ui::MenuSourceType source_type = ui::MENU_SOURCE_MOUSE;
566 if (event.IsKeyEvent())
567 source_type = ui::MENU_SOURCE_KEYBOARD;
568 else if (event.IsGestureEvent() || event.IsTouchEvent())
569 source_type = ui::MENU_SOURCE_TOUCH;
570 ShowDropDownMenu(source_type);
571 }
572 }
573
350 void Combobox::UpdateFromModel() { 574 void Combobox::UpdateFromModel() {
351 int max_width = 0; 575 int max_width = 0;
352 const gfx::Font& font = Combobox::GetFont(); 576 const gfx::Font& font = Combobox::GetFont();
353 577
354 MenuItemView* menu = new MenuItemView(this); 578 MenuItemView* menu = new MenuItemView(this);
355 // MenuRunner owns |menu|. 579 // MenuRunner owns |menu|.
356 dropdown_list_menu_runner_.reset(new MenuRunner(menu)); 580 dropdown_list_menu_runner_.reset(new MenuRunner(menu));
581 if (menu_runner_core_.get())
582 dropdown_list_menu_runner_->set_runner_core(menu_runner_core_.get());
357 583
358 int num_items = model()->GetItemCount(); 584 int num_items = model()->GetItemCount();
359 for (int i = 0; i < num_items; ++i) { 585 for (int i = 0; i < num_items; ++i) {
360 if (model()->IsItemSeparatorAt(i)) { 586 if (model()->IsItemSeparatorAt(i)) {
361 menu->AppendSeparator(); 587 menu->AppendSeparator();
362 continue; 588 continue;
363 } 589 }
364 590
365 string16 text = model()->GetItemAt(i); 591 string16 text = model()->GetItemAt(i);
366 592
367 // Inserting the Unicode formatting characters if necessary so that the 593 // Inserting the Unicode formatting characters if necessary so that the
368 // text is displayed correctly in right-to-left UIs. 594 // text is displayed correctly in right-to-left UIs.
369 base::i18n::AdjustStringForLocaleDirection(&text); 595 base::i18n::AdjustStringForLocaleDirection(&text);
370 596
371 menu->AppendMenuItem(i + kFirstMenuItemId, text, MenuItemView::NORMAL); 597 menu->AppendMenuItem(i + kFirstMenuItemId, text, MenuItemView::NORMAL);
372 max_width = std::max(max_width, font.GetStringWidth(text)); 598 max_width = std::max(max_width, font.GetStringWidth(text));
373 } 599 }
374 600
375 content_size_.SetSize(max_width, font.GetHeight()); 601 content_size_.SetSize(max_width, font.GetHeight());
376 } 602 }
377 603
604 void Combobox::UpdateBorder() {
605 FocusableBorder* border = new FocusableBorder();
606 if (style_ == STYLE_NOTIFY_ON_CLICK)
607 border->SetInsets(8, 13, 8, 13);
608 if (invalid_)
609 border->SetColor(kWarningColor);
610 set_border(border);
611 }
612
378 void Combobox::AdjustBoundsForRTLUI(gfx::Rect* rect) const { 613 void Combobox::AdjustBoundsForRTLUI(gfx::Rect* rect) const {
379 rect->set_x(GetMirroredXForRect(*rect)); 614 rect->set_x(GetMirroredXForRect(*rect));
380 } 615 }
381 616
382 void Combobox::PaintText(gfx::Canvas* canvas) { 617 void Combobox::PaintText(gfx::Canvas* canvas) {
383 gfx::Insets insets = GetInsets(); 618 gfx::Insets insets = GetInsets();
384 619
385 canvas->Save(); 620 gfx::ScopedCanvas scoped_canvas(canvas);
386 canvas->ClipRect(GetContentsBounds()); 621 canvas->ClipRect(GetContentsBounds());
387 622
388 int x = insets.left(); 623 int x = insets.left();
389 int y = insets.top(); 624 int y = insets.top();
390 int text_height = height() - insets.height(); 625 int text_height = height() - insets.height();
391 SkColor text_color = invalid() ? kInvalidTextColor : 626 SkColor text_color = invalid() ? kInvalidTextColor :
392 GetNativeTheme()->GetSystemColor( 627 GetNativeTheme()->GetSystemColor(
393 ui::NativeTheme::kColorId_LabelEnabledColor); 628 ui::NativeTheme::kColorId_LabelEnabledColor);
394 629
395 DCHECK_GE(selected_index_, 0); 630 DCHECK_GE(selected_index_, 0);
396 DCHECK_LT(selected_index_, model()->GetItemCount()); 631 DCHECK_LT(selected_index_, model()->GetItemCount());
397 if (selected_index_ < 0 || selected_index_ > model()->GetItemCount()) 632 if (selected_index_ < 0 || selected_index_ > model()->GetItemCount())
398 selected_index_ = 0; 633 selected_index_ = 0;
399 string16 text = model()->GetItemAt(selected_index_); 634 string16 text = model()->GetItemAt(selected_index_);
400 635
401 int disclosure_arrow_offset = width() - disclosure_arrow_->width() 636 int disclosure_arrow_offset = width() - disclosure_arrow_->width() -
402 - kDisclosureArrowLeftPadding - kDisclosureArrowRightPadding; 637 GetDisclosureArrowLeftPadding() - GetDisclosureArrowRightPadding();
403 638
404 const gfx::Font& font = Combobox::GetFont(); 639 const gfx::Font& font = Combobox::GetFont();
405 int text_width = font.GetStringWidth(text); 640 int text_width = font.GetStringWidth(text);
406 if ((text_width + insets.width()) > disclosure_arrow_offset) 641 if ((text_width + insets.width()) > disclosure_arrow_offset)
407 text_width = disclosure_arrow_offset - insets.width(); 642 text_width = disclosure_arrow_offset - insets.width();
408 643
409 gfx::Rect text_bounds(x, y, text_width, text_height); 644 gfx::Rect text_bounds(x, y, text_width, text_height);
410 AdjustBoundsForRTLUI(&text_bounds); 645 AdjustBoundsForRTLUI(&text_bounds);
411 canvas->DrawStringInt(text, font, text_color, text_bounds); 646 canvas->DrawStringInt(text, font, text_color, text_bounds);
412 647
413 gfx::Rect arrow_bounds(disclosure_arrow_offset + kDisclosureArrowLeftPadding, 648 int arrow_x = disclosure_arrow_offset + GetDisclosureArrowLeftPadding();
649 gfx::Rect arrow_bounds(arrow_x,
414 height() / 2 - disclosure_arrow_->height() / 2, 650 height() / 2 - disclosure_arrow_->height() / 2,
415 disclosure_arrow_->width(), 651 disclosure_arrow_->width(),
416 disclosure_arrow_->height()); 652 disclosure_arrow_->height());
417 AdjustBoundsForRTLUI(&arrow_bounds); 653 AdjustBoundsForRTLUI(&arrow_bounds);
418 654
419 SkPaint paint; 655 SkPaint paint;
420 // This makes the arrow subtractive. 656 // This makes the arrow subtractive.
421 if (invalid()) 657 if (invalid())
422 paint.setXfermodeMode(SkXfermode::kDstOut_Mode); 658 paint.setXfermodeMode(SkXfermode::kDstOut_Mode);
423 canvas->DrawImageInt(*disclosure_arrow_, arrow_bounds.x(), arrow_bounds.y(), 659 canvas->DrawImageInt(*disclosure_arrow_, arrow_bounds.x(), arrow_bounds.y(),
424 paint); 660 paint);
661 }
425 662
426 canvas->Restore(); 663 void Combobox::PaintButtons(gfx::Canvas* canvas) {
664 DCHECK(style_ == STYLE_NOTIFY_ON_CLICK);
665
666 gfx::ScopedCanvas scoped_canvas(canvas);
667 if (base::i18n::IsRTL()) {
668 canvas->Translate(gfx::Vector2d(width(), 0));
669 canvas->Scale(-1, 1);
670 }
671
672 bool focused = HasFocus();
673 const std::vector<const gfx::ImageSkia*>& arrow_button_images =
674 menu_button_images_[focused][
675 arrow_button_->state() == Button::STATE_HOVERED ?
676 Button::STATE_NORMAL : arrow_button_->state()];
677
678 int text_button_hover_alpha =
679 text_button_->state() == Button::STATE_PRESSED ? 0 :
680 static_cast<int>(static_cast<TransparentButton*>(text_button_)->
681 GetAnimationValue() * 255);
682 if (text_button_hover_alpha < 255) {
683 canvas->SaveLayerAlpha(255 - text_button_hover_alpha);
684 Painter* text_button_painter =
685 body_button_painters_[focused][
686 text_button_->state() == Button::STATE_HOVERED ?
687 Button::STATE_NORMAL : text_button_->state()].get();
688 Painter::PaintPainterAt(canvas, text_button_painter,
689 gfx::Rect(0, 0, text_button_->width(), height()));
690 canvas->Restore();
691 }
692 if (0 < text_button_hover_alpha) {
693 canvas->SaveLayerAlpha(text_button_hover_alpha);
694 Painter* text_button_hovered_painter =
695 body_button_painters_[focused][Button::STATE_HOVERED].get();
696 Painter::PaintPainterAt(canvas, text_button_hovered_painter,
697 gfx::Rect(0, 0, text_button_->width(), height()));
698 canvas->Restore();
699 }
700
701 int arrow_button_hover_alpha =
702 arrow_button_->state() == Button::STATE_PRESSED ? 0 :
703 static_cast<int>(static_cast<TransparentButton*>(arrow_button_)->
704 GetAnimationValue() * 255);
705 if (arrow_button_hover_alpha < 255) {
706 canvas->SaveLayerAlpha(255 - arrow_button_hover_alpha);
707 PaintArrowButton(canvas, arrow_button_images, arrow_button_->x(), height());
708 canvas->Restore();
709 }
710 if (0 < arrow_button_hover_alpha) {
711 canvas->SaveLayerAlpha(arrow_button_hover_alpha);
712 const std::vector<const gfx::ImageSkia*>& arrow_button_hovered_images =
713 menu_button_images_[focused][Button::STATE_HOVERED];
714 PaintArrowButton(canvas, arrow_button_hovered_images,
715 arrow_button_->x(), height());
716 canvas->Restore();
717 }
427 } 718 }
428 719
429 void Combobox::ShowDropDownMenu(ui::MenuSourceType source_type) { 720 void Combobox::ShowDropDownMenu(ui::MenuSourceType source_type) {
430 if (!dropdown_list_menu_runner_.get()) 721 if (!dropdown_list_menu_runner_.get())
431 UpdateFromModel(); 722 UpdateFromModel();
432 723
433 // Extend the menu to the width of the combobox. 724 // Extend the menu to the width of the combobox.
434 MenuItemView* menu = dropdown_list_menu_runner_->GetMenu(); 725 MenuItemView* menu = dropdown_list_menu_runner_->GetMenu();
435 SubmenuView* submenu = menu->CreateSubmenu(); 726 SubmenuView* submenu = menu->CreateSubmenu();
436 submenu->set_minimum_preferred_width(size().width() - 727 submenu->set_minimum_preferred_width(
437 (kMenuBorderWidthLeft + kMenuBorderWidthRight)); 728 size().width() - (kMenuBorderWidthLeft + kMenuBorderWidthRight));
438 729
439 gfx::Rect lb = GetLocalBounds(); 730 gfx::Rect lb = GetLocalBounds();
440 gfx::Point menu_position(lb.origin()); 731 gfx::Point menu_position(lb.origin());
441 732
442 // Inset the menu's requested position so the border of the menu lines up 733 // Inset the menu's requested position so the border of the menu lines up
443 // with the border of the combobox. 734 // with the border of the combobox.
444 menu_position.set_x(menu_position.x() + kMenuBorderWidthLeft); 735 menu_position.set_x(menu_position.x() + kMenuBorderWidthLeft);
445 menu_position.set_y(menu_position.y() + kMenuBorderWidthTop); 736 menu_position.set_y(menu_position.y() + kMenuBorderWidthTop);
446 lb.set_width(lb.width() - (kMenuBorderWidthLeft + kMenuBorderWidthRight)); 737 lb.set_width(lb.width() - (kMenuBorderWidthLeft + kMenuBorderWidthRight));
447 738
448 View::ConvertPointToScreen(this, &menu_position); 739 View::ConvertPointToScreen(this, &menu_position);
449 if (menu_position.x() < 0) 740 if (menu_position.x() < 0)
450 menu_position.set_x(0); 741 menu_position.set_x(0);
451 742
452 gfx::Rect bounds(menu_position, lb.size()); 743 gfx::Rect bounds(menu_position, lb.size());
453 744
745 Button::ButtonState original_state;
746 if (arrow_button_) {
747 original_state = arrow_button_->state();
748 arrow_button_->SetState(Button::STATE_PRESSED);
749 }
454 dropdown_open_ = true; 750 dropdown_open_ = true;
455 if (dropdown_list_menu_runner_->RunMenuAt(GetWidget(), NULL, bounds, 751 if (dropdown_list_menu_runner_->RunMenuAt(GetWidget(), NULL, bounds,
456 MenuItemView::TOPLEFT, source_type, MenuRunner::COMBOBOX) == 752 MenuItemView::TOPLEFT, source_type,
457 MenuRunner::MENU_DELETED) 753 MenuRunner::COMBOBOX) ==
754 MenuRunner::MENU_DELETED) {
458 return; 755 return;
756 }
459 dropdown_open_ = false; 757 dropdown_open_ = false;
758 if (arrow_button_)
759 arrow_button_->SetState(original_state);
460 closed_time_ = base::Time::Now(); 760 closed_time_ = base::Time::Now();
461 761
462 // Need to explicitly clear mouse handler so that events get sent 762 // Need to explicitly clear mouse handler so that events get sent
463 // properly after the menu finishes running. If we don't do this, then 763 // properly after the menu finishes running. If we don't do this, then
464 // the first click to other parts of the UI is eaten. 764 // the first click to other parts of the UI is eaten.
465 SetMouseHandler(NULL); 765 SetMouseHandler(NULL);
466 } 766 }
467 767
468 void Combobox::OnSelectionChanged() { 768 void Combobox::OnSelectionChanged() {
469 NotifyAccessibilityEvent(ui::AccessibilityTypes::EVENT_VALUE_CHANGED, false); 769 NotifyAccessibilityEvent(ui::AccessibilityTypes::EVENT_VALUE_CHANGED, false);
470 SchedulePaint(); 770 SchedulePaint();
471 if (listener_) 771 if (listener_)
472 listener_->OnSelectedIndexChanged(this); 772 listener_->OnSelectedIndexChanged(this);
473 // |this| may now be deleted. 773 // |this| may now be deleted.
474 } 774 }
475 775
476 int Combobox::MenuCommandToIndex(int menu_command_id) const { 776 int Combobox::MenuCommandToIndex(int menu_command_id) const {
477 // (note that the id received is offset by kFirstMenuItemId) 777 // (note that the id received is offset by kFirstMenuItemId)
478 // Revert menu ID offset to map back to combobox model. 778 // Revert menu ID offset to map back to combobox model.
479 int index = menu_command_id - kFirstMenuItemId; 779 int index = menu_command_id - kFirstMenuItemId;
480 DCHECK_LT(index, model()->GetItemCount()); 780 DCHECK_LT(index, model()->GetItemCount());
481 return index; 781 return index;
482 } 782 }
483 783
784 int Combobox::GetDisclosureArrowLeftPadding() const {
785 switch (style_) {
786 case STYLE_SHOW_DROP_DOWN_ON_CLICK:
787 return kDisclosureArrowLeftPadding;
788 case STYLE_NOTIFY_ON_CLICK:
789 return kDisclosureArrowButtonLeftPadding;
790 }
791 NOTREACHED();
792 return 0;
793 }
794
795 int Combobox::GetDisclosureArrowRightPadding() const {
796 switch (style_) {
797 case STYLE_SHOW_DROP_DOWN_ON_CLICK:
798 return kDisclosureArrowRightPadding;
799 case STYLE_NOTIFY_ON_CLICK:
800 return kDisclosureArrowButtonRightPadding;
801 }
802 NOTREACHED();
803 return 0;
804 }
805
806 void Combobox::HandleClickEvent() {
807 if (style_ != STYLE_NOTIFY_ON_CLICK)
808 return;
809
810 if (listener_)
811 listener_->OnComboboxTextButtonClicked(this);
812 }
813
484 } // namespace views 814 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698