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 "views/controls/scrollbar/bitmap_scroll_bar.h" | 5 #include "views/controls/scrollbar/bitmap_scroll_bar.h" |
6 | 6 |
7 #if defined(OS_LINUX) | 7 #if defined(OS_LINUX) |
8 #include "views/screen.h" | 8 #include "views/screen.h" |
9 #endif | 9 #endif |
10 | 10 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 class AutorepeatButton : public ImageButton { | 46 class AutorepeatButton : public ImageButton { |
47 public: | 47 public: |
48 AutorepeatButton(ButtonListener* listener) | 48 AutorepeatButton(ButtonListener* listener) |
49 : ImageButton(listener), | 49 : ImageButton(listener), |
50 ALLOW_THIS_IN_INITIALIZER_LIST(repeater_( | 50 ALLOW_THIS_IN_INITIALIZER_LIST(repeater_( |
51 NewCallback<AutorepeatButton>(this, | 51 NewCallback<AutorepeatButton>(this, |
52 &AutorepeatButton::NotifyClick))) { | 52 &AutorepeatButton::NotifyClick))) { |
53 } | 53 } |
54 virtual ~AutorepeatButton() {} | 54 virtual ~AutorepeatButton() {} |
55 | 55 |
56 protected: | 56 virtual bool OnMousePressed(const MouseEvent& event) OVERRIDE { |
57 virtual bool OnMousePressed(const MouseEvent& event) { | |
58 Button::NotifyClick(event); | 57 Button::NotifyClick(event); |
59 repeater_.Start(); | 58 repeater_.Start(); |
60 return true; | 59 return true; |
61 } | 60 } |
62 | 61 |
63 virtual void OnMouseReleased(const MouseEvent& event, bool canceled) { | 62 virtual void OnMouseReleased(const MouseEvent& event) OVERRIDE { |
64 repeater_.Stop(); | 63 repeater_.Stop(); |
65 View::OnMouseReleased(event, canceled); | 64 View::OnMouseReleased(event); |
| 65 } |
| 66 |
| 67 virtual void OnMouseCaptureLost() OVERRIDE { |
| 68 repeater_.Stop(); |
| 69 View::OnMouseCaptureLost(); |
66 } | 70 } |
67 | 71 |
68 private: | 72 private: |
69 void NotifyClick() { | 73 void NotifyClick() { |
70 #if defined(OS_WIN) | 74 #if defined(OS_WIN) |
71 DWORD pos = GetMessagePos(); | 75 DWORD pos = GetMessagePos(); |
72 POINTS points = MAKEPOINTS(pos); | 76 POINTS points = MAKEPOINTS(pos); |
73 gfx::Point cursor_point(points.x, points.y); | 77 gfx::Point cursor_point(points.x, points.y); |
74 #elif defined(OS_LINUX) | 78 #elif defined(OS_LINUX) |
75 gfx::Point cursor_point = Screen::GetCursorScreenPoint(); | 79 gfx::Point cursor_point = Screen::GetCursorScreenPoint(); |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 | 146 |
143 // Gets the position of the thumb on the x or y axis. | 147 // Gets the position of the thumb on the x or y axis. |
144 int GetPosition() const { | 148 int GetPosition() const { |
145 gfx::Rect track_bounds = scroll_bar_->GetTrackBounds(); | 149 gfx::Rect track_bounds = scroll_bar_->GetTrackBounds(); |
146 if (scroll_bar_->IsHorizontal()) | 150 if (scroll_bar_->IsHorizontal()) |
147 return x() - track_bounds.x(); | 151 return x() - track_bounds.x(); |
148 return y() - track_bounds.y(); | 152 return y() - track_bounds.y(); |
149 } | 153 } |
150 | 154 |
151 // View overrides: | 155 // View overrides: |
152 virtual gfx::Size GetPreferredSize() { | 156 virtual gfx::Size GetPreferredSize() OVERRIDE { |
153 return gfx::Size(background_bitmap()->width(), | 157 return gfx::Size(background_bitmap()->width(), |
154 start_cap_bitmap()->height() + | 158 start_cap_bitmap()->height() + |
155 end_cap_bitmap()->height() + | 159 end_cap_bitmap()->height() + |
156 grippy_bitmap()->height()); | 160 grippy_bitmap()->height()); |
157 } | 161 } |
158 | 162 |
159 protected: | 163 virtual void OnMouseEntered(const MouseEvent& event) OVERRIDE { |
160 // View overrides: | |
161 virtual void Paint(gfx::Canvas* canvas) { | |
162 canvas->DrawBitmapInt(*start_cap_bitmap(), 0, 0); | |
163 int top_cap_height = start_cap_bitmap()->height(); | |
164 int bottom_cap_height = end_cap_bitmap()->height(); | |
165 int thumb_body_height = height() - top_cap_height - bottom_cap_height; | |
166 canvas->TileImageInt(*background_bitmap(), 0, top_cap_height, | |
167 background_bitmap()->width(), thumb_body_height); | |
168 canvas->DrawBitmapInt(*end_cap_bitmap(), 0, | |
169 height() - bottom_cap_height); | |
170 | |
171 // Paint the grippy over the track. | |
172 int grippy_x = (width() - grippy_bitmap()->width()) / 2; | |
173 int grippy_y = (thumb_body_height - grippy_bitmap()->height()) / 2; | |
174 canvas->DrawBitmapInt(*grippy_bitmap(), grippy_x, grippy_y); | |
175 } | |
176 | |
177 virtual void OnMouseEntered(const MouseEvent& event) { | |
178 SetState(CustomButton::BS_HOT); | 164 SetState(CustomButton::BS_HOT); |
179 } | 165 } |
180 | 166 |
181 virtual void OnMouseExited(const MouseEvent& event) { | 167 virtual void OnMouseExited(const MouseEvent& event) OVERRIDE { |
182 SetState(CustomButton::BS_NORMAL); | 168 SetState(CustomButton::BS_NORMAL); |
183 } | 169 } |
184 | 170 |
185 virtual bool OnMousePressed(const MouseEvent& event) { | 171 virtual bool OnMousePressed(const MouseEvent& event) OVERRIDE { |
186 mouse_offset_ = scroll_bar_->IsHorizontal() ? event.x() : event.y(); | 172 mouse_offset_ = scroll_bar_->IsHorizontal() ? event.x() : event.y(); |
187 drag_start_position_ = GetPosition(); | 173 drag_start_position_ = GetPosition(); |
188 SetState(CustomButton::BS_PUSHED); | 174 SetState(CustomButton::BS_PUSHED); |
189 return true; | 175 return true; |
190 } | 176 } |
191 | 177 |
192 virtual bool OnMouseDragged(const MouseEvent& event) { | 178 virtual bool OnMouseDragged(const MouseEvent& event) OVERRIDE { |
193 // If the user moves the mouse more than |kScrollThumbDragOutSnap| outside | 179 // If the user moves the mouse more than |kScrollThumbDragOutSnap| outside |
194 // the bounds of the thumb, the scrollbar will snap the scroll back to the | 180 // the bounds of the thumb, the scrollbar will snap the scroll back to the |
195 // point it was at before the drag began. | 181 // point it was at before the drag began. |
196 if (scroll_bar_->IsHorizontal()) { | 182 if (scroll_bar_->IsHorizontal()) { |
197 if ((event.y() < y() - kScrollThumbDragOutSnap) || | 183 if ((event.y() < y() - kScrollThumbDragOutSnap) || |
198 (event.y() > (y() + height() + kScrollThumbDragOutSnap))) { | 184 (event.y() > (y() + height() + kScrollThumbDragOutSnap))) { |
199 scroll_bar_->ScrollToThumbPosition(drag_start_position_, false); | 185 scroll_bar_->ScrollToThumbPosition(drag_start_position_, false); |
200 return true; | 186 return true; |
201 } | 187 } |
202 } else { | 188 } else { |
203 if ((event.x() < x() - kScrollThumbDragOutSnap) || | 189 if ((event.x() < x() - kScrollThumbDragOutSnap) || |
204 (event.x() > (x() + width() + kScrollThumbDragOutSnap))) { | 190 (event.x() > (x() + width() + kScrollThumbDragOutSnap))) { |
205 scroll_bar_->ScrollToThumbPosition(drag_start_position_, false); | 191 scroll_bar_->ScrollToThumbPosition(drag_start_position_, false); |
206 return true; | 192 return true; |
207 } | 193 } |
208 } | 194 } |
209 if (scroll_bar_->IsHorizontal()) { | 195 if (scroll_bar_->IsHorizontal()) { |
210 int thumb_x = event.x() - mouse_offset_; | 196 int thumb_x = event.x() - mouse_offset_; |
211 scroll_bar_->ScrollToThumbPosition(x() + thumb_x, false); | 197 scroll_bar_->ScrollToThumbPosition(x() + thumb_x, false); |
212 } else { | 198 } else { |
213 int thumb_y = event.y() - mouse_offset_; | 199 int thumb_y = event.y() - mouse_offset_; |
214 scroll_bar_->ScrollToThumbPosition(y() + thumb_y, false); | 200 scroll_bar_->ScrollToThumbPosition(y() + thumb_y, false); |
215 } | 201 } |
216 return true; | 202 return true; |
217 } | 203 } |
218 | 204 |
219 virtual void OnMouseReleased(const MouseEvent& event, | 205 virtual void OnMouseReleased(const MouseEvent& event) OVERRIDE { |
220 bool canceled) { | |
221 SetState(CustomButton::BS_HOT); | 206 SetState(CustomButton::BS_HOT); |
222 View::OnMouseReleased(event, canceled); | 207 View::OnMouseReleased(event); |
| 208 } |
| 209 |
| 210 virtual void OnMouseCaptureLost() OVERRIDE { |
| 211 SetState(CustomButton::BS_HOT); |
| 212 View::OnMouseCaptureLost(); |
| 213 } |
| 214 |
| 215 protected: |
| 216 // View overrides: |
| 217 virtual void Paint(gfx::Canvas* canvas) OVERRIDE { |
| 218 canvas->DrawBitmapInt(*start_cap_bitmap(), 0, 0); |
| 219 int top_cap_height = start_cap_bitmap()->height(); |
| 220 int bottom_cap_height = end_cap_bitmap()->height(); |
| 221 int thumb_body_height = height() - top_cap_height - bottom_cap_height; |
| 222 canvas->TileImageInt(*background_bitmap(), 0, top_cap_height, |
| 223 background_bitmap()->width(), thumb_body_height); |
| 224 canvas->DrawBitmapInt(*end_cap_bitmap(), 0, |
| 225 height() - bottom_cap_height); |
| 226 |
| 227 // Paint the grippy over the track. |
| 228 int grippy_x = (width() - grippy_bitmap()->width()) / 2; |
| 229 int grippy_y = (thumb_body_height - grippy_bitmap()->height()) / 2; |
| 230 canvas->DrawBitmapInt(*grippy_bitmap(), grippy_x, grippy_y); |
223 } | 231 } |
224 | 232 |
225 private: | 233 private: |
226 // Returns the bitmap rendered at the start of the thumb. | 234 // Returns the bitmap rendered at the start of the thumb. |
227 SkBitmap* start_cap_bitmap() const { | 235 SkBitmap* start_cap_bitmap() const { |
228 return scroll_bar_->images_[BitmapScrollBar::THUMB_START_CAP][state_]; | 236 return scroll_bar_->images_[BitmapScrollBar::THUMB_START_CAP][state_]; |
229 } | 237 } |
230 | 238 |
231 // Returns the bitmap rendered at the end of the thumb. | 239 // Returns the bitmap rendered at the end of the thumb. |
232 SkBitmap* end_cap_bitmap() const { | 240 SkBitmap* end_cap_bitmap() const { |
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
464 } else if (event.y() > thumb_bounds.bottom()) { | 472 } else if (event.y() > thumb_bounds.bottom()) { |
465 last_scroll_amount_ = SCROLL_NEXT_PAGE; | 473 last_scroll_amount_ = SCROLL_NEXT_PAGE; |
466 } | 474 } |
467 } | 475 } |
468 TrackClicked(); | 476 TrackClicked(); |
469 repeater_.Start(); | 477 repeater_.Start(); |
470 } | 478 } |
471 return true; | 479 return true; |
472 } | 480 } |
473 | 481 |
474 void BitmapScrollBar::OnMouseReleased(const MouseEvent& event, bool canceled) { | 482 void BitmapScrollBar::OnMouseReleased(const MouseEvent& event) { |
475 SetThumbTrackState(CustomButton::BS_NORMAL); | 483 SetThumbTrackState(CustomButton::BS_NORMAL); |
476 repeater_.Stop(); | 484 repeater_.Stop(); |
477 View::OnMouseReleased(event, canceled); | 485 View::OnMouseReleased(event); |
| 486 } |
| 487 |
| 488 void BitmapScrollBar::OnMouseCaptureLost() { |
| 489 SetThumbTrackState(CustomButton::BS_NORMAL); |
| 490 repeater_.Stop(); |
| 491 View::OnMouseCaptureLost(); |
478 } | 492 } |
479 | 493 |
480 bool BitmapScrollBar::OnKeyPressed(const KeyEvent& event) { | 494 bool BitmapScrollBar::OnKeyPressed(const KeyEvent& event) { |
481 ScrollAmount amount = SCROLL_NONE; | 495 ScrollAmount amount = SCROLL_NONE; |
482 switch (event.key_code()) { | 496 switch (event.key_code()) { |
483 case ui::VKEY_UP: | 497 case ui::VKEY_UP: |
484 if (!IsHorizontal()) | 498 if (!IsHorizontal()) |
485 amount = SCROLL_PREV_LINE; | 499 amount = SCROLL_PREV_LINE; |
486 break; | 500 break; |
487 case ui::VKEY_DOWN: | 501 case ui::VKEY_DOWN: |
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
719 thumb_position = thumb_position - (thumb_->GetSize() / 2); | 733 thumb_position = thumb_position - (thumb_->GetSize() / 2); |
720 return (thumb_position * contents_size_) / GetTrackSize(); | 734 return (thumb_position * contents_size_) / GetTrackSize(); |
721 } | 735 } |
722 | 736 |
723 void BitmapScrollBar::SetThumbTrackState(CustomButton::ButtonState state) { | 737 void BitmapScrollBar::SetThumbTrackState(CustomButton::ButtonState state) { |
724 thumb_track_state_ = state; | 738 thumb_track_state_ = state; |
725 SchedulePaint(); | 739 SchedulePaint(); |
726 } | 740 } |
727 | 741 |
728 } // namespace views | 742 } // namespace views |
OLD | NEW |