Explicitly add python-numpy dependency to install-build-deps.
[chromium-blink-merge.git] / ui / views / controls / menu / menu_item_view.cc
blobdecd4bf871211354c1766d2e46e2f0c6baed3f57
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
3 // found in the LICENSE file.
5 #include "ui/views/controls/menu/menu_item_view.h"
7 #include "base/i18n/case_conversion.h"
8 #include "base/stl_util.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "ui/accessibility/ax_view_state.h"
11 #include "ui/base/l10n/l10n_util.h"
12 #include "ui/base/models/menu_model.h"
13 #include "ui/gfx/canvas.h"
14 #include "ui/gfx/geometry/rect.h"
15 #include "ui/gfx/geometry/vector2d.h"
16 #include "ui/gfx/image/image.h"
17 #include "ui/gfx/text_utils.h"
18 #include "ui/native_theme/common_theme.h"
19 #include "ui/resources/grit/ui_resources.h"
20 #include "ui/strings/grit/ui_strings.h"
21 #include "ui/views/controls/button/menu_button.h"
22 #include "ui/views/controls/image_view.h"
23 #include "ui/views/controls/menu/menu_config.h"
24 #include "ui/views/controls/menu/menu_controller.h"
25 #include "ui/views/controls/menu/menu_image_util.h"
26 #include "ui/views/controls/menu/menu_scroll_view_container.h"
27 #include "ui/views/controls/menu/menu_separator.h"
28 #include "ui/views/controls/menu/submenu_view.h"
29 #include "ui/views/widget/widget.h"
31 namespace views {
33 namespace {
35 // EmptyMenuMenuItem ---------------------------------------------------------
37 // EmptyMenuMenuItem is used when a menu has no menu items. EmptyMenuMenuItem
38 // is itself a MenuItemView, but it uses a different ID so that it isn't
39 // identified as a MenuItemView.
41 class EmptyMenuMenuItem : public MenuItemView {
42 public:
43 explicit EmptyMenuMenuItem(MenuItemView* parent)
44 : MenuItemView(parent, 0, EMPTY) {
45 // Set this so that we're not identified as a normal menu item.
46 set_id(kEmptyMenuItemViewID);
47 SetTitle(l10n_util::GetStringUTF16(IDS_APP_MENU_EMPTY_SUBMENU));
48 SetEnabled(false);
51 bool GetTooltipText(const gfx::Point& p,
52 base::string16* tooltip) const override {
53 // Empty menu items shouldn't have a tooltip.
54 return false;
57 private:
58 DISALLOW_COPY_AND_ASSIGN(EmptyMenuMenuItem);
61 } // namespace
63 // Padding between child views.
64 static const int kChildXPadding = 8;
66 // MenuItemView ---------------------------------------------------------------
68 // static
69 const int MenuItemView::kMenuItemViewID = 1001;
71 // static
72 const int MenuItemView::kEmptyMenuItemViewID =
73 MenuItemView::kMenuItemViewID + 1;
75 // static
76 int MenuItemView::icon_area_width_ = 0;
78 // static
79 int MenuItemView::label_start_;
81 // static
82 int MenuItemView::item_right_margin_;
84 // static
85 int MenuItemView::pref_menu_height_;
87 // static
88 const char MenuItemView::kViewClassName[] = "MenuItemView";
90 MenuItemView::MenuItemView(MenuDelegate* delegate)
91 : delegate_(delegate),
92 controller_(NULL),
93 canceled_(false),
94 parent_menu_item_(NULL),
95 type_(SUBMENU),
96 selected_(false),
97 command_(0),
98 submenu_(NULL),
99 has_mnemonics_(false),
100 show_mnemonics_(false),
101 has_icons_(false),
102 icon_view_(NULL),
103 top_margin_(-1),
104 bottom_margin_(-1),
105 left_icon_margin_(0),
106 right_icon_margin_(0),
107 requested_menu_position_(POSITION_BEST_FIT),
108 actual_menu_position_(requested_menu_position_),
109 use_right_margin_(true) {
110 // NOTE: don't check the delegate for NULL, UpdateMenuPartSizes() supplies a
111 // NULL delegate.
112 Init(NULL, 0, SUBMENU, delegate);
115 void MenuItemView::ChildPreferredSizeChanged(View* child) {
116 invalidate_dimensions();
117 PreferredSizeChanged();
120 bool MenuItemView::GetTooltipText(const gfx::Point& p,
121 base::string16* tooltip) const {
122 *tooltip = tooltip_;
123 if (!tooltip->empty())
124 return true;
126 if (GetType() == SEPARATOR)
127 return false;
129 const MenuController* controller = GetMenuController();
130 if (!controller || controller->exit_type() != MenuController::EXIT_NONE) {
131 // Either the menu has been closed or we're in the process of closing the
132 // menu. Don't attempt to query the delegate as it may no longer be valid.
133 return false;
136 const MenuItemView* root_menu_item = GetRootMenuItem();
137 if (root_menu_item->canceled_) {
138 // TODO(sky): if |canceled_| is true, controller->exit_type() should be
139 // something other than EXIT_NONE, but crash reports seem to indicate
140 // otherwise. Figure out why this is needed.
141 return false;
144 const MenuDelegate* delegate = GetDelegate();
145 CHECK(delegate);
146 gfx::Point location(p);
147 ConvertPointToScreen(this, &location);
148 *tooltip = delegate->GetTooltipText(command_, location);
149 return !tooltip->empty();
152 void MenuItemView::GetAccessibleState(ui::AXViewState* state) {
153 state->role = ui::AX_ROLE_MENU_ITEM;
155 base::string16 item_text;
156 if (IsContainer()) {
157 // The first child is taking over, just use its accessible name instead of
158 // |title_|.
159 View* child = child_at(0);
160 ui::AXViewState state;
161 child->GetAccessibleState(&state);
162 item_text = state.name;
163 } else {
164 item_text = title_;
166 state->name = GetAccessibleNameForMenuItem(item_text, GetMinorText());
168 switch (GetType()) {
169 case SUBMENU:
170 state->AddStateFlag(ui::AX_STATE_HASPOPUP);
171 break;
172 case CHECKBOX:
173 case RADIO:
174 if (GetDelegate()->IsItemChecked(GetCommand()))
175 state->AddStateFlag(ui::AX_STATE_CHECKED);
176 break;
177 case NORMAL:
178 case SEPARATOR:
179 case EMPTY:
180 // No additional accessibility states currently for these menu states.
181 break;
185 // static
186 bool MenuItemView::IsBubble(MenuAnchorPosition anchor) {
187 return anchor == MENU_ANCHOR_BUBBLE_LEFT ||
188 anchor == MENU_ANCHOR_BUBBLE_RIGHT ||
189 anchor == MENU_ANCHOR_BUBBLE_ABOVE ||
190 anchor == MENU_ANCHOR_BUBBLE_BELOW;
193 // static
194 base::string16 MenuItemView::GetAccessibleNameForMenuItem(
195 const base::string16& item_text, const base::string16& minor_text) {
196 base::string16 accessible_name = item_text;
198 // Filter out the "&" for accessibility clients.
199 size_t index = 0;
200 const base::char16 amp = '&';
201 while ((index = accessible_name.find(amp, index)) != base::string16::npos &&
202 index + 1 < accessible_name.length()) {
203 accessible_name.replace(index, accessible_name.length() - index,
204 accessible_name.substr(index + 1));
206 // Special case for "&&" (escaped for "&").
207 if (accessible_name[index] == '&')
208 ++index;
211 // Append subtext.
212 if (!minor_text.empty()) {
213 accessible_name.push_back(' ');
214 accessible_name.append(minor_text);
217 return accessible_name;
220 void MenuItemView::Cancel() {
221 if (controller_ && !canceled_) {
222 canceled_ = true;
223 controller_->Cancel(MenuController::EXIT_ALL);
227 MenuItemView* MenuItemView::AddMenuItemAt(
228 int index,
229 int item_id,
230 const base::string16& label,
231 const base::string16& sublabel,
232 const base::string16& minor_text,
233 const gfx::ImageSkia& icon,
234 Type type,
235 ui::MenuSeparatorType separator_style) {
236 DCHECK_NE(type, EMPTY);
237 DCHECK_LE(0, index);
238 if (!submenu_)
239 CreateSubmenu();
240 DCHECK_GE(submenu_->child_count(), index);
241 if (type == SEPARATOR) {
242 submenu_->AddChildViewAt(new MenuSeparator(this, separator_style), index);
243 return NULL;
245 MenuItemView* item = new MenuItemView(this, item_id, type);
246 if (label.empty() && GetDelegate())
247 item->SetTitle(GetDelegate()->GetLabel(item_id));
248 else
249 item->SetTitle(label);
250 item->SetSubtitle(sublabel);
251 item->SetMinorText(minor_text);
252 if (!icon.isNull())
253 item->SetIcon(icon);
254 if (type == SUBMENU)
255 item->CreateSubmenu();
256 if (GetDelegate() && !GetDelegate()->IsCommandVisible(item_id))
257 item->SetVisible(false);
258 submenu_->AddChildViewAt(item, index);
259 return item;
262 void MenuItemView::RemoveMenuItemAt(int index) {
263 DCHECK(submenu_);
264 DCHECK_LE(0, index);
265 DCHECK_GT(submenu_->child_count(), index);
267 View* item = submenu_->child_at(index);
268 DCHECK(item);
269 submenu_->RemoveChildView(item);
271 // RemoveChildView() does not delete the item, which is a good thing
272 // in case a submenu is being displayed while items are being removed.
273 // Deletion will be done by ChildrenChanged() or at destruction.
274 removed_items_.push_back(item);
277 MenuItemView* MenuItemView::AppendMenuItem(int item_id,
278 const base::string16& label,
279 Type type) {
280 return AppendMenuItemImpl(item_id, label, base::string16(), base::string16(),
281 gfx::ImageSkia(), type, ui::NORMAL_SEPARATOR);
284 MenuItemView* MenuItemView::AppendSubMenu(int item_id,
285 const base::string16& label) {
286 return AppendMenuItemImpl(item_id, label, base::string16(), base::string16(),
287 gfx::ImageSkia(), SUBMENU, ui::NORMAL_SEPARATOR);
290 MenuItemView* MenuItemView::AppendSubMenuWithIcon(int item_id,
291 const base::string16& label,
292 const gfx::ImageSkia& icon) {
293 return AppendMenuItemImpl(item_id, label, base::string16(), base::string16(),
294 icon, SUBMENU, ui::NORMAL_SEPARATOR);
297 MenuItemView* MenuItemView::AppendMenuItemWithLabel(
298 int item_id,
299 const base::string16& label) {
300 return AppendMenuItem(item_id, label, NORMAL);
303 MenuItemView* MenuItemView::AppendDelegateMenuItem(int item_id) {
304 return AppendMenuItem(item_id, base::string16(), NORMAL);
307 void MenuItemView::AppendSeparator() {
308 AppendMenuItemImpl(0, base::string16(), base::string16(), base::string16(),
309 gfx::ImageSkia(), SEPARATOR, ui::NORMAL_SEPARATOR);
312 MenuItemView* MenuItemView::AppendMenuItemWithIcon(int item_id,
313 const base::string16& label,
314 const gfx::ImageSkia& icon) {
315 return AppendMenuItemImpl(item_id, label, base::string16(), base::string16(),
316 icon, NORMAL, ui::NORMAL_SEPARATOR);
319 MenuItemView* MenuItemView::AppendMenuItemImpl(
320 int item_id,
321 const base::string16& label,
322 const base::string16& sublabel,
323 const base::string16& minor_text,
324 const gfx::ImageSkia& icon,
325 Type type,
326 ui::MenuSeparatorType separator_style) {
327 const int index = submenu_ ? submenu_->child_count() : 0;
328 return AddMenuItemAt(index, item_id, label, sublabel, minor_text, icon, type,
329 separator_style);
332 SubmenuView* MenuItemView::CreateSubmenu() {
333 if (!submenu_)
334 submenu_ = new SubmenuView(this);
335 return submenu_;
338 bool MenuItemView::HasSubmenu() const {
339 return (submenu_ != NULL);
342 SubmenuView* MenuItemView::GetSubmenu() const {
343 return submenu_;
346 void MenuItemView::SetTitle(const base::string16& title) {
347 title_ = title;
348 invalidate_dimensions(); // Triggers preferred size recalculation.
351 void MenuItemView::SetSubtitle(const base::string16& subtitle) {
352 subtitle_ = subtitle;
353 invalidate_dimensions(); // Triggers preferred size recalculation.
356 void MenuItemView::SetMinorText(const base::string16& minor_text) {
357 minor_text_ = minor_text;
358 invalidate_dimensions(); // Triggers preferred size recalculation.
361 void MenuItemView::SetSelected(bool selected) {
362 selected_ = selected;
363 SchedulePaint();
366 void MenuItemView::SetTooltip(const base::string16& tooltip, int item_id) {
367 MenuItemView* item = GetMenuItemByID(item_id);
368 DCHECK(item);
369 item->tooltip_ = tooltip;
372 void MenuItemView::SetIcon(const gfx::ImageSkia& icon, int item_id) {
373 MenuItemView* item = GetMenuItemByID(item_id);
374 DCHECK(item);
375 item->SetIcon(icon);
378 void MenuItemView::SetIcon(const gfx::ImageSkia& icon) {
379 if (icon.isNull()) {
380 SetIconView(NULL);
381 return;
384 ImageView* icon_view = new ImageView();
385 icon_view->SetImage(&icon);
386 SetIconView(icon_view);
389 void MenuItemView::SetIconView(View* icon_view) {
390 if (icon_view_) {
391 RemoveChildView(icon_view_);
392 delete icon_view_;
393 icon_view_ = NULL;
395 if (icon_view) {
396 AddChildView(icon_view);
397 icon_view_ = icon_view;
399 Layout();
400 SchedulePaint();
403 void MenuItemView::OnPaint(gfx::Canvas* canvas) {
404 PaintButton(canvas, PB_NORMAL);
407 gfx::Size MenuItemView::GetPreferredSize() const {
408 const MenuItemDimensions& dimensions(GetDimensions());
409 return gfx::Size(dimensions.standard_width + dimensions.children_width,
410 dimensions.height);
413 int MenuItemView::GetHeightForWidth(int width) const {
414 // If this isn't a container, we can just use the preferred size's height.
415 if (!IsContainer())
416 return GetPreferredSize().height();
418 int height = child_at(0)->GetHeightForWidth(width);
419 if (!icon_view_ && GetRootMenuItem()->has_icons())
420 height = std::max(height, GetMenuConfig().check_height);
421 height += GetBottomMargin() + GetTopMargin();
423 return height;
426 const MenuItemView::MenuItemDimensions& MenuItemView::GetDimensions() const {
427 if (!is_dimensions_valid())
428 dimensions_ = CalculateDimensions();
429 DCHECK(is_dimensions_valid());
430 return dimensions_;
433 MenuController* MenuItemView::GetMenuController() {
434 return GetRootMenuItem()->controller_;
437 const MenuController* MenuItemView::GetMenuController() const {
438 return GetRootMenuItem()->controller_;
441 MenuDelegate* MenuItemView::GetDelegate() {
442 return GetRootMenuItem()->delegate_;
445 const MenuDelegate* MenuItemView::GetDelegate() const {
446 return GetRootMenuItem()->delegate_;
449 MenuItemView* MenuItemView::GetRootMenuItem() {
450 return const_cast<MenuItemView*>(
451 static_cast<const MenuItemView*>(this)->GetRootMenuItem());
454 const MenuItemView* MenuItemView::GetRootMenuItem() const {
455 const MenuItemView* item = this;
456 for (const MenuItemView* parent = GetParentMenuItem(); parent;
457 parent = item->GetParentMenuItem())
458 item = parent;
459 return item;
462 base::char16 MenuItemView::GetMnemonic() {
463 if (!GetRootMenuItem()->has_mnemonics_)
464 return 0;
466 size_t index = 0;
467 do {
468 index = title_.find('&', index);
469 if (index != base::string16::npos) {
470 if (index + 1 != title_.size() && title_[index + 1] != '&') {
471 base::char16 char_array[] = { title_[index + 1], 0 };
472 // TODO(jshin): What about Turkish locale? See http://crbug.com/81719.
473 // If the mnemonic is capital I and the UI language is Turkish,
474 // lowercasing it results in 'small dotless i', which is different
475 // from a 'dotted i'. Similar issues may exist for az and lt locales.
476 return base::i18n::ToLower(char_array)[0];
478 index++;
480 } while (index != base::string16::npos);
481 return 0;
484 MenuItemView* MenuItemView::GetMenuItemByID(int id) {
485 if (GetCommand() == id)
486 return this;
487 if (!HasSubmenu())
488 return NULL;
489 for (int i = 0; i < GetSubmenu()->child_count(); ++i) {
490 View* child = GetSubmenu()->child_at(i);
491 if (child->id() == MenuItemView::kMenuItemViewID) {
492 MenuItemView* result = static_cast<MenuItemView*>(child)->
493 GetMenuItemByID(id);
494 if (result)
495 return result;
498 return NULL;
501 void MenuItemView::ChildrenChanged() {
502 MenuController* controller = GetMenuController();
503 if (controller) {
504 // Handles the case where we were empty and are no longer empty.
505 RemoveEmptyMenus();
507 // Handles the case where we were not empty, but now are.
508 AddEmptyMenus();
510 controller->MenuChildrenChanged(this);
512 if (submenu_) {
513 // Force a paint and layout. This handles the case of the top
514 // level window's size remaining the same, resulting in no
515 // change to the submenu's size and no layout.
516 submenu_->Layout();
517 submenu_->SchedulePaint();
518 // Update the menu selection after layout.
519 controller->UpdateSubmenuSelection(submenu_);
523 STLDeleteElements(&removed_items_);
526 void MenuItemView::Layout() {
527 if (!has_children())
528 return;
530 if (IsContainer()) {
531 View* child = child_at(0);
532 gfx::Size size = child->GetPreferredSize();
533 child->SetBounds(0, GetTopMargin(), size.width(), size.height());
534 } else {
535 // Child views are laid out right aligned and given the full height. To
536 // right align start with the last view and progress to the first.
537 int x = width() - (use_right_margin_ ? item_right_margin_ : 0);
538 for (int i = child_count() - 1; i >= 0; --i) {
539 View* child = child_at(i);
540 if (icon_view_ && (icon_view_ == child))
541 continue;
542 int width = child->GetPreferredSize().width();
543 child->SetBounds(x - width, 0, width, height());
544 x -= width - kChildXPadding;
546 // Position |icon_view|.
547 const MenuConfig& config = GetMenuConfig();
548 if (icon_view_) {
549 icon_view_->SizeToPreferredSize();
550 gfx::Size size = icon_view_->GetPreferredSize();
551 int x = config.item_left_margin + left_icon_margin_ +
552 (icon_area_width_ - size.width()) / 2;
553 if (type_ == CHECKBOX || type_ == RADIO)
554 x = label_start_;
555 int y =
556 (height() + GetTopMargin() - GetBottomMargin() - size.height()) / 2;
557 icon_view_->SetPosition(gfx::Point(x, y));
562 void MenuItemView::SetMargins(int top_margin, int bottom_margin) {
563 top_margin_ = top_margin;
564 bottom_margin_ = bottom_margin;
566 invalidate_dimensions();
569 const MenuConfig& MenuItemView::GetMenuConfig() const {
570 const MenuController* controller = GetMenuController();
571 if (controller)
572 return controller->menu_config_;
573 return MenuConfig::instance(NULL);
576 MenuItemView::MenuItemView(MenuItemView* parent,
577 int command,
578 MenuItemView::Type type)
579 : delegate_(NULL),
580 controller_(NULL),
581 canceled_(false),
582 parent_menu_item_(parent),
583 type_(type),
584 selected_(false),
585 command_(command),
586 submenu_(NULL),
587 has_mnemonics_(false),
588 show_mnemonics_(false),
589 has_icons_(false),
590 icon_view_(NULL),
591 top_margin_(-1),
592 bottom_margin_(-1),
593 left_icon_margin_(0),
594 right_icon_margin_(0),
595 requested_menu_position_(POSITION_BEST_FIT),
596 actual_menu_position_(requested_menu_position_),
597 use_right_margin_(true) {
598 Init(parent, command, type, NULL);
601 MenuItemView::~MenuItemView() {
602 delete submenu_;
603 STLDeleteElements(&removed_items_);
606 const char* MenuItemView::GetClassName() const {
607 return kViewClassName;
610 // Calculates all sizes that we can from the OS.
612 // This is invoked prior to Running a menu.
613 void MenuItemView::UpdateMenuPartSizes() {
614 const MenuConfig& config = GetMenuConfig();
616 item_right_margin_ = config.label_to_arrow_padding + config.arrow_width +
617 config.arrow_to_edge_padding;
618 icon_area_width_ = config.check_width;
619 if (has_icons_)
620 icon_area_width_ = std::max(icon_area_width_, GetMaxIconViewWidth());
622 label_start_ = config.item_left_margin + icon_area_width_;
623 int padding = 0;
624 if (config.always_use_icon_to_label_padding) {
625 padding = config.icon_to_label_padding;
626 } else {
627 padding = (has_icons_ || HasChecksOrRadioButtons()) ?
628 config.icon_to_label_padding : 0;
630 label_start_ += padding;
632 EmptyMenuMenuItem menu_item(this);
633 menu_item.set_controller(GetMenuController());
634 pref_menu_height_ = menu_item.GetPreferredSize().height();
637 void MenuItemView::Init(MenuItemView* parent,
638 int command,
639 MenuItemView::Type type,
640 MenuDelegate* delegate) {
641 delegate_ = delegate;
642 controller_ = NULL;
643 canceled_ = false;
644 parent_menu_item_ = parent;
645 type_ = type;
646 selected_ = false;
647 command_ = command;
648 submenu_ = NULL;
649 show_mnemonics_ = false;
650 // Assign our ID, this allows SubmenuItemView to find MenuItemViews.
651 set_id(kMenuItemViewID);
652 has_icons_ = false;
654 // Don't request enabled status from the root menu item as it is just
655 // a container for real items. EMPTY items will be disabled.
656 MenuDelegate* root_delegate = GetDelegate();
657 if (parent && type != EMPTY && root_delegate)
658 SetEnabled(root_delegate->IsCommandEnabled(command));
661 void MenuItemView::PrepareForRun(bool is_first_menu,
662 bool has_mnemonics,
663 bool show_mnemonics) {
664 // Currently we only support showing the root.
665 DCHECK(!parent_menu_item_);
667 // Force us to have a submenu.
668 CreateSubmenu();
669 actual_menu_position_ = requested_menu_position_;
670 canceled_ = false;
672 has_mnemonics_ = has_mnemonics;
673 show_mnemonics_ = has_mnemonics && show_mnemonics;
675 AddEmptyMenus();
677 if (is_first_menu) {
678 // Only update the menu size if there are no menus showing, otherwise
679 // things may shift around.
680 UpdateMenuPartSizes();
684 int MenuItemView::GetDrawStringFlags() {
685 int flags = 0;
686 if (base::i18n::IsRTL())
687 flags |= gfx::Canvas::TEXT_ALIGN_RIGHT;
688 else
689 flags |= gfx::Canvas::TEXT_ALIGN_LEFT;
691 if (GetRootMenuItem()->has_mnemonics_) {
692 if (GetMenuConfig().show_mnemonics || GetRootMenuItem()->show_mnemonics_) {
693 flags |= gfx::Canvas::SHOW_PREFIX;
694 } else {
695 flags |= gfx::Canvas::HIDE_PREFIX;
698 return flags;
701 const gfx::FontList& MenuItemView::GetFontList() const {
702 const MenuDelegate* delegate = GetDelegate();
703 if (delegate) {
704 const gfx::FontList* font_list = delegate->GetLabelFontList(GetCommand());
705 if (font_list)
706 return *font_list;
708 return GetMenuConfig().font_list;
711 void MenuItemView::AddEmptyMenus() {
712 DCHECK(HasSubmenu());
713 if (!submenu_->has_children()) {
714 submenu_->AddChildViewAt(new EmptyMenuMenuItem(this), 0);
715 } else {
716 for (int i = 0, item_count = submenu_->GetMenuItemCount(); i < item_count;
717 ++i) {
718 MenuItemView* child = submenu_->GetMenuItemAt(i);
719 if (child->HasSubmenu())
720 child->AddEmptyMenus();
725 void MenuItemView::RemoveEmptyMenus() {
726 DCHECK(HasSubmenu());
727 // Iterate backwards as we may end up removing views, which alters the child
728 // view count.
729 for (int i = submenu_->child_count() - 1; i >= 0; --i) {
730 View* child = submenu_->child_at(i);
731 if (child->id() == MenuItemView::kMenuItemViewID) {
732 MenuItemView* menu_item = static_cast<MenuItemView*>(child);
733 if (menu_item->HasSubmenu())
734 menu_item->RemoveEmptyMenus();
735 } else if (child->id() == EmptyMenuMenuItem::kEmptyMenuItemViewID) {
736 submenu_->RemoveChildView(child);
737 delete child;
738 child = NULL;
743 void MenuItemView::AdjustBoundsForRTLUI(gfx::Rect* rect) const {
744 rect->set_x(GetMirroredXForRect(*rect));
747 void MenuItemView::PaintButton(gfx::Canvas* canvas, PaintButtonMode mode) {
748 const MenuConfig& config = GetMenuConfig();
749 bool render_selection =
750 (mode == PB_NORMAL && IsSelected() &&
751 parent_menu_item_->GetSubmenu()->GetShowSelection(this) &&
752 (NonIconChildViewsCount() == 0));
754 MenuDelegate *delegate = GetDelegate();
755 // Render the background. As MenuScrollViewContainer draws the background, we
756 // only need the background when we want it to look different, as when we're
757 // selected.
758 ui::NativeTheme* native_theme = GetNativeTheme();
759 SkColor override_color;
760 if (delegate && delegate->GetBackgroundColor(GetCommand(),
761 render_selection,
762 &override_color)) {
763 canvas->DrawColor(override_color);
764 } else if (render_selection) {
765 gfx::Rect item_bounds(0, 0, width(), height());
766 AdjustBoundsForRTLUI(&item_bounds);
768 native_theme->Paint(canvas->sk_canvas(),
769 ui::NativeTheme::kMenuItemBackground,
770 ui::NativeTheme::kHovered,
771 item_bounds,
772 ui::NativeTheme::ExtraParams());
775 const int icon_x = config.item_left_margin + left_icon_margin_;
776 const int top_margin = GetTopMargin();
777 const int bottom_margin = GetBottomMargin();
778 const int available_height = height() - top_margin - bottom_margin;
780 // Render the check.
781 if (type_ == CHECKBOX && delegate->IsItemChecked(GetCommand())) {
782 gfx::ImageSkia check = GetMenuCheckImage(render_selection);
783 // Don't use config.check_width here as it's padded
784 // to force more padding (AURA).
785 gfx::Rect check_bounds(icon_x,
786 top_margin + (available_height - check.height()) / 2,
787 check.width(),
788 check.height());
789 AdjustBoundsForRTLUI(&check_bounds);
790 canvas->DrawImageInt(check, check_bounds.x(), check_bounds.y());
791 } else if (type_ == RADIO) {
792 gfx::ImageSkia image =
793 GetRadioButtonImage(delegate->IsItemChecked(GetCommand()));
794 gfx::Rect radio_bounds(icon_x,
795 top_margin + (available_height - image.height()) / 2,
796 image.width(),
797 image.height());
798 AdjustBoundsForRTLUI(&radio_bounds);
799 canvas->DrawImageInt(image, radio_bounds.x(), radio_bounds.y());
802 // Render the foreground.
803 ui::NativeTheme::ColorId color_id;
804 if (enabled()) {
805 color_id = render_selection ?
806 ui::NativeTheme::kColorId_SelectedMenuItemForegroundColor:
807 ui::NativeTheme::kColorId_EnabledMenuItemForegroundColor;
808 } else {
809 bool emphasized = delegate &&
810 delegate->GetShouldUseDisabledEmphasizedForegroundColor(
811 GetCommand());
812 color_id = emphasized ?
813 ui::NativeTheme::kColorId_DisabledEmphasizedMenuItemForegroundColor :
814 ui::NativeTheme::kColorId_DisabledMenuItemForegroundColor;
816 SkColor fg_color = native_theme->GetSystemColor(color_id);
817 SkColor override_foreground_color;
818 if (delegate && delegate->GetForegroundColor(GetCommand(),
819 render_selection,
820 &override_foreground_color))
821 fg_color = override_foreground_color;
823 const gfx::FontList& font_list = GetFontList();
824 int accel_width = parent_menu_item_->GetSubmenu()->max_minor_text_width();
825 int label_start = GetLabelStartForThisItem();
827 int width = this->width() - label_start - accel_width -
828 (!delegate ||
829 delegate->ShouldReserveSpaceForSubmenuIndicator() ?
830 item_right_margin_ : config.arrow_to_edge_padding);
831 gfx::Rect text_bounds(label_start, top_margin, width,
832 subtitle_.empty() ? available_height
833 : available_height / 2);
834 text_bounds.set_x(GetMirroredXForRect(text_bounds));
835 int flags = GetDrawStringFlags();
836 if (mode == PB_FOR_DRAG)
837 flags |= gfx::Canvas::NO_SUBPIXEL_RENDERING;
838 canvas->DrawStringRectWithFlags(title(), font_list, fg_color, text_bounds,
839 flags);
840 if (!subtitle_.empty()) {
841 canvas->DrawStringRectWithFlags(
842 subtitle_,
843 font_list,
844 GetNativeTheme()->GetSystemColor(
845 ui::NativeTheme::kColorId_ButtonDisabledColor),
846 text_bounds + gfx::Vector2d(0, font_list.GetHeight()),
847 flags);
850 PaintMinorText(canvas, render_selection);
852 // Render the submenu indicator (arrow).
853 if (HasSubmenu()) {
854 gfx::ImageSkia arrow = GetSubmenuArrowImage(render_selection);
855 gfx::Rect arrow_bounds(this->width() - config.arrow_width -
856 config.arrow_to_edge_padding,
857 top_margin + (available_height - arrow.height()) / 2,
858 config.arrow_width,
859 arrow.height());
860 AdjustBoundsForRTLUI(&arrow_bounds);
861 canvas->DrawImageInt(arrow, arrow_bounds.x(), arrow_bounds.y());
865 void MenuItemView::PaintMinorText(gfx::Canvas* canvas,
866 bool render_selection) {
867 base::string16 minor_text = GetMinorText();
868 if (minor_text.empty())
869 return;
871 int available_height = height() - GetTopMargin() - GetBottomMargin();
872 int max_accel_width =
873 parent_menu_item_->GetSubmenu()->max_minor_text_width();
874 const MenuConfig& config = GetMenuConfig();
875 int accel_right_margin = config.align_arrow_and_shortcut ?
876 config.arrow_to_edge_padding : item_right_margin_;
877 gfx::Rect accel_bounds(width() - accel_right_margin - max_accel_width,
878 GetTopMargin(), max_accel_width, available_height);
879 accel_bounds.set_x(GetMirroredXForRect(accel_bounds));
880 int flags = GetDrawStringFlags();
881 flags &= ~(gfx::Canvas::TEXT_ALIGN_RIGHT | gfx::Canvas::TEXT_ALIGN_LEFT);
882 if (base::i18n::IsRTL())
883 flags |= gfx::Canvas::TEXT_ALIGN_LEFT;
884 else
885 flags |= gfx::Canvas::TEXT_ALIGN_RIGHT;
886 canvas->DrawStringRectWithFlags(
887 minor_text,
888 GetFontList(),
889 GetNativeTheme()->GetSystemColor(render_selection ?
890 ui::NativeTheme::kColorId_SelectedMenuItemForegroundColor :
891 ui::NativeTheme::kColorId_ButtonDisabledColor),
892 accel_bounds,
893 flags);
896 void MenuItemView::DestroyAllMenuHosts() {
897 if (!HasSubmenu())
898 return;
900 submenu_->Close();
901 for (int i = 0, item_count = submenu_->GetMenuItemCount(); i < item_count;
902 ++i) {
903 submenu_->GetMenuItemAt(i)->DestroyAllMenuHosts();
907 int MenuItemView::GetTopMargin() const {
908 if (top_margin_ >= 0)
909 return top_margin_;
911 const MenuItemView* root = GetRootMenuItem();
912 return root && root->has_icons_
913 ? GetMenuConfig().item_top_margin :
914 GetMenuConfig().item_no_icon_top_margin;
917 int MenuItemView::GetBottomMargin() const {
918 if (bottom_margin_ >= 0)
919 return bottom_margin_;
921 const MenuItemView* root = GetRootMenuItem();
922 return root && root->has_icons_
923 ? GetMenuConfig().item_bottom_margin :
924 GetMenuConfig().item_no_icon_bottom_margin;
927 gfx::Size MenuItemView::GetChildPreferredSize() const {
928 if (!has_children())
929 return gfx::Size();
931 if (IsContainer())
932 return child_at(0)->GetPreferredSize();
934 int width = 0;
935 for (int i = 0; i < child_count(); ++i) {
936 const View* child = child_at(i);
937 if (icon_view_ && (icon_view_ == child))
938 continue;
939 if (i)
940 width += kChildXPadding;
941 width += child->GetPreferredSize().width();
943 int height = 0;
944 if (icon_view_)
945 height = icon_view_->GetPreferredSize().height();
947 // If there is no icon view it returns a height of 0 to indicate that
948 // we should use the title height instead.
949 return gfx::Size(width, height);
952 MenuItemView::MenuItemDimensions MenuItemView::CalculateDimensions() const {
953 gfx::Size child_size = GetChildPreferredSize();
955 MenuItemDimensions dimensions;
956 // Get the container height.
957 dimensions.children_width = child_size.width();
958 dimensions.height = child_size.height();
959 // Adjust item content height if menu has both items with and without icons.
960 // This way all menu items will have the same height.
961 if (!icon_view_ && GetRootMenuItem()->has_icons()) {
962 dimensions.height = std::max(dimensions.height,
963 GetMenuConfig().check_height);
965 dimensions.height += GetBottomMargin() + GetTopMargin();
967 // In case of a container, only the container size needs to be filled.
968 if (IsContainer())
969 return dimensions;
971 // Determine the length of the label text.
972 const gfx::FontList& font_list = GetFontList();
974 // Get Icon margin overrides for this particular item.
975 const MenuDelegate* delegate = GetDelegate();
976 if (delegate) {
977 delegate->GetHorizontalIconMargins(command_,
978 icon_area_width_,
979 &left_icon_margin_,
980 &right_icon_margin_);
981 } else {
982 left_icon_margin_ = 0;
983 right_icon_margin_ = 0;
985 int label_start = GetLabelStartForThisItem();
987 int string_width = gfx::GetStringWidth(title_, font_list);
988 if (!subtitle_.empty()) {
989 string_width = std::max(string_width,
990 gfx::GetStringWidth(subtitle_, font_list));
993 dimensions.standard_width = string_width + label_start +
994 item_right_margin_;
995 // Determine the length of the right-side text.
996 base::string16 minor_text = GetMinorText();
997 dimensions.minor_text_width =
998 minor_text.empty() ? 0 : gfx::GetStringWidth(minor_text, font_list);
1000 // Determine the height to use.
1001 dimensions.height =
1002 std::max(dimensions.height,
1003 (subtitle_.empty() ? 0 : font_list.GetHeight()) +
1004 font_list.GetHeight() + GetBottomMargin() + GetTopMargin());
1005 dimensions.height = std::max(dimensions.height,
1006 GetMenuConfig().item_min_height);
1007 return dimensions;
1010 int MenuItemView::GetLabelStartForThisItem() const {
1011 int label_start = label_start_ + left_icon_margin_ + right_icon_margin_;
1012 if ((type_ == CHECKBOX || type_ == RADIO) && icon_view_) {
1013 label_start += icon_view_->size().width() +
1014 GetMenuConfig().icon_to_label_padding;
1016 return label_start;
1019 base::string16 MenuItemView::GetMinorText() const {
1020 if (id() == kEmptyMenuItemViewID) {
1021 // Don't query the delegate for menus that represent no children.
1022 return base::string16();
1025 ui::Accelerator accelerator;
1026 if (GetMenuConfig().show_accelerators && GetDelegate() && GetCommand() &&
1027 GetDelegate()->GetAccelerator(GetCommand(), &accelerator)) {
1028 return accelerator.GetShortcutText();
1031 return minor_text_;
1034 bool MenuItemView::IsContainer() const {
1035 // Let the first child take over |this| when we only have one child and no
1036 // title.
1037 return (NonIconChildViewsCount() == 1) && title_.empty();
1040 int MenuItemView::NonIconChildViewsCount() const {
1041 // Note that what child_count() returns is the number of children,
1042 // not the number of menu items.
1043 return child_count() - (icon_view_ ? 1 : 0);
1046 int MenuItemView::GetMaxIconViewWidth() const {
1047 int width = 0;
1048 for (int i = 0; i < submenu_->GetMenuItemCount(); ++i) {
1049 MenuItemView* menu_item = submenu_->GetMenuItemAt(i);
1050 int temp_width = 0;
1051 if (menu_item->GetType() == CHECKBOX ||
1052 menu_item->GetType() == RADIO) {
1053 // If this item has a radio or checkbox, the icon will not affect
1054 // alignment of other items.
1055 continue;
1056 } else if (menu_item->HasSubmenu()) {
1057 temp_width = menu_item->GetMaxIconViewWidth();
1058 } else if (menu_item->icon_view()) {
1059 temp_width = menu_item->icon_view()->GetPreferredSize().width();
1061 width = std::max(width, temp_width);
1063 return width;
1066 bool MenuItemView::HasChecksOrRadioButtons() const {
1067 for (int i = 0; i < submenu_->GetMenuItemCount(); ++i) {
1068 MenuItemView* menu_item = submenu_->GetMenuItemAt(i);
1069 if (menu_item->HasSubmenu()) {
1070 if (menu_item->HasChecksOrRadioButtons())
1071 return true;
1072 } else {
1073 const Type& type = menu_item->GetType();
1074 if (type == CHECKBOX || type == RADIO)
1075 return true;
1078 return false;
1081 } // namespace views