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
3 // found in the LICENSE file.
5 #include "ui/views/controls/menu/submenu_view.h"
9 #include "base/compiler_specific.h"
10 #include "ui/base/accessibility/accessible_view_state.h"
11 #include "ui/events/event.h"
12 #include "ui/gfx/canvas.h"
13 #include "ui/gfx/geometry/safe_integer_conversions.h"
14 #include "ui/views/controls/menu/menu_config.h"
15 #include "ui/views/controls/menu/menu_controller.h"
16 #include "ui/views/controls/menu/menu_host.h"
17 #include "ui/views/controls/menu/menu_scroll_view_container.h"
18 #include "ui/views/widget/root_view.h"
19 #include "ui/views/widget/widget.h"
23 // Height of the drop indicator. This should be an even number.
24 const int kDropIndicatorHeight = 2;
26 // Color of the drop indicator.
27 const SkColor kDropIndicatorColor = SK_ColorBLACK;
34 const char SubmenuView::kViewClassName[] = "SubmenuView";
36 SubmenuView::SubmenuView(MenuItemView* parent)
37 : parent_menu_item_(parent),
40 drop_position_(MenuDelegate::DROP_NONE),
41 scroll_view_container_(NULL),
42 max_minor_text_width_(0),
43 minimum_preferred_width_(0),
44 resize_open_menu_(false),
45 scroll_animator_(new ScrollAnimator(this)),
48 // We'll delete ourselves, otherwise the ScrollView would delete us on close.
49 set_owned_by_client();
52 SubmenuView::~SubmenuView() {
53 // The menu may not have been closed yet (it will be hidden, but not
54 // necessarily closed).
57 delete scroll_view_container_;
60 int SubmenuView::GetMenuItemCount() {
62 for (int i = 0; i < child_count(); ++i) {
63 if (child_at(i)->id() == MenuItemView::kMenuItemViewID)
69 MenuItemView* SubmenuView::GetMenuItemAt(int index) {
70 for (int i = 0, count = 0; i < child_count(); ++i) {
71 if (child_at(i)->id() == MenuItemView::kMenuItemViewID &&
73 return static_cast<MenuItemView*>(child_at(i));
80 void SubmenuView::ChildPreferredSizeChanged(View* child) {
81 if (!resize_open_menu_)
84 MenuItemView *item = GetMenuItem();
85 MenuController* controller = item->GetMenuController();
89 gfx::Rect bounds = controller->CalculateMenuBounds(item, false, &dir);
94 void SubmenuView::Layout() {
95 // We're in a ScrollView, and need to set our width/height ourselves.
99 // Use our current y, unless it means part of the menu isn't visible anymore.
100 int pref_height = GetPreferredSize().height();
102 if (pref_height > parent()->height())
103 new_y = std::max(parent()->height() - pref_height, y());
106 SetBounds(x(), new_y, parent()->width(), pref_height);
108 gfx::Insets insets = GetInsets();
109 int x = insets.left();
110 int y = insets.top();
111 int menu_item_width = width() - insets.width();
112 for (int i = 0; i < child_count(); ++i) {
113 View* child = child_at(i);
114 if (child->visible()) {
115 gfx::Size child_pref_size = child->GetPreferredSize();
116 child->SetBounds(x, y, menu_item_width, child_pref_size.height());
117 y += child_pref_size.height();
122 gfx::Size SubmenuView::GetPreferredSize() {
126 max_minor_text_width_ = 0;
127 // The maximum width of items which contain maybe a label and multiple views.
128 int max_complex_width = 0;
129 // The max. width of items which contain a label and maybe an accelerator.
130 int max_simple_width = 0;
132 for (int i = 0; i < child_count(); ++i) {
133 View* child = child_at(i);
134 if (!child->visible())
136 if (child->id() == MenuItemView::kMenuItemViewID) {
137 MenuItemView* menu = static_cast<MenuItemView*>(child);
138 const MenuItemView::MenuItemDimensions& dimensions =
139 menu->GetDimensions();
140 max_simple_width = std::max(
141 max_simple_width, dimensions.standard_width);
142 max_minor_text_width_ =
143 std::max(max_minor_text_width_, dimensions.minor_text_width);
144 max_complex_width = std::max(max_complex_width,
145 dimensions.standard_width + dimensions.children_width);
146 height += dimensions.height;
148 gfx::Size child_pref_size =
149 child->visible() ? child->GetPreferredSize() : gfx::Size();
150 max_complex_width = std::max(max_complex_width, child_pref_size.width());
151 height += child_pref_size.height();
154 if (max_minor_text_width_ > 0) {
155 max_minor_text_width_ +=
156 GetMenuItem()->GetMenuConfig().label_to_minor_text_padding;
158 gfx::Insets insets = GetInsets();
160 std::max(max_complex_width,
161 std::max(max_simple_width + max_minor_text_width_ +
163 minimum_preferred_width_ - 2 * insets.width())),
164 height + insets.height());
167 void SubmenuView::GetAccessibleState(ui::AccessibleViewState* state) {
168 // Inherit most of the state from the parent menu item, except the role.
170 GetMenuItem()->GetAccessibleState(state);
171 state->role = ui::AccessibilityTypes::ROLE_MENUPOPUP;
174 void SubmenuView::PaintChildren(gfx::Canvas* canvas) {
175 View::PaintChildren(canvas);
177 if (drop_item_ && drop_position_ != MenuDelegate::DROP_ON)
178 PaintDropIndicator(canvas, drop_item_, drop_position_);
181 bool SubmenuView::GetDropFormats(
183 std::set<OSExchangeData::CustomFormat>* custom_formats) {
184 DCHECK(GetMenuItem()->GetMenuController());
185 return GetMenuItem()->GetMenuController()->GetDropFormats(this, formats,
189 bool SubmenuView::AreDropTypesRequired() {
190 DCHECK(GetMenuItem()->GetMenuController());
191 return GetMenuItem()->GetMenuController()->AreDropTypesRequired(this);
194 bool SubmenuView::CanDrop(const OSExchangeData& data) {
195 DCHECK(GetMenuItem()->GetMenuController());
196 return GetMenuItem()->GetMenuController()->CanDrop(this, data);
199 void SubmenuView::OnDragEntered(const ui::DropTargetEvent& event) {
200 DCHECK(GetMenuItem()->GetMenuController());
201 GetMenuItem()->GetMenuController()->OnDragEntered(this, event);
204 int SubmenuView::OnDragUpdated(const ui::DropTargetEvent& event) {
205 DCHECK(GetMenuItem()->GetMenuController());
206 return GetMenuItem()->GetMenuController()->OnDragUpdated(this, event);
209 void SubmenuView::OnDragExited() {
210 DCHECK(GetMenuItem()->GetMenuController());
211 GetMenuItem()->GetMenuController()->OnDragExited(this);
214 int SubmenuView::OnPerformDrop(const ui::DropTargetEvent& event) {
215 DCHECK(GetMenuItem()->GetMenuController());
216 return GetMenuItem()->GetMenuController()->OnPerformDrop(this, event);
219 bool SubmenuView::OnMouseWheel(const ui::MouseWheelEvent& e) {
220 gfx::Rect vis_bounds = GetVisibleBounds();
221 int menu_item_count = GetMenuItemCount();
222 if (vis_bounds.height() == height() || !menu_item_count) {
223 // All menu items are visible, nothing to scroll.
227 // Find the index of the first menu item whose y-coordinate is >= visible
230 while ((i < menu_item_count) && (GetMenuItemAt(i)->y() < vis_bounds.y()))
232 if (i == menu_item_count)
234 int first_vis_index = std::max(0,
235 (GetMenuItemAt(i)->y() == vis_bounds.y()) ? i : i - 1);
237 // If the first item isn't entirely visible, make it visible, otherwise make
238 // the next/previous one entirely visible. If enough wasn't scrolled to show
239 // any new rows, then just scroll the amount so that smooth scrolling using
240 // the trackpad is possible.
241 int delta = abs(e.y_offset() / ui::MouseWheelEvent::kWheelDelta);
243 return OnScroll(0, e.y_offset());
244 for (bool scroll_up = (e.y_offset() > 0); delta != 0; --delta) {
247 if (GetMenuItemAt(first_vis_index)->y() == vis_bounds.y()) {
248 if (first_vis_index == 0)
252 scroll_target = GetMenuItemAt(first_vis_index)->y();
254 if (first_vis_index + 1 == menu_item_count)
256 scroll_target = GetMenuItemAt(first_vis_index + 1)->y();
257 if (GetMenuItemAt(first_vis_index)->y() == vis_bounds.y())
260 ScrollRectToVisible(gfx::Rect(gfx::Point(0, scroll_target),
262 vis_bounds = GetVisibleBounds();
268 void SubmenuView::OnGestureEvent(ui::GestureEvent* event) {
270 switch (event->type()) {
271 case ui::ET_GESTURE_SCROLL_BEGIN:
272 scroll_animator_->Stop();
274 case ui::ET_GESTURE_SCROLL_UPDATE:
275 handled = OnScroll(0, event->details().scroll_y());
277 case ui::ET_GESTURE_SCROLL_END:
279 case ui::ET_SCROLL_FLING_START:
280 if (event->details().velocity_y() != 0.0f)
281 scroll_animator_->Start(0, event->details().velocity_y());
283 case ui::ET_GESTURE_TAP_DOWN:
284 case ui::ET_SCROLL_FLING_CANCEL:
285 if (scroll_animator_->is_scrolling())
286 scroll_animator_->Stop();
298 bool SubmenuView::IsShowing() {
299 return host_ && host_->IsMenuHostVisible();
302 void SubmenuView::ShowAt(Widget* parent,
303 const gfx::Rect& bounds,
306 host_->ShowMenuHost(do_capture);
308 host_ = new MenuHost(this);
309 // Force construction of the scroll view container.
310 GetScrollViewContainer();
311 // Force a layout since our preferred size may not have changed but our
314 host_->InitMenuHost(parent, bounds, scroll_view_container_, do_capture);
317 GetScrollViewContainer()->NotifyAccessibilityEvent(
318 ui::AccessibilityTypes::EVENT_MENUSTART,
320 NotifyAccessibilityEvent(
321 ui::AccessibilityTypes::EVENT_MENUPOPUPSTART,
325 void SubmenuView::Reposition(const gfx::Rect& bounds) {
327 host_->SetMenuHostBounds(bounds);
330 void SubmenuView::Close() {
332 NotifyAccessibilityEvent(ui::AccessibilityTypes::EVENT_MENUPOPUPEND, true);
333 GetScrollViewContainer()->NotifyAccessibilityEvent(
334 ui::AccessibilityTypes::EVENT_MENUEND, true);
336 host_->DestroyMenuHost();
341 void SubmenuView::Hide() {
343 host_->HideMenuHost();
344 if (scroll_animator_->is_scrolling())
345 scroll_animator_->Stop();
348 void SubmenuView::ReleaseCapture() {
350 host_->ReleaseMenuHostCapture();
353 bool SubmenuView::SkipDefaultKeyEventProcessing(const ui::KeyEvent& e) {
354 return views::FocusManager::IsTabTraversalKeyEvent(e);
357 MenuItemView* SubmenuView::GetMenuItem() const {
358 return parent_menu_item_;
361 void SubmenuView::SetDropMenuItem(MenuItemView* item,
362 MenuDelegate::DropPosition position) {
363 if (drop_item_ == item && drop_position_ == position)
365 SchedulePaintForDropIndicator(drop_item_, drop_position_);
367 drop_position_ = position;
368 SchedulePaintForDropIndicator(drop_item_, drop_position_);
371 bool SubmenuView::GetShowSelection(MenuItemView* item) {
372 if (drop_item_ == NULL)
374 // Something is being dropped on one of this menus items. Show the
375 // selection if the drop is on the passed in item and the drop position is
377 return (drop_item_ == item && drop_position_ == MenuDelegate::DROP_ON);
380 MenuScrollViewContainer* SubmenuView::GetScrollViewContainer() {
381 if (!scroll_view_container_) {
382 scroll_view_container_ = new MenuScrollViewContainer(this);
383 // Otherwise MenuHost would delete us.
384 scroll_view_container_->set_owned_by_client();
386 return scroll_view_container_;
389 void SubmenuView::MenuHostDestroyed() {
391 GetMenuItem()->GetMenuController()->Cancel(MenuController::EXIT_DESTROYED);
394 const char* SubmenuView::GetClassName() const {
395 return kViewClassName;
398 void SubmenuView::OnBoundsChanged(const gfx::Rect& previous_bounds) {
402 void SubmenuView::PaintDropIndicator(gfx::Canvas* canvas,
404 MenuDelegate::DropPosition position) {
405 if (position == MenuDelegate::DROP_NONE)
408 gfx::Rect bounds = CalculateDropIndicatorBounds(item, position);
409 canvas->FillRect(bounds, kDropIndicatorColor);
412 void SubmenuView::SchedulePaintForDropIndicator(
414 MenuDelegate::DropPosition position) {
418 if (position == MenuDelegate::DROP_ON) {
419 item->SchedulePaint();
420 } else if (position != MenuDelegate::DROP_NONE) {
421 SchedulePaintInRect(CalculateDropIndicatorBounds(item, position));
425 gfx::Rect SubmenuView::CalculateDropIndicatorBounds(
427 MenuDelegate::DropPosition position) {
428 DCHECK(position != MenuDelegate::DROP_NONE);
429 gfx::Rect item_bounds = item->bounds();
431 case MenuDelegate::DROP_BEFORE:
432 item_bounds.Offset(0, -kDropIndicatorHeight / 2);
433 item_bounds.set_height(kDropIndicatorHeight);
436 case MenuDelegate::DROP_AFTER:
437 item_bounds.Offset(0, item_bounds.height() - kDropIndicatorHeight / 2);
438 item_bounds.set_height(kDropIndicatorHeight);
442 // Don't render anything for on.
447 bool SubmenuView::OnScroll(float dx, float dy) {
448 const gfx::Rect& vis_bounds = GetVisibleBounds();
449 const gfx::Rect& full_bounds = bounds();
450 int x = vis_bounds.x();
451 float y_f = vis_bounds.y() - dy - roundoff_error_;
452 int y = gfx::ToRoundedInt(y_f);
453 roundoff_error_ = y - y_f;
454 // clamp y to [0, full_height - vis_height)
455 y = std::min(y, full_bounds.height() - vis_bounds.height() - 1);
457 gfx::Rect new_vis_bounds(x, y, vis_bounds.width(), vis_bounds.height());
458 if (new_vis_bounds != vis_bounds) {
459 ScrollRectToVisible(new_vis_bounds);