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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: ui/views/controls/menu/menu_item_view.cc
diff --git a/ui/views/controls/menu/menu_item_view.cc b/ui/views/controls/menu/menu_item_view.cc
index b5f81117e569010cab74e08c60ef3a17419eb70f..33713919f0606855188cecfd8d590391727903fb 100644
--- a/ui/views/controls/menu/menu_item_view.cc
+++ b/ui/views/controls/menu/menu_item_view.cc
@@ -86,6 +86,8 @@ MenuItemView::MenuItemView(MenuDelegate* delegate)
has_mnemonics_(false),
show_mnemonics_(false),
has_icons_(false),
+ 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
+ has_icon_view_(false),
top_margin_(-1),
bottom_margin_(-1),
requested_menu_position_(POSITION_BEST_FIT),
@@ -354,16 +356,32 @@ void MenuItemView::SetTooltip(const string16& tooltip, int item_id) {
}
void MenuItemView::SetIcon(const gfx::ImageSkia& icon, int item_id) {
+ DCHECK(!has_icon_view_);
MenuItemView* item = GetMenuItemByID(item_id);
DCHECK(item);
item->SetIcon(icon);
}
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.
+ DCHECK(!has_icon_view_);
icon_ = icon;
SchedulePaint();
}
+void MenuItemView::SetIconView(View* icon_view) {
+ DCHECK(icon_view);
+ DCHECK(icon_.empty());
+ AddChildViewAt(icon_view, 0);
+ has_icon_view_ = true;
+}
+
+View* MenuItemView::GetIconView() {
+ if (!has_icon_view_)
+ return NULL;
+
+ return child_at(0);
+}
+
void MenuItemView::OnPaint(gfx::Canvas* canvas) {
PaintButton(canvas, PB_NORMAL);
}
@@ -479,12 +497,19 @@ void MenuItemView::Layout() {
// Child views are laid out right aligned and given the full height. To
// right align start with the last view and progress to the first.
int x = width() - (use_right_margin_ ? item_right_margin_ : 0);
- for (int i = child_count() - 1; i >= 0; --i) {
+ 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.
+ for (int i = child_count() - 1; i >= last_right_side_child; --i) {
View* child = child_at(i);
int width = child->GetPreferredSize().width();
child->SetBounds(x - width, 0, width, height());
x -= width - kChildXPadding;
}
+ // Position icon_view
+ if (has_icon_view_) {
+ View* child = child_at(0);
+ child->SetPosition(gfx::Point(0,0));
+ child->SizeToPreferredSize();
+ }
}
}
@@ -501,6 +526,18 @@ void MenuItemView::SetMargins(int top_margin, int bottom_margin) {
pref_size_.SetSize(0,0);
}
+Border* MenuItemView::GetMenuBorder() {
+ return Border::CreateEmptyBorder(
+ MenuConfig::instance().submenu_vertical_margin_size,
+ MenuConfig::instance().submenu_horizontal_margin_size,
+ MenuConfig::instance().submenu_vertical_margin_size,
+ MenuConfig::instance().submenu_horizontal_margin_size);
+}
+
+Background* MenuItemView::GetMenuBackground() {
+ return NULL;
+}
+
MenuItemView::MenuItemView(MenuItemView* parent,
int command,
MenuItemView::Type type)
@@ -515,6 +552,8 @@ MenuItemView::MenuItemView(MenuItemView* parent,
has_mnemonics_(false),
show_mnemonics_(false),
has_icons_(false),
+ has_icon_views_(false),
+ has_icon_view_(false),
top_margin_(-1),
bottom_margin_(-1),
requested_menu_position_(POSITION_BEST_FIT),
@@ -534,21 +573,30 @@ std::string MenuItemView::GetClassName() const {
// Calculates all sizes that we can from the OS.
//
// This is invoked prior to Running a menu.
-void MenuItemView::UpdateMenuPartSizes(bool has_icons) {
+void MenuItemView::UpdateMenuPartSizes() {
MenuConfig::Reset();
const MenuConfig& config = MenuConfig::instance();
item_right_margin_ = config.label_to_arrow_padding + config.arrow_width +
config.arrow_to_edge_padding;
+ int icon_width = config.check_width;
+ if (has_icon_views_) {
+ for (int i = 0; i < submenu_->GetMenuItemCount(); ++i) {
+ MenuItemView* menu_item = submenu_->GetMenuItemAt(i);
+ if (menu_item->has_icon_view_)
+ icon_width = std::max(icon_width,
+ menu_item->child_at(0)->GetPreferredSize().width());
+ }
+ }
if (config.always_use_icon_to_label_padding)
- label_start_ = config.item_left_margin + config.check_width +
+ label_start_ = config.item_left_margin + icon_width +
config.icon_to_label_padding;
else
// If there are no icons don't pad by the icon to label padding. This
// makes us look close to system menus.
- label_start_ = config.item_left_margin + config.check_width +
- (has_icons ? config.icon_to_label_padding : 0);
+ label_start_ = config.item_left_margin + icon_width +
+ (has_icons_ ? config.icon_to_label_padding : 0);
if (config.render_gutter)
label_start_ += config.gutter_width + config.gutter_to_label;
@@ -599,7 +647,7 @@ void MenuItemView::PrepareForRun(bool has_mnemonics, bool show_mnemonics) {
if (!MenuController::GetActiveInstance()) {
// Only update the menu size if there are no menus showing, otherwise
// things may shift around.
- UpdateMenuPartSizes(has_icons_);
+ UpdateMenuPartSizes();
}
}
@@ -734,14 +782,20 @@ gfx::Size MenuItemView::GetChildPreferredSize() {
}
int width = 0;
- for (int i = 0; i < child_count(); ++i) {
+ int i = has_icon_view_ ? 1 : 0;
+ for (; i < child_count(); ++i) {
if (i)
width += kChildXPadding;
- width += child_at(i)->GetPreferredSize().width();
+ gfx::Size size = child_at(i)->GetPreferredSize();
+ width += size.width();
}
+ int height = 0;
+ if (has_icon_view_)
+ height = child_at(0)->GetPreferredSize().height();
+
// Return a height of 0 to indicate that we should use the title height
// instead.
- return gfx::Size(width, 0);
+ return gfx::Size(width, height);
}
gfx::Size MenuItemView::CalculatePreferredSize() {
@@ -783,4 +837,8 @@ bool MenuItemView::IsContainer() const {
return child_count() == 1 && title_.empty();
}
+bool MenuItemView::HasNonIconChildViews() {
+ return child_count() > (has_icon_view_ ? 1 : 0);
+}
+
} // namespace views

Powered by Google App Engine
This is Rietveld 408576698