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