| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "views/controls/menu/menu_item_view.h" | 5 #include "views/controls/menu/menu_item_view.h" |
| 6 | 6 |
| 7 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
| 8 #include "gfx/canvas.h" | 8 #include "gfx/canvas.h" |
| 9 #include "grit/app_strings.h" | 9 #include "grit/app_strings.h" |
| 10 #include "views/controls/button/text_button.h" |
| 10 #include "views/controls/menu/menu_config.h" | 11 #include "views/controls/menu/menu_config.h" |
| 11 #include "views/controls/menu/menu_controller.h" | 12 #include "views/controls/menu/menu_controller.h" |
| 12 #include "views/controls/menu/menu_separator.h" | 13 #include "views/controls/menu/menu_separator.h" |
| 13 #include "views/controls/menu/submenu_view.h" | 14 #include "views/controls/menu/submenu_view.h" |
| 14 | 15 |
| 15 namespace views { | 16 namespace views { |
| 16 | 17 |
| 17 namespace { | 18 namespace { |
| 18 | 19 |
| 19 // EmptyMenuMenuItem --------------------------------------------------------- | 20 // EmptyMenuMenuItem --------------------------------------------------------- |
| 20 | 21 |
| 21 // EmptyMenuMenuItem is used when a menu has no menu items. EmptyMenuMenuItem | 22 // EmptyMenuMenuItem is used when a menu has no menu items. EmptyMenuMenuItem |
| 22 // is itself a MenuItemView, but it uses a different ID so that it isn't | 23 // is itself a MenuItemView, but it uses a different ID so that it isn't |
| 23 // identified as a MenuItemView. | 24 // identified as a MenuItemView. |
| 24 | 25 |
| 25 class EmptyMenuMenuItem : public MenuItemView { | 26 class EmptyMenuMenuItem : public MenuItemView { |
| 26 public: | 27 public: |
| 27 explicit EmptyMenuMenuItem(MenuItemView* parent) : | 28 explicit EmptyMenuMenuItem(MenuItemView* parent) |
| 28 MenuItemView(parent, 0, NORMAL) { | 29 : MenuItemView(parent, 0, NORMAL) { |
| 29 SetTitle(l10n_util::GetString(IDS_APP_MENU_EMPTY_SUBMENU)); | 30 SetTitle(l10n_util::GetString(IDS_APP_MENU_EMPTY_SUBMENU)); |
| 30 // Set this so that we're not identified as a normal menu item. | 31 // Set this so that we're not identified as a normal menu item. |
| 31 SetID(kEmptyMenuItemViewID); | 32 SetID(kEmptyMenuItemViewID); |
| 32 SetEnabled(false); | 33 SetEnabled(false); |
| 33 } | 34 } |
| 34 | 35 |
| 35 private: | 36 private: |
| 36 DISALLOW_COPY_AND_ASSIGN(EmptyMenuMenuItem); | 37 DISALLOW_COPY_AND_ASSIGN(EmptyMenuMenuItem); |
| 37 }; | 38 }; |
| 38 | 39 |
| (...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 305 // Child views are layed out right aligned and given the full height. To right | 306 // Child views are layed out right aligned and given the full height. To right |
| 306 // align start with the last view and progress to the first. | 307 // align start with the last view and progress to the first. |
| 307 for (int i = child_count - 1, x = width() - item_right_margin_; i >= 0; --i) { | 308 for (int i = child_count - 1, x = width() - item_right_margin_; i >= 0; --i) { |
| 308 View* child = GetChildViewAt(i); | 309 View* child = GetChildViewAt(i); |
| 309 int width = child->GetPreferredSize().width(); | 310 int width = child->GetPreferredSize().width(); |
| 310 child->SetBounds(x - width, 0, width, height()); | 311 child->SetBounds(x - width, 0, width, height()); |
| 311 x -= width - kChildXPadding; | 312 x -= width - kChildXPadding; |
| 312 } | 313 } |
| 313 } | 314 } |
| 314 | 315 |
| 316 int MenuItemView::GetAcceleratorTextWidth() { |
| 317 std::wstring text = GetAcceleratorText(); |
| 318 return text.empty() ? 0 : MenuConfig::instance().font.GetStringWidth(text); |
| 319 } |
| 320 |
| 315 MenuItemView::MenuItemView(MenuItemView* parent, | 321 MenuItemView::MenuItemView(MenuItemView* parent, |
| 316 int command, | 322 int command, |
| 317 MenuItemView::Type type) { | 323 MenuItemView::Type type) { |
| 318 Init(parent, command, type, NULL); | 324 Init(parent, command, type, NULL); |
| 319 } | 325 } |
| 320 | 326 |
| 321 // Calculates all sizes that we can from the OS. | 327 // Calculates all sizes that we can from the OS. |
| 322 // | 328 // |
| 323 // This is invoked prior to Running a menu. | 329 // This is invoked prior to Running a menu. |
| 324 void MenuItemView::UpdateMenuPartSizes(bool has_icons) { | 330 void MenuItemView::UpdateMenuPartSizes(bool has_icons) { |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 445 } else if (child->GetID() == EmptyMenuMenuItem::kEmptyMenuItemViewID) { | 451 } else if (child->GetID() == EmptyMenuMenuItem::kEmptyMenuItemViewID) { |
| 446 submenu_->RemoveChildView(child); | 452 submenu_->RemoveChildView(child); |
| 447 } | 453 } |
| 448 } | 454 } |
| 449 } | 455 } |
| 450 | 456 |
| 451 void MenuItemView::AdjustBoundsForRTLUI(gfx::Rect* rect) const { | 457 void MenuItemView::AdjustBoundsForRTLUI(gfx::Rect* rect) const { |
| 452 rect->set_x(MirroredLeftPointForRect(*rect)); | 458 rect->set_x(MirroredLeftPointForRect(*rect)); |
| 453 } | 459 } |
| 454 | 460 |
| 461 void MenuItemView::PaintAccelerator(gfx::Canvas* canvas) { |
| 462 std::wstring accel_text = GetAcceleratorText(); |
| 463 if (accel_text.empty()) |
| 464 return; |
| 465 |
| 466 const gfx::Font& font = MenuConfig::instance().font; |
| 467 int available_height = height() - GetTopMargin() - GetBottomMargin(); |
| 468 int max_accel_width = |
| 469 parent_menu_item_->GetSubmenu()->max_accelerator_width(); |
| 470 gfx::Rect accel_bounds(width() - item_right_margin_ - max_accel_width, |
| 471 GetTopMargin(), max_accel_width, available_height); |
| 472 accel_bounds.set_x(MirroredLeftPointForRect(accel_bounds)); |
| 473 int flags = GetRootMenuItem()->GetDrawStringFlags() | |
| 474 gfx::Canvas::TEXT_VALIGN_MIDDLE; |
| 475 flags &= ~(gfx::Canvas::TEXT_ALIGN_RIGHT | gfx::Canvas::TEXT_ALIGN_LEFT); |
| 476 if (base::i18n::IsRTL()) |
| 477 flags |= gfx::Canvas::TEXT_ALIGN_LEFT; |
| 478 else |
| 479 flags |= gfx::Canvas::TEXT_ALIGN_RIGHT; |
| 480 canvas->DrawStringInt( |
| 481 accel_text, font, TextButton::kDisabledColor, accel_bounds.x(), |
| 482 accel_bounds.y(), accel_bounds.width(), accel_bounds.height(), |
| 483 flags); |
| 484 } |
| 485 |
| 455 void MenuItemView::DestroyAllMenuHosts() { | 486 void MenuItemView::DestroyAllMenuHosts() { |
| 456 if (!HasSubmenu()) | 487 if (!HasSubmenu()) |
| 457 return; | 488 return; |
| 458 | 489 |
| 459 submenu_->Close(); | 490 submenu_->Close(); |
| 460 for (int i = 0, item_count = submenu_->GetMenuItemCount(); i < item_count; | 491 for (int i = 0, item_count = submenu_->GetMenuItemCount(); i < item_count; |
| 461 ++i) { | 492 ++i) { |
| 462 submenu_->GetMenuItemAt(i)->DestroyAllMenuHosts(); | 493 submenu_->GetMenuItemAt(i)->DestroyAllMenuHosts(); |
| 463 } | 494 } |
| 464 } | 495 } |
| (...skipping 19 matching lines...) Expand all Loading... |
| 484 | 515 |
| 485 int width = 0; | 516 int width = 0; |
| 486 for (int i = 0; i < child_count; ++i) { | 517 for (int i = 0; i < child_count; ++i) { |
| 487 if (i) | 518 if (i) |
| 488 width += kChildXPadding; | 519 width += kChildXPadding; |
| 489 width += GetChildViewAt(i)->GetPreferredSize().width(); | 520 width += GetChildViewAt(i)->GetPreferredSize().width(); |
| 490 } | 521 } |
| 491 return width; | 522 return width; |
| 492 } | 523 } |
| 493 | 524 |
| 525 std::wstring MenuItemView::GetAcceleratorText() { |
| 526 Accelerator accelerator; |
| 527 return (GetDelegate() && |
| 528 GetDelegate()->GetAccelerator(GetCommand(), &accelerator)) ? |
| 529 accelerator.GetShortcutText() : std::wstring(); |
| 530 } |
| 531 |
| 494 } // namespace views | 532 } // namespace views |
| OLD | NEW |