blob: c798d5abfcba9355513698fc98c50f2ce90b3bf3 [file] [log] [blame]
[email protected]eccda9db2010-06-23 17:27:081// Copyright (c) 2010 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 "chrome/browser/views/wrench_menu.h"
6
7#include <cmath>
8
9#include "app/l10n_util.h"
10#include "app/resource_bundle.h"
[email protected]e83326f2010-07-31 17:29:2511#include "base/string_number_conversions.h"
[email protected]eccda9db2010-06-23 17:27:0812#include "base/utf_string_conversions.h"
[email protected]1a3aba82010-11-08 23:52:5413#include "chrome/app/chrome_command_ids.h"
[email protected]eccda9db2010-06-23 17:27:0814#include "chrome/browser/profile.h"
15#include "chrome/browser/tab_contents/tab_contents.h"
[email protected]f46be6e2010-11-16 03:52:3216#include "chrome/browser/ui/browser.h"
[email protected]eccda9db2010-06-23 17:27:0817#include "chrome/common/notification_observer.h"
18#include "chrome/common/notification_registrar.h"
19#include "chrome/common/notification_source.h"
20#include "chrome/common/notification_type.h"
21#include "gfx/canvas.h"
[email protected]84e4f8c2010-07-15 02:18:3222#include "gfx/canvas_skia.h"
[email protected]eccda9db2010-06-23 17:27:0823#include "gfx/skia_util.h"
[email protected]e12e3f62010-08-06 00:44:5924#include "grit/chromium_strings.h"
[email protected]eccda9db2010-06-23 17:27:0825#include "grit/generated_resources.h"
26#include "grit/theme_resources.h"
27#include "third_party/skia/include/core/SkPaint.h"
28#include "views/background.h"
29#include "views/controls/button/image_button.h"
[email protected]a00b0322010-06-25 16:21:3230#include "views/controls/button/menu_button.h"
[email protected]eccda9db2010-06-23 17:27:0831#include "views/controls/button/text_button.h"
32#include "views/controls/label.h"
33#include "views/controls/menu/menu_config.h"
34#include "views/controls/menu/menu_item_view.h"
35#include "views/controls/menu/menu_scroll_view_container.h"
36#include "views/controls/menu/submenu_view.h"
37#include "views/window/window.h"
38
39using menus::MenuModel;
40using views::CustomButton;
41using views::ImageButton;
42using views::Label;
43using views::MenuConfig;
44using views::MenuItemView;
45using views::TextButton;
46using views::View;
47
48namespace {
49
50// Colors used for buttons.
51const SkColor kHotBorderColor = SkColorSetARGB(72, 0, 0, 0);
52const SkColor kBorderColor = SkColorSetARGB(36, 0, 0, 0);
53const SkColor kPushedBorderColor = SkColorSetARGB(72, 0, 0, 0);
54const SkColor kHotBackgroundColor = SkColorSetARGB(204, 255, 255, 255);
55const SkColor kBackgroundColor = SkColorSetARGB(102, 255, 255, 255);
56const SkColor kPushedBackgroundColor = SkColorSetARGB(13, 0, 0, 0);
57
58// Horizontal padding on the edges of the buttons.
59const int kHorizontalPadding = 6;
60
61// Subclass of ImageButton whose preferred size includes the size of the border.
62class FullscreenButton : public ImageButton {
63 public:
[email protected]65ecd5b2010-07-27 21:29:0664 explicit FullscreenButton(views::ButtonListener* listener)
65 : ImageButton(listener) { }
[email protected]eccda9db2010-06-23 17:27:0866
67 virtual gfx::Size GetPreferredSize() {
68 gfx::Size pref = ImageButton::GetPreferredSize();
69 gfx::Insets insets;
70 if (border())
71 border()->GetInsets(&insets);
72 pref.Enlarge(insets.width(), insets.height());
73 return pref;
74 }
75
76 private:
77 DISALLOW_COPY_AND_ASSIGN(FullscreenButton);
78};
79
80// Border for buttons contained in the menu. This is only used for getting the
81// insets, the actual painting is done in MenuButtonBackground.
82class MenuButtonBorder : public views::Border {
83 public:
84 MenuButtonBorder() {}
85
86 virtual void Paint(const View& view, gfx::Canvas* canvas) const {
87 // Painting of border is done in MenuButtonBackground.
88 }
89
90 virtual void GetInsets(gfx::Insets* insets) const {
[email protected]e57bfac2010-08-03 17:19:5491 insets->Set(MenuConfig::instance().item_top_margin,
[email protected]eccda9db2010-06-23 17:27:0892 kHorizontalPadding,
[email protected]e57bfac2010-08-03 17:19:5493 MenuConfig::instance().item_bottom_margin,
[email protected]eccda9db2010-06-23 17:27:0894 kHorizontalPadding);
95 }
96
97 private:
98 DISALLOW_COPY_AND_ASSIGN(MenuButtonBorder);
99};
100
101// Combination border/background for the buttons contained in the menu. The
102// painting of the border/background is done here as TextButton does not always
103// paint the border.
104class MenuButtonBackground : public views::Background {
105 public:
106 enum ButtonType {
107 LEFT_BUTTON,
108 CENTER_BUTTON,
109 RIGHT_BUTTON,
110 SINGLE_BUTTON,
111 };
112
113 explicit MenuButtonBackground(ButtonType type)
114 : type_(type),
115 left_button_(NULL),
116 right_button_(NULL) {}
117
118 // Used when the type is CENTER_BUTTON to determine if the left/right edge
119 // needs to be rendered selected.
120 void SetOtherButtons(CustomButton* left_button, CustomButton* right_button) {
[email protected]5214bdd2010-08-02 21:43:47121 if (base::i18n::IsRTL()) {
122 left_button_ = right_button;
123 right_button_ = left_button;
124 } else {
125 left_button_ = left_button;
126 right_button_ = right_button;
127 }
[email protected]eccda9db2010-06-23 17:27:08128 }
129
130 virtual void Paint(gfx::Canvas* canvas, View* view) const {
131 CustomButton::ButtonState state =
132 (view->GetClassName() == views::Label::kViewClassName) ?
133 CustomButton::BS_NORMAL : static_cast<CustomButton*>(view)->state();
134 int w = view->width();
135 int h = view->height();
[email protected]34344902010-07-07 20:26:26136 switch (TypeAdjustedForRTL()) {
[email protected]eccda9db2010-06-23 17:27:08137 case LEFT_BUTTON:
138 canvas->FillRectInt(background_color(state), 1, 1, w, h - 2);
139 canvas->FillRectInt(border_color(state), 2, 0, w, 1);
140 canvas->FillRectInt(border_color(state), 1, 1, 1, 1);
141 canvas->FillRectInt(border_color(state), 0, 2, 1, h - 4);
142 canvas->FillRectInt(border_color(state), 1, h - 2, 1, 1);
143 canvas->FillRectInt(border_color(state), 2, h - 1, w, 1);
144 break;
145
146 case CENTER_BUTTON: {
147 canvas->FillRectInt(background_color(state), 1, 1, w - 2, h - 2);
148 SkColor left_color = state != CustomButton::BS_NORMAL ?
149 border_color(state) : border_color(left_button_->state());
150 canvas->FillRectInt(left_color, 0, 0, 1, h);
151 canvas->FillRectInt(border_color(state), 1, 0, w - 2, 1);
152 canvas->FillRectInt(border_color(state), 1, h - 1, w - 2, 1);
153 SkColor right_color = state != CustomButton::BS_NORMAL ?
154 border_color(state) : border_color(right_button_->state());
155 canvas->FillRectInt(right_color, w - 1, 0, 1, h);
156 break;
157 }
158
159 case RIGHT_BUTTON:
160 canvas->FillRectInt(background_color(state), 0, 1, w - 1, h - 2);
161 canvas->FillRectInt(border_color(state), 0, 0, w - 2, 1);
162 canvas->FillRectInt(border_color(state), w - 2, 1, 1, 1);
163 canvas->FillRectInt(border_color(state), w - 1, 2, 1, h - 4);
164 canvas->FillRectInt(border_color(state), w - 2, h - 2, 1, 1);
165 canvas->FillRectInt(border_color(state), 0, h - 1, w - 2, 1);
166 break;
167
168 case SINGLE_BUTTON:
169 canvas->FillRectInt(background_color(state), 1, 1, w - 2, h - 2);
170 canvas->FillRectInt(border_color(state), 2, 0, w - 4, 1);
171 canvas->FillRectInt(border_color(state), 1, 1, 1, 1);
172 canvas->FillRectInt(border_color(state), 0, 2, 1, h - 4);
173 canvas->FillRectInt(border_color(state), 1, h - 2, 1, 1);
174 canvas->FillRectInt(border_color(state), 2, h - 1, w - 4, 1);
175 canvas->FillRectInt(border_color(state), w - 2, 1, 1, 1);
176 canvas->FillRectInt(border_color(state), w - 1, 2, 1, h - 4);
177 canvas->FillRectInt(border_color(state), w - 2, h - 2, 1, 1);
178 break;
179
180 default:
181 NOTREACHED();
182 break;
183 }
184 }
185
186 private:
187 static SkColor border_color(CustomButton::ButtonState state) {
188 switch (state) {
189 case CustomButton::BS_HOT: return kHotBorderColor;
190 case CustomButton::BS_PUSHED: return kPushedBorderColor;
191 default: return kBorderColor;
192 }
193 }
194
195 static SkColor background_color(CustomButton::ButtonState state) {
196 switch (state) {
197 case CustomButton::BS_HOT: return kHotBackgroundColor;
198 case CustomButton::BS_PUSHED: return kPushedBackgroundColor;
199 default: return kBackgroundColor;
200 }
201 }
202
[email protected]34344902010-07-07 20:26:26203 ButtonType TypeAdjustedForRTL() const {
204 if (!base::i18n::IsRTL())
205 return type_;
206
207 switch (type_) {
208 case LEFT_BUTTON: return RIGHT_BUTTON;
209 case RIGHT_BUTTON: return LEFT_BUTTON;
210 default: break;
211 }
212 return type_;
213 }
214
[email protected]eccda9db2010-06-23 17:27:08215 const ButtonType type_;
216
217 // See description above setter for details.
218 CustomButton* left_button_;
219 CustomButton* right_button_;
220
221 DISALLOW_COPY_AND_ASSIGN(MenuButtonBackground);
222};
223
224// A View subclass that forces SchedulePaint to paint all. Normally when the
225// mouse enters/exits a button the buttons invokes SchedulePaint. As part of the
226// button border (MenuButtonBackground) is rendered by the button to the
227// left/right of it SchedulePaint on the the button may not be enough, so this
228// forces a paint all.
229class ScheduleAllView : public views::View {
230 public:
231 ScheduleAllView() {}
232
233 virtual void SchedulePaint(const gfx::Rect& r, bool urgent) {
234 if (!IsVisible())
235 return;
236
[email protected]34344902010-07-07 20:26:26237 if (GetParent()) {
238 GetParent()->SchedulePaint(GetBounds(APPLY_MIRRORING_TRANSFORMATION),
239 urgent);
240 }
[email protected]eccda9db2010-06-23 17:27:08241 }
242
243 private:
244 DISALLOW_COPY_AND_ASSIGN(ScheduleAllView);
245};
246
[email protected]2cc800b2010-10-01 18:27:34247std::wstring GetAccessibleNameForWrenchMenuItem(
248 MenuModel* model, int item_index, int accessible_string_id) {
249 std::wstring accessible_name = l10n_util::GetString(accessible_string_id);
250 std::wstring accelerator_text;
251
252 menus::Accelerator menu_accelerator;
253 if (model->GetAcceleratorAt(item_index, &menu_accelerator)) {
254 accelerator_text =
255 views::Accelerator(menu_accelerator.GetKeyCode(),
256 menu_accelerator.modifiers()).GetShortcutText();
257 }
258
259 return MenuItemView::GetAccessibleNameForMenuItem(
260 accessible_name, accelerator_text);
[email protected]eccda9db2010-06-23 17:27:08261}
262
[email protected]2cc800b2010-10-01 18:27:34263// WrenchMenuView is a view that can contain text buttons.
264class WrenchMenuView : public ScheduleAllView, public views::ButtonListener {
265 public:
266 WrenchMenuView(WrenchMenu* menu, MenuModel* menu_model)
267 : menu_(menu), menu_model_(menu_model) { }
268
269 TextButton* CreateAndConfigureButton(int string_id,
270 MenuButtonBackground::ButtonType type,
271 int index,
272 MenuButtonBackground** background) {
273 return CreateButtonWithAccName(
274 string_id, type, index, background, string_id);
275 }
276
277 TextButton* CreateButtonWithAccName(int string_id,
278 MenuButtonBackground::ButtonType type,
279 int index,
280 MenuButtonBackground** background,
281 int acc_string_id) {
282 TextButton* button =
283 new TextButton(this, l10n_util::GetString(string_id));
284 button->SetAccessibleName(
285 GetAccessibleNameForWrenchMenuItem(menu_model_, index, acc_string_id));
286 button->SetFocusable(true);
287 button->set_request_focus_on_press(false);
288 button->set_tag(index);
289 button->SetEnabled(menu_model_->IsEnabledAt(index));
290 button->set_prefix_type(TextButton::PREFIX_HIDE);
291 MenuButtonBackground* bg = new MenuButtonBackground(type);
292 button->set_background(bg);
293 button->SetEnabledColor(MenuConfig::instance().text_color);
294 if (background)
295 *background = bg;
296 button->set_border(new MenuButtonBorder());
297 button->set_alignment(TextButton::ALIGN_CENTER);
298 button->SetNormalHasBorder(true);
299 button->SetFont(views::MenuConfig::instance().font);
300 button->ClearMaxTextSize();
301 AddChildView(button);
302 return button;
303 }
304
305 protected:
306 // Hosting WrenchMenu.
307 WrenchMenu* menu_;
308
309 // The menu model containing the increment/decrement/reset items.
310 MenuModel* menu_model_;
311
312 private:
313 DISALLOW_COPY_AND_ASSIGN(WrenchMenuView);
314};
315
[email protected]eccda9db2010-06-23 17:27:08316} // namespace
317
318// CutCopyPasteView ------------------------------------------------------------
319
320// CutCopyPasteView is the view containing the cut/copy/paste buttons.
[email protected]2cc800b2010-10-01 18:27:34321class WrenchMenu::CutCopyPasteView : public WrenchMenuView {
[email protected]eccda9db2010-06-23 17:27:08322 public:
323 CutCopyPasteView(WrenchMenu* menu,
324 MenuModel* menu_model,
325 int cut_index,
326 int copy_index,
327 int paste_index)
[email protected]2cc800b2010-10-01 18:27:34328 : WrenchMenuView(menu, menu_model) {
[email protected]eccda9db2010-06-23 17:27:08329 TextButton* cut = CreateAndConfigureButton(
[email protected]2cc800b2010-10-01 18:27:34330 IDS_CUT, MenuButtonBackground::LEFT_BUTTON, cut_index, NULL);
[email protected]eccda9db2010-06-23 17:27:08331
332 MenuButtonBackground* copy_background = NULL;
333 CreateAndConfigureButton(
[email protected]2cc800b2010-10-01 18:27:34334 IDS_COPY, MenuButtonBackground::CENTER_BUTTON, copy_index,
335 &copy_background);
[email protected]eccda9db2010-06-23 17:27:08336
337 TextButton* paste = CreateAndConfigureButton(
[email protected]2cc800b2010-10-01 18:27:34338 IDS_PASTE, MenuButtonBackground::RIGHT_BUTTON, paste_index, NULL);
[email protected]eccda9db2010-06-23 17:27:08339
340 copy_background->SetOtherButtons(cut, paste);
341 }
342
343 gfx::Size GetPreferredSize() {
344 // Returned height doesn't matter as MenuItemView forces everything to the
345 // height of the menuitemview.
346 return gfx::Size(GetMaxChildViewPreferredWidth() * GetChildViewCount(), 0);
347 }
348
349 void Layout() {
350 // All buttons are given the same width.
351 int width = GetMaxChildViewPreferredWidth();
352 for (int i = 0; i < GetChildViewCount(); ++i)
353 GetChildViewAt(i)->SetBounds(i * width, 0, width, height());
354 }
355
356 // ButtonListener
357 virtual void ButtonPressed(views::Button* sender, const views::Event& event) {
358 menu_->CancelAndEvaluate(menu_model_, sender->tag());
359 }
360
361 private:
362 // Returns the max preferred width of all the children.
363 int GetMaxChildViewPreferredWidth() {
364 int width = 0;
365 for (int i = 0; i < GetChildViewCount(); ++i)
366 width = std::max(width, GetChildViewAt(i)->GetPreferredSize().width());
367 return width;
368 }
369
[email protected]eccda9db2010-06-23 17:27:08370 DISALLOW_COPY_AND_ASSIGN(CutCopyPasteView);
371};
372
373// ZoomView --------------------------------------------------------------------
374
375// Padding between the increment buttons and the reset button.
376static const int kZoomPadding = 6;
377
378// ZoomView contains the various zoom controls: two buttons to increase/decrease
379// the zoom, a label showing the current zoom percent, and a button to go
380// full-screen.
[email protected]2cc800b2010-10-01 18:27:34381class WrenchMenu::ZoomView : public WrenchMenuView,
[email protected]eccda9db2010-06-23 17:27:08382 public NotificationObserver {
383 public:
384 ZoomView(WrenchMenu* menu,
385 MenuModel* menu_model,
[email protected]eccda9db2010-06-23 17:27:08386 int decrement_index,
[email protected]e1a02d62010-07-16 16:27:41387 int increment_index,
[email protected]eccda9db2010-06-23 17:27:08388 int fullscreen_index)
[email protected]2cc800b2010-10-01 18:27:34389 : WrenchMenuView(menu, menu_model),
[email protected]eccda9db2010-06-23 17:27:08390 fullscreen_index_(fullscreen_index),
391 increment_button_(NULL),
392 zoom_label_(NULL),
393 decrement_button_(NULL),
394 fullscreen_button_(NULL),
395 zoom_label_width_(0) {
[email protected]2cc800b2010-10-01 18:27:34396 decrement_button_ = CreateButtonWithAccName(
397 IDS_ZOOM_MINUS2, MenuButtonBackground::LEFT_BUTTON, decrement_index,
398 NULL, IDS_ACCNAME_ZOOM_MINUS2);
[email protected]eccda9db2010-06-23 17:27:08399
400 zoom_label_ = new Label(l10n_util::GetStringF(IDS_ZOOM_PERCENT, L"100"));
401 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]955c2f7f2010-08-03 19:07:35431 fullscreen_button_->SetAccessibleName(
[email protected]2cc800b2010-10-01 18:27:34432 GetAccessibleNameForWrenchMenuItem(
433 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]e1a02d62010-07-16 16:27:41459 decrement_button_->SetBounds(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_);
464 zoom_label_->SetBounds(bounds);
465
466 x += bounds.width();
467 bounds.set_x(x);
468 bounds.set_width(button_width);
[email protected]e1a02d62010-07-16 16:27:41469 increment_button_->SetBounds(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());
474 fullscreen_button_->SetBounds(bounds);
475 }
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]7e1f2dec2010-07-21 18:31:43505 zoom_label_->SetText(l10n_util::GetStringF(
[email protected]e83326f2010-07-31 17:29:25506 IDS_ZOOM_PERCENT,
[email protected]b75b8292010-10-01 07:28:25507 UTF8ToWide(base::IntToString(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) {
528 int w = font.GetStringWidth(l10n_util::GetStringF(IDS_ZOOM_PERCENT, i));
529 max_w = std::max(w, max_w);
530 }
531 } else {
[email protected]e06bea62010-10-01 07:41:11532 max_w = font.GetStringWidth(l10n_util::GetStringF(IDS_ZOOM_PERCENT, 100));
[email protected]84e4f8c2010-07-15 02:18:32533 }
[email protected]b75b8292010-10-01 07:28:25534
[email protected]84e4f8c2010-07-15 02:18:32535 return max_w + insets.width();
536 }
537
[email protected]eccda9db2010-06-23 17:27:08538 // Index of the fullscreen menu item in the model.
539 const int fullscreen_index_;
540
541 NotificationRegistrar registrar_;
542
543 // Button for incrementing the zoom.
544 TextButton* increment_button_;
545
546 // Label showing zoom as a percent.
547 Label* zoom_label_;
548
549 // Button for decrementing the zoom.
550 TextButton* decrement_button_;
551
552 ImageButton* fullscreen_button_;
553
554 // Width given to |zoom_label_|. This is the width at 100%.
555 int zoom_label_width_;
556
557 DISALLOW_COPY_AND_ASSIGN(ZoomView);
558};
559
560// WrenchMenu ------------------------------------------------------------------
561
562WrenchMenu::WrenchMenu(Browser* browser)
563 : browser_(browser),
564 selected_menu_model_(NULL),
565 selected_index_(0) {
566}
567
[email protected]eccda9db2010-06-23 17:27:08568void WrenchMenu::Init(menus::MenuModel* model) {
569 DCHECK(!root_.get());
570 root_.reset(new MenuItemView(this));
[email protected]e12e3f62010-08-06 00:44:59571 root_->SetAccessibleName(l10n_util::GetString(IDS_ACCNAME_APP));
[email protected]eccda9db2010-06-23 17:27:08572 root_->set_has_icons(true); // We have checks, radios and icons, set this
573 // so we get the taller menu style.
574 int next_id = 1;
575 PopulateMenu(root_.get(), model, &next_id);
576}
577
[email protected]a00b0322010-06-25 16:21:32578void WrenchMenu::RunMenu(views::MenuButton* host) {
[email protected]428f54b2010-10-05 03:31:27579 // Up the ref count while the menu is displaying. This way if the window is
580 // deleted while we're running we won't prematurely delete the menu.
581 // TODO(sky): fix this, the menu should really take ownership of the menu
582 // (57890).
583 scoped_refptr<WrenchMenu> dont_delete_while_running(this);
[email protected]eccda9db2010-06-23 17:27:08584 gfx::Point screen_loc;
585 views::View::ConvertPointToScreen(host, &screen_loc);
[email protected]e78f12a22010-07-28 22:41:05586 gfx::Rect bounds(screen_loc, host->size());
[email protected]a00b0322010-06-25 16:21:32587 root_->RunMenuAt(host->GetWindow()->GetNativeWindow(), host, bounds,
[email protected]627da3f2010-07-27 21:19:01588 base::i18n::IsRTL() ? MenuItemView::TOPLEFT : MenuItemView::TOPRIGHT,
589 true);
[email protected]eccda9db2010-06-23 17:27:08590 if (selected_menu_model_)
591 selected_menu_model_->ActivatedAt(selected_index_);
592}
593
594bool WrenchMenu::IsItemChecked(int id) const {
595 const Entry& entry = id_to_entry_.find(id)->second;
596 return entry.first->IsItemCheckedAt(entry.second);
597}
598
599bool WrenchMenu::IsCommandEnabled(int id) const {
600 if (id == 0)
601 return false; // The root item.
602
[email protected]eccda9db2010-06-23 17:27:08603 const Entry& entry = id_to_entry_.find(id)->second;
604 int command_id = entry.first->GetCommandIdAt(entry.second);
605 // The items representing the cut (cut/copy/paste) and zoom menu
606 // (increment/decrement/reset) are always enabled. The child views of these
607 // items enabled state updates appropriately.
[email protected]e1a02d62010-07-16 16:27:41608 return command_id == IDC_CUT || command_id == IDC_ZOOM_MINUS ||
[email protected]eccda9db2010-06-23 17:27:08609 entry.first->IsEnabledAt(entry.second);
610}
611
612void WrenchMenu::ExecuteCommand(int id) {
613 const Entry& entry = id_to_entry_.find(id)->second;
[email protected]60555ec2010-07-02 20:18:25614 int command_id = entry.first->GetCommandIdAt(entry.second);
615
[email protected]e1a02d62010-07-16 16:27:41616 if (command_id == IDC_CUT || command_id == IDC_ZOOM_MINUS) {
[email protected]60555ec2010-07-02 20:18:25617 // These items are represented by child views. If ExecuteCommand is invoked
618 // it means the user clicked on the area around the buttons and we should
619 // not do anyting.
620 return;
621 }
622
[email protected]eccda9db2010-06-23 17:27:08623 return entry.first->ActivatedAt(entry.second);
624}
625
626bool WrenchMenu::GetAccelerator(int id, views::Accelerator* accelerator) {
627 const Entry& entry = id_to_entry_.find(id)->second;
628 int command_id = entry.first->GetCommandIdAt(entry.second);
[email protected]e1a02d62010-07-16 16:27:41629 if (command_id == IDC_CUT || command_id == IDC_ZOOM_MINUS) {
[email protected]eccda9db2010-06-23 17:27:08630 // These have special child views; don't show the accelerator for them.
631 return false;
632 }
633
634 menus::Accelerator menu_accelerator;
635 if (!entry.first->GetAcceleratorAt(entry.second, &menu_accelerator))
636 return false;
637
638 *accelerator = views::Accelerator(menu_accelerator.GetKeyCode(),
639 menu_accelerator.modifiers());
640 return true;
641}
642
[email protected]428f54b2010-10-05 03:31:27643WrenchMenu::~WrenchMenu() {
644}
645
[email protected]eccda9db2010-06-23 17:27:08646void WrenchMenu::PopulateMenu(MenuItemView* parent,
647 MenuModel* model,
648 int* next_id) {
649 int index_offset = model->GetFirstItemIndex(NULL);
650 for (int i = 0, max = model->GetItemCount(); i < max; ++i) {
651 int index = i + index_offset;
652
653 MenuItemView* item =
654 AppendMenuItem(parent, model, index, model->GetTypeAt(index), next_id);
655
656 if (model->GetTypeAt(index) == MenuModel::TYPE_SUBMENU)
657 PopulateMenu(item, model->GetSubmenuModelAt(index), next_id);
658
659 if (model->GetCommandIdAt(index) == IDC_CUT) {
660 DCHECK_EQ(MenuModel::TYPE_COMMAND, model->GetTypeAt(index));
661 DCHECK_LT(i + 2, max);
662 DCHECK_EQ(IDC_COPY, model->GetCommandIdAt(index + 1));
663 DCHECK_EQ(IDC_PASTE, model->GetCommandIdAt(index + 2));
664 item->SetTitle(l10n_util::GetString(IDS_EDIT2));
665 item->AddChildView(
666 new CutCopyPasteView(this, model, index, index + 1, index + 2));
667 i += 2;
[email protected]e1a02d62010-07-16 16:27:41668 } else if (model->GetCommandIdAt(index) == IDC_ZOOM_MINUS) {
[email protected]eccda9db2010-06-23 17:27:08669 DCHECK_EQ(MenuModel::TYPE_COMMAND, model->GetTypeAt(index));
[email protected]e1a02d62010-07-16 16:27:41670 DCHECK_EQ(IDC_ZOOM_PLUS, model->GetCommandIdAt(index + 1));
[email protected]eccda9db2010-06-23 17:27:08671 DCHECK_EQ(IDC_FULLSCREEN, model->GetCommandIdAt(index + 2));
672 item->SetTitle(l10n_util::GetString(IDS_ZOOM_MENU2));
673 item->AddChildView(
674 new ZoomView(this, model, index, index + 1, index + 2));
675 i += 2;
676 }
677 }
678}
679
680MenuItemView* WrenchMenu::AppendMenuItem(MenuItemView* parent,
681 MenuModel* model,
682 int index,
683 MenuModel::ItemType menu_type,
684 int* next_id) {
685 int id = (*next_id)++;
686 SkBitmap icon;
687 std::wstring label;
688 MenuItemView::Type type;
689 switch (menu_type) {
690 case MenuModel::TYPE_COMMAND:
691 model->GetIconAt(index, &icon);
692 type = MenuItemView::NORMAL;
693 label = UTF16ToWide(model->GetLabelAt(index));
694 break;
695 case MenuModel::TYPE_CHECK:
696 type = MenuItemView::CHECKBOX;
697 label = UTF16ToWide(model->GetLabelAt(index));
698 break;
699 case MenuModel::TYPE_RADIO:
700 type = MenuItemView::RADIO;
701 label = UTF16ToWide(model->GetLabelAt(index));
702 break;
703 case MenuModel::TYPE_SEPARATOR:
704 type = MenuItemView::SEPARATOR;
705 break;
706 case MenuModel::TYPE_SUBMENU:
707 type = MenuItemView::SUBMENU;
708 label = UTF16ToWide(model->GetLabelAt(index));
709 break;
710 default:
711 NOTREACHED();
712 type = MenuItemView::NORMAL;
713 break;
714 }
715
716 id_to_entry_[id].first = model;
717 id_to_entry_[id].second = index;
718
719 MenuItemView* menu_item = parent->AppendMenuItemImpl(id, label, icon, type);
720
[email protected]d4c4f252010-09-01 19:27:46721 if (menu_item)
722 menu_item->SetVisible(model->IsVisibleAt(index));
723
[email protected]eccda9db2010-06-23 17:27:08724 if (menu_type == MenuModel::TYPE_COMMAND && model->HasIcons()) {
725 SkBitmap icon;
726 if (model->GetIconAt(index, &icon))
727 menu_item->SetIcon(icon);
728 }
729
730 return menu_item;
731}
732
733void WrenchMenu::CancelAndEvaluate(MenuModel* model, int index) {
734 selected_menu_model_ = model;
735 selected_index_ = index;
736 root_->Cancel();
737}