OLD | NEW |
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/submenu_view.h" | 5 #include "ui/views/controls/menu/submenu_view.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
10 #include "ui/accessibility/ax_view_state.h" | 10 #include "ui/accessibility/ax_view_state.h" |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 new_y = 0; | 107 new_y = 0; |
108 SetBounds(x(), new_y, parent()->width(), pref_height); | 108 SetBounds(x(), new_y, parent()->width(), pref_height); |
109 | 109 |
110 gfx::Insets insets = GetInsets(); | 110 gfx::Insets insets = GetInsets(); |
111 int x = insets.left(); | 111 int x = insets.left(); |
112 int y = insets.top(); | 112 int y = insets.top(); |
113 int menu_item_width = width() - insets.width(); | 113 int menu_item_width = width() - insets.width(); |
114 for (int i = 0; i < child_count(); ++i) { | 114 for (int i = 0; i < child_count(); ++i) { |
115 View* child = child_at(i); | 115 View* child = child_at(i); |
116 if (child->visible()) { | 116 if (child->visible()) { |
117 gfx::Size child_pref_size = child->GetPreferredSize(); | 117 int child_height = child->GetHeightForWidth(menu_item_width); |
118 child->SetBounds(x, y, menu_item_width, child_pref_size.height()); | 118 child->SetBounds(x, y, menu_item_width, child_height); |
119 y += child_pref_size.height(); | 119 y += child_height; |
120 } | 120 } |
121 } | 121 } |
122 } | 122 } |
123 | 123 |
124 gfx::Size SubmenuView::GetPreferredSize() const { | 124 gfx::Size SubmenuView::GetPreferredSize() const { |
125 if (!has_children()) | 125 if (!has_children()) |
126 return gfx::Size(); | 126 return gfx::Size(); |
127 | 127 |
128 max_minor_text_width_ = 0; | 128 max_minor_text_width_ = 0; |
129 // The maximum width of items which contain maybe a label and multiple views. | 129 // The maximum width of items which contain maybe a label and multiple views. |
130 int max_complex_width = 0; | 130 int max_complex_width = 0; |
131 // The max. width of items which contain a label and maybe an accelerator. | 131 // The max. width of items which contain a label and maybe an accelerator. |
132 int max_simple_width = 0; | 132 int max_simple_width = 0; |
133 int height = 0; | 133 |
| 134 // We perform the size calculation in two passes. In the first pass, we |
| 135 // calculate the width of the menu. In the second, we calculate the height |
| 136 // using that width. This allows views that have flexible widths to adjust |
| 137 // accordingly. |
134 for (int i = 0; i < child_count(); ++i) { | 138 for (int i = 0; i < child_count(); ++i) { |
135 const View* child = child_at(i); | 139 const View* child = child_at(i); |
136 if (!child->visible()) | 140 if (!child->visible()) |
137 continue; | 141 continue; |
138 if (child->id() == MenuItemView::kMenuItemViewID) { | 142 if (child->id() == MenuItemView::kMenuItemViewID) { |
139 const MenuItemView* menu = static_cast<const MenuItemView*>(child); | 143 const MenuItemView* menu = static_cast<const MenuItemView*>(child); |
140 const MenuItemView::MenuItemDimensions& dimensions = | 144 const MenuItemView::MenuItemDimensions& dimensions = |
141 menu->GetDimensions(); | 145 menu->GetDimensions(); |
142 max_simple_width = std::max( | 146 max_simple_width = std::max( |
143 max_simple_width, dimensions.standard_width); | 147 max_simple_width, dimensions.standard_width); |
144 max_minor_text_width_ = | 148 max_minor_text_width_ = |
145 std::max(max_minor_text_width_, dimensions.minor_text_width); | 149 std::max(max_minor_text_width_, dimensions.minor_text_width); |
146 max_complex_width = std::max(max_complex_width, | 150 max_complex_width = std::max(max_complex_width, |
147 dimensions.standard_width + dimensions.children_width); | 151 dimensions.standard_width + dimensions.children_width); |
148 height += dimensions.height; | |
149 } else { | 152 } else { |
150 gfx::Size child_pref_size = | 153 max_complex_width = std::max(max_complex_width, |
151 child->visible() ? child->GetPreferredSize() : gfx::Size(); | 154 child->GetPreferredSize().width()); |
152 max_complex_width = std::max(max_complex_width, child_pref_size.width()); | |
153 height += child_pref_size.height(); | |
154 } | 155 } |
155 } | 156 } |
156 if (max_minor_text_width_ > 0) { | 157 if (max_minor_text_width_ > 0) { |
157 max_minor_text_width_ += | 158 max_minor_text_width_ += |
158 GetMenuItem()->GetMenuConfig().label_to_minor_text_padding; | 159 GetMenuItem()->GetMenuConfig().label_to_minor_text_padding; |
159 } | 160 } |
| 161 // Finish calculating our optimum width. |
160 gfx::Insets insets = GetInsets(); | 162 gfx::Insets insets = GetInsets(); |
161 return gfx::Size( | 163 int width = std::max(max_complex_width, |
162 std::max(max_complex_width, | 164 std::max(max_simple_width + max_minor_text_width_ + |
163 std::max(max_simple_width + max_minor_text_width_ + | 165 insets.width(), |
164 insets.width(), | 166 minimum_preferred_width_ - 2 * insets.width())); |
165 minimum_preferred_width_ - 2 * insets.width())), | 167 |
166 height + insets.height()); | 168 // Then, the height for that width. |
| 169 int height = 0; |
| 170 int menu_item_width = width - insets.width(); |
| 171 for (int i = 0; i < child_count(); ++i) { |
| 172 const View* child = child_at(i); |
| 173 height += child->visible() ? child->GetHeightForWidth(menu_item_width) : 0; |
| 174 } |
| 175 |
| 176 return gfx::Size(width, height + insets.height()); |
167 } | 177 } |
168 | 178 |
169 void SubmenuView::GetAccessibleState(ui::AXViewState* state) { | 179 void SubmenuView::GetAccessibleState(ui::AXViewState* state) { |
170 // Inherit most of the state from the parent menu item, except the role. | 180 // Inherit most of the state from the parent menu item, except the role. |
171 if (GetMenuItem()) | 181 if (GetMenuItem()) |
172 GetMenuItem()->GetAccessibleState(state); | 182 GetMenuItem()->GetAccessibleState(state); |
173 state->role = ui::AX_ROLE_MENU_LIST_POPUP; | 183 state->role = ui::AX_ROLE_MENU_LIST_POPUP; |
174 } | 184 } |
175 | 185 |
176 ui::TextInputClient* SubmenuView::GetTextInputClient() { | 186 ui::TextInputClient* SubmenuView::GetTextInputClient() { |
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
492 y = std::max(y, 0); | 502 y = std::max(y, 0); |
493 gfx::Rect new_vis_bounds(x, y, vis_bounds.width(), vis_bounds.height()); | 503 gfx::Rect new_vis_bounds(x, y, vis_bounds.width(), vis_bounds.height()); |
494 if (new_vis_bounds != vis_bounds) { | 504 if (new_vis_bounds != vis_bounds) { |
495 ScrollRectToVisible(new_vis_bounds); | 505 ScrollRectToVisible(new_vis_bounds); |
496 return true; | 506 return true; |
497 } | 507 } |
498 return false; | 508 return false; |
499 } | 509 } |
500 | 510 |
501 } // namespace views | 511 } // namespace views |
OLD | NEW |