 Chromium Code Reviews
 Chromium Code Reviews Issue 141523005:
  Combobox: Rename styles to STYLE_NORMAL and STYLE_ACTION and modify behaviors  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master
    
  
    Issue 141523005:
  Combobox: Rename styles to STYLE_NORMAL and STYLE_ACTION and modify behaviors  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master| 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/logging.h" | 8 #include "base/logging.h" | 
| 9 #include "base/message_loop/message_loop_proxy.h" | |
| 8 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" | 
| 9 #include "grit/ui_resources.h" | 11 #include "grit/ui_resources.h" | 
| 10 #include "ui/base/accessibility/accessible_view_state.h" | 12 #include "ui/base/accessibility/accessible_view_state.h" | 
| 11 #include "ui/base/models/combobox_model.h" | 13 #include "ui/base/models/combobox_model.h" | 
| 12 #include "ui/base/resource/resource_bundle.h" | 14 #include "ui/base/resource/resource_bundle.h" | 
| 13 #include "ui/events/event.h" | 15 #include "ui/events/event.h" | 
| 14 #include "ui/events/keycodes/keyboard_codes.h" | 16 #include "ui/events/keycodes/keyboard_codes.h" | 
| 15 #include "ui/gfx/animation/throb_animation.h" | 17 #include "ui/gfx/animation/throb_animation.h" | 
| 16 #include "ui/gfx/canvas.h" | 18 #include "ui/gfx/canvas.h" | 
| 17 #include "ui/gfx/image/image.h" | 19 #include "ui/gfx/image/image.h" | 
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 212 } // namespace | 214 } // namespace | 
| 213 | 215 | 
| 214 // static | 216 // static | 
| 215 const char Combobox::kViewClassName[] = "views/Combobox"; | 217 const char Combobox::kViewClassName[] = "views/Combobox"; | 
| 216 | 218 | 
| 217 //////////////////////////////////////////////////////////////////////////////// | 219 //////////////////////////////////////////////////////////////////////////////// | 
| 218 // Combobox, public: | 220 // Combobox, public: | 
| 219 | 221 | 
| 220 Combobox::Combobox(ui::ComboboxModel* model) | 222 Combobox::Combobox(ui::ComboboxModel* model) | 
| 221 : model_(model), | 223 : model_(model), | 
| 222 style_(STYLE_SHOW_DROP_DOWN_ON_CLICK), | 224 style_(STYLE_NORMAL), | 
| 223 listener_(NULL), | 225 listener_(NULL), | 
| 224 selected_index_(model_->GetDefaultIndex()), | 226 selected_index_(model_->GetDefaultIndex()), | 
| 225 invalid_(false), | 227 invalid_(false), | 
| 226 disclosure_arrow_(ui::ResourceBundle::GetSharedInstance().GetImageNamed( | 228 disclosure_arrow_(ui::ResourceBundle::GetSharedInstance().GetImageNamed( | 
| 227 IDR_MENU_DROPARROW).ToImageSkia()), | 229 IDR_MENU_DROPARROW).ToImageSkia()), | 
| 228 dropdown_open_(false), | 230 dropdown_open_(false), | 
| 229 text_button_(new TransparentButton(this)), | 231 text_button_(new TransparentButton(this)), | 
| 230 arrow_button_(new TransparentButton(this)) { | 232 arrow_button_(new TransparentButton(this)), | 
| 233 weak_ptr_factory_(this) { | |
| 231 model_->AddObserver(this); | 234 model_->AddObserver(this); | 
| 232 UpdateFromModel(); | 235 UpdateFromModel(); | 
| 233 SetFocusable(true); | 236 SetFocusable(true); | 
| 234 UpdateBorder(); | 237 UpdateBorder(); | 
| 235 | 238 | 
| 236 // Initialize the button images. | 239 // Initialize the button images. | 
| 237 Button::ButtonState button_states[] = { | 240 Button::ButtonState button_states[] = { | 
| 238 Button::STATE_DISABLED, | 241 Button::STATE_DISABLED, | 
| 239 Button::STATE_NORMAL, | 242 Button::STATE_NORMAL, | 
| 240 Button::STATE_HOVERED, | 243 Button::STATE_HOVERED, | 
| (...skipping 28 matching lines...) Expand all Loading... | |
| 269 const gfx::FontList& Combobox::GetFontList() { | 272 const gfx::FontList& Combobox::GetFontList() { | 
| 270 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 273 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 
| 271 return rb.GetFontList(ui::ResourceBundle::BaseFont); | 274 return rb.GetFontList(ui::ResourceBundle::BaseFont); | 
| 272 } | 275 } | 
| 273 | 276 | 
| 274 void Combobox::SetStyle(Style style) { | 277 void Combobox::SetStyle(Style style) { | 
| 275 if (style_ == style) | 278 if (style_ == style) | 
| 276 return; | 279 return; | 
| 277 | 280 | 
| 278 style_ = style; | 281 style_ = style; | 
| 282 if (style_ == STYLE_ACTION) | |
| 283 selected_index_ = 0; | |
| 279 | 284 | 
| 280 UpdateBorder(); | 285 UpdateBorder(); | 
| 286 UpdateFromModel(); | |
| 281 PreferredSizeChanged(); | 287 PreferredSizeChanged(); | 
| 282 } | 288 } | 
| 283 | 289 | 
| 284 void Combobox::ModelChanged() { | 290 void Combobox::ModelChanged() { | 
| 285 selected_index_ = std::min(0, model_->GetItemCount()); | 291 selected_index_ = std::min(0, model_->GetItemCount()); | 
| 286 UpdateFromModel(); | 292 UpdateFromModel(); | 
| 287 PreferredSizeChanged(); | 293 PreferredSizeChanged(); | 
| 288 } | 294 } | 
| 289 | 295 | 
| 290 void Combobox::SetSelectedIndex(int index) { | 296 void Combobox::SetSelectedIndex(int index) { | 
| 297 if (style_ == STYLE_ACTION) | |
| 298 return; | |
| 299 | |
| 291 selected_index_ = index; | 300 selected_index_ = index; | 
| 292 SchedulePaint(); | 301 SchedulePaint(); | 
| 293 } | 302 } | 
| 294 | 303 | 
| 295 bool Combobox::SelectValue(const base::string16& value) { | 304 bool Combobox::SelectValue(const base::string16& value) { | 
| 305 if (style_ == STYLE_ACTION) | |
| 306 return false; | |
| 307 | |
| 296 for (int i = 0; i < model()->GetItemCount(); ++i) { | 308 for (int i = 0; i < model()->GetItemCount(); ++i) { | 
| 297 if (value == model()->GetItemAt(i)) { | 309 if (value == model()->GetItemAt(i)) { | 
| 298 SetSelectedIndex(i); | 310 SetSelectedIndex(i); | 
| 299 return true; | 311 return true; | 
| 300 } | 312 } | 
| 301 } | 313 } | 
| 302 return false; | 314 return false; | 
| 303 } | 315 } | 
| 304 | 316 | 
| 305 void Combobox::SetAccessibleName(const base::string16& name) { | 317 void Combobox::SetAccessibleName(const base::string16& name) { | 
| (...skipping 17 matching lines...) Expand all Loading... | |
| 323 } | 335 } | 
| 324 | 336 | 
| 325 void Combobox::Layout() { | 337 void Combobox::Layout() { | 
| 326 PrefixDelegate::Layout(); | 338 PrefixDelegate::Layout(); | 
| 327 | 339 | 
| 328 gfx::Insets insets = GetInsets(); | 340 gfx::Insets insets = GetInsets(); | 
| 329 int text_button_width = 0; | 341 int text_button_width = 0; | 
| 330 int arrow_button_width = 0; | 342 int arrow_button_width = 0; | 
| 331 | 343 | 
| 332 switch (style_) { | 344 switch (style_) { | 
| 333 case STYLE_SHOW_DROP_DOWN_ON_CLICK: { | 345 case STYLE_NORMAL: { | 
| 334 arrow_button_width = width(); | 346 arrow_button_width = width(); | 
| 335 break; | 347 break; | 
| 336 } | 348 } | 
| 337 case STYLE_NOTIFY_ON_CLICK: { | 349 case STYLE_ACTION: { | 
| 338 arrow_button_width = GetDisclosureArrowLeftPadding() + | 350 arrow_button_width = GetDisclosureArrowLeftPadding() + | 
| 339 disclosure_arrow_->width() + GetDisclosureArrowRightPadding(); | 351 disclosure_arrow_->width() + GetDisclosureArrowRightPadding(); | 
| 340 text_button_width = width() - arrow_button_width; | 352 text_button_width = width() - arrow_button_width; | 
| 341 break; | 353 break; | 
| 342 } | 354 } | 
| 343 } | 355 } | 
| 344 | 356 | 
| 345 int arrow_button_x = std::max(0, text_button_width); | 357 int arrow_button_x = std::max(0, text_button_width); | 
| 346 text_button_->SetBounds(0, 0, std::max(0, text_button_width), height()); | 358 text_button_->SetBounds(0, 0, std::max(0, text_button_width), height()); | 
| 347 arrow_button_->SetBounds(arrow_button_x, 0, arrow_button_width, height()); | 359 arrow_button_->SetBounds(arrow_button_x, 0, arrow_button_width, height()); | 
| 348 } | 360 } | 
| 349 | 361 | 
| 350 bool Combobox::IsItemChecked(int id) const { | 362 bool Combobox::IsItemChecked(int id) const { | 
| 351 return false; | 363 return false; | 
| 352 } | 364 } | 
| 353 | 365 | 
| 354 bool Combobox::IsCommandEnabled(int id) const { | 366 bool Combobox::IsCommandEnabled(int id) const { | 
| 355 return model()->IsItemEnabledAt(MenuCommandToIndex(id)); | 367 return model()->IsItemEnabledAt(MenuCommandToIndex(id)); | 
| 356 } | 368 } | 
| 357 | 369 | 
| 358 void Combobox::ExecuteCommand(int id) { | 370 void Combobox::ExecuteCommand(int id) { | 
| 359 selected_index_ = MenuCommandToIndex(id); | 371 selected_index_ = MenuCommandToIndex(id); | 
| 360 OnSelectionChanged(); | 372 OnPerformAction(); | 
| 361 } | 373 } | 
| 362 | 374 | 
| 363 bool Combobox::GetAccelerator(int id, ui::Accelerator* accel) { | 375 bool Combobox::GetAccelerator(int id, ui::Accelerator* accel) { | 
| 364 return false; | 376 return false; | 
| 365 } | 377 } | 
| 366 | 378 | 
| 367 int Combobox::GetRowCount() { | 379 int Combobox::GetRowCount() { | 
| 368 return model()->GetItemCount(); | 380 return model()->GetItemCount(); | 
| 369 } | 381 } | 
| 370 | 382 | 
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 449 new_index = GetAdjacentIndex(model(), 1, -1); | 461 new_index = GetAdjacentIndex(model(), 1, -1); | 
| 450 break; | 462 break; | 
| 451 | 463 | 
| 452 // Move to the previous item if any. | 464 // Move to the previous item if any. | 
| 453 case ui::VKEY_UP: | 465 case ui::VKEY_UP: | 
| 454 new_index = GetAdjacentIndex(model(), -1, selected_index_); | 466 new_index = GetAdjacentIndex(model(), -1, selected_index_); | 
| 455 break; | 467 break; | 
| 456 | 468 | 
| 457 // Click the button only when the button style mode. | 469 // Click the button only when the button style mode. | 
| 458 case ui::VKEY_SPACE: | 470 case ui::VKEY_SPACE: | 
| 459 if (style_ == STYLE_NOTIFY_ON_CLICK) { | 471 if (style_ == STYLE_ACTION) { | 
| 460 // When pressing space, the click event will be raised after the key is | 472 // When pressing space, the click event will be raised after the key is | 
| 461 // released. | 473 // released. | 
| 462 text_button_->SetState(Button::STATE_PRESSED); | 474 text_button_->SetState(Button::STATE_PRESSED); | 
| 463 } else { | 475 } else { | 
| 464 return false; | 476 return false; | 
| 465 } | 477 } | 
| 466 break; | 478 break; | 
| 467 | 479 | 
| 468 // Click the button only when the button style mode. | 480 // Click the button only when the button style mode. | 
| 469 case ui::VKEY_RETURN: | 481 case ui::VKEY_RETURN: | 
| 470 if (style_ != STYLE_NOTIFY_ON_CLICK) | 482 if (style_ != STYLE_ACTION) | 
| 471 return false; | 483 return false; | 
| 472 HandleClickEvent(); | 484 OnPerformAction(); | 
| 473 break; | 485 break; | 
| 474 | 486 | 
| 475 default: | 487 default: | 
| 476 return false; | 488 return false; | 
| 477 } | 489 } | 
| 478 | 490 | 
| 479 if (show_menu) { | 491 if (show_menu) { | 
| 480 UpdateFromModel(); | 492 UpdateFromModel(); | 
| 481 ShowDropDownMenu(ui::MENU_SOURCE_KEYBOARD); | 493 ShowDropDownMenu(ui::MENU_SOURCE_KEYBOARD); | 
| 482 } else if (new_index != selected_index_ && new_index != kNoSelection) { | 494 } else if (new_index != selected_index_ && new_index != kNoSelection) { | 
| 483 DCHECK(!model()->IsItemSeparatorAt(new_index)); | 495 if (style_ != STYLE_ACTION) { | 
| 
sky
2014/02/03 16:58:19
nit: combine with if on 494.
 
hajimehoshi
2014/02/04 04:06:32
Done.
 | |
| 484 selected_index_ = new_index; | 496 DCHECK(!model()->IsItemSeparatorAt(new_index)); | 
| 485 OnSelectionChanged(); | 497 selected_index_ = new_index; | 
| 498 OnPerformAction(); | |
| 499 } | |
| 486 } | 500 } | 
| 487 | 501 | 
| 488 return true; | 502 return true; | 
| 489 } | 503 } | 
| 490 | 504 | 
| 491 bool Combobox::OnKeyReleased(const ui::KeyEvent& e) { | 505 bool Combobox::OnKeyReleased(const ui::KeyEvent& e) { | 
| 492 if (style_ != STYLE_NOTIFY_ON_CLICK) | 506 if (style_ != STYLE_ACTION) | 
| 493 return false; // crbug.com/127520 | 507 return false; // crbug.com/127520 | 
| 494 | 508 | 
| 495 if (e.key_code() == ui::VKEY_SPACE) | 509 if (e.key_code() == ui::VKEY_SPACE && style_ == STYLE_ACTION) | 
| 496 HandleClickEvent(); | 510 OnPerformAction(); | 
| 497 | 511 | 
| 498 return false; | 512 return false; | 
| 499 } | 513 } | 
| 500 | 514 | 
| 501 void Combobox::OnPaint(gfx::Canvas* canvas) { | 515 void Combobox::OnPaint(gfx::Canvas* canvas) { | 
| 502 switch (style_) { | 516 switch (style_) { | 
| 503 case STYLE_SHOW_DROP_DOWN_ON_CLICK: { | 517 case STYLE_NORMAL: { | 
| 504 OnPaintBackground(canvas); | 518 OnPaintBackground(canvas); | 
| 505 PaintText(canvas); | 519 PaintText(canvas); | 
| 506 OnPaintBorder(canvas); | 520 OnPaintBorder(canvas); | 
| 507 break; | 521 break; | 
| 508 } | 522 } | 
| 509 case STYLE_NOTIFY_ON_CLICK: { | 523 case STYLE_ACTION: { | 
| 510 PaintButtons(canvas); | 524 PaintButtons(canvas); | 
| 511 PaintText(canvas); | 525 PaintText(canvas); | 
| 512 break; | 526 break; | 
| 513 } | 527 } | 
| 514 } | 528 } | 
| 515 } | 529 } | 
| 516 | 530 | 
| 517 void Combobox::OnFocus() { | 531 void Combobox::OnFocus() { | 
| 518 GetInputMethod()->OnFocus(); | 532 GetInputMethod()->OnFocus(); | 
| 519 View::OnFocus(); | 533 View::OnFocus(); | 
| (...skipping 19 matching lines...) Expand all Loading... | |
| 539 | 553 | 
| 540 void Combobox::OnComboboxModelChanged(ui::ComboboxModel* model) { | 554 void Combobox::OnComboboxModelChanged(ui::ComboboxModel* model) { | 
| 541 DCHECK_EQ(model, model_); | 555 DCHECK_EQ(model, model_); | 
| 542 ModelChanged(); | 556 ModelChanged(); | 
| 543 } | 557 } | 
| 544 | 558 | 
| 545 void Combobox::ButtonPressed(Button* sender, const ui::Event& event) { | 559 void Combobox::ButtonPressed(Button* sender, const ui::Event& event) { | 
| 546 RequestFocus(); | 560 RequestFocus(); | 
| 547 | 561 | 
| 548 if (sender == text_button_) { | 562 if (sender == text_button_) { | 
| 549 HandleClickEvent(); | 563 OnPerformAction(); | 
| 550 } else { | 564 } else { | 
| 551 DCHECK_EQ(arrow_button_, sender); | 565 DCHECK_EQ(arrow_button_, sender); | 
| 552 // TODO(hajimehoshi): Fix the problem that the arrow button blinks when | 566 // TODO(hajimehoshi): Fix the problem that the arrow button blinks when | 
| 553 // cliking this while the dropdown menu is opened. | 567 // cliking this while the dropdown menu is opened. | 
| 554 const base::TimeDelta delta = base::Time::Now() - closed_time_; | 568 const base::TimeDelta delta = base::Time::Now() - closed_time_; | 
| 555 if (delta.InMilliseconds() <= kMinimumMsBetweenButtonClicks) | 569 if (delta.InMilliseconds() <= kMinimumMsBetweenButtonClicks) | 
| 556 return; | 570 return; | 
| 557 | 571 | 
| 558 ui::MenuSourceType source_type = ui::MENU_SOURCE_MOUSE; | 572 ui::MenuSourceType source_type = ui::MENU_SOURCE_MOUSE; | 
| 559 if (event.IsKeyEvent()) | 573 if (event.IsKeyEvent()) | 
| 560 source_type = ui::MENU_SOURCE_KEYBOARD; | 574 source_type = ui::MENU_SOURCE_KEYBOARD; | 
| 561 else if (event.IsGestureEvent() || event.IsTouchEvent()) | 575 else if (event.IsGestureEvent() || event.IsTouchEvent()) | 
| 562 source_type = ui::MENU_SOURCE_TOUCH; | 576 source_type = ui::MENU_SOURCE_TOUCH; | 
| 563 ShowDropDownMenu(source_type); | 577 ShowDropDownMenu(source_type); | 
| 564 } | 578 } | 
| 565 } | 579 } | 
| 566 | 580 | 
| 567 void Combobox::UpdateFromModel() { | 581 void Combobox::UpdateFromModel() { | 
| 568 int max_width = 0; | |
| 569 const gfx::FontList& font_list = Combobox::GetFontList(); | 582 const gfx::FontList& font_list = Combobox::GetFontList(); | 
| 570 | 583 | 
| 571 MenuItemView* menu = new MenuItemView(this); | 584 MenuItemView* menu = new MenuItemView(this); | 
| 572 // MenuRunner owns |menu|. | 585 // MenuRunner owns |menu|. | 
| 573 dropdown_list_menu_runner_.reset(new MenuRunner(menu)); | 586 dropdown_list_menu_runner_.reset(new MenuRunner(menu)); | 
| 574 | 587 | 
| 575 int num_items = model()->GetItemCount(); | 588 int num_items = model()->GetItemCount(); | 
| 589 int width = 0; | |
| 576 for (int i = 0; i < num_items; ++i) { | 590 for (int i = 0; i < num_items; ++i) { | 
| 577 if (model()->IsItemSeparatorAt(i)) { | 591 if (model()->IsItemSeparatorAt(i)) { | 
| 578 menu->AppendSeparator(); | 592 menu->AppendSeparator(); | 
| 579 continue; | 593 continue; | 
| 580 } | 594 } | 
| 581 | 595 | 
| 582 base::string16 text = model()->GetItemAt(i); | 596 base::string16 text = model()->GetItemAt(i); | 
| 583 | 597 | 
| 584 // Inserting the Unicode formatting characters if necessary so that the | 598 // Inserting the Unicode formatting characters if necessary so that the | 
| 585 // text is displayed correctly in right-to-left UIs. | 599 // text is displayed correctly in right-to-left UIs. | 
| 586 base::i18n::AdjustStringForLocaleDirection(&text); | 600 base::i18n::AdjustStringForLocaleDirection(&text); | 
| 587 | 601 | 
| 588 menu->AppendMenuItem(i + kFirstMenuItemId, text, MenuItemView::NORMAL); | 602 menu->AppendMenuItem(i + kFirstMenuItemId, text, MenuItemView::NORMAL); | 
| 589 max_width = std::max(max_width, gfx::GetStringWidth(text, font_list)); | 603 | 
| 604 if (style_ != STYLE_ACTION || i == selected_index_) | |
| 605 width = std::max(width, gfx::GetStringWidth(text, font_list)); | |
| 590 } | 606 } | 
| 591 | 607 | 
| 592 content_size_.SetSize(max_width, font_list.GetHeight()); | 608 content_size_.SetSize(width, font_list.GetHeight()); | 
| 593 } | 609 } | 
| 594 | 610 | 
| 595 void Combobox::UpdateBorder() { | 611 void Combobox::UpdateBorder() { | 
| 596 scoped_ptr<FocusableBorder> border(new FocusableBorder()); | 612 scoped_ptr<FocusableBorder> border(new FocusableBorder()); | 
| 597 if (style_ == STYLE_NOTIFY_ON_CLICK) | 613 if (style_ == STYLE_ACTION) | 
| 598 border->SetInsets(8, 13, 8, 13); | 614 border->SetInsets(8, 13, 8, 13); | 
| 599 if (invalid_) | 615 if (invalid_) | 
| 600 border->SetColor(kWarningColor); | 616 border->SetColor(kWarningColor); | 
| 601 SetBorder(border.PassAs<Border>()); | 617 SetBorder(border.PassAs<Border>()); | 
| 602 } | 618 } | 
| 603 | 619 | 
| 604 void Combobox::AdjustBoundsForRTLUI(gfx::Rect* rect) const { | 620 void Combobox::AdjustBoundsForRTLUI(gfx::Rect* rect) const { | 
| 605 rect->set_x(GetMirroredXForRect(*rect)); | 621 rect->set_x(GetMirroredXForRect(*rect)); | 
| 606 } | 622 } | 
| 607 | 623 | 
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 639 gfx::Rect arrow_bounds(arrow_x, | 655 gfx::Rect arrow_bounds(arrow_x, | 
| 640 height() / 2 - disclosure_arrow_->height() / 2, | 656 height() / 2 - disclosure_arrow_->height() / 2, | 
| 641 disclosure_arrow_->width(), | 657 disclosure_arrow_->width(), | 
| 642 disclosure_arrow_->height()); | 658 disclosure_arrow_->height()); | 
| 643 AdjustBoundsForRTLUI(&arrow_bounds); | 659 AdjustBoundsForRTLUI(&arrow_bounds); | 
| 644 | 660 | 
| 645 canvas->DrawImageInt(*disclosure_arrow_, arrow_bounds.x(), arrow_bounds.y()); | 661 canvas->DrawImageInt(*disclosure_arrow_, arrow_bounds.x(), arrow_bounds.y()); | 
| 646 } | 662 } | 
| 647 | 663 | 
| 648 void Combobox::PaintButtons(gfx::Canvas* canvas) { | 664 void Combobox::PaintButtons(gfx::Canvas* canvas) { | 
| 649 DCHECK(style_ == STYLE_NOTIFY_ON_CLICK); | 665 DCHECK(style_ == STYLE_ACTION); | 
| 650 | 666 | 
| 651 gfx::ScopedCanvas scoped_canvas(canvas); | 667 gfx::ScopedCanvas scoped_canvas(canvas); | 
| 652 if (base::i18n::IsRTL()) { | 668 if (base::i18n::IsRTL()) { | 
| 653 canvas->Translate(gfx::Vector2d(width(), 0)); | 669 canvas->Translate(gfx::Vector2d(width(), 0)); | 
| 654 canvas->Scale(-1, 1); | 670 canvas->Scale(-1, 1); | 
| 655 } | 671 } | 
| 656 | 672 | 
| 657 bool focused = HasFocus(); | 673 bool focused = HasFocus(); | 
| 658 const std::vector<const gfx::ImageSkia*>& arrow_button_images = | 674 const std::vector<const gfx::ImageSkia*>& arrow_button_images = | 
| 659 menu_button_images_[focused][ | 675 menu_button_images_[focused][ | 
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 743 if (arrow_button_) | 759 if (arrow_button_) | 
| 744 arrow_button_->SetState(original_state); | 760 arrow_button_->SetState(original_state); | 
| 745 closed_time_ = base::Time::Now(); | 761 closed_time_ = base::Time::Now(); | 
| 746 | 762 | 
| 747 // Need to explicitly clear mouse handler so that events get sent | 763 // Need to explicitly clear mouse handler so that events get sent | 
| 748 // properly after the menu finishes running. If we don't do this, then | 764 // properly after the menu finishes running. If we don't do this, then | 
| 749 // the first click to other parts of the UI is eaten. | 765 // the first click to other parts of the UI is eaten. | 
| 750 SetMouseHandler(NULL); | 766 SetMouseHandler(NULL); | 
| 751 } | 767 } | 
| 752 | 768 | 
| 753 void Combobox::OnSelectionChanged() { | 769 void Combobox::OnPerformAction() { | 
| 754 NotifyAccessibilityEvent(ui::AccessibilityTypes::EVENT_VALUE_CHANGED, false); | 770 NotifyAccessibilityEvent(ui::AccessibilityTypes::EVENT_VALUE_CHANGED, false); | 
| 755 SchedulePaint(); | 771 SchedulePaint(); | 
| 772 | |
| 773 // Because the combobox might die during processing at the lister, the | |
| 774 // cleaning up should be executed as a posted task with a weak ref pointer. | |
| 775 base::MessageLoop::current()->message_loop_proxy()->PostTaskAndReply( | |
| 776 FROM_HERE, | |
| 777 base::Bind(&Combobox::NotifyPerformAction, | |
| 
sky
2014/02/03 16:58:19
You should not delay the notification. Instead bef
 
hajimehoshi
2014/02/04 04:06:32
Done.
 | |
| 778 weak_ptr_factory_.GetWeakPtr()), | |
| 779 base::Bind(&Combobox::AfterPerformAction, | |
| 780 weak_ptr_factory_.GetWeakPtr())); | |
| 781 } | |
| 782 | |
| 783 void Combobox::NotifyPerformAction() { | |
| 756 if (listener_) | 784 if (listener_) | 
| 757 listener_->OnSelectedIndexChanged(this); | 785 listener_->OnPerformAction(this); | 
| 758 // |this| may now be deleted. | 786 } | 
| 787 | |
| 788 void Combobox::AfterPerformAction() { | |
| 789 if (style_ == STYLE_ACTION) | |
| 790 selected_index_ = 0; | |
| 759 } | 791 } | 
| 760 | 792 | 
| 761 int Combobox::MenuCommandToIndex(int menu_command_id) const { | 793 int Combobox::MenuCommandToIndex(int menu_command_id) const { | 
| 762 // (note that the id received is offset by kFirstMenuItemId) | 794 // (note that the id received is offset by kFirstMenuItemId) | 
| 763 // Revert menu ID offset to map back to combobox model. | 795 // Revert menu ID offset to map back to combobox model. | 
| 764 int index = menu_command_id - kFirstMenuItemId; | 796 int index = menu_command_id - kFirstMenuItemId; | 
| 765 DCHECK_LT(index, model()->GetItemCount()); | 797 DCHECK_LT(index, model()->GetItemCount()); | 
| 766 return index; | 798 return index; | 
| 767 } | 799 } | 
| 768 | 800 | 
| 769 int Combobox::GetDisclosureArrowLeftPadding() const { | 801 int Combobox::GetDisclosureArrowLeftPadding() const { | 
| 770 switch (style_) { | 802 switch (style_) { | 
| 771 case STYLE_SHOW_DROP_DOWN_ON_CLICK: | 803 case STYLE_NORMAL: | 
| 772 return kDisclosureArrowLeftPadding; | 804 return kDisclosureArrowLeftPadding; | 
| 773 case STYLE_NOTIFY_ON_CLICK: | 805 case STYLE_ACTION: | 
| 774 return kDisclosureArrowButtonLeftPadding; | 806 return kDisclosureArrowButtonLeftPadding; | 
| 775 } | 807 } | 
| 776 NOTREACHED(); | 808 NOTREACHED(); | 
| 777 return 0; | 809 return 0; | 
| 778 } | 810 } | 
| 779 | 811 | 
| 780 int Combobox::GetDisclosureArrowRightPadding() const { | 812 int Combobox::GetDisclosureArrowRightPadding() const { | 
| 781 switch (style_) { | 813 switch (style_) { | 
| 782 case STYLE_SHOW_DROP_DOWN_ON_CLICK: | 814 case STYLE_NORMAL: | 
| 783 return kDisclosureArrowRightPadding; | 815 return kDisclosureArrowRightPadding; | 
| 784 case STYLE_NOTIFY_ON_CLICK: | 816 case STYLE_ACTION: | 
| 785 return kDisclosureArrowButtonRightPadding; | 817 return kDisclosureArrowButtonRightPadding; | 
| 786 } | 818 } | 
| 787 NOTREACHED(); | 819 NOTREACHED(); | 
| 788 return 0; | 820 return 0; | 
| 789 } | 821 } | 
| 790 | 822 | 
| 791 void Combobox::HandleClickEvent() { | |
| 792 if (style_ != STYLE_NOTIFY_ON_CLICK) | |
| 793 return; | |
| 794 | |
| 795 if (listener_) | |
| 796 listener_->OnComboboxTextButtonClicked(this); | |
| 797 } | |
| 798 | |
| 799 } // namespace views | 823 } // namespace views | 
| OLD | NEW |