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"
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
{
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
));
51 virtual bool GetTooltipText(const gfx::Point
& p
,
52 base::string16
* tooltip
) const OVERRIDE
{
53 // Empty menu items shouldn't have a tooltip.
58 DISALLOW_COPY_AND_ASSIGN(EmptyMenuMenuItem
);
63 // Padding between child views.
64 static const int kChildXPadding
= 8;
66 // MenuItemView ---------------------------------------------------------------
69 const int MenuItemView::kMenuItemViewID
= 1001;
72 const int MenuItemView::kEmptyMenuItemViewID
=
73 MenuItemView::kMenuItemViewID
+ 1;
76 int MenuItemView::icon_area_width_
= 0;
79 int MenuItemView::label_start_
;
82 int MenuItemView::item_right_margin_
;
85 int MenuItemView::pref_menu_height_
;
88 const char MenuItemView::kViewClassName
[] = "MenuItemView";
90 MenuItemView::MenuItemView(MenuDelegate
* delegate
)
91 : delegate_(delegate
),
94 parent_menu_item_(NULL
),
99 has_mnemonics_(false),
100 show_mnemonics_(false),
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
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 {
123 if (!tooltip
->empty())
126 if (GetType() == SEPARATOR
)
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.
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.
144 const MenuDelegate
* delegate
= GetDelegate();
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
;
157 // The first child is taking over, just use its accessible name instead of
159 View
* child
= child_at(0);
160 ui::AXViewState state
;
161 child
->GetAccessibleState(&state
);
162 item_text
= state
.name
;
166 state
->name
= GetAccessibleNameForMenuItem(item_text
, GetMinorText());
170 state
->AddStateFlag(ui::AX_STATE_HASPOPUP
);
174 if (GetDelegate()->IsItemChecked(GetCommand()))
175 state
->AddStateFlag(ui::AX_STATE_CHECKED
);
180 // No additional accessibility states currently for these menu states.
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
;
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.
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
] == '&')
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_
) {
223 controller_
->Cancel(MenuController::EXIT_ALL
);
227 MenuItemView
* MenuItemView::AddMenuItemAt(
230 const base::string16
& label
,
231 const base::string16
& sublabel
,
232 const base::string16
& minor_text
,
233 const gfx::ImageSkia
& icon
,
235 ui::MenuSeparatorType separator_style
) {
236 DCHECK_NE(type
, EMPTY
);
240 DCHECK_GE(submenu_
->child_count(), index
);
241 if (type
== SEPARATOR
) {
242 submenu_
->AddChildViewAt(new MenuSeparator(this, separator_style
), index
);
245 MenuItemView
* item
= new MenuItemView(this, item_id
, type
);
246 if (label
.empty() && GetDelegate())
247 item
->SetTitle(GetDelegate()->GetLabel(item_id
));
249 item
->SetTitle(label
);
250 item
->SetSubtitle(sublabel
);
251 item
->SetMinorText(minor_text
);
255 item
->CreateSubmenu();
256 if (GetDelegate() && !GetDelegate()->IsCommandVisible(item_id
))
257 item
->SetVisible(false);
258 submenu_
->AddChildViewAt(item
, index
);
262 void MenuItemView::RemoveMenuItemAt(int index
) {
265 DCHECK_GT(submenu_
->child_count(), index
);
267 View
* item
= submenu_
->child_at(index
);
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
,
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(
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(
321 const base::string16
& label
,
322 const base::string16
& sublabel
,
323 const base::string16
& minor_text
,
324 const gfx::ImageSkia
& icon
,
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
,
332 SubmenuView
* MenuItemView::CreateSubmenu() {
334 submenu_
= new SubmenuView(this);
338 bool MenuItemView::HasSubmenu() const {
339 return (submenu_
!= NULL
);
342 SubmenuView
* MenuItemView::GetSubmenu() const {
346 void MenuItemView::SetTitle(const base::string16
& 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
;
366 void MenuItemView::SetTooltip(const base::string16
& tooltip
, int item_id
) {
367 MenuItemView
* item
= GetMenuItemByID(item_id
);
369 item
->tooltip_
= tooltip
;
372 void MenuItemView::SetIcon(const gfx::ImageSkia
& icon
, int item_id
) {
373 MenuItemView
* item
= GetMenuItemByID(item_id
);
378 void MenuItemView::SetIcon(const gfx::ImageSkia
& icon
) {
384 ImageView
* icon_view
= new ImageView();
385 icon_view
->SetImage(&icon
);
386 SetIconView(icon_view
);
389 void MenuItemView::SetIconView(View
* icon_view
) {
391 RemoveChildView(icon_view_
);
396 AddChildView(icon_view
);
397 icon_view_
= icon_view
;
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
,
413 const MenuItemView::MenuItemDimensions
& MenuItemView::GetDimensions() const {
414 if (!is_dimensions_valid())
415 dimensions_
= CalculateDimensions();
416 DCHECK(is_dimensions_valid());
420 MenuController
* MenuItemView::GetMenuController() {
421 return GetRootMenuItem()->controller_
;
424 const MenuController
* MenuItemView::GetMenuController() const {
425 return GetRootMenuItem()->controller_
;
428 MenuDelegate
* MenuItemView::GetDelegate() {
429 return GetRootMenuItem()->delegate_
;
432 const MenuDelegate
* MenuItemView::GetDelegate() const {
433 return GetRootMenuItem()->delegate_
;
436 MenuItemView
* MenuItemView::GetRootMenuItem() {
437 return const_cast<MenuItemView
*>(
438 static_cast<const MenuItemView
*>(this)->GetRootMenuItem());
441 const MenuItemView
* MenuItemView::GetRootMenuItem() const {
442 const MenuItemView
* item
= this;
443 for (const MenuItemView
* parent
= GetParentMenuItem(); parent
;
444 parent
= item
->GetParentMenuItem())
449 base::char16
MenuItemView::GetMnemonic() {
450 if (!GetRootMenuItem()->has_mnemonics_
)
455 index
= title_
.find('&', index
);
456 if (index
!= base::string16::npos
) {
457 if (index
+ 1 != title_
.size() && title_
[index
+ 1] != '&') {
458 base::char16 char_array
[] = { title_
[index
+ 1], 0 };
459 // TODO(jshin): What about Turkish locale? See http://crbug.com/81719.
460 // If the mnemonic is capital I and the UI language is Turkish,
461 // lowercasing it results in 'small dotless i', which is different
462 // from a 'dotted i'. Similar issues may exist for az and lt locales.
463 return base::i18n::ToLower(char_array
)[0];
467 } while (index
!= base::string16::npos
);
471 MenuItemView
* MenuItemView::GetMenuItemByID(int id
) {
472 if (GetCommand() == id
)
476 for (int i
= 0; i
< GetSubmenu()->child_count(); ++i
) {
477 View
* child
= GetSubmenu()->child_at(i
);
478 if (child
->id() == MenuItemView::kMenuItemViewID
) {
479 MenuItemView
* result
= static_cast<MenuItemView
*>(child
)->
488 void MenuItemView::ChildrenChanged() {
489 MenuController
* controller
= GetMenuController();
491 // Handles the case where we were empty and are no longer empty.
494 // Handles the case where we were not empty, but now are.
497 controller
->MenuChildrenChanged(this);
500 // Force a paint and layout. This handles the case of the top
501 // level window's size remaining the same, resulting in no
502 // change to the submenu's size and no layout.
504 submenu_
->SchedulePaint();
505 // Update the menu selection after layout.
506 controller
->UpdateSubmenuSelection(submenu_
);
510 STLDeleteElements(&removed_items_
);
513 void MenuItemView::Layout() {
518 View
* child
= child_at(0);
519 gfx::Size size
= child
->GetPreferredSize();
520 child
->SetBounds(0, GetTopMargin(), size
.width(), size
.height());
522 // Child views are laid out right aligned and given the full height. To
523 // right align start with the last view and progress to the first.
524 int x
= width() - (use_right_margin_
? item_right_margin_
: 0);
525 for (int i
= child_count() - 1; i
>= 0; --i
) {
526 View
* child
= child_at(i
);
527 if (icon_view_
&& (icon_view_
== child
))
529 int width
= child
->GetPreferredSize().width();
530 child
->SetBounds(x
- width
, 0, width
, height());
531 x
-= width
- kChildXPadding
;
533 // Position |icon_view|.
534 const MenuConfig
& config
= GetMenuConfig();
536 icon_view_
->SizeToPreferredSize();
537 gfx::Size size
= icon_view_
->GetPreferredSize();
538 int x
= config
.item_left_margin
+ left_icon_margin_
+
539 (icon_area_width_
- size
.width()) / 2;
540 if (type_
== CHECKBOX
|| type_
== RADIO
)
543 (height() + GetTopMargin() - GetBottomMargin() - size
.height()) / 2;
544 icon_view_
->SetPosition(gfx::Point(x
, y
));
549 void MenuItemView::SetMargins(int top_margin
, int bottom_margin
) {
550 top_margin_
= top_margin
;
551 bottom_margin_
= bottom_margin
;
553 invalidate_dimensions();
556 const MenuConfig
& MenuItemView::GetMenuConfig() const {
557 const MenuController
* controller
= GetMenuController();
559 return controller
->menu_config_
;
560 return MenuConfig::instance(NULL
);
563 MenuItemView::MenuItemView(MenuItemView
* parent
,
565 MenuItemView::Type type
)
569 parent_menu_item_(parent
),
574 has_mnemonics_(false),
575 show_mnemonics_(false),
580 left_icon_margin_(0),
581 right_icon_margin_(0),
582 requested_menu_position_(POSITION_BEST_FIT
),
583 actual_menu_position_(requested_menu_position_
),
584 use_right_margin_(true) {
585 Init(parent
, command
, type
, NULL
);
588 MenuItemView::~MenuItemView() {
590 STLDeleteElements(&removed_items_
);
593 const char* MenuItemView::GetClassName() const {
594 return kViewClassName
;
597 // Calculates all sizes that we can from the OS.
599 // This is invoked prior to Running a menu.
600 void MenuItemView::UpdateMenuPartSizes() {
601 const MenuConfig
& config
= GetMenuConfig();
603 item_right_margin_
= config
.label_to_arrow_padding
+ config
.arrow_width
+
604 config
.arrow_to_edge_padding
;
605 icon_area_width_
= config
.check_width
;
607 icon_area_width_
= std::max(icon_area_width_
, GetMaxIconViewWidth());
609 label_start_
= config
.item_left_margin
+ icon_area_width_
;
611 if (config
.always_use_icon_to_label_padding
) {
612 padding
= config
.icon_to_label_padding
;
613 } else if (config
.render_gutter
) {
614 padding
= config
.item_left_margin
;
616 padding
= (has_icons_
|| HasChecksOrRadioButtons()) ?
617 config
.icon_to_label_padding
: 0;
619 label_start_
+= padding
;
621 if (config
.render_gutter
)
622 label_start_
+= config
.gutter_width
+ config
.gutter_to_label
;
624 EmptyMenuMenuItem
menu_item(this);
625 menu_item
.set_controller(GetMenuController());
626 pref_menu_height_
= menu_item
.GetPreferredSize().height();
629 void MenuItemView::Init(MenuItemView
* parent
,
631 MenuItemView::Type type
,
632 MenuDelegate
* delegate
) {
633 delegate_
= delegate
;
636 parent_menu_item_
= parent
;
641 show_mnemonics_
= false;
642 // Assign our ID, this allows SubmenuItemView to find MenuItemViews.
643 set_id(kMenuItemViewID
);
646 // Don't request enabled status from the root menu item as it is just
647 // a container for real items. EMPTY items will be disabled.
648 MenuDelegate
* root_delegate
= GetDelegate();
649 if (parent
&& type
!= EMPTY
&& root_delegate
)
650 SetEnabled(root_delegate
->IsCommandEnabled(command
));
653 void MenuItemView::PrepareForRun(bool is_first_menu
,
655 bool show_mnemonics
) {
656 // Currently we only support showing the root.
657 DCHECK(!parent_menu_item_
);
659 // Force us to have a submenu.
661 actual_menu_position_
= requested_menu_position_
;
664 has_mnemonics_
= has_mnemonics
;
665 show_mnemonics_
= has_mnemonics
&& show_mnemonics
;
670 // Only update the menu size if there are no menus showing, otherwise
671 // things may shift around.
672 UpdateMenuPartSizes();
676 int MenuItemView::GetDrawStringFlags() {
678 if (base::i18n::IsRTL())
679 flags
|= gfx::Canvas::TEXT_ALIGN_RIGHT
;
681 flags
|= gfx::Canvas::TEXT_ALIGN_LEFT
;
683 if (GetRootMenuItem()->has_mnemonics_
) {
684 if (GetMenuConfig().show_mnemonics
|| GetRootMenuItem()->show_mnemonics_
) {
685 flags
|= gfx::Canvas::SHOW_PREFIX
;
687 flags
|= gfx::Canvas::HIDE_PREFIX
;
693 const gfx::FontList
& MenuItemView::GetFontList() const {
694 const MenuDelegate
* delegate
= GetDelegate();
696 const gfx::FontList
* font_list
= delegate
->GetLabelFontList(GetCommand());
700 return GetMenuConfig().font_list
;
703 void MenuItemView::AddEmptyMenus() {
704 DCHECK(HasSubmenu());
705 if (!submenu_
->has_children()) {
706 submenu_
->AddChildViewAt(new EmptyMenuMenuItem(this), 0);
708 for (int i
= 0, item_count
= submenu_
->GetMenuItemCount(); i
< item_count
;
710 MenuItemView
* child
= submenu_
->GetMenuItemAt(i
);
711 if (child
->HasSubmenu())
712 child
->AddEmptyMenus();
717 void MenuItemView::RemoveEmptyMenus() {
718 DCHECK(HasSubmenu());
719 // Iterate backwards as we may end up removing views, which alters the child
721 for (int i
= submenu_
->child_count() - 1; i
>= 0; --i
) {
722 View
* child
= submenu_
->child_at(i
);
723 if (child
->id() == MenuItemView::kMenuItemViewID
) {
724 MenuItemView
* menu_item
= static_cast<MenuItemView
*>(child
);
725 if (menu_item
->HasSubmenu())
726 menu_item
->RemoveEmptyMenus();
727 } else if (child
->id() == EmptyMenuMenuItem::kEmptyMenuItemViewID
) {
728 submenu_
->RemoveChildView(child
);
735 void MenuItemView::AdjustBoundsForRTLUI(gfx::Rect
* rect
) const {
736 rect
->set_x(GetMirroredXForRect(*rect
));
739 void MenuItemView::PaintButton(gfx::Canvas
* canvas
, PaintButtonMode mode
) {
740 const MenuConfig
& config
= GetMenuConfig();
741 bool render_selection
=
742 (mode
== PB_NORMAL
&& IsSelected() &&
743 parent_menu_item_
->GetSubmenu()->GetShowSelection(this) &&
744 (NonIconChildViewsCount() == 0));
746 MenuDelegate
*delegate
= GetDelegate();
747 // Render the background. As MenuScrollViewContainer draws the background, we
748 // only need the background when we want it to look different, as when we're
750 ui::NativeTheme
* native_theme
= GetNativeTheme();
751 SkColor override_color
;
752 if (delegate
&& delegate
->GetBackgroundColor(GetCommand(),
755 canvas
->DrawColor(override_color
);
756 } else if (render_selection
) {
757 gfx::Rect
item_bounds(0, 0, width(), height());
758 AdjustBoundsForRTLUI(&item_bounds
);
760 native_theme
->Paint(canvas
->sk_canvas(),
761 ui::NativeTheme::kMenuItemBackground
,
762 ui::NativeTheme::kHovered
,
764 ui::NativeTheme::ExtraParams());
767 const int icon_x
= config
.item_left_margin
+ left_icon_margin_
;
768 const int top_margin
= GetTopMargin();
769 const int bottom_margin
= GetBottomMargin();
770 const int available_height
= height() - top_margin
- bottom_margin
;
773 if (type_
== CHECKBOX
&& delegate
->IsItemChecked(GetCommand())) {
774 gfx::ImageSkia check
= GetMenuCheckImage(render_selection
);
775 // Don't use config.check_width here as it's padded
776 // to force more padding (AURA).
777 gfx::Rect
check_bounds(icon_x
,
778 top_margin
+ (available_height
- check
.height()) / 2,
781 AdjustBoundsForRTLUI(&check_bounds
);
782 canvas
->DrawImageInt(check
, check_bounds
.x(), check_bounds
.y());
783 } else if (type_
== RADIO
) {
784 gfx::ImageSkia image
=
785 GetRadioButtonImage(delegate
->IsItemChecked(GetCommand()));
786 gfx::Rect
radio_bounds(icon_x
,
787 top_margin
+ (available_height
- image
.height()) / 2,
790 AdjustBoundsForRTLUI(&radio_bounds
);
791 canvas
->DrawImageInt(image
, radio_bounds
.x(), radio_bounds
.y());
794 // Render the foreground.
795 ui::NativeTheme::ColorId color_id
;
797 color_id
= render_selection
?
798 ui::NativeTheme::kColorId_SelectedMenuItemForegroundColor
:
799 ui::NativeTheme::kColorId_EnabledMenuItemForegroundColor
;
801 bool emphasized
= delegate
&&
802 delegate
->GetShouldUseDisabledEmphasizedForegroundColor(
804 color_id
= emphasized
?
805 ui::NativeTheme::kColorId_DisabledEmphasizedMenuItemForegroundColor
:
806 ui::NativeTheme::kColorId_DisabledMenuItemForegroundColor
;
808 SkColor fg_color
= native_theme
->GetSystemColor(color_id
);
809 SkColor override_foreground_color
;
810 if (delegate
&& delegate
->GetForegroundColor(GetCommand(),
812 &override_foreground_color
))
813 fg_color
= override_foreground_color
;
815 const gfx::FontList
& font_list
= GetFontList();
816 int accel_width
= parent_menu_item_
->GetSubmenu()->max_minor_text_width();
817 int label_start
= GetLabelStartForThisItem();
819 int width
= this->width() - label_start
- accel_width
-
821 delegate
->ShouldReserveSpaceForSubmenuIndicator() ?
822 item_right_margin_
: config
.arrow_to_edge_padding
);
823 gfx::Rect
text_bounds(label_start
, top_margin
, width
,
824 subtitle_
.empty() ? available_height
825 : available_height
/ 2);
826 text_bounds
.set_x(GetMirroredXForRect(text_bounds
));
827 int flags
= GetDrawStringFlags();
828 if (mode
== PB_FOR_DRAG
)
829 flags
|= gfx::Canvas::NO_SUBPIXEL_RENDERING
;
830 canvas
->DrawStringRectWithFlags(title(), font_list
, fg_color
, text_bounds
,
832 if (!subtitle_
.empty()) {
833 canvas
->DrawStringRectWithFlags(
836 GetNativeTheme()->GetSystemColor(
837 ui::NativeTheme::kColorId_ButtonDisabledColor
),
838 text_bounds
+ gfx::Vector2d(0, font_list
.GetHeight()),
842 PaintMinorText(canvas
, render_selection
);
844 // Render the submenu indicator (arrow).
846 gfx::ImageSkia arrow
= GetSubmenuArrowImage(render_selection
);
847 gfx::Rect
arrow_bounds(this->width() - config
.arrow_width
-
848 config
.arrow_to_edge_padding
,
849 top_margin
+ (available_height
- arrow
.height()) / 2,
852 AdjustBoundsForRTLUI(&arrow_bounds
);
853 canvas
->DrawImageInt(arrow
, arrow_bounds
.x(), arrow_bounds
.y());
857 void MenuItemView::PaintMinorText(gfx::Canvas
* canvas
,
858 bool render_selection
) {
859 base::string16 minor_text
= GetMinorText();
860 if (minor_text
.empty())
863 int available_height
= height() - GetTopMargin() - GetBottomMargin();
864 int max_accel_width
=
865 parent_menu_item_
->GetSubmenu()->max_minor_text_width();
866 const MenuConfig
& config
= GetMenuConfig();
867 int accel_right_margin
= config
.align_arrow_and_shortcut
?
868 config
.arrow_to_edge_padding
: item_right_margin_
;
869 gfx::Rect
accel_bounds(width() - accel_right_margin
- max_accel_width
,
870 GetTopMargin(), max_accel_width
, available_height
);
871 accel_bounds
.set_x(GetMirroredXForRect(accel_bounds
));
872 int flags
= GetDrawStringFlags();
873 flags
&= ~(gfx::Canvas::TEXT_ALIGN_RIGHT
| gfx::Canvas::TEXT_ALIGN_LEFT
);
874 if (base::i18n::IsRTL())
875 flags
|= gfx::Canvas::TEXT_ALIGN_LEFT
;
877 flags
|= gfx::Canvas::TEXT_ALIGN_RIGHT
;
878 canvas
->DrawStringRectWithFlags(
881 GetNativeTheme()->GetSystemColor(render_selection
?
882 ui::NativeTheme::kColorId_SelectedMenuItemForegroundColor
:
883 ui::NativeTheme::kColorId_ButtonDisabledColor
),
888 void MenuItemView::DestroyAllMenuHosts() {
893 for (int i
= 0, item_count
= submenu_
->GetMenuItemCount(); i
< item_count
;
895 submenu_
->GetMenuItemAt(i
)->DestroyAllMenuHosts();
899 int MenuItemView::GetTopMargin() const {
900 if (top_margin_
>= 0)
903 const MenuItemView
* root
= GetRootMenuItem();
904 return root
&& root
->has_icons_
905 ? GetMenuConfig().item_top_margin
:
906 GetMenuConfig().item_no_icon_top_margin
;
909 int MenuItemView::GetBottomMargin() const {
910 if (bottom_margin_
>= 0)
911 return bottom_margin_
;
913 const MenuItemView
* root
= GetRootMenuItem();
914 return root
&& root
->has_icons_
915 ? GetMenuConfig().item_bottom_margin
:
916 GetMenuConfig().item_no_icon_bottom_margin
;
919 gfx::Size
MenuItemView::GetChildPreferredSize() const {
924 return child_at(0)->GetPreferredSize();
927 for (int i
= 0; i
< child_count(); ++i
) {
928 const View
* child
= child_at(i
);
929 if (icon_view_
&& (icon_view_
== child
))
932 width
+= kChildXPadding
;
933 width
+= child
->GetPreferredSize().width();
937 height
= icon_view_
->GetPreferredSize().height();
939 // If there is no icon view it returns a height of 0 to indicate that
940 // we should use the title height instead.
941 return gfx::Size(width
, height
);
944 MenuItemView::MenuItemDimensions
MenuItemView::CalculateDimensions() const {
945 gfx::Size child_size
= GetChildPreferredSize();
947 MenuItemDimensions dimensions
;
948 // Get the container height.
949 dimensions
.children_width
= child_size
.width();
950 dimensions
.height
= child_size
.height();
951 // Adjust item content height if menu has both items with and without icons.
952 // This way all menu items will have the same height.
953 if (!icon_view_
&& GetRootMenuItem()->has_icons()) {
954 dimensions
.height
= std::max(dimensions
.height
,
955 GetMenuConfig().check_height
);
957 dimensions
.height
+= GetBottomMargin() + GetTopMargin();
959 // In case of a container, only the container size needs to be filled.
963 // Determine the length of the label text.
964 const gfx::FontList
& font_list
= GetFontList();
966 // Get Icon margin overrides for this particular item.
967 const MenuDelegate
* delegate
= GetDelegate();
969 delegate
->GetHorizontalIconMargins(command_
,
972 &right_icon_margin_
);
974 left_icon_margin_
= 0;
975 right_icon_margin_
= 0;
977 int label_start
= GetLabelStartForThisItem();
979 int string_width
= gfx::GetStringWidth(title_
, font_list
);
980 if (!subtitle_
.empty()) {
981 string_width
= std::max(string_width
,
982 gfx::GetStringWidth(subtitle_
, font_list
));
985 dimensions
.standard_width
= string_width
+ label_start
+
987 // Determine the length of the right-side text.
988 base::string16 minor_text
= GetMinorText();
989 dimensions
.minor_text_width
=
990 minor_text
.empty() ? 0 : gfx::GetStringWidth(minor_text
, font_list
);
992 // Determine the height to use.
994 std::max(dimensions
.height
,
995 (subtitle_
.empty() ? 0 : font_list
.GetHeight()) +
996 font_list
.GetHeight() + GetBottomMargin() + GetTopMargin());
997 dimensions
.height
= std::max(dimensions
.height
,
998 GetMenuConfig().item_min_height
);
1002 int MenuItemView::GetLabelStartForThisItem() const {
1003 int label_start
= label_start_
+ left_icon_margin_
+ right_icon_margin_
;
1004 if ((type_
== CHECKBOX
|| type_
== RADIO
) && icon_view_
) {
1005 label_start
+= icon_view_
->size().width() +
1006 GetMenuConfig().icon_to_label_padding
;
1011 base::string16
MenuItemView::GetMinorText() const {
1012 if (id() == kEmptyMenuItemViewID
) {
1013 // Don't query the delegate for menus that represent no children.
1014 return base::string16();
1017 ui::Accelerator accelerator
;
1018 if (GetMenuConfig().show_accelerators
&& GetDelegate() && GetCommand() &&
1019 GetDelegate()->GetAccelerator(GetCommand(), &accelerator
)) {
1020 return accelerator
.GetShortcutText();
1026 bool MenuItemView::IsContainer() const {
1027 // Let the first child take over |this| when we only have one child and no
1029 return (NonIconChildViewsCount() == 1) && title_
.empty();
1032 int MenuItemView::NonIconChildViewsCount() const {
1033 // Note that what child_count() returns is the number of children,
1034 // not the number of menu items.
1035 return child_count() - (icon_view_
? 1 : 0);
1038 int MenuItemView::GetMaxIconViewWidth() const {
1040 for (int i
= 0; i
< submenu_
->GetMenuItemCount(); ++i
) {
1041 MenuItemView
* menu_item
= submenu_
->GetMenuItemAt(i
);
1043 if (menu_item
->GetType() == CHECKBOX
||
1044 menu_item
->GetType() == RADIO
) {
1045 // If this item has a radio or checkbox, the icon will not affect
1046 // alignment of other items.
1048 } else if (menu_item
->HasSubmenu()) {
1049 temp_width
= menu_item
->GetMaxIconViewWidth();
1050 } else if (menu_item
->icon_view()) {
1051 temp_width
= menu_item
->icon_view()->GetPreferredSize().width();
1053 width
= std::max(width
, temp_width
);
1058 bool MenuItemView::HasChecksOrRadioButtons() const {
1059 for (int i
= 0; i
< submenu_
->GetMenuItemCount(); ++i
) {
1060 MenuItemView
* menu_item
= submenu_
->GetMenuItemAt(i
);
1061 if (menu_item
->HasSubmenu()) {
1062 if (menu_item
->HasChecksOrRadioButtons())
1065 const Type
& type
= menu_item
->GetType();
1066 if (type
== CHECKBOX
|| type
== RADIO
)
1073 } // namespace views