blob: 7af0292dea0e25fc5594e7dff561bc40b4fdf2cc [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]c5174282011-05-20 23:42:0612#include "chrome/browser/bookmarks/bookmark_model.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]c5174282011-05-20 23:42:0615#include "chrome/browser/ui/browser_window.h"
16#include "chrome/browser/ui/views/bookmarks/bookmark_menu_delegate.h"
[email protected]21f11682011-03-02 16:45:4217#include "content/browser/tab_contents/tab_contents.h"
[email protected]afd1e522011-04-27 23:29:5918#include "content/browser/user_metrics.h"
[email protected]f4c2a6f2011-03-08 18:28:0619#include "content/common/notification_observer.h"
20#include "content/common/notification_registrar.h"
21#include "content/common/notification_source.h"
22#include "content/common/notification_type.h"
[email protected]e12e3f62010-08-06 00:44:5923#include "grit/chromium_strings.h"
[email protected]eccda9db2010-06-23 17:27:0824#include "grit/generated_resources.h"
25#include "grit/theme_resources.h"
26#include "third_party/skia/include/core/SkPaint.h"
[email protected]c051a1b2011-01-21 23:30:1727#include "ui/base/l10n/l10n_util.h"
[email protected]42ce29d2011-01-20 23:19:4628#include "ui/base/resource/resource_bundle.h"
[email protected]08397d52011-02-05 01:53:3829#include "ui/gfx/canvas.h"
30#include "ui/gfx/canvas_skia.h"
31#include "ui/gfx/skia_util.h"
[email protected]eccda9db2010-06-23 17:27:0832#include "views/background.h"
33#include "views/controls/button/image_button.h"
[email protected]a00b0322010-06-25 16:21:3234#include "views/controls/button/menu_button.h"
[email protected]eccda9db2010-06-23 17:27:0835#include "views/controls/button/text_button.h"
36#include "views/controls/label.h"
37#include "views/controls/menu/menu_config.h"
38#include "views/controls/menu/menu_item_view.h"
39#include "views/controls/menu/menu_scroll_view_container.h"
40#include "views/controls/menu/submenu_view.h"
41#include "views/window/window.h"
42
[email protected]44cbd9e2011-01-14 15:49:4043using ui::MenuModel;
[email protected]eccda9db2010-06-23 17:27:0844using views::CustomButton;
45using views::ImageButton;
46using views::Label;
47using views::MenuConfig;
48using views::MenuItemView;
49using views::TextButton;
50using views::View;
51
52namespace {
53
54// Colors used for buttons.
55const SkColor kHotBorderColor = SkColorSetARGB(72, 0, 0, 0);
56const SkColor kBorderColor = SkColorSetARGB(36, 0, 0, 0);
57const SkColor kPushedBorderColor = SkColorSetARGB(72, 0, 0, 0);
58const SkColor kHotBackgroundColor = SkColorSetARGB(204, 255, 255, 255);
59const SkColor kBackgroundColor = SkColorSetARGB(102, 255, 255, 255);
60const SkColor kPushedBackgroundColor = SkColorSetARGB(13, 0, 0, 0);
61
62// Horizontal padding on the edges of the buttons.
63const int kHorizontalPadding = 6;
64
65// Subclass of ImageButton whose preferred size includes the size of the border.
66class FullscreenButton : public ImageButton {
67 public:
[email protected]65ecd5b2010-07-27 21:29:0668 explicit FullscreenButton(views::ButtonListener* listener)
69 : ImageButton(listener) { }
[email protected]eccda9db2010-06-23 17:27:0870
71 virtual gfx::Size GetPreferredSize() {
72 gfx::Size pref = ImageButton::GetPreferredSize();
73 gfx::Insets insets;
74 if (border())
75 border()->GetInsets(&insets);
76 pref.Enlarge(insets.width(), insets.height());
77 return pref;
78 }
79
80 private:
81 DISALLOW_COPY_AND_ASSIGN(FullscreenButton);
82};
83
84// Border for buttons contained in the menu. This is only used for getting the
85// insets, the actual painting is done in MenuButtonBackground.
86class MenuButtonBorder : public views::Border {
87 public:
88 MenuButtonBorder() {}
89
90 virtual void Paint(const View& view, gfx::Canvas* canvas) const {
91 // Painting of border is done in MenuButtonBackground.
92 }
93
94 virtual void GetInsets(gfx::Insets* insets) const {
[email protected]e57bfac2010-08-03 17:19:5495 insets->Set(MenuConfig::instance().item_top_margin,
[email protected]eccda9db2010-06-23 17:27:0896 kHorizontalPadding,
[email protected]e57bfac2010-08-03 17:19:5497 MenuConfig::instance().item_bottom_margin,
[email protected]eccda9db2010-06-23 17:27:0898 kHorizontalPadding);
99 }
100
101 private:
102 DISALLOW_COPY_AND_ASSIGN(MenuButtonBorder);
103};
104
105// Combination border/background for the buttons contained in the menu. The
106// painting of the border/background is done here as TextButton does not always
107// paint the border.
108class MenuButtonBackground : public views::Background {
109 public:
110 enum ButtonType {
111 LEFT_BUTTON,
112 CENTER_BUTTON,
113 RIGHT_BUTTON,
114 SINGLE_BUTTON,
115 };
116
117 explicit MenuButtonBackground(ButtonType type)
118 : type_(type),
119 left_button_(NULL),
120 right_button_(NULL) {}
121
122 // Used when the type is CENTER_BUTTON to determine if the left/right edge
123 // needs to be rendered selected.
124 void SetOtherButtons(CustomButton* left_button, CustomButton* right_button) {
[email protected]5214bdd2010-08-02 21:43:47125 if (base::i18n::IsRTL()) {
126 left_button_ = right_button;
127 right_button_ = left_button;
128 } else {
129 left_button_ = left_button;
130 right_button_ = right_button;
131 }
[email protected]eccda9db2010-06-23 17:27:08132 }
133
134 virtual void Paint(gfx::Canvas* canvas, View* view) const {
135 CustomButton::ButtonState state =
136 (view->GetClassName() == views::Label::kViewClassName) ?
137 CustomButton::BS_NORMAL : static_cast<CustomButton*>(view)->state();
138 int w = view->width();
139 int h = view->height();
[email protected]34344902010-07-07 20:26:26140 switch (TypeAdjustedForRTL()) {
[email protected]eccda9db2010-06-23 17:27:08141 case LEFT_BUTTON:
142 canvas->FillRectInt(background_color(state), 1, 1, w, h - 2);
143 canvas->FillRectInt(border_color(state), 2, 0, w, 1);
144 canvas->FillRectInt(border_color(state), 1, 1, 1, 1);
145 canvas->FillRectInt(border_color(state), 0, 2, 1, h - 4);
146 canvas->FillRectInt(border_color(state), 1, h - 2, 1, 1);
147 canvas->FillRectInt(border_color(state), 2, h - 1, w, 1);
148 break;
149
150 case CENTER_BUTTON: {
151 canvas->FillRectInt(background_color(state), 1, 1, w - 2, h - 2);
152 SkColor left_color = state != CustomButton::BS_NORMAL ?
153 border_color(state) : border_color(left_button_->state());
154 canvas->FillRectInt(left_color, 0, 0, 1, h);
155 canvas->FillRectInt(border_color(state), 1, 0, w - 2, 1);
156 canvas->FillRectInt(border_color(state), 1, h - 1, w - 2, 1);
157 SkColor right_color = state != CustomButton::BS_NORMAL ?
158 border_color(state) : border_color(right_button_->state());
159 canvas->FillRectInt(right_color, w - 1, 0, 1, h);
160 break;
161 }
162
163 case RIGHT_BUTTON:
164 canvas->FillRectInt(background_color(state), 0, 1, w - 1, h - 2);
165 canvas->FillRectInt(border_color(state), 0, 0, w - 2, 1);
166 canvas->FillRectInt(border_color(state), w - 2, 1, 1, 1);
167 canvas->FillRectInt(border_color(state), w - 1, 2, 1, h - 4);
168 canvas->FillRectInt(border_color(state), w - 2, h - 2, 1, 1);
169 canvas->FillRectInt(border_color(state), 0, h - 1, w - 2, 1);
170 break;
171
172 case SINGLE_BUTTON:
173 canvas->FillRectInt(background_color(state), 1, 1, w - 2, h - 2);
174 canvas->FillRectInt(border_color(state), 2, 0, w - 4, 1);
175 canvas->FillRectInt(border_color(state), 1, 1, 1, 1);
176 canvas->FillRectInt(border_color(state), 0, 2, 1, h - 4);
177 canvas->FillRectInt(border_color(state), 1, h - 2, 1, 1);
178 canvas->FillRectInt(border_color(state), 2, h - 1, w - 4, 1);
179 canvas->FillRectInt(border_color(state), w - 2, 1, 1, 1);
180 canvas->FillRectInt(border_color(state), w - 1, 2, 1, h - 4);
181 canvas->FillRectInt(border_color(state), w - 2, h - 2, 1, 1);
182 break;
183
184 default:
185 NOTREACHED();
186 break;
187 }
188 }
189
190 private:
191 static SkColor border_color(CustomButton::ButtonState state) {
192 switch (state) {
193 case CustomButton::BS_HOT: return kHotBorderColor;
194 case CustomButton::BS_PUSHED: return kPushedBorderColor;
195 default: return kBorderColor;
196 }
197 }
198
199 static SkColor background_color(CustomButton::ButtonState state) {
200 switch (state) {
201 case CustomButton::BS_HOT: return kHotBackgroundColor;
202 case CustomButton::BS_PUSHED: return kPushedBackgroundColor;
203 default: return kBackgroundColor;
204 }
205 }
206
[email protected]34344902010-07-07 20:26:26207 ButtonType TypeAdjustedForRTL() const {
208 if (!base::i18n::IsRTL())
209 return type_;
210
211 switch (type_) {
212 case LEFT_BUTTON: return RIGHT_BUTTON;
213 case RIGHT_BUTTON: return LEFT_BUTTON;
214 default: break;
215 }
216 return type_;
217 }
218
[email protected]eccda9db2010-06-23 17:27:08219 const ButtonType type_;
220
221 // See description above setter for details.
222 CustomButton* left_button_;
223 CustomButton* right_button_;
224
225 DISALLOW_COPY_AND_ASSIGN(MenuButtonBackground);
226};
227
228// A View subclass that forces SchedulePaint to paint all. Normally when the
229// mouse enters/exits a button the buttons invokes SchedulePaint. As part of the
230// button border (MenuButtonBackground) is rendered by the button to the
231// left/right of it SchedulePaint on the the button may not be enough, so this
232// forces a paint all.
233class ScheduleAllView : public views::View {
234 public:
235 ScheduleAllView() {}
236
[email protected]64831952011-06-03 21:19:32237#if !defined(COMPOSITOR_2)
[email protected]0a119c92011-02-23 17:50:56238 virtual void SchedulePaintInRect(const gfx::Rect& r) {
[email protected]eccda9db2010-06-23 17:27:08239 if (!IsVisible())
240 return;
241
[email protected]20838602011-02-09 04:50:21242 if (parent())
[email protected]0a119c92011-02-23 17:50:56243 parent()->SchedulePaintInRect(GetMirroredBounds());
[email protected]eccda9db2010-06-23 17:27:08244 }
[email protected]64831952011-06-03 21:19:32245#else
246 virtual void SchedulePaintInRect(const gfx::Rect& r) {
247 View::SchedulePaintInRect(gfx::Rect(0, 0, width(), height()));
248 }
249
250 virtual void SchedulePaintInternal(const gfx::Rect& r) {
251 View::SchedulePaintInternal(gfx::Rect(0, 0, width(), height()));
252 }
253#endif
[email protected]eccda9db2010-06-23 17:27:08254
255 private:
256 DISALLOW_COPY_AND_ASSIGN(ScheduleAllView);
257};
258
[email protected]6f1d18e2011-01-10 20:57:00259string16 GetAccessibleNameForWrenchMenuItem(
[email protected]2cc800b2010-10-01 18:27:34260 MenuModel* model, int item_index, int accessible_string_id) {
[email protected]6f1d18e2011-01-10 20:57:00261 string16 accessible_name = l10n_util::GetStringUTF16(accessible_string_id);
262 string16 accelerator_text;
[email protected]2cc800b2010-10-01 18:27:34263
[email protected]44cbd9e2011-01-14 15:49:40264 ui::Accelerator menu_accelerator;
[email protected]2cc800b2010-10-01 18:27:34265 if (model->GetAcceleratorAt(item_index, &menu_accelerator)) {
266 accelerator_text =
[email protected]3a37ad412011-05-20 14:46:05267 views::Accelerator(menu_accelerator.key_code(),
[email protected]2cc800b2010-10-01 18:27:34268 menu_accelerator.modifiers()).GetShortcutText();
269 }
270
271 return MenuItemView::GetAccessibleNameForMenuItem(
272 accessible_name, accelerator_text);
[email protected]eccda9db2010-06-23 17:27:08273}
274
[email protected]2cc800b2010-10-01 18:27:34275// WrenchMenuView is a view that can contain text buttons.
276class WrenchMenuView : public ScheduleAllView, public views::ButtonListener {
277 public:
278 WrenchMenuView(WrenchMenu* menu, MenuModel* menu_model)
279 : menu_(menu), menu_model_(menu_model) { }
280
281 TextButton* CreateAndConfigureButton(int string_id,
282 MenuButtonBackground::ButtonType type,
283 int index,
284 MenuButtonBackground** background) {
285 return CreateButtonWithAccName(
286 string_id, type, index, background, string_id);
287 }
288
289 TextButton* CreateButtonWithAccName(int string_id,
290 MenuButtonBackground::ButtonType type,
291 int index,
292 MenuButtonBackground** background,
293 int acc_string_id) {
294 TextButton* button =
[email protected]7c1b7b42011-01-06 21:39:37295 new TextButton(this, UTF16ToWide(l10n_util::GetStringUTF16(string_id)));
[email protected]001f08a2011-01-14 18:34:56296 button->SetAccessibleName(
297 GetAccessibleNameForWrenchMenuItem(menu_model_, index, acc_string_id));
[email protected]4d81c242011-06-01 17:59:47298 button->set_focusable(true);
[email protected]2cc800b2010-10-01 18:27:34299 button->set_request_focus_on_press(false);
300 button->set_tag(index);
301 button->SetEnabled(menu_model_->IsEnabledAt(index));
302 button->set_prefix_type(TextButton::PREFIX_HIDE);
303 MenuButtonBackground* bg = new MenuButtonBackground(type);
304 button->set_background(bg);
305 button->SetEnabledColor(MenuConfig::instance().text_color);
306 if (background)
307 *background = bg;
308 button->set_border(new MenuButtonBorder());
309 button->set_alignment(TextButton::ALIGN_CENTER);
[email protected]2cc800b2010-10-01 18:27:34310 button->SetFont(views::MenuConfig::instance().font);
311 button->ClearMaxTextSize();
312 AddChildView(button);
313 return button;
314 }
315
316 protected:
317 // Hosting WrenchMenu.
318 WrenchMenu* menu_;
319
320 // The menu model containing the increment/decrement/reset items.
321 MenuModel* menu_model_;
322
323 private:
324 DISALLOW_COPY_AND_ASSIGN(WrenchMenuView);
325};
326
[email protected]eccda9db2010-06-23 17:27:08327} // namespace
328
329// CutCopyPasteView ------------------------------------------------------------
330
331// CutCopyPasteView is the view containing the cut/copy/paste buttons.
[email protected]2cc800b2010-10-01 18:27:34332class WrenchMenu::CutCopyPasteView : public WrenchMenuView {
[email protected]eccda9db2010-06-23 17:27:08333 public:
334 CutCopyPasteView(WrenchMenu* menu,
335 MenuModel* menu_model,
336 int cut_index,
337 int copy_index,
338 int paste_index)
[email protected]2cc800b2010-10-01 18:27:34339 : WrenchMenuView(menu, menu_model) {
[email protected]eccda9db2010-06-23 17:27:08340 TextButton* cut = CreateAndConfigureButton(
[email protected]2cc800b2010-10-01 18:27:34341 IDS_CUT, MenuButtonBackground::LEFT_BUTTON, cut_index, NULL);
[email protected]eccda9db2010-06-23 17:27:08342
343 MenuButtonBackground* copy_background = NULL;
344 CreateAndConfigureButton(
[email protected]2cc800b2010-10-01 18:27:34345 IDS_COPY, MenuButtonBackground::CENTER_BUTTON, copy_index,
346 &copy_background);
[email protected]eccda9db2010-06-23 17:27:08347
348 TextButton* paste = CreateAndConfigureButton(
[email protected]2cc800b2010-10-01 18:27:34349 IDS_PASTE, MenuButtonBackground::RIGHT_BUTTON, paste_index, NULL);
[email protected]eccda9db2010-06-23 17:27:08350
351 copy_background->SetOtherButtons(cut, paste);
352 }
353
354 gfx::Size GetPreferredSize() {
355 // Returned height doesn't matter as MenuItemView forces everything to the
356 // height of the menuitemview.
[email protected]20838602011-02-09 04:50:21357 return gfx::Size(GetMaxChildViewPreferredWidth() * child_count(), 0);
[email protected]eccda9db2010-06-23 17:27:08358 }
359
360 void Layout() {
361 // All buttons are given the same width.
362 int width = GetMaxChildViewPreferredWidth();
[email protected]20838602011-02-09 04:50:21363 for (int i = 0; i < child_count(); ++i)
[email protected]eccda9db2010-06-23 17:27:08364 GetChildViewAt(i)->SetBounds(i * width, 0, width, height());
365 }
366
367 // ButtonListener
368 virtual void ButtonPressed(views::Button* sender, const views::Event& event) {
369 menu_->CancelAndEvaluate(menu_model_, sender->tag());
370 }
371
372 private:
373 // Returns the max preferred width of all the children.
374 int GetMaxChildViewPreferredWidth() {
375 int width = 0;
[email protected]20838602011-02-09 04:50:21376 for (int i = 0; i < child_count(); ++i)
[email protected]eccda9db2010-06-23 17:27:08377 width = std::max(width, GetChildViewAt(i)->GetPreferredSize().width());
378 return width;
379 }
380
[email protected]eccda9db2010-06-23 17:27:08381 DISALLOW_COPY_AND_ASSIGN(CutCopyPasteView);
382};
383
384// ZoomView --------------------------------------------------------------------
385
386// Padding between the increment buttons and the reset button.
387static const int kZoomPadding = 6;
388
389// ZoomView contains the various zoom controls: two buttons to increase/decrease
390// the zoom, a label showing the current zoom percent, and a button to go
391// full-screen.
[email protected]2cc800b2010-10-01 18:27:34392class WrenchMenu::ZoomView : public WrenchMenuView,
[email protected]eccda9db2010-06-23 17:27:08393 public NotificationObserver {
394 public:
395 ZoomView(WrenchMenu* menu,
396 MenuModel* menu_model,
[email protected]eccda9db2010-06-23 17:27:08397 int decrement_index,
[email protected]e1a02d62010-07-16 16:27:41398 int increment_index,
[email protected]eccda9db2010-06-23 17:27:08399 int fullscreen_index)
[email protected]2cc800b2010-10-01 18:27:34400 : WrenchMenuView(menu, menu_model),
[email protected]eccda9db2010-06-23 17:27:08401 fullscreen_index_(fullscreen_index),
402 increment_button_(NULL),
403 zoom_label_(NULL),
404 decrement_button_(NULL),
405 fullscreen_button_(NULL),
406 zoom_label_width_(0) {
[email protected]2cc800b2010-10-01 18:27:34407 decrement_button_ = CreateButtonWithAccName(
408 IDS_ZOOM_MINUS2, MenuButtonBackground::LEFT_BUTTON, decrement_index,
409 NULL, IDS_ACCNAME_ZOOM_MINUS2);
[email protected]eccda9db2010-06-23 17:27:08410
[email protected]7c1b7b42011-01-06 21:39:37411 zoom_label_ = new Label(
412 UTF16ToWide(l10n_util::GetStringFUTF16Int(IDS_ZOOM_PERCENT, 100)));
[email protected]eccda9db2010-06-23 17:27:08413 zoom_label_->SetColor(MenuConfig::instance().text_color);
414 zoom_label_->SetHorizontalAlignment(Label::ALIGN_RIGHT);
415 MenuButtonBackground* center_bg =
416 new MenuButtonBackground(MenuButtonBackground::CENTER_BUTTON);
417 zoom_label_->set_background(center_bg);
418 zoom_label_->set_border(new MenuButtonBorder());
419 zoom_label_->SetFont(MenuConfig::instance().font);
420 AddChildView(zoom_label_);
[email protected]84e4f8c2010-07-15 02:18:32421 zoom_label_width_ = MaxWidthForZoomLabel();
[email protected]eccda9db2010-06-23 17:27:08422
[email protected]2cc800b2010-10-01 18:27:34423 increment_button_ = CreateButtonWithAccName(
424 IDS_ZOOM_PLUS2, MenuButtonBackground::RIGHT_BUTTON, increment_index,
425 NULL, IDS_ACCNAME_ZOOM_PLUS2);
[email protected]eccda9db2010-06-23 17:27:08426
[email protected]5214bdd2010-08-02 21:43:47427 center_bg->SetOtherButtons(decrement_button_, increment_button_);
[email protected]eccda9db2010-06-23 17:27:08428
429 fullscreen_button_ = new FullscreenButton(this);
430 fullscreen_button_->SetImage(
431 ImageButton::BS_NORMAL,
432 ResourceBundle::GetSharedInstance().GetBitmapNamed(
433 IDR_FULLSCREEN_MENU_BUTTON));
[email protected]4d81c242011-06-01 17:59:47434 fullscreen_button_->set_focusable(true);
[email protected]eccda9db2010-06-23 17:27:08435 fullscreen_button_->set_request_focus_on_press(false);
436 fullscreen_button_->set_tag(fullscreen_index);
437 fullscreen_button_->SetImageAlignment(
438 ImageButton::ALIGN_CENTER, ImageButton::ALIGN_MIDDLE);
439 fullscreen_button_->set_border(views::Border::CreateEmptyBorder(
440 0, kHorizontalPadding, 0, kHorizontalPadding));
441 fullscreen_button_->set_background(
442 new MenuButtonBackground(MenuButtonBackground::SINGLE_BUTTON));
[email protected]001f08a2011-01-14 18:34:56443 fullscreen_button_->SetAccessibleName(
[email protected]2cc800b2010-10-01 18:27:34444 GetAccessibleNameForWrenchMenuItem(
[email protected]001f08a2011-01-14 18:34:56445 menu_model, fullscreen_index, IDS_ACCNAME_FULLSCREEN));
[email protected]eccda9db2010-06-23 17:27:08446 AddChildView(fullscreen_button_);
447
448 UpdateZoomControls();
449
[email protected]89805c12011-05-23 23:53:00450 registrar_.Add(
451 this, NotificationType::ZOOM_LEVEL_CHANGED,
452 Source<HostZoomMap>(menu->browser_->profile()->GetHostZoomMap()));
[email protected]eccda9db2010-06-23 17:27:08453 }
454
455 gfx::Size GetPreferredSize() {
456 // The increment/decrement button are forced to the same width.
457 int button_width = std::max(increment_button_->GetPreferredSize().width(),
458 decrement_button_->GetPreferredSize().width());
459 int fullscreen_width = fullscreen_button_->GetPreferredSize().width();
460 // Returned height doesn't matter as MenuItemView forces everything to the
461 // height of the menuitemview.
462 return gfx::Size(button_width + zoom_label_width_ + button_width +
463 kZoomPadding + fullscreen_width, 0);
464 }
465
466 void Layout() {
467 int x = 0;
468 int button_width = std::max(increment_button_->GetPreferredSize().width(),
469 decrement_button_->GetPreferredSize().width());
470 gfx::Rect bounds(0, 0, button_width, height());
471
[email protected]97a31142011-02-08 00:09:16472 decrement_button_->SetBoundsRect(bounds);
[email protected]eccda9db2010-06-23 17:27:08473
474 x += bounds.width();
475 bounds.set_x(x);
476 bounds.set_width(zoom_label_width_);
[email protected]97a31142011-02-08 00:09:16477 zoom_label_->SetBoundsRect(bounds);
[email protected]eccda9db2010-06-23 17:27:08478
479 x += bounds.width();
480 bounds.set_x(x);
481 bounds.set_width(button_width);
[email protected]97a31142011-02-08 00:09:16482 increment_button_->SetBoundsRect(bounds);
[email protected]eccda9db2010-06-23 17:27:08483
484 x += bounds.width() + kZoomPadding;
485 bounds.set_x(x);
486 bounds.set_width(fullscreen_button_->GetPreferredSize().width());
[email protected]97a31142011-02-08 00:09:16487 fullscreen_button_->SetBoundsRect(bounds);
[email protected]eccda9db2010-06-23 17:27:08488 }
489
490 // ButtonListener:
491 virtual void ButtonPressed(views::Button* sender, const views::Event& event) {
492 if (sender->tag() == fullscreen_index_) {
493 menu_->CancelAndEvaluate(menu_model_, sender->tag());
494 } else {
495 // Zoom buttons don't close the menu.
496 menu_model_->ActivatedAt(sender->tag());
497 }
498 }
499
500 // NotificationObserver:
501 virtual void Observe(NotificationType type,
502 const NotificationSource& source,
503 const NotificationDetails& details) {
504 DCHECK_EQ(NotificationType::ZOOM_LEVEL_CHANGED, type.value);
505 UpdateZoomControls();
506 }
507
508 private:
509 void UpdateZoomControls() {
[email protected]b75b8292010-10-01 07:28:25510 bool enable_increment = false;
511 bool enable_decrement = false;
512 TabContents* selected_tab = menu_->browser_->GetSelectedTabContents();
513 int zoom = 100;
514 if (selected_tab)
515 zoom = selected_tab->GetZoomPercent(&enable_increment, &enable_decrement);
[email protected]eccda9db2010-06-23 17:27:08516 increment_button_->SetEnabled(enable_increment);
517 decrement_button_->SetEnabled(enable_decrement);
[email protected]7c1b7b42011-01-06 21:39:37518 zoom_label_->SetText(UTF16ToWide(l10n_util::GetStringFUTF16Int(
519 IDS_ZOOM_PERCENT,
520 zoom)));
[email protected]eccda9db2010-06-23 17:27:08521
[email protected]b75b8292010-10-01 07:28:25522 zoom_label_width_ = MaxWidthForZoomLabel();
[email protected]84e4f8c2010-07-15 02:18:32523 }
524
525 // Calculates the max width the zoom string can be.
526 int MaxWidthForZoomLabel() {
527 gfx::Font font = zoom_label_->font();
528 gfx::Insets insets;
529 if (zoom_label_->border())
530 zoom_label_->border()->GetInsets(&insets);
[email protected]b75b8292010-10-01 07:28:25531
[email protected]84e4f8c2010-07-15 02:18:32532 int max_w = 0;
[email protected]b75b8292010-10-01 07:28:25533
534 TabContents* selected_tab = menu_->browser_->GetSelectedTabContents();
535 if (selected_tab) {
536 int min_percent = selected_tab->minimum_zoom_percent();
537 int max_percent = selected_tab->maximum_zoom_percent();
538
539 int step = (max_percent - min_percent) / 10;
540 for (int i = min_percent; i <= max_percent; i += step) {
[email protected]13658c42011-01-04 20:46:14541 int w = font.GetStringWidth(
542 l10n_util::GetStringFUTF16Int(IDS_ZOOM_PERCENT, i));
[email protected]b75b8292010-10-01 07:28:25543 max_w = std::max(w, max_w);
544 }
545 } else {
[email protected]13658c42011-01-04 20:46:14546 max_w = font.GetStringWidth(
547 l10n_util::GetStringFUTF16Int(IDS_ZOOM_PERCENT, 100));
[email protected]84e4f8c2010-07-15 02:18:32548 }
[email protected]b75b8292010-10-01 07:28:25549
[email protected]84e4f8c2010-07-15 02:18:32550 return max_w + insets.width();
551 }
552
[email protected]eccda9db2010-06-23 17:27:08553 // Index of the fullscreen menu item in the model.
554 const int fullscreen_index_;
555
556 NotificationRegistrar registrar_;
557
558 // Button for incrementing the zoom.
559 TextButton* increment_button_;
560
561 // Label showing zoom as a percent.
562 Label* zoom_label_;
563
564 // Button for decrementing the zoom.
565 TextButton* decrement_button_;
566
567 ImageButton* fullscreen_button_;
568
569 // Width given to |zoom_label_|. This is the width at 100%.
570 int zoom_label_width_;
571
572 DISALLOW_COPY_AND_ASSIGN(ZoomView);
573};
574
575// WrenchMenu ------------------------------------------------------------------
576
577WrenchMenu::WrenchMenu(Browser* browser)
578 : browser_(browser),
579 selected_menu_model_(NULL),
[email protected]c5174282011-05-20 23:42:06580 selected_index_(0),
581 bookmark_menu_(NULL),
582 first_bookmark_command_id_(0) {
[email protected]eccda9db2010-06-23 17:27:08583}
584
[email protected]44cbd9e2011-01-14 15:49:40585void WrenchMenu::Init(ui::MenuModel* model) {
[email protected]eccda9db2010-06-23 17:27:08586 DCHECK(!root_.get());
587 root_.reset(new MenuItemView(this));
588 root_->set_has_icons(true); // We have checks, radios and icons, set this
589 // so we get the taller menu style.
590 int next_id = 1;
591 PopulateMenu(root_.get(), model, &next_id);
[email protected]c5174282011-05-20 23:42:06592 first_bookmark_command_id_ = next_id + 1;
[email protected]eccda9db2010-06-23 17:27:08593}
594
[email protected]a00b0322010-06-25 16:21:32595void WrenchMenu::RunMenu(views::MenuButton* host) {
[email protected]428f54b2010-10-05 03:31:27596 // Up the ref count while the menu is displaying. This way if the window is
597 // deleted while we're running we won't prematurely delete the menu.
598 // TODO(sky): fix this, the menu should really take ownership of the menu
599 // (57890).
600 scoped_refptr<WrenchMenu> dont_delete_while_running(this);
[email protected]eccda9db2010-06-23 17:27:08601 gfx::Point screen_loc;
602 views::View::ConvertPointToScreen(host, &screen_loc);
[email protected]e78f12a22010-07-28 22:41:05603 gfx::Rect bounds(screen_loc, host->size());
[email protected]366069f2011-01-11 09:36:12604 UserMetrics::RecordAction(UserMetricsAction("ShowAppMenu"));
[email protected]a00b0322010-06-25 16:21:32605 root_->RunMenuAt(host->GetWindow()->GetNativeWindow(), host, bounds,
[email protected]77745c32011-06-07 22:15:35606 MenuItemView::TOPRIGHT, true);
[email protected]c5174282011-05-20 23:42:06607 if (bookmark_menu_delegate_.get()) {
608 BookmarkModel* model = browser_->profile()->GetBookmarkModel();
609 if (model)
610 model->RemoveObserver(this);
611 }
[email protected]eccda9db2010-06-23 17:27:08612 if (selected_menu_model_)
613 selected_menu_model_->ActivatedAt(selected_index_);
614}
615
[email protected]c5174282011-05-20 23:42:06616std::wstring WrenchMenu::GetTooltipText(int id,
617 const gfx::Point& p) {
618 return is_bookmark_command(id) ?
619 bookmark_menu_delegate_->GetTooltipText(id, p) : std::wstring();
620}
621
622bool WrenchMenu::IsTriggerableEvent(views::MenuItemView* menu,
623 const views::MouseEvent& e) {
624 return is_bookmark_command(menu->GetCommand()) ?
625 bookmark_menu_delegate_->IsTriggerableEvent(menu, e) :
626 MenuDelegate::IsTriggerableEvent(menu, e);
627}
628
629bool WrenchMenu::GetDropFormats(
630 MenuItemView* menu,
631 int* formats,
632 std::set<ui::OSExchangeData::CustomFormat>* custom_formats) {
633 CreateBookmarkMenu();
634 return bookmark_menu_delegate_.get() &&
635 bookmark_menu_delegate_->GetDropFormats(menu, formats, custom_formats);
636}
637
638bool WrenchMenu::AreDropTypesRequired(MenuItemView* menu) {
639 CreateBookmarkMenu();
640 return bookmark_menu_delegate_.get() &&
641 bookmark_menu_delegate_->AreDropTypesRequired(menu);
642}
643
644bool WrenchMenu::CanDrop(MenuItemView* menu,
645 const ui::OSExchangeData& data) {
646 CreateBookmarkMenu();
647 return bookmark_menu_delegate_.get() &&
648 bookmark_menu_delegate_->CanDrop(menu, data);
649}
650
651int WrenchMenu::GetDropOperation(
652 MenuItemView* item,
653 const views::DropTargetEvent& event,
654 DropPosition* position) {
655 return is_bookmark_command(item->GetCommand()) ?
656 bookmark_menu_delegate_->GetDropOperation(item, event, position) :
657 ui::DragDropTypes::DRAG_NONE;
658}
659
660int WrenchMenu::OnPerformDrop(MenuItemView* menu,
661 DropPosition position,
662 const views::DropTargetEvent& event) {
663 if (!is_bookmark_command(menu->GetCommand()))
664 return ui::DragDropTypes::DRAG_NONE;
665
666 int result = bookmark_menu_delegate_->OnPerformDrop(menu, position, event);
667 return result;
668}
669
670bool WrenchMenu::ShowContextMenu(MenuItemView* source,
671 int id,
672 const gfx::Point& p,
673 bool is_mouse_gesture) {
674 return is_bookmark_command(id) ?
675 bookmark_menu_delegate_->ShowContextMenu(source, id, p,
676 is_mouse_gesture) :
677 false;
678}
679
680bool WrenchMenu::CanDrag(MenuItemView* menu) {
681 return is_bookmark_command(menu->GetCommand()) ?
682 bookmark_menu_delegate_->CanDrag(menu) : false;
683}
684
685void WrenchMenu::WriteDragData(MenuItemView* sender,
686 ui::OSExchangeData* data) {
687 DCHECK(is_bookmark_command(sender->GetCommand()));
688 return bookmark_menu_delegate_->WriteDragData(sender, data);
689}
690
691int WrenchMenu::GetDragOperations(MenuItemView* sender) {
692 return is_bookmark_command(sender->GetCommand()) ?
693 bookmark_menu_delegate_->GetDragOperations(sender) :
694 MenuDelegate::GetDragOperations(sender);
695}
696
697int WrenchMenu::GetMaxWidthForMenu(MenuItemView* menu) {
698 return is_bookmark_command(menu->GetCommand()) ?
699 bookmark_menu_delegate_->GetMaxWidthForMenu(menu) :
700 MenuDelegate::GetMaxWidthForMenu(menu);
701}
702
[email protected]eccda9db2010-06-23 17:27:08703bool WrenchMenu::IsItemChecked(int id) const {
[email protected]c7b6a222011-05-27 16:24:43704 if (is_bookmark_command(id))
[email protected]c5174282011-05-20 23:42:06705 return false;
706
[email protected]eccda9db2010-06-23 17:27:08707 const Entry& entry = id_to_entry_.find(id)->second;
708 return entry.first->IsItemCheckedAt(entry.second);
709}
710
711bool WrenchMenu::IsCommandEnabled(int id) const {
[email protected]c5174282011-05-20 23:42:06712 if (is_bookmark_command(id))
713 return true;
714
[email protected]eccda9db2010-06-23 17:27:08715 if (id == 0)
716 return false; // The root item.
717
718 const Entry& entry = id_to_entry_.find(id)->second;
719 int command_id = entry.first->GetCommandIdAt(entry.second);
720 // The items representing the cut (cut/copy/paste) and zoom menu
721 // (increment/decrement/reset) are always enabled. The child views of these
722 // items enabled state updates appropriately.
[email protected]e1a02d62010-07-16 16:27:41723 return command_id == IDC_CUT || command_id == IDC_ZOOM_MINUS ||
[email protected]eccda9db2010-06-23 17:27:08724 entry.first->IsEnabledAt(entry.second);
725}
726
[email protected]c5174282011-05-20 23:42:06727void WrenchMenu::ExecuteCommand(int id, int mouse_event_flags) {
728 if (is_bookmark_command(id)) {
729 bookmark_menu_delegate_->ExecuteCommand(id, mouse_event_flags);
730 return;
731 }
732
733 // Not a bookmark
[email protected]eccda9db2010-06-23 17:27:08734 const Entry& entry = id_to_entry_.find(id)->second;
[email protected]60555ec2010-07-02 20:18:25735 int command_id = entry.first->GetCommandIdAt(entry.second);
736
[email protected]e1a02d62010-07-16 16:27:41737 if (command_id == IDC_CUT || command_id == IDC_ZOOM_MINUS) {
[email protected]60555ec2010-07-02 20:18:25738 // These items are represented by child views. If ExecuteCommand is invoked
739 // it means the user clicked on the area around the buttons and we should
740 // not do anyting.
741 return;
742 }
743
[email protected]eccda9db2010-06-23 17:27:08744 return entry.first->ActivatedAt(entry.second);
745}
746
747bool WrenchMenu::GetAccelerator(int id, views::Accelerator* accelerator) {
[email protected]c5174282011-05-20 23:42:06748 if (is_bookmark_command(id))
749 return false;
750
[email protected]eccda9db2010-06-23 17:27:08751 const Entry& entry = id_to_entry_.find(id)->second;
752 int command_id = entry.first->GetCommandIdAt(entry.second);
[email protected]e1a02d62010-07-16 16:27:41753 if (command_id == IDC_CUT || command_id == IDC_ZOOM_MINUS) {
[email protected]eccda9db2010-06-23 17:27:08754 // These have special child views; don't show the accelerator for them.
755 return false;
756 }
757
[email protected]44cbd9e2011-01-14 15:49:40758 ui::Accelerator menu_accelerator;
[email protected]eccda9db2010-06-23 17:27:08759 if (!entry.first->GetAcceleratorAt(entry.second, &menu_accelerator))
760 return false;
761
[email protected]3a37ad412011-05-20 14:46:05762 *accelerator = views::Accelerator(menu_accelerator.key_code(),
[email protected]eccda9db2010-06-23 17:27:08763 menu_accelerator.modifiers());
764 return true;
765}
766
[email protected]c5174282011-05-20 23:42:06767void WrenchMenu::WillShowMenu(MenuItemView* menu) {
768 if (menu == bookmark_menu_)
769 CreateBookmarkMenu();
770}
771
772void WrenchMenu::BookmarkModelChanged() {
[email protected]c7b6a222011-05-27 16:24:43773 DCHECK(bookmark_menu_delegate_.get());
774 if (!bookmark_menu_delegate_->is_mutating_model())
775 root_->Cancel();
[email protected]c5174282011-05-20 23:42:06776}
777
[email protected]428f54b2010-10-05 03:31:27778WrenchMenu::~WrenchMenu() {
779}
780
[email protected]eccda9db2010-06-23 17:27:08781void WrenchMenu::PopulateMenu(MenuItemView* parent,
782 MenuModel* model,
783 int* next_id) {
784 int index_offset = model->GetFirstItemIndex(NULL);
785 for (int i = 0, max = model->GetItemCount(); i < max; ++i) {
786 int index = i + index_offset;
787
788 MenuItemView* item =
789 AppendMenuItem(parent, model, index, model->GetTypeAt(index), next_id);
790
791 if (model->GetTypeAt(index) == MenuModel::TYPE_SUBMENU)
792 PopulateMenu(item, model->GetSubmenuModelAt(index), next_id);
793
[email protected]c5174282011-05-20 23:42:06794 switch (model->GetCommandIdAt(index)) {
795 case IDC_CUT:
796 DCHECK_EQ(MenuModel::TYPE_COMMAND, model->GetTypeAt(index));
797 DCHECK_LT(i + 2, max);
798 DCHECK_EQ(IDC_COPY, model->GetCommandIdAt(index + 1));
799 DCHECK_EQ(IDC_PASTE, model->GetCommandIdAt(index + 2));
800 item->SetTitle(UTF16ToWide(l10n_util::GetStringUTF16(IDS_EDIT2)));
801 item->AddChildView(
802 new CutCopyPasteView(this, model, index, index + 1, index + 2));
803 i += 2;
804 break;
805
806 case IDC_ZOOM_MINUS:
807 DCHECK_EQ(MenuModel::TYPE_COMMAND, model->GetTypeAt(index));
808 DCHECK_EQ(IDC_ZOOM_PLUS, model->GetCommandIdAt(index + 1));
809 DCHECK_EQ(IDC_FULLSCREEN, model->GetCommandIdAt(index + 2));
810 item->SetTitle(UTF16ToWide(l10n_util::GetStringUTF16(IDS_ZOOM_MENU2)));
811 item->AddChildView(
812 new ZoomView(this, model, index, index + 1, index + 2));
813 i += 2;
814 break;
815
816 case IDC_BOOKMARKS_MENU:
817 DCHECK(!bookmark_menu_);
818 bookmark_menu_ = item;
819 break;
820
821 default:
822 break;
[email protected]eccda9db2010-06-23 17:27:08823 }
824 }
825}
826
827MenuItemView* WrenchMenu::AppendMenuItem(MenuItemView* parent,
828 MenuModel* model,
829 int index,
830 MenuModel::ItemType menu_type,
831 int* next_id) {
832 int id = (*next_id)++;
[email protected]eccda9db2010-06-23 17:27:08833
834 id_to_entry_[id].first = model;
835 id_to_entry_[id].second = index;
836
[email protected]74434942010-12-05 04:00:59837 MenuItemView* menu_item = parent->AppendMenuItemFromModel(model, index, id);
[email protected]eccda9db2010-06-23 17:27:08838
[email protected]d4c4f252010-09-01 19:27:46839 if (menu_item)
840 menu_item->SetVisible(model->IsVisibleAt(index));
841
[email protected]eccda9db2010-06-23 17:27:08842 if (menu_type == MenuModel::TYPE_COMMAND && model->HasIcons()) {
843 SkBitmap icon;
844 if (model->GetIconAt(index, &icon))
845 menu_item->SetIcon(icon);
846 }
847
848 return menu_item;
849}
850
851void WrenchMenu::CancelAndEvaluate(MenuModel* model, int index) {
852 selected_menu_model_ = model;
853 selected_index_ = index;
854 root_->Cancel();
855}
[email protected]c5174282011-05-20 23:42:06856
857void WrenchMenu::CreateBookmarkMenu() {
858 if (bookmark_menu_delegate_.get())
859 return; // Already created the menu.
860
861 BookmarkModel* model = browser_->profile()->GetBookmarkModel();
862 if (!model->IsLoaded())
863 return;
864
865 model->AddObserver(this);
866 bookmark_menu_delegate_.reset(
867 new BookmarkMenuDelegate(browser_->profile(),
868 NULL,
869 browser_->window()->GetNativeHandle(),
870 first_bookmark_command_id_));
871 bookmark_menu_delegate_->Init(
872 this, bookmark_menu_, model->GetBookmarkBarNode(), 0,
873 BookmarkMenuDelegate::SHOW_OTHER_FOLDER);
874}