[email protected] | 9e790bd | 2011-01-10 23:48:54 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 9e790bd | 2011-01-10 23:48:54 | [diff] [blame] | 5 | #include "chrome/browser/ui/views/wrench_menu.h" |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 6 | |
| 7 | #include <cmath> |
| 8 | |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 9 | #include "base/string_number_conversions.h" |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 10 | #include "base/utf_string_conversions.h" |
[email protected] | 1a3aba8 | 2010-11-08 23:52:54 | [diff] [blame] | 11 | #include "chrome/app/chrome_command_ids.h" |
[email protected] | c517428 | 2011-05-20 23:42:06 | [diff] [blame] | 12 | #include "chrome/browser/bookmarks/bookmark_model.h" |
[email protected] | 8ecad5e | 2010-12-02 21:18:33 | [diff] [blame] | 13 | #include "chrome/browser/profiles/profile.h" |
[email protected] | f46be6e | 2010-11-16 03:52:32 | [diff] [blame] | 14 | #include "chrome/browser/ui/browser.h" |
[email protected] | c517428 | 2011-05-20 23:42:06 | [diff] [blame] | 15 | #include "chrome/browser/ui/browser_window.h" |
| 16 | #include "chrome/browser/ui/views/bookmarks/bookmark_menu_delegate.h" |
[email protected] | 21f1168 | 2011-03-02 16:45:42 | [diff] [blame] | 17 | #include "content/browser/tab_contents/tab_contents.h" |
[email protected] | afd1e52 | 2011-04-27 23:29:59 | [diff] [blame] | 18 | #include "content/browser/user_metrics.h" |
[email protected] | f4c2a6f | 2011-03-08 18:28:06 | [diff] [blame] | 19 | #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] | e12e3f6 | 2010-08-06 00:44:59 | [diff] [blame] | 23 | #include "grit/chromium_strings.h" |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 24 | #include "grit/generated_resources.h" |
| 25 | #include "grit/theme_resources.h" |
| 26 | #include "third_party/skia/include/core/SkPaint.h" |
[email protected] | c051a1b | 2011-01-21 23:30:17 | [diff] [blame] | 27 | #include "ui/base/l10n/l10n_util.h" |
[email protected] | 42ce29d | 2011-01-20 23:19:46 | [diff] [blame] | 28 | #include "ui/base/resource/resource_bundle.h" |
[email protected] | 08397d5 | 2011-02-05 01:53:38 | [diff] [blame] | 29 | #include "ui/gfx/canvas.h" |
| 30 | #include "ui/gfx/canvas_skia.h" |
| 31 | #include "ui/gfx/skia_util.h" |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 32 | #include "views/background.h" |
| 33 | #include "views/controls/button/image_button.h" |
[email protected] | a00b032 | 2010-06-25 16:21:32 | [diff] [blame] | 34 | #include "views/controls/button/menu_button.h" |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 35 | #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] | 44cbd9e | 2011-01-14 15:49:40 | [diff] [blame] | 43 | using ui::MenuModel; |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 44 | using views::CustomButton; |
| 45 | using views::ImageButton; |
| 46 | using views::Label; |
| 47 | using views::MenuConfig; |
| 48 | using views::MenuItemView; |
| 49 | using views::TextButton; |
| 50 | using views::View; |
| 51 | |
| 52 | namespace { |
| 53 | |
| 54 | // Colors used for buttons. |
| 55 | const SkColor kHotBorderColor = SkColorSetARGB(72, 0, 0, 0); |
| 56 | const SkColor kBorderColor = SkColorSetARGB(36, 0, 0, 0); |
| 57 | const SkColor kPushedBorderColor = SkColorSetARGB(72, 0, 0, 0); |
| 58 | const SkColor kHotBackgroundColor = SkColorSetARGB(204, 255, 255, 255); |
| 59 | const SkColor kBackgroundColor = SkColorSetARGB(102, 255, 255, 255); |
| 60 | const SkColor kPushedBackgroundColor = SkColorSetARGB(13, 0, 0, 0); |
| 61 | |
| 62 | // Horizontal padding on the edges of the buttons. |
| 63 | const int kHorizontalPadding = 6; |
| 64 | |
| 65 | // Subclass of ImageButton whose preferred size includes the size of the border. |
| 66 | class FullscreenButton : public ImageButton { |
| 67 | public: |
[email protected] | 65ecd5b | 2010-07-27 21:29:06 | [diff] [blame] | 68 | explicit FullscreenButton(views::ButtonListener* listener) |
| 69 | : ImageButton(listener) { } |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 70 | |
| 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. |
| 86 | class 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] | e57bfac | 2010-08-03 17:19:54 | [diff] [blame] | 95 | insets->Set(MenuConfig::instance().item_top_margin, |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 96 | kHorizontalPadding, |
[email protected] | e57bfac | 2010-08-03 17:19:54 | [diff] [blame] | 97 | MenuConfig::instance().item_bottom_margin, |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 98 | 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. |
| 108 | class 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] | 5214bdd | 2010-08-02 21:43:47 | [diff] [blame] | 125 | 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] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 132 | } |
| 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] | 3434490 | 2010-07-07 20:26:26 | [diff] [blame] | 140 | switch (TypeAdjustedForRTL()) { |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 141 | 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] | 3434490 | 2010-07-07 20:26:26 | [diff] [blame] | 207 | 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] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 219 | 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. |
| 233 | class ScheduleAllView : public views::View { |
| 234 | public: |
| 235 | ScheduleAllView() {} |
| 236 | |
[email protected] | 6483195 | 2011-06-03 21:19:32 | [diff] [blame] | 237 | #if !defined(COMPOSITOR_2) |
[email protected] | 0a119c9 | 2011-02-23 17:50:56 | [diff] [blame] | 238 | virtual void SchedulePaintInRect(const gfx::Rect& r) { |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 239 | if (!IsVisible()) |
| 240 | return; |
| 241 | |
[email protected] | 2083860 | 2011-02-09 04:50:21 | [diff] [blame] | 242 | if (parent()) |
[email protected] | 0a119c9 | 2011-02-23 17:50:56 | [diff] [blame] | 243 | parent()->SchedulePaintInRect(GetMirroredBounds()); |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 244 | } |
[email protected] | 6483195 | 2011-06-03 21:19:32 | [diff] [blame] | 245 | #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] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 254 | |
| 255 | private: |
| 256 | DISALLOW_COPY_AND_ASSIGN(ScheduleAllView); |
| 257 | }; |
| 258 | |
[email protected] | 6f1d18e | 2011-01-10 20:57:00 | [diff] [blame] | 259 | string16 GetAccessibleNameForWrenchMenuItem( |
[email protected] | 2cc800b | 2010-10-01 18:27:34 | [diff] [blame] | 260 | MenuModel* model, int item_index, int accessible_string_id) { |
[email protected] | 6f1d18e | 2011-01-10 20:57:00 | [diff] [blame] | 261 | string16 accessible_name = l10n_util::GetStringUTF16(accessible_string_id); |
| 262 | string16 accelerator_text; |
[email protected] | 2cc800b | 2010-10-01 18:27:34 | [diff] [blame] | 263 | |
[email protected] | 44cbd9e | 2011-01-14 15:49:40 | [diff] [blame] | 264 | ui::Accelerator menu_accelerator; |
[email protected] | 2cc800b | 2010-10-01 18:27:34 | [diff] [blame] | 265 | if (model->GetAcceleratorAt(item_index, &menu_accelerator)) { |
| 266 | accelerator_text = |
[email protected] | 3a37ad41 | 2011-05-20 14:46:05 | [diff] [blame] | 267 | views::Accelerator(menu_accelerator.key_code(), |
[email protected] | 2cc800b | 2010-10-01 18:27:34 | [diff] [blame] | 268 | menu_accelerator.modifiers()).GetShortcutText(); |
| 269 | } |
| 270 | |
| 271 | return MenuItemView::GetAccessibleNameForMenuItem( |
| 272 | accessible_name, accelerator_text); |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 273 | } |
| 274 | |
[email protected] | 2cc800b | 2010-10-01 18:27:34 | [diff] [blame] | 275 | // WrenchMenuView is a view that can contain text buttons. |
| 276 | class 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] | 7c1b7b4 | 2011-01-06 21:39:37 | [diff] [blame] | 295 | new TextButton(this, UTF16ToWide(l10n_util::GetStringUTF16(string_id))); |
[email protected] | 001f08a | 2011-01-14 18:34:56 | [diff] [blame] | 296 | button->SetAccessibleName( |
| 297 | GetAccessibleNameForWrenchMenuItem(menu_model_, index, acc_string_id)); |
[email protected] | 4d81c24 | 2011-06-01 17:59:47 | [diff] [blame] | 298 | button->set_focusable(true); |
[email protected] | 2cc800b | 2010-10-01 18:27:34 | [diff] [blame] | 299 | 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] | 2cc800b | 2010-10-01 18:27:34 | [diff] [blame] | 310 | 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] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 327 | } // namespace |
| 328 | |
| 329 | // CutCopyPasteView ------------------------------------------------------------ |
| 330 | |
| 331 | // CutCopyPasteView is the view containing the cut/copy/paste buttons. |
[email protected] | 2cc800b | 2010-10-01 18:27:34 | [diff] [blame] | 332 | class WrenchMenu::CutCopyPasteView : public WrenchMenuView { |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 333 | public: |
| 334 | CutCopyPasteView(WrenchMenu* menu, |
| 335 | MenuModel* menu_model, |
| 336 | int cut_index, |
| 337 | int copy_index, |
| 338 | int paste_index) |
[email protected] | 2cc800b | 2010-10-01 18:27:34 | [diff] [blame] | 339 | : WrenchMenuView(menu, menu_model) { |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 340 | TextButton* cut = CreateAndConfigureButton( |
[email protected] | 2cc800b | 2010-10-01 18:27:34 | [diff] [blame] | 341 | IDS_CUT, MenuButtonBackground::LEFT_BUTTON, cut_index, NULL); |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 342 | |
| 343 | MenuButtonBackground* copy_background = NULL; |
| 344 | CreateAndConfigureButton( |
[email protected] | 2cc800b | 2010-10-01 18:27:34 | [diff] [blame] | 345 | IDS_COPY, MenuButtonBackground::CENTER_BUTTON, copy_index, |
| 346 | ©_background); |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 347 | |
| 348 | TextButton* paste = CreateAndConfigureButton( |
[email protected] | 2cc800b | 2010-10-01 18:27:34 | [diff] [blame] | 349 | IDS_PASTE, MenuButtonBackground::RIGHT_BUTTON, paste_index, NULL); |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 350 | |
| 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] | 2083860 | 2011-02-09 04:50:21 | [diff] [blame] | 357 | return gfx::Size(GetMaxChildViewPreferredWidth() * child_count(), 0); |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | void Layout() { |
| 361 | // All buttons are given the same width. |
| 362 | int width = GetMaxChildViewPreferredWidth(); |
[email protected] | 2083860 | 2011-02-09 04:50:21 | [diff] [blame] | 363 | for (int i = 0; i < child_count(); ++i) |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 364 | 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] | 2083860 | 2011-02-09 04:50:21 | [diff] [blame] | 376 | for (int i = 0; i < child_count(); ++i) |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 377 | width = std::max(width, GetChildViewAt(i)->GetPreferredSize().width()); |
| 378 | return width; |
| 379 | } |
| 380 | |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 381 | DISALLOW_COPY_AND_ASSIGN(CutCopyPasteView); |
| 382 | }; |
| 383 | |
| 384 | // ZoomView -------------------------------------------------------------------- |
| 385 | |
| 386 | // Padding between the increment buttons and the reset button. |
| 387 | static 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] | 2cc800b | 2010-10-01 18:27:34 | [diff] [blame] | 392 | class WrenchMenu::ZoomView : public WrenchMenuView, |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 393 | public NotificationObserver { |
| 394 | public: |
| 395 | ZoomView(WrenchMenu* menu, |
| 396 | MenuModel* menu_model, |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 397 | int decrement_index, |
[email protected] | e1a02d6 | 2010-07-16 16:27:41 | [diff] [blame] | 398 | int increment_index, |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 399 | int fullscreen_index) |
[email protected] | 2cc800b | 2010-10-01 18:27:34 | [diff] [blame] | 400 | : WrenchMenuView(menu, menu_model), |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 401 | 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] | 2cc800b | 2010-10-01 18:27:34 | [diff] [blame] | 407 | decrement_button_ = CreateButtonWithAccName( |
| 408 | IDS_ZOOM_MINUS2, MenuButtonBackground::LEFT_BUTTON, decrement_index, |
| 409 | NULL, IDS_ACCNAME_ZOOM_MINUS2); |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 410 | |
[email protected] | 7c1b7b4 | 2011-01-06 21:39:37 | [diff] [blame] | 411 | zoom_label_ = new Label( |
| 412 | UTF16ToWide(l10n_util::GetStringFUTF16Int(IDS_ZOOM_PERCENT, 100))); |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 413 | 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] | 84e4f8c | 2010-07-15 02:18:32 | [diff] [blame] | 421 | zoom_label_width_ = MaxWidthForZoomLabel(); |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 422 | |
[email protected] | 2cc800b | 2010-10-01 18:27:34 | [diff] [blame] | 423 | increment_button_ = CreateButtonWithAccName( |
| 424 | IDS_ZOOM_PLUS2, MenuButtonBackground::RIGHT_BUTTON, increment_index, |
| 425 | NULL, IDS_ACCNAME_ZOOM_PLUS2); |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 426 | |
[email protected] | 5214bdd | 2010-08-02 21:43:47 | [diff] [blame] | 427 | center_bg->SetOtherButtons(decrement_button_, increment_button_); |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 428 | |
| 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] | 4d81c24 | 2011-06-01 17:59:47 | [diff] [blame] | 434 | fullscreen_button_->set_focusable(true); |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 435 | 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] | 001f08a | 2011-01-14 18:34:56 | [diff] [blame] | 443 | fullscreen_button_->SetAccessibleName( |
[email protected] | 2cc800b | 2010-10-01 18:27:34 | [diff] [blame] | 444 | GetAccessibleNameForWrenchMenuItem( |
[email protected] | 001f08a | 2011-01-14 18:34:56 | [diff] [blame] | 445 | menu_model, fullscreen_index, IDS_ACCNAME_FULLSCREEN)); |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 446 | AddChildView(fullscreen_button_); |
| 447 | |
| 448 | UpdateZoomControls(); |
| 449 | |
[email protected] | 89805c1 | 2011-05-23 23:53:00 | [diff] [blame] | 450 | registrar_.Add( |
| 451 | this, NotificationType::ZOOM_LEVEL_CHANGED, |
| 452 | Source<HostZoomMap>(menu->browser_->profile()->GetHostZoomMap())); |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 453 | } |
| 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] | 97a3114 | 2011-02-08 00:09:16 | [diff] [blame] | 472 | decrement_button_->SetBoundsRect(bounds); |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 473 | |
| 474 | x += bounds.width(); |
| 475 | bounds.set_x(x); |
| 476 | bounds.set_width(zoom_label_width_); |
[email protected] | 97a3114 | 2011-02-08 00:09:16 | [diff] [blame] | 477 | zoom_label_->SetBoundsRect(bounds); |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 478 | |
| 479 | x += bounds.width(); |
| 480 | bounds.set_x(x); |
| 481 | bounds.set_width(button_width); |
[email protected] | 97a3114 | 2011-02-08 00:09:16 | [diff] [blame] | 482 | increment_button_->SetBoundsRect(bounds); |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 483 | |
| 484 | x += bounds.width() + kZoomPadding; |
| 485 | bounds.set_x(x); |
| 486 | bounds.set_width(fullscreen_button_->GetPreferredSize().width()); |
[email protected] | 97a3114 | 2011-02-08 00:09:16 | [diff] [blame] | 487 | fullscreen_button_->SetBoundsRect(bounds); |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 488 | } |
| 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] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 510 | 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] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 516 | increment_button_->SetEnabled(enable_increment); |
| 517 | decrement_button_->SetEnabled(enable_decrement); |
[email protected] | 7c1b7b4 | 2011-01-06 21:39:37 | [diff] [blame] | 518 | zoom_label_->SetText(UTF16ToWide(l10n_util::GetStringFUTF16Int( |
| 519 | IDS_ZOOM_PERCENT, |
| 520 | zoom))); |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 521 | |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 522 | zoom_label_width_ = MaxWidthForZoomLabel(); |
[email protected] | 84e4f8c | 2010-07-15 02:18:32 | [diff] [blame] | 523 | } |
| 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] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 531 | |
[email protected] | 84e4f8c | 2010-07-15 02:18:32 | [diff] [blame] | 532 | int max_w = 0; |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 533 | |
| 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] | 13658c4 | 2011-01-04 20:46:14 | [diff] [blame] | 541 | int w = font.GetStringWidth( |
| 542 | l10n_util::GetStringFUTF16Int(IDS_ZOOM_PERCENT, i)); |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 543 | max_w = std::max(w, max_w); |
| 544 | } |
| 545 | } else { |
[email protected] | 13658c4 | 2011-01-04 20:46:14 | [diff] [blame] | 546 | max_w = font.GetStringWidth( |
| 547 | l10n_util::GetStringFUTF16Int(IDS_ZOOM_PERCENT, 100)); |
[email protected] | 84e4f8c | 2010-07-15 02:18:32 | [diff] [blame] | 548 | } |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 549 | |
[email protected] | 84e4f8c | 2010-07-15 02:18:32 | [diff] [blame] | 550 | return max_w + insets.width(); |
| 551 | } |
| 552 | |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 553 | // 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 | |
| 577 | WrenchMenu::WrenchMenu(Browser* browser) |
| 578 | : browser_(browser), |
| 579 | selected_menu_model_(NULL), |
[email protected] | c517428 | 2011-05-20 23:42:06 | [diff] [blame] | 580 | selected_index_(0), |
| 581 | bookmark_menu_(NULL), |
| 582 | first_bookmark_command_id_(0) { |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 583 | } |
| 584 | |
[email protected] | 44cbd9e | 2011-01-14 15:49:40 | [diff] [blame] | 585 | void WrenchMenu::Init(ui::MenuModel* model) { |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 586 | 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] | c517428 | 2011-05-20 23:42:06 | [diff] [blame] | 592 | first_bookmark_command_id_ = next_id + 1; |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 593 | } |
| 594 | |
[email protected] | a00b032 | 2010-06-25 16:21:32 | [diff] [blame] | 595 | void WrenchMenu::RunMenu(views::MenuButton* host) { |
[email protected] | 428f54b | 2010-10-05 03:31:27 | [diff] [blame] | 596 | // 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] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 601 | gfx::Point screen_loc; |
| 602 | views::View::ConvertPointToScreen(host, &screen_loc); |
[email protected] | e78f12a2 | 2010-07-28 22:41:05 | [diff] [blame] | 603 | gfx::Rect bounds(screen_loc, host->size()); |
[email protected] | 366069f | 2011-01-11 09:36:12 | [diff] [blame] | 604 | UserMetrics::RecordAction(UserMetricsAction("ShowAppMenu")); |
[email protected] | a00b032 | 2010-06-25 16:21:32 | [diff] [blame] | 605 | root_->RunMenuAt(host->GetWindow()->GetNativeWindow(), host, bounds, |
[email protected] | 77745c3 | 2011-06-07 22:15:35 | [diff] [blame^] | 606 | MenuItemView::TOPRIGHT, true); |
[email protected] | c517428 | 2011-05-20 23:42:06 | [diff] [blame] | 607 | if (bookmark_menu_delegate_.get()) { |
| 608 | BookmarkModel* model = browser_->profile()->GetBookmarkModel(); |
| 609 | if (model) |
| 610 | model->RemoveObserver(this); |
| 611 | } |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 612 | if (selected_menu_model_) |
| 613 | selected_menu_model_->ActivatedAt(selected_index_); |
| 614 | } |
| 615 | |
[email protected] | c517428 | 2011-05-20 23:42:06 | [diff] [blame] | 616 | std::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 | |
| 622 | bool 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 | |
| 629 | bool 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 | |
| 638 | bool WrenchMenu::AreDropTypesRequired(MenuItemView* menu) { |
| 639 | CreateBookmarkMenu(); |
| 640 | return bookmark_menu_delegate_.get() && |
| 641 | bookmark_menu_delegate_->AreDropTypesRequired(menu); |
| 642 | } |
| 643 | |
| 644 | bool 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 | |
| 651 | int 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 | |
| 660 | int 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 | |
| 670 | bool 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 | |
| 680 | bool WrenchMenu::CanDrag(MenuItemView* menu) { |
| 681 | return is_bookmark_command(menu->GetCommand()) ? |
| 682 | bookmark_menu_delegate_->CanDrag(menu) : false; |
| 683 | } |
| 684 | |
| 685 | void 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 | |
| 691 | int WrenchMenu::GetDragOperations(MenuItemView* sender) { |
| 692 | return is_bookmark_command(sender->GetCommand()) ? |
| 693 | bookmark_menu_delegate_->GetDragOperations(sender) : |
| 694 | MenuDelegate::GetDragOperations(sender); |
| 695 | } |
| 696 | |
| 697 | int 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] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 703 | bool WrenchMenu::IsItemChecked(int id) const { |
[email protected] | c7b6a22 | 2011-05-27 16:24:43 | [diff] [blame] | 704 | if (is_bookmark_command(id)) |
[email protected] | c517428 | 2011-05-20 23:42:06 | [diff] [blame] | 705 | return false; |
| 706 | |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 707 | const Entry& entry = id_to_entry_.find(id)->second; |
| 708 | return entry.first->IsItemCheckedAt(entry.second); |
| 709 | } |
| 710 | |
| 711 | bool WrenchMenu::IsCommandEnabled(int id) const { |
[email protected] | c517428 | 2011-05-20 23:42:06 | [diff] [blame] | 712 | if (is_bookmark_command(id)) |
| 713 | return true; |
| 714 | |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 715 | 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] | e1a02d6 | 2010-07-16 16:27:41 | [diff] [blame] | 723 | return command_id == IDC_CUT || command_id == IDC_ZOOM_MINUS || |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 724 | entry.first->IsEnabledAt(entry.second); |
| 725 | } |
| 726 | |
[email protected] | c517428 | 2011-05-20 23:42:06 | [diff] [blame] | 727 | void 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] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 734 | const Entry& entry = id_to_entry_.find(id)->second; |
[email protected] | 60555ec | 2010-07-02 20:18:25 | [diff] [blame] | 735 | int command_id = entry.first->GetCommandIdAt(entry.second); |
| 736 | |
[email protected] | e1a02d6 | 2010-07-16 16:27:41 | [diff] [blame] | 737 | if (command_id == IDC_CUT || command_id == IDC_ZOOM_MINUS) { |
[email protected] | 60555ec | 2010-07-02 20:18:25 | [diff] [blame] | 738 | // 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] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 744 | return entry.first->ActivatedAt(entry.second); |
| 745 | } |
| 746 | |
| 747 | bool WrenchMenu::GetAccelerator(int id, views::Accelerator* accelerator) { |
[email protected] | c517428 | 2011-05-20 23:42:06 | [diff] [blame] | 748 | if (is_bookmark_command(id)) |
| 749 | return false; |
| 750 | |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 751 | const Entry& entry = id_to_entry_.find(id)->second; |
| 752 | int command_id = entry.first->GetCommandIdAt(entry.second); |
[email protected] | e1a02d6 | 2010-07-16 16:27:41 | [diff] [blame] | 753 | if (command_id == IDC_CUT || command_id == IDC_ZOOM_MINUS) { |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 754 | // These have special child views; don't show the accelerator for them. |
| 755 | return false; |
| 756 | } |
| 757 | |
[email protected] | 44cbd9e | 2011-01-14 15:49:40 | [diff] [blame] | 758 | ui::Accelerator menu_accelerator; |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 759 | if (!entry.first->GetAcceleratorAt(entry.second, &menu_accelerator)) |
| 760 | return false; |
| 761 | |
[email protected] | 3a37ad41 | 2011-05-20 14:46:05 | [diff] [blame] | 762 | *accelerator = views::Accelerator(menu_accelerator.key_code(), |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 763 | menu_accelerator.modifiers()); |
| 764 | return true; |
| 765 | } |
| 766 | |
[email protected] | c517428 | 2011-05-20 23:42:06 | [diff] [blame] | 767 | void WrenchMenu::WillShowMenu(MenuItemView* menu) { |
| 768 | if (menu == bookmark_menu_) |
| 769 | CreateBookmarkMenu(); |
| 770 | } |
| 771 | |
| 772 | void WrenchMenu::BookmarkModelChanged() { |
[email protected] | c7b6a22 | 2011-05-27 16:24:43 | [diff] [blame] | 773 | DCHECK(bookmark_menu_delegate_.get()); |
| 774 | if (!bookmark_menu_delegate_->is_mutating_model()) |
| 775 | root_->Cancel(); |
[email protected] | c517428 | 2011-05-20 23:42:06 | [diff] [blame] | 776 | } |
| 777 | |
[email protected] | 428f54b | 2010-10-05 03:31:27 | [diff] [blame] | 778 | WrenchMenu::~WrenchMenu() { |
| 779 | } |
| 780 | |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 781 | void 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] | c517428 | 2011-05-20 23:42:06 | [diff] [blame] | 794 | 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] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 823 | } |
| 824 | } |
| 825 | } |
| 826 | |
| 827 | MenuItemView* 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] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 833 | |
| 834 | id_to_entry_[id].first = model; |
| 835 | id_to_entry_[id].second = index; |
| 836 | |
[email protected] | 7443494 | 2010-12-05 04:00:59 | [diff] [blame] | 837 | MenuItemView* menu_item = parent->AppendMenuItemFromModel(model, index, id); |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 838 | |
[email protected] | d4c4f25 | 2010-09-01 19:27:46 | [diff] [blame] | 839 | if (menu_item) |
| 840 | menu_item->SetVisible(model->IsVisibleAt(index)); |
| 841 | |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 842 | 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 | |
| 851 | void WrenchMenu::CancelAndEvaluate(MenuModel* model, int index) { |
| 852 | selected_menu_model_ = model; |
| 853 | selected_index_ = index; |
| 854 | root_->Cancel(); |
| 855 | } |
[email protected] | c517428 | 2011-05-20 23:42:06 | [diff] [blame] | 856 | |
| 857 | void 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 | } |