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

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: rebasing 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/slide_animation.h"
15 #include "ui/gfx/canvas.h" 16 #include "ui/gfx/canvas.h"
17 #include "ui/gfx/image/image.h"
16 #include "ui/native_theme/native_theme.h" 18 #include "ui/native_theme/native_theme.h"
17 #include "ui/views/color_constants.h" 19 #include "ui/views/color_constants.h"
20 #include "ui/views/controls/button/custom_button.h"
18 #include "ui/views/controls/combobox/combobox_listener.h" 21 #include "ui/views/controls/combobox/combobox_listener.h"
19 #include "ui/views/controls/focusable_border.h" 22 #include "ui/views/controls/focusable_border.h"
20 #include "ui/views/controls/menu/menu_runner.h" 23 #include "ui/views/controls/menu/menu_runner.h"
21 #include "ui/views/controls/menu/submenu_view.h" 24 #include "ui/views/controls/menu/submenu_view.h"
22 #include "ui/views/controls/prefix_selector.h" 25 #include "ui/views/controls/prefix_selector.h"
23 #include "ui/views/ime/input_method.h" 26 #include "ui/views/ime/input_method.h"
24 #include "ui/views/mouse_constants.h" 27 #include "ui/views/mouse_constants.h"
28 #include "ui/views/painter.h"
25 #include "ui/views/widget/widget.h" 29 #include "ui/views/widget/widget.h"
26 30
27 namespace views { 31 namespace views {
28 32
29 namespace { 33 namespace {
30 34
31 // Menu border widths 35 // Menu border widths
32 const int kMenuBorderWidthLeft = 1; 36 const int kMenuBorderWidthLeft = 1;
33 const int kMenuBorderWidthTop = 1; 37 const int kMenuBorderWidthTop = 1;
34 const int kMenuBorderWidthRight = 1; 38 const int kMenuBorderWidthRight = 1;
35 39
36 // Limit how small a combobox can be. 40 // Limit how small a combobox can be.
37 const int kMinComboboxWidth = 25; 41 const int kMinComboboxWidth = 25;
38 42
39 // Size of the combobox arrow margins 43 // Size of the combobox arrow margins
40 const int kDisclosureArrowLeftPadding = 7; 44 const int kDisclosureArrowLeftPadding = 7;
41 const int kDisclosureArrowRightPadding = 7; 45 const int kDisclosureArrowRightPadding = 7;
46 const int kDisclosureArrowButtonLeftPadding = 11;
47 const int kDisclosureArrowButtonRightPadding = 12;
42 48
43 // Define the id of the first item in the menu (since it needs to be > 0) 49 // Define the id of the first item in the menu (since it needs to be > 0)
44 const int kFirstMenuItemId = 1000; 50 const int kFirstMenuItemId = 1000;
45 51
46 const SkColor kInvalidTextColor = SK_ColorWHITE; 52 const SkColor kInvalidTextColor = SK_ColorWHITE;
47 53
48 // Used to indicate that no item is currently selected by the user. 54 // Used to indicate that no item is currently selected by the user.
49 const int kNoSelection = -1; 55 const int kNoSelection = -1;
50 56
57 const int kBodyButtonImages[] = IMAGE_GRID(IDR_COMBOBOX_BUTTON);
58 const int kHoveredBodyButtonImages[] = IMAGE_GRID2(IDR_COMBOBOX_BUTTON, H);
59 const int kPressedBodyButtonImages[] = IMAGE_GRID2(IDR_COMBOBOX_BUTTON, P);
60
61 #define MENU_IMAGE_GRID(x) { \
62 x ## _MENU_TOP, x ## _MENU_CENTER, x ## _MENU_BOTTOM, }
63 #define MENU_IMAGE_GRID2(x, y) { \
64 x ## _MENU_TOP_ ## y, x ## _MENU_CENTER_ ## y, x ## _MENU_BOTTOM_ ## y, }
65
66 const int kMenuButtonImages[] = MENU_IMAGE_GRID(IDR_COMBOBOX_BUTTON);
67 const int kHoveredMenuButtonImages[] = MENU_IMAGE_GRID2(IDR_COMBOBOX_BUTTON, H);
68 const int kPressedMenuButtonImages[] = MENU_IMAGE_GRID2(IDR_COMBOBOX_BUTTON, P);
69
70 #undef MENU_IMAGE_GRID
71 #undef MENU_IMAGE_GRID2
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.
(...skipping 13 matching lines...) Expand all
74 96
75 index += increment; 97 index += increment;
76 while (index >= 0 && index < model->GetItemCount()) { 98 while (index >= 0 && index < model->GetItemCount()) {
77 if (!model->IsItemSeparatorAt(index) || !model->IsItemEnabledAt(index)) 99 if (!model->IsItemSeparatorAt(index) || !model->IsItemEnabledAt(index))
78 return index; 100 return index;
79 index += increment; 101 index += increment;
80 } 102 }
81 return kNoSelection; 103 return kNoSelection;
82 } 104 }
83 105
106 // Returns the image resource ids of an array for the body button.
107 //
108 // TODO(hajimehoshi): This function should return the images for the 'disabled'
109 // status. (crbug/270052)
110 //
111 // TODO(hajimehoshi): Currently, |focused| is ignored. This should return the
112 // images for the 'focused' status. (crbug/270052)
113 const int* GetBodyButtonImageIds(bool focused, Button::ButtonState state) {
114 switch (state) {
115 case Button::STATE_DISABLED:
116 return kBodyButtonImages;
117 case Button::STATE_NORMAL:
118 return kBodyButtonImages;
119 case Button::STATE_HOVERED:
120 return kHoveredBodyButtonImages;
121 case Button::STATE_PRESSED:
122 return kPressedBodyButtonImages;
123 default:
124 NOTREACHED();
125 }
126 return NULL;
127 }
128
129 // Returns the image resource ids of an array for the menu button.
130 const int* GetMenuButtonImageIds(bool focused, Button::ButtonState state) {
131 switch (state) {
132 case Button::STATE_DISABLED:
133 return kMenuButtonImages;
134 case Button::STATE_NORMAL:
135 return kMenuButtonImages;
136 case Button::STATE_HOVERED:
137 return kHoveredMenuButtonImages;
138 case Button::STATE_PRESSED:
139 return kPressedMenuButtonImages;
140 default:
141 NOTREACHED();
142 }
143 return NULL;
144 }
145
146 // Returns the images for the buttons.
147 std::vector<const gfx::ImageSkia*> GetButtonImages(bool menu,
148 bool focused,
149 Button::ButtonState state) {
150 const int* ids;
151 size_t num_ids;
152 if (!menu) {
153 ids = GetBodyButtonImageIds(focused, state);
154 num_ids = 9;
155 } else {
156 ids = GetMenuButtonImageIds(focused, state);
157 num_ids = 3;
158 }
159 std::vector<const gfx::ImageSkia*> images;
160 images.reserve(num_ids);
161 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
162 for (size_t i = 0; i < num_ids; i++) {
163 int id = ids[i];
164 images.push_back(rb.GetImageSkiaNamed(id));
165 }
166 return images;
167 }
168
169 // Paints three images in a row at the given location.
170 void PaintTandemImages(gfx::Canvas* canvas,
171 const gfx::ImageSkia& top_image,
172 const gfx::ImageSkia& center_image,
173 const gfx::ImageSkia& bottom_image,
174 int x, int y, int width, int height) {
175 canvas->DrawImageInt(top_image,
176 0, 0, top_image.width(), top_image.height(),
177 x, y, width, top_image.height(), false);
178 y += top_image.height();
179 int center_height = height - top_image.height() - bottom_image.height();
180 canvas->DrawImageInt(center_image,
181 0, 0, center_image.width(), center_image.height(),
182 x, y, width, center_height, false);
183 y += center_height;
184 canvas->DrawImageInt(bottom_image,
185 0, 0, bottom_image.width(), bottom_image.height(),
186 x, y, width, bottom_image.height(), false);
187 }
188
189 // Paints the text button.
190 void PaintTextButton(
191 gfx::Canvas* canvas,
192 const std::vector<const gfx::ImageSkia*>& text_button_images,
193 int width, int height) {
194 int current_x = 0;
195 int current_width = text_button_images[0]->width();
196 PaintTandemImages(canvas,
197 *text_button_images[0],
198 *text_button_images[3],
199 *text_button_images[6],
200 current_x, 0, current_width, height);
201
202 current_x += current_width;
203 current_width = width - text_button_images[0]->width() -
204 text_button_images[2]->width();
205 PaintTandemImages(canvas,
206 *text_button_images[1],
207 *text_button_images[4],
208 *text_button_images[7],
209 current_x, 0, current_width, height);
210
211 current_x += current_width;
212 current_width = text_button_images[2]->width();
213 PaintTandemImages(canvas,
214 *text_button_images[2],
215 *text_button_images[5],
216 *text_button_images[8],
217 current_x, 0, current_width, height);
218 }
219
220 // Paints the arrow button.
221 void PaintArrowButton(
222 gfx::Canvas* canvas,
223 const std::vector<const gfx::ImageSkia*>& arrow_button_images,
224 int x, int height) {
225 PaintTandemImages(canvas,
226 *arrow_button_images[0],
227 *arrow_button_images[1],
228 *arrow_button_images[2],
229 x, 0, arrow_button_images[0]->width(), height);
230 }
231
84 } // namespace 232 } // namespace
85 233
86 // static 234 // static
87 const char Combobox::kViewClassName[] = "views/Combobox"; 235 const char Combobox::kViewClassName[] = "views/Combobox";
88 236
89 //////////////////////////////////////////////////////////////////////////////// 237 ////////////////////////////////////////////////////////////////////////////////
90 // Combobox, public: 238 // Combobox, public:
91 239
92 Combobox::Combobox(ui::ComboboxModel* model) 240 Combobox::Combobox(ui::ComboboxModel* model)
93 : model_(model), 241 : model_(model),
242 style_(STYLE_NORMAL),
94 listener_(NULL), 243 listener_(NULL),
95 selected_index_(model_->GetDefaultIndex()), 244 selected_index_(model_->GetDefaultIndex()),
96 invalid_(false), 245 invalid_(false),
97 text_border_(new FocusableBorder()), 246 text_border_(new FocusableBorder()),
98 disclosure_arrow_(ui::ResourceBundle::GetSharedInstance().GetImageNamed( 247 disclosure_arrow_(ui::ResourceBundle::GetSharedInstance().GetImageNamed(
99 IDR_MENU_DROPARROW).ToImageSkia()), 248 IDR_MENU_DROPARROW).ToImageSkia()),
100 dropdown_open_(false) { 249 dropdown_open_(false),
250 drag_started_(false),
251 text_button_state_(Button::STATE_NORMAL),
252 arrow_button_state_(Button::STATE_NORMAL) {
101 model_->AddObserver(this); 253 model_->AddObserver(this);
102 UpdateFromModel(); 254 UpdateFromModel();
103 set_focusable(true); 255 set_focusable(true);
104 set_border(text_border_); 256 set_border(text_border_);
257
258 // Initialize the button images.
259 Button::ButtonState button_states[] = {
260 Button::STATE_DISABLED,
261 Button::STATE_NORMAL,
262 Button::STATE_HOVERED,
263 Button::STATE_PRESSED,
264 };
265 for (int focused = 0; focused < 2; focused++) {
266 for (size_t state_index = 0; state_index < arraysize(button_states);
267 state_index++) {
268 Button::ButtonState state = button_states[state_index];
269 body_button_images_[focused][state] =
270 GetButtonImages(false, focused, state);
271 menu_button_images_[focused][state] =
272 GetButtonImages(true, focused, state);
273 }
274 }
275
276 text_button_hover_animation_.reset(new gfx::SlideAnimation(this));
277 text_button_hover_animation_->SetSlideDuration(
278 CustomButton::kHoverFadeDurationMs);
279 arrow_button_hover_animation_.reset(new gfx::SlideAnimation(this));
280 arrow_button_hover_animation_->SetSlideDuration(
281 CustomButton::kHoverFadeDurationMs);
105 } 282 }
106 283
107 Combobox::~Combobox() { 284 Combobox::~Combobox() {
108 model_->RemoveObserver(this); 285 model_->RemoveObserver(this);
109 } 286 }
110 287
111 // static 288 // static
112 const gfx::Font& Combobox::GetFont() { 289 const gfx::Font& Combobox::GetFont() {
113 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); 290 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
114 return rb.GetFont(ui::ResourceBundle::BaseFont); 291 return rb.GetFont(ui::ResourceBundle::BaseFont);
115 } 292 }
116 293
294 void Combobox::SetStyle(Style style) {
295 if (style_ == style)
296 return;
297
298 style_ = style;
299 switch (style) {
300 case STYLE_NORMAL:
301 text_border_->ResetInsets();
302 break;
303 case STYLE_BUTTONS:
304 text_border_->SetInsets(8, 13, 8, 13);
305 break;
306 }
307 PreferredSizeChanged();
308 }
309
117 void Combobox::ModelChanged() { 310 void Combobox::ModelChanged() {
118 selected_index_ = std::min(0, model_->GetItemCount()); 311 selected_index_ = std::min(0, model_->GetItemCount());
119 UpdateFromModel(); 312 UpdateFromModel();
120 PreferredSizeChanged(); 313 PreferredSizeChanged();
121 } 314 }
122 315
123 void Combobox::SetSelectedIndex(int index) { 316 void Combobox::SetSelectedIndex(int index) {
124 selected_index_ = index; 317 selected_index_ = index;
125 SchedulePaint(); 318 SchedulePaint();
126 } 319 }
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
194 // Combobox, View overrides: 387 // Combobox, View overrides:
195 388
196 gfx::Size Combobox::GetPreferredSize() { 389 gfx::Size Combobox::GetPreferredSize() {
197 if (content_size_.IsEmpty()) 390 if (content_size_.IsEmpty())
198 UpdateFromModel(); 391 UpdateFromModel();
199 392
200 // The preferred size will drive the local bounds which in turn is used to set 393 // The preferred size will drive the local bounds which in turn is used to set
201 // the minimum width for the dropdown list. 394 // the minimum width for the dropdown list.
202 gfx::Insets insets = GetInsets(); 395 gfx::Insets insets = GetInsets();
203 int total_width = std::max(kMinComboboxWidth, content_size_.width()) + 396 int total_width = std::max(kMinComboboxWidth, content_size_.width()) +
204 insets.width() + kDisclosureArrowLeftPadding + 397 insets.width() + GetDisclosureArrowLeftPadding() +
205 disclosure_arrow_->width() + kDisclosureArrowRightPadding; 398 disclosure_arrow_->width() + GetDisclosureArrowRightPadding();
206
207 return gfx::Size(total_width, content_size_.height() + insets.height()); 399 return gfx::Size(total_width, content_size_.height() + insets.height());
208 } 400 }
209 401
210 const char* Combobox::GetClassName() const { 402 const char* Combobox::GetClassName() const {
211 return kViewClassName; 403 return kViewClassName;
212 } 404 }
213 405
214 bool Combobox::SkipDefaultKeyEventProcessing(const ui::KeyEvent& e) { 406 bool Combobox::SkipDefaultKeyEventProcessing(const ui::KeyEvent& e) {
215 // Escape should close the drop down list when it is active, not host UI. 407 // Escape should close the drop down list when it is active, not host UI.
216 if (e.key_code() != ui::VKEY_ESCAPE || 408 if (e.key_code() != ui::VKEY_ESCAPE ||
217 e.IsShiftDown() || e.IsControlDown() || e.IsAltDown()) { 409 e.IsShiftDown() || e.IsControlDown() || e.IsAltDown()) {
218 return false; 410 return false;
219 } 411 }
220 return dropdown_open_; 412 return dropdown_open_;
221 } 413 }
222 414
223 bool Combobox::OnMousePressed(const ui::MouseEvent& mouse_event) { 415 bool Combobox::OnMousePressed(const ui::MouseEvent& mouse_event) {
224 RequestFocus(); 416 RequestFocus();
225 const base::TimeDelta delta = base::Time::Now() - closed_time_; 417 const base::TimeDelta delta = base::Time::Now() - closed_time_;
226 if (mouse_event.IsLeftMouseButton() && 418 if (mouse_event.IsLeftMouseButton() &&
227 (delta.InMilliseconds() > kMinimumMsBetweenButtonClicks)) { 419 (delta.InMilliseconds() > kMinimumMsBetweenButtonClicks)) {
228 UpdateFromModel(); 420 if (InArrowButton(mouse_event.location())) {
229 ShowDropDownMenu(ui::MENU_SOURCE_MOUSE); 421 UpdateFromModel();
422 ShowDropDownMenu(ui::MENU_SOURCE_MOUSE);
423 } else if (InTextButton(mouse_event.location())) {
424 SetButtonStates(Button::STATE_PRESSED, Button::STATE_NORMAL);
425 }
230 } 426 }
231
232 return true; 427 return true;
233 } 428 }
234 429
235 bool Combobox::OnMouseDragged(const ui::MouseEvent& mouse_event) { 430 bool Combobox::OnMouseDragged(const ui::MouseEvent& mouse_event) {
431 if (!drag_started_) {
432 drag_started_ = true;
433 drag_start_point_ = mouse_event.location();
434 }
435 bool on_text = InTextButton(mouse_event.location()) &&
436 InTextButton(drag_start_point_);
437 bool on_arrow = InArrowButton(mouse_event.location()) &&
438 InArrowButton(drag_start_point_);
439 SetButtonStates(on_text ? Button::STATE_PRESSED : Button::STATE_NORMAL,
440 on_arrow ? Button::STATE_PRESSED : Button::STATE_NORMAL);
236 return true; 441 return true;
237 } 442 }
238 443
444 void Combobox::OnMouseReleased(const ui::MouseEvent& mouse_event) {
445 bool on_text = InTextButton(mouse_event.location());
446 bool on_arrow = InArrowButton(mouse_event.location());
447 SetButtonStates(on_text ? Button::STATE_HOVERED : Button::STATE_NORMAL,
448 on_arrow ? Button::STATE_HOVERED : Button::STATE_NORMAL);
449 if (mouse_event.IsOnlyLeftMouseButton() &&
450 InTextButton(mouse_event.location())) {
451 HandleClickEvent();
452 }
453 }
454
455 void Combobox::OnMouseCaptureLost() {
456 drag_started_ = false;
457 SetButtonStates(Button::STATE_NORMAL, Button::STATE_NORMAL);
458 }
459
460 void Combobox::OnMouseEntered(const ui::MouseEvent& mouse_event) {
461 bool on_text = InTextButton(mouse_event.location());
462 bool on_arrow = InArrowButton(mouse_event.location());
463 SetButtonStates(on_text ? Button::STATE_HOVERED : Button::STATE_NORMAL,
464 on_arrow ? Button::STATE_HOVERED : Button::STATE_NORMAL);
465 }
466
467 void Combobox::OnMouseExited(const ui::MouseEvent& mouse_event) {
468 SetButtonStates(Button::STATE_NORMAL, Button::STATE_NORMAL);
469 }
470
471 void Combobox::OnMouseMoved(const ui::MouseEvent& mouse_event) {
472 bool on_text = InTextButton(mouse_event.location());
473 bool on_arrow = InArrowButton(mouse_event.location());
474 SetButtonStates(on_text ? Button::STATE_HOVERED : Button::STATE_NORMAL,
475 on_arrow ? Button::STATE_HOVERED : Button::STATE_NORMAL);
476 }
477
239 bool Combobox::OnKeyPressed(const ui::KeyEvent& e) { 478 bool Combobox::OnKeyPressed(const ui::KeyEvent& e) {
240 // TODO(oshima): handle IME. 479 // TODO(oshima): handle IME.
241 DCHECK_EQ(e.type(), ui::ET_KEY_PRESSED); 480 DCHECK_EQ(e.type(), ui::ET_KEY_PRESSED);
242 481
243 DCHECK_GE(selected_index_, 0); 482 DCHECK_GE(selected_index_, 0);
244 DCHECK_LT(selected_index_, model()->GetItemCount()); 483 DCHECK_LT(selected_index_, model()->GetItemCount());
245 if (selected_index_ < 0 || selected_index_ > model()->GetItemCount()) 484 if (selected_index_ < 0 || selected_index_ > model()->GetItemCount())
246 selected_index_ = 0; 485 selected_index_ = 0;
247 486
248 bool show_menu = false; 487 bool show_menu = false;
(...skipping 24 matching lines...) Expand all
273 case ui::VKEY_HOME: 512 case ui::VKEY_HOME:
274 case ui::VKEY_PRIOR: // Page up. 513 case ui::VKEY_PRIOR: // Page up.
275 new_index = GetAdjacentIndex(model(), 1, -1); 514 new_index = GetAdjacentIndex(model(), 1, -1);
276 break; 515 break;
277 516
278 // Move to the previous item if any. 517 // Move to the previous item if any.
279 case ui::VKEY_UP: 518 case ui::VKEY_UP:
280 new_index = GetAdjacentIndex(model(), -1, selected_index_); 519 new_index = GetAdjacentIndex(model(), -1, selected_index_);
281 break; 520 break;
282 521
522 // Click the button only when the button style mode.
523 case ui::VKEY_SPACE:
524 SetButtonStates(Button::STATE_PRESSED, Button::STATE_NORMAL);
525 // When pressing space, the click event will be raised after the key is
526 // released.
527 break;
528
529 // Click the button only when the button style mode.
530 case ui::VKEY_RETURN:
531 SetButtonStates(Button::STATE_NORMAL, Button::STATE_NORMAL);
532 HandleClickEvent();
533 break;
534
283 default: 535 default:
284 return false; 536 return false;
285 } 537 }
286 538
287 if (show_menu) { 539 if (show_menu) {
288 UpdateFromModel(); 540 UpdateFromModel();
289 ShowDropDownMenu(ui::MENU_SOURCE_KEYBOARD); 541 ShowDropDownMenu(ui::MENU_SOURCE_KEYBOARD);
290 } else if (new_index != selected_index_ && new_index != kNoSelection) { 542 } else if (new_index != selected_index_ && new_index != kNoSelection) {
291 DCHECK(!model()->IsItemSeparatorAt(new_index)); 543 DCHECK(!model()->IsItemSeparatorAt(new_index));
292 selected_index_ = new_index; 544 selected_index_ = new_index;
293 OnSelectionChanged(); 545 OnSelectionChanged();
294 } 546 }
295 547
296 return true; 548 return true;
297 } 549 }
298 550
299 bool Combobox::OnKeyReleased(const ui::KeyEvent& e) { 551 bool Combobox::OnKeyReleased(const ui::KeyEvent& e) {
300 return false; // crbug.com/127520 552 if (style_ != STYLE_BUTTONS)
553 return false; // crbug.com/127520
554
555 if (e.key_code() == ui::VKEY_SPACE) {
556 SetButtonStates(Button::STATE_NORMAL, Button::STATE_NORMAL);
557 HandleClickEvent();
558 }
559 return false;
301 } 560 }
302 561
303 void Combobox::OnGestureEvent(ui::GestureEvent* gesture) { 562 void Combobox::OnGestureEvent(ui::GestureEvent* gesture) {
304 if (gesture->type() == ui::ET_GESTURE_TAP) { 563 if (gesture->type() == ui::ET_GESTURE_TAP) {
305 UpdateFromModel(); 564 if (InArrowButton(gesture->location())) {
306 ShowDropDownMenu(ui::MENU_SOURCE_TOUCH); 565 UpdateFromModel();
566 ShowDropDownMenu(ui::MENU_SOURCE_TOUCH);
567 } else if (InTextButton(gesture->location())) {
568 SetButtonStates(Button::STATE_PRESSED, Button::STATE_NORMAL);
569 HandleClickEvent();
570 }
307 gesture->StopPropagation(); 571 gesture->StopPropagation();
308 return; 572 return;
309 } 573 }
310 View::OnGestureEvent(gesture); 574 View::OnGestureEvent(gesture);
311 } 575 }
312 576
313 void Combobox::OnPaint(gfx::Canvas* canvas) { 577 void Combobox::OnPaint(gfx::Canvas* canvas) {
314 OnPaintBackground(canvas); 578 switch (style_) {
315 PaintText(canvas); 579 case STYLE_NORMAL: {
316 OnPaintBorder(canvas); 580 OnPaintBackground(canvas);
581 PaintText(canvas);
582 OnPaintBorder(canvas);
583 break;
584 }
585 case STYLE_BUTTONS: {
586 PaintButtons(canvas);
587 PaintText(canvas);
588 break;
589 }
590 }
317 } 591 }
318 592
319 void Combobox::OnFocus() { 593 void Combobox::OnFocus() {
320 GetInputMethod()->OnFocus(); 594 GetInputMethod()->OnFocus();
321 text_border_->set_has_focus(true); 595 text_border_->set_has_focus(true);
322 View::OnFocus(); 596 View::OnFocus();
323 } 597 }
324 598
325 void Combobox::OnBlur() { 599 void Combobox::OnBlur() {
326 GetInputMethod()->OnBlur(); 600 GetInputMethod()->OnBlur();
327 if (selector_) 601 if (selector_)
328 selector_->OnViewBlur(); 602 selector_->OnViewBlur();
329 text_border_->set_has_focus(false); 603 text_border_->set_has_focus(false);
330 } 604 }
331 605
332 void Combobox::GetAccessibleState(ui::AccessibleViewState* state) { 606 void Combobox::GetAccessibleState(ui::AccessibleViewState* state) {
333 state->role = ui::AccessibilityTypes::ROLE_COMBOBOX; 607 state->role = ui::AccessibilityTypes::ROLE_COMBOBOX;
334 state->name = accessible_name_; 608 state->name = accessible_name_;
335 state->value = model_->GetItemAt(selected_index_); 609 state->value = model_->GetItemAt(selected_index_);
336 state->index = selected_index_; 610 state->index = selected_index_;
337 state->count = model_->GetItemCount(); 611 state->count = model_->GetItemCount();
338 } 612 }
339 613
340 void Combobox::OnModelChanged() { 614 void Combobox::OnModelChanged() {
341 ModelChanged(); 615 ModelChanged();
342 } 616 }
343 617
618 void Combobox::AnimationProgressed(const gfx::Animation* animation) {
619 SchedulePaint();
620 }
621
344 void Combobox::UpdateFromModel() { 622 void Combobox::UpdateFromModel() {
345 int max_width = 0; 623 int max_width = 0;
346 const gfx::Font& font = Combobox::GetFont(); 624 const gfx::Font& font = Combobox::GetFont();
347 625
348 MenuItemView* menu = new MenuItemView(this); 626 MenuItemView* menu = new MenuItemView(this);
349 // MenuRunner owns |menu|. 627 // MenuRunner owns |menu|.
350 dropdown_list_menu_runner_.reset(new MenuRunner(menu)); 628 dropdown_list_menu_runner_.reset(new MenuRunner(menu));
351 629
352 int num_items = model()->GetItemCount(); 630 int num_items = model()->GetItemCount();
353 for (int i = 0; i < num_items; ++i) { 631 for (int i = 0; i < num_items; ++i) {
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
385 SkColor text_color = invalid() ? kInvalidTextColor : 663 SkColor text_color = invalid() ? kInvalidTextColor :
386 GetNativeTheme()->GetSystemColor( 664 GetNativeTheme()->GetSystemColor(
387 ui::NativeTheme::kColorId_LabelEnabledColor); 665 ui::NativeTheme::kColorId_LabelEnabledColor);
388 666
389 DCHECK_GE(selected_index_, 0); 667 DCHECK_GE(selected_index_, 0);
390 DCHECK_LT(selected_index_, model()->GetItemCount()); 668 DCHECK_LT(selected_index_, model()->GetItemCount());
391 if (selected_index_ < 0 || selected_index_ > model()->GetItemCount()) 669 if (selected_index_ < 0 || selected_index_ > model()->GetItemCount())
392 selected_index_ = 0; 670 selected_index_ = 0;
393 string16 text = model()->GetItemAt(selected_index_); 671 string16 text = model()->GetItemAt(selected_index_);
394 672
395 int disclosure_arrow_offset = width() - disclosure_arrow_->width() 673 int disclosure_arrow_offset = width() - disclosure_arrow_->width() -
396 - kDisclosureArrowLeftPadding - kDisclosureArrowRightPadding; 674 GetDisclosureArrowLeftPadding() - GetDisclosureArrowRightPadding();
397 675
398 const gfx::Font& font = Combobox::GetFont(); 676 const gfx::Font& font = Combobox::GetFont();
399 int text_width = font.GetStringWidth(text); 677 int text_width = font.GetStringWidth(text);
400 if ((text_width + insets.width()) > disclosure_arrow_offset) 678 if ((text_width + insets.width()) > disclosure_arrow_offset)
401 text_width = disclosure_arrow_offset - insets.width(); 679 text_width = disclosure_arrow_offset - insets.width();
402 680
403 gfx::Rect text_bounds(x, y, text_width, text_height); 681 gfx::Rect text_bounds(x, y, text_width, text_height);
404 AdjustBoundsForRTLUI(&text_bounds); 682 AdjustBoundsForRTLUI(&text_bounds);
405 canvas->DrawStringInt(text, font, text_color, text_bounds); 683 canvas->DrawStringInt(text, font, text_color, text_bounds);
406 684
407 gfx::Rect arrow_bounds(disclosure_arrow_offset + kDisclosureArrowLeftPadding, 685 int arrow_x = disclosure_arrow_offset + GetDisclosureArrowLeftPadding();
686 gfx::Rect arrow_bounds(arrow_x,
408 height() / 2 - disclosure_arrow_->height() / 2, 687 height() / 2 - disclosure_arrow_->height() / 2,
409 disclosure_arrow_->width(), 688 disclosure_arrow_->width(),
410 disclosure_arrow_->height()); 689 disclosure_arrow_->height());
411 AdjustBoundsForRTLUI(&arrow_bounds); 690 AdjustBoundsForRTLUI(&arrow_bounds);
412 691
413 SkPaint paint; 692 SkPaint paint;
414 // This makes the arrow subtractive. 693 // This makes the arrow subtractive.
415 if (invalid()) 694 if (invalid())
416 paint.setXfermodeMode(SkXfermode::kDstOut_Mode); 695 paint.setXfermodeMode(SkXfermode::kDstOut_Mode);
417 canvas->DrawImageInt(*disclosure_arrow_, arrow_bounds.x(), arrow_bounds.y(), 696 canvas->DrawImageInt(*disclosure_arrow_, arrow_bounds.x(), arrow_bounds.y(),
418 paint); 697 paint);
419 698
420 canvas->Restore(); 699 canvas->Restore();
421 } 700 }
422 701
702 void Combobox::PaintButtons(gfx::Canvas* canvas) {
703 canvas->Save();
704 if (base::i18n::IsRTL()) {
705 canvas->Translate(gfx::Vector2d(width(), 0));
706 canvas->Scale(-1, 1);
707 }
708
709 bool focused = HasFocus() && IsAccessibilityFocusable();
710 const std::vector<const gfx::ImageSkia*>& text_button_images =
711 body_button_images_[focused][text_button_state_ == Button::STATE_HOVERED ?
712 Button::STATE_NORMAL : text_button_state_];
713 const std::vector<const gfx::ImageSkia*>& arrow_button_images =
714 menu_button_images_[focused][
715 arrow_button_state_ == Button::STATE_HOVERED ?
716 Button::STATE_NORMAL : arrow_button_state_];
717
718 int text_button_width = width() - arrow_button_images[0]->width();
719 int text_button_hover_alpha =
720 static_cast<int>(text_button_hover_animation_->GetCurrentValue() * 255);
721 if (text_button_hover_alpha < 255) {
722 canvas->SaveLayerAlpha(255 - text_button_hover_alpha);
723 PaintTextButton(canvas, text_button_images, text_button_width, height());
724 canvas->Restore();
725 }
726 if (0 < text_button_hover_alpha) {
727 canvas->SaveLayerAlpha(text_button_hover_alpha);
728 const std::vector<const gfx::ImageSkia*>& text_button_hovered_images =
729 body_button_images_[focused][Button::STATE_HOVERED];
730 PaintTextButton(canvas, text_button_hovered_images,
731 text_button_width, height());
732 canvas->Restore();
733 }
734
735 int arrow_button_x = text_button_width;
736 int arrow_button_hover_alpha =
737 static_cast<int>(arrow_button_hover_animation_->GetCurrentValue() * 255);
738 if (arrow_button_hover_alpha < 255) {
739 canvas->SaveLayerAlpha(255 - arrow_button_hover_alpha);
740 PaintArrowButton(canvas, arrow_button_images, arrow_button_x, height());
741 canvas->Restore();
742 }
743 if (0 < arrow_button_hover_alpha) {
744 canvas->SaveLayerAlpha(arrow_button_hover_alpha);
745 const std::vector<const gfx::ImageSkia*>& arrow_button_hovered_images =
746 menu_button_images_[focused][Button::STATE_HOVERED];
747 PaintArrowButton(canvas, arrow_button_hovered_images,
748 arrow_button_x, height());
749 canvas->Restore();
750 }
751
752 canvas->Restore();
753 }
754
423 void Combobox::ShowDropDownMenu(ui::MenuSourceType source_type) { 755 void Combobox::ShowDropDownMenu(ui::MenuSourceType source_type) {
424 if (!dropdown_list_menu_runner_.get()) 756 if (!dropdown_list_menu_runner_.get())
425 UpdateFromModel(); 757 UpdateFromModel();
426 758
427 // Extend the menu to the width of the combobox. 759 // Extend the menu to the width of the combobox.
428 MenuItemView* menu = dropdown_list_menu_runner_->GetMenu(); 760 MenuItemView* menu = dropdown_list_menu_runner_->GetMenu();
429 SubmenuView* submenu = menu->CreateSubmenu(); 761 SubmenuView* submenu = menu->CreateSubmenu();
430 submenu->set_minimum_preferred_width(size().width() - 762 submenu->set_minimum_preferred_width(
431 (kMenuBorderWidthLeft + kMenuBorderWidthRight)); 763 size().width() - (kMenuBorderWidthLeft + kMenuBorderWidthRight));
432 764
433 gfx::Rect lb = GetLocalBounds(); 765 gfx::Rect lb = GetLocalBounds();
434 gfx::Point menu_position(lb.origin()); 766 gfx::Point menu_position(lb.origin());
435 767
436 // Inset the menu's requested position so the border of the menu lines up 768 // Inset the menu's requested position so the border of the menu lines up
437 // with the border of the combobox. 769 // with the border of the combobox.
438 menu_position.set_x(menu_position.x() + kMenuBorderWidthLeft); 770 menu_position.set_x(menu_position.x() + kMenuBorderWidthLeft);
439 menu_position.set_y(menu_position.y() + kMenuBorderWidthTop); 771 menu_position.set_y(menu_position.y() + kMenuBorderWidthTop);
440 lb.set_width(lb.width() - (kMenuBorderWidthLeft + kMenuBorderWidthRight)); 772 lb.set_width(lb.width() - (kMenuBorderWidthLeft + kMenuBorderWidthRight));
441 773
442 View::ConvertPointToScreen(this, &menu_position); 774 View::ConvertPointToScreen(this, &menu_position);
443 if (menu_position.x() < 0) 775 if (menu_position.x() < 0)
444 menu_position.set_x(0); 776 menu_position.set_x(0);
445 777
446 gfx::Rect bounds(menu_position, lb.size()); 778 gfx::Rect bounds(menu_position, lb.size());
447 779
448 dropdown_open_ = true; 780 dropdown_open_ = true;
781 Button::ButtonState original_arrow_button_state = arrow_button_state_;
782 SetButtonStates(Button::STATE_NORMAL, Button::STATE_PRESSED);
449 if (dropdown_list_menu_runner_->RunMenuAt(GetWidget(), NULL, bounds, 783 if (dropdown_list_menu_runner_->RunMenuAt(GetWidget(), NULL, bounds,
450 MenuItemView::TOPLEFT, source_type, MenuRunner::COMBOBOX) == 784 MenuItemView::TOPLEFT, source_type, MenuRunner::COMBOBOX) ==
451 MenuRunner::MENU_DELETED) 785 MenuRunner::MENU_DELETED) {
452 return; 786 return;
787 }
788 SetButtonStates(Button::STATE_NORMAL, original_arrow_button_state);
453 dropdown_open_ = false; 789 dropdown_open_ = false;
454 closed_time_ = base::Time::Now(); 790 closed_time_ = base::Time::Now();
455 791
456 // Need to explicitly clear mouse handler so that events get sent 792 // Need to explicitly clear mouse handler so that events get sent
457 // properly after the menu finishes running. If we don't do this, then 793 // properly after the menu finishes running. If we don't do this, then
458 // the first click to other parts of the UI is eaten. 794 // the first click to other parts of the UI is eaten.
459 SetMouseHandler(NULL); 795 SetMouseHandler(NULL);
460 } 796 }
461 797
462 void Combobox::OnSelectionChanged() { 798 void Combobox::OnSelectionChanged() {
463 if (listener_) 799 if (listener_)
464 listener_->OnSelectedIndexChanged(this); 800 listener_->OnSelectedIndexChanged(this);
465 NotifyAccessibilityEvent(ui::AccessibilityTypes::EVENT_VALUE_CHANGED, false); 801 NotifyAccessibilityEvent(ui::AccessibilityTypes::EVENT_VALUE_CHANGED, false);
466 SchedulePaint(); 802 SchedulePaint();
467 } 803 }
468 804
469 int Combobox::MenuCommandToIndex(int menu_command_id) const { 805 int Combobox::MenuCommandToIndex(int menu_command_id) const {
470 // (note that the id received is offset by kFirstMenuItemId) 806 // (note that the id received is offset by kFirstMenuItemId)
471 // Revert menu ID offset to map back to combobox model. 807 // Revert menu ID offset to map back to combobox model.
472 int index = menu_command_id - kFirstMenuItemId; 808 int index = menu_command_id - kFirstMenuItemId;
473 DCHECK_LT(index, model()->GetItemCount()); 809 DCHECK_LT(index, model()->GetItemCount());
474 return index; 810 return index;
475 } 811 }
476 812
813 int Combobox::GetDisclosureArrowLeftPadding() const {
814 switch (style_) {
815 case STYLE_NORMAL:
816 return kDisclosureArrowLeftPadding;
817 case STYLE_BUTTONS:
818 return kDisclosureArrowButtonLeftPadding;
819 }
820 NOTREACHED();
821 return 0;
822 }
823
824 int Combobox::GetDisclosureArrowRightPadding() const {
825 switch (style_) {
826 case STYLE_NORMAL:
827 return kDisclosureArrowRightPadding;
828 case STYLE_BUTTONS:
829 return kDisclosureArrowButtonRightPadding;
830 }
831 NOTREACHED();
832 return 0;
833 }
834
835 void Combobox::HandleClickEvent() {
836 if (style_ != STYLE_BUTTONS)
837 return;
838
839 if (listener_)
840 listener_->OnTextButtonClicked(this);
841 }
842
843 void Combobox::SetButtonStates(Button::ButtonState text_button_state,
844 Button::ButtonState arrow_button_state) {
845 if (style_ != STYLE_BUTTONS)
846 return;
847
848 AnimateStateTransition(text_button_hover_animation_.get(),
849 text_button_state_, text_button_state);
850 AnimateStateTransition(arrow_button_hover_animation_.get(),
851 arrow_button_state_, arrow_button_state);
852
853 text_button_state_ = text_button_state;
854 arrow_button_state_ = arrow_button_state;
855 SchedulePaint();
856 }
857
858 void Combobox::AnimateStateTransition(gfx::SlideAnimation* animation,
859 Button::ButtonState from,
860 Button::ButtonState to) {
861 if (from == Button::STATE_NORMAL && to == Button::STATE_HOVERED)
862 animation->Show();
863 else if (from == Button::STATE_HOVERED && to == Button::STATE_NORMAL)
864 animation->Hide();
865 else if (from != to)
866 animation->Reset(to == Button::STATE_HOVERED ? 1.0 : 0.0);
867 }
868
869 bool Combobox::InTextButton(const gfx::Point& point) {
870 if (!HitTestPoint(point))
871 return false;
872
873 if (style_ != STYLE_BUTTONS)
874 return false;
875
876 int x = point.x();
877 const gfx::ImageSkia* menu_button_image =
878 menu_button_images_[false][Button::STATE_NORMAL][0];
879 return x < width() - menu_button_image->width();
880 }
881
882 bool Combobox::InArrowButton(const gfx::Point& point) {
883 if (!HitTestPoint(point))
884 return false;
885
886 // When |style_| is not the button style, whole of this control functions as a
887 // disclosure arrow button.
888 if (style_ != STYLE_BUTTONS)
889 return true;
890
891 int x = point.x();
892 const gfx::ImageSkia* menu_button_image =
893 menu_button_images_[false][Button::STATE_NORMAL][0];
894 return width() - menu_button_image->width() <= x && x < width();
895 }
896
477 } // namespace views 897 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698