| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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. | |
| 4 | |
| 5 #include "views/controls/menu/menu_item_view.h" | |
| 6 | |
| 7 #include "base/utf_string_conversions.h" | |
| 8 #include "grit/ui_resources.h" | |
| 9 #include "third_party/skia/include/effects/SkGradientShader.h" | |
| 10 #include "ui/base/resource/resource_bundle.h" | |
| 11 #include "ui/gfx/canvas_skia.h" | |
| 12 #include "ui/gfx/favicon_size.h" | |
| 13 #include "views/controls/button/text_button.h" | |
| 14 #include "views/controls/menu/menu_config.h" | |
| 15 #include "views/controls/menu/menu_image_util_gtk.h" | |
| 16 #include "views/controls/menu/submenu_view.h" | |
| 17 | |
| 18 namespace views { | |
| 19 | |
| 20 // Background color when the menu item is selected. | |
| 21 #if defined(OS_CHROMEOS) | |
| 22 static const SkColor kSelectedBackgroundColor = SkColorSetRGB(0xDC, 0xE4, 0xFA); | |
| 23 #else | |
| 24 static const SkColor kSelectedBackgroundColor = SkColorSetRGB(246, 249, 253); | |
| 25 #endif | |
| 26 | |
| 27 #if defined(TOUCH_UI) | |
| 28 const int kMinTouchHeight = 46; | |
| 29 #endif | |
| 30 | |
| 31 gfx::Size MenuItemView::CalculatePreferredSize() { | |
| 32 const gfx::Font& font = GetFont(); | |
| 33 #if defined(TOUCH_UI) | |
| 34 int height = std::max(font.GetHeight(), kMinTouchHeight); | |
| 35 #else | |
| 36 int height = font.GetHeight(); | |
| 37 #endif | |
| 38 gfx::Size child_size = GetChildPreferredSize(); | |
| 39 return gfx::Size( | |
| 40 font.GetStringWidth(title_) + label_start_ + | |
| 41 item_right_margin_ + child_size.width(), | |
| 42 std::max(height, child_size.height()) + GetBottomMargin() + | |
| 43 GetTopMargin()); | |
| 44 } | |
| 45 | |
| 46 void MenuItemView::PaintButton(gfx::Canvas* canvas, PaintButtonMode mode) { | |
| 47 const MenuConfig& config = MenuConfig::instance(); | |
| 48 bool render_selection = | |
| 49 (mode == PB_NORMAL && IsSelected() && | |
| 50 parent_menu_item_->GetSubmenu()->GetShowSelection(this) && | |
| 51 !has_children()); | |
| 52 | |
| 53 int icon_x = config.item_left_margin; | |
| 54 int top_margin = GetTopMargin(); | |
| 55 int bottom_margin = GetBottomMargin(); | |
| 56 int icon_y = top_margin + (height() - config.item_top_margin - | |
| 57 bottom_margin - config.check_height) / 2; | |
| 58 int icon_height = config.check_height; | |
| 59 int available_height = height() - top_margin - bottom_margin; | |
| 60 | |
| 61 // Render the background. As MenuScrollViewContainer draws the background, we | |
| 62 // only need the background when we want it to look different, as when we're | |
| 63 // selected. | |
| 64 if (render_selection) | |
| 65 canvas->AsCanvasSkia()->drawColor(kSelectedBackgroundColor, | |
| 66 SkXfermode::kSrc_Mode); | |
| 67 | |
| 68 // Render the check. | |
| 69 if (type_ == CHECKBOX && GetDelegate()->IsItemChecked(GetCommand())) { | |
| 70 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
| 71 SkBitmap* check = rb.GetBitmapNamed(IDR_MENU_CHECK); | |
| 72 // Don't use config.check_width here as it's padded to force more padding. | |
| 73 gfx::Rect check_bounds(icon_x, icon_y, check->width(), icon_height); | |
| 74 AdjustBoundsForRTLUI(&check_bounds); | |
| 75 canvas->DrawBitmapInt(*check, check_bounds.x(), check_bounds.y()); | |
| 76 } else if (type_ == RADIO) { | |
| 77 const SkBitmap* image = | |
| 78 GetRadioButtonImage(GetDelegate()->IsItemChecked(GetCommand())); | |
| 79 gfx::Rect radio_bounds(icon_x, | |
| 80 top_margin + | |
| 81 (height() - top_margin - bottom_margin - | |
| 82 image->height()) / 2, | |
| 83 image->width(), | |
| 84 image->height()); | |
| 85 AdjustBoundsForRTLUI(&radio_bounds); | |
| 86 canvas->DrawBitmapInt(*image, radio_bounds.x(), radio_bounds.y()); | |
| 87 } | |
| 88 | |
| 89 // Render the foreground. | |
| 90 #if defined(OS_CHROMEOS) | |
| 91 SkColor fg_color = | |
| 92 IsEnabled() ? SK_ColorBLACK : SkColorSetRGB(0x80, 0x80, 0x80); | |
| 93 #else | |
| 94 SkColor fg_color = | |
| 95 IsEnabled() ? TextButton::kEnabledColor : TextButton::kDisabledColor; | |
| 96 #endif | |
| 97 const gfx::Font& font = GetFont(); | |
| 98 int accel_width = parent_menu_item_->GetSubmenu()->max_accelerator_width(); | |
| 99 int width = this->width() - item_right_margin_ - label_start_ - accel_width; | |
| 100 gfx::Rect text_bounds(label_start_, top_margin + | |
| 101 (available_height - font.GetHeight()) / 2, width, | |
| 102 font.GetHeight()); | |
| 103 text_bounds.set_x(GetMirroredXForRect(text_bounds)); | |
| 104 canvas->DrawStringInt(WideToUTF16Hack(GetTitle()), font, fg_color, | |
| 105 text_bounds.x(), text_bounds.y(), text_bounds.width(), | |
| 106 text_bounds.height(), | |
| 107 GetRootMenuItem()->GetDrawStringFlags()); | |
| 108 | |
| 109 PaintAccelerator(canvas); | |
| 110 | |
| 111 // Render the icon. | |
| 112 if (icon_.width() > 0) { | |
| 113 gfx::Rect icon_bounds(config.item_left_margin, | |
| 114 top_margin + (height() - top_margin - | |
| 115 bottom_margin - icon_.height()) / 2, | |
| 116 icon_.width(), | |
| 117 icon_.height()); | |
| 118 icon_bounds.set_x(GetMirroredXForRect(icon_bounds)); | |
| 119 canvas->DrawBitmapInt(icon_, icon_bounds.x(), icon_bounds.y()); | |
| 120 } | |
| 121 | |
| 122 // Render the submenu indicator (arrow). | |
| 123 if (HasSubmenu()) { | |
| 124 gfx::Rect arrow_bounds(this->width() - item_right_margin_ + | |
| 125 config.label_to_arrow_padding, | |
| 126 top_margin + (available_height - | |
| 127 config.arrow_width) / 2, | |
| 128 config.arrow_width, height()); | |
| 129 AdjustBoundsForRTLUI(&arrow_bounds); | |
| 130 canvas->DrawBitmapInt(*GetSubmenuArrowImage(), | |
| 131 arrow_bounds.x(), arrow_bounds.y()); | |
| 132 } | |
| 133 } | |
| 134 | |
| 135 } // namespace views | |
| OLD | NEW |