blob: 2720aca2ca3dba706f5ec4ba906525e0d40844a8 [file] [log] [blame]
[email protected]9e790bd2011-01-10 23:48:541// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]eccda9db2010-06-23 17:27:082// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]9e790bd2011-01-10 23:48:545#include "chrome/browser/ui/views/wrench_menu.h"
[email protected]eccda9db2010-06-23 17:27:086
7#include <cmath>
8
[email protected]e83326f2010-07-31 17:29:259#include "base/string_number_conversions.h"
[email protected]eccda9db2010-06-23 17:27:0810#include "base/utf_string_conversions.h"
[email protected]1a3aba82010-11-08 23:52:5411#include "chrome/app/chrome_command_ids.h"
[email protected]366069f2011-01-11 09:36:1212#include "chrome/browser/metrics/user_metrics.h"
[email protected]8ecad5e2010-12-02 21:18:3313#include "chrome/browser/profiles/profile.h"
[email protected]f46be6e2010-11-16 03:52:3214#include "chrome/browser/ui/browser.h"
[email protected]eccda9db2010-06-23 17:27:0815#include "chrome/common/notification_observer.h"
16#include "chrome/common/notification_registrar.h"
17#include "chrome/common/notification_source.h"
18#include "chrome/common/notification_type.h"
[email protected]21f11682011-03-02 16:45:4219#include "content/browser/tab_contents/tab_contents.h"
[email protected]e12e3f62010-08-06 00:44:5920#include "grit/chromium_strings.h"
[email protected]eccda9db2010-06-23 17:27:0821#include "grit/generated_resources.h"
22#include "grit/theme_resources.h"
23#include "third_party/skia/include/core/SkPaint.h"
[email protected]c051a1b2011-01-21 23:30:1724#include "ui/base/l10n/l10n_util.h"
[email protected]42ce29d2011-01-20 23:19:4625#include "ui/base/resource/resource_bundle.h"
[email protected]08397d52011-02-05 01:53:3826#include "ui/gfx/canvas.h"
27#include "ui/gfx/canvas_skia.h"
28#include "ui/gfx/skia_util.h"
[email protected]eccda9db2010-06-23 17:27:0829#include "views/background.h"
30#include "views/controls/button/image_button.h"
[email protected]a00b0322010-06-25 16:21:3231#include "views/controls/button/menu_button.h"
[email protected]eccda9db2010-06-23 17:27:0832#include "views/controls/button/text_button.h"
33#include "views/controls/label.h"
34#include "views/controls/menu/menu_config.h"
35#include "views/controls/menu/menu_item_view.h"
36#include "views/controls/menu/menu_scroll_view_container.h"
37#include "views/controls/menu/submenu_view.h"
38#include "views/window/window.h"
39
[email protected]44cbd9e2011-01-14 15:49:4040using ui::MenuModel;
[email protected]eccda9db2010-06-23 17:27:0841using views::CustomButton;
42using views::ImageButton;
43using views::Label;
44using views::MenuConfig;
45using views::MenuItemView;
46using views::TextButton;
47using views::View;
48
49namespace {
50
51// Colors used for buttons.
52const SkColor kHotBorderColor = SkColorSetARGB(72, 0, 0, 0);
53const SkColor kBorderColor = SkColorSetARGB(36, 0, 0, 0);
54const SkColor kPushedBorderColor = SkColorSetARGB(72, 0, 0, 0);
55const SkColor kHotBackgroundColor = SkColorSetARGB(204, 255, 255, 255);
56const SkColor kBackgroundColor = SkColorSetARGB(102, 255, 255, 255);
57const SkColor kPushedBackgroundColor = SkColorSetARGB(13, 0, 0, 0);
58
59// Horizontal padding on the edges of the buttons.
60const int kHorizontalPadding = 6;
61
62// Subclass of ImageButton whose preferred size includes the size of the border.
63class FullscreenButton : public ImageButton {
64 public:
[email protected]65ecd5b2010-07-27 21:29:0665 explicit FullscreenButton(views::ButtonListener* listener)
66 : ImageButton(listener) { }
[email protected]eccda9db2010-06-23 17:27:0867
68 virtual gfx::Size GetPreferredSize() {
69 gfx::Size pref = ImageButton::GetPreferredSize();
70 gfx::Insets insets;
71 if (border())
72 border()->GetInsets(&insets);
73 pref.Enlarge(insets.width(), insets.height());
74 return pref;
75 }
76
77 private:
78 DISALLOW_COPY_AND_ASSIGN(FullscreenButton);
79};
80
81// Border for buttons contained in the menu. This is only used for getting the
82// insets, the actual painting is done in MenuButtonBackground.
83class MenuButtonBorder : public views::Border {
84 public:
85 MenuButtonBorder() {}
86
87 virtual void Paint(const View& view, gfx::Canvas* canvas) const {
88 // Painting of border is done in MenuButtonBackground.
89 }
90
91 virtual void GetInsets(gfx::Insets* insets) const {
[email protected]e57bfac2010-08-03 17:19:5492 insets->Set(MenuConfig::instance().item_top_margin,
[email protected]eccda9db2010-06-23 17:27:0893 kHorizontalPadding,
[email protected]e57bfac2010-08-03 17:19:5494 MenuConfig::instance().item_bottom_margin,
[email protected]eccda9db2010-06-23 17:27:0895 kHorizontalPadding);
96 }
97
98 private:
99 DISALLOW_COPY_AND_ASSIGN(MenuButtonBorder);
100};
101
102// Combination border/background for the buttons contained in the menu. The
103// painting of the border/background is done here as TextButton does not always
104// paint the border.
105class MenuButtonBackground : public views::Background {
106 public:
107 enum ButtonType {
108 LEFT_BUTTON,
109 CENTER_BUTTON,
110 RIGHT_BUTTON,
111 SINGLE_BUTTON,
112 };
113
114 explicit MenuButtonBackground(ButtonType type)
115 : type_(type),
116 left_button_(NULL),
117 right_button_(NULL) {}
118
119 // Used when the type is CENTER_BUTTON to determine if the left/right edge
120 // needs to be rendered selected.
121 void SetOtherButtons(CustomButton* left_button, CustomButton* right_button) {
[email protected]5214bdd2010-08-02 21:43:47122 if (base::i18n::IsRTL()) {
123 left_button_ = right_button;
124 right_button_ = left_button;
125 } else {
126 left_button_ = left_button;
127 right_button_ = right_button;
128 }
[email protected]eccda9db2010-06-23 17:27:08129 }
130
131 virtual void Paint(gfx::Canvas* canvas, View* view) const {
132 CustomButton::ButtonState state =
133 (view->GetClassName() == views::Label::kViewClassName) ?
134 CustomButton::BS_NORMAL : static_cast<CustomButton*>(view)->state();
135 int w = view->width();
136 int h = view->height();
[email protected]34344902010-07-07 20:26:26137 switch (TypeAdjustedForRTL()) {
[email protected]eccda9db2010-06-23 17:27:08138 case LEFT_BUTTON:
139 canvas->FillRectInt(background_color(state), 1, 1, w, h - 2);
140 canvas->FillRectInt(border_color(state), 2, 0, w, 1);
141 canvas->FillRectInt(border_color(state), 1, 1, 1, 1);
142 canvas->FillRectInt(border_color(state), 0, 2, 1, h - 4);
143 canvas->FillRectInt(border_color(state), 1, h - 2, 1, 1);
144 canvas->FillRectInt(border_color(state), 2, h - 1, w, 1);
145 break;
146
147 case CENTER_BUTTON: {
148 canvas->FillRectInt(background_color(state), 1, 1, w - 2, h - 2);
149 SkColor left_color = state != CustomButton::BS_NORMAL ?
150 border_color(state) : border_color(left_button_->state());
151 canvas->FillRectInt(left_color, 0, 0, 1, h);
152 canvas->FillRectInt(border_color(state), 1, 0, w - 2, 1);
153 canvas->FillRectInt(border_color(state), 1, h - 1, w - 2, 1);
154 SkColor right_color = state != CustomButton::BS_NORMAL ?
155 border_color(state) : border_color(right_button_->state());
156 canvas->FillRectInt(right_color, w - 1, 0, 1, h);
157 break;
158 }
159
160 case RIGHT_BUTTON:
161 canvas->FillRectInt(background_color(state), 0, 1, w - 1, h - 2);
162 canvas->FillRectInt(border_color(state), 0, 0, w - 2, 1);
163 canvas->FillRectInt(border_color(state), w - 2, 1, 1, 1);
164 canvas->FillRectInt(border_color(state), w - 1, 2, 1, h - 4);
165 canvas->FillRectInt(border_color(state), w - 2, h - 2, 1, 1);
166 canvas->FillRectInt(border_color(state), 0, h - 1, w - 2, 1);
167 break;
168
169 case SINGLE_BUTTON:
170 canvas->FillRectInt(background_color(state), 1, 1, w - 2, h - 2);
171 canvas->FillRectInt(border_color(state), 2, 0, w - 4, 1);
172 canvas->FillRectInt(border_color(state), 1, 1, 1, 1);
173 canvas->FillRectInt(border_color(state), 0, 2, 1, h - 4);
174 canvas->FillRectInt(border_color(state), 1, h - 2, 1, 1);
175 canvas->FillRectInt(border_color(state), 2, h - 1, w - 4, 1);
176 canvas->FillRectInt(border_color(state), w - 2, 1, 1, 1);
177 canvas->FillRectInt(border_color(state), w - 1, 2, 1, h - 4);
178 canvas->FillRectInt(border_color(state), w - 2, h - 2, 1, 1);
179 break;
180
181 default:
182 NOTREACHED();
183 break;
184 }
185 }
186
187 private:
188 static SkColor border_color(CustomButton::ButtonState state) {
189 switch (state) {
190 case CustomButton::BS_HOT: return kHotBorderColor;
191 case CustomButton::BS_PUSHED: return kPushedBorderColor;
192 default: return kBorderColor;
193 }
194 }
195
196 static SkColor background_color(CustomButton::ButtonState state) {
197 switch (state) {
198 case CustomButton::BS_HOT: return kHotBackgroundColor;
199 case CustomButton::BS_PUSHED: return kPushedBackgroundColor;
200 default: return kBackgroundColor;
201 }
202 }
203
[email protected]34344902010-07-07 20:26:26204 ButtonType TypeAdjustedForRTL() const {
205 if (!base::i18n::IsRTL())
206 return type_;
207
208 switch (type_) {
209 case LEFT_BUTTON: return RIGHT_BUTTON;
210 case RIGHT_BUTTON: return LEFT_BUTTON;
211 default: break;
212 }
213 return type_;
214 }
215
[email protected]eccda9db2010-06-23 17:27:08216 const ButtonType type_;
217
218 // See description above setter for details.
219 CustomButton* left_button_;
220 CustomButton* right_button_;
221
222 DISALLOW_COPY_AND_ASSIGN(MenuButtonBackground);
223};
224
225// A View subclass that forces SchedulePaint to paint all. Normally when the
226// mouse enters/exits a button the buttons invokes SchedulePaint. As part of the
227// button border (MenuButtonBackground) is rendered by the button to the
228// left/right of it SchedulePaint on the the button may not be enough, so this
229// forces a paint all.
230class ScheduleAllView : public views::View {
231 public:
232 ScheduleAllView() {}
233
[email protected]0a119c92011-02-23 17:50:56234 virtual void SchedulePaintInRect(const gfx::Rect& r) {
[email protected]eccda9db2010-06-23 17:27:08235 if (!IsVisible())
236 return;
237
[email protected]20838602011-02-09 04:50:21238 if (parent())
[email protected]0a119c92011-02-23 17:50:56239 parent()->SchedulePaintInRect(GetMirroredBounds());
[email protected]eccda9db2010-06-23 17:27:08240 }
241
242 private:
243 DISALLOW_COPY_AND_ASSIGN(ScheduleAllView);
244};
245
[email protected]6f1d18e2011-01-10 20:57:00246string16 GetAccessibleNameForWrenchMenuItem(
[email protected]2cc800b2010-10-01 18:27:34247 MenuModel* model, int item_index, int accessible_string_id) {
[email protected]6f1d18e2011-01-10 20:57:00248 string16 accessible_name = l10n_util::GetStringUTF16(accessible_string_id);
249 string16 accelerator_text;
[email protected]2cc800b2010-10-01 18:27:34250
[email protected]44cbd9e2011-01-14 15:49:40251 ui::Accelerator menu_accelerator;
[email protected]2cc800b2010-10-01 18:27:34252 if (model->GetAcceleratorAt(item_index, &menu_accelerator)) {
253 accelerator_text =
254 views::Accelerator(menu_accelerator.GetKeyCode(),
255 menu_accelerator.modifiers()).GetShortcutText();
256 }
257
258 return MenuItemView::GetAccessibleNameForMenuItem(
259 accessible_name, accelerator_text);
[email protected]eccda9db2010-06-23 17:27:08260}
261
[email protected]2cc800b2010-10-01 18:27:34262// WrenchMenuView is a view that can contain text buttons.
263class WrenchMenuView : public ScheduleAllView, public views::ButtonListener {
264 public:
265 WrenchMenuView(WrenchMenu* menu, MenuModel* menu_model)
266 : menu_(menu), menu_model_(menu_model) { }
267
268 TextButton* CreateAndConfigureButton(int string_id,
269 MenuButtonBackground::ButtonType type,
270 int index,
271 MenuButtonBackground** background) {
272 return CreateButtonWithAccName(
273 string_id, type, index, background, string_id);
274 }
275
276 TextButton* CreateButtonWithAccName(int string_id,
277 MenuButtonBackground::ButtonType type,
278 int index,
279 MenuButtonBackground** background,
280 int acc_string_id) {
281 TextButton* button =
[email protected]7c1b7b42011-01-06 21:39:37282 new TextButton(this, UTF16ToWide(l10n_util::GetStringUTF16(string_id)));
[email protected]001f08a2011-01-14 18:34:56283 button->SetAccessibleName(
284 GetAccessibleNameForWrenchMenuItem(menu_model_, index, acc_string_id));
[email protected]2cc800b2010-10-01 18:27:34285 button->SetFocusable(true);
286 button->set_request_focus_on_press(false);
287 button->set_tag(index);
288 button->SetEnabled(menu_model_->IsEnabledAt(index));
289 button->set_prefix_type(TextButton::PREFIX_HIDE);
290 MenuButtonBackground* bg = new MenuButtonBackground(type);
291 button->set_background(bg);
292 button->SetEnabledColor(MenuConfig::instance().text_color);
293 if (background)
294 *background = bg;
295 button->set_border(new MenuButtonBorder());
296 button->set_alignment(TextButton::ALIGN_CENTER);
297 button->SetNormalHasBorder(true);
298 button->SetFont(views::MenuConfig::instance().font);
299 button->ClearMaxTextSize();
300 AddChildView(button);
301 return button;
302 }
303
304 protected:
305 // Hosting WrenchMenu.
306 WrenchMenu* menu_;
307
308 // The menu model containing the increment/decrement/reset items.
309 MenuModel* menu_model_;
310
311 private:
312 DISALLOW_COPY_AND_ASSIGN(WrenchMenuView);
313};
314
[email protected]eccda9db2010-06-23 17:27:08315} // namespace
316
317// CutCopyPasteView ------------------------------------------------------------
318
319// CutCopyPasteView is the view containing the cut/copy/paste buttons.
[email protected]2cc800b2010-10-01 18:27:34320class WrenchMenu::CutCopyPasteView : public WrenchMenuView {
[email protected]eccda9db2010-06-23 17:27:08321 public:
322 CutCopyPasteView(WrenchMenu* menu,
323 MenuModel* menu_model,
324 int cut_index,
325 int copy_index,
326 int paste_index)
[email protected]2cc800b2010-10-01 18:27:34327 : WrenchMenuView(menu, menu_model) {
[email protected]eccda9db2010-06-23 17:27:08328 TextButton* cut = CreateAndConfigureButton(
[email protected]2cc800b2010-10-01 18:27:34329 IDS_CUT, MenuButtonBackground::LEFT_BUTTON, cut_index, NULL);
[email protected]eccda9db2010-06-23 17:27:08330
331 MenuButtonBackground* copy_background = NULL;
332 CreateAndConfigureButton(
[email protected]2cc800b2010-10-01 18:27:34333 IDS_COPY, MenuButtonBackground::CENTER_BUTTON, copy_index,
334 &copy_background);
[email protected]eccda9db2010-06-23 17:27:08335
336 TextButton* paste = CreateAndConfigureButton(
[email protected]2cc800b2010-10-01 18:27:34337 IDS_PASTE, MenuButtonBackground::RIGHT_BUTTON, paste_index, NULL);
[email protected]eccda9db2010-06-23 17:27:08338
339 copy_background->SetOtherButtons(cut, paste);
340 }
341
342 gfx::Size GetPreferredSize() {
343 // Returned height doesn't matter as MenuItemView forces everything to the
344 // height of the menuitemview.
[email protected]20838602011-02-09 04:50:21345 return gfx::Size(GetMaxChildViewPreferredWidth() * child_count(), 0);
[email protected]eccda9db2010-06-23 17:27:08346 }
347
348 void Layout() {
349 // All buttons are given the same width.
350 int width = GetMaxChildViewPreferredWidth();
[email protected]20838602011-02-09 04:50:21351 for (int i = 0; i < child_count(); ++i)
[email protected]eccda9db2010-06-23 17:27:08352 GetChildViewAt(i)->SetBounds(i * width, 0, width, height());
353 }
354
355 // ButtonListener
356 virtual void ButtonPressed(views::Button* sender, const views::Event& event) {
357 menu_->CancelAndEvaluate(menu_model_, sender->tag());
358 }
359
360 private:
361 // Returns the max preferred width of all the children.
362 int GetMaxChildViewPreferredWidth() {
363 int width = 0;
[email protected]20838602011-02-09 04:50:21364 for (int i = 0; i < child_count(); ++i)
[email protected]eccda9db2010-06-23 17:27:08365 width = std::max(width, GetChildViewAt(i)->GetPreferredSize().width());
366 return width;
367 }
368
[email protected]eccda9db2010-06-23 17:27:08369 DISALLOW_COPY_AND_ASSIGN(CutCopyPasteView);
370};
371
372// ZoomView --------------------------------------------------------------------
373
374// Padding between the increment buttons and the reset button.
375static const int kZoomPadding = 6;
376
377// ZoomView contains the various zoom controls: two buttons to increase/decrease
378// the zoom, a label showing the current zoom percent, and a button to go
379// full-screen.
[email protected]2cc800b2010-10-01 18:27:34380class WrenchMenu::ZoomView : public WrenchMenuView,
[email protected]eccda9db2010-06-23 17:27:08381 public NotificationObserver {
382 public:
383 ZoomView(WrenchMenu* menu,
384 MenuModel* menu_model,
[email protected]eccda9db2010-06-23 17:27:08385 int decrement_index,
[email protected]e1a02d62010-07-16 16:27:41386 int increment_index,
[email protected]eccda9db2010-06-23 17:27:08387 int fullscreen_index)
[email protected]2cc800b2010-10-01 18:27:34388 : WrenchMenuView(menu, menu_model),
[email protected]eccda9db2010-06-23 17:27:08389 fullscreen_index_(fullscreen_index),
390 increment_button_(NULL),
391 zoom_label_(NULL),
392 decrement_button_(NULL),
393 fullscreen_button_(NULL),
394 zoom_label_width_(0) {
[email protected]2cc800b2010-10-01 18:27:34395 decrement_button_ = CreateButtonWithAccName(
396 IDS_ZOOM_MINUS2, MenuButtonBackground::LEFT_BUTTON, decrement_index,
397 NULL, IDS_ACCNAME_ZOOM_MINUS2);
[email protected]eccda9db2010-06-23 17:27:08398
[email protected]7c1b7b42011-01-06 21:39:37399 zoom_label_ = new Label(
400 UTF16ToWide(l10n_util::GetStringFUTF16Int(IDS_ZOOM_PERCENT, 100)));
[email protected]eccda9db2010-06-23 17:27:08401 zoom_label_->SetColor(MenuConfig::instance().text_color);
402 zoom_label_->SetHorizontalAlignment(Label::ALIGN_RIGHT);
403 MenuButtonBackground* center_bg =
404 new MenuButtonBackground(MenuButtonBackground::CENTER_BUTTON);
405 zoom_label_->set_background(center_bg);
406 zoom_label_->set_border(new MenuButtonBorder());
407 zoom_label_->SetFont(MenuConfig::instance().font);
408 AddChildView(zoom_label_);
[email protected]84e4f8c2010-07-15 02:18:32409 zoom_label_width_ = MaxWidthForZoomLabel();
[email protected]eccda9db2010-06-23 17:27:08410
[email protected]2cc800b2010-10-01 18:27:34411 increment_button_ = CreateButtonWithAccName(
412 IDS_ZOOM_PLUS2, MenuButtonBackground::RIGHT_BUTTON, increment_index,
413 NULL, IDS_ACCNAME_ZOOM_PLUS2);
[email protected]eccda9db2010-06-23 17:27:08414
[email protected]5214bdd2010-08-02 21:43:47415 center_bg->SetOtherButtons(decrement_button_, increment_button_);
[email protected]eccda9db2010-06-23 17:27:08416
417 fullscreen_button_ = new FullscreenButton(this);
418 fullscreen_button_->SetImage(
419 ImageButton::BS_NORMAL,
420 ResourceBundle::GetSharedInstance().GetBitmapNamed(
421 IDR_FULLSCREEN_MENU_BUTTON));
422 fullscreen_button_->SetFocusable(true);
423 fullscreen_button_->set_request_focus_on_press(false);
424 fullscreen_button_->set_tag(fullscreen_index);
425 fullscreen_button_->SetImageAlignment(
426 ImageButton::ALIGN_CENTER, ImageButton::ALIGN_MIDDLE);
427 fullscreen_button_->set_border(views::Border::CreateEmptyBorder(
428 0, kHorizontalPadding, 0, kHorizontalPadding));
429 fullscreen_button_->set_background(
430 new MenuButtonBackground(MenuButtonBackground::SINGLE_BUTTON));
[email protected]001f08a2011-01-14 18:34:56431 fullscreen_button_->SetAccessibleName(
[email protected]2cc800b2010-10-01 18:27:34432 GetAccessibleNameForWrenchMenuItem(
[email protected]001f08a2011-01-14 18:34:56433 menu_model, fullscreen_index, IDS_ACCNAME_FULLSCREEN));
[email protected]eccda9db2010-06-23 17:27:08434 AddChildView(fullscreen_button_);
435
436 UpdateZoomControls();
437
438 registrar_.Add(this, NotificationType::ZOOM_LEVEL_CHANGED,
439 Source<Profile>(menu->browser_->profile()));
440 }
441
442 gfx::Size GetPreferredSize() {
443 // The increment/decrement button are forced to the same width.
444 int button_width = std::max(increment_button_->GetPreferredSize().width(),
445 decrement_button_->GetPreferredSize().width());
446 int fullscreen_width = fullscreen_button_->GetPreferredSize().width();
447 // Returned height doesn't matter as MenuItemView forces everything to the
448 // height of the menuitemview.
449 return gfx::Size(button_width + zoom_label_width_ + button_width +
450 kZoomPadding + fullscreen_width, 0);
451 }
452
453 void Layout() {
454 int x = 0;
455 int button_width = std::max(increment_button_->GetPreferredSize().width(),
456 decrement_button_->GetPreferredSize().width());
457 gfx::Rect bounds(0, 0, button_width, height());
458
[email protected]97a31142011-02-08 00:09:16459 decrement_button_->SetBoundsRect(bounds);
[email protected]eccda9db2010-06-23 17:27:08460
461 x += bounds.width();
462 bounds.set_x(x);
463 bounds.set_width(zoom_label_width_);
[email protected]97a31142011-02-08 00:09:16464 zoom_label_->SetBoundsRect(bounds);
[email protected]eccda9db2010-06-23 17:27:08465
466 x += bounds.width();
467 bounds.set_x(x);
468 bounds.set_width(button_width);
[email protected]97a31142011-02-08 00:09:16469 increment_button_->SetBoundsRect(bounds);
[email protected]eccda9db2010-06-23 17:27:08470
471 x += bounds.width() + kZoomPadding;
472 bounds.set_x(x);
473 bounds.set_width(fullscreen_button_->GetPreferredSize().width());
[email protected]97a31142011-02-08 00:09:16474 fullscreen_button_->SetBoundsRect(bounds);
[email protected]eccda9db2010-06-23 17:27:08475 }
476
477 // ButtonListener:
478 virtual void ButtonPressed(views::Button* sender, const views::Event& event) {
479 if (sender->tag() == fullscreen_index_) {
480 menu_->CancelAndEvaluate(menu_model_, sender->tag());
481 } else {
482 // Zoom buttons don't close the menu.
483 menu_model_->ActivatedAt(sender->tag());
484 }
485 }
486
487 // NotificationObserver:
488 virtual void Observe(NotificationType type,
489 const NotificationSource& source,
490 const NotificationDetails& details) {
491 DCHECK_EQ(NotificationType::ZOOM_LEVEL_CHANGED, type.value);
492 UpdateZoomControls();
493 }
494
495 private:
496 void UpdateZoomControls() {
[email protected]b75b8292010-10-01 07:28:25497 bool enable_increment = false;
498 bool enable_decrement = false;
499 TabContents* selected_tab = menu_->browser_->GetSelectedTabContents();
500 int zoom = 100;
501 if (selected_tab)
502 zoom = selected_tab->GetZoomPercent(&enable_increment, &enable_decrement);
[email protected]eccda9db2010-06-23 17:27:08503 increment_button_->SetEnabled(enable_increment);
504 decrement_button_->SetEnabled(enable_decrement);
[email protected]7c1b7b42011-01-06 21:39:37505 zoom_label_->SetText(UTF16ToWide(l10n_util::GetStringFUTF16Int(
506 IDS_ZOOM_PERCENT,
507 zoom)));
[email protected]eccda9db2010-06-23 17:27:08508
[email protected]b75b8292010-10-01 07:28:25509 zoom_label_width_ = MaxWidthForZoomLabel();
[email protected]84e4f8c2010-07-15 02:18:32510 }
511
512 // Calculates the max width the zoom string can be.
513 int MaxWidthForZoomLabel() {
514 gfx::Font font = zoom_label_->font();
515 gfx::Insets insets;
516 if (zoom_label_->border())
517 zoom_label_->border()->GetInsets(&insets);
[email protected]b75b8292010-10-01 07:28:25518
[email protected]84e4f8c2010-07-15 02:18:32519 int max_w = 0;
[email protected]b75b8292010-10-01 07:28:25520
521 TabContents* selected_tab = menu_->browser_->GetSelectedTabContents();
522 if (selected_tab) {
523 int min_percent = selected_tab->minimum_zoom_percent();
524 int max_percent = selected_tab->maximum_zoom_percent();
525
526 int step = (max_percent - min_percent) / 10;
527 for (int i = min_percent; i <= max_percent; i += step) {
[email protected]13658c42011-01-04 20:46:14528 int w = font.GetStringWidth(
529 l10n_util::GetStringFUTF16Int(IDS_ZOOM_PERCENT, i));
[email protected]b75b8292010-10-01 07:28:25530 max_w = std::max(w, max_w);
531 }
532 } else {
[email protected]13658c42011-01-04 20:46:14533 max_w = font.GetStringWidth(
534 l10n_util::GetStringFUTF16Int(IDS_ZOOM_PERCENT, 100));
[email protected]84e4f8c2010-07-15 02:18:32535 }
[email protected]b75b8292010-10-01 07:28:25536
[email protected]84e4f8c2010-07-15 02:18:32537 return max_w + insets.width();
538 }
539
[email protected]eccda9db2010-06-23 17:27:08540 // Index of the fullscreen menu item in the model.
541 const int fullscreen_index_;
542
543 NotificationRegistrar registrar_;
544
545 // Button for incrementing the zoom.
546 TextButton* increment_button_;
547
548 // Label showing zoom as a percent.
549 Label* zoom_label_;
550
551 // Button for decrementing the zoom.
552 TextButton* decrement_button_;
553
554 ImageButton* fullscreen_button_;
555
556 // Width given to |zoom_label_|. This is the width at 100%.
557 int zoom_label_width_;
558
559 DISALLOW_COPY_AND_ASSIGN(ZoomView);
560};
561
562// WrenchMenu ------------------------------------------------------------------
563
564WrenchMenu::WrenchMenu(Browser* browser)
565 : browser_(browser),
566 selected_menu_model_(NULL),
567 selected_index_(0) {
568}
569
[email protected]44cbd9e2011-01-14 15:49:40570void WrenchMenu::Init(ui::MenuModel* model) {
[email protected]eccda9db2010-06-23 17:27:08571 DCHECK(!root_.get());
572 root_.reset(new MenuItemView(this));
[email protected]001f08a2011-01-14 18:34:56573 root_->SetAccessibleName(l10n_util::GetStringUTF16(IDS_ACCNAME_APP));
[email protected]eccda9db2010-06-23 17:27:08574 root_->set_has_icons(true); // We have checks, radios and icons, set this
575 // so we get the taller menu style.
576 int next_id = 1;
577 PopulateMenu(root_.get(), model, &next_id);
578}
579
[email protected]a00b0322010-06-25 16:21:32580void WrenchMenu::RunMenu(views::MenuButton* host) {
[email protected]428f54b2010-10-05 03:31:27581 // Up the ref count while the menu is displaying. This way if the window is
582 // deleted while we're running we won't prematurely delete the menu.
583 // TODO(sky): fix this, the menu should really take ownership of the menu
584 // (57890).
585 scoped_refptr<WrenchMenu> dont_delete_while_running(this);
[email protected]eccda9db2010-06-23 17:27:08586 gfx::Point screen_loc;
587 views::View::ConvertPointToScreen(host, &screen_loc);
[email protected]e78f12a22010-07-28 22:41:05588 gfx::Rect bounds(screen_loc, host->size());
[email protected]366069f2011-01-11 09:36:12589 UserMetrics::RecordAction(UserMetricsAction("ShowAppMenu"));
[email protected]a00b0322010-06-25 16:21:32590 root_->RunMenuAt(host->GetWindow()->GetNativeWindow(), host, bounds,
[email protected]627da3f2010-07-27 21:19:01591 base::i18n::IsRTL() ? MenuItemView::TOPLEFT : MenuItemView::TOPRIGHT,
592 true);
[email protected]eccda9db2010-06-23 17:27:08593 if (selected_menu_model_)
594 selected_menu_model_->ActivatedAt(selected_index_);
595}
596
597bool WrenchMenu::IsItemChecked(int id) const {
598 const Entry& entry = id_to_entry_.find(id)->second;
599 return entry.first->IsItemCheckedAt(entry.second);
600}
601
602bool WrenchMenu::IsCommandEnabled(int id) const {
603 if (id == 0)
604 return false; // The root item.
605
606 const Entry& entry = id_to_entry_.find(id)->second;
607 int command_id = entry.first->GetCommandIdAt(entry.second);
608 // The items representing the cut (cut/copy/paste) and zoom menu
609 // (increment/decrement/reset) are always enabled. The child views of these
610 // items enabled state updates appropriately.
[email protected]e1a02d62010-07-16 16:27:41611 return command_id == IDC_CUT || command_id == IDC_ZOOM_MINUS ||
[email protected]eccda9db2010-06-23 17:27:08612 entry.first->IsEnabledAt(entry.second);
613}
614
615void WrenchMenu::ExecuteCommand(int id) {
616 const Entry& entry = id_to_entry_.find(id)->second;
[email protected]60555ec2010-07-02 20:18:25617 int command_id = entry.first->GetCommandIdAt(entry.second);
618
[email protected]e1a02d62010-07-16 16:27:41619 if (command_id == IDC_CUT || command_id == IDC_ZOOM_MINUS) {
[email protected]60555ec2010-07-02 20:18:25620 // These items are represented by child views. If ExecuteCommand is invoked
621 // it means the user clicked on the area around the buttons and we should
622 // not do anyting.
623 return;
624 }
625
[email protected]eccda9db2010-06-23 17:27:08626 return entry.first->ActivatedAt(entry.second);
627}
628
629bool WrenchMenu::GetAccelerator(int id, views::Accelerator* accelerator) {
630 const Entry& entry = id_to_entry_.find(id)->second;
631 int command_id = entry.first->GetCommandIdAt(entry.second);
[email protected]e1a02d62010-07-16 16:27:41632 if (command_id == IDC_CUT || command_id == IDC_ZOOM_MINUS) {
[email protected]eccda9db2010-06-23 17:27:08633 // These have special child views; don't show the accelerator for them.
634 return false;
635 }
636
[email protected]44cbd9e2011-01-14 15:49:40637 ui::Accelerator menu_accelerator;
[email protected]eccda9db2010-06-23 17:27:08638 if (!entry.first->GetAcceleratorAt(entry.second, &menu_accelerator))
639 return false;
640
641 *accelerator = views::Accelerator(menu_accelerator.GetKeyCode(),
642 menu_accelerator.modifiers());
643 return true;
644}
645
[email protected]428f54b2010-10-05 03:31:27646WrenchMenu::~WrenchMenu() {
647}
648
[email protected]eccda9db2010-06-23 17:27:08649void WrenchMenu::PopulateMenu(MenuItemView* parent,
650 MenuModel* model,
651 int* next_id) {
652 int index_offset = model->GetFirstItemIndex(NULL);
653 for (int i = 0, max = model->GetItemCount(); i < max; ++i) {
654 int index = i + index_offset;
655
656 MenuItemView* item =
657 AppendMenuItem(parent, model, index, model->GetTypeAt(index), next_id);
658
659 if (model->GetTypeAt(index) == MenuModel::TYPE_SUBMENU)
660 PopulateMenu(item, model->GetSubmenuModelAt(index), next_id);
661
662 if (model->GetCommandIdAt(index) == IDC_CUT) {
663 DCHECK_EQ(MenuModel::TYPE_COMMAND, model->GetTypeAt(index));
664 DCHECK_LT(i + 2, max);
665 DCHECK_EQ(IDC_COPY, model->GetCommandIdAt(index + 1));
666 DCHECK_EQ(IDC_PASTE, model->GetCommandIdAt(index + 2));
[email protected]7c1b7b42011-01-06 21:39:37667 item->SetTitle(UTF16ToWide(l10n_util::GetStringUTF16(IDS_EDIT2)));
[email protected]eccda9db2010-06-23 17:27:08668 item->AddChildView(
669 new CutCopyPasteView(this, model, index, index + 1, index + 2));
670 i += 2;
[email protected]e1a02d62010-07-16 16:27:41671 } else if (model->GetCommandIdAt(index) == IDC_ZOOM_MINUS) {
[email protected]eccda9db2010-06-23 17:27:08672 DCHECK_EQ(MenuModel::TYPE_COMMAND, model->GetTypeAt(index));
[email protected]e1a02d62010-07-16 16:27:41673 DCHECK_EQ(IDC_ZOOM_PLUS, model->GetCommandIdAt(index + 1));
[email protected]eccda9db2010-06-23 17:27:08674 DCHECK_EQ(IDC_FULLSCREEN, model->GetCommandIdAt(index + 2));
[email protected]7c1b7b42011-01-06 21:39:37675 item->SetTitle(UTF16ToWide(l10n_util::GetStringUTF16(IDS_ZOOM_MENU2)));
[email protected]eccda9db2010-06-23 17:27:08676 item->AddChildView(
677 new ZoomView(this, model, index, index + 1, index + 2));
678 i += 2;
679 }
680 }
681}
682
683MenuItemView* WrenchMenu::AppendMenuItem(MenuItemView* parent,
684 MenuModel* model,
685 int index,
686 MenuModel::ItemType menu_type,
687 int* next_id) {
688 int id = (*next_id)++;
[email protected]eccda9db2010-06-23 17:27:08689
690 id_to_entry_[id].first = model;
691 id_to_entry_[id].second = index;
692
[email protected]74434942010-12-05 04:00:59693 MenuItemView* menu_item = parent->AppendMenuItemFromModel(model, index, id);
[email protected]eccda9db2010-06-23 17:27:08694
[email protected]d4c4f252010-09-01 19:27:46695 if (menu_item)
696 menu_item->SetVisible(model->IsVisibleAt(index));
697
[email protected]eccda9db2010-06-23 17:27:08698 if (menu_type == MenuModel::TYPE_COMMAND && model->HasIcons()) {
699 SkBitmap icon;
700 if (model->GetIconAt(index, &icon))
701 menu_item->SetIcon(icon);
702 }
703
704 return menu_item;
705}
706
707void WrenchMenu::CancelAndEvaluate(MenuModel* model, int index) {
708 selected_menu_model_ = model;
709 selected_index_ = index;
710 root_->Cancel();
711}