 Chromium Code Reviews
 Chromium Code Reviews Issue 6452011:
  Rework tree APIs to reflect Google style and more const-correctness....  (Closed) 
  Base URL: svn://svn.chromium.org/chrome/trunk/src/
    
  
    Issue 6452011:
  Rework tree APIs to reflect Google style and more const-correctness....  (Closed) 
  Base URL: svn://svn.chromium.org/chrome/trunk/src/| 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 "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" | 
| 8 #include "grit/app_strings.h" | 8 #include "grit/app_strings.h" | 
| 9 #include "ui/base/l10n/l10n_util.h" | 9 #include "ui/base/l10n/l10n_util.h" | 
| 10 #include "ui/base/models/menu_model.h" | 10 #include "ui/base/models/menu_model.h" | 
| (...skipping 382 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 393 } | 393 } | 
| 394 } while (index != std::wstring::npos); | 394 } while (index != std::wstring::npos); | 
| 395 return 0; | 395 return 0; | 
| 396 } | 396 } | 
| 397 | 397 | 
| 398 MenuItemView* MenuItemView::GetMenuItemByID(int id) { | 398 MenuItemView* MenuItemView::GetMenuItemByID(int id) { | 
| 399 if (GetCommand() == id) | 399 if (GetCommand() == id) | 
| 400 return this; | 400 return this; | 
| 401 if (!HasSubmenu()) | 401 if (!HasSubmenu()) | 
| 402 return NULL; | 402 return NULL; | 
| 403 for (int i = 0; i < GetSubmenu()->GetChildViewCount(); ++i) { | 403 for (size_t i = 0; i < GetSubmenu()->child_count(); ++i) { | 
| 404 View* child = GetSubmenu()->GetChildViewAt(i); | 404 View* child = GetSubmenu()->GetChildViewAt(i); | 
| 405 if (child->GetID() == MenuItemView::kMenuItemViewID) { | 405 if (child->GetID() == MenuItemView::kMenuItemViewID) { | 
| 406 MenuItemView* result = static_cast<MenuItemView*>(child)-> | 406 MenuItemView* result = static_cast<MenuItemView*>(child)-> | 
| 407 GetMenuItemByID(id); | 407 GetMenuItemByID(id); | 
| 408 if (result) | 408 if (result) | 
| 409 return result; | 409 return result; | 
| 410 } | 410 } | 
| 411 } | 411 } | 
| 412 return NULL; | 412 return NULL; | 
| 413 } | 413 } | 
| (...skipping 14 matching lines...) Expand all Loading... | |
| 428 if (submenu_) { | 428 if (submenu_) { | 
| 429 // Force a paint and layout. This handles the case of the top level window's | 429 // Force a paint and layout. This handles the case of the top level window's | 
| 430 // size remaining the same, resulting in no change to the submenu's size and | 430 // size remaining the same, resulting in no change to the submenu's size and | 
| 431 // no layout. | 431 // no layout. | 
| 432 submenu_->Layout(); | 432 submenu_->Layout(); | 
| 433 submenu_->SchedulePaint(); | 433 submenu_->SchedulePaint(); | 
| 434 } | 434 } | 
| 435 } | 435 } | 
| 436 | 436 | 
| 437 void MenuItemView::Layout() { | 437 void MenuItemView::Layout() { | 
| 438 int child_count = GetChildViewCount(); | 438 if (!has_children()) | 
| 439 if (child_count == 0) | |
| 440 return; | 439 return; | 
| 441 | 440 | 
| 442 // Child views are layed out right aligned and given the full height. To right | 441 // Child views are laid out right aligned and given the full height. To right | 
| 443 // align start with the last view and progress to the first. | 442 // align start with the last view and progress to the first. | 
| 444 for (int i = child_count - 1, x = width() - item_right_margin_; i >= 0; --i) { | 443 for (size_t i = child_count() - 1, x = width() - item_right_margin_; i >= 0; | 
| 
sky
2011/02/08 19:11:54
this never stops.
PS I don't like size_t.
 | |
| 444 --i) { | |
| 445 View* child = GetChildViewAt(i); | 445 View* child = GetChildViewAt(i); | 
| 446 int width = child->GetPreferredSize().width(); | 446 int width = child->GetPreferredSize().width(); | 
| 447 child->SetBounds(x - width, 0, width, height()); | 447 child->SetBounds(x - width, 0, width, height()); | 
| 448 x -= width - kChildXPadding; | 448 x -= width - kChildXPadding; | 
| 449 } | 449 } | 
| 450 } | 450 } | 
| 451 | 451 | 
| 452 int MenuItemView::GetAcceleratorTextWidth() { | 452 int MenuItemView::GetAcceleratorTextWidth() { | 
| 453 string16 text = GetAcceleratorText(); | 453 string16 text = GetAcceleratorText(); | 
| 454 return text.empty() ? 0 : MenuConfig::instance().font.GetStringWidth(text); | 454 return text.empty() ? 0 : MenuConfig::instance().font.GetStringWidth(text); | 
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 574 flags |= gfx::Canvas::SHOW_PREFIX; | 574 flags |= gfx::Canvas::SHOW_PREFIX; | 
| 575 } else { | 575 } else { | 
| 576 flags |= gfx::Canvas::HIDE_PREFIX; | 576 flags |= gfx::Canvas::HIDE_PREFIX; | 
| 577 } | 577 } | 
| 578 } | 578 } | 
| 579 return flags; | 579 return flags; | 
| 580 } | 580 } | 
| 581 | 581 | 
| 582 void MenuItemView::AddEmptyMenus() { | 582 void MenuItemView::AddEmptyMenus() { | 
| 583 DCHECK(HasSubmenu()); | 583 DCHECK(HasSubmenu()); | 
| 584 if (submenu_->GetChildViewCount() == 0) { | 584 if (!submenu_->has_children()) { | 
| 585 submenu_->AddChildView(0, new EmptyMenuMenuItem(this)); | 585 submenu_->AddChildViewAt(new EmptyMenuMenuItem(this), 0); | 
| 586 } else { | 586 } else { | 
| 587 for (int i = 0, item_count = submenu_->GetMenuItemCount(); i < item_count; | 587 for (int i = 0, item_count = submenu_->GetMenuItemCount(); i < item_count; | 
| 588 ++i) { | 588 ++i) { | 
| 589 MenuItemView* child = submenu_->GetMenuItemAt(i); | 589 MenuItemView* child = submenu_->GetMenuItemAt(i); | 
| 590 if (child->HasSubmenu()) | 590 if (child->HasSubmenu()) | 
| 591 child->AddEmptyMenus(); | 591 child->AddEmptyMenus(); | 
| 592 } | 592 } | 
| 593 } | 593 } | 
| 594 } | 594 } | 
| 595 | 595 | 
| 596 void MenuItemView::RemoveEmptyMenus() { | 596 void MenuItemView::RemoveEmptyMenus() { | 
| 597 DCHECK(HasSubmenu()); | 597 DCHECK(HasSubmenu()); | 
| 598 // Iterate backwards as we may end up removing views, which alters the child | 598 // Iterate backwards as we may end up removing views, which alters the child | 
| 599 // view count. | 599 // view count. | 
| 600 for (int i = submenu_->GetChildViewCount() - 1; i >= 0; --i) { | 600 for (size_t i = submenu_->child_count() - 1; i >= 0; --i) { | 
| 
sky
2011/02/08 19:11:54
this never stops.
PS I don't like size_t.
 | |
| 601 View* child = submenu_->GetChildViewAt(i); | 601 View* child = submenu_->GetChildViewAt(i); | 
| 602 if (child->GetID() == MenuItemView::kMenuItemViewID) { | 602 if (child->GetID() == MenuItemView::kMenuItemViewID) { | 
| 603 MenuItemView* menu_item = static_cast<MenuItemView*>(child); | 603 MenuItemView* menu_item = static_cast<MenuItemView*>(child); | 
| 604 if (menu_item->HasSubmenu()) | 604 if (menu_item->HasSubmenu()) | 
| 605 menu_item->RemoveEmptyMenus(); | 605 menu_item->RemoveEmptyMenus(); | 
| 606 } else if (child->GetID() == EmptyMenuMenuItem::kEmptyMenuItemViewID) { | 606 } else if (child->GetID() == EmptyMenuMenuItem::kEmptyMenuItemViewID) { | 
| 607 submenu_->RemoveChildView(child); | 607 submenu_->RemoveChildView(child); | 
| 608 } | 608 } | 
| 609 } | 609 } | 
| 610 } | 610 } | 
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 657 } | 657 } | 
| 658 | 658 | 
| 659 int MenuItemView::GetBottomMargin() { | 659 int MenuItemView::GetBottomMargin() { | 
| 660 MenuItemView* root = GetRootMenuItem(); | 660 MenuItemView* root = GetRootMenuItem(); | 
| 661 return root && root->has_icons_ | 661 return root && root->has_icons_ | 
| 662 ? MenuConfig::instance().item_bottom_margin : | 662 ? MenuConfig::instance().item_bottom_margin : | 
| 663 MenuConfig::instance().item_no_icon_bottom_margin; | 663 MenuConfig::instance().item_no_icon_bottom_margin; | 
| 664 } | 664 } | 
| 665 | 665 | 
| 666 int MenuItemView::GetChildPreferredWidth() { | 666 int MenuItemView::GetChildPreferredWidth() { | 
| 667 int child_count = GetChildViewCount(); | 667 if (!has_children()) | 
| 668 if (child_count == 0) | |
| 669 return 0; | 668 return 0; | 
| 670 | 669 | 
| 671 int width = 0; | 670 int width = 0; | 
| 672 for (int i = 0; i < child_count; ++i) { | 671 for (size_t i = 0; i < child_count(); ++i) { | 
| 673 if (i) | 672 if (i) | 
| 674 width += kChildXPadding; | 673 width += kChildXPadding; | 
| 675 width += GetChildViewAt(i)->GetPreferredSize().width(); | 674 width += GetChildViewAt(i)->GetPreferredSize().width(); | 
| 676 } | 675 } | 
| 677 return width; | 676 return width; | 
| 678 } | 677 } | 
| 679 | 678 | 
| 680 string16 MenuItemView::GetAcceleratorText() { | 679 string16 MenuItemView::GetAcceleratorText() { | 
| 681 Accelerator accelerator; | 680 Accelerator accelerator; | 
| 682 return (GetDelegate() && | 681 return (GetDelegate() && | 
| 683 GetDelegate()->GetAccelerator(GetCommand(), &accelerator)) ? | 682 GetDelegate()->GetAccelerator(GetCommand(), &accelerator)) ? | 
| 684 accelerator.GetShortcutText() : string16(); | 683 accelerator.GetShortcutText() : string16(); | 
| 685 } | 684 } | 
| 686 | 685 | 
| 687 } // namespace views | 686 } // namespace views | 
| OLD | NEW |