blob: c08882fb4b0a4b26728adfb49912e5f3ba8f947a [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]432115822011-07-10 15:52:2719#include "content/common/content_notification_types.h"
[email protected]f4c2a6f2011-03-08 18:28:0620#include "content/common/notification_observer.h"
21#include "content/common/notification_registrar.h"
22#include "content/common/notification_source.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"
[email protected]2fdd00a2011-06-13 21:56:2641#include "views/widget/widget.h"
[email protected]eccda9db2010-06-23 17:27:0842
[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 virtual void SchedulePaintInRect(const gfx::Rect& r) {
238 View::SchedulePaintInRect(gfx::Rect(0, 0, width(), height()));
239 }
240
241 virtual void SchedulePaintInternal(const gfx::Rect& r) {
242 View::SchedulePaintInternal(gfx::Rect(0, 0, width(), height()));
243 }
[email protected]eccda9db2010-06-23 17:27:08244
245 private:
246 DISALLOW_COPY_AND_ASSIGN(ScheduleAllView);
247};
248
[email protected]6f1d18e2011-01-10 20:57:00249string16 GetAccessibleNameForWrenchMenuItem(
[email protected]2cc800b2010-10-01 18:27:34250 MenuModel* model, int item_index, int accessible_string_id) {
[email protected]6f1d18e2011-01-10 20:57:00251 string16 accessible_name = l10n_util::GetStringUTF16(accessible_string_id);
252 string16 accelerator_text;
[email protected]2cc800b2010-10-01 18:27:34253
[email protected]44cbd9e2011-01-14 15:49:40254 ui::Accelerator menu_accelerator;
[email protected]2cc800b2010-10-01 18:27:34255 if (model->GetAcceleratorAt(item_index, &menu_accelerator)) {
256 accelerator_text =
[email protected]3a37ad412011-05-20 14:46:05257 views::Accelerator(menu_accelerator.key_code(),
[email protected]2cc800b2010-10-01 18:27:34258 menu_accelerator.modifiers()).GetShortcutText();
259 }
260
261 return MenuItemView::GetAccessibleNameForMenuItem(
262 accessible_name, accelerator_text);
[email protected]eccda9db2010-06-23 17:27:08263}
264
[email protected]2cc800b2010-10-01 18:27:34265// WrenchMenuView is a view that can contain text buttons.
266class WrenchMenuView : public ScheduleAllView, public views::ButtonListener {
267 public:
268 WrenchMenuView(WrenchMenu* menu, MenuModel* menu_model)
269 : menu_(menu), menu_model_(menu_model) { }
270
271 TextButton* CreateAndConfigureButton(int string_id,
272 MenuButtonBackground::ButtonType type,
273 int index,
274 MenuButtonBackground** background) {
275 return CreateButtonWithAccName(
276 string_id, type, index, background, string_id);
277 }
278
279 TextButton* CreateButtonWithAccName(int string_id,
280 MenuButtonBackground::ButtonType type,
281 int index,
282 MenuButtonBackground** background,
283 int acc_string_id) {
284 TextButton* button =
[email protected]7c1b7b42011-01-06 21:39:37285 new TextButton(this, UTF16ToWide(l10n_util::GetStringUTF16(string_id)));
[email protected]001f08a2011-01-14 18:34:56286 button->SetAccessibleName(
287 GetAccessibleNameForWrenchMenuItem(menu_model_, index, acc_string_id));
[email protected]4d81c242011-06-01 17:59:47288 button->set_focusable(true);
[email protected]2cc800b2010-10-01 18:27:34289 button->set_request_focus_on_press(false);
290 button->set_tag(index);
291 button->SetEnabled(menu_model_->IsEnabledAt(index));
292 button->set_prefix_type(TextButton::PREFIX_HIDE);
293 MenuButtonBackground* bg = new MenuButtonBackground(type);
294 button->set_background(bg);
295 button->SetEnabledColor(MenuConfig::instance().text_color);
296 if (background)
297 *background = bg;
298 button->set_border(new MenuButtonBorder());
299 button->set_alignment(TextButton::ALIGN_CENTER);
[email protected]2cc800b2010-10-01 18:27:34300 button->SetFont(views::MenuConfig::instance().font);
301 button->ClearMaxTextSize();
302 AddChildView(button);
303 return button;
304 }
305
306 protected:
307 // Hosting WrenchMenu.
308 WrenchMenu* menu_;
309
310 // The menu model containing the increment/decrement/reset items.
311 MenuModel* menu_model_;
312
313 private:
314 DISALLOW_COPY_AND_ASSIGN(WrenchMenuView);
315};
316
[email protected]eccda9db2010-06-23 17:27:08317} // namespace
318
319// CutCopyPasteView ------------------------------------------------------------
320
321// CutCopyPasteView is the view containing the cut/copy/paste buttons.
[email protected]2cc800b2010-10-01 18:27:34322class WrenchMenu::CutCopyPasteView : public WrenchMenuView {
[email protected]eccda9db2010-06-23 17:27:08323 public:
324 CutCopyPasteView(WrenchMenu* menu,
325 MenuModel* menu_model,
326 int cut_index,
327 int copy_index,
328 int paste_index)
[email protected]2cc800b2010-10-01 18:27:34329 : WrenchMenuView(menu, menu_model) {
[email protected]eccda9db2010-06-23 17:27:08330 TextButton* cut = CreateAndConfigureButton(
[email protected]2cc800b2010-10-01 18:27:34331 IDS_CUT, MenuButtonBackground::LEFT_BUTTON, cut_index, NULL);
[email protected]eccda9db2010-06-23 17:27:08332
333 MenuButtonBackground* copy_background = NULL;
334 CreateAndConfigureButton(
[email protected]2cc800b2010-10-01 18:27:34335 IDS_COPY, MenuButtonBackground::CENTER_BUTTON, copy_index,
336 &copy_background);
[email protected]eccda9db2010-06-23 17:27:08337
338 TextButton* paste = CreateAndConfigureButton(
[email protected]2cc800b2010-10-01 18:27:34339 IDS_PASTE, MenuButtonBackground::RIGHT_BUTTON, paste_index, NULL);
[email protected]eccda9db2010-06-23 17:27:08340
341 copy_background->SetOtherButtons(cut, paste);
342 }
343
344 gfx::Size GetPreferredSize() {
345 // Returned height doesn't matter as MenuItemView forces everything to the
346 // height of the menuitemview.
[email protected]20838602011-02-09 04:50:21347 return gfx::Size(GetMaxChildViewPreferredWidth() * child_count(), 0);
[email protected]eccda9db2010-06-23 17:27:08348 }
349
350 void Layout() {
351 // All buttons are given the same width.
352 int width = GetMaxChildViewPreferredWidth();
[email protected]20838602011-02-09 04:50:21353 for (int i = 0; i < child_count(); ++i)
[email protected]eccda9db2010-06-23 17:27:08354 GetChildViewAt(i)->SetBounds(i * width, 0, width, height());
355 }
356
357 // ButtonListener
358 virtual void ButtonPressed(views::Button* sender, const views::Event& event) {
359 menu_->CancelAndEvaluate(menu_model_, sender->tag());
360 }
361
362 private:
363 // Returns the max preferred width of all the children.
364 int GetMaxChildViewPreferredWidth() {
365 int width = 0;
[email protected]20838602011-02-09 04:50:21366 for (int i = 0; i < child_count(); ++i)
[email protected]eccda9db2010-06-23 17:27:08367 width = std::max(width, GetChildViewAt(i)->GetPreferredSize().width());
368 return width;
369 }
370
[email protected]eccda9db2010-06-23 17:27:08371 DISALLOW_COPY_AND_ASSIGN(CutCopyPasteView);
372};
373
374// ZoomView --------------------------------------------------------------------
375
376// Padding between the increment buttons and the reset button.
377static const int kZoomPadding = 6;
378
379// ZoomView contains the various zoom controls: two buttons to increase/decrease
380// the zoom, a label showing the current zoom percent, and a button to go
381// full-screen.
[email protected]2cc800b2010-10-01 18:27:34382class WrenchMenu::ZoomView : public WrenchMenuView,
[email protected]eccda9db2010-06-23 17:27:08383 public NotificationObserver {
384 public:
385 ZoomView(WrenchMenu* menu,
386 MenuModel* menu_model,
[email protected]eccda9db2010-06-23 17:27:08387 int decrement_index,
[email protected]e1a02d62010-07-16 16:27:41388 int increment_index,
[email protected]eccda9db2010-06-23 17:27:08389 int fullscreen_index)
[email protected]2cc800b2010-10-01 18:27:34390 : WrenchMenuView(menu, menu_model),
[email protected]eccda9db2010-06-23 17:27:08391 fullscreen_index_(fullscreen_index),
392 increment_button_(NULL),
393 zoom_label_(NULL),
394 decrement_button_(NULL),
395 fullscreen_button_(NULL),
396 zoom_label_width_(0) {
[email protected]2cc800b2010-10-01 18:27:34397 decrement_button_ = CreateButtonWithAccName(
398 IDS_ZOOM_MINUS2, MenuButtonBackground::LEFT_BUTTON, decrement_index,
399 NULL, IDS_ACCNAME_ZOOM_MINUS2);
[email protected]eccda9db2010-06-23 17:27:08400
[email protected]7c1b7b42011-01-06 21:39:37401 zoom_label_ = new Label(
402 UTF16ToWide(l10n_util::GetStringFUTF16Int(IDS_ZOOM_PERCENT, 100)));
[email protected]eccda9db2010-06-23 17:27:08403 zoom_label_->SetColor(MenuConfig::instance().text_color);
404 zoom_label_->SetHorizontalAlignment(Label::ALIGN_RIGHT);
405 MenuButtonBackground* center_bg =
406 new MenuButtonBackground(MenuButtonBackground::CENTER_BUTTON);
407 zoom_label_->set_background(center_bg);
408 zoom_label_->set_border(new MenuButtonBorder());
409 zoom_label_->SetFont(MenuConfig::instance().font);
410 AddChildView(zoom_label_);
[email protected]84e4f8c2010-07-15 02:18:32411 zoom_label_width_ = MaxWidthForZoomLabel();
[email protected]eccda9db2010-06-23 17:27:08412
[email protected]2cc800b2010-10-01 18:27:34413 increment_button_ = CreateButtonWithAccName(
414 IDS_ZOOM_PLUS2, MenuButtonBackground::RIGHT_BUTTON, increment_index,
415 NULL, IDS_ACCNAME_ZOOM_PLUS2);
[email protected]eccda9db2010-06-23 17:27:08416
[email protected]5214bdd2010-08-02 21:43:47417 center_bg->SetOtherButtons(decrement_button_, increment_button_);
[email protected]eccda9db2010-06-23 17:27:08418
419 fullscreen_button_ = new FullscreenButton(this);
420 fullscreen_button_->SetImage(
421 ImageButton::BS_NORMAL,
422 ResourceBundle::GetSharedInstance().GetBitmapNamed(
423 IDR_FULLSCREEN_MENU_BUTTON));
[email protected]4d81c242011-06-01 17:59:47424 fullscreen_button_->set_focusable(true);
[email protected]eccda9db2010-06-23 17:27:08425 fullscreen_button_->set_request_focus_on_press(false);
426 fullscreen_button_->set_tag(fullscreen_index);
427 fullscreen_button_->SetImageAlignment(
428 ImageButton::ALIGN_CENTER, ImageButton::ALIGN_MIDDLE);
429 fullscreen_button_->set_border(views::Border::CreateEmptyBorder(
430 0, kHorizontalPadding, 0, kHorizontalPadding));
431 fullscreen_button_->set_background(
432 new MenuButtonBackground(MenuButtonBackground::SINGLE_BUTTON));
[email protected]001f08a2011-01-14 18:34:56433 fullscreen_button_->SetAccessibleName(
[email protected]2cc800b2010-10-01 18:27:34434 GetAccessibleNameForWrenchMenuItem(
[email protected]001f08a2011-01-14 18:34:56435 menu_model, fullscreen_index, IDS_ACCNAME_FULLSCREEN));
[email protected]eccda9db2010-06-23 17:27:08436 AddChildView(fullscreen_button_);
437
438 UpdateZoomControls();
439
[email protected]89805c12011-05-23 23:53:00440 registrar_.Add(
[email protected]432115822011-07-10 15:52:27441 this, content::NOTIFICATION_ZOOM_LEVEL_CHANGED,
[email protected]89805c12011-05-23 23:53:00442 Source<HostZoomMap>(menu->browser_->profile()->GetHostZoomMap()));
[email protected]eccda9db2010-06-23 17:27:08443 }
444
445 gfx::Size GetPreferredSize() {
446 // The increment/decrement button are forced to the same width.
447 int button_width = std::max(increment_button_->GetPreferredSize().width(),
448 decrement_button_->GetPreferredSize().width());
449 int fullscreen_width = fullscreen_button_->GetPreferredSize().width();
450 // Returned height doesn't matter as MenuItemView forces everything to the
451 // height of the menuitemview.
452 return gfx::Size(button_width + zoom_label_width_ + button_width +
453 kZoomPadding + fullscreen_width, 0);
454 }
455
456 void Layout() {
457 int x = 0;
458 int button_width = std::max(increment_button_->GetPreferredSize().width(),
459 decrement_button_->GetPreferredSize().width());
460 gfx::Rect bounds(0, 0, button_width, height());
461
[email protected]97a31142011-02-08 00:09:16462 decrement_button_->SetBoundsRect(bounds);
[email protected]eccda9db2010-06-23 17:27:08463
464 x += bounds.width();
465 bounds.set_x(x);
466 bounds.set_width(zoom_label_width_);
[email protected]97a31142011-02-08 00:09:16467 zoom_label_->SetBoundsRect(bounds);
[email protected]eccda9db2010-06-23 17:27:08468
469 x += bounds.width();
470 bounds.set_x(x);
471 bounds.set_width(button_width);
[email protected]97a31142011-02-08 00:09:16472 increment_button_->SetBoundsRect(bounds);
[email protected]eccda9db2010-06-23 17:27:08473
474 x += bounds.width() + kZoomPadding;
475 bounds.set_x(x);
476 bounds.set_width(fullscreen_button_->GetPreferredSize().width());
[email protected]97a31142011-02-08 00:09:16477 fullscreen_button_->SetBoundsRect(bounds);
[email protected]eccda9db2010-06-23 17:27:08478 }
479
480 // ButtonListener:
481 virtual void ButtonPressed(views::Button* sender, const views::Event& event) {
482 if (sender->tag() == fullscreen_index_) {
483 menu_->CancelAndEvaluate(menu_model_, sender->tag());
484 } else {
485 // Zoom buttons don't close the menu.
486 menu_model_->ActivatedAt(sender->tag());
487 }
488 }
489
490 // NotificationObserver:
[email protected]432115822011-07-10 15:52:27491 virtual void Observe(int type,
[email protected]eccda9db2010-06-23 17:27:08492 const NotificationSource& source,
493 const NotificationDetails& details) {
[email protected]432115822011-07-10 15:52:27494 DCHECK_EQ(content::NOTIFICATION_ZOOM_LEVEL_CHANGED, type);
[email protected]eccda9db2010-06-23 17:27:08495 UpdateZoomControls();
496 }
497
498 private:
499 void UpdateZoomControls() {
[email protected]b75b8292010-10-01 07:28:25500 bool enable_increment = false;
501 bool enable_decrement = false;
502 TabContents* selected_tab = menu_->browser_->GetSelectedTabContents();
503 int zoom = 100;
504 if (selected_tab)
505 zoom = selected_tab->GetZoomPercent(&enable_increment, &enable_decrement);
[email protected]eccda9db2010-06-23 17:27:08506 increment_button_->SetEnabled(enable_increment);
507 decrement_button_->SetEnabled(enable_decrement);
[email protected]7c1b7b42011-01-06 21:39:37508 zoom_label_->SetText(UTF16ToWide(l10n_util::GetStringFUTF16Int(
509 IDS_ZOOM_PERCENT,
510 zoom)));
[email protected]eccda9db2010-06-23 17:27:08511
[email protected]b75b8292010-10-01 07:28:25512 zoom_label_width_ = MaxWidthForZoomLabel();
[email protected]84e4f8c2010-07-15 02:18:32513 }
514
515 // Calculates the max width the zoom string can be.
516 int MaxWidthForZoomLabel() {
517 gfx::Font font = zoom_label_->font();
518 gfx::Insets insets;
519 if (zoom_label_->border())
520 zoom_label_->border()->GetInsets(&insets);
[email protected]b75b8292010-10-01 07:28:25521
[email protected]84e4f8c2010-07-15 02:18:32522 int max_w = 0;
[email protected]b75b8292010-10-01 07:28:25523
524 TabContents* selected_tab = menu_->browser_->GetSelectedTabContents();
525 if (selected_tab) {
526 int min_percent = selected_tab->minimum_zoom_percent();
527 int max_percent = selected_tab->maximum_zoom_percent();
528
529 int step = (max_percent - min_percent) / 10;
530 for (int i = min_percent; i <= max_percent; i += step) {
[email protected]13658c42011-01-04 20:46:14531 int w = font.GetStringWidth(
532 l10n_util::GetStringFUTF16Int(IDS_ZOOM_PERCENT, i));
[email protected]b75b8292010-10-01 07:28:25533 max_w = std::max(w, max_w);
534 }
535 } else {
[email protected]13658c42011-01-04 20:46:14536 max_w = font.GetStringWidth(
537 l10n_util::GetStringFUTF16Int(IDS_ZOOM_PERCENT, 100));
[email protected]84e4f8c2010-07-15 02:18:32538 }
[email protected]b75b8292010-10-01 07:28:25539
[email protected]84e4f8c2010-07-15 02:18:32540 return max_w + insets.width();
541 }
542
[email protected]eccda9db2010-06-23 17:27:08543 // Index of the fullscreen menu item in the model.
544 const int fullscreen_index_;
545
546 NotificationRegistrar registrar_;
547
548 // Button for incrementing the zoom.
549 TextButton* increment_button_;
550
551 // Label showing zoom as a percent.
552 Label* zoom_label_;
553
554 // Button for decrementing the zoom.
555 TextButton* decrement_button_;
556
557 ImageButton* fullscreen_button_;
558
559 // Width given to |zoom_label_|. This is the width at 100%.
560 int zoom_label_width_;
561
562 DISALLOW_COPY_AND_ASSIGN(ZoomView);
563};
564
565// WrenchMenu ------------------------------------------------------------------
566
567WrenchMenu::WrenchMenu(Browser* browser)
568 : browser_(browser),
569 selected_menu_model_(NULL),
[email protected]c5174282011-05-20 23:42:06570 selected_index_(0),
571 bookmark_menu_(NULL),
572 first_bookmark_command_id_(0) {
[email protected]eccda9db2010-06-23 17:27:08573}
574
[email protected]44cbd9e2011-01-14 15:49:40575void WrenchMenu::Init(ui::MenuModel* model) {
[email protected]eccda9db2010-06-23 17:27:08576 DCHECK(!root_.get());
577 root_.reset(new MenuItemView(this));
578 root_->set_has_icons(true); // We have checks, radios and icons, set this
579 // so we get the taller menu style.
580 int next_id = 1;
581 PopulateMenu(root_.get(), model, &next_id);
[email protected]c5174282011-05-20 23:42:06582 first_bookmark_command_id_ = next_id + 1;
[email protected]eccda9db2010-06-23 17:27:08583}
584
[email protected]a00b0322010-06-25 16:21:32585void WrenchMenu::RunMenu(views::MenuButton* host) {
[email protected]428f54b2010-10-05 03:31:27586 // Up the ref count while the menu is displaying. This way if the window is
587 // deleted while we're running we won't prematurely delete the menu.
588 // TODO(sky): fix this, the menu should really take ownership of the menu
589 // (57890).
590 scoped_refptr<WrenchMenu> dont_delete_while_running(this);
[email protected]eccda9db2010-06-23 17:27:08591 gfx::Point screen_loc;
592 views::View::ConvertPointToScreen(host, &screen_loc);
[email protected]e78f12a22010-07-28 22:41:05593 gfx::Rect bounds(screen_loc, host->size());
[email protected]366069f2011-01-11 09:36:12594 UserMetrics::RecordAction(UserMetricsAction("ShowAppMenu"));
[email protected]68c477d2011-06-08 17:47:43595 root_->RunMenuAt(host->GetWidget()->GetNativeWindow(), host, bounds,
[email protected]77745c32011-06-07 22:15:35596 MenuItemView::TOPRIGHT, true);
[email protected]c5174282011-05-20 23:42:06597 if (bookmark_menu_delegate_.get()) {
598 BookmarkModel* model = browser_->profile()->GetBookmarkModel();
599 if (model)
600 model->RemoveObserver(this);
601 }
[email protected]eccda9db2010-06-23 17:27:08602 if (selected_menu_model_)
603 selected_menu_model_->ActivatedAt(selected_index_);
604}
605
[email protected]c5174282011-05-20 23:42:06606std::wstring WrenchMenu::GetTooltipText(int id,
607 const gfx::Point& p) {
608 return is_bookmark_command(id) ?
609 bookmark_menu_delegate_->GetTooltipText(id, p) : std::wstring();
610}
611
612bool WrenchMenu::IsTriggerableEvent(views::MenuItemView* menu,
613 const views::MouseEvent& e) {
614 return is_bookmark_command(menu->GetCommand()) ?
615 bookmark_menu_delegate_->IsTriggerableEvent(menu, e) :
616 MenuDelegate::IsTriggerableEvent(menu, e);
617}
618
619bool WrenchMenu::GetDropFormats(
620 MenuItemView* menu,
621 int* formats,
622 std::set<ui::OSExchangeData::CustomFormat>* custom_formats) {
623 CreateBookmarkMenu();
624 return bookmark_menu_delegate_.get() &&
625 bookmark_menu_delegate_->GetDropFormats(menu, formats, custom_formats);
626}
627
628bool WrenchMenu::AreDropTypesRequired(MenuItemView* menu) {
629 CreateBookmarkMenu();
630 return bookmark_menu_delegate_.get() &&
631 bookmark_menu_delegate_->AreDropTypesRequired(menu);
632}
633
634bool WrenchMenu::CanDrop(MenuItemView* menu,
635 const ui::OSExchangeData& data) {
636 CreateBookmarkMenu();
637 return bookmark_menu_delegate_.get() &&
638 bookmark_menu_delegate_->CanDrop(menu, data);
639}
640
641int WrenchMenu::GetDropOperation(
642 MenuItemView* item,
643 const views::DropTargetEvent& event,
644 DropPosition* position) {
645 return is_bookmark_command(item->GetCommand()) ?
646 bookmark_menu_delegate_->GetDropOperation(item, event, position) :
647 ui::DragDropTypes::DRAG_NONE;
648}
649
650int WrenchMenu::OnPerformDrop(MenuItemView* menu,
651 DropPosition position,
652 const views::DropTargetEvent& event) {
653 if (!is_bookmark_command(menu->GetCommand()))
654 return ui::DragDropTypes::DRAG_NONE;
655
656 int result = bookmark_menu_delegate_->OnPerformDrop(menu, position, event);
657 return result;
658}
659
660bool WrenchMenu::ShowContextMenu(MenuItemView* source,
661 int id,
662 const gfx::Point& p,
663 bool is_mouse_gesture) {
664 return is_bookmark_command(id) ?
665 bookmark_menu_delegate_->ShowContextMenu(source, id, p,
666 is_mouse_gesture) :
667 false;
668}
669
670bool WrenchMenu::CanDrag(MenuItemView* menu) {
671 return is_bookmark_command(menu->GetCommand()) ?
672 bookmark_menu_delegate_->CanDrag(menu) : false;
673}
674
675void WrenchMenu::WriteDragData(MenuItemView* sender,
676 ui::OSExchangeData* data) {
677 DCHECK(is_bookmark_command(sender->GetCommand()));
678 return bookmark_menu_delegate_->WriteDragData(sender, data);
679}
680
681int WrenchMenu::GetDragOperations(MenuItemView* sender) {
682 return is_bookmark_command(sender->GetCommand()) ?
683 bookmark_menu_delegate_->GetDragOperations(sender) :
684 MenuDelegate::GetDragOperations(sender);
685}
686
687int WrenchMenu::GetMaxWidthForMenu(MenuItemView* menu) {
688 return is_bookmark_command(menu->GetCommand()) ?
689 bookmark_menu_delegate_->GetMaxWidthForMenu(menu) :
690 MenuDelegate::GetMaxWidthForMenu(menu);
691}
692
[email protected]eccda9db2010-06-23 17:27:08693bool WrenchMenu::IsItemChecked(int id) const {
[email protected]c7b6a222011-05-27 16:24:43694 if (is_bookmark_command(id))
[email protected]c5174282011-05-20 23:42:06695 return false;
696
[email protected]eccda9db2010-06-23 17:27:08697 const Entry& entry = id_to_entry_.find(id)->second;
698 return entry.first->IsItemCheckedAt(entry.second);
699}
700
701bool WrenchMenu::IsCommandEnabled(int id) const {
[email protected]c5174282011-05-20 23:42:06702 if (is_bookmark_command(id))
703 return true;
704
[email protected]eccda9db2010-06-23 17:27:08705 if (id == 0)
706 return false; // The root item.
707
708 const Entry& entry = id_to_entry_.find(id)->second;
709 int command_id = entry.first->GetCommandIdAt(entry.second);
710 // The items representing the cut (cut/copy/paste) and zoom menu
711 // (increment/decrement/reset) are always enabled. The child views of these
712 // items enabled state updates appropriately.
[email protected]e1a02d62010-07-16 16:27:41713 return command_id == IDC_CUT || command_id == IDC_ZOOM_MINUS ||
[email protected]eccda9db2010-06-23 17:27:08714 entry.first->IsEnabledAt(entry.second);
715}
716
[email protected]c5174282011-05-20 23:42:06717void WrenchMenu::ExecuteCommand(int id, int mouse_event_flags) {
718 if (is_bookmark_command(id)) {
719 bookmark_menu_delegate_->ExecuteCommand(id, mouse_event_flags);
720 return;
721 }
722
723 // Not a bookmark
[email protected]eccda9db2010-06-23 17:27:08724 const Entry& entry = id_to_entry_.find(id)->second;
[email protected]60555ec2010-07-02 20:18:25725 int command_id = entry.first->GetCommandIdAt(entry.second);
726
[email protected]e1a02d62010-07-16 16:27:41727 if (command_id == IDC_CUT || command_id == IDC_ZOOM_MINUS) {
[email protected]60555ec2010-07-02 20:18:25728 // These items are represented by child views. If ExecuteCommand is invoked
729 // it means the user clicked on the area around the buttons and we should
730 // not do anyting.
731 return;
732 }
733
[email protected]eccda9db2010-06-23 17:27:08734 return entry.first->ActivatedAt(entry.second);
735}
736
737bool WrenchMenu::GetAccelerator(int id, views::Accelerator* accelerator) {
[email protected]c5174282011-05-20 23:42:06738 if (is_bookmark_command(id))
739 return false;
740
[email protected]eccda9db2010-06-23 17:27:08741 const Entry& entry = id_to_entry_.find(id)->second;
742 int command_id = entry.first->GetCommandIdAt(entry.second);
[email protected]e1a02d62010-07-16 16:27:41743 if (command_id == IDC_CUT || command_id == IDC_ZOOM_MINUS) {
[email protected]eccda9db2010-06-23 17:27:08744 // These have special child views; don't show the accelerator for them.
745 return false;
746 }
747
[email protected]44cbd9e2011-01-14 15:49:40748 ui::Accelerator menu_accelerator;
[email protected]eccda9db2010-06-23 17:27:08749 if (!entry.first->GetAcceleratorAt(entry.second, &menu_accelerator))
750 return false;
751
[email protected]3a37ad412011-05-20 14:46:05752 *accelerator = views::Accelerator(menu_accelerator.key_code(),
[email protected]eccda9db2010-06-23 17:27:08753 menu_accelerator.modifiers());
754 return true;
755}
756
[email protected]c5174282011-05-20 23:42:06757void WrenchMenu::WillShowMenu(MenuItemView* menu) {
758 if (menu == bookmark_menu_)
759 CreateBookmarkMenu();
760}
761
762void WrenchMenu::BookmarkModelChanged() {
[email protected]c7b6a222011-05-27 16:24:43763 DCHECK(bookmark_menu_delegate_.get());
764 if (!bookmark_menu_delegate_->is_mutating_model())
765 root_->Cancel();
[email protected]c5174282011-05-20 23:42:06766}
767
[email protected]428f54b2010-10-05 03:31:27768WrenchMenu::~WrenchMenu() {
769}
770
[email protected]eccda9db2010-06-23 17:27:08771void WrenchMenu::PopulateMenu(MenuItemView* parent,
772 MenuModel* model,
773 int* next_id) {
774 int index_offset = model->GetFirstItemIndex(NULL);
775 for (int i = 0, max = model->GetItemCount(); i < max; ++i) {
776 int index = i + index_offset;
777
778 MenuItemView* item =
779 AppendMenuItem(parent, model, index, model->GetTypeAt(index), next_id);
780
781 if (model->GetTypeAt(index) == MenuModel::TYPE_SUBMENU)
782 PopulateMenu(item, model->GetSubmenuModelAt(index), next_id);
783
[email protected]c5174282011-05-20 23:42:06784 switch (model->GetCommandIdAt(index)) {
785 case IDC_CUT:
786 DCHECK_EQ(MenuModel::TYPE_COMMAND, model->GetTypeAt(index));
787 DCHECK_LT(i + 2, max);
788 DCHECK_EQ(IDC_COPY, model->GetCommandIdAt(index + 1));
789 DCHECK_EQ(IDC_PASTE, model->GetCommandIdAt(index + 2));
790 item->SetTitle(UTF16ToWide(l10n_util::GetStringUTF16(IDS_EDIT2)));
791 item->AddChildView(
792 new CutCopyPasteView(this, model, index, index + 1, index + 2));
793 i += 2;
794 break;
795
796 case IDC_ZOOM_MINUS:
797 DCHECK_EQ(MenuModel::TYPE_COMMAND, model->GetTypeAt(index));
798 DCHECK_EQ(IDC_ZOOM_PLUS, model->GetCommandIdAt(index + 1));
799 DCHECK_EQ(IDC_FULLSCREEN, model->GetCommandIdAt(index + 2));
800 item->SetTitle(UTF16ToWide(l10n_util::GetStringUTF16(IDS_ZOOM_MENU2)));
801 item->AddChildView(
802 new ZoomView(this, model, index, index + 1, index + 2));
803 i += 2;
804 break;
805
806 case IDC_BOOKMARKS_MENU:
807 DCHECK(!bookmark_menu_);
808 bookmark_menu_ = item;
809 break;
810
811 default:
812 break;
[email protected]eccda9db2010-06-23 17:27:08813 }
814 }
815}
816
817MenuItemView* WrenchMenu::AppendMenuItem(MenuItemView* parent,
818 MenuModel* model,
819 int index,
820 MenuModel::ItemType menu_type,
821 int* next_id) {
822 int id = (*next_id)++;
[email protected]eccda9db2010-06-23 17:27:08823
824 id_to_entry_[id].first = model;
825 id_to_entry_[id].second = index;
826
[email protected]74434942010-12-05 04:00:59827 MenuItemView* menu_item = parent->AppendMenuItemFromModel(model, index, id);
[email protected]eccda9db2010-06-23 17:27:08828
[email protected]d4c4f252010-09-01 19:27:46829 if (menu_item)
830 menu_item->SetVisible(model->IsVisibleAt(index));
831
[email protected]eccda9db2010-06-23 17:27:08832 if (menu_type == MenuModel::TYPE_COMMAND && model->HasIcons()) {
833 SkBitmap icon;
834 if (model->GetIconAt(index, &icon))
835 menu_item->SetIcon(icon);
836 }
837
838 return menu_item;
839}
840
841void WrenchMenu::CancelAndEvaluate(MenuModel* model, int index) {
842 selected_menu_model_ = model;
843 selected_index_ = index;
844 root_->Cancel();
845}
[email protected]c5174282011-05-20 23:42:06846
847void WrenchMenu::CreateBookmarkMenu() {
848 if (bookmark_menu_delegate_.get())
849 return; // Already created the menu.
850
851 BookmarkModel* model = browser_->profile()->GetBookmarkModel();
852 if (!model->IsLoaded())
853 return;
854
855 model->AddObserver(this);
856 bookmark_menu_delegate_.reset(
857 new BookmarkMenuDelegate(browser_->profile(),
858 NULL,
859 browser_->window()->GetNativeHandle(),
860 first_bookmark_command_id_));
861 bookmark_menu_delegate_->Init(
862 this, bookmark_menu_, model->GetBookmarkBarNode(), 0,
863 BookmarkMenuDelegate::SHOW_OTHER_FOLDER);
864}