| 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/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 Loading... |
| 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 Loading... |
| 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(); |
| 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 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 if (style_ != STYLE_NOTIFY_ON_CLICK) { |
| 484 selected_index_ = new_index; | 487 DCHECK(!model()->IsItemSeparatorAt(new_index)); |
| 485 OnSelectionChanged(); | 488 selected_index_ = new_index; |
| 489 OnSelectionChanged(); |
| 490 } |
| 486 } | 491 } |
| 487 | 492 |
| 488 return true; | 493 return true; |
| 489 } | 494 } |
| 490 | 495 |
| 491 bool Combobox::OnKeyReleased(const ui::KeyEvent& e) { | 496 bool Combobox::OnKeyReleased(const ui::KeyEvent& e) { |
| 492 if (style_ != STYLE_NOTIFY_ON_CLICK) | 497 if (style_ != STYLE_NOTIFY_ON_CLICK) |
| 493 return false; // crbug.com/127520 | 498 return false; // crbug.com/127520 |
| 494 | 499 |
| 495 if (e.key_code() == ui::VKEY_SPACE) | 500 if (e.key_code() == ui::VKEY_SPACE && style_ == STYLE_NOTIFY_ON_CLICK) |
| 496 HandleClickEvent(); | 501 HandleClickEvent(); |
| 497 | 502 |
| 498 return false; | 503 return false; |
| 499 } | 504 } |
| 500 | 505 |
| 501 void Combobox::OnPaint(gfx::Canvas* canvas) { | 506 void Combobox::OnPaint(gfx::Canvas* canvas) { |
| 502 switch (style_) { | 507 switch (style_) { |
| 503 case STYLE_SHOW_DROP_DOWN_ON_CLICK: { | 508 case STYLE_SHOW_DROP_DOWN_ON_CLICK: { |
| 504 OnPaintBackground(canvas); | 509 OnPaintBackground(canvas); |
| 505 PaintText(canvas); | 510 PaintText(canvas); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 558 ui::MenuSourceType source_type = ui::MENU_SOURCE_MOUSE; | 563 ui::MenuSourceType source_type = ui::MENU_SOURCE_MOUSE; |
| 559 if (event.IsKeyEvent()) | 564 if (event.IsKeyEvent()) |
| 560 source_type = ui::MENU_SOURCE_KEYBOARD; | 565 source_type = ui::MENU_SOURCE_KEYBOARD; |
| 561 else if (event.IsGestureEvent() || event.IsTouchEvent()) | 566 else if (event.IsGestureEvent() || event.IsTouchEvent()) |
| 562 source_type = ui::MENU_SOURCE_TOUCH; | 567 source_type = ui::MENU_SOURCE_TOUCH; |
| 563 ShowDropDownMenu(source_type); | 568 ShowDropDownMenu(source_type); |
| 564 } | 569 } |
| 565 } | 570 } |
| 566 | 571 |
| 567 void Combobox::UpdateFromModel() { | 572 void Combobox::UpdateFromModel() { |
| 568 int max_width = 0; | |
| 569 const gfx::FontList& font_list = Combobox::GetFontList(); | 573 const gfx::FontList& font_list = Combobox::GetFontList(); |
| 570 | 574 |
| 571 MenuItemView* menu = new MenuItemView(this); | 575 MenuItemView* menu = new MenuItemView(this); |
| 572 // MenuRunner owns |menu|. | 576 // MenuRunner owns |menu|. |
| 573 dropdown_list_menu_runner_.reset(new MenuRunner(menu)); | 577 dropdown_list_menu_runner_.reset(new MenuRunner(menu)); |
| 574 | 578 |
| 575 int num_items = model()->GetItemCount(); | 579 int num_items = model()->GetItemCount(); |
| 580 int width = 0; |
| 576 for (int i = 0; i < num_items; ++i) { | 581 for (int i = 0; i < num_items; ++i) { |
| 577 if (model()->IsItemSeparatorAt(i)) { | 582 if (model()->IsItemSeparatorAt(i)) { |
| 578 menu->AppendSeparator(); | 583 menu->AppendSeparator(); |
| 579 continue; | 584 continue; |
| 580 } | 585 } |
| 581 | 586 |
| 582 base::string16 text = model()->GetItemAt(i); | 587 base::string16 text = model()->GetItemAt(i); |
| 583 | 588 |
| 584 // Inserting the Unicode formatting characters if necessary so that the | 589 // Inserting the Unicode formatting characters if necessary so that the |
| 585 // text is displayed correctly in right-to-left UIs. | 590 // text is displayed correctly in right-to-left UIs. |
| 586 base::i18n::AdjustStringForLocaleDirection(&text); | 591 base::i18n::AdjustStringForLocaleDirection(&text); |
| 587 | 592 |
| 588 menu->AppendMenuItem(i + kFirstMenuItemId, text, MenuItemView::NORMAL); | 593 menu->AppendMenuItem(i + kFirstMenuItemId, text, MenuItemView::NORMAL); |
| 589 max_width = std::max(max_width, gfx::GetStringWidth(text, font_list)); | 594 |
| 595 if (style_ != STYLE_NOTIFY_ON_CLICK || i == selected_index_) |
| 596 width = std::max(width, gfx::GetStringWidth(text, font_list)); |
| 590 } | 597 } |
| 591 | 598 |
| 592 content_size_.SetSize(max_width, font_list.GetHeight()); | 599 content_size_.SetSize(width, font_list.GetHeight()); |
| 593 } | 600 } |
| 594 | 601 |
| 595 void Combobox::UpdateBorder() { | 602 void Combobox::UpdateBorder() { |
| 596 FocusableBorder* border = new FocusableBorder(); | 603 FocusableBorder* border = new FocusableBorder(); |
| 597 if (style_ == STYLE_NOTIFY_ON_CLICK) | 604 if (style_ == STYLE_NOTIFY_ON_CLICK) |
| 598 border->SetInsets(8, 13, 8, 13); | 605 border->SetInsets(8, 13, 8, 13); |
| 599 if (invalid_) | 606 if (invalid_) |
| 600 border->SetColor(kWarningColor); | 607 border->SetColor(kWarningColor); |
| 601 set_border(border); | 608 set_border(border); |
| 602 } | 609 } |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 790 | 797 |
| 791 void Combobox::HandleClickEvent() { | 798 void Combobox::HandleClickEvent() { |
| 792 if (style_ != STYLE_NOTIFY_ON_CLICK) | 799 if (style_ != STYLE_NOTIFY_ON_CLICK) |
| 793 return; | 800 return; |
| 794 | 801 |
| 795 if (listener_) | 802 if (listener_) |
| 796 listener_->OnComboboxTextButtonClicked(this); | 803 listener_->OnComboboxTextButtonClicked(this); |
| 797 } | 804 } |
| 798 | 805 |
| 799 } // namespace views | 806 } // namespace views |
| OLD | NEW |