| 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 202 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 213 | 213 | 
| 214 // static | 214 // static | 
| 215 const char Combobox::kViewClassName[] = "views/Combobox"; | 215 const char Combobox::kViewClassName[] = "views/Combobox"; | 
| 216 | 216 | 
| 217 //////////////////////////////////////////////////////////////////////////////// | 217 //////////////////////////////////////////////////////////////////////////////// | 
| 218 // Combobox, public: | 218 // Combobox, public: | 
| 219 | 219 | 
| 220 Combobox::Combobox(ui::ComboboxModel* model) | 220 Combobox::Combobox(ui::ComboboxModel* model) | 
| 221     : model_(model), | 221     : model_(model), | 
| 222       style_(STYLE_SHOW_DROP_DOWN_ON_CLICK), | 222       style_(STYLE_SHOW_DROP_DOWN_ON_CLICK), | 
|  | 223       menu_item_raises_click_event_(false), | 
| 223       listener_(NULL), | 224       listener_(NULL), | 
| 224       selected_index_(model_->GetDefaultIndex()), | 225       selected_index_(model_->GetDefaultIndex()), | 
| 225       invalid_(false), | 226       invalid_(false), | 
| 226       disclosure_arrow_(ui::ResourceBundle::GetSharedInstance().GetImageNamed( | 227       disclosure_arrow_(ui::ResourceBundle::GetSharedInstance().GetImageNamed( | 
| 227           IDR_MENU_DROPARROW).ToImageSkia()), | 228           IDR_MENU_DROPARROW).ToImageSkia()), | 
| 228       dropdown_open_(false), | 229       dropdown_open_(false), | 
| 229       text_button_(new TransparentButton(this)), | 230       text_button_(new TransparentButton(this)), | 
| 230       arrow_button_(new TransparentButton(this)) { | 231       arrow_button_(new TransparentButton(this)) { | 
| 231   model_->AddObserver(this); | 232   model_->AddObserver(this); | 
| 232   UpdateFromModel(); | 233   UpdateFromModel(); | 
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 274 void Combobox::SetStyle(Style style) { | 275 void Combobox::SetStyle(Style style) { | 
| 275   if (style_ == style) | 276   if (style_ == style) | 
| 276     return; | 277     return; | 
| 277 | 278 | 
| 278   style_ = style; | 279   style_ = style; | 
| 279 | 280 | 
| 280   UpdateBorder(); | 281   UpdateBorder(); | 
| 281   PreferredSizeChanged(); | 282   PreferredSizeChanged(); | 
| 282 } | 283 } | 
| 283 | 284 | 
|  | 285 void Combobox::SetMenuItemRaisesClickEvent(bool value) { | 
|  | 286   if (menu_item_raises_click_event_ == value) | 
|  | 287     return; | 
|  | 288 | 
|  | 289   menu_item_raises_click_event_ = value; | 
|  | 290 | 
|  | 291   UpdateFromModel(); | 
|  | 292   PreferredSizeChanged(); | 
|  | 293 } | 
|  | 294 | 
| 284 void Combobox::ModelChanged() { | 295 void Combobox::ModelChanged() { | 
| 285   selected_index_ = std::min(0, model_->GetItemCount()); | 296   selected_index_ = std::min(0, model_->GetItemCount()); | 
| 286   UpdateFromModel(); | 297   UpdateFromModel(); | 
| 287   PreferredSizeChanged(); | 298   PreferredSizeChanged(); | 
| 288 } | 299 } | 
| 289 | 300 | 
| 290 void Combobox::SetSelectedIndex(int index) { | 301 void Combobox::SetSelectedIndex(int index) { | 
| 291   selected_index_ = index; | 302   selected_index_ = index; | 
| 292   SchedulePaint(); | 303   SchedulePaint(); | 
| 293 } | 304 } | 
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 351   return false; | 362   return false; | 
| 352 } | 363 } | 
| 353 | 364 | 
| 354 bool Combobox::IsCommandEnabled(int id) const { | 365 bool Combobox::IsCommandEnabled(int id) const { | 
| 355   return model()->IsItemEnabledAt(MenuCommandToIndex(id)); | 366   return model()->IsItemEnabledAt(MenuCommandToIndex(id)); | 
| 356 } | 367 } | 
| 357 | 368 | 
| 358 void Combobox::ExecuteCommand(int id) { | 369 void Combobox::ExecuteCommand(int id) { | 
| 359   selected_index_ = MenuCommandToIndex(id); | 370   selected_index_ = MenuCommandToIndex(id); | 
| 360   OnSelectionChanged(); | 371   OnSelectionChanged(); | 
|  | 372   if (style_ == STYLE_NOTIFY_ON_CLICK && menu_item_raises_click_event_) | 
|  | 373     HandleClickEvent(); | 
| 361 } | 374 } | 
| 362 | 375 | 
| 363 bool Combobox::GetAccelerator(int id, ui::Accelerator* accel) { | 376 bool Combobox::GetAccelerator(int id, ui::Accelerator* accel) { | 
| 364   return false; | 377   return false; | 
| 365 } | 378 } | 
| 366 | 379 | 
| 367 int Combobox::GetRowCount() { | 380 int Combobox::GetRowCount() { | 
| 368   return model()->GetItemCount(); | 381   return model()->GetItemCount(); | 
| 369 } | 382 } | 
| 370 | 383 | 
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 558     ui::MenuSourceType source_type = ui::MENU_SOURCE_MOUSE; | 571     ui::MenuSourceType source_type = ui::MENU_SOURCE_MOUSE; | 
| 559     if (event.IsKeyEvent()) | 572     if (event.IsKeyEvent()) | 
| 560       source_type = ui::MENU_SOURCE_KEYBOARD; | 573       source_type = ui::MENU_SOURCE_KEYBOARD; | 
| 561     else if (event.IsGestureEvent() || event.IsTouchEvent()) | 574     else if (event.IsGestureEvent() || event.IsTouchEvent()) | 
| 562       source_type = ui::MENU_SOURCE_TOUCH; | 575       source_type = ui::MENU_SOURCE_TOUCH; | 
| 563     ShowDropDownMenu(source_type); | 576     ShowDropDownMenu(source_type); | 
| 564   } | 577   } | 
| 565 } | 578 } | 
| 566 | 579 | 
| 567 void Combobox::UpdateFromModel() { | 580 void Combobox::UpdateFromModel() { | 
| 568   int max_width = 0; |  | 
| 569   const gfx::FontList& font_list = Combobox::GetFontList(); | 581   const gfx::FontList& font_list = Combobox::GetFontList(); | 
| 570 | 582 | 
| 571   MenuItemView* menu = new MenuItemView(this); | 583   MenuItemView* menu = new MenuItemView(this); | 
| 572   // MenuRunner owns |menu|. | 584   // MenuRunner owns |menu|. | 
| 573   dropdown_list_menu_runner_.reset(new MenuRunner(menu)); | 585   dropdown_list_menu_runner_.reset(new MenuRunner(menu)); | 
| 574 | 586 | 
| 575   int num_items = model()->GetItemCount(); | 587   int num_items = model()->GetItemCount(); | 
|  | 588   int width = 0; | 
| 576   for (int i = 0; i < num_items; ++i) { | 589   for (int i = 0; i < num_items; ++i) { | 
| 577     if (model()->IsItemSeparatorAt(i)) { | 590     if (model()->IsItemSeparatorAt(i)) { | 
| 578       menu->AppendSeparator(); | 591       menu->AppendSeparator(); | 
| 579       continue; | 592       continue; | 
| 580     } | 593     } | 
| 581 | 594 | 
| 582     base::string16 text = model()->GetItemAt(i); | 595     base::string16 text = model()->GetItemAt(i); | 
| 583 | 596 | 
| 584     // Inserting the Unicode formatting characters if necessary so that the | 597     // Inserting the Unicode formatting characters if necessary so that the | 
| 585     // text is displayed correctly in right-to-left UIs. | 598     // text is displayed correctly in right-to-left UIs. | 
| 586     base::i18n::AdjustStringForLocaleDirection(&text); | 599     base::i18n::AdjustStringForLocaleDirection(&text); | 
| 587 | 600 | 
| 588     menu->AppendMenuItem(i + kFirstMenuItemId, text, MenuItemView::NORMAL); | 601     menu->AppendMenuItem(i + kFirstMenuItemId, text, MenuItemView::NORMAL); | 
| 589     max_width = std::max(max_width, gfx::GetStringWidth(text, font_list)); | 602     if (!menu_item_raises_click_event_ || i == 0) | 
|  | 603       width = std::max(width, gfx::GetStringWidth(text, font_list)); | 
| 590   } | 604   } | 
| 591 | 605 | 
| 592   content_size_.SetSize(max_width, font_list.GetHeight()); | 606   content_size_.SetSize(width, font_list.GetHeight()); | 
| 593 } | 607 } | 
| 594 | 608 | 
| 595 void Combobox::UpdateBorder() { | 609 void Combobox::UpdateBorder() { | 
| 596   FocusableBorder* border = new FocusableBorder(); | 610   FocusableBorder* border = new FocusableBorder(); | 
| 597   if (style_ == STYLE_NOTIFY_ON_CLICK) | 611   if (style_ == STYLE_NOTIFY_ON_CLICK) | 
| 598     border->SetInsets(8, 13, 8, 13); | 612     border->SetInsets(8, 13, 8, 13); | 
| 599   if (invalid_) | 613   if (invalid_) | 
| 600     border->SetColor(kWarningColor); | 614     border->SetColor(kWarningColor); | 
| 601   set_border(border); | 615   set_border(border); | 
| 602 } | 616 } | 
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 790 | 804 | 
| 791 void Combobox::HandleClickEvent() { | 805 void Combobox::HandleClickEvent() { | 
| 792   if (style_ != STYLE_NOTIFY_ON_CLICK) | 806   if (style_ != STYLE_NOTIFY_ON_CLICK) | 
| 793     return; | 807     return; | 
| 794 | 808 | 
| 795   if (listener_) | 809   if (listener_) | 
| 796     listener_->OnComboboxTextButtonClicked(this); | 810     listener_->OnComboboxTextButtonClicked(this); | 
| 797 } | 811 } | 
| 798 | 812 | 
| 799 }  // namespace views | 813 }  // namespace views | 
| OLD | NEW | 
|---|