| 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/menu/menu_item_view.h" | 5 #include "ui/views/controls/menu/menu_item_view.h" |
| 6 | 6 |
| 7 #include "base/i18n/case_conversion.h" | 7 #include "base/i18n/case_conversion.h" |
| 8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
| 9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
| 10 #include "grit/ui_strings.h" | 10 #include "grit/ui_strings.h" |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 const string16& label, | 212 const string16& label, |
| 213 const gfx::ImageSkia& icon, | 213 const gfx::ImageSkia& icon, |
| 214 Type type, | 214 Type type, |
| 215 ui::MenuSeparatorType separator_style) { | 215 ui::MenuSeparatorType separator_style) { |
| 216 DCHECK_NE(type, EMPTY); | 216 DCHECK_NE(type, EMPTY); |
| 217 DCHECK_LE(0, index); | 217 DCHECK_LE(0, index); |
| 218 if (!submenu_) | 218 if (!submenu_) |
| 219 CreateSubmenu(); | 219 CreateSubmenu(); |
| 220 DCHECK_GE(submenu_->child_count(), index); | 220 DCHECK_GE(submenu_->child_count(), index); |
| 221 if (type == SEPARATOR) { | 221 if (type == SEPARATOR) { |
| 222 submenu_->AddChildViewAt(new MenuSeparator(this, separator_style), index); | 222 submenu_->AddChildViewAt(new MenuSeparator(separator_style), index); |
| 223 return NULL; | 223 return NULL; |
| 224 } | 224 } |
| 225 MenuItemView* item = new MenuItemView(this, item_id, type); | 225 MenuItemView* item = new MenuItemView(this, item_id, type); |
| 226 if (label.empty() && GetDelegate()) | 226 if (label.empty() && GetDelegate()) |
| 227 item->SetTitle(GetDelegate()->GetLabel(item_id)); | 227 item->SetTitle(GetDelegate()->GetLabel(item_id)); |
| 228 else | 228 else |
| 229 item->SetTitle(label); | 229 item->SetTitle(label); |
| 230 if (!icon.isNull()) | 230 if (!icon.isNull()) |
| 231 item->SetIcon(icon); | 231 item->SetIcon(icon); |
| 232 if (type == SUBMENU) | 232 if (type == SUBMENU) |
| (...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 523 int x = width() - (use_right_margin_ ? item_right_margin_ : 0); | 523 int x = width() - (use_right_margin_ ? item_right_margin_ : 0); |
| 524 for (int i = child_count() - 1; i >= 0; --i) { | 524 for (int i = child_count() - 1; i >= 0; --i) { |
| 525 View* child = child_at(i); | 525 View* child = child_at(i); |
| 526 if (icon_view_ && (icon_view_ == child)) | 526 if (icon_view_ && (icon_view_ == child)) |
| 527 continue; | 527 continue; |
| 528 int width = child->GetPreferredSize().width(); | 528 int width = child->GetPreferredSize().width(); |
| 529 child->SetBounds(x - width, 0, width, height()); | 529 child->SetBounds(x - width, 0, width, height()); |
| 530 x -= width - kChildXPadding; | 530 x -= width - kChildXPadding; |
| 531 } | 531 } |
| 532 // Position |icon_view|. | 532 // Position |icon_view|. |
| 533 const MenuConfig& config = GetMenuConfig(); | 533 const MenuConfig& config = MenuConfig::instance(); |
| 534 if (icon_view_) { | 534 if (icon_view_) { |
| 535 icon_view_->SizeToPreferredSize(); | 535 icon_view_->SizeToPreferredSize(); |
| 536 gfx::Size size = icon_view_->GetPreferredSize(); | 536 gfx::Size size = icon_view_->GetPreferredSize(); |
| 537 int x = config.item_left_margin + (icon_area_width_ - size.width()) / 2; | 537 int x = config.item_left_margin + (icon_area_width_ - size.width()) / 2; |
| 538 int y = | 538 int y = |
| 539 (height() + GetTopMargin() - GetBottomMargin() - size.height()) / 2; | 539 (height() + GetTopMargin() - GetBottomMargin() - size.height()) / 2; |
| 540 icon_view_->SetPosition(gfx::Point(x, y)); | 540 icon_view_->SetPosition(gfx::Point(x, y)); |
| 541 } | 541 } |
| 542 } | 542 } |
| 543 } | 543 } |
| 544 | 544 |
| 545 void MenuItemView::SetMargins(int top_margin, int bottom_margin) { | 545 void MenuItemView::SetMargins(int top_margin, int bottom_margin) { |
| 546 top_margin_ = top_margin; | 546 top_margin_ = top_margin; |
| 547 bottom_margin_ = bottom_margin; | 547 bottom_margin_ = bottom_margin; |
| 548 | 548 |
| 549 // invalidate GetPreferredSize() cache | 549 // invalidate GetPreferredSize() cache |
| 550 pref_size_.SetSize(0,0); | 550 pref_size_.SetSize(0,0); |
| 551 } | 551 } |
| 552 | 552 |
| 553 const MenuConfig& MenuItemView::GetMenuConfig() const { | |
| 554 const MenuItemView* root_menu_item = GetRootMenuItem(); | |
| 555 if (root_menu_item->menu_config_.get()) | |
| 556 return *(root_menu_item->menu_config_); | |
| 557 | |
| 558 return MenuConfig::instance(); | |
| 559 } | |
| 560 | |
| 561 MenuItemView::MenuItemView(MenuItemView* parent, | 553 MenuItemView::MenuItemView(MenuItemView* parent, |
| 562 int command, | 554 int command, |
| 563 MenuItemView::Type type) | 555 MenuItemView::Type type) |
| 564 : delegate_(NULL), | 556 : delegate_(NULL), |
| 565 controller_(NULL), | 557 controller_(NULL), |
| 566 canceled_(false), | 558 canceled_(false), |
| 567 parent_menu_item_(parent), | 559 parent_menu_item_(parent), |
| 568 type_(type), | 560 type_(type), |
| 569 selected_(false), | 561 selected_(false), |
| 570 command_(command), | 562 command_(command), |
| (...skipping 16 matching lines...) Expand all Loading... |
| 587 | 579 |
| 588 std::string MenuItemView::GetClassName() const { | 580 std::string MenuItemView::GetClassName() const { |
| 589 return kViewClassName; | 581 return kViewClassName; |
| 590 } | 582 } |
| 591 | 583 |
| 592 // Calculates all sizes that we can from the OS. | 584 // Calculates all sizes that we can from the OS. |
| 593 // | 585 // |
| 594 // This is invoked prior to Running a menu. | 586 // This is invoked prior to Running a menu. |
| 595 void MenuItemView::UpdateMenuPartSizes() { | 587 void MenuItemView::UpdateMenuPartSizes() { |
| 596 MenuConfig::Reset(); | 588 MenuConfig::Reset(); |
| 597 const MenuConfig& config = GetMenuConfig(); | 589 const MenuConfig& config = MenuConfig::instance(); |
| 598 | 590 |
| 599 item_right_margin_ = config.label_to_arrow_padding + config.arrow_width + | 591 item_right_margin_ = config.label_to_arrow_padding + config.arrow_width + |
| 600 config.arrow_to_edge_padding; | 592 config.arrow_to_edge_padding; |
| 601 icon_area_width_ = config.check_width; | 593 icon_area_width_ = config.check_width; |
| 602 if (has_icons_) | 594 if (has_icons_) |
| 603 icon_area_width_ = std::max(icon_area_width_, GetMaxIconViewWidth()); | 595 icon_area_width_ = std::max(icon_area_width_, GetMaxIconViewWidth()); |
| 604 | 596 |
| 605 if (config.always_use_icon_to_label_padding) | 597 if (config.always_use_icon_to_label_padding) |
| 606 label_start_ = config.item_left_margin + icon_area_width_ + | 598 label_start_ = config.item_left_margin + icon_area_width_ + |
| 607 config.icon_to_label_padding; | 599 config.icon_to_label_padding; |
| 608 else | 600 else |
| 609 // If there are no icons don't pad by the icon to label padding. This | 601 // If there are no icons don't pad by the icon to label padding. This |
| 610 // makes us look close to system menus. | 602 // makes us look close to system menus. |
| 611 label_start_ = config.item_left_margin + icon_area_width_ + | 603 label_start_ = config.item_left_margin + icon_area_width_ + |
| 612 (has_icons_ ? config.icon_to_label_padding : 0); | 604 (has_icons_ ? config.icon_to_label_padding : 0); |
| 613 | 605 |
| 614 if (config.render_gutter) | 606 if (config.render_gutter) |
| 615 label_start_ += config.gutter_width + config.gutter_to_label; | 607 label_start_ += config.gutter_width + config.gutter_to_label; |
| 616 | 608 |
| 617 MenuItemView menu_item(this, 0, NORMAL); | 609 MenuItemView menu_item(NULL); |
| 618 menu_item.SetTitle(ASCIIToUTF16("blah")); // Text doesn't matter here. | 610 menu_item.SetTitle(ASCIIToUTF16("blah")); // Text doesn't matter here. |
| 619 pref_menu_height_ = menu_item.GetPreferredSize().height(); | 611 pref_menu_height_ = menu_item.GetPreferredSize().height(); |
| 620 } | 612 } |
| 621 | 613 |
| 622 void MenuItemView::Init(MenuItemView* parent, | 614 void MenuItemView::Init(MenuItemView* parent, |
| 623 int command, | 615 int command, |
| 624 MenuItemView::Type type, | 616 MenuItemView::Type type, |
| 625 MenuDelegate* delegate) { | 617 MenuDelegate* delegate) { |
| 626 delegate_ = delegate; | 618 delegate_ = delegate; |
| 627 controller_ = NULL; | 619 controller_ = NULL; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 665 } | 657 } |
| 666 | 658 |
| 667 int MenuItemView::GetDrawStringFlags() { | 659 int MenuItemView::GetDrawStringFlags() { |
| 668 int flags = 0; | 660 int flags = 0; |
| 669 if (base::i18n::IsRTL()) | 661 if (base::i18n::IsRTL()) |
| 670 flags |= gfx::Canvas::TEXT_ALIGN_RIGHT; | 662 flags |= gfx::Canvas::TEXT_ALIGN_RIGHT; |
| 671 else | 663 else |
| 672 flags |= gfx::Canvas::TEXT_ALIGN_LEFT; | 664 flags |= gfx::Canvas::TEXT_ALIGN_LEFT; |
| 673 | 665 |
| 674 if (has_mnemonics_) { | 666 if (has_mnemonics_) { |
| 675 if (GetMenuConfig().show_mnemonics || GetRootMenuItem()->show_mnemonics_) { | 667 if (MenuConfig::instance().show_mnemonics || |
| 668 GetRootMenuItem()->show_mnemonics_) { |
| 676 flags |= gfx::Canvas::SHOW_PREFIX; | 669 flags |= gfx::Canvas::SHOW_PREFIX; |
| 677 } else { | 670 } else { |
| 678 flags |= gfx::Canvas::HIDE_PREFIX; | 671 flags |= gfx::Canvas::HIDE_PREFIX; |
| 679 } | 672 } |
| 680 } | 673 } |
| 681 return flags; | 674 return flags; |
| 682 } | 675 } |
| 683 | 676 |
| 684 const gfx::Font& MenuItemView::GetFont() { | 677 const gfx::Font& MenuItemView::GetFont() { |
| 685 // Check for item-specific font. | 678 // Check for item-specific font. |
| 686 const MenuDelegate* delegate = GetDelegate(); | 679 const MenuDelegate* delegate = GetDelegate(); |
| 687 return delegate ? | 680 return delegate ? |
| 688 delegate->GetLabelFont(GetCommand()) : GetMenuConfig().font; | 681 delegate->GetLabelFont(GetCommand()) : MenuConfig::instance().font; |
| 689 } | 682 } |
| 690 | 683 |
| 691 void MenuItemView::AddEmptyMenus() { | 684 void MenuItemView::AddEmptyMenus() { |
| 692 DCHECK(HasSubmenu()); | 685 DCHECK(HasSubmenu()); |
| 693 if (!submenu_->has_children()) { | 686 if (!submenu_->has_children()) { |
| 694 submenu_->AddChildViewAt(new EmptyMenuMenuItem(this), 0); | 687 submenu_->AddChildViewAt(new EmptyMenuMenuItem(this), 0); |
| 695 } else { | 688 } else { |
| 696 for (int i = 0, item_count = submenu_->GetMenuItemCount(); i < item_count; | 689 for (int i = 0, item_count = submenu_->GetMenuItemCount(); i < item_count; |
| 697 ++i) { | 690 ++i) { |
| 698 MenuItemView* child = submenu_->GetMenuItemAt(i); | 691 MenuItemView* child = submenu_->GetMenuItemAt(i); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 726 | 719 |
| 727 void MenuItemView::PaintAccelerator(gfx::Canvas* canvas) { | 720 void MenuItemView::PaintAccelerator(gfx::Canvas* canvas) { |
| 728 string16 accel_text = GetAcceleratorText(); | 721 string16 accel_text = GetAcceleratorText(); |
| 729 if (accel_text.empty()) | 722 if (accel_text.empty()) |
| 730 return; | 723 return; |
| 731 | 724 |
| 732 const gfx::Font& font = GetFont(); | 725 const gfx::Font& font = GetFont(); |
| 733 int available_height = height() - GetTopMargin() - GetBottomMargin(); | 726 int available_height = height() - GetTopMargin() - GetBottomMargin(); |
| 734 int max_accel_width = | 727 int max_accel_width = |
| 735 parent_menu_item_->GetSubmenu()->max_accelerator_width(); | 728 parent_menu_item_->GetSubmenu()->max_accelerator_width(); |
| 736 const MenuConfig& config = GetMenuConfig(); | 729 const MenuConfig& config = MenuConfig::instance(); |
| 737 int accel_right_margin = config.align_arrow_and_shortcut ? | 730 int accel_right_margin = config.align_arrow_and_shortcut ? |
| 738 config.arrow_to_edge_padding : item_right_margin_; | 731 config.arrow_to_edge_padding : item_right_margin_; |
| 739 gfx::Rect accel_bounds(width() - accel_right_margin - max_accel_width, | 732 gfx::Rect accel_bounds(width() - accel_right_margin - max_accel_width, |
| 740 GetTopMargin(), max_accel_width, available_height); | 733 GetTopMargin(), max_accel_width, available_height); |
| 741 accel_bounds.set_x(GetMirroredXForRect(accel_bounds)); | 734 accel_bounds.set_x(GetMirroredXForRect(accel_bounds)); |
| 742 int flags = GetRootMenuItem()->GetDrawStringFlags() | | 735 int flags = GetRootMenuItem()->GetDrawStringFlags() | |
| 743 gfx::Canvas::TEXT_VALIGN_MIDDLE; | 736 gfx::Canvas::TEXT_VALIGN_MIDDLE; |
| 744 flags &= ~(gfx::Canvas::TEXT_ALIGN_RIGHT | gfx::Canvas::TEXT_ALIGN_LEFT); | 737 flags &= ~(gfx::Canvas::TEXT_ALIGN_RIGHT | gfx::Canvas::TEXT_ALIGN_LEFT); |
| 745 if (base::i18n::IsRTL()) | 738 if (base::i18n::IsRTL()) |
| 746 flags |= gfx::Canvas::TEXT_ALIGN_LEFT; | 739 flags |= gfx::Canvas::TEXT_ALIGN_LEFT; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 763 submenu_->GetMenuItemAt(i)->DestroyAllMenuHosts(); | 756 submenu_->GetMenuItemAt(i)->DestroyAllMenuHosts(); |
| 764 } | 757 } |
| 765 } | 758 } |
| 766 | 759 |
| 767 int MenuItemView::GetTopMargin() { | 760 int MenuItemView::GetTopMargin() { |
| 768 if (top_margin_ >= 0) | 761 if (top_margin_ >= 0) |
| 769 return top_margin_; | 762 return top_margin_; |
| 770 | 763 |
| 771 MenuItemView* root = GetRootMenuItem(); | 764 MenuItemView* root = GetRootMenuItem(); |
| 772 return root && root->has_icons_ | 765 return root && root->has_icons_ |
| 773 ? GetMenuConfig().item_top_margin : | 766 ? MenuConfig::instance().item_top_margin : |
| 774 GetMenuConfig().item_no_icon_top_margin; | 767 MenuConfig::instance().item_no_icon_top_margin; |
| 775 } | 768 } |
| 776 | 769 |
| 777 int MenuItemView::GetBottomMargin() { | 770 int MenuItemView::GetBottomMargin() { |
| 778 if (bottom_margin_ >= 0) | 771 if (bottom_margin_ >= 0) |
| 779 return bottom_margin_; | 772 return bottom_margin_; |
| 780 | 773 |
| 781 MenuItemView* root = GetRootMenuItem(); | 774 MenuItemView* root = GetRootMenuItem(); |
| 782 return root && root->has_icons_ | 775 return root && root->has_icons_ |
| 783 ? GetMenuConfig().item_bottom_margin : | 776 ? MenuConfig::instance().item_bottom_margin : |
| 784 GetMenuConfig().item_no_icon_bottom_margin; | 777 MenuConfig::instance().item_no_icon_bottom_margin; |
| 785 } | 778 } |
| 786 | 779 |
| 787 gfx::Size MenuItemView::GetChildPreferredSize() { | 780 gfx::Size MenuItemView::GetChildPreferredSize() { |
| 788 if (!has_children()) | 781 if (!has_children()) |
| 789 return gfx::Size(); | 782 return gfx::Size(); |
| 790 | 783 |
| 791 if (IsContainer()) { | 784 if (IsContainer()) { |
| 792 View* child = child_at(0); | 785 View* child = child_at(0); |
| 793 return child->GetPreferredSize(); | 786 return child->GetPreferredSize(); |
| 794 } | 787 } |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 829 item_right_margin_; | 822 item_right_margin_; |
| 830 // Determine the length of the accelerator text. | 823 // Determine the length of the accelerator text. |
| 831 string16 text = GetAcceleratorText(); | 824 string16 text = GetAcceleratorText(); |
| 832 dimensions.accelerator_width = | 825 dimensions.accelerator_width = |
| 833 text.empty() ? 0 : GetFont().GetStringWidth(text); | 826 text.empty() ? 0 : GetFont().GetStringWidth(text); |
| 834 | 827 |
| 835 // Determine the height to use. | 828 // Determine the height to use. |
| 836 dimensions.height = std::max(dimensions.height, | 829 dimensions.height = std::max(dimensions.height, |
| 837 font.GetHeight() + GetBottomMargin() + GetTopMargin()); | 830 font.GetHeight() + GetBottomMargin() + GetTopMargin()); |
| 838 dimensions.height = std::max(dimensions.height, | 831 dimensions.height = std::max(dimensions.height, |
| 839 GetMenuConfig().item_min_height); | 832 MenuConfig::instance().item_min_height); |
| 840 return dimensions; | 833 return dimensions; |
| 841 } | 834 } |
| 842 | 835 |
| 843 gfx::Size MenuItemView::CalculatePreferredSize() { | 836 gfx::Size MenuItemView::CalculatePreferredSize() { |
| 844 MenuItemView::MenuItemDimensions dimensions = GetPreferredDimensions(); | 837 MenuItemView::MenuItemDimensions dimensions = GetPreferredDimensions(); |
| 845 return gfx::Size(dimensions.standard_width + dimensions.children_width, | 838 return gfx::Size(dimensions.standard_width + dimensions.children_width, |
| 846 dimensions.height); | 839 dimensions.height); |
| 847 } | 840 } |
| 848 | 841 |
| 849 string16 MenuItemView::GetAcceleratorText() { | 842 string16 MenuItemView::GetAcceleratorText() { |
| 850 if (id() == kEmptyMenuItemViewID) { | 843 if (id() == kEmptyMenuItemViewID) { |
| 851 // Don't query the delegate for menus that represent no children. | 844 // Don't query the delegate for menus that represent no children. |
| 852 return string16(); | 845 return string16(); |
| 853 } | 846 } |
| 854 | 847 |
| 855 if(!GetMenuConfig().show_accelerators) | 848 if(!MenuConfig::instance().show_accelerators) |
| 856 return string16(); | 849 return string16(); |
| 857 | 850 |
| 858 ui::Accelerator accelerator; | 851 ui::Accelerator accelerator; |
| 859 return (GetDelegate() && | 852 return (GetDelegate() && |
| 860 GetDelegate()->GetAccelerator(GetCommand(), &accelerator)) ? | 853 GetDelegate()->GetAccelerator(GetCommand(), &accelerator)) ? |
| 861 accelerator.GetShortcutText() : string16(); | 854 accelerator.GetShortcutText() : string16(); |
| 862 } | 855 } |
| 863 | 856 |
| 864 bool MenuItemView::IsContainer() const { | 857 bool MenuItemView::IsContainer() const { |
| 865 // Let the first child take over |this| when we only have one child and no | 858 // Let the first child take over |this| when we only have one child and no |
| (...skipping 16 matching lines...) Expand all Loading... |
| 882 temp_width = menu_item->GetMaxIconViewWidth(); | 875 temp_width = menu_item->GetMaxIconViewWidth(); |
| 883 } else if (menu_item->icon_view()) { | 876 } else if (menu_item->icon_view()) { |
| 884 temp_width = menu_item->icon_view()->GetPreferredSize().width(); | 877 temp_width = menu_item->icon_view()->GetPreferredSize().width(); |
| 885 } | 878 } |
| 886 width = std::max(width, temp_width); | 879 width = std::max(width, temp_width); |
| 887 } | 880 } |
| 888 return width; | 881 return width; |
| 889 } | 882 } |
| 890 | 883 |
| 891 } // namespace views | 884 } // namespace views |
| OLD | NEW |