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