Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(182)

Side by Side Diff: ui/views/controls/menu/menu_item_view.cc

Issue 10532171: Added support for icon views (view used instead of icon in a menu item). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Added support for icon views (view used instead of icon in a menu item). Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "ui/views/controls/menu/menu_item_view.h" 5 #include "ui/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.h" 8 #include "base/stl_util.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 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
79 controller_(NULL), 79 controller_(NULL),
80 canceled_(false), 80 canceled_(false),
81 parent_menu_item_(NULL), 81 parent_menu_item_(NULL),
82 type_(SUBMENU), 82 type_(SUBMENU),
83 selected_(false), 83 selected_(false),
84 command_(0), 84 command_(0),
85 submenu_(NULL), 85 submenu_(NULL),
86 has_mnemonics_(false), 86 has_mnemonics_(false),
87 show_mnemonics_(false), 87 show_mnemonics_(false),
88 has_icons_(false), 88 has_icons_(false),
89 has_icon_views_(false),
Aaron Boodman 2012/06/19 00:06:33 I don't see where has_icon_views_ is ever assigned
yefimt 2012/06/20 16:53:04 There is a set function in an h file On 2012/06/1
90 has_icon_view_(false),
89 top_margin_(-1), 91 top_margin_(-1),
90 bottom_margin_(-1), 92 bottom_margin_(-1),
91 requested_menu_position_(POSITION_BEST_FIT), 93 requested_menu_position_(POSITION_BEST_FIT),
92 actual_menu_position_(requested_menu_position_), 94 actual_menu_position_(requested_menu_position_),
93 use_right_margin_(true) { 95 use_right_margin_(true) {
94 // NOTE: don't check the delegate for NULL, UpdateMenuPartSizes supplies a 96 // NOTE: don't check the delegate for NULL, UpdateMenuPartSizes supplies a
95 // NULL delegate. 97 // NULL delegate.
96 Init(NULL, 0, SUBMENU, delegate); 98 Init(NULL, 0, SUBMENU, delegate);
97 } 99 }
98 100
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after
347 SchedulePaint(); 349 SchedulePaint();
348 } 350 }
349 351
350 void MenuItemView::SetTooltip(const string16& tooltip, int item_id) { 352 void MenuItemView::SetTooltip(const string16& tooltip, int item_id) {
351 MenuItemView* item = GetMenuItemByID(item_id); 353 MenuItemView* item = GetMenuItemByID(item_id);
352 DCHECK(item); 354 DCHECK(item);
353 item->tooltip_ = tooltip; 355 item->tooltip_ = tooltip;
354 } 356 }
355 357
356 void MenuItemView::SetIcon(const gfx::ImageSkia& icon, int item_id) { 358 void MenuItemView::SetIcon(const gfx::ImageSkia& icon, int item_id) {
359 DCHECK(!has_icon_view_);
357 MenuItemView* item = GetMenuItemByID(item_id); 360 MenuItemView* item = GetMenuItemByID(item_id);
358 DCHECK(item); 361 DCHECK(item);
359 item->SetIcon(icon); 362 item->SetIcon(icon);
360 } 363 }
361 364
362 void MenuItemView::SetIcon(const gfx::ImageSkia& icon) { 365 void MenuItemView::SetIcon(const gfx::ImageSkia& icon) {
Aaron Boodman 2012/06/19 00:06:33 Is it possible to reimplement SetIcon() in terms o
yefimt 2012/06/20 16:53:04 Done.
366 DCHECK(!has_icon_view_);
363 icon_ = icon; 367 icon_ = icon;
364 SchedulePaint(); 368 SchedulePaint();
365 } 369 }
366 370
371 void MenuItemView::SetIconView(View* icon_view) {
372 DCHECK(icon_view);
373 DCHECK(icon_.empty());
374 AddChildViewAt(icon_view, 0);
375 has_icon_view_ = true;
376 }
377
378 View* MenuItemView::GetIconView() {
379 if (!has_icon_view_)
380 return NULL;
381
382 return child_at(0);
383 }
384
367 void MenuItemView::OnPaint(gfx::Canvas* canvas) { 385 void MenuItemView::OnPaint(gfx::Canvas* canvas) {
368 PaintButton(canvas, PB_NORMAL); 386 PaintButton(canvas, PB_NORMAL);
369 } 387 }
370 388
371 gfx::Size MenuItemView::GetPreferredSize() { 389 gfx::Size MenuItemView::GetPreferredSize() {
372 if (pref_size_.IsEmpty()) 390 if (pref_size_.IsEmpty())
373 pref_size_ = CalculatePreferredSize(); 391 pref_size_ = CalculatePreferredSize();
374 return pref_size_; 392 return pref_size_;
375 } 393 }
376 394
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
472 return; 490 return;
473 491
474 if (IsContainer()) { 492 if (IsContainer()) {
475 View* child = child_at(0); 493 View* child = child_at(0);
476 gfx::Size size = child->GetPreferredSize(); 494 gfx::Size size = child->GetPreferredSize();
477 child->SetBounds(0, GetTopMargin(), size.width(), size.height()); 495 child->SetBounds(0, GetTopMargin(), size.width(), size.height());
478 } else { 496 } else {
479 // Child views are laid out right aligned and given the full height. To 497 // Child views are laid out right aligned and given the full height. To
480 // right align start with the last view and progress to the first. 498 // right align start with the last view and progress to the first.
481 int x = width() - (use_right_margin_ ? item_right_margin_ : 0); 499 int x = width() - (use_right_margin_ ? item_right_margin_ : 0);
482 for (int i = child_count() - 1; i >= 0; --i) { 500 int last_right_side_child = has_icon_view_ ? 1 : 0;
Aaron Boodman 2012/06/19 00:06:33 naming suggestion: first_non_icon_view.
yefimt 2012/06/20 16:53:04 Done.
501 for (int i = child_count() - 1; i >= last_right_side_child; --i) {
483 View* child = child_at(i); 502 View* child = child_at(i);
484 int width = child->GetPreferredSize().width(); 503 int width = child->GetPreferredSize().width();
485 child->SetBounds(x - width, 0, width, height()); 504 child->SetBounds(x - width, 0, width, height());
486 x -= width - kChildXPadding; 505 x -= width - kChildXPadding;
487 } 506 }
507 // Position icon_view
508 if (has_icon_view_) {
509 View* child = child_at(0);
510 child->SetPosition(gfx::Point(0,0));
511 child->SizeToPreferredSize();
512 }
488 } 513 }
489 } 514 }
490 515
491 int MenuItemView::GetAcceleratorTextWidth() { 516 int MenuItemView::GetAcceleratorTextWidth() {
492 string16 text = GetAcceleratorText(); 517 string16 text = GetAcceleratorText();
493 return text.empty() ? 0 : GetFont().GetStringWidth(text); 518 return text.empty() ? 0 : GetFont().GetStringWidth(text);
494 } 519 }
495 520
496 void MenuItemView::SetMargins(int top_margin, int bottom_margin) { 521 void MenuItemView::SetMargins(int top_margin, int bottom_margin) {
497 top_margin_ = top_margin; 522 top_margin_ = top_margin;
498 bottom_margin_ = bottom_margin; 523 bottom_margin_ = bottom_margin;
499 524
500 // invalidate GetPreferredSize() cache 525 // invalidate GetPreferredSize() cache
501 pref_size_.SetSize(0,0); 526 pref_size_.SetSize(0,0);
502 } 527 }
503 528
529 Border* MenuItemView::GetMenuBorder() {
530 return Border::CreateEmptyBorder(
531 MenuConfig::instance().submenu_vertical_margin_size,
532 MenuConfig::instance().submenu_horizontal_margin_size,
533 MenuConfig::instance().submenu_vertical_margin_size,
534 MenuConfig::instance().submenu_horizontal_margin_size);
535 }
536
537 Background* MenuItemView::GetMenuBackground() {
538 return NULL;
539 }
540
504 MenuItemView::MenuItemView(MenuItemView* parent, 541 MenuItemView::MenuItemView(MenuItemView* parent,
505 int command, 542 int command,
506 MenuItemView::Type type) 543 MenuItemView::Type type)
507 : delegate_(NULL), 544 : delegate_(NULL),
508 controller_(NULL), 545 controller_(NULL),
509 canceled_(false), 546 canceled_(false),
510 parent_menu_item_(parent), 547 parent_menu_item_(parent),
511 type_(type), 548 type_(type),
512 selected_(false), 549 selected_(false),
513 command_(command), 550 command_(command),
514 submenu_(NULL), 551 submenu_(NULL),
515 has_mnemonics_(false), 552 has_mnemonics_(false),
516 show_mnemonics_(false), 553 show_mnemonics_(false),
517 has_icons_(false), 554 has_icons_(false),
555 has_icon_views_(false),
556 has_icon_view_(false),
518 top_margin_(-1), 557 top_margin_(-1),
519 bottom_margin_(-1), 558 bottom_margin_(-1),
520 requested_menu_position_(POSITION_BEST_FIT), 559 requested_menu_position_(POSITION_BEST_FIT),
521 actual_menu_position_(requested_menu_position_) { 560 actual_menu_position_(requested_menu_position_) {
522 Init(parent, command, type, NULL); 561 Init(parent, command, type, NULL);
523 } 562 }
524 563
525 MenuItemView::~MenuItemView() { 564 MenuItemView::~MenuItemView() {
526 delete submenu_; 565 delete submenu_;
527 STLDeleteElements(&removed_items_); 566 STLDeleteElements(&removed_items_);
528 } 567 }
529 568
530 std::string MenuItemView::GetClassName() const { 569 std::string MenuItemView::GetClassName() const {
531 return kViewClassName; 570 return kViewClassName;
532 } 571 }
533 572
534 // Calculates all sizes that we can from the OS. 573 // Calculates all sizes that we can from the OS.
535 // 574 //
536 // This is invoked prior to Running a menu. 575 // This is invoked prior to Running a menu.
537 void MenuItemView::UpdateMenuPartSizes(bool has_icons) { 576 void MenuItemView::UpdateMenuPartSizes() {
538 MenuConfig::Reset(); 577 MenuConfig::Reset();
539 const MenuConfig& config = MenuConfig::instance(); 578 const MenuConfig& config = MenuConfig::instance();
540 579
541 item_right_margin_ = config.label_to_arrow_padding + config.arrow_width + 580 item_right_margin_ = config.label_to_arrow_padding + config.arrow_width +
542 config.arrow_to_edge_padding; 581 config.arrow_to_edge_padding;
582 int icon_width = config.check_width;
583 if (has_icon_views_) {
584 for (int i = 0; i < submenu_->GetMenuItemCount(); ++i) {
585 MenuItemView* menu_item = submenu_->GetMenuItemAt(i);
586 if (menu_item->has_icon_view_)
587 icon_width = std::max(icon_width,
588 menu_item->child_at(0)->GetPreferredSize().width());
589 }
590 }
543 591
544 if (config.always_use_icon_to_label_padding) 592 if (config.always_use_icon_to_label_padding)
545 label_start_ = config.item_left_margin + config.check_width + 593 label_start_ = config.item_left_margin + icon_width +
546 config.icon_to_label_padding; 594 config.icon_to_label_padding;
547 else 595 else
548 // If there are no icons don't pad by the icon to label padding. This 596 // If there are no icons don't pad by the icon to label padding. This
549 // makes us look close to system menus. 597 // makes us look close to system menus.
550 label_start_ = config.item_left_margin + config.check_width + 598 label_start_ = config.item_left_margin + icon_width +
551 (has_icons ? config.icon_to_label_padding : 0); 599 (has_icons_ ? config.icon_to_label_padding : 0);
552 600
553 if (config.render_gutter) 601 if (config.render_gutter)
554 label_start_ += config.gutter_width + config.gutter_to_label; 602 label_start_ += config.gutter_width + config.gutter_to_label;
555 603
556 MenuItemView menu_item(NULL); 604 MenuItemView menu_item(NULL);
557 menu_item.SetTitle(ASCIIToUTF16("blah")); // Text doesn't matter here. 605 menu_item.SetTitle(ASCIIToUTF16("blah")); // Text doesn't matter here.
558 pref_menu_height_ = menu_item.GetPreferredSize().height(); 606 pref_menu_height_ = menu_item.GetPreferredSize().height();
559 } 607 }
560 608
561 void MenuItemView::Init(MenuItemView* parent, 609 void MenuItemView::Init(MenuItemView* parent,
(...skipping 30 matching lines...) Expand all
592 canceled_ = false; 640 canceled_ = false;
593 641
594 has_mnemonics_ = has_mnemonics; 642 has_mnemonics_ = has_mnemonics;
595 show_mnemonics_ = has_mnemonics && show_mnemonics; 643 show_mnemonics_ = has_mnemonics && show_mnemonics;
596 644
597 AddEmptyMenus(); 645 AddEmptyMenus();
598 646
599 if (!MenuController::GetActiveInstance()) { 647 if (!MenuController::GetActiveInstance()) {
600 // Only update the menu size if there are no menus showing, otherwise 648 // Only update the menu size if there are no menus showing, otherwise
601 // things may shift around. 649 // things may shift around.
602 UpdateMenuPartSizes(has_icons_); 650 UpdateMenuPartSizes();
603 } 651 }
604 } 652 }
605 653
606 int MenuItemView::GetDrawStringFlags() { 654 int MenuItemView::GetDrawStringFlags() {
607 int flags = 0; 655 int flags = 0;
608 if (base::i18n::IsRTL()) 656 if (base::i18n::IsRTL())
609 flags |= gfx::Canvas::TEXT_ALIGN_RIGHT; 657 flags |= gfx::Canvas::TEXT_ALIGN_RIGHT;
610 else 658 else
611 flags |= gfx::Canvas::TEXT_ALIGN_LEFT; 659 flags |= gfx::Canvas::TEXT_ALIGN_LEFT;
612 660
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
727 gfx::Size MenuItemView::GetChildPreferredSize() { 775 gfx::Size MenuItemView::GetChildPreferredSize() {
728 if (!has_children()) 776 if (!has_children())
729 return gfx::Size(); 777 return gfx::Size();
730 778
731 if (IsContainer()) { 779 if (IsContainer()) {
732 View* child = child_at(0); 780 View* child = child_at(0);
733 return child->GetPreferredSize(); 781 return child->GetPreferredSize();
734 } 782 }
735 783
736 int width = 0; 784 int width = 0;
737 for (int i = 0; i < child_count(); ++i) { 785 int i = has_icon_view_ ? 1 : 0;
786 for (; i < child_count(); ++i) {
738 if (i) 787 if (i)
739 width += kChildXPadding; 788 width += kChildXPadding;
740 width += child_at(i)->GetPreferredSize().width(); 789 gfx::Size size = child_at(i)->GetPreferredSize();
790 width += size.width();
741 } 791 }
792 int height = 0;
793 if (has_icon_view_)
794 height = child_at(0)->GetPreferredSize().height();
795
742 // Return a height of 0 to indicate that we should use the title height 796 // Return a height of 0 to indicate that we should use the title height
743 // instead. 797 // instead.
744 return gfx::Size(width, 0); 798 return gfx::Size(width, height);
745 } 799 }
746 800
747 gfx::Size MenuItemView::CalculatePreferredSize() { 801 gfx::Size MenuItemView::CalculatePreferredSize() {
748 gfx::Size child_size = GetChildPreferredSize(); 802 gfx::Size child_size = GetChildPreferredSize();
749 if (IsContainer()) { 803 if (IsContainer()) {
750 return gfx::Size( 804 return gfx::Size(
751 child_size.width(), 805 child_size.width(),
752 child_size.height() + GetBottomMargin() + GetTopMargin()); 806 child_size.height() + GetBottomMargin() + GetTopMargin());
753 } 807 }
754 808
(...skipping 21 matching lines...) Expand all
776 accelerator.GetShortcutText() : string16(); 830 accelerator.GetShortcutText() : string16();
777 } 831 }
778 832
779 bool MenuItemView::IsContainer() const { 833 bool MenuItemView::IsContainer() const {
780 // Let the first child take over |this| when we only have one child and no 834 // Let the first child take over |this| when we only have one child and no
781 // title. Note that what child_count() returns is the number of children, 835 // title. Note that what child_count() returns is the number of children,
782 // not the number of menu items. 836 // not the number of menu items.
783 return child_count() == 1 && title_.empty(); 837 return child_count() == 1 && title_.empty();
784 } 838 }
785 839
840 bool MenuItemView::HasNonIconChildViews() {
841 return child_count() > (has_icon_view_ ? 1 : 0);
842 }
843
786 } // namespace views 844 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698