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 gfx::Size child_size = GetChildPreferredSize(); | |
33 if (child_count() == 1 && title_.size() == 0) { | |
34 return gfx::Size( | |
35 child_size.width(), | |
36 child_size.height() + GetBottomMargin() + GetTopMargin()); | |
37 } | |
38 | |
39 const gfx::Font& font = GetFont(); | |
40 #if defined(TOUCH_UI) | |
41 int height = std::max(font.GetHeight(), kMinTouchHeight); | |
42 #else | |
43 int height = font.GetHeight(); | |
44 #endif | |
45 return gfx::Size( | |
46 font.GetStringWidth(title_) + label_start_ + | |
47 item_right_margin_ + child_size.width(), | |
48 std::max(height, child_size.height()) + GetBottomMargin() + | |
49 GetTopMargin()); | |
50 } | |
51 | |
52 void MenuItemView::PaintButton(gfx::Canvas* canvas, PaintButtonMode mode) { | |
53 const MenuConfig& config = MenuConfig::instance(); | |
54 bool render_selection = | |
55 (mode == PB_NORMAL && IsSelected() && | |
56 parent_menu_item_->GetSubmenu()->GetShowSelection(this) && | |
57 !has_children()); | |
58 | |
59 int icon_x = config.item_left_margin; | |
60 int top_margin = GetTopMargin(); | |
61 int bottom_margin = GetBottomMargin(); | |
62 int icon_y = top_margin + (height() - config.item_top_margin - | |
63 bottom_margin - config.check_height) / 2; | |
64 int icon_height = config.check_height; | |
65 int available_height = height() - top_margin - bottom_margin; | |
66 | |
67 // Render the background. As MenuScrollViewContainer draws the background, we | |
68 // only need the background when we want it to look different, as when we're | |
69 // selected. | |
70 if (render_selection) | |
71 canvas->AsCanvasSkia()->drawColor(kSelectedBackgroundColor, | |
72 SkXfermode::kSrc_Mode); | |
73 | |
74 // Render the check. | |
75 if (type_ == CHECKBOX && GetDelegate()->IsItemChecked(GetCommand())) { | |
76 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
77 SkBitmap* check = rb.GetBitmapNamed(IDR_MENU_CHECK); | |
78 // Don't use config.check_width here as it's padded to force more padding. | |
79 gfx::Rect check_bounds(icon_x, icon_y, check->width(), icon_height); | |
80 AdjustBoundsForRTLUI(&check_bounds); | |
81 canvas->DrawBitmapInt(*check, check_bounds.x(), check_bounds.y()); | |
82 } else if (type_ == RADIO) { | |
83 const SkBitmap* image = | |
84 GetRadioButtonImage(GetDelegate()->IsItemChecked(GetCommand())); | |
85 gfx::Rect radio_bounds(icon_x, | |
86 top_margin + | |
87 (height() - top_margin - bottom_margin - | |
88 image->height()) / 2, | |
89 image->width(), | |
90 image->height()); | |
91 AdjustBoundsForRTLUI(&radio_bounds); | |
92 canvas->DrawBitmapInt(*image, radio_bounds.x(), radio_bounds.y()); | |
93 } | |
94 | |
95 // Render the foreground. | |
96 #if defined(OS_CHROMEOS) | |
97 SkColor fg_color = | |
98 IsEnabled() ? SK_ColorBLACK : SkColorSetRGB(0x80, 0x80, 0x80); | |
99 #else | |
100 SkColor fg_color = | |
101 IsEnabled() ? TextButton::kEnabledColor : TextButton::kDisabledColor; | |
102 #endif | |
103 const gfx::Font& font = GetFont(); | |
104 int accel_width = parent_menu_item_->GetSubmenu()->max_accelerator_width(); | |
105 int width = this->width() - item_right_margin_ - label_start_ - accel_width; | |
106 gfx::Rect text_bounds(label_start_, top_margin + | |
107 (available_height - font.GetHeight()) / 2, width, | |
108 font.GetHeight()); | |
109 text_bounds.set_x(GetMirroredXForRect(text_bounds)); | |
110 canvas->DrawStringInt(WideToUTF16Hack(GetTitle()), font, fg_color, | |
111 text_bounds.x(), text_bounds.y(), text_bounds.width(), | |
112 text_bounds.height(), | |
113 GetRootMenuItem()->GetDrawStringFlags()); | |
114 | |
115 PaintAccelerator(canvas); | |
116 | |
117 // Render the icon. | |
118 if (icon_.width() > 0) { | |
119 gfx::Rect icon_bounds(config.item_left_margin, | |
120 top_margin + (height() - top_margin - | |
121 bottom_margin - icon_.height()) / 2, | |
122 icon_.width(), | |
123 icon_.height()); | |
124 icon_bounds.set_x(GetMirroredXForRect(icon_bounds)); | |
125 canvas->DrawBitmapInt(icon_, icon_bounds.x(), icon_bounds.y()); | |
126 } | |
127 | |
128 // Render the submenu indicator (arrow). | |
129 if (HasSubmenu()) { | |
130 gfx::Rect arrow_bounds(this->width() - item_right_margin_ + | |
131 config.label_to_arrow_padding, | |
132 top_margin + (available_height - | |
133 config.arrow_width) / 2, | |
134 config.arrow_width, height()); | |
135 AdjustBoundsForRTLUI(&arrow_bounds); | |
136 canvas->DrawBitmapInt(*GetSubmenuArrowImage(), | |
137 arrow_bounds.x(), arrow_bounds.y()); | |
138 } | |
139 } | |
140 | |
141 } // namespace views | |
OLD | NEW |