OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 "views/controls/scrollbar/base_scroll_bar_thumb.h" |
| 6 |
| 7 #include "views/controls/scrollbar/base_scroll_bar.h" |
| 8 #include "ui/gfx/canvas.h" |
| 9 #include "ui/gfx/rect.h" |
| 10 |
| 11 namespace { |
| 12 // The distance the mouse can be dragged outside the bounds of the thumb during |
| 13 // dragging before the scrollbar will snap back to its regular position. |
| 14 static const int kScrollThumbDragOutSnap = 100; |
| 15 } |
| 16 |
| 17 namespace views { |
| 18 |
| 19 BaseScrollBarThumb::BaseScrollBarThumb(BaseScrollBar* scroll_bar) |
| 20 : scroll_bar_(scroll_bar), |
| 21 drag_start_position_(-1), |
| 22 mouse_offset_(-1), |
| 23 state_(CustomButton::BS_NORMAL) { |
| 24 } |
| 25 |
| 26 BaseScrollBarThumb::~BaseScrollBarThumb() { |
| 27 } |
| 28 |
| 29 void BaseScrollBarThumb::SetSize(int size) { |
| 30 // Make sure the thumb is never sized smaller than its minimum possible |
| 31 // display size. |
| 32 gfx::Size prefsize = GetPreferredSize(); |
| 33 size = std::max(size, scroll_bar_->IsHorizontal() ? prefsize.width() : |
| 34 prefsize.height()); |
| 35 gfx::Rect thumb_bounds = bounds(); |
| 36 if (scroll_bar_->IsHorizontal()) { |
| 37 thumb_bounds.set_width(size); |
| 38 } else { |
| 39 thumb_bounds.set_height(size); |
| 40 } |
| 41 SetBoundsRect(thumb_bounds); |
| 42 } |
| 43 |
| 44 int BaseScrollBarThumb::GetSize() const { |
| 45 if (scroll_bar_->IsHorizontal()) |
| 46 return width(); |
| 47 return height(); |
| 48 } |
| 49 |
| 50 void BaseScrollBarThumb::SetPosition(int position) { |
| 51 gfx::Rect thumb_bounds = bounds(); |
| 52 gfx::Rect track_bounds = scroll_bar_->GetTrackBounds(); |
| 53 if (scroll_bar_->IsHorizontal()) { |
| 54 thumb_bounds.set_x(track_bounds.x() + position); |
| 55 } else { |
| 56 thumb_bounds.set_y(track_bounds.y() + position); |
| 57 } |
| 58 SetBoundsRect(thumb_bounds); |
| 59 } |
| 60 |
| 61 int BaseScrollBarThumb::GetPosition() const { |
| 62 gfx::Rect track_bounds = scroll_bar_->GetTrackBounds(); |
| 63 if (scroll_bar_->IsHorizontal()) |
| 64 return x() - track_bounds.x(); |
| 65 return y() - track_bounds.y(); |
| 66 } |
| 67 |
| 68 void BaseScrollBarThumb::OnMouseEntered(const MouseEvent& event) { |
| 69 SetState(CustomButton::BS_HOT); |
| 70 } |
| 71 |
| 72 void BaseScrollBarThumb::OnMouseExited(const MouseEvent& event) { |
| 73 SetState(CustomButton::BS_NORMAL); |
| 74 } |
| 75 |
| 76 bool BaseScrollBarThumb::OnMousePressed(const MouseEvent& event) { |
| 77 mouse_offset_ = scroll_bar_->IsHorizontal() ? event.x() : event.y(); |
| 78 drag_start_position_ = GetPosition(); |
| 79 SetState(CustomButton::BS_PUSHED); |
| 80 return true; |
| 81 } |
| 82 |
| 83 bool BaseScrollBarThumb::OnMouseDragged(const MouseEvent& event) { |
| 84 // If the user moves the mouse more than |kScrollThumbDragOutSnap| outside |
| 85 // the bounds of the thumb, the scrollbar will snap the scroll back to the |
| 86 // point it was at before the drag began. |
| 87 if (scroll_bar_->IsHorizontal()) { |
| 88 if ((event.y() < y() - kScrollThumbDragOutSnap) || |
| 89 (event.y() > (y() + height() + kScrollThumbDragOutSnap))) { |
| 90 scroll_bar_->ScrollToThumbPosition(drag_start_position_, false); |
| 91 return true; |
| 92 } |
| 93 } else { |
| 94 if ((event.x() < x() - kScrollThumbDragOutSnap) || |
| 95 (event.x() > (x() + width() + kScrollThumbDragOutSnap))) { |
| 96 scroll_bar_->ScrollToThumbPosition(drag_start_position_, false); |
| 97 return true; |
| 98 } |
| 99 } |
| 100 if (scroll_bar_->IsHorizontal()) { |
| 101 int thumb_x = event.x() - mouse_offset_; |
| 102 scroll_bar_->ScrollToThumbPosition(GetPosition() + thumb_x, false); |
| 103 } else { |
| 104 int thumb_y = event.y() - mouse_offset_; |
| 105 scroll_bar_->ScrollToThumbPosition(GetPosition() + thumb_y, false); |
| 106 } |
| 107 return true; |
| 108 } |
| 109 |
| 110 void BaseScrollBarThumb::OnMouseReleased(const MouseEvent& event) { |
| 111 OnMouseCaptureLost(); |
| 112 } |
| 113 |
| 114 void BaseScrollBarThumb::OnMouseCaptureLost() { |
| 115 SetState(CustomButton::BS_HOT); |
| 116 } |
| 117 |
| 118 CustomButton::ButtonState BaseScrollBarThumb::GetState() const { |
| 119 return state_; |
| 120 } |
| 121 |
| 122 void BaseScrollBarThumb::SetState(CustomButton::ButtonState state) { |
| 123 state_ = state; |
| 124 SchedulePaint(); |
| 125 } |
| 126 |
| 127 } // namespace views |
OLD | NEW |