Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 47 namespace { | 47 namespace { |
| 48 | 48 |
| 49 // Menu border widths | 49 // Menu border widths |
| 50 const int kMenuBorderWidthLeft = 1; | 50 const int kMenuBorderWidthLeft = 1; |
| 51 const int kMenuBorderWidthTop = 1; | 51 const int kMenuBorderWidthTop = 1; |
| 52 const int kMenuBorderWidthRight = 1; | 52 const int kMenuBorderWidthRight = 1; |
| 53 | 53 |
| 54 // Limit how small a combobox can be. | 54 // Limit how small a combobox can be. |
| 55 const int kMinComboboxWidth = 25; | 55 const int kMinComboboxWidth = 25; |
| 56 | 56 |
| 57 // Size of the combobox arrow margins | |
| 58 const int kDisclosureArrowLeftPadding = 7; | |
| 59 const int kDisclosureArrowRightPadding = 7; | |
| 60 const int kDisclosureArrowButtonLeftPadding = 11; | |
| 61 const int kDisclosureArrowButtonRightPadding = 12; | |
| 62 | |
| 63 // Define the id of the first item in the menu (since it needs to be > 0) | 57 // Define the id of the first item in the menu (since it needs to be > 0) |
| 64 const int kFirstMenuItemId = 1000; | 58 const int kFirstMenuItemId = 1000; |
| 65 | 59 |
| 66 // Used to indicate that no item is currently selected by the user. | 60 // Used to indicate that no item is currently selected by the user. |
| 67 const int kNoSelection = -1; | 61 const int kNoSelection = -1; |
| 68 | 62 |
| 69 const int kBodyButtonImages[] = IMAGE_GRID(IDR_COMBOBOX_BUTTON); | 63 const int kBodyButtonImages[] = IMAGE_GRID(IDR_COMBOBOX_BUTTON); |
| 70 const int kHoveredBodyButtonImages[] = IMAGE_GRID(IDR_COMBOBOX_BUTTON_H); | 64 const int kHoveredBodyButtonImages[] = IMAGE_GRID(IDR_COMBOBOX_BUTTON_H); |
| 71 const int kPressedBodyButtonImages[] = IMAGE_GRID(IDR_COMBOBOX_BUTTON_P); | 65 const int kPressedBodyButtonImages[] = IMAGE_GRID(IDR_COMBOBOX_BUTTON_P); |
| 72 const int kFocusedBodyButtonImages[] = IMAGE_GRID(IDR_COMBOBOX_BUTTON_F); | 66 const int kFocusedBodyButtonImages[] = IMAGE_GRID(IDR_COMBOBOX_BUTTON_F); |
| (...skipping 382 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 455 if (invalid == invalid_) | 449 if (invalid == invalid_) |
| 456 return; | 450 return; |
| 457 | 451 |
| 458 invalid_ = invalid; | 452 invalid_ = invalid; |
| 459 | 453 |
| 460 UpdateBorder(); | 454 UpdateBorder(); |
| 461 SchedulePaint(); | 455 SchedulePaint(); |
| 462 } | 456 } |
| 463 | 457 |
| 464 int Combobox::GetArrowButtonWidth() const { | 458 int Combobox::GetArrowButtonWidth() const { |
| 465 return GetDisclosureArrowLeftPadding() + | 459 return PlatformStyle::GetComboboxArrowWidth(style_); |
|
tapted
2016/04/21 06:06:46
Maybe "ShoulderWidth"
Elly Fong-Jones
2016/04/21 15:30:26
Done.
| |
| 466 ArrowSize().width() + | |
| 467 GetDisclosureArrowRightPadding(); | |
| 468 } | 460 } |
| 469 | 461 |
| 470 void Combobox::Layout() { | 462 void Combobox::Layout() { |
| 471 PrefixDelegate::Layout(); | 463 PrefixDelegate::Layout(); |
| 472 | 464 |
| 473 gfx::Insets insets = GetInsets(); | 465 gfx::Insets insets = GetInsets(); |
| 474 int text_button_width = 0; | 466 int text_button_width = 0; |
| 475 int arrow_button_width = 0; | 467 int arrow_button_width = 0; |
| 476 | 468 |
| 477 switch (style_) { | 469 switch (style_) { |
| 478 case STYLE_NORMAL: { | 470 case STYLE_NORMAL: { |
| 479 arrow_button_width = width(); | 471 arrow_button_width = width(); |
| 480 break; | 472 break; |
| 481 } | 473 } |
| 482 case STYLE_ACTION: { | 474 case STYLE_ACTION: { |
| 483 arrow_button_width = GetDisclosureArrowLeftPadding() + | 475 arrow_button_width = GetArrowButtonWidth(); |
| 484 ArrowSize().width() + | |
| 485 GetDisclosureArrowRightPadding(); | |
| 486 text_button_width = width() - arrow_button_width; | 476 text_button_width = width() - arrow_button_width; |
| 487 break; | 477 break; |
| 488 } | 478 } |
| 489 } | 479 } |
| 490 | 480 |
| 491 int arrow_button_x = std::max(0, text_button_width); | 481 int arrow_button_x = std::max(0, text_button_width); |
| 492 text_button_->SetBounds(0, 0, std::max(0, text_button_width), height()); | 482 text_button_->SetBounds(0, 0, std::max(0, text_button_width), height()); |
| 493 arrow_button_->SetBounds(arrow_button_x, 0, arrow_button_width, height()); | 483 arrow_button_->SetBounds(arrow_button_x, 0, arrow_button_width, height()); |
| 494 } | 484 } |
| 495 | 485 |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 518 | 508 |
| 519 gfx::Size Combobox::GetPreferredSize() const { | 509 gfx::Size Combobox::GetPreferredSize() const { |
| 520 // The preferred size will drive the local bounds which in turn is used to set | 510 // The preferred size will drive the local bounds which in turn is used to set |
| 521 // the minimum width for the dropdown list. | 511 // the minimum width for the dropdown list. |
| 522 gfx::Insets insets = GetInsets(); | 512 gfx::Insets insets = GetInsets(); |
| 523 insets += gfx::Insets(Textfield::kTextPadding, | 513 insets += gfx::Insets(Textfield::kTextPadding, |
| 524 Textfield::kTextPadding, | 514 Textfield::kTextPadding, |
| 525 Textfield::kTextPadding, | 515 Textfield::kTextPadding, |
| 526 Textfield::kTextPadding); | 516 Textfield::kTextPadding); |
| 527 int total_width = std::max(kMinComboboxWidth, content_size_.width()) + | 517 int total_width = std::max(kMinComboboxWidth, content_size_.width()) + |
| 528 insets.width() + GetDisclosureArrowLeftPadding() + | 518 insets.width() + GetArrowButtonWidth(); |
| 529 ArrowSize().width() + GetDisclosureArrowRightPadding(); | |
| 530 return gfx::Size(total_width, content_size_.height() + insets.height()); | 519 return gfx::Size(total_width, content_size_.height() + insets.height()); |
| 531 } | 520 } |
| 532 | 521 |
| 533 const char* Combobox::GetClassName() const { | 522 const char* Combobox::GetClassName() const { |
| 534 return kViewClassName; | 523 return kViewClassName; |
| 535 } | 524 } |
| 536 | 525 |
| 537 bool Combobox::SkipDefaultKeyEventProcessing(const ui::KeyEvent& e) { | 526 bool Combobox::SkipDefaultKeyEventProcessing(const ui::KeyEvent& e) { |
| 538 // Escape should close the drop down list when it is active, not host UI. | 527 // Escape should close the drop down list when it is active, not host UI. |
| 539 if (e.key_code() != ui::VKEY_ESCAPE || | 528 if (e.key_code() != ui::VKEY_ESCAPE || |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 726 SkColor text_color = GetNativeTheme()->GetSystemColor( | 715 SkColor text_color = GetNativeTheme()->GetSystemColor( |
| 727 enabled() ? ui::NativeTheme::kColorId_LabelEnabledColor : | 716 enabled() ? ui::NativeTheme::kColorId_LabelEnabledColor : |
| 728 ui::NativeTheme::kColorId_LabelDisabledColor); | 717 ui::NativeTheme::kColorId_LabelDisabledColor); |
| 729 | 718 |
| 730 DCHECK_GE(selected_index_, 0); | 719 DCHECK_GE(selected_index_, 0); |
| 731 DCHECK_LT(selected_index_, model()->GetItemCount()); | 720 DCHECK_LT(selected_index_, model()->GetItemCount()); |
| 732 if (selected_index_ < 0 || selected_index_ > model()->GetItemCount()) | 721 if (selected_index_ < 0 || selected_index_ > model()->GetItemCount()) |
| 733 selected_index_ = 0; | 722 selected_index_ = 0; |
| 734 base::string16 text = model()->GetItemAt(selected_index_); | 723 base::string16 text = model()->GetItemAt(selected_index_); |
| 735 | 724 |
| 736 gfx::Size arrow_size = ArrowSize(); | 725 int disclosure_arrow_offset = width() - GetArrowButtonWidth(); |
| 737 int disclosure_arrow_offset = width() - arrow_size.width() - | |
| 738 GetDisclosureArrowLeftPadding() - GetDisclosureArrowRightPadding(); | |
| 739 | 726 |
| 740 const gfx::FontList& font_list = Combobox::GetFontList(); | 727 const gfx::FontList& font_list = Combobox::GetFontList(); |
| 741 int text_width = gfx::GetStringWidth(text, font_list); | 728 int text_width = gfx::GetStringWidth(text, font_list); |
| 742 if ((text_width + insets.width()) > disclosure_arrow_offset) | 729 if ((text_width + insets.width()) > disclosure_arrow_offset) |
| 743 text_width = disclosure_arrow_offset - insets.width(); | 730 text_width = disclosure_arrow_offset - insets.width(); |
| 744 | 731 |
| 745 gfx::Rect text_bounds(x, y, text_width, text_height); | 732 gfx::Rect text_bounds(x, y, text_width, text_height); |
| 746 AdjustBoundsForRTLUI(&text_bounds); | 733 AdjustBoundsForRTLUI(&text_bounds); |
| 747 canvas->DrawStringRect(text, font_list, text_color, text_bounds); | 734 canvas->DrawStringRect(text, font_list, text_color, text_bounds); |
| 748 | 735 |
| 749 int arrow_x = disclosure_arrow_offset + GetDisclosureArrowLeftPadding(); | 736 gfx::ImageSkia arrow_image = PlatformStyle::CreateComboboxArrow( |
| 750 gfx::Rect arrow_bounds(arrow_x, | 737 enabled(), style_); |
| 751 height() / 2 - arrow_size.height() / 2, | 738 gfx::Rect arrow_bounds(disclosure_arrow_offset, 0, GetArrowButtonWidth(), |
| 752 arrow_size.width(), | 739 height()); |
| 753 arrow_size.height()); | 740 arrow_bounds.ClampToCenteredSize(ArrowSize()); |
| 754 AdjustBoundsForRTLUI(&arrow_bounds); | 741 AdjustBoundsForRTLUI(&arrow_bounds); |
| 755 | 742 |
| 756 gfx::ImageSkia arrow_image = PlatformStyle::CreateComboboxArrow( | |
| 757 enabled(), style_); | |
| 758 canvas->DrawImageInt(arrow_image, arrow_bounds.x(), arrow_bounds.y()); | 743 canvas->DrawImageInt(arrow_image, arrow_bounds.x(), arrow_bounds.y()); |
| 759 } | 744 } |
| 760 | 745 |
| 761 void Combobox::PaintButtons(gfx::Canvas* canvas) { | 746 void Combobox::PaintButtons(gfx::Canvas* canvas) { |
| 762 DCHECK(style_ == STYLE_ACTION); | 747 DCHECK(style_ == STYLE_ACTION); |
| 763 | 748 |
| 764 gfx::ScopedCanvas scoped_canvas(canvas); | 749 gfx::ScopedCanvas scoped_canvas(canvas); |
| 765 if (base::i18n::IsRTL()) { | 750 if (base::i18n::IsRTL()) { |
| 766 canvas->Translate(gfx::Vector2d(width(), 0)); | 751 canvas->Translate(gfx::Vector2d(width(), 0)); |
| 767 canvas->Scale(-1, 1); | 752 canvas->Scale(-1, 1); |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 866 | 851 |
| 867 // This combobox may be deleted by the listener. | 852 // This combobox may be deleted by the listener. |
| 868 base::WeakPtr<Combobox> weak_ptr = weak_ptr_factory_.GetWeakPtr(); | 853 base::WeakPtr<Combobox> weak_ptr = weak_ptr_factory_.GetWeakPtr(); |
| 869 if (listener_) | 854 if (listener_) |
| 870 listener_->OnPerformAction(this); | 855 listener_->OnPerformAction(this); |
| 871 | 856 |
| 872 if (weak_ptr && style_ == STYLE_ACTION) | 857 if (weak_ptr && style_ == STYLE_ACTION) |
| 873 selected_index_ = 0; | 858 selected_index_ = 0; |
| 874 } | 859 } |
| 875 | 860 |
| 876 int Combobox::GetDisclosureArrowLeftPadding() const { | |
| 877 switch (style_) { | |
| 878 case STYLE_NORMAL: | |
| 879 return kDisclosureArrowLeftPadding; | |
| 880 case STYLE_ACTION: | |
| 881 return kDisclosureArrowButtonLeftPadding; | |
| 882 } | |
| 883 NOTREACHED(); | |
| 884 return 0; | |
| 885 } | |
| 886 | |
| 887 int Combobox::GetDisclosureArrowRightPadding() const { | |
| 888 switch (style_) { | |
| 889 case STYLE_NORMAL: | |
| 890 return kDisclosureArrowRightPadding; | |
| 891 case STYLE_ACTION: | |
| 892 return kDisclosureArrowButtonRightPadding; | |
| 893 } | |
| 894 NOTREACHED(); | |
| 895 return 0; | |
| 896 } | |
| 897 | |
| 898 gfx::Size Combobox::ArrowSize() const { | 861 gfx::Size Combobox::ArrowSize() const { |
| 899 return PlatformStyle::CreateComboboxArrow(enabled(), style_).size(); | 862 return PlatformStyle::CreateComboboxArrow(enabled(), style_).size(); |
| 900 } | 863 } |
| 901 | 864 |
| 902 gfx::Size Combobox::GetContentSize() const { | 865 gfx::Size Combobox::GetContentSize() const { |
| 903 const gfx::FontList& font_list = GetFontList(); | 866 const gfx::FontList& font_list = GetFontList(); |
| 904 | 867 |
| 905 int width = 0; | 868 int width = 0; |
| 906 for (int i = 0; i < model()->GetItemCount(); ++i) { | 869 for (int i = 0; i < model()->GetItemCount(); ++i) { |
| 907 if (model_->IsItemSeparatorAt(i)) | 870 if (model_->IsItemSeparatorAt(i)) |
| 908 continue; | 871 continue; |
| 909 | 872 |
| 910 if (style_ != STYLE_ACTION || i == selected_index_) { | 873 if (style_ != STYLE_ACTION || i == selected_index_) { |
| 911 width = std::max( | 874 width = std::max( |
| 912 width, | 875 width, |
| 913 gfx::GetStringWidth(menu_model_adapter_->GetLabelAt(i), font_list)); | 876 gfx::GetStringWidth(menu_model_adapter_->GetLabelAt(i), font_list)); |
| 914 } | 877 } |
| 915 } | 878 } |
| 916 return gfx::Size(width, font_list.GetHeight()); | 879 return gfx::Size(width, font_list.GetHeight()); |
| 917 } | 880 } |
| 918 | 881 |
| 919 PrefixSelector* Combobox::GetPrefixSelector() { | 882 PrefixSelector* Combobox::GetPrefixSelector() { |
| 920 if (!selector_) | 883 if (!selector_) |
| 921 selector_.reset(new PrefixSelector(this)); | 884 selector_.reset(new PrefixSelector(this)); |
| 922 return selector_.get(); | 885 return selector_.get(); |
| 923 } | 886 } |
| 924 | 887 |
| 925 } // namespace views | 888 } // namespace views |
| OLD | NEW |