diff options
Diffstat (limited to 'views/controls/menu')
-rw-r--r-- | views/controls/menu/menu.h | 12 | ||||
-rw-r--r-- | views/controls/menu/menu_controller.cc | 64 | ||||
-rw-r--r-- | views/controls/menu/menu_controller.h | 8 | ||||
-rw-r--r-- | views/controls/menu/menu_delegate.h | 9 | ||||
-rw-r--r-- | views/controls/menu/menu_item_view.cc | 2 | ||||
-rw-r--r-- | views/controls/menu/menu_item_view.h | 2 | ||||
-rw-r--r-- | views/controls/menu/menu_scroll_view_container.cc | 15 | ||||
-rw-r--r-- | views/controls/menu/menu_win.cc | 4 | ||||
-rw-r--r-- | views/controls/menu/submenu_view.cc | 50 |
9 files changed, 66 insertions, 100 deletions
diff --git a/views/controls/menu/menu.h b/views/controls/menu/menu.h index e3e1c78..624376c 100644 --- a/views/controls/menu/menu.h +++ b/views/controls/menu/menu.h @@ -1,4 +1,4 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Copyright (c) 2010 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -10,6 +10,9 @@ #include "views/controls/menu/controller.h" class SkBitmap; +namespace gfx { +class Point; +} namespace views { @@ -71,13 +74,12 @@ class Menu { // user does the appropriate gesture to show a context menu. The id // identifies the id of the menu to show the context menu for. // is_mouse_gesture is true if this is the result of a mouse gesture. - // If this is not the result of a mouse gesture x/y is the recommended - // location to display the content menu at. In either case, x/y is in + // If this is not the result of a mouse gesture |p| is the recommended + // location to display the content menu at. In either case, |p| is in // screen coordinates. virtual void ShowContextMenu(Menu* source, int id, - int x, - int y, + const gfx::Point& p, bool is_mouse_gesture) { } diff --git a/views/controls/menu/menu_controller.cc b/views/controls/menu/menu_controller.cc index fb35908..49f68ed 100644 --- a/views/controls/menu/menu_controller.cc +++ b/views/controls/menu/menu_controller.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Copyright (c) 2010 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -42,7 +42,7 @@ namespace views { // Convenience for scrolling the view such that the origin is visible. static void ScrollToVisible(View* view) { - view->ScrollRectToVisible(0, 0, view->width(), view->height()); + view->ScrollRectToVisible(gfx::Rect(gfx::Point(), view->size())); } // MenuScrollTask -------------------------------------------------------------- @@ -97,14 +97,10 @@ class MenuController::MenuScrollTask { const int delta_y = static_cast<int>( (Time::Now() - start_scroll_time_).InMilliseconds() * pixels_per_second_ / 1000); - int target_y = start_y_; - if (is_scrolling_up_) - target_y = std::max(0, target_y - delta_y); - else - target_y = std::min(submenu_->height() - vis_rect.height(), - target_y + delta_y); - submenu_->ScrollRectToVisible(vis_rect.x(), target_y, vis_rect.width(), - vis_rect.height()); + vis_rect.set_y(is_scrolling_up_ ? + std::max(0, start_y_ - delta_y) : + std::min(submenu_->height() - vis_rect.height(), start_y_ + delta_y)); + submenu_->ScrollRectToVisible(vis_rect); } // SubmenuView being scrolled. @@ -347,8 +343,7 @@ void MenuController::OnMousePressed(SubmenuView* source, } else { if (part.menu->GetDelegate()->CanDrag(part.menu)) { possible_drag_ = true; - press_x_ = event.x(); - press_y_ = event.y(); + press_pt_ = event.location(); } if (part.menu->HasSubmenu()) open_submenu = true; @@ -370,27 +365,23 @@ void MenuController::OnMouseDragged(SubmenuView* source, return; if (possible_drag_) { - if (View::ExceededDragThreshold(event.x() - press_x_, - event.y() - press_y_)) { + if (View::ExceededDragThreshold(event.x() - press_pt_.x(), + event.y() - press_pt_.y())) { MenuItemView* item = state_.item; DCHECK(item); // Points are in the coordinates of the submenu, need to map to that of // the selected item. Additionally source may not be the parent of // the selected item, so need to map to screen first then to item. - gfx::Point press_loc(press_x_, press_y_); + gfx::Point press_loc(press_pt_); View::ConvertPointToScreen(source->GetScrollViewContainer(), &press_loc); View::ConvertPointToView(NULL, item, &press_loc); - gfx::Point drag_loc(event.location()); - View::ConvertPointToScreen(source->GetScrollViewContainer(), &drag_loc); - View::ConvertPointToView(NULL, item, &drag_loc); gfx::Canvas canvas(item->width(), item->height(), false); item->Paint(&canvas, true); OSExchangeData data; item->GetDelegate()->WriteDragData(item, &data); - drag_utils::SetDragImageOnDataObject(canvas, item->width(), - item->height(), press_loc.x(), - press_loc.y(), &data); + drag_utils::SetDragImageOnDataObject(canvas, item->size(), press_loc, + &data); StopScrolling(); int drag_ops = item->GetDelegate()->GetDragOperations(item); item->GetRootView()->StartDragForViewFromMouseEvent( @@ -437,8 +428,8 @@ void MenuController::OnMouseReleased(SubmenuView* source, View::ConvertPointToScreen(source->GetScrollViewContainer(), &loc); // If we open a context menu just return now - if (part.menu->GetDelegate()->ShowContextMenu( - part.menu, part.menu->GetCommand(), loc.x(), loc.y(), true)) + if (part.menu->GetDelegate()->ShowContextMenu(part.menu, + part.menu->GetCommand(), loc, true)) return; } @@ -521,12 +512,9 @@ int MenuController::OnDragUpdated(SubmenuView* source, gfx::Point screen_loc(event.location()); View::ConvertPointToScreen(source, &screen_loc); - if (valid_drop_coordinates_ && screen_loc.x() == drop_x_ && - screen_loc.y() == drop_y_) { + if (valid_drop_coordinates_ && screen_loc == drop_pt_) return last_drop_operation_; - } - drop_x_ = screen_loc.x(); - drop_y_ = screen_loc.y(); + drop_pt_ = screen_loc; valid_drop_coordinates_ = true; MenuItemView* menu_item = GetMenuItemAt(source, event.x(), event.y()); @@ -549,10 +537,9 @@ int MenuController::OnDragUpdated(SubmenuView* source, (menu_item_loc.y() > kDropBetweenPixels && menu_item_loc.y() < (menu_item_height - kDropBetweenPixels))) { drop_position = MenuDelegate::DROP_ON; - } else if (menu_item_loc.y() < menu_item_height / 2) { - drop_position = MenuDelegate::DROP_BEFORE; } else { - drop_position = MenuDelegate::DROP_AFTER; + drop_position = (menu_item_loc.y() < menu_item_height / 2) ? + MenuDelegate::DROP_BEFORE : MenuDelegate::DROP_AFTER; } query_menu_item = menu_item; } else { @@ -562,17 +549,12 @@ int MenuController::OnDragUpdated(SubmenuView* source, drop_operation = menu_item->GetDelegate()->GetDropOperation( query_menu_item, event, &drop_position); - if (menu_item->HasSubmenu()) { - // The menu has a submenu, schedule the submenu to open. - SetSelection(menu_item, true, false); - } else { - SetSelection(menu_item, false, false); - } + // If the menu has a submenu, schedule the submenu to open. + SetSelection(menu_item, menu_item->HasSubmenu(), false); if (drop_position == MenuDelegate::DROP_NONE || - drop_operation == DragDropTypes::DRAG_NONE) { + drop_operation == DragDropTypes::DRAG_NONE) menu_item = NULL; - } } else { SetSelection(source->GetMenuItem(), true, false); } @@ -666,8 +648,8 @@ bool MenuController::Dispatch(const MSG& msg) { if (item && item->GetRootMenuItem() != item) { gfx::Point screen_loc(0, item->height()); View::ConvertPointToScreen(item, &screen_loc); - item->GetDelegate()->ShowContextMenu( - item, item->GetCommand(), screen_loc.x(), screen_loc.y(), false); + item->GetDelegate()->ShowContextMenu(item, item->GetCommand(), + screen_loc, false); } return true; } diff --git a/views/controls/menu/menu_controller.h b/views/controls/menu/menu_controller.h index c92f4b9..f8a459a 100644 --- a/views/controls/menu/menu_controller.h +++ b/views/controls/menu/menu_controller.h @@ -1,4 +1,4 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Copyright (c) 2010 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -386,14 +386,12 @@ class MenuController : public MessageLoopForUI::Dispatcher { bool possible_drag_; // Location the mouse was pressed at. Used to detect d&d. - int press_x_; - int press_y_; + gfx::Point press_pt_; // We get a slew of drag updated messages as the mouse is over us. To avoid // continually processing whether we can drop, we cache the coordinates. bool valid_drop_coordinates_; - int drop_x_; - int drop_y_; + gfx::Point drop_pt_; int last_drop_operation_; // If true, we're in the middle of invoking ShowAt on a submenu. diff --git a/views/controls/menu/menu_delegate.h b/views/controls/menu/menu_delegate.h index cbb295a..2ad1feb 100644 --- a/views/controls/menu/menu_delegate.h +++ b/views/controls/menu/menu_delegate.h @@ -1,4 +1,4 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Copyright (c) 2010 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -59,14 +59,13 @@ class MenuDelegate : Controller { // user does the appropriate gesture to show a context menu. The id // identifies the id of the menu to show the context menu for. // is_mouse_gesture is true if this is the result of a mouse gesture. - // If this is not the result of a mouse gesture x/y is the recommended - // location to display the content menu at. In either case, x/y is in + // If this is not the result of a mouse gesture |p| is the recommended + // location to display the content menu at. In either case, |p| is in // screen coordinates. // Returns true if a context menu was displayed, otherwise false virtual bool ShowContextMenu(MenuItemView* source, int id, - int x, - int y, + const gfx::Point& p, bool is_mouse_gesture) { return false; } diff --git a/views/controls/menu/menu_item_view.cc b/views/controls/menu/menu_item_view.cc index 2c243c4..95e22d0 100644 --- a/views/controls/menu/menu_item_view.cc +++ b/views/controls/menu/menu_item_view.cc @@ -73,7 +73,7 @@ MenuItemView::~MenuItemView() { delete submenu_; } -bool MenuItemView::GetTooltipText(int x, int y, std::wstring* tooltip) { +bool MenuItemView::GetTooltipText(const gfx::Point& p, std::wstring* tooltip) { *tooltip = tooltip_; return !tooltip_.empty(); } diff --git a/views/controls/menu/menu_item_view.h b/views/controls/menu/menu_item_view.h index 6caa09a..b342356 100644 --- a/views/controls/menu/menu_item_view.h +++ b/views/controls/menu/menu_item_view.h @@ -72,7 +72,7 @@ class MenuItemView : public View { virtual ~MenuItemView(); // Overridden from View: - virtual bool GetTooltipText(int x, int y, std::wstring* tooltip); + virtual bool GetTooltipText(const gfx::Point& p, std::wstring* tooltip); // Returns the preferred height of menu items. This is only valid when the // menu is about to be shown. diff --git a/views/controls/menu/menu_scroll_view_container.cc b/views/controls/menu/menu_scroll_view_container.cc index 64aa4ab..25817e4 100644 --- a/views/controls/menu/menu_scroll_view_container.cc +++ b/views/controls/menu/menu_scroll_view_container.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Copyright (c) 2010 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -139,16 +139,15 @@ class MenuScrollViewContainer::MenuScrollView : public View { AddChildView(child); } - virtual void ScrollRectToVisible(int x, int y, int width, int height) { + virtual void ScrollRectToVisible(const gfx::Rect& rect) { // NOTE: this assumes we only want to scroll in the y direction. + // Convert rect.y() to view's coordinates and make sure we don't show past + // the bottom of the view. View* child = GetContents(); - // Convert y to view's coordinates. - y -= child->y(); - gfx::Size pref = child->GetPreferredSize(); - // Constrain y to make sure we don't show past the bottom of the view. - y = std::max(0, std::min(pref.height() - this->height(), y)); - child->SetY(-y); + child->SetY(-std::max(0, std::min( + child->GetPreferredSize().height() - this->height(), + rect.y() - child->y()))); } // Returns the contents, which is the SubmenuView. diff --git a/views/controls/menu/menu_win.cc b/views/controls/menu/menu_win.cc index a39ec44..fd03318 100644 --- a/views/controls/menu/menu_win.cc +++ b/views/controls/menu/menu_win.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Copyright (c) 2010 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -96,7 +96,7 @@ class MenuHostWindow : public app::WindowImpl { void OnRButtonUp(UINT w_param, const CPoint& loc) { int id; if (menu_->delegate() && FindMenuIDByLocation(menu_, loc, &id)) - menu_->delegate()->ShowContextMenu(menu_, id, loc.x, loc.y, true); + menu_->delegate()->ShowContextMenu(menu_, id, gfx::Point(loc), true); } void OnMeasureItem(WPARAM w_param, MEASUREITEMSTRUCT* lpmis) { diff --git a/views/controls/menu/submenu_view.cc b/views/controls/menu/submenu_view.cc index f89f51c..14aae44 100644 --- a/views/controls/menu/submenu_view.cc +++ b/views/controls/menu/submenu_view.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Copyright (c) 2010 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -168,19 +168,13 @@ bool SubmenuView::OnMouseWheel(const MouseWheelEvent& e) { // Find the index of the first menu item whose y-coordinate is >= visible // y-coordinate. - int first_vis_index = -1; - for (int i = 0; i < menu_item_count; ++i) { - MenuItemView* menu_item = GetMenuItemAt(i); - if (menu_item->y() == vis_bounds.y()) { - first_vis_index = i; - break; - } else if (menu_item->y() > vis_bounds.y()) { - first_vis_index = std::max(0, i - 1); - break; - } - } - if (first_vis_index == -1) + int i = 0; + while ((i < menu_item_count) && (GetMenuItemAt(i)->y() < vis_bounds.y())) + ++i; + if (i == menu_item_count) return true; + int first_vis_index = std::max(0, + (GetMenuItemAt(i)->y() == vis_bounds.y()) ? i : i - 1); // If the first item isn't entirely visible, make it visible, otherwise make // the next/previous one entirely visible. @@ -189,31 +183,24 @@ bool SubmenuView::OnMouseWheel(const MouseWheelEvent& e) { #elif defined(OS_LINUX) int delta = abs(e.GetOffset()); #endif - bool scroll_up = (e.GetOffset() > 0); - while (delta-- > 0) { - int scroll_amount = 0; + for (bool scroll_up = (e.GetOffset() > 0); delta != 0; --delta) { + int scroll_target; if (scroll_up) { if (GetMenuItemAt(first_vis_index)->y() == vis_bounds.y()) { - if (first_vis_index != 0) { - scroll_amount = GetMenuItemAt(first_vis_index - 1)->y() - - vis_bounds.y(); - first_vis_index--; - } else { + if (first_vis_index == 0) break; - } - } else { - scroll_amount = GetMenuItemAt(first_vis_index)->y() - vis_bounds.y(); + first_vis_index--; } + scroll_target = GetMenuItemAt(first_vis_index)->y(); } else { - if (first_vis_index + 1 == GetMenuItemCount()) + if (first_vis_index + 1 == menu_item_count) break; - scroll_amount = GetMenuItemAt(first_vis_index + 1)->y() - - vis_bounds.y(); + scroll_target = GetMenuItemAt(first_vis_index + 1)->y(); if (GetMenuItemAt(first_vis_index)->y() == vis_bounds.y()) first_vis_index++; } - ScrollRectToVisible(0, vis_bounds.y() + scroll_amount, vis_bounds.width(), - vis_bounds.height()); + ScrollRectToVisible(gfx::Rect(gfx::Point(0, scroll_target), + vis_bounds.size())); vis_bounds = GetVisibleBounds(); } @@ -238,7 +225,7 @@ void SubmenuView::ShowAt(gfx::NativeWindow parent, // Force construction of the scroll view container. GetScrollViewContainer(); // Make sure the first row is visible. - ScrollRectToVisible(0, 0, 1, 1); + ScrollRectToVisible(gfx::Rect(gfx::Point(), gfx::Size(1, 1))); host_->Init(parent, bounds, scroll_view_container_, do_capture); } @@ -318,8 +305,7 @@ void SubmenuView::SchedulePaintForDropIndicator( if (position == MenuDelegate::DROP_ON) { item->SchedulePaint(); } else if (position != MenuDelegate::DROP_NONE) { - gfx::Rect bounds = CalculateDropIndicatorBounds(item, position); - SchedulePaint(bounds.x(), bounds.y(), bounds.width(), bounds.height()); + SchedulePaint(CalculateDropIndicatorBounds(item, position), false); } } |