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