Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(57)

Side by Side Diff: ui/views/controls/menu/menu_item_view.cc

Issue 11026076: Added MenuConfig setter to MenuItemView, updated rest of the code to use set MenuConfig if it avail… (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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(separator_style), index); 222 submenu_->AddChildViewAt(new MenuSeparator(this, 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
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 = MenuConfig::instance(); 533 const MenuConfig& config = GetMenuConfig();
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
553 MenuItemView::MenuItemView(MenuItemView* parent, 561 MenuItemView::MenuItemView(MenuItemView* parent,
554 int command, 562 int command,
555 MenuItemView::Type type) 563 MenuItemView::Type type)
556 : delegate_(NULL), 564 : delegate_(NULL),
557 controller_(NULL), 565 controller_(NULL),
558 canceled_(false), 566 canceled_(false),
559 parent_menu_item_(parent), 567 parent_menu_item_(parent),
560 type_(type), 568 type_(type),
561 selected_(false), 569 selected_(false),
562 command_(command), 570 command_(command),
(...skipping 16 matching lines...) Expand all
579 587
580 std::string MenuItemView::GetClassName() const { 588 std::string MenuItemView::GetClassName() const {
581 return kViewClassName; 589 return kViewClassName;
582 } 590 }
583 591
584 // Calculates all sizes that we can from the OS. 592 // Calculates all sizes that we can from the OS.
585 // 593 //
586 // This is invoked prior to Running a menu. 594 // This is invoked prior to Running a menu.
587 void MenuItemView::UpdateMenuPartSizes() { 595 void MenuItemView::UpdateMenuPartSizes() {
588 MenuConfig::Reset(); 596 MenuConfig::Reset();
589 const MenuConfig& config = MenuConfig::instance(); 597 const MenuConfig& config = GetMenuConfig();
590 598
591 item_right_margin_ = config.label_to_arrow_padding + config.arrow_width + 599 item_right_margin_ = config.label_to_arrow_padding + config.arrow_width +
592 config.arrow_to_edge_padding; 600 config.arrow_to_edge_padding;
593 icon_area_width_ = config.check_width; 601 icon_area_width_ = config.check_width;
594 if (has_icons_) 602 if (has_icons_)
595 icon_area_width_ = std::max(icon_area_width_, GetMaxIconViewWidth()); 603 icon_area_width_ = std::max(icon_area_width_, GetMaxIconViewWidth());
596 604
597 if (config.always_use_icon_to_label_padding) 605 if (config.always_use_icon_to_label_padding)
598 label_start_ = config.item_left_margin + icon_area_width_ + 606 label_start_ = config.item_left_margin + icon_area_width_ +
599 config.icon_to_label_padding; 607 config.icon_to_label_padding;
600 else 608 else
601 // If there are no icons don't pad by the icon to label padding. This 609 // If there are no icons don't pad by the icon to label padding. This
602 // makes us look close to system menus. 610 // makes us look close to system menus.
603 label_start_ = config.item_left_margin + icon_area_width_ + 611 label_start_ = config.item_left_margin + icon_area_width_ +
604 (has_icons_ ? config.icon_to_label_padding : 0); 612 (has_icons_ ? config.icon_to_label_padding : 0);
605 613
606 if (config.render_gutter) 614 if (config.render_gutter)
607 label_start_ += config.gutter_width + config.gutter_to_label; 615 label_start_ += config.gutter_width + config.gutter_to_label;
608 616
609 MenuItemView menu_item(NULL); 617 MenuItemView menu_item(this, 0, NORMAL);
610 menu_item.SetTitle(ASCIIToUTF16("blah")); // Text doesn't matter here. 618 menu_item.SetTitle(ASCIIToUTF16("blah")); // Text doesn't matter here.
611 pref_menu_height_ = menu_item.GetPreferredSize().height(); 619 pref_menu_height_ = menu_item.GetPreferredSize().height();
612 } 620 }
613 621
614 void MenuItemView::Init(MenuItemView* parent, 622 void MenuItemView::Init(MenuItemView* parent,
615 int command, 623 int command,
616 MenuItemView::Type type, 624 MenuItemView::Type type,
617 MenuDelegate* delegate) { 625 MenuDelegate* delegate) {
618 delegate_ = delegate; 626 delegate_ = delegate;
619 controller_ = NULL; 627 controller_ = NULL;
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
657 } 665 }
658 666
659 int MenuItemView::GetDrawStringFlags() { 667 int MenuItemView::GetDrawStringFlags() {
660 int flags = 0; 668 int flags = 0;
661 if (base::i18n::IsRTL()) 669 if (base::i18n::IsRTL())
662 flags |= gfx::Canvas::TEXT_ALIGN_RIGHT; 670 flags |= gfx::Canvas::TEXT_ALIGN_RIGHT;
663 else 671 else
664 flags |= gfx::Canvas::TEXT_ALIGN_LEFT; 672 flags |= gfx::Canvas::TEXT_ALIGN_LEFT;
665 673
666 if (has_mnemonics_) { 674 if (has_mnemonics_) {
667 if (MenuConfig::instance().show_mnemonics || 675 if (GetMenuConfig().show_mnemonics || GetRootMenuItem()->show_mnemonics_) {
668 GetRootMenuItem()->show_mnemonics_) {
669 flags |= gfx::Canvas::SHOW_PREFIX; 676 flags |= gfx::Canvas::SHOW_PREFIX;
670 } else { 677 } else {
671 flags |= gfx::Canvas::HIDE_PREFIX; 678 flags |= gfx::Canvas::HIDE_PREFIX;
672 } 679 }
673 } 680 }
674 return flags; 681 return flags;
675 } 682 }
676 683
677 const gfx::Font& MenuItemView::GetFont() { 684 const gfx::Font& MenuItemView::GetFont() {
678 // Check for item-specific font. 685 // Check for item-specific font.
679 const MenuDelegate* delegate = GetDelegate(); 686 const MenuDelegate* delegate = GetDelegate();
680 return delegate ? 687 return delegate ?
681 delegate->GetLabelFont(GetCommand()) : MenuConfig::instance().font; 688 delegate->GetLabelFont(GetCommand()) : GetMenuConfig().font;
682 } 689 }
683 690
684 void MenuItemView::AddEmptyMenus() { 691 void MenuItemView::AddEmptyMenus() {
685 DCHECK(HasSubmenu()); 692 DCHECK(HasSubmenu());
686 if (!submenu_->has_children()) { 693 if (!submenu_->has_children()) {
687 submenu_->AddChildViewAt(new EmptyMenuMenuItem(this), 0); 694 submenu_->AddChildViewAt(new EmptyMenuMenuItem(this), 0);
688 } else { 695 } else {
689 for (int i = 0, item_count = submenu_->GetMenuItemCount(); i < item_count; 696 for (int i = 0, item_count = submenu_->GetMenuItemCount(); i < item_count;
690 ++i) { 697 ++i) {
691 MenuItemView* child = submenu_->GetMenuItemAt(i); 698 MenuItemView* child = submenu_->GetMenuItemAt(i);
(...skipping 27 matching lines...) Expand all
719 726
720 void MenuItemView::PaintAccelerator(gfx::Canvas* canvas) { 727 void MenuItemView::PaintAccelerator(gfx::Canvas* canvas) {
721 string16 accel_text = GetAcceleratorText(); 728 string16 accel_text = GetAcceleratorText();
722 if (accel_text.empty()) 729 if (accel_text.empty())
723 return; 730 return;
724 731
725 const gfx::Font& font = GetFont(); 732 const gfx::Font& font = GetFont();
726 int available_height = height() - GetTopMargin() - GetBottomMargin(); 733 int available_height = height() - GetTopMargin() - GetBottomMargin();
727 int max_accel_width = 734 int max_accel_width =
728 parent_menu_item_->GetSubmenu()->max_accelerator_width(); 735 parent_menu_item_->GetSubmenu()->max_accelerator_width();
729 const MenuConfig& config = MenuConfig::instance(); 736 const MenuConfig& config = GetMenuConfig();
730 int accel_right_margin = config.align_arrow_and_shortcut ? 737 int accel_right_margin = config.align_arrow_and_shortcut ?
731 config.arrow_to_edge_padding : item_right_margin_; 738 config.arrow_to_edge_padding : item_right_margin_;
732 gfx::Rect accel_bounds(width() - accel_right_margin - max_accel_width, 739 gfx::Rect accel_bounds(width() - accel_right_margin - max_accel_width,
733 GetTopMargin(), max_accel_width, available_height); 740 GetTopMargin(), max_accel_width, available_height);
734 accel_bounds.set_x(GetMirroredXForRect(accel_bounds)); 741 accel_bounds.set_x(GetMirroredXForRect(accel_bounds));
735 int flags = GetRootMenuItem()->GetDrawStringFlags() | 742 int flags = GetRootMenuItem()->GetDrawStringFlags() |
736 gfx::Canvas::TEXT_VALIGN_MIDDLE; 743 gfx::Canvas::TEXT_VALIGN_MIDDLE;
737 flags &= ~(gfx::Canvas::TEXT_ALIGN_RIGHT | gfx::Canvas::TEXT_ALIGN_LEFT); 744 flags &= ~(gfx::Canvas::TEXT_ALIGN_RIGHT | gfx::Canvas::TEXT_ALIGN_LEFT);
738 if (base::i18n::IsRTL()) 745 if (base::i18n::IsRTL())
739 flags |= gfx::Canvas::TEXT_ALIGN_LEFT; 746 flags |= gfx::Canvas::TEXT_ALIGN_LEFT;
(...skipping 16 matching lines...) Expand all
756 submenu_->GetMenuItemAt(i)->DestroyAllMenuHosts(); 763 submenu_->GetMenuItemAt(i)->DestroyAllMenuHosts();
757 } 764 }
758 } 765 }
759 766
760 int MenuItemView::GetTopMargin() { 767 int MenuItemView::GetTopMargin() {
761 if (top_margin_ >= 0) 768 if (top_margin_ >= 0)
762 return top_margin_; 769 return top_margin_;
763 770
764 MenuItemView* root = GetRootMenuItem(); 771 MenuItemView* root = GetRootMenuItem();
765 return root && root->has_icons_ 772 return root && root->has_icons_
766 ? MenuConfig::instance().item_top_margin : 773 ? GetMenuConfig().item_top_margin :
767 MenuConfig::instance().item_no_icon_top_margin; 774 GetMenuConfig().item_no_icon_top_margin;
768 } 775 }
769 776
770 int MenuItemView::GetBottomMargin() { 777 int MenuItemView::GetBottomMargin() {
771 if (bottom_margin_ >= 0) 778 if (bottom_margin_ >= 0)
772 return bottom_margin_; 779 return bottom_margin_;
773 780
774 MenuItemView* root = GetRootMenuItem(); 781 MenuItemView* root = GetRootMenuItem();
775 return root && root->has_icons_ 782 return root && root->has_icons_
776 ? MenuConfig::instance().item_bottom_margin : 783 ? GetMenuConfig().item_bottom_margin :
777 MenuConfig::instance().item_no_icon_bottom_margin; 784 GetMenuConfig().item_no_icon_bottom_margin;
778 } 785 }
779 786
780 gfx::Size MenuItemView::GetChildPreferredSize() { 787 gfx::Size MenuItemView::GetChildPreferredSize() {
781 if (!has_children()) 788 if (!has_children())
782 return gfx::Size(); 789 return gfx::Size();
783 790
784 if (IsContainer()) { 791 if (IsContainer()) {
785 View* child = child_at(0); 792 View* child = child_at(0);
786 return child->GetPreferredSize(); 793 return child->GetPreferredSize();
787 } 794 }
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
822 item_right_margin_; 829 item_right_margin_;
823 // Determine the length of the accelerator text. 830 // Determine the length of the accelerator text.
824 string16 text = GetAcceleratorText(); 831 string16 text = GetAcceleratorText();
825 dimensions.accelerator_width = 832 dimensions.accelerator_width =
826 text.empty() ? 0 : GetFont().GetStringWidth(text); 833 text.empty() ? 0 : GetFont().GetStringWidth(text);
827 834
828 // Determine the height to use. 835 // Determine the height to use.
829 dimensions.height = std::max(dimensions.height, 836 dimensions.height = std::max(dimensions.height,
830 font.GetHeight() + GetBottomMargin() + GetTopMargin()); 837 font.GetHeight() + GetBottomMargin() + GetTopMargin());
831 dimensions.height = std::max(dimensions.height, 838 dimensions.height = std::max(dimensions.height,
832 MenuConfig::instance().item_min_height); 839 GetMenuConfig().item_min_height);
833 return dimensions; 840 return dimensions;
834 } 841 }
835 842
836 gfx::Size MenuItemView::CalculatePreferredSize() { 843 gfx::Size MenuItemView::CalculatePreferredSize() {
837 MenuItemView::MenuItemDimensions dimensions = GetPreferredDimensions(); 844 MenuItemView::MenuItemDimensions dimensions = GetPreferredDimensions();
838 return gfx::Size(dimensions.standard_width + dimensions.children_width, 845 return gfx::Size(dimensions.standard_width + dimensions.children_width,
839 dimensions.height); 846 dimensions.height);
840 } 847 }
841 848
842 string16 MenuItemView::GetAcceleratorText() { 849 string16 MenuItemView::GetAcceleratorText() {
843 if (id() == kEmptyMenuItemViewID) { 850 if (id() == kEmptyMenuItemViewID) {
844 // Don't query the delegate for menus that represent no children. 851 // Don't query the delegate for menus that represent no children.
845 return string16(); 852 return string16();
846 } 853 }
847 854
848 if(!MenuConfig::instance().show_accelerators) 855 if(!GetMenuConfig().show_accelerators)
849 return string16(); 856 return string16();
850 857
851 ui::Accelerator accelerator; 858 ui::Accelerator accelerator;
852 return (GetDelegate() && 859 return (GetDelegate() &&
853 GetDelegate()->GetAccelerator(GetCommand(), &accelerator)) ? 860 GetDelegate()->GetAccelerator(GetCommand(), &accelerator)) ?
854 accelerator.GetShortcutText() : string16(); 861 accelerator.GetShortcutText() : string16();
855 } 862 }
856 863
857 bool MenuItemView::IsContainer() const { 864 bool MenuItemView::IsContainer() const {
858 // Let the first child take over |this| when we only have one child and no 865 // Let the first child take over |this| when we only have one child and no
(...skipping 16 matching lines...) Expand all
875 temp_width = menu_item->GetMaxIconViewWidth(); 882 temp_width = menu_item->GetMaxIconViewWidth();
876 } else if (menu_item->icon_view()) { 883 } else if (menu_item->icon_view()) {
877 temp_width = menu_item->icon_view()->GetPreferredSize().width(); 884 temp_width = menu_item->icon_view()->GetPreferredSize().width();
878 } 885 }
879 width = std::max(width, temp_width); 886 width = std::max(width, temp_width);
880 } 887 }
881 return width; 888 return width;
882 } 889 }
883 890
884 } // namespace views 891 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698