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

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

Issue 141523005: Combobox: Rename styles to STYLE_NORMAL and STYLE_ACTION and modify behaviors (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix around selected_index_ Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/views/controls/combobox/combobox.h ('k') | ui/views/controls/combobox/combobox_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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"
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after
271 return rb.GetFontList(ui::ResourceBundle::BaseFont); 271 return rb.GetFontList(ui::ResourceBundle::BaseFont);
272 } 272 }
273 273
274 void Combobox::SetStyle(Style style) { 274 void Combobox::SetStyle(Style style) {
275 if (style_ == style) 275 if (style_ == style)
276 return; 276 return;
277 277
278 style_ = style; 278 style_ = style;
279 279
280 UpdateBorder(); 280 UpdateBorder();
281 UpdateFromModel();
281 PreferredSizeChanged(); 282 PreferredSizeChanged();
282 } 283 }
283 284
284 void Combobox::ModelChanged() { 285 void Combobox::ModelChanged() {
285 selected_index_ = std::min(0, model_->GetItemCount()); 286 selected_index_ = std::min(0, model_->GetItemCount());
286 UpdateFromModel(); 287 UpdateFromModel();
287 PreferredSizeChanged(); 288 PreferredSizeChanged();
288 } 289 }
289 290
290 void Combobox::SetSelectedIndex(int index) { 291 void Combobox::SetSelectedIndex(int index) {
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
351 return false; 352 return false;
352 } 353 }
353 354
354 bool Combobox::IsCommandEnabled(int id) const { 355 bool Combobox::IsCommandEnabled(int id) const {
355 return model()->IsItemEnabledAt(MenuCommandToIndex(id)); 356 return model()->IsItemEnabledAt(MenuCommandToIndex(id));
356 } 357 }
357 358
358 void Combobox::ExecuteCommand(int id) { 359 void Combobox::ExecuteCommand(int id) {
359 selected_index_ = MenuCommandToIndex(id); 360 selected_index_ = MenuCommandToIndex(id);
360 OnSelectionChanged(); 361 OnSelectionChanged();
362 if (style_ == STYLE_NOTIFY_ON_CLICK)
363 HandleClickEvent(CLICK_EVENT_SOURCE_MENU_ITEM);
361 } 364 }
362 365
363 bool Combobox::GetAccelerator(int id, ui::Accelerator* accel) { 366 bool Combobox::GetAccelerator(int id, ui::Accelerator* accel) {
364 return false; 367 return false;
365 } 368 }
366 369
367 int Combobox::GetRowCount() { 370 int Combobox::GetRowCount() {
368 return model()->GetItemCount(); 371 return model()->GetItemCount();
369 } 372 }
370 373
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
462 text_button_->SetState(Button::STATE_PRESSED); 465 text_button_->SetState(Button::STATE_PRESSED);
463 } else { 466 } else {
464 return false; 467 return false;
465 } 468 }
466 break; 469 break;
467 470
468 // Click the button only when the button style mode. 471 // Click the button only when the button style mode.
469 case ui::VKEY_RETURN: 472 case ui::VKEY_RETURN:
470 if (style_ != STYLE_NOTIFY_ON_CLICK) 473 if (style_ != STYLE_NOTIFY_ON_CLICK)
471 return false; 474 return false;
472 HandleClickEvent(); 475 HandleClickEvent(CLICK_EVENT_SOURCE_KEYBOARD);
473 break; 476 break;
474 477
475 default: 478 default:
476 return false; 479 return false;
477 } 480 }
478 481
479 if (show_menu) { 482 if (show_menu) {
480 UpdateFromModel(); 483 UpdateFromModel();
481 ShowDropDownMenu(ui::MENU_SOURCE_KEYBOARD); 484 ShowDropDownMenu(ui::MENU_SOURCE_KEYBOARD);
482 } else if (new_index != selected_index_ && new_index != kNoSelection) { 485 } else if (new_index != selected_index_ && new_index != kNoSelection) {
483 DCHECK(!model()->IsItemSeparatorAt(new_index)); 486 DCHECK(!model()->IsItemSeparatorAt(new_index));
484 selected_index_ = new_index; 487 selected_index_ = new_index;
485 OnSelectionChanged(); 488 OnSelectionChanged();
486 } 489 }
487 490
488 return true; 491 return true;
489 } 492 }
490 493
491 bool Combobox::OnKeyReleased(const ui::KeyEvent& e) { 494 bool Combobox::OnKeyReleased(const ui::KeyEvent& e) {
492 if (style_ != STYLE_NOTIFY_ON_CLICK) 495 if (style_ != STYLE_NOTIFY_ON_CLICK)
493 return false; // crbug.com/127520 496 return false; // crbug.com/127520
494 497
495 if (e.key_code() == ui::VKEY_SPACE) 498 if (e.key_code() == ui::VKEY_SPACE && style_ == STYLE_NOTIFY_ON_CLICK)
496 HandleClickEvent(); 499 HandleClickEvent(CLICK_EVENT_SOURCE_KEYBOARD);
497 500
498 return false; 501 return false;
499 } 502 }
500 503
501 void Combobox::OnPaint(gfx::Canvas* canvas) { 504 void Combobox::OnPaint(gfx::Canvas* canvas) {
502 switch (style_) { 505 switch (style_) {
503 case STYLE_SHOW_DROP_DOWN_ON_CLICK: { 506 case STYLE_SHOW_DROP_DOWN_ON_CLICK: {
504 OnPaintBackground(canvas); 507 OnPaintBackground(canvas);
505 PaintText(canvas); 508 PaintText(canvas);
506 OnPaintBorder(canvas); 509 OnPaintBorder(canvas);
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
539 542
540 void Combobox::OnComboboxModelChanged(ui::ComboboxModel* model) { 543 void Combobox::OnComboboxModelChanged(ui::ComboboxModel* model) {
541 DCHECK_EQ(model, model_); 544 DCHECK_EQ(model, model_);
542 ModelChanged(); 545 ModelChanged();
543 } 546 }
544 547
545 void Combobox::ButtonPressed(Button* sender, const ui::Event& event) { 548 void Combobox::ButtonPressed(Button* sender, const ui::Event& event) {
546 RequestFocus(); 549 RequestFocus();
547 550
548 if (sender == text_button_) { 551 if (sender == text_button_) {
549 HandleClickEvent(); 552 HandleClickEvent(CLICK_EVENT_SOURCE_MOUSE);
550 } else { 553 } else {
551 DCHECK_EQ(arrow_button_, sender); 554 DCHECK_EQ(arrow_button_, sender);
552 // TODO(hajimehoshi): Fix the problem that the arrow button blinks when 555 // TODO(hajimehoshi): Fix the problem that the arrow button blinks when
553 // cliking this while the dropdown menu is opened. 556 // cliking this while the dropdown menu is opened.
554 const base::TimeDelta delta = base::Time::Now() - closed_time_; 557 const base::TimeDelta delta = base::Time::Now() - closed_time_;
555 if (delta.InMilliseconds() <= kMinimumMsBetweenButtonClicks) 558 if (delta.InMilliseconds() <= kMinimumMsBetweenButtonClicks)
556 return; 559 return;
557 560
558 ui::MenuSourceType source_type = ui::MENU_SOURCE_MOUSE; 561 ui::MenuSourceType source_type = ui::MENU_SOURCE_MOUSE;
559 if (event.IsKeyEvent()) 562 if (event.IsKeyEvent())
560 source_type = ui::MENU_SOURCE_KEYBOARD; 563 source_type = ui::MENU_SOURCE_KEYBOARD;
561 else if (event.IsGestureEvent() || event.IsTouchEvent()) 564 else if (event.IsGestureEvent() || event.IsTouchEvent())
562 source_type = ui::MENU_SOURCE_TOUCH; 565 source_type = ui::MENU_SOURCE_TOUCH;
563 ShowDropDownMenu(source_type); 566 ShowDropDownMenu(source_type);
564 } 567 }
565 } 568 }
566 569
567 void Combobox::UpdateFromModel() { 570 void Combobox::UpdateFromModel() {
568 int max_width = 0;
569 const gfx::FontList& font_list = Combobox::GetFontList(); 571 const gfx::FontList& font_list = Combobox::GetFontList();
570 572
571 MenuItemView* menu = new MenuItemView(this); 573 MenuItemView* menu = new MenuItemView(this);
572 // MenuRunner owns |menu|. 574 // MenuRunner owns |menu|.
573 dropdown_list_menu_runner_.reset(new MenuRunner(menu)); 575 dropdown_list_menu_runner_.reset(new MenuRunner(menu));
574 576
575 int num_items = model()->GetItemCount(); 577 int num_items = model()->GetItemCount();
578 int width = 0;
576 for (int i = 0; i < num_items; ++i) { 579 for (int i = 0; i < num_items; ++i) {
577 if (model()->IsItemSeparatorAt(i)) { 580 if (model()->IsItemSeparatorAt(i)) {
578 menu->AppendSeparator(); 581 menu->AppendSeparator();
579 continue; 582 continue;
580 } 583 }
581 584
582 base::string16 text = model()->GetItemAt(i); 585 base::string16 text = model()->GetItemAt(i);
583 586
584 // Inserting the Unicode formatting characters if necessary so that the 587 // Inserting the Unicode formatting characters if necessary so that the
585 // text is displayed correctly in right-to-left UIs. 588 // text is displayed correctly in right-to-left UIs.
586 base::i18n::AdjustStringForLocaleDirection(&text); 589 base::i18n::AdjustStringForLocaleDirection(&text);
587 590
588 menu->AppendMenuItem(i + kFirstMenuItemId, text, MenuItemView::NORMAL); 591 menu->AppendMenuItem(i + kFirstMenuItemId, text, MenuItemView::NORMAL);
589 max_width = std::max(max_width, gfx::GetStringWidth(text, font_list)); 592 if (style_ != STYLE_NOTIFY_ON_CLICK || i == 0)
sky 2014/01/23 15:40:25 Based on your description I think this should use
hajimehoshi 2014/01/24 04:10:56 Done.
593 width = std::max(width, gfx::GetStringWidth(text, font_list));
590 } 594 }
591 595
592 content_size_.SetSize(max_width, font_list.GetHeight()); 596 content_size_.SetSize(width, font_list.GetHeight());
593 } 597 }
594 598
595 void Combobox::UpdateBorder() { 599 void Combobox::UpdateBorder() {
596 FocusableBorder* border = new FocusableBorder(); 600 FocusableBorder* border = new FocusableBorder();
597 if (style_ == STYLE_NOTIFY_ON_CLICK) 601 if (style_ == STYLE_NOTIFY_ON_CLICK)
598 border->SetInsets(8, 13, 8, 13); 602 border->SetInsets(8, 13, 8, 13);
599 if (invalid_) 603 if (invalid_)
600 border->SetColor(kWarningColor); 604 border->SetColor(kWarningColor);
601 set_border(border); 605 set_border(border);
602 } 606 }
(...skipping 11 matching lines...) Expand all
614 int x = insets.left(); 618 int x = insets.left();
615 int y = insets.top(); 619 int y = insets.top();
616 int text_height = height() - insets.height(); 620 int text_height = height() - insets.height();
617 SkColor text_color = GetNativeTheme()->GetSystemColor( 621 SkColor text_color = GetNativeTheme()->GetSystemColor(
618 ui::NativeTheme::kColorId_LabelEnabledColor); 622 ui::NativeTheme::kColorId_LabelEnabledColor);
619 623
620 DCHECK_GE(selected_index_, 0); 624 DCHECK_GE(selected_index_, 0);
621 DCHECK_LT(selected_index_, model()->GetItemCount()); 625 DCHECK_LT(selected_index_, model()->GetItemCount());
622 if (selected_index_ < 0 || selected_index_ > model()->GetItemCount()) 626 if (selected_index_ < 0 || selected_index_ > model()->GetItemCount())
623 selected_index_ = 0; 627 selected_index_ = 0;
624 base::string16 text = model()->GetItemAt(selected_index_); 628 base::string16 text;
629 if (style_ == STYLE_SHOW_DROP_DOWN_ON_CLICK)
sky 2014/01/23 15:40:25 This shouldn't be needed. If a consumer wants to f
hajimehoshi 2014/01/24 04:10:56 Done.
630 text = model()->GetItemAt(selected_index_);
631 else
632 text = model()->GetItemAt(0);
625 633
626 int disclosure_arrow_offset = width() - disclosure_arrow_->width() - 634 int disclosure_arrow_offset = width() - disclosure_arrow_->width() -
627 GetDisclosureArrowLeftPadding() - GetDisclosureArrowRightPadding(); 635 GetDisclosureArrowLeftPadding() - GetDisclosureArrowRightPadding();
628 636
629 const gfx::FontList& font_list = Combobox::GetFontList(); 637 const gfx::FontList& font_list = Combobox::GetFontList();
630 int text_width = gfx::GetStringWidth(text, font_list); 638 int text_width = gfx::GetStringWidth(text, font_list);
631 if ((text_width + insets.width()) > disclosure_arrow_offset) 639 if ((text_width + insets.width()) > disclosure_arrow_offset)
632 text_width = disclosure_arrow_offset - insets.width(); 640 text_width = disclosure_arrow_offset - insets.width();
633 641
634 gfx::Rect text_bounds(x, y, text_width, text_height); 642 gfx::Rect text_bounds(x, y, text_width, text_height);
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
781 switch (style_) { 789 switch (style_) {
782 case STYLE_SHOW_DROP_DOWN_ON_CLICK: 790 case STYLE_SHOW_DROP_DOWN_ON_CLICK:
783 return kDisclosureArrowRightPadding; 791 return kDisclosureArrowRightPadding;
784 case STYLE_NOTIFY_ON_CLICK: 792 case STYLE_NOTIFY_ON_CLICK:
785 return kDisclosureArrowButtonRightPadding; 793 return kDisclosureArrowButtonRightPadding;
786 } 794 }
787 NOTREACHED(); 795 NOTREACHED();
788 return 0; 796 return 0;
789 } 797 }
790 798
791 void Combobox::HandleClickEvent() { 799 void Combobox::HandleClickEvent(ClickEventSource source) {
792 if (style_ != STYLE_NOTIFY_ON_CLICK) 800 if (style_ != STYLE_NOTIFY_ON_CLICK)
793 return; 801 return;
794 802
803 if (source != CLICK_EVENT_SOURCE_MENU_ITEM)
sky 2014/01/23 15:40:25 Same comment as above. Your code should change sel
hajimehoshi 2014/01/24 04:10:56 Done.
804 selected_index_ = 0;
805
795 if (listener_) 806 if (listener_)
796 listener_->OnComboboxTextButtonClicked(this); 807 listener_->OnComboboxTextButtonClicked(this);
797 } 808 }
798 809
799 } // namespace views 810 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/controls/combobox/combobox.h ('k') | ui/views/controls/combobox/combobox_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698