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 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 int MenuItemView::GetHeightForWidth(int width
) const {
414 // If this isn't a container, we can just use the preferred size's height.
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();
426 const MenuItemView::MenuItemDimensions
& MenuItemView::GetDimensions() const {
427 if (!is_dimensions_valid())
428 dimensions_
= CalculateDimensions();
429 DCHECK(is_dimensions_valid());
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())
462 base::char16
MenuItemView::GetMnemonic() {
463 if (!GetRootMenuItem()->has_mnemonics_
)
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];
480 } while (index
!= base::string16::npos
);
484 MenuItemView
* MenuItemView::GetMenuItemByID(int id
) {
485 if (GetCommand() == id
)
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
)->
501 void MenuItemView::ChildrenChanged() {
502 MenuController
* controller
= GetMenuController();
504 // Handles the case where we were empty and are no longer empty.
507 // Handles the case where we were not empty, but now are.
510 controller
->MenuChildrenChanged(this);
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.
517 submenu_
->SchedulePaint();
518 // Update the menu selection after layout.
519 controller
->UpdateSubmenuSelection(submenu_
);
523 STLDeleteElements(&removed_items_
);
526 void MenuItemView::Layout() {
531 View
* child
= child_at(0);
532 gfx::Size size
= child
->GetPreferredSize();
533 child
->SetBounds(0, GetTopMargin(), size
.width(), size
.height());
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
))
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();
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
)
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();
572 return controller
->menu_config_
;
573 return MenuConfig::instance(NULL
);
576 MenuItemView::MenuItemView(MenuItemView
* parent
,
578 MenuItemView::Type type
)
582 parent_menu_item_(parent
),
587 has_mnemonics_(false),
588 show_mnemonics_(false),
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() {
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
;
620 icon_area_width_
= std::max(icon_area_width_
, GetMaxIconViewWidth());
622 label_start_
= config
.item_left_margin
+ icon_area_width_
;
624 if (config
.always_use_icon_to_label_padding
) {
625 padding
= config
.icon_to_label_padding
;
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
,
639 MenuItemView::Type type
,
640 MenuDelegate
* delegate
) {
641 delegate_
= delegate
;
644 parent_menu_item_
= parent
;
649 show_mnemonics_
= false;
650 // Assign our ID, this allows SubmenuItemView to find MenuItemViews.
651 set_id(kMenuItemViewID
);
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
,
663 bool show_mnemonics
) {
664 // Currently we only support showing the root.
665 DCHECK(!parent_menu_item_
);
667 // Force us to have a submenu.
669 actual_menu_position_
= requested_menu_position_
;
672 has_mnemonics_
= has_mnemonics
;
673 show_mnemonics_
= has_mnemonics
&& show_mnemonics
;
678 // Only update the menu size if there are no menus showing, otherwise
679 // things may shift around.
680 UpdateMenuPartSizes();
684 int MenuItemView::GetDrawStringFlags() {
686 if (base::i18n::IsRTL())
687 flags
|= gfx::Canvas::TEXT_ALIGN_RIGHT
;
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
;
695 flags
|= gfx::Canvas::HIDE_PREFIX
;
701 const gfx::FontList
& MenuItemView::GetFontList() const {
702 const MenuDelegate
* delegate
= GetDelegate();
704 const gfx::FontList
* font_list
= delegate
->GetLabelFontList(GetCommand());
708 return GetMenuConfig().font_list
;
711 void MenuItemView::AddEmptyMenus() {
712 DCHECK(HasSubmenu());
713 if (!submenu_
->has_children()) {
714 submenu_
->AddChildViewAt(new EmptyMenuMenuItem(this), 0);
716 for (int i
= 0, item_count
= submenu_
->GetMenuItemCount(); i
< item_count
;
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
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
);
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
758 ui::NativeTheme
* native_theme
= GetNativeTheme();
759 SkColor override_color
;
760 if (delegate
&& delegate
->GetBackgroundColor(GetCommand(),
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
,
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
;
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,
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,
798 AdjustBoundsForRTLUI(&radio_bounds
);
799 canvas
->DrawImageInt(image
, radio_bounds
.x(), radio_bounds
.y());
802 // Render the foreground.
803 ui::NativeTheme::ColorId color_id
;
805 color_id
= render_selection
?
806 ui::NativeTheme::kColorId_SelectedMenuItemForegroundColor
:
807 ui::NativeTheme::kColorId_EnabledMenuItemForegroundColor
;
809 bool emphasized
= delegate
&&
810 delegate
->GetShouldUseDisabledEmphasizedForegroundColor(
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(),
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
-
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
,
840 if (!subtitle_
.empty()) {
841 canvas
->DrawStringRectWithFlags(
844 GetNativeTheme()->GetSystemColor(
845 ui::NativeTheme::kColorId_ButtonDisabledColor
),
846 text_bounds
+ gfx::Vector2d(0, font_list
.GetHeight()),
850 PaintMinorText(canvas
, render_selection
);
852 // Render the submenu indicator (arrow).
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,
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())
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
;
885 flags
|= gfx::Canvas::TEXT_ALIGN_RIGHT
;
886 canvas
->DrawStringRectWithFlags(
889 GetNativeTheme()->GetSystemColor(render_selection
?
890 ui::NativeTheme::kColorId_SelectedMenuItemForegroundColor
:
891 ui::NativeTheme::kColorId_ButtonDisabledColor
),
896 void MenuItemView::DestroyAllMenuHosts() {
901 for (int i
= 0, item_count
= submenu_
->GetMenuItemCount(); i
< item_count
;
903 submenu_
->GetMenuItemAt(i
)->DestroyAllMenuHosts();
907 int MenuItemView::GetTopMargin() const {
908 if (top_margin_
>= 0)
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 {
932 return child_at(0)->GetPreferredSize();
935 for (int i
= 0; i
< child_count(); ++i
) {
936 const View
* child
= child_at(i
);
937 if (icon_view_
&& (icon_view_
== child
))
940 width
+= kChildXPadding
;
941 width
+= child
->GetPreferredSize().width();
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.
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();
977 delegate
->GetHorizontalIconMargins(command_
,
980 &right_icon_margin_
);
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
+
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.
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
);
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
;
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();
1034 bool MenuItemView::IsContainer() const {
1035 // Let the first child take over |this| when we only have one child and no
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 {
1048 for (int i
= 0; i
< submenu_
->GetMenuItemCount(); ++i
) {
1049 MenuItemView
* menu_item
= submenu_
->GetMenuItemAt(i
);
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.
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
);
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())
1073 const Type
& type
= menu_item
->GetType();
1074 if (type
== CHECKBOX
|| type
== RADIO
)
1081 } // namespace views