| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/scroll_view.h" | 5 #include "views/controls/scroll_view.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "views/controls/scrollbar/native_scroll_bar.h" | 8 #include "views/controls/scrollbar/native_scroll_bar.h" |
| 9 #include "views/widget/root_view.h" | 9 #include "views/widget/root_view.h" |
| 10 | 10 |
| 11 namespace views { | 11 namespace views { |
| 12 | 12 |
| 13 const char* const ScrollView::kViewClassName = "views/ScrollView"; | 13 const char* const ScrollView::kViewClassName = "views/ScrollView"; |
| 14 | 14 |
| 15 // Viewport contains the contents View of the ScrollView. | 15 // Viewport contains the contents View of the ScrollView. |
| 16 class Viewport : public View { | 16 class Viewport : public View { |
| 17 public: | 17 public: |
| 18 Viewport() {} | 18 Viewport() {} |
| 19 virtual ~Viewport() {} | 19 virtual ~Viewport() {} |
| 20 | 20 |
| 21 virtual void ScrollRectToVisible(const gfx::Rect& rect) { | 21 virtual void ScrollRectToVisible(const gfx::Rect& rect) { |
| 22 if (!GetChildViewCount() || !GetParent()) | 22 if (!has_children() || !parent()) |
| 23 return; | 23 return; |
| 24 | 24 |
| 25 View* contents = GetChildViewAt(0); | 25 View* contents = GetChildViewAt(0); |
| 26 gfx::Rect scroll_rect(rect); | 26 gfx::Rect scroll_rect(rect); |
| 27 scroll_rect.Offset(-contents->x(), -contents->y()); | 27 scroll_rect.Offset(-contents->x(), -contents->y()); |
| 28 static_cast<ScrollView*>(GetParent())->ScrollContentsRegionToBeVisible( | 28 static_cast<ScrollView*>(parent())->ScrollContentsRegionToBeVisible( |
| 29 scroll_rect); | 29 scroll_rect); |
| 30 } | 30 } |
| 31 | 31 |
| 32 private: | 32 private: |
| 33 DISALLOW_COPY_AND_ASSIGN(Viewport); | 33 DISALLOW_COPY_AND_ASSIGN(Viewport); |
| 34 }; | 34 }; |
| 35 | 35 |
| 36 | 36 |
| 37 ScrollView::ScrollView() { | 37 ScrollView::ScrollView() { |
| 38 Init(new NativeScrollBar(true), new NativeScrollBar(false), NULL); | 38 Init(new NativeScrollBar(true), new NativeScrollBar(false), NULL); |
| 39 } | 39 } |
| 40 | 40 |
| 41 ScrollView::ScrollView(ScrollBar* horizontal_scrollbar, | 41 ScrollView::ScrollView(ScrollBar* horizontal_scrollbar, |
| 42 ScrollBar* vertical_scrollbar, | 42 ScrollBar* vertical_scrollbar, |
| 43 View* resize_corner) { | 43 View* resize_corner) { |
| 44 Init(horizontal_scrollbar, vertical_scrollbar, resize_corner); | 44 Init(horizontal_scrollbar, vertical_scrollbar, resize_corner); |
| 45 } | 45 } |
| 46 | 46 |
| 47 ScrollView::~ScrollView() { | 47 ScrollView::~ScrollView() { |
| 48 // If scrollbars are currently not used, delete them | 48 // If scrollbars are currently not used, delete them |
| 49 if (!horiz_sb_->GetParent()) { | 49 if (!horiz_sb_->parent()) |
| 50 delete horiz_sb_; | 50 delete horiz_sb_; |
| 51 } | |
| 52 | 51 |
| 53 if (!vert_sb_->GetParent()) { | 52 if (!vert_sb_->parent()) |
| 54 delete vert_sb_; | 53 delete vert_sb_; |
| 55 } | |
| 56 | 54 |
| 57 if (resize_corner_ && !resize_corner_->GetParent()) { | 55 if (resize_corner_ && !resize_corner_->parent()) |
| 58 delete resize_corner_; | 56 delete resize_corner_; |
| 59 } | |
| 60 } | 57 } |
| 61 | 58 |
| 62 void ScrollView::SetContents(View* a_view) { | 59 void ScrollView::SetContents(View* a_view) { |
| 63 if (contents_ && contents_ != a_view) { | 60 if (contents_ && contents_ != a_view) { |
| 64 viewport_->RemoveChildView(contents_); | 61 viewport_->RemoveChildView(contents_); |
| 65 delete contents_; | 62 delete contents_; |
| 66 contents_ = NULL; | 63 contents_ = NULL; |
| 67 } | 64 } |
| 68 | 65 |
| 69 if (a_view) { | 66 if (a_view) { |
| (...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 442 | 439 |
| 443 VariableRowHeightScrollHelper::~VariableRowHeightScrollHelper() { | 440 VariableRowHeightScrollHelper::~VariableRowHeightScrollHelper() { |
| 444 } | 441 } |
| 445 | 442 |
| 446 int VariableRowHeightScrollHelper::GetPageScrollIncrement( | 443 int VariableRowHeightScrollHelper::GetPageScrollIncrement( |
| 447 ScrollView* scroll_view, bool is_horizontal, bool is_positive) { | 444 ScrollView* scroll_view, bool is_horizontal, bool is_positive) { |
| 448 if (is_horizontal) | 445 if (is_horizontal) |
| 449 return 0; | 446 return 0; |
| 450 // y coordinate is most likely negative. | 447 // y coordinate is most likely negative. |
| 451 int y = abs(scroll_view->GetContents()->y()); | 448 int y = abs(scroll_view->GetContents()->y()); |
| 452 int vis_height = scroll_view->GetContents()->GetParent()->height(); | 449 int vis_height = scroll_view->GetContents()->parent()->height(); |
| 453 if (is_positive) { | 450 if (is_positive) { |
| 454 // Align the bottom most row to the top of the view. | 451 // Align the bottom most row to the top of the view. |
| 455 int bottom = std::min(scroll_view->GetContents()->height() - 1, | 452 int bottom = std::min(scroll_view->GetContents()->height() - 1, |
| 456 y + vis_height); | 453 y + vis_height); |
| 457 RowInfo bottom_row_info = GetRowInfo(bottom); | 454 RowInfo bottom_row_info = GetRowInfo(bottom); |
| 458 // If 0, ScrollView will provide a default value. | 455 // If 0, ScrollView will provide a default value. |
| 459 return std::max(0, bottom_row_info.origin - y); | 456 return std::max(0, bottom_row_info.origin - y); |
| 460 } else { | 457 } else { |
| 461 // Align the row on the previous page to to the top of the view. | 458 // Align the row on the previous page to to the top of the view. |
| 462 int last_page_y = y - vis_height; | 459 int last_page_y = y - vis_height; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 501 | 498 |
| 502 VariableRowHeightScrollHelper::RowInfo | 499 VariableRowHeightScrollHelper::RowInfo |
| 503 FixedRowHeightScrollHelper::GetRowInfo(int y) { | 500 FixedRowHeightScrollHelper::GetRowInfo(int y) { |
| 504 if (y < top_margin_) | 501 if (y < top_margin_) |
| 505 return RowInfo(0, top_margin_); | 502 return RowInfo(0, top_margin_); |
| 506 return RowInfo((y - top_margin_) / row_height_ * row_height_ + top_margin_, | 503 return RowInfo((y - top_margin_) / row_height_ * row_height_ + top_margin_, |
| 507 row_height_); | 504 row_height_); |
| 508 } | 505 } |
| 509 | 506 |
| 510 } // namespace views | 507 } // namespace views |
| OLD | NEW |