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 "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/message_loop/message_loop_proxy.h" | 9 #include "base/message_loop/message_loop_proxy.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
(...skipping 571 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
582 source_type = ui::MENU_SOURCE_KEYBOARD; | 582 source_type = ui::MENU_SOURCE_KEYBOARD; |
583 else if (event.IsGestureEvent() || event.IsTouchEvent()) | 583 else if (event.IsGestureEvent() || event.IsTouchEvent()) |
584 source_type = ui::MENU_SOURCE_TOUCH; | 584 source_type = ui::MENU_SOURCE_TOUCH; |
585 ShowDropDownMenu(source_type); | 585 ShowDropDownMenu(source_type); |
586 } | 586 } |
587 } | 587 } |
588 | 588 |
589 void Combobox::UpdateFromModel() { | 589 void Combobox::UpdateFromModel() { |
590 const gfx::FontList& font_list = Combobox::GetFontList(); | 590 const gfx::FontList& font_list = Combobox::GetFontList(); |
591 | 591 |
592 MenuItemView* menu = new MenuItemView(this); | 592 menu_ = new MenuItemView(this); |
593 // MenuRunner owns |menu|. | 593 // MenuRunner owns |menu_|. |
594 dropdown_list_menu_runner_.reset(new MenuRunner(menu, MenuRunner::COMBOBOX)); | 594 dropdown_list_menu_runner_.reset(new MenuRunner(menu_, MenuRunner::COMBOBOX)); |
595 | 595 |
596 int num_items = model()->GetItemCount(); | 596 int num_items = model()->GetItemCount(); |
597 int width = 0; | 597 int width = 0; |
598 bool text_item_appended = false; | 598 bool text_item_appended = false; |
599 for (int i = 0; i < num_items; ++i) { | 599 for (int i = 0; i < num_items; ++i) { |
600 // When STYLE_ACTION is used, the first item and the following separators | 600 // When STYLE_ACTION is used, the first item and the following separators |
601 // are not added to the dropdown menu. It is assumed that the first item is | 601 // are not added to the dropdown menu. It is assumed that the first item is |
602 // always selected and rendered on the top of the action button. | 602 // always selected and rendered on the top of the action button. |
603 if (model()->IsItemSeparatorAt(i)) { | 603 if (model()->IsItemSeparatorAt(i)) { |
604 if (text_item_appended || style_ != STYLE_ACTION) | 604 if (text_item_appended || style_ != STYLE_ACTION) |
605 menu->AppendSeparator(); | 605 menu_->AppendSeparator(); |
606 continue; | 606 continue; |
607 } | 607 } |
608 | 608 |
609 base::string16 text = model()->GetItemAt(i); | 609 base::string16 text = model()->GetItemAt(i); |
610 | 610 |
611 // Inserting the Unicode formatting characters if necessary so that the | 611 // Inserting the Unicode formatting characters if necessary so that the |
612 // text is displayed correctly in right-to-left UIs. | 612 // text is displayed correctly in right-to-left UIs. |
613 base::i18n::AdjustStringForLocaleDirection(&text); | 613 base::i18n::AdjustStringForLocaleDirection(&text); |
614 | 614 |
615 if (style_ != STYLE_ACTION || i > 0) { | 615 if (style_ != STYLE_ACTION || i > 0) { |
616 menu->AppendMenuItem(i + kFirstMenuItemId, text, MenuItemView::NORMAL); | 616 menu_->AppendMenuItem(i + kFirstMenuItemId, text, MenuItemView::NORMAL); |
617 text_item_appended = true; | 617 text_item_appended = true; |
618 } | 618 } |
619 | 619 |
620 if (style_ != STYLE_ACTION || i == selected_index_) | 620 if (style_ != STYLE_ACTION || i == selected_index_) |
621 width = std::max(width, gfx::GetStringWidth(text, font_list)); | 621 width = std::max(width, gfx::GetStringWidth(text, font_list)); |
622 } | 622 } |
623 | 623 |
624 content_size_.SetSize(width, font_list.GetHeight()); | 624 content_size_.SetSize(width, font_list.GetHeight()); |
625 } | 625 } |
626 | 626 |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
745 arrow_button_->x(), height()); | 745 arrow_button_->x(), height()); |
746 canvas->Restore(); | 746 canvas->Restore(); |
747 } | 747 } |
748 } | 748 } |
749 | 749 |
750 void Combobox::ShowDropDownMenu(ui::MenuSourceType source_type) { | 750 void Combobox::ShowDropDownMenu(ui::MenuSourceType source_type) { |
751 if (!dropdown_list_menu_runner_.get()) | 751 if (!dropdown_list_menu_runner_.get()) |
752 UpdateFromModel(); | 752 UpdateFromModel(); |
753 | 753 |
754 // Extend the menu to the width of the combobox. | 754 // Extend the menu to the width of the combobox. |
755 MenuItemView* menu = dropdown_list_menu_runner_->GetMenu(); | 755 SubmenuView* submenu = menu_->CreateSubmenu(); |
756 SubmenuView* submenu = menu->CreateSubmenu(); | |
757 submenu->set_minimum_preferred_width( | 756 submenu->set_minimum_preferred_width( |
758 size().width() - (kMenuBorderWidthLeft + kMenuBorderWidthRight)); | 757 size().width() - (kMenuBorderWidthLeft + kMenuBorderWidthRight)); |
759 | 758 |
760 gfx::Rect lb = GetLocalBounds(); | 759 gfx::Rect lb = GetLocalBounds(); |
761 gfx::Point menu_position(lb.origin()); | 760 gfx::Point menu_position(lb.origin()); |
762 | 761 |
763 if (style_ == STYLE_NORMAL) { | 762 if (style_ == STYLE_NORMAL) { |
764 // Inset the menu's requested position so the border of the menu lines up | 763 // Inset the menu's requested position so the border of the menu lines up |
765 // with the border of the combobox. | 764 // with the border of the combobox. |
766 menu_position.set_x(menu_position.x() + kMenuBorderWidthLeft); | 765 menu_position.set_x(menu_position.x() + kMenuBorderWidthLeft); |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
853 const ui::NativeTheme* native_theme_for_arrow = GetNativeTheme(); | 852 const ui::NativeTheme* native_theme_for_arrow = GetNativeTheme(); |
854 #endif | 853 #endif |
855 | 854 |
856 ui::NativeTheme::ExtraParams ignored; | 855 ui::NativeTheme::ExtraParams ignored; |
857 return native_theme_for_arrow->GetPartSize(ui::NativeTheme::kComboboxArrow, | 856 return native_theme_for_arrow->GetPartSize(ui::NativeTheme::kComboboxArrow, |
858 ui::NativeTheme::kNormal, | 857 ui::NativeTheme::kNormal, |
859 ignored); | 858 ignored); |
860 } | 859 } |
861 | 860 |
862 } // namespace views | 861 } // namespace views |
OLD | NEW |