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/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "grit/ui_resources.h" | 10 #include "grit/ui_resources.h" |
(...skipping 385 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
396 icon_view_ = icon_view; | 396 icon_view_ = icon_view; |
397 } | 397 } |
398 Layout(); | 398 Layout(); |
399 SchedulePaint(); | 399 SchedulePaint(); |
400 } | 400 } |
401 | 401 |
402 void MenuItemView::OnPaint(gfx::Canvas* canvas) { | 402 void MenuItemView::OnPaint(gfx::Canvas* canvas) { |
403 PaintButton(canvas, PB_NORMAL); | 403 PaintButton(canvas, PB_NORMAL); |
404 } | 404 } |
405 | 405 |
406 gfx::Size MenuItemView::GetPreferredSize() { | 406 gfx::Size MenuItemView::GetPreferredSize() const { |
407 const MenuItemDimensions& dimensions(GetDimensions()); | 407 const MenuItemDimensions& dimensions(GetDimensions()); |
408 return gfx::Size(dimensions.standard_width + dimensions.children_width, | 408 return gfx::Size(dimensions.standard_width + dimensions.children_width, |
409 dimensions.height); | 409 dimensions.height); |
410 } | 410 } |
411 | 411 |
412 const MenuItemView::MenuItemDimensions& MenuItemView::GetDimensions() { | 412 const MenuItemView::MenuItemDimensions& MenuItemView::GetDimensions() const { |
413 if (!is_dimensions_valid()) | 413 if (!is_dimensions_valid()) |
414 dimensions_ = CalculateDimensions(); | 414 dimensions_ = CalculateDimensions(); |
415 DCHECK(is_dimensions_valid()); | 415 DCHECK(is_dimensions_valid()); |
416 return dimensions_; | 416 return dimensions_; |
417 } | 417 } |
418 | 418 |
419 MenuController* MenuItemView::GetMenuController() { | 419 MenuController* MenuItemView::GetMenuController() { |
420 return GetRootMenuItem()->controller_; | 420 return GetRootMenuItem()->controller_; |
421 } | 421 } |
422 | 422 |
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
682 if (GetRootMenuItem()->has_mnemonics_) { | 682 if (GetRootMenuItem()->has_mnemonics_) { |
683 if (GetMenuConfig().show_mnemonics || GetRootMenuItem()->show_mnemonics_) { | 683 if (GetMenuConfig().show_mnemonics || GetRootMenuItem()->show_mnemonics_) { |
684 flags |= gfx::Canvas::SHOW_PREFIX; | 684 flags |= gfx::Canvas::SHOW_PREFIX; |
685 } else { | 685 } else { |
686 flags |= gfx::Canvas::HIDE_PREFIX; | 686 flags |= gfx::Canvas::HIDE_PREFIX; |
687 } | 687 } |
688 } | 688 } |
689 return flags; | 689 return flags; |
690 } | 690 } |
691 | 691 |
692 const gfx::FontList& MenuItemView::GetFontList() { | 692 const gfx::FontList& MenuItemView::GetFontList() const { |
693 const MenuDelegate* delegate = GetDelegate(); | 693 const MenuDelegate* delegate = GetDelegate(); |
694 if (delegate) { | 694 if (delegate) { |
695 const gfx::FontList* font_list = delegate->GetLabelFontList(GetCommand()); | 695 const gfx::FontList* font_list = delegate->GetLabelFontList(GetCommand()); |
696 if (font_list) | 696 if (font_list) |
697 return *font_list; | 697 return *font_list; |
698 } | 698 } |
699 return GetMenuConfig().font_list; | 699 return GetMenuConfig().font_list; |
700 } | 700 } |
701 | 701 |
702 void MenuItemView::AddEmptyMenus() { | 702 void MenuItemView::AddEmptyMenus() { |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
888 if (!HasSubmenu()) | 888 if (!HasSubmenu()) |
889 return; | 889 return; |
890 | 890 |
891 submenu_->Close(); | 891 submenu_->Close(); |
892 for (int i = 0, item_count = submenu_->GetMenuItemCount(); i < item_count; | 892 for (int i = 0, item_count = submenu_->GetMenuItemCount(); i < item_count; |
893 ++i) { | 893 ++i) { |
894 submenu_->GetMenuItemAt(i)->DestroyAllMenuHosts(); | 894 submenu_->GetMenuItemAt(i)->DestroyAllMenuHosts(); |
895 } | 895 } |
896 } | 896 } |
897 | 897 |
898 int MenuItemView::GetTopMargin() { | 898 int MenuItemView::GetTopMargin() const { |
899 if (top_margin_ >= 0) | 899 if (top_margin_ >= 0) |
900 return top_margin_; | 900 return top_margin_; |
901 | 901 |
902 MenuItemView* root = GetRootMenuItem(); | 902 const MenuItemView* root = GetRootMenuItem(); |
903 return root && root->has_icons_ | 903 return root && root->has_icons_ |
904 ? GetMenuConfig().item_top_margin : | 904 ? GetMenuConfig().item_top_margin : |
905 GetMenuConfig().item_no_icon_top_margin; | 905 GetMenuConfig().item_no_icon_top_margin; |
906 } | 906 } |
907 | 907 |
908 int MenuItemView::GetBottomMargin() { | 908 int MenuItemView::GetBottomMargin() const { |
909 if (bottom_margin_ >= 0) | 909 if (bottom_margin_ >= 0) |
910 return bottom_margin_; | 910 return bottom_margin_; |
911 | 911 |
912 MenuItemView* root = GetRootMenuItem(); | 912 const MenuItemView* root = GetRootMenuItem(); |
913 return root && root->has_icons_ | 913 return root && root->has_icons_ |
914 ? GetMenuConfig().item_bottom_margin : | 914 ? GetMenuConfig().item_bottom_margin : |
915 GetMenuConfig().item_no_icon_bottom_margin; | 915 GetMenuConfig().item_no_icon_bottom_margin; |
916 } | 916 } |
917 | 917 |
918 gfx::Size MenuItemView::GetChildPreferredSize() { | 918 gfx::Size MenuItemView::GetChildPreferredSize() const { |
919 if (!has_children()) | 919 if (!has_children()) |
920 return gfx::Size(); | 920 return gfx::Size(); |
921 | 921 |
922 if (IsContainer()) { | 922 if (IsContainer()) |
923 View* child = child_at(0); | 923 return child_at(0)->GetPreferredSize(); |
924 return child->GetPreferredSize(); | |
925 } | |
926 | 924 |
927 int width = 0; | 925 int width = 0; |
928 for (int i = 0; i < child_count(); ++i) { | 926 for (int i = 0; i < child_count(); ++i) { |
929 View* child = child_at(i); | 927 const View* child = child_at(i); |
930 if (icon_view_ && (icon_view_ == child)) | 928 if (icon_view_ && (icon_view_ == child)) |
931 continue; | 929 continue; |
932 if (i) | 930 if (i) |
933 width += kChildXPadding; | 931 width += kChildXPadding; |
934 width += child->GetPreferredSize().width(); | 932 width += child->GetPreferredSize().width(); |
935 } | 933 } |
936 int height = 0; | 934 int height = 0; |
937 if (icon_view_) | 935 if (icon_view_) |
938 height = icon_view_->GetPreferredSize().height(); | 936 height = icon_view_->GetPreferredSize().height(); |
939 | 937 |
940 // If there is no icon view it returns a height of 0 to indicate that | 938 // If there is no icon view it returns a height of 0 to indicate that |
941 // we should use the title height instead. | 939 // we should use the title height instead. |
942 return gfx::Size(width, height); | 940 return gfx::Size(width, height); |
943 } | 941 } |
944 | 942 |
945 MenuItemView::MenuItemDimensions MenuItemView::CalculateDimensions() { | 943 MenuItemView::MenuItemDimensions MenuItemView::CalculateDimensions() const { |
946 gfx::Size child_size = GetChildPreferredSize(); | 944 gfx::Size child_size = GetChildPreferredSize(); |
947 | 945 |
948 MenuItemDimensions dimensions; | 946 MenuItemDimensions dimensions; |
949 // Get the container height. | 947 // Get the container height. |
950 dimensions.children_width = child_size.width(); | 948 dimensions.children_width = child_size.width(); |
951 dimensions.height = child_size.height(); | 949 dimensions.height = child_size.height(); |
952 // Adjust item content height if menu has both items with and without icons. | 950 // Adjust item content height if menu has both items with and without icons. |
953 // This way all menu items will have the same height. | 951 // This way all menu items will have the same height. |
954 if (!icon_view_ && GetRootMenuItem()->has_icons()) { | 952 if (!icon_view_ && GetRootMenuItem()->has_icons()) { |
955 dimensions.height = std::max(dimensions.height, | 953 dimensions.height = std::max(dimensions.height, |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
993 // Determine the height to use. | 991 // Determine the height to use. |
994 dimensions.height = | 992 dimensions.height = |
995 std::max(dimensions.height, | 993 std::max(dimensions.height, |
996 (subtitle_.empty() ? 0 : font_list.GetHeight()) + | 994 (subtitle_.empty() ? 0 : font_list.GetHeight()) + |
997 font_list.GetHeight() + GetBottomMargin() + GetTopMargin()); | 995 font_list.GetHeight() + GetBottomMargin() + GetTopMargin()); |
998 dimensions.height = std::max(dimensions.height, | 996 dimensions.height = std::max(dimensions.height, |
999 GetMenuConfig().item_min_height); | 997 GetMenuConfig().item_min_height); |
1000 return dimensions; | 998 return dimensions; |
1001 } | 999 } |
1002 | 1000 |
1003 int MenuItemView::GetLabelStartForThisItem() { | 1001 int MenuItemView::GetLabelStartForThisItem() const { |
1004 int label_start = label_start_ + left_icon_margin_ + right_icon_margin_; | 1002 int label_start = label_start_ + left_icon_margin_ + right_icon_margin_; |
1005 if ((type_ == CHECKBOX || type_ == RADIO) && icon_view_) { | 1003 if ((type_ == CHECKBOX || type_ == RADIO) && icon_view_) { |
1006 label_start += icon_view_->size().width() + | 1004 label_start += icon_view_->size().width() + |
1007 GetMenuConfig().icon_to_label_padding; | 1005 GetMenuConfig().icon_to_label_padding; |
1008 } | 1006 } |
1009 return label_start; | 1007 return label_start; |
1010 } | 1008 } |
1011 | 1009 |
1012 base::string16 MenuItemView::GetMinorText() { | 1010 base::string16 MenuItemView::GetMinorText() const { |
1013 if (id() == kEmptyMenuItemViewID) { | 1011 if (id() == kEmptyMenuItemViewID) { |
1014 // Don't query the delegate for menus that represent no children. | 1012 // Don't query the delegate for menus that represent no children. |
1015 return base::string16(); | 1013 return base::string16(); |
1016 } | 1014 } |
1017 | 1015 |
1018 ui::Accelerator accelerator; | 1016 ui::Accelerator accelerator; |
1019 if (GetMenuConfig().show_accelerators && GetDelegate() && GetCommand() && | 1017 if (GetMenuConfig().show_accelerators && GetDelegate() && GetCommand() && |
1020 GetDelegate()->GetAccelerator(GetCommand(), &accelerator)) { | 1018 GetDelegate()->GetAccelerator(GetCommand(), &accelerator)) { |
1021 return accelerator.GetShortcutText(); | 1019 return accelerator.GetShortcutText(); |
1022 } | 1020 } |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1065 } else { | 1063 } else { |
1066 const Type& type = menu_item->GetType(); | 1064 const Type& type = menu_item->GetType(); |
1067 if (type == CHECKBOX || type == RADIO) | 1065 if (type == CHECKBOX || type == RADIO) |
1068 return true; | 1066 return true; |
1069 } | 1067 } |
1070 } | 1068 } |
1071 return false; | 1069 return false; |
1072 } | 1070 } |
1073 | 1071 |
1074 } // namespace views | 1072 } // namespace views |
OLD | NEW |