base_scroll_bar.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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. 4 5#include "ui/views/controls/scrollbar/base_scroll_bar.h" 6 7#include "base/bind.h" 8#include "base/bind_helpers.h" 9#include "base/callback.h" 10#include "base/compiler_specific.h" 11#include "base/message_loop/message_loop.h" 12#include "base/strings/string16.h" 13#include "base/strings/utf_string_conversions.h" 14#include "build/build_config.h" 15#include "grit/ui_strings.h" 16#include "ui/base/l10n/l10n_util.h" 17#include "ui/events/event.h" 18#include "ui/events/keycodes/keyboard_codes.h" 19#include "ui/gfx/canvas.h" 20#include "ui/gfx/geometry/safe_integer_conversions.h" 21#include "ui/views/controls/menu/menu_item_view.h" 22#include "ui/views/controls/menu/menu_runner.h" 23#include "ui/views/controls/scroll_view.h" 24#include "ui/views/controls/scrollbar/base_scroll_bar_thumb.h" 25#include "ui/views/widget/widget.h" 26 27#if defined(OS_LINUX) 28#include "ui/gfx/screen.h" 29#endif 30 31#undef min 32#undef max 33 34namespace views { 35 36/////////////////////////////////////////////////////////////////////////////// 37// BaseScrollBar, public: 38 39BaseScrollBar::BaseScrollBar(bool horizontal, BaseScrollBarThumb* thumb) 40 : ScrollBar(horizontal), 41 thumb_(thumb), 42 contents_size_(0), 43 contents_scroll_offset_(0), 44 viewport_size_(0), 45 thumb_track_state_(CustomButton::STATE_NORMAL), 46 last_scroll_amount_(SCROLL_NONE), 47 repeater_(base::Bind(&BaseScrollBar::TrackClicked, 48 base::Unretained(this))), 49 context_menu_mouse_position_(0) { 50 AddChildView(thumb_); 51 52 set_context_menu_controller(this); 53 thumb_->set_context_menu_controller(this); 54} 55 56void BaseScrollBar::ScrollByAmount(ScrollAmount amount) { 57 int offset = contents_scroll_offset_; 58 switch (amount) { 59 case SCROLL_START: 60 offset = GetMinPosition(); 61 break; 62 case SCROLL_END: 63 offset = GetMaxPosition(); 64 break; 65 case SCROLL_PREV_LINE: 66 offset -= GetScrollIncrement(false, false); 67 offset = std::max(GetMinPosition(), offset); 68 break; 69 case SCROLL_NEXT_LINE: 70 offset += GetScrollIncrement(false, true); 71 offset = std::min(GetMaxPosition(), offset); 72 break; 73 case SCROLL_PREV_PAGE: 74 offset -= GetScrollIncrement(true, false); 75 offset = std::max(GetMinPosition(), offset); 76 break; 77 case SCROLL_NEXT_PAGE: 78 offset += GetScrollIncrement(true, true); 79 offset = std::min(GetMaxPosition(), offset); 80 break; 81 default: 82 break; 83 } 84 contents_scroll_offset_ = offset; 85 ScrollContentsToOffset(); 86} 87 88BaseScrollBar::~BaseScrollBar() { 89} 90 91void BaseScrollBar::ScrollToThumbPosition(int thumb_position, 92 bool scroll_to_middle) { 93 contents_scroll_offset_ = 94 CalculateContentsOffset(thumb_position, scroll_to_middle); 95 if (contents_scroll_offset_ < GetMinPosition()) { 96 contents_scroll_offset_ = GetMinPosition(); 97 } else if (contents_scroll_offset_ > GetMaxPosition()) { 98 contents_scroll_offset_ = GetMaxPosition(); 99 } 100 ScrollContentsToOffset(); 101 SchedulePaint(); 102} 103 104bool BaseScrollBar::ScrollByContentsOffset(int contents_offset) { 105 int old_offset = contents_scroll_offset_; 106 contents_scroll_offset_ -= contents_offset; 107 if (contents_scroll_offset_ < GetMinPosition()) { 108 contents_scroll_offset_ = GetMinPosition(); 109 } else if (contents_scroll_offset_ > GetMaxPosition()) { 110 contents_scroll_offset_ = GetMaxPosition(); 111 } 112 if (old_offset == contents_scroll_offset_) 113 return false; 114 115 ScrollContentsToOffset(); 116 return true; 117} 118 119void BaseScrollBar::OnThumbStateChanged(CustomButton::ButtonState old_state, 120 CustomButton::ButtonState new_state) { 121 if (old_state == CustomButton::STATE_PRESSED && 122 new_state == CustomButton::STATE_NORMAL && 123 GetThumbTrackState() == CustomButton::STATE_HOVERED) { 124 SetThumbTrackState(CustomButton::STATE_NORMAL); 125 } 126} 127 128/////////////////////////////////////////////////////////////////////////////// 129// BaseScrollBar, View implementation: 130 131bool BaseScrollBar::OnMousePressed(const ui::MouseEvent& event) { 132 if (event.IsOnlyLeftMouseButton()) 133 ProcessPressEvent(event); 134 return true; 135} 136 137void BaseScrollBar::OnMouseReleased(const ui::MouseEvent& event) { 138 SetState(HitTestPoint(event.location()) ? 139 CustomButton::STATE_HOVERED : CustomButton::STATE_NORMAL); 140} 141 142void BaseScrollBar::OnMouseCaptureLost() { 143 SetState(CustomButton::STATE_NORMAL); 144} 145 146void BaseScrollBar::OnMouseEntered(const ui::MouseEvent& event) { 147 SetThumbTrackState(CustomButton::STATE_HOVERED); 148} 149 150void BaseScrollBar::OnMouseExited(const ui::MouseEvent& event) { 151 if (GetThumbTrackState() == CustomButton::STATE_HOVERED) 152 SetState(CustomButton::STATE_NORMAL); 153} 154 155bool BaseScrollBar::OnKeyPressed(const ui::KeyEvent& event) { 156 ScrollAmount amount = SCROLL_NONE; 157 switch (event.key_code()) { 158 case ui::VKEY_UP: 159 if (!IsHorizontal()) 160 amount = SCROLL_PREV_LINE; 161 break; 162 case ui::VKEY_DOWN: 163 if (!IsHorizontal()) 164 amount = SCROLL_NEXT_LINE; 165 break; 166 case ui::VKEY_LEFT: 167 if (IsHorizontal()) 168 amount = SCROLL_PREV_LINE; 169 break; 170 case ui::VKEY_RIGHT: 171 if (IsHorizontal()) 172 amount = SCROLL_NEXT_LINE; 173 break; 174 case ui::VKEY_PRIOR: 175 amount = SCROLL_PREV_PAGE; 176 break; 177 case ui::VKEY_NEXT: 178 amount = SCROLL_NEXT_PAGE; 179 break; 180 case ui::VKEY_HOME: 181 amount = SCROLL_START; 182 break; 183 case ui::VKEY_END: 184 amount = SCROLL_END; 185 break; 186 default: 187 break; 188 } 189 if (amount != SCROLL_NONE) { 190 ScrollByAmount(amount); 191 return true; 192 } 193 return false; 194} 195 196bool BaseScrollBar::OnMouseWheel(const ui::MouseWheelEvent& event) { 197 ScrollByContentsOffset(event.y_offset()); 198 return true; 199} 200 201void BaseScrollBar::OnGestureEvent(ui::GestureEvent* event) { 202 // If a fling is in progress, then stop the fling for any incoming gesture 203 // event (except for the GESTURE_END event that is generated at the end of the 204 // fling). 205 if (scroll_animator_.get() && scroll_animator_->is_scrolling() && 206 (event->type() != ui::ET_GESTURE_END || 207 event->details().touch_points() > 1)) { 208 scroll_animator_->Stop(); 209 } 210 211 if (event->type() == ui::ET_GESTURE_TAP_DOWN) { 212 ProcessPressEvent(*event); 213 event->SetHandled(); 214 return; 215 } 216 217 if (event->type() == ui::ET_GESTURE_LONG_PRESS) { 218 // For a long-press, the repeater started in tap-down should continue. So 219 // return early. 220 return; 221 } 222 223 SetState(CustomButton::STATE_NORMAL); 224 225 if (event->type() == ui::ET_GESTURE_TAP) { 226 // TAP_DOWN would have already scrolled some amount. So scrolling again on 227 // TAP is not necessary. 228 event->SetHandled(); 229 return; 230 } 231 232 if (event->type() == ui::ET_GESTURE_SCROLL_BEGIN || 233 event->type() == ui::ET_GESTURE_SCROLL_END) { 234 event->SetHandled(); 235 return; 236 } 237 238 if (event->type() == ui::ET_GESTURE_SCROLL_UPDATE) { 239 float scroll_amount_f; 240 int scroll_amount; 241 if (IsHorizontal()) { 242 scroll_amount_f = event->details().scroll_x() - roundoff_error_.x(); 243 scroll_amount = gfx::ToRoundedInt(scroll_amount_f); 244 roundoff_error_.set_x(scroll_amount - scroll_amount_f); 245 } else { 246 scroll_amount_f = event->details().scroll_y() - roundoff_error_.y(); 247 scroll_amount = gfx::ToRoundedInt(scroll_amount_f); 248 roundoff_error_.set_y(scroll_amount - scroll_amount_f); 249 } 250 if (ScrollByContentsOffset(scroll_amount)) 251 event->SetHandled(); 252 return; 253 } 254 255 if (event->type() == ui::ET_SCROLL_FLING_START) { 256 if (!scroll_animator_.get()) 257 scroll_animator_.reset(new ScrollAnimator(this)); 258 scroll_animator_->Start( 259 IsHorizontal() ? event->details().velocity_x() : 0.f, 260 IsHorizontal() ? 0.f : event->details().velocity_y()); 261 event->SetHandled(); 262 } 263} 264 265/////////////////////////////////////////////////////////////////////////////// 266// BaseScrollBar, ScrollDelegate implementation: 267 268bool BaseScrollBar::OnScroll(float dx, float dy) { 269 return IsHorizontal() ? ScrollByContentsOffset(dx) : 270 ScrollByContentsOffset(dy); 271} 272 273/////////////////////////////////////////////////////////////////////////////// 274// BaseScrollBar, ContextMenuController implementation: 275 276enum ScrollBarContextMenuCommands { 277 ScrollBarContextMenuCommand_ScrollHere = 1, 278 ScrollBarContextMenuCommand_ScrollStart, 279 ScrollBarContextMenuCommand_ScrollEnd, 280 ScrollBarContextMenuCommand_ScrollPageUp, 281 ScrollBarContextMenuCommand_ScrollPageDown, 282 ScrollBarContextMenuCommand_ScrollPrev, 283 ScrollBarContextMenuCommand_ScrollNext 284}; 285 286void BaseScrollBar::ShowContextMenuForView(View* source, 287 const gfx::Point& p, 288 ui::MenuSourceType source_type) { 289 Widget* widget = GetWidget(); 290 gfx::Rect widget_bounds = widget->GetWindowBoundsInScreen(); 291 gfx::Point temp_pt(p.x() - widget_bounds.x(), p.y() - widget_bounds.y()); 292 View::ConvertPointFromWidget(this, &temp_pt); 293 context_menu_mouse_position_ = IsHorizontal() ? temp_pt.x() : temp_pt.y(); 294 295 views::MenuItemView* menu = new views::MenuItemView(this); 296 // MenuRunner takes ownership of |menu|. 297 menu_runner_.reset(new MenuRunner(menu)); 298 menu->AppendDelegateMenuItem(ScrollBarContextMenuCommand_ScrollHere); 299 menu->AppendSeparator(); 300 menu->AppendDelegateMenuItem(ScrollBarContextMenuCommand_ScrollStart); 301 menu->AppendDelegateMenuItem(ScrollBarContextMenuCommand_ScrollEnd); 302 menu->AppendSeparator(); 303 menu->AppendDelegateMenuItem(ScrollBarContextMenuCommand_ScrollPageUp); 304 menu->AppendDelegateMenuItem(ScrollBarContextMenuCommand_ScrollPageDown); 305 menu->AppendSeparator(); 306 menu->AppendDelegateMenuItem(ScrollBarContextMenuCommand_ScrollPrev); 307 menu->AppendDelegateMenuItem(ScrollBarContextMenuCommand_ScrollNext); 308 if (menu_runner_->RunMenuAt(GetWidget(), NULL, gfx::Rect(p, gfx::Size()), 309 views::MenuItemView::TOPLEFT, source_type, MenuRunner::HAS_MNEMONICS | 310 views::MenuRunner::CONTEXT_MENU) == 311 MenuRunner::MENU_DELETED) 312 return; 313} 314 315/////////////////////////////////////////////////////////////////////////////// 316// BaseScrollBar, Menu::Delegate implementation: 317 318base::string16 BaseScrollBar::GetLabel(int id) const { 319 int ids_value = 0; 320 switch (id) { 321 case ScrollBarContextMenuCommand_ScrollHere: 322 ids_value = IDS_APP_SCROLLBAR_CXMENU_SCROLLHERE; 323 break; 324 case ScrollBarContextMenuCommand_ScrollStart: 325 ids_value = IsHorizontal() ? IDS_APP_SCROLLBAR_CXMENU_SCROLLLEFTEDGE 326 : IDS_APP_SCROLLBAR_CXMENU_SCROLLHOME; 327 break; 328 case ScrollBarContextMenuCommand_ScrollEnd: 329 ids_value = IsHorizontal() ? IDS_APP_SCROLLBAR_CXMENU_SCROLLRIGHTEDGE 330 : IDS_APP_SCROLLBAR_CXMENU_SCROLLEND; 331 break; 332 case ScrollBarContextMenuCommand_ScrollPageUp: 333 ids_value = IDS_APP_SCROLLBAR_CXMENU_SCROLLPAGEUP; 334 break; 335 case ScrollBarContextMenuCommand_ScrollPageDown: 336 ids_value = IDS_APP_SCROLLBAR_CXMENU_SCROLLPAGEDOWN; 337 break; 338 case ScrollBarContextMenuCommand_ScrollPrev: 339 ids_value = IsHorizontal() ? IDS_APP_SCROLLBAR_CXMENU_SCROLLLEFT 340 : IDS_APP_SCROLLBAR_CXMENU_SCROLLUP; 341 break; 342 case ScrollBarContextMenuCommand_ScrollNext: 343 ids_value = IsHorizontal() ? IDS_APP_SCROLLBAR_CXMENU_SCROLLRIGHT 344 : IDS_APP_SCROLLBAR_CXMENU_SCROLLDOWN; 345 break; 346 default: 347 NOTREACHED() << "Invalid BaseScrollBar Context Menu command!"; 348 } 349 350 return ids_value ? l10n_util::GetStringUTF16(ids_value) : base::string16(); 351} 352 353bool BaseScrollBar::IsCommandEnabled(int id) const { 354 switch (id) { 355 case ScrollBarContextMenuCommand_ScrollPageUp: 356 case ScrollBarContextMenuCommand_ScrollPageDown: 357 return !IsHorizontal(); 358 } 359 return true; 360} 361 362void BaseScrollBar::ExecuteCommand(int id) { 363 switch (id) { 364 case ScrollBarContextMenuCommand_ScrollHere: 365 ScrollToThumbPosition(context_menu_mouse_position_, true); 366 break; 367 case ScrollBarContextMenuCommand_ScrollStart: 368 ScrollByAmount(SCROLL_START); 369 break; 370 case ScrollBarContextMenuCommand_ScrollEnd: 371 ScrollByAmount(SCROLL_END); 372 break; 373 case ScrollBarContextMenuCommand_ScrollPageUp: 374 ScrollByAmount(SCROLL_PREV_PAGE); 375 break; 376 case ScrollBarContextMenuCommand_ScrollPageDown: 377 ScrollByAmount(SCROLL_NEXT_PAGE); 378 break; 379 case ScrollBarContextMenuCommand_ScrollPrev: 380 ScrollByAmount(SCROLL_PREV_LINE); 381 break; 382 case ScrollBarContextMenuCommand_ScrollNext: 383 ScrollByAmount(SCROLL_NEXT_LINE); 384 break; 385 } 386} 387 388/////////////////////////////////////////////////////////////////////////////// 389// BaseScrollBar, ScrollBar implementation: 390 391void BaseScrollBar::Update(int viewport_size, int content_size, 392 int contents_scroll_offset) { 393 ScrollBar::Update(viewport_size, content_size, contents_scroll_offset); 394 395 // Make sure contents_size is always > 0 to avoid divide by zero errors in 396 // calculations throughout this code. 397 contents_size_ = std::max(1, content_size); 398 399 viewport_size_ = std::max(1, viewport_size); 400 401 if (content_size < 0) 402 content_size = 0; 403 if (contents_scroll_offset < 0) 404 contents_scroll_offset = 0; 405 if (contents_scroll_offset > content_size) 406 contents_scroll_offset = content_size; 407 408 // Thumb Height and Thumb Pos. 409 // The height of the thumb is the ratio of the Viewport height to the 410 // content size multiplied by the height of the thumb track. 411 double ratio = static_cast<double>(viewport_size) / contents_size_; 412 int thumb_size = static_cast<int>(ratio * GetTrackSize()); 413 thumb_->SetSize(thumb_size); 414 415 int thumb_position = CalculateThumbPosition(contents_scroll_offset); 416 thumb_->SetPosition(thumb_position); 417} 418 419int BaseScrollBar::GetPosition() const { 420 return thumb_->GetPosition(); 421} 422 423/////////////////////////////////////////////////////////////////////////////// 424// BaseScrollBar, protected: 425 426BaseScrollBarThumb* BaseScrollBar::GetThumb() const { 427 return thumb_; 428} 429 430CustomButton::ButtonState BaseScrollBar::GetThumbTrackState() const { 431 return thumb_track_state_; 432} 433 434void BaseScrollBar::ScrollToPosition(int position) { 435 controller()->ScrollToPosition(this, position); 436} 437 438int BaseScrollBar::GetScrollIncrement(bool is_page, bool is_positive) { 439 return controller()->GetScrollIncrement(this, is_page, is_positive); 440} 441 442/////////////////////////////////////////////////////////////////////////////// 443// BaseScrollBar, private: 444 445int BaseScrollBar::GetThumbSizeForTest() { 446 return thumb_->GetSize(); 447} 448 449void BaseScrollBar::ProcessPressEvent(const ui::LocatedEvent& event) { 450 SetThumbTrackState(CustomButton::STATE_PRESSED); 451 gfx::Rect thumb_bounds = thumb_->bounds(); 452 if (IsHorizontal()) { 453 if (GetMirroredXInView(event.x()) < thumb_bounds.x()) { 454 last_scroll_amount_ = SCROLL_PREV_PAGE; 455 } else if (GetMirroredXInView(event.x()) > thumb_bounds.right()) { 456 last_scroll_amount_ = SCROLL_NEXT_PAGE; 457 } 458 } else { 459 if (event.y() < thumb_bounds.y()) { 460 last_scroll_amount_ = SCROLL_PREV_PAGE; 461 } else if (event.y() > thumb_bounds.bottom()) { 462 last_scroll_amount_ = SCROLL_NEXT_PAGE; 463 } 464 } 465 TrackClicked(); 466 repeater_.Start(); 467} 468 469void BaseScrollBar::SetState(CustomButton::ButtonState state) { 470 SetThumbTrackState(state); 471 repeater_.Stop(); 472} 473 474void BaseScrollBar::TrackClicked() { 475 if (last_scroll_amount_ != SCROLL_NONE) 476 ScrollByAmount(last_scroll_amount_); 477} 478 479void BaseScrollBar::ScrollContentsToOffset() { 480 ScrollToPosition(contents_scroll_offset_); 481 thumb_->SetPosition(CalculateThumbPosition(contents_scroll_offset_)); 482} 483 484int BaseScrollBar::GetTrackSize() const { 485 gfx::Rect track_bounds = GetTrackBounds(); 486 return IsHorizontal() ? track_bounds.width() : track_bounds.height(); 487} 488 489int BaseScrollBar::CalculateThumbPosition(int contents_scroll_offset) const { 490 // In some combination of viewport_size and contents_size_, the result of 491 // simple division can be rounded and there could be 1 pixel gap even when the 492 // contents scroll down to the bottom. See crbug.com/244671 493 if (contents_scroll_offset + viewport_size_ == contents_size_) { 494 int track_size = GetTrackSize(); 495 return track_size - (viewport_size_ * GetTrackSize() / contents_size_); 496 } 497 return (contents_scroll_offset * GetTrackSize()) / contents_size_; 498} 499 500int BaseScrollBar::CalculateContentsOffset(int thumb_position, 501 bool scroll_to_middle) const { 502 if (scroll_to_middle) 503 thumb_position = thumb_position - (thumb_->GetSize() / 2); 504 return (thumb_position * contents_size_) / GetTrackSize(); 505} 506 507void BaseScrollBar::SetThumbTrackState(CustomButton::ButtonState state) { 508 thumb_track_state_ = state; 509 SchedulePaint(); 510} 511 512} // namespace views 513