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