| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/i18n/case_conversion.h" | 7 #include "base/i18n/case_conversion.h" |
| 8 #include "base/stl_util-inl.h" | 8 #include "base/stl_util-inl.h" |
| 9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
| 10 #include "grit/ui_strings.h" | 10 #include "grit/ui_strings.h" |
| (...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 282 item->CreateSubmenu(); | 282 item->CreateSubmenu(); |
| 283 submenu_->AddChildViewAt(item, index); | 283 submenu_->AddChildViewAt(item, index); |
| 284 return item; | 284 return item; |
| 285 } | 285 } |
| 286 | 286 |
| 287 void MenuItemView::RemoveMenuItemAt(int index) { | 287 void MenuItemView::RemoveMenuItemAt(int index) { |
| 288 DCHECK(submenu_); | 288 DCHECK(submenu_); |
| 289 DCHECK_LE(0, index); | 289 DCHECK_LE(0, index); |
| 290 DCHECK_GT(submenu_->child_count(), index); | 290 DCHECK_GT(submenu_->child_count(), index); |
| 291 | 291 |
| 292 View* item = submenu_->GetChildViewAt(index); | 292 View* item = submenu_->child_at(index); |
| 293 DCHECK(item); | 293 DCHECK(item); |
| 294 submenu_->RemoveChildView(item); | 294 submenu_->RemoveChildView(item); |
| 295 | 295 |
| 296 // RemoveChildView() does not delete the item, which is a good thing | 296 // RemoveChildView() does not delete the item, which is a good thing |
| 297 // in case a submenu is being displayed while items are being removed. | 297 // in case a submenu is being displayed while items are being removed. |
| 298 // Deletion will be done by ChildrenChanged() or at destruction. | 298 // Deletion will be done by ChildrenChanged() or at destruction. |
| 299 removed_items_.push_back(item); | 299 removed_items_.push_back(item); |
| 300 } | 300 } |
| 301 | 301 |
| 302 MenuItemView* MenuItemView::AppendMenuItemFromModel(ui::MenuModel* model, | 302 MenuItemView* MenuItemView::AppendMenuItemFromModel(ui::MenuModel* model, |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 439 } while (index != string16::npos); | 439 } while (index != string16::npos); |
| 440 return 0; | 440 return 0; |
| 441 } | 441 } |
| 442 | 442 |
| 443 MenuItemView* MenuItemView::GetMenuItemByID(int id) { | 443 MenuItemView* MenuItemView::GetMenuItemByID(int id) { |
| 444 if (GetCommand() == id) | 444 if (GetCommand() == id) |
| 445 return this; | 445 return this; |
| 446 if (!HasSubmenu()) | 446 if (!HasSubmenu()) |
| 447 return NULL; | 447 return NULL; |
| 448 for (int i = 0; i < GetSubmenu()->child_count(); ++i) { | 448 for (int i = 0; i < GetSubmenu()->child_count(); ++i) { |
| 449 View* child = GetSubmenu()->GetChildViewAt(i); | 449 View* child = GetSubmenu()->child_at(i); |
| 450 if (child->id() == MenuItemView::kMenuItemViewID) { | 450 if (child->id() == MenuItemView::kMenuItemViewID) { |
| 451 MenuItemView* result = static_cast<MenuItemView*>(child)-> | 451 MenuItemView* result = static_cast<MenuItemView*>(child)-> |
| 452 GetMenuItemByID(id); | 452 GetMenuItemByID(id); |
| 453 if (result) | 453 if (result) |
| 454 return result; | 454 return result; |
| 455 } | 455 } |
| 456 } | 456 } |
| 457 return NULL; | 457 return NULL; |
| 458 } | 458 } |
| 459 | 459 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 480 STLDeleteElements(&removed_items_); | 480 STLDeleteElements(&removed_items_); |
| 481 } | 481 } |
| 482 | 482 |
| 483 void MenuItemView::Layout() { | 483 void MenuItemView::Layout() { |
| 484 if (!has_children()) | 484 if (!has_children()) |
| 485 return; | 485 return; |
| 486 | 486 |
| 487 if (child_count() == 1 && GetTitle().size() == 0) { | 487 if (child_count() == 1 && GetTitle().size() == 0) { |
| 488 // We only have one child and no title so let the view take over all the | 488 // We only have one child and no title so let the view take over all the |
| 489 // space. | 489 // space. |
| 490 View* child = GetChildViewAt(0); | 490 View* child = child_at(0); |
| 491 gfx::Size size = child->GetPreferredSize(); | 491 gfx::Size size = child->GetPreferredSize(); |
| 492 child->SetBounds(label_start_, GetTopMargin(), size.width(), size.height()); | 492 child->SetBounds(label_start_, GetTopMargin(), size.width(), size.height()); |
| 493 } else { | 493 } else { |
| 494 // Child views are laid out right aligned and given the full height. To | 494 // Child views are laid out right aligned and given the full height. To |
| 495 // right align start with the last view and progress to the first. | 495 // right align start with the last view and progress to the first. |
| 496 for (int i = child_count() - 1, x = width() - item_right_margin_; i >= 0; | 496 for (int i = child_count() - 1, x = width() - item_right_margin_; i >= 0; |
| 497 --i) { | 497 --i) { |
| 498 View* child = GetChildViewAt(i); | 498 View* child = child_at(i); |
| 499 int width = child->GetPreferredSize().width(); | 499 int width = child->GetPreferredSize().width(); |
| 500 child->SetBounds(x - width, 0, width, height()); | 500 child->SetBounds(x - width, 0, width, height()); |
| 501 x -= width - kChildXPadding; | 501 x -= width - kChildXPadding; |
| 502 } | 502 } |
| 503 } | 503 } |
| 504 } | 504 } |
| 505 | 505 |
| 506 int MenuItemView::GetAcceleratorTextWidth() { | 506 int MenuItemView::GetAcceleratorTextWidth() { |
| 507 string16 text = GetAcceleratorText(); | 507 string16 text = GetAcceleratorText(); |
| 508 return text.empty() ? 0 : GetFont().GetStringWidth(text); | 508 return text.empty() ? 0 : GetFont().GetStringWidth(text); |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 660 child->AddEmptyMenus(); | 660 child->AddEmptyMenus(); |
| 661 } | 661 } |
| 662 } | 662 } |
| 663 } | 663 } |
| 664 | 664 |
| 665 void MenuItemView::RemoveEmptyMenus() { | 665 void MenuItemView::RemoveEmptyMenus() { |
| 666 DCHECK(HasSubmenu()); | 666 DCHECK(HasSubmenu()); |
| 667 // Iterate backwards as we may end up removing views, which alters the child | 667 // Iterate backwards as we may end up removing views, which alters the child |
| 668 // view count. | 668 // view count. |
| 669 for (int i = submenu_->child_count() - 1; i >= 0; --i) { | 669 for (int i = submenu_->child_count() - 1; i >= 0; --i) { |
| 670 View* child = submenu_->GetChildViewAt(i); | 670 View* child = submenu_->child_at(i); |
| 671 if (child->id() == MenuItemView::kMenuItemViewID) { | 671 if (child->id() == MenuItemView::kMenuItemViewID) { |
| 672 MenuItemView* menu_item = static_cast<MenuItemView*>(child); | 672 MenuItemView* menu_item = static_cast<MenuItemView*>(child); |
| 673 if (menu_item->HasSubmenu()) | 673 if (menu_item->HasSubmenu()) |
| 674 menu_item->RemoveEmptyMenus(); | 674 menu_item->RemoveEmptyMenus(); |
| 675 } else if (child->id() == EmptyMenuMenuItem::kEmptyMenuItemViewID) { | 675 } else if (child->id() == EmptyMenuMenuItem::kEmptyMenuItemViewID) { |
| 676 submenu_->RemoveChildView(child); | 676 submenu_->RemoveChildView(child); |
| 677 delete child; | 677 delete child; |
| 678 child = NULL; | 678 child = NULL; |
| 679 } | 679 } |
| 680 } | 680 } |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 738 return root && root->has_icons_ | 738 return root && root->has_icons_ |
| 739 ? MenuConfig::instance().item_bottom_margin : | 739 ? MenuConfig::instance().item_bottom_margin : |
| 740 MenuConfig::instance().item_no_icon_bottom_margin; | 740 MenuConfig::instance().item_no_icon_bottom_margin; |
| 741 } | 741 } |
| 742 | 742 |
| 743 gfx::Size MenuItemView::GetChildPreferredSize() { | 743 gfx::Size MenuItemView::GetChildPreferredSize() { |
| 744 if (!has_children()) | 744 if (!has_children()) |
| 745 return gfx::Size(); | 745 return gfx::Size(); |
| 746 | 746 |
| 747 if (GetTitle().size() == 0 && child_count() == 1) { | 747 if (GetTitle().size() == 0 && child_count() == 1) { |
| 748 View* child = GetChildViewAt(0); | 748 View* child = child_at(0); |
| 749 return child->GetPreferredSize(); | 749 return child->GetPreferredSize(); |
| 750 } | 750 } |
| 751 | 751 |
| 752 int width = 0; | 752 int width = 0; |
| 753 for (int i = 0; i < child_count(); ++i) { | 753 for (int i = 0; i < child_count(); ++i) { |
| 754 if (i) | 754 if (i) |
| 755 width += kChildXPadding; | 755 width += kChildXPadding; |
| 756 width += GetChildViewAt(i)->GetPreferredSize().width(); | 756 width += child_at(i)->GetPreferredSize().width(); |
| 757 } | 757 } |
| 758 // Return a height of 0 to indicate that we should use the title height | 758 // Return a height of 0 to indicate that we should use the title height |
| 759 // instead. | 759 // instead. |
| 760 return gfx::Size(width, 0); | 760 return gfx::Size(width, 0); |
| 761 } | 761 } |
| 762 | 762 |
| 763 string16 MenuItemView::GetAcceleratorText() { | 763 string16 MenuItemView::GetAcceleratorText() { |
| 764 if (id() == kEmptyMenuItemViewID) { | 764 if (id() == kEmptyMenuItemViewID) { |
| 765 // Don't query the delegate for menus that represent no children. | 765 // Don't query the delegate for menus that represent no children. |
| 766 return string16(); | 766 return string16(); |
| 767 } | 767 } |
| 768 | 768 |
| 769 Accelerator accelerator; | 769 Accelerator accelerator; |
| 770 return (GetDelegate() && | 770 return (GetDelegate() && |
| 771 GetDelegate()->GetAccelerator(GetCommand(), &accelerator)) ? | 771 GetDelegate()->GetAccelerator(GetCommand(), &accelerator)) ? |
| 772 accelerator.GetShortcutText() : string16(); | 772 accelerator.GetShortcutText() : string16(); |
| 773 } | 773 } |
| 774 | 774 |
| 775 } // namespace views | 775 } // namespace views |
| OLD | NEW |