[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/views/wrench_menu.h" |
| 6 | |
| 7 | #include <cmath> |
| 8 | |
| 9 | #include "app/l10n_util.h" |
| 10 | #include "app/resource_bundle.h" |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 11 | #include "base/string_number_conversions.h" |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 12 | #include "base/utf_string_conversions.h" |
[email protected] | 1a3aba8 | 2010-11-08 23:52:54 | [diff] [blame] | 13 | #include "chrome/app/chrome_command_ids.h" |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 14 | #include "chrome/browser/profile.h" |
| 15 | #include "chrome/browser/tab_contents/tab_contents.h" |
[email protected] | f46be6e | 2010-11-16 03:52:32 | [diff] [blame^] | 16 | #include "chrome/browser/ui/browser.h" |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 17 | #include "chrome/common/notification_observer.h" |
| 18 | #include "chrome/common/notification_registrar.h" |
| 19 | #include "chrome/common/notification_source.h" |
| 20 | #include "chrome/common/notification_type.h" |
| 21 | #include "gfx/canvas.h" |
[email protected] | 84e4f8c | 2010-07-15 02:18:32 | [diff] [blame] | 22 | #include "gfx/canvas_skia.h" |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 23 | #include "gfx/skia_util.h" |
[email protected] | e12e3f6 | 2010-08-06 00:44:59 | [diff] [blame] | 24 | #include "grit/chromium_strings.h" |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 25 | #include "grit/generated_resources.h" |
| 26 | #include "grit/theme_resources.h" |
| 27 | #include "third_party/skia/include/core/SkPaint.h" |
| 28 | #include "views/background.h" |
| 29 | #include "views/controls/button/image_button.h" |
[email protected] | a00b032 | 2010-06-25 16:21:32 | [diff] [blame] | 30 | #include "views/controls/button/menu_button.h" |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 31 | #include "views/controls/button/text_button.h" |
| 32 | #include "views/controls/label.h" |
| 33 | #include "views/controls/menu/menu_config.h" |
| 34 | #include "views/controls/menu/menu_item_view.h" |
| 35 | #include "views/controls/menu/menu_scroll_view_container.h" |
| 36 | #include "views/controls/menu/submenu_view.h" |
| 37 | #include "views/window/window.h" |
| 38 | |
| 39 | using menus::MenuModel; |
| 40 | using views::CustomButton; |
| 41 | using views::ImageButton; |
| 42 | using views::Label; |
| 43 | using views::MenuConfig; |
| 44 | using views::MenuItemView; |
| 45 | using views::TextButton; |
| 46 | using views::View; |
| 47 | |
| 48 | namespace { |
| 49 | |
| 50 | // Colors used for buttons. |
| 51 | const SkColor kHotBorderColor = SkColorSetARGB(72, 0, 0, 0); |
| 52 | const SkColor kBorderColor = SkColorSetARGB(36, 0, 0, 0); |
| 53 | const SkColor kPushedBorderColor = SkColorSetARGB(72, 0, 0, 0); |
| 54 | const SkColor kHotBackgroundColor = SkColorSetARGB(204, 255, 255, 255); |
| 55 | const SkColor kBackgroundColor = SkColorSetARGB(102, 255, 255, 255); |
| 56 | const SkColor kPushedBackgroundColor = SkColorSetARGB(13, 0, 0, 0); |
| 57 | |
| 58 | // Horizontal padding on the edges of the buttons. |
| 59 | const int kHorizontalPadding = 6; |
| 60 | |
| 61 | // Subclass of ImageButton whose preferred size includes the size of the border. |
| 62 | class FullscreenButton : public ImageButton { |
| 63 | public: |
[email protected] | 65ecd5b | 2010-07-27 21:29:06 | [diff] [blame] | 64 | explicit FullscreenButton(views::ButtonListener* listener) |
| 65 | : ImageButton(listener) { } |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 66 | |
| 67 | virtual gfx::Size GetPreferredSize() { |
| 68 | gfx::Size pref = ImageButton::GetPreferredSize(); |
| 69 | gfx::Insets insets; |
| 70 | if (border()) |
| 71 | border()->GetInsets(&insets); |
| 72 | pref.Enlarge(insets.width(), insets.height()); |
| 73 | return pref; |
| 74 | } |
| 75 | |
| 76 | private: |
| 77 | DISALLOW_COPY_AND_ASSIGN(FullscreenButton); |
| 78 | }; |
| 79 | |
| 80 | // Border for buttons contained in the menu. This is only used for getting the |
| 81 | // insets, the actual painting is done in MenuButtonBackground. |
| 82 | class MenuButtonBorder : public views::Border { |
| 83 | public: |
| 84 | MenuButtonBorder() {} |
| 85 | |
| 86 | virtual void Paint(const View& view, gfx::Canvas* canvas) const { |
| 87 | // Painting of border is done in MenuButtonBackground. |
| 88 | } |
| 89 | |
| 90 | virtual void GetInsets(gfx::Insets* insets) const { |
[email protected] | e57bfac | 2010-08-03 17:19:54 | [diff] [blame] | 91 | insets->Set(MenuConfig::instance().item_top_margin, |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 92 | kHorizontalPadding, |
[email protected] | e57bfac | 2010-08-03 17:19:54 | [diff] [blame] | 93 | MenuConfig::instance().item_bottom_margin, |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 94 | kHorizontalPadding); |
| 95 | } |
| 96 | |
| 97 | private: |
| 98 | DISALLOW_COPY_AND_ASSIGN(MenuButtonBorder); |
| 99 | }; |
| 100 | |
| 101 | // Combination border/background for the buttons contained in the menu. The |
| 102 | // painting of the border/background is done here as TextButton does not always |
| 103 | // paint the border. |
| 104 | class MenuButtonBackground : public views::Background { |
| 105 | public: |
| 106 | enum ButtonType { |
| 107 | LEFT_BUTTON, |
| 108 | CENTER_BUTTON, |
| 109 | RIGHT_BUTTON, |
| 110 | SINGLE_BUTTON, |
| 111 | }; |
| 112 | |
| 113 | explicit MenuButtonBackground(ButtonType type) |
| 114 | : type_(type), |
| 115 | left_button_(NULL), |
| 116 | right_button_(NULL) {} |
| 117 | |
| 118 | // Used when the type is CENTER_BUTTON to determine if the left/right edge |
| 119 | // needs to be rendered selected. |
| 120 | void SetOtherButtons(CustomButton* left_button, CustomButton* right_button) { |
[email protected] | 5214bdd | 2010-08-02 21:43:47 | [diff] [blame] | 121 | if (base::i18n::IsRTL()) { |
| 122 | left_button_ = right_button; |
| 123 | right_button_ = left_button; |
| 124 | } else { |
| 125 | left_button_ = left_button; |
| 126 | right_button_ = right_button; |
| 127 | } |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | virtual void Paint(gfx::Canvas* canvas, View* view) const { |
| 131 | CustomButton::ButtonState state = |
| 132 | (view->GetClassName() == views::Label::kViewClassName) ? |
| 133 | CustomButton::BS_NORMAL : static_cast<CustomButton*>(view)->state(); |
| 134 | int w = view->width(); |
| 135 | int h = view->height(); |
[email protected] | 3434490 | 2010-07-07 20:26:26 | [diff] [blame] | 136 | switch (TypeAdjustedForRTL()) { |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 137 | case LEFT_BUTTON: |
| 138 | canvas->FillRectInt(background_color(state), 1, 1, w, h - 2); |
| 139 | canvas->FillRectInt(border_color(state), 2, 0, w, 1); |
| 140 | canvas->FillRectInt(border_color(state), 1, 1, 1, 1); |
| 141 | canvas->FillRectInt(border_color(state), 0, 2, 1, h - 4); |
| 142 | canvas->FillRectInt(border_color(state), 1, h - 2, 1, 1); |
| 143 | canvas->FillRectInt(border_color(state), 2, h - 1, w, 1); |
| 144 | break; |
| 145 | |
| 146 | case CENTER_BUTTON: { |
| 147 | canvas->FillRectInt(background_color(state), 1, 1, w - 2, h - 2); |
| 148 | SkColor left_color = state != CustomButton::BS_NORMAL ? |
| 149 | border_color(state) : border_color(left_button_->state()); |
| 150 | canvas->FillRectInt(left_color, 0, 0, 1, h); |
| 151 | canvas->FillRectInt(border_color(state), 1, 0, w - 2, 1); |
| 152 | canvas->FillRectInt(border_color(state), 1, h - 1, w - 2, 1); |
| 153 | SkColor right_color = state != CustomButton::BS_NORMAL ? |
| 154 | border_color(state) : border_color(right_button_->state()); |
| 155 | canvas->FillRectInt(right_color, w - 1, 0, 1, h); |
| 156 | break; |
| 157 | } |
| 158 | |
| 159 | case RIGHT_BUTTON: |
| 160 | canvas->FillRectInt(background_color(state), 0, 1, w - 1, h - 2); |
| 161 | canvas->FillRectInt(border_color(state), 0, 0, w - 2, 1); |
| 162 | canvas->FillRectInt(border_color(state), w - 2, 1, 1, 1); |
| 163 | canvas->FillRectInt(border_color(state), w - 1, 2, 1, h - 4); |
| 164 | canvas->FillRectInt(border_color(state), w - 2, h - 2, 1, 1); |
| 165 | canvas->FillRectInt(border_color(state), 0, h - 1, w - 2, 1); |
| 166 | break; |
| 167 | |
| 168 | case SINGLE_BUTTON: |
| 169 | canvas->FillRectInt(background_color(state), 1, 1, w - 2, h - 2); |
| 170 | canvas->FillRectInt(border_color(state), 2, 0, w - 4, 1); |
| 171 | canvas->FillRectInt(border_color(state), 1, 1, 1, 1); |
| 172 | canvas->FillRectInt(border_color(state), 0, 2, 1, h - 4); |
| 173 | canvas->FillRectInt(border_color(state), 1, h - 2, 1, 1); |
| 174 | canvas->FillRectInt(border_color(state), 2, h - 1, w - 4, 1); |
| 175 | canvas->FillRectInt(border_color(state), w - 2, 1, 1, 1); |
| 176 | canvas->FillRectInt(border_color(state), w - 1, 2, 1, h - 4); |
| 177 | canvas->FillRectInt(border_color(state), w - 2, h - 2, 1, 1); |
| 178 | break; |
| 179 | |
| 180 | default: |
| 181 | NOTREACHED(); |
| 182 | break; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | private: |
| 187 | static SkColor border_color(CustomButton::ButtonState state) { |
| 188 | switch (state) { |
| 189 | case CustomButton::BS_HOT: return kHotBorderColor; |
| 190 | case CustomButton::BS_PUSHED: return kPushedBorderColor; |
| 191 | default: return kBorderColor; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | static SkColor background_color(CustomButton::ButtonState state) { |
| 196 | switch (state) { |
| 197 | case CustomButton::BS_HOT: return kHotBackgroundColor; |
| 198 | case CustomButton::BS_PUSHED: return kPushedBackgroundColor; |
| 199 | default: return kBackgroundColor; |
| 200 | } |
| 201 | } |
| 202 | |
[email protected] | 3434490 | 2010-07-07 20:26:26 | [diff] [blame] | 203 | ButtonType TypeAdjustedForRTL() const { |
| 204 | if (!base::i18n::IsRTL()) |
| 205 | return type_; |
| 206 | |
| 207 | switch (type_) { |
| 208 | case LEFT_BUTTON: return RIGHT_BUTTON; |
| 209 | case RIGHT_BUTTON: return LEFT_BUTTON; |
| 210 | default: break; |
| 211 | } |
| 212 | return type_; |
| 213 | } |
| 214 | |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 215 | const ButtonType type_; |
| 216 | |
| 217 | // See description above setter for details. |
| 218 | CustomButton* left_button_; |
| 219 | CustomButton* right_button_; |
| 220 | |
| 221 | DISALLOW_COPY_AND_ASSIGN(MenuButtonBackground); |
| 222 | }; |
| 223 | |
| 224 | // A View subclass that forces SchedulePaint to paint all. Normally when the |
| 225 | // mouse enters/exits a button the buttons invokes SchedulePaint. As part of the |
| 226 | // button border (MenuButtonBackground) is rendered by the button to the |
| 227 | // left/right of it SchedulePaint on the the button may not be enough, so this |
| 228 | // forces a paint all. |
| 229 | class ScheduleAllView : public views::View { |
| 230 | public: |
| 231 | ScheduleAllView() {} |
| 232 | |
| 233 | virtual void SchedulePaint(const gfx::Rect& r, bool urgent) { |
| 234 | if (!IsVisible()) |
| 235 | return; |
| 236 | |
[email protected] | 3434490 | 2010-07-07 20:26:26 | [diff] [blame] | 237 | if (GetParent()) { |
| 238 | GetParent()->SchedulePaint(GetBounds(APPLY_MIRRORING_TRANSFORMATION), |
| 239 | urgent); |
| 240 | } |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | private: |
| 244 | DISALLOW_COPY_AND_ASSIGN(ScheduleAllView); |
| 245 | }; |
| 246 | |
[email protected] | 2cc800b | 2010-10-01 18:27:34 | [diff] [blame] | 247 | std::wstring GetAccessibleNameForWrenchMenuItem( |
| 248 | MenuModel* model, int item_index, int accessible_string_id) { |
| 249 | std::wstring accessible_name = l10n_util::GetString(accessible_string_id); |
| 250 | std::wstring accelerator_text; |
| 251 | |
| 252 | menus::Accelerator menu_accelerator; |
| 253 | if (model->GetAcceleratorAt(item_index, &menu_accelerator)) { |
| 254 | accelerator_text = |
| 255 | views::Accelerator(menu_accelerator.GetKeyCode(), |
| 256 | menu_accelerator.modifiers()).GetShortcutText(); |
| 257 | } |
| 258 | |
| 259 | return MenuItemView::GetAccessibleNameForMenuItem( |
| 260 | accessible_name, accelerator_text); |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 261 | } |
| 262 | |
[email protected] | 2cc800b | 2010-10-01 18:27:34 | [diff] [blame] | 263 | // WrenchMenuView is a view that can contain text buttons. |
| 264 | class WrenchMenuView : public ScheduleAllView, public views::ButtonListener { |
| 265 | public: |
| 266 | WrenchMenuView(WrenchMenu* menu, MenuModel* menu_model) |
| 267 | : menu_(menu), menu_model_(menu_model) { } |
| 268 | |
| 269 | TextButton* CreateAndConfigureButton(int string_id, |
| 270 | MenuButtonBackground::ButtonType type, |
| 271 | int index, |
| 272 | MenuButtonBackground** background) { |
| 273 | return CreateButtonWithAccName( |
| 274 | string_id, type, index, background, string_id); |
| 275 | } |
| 276 | |
| 277 | TextButton* CreateButtonWithAccName(int string_id, |
| 278 | MenuButtonBackground::ButtonType type, |
| 279 | int index, |
| 280 | MenuButtonBackground** background, |
| 281 | int acc_string_id) { |
| 282 | TextButton* button = |
| 283 | new TextButton(this, l10n_util::GetString(string_id)); |
| 284 | button->SetAccessibleName( |
| 285 | GetAccessibleNameForWrenchMenuItem(menu_model_, index, acc_string_id)); |
| 286 | button->SetFocusable(true); |
| 287 | button->set_request_focus_on_press(false); |
| 288 | button->set_tag(index); |
| 289 | button->SetEnabled(menu_model_->IsEnabledAt(index)); |
| 290 | button->set_prefix_type(TextButton::PREFIX_HIDE); |
| 291 | MenuButtonBackground* bg = new MenuButtonBackground(type); |
| 292 | button->set_background(bg); |
| 293 | button->SetEnabledColor(MenuConfig::instance().text_color); |
| 294 | if (background) |
| 295 | *background = bg; |
| 296 | button->set_border(new MenuButtonBorder()); |
| 297 | button->set_alignment(TextButton::ALIGN_CENTER); |
| 298 | button->SetNormalHasBorder(true); |
| 299 | button->SetFont(views::MenuConfig::instance().font); |
| 300 | button->ClearMaxTextSize(); |
| 301 | AddChildView(button); |
| 302 | return button; |
| 303 | } |
| 304 | |
| 305 | protected: |
| 306 | // Hosting WrenchMenu. |
| 307 | WrenchMenu* menu_; |
| 308 | |
| 309 | // The menu model containing the increment/decrement/reset items. |
| 310 | MenuModel* menu_model_; |
| 311 | |
| 312 | private: |
| 313 | DISALLOW_COPY_AND_ASSIGN(WrenchMenuView); |
| 314 | }; |
| 315 | |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 316 | } // namespace |
| 317 | |
| 318 | // CutCopyPasteView ------------------------------------------------------------ |
| 319 | |
| 320 | // CutCopyPasteView is the view containing the cut/copy/paste buttons. |
[email protected] | 2cc800b | 2010-10-01 18:27:34 | [diff] [blame] | 321 | class WrenchMenu::CutCopyPasteView : public WrenchMenuView { |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 322 | public: |
| 323 | CutCopyPasteView(WrenchMenu* menu, |
| 324 | MenuModel* menu_model, |
| 325 | int cut_index, |
| 326 | int copy_index, |
| 327 | int paste_index) |
[email protected] | 2cc800b | 2010-10-01 18:27:34 | [diff] [blame] | 328 | : WrenchMenuView(menu, menu_model) { |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 329 | TextButton* cut = CreateAndConfigureButton( |
[email protected] | 2cc800b | 2010-10-01 18:27:34 | [diff] [blame] | 330 | IDS_CUT, MenuButtonBackground::LEFT_BUTTON, cut_index, NULL); |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 331 | |
| 332 | MenuButtonBackground* copy_background = NULL; |
| 333 | CreateAndConfigureButton( |
[email protected] | 2cc800b | 2010-10-01 18:27:34 | [diff] [blame] | 334 | IDS_COPY, MenuButtonBackground::CENTER_BUTTON, copy_index, |
| 335 | ©_background); |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 336 | |
| 337 | TextButton* paste = CreateAndConfigureButton( |
[email protected] | 2cc800b | 2010-10-01 18:27:34 | [diff] [blame] | 338 | IDS_PASTE, MenuButtonBackground::RIGHT_BUTTON, paste_index, NULL); |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 339 | |
| 340 | copy_background->SetOtherButtons(cut, paste); |
| 341 | } |
| 342 | |
| 343 | gfx::Size GetPreferredSize() { |
| 344 | // Returned height doesn't matter as MenuItemView forces everything to the |
| 345 | // height of the menuitemview. |
| 346 | return gfx::Size(GetMaxChildViewPreferredWidth() * GetChildViewCount(), 0); |
| 347 | } |
| 348 | |
| 349 | void Layout() { |
| 350 | // All buttons are given the same width. |
| 351 | int width = GetMaxChildViewPreferredWidth(); |
| 352 | for (int i = 0; i < GetChildViewCount(); ++i) |
| 353 | GetChildViewAt(i)->SetBounds(i * width, 0, width, height()); |
| 354 | } |
| 355 | |
| 356 | // ButtonListener |
| 357 | virtual void ButtonPressed(views::Button* sender, const views::Event& event) { |
| 358 | menu_->CancelAndEvaluate(menu_model_, sender->tag()); |
| 359 | } |
| 360 | |
| 361 | private: |
| 362 | // Returns the max preferred width of all the children. |
| 363 | int GetMaxChildViewPreferredWidth() { |
| 364 | int width = 0; |
| 365 | for (int i = 0; i < GetChildViewCount(); ++i) |
| 366 | width = std::max(width, GetChildViewAt(i)->GetPreferredSize().width()); |
| 367 | return width; |
| 368 | } |
| 369 | |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 370 | DISALLOW_COPY_AND_ASSIGN(CutCopyPasteView); |
| 371 | }; |
| 372 | |
| 373 | // ZoomView -------------------------------------------------------------------- |
| 374 | |
| 375 | // Padding between the increment buttons and the reset button. |
| 376 | static const int kZoomPadding = 6; |
| 377 | |
| 378 | // ZoomView contains the various zoom controls: two buttons to increase/decrease |
| 379 | // the zoom, a label showing the current zoom percent, and a button to go |
| 380 | // full-screen. |
[email protected] | 2cc800b | 2010-10-01 18:27:34 | [diff] [blame] | 381 | class WrenchMenu::ZoomView : public WrenchMenuView, |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 382 | public NotificationObserver { |
| 383 | public: |
| 384 | ZoomView(WrenchMenu* menu, |
| 385 | MenuModel* menu_model, |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 386 | int decrement_index, |
[email protected] | e1a02d6 | 2010-07-16 16:27:41 | [diff] [blame] | 387 | int increment_index, |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 388 | int fullscreen_index) |
[email protected] | 2cc800b | 2010-10-01 18:27:34 | [diff] [blame] | 389 | : WrenchMenuView(menu, menu_model), |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 390 | fullscreen_index_(fullscreen_index), |
| 391 | increment_button_(NULL), |
| 392 | zoom_label_(NULL), |
| 393 | decrement_button_(NULL), |
| 394 | fullscreen_button_(NULL), |
| 395 | zoom_label_width_(0) { |
[email protected] | 2cc800b | 2010-10-01 18:27:34 | [diff] [blame] | 396 | decrement_button_ = CreateButtonWithAccName( |
| 397 | IDS_ZOOM_MINUS2, MenuButtonBackground::LEFT_BUTTON, decrement_index, |
| 398 | NULL, IDS_ACCNAME_ZOOM_MINUS2); |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 399 | |
| 400 | zoom_label_ = new Label(l10n_util::GetStringF(IDS_ZOOM_PERCENT, L"100")); |
| 401 | zoom_label_->SetColor(MenuConfig::instance().text_color); |
| 402 | zoom_label_->SetHorizontalAlignment(Label::ALIGN_RIGHT); |
| 403 | MenuButtonBackground* center_bg = |
| 404 | new MenuButtonBackground(MenuButtonBackground::CENTER_BUTTON); |
| 405 | zoom_label_->set_background(center_bg); |
| 406 | zoom_label_->set_border(new MenuButtonBorder()); |
| 407 | zoom_label_->SetFont(MenuConfig::instance().font); |
| 408 | AddChildView(zoom_label_); |
[email protected] | 84e4f8c | 2010-07-15 02:18:32 | [diff] [blame] | 409 | zoom_label_width_ = MaxWidthForZoomLabel(); |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 410 | |
[email protected] | 2cc800b | 2010-10-01 18:27:34 | [diff] [blame] | 411 | increment_button_ = CreateButtonWithAccName( |
| 412 | IDS_ZOOM_PLUS2, MenuButtonBackground::RIGHT_BUTTON, increment_index, |
| 413 | NULL, IDS_ACCNAME_ZOOM_PLUS2); |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 414 | |
[email protected] | 5214bdd | 2010-08-02 21:43:47 | [diff] [blame] | 415 | center_bg->SetOtherButtons(decrement_button_, increment_button_); |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 416 | |
| 417 | fullscreen_button_ = new FullscreenButton(this); |
| 418 | fullscreen_button_->SetImage( |
| 419 | ImageButton::BS_NORMAL, |
| 420 | ResourceBundle::GetSharedInstance().GetBitmapNamed( |
| 421 | IDR_FULLSCREEN_MENU_BUTTON)); |
| 422 | fullscreen_button_->SetFocusable(true); |
| 423 | fullscreen_button_->set_request_focus_on_press(false); |
| 424 | fullscreen_button_->set_tag(fullscreen_index); |
| 425 | fullscreen_button_->SetImageAlignment( |
| 426 | ImageButton::ALIGN_CENTER, ImageButton::ALIGN_MIDDLE); |
| 427 | fullscreen_button_->set_border(views::Border::CreateEmptyBorder( |
| 428 | 0, kHorizontalPadding, 0, kHorizontalPadding)); |
| 429 | fullscreen_button_->set_background( |
| 430 | new MenuButtonBackground(MenuButtonBackground::SINGLE_BUTTON)); |
[email protected] | 955c2f7f | 2010-08-03 19:07:35 | [diff] [blame] | 431 | fullscreen_button_->SetAccessibleName( |
[email protected] | 2cc800b | 2010-10-01 18:27:34 | [diff] [blame] | 432 | GetAccessibleNameForWrenchMenuItem( |
| 433 | menu_model, fullscreen_index, IDS_ACCNAME_FULLSCREEN)); |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 434 | AddChildView(fullscreen_button_); |
| 435 | |
| 436 | UpdateZoomControls(); |
| 437 | |
| 438 | registrar_.Add(this, NotificationType::ZOOM_LEVEL_CHANGED, |
| 439 | Source<Profile>(menu->browser_->profile())); |
| 440 | } |
| 441 | |
| 442 | gfx::Size GetPreferredSize() { |
| 443 | // The increment/decrement button are forced to the same width. |
| 444 | int button_width = std::max(increment_button_->GetPreferredSize().width(), |
| 445 | decrement_button_->GetPreferredSize().width()); |
| 446 | int fullscreen_width = fullscreen_button_->GetPreferredSize().width(); |
| 447 | // Returned height doesn't matter as MenuItemView forces everything to the |
| 448 | // height of the menuitemview. |
| 449 | return gfx::Size(button_width + zoom_label_width_ + button_width + |
| 450 | kZoomPadding + fullscreen_width, 0); |
| 451 | } |
| 452 | |
| 453 | void Layout() { |
| 454 | int x = 0; |
| 455 | int button_width = std::max(increment_button_->GetPreferredSize().width(), |
| 456 | decrement_button_->GetPreferredSize().width()); |
| 457 | gfx::Rect bounds(0, 0, button_width, height()); |
| 458 | |
[email protected] | e1a02d6 | 2010-07-16 16:27:41 | [diff] [blame] | 459 | decrement_button_->SetBounds(bounds); |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 460 | |
| 461 | x += bounds.width(); |
| 462 | bounds.set_x(x); |
| 463 | bounds.set_width(zoom_label_width_); |
| 464 | zoom_label_->SetBounds(bounds); |
| 465 | |
| 466 | x += bounds.width(); |
| 467 | bounds.set_x(x); |
| 468 | bounds.set_width(button_width); |
[email protected] | e1a02d6 | 2010-07-16 16:27:41 | [diff] [blame] | 469 | increment_button_->SetBounds(bounds); |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 470 | |
| 471 | x += bounds.width() + kZoomPadding; |
| 472 | bounds.set_x(x); |
| 473 | bounds.set_width(fullscreen_button_->GetPreferredSize().width()); |
| 474 | fullscreen_button_->SetBounds(bounds); |
| 475 | } |
| 476 | |
| 477 | // ButtonListener: |
| 478 | virtual void ButtonPressed(views::Button* sender, const views::Event& event) { |
| 479 | if (sender->tag() == fullscreen_index_) { |
| 480 | menu_->CancelAndEvaluate(menu_model_, sender->tag()); |
| 481 | } else { |
| 482 | // Zoom buttons don't close the menu. |
| 483 | menu_model_->ActivatedAt(sender->tag()); |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | // NotificationObserver: |
| 488 | virtual void Observe(NotificationType type, |
| 489 | const NotificationSource& source, |
| 490 | const NotificationDetails& details) { |
| 491 | DCHECK_EQ(NotificationType::ZOOM_LEVEL_CHANGED, type.value); |
| 492 | UpdateZoomControls(); |
| 493 | } |
| 494 | |
| 495 | private: |
| 496 | void UpdateZoomControls() { |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 497 | bool enable_increment = false; |
| 498 | bool enable_decrement = false; |
| 499 | TabContents* selected_tab = menu_->browser_->GetSelectedTabContents(); |
| 500 | int zoom = 100; |
| 501 | if (selected_tab) |
| 502 | zoom = selected_tab->GetZoomPercent(&enable_increment, &enable_decrement); |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 503 | increment_button_->SetEnabled(enable_increment); |
| 504 | decrement_button_->SetEnabled(enable_decrement); |
[email protected] | 7e1f2dec | 2010-07-21 18:31:43 | [diff] [blame] | 505 | zoom_label_->SetText(l10n_util::GetStringF( |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 506 | IDS_ZOOM_PERCENT, |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 507 | UTF8ToWide(base::IntToString(zoom)))); |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 508 | |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 509 | zoom_label_width_ = MaxWidthForZoomLabel(); |
[email protected] | 84e4f8c | 2010-07-15 02:18:32 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | // Calculates the max width the zoom string can be. |
| 513 | int MaxWidthForZoomLabel() { |
| 514 | gfx::Font font = zoom_label_->font(); |
| 515 | gfx::Insets insets; |
| 516 | if (zoom_label_->border()) |
| 517 | zoom_label_->border()->GetInsets(&insets); |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 518 | |
[email protected] | 84e4f8c | 2010-07-15 02:18:32 | [diff] [blame] | 519 | int max_w = 0; |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 520 | |
| 521 | TabContents* selected_tab = menu_->browser_->GetSelectedTabContents(); |
| 522 | if (selected_tab) { |
| 523 | int min_percent = selected_tab->minimum_zoom_percent(); |
| 524 | int max_percent = selected_tab->maximum_zoom_percent(); |
| 525 | |
| 526 | int step = (max_percent - min_percent) / 10; |
| 527 | for (int i = min_percent; i <= max_percent; i += step) { |
| 528 | int w = font.GetStringWidth(l10n_util::GetStringF(IDS_ZOOM_PERCENT, i)); |
| 529 | max_w = std::max(w, max_w); |
| 530 | } |
| 531 | } else { |
[email protected] | e06bea6 | 2010-10-01 07:41:11 | [diff] [blame] | 532 | max_w = font.GetStringWidth(l10n_util::GetStringF(IDS_ZOOM_PERCENT, 100)); |
[email protected] | 84e4f8c | 2010-07-15 02:18:32 | [diff] [blame] | 533 | } |
[email protected] | b75b829 | 2010-10-01 07:28:25 | [diff] [blame] | 534 | |
[email protected] | 84e4f8c | 2010-07-15 02:18:32 | [diff] [blame] | 535 | return max_w + insets.width(); |
| 536 | } |
| 537 | |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 538 | // Index of the fullscreen menu item in the model. |
| 539 | const int fullscreen_index_; |
| 540 | |
| 541 | NotificationRegistrar registrar_; |
| 542 | |
| 543 | // Button for incrementing the zoom. |
| 544 | TextButton* increment_button_; |
| 545 | |
| 546 | // Label showing zoom as a percent. |
| 547 | Label* zoom_label_; |
| 548 | |
| 549 | // Button for decrementing the zoom. |
| 550 | TextButton* decrement_button_; |
| 551 | |
| 552 | ImageButton* fullscreen_button_; |
| 553 | |
| 554 | // Width given to |zoom_label_|. This is the width at 100%. |
| 555 | int zoom_label_width_; |
| 556 | |
| 557 | DISALLOW_COPY_AND_ASSIGN(ZoomView); |
| 558 | }; |
| 559 | |
| 560 | // WrenchMenu ------------------------------------------------------------------ |
| 561 | |
| 562 | WrenchMenu::WrenchMenu(Browser* browser) |
| 563 | : browser_(browser), |
| 564 | selected_menu_model_(NULL), |
| 565 | selected_index_(0) { |
| 566 | } |
| 567 | |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 568 | void WrenchMenu::Init(menus::MenuModel* model) { |
| 569 | DCHECK(!root_.get()); |
| 570 | root_.reset(new MenuItemView(this)); |
[email protected] | e12e3f6 | 2010-08-06 00:44:59 | [diff] [blame] | 571 | root_->SetAccessibleName(l10n_util::GetString(IDS_ACCNAME_APP)); |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 572 | root_->set_has_icons(true); // We have checks, radios and icons, set this |
| 573 | // so we get the taller menu style. |
| 574 | int next_id = 1; |
| 575 | PopulateMenu(root_.get(), model, &next_id); |
| 576 | } |
| 577 | |
[email protected] | a00b032 | 2010-06-25 16:21:32 | [diff] [blame] | 578 | void WrenchMenu::RunMenu(views::MenuButton* host) { |
[email protected] | 428f54b | 2010-10-05 03:31:27 | [diff] [blame] | 579 | // Up the ref count while the menu is displaying. This way if the window is |
| 580 | // deleted while we're running we won't prematurely delete the menu. |
| 581 | // TODO(sky): fix this, the menu should really take ownership of the menu |
| 582 | // (57890). |
| 583 | scoped_refptr<WrenchMenu> dont_delete_while_running(this); |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 584 | gfx::Point screen_loc; |
| 585 | views::View::ConvertPointToScreen(host, &screen_loc); |
[email protected] | e78f12a2 | 2010-07-28 22:41:05 | [diff] [blame] | 586 | gfx::Rect bounds(screen_loc, host->size()); |
[email protected] | a00b032 | 2010-06-25 16:21:32 | [diff] [blame] | 587 | root_->RunMenuAt(host->GetWindow()->GetNativeWindow(), host, bounds, |
[email protected] | 627da3f | 2010-07-27 21:19:01 | [diff] [blame] | 588 | base::i18n::IsRTL() ? MenuItemView::TOPLEFT : MenuItemView::TOPRIGHT, |
| 589 | true); |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 590 | if (selected_menu_model_) |
| 591 | selected_menu_model_->ActivatedAt(selected_index_); |
| 592 | } |
| 593 | |
| 594 | bool WrenchMenu::IsItemChecked(int id) const { |
| 595 | const Entry& entry = id_to_entry_.find(id)->second; |
| 596 | return entry.first->IsItemCheckedAt(entry.second); |
| 597 | } |
| 598 | |
| 599 | bool WrenchMenu::IsCommandEnabled(int id) const { |
| 600 | if (id == 0) |
| 601 | return false; // The root item. |
| 602 | |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 603 | const Entry& entry = id_to_entry_.find(id)->second; |
| 604 | int command_id = entry.first->GetCommandIdAt(entry.second); |
| 605 | // The items representing the cut (cut/copy/paste) and zoom menu |
| 606 | // (increment/decrement/reset) are always enabled. The child views of these |
| 607 | // items enabled state updates appropriately. |
[email protected] | e1a02d6 | 2010-07-16 16:27:41 | [diff] [blame] | 608 | return command_id == IDC_CUT || command_id == IDC_ZOOM_MINUS || |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 609 | entry.first->IsEnabledAt(entry.second); |
| 610 | } |
| 611 | |
| 612 | void WrenchMenu::ExecuteCommand(int id) { |
| 613 | const Entry& entry = id_to_entry_.find(id)->second; |
[email protected] | 60555ec | 2010-07-02 20:18:25 | [diff] [blame] | 614 | int command_id = entry.first->GetCommandIdAt(entry.second); |
| 615 | |
[email protected] | e1a02d6 | 2010-07-16 16:27:41 | [diff] [blame] | 616 | if (command_id == IDC_CUT || command_id == IDC_ZOOM_MINUS) { |
[email protected] | 60555ec | 2010-07-02 20:18:25 | [diff] [blame] | 617 | // These items are represented by child views. If ExecuteCommand is invoked |
| 618 | // it means the user clicked on the area around the buttons and we should |
| 619 | // not do anyting. |
| 620 | return; |
| 621 | } |
| 622 | |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 623 | return entry.first->ActivatedAt(entry.second); |
| 624 | } |
| 625 | |
| 626 | bool WrenchMenu::GetAccelerator(int id, views::Accelerator* accelerator) { |
| 627 | const Entry& entry = id_to_entry_.find(id)->second; |
| 628 | int command_id = entry.first->GetCommandIdAt(entry.second); |
[email protected] | e1a02d6 | 2010-07-16 16:27:41 | [diff] [blame] | 629 | if (command_id == IDC_CUT || command_id == IDC_ZOOM_MINUS) { |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 630 | // These have special child views; don't show the accelerator for them. |
| 631 | return false; |
| 632 | } |
| 633 | |
| 634 | menus::Accelerator menu_accelerator; |
| 635 | if (!entry.first->GetAcceleratorAt(entry.second, &menu_accelerator)) |
| 636 | return false; |
| 637 | |
| 638 | *accelerator = views::Accelerator(menu_accelerator.GetKeyCode(), |
| 639 | menu_accelerator.modifiers()); |
| 640 | return true; |
| 641 | } |
| 642 | |
[email protected] | 428f54b | 2010-10-05 03:31:27 | [diff] [blame] | 643 | WrenchMenu::~WrenchMenu() { |
| 644 | } |
| 645 | |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 646 | void WrenchMenu::PopulateMenu(MenuItemView* parent, |
| 647 | MenuModel* model, |
| 648 | int* next_id) { |
| 649 | int index_offset = model->GetFirstItemIndex(NULL); |
| 650 | for (int i = 0, max = model->GetItemCount(); i < max; ++i) { |
| 651 | int index = i + index_offset; |
| 652 | |
| 653 | MenuItemView* item = |
| 654 | AppendMenuItem(parent, model, index, model->GetTypeAt(index), next_id); |
| 655 | |
| 656 | if (model->GetTypeAt(index) == MenuModel::TYPE_SUBMENU) |
| 657 | PopulateMenu(item, model->GetSubmenuModelAt(index), next_id); |
| 658 | |
| 659 | if (model->GetCommandIdAt(index) == IDC_CUT) { |
| 660 | DCHECK_EQ(MenuModel::TYPE_COMMAND, model->GetTypeAt(index)); |
| 661 | DCHECK_LT(i + 2, max); |
| 662 | DCHECK_EQ(IDC_COPY, model->GetCommandIdAt(index + 1)); |
| 663 | DCHECK_EQ(IDC_PASTE, model->GetCommandIdAt(index + 2)); |
| 664 | item->SetTitle(l10n_util::GetString(IDS_EDIT2)); |
| 665 | item->AddChildView( |
| 666 | new CutCopyPasteView(this, model, index, index + 1, index + 2)); |
| 667 | i += 2; |
[email protected] | e1a02d6 | 2010-07-16 16:27:41 | [diff] [blame] | 668 | } else if (model->GetCommandIdAt(index) == IDC_ZOOM_MINUS) { |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 669 | DCHECK_EQ(MenuModel::TYPE_COMMAND, model->GetTypeAt(index)); |
[email protected] | e1a02d6 | 2010-07-16 16:27:41 | [diff] [blame] | 670 | DCHECK_EQ(IDC_ZOOM_PLUS, model->GetCommandIdAt(index + 1)); |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 671 | DCHECK_EQ(IDC_FULLSCREEN, model->GetCommandIdAt(index + 2)); |
| 672 | item->SetTitle(l10n_util::GetString(IDS_ZOOM_MENU2)); |
| 673 | item->AddChildView( |
| 674 | new ZoomView(this, model, index, index + 1, index + 2)); |
| 675 | i += 2; |
| 676 | } |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | MenuItemView* WrenchMenu::AppendMenuItem(MenuItemView* parent, |
| 681 | MenuModel* model, |
| 682 | int index, |
| 683 | MenuModel::ItemType menu_type, |
| 684 | int* next_id) { |
| 685 | int id = (*next_id)++; |
| 686 | SkBitmap icon; |
| 687 | std::wstring label; |
| 688 | MenuItemView::Type type; |
| 689 | switch (menu_type) { |
| 690 | case MenuModel::TYPE_COMMAND: |
| 691 | model->GetIconAt(index, &icon); |
| 692 | type = MenuItemView::NORMAL; |
| 693 | label = UTF16ToWide(model->GetLabelAt(index)); |
| 694 | break; |
| 695 | case MenuModel::TYPE_CHECK: |
| 696 | type = MenuItemView::CHECKBOX; |
| 697 | label = UTF16ToWide(model->GetLabelAt(index)); |
| 698 | break; |
| 699 | case MenuModel::TYPE_RADIO: |
| 700 | type = MenuItemView::RADIO; |
| 701 | label = UTF16ToWide(model->GetLabelAt(index)); |
| 702 | break; |
| 703 | case MenuModel::TYPE_SEPARATOR: |
| 704 | type = MenuItemView::SEPARATOR; |
| 705 | break; |
| 706 | case MenuModel::TYPE_SUBMENU: |
| 707 | type = MenuItemView::SUBMENU; |
| 708 | label = UTF16ToWide(model->GetLabelAt(index)); |
| 709 | break; |
| 710 | default: |
| 711 | NOTREACHED(); |
| 712 | type = MenuItemView::NORMAL; |
| 713 | break; |
| 714 | } |
| 715 | |
| 716 | id_to_entry_[id].first = model; |
| 717 | id_to_entry_[id].second = index; |
| 718 | |
| 719 | MenuItemView* menu_item = parent->AppendMenuItemImpl(id, label, icon, type); |
| 720 | |
[email protected] | d4c4f25 | 2010-09-01 19:27:46 | [diff] [blame] | 721 | if (menu_item) |
| 722 | menu_item->SetVisible(model->IsVisibleAt(index)); |
| 723 | |
[email protected] | eccda9db | 2010-06-23 17:27:08 | [diff] [blame] | 724 | if (menu_type == MenuModel::TYPE_COMMAND && model->HasIcons()) { |
| 725 | SkBitmap icon; |
| 726 | if (model->GetIconAt(index, &icon)) |
| 727 | menu_item->SetIcon(icon); |
| 728 | } |
| 729 | |
| 730 | return menu_item; |
| 731 | } |
| 732 | |
| 733 | void WrenchMenu::CancelAndEvaluate(MenuModel* model, int index) { |
| 734 | selected_menu_model_ = model; |
| 735 | selected_index_ = index; |
| 736 | root_->Cancel(); |
| 737 | } |