| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/scroll_view.h" | 5 #include "ui/views/controls/scroll_view.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "ui/events/event.h" | 8 #include "ui/events/event.h" |
| 9 #include "ui/gfx/canvas.h" |
| 9 #include "ui/native_theme/native_theme.h" | 10 #include "ui/native_theme/native_theme.h" |
| 10 #include "ui/views/border.h" | 11 #include "ui/views/border.h" |
| 11 #include "ui/views/controls/scrollbar/native_scroll_bar.h" | 12 #include "ui/views/controls/scrollbar/native_scroll_bar.h" |
| 12 #include "ui/views/widget/root_view.h" | 13 #include "ui/views/widget/root_view.h" |
| 13 | 14 |
| 14 namespace views { | 15 namespace views { |
| 15 | 16 |
| 16 const char ScrollView::kViewClassName[] = "ScrollView"; | 17 const char ScrollView::kViewClassName[] = "ScrollView"; |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| 19 | 20 |
| 20 // Subclass of ScrollView that resets the border when the theme changes. | 21 // Subclass of ScrollView that resets the border when the theme changes. |
| 21 class ScrollViewWithBorder : public views::ScrollView { | 22 class ScrollViewWithBorder : public views::ScrollView { |
| 22 public: | 23 public: |
| 23 ScrollViewWithBorder() {} | 24 ScrollViewWithBorder() {} |
| 24 | 25 |
| 25 // View overrides; | 26 // View overrides; |
| 26 virtual void OnNativeThemeChanged(const ui::NativeTheme* theme) OVERRIDE { | 27 virtual void OnNativeThemeChanged(const ui::NativeTheme* theme) OVERRIDE { |
| 27 SetBorder(Border::CreateSolidBorder( | 28 SetBorder(Border::CreateSolidBorder( |
| 28 1, | 29 1, |
| 29 theme->GetSystemColor(ui::NativeTheme::kColorId_UnfocusedBorderColor))); | 30 theme->GetSystemColor(ui::NativeTheme::kColorId_UnfocusedBorderColor))); |
| 30 } | 31 } |
| 31 | 32 |
| 32 private: | 33 private: |
| 33 DISALLOW_COPY_AND_ASSIGN(ScrollViewWithBorder); | 34 DISALLOW_COPY_AND_ASSIGN(ScrollViewWithBorder); |
| 34 }; | 35 }; |
| 35 | 36 |
| 37 class ScrollCornerView : public views::View { |
| 38 public: |
| 39 ScrollCornerView() {} |
| 40 |
| 41 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE { |
| 42 ui::NativeTheme::ExtraParams ignored; |
| 43 GetNativeTheme()->Paint(canvas->sk_canvas(), |
| 44 ui::NativeTheme::kScrollbarCorner, |
| 45 ui::NativeTheme::kNormal, |
| 46 GetLocalBounds(), |
| 47 ignored); |
| 48 } |
| 49 |
| 50 private: |
| 51 DISALLOW_COPY_AND_ASSIGN(ScrollCornerView); |
| 52 }; |
| 53 |
| 36 // Returns the position for the view so that it isn't scrolled off the visible | 54 // Returns the position for the view so that it isn't scrolled off the visible |
| 37 // region. | 55 // region. |
| 38 int CheckScrollBounds(int viewport_size, int content_size, int current_pos) { | 56 int CheckScrollBounds(int viewport_size, int content_size, int current_pos) { |
| 39 int max = std::max(content_size - viewport_size, 0); | 57 int max = std::max(content_size - viewport_size, 0); |
| 40 if (current_pos < 0) | 58 if (current_pos < 0) |
| 41 return 0; | 59 return 0; |
| 42 if (current_pos > max) | 60 if (current_pos > max) |
| 43 return max; | 61 return max; |
| 44 return current_pos; | 62 return current_pos; |
| 45 } | 63 } |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 DISALLOW_COPY_AND_ASSIGN(Viewport); | 120 DISALLOW_COPY_AND_ASSIGN(Viewport); |
| 103 }; | 121 }; |
| 104 | 122 |
| 105 ScrollView::ScrollView() | 123 ScrollView::ScrollView() |
| 106 : contents_(NULL), | 124 : contents_(NULL), |
| 107 contents_viewport_(new Viewport()), | 125 contents_viewport_(new Viewport()), |
| 108 header_(NULL), | 126 header_(NULL), |
| 109 header_viewport_(new Viewport()), | 127 header_viewport_(new Viewport()), |
| 110 horiz_sb_(new NativeScrollBar(true)), | 128 horiz_sb_(new NativeScrollBar(true)), |
| 111 vert_sb_(new NativeScrollBar(false)), | 129 vert_sb_(new NativeScrollBar(false)), |
| 112 resize_corner_(NULL), | 130 corner_view_(new ScrollCornerView()), |
| 113 min_height_(-1), | 131 min_height_(-1), |
| 114 max_height_(-1), | 132 max_height_(-1), |
| 115 hide_horizontal_scrollbar_(false) { | 133 hide_horizontal_scrollbar_(false) { |
| 116 set_notify_enter_exit_on_child(true); | 134 set_notify_enter_exit_on_child(true); |
| 117 | 135 |
| 118 AddChildView(contents_viewport_); | 136 AddChildView(contents_viewport_); |
| 119 AddChildView(header_viewport_); | 137 AddChildView(header_viewport_); |
| 120 | 138 |
| 121 // Don't add the scrollbars as children until we discover we need them | 139 // Don't add the scrollbars as children until we discover we need them |
| 122 // (ShowOrHideScrollBar). | 140 // (ShowOrHideScrollBar). |
| 123 horiz_sb_->SetVisible(false); | 141 horiz_sb_->SetVisible(false); |
| 124 horiz_sb_->set_controller(this); | 142 horiz_sb_->set_controller(this); |
| 125 vert_sb_->SetVisible(false); | 143 vert_sb_->SetVisible(false); |
| 126 vert_sb_->set_controller(this); | 144 vert_sb_->set_controller(this); |
| 127 if (resize_corner_) | 145 corner_view_->SetVisible(false); |
| 128 resize_corner_->SetVisible(false); | |
| 129 } | 146 } |
| 130 | 147 |
| 131 ScrollView::~ScrollView() { | 148 ScrollView::~ScrollView() { |
| 132 // The scrollbars may not have been added, delete them to ensure they get | 149 // The scrollbars may not have been added, delete them to ensure they get |
| 133 // deleted. | 150 // deleted. |
| 134 delete horiz_sb_; | 151 delete horiz_sb_; |
| 135 delete vert_sb_; | 152 delete vert_sb_; |
| 136 | 153 delete corner_view_; |
| 137 if (resize_corner_ && !resize_corner_->parent()) | |
| 138 delete resize_corner_; | |
| 139 } | 154 } |
| 140 | 155 |
| 141 // static | 156 // static |
| 142 ScrollView* ScrollView::CreateScrollViewWithBorder() { | 157 ScrollView* ScrollView::CreateScrollViewWithBorder() { |
| 143 return new ScrollViewWithBorder(); | 158 return new ScrollViewWithBorder(); |
| 144 } | 159 } |
| 145 | 160 |
| 146 void ScrollView::SetContents(View* a_view) { | 161 void ScrollView::SetContents(View* a_view) { |
| 147 SetHeaderOrContents(contents_viewport_, a_view, &contents_); | 162 SetHeaderOrContents(contents_viewport_, a_view, &contents_); |
| 148 } | 163 } |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 261 bool should_layout_contents = false; | 276 bool should_layout_contents = false; |
| 262 bool horiz_sb_required = false; | 277 bool horiz_sb_required = false; |
| 263 bool vert_sb_required = false; | 278 bool vert_sb_required = false; |
| 264 if (contents_) { | 279 if (contents_) { |
| 265 gfx::Size content_size = contents_->size(); | 280 gfx::Size content_size = contents_->size(); |
| 266 ComputeScrollBarsVisibility(viewport_size, | 281 ComputeScrollBarsVisibility(viewport_size, |
| 267 content_size, | 282 content_size, |
| 268 &horiz_sb_required, | 283 &horiz_sb_required, |
| 269 &vert_sb_required); | 284 &vert_sb_required); |
| 270 } | 285 } |
| 271 bool resize_corner_required = resize_corner_ && horiz_sb_required && | 286 bool corner_view_required = horiz_sb_required && vert_sb_required; |
| 272 vert_sb_required; | |
| 273 // Take action. | 287 // Take action. |
| 274 SetControlVisibility(horiz_sb_, horiz_sb_required); | 288 SetControlVisibility(horiz_sb_, horiz_sb_required); |
| 275 SetControlVisibility(vert_sb_, vert_sb_required); | 289 SetControlVisibility(vert_sb_, vert_sb_required); |
| 276 SetControlVisibility(resize_corner_, resize_corner_required); | 290 SetControlVisibility(corner_view_, corner_view_required); |
| 277 | 291 |
| 278 // Non-default. | 292 // Non-default. |
| 279 if (horiz_sb_required) { | 293 if (horiz_sb_required) { |
| 280 viewport_bounds.set_height( | 294 viewport_bounds.set_height( |
| 281 std::max(0, viewport_bounds.height() - horiz_sb_height)); | 295 std::max(0, viewport_bounds.height() - horiz_sb_height)); |
| 282 should_layout_contents = true; | 296 should_layout_contents = true; |
| 283 } | 297 } |
| 284 // Default. | 298 // Default. |
| 285 if (!vert_sb_required) { | 299 if (!vert_sb_required) { |
| 286 viewport_bounds.set_width(viewport_bounds.width() + vert_sb_width); | 300 viewport_bounds.set_width(viewport_bounds.width() + vert_sb_width); |
| 287 should_layout_contents = true; | 301 should_layout_contents = true; |
| 288 } | 302 } |
| 289 | 303 |
| 290 if (horiz_sb_required) { | 304 if (horiz_sb_required) { |
| 291 int height_offset = horiz_sb_->GetContentOverlapSize(); | 305 int height_offset = horiz_sb_->GetContentOverlapSize(); |
| 292 horiz_sb_->SetBounds(0, | 306 horiz_sb_->SetBounds(0, |
| 293 viewport_bounds.bottom() - height_offset, | 307 viewport_bounds.bottom() - height_offset, |
| 294 viewport_bounds.right(), | 308 viewport_bounds.right(), |
| 295 horiz_sb_height + height_offset); | 309 horiz_sb_height + height_offset); |
| 296 } | 310 } |
| 297 if (vert_sb_required) { | 311 if (vert_sb_required) { |
| 298 int width_offset = vert_sb_->GetContentOverlapSize(); | 312 int width_offset = vert_sb_->GetContentOverlapSize(); |
| 299 vert_sb_->SetBounds(viewport_bounds.right() - width_offset, | 313 vert_sb_->SetBounds(viewport_bounds.right() - width_offset, |
| 300 0, | 314 0, |
| 301 vert_sb_width + width_offset, | 315 vert_sb_width + width_offset, |
| 302 viewport_bounds.bottom()); | 316 viewport_bounds.bottom()); |
| 303 } | 317 } |
| 304 if (resize_corner_required) { | 318 if (corner_view_required) { |
| 305 // Show the resize corner. | 319 // Show the resize corner. |
| 306 resize_corner_->SetBounds(viewport_bounds.right(), | 320 corner_view_->SetBounds(viewport_bounds.right(), |
| 307 viewport_bounds.bottom(), | 321 viewport_bounds.bottom(), |
| 308 vert_sb_width, | 322 vert_sb_width, |
| 309 horiz_sb_height); | 323 horiz_sb_height); |
| 310 } | 324 } |
| 311 | 325 |
| 312 // Update to the real client size with the visible scrollbars. | 326 // Update to the real client size with the visible scrollbars. |
| 313 contents_viewport_->SetBoundsRect(viewport_bounds); | 327 contents_viewport_->SetBoundsRect(viewport_bounds); |
| 314 if (should_layout_contents && contents_) | 328 if (should_layout_contents && contents_) |
| 315 contents_->Layout(); | 329 contents_->Layout(); |
| 316 | 330 |
| 317 header_viewport_->SetBounds(contents_x, contents_y, | 331 header_viewport_->SetBounds(contents_x, contents_y, |
| 318 viewport_bounds.width(), header_height); | 332 viewport_bounds.width(), header_height); |
| 319 if (header_) | 333 if (header_) |
| (...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 621 | 635 |
| 622 VariableRowHeightScrollHelper::RowInfo | 636 VariableRowHeightScrollHelper::RowInfo |
| 623 FixedRowHeightScrollHelper::GetRowInfo(int y) { | 637 FixedRowHeightScrollHelper::GetRowInfo(int y) { |
| 624 if (y < top_margin_) | 638 if (y < top_margin_) |
| 625 return RowInfo(0, top_margin_); | 639 return RowInfo(0, top_margin_); |
| 626 return RowInfo((y - top_margin_) / row_height_ * row_height_ + top_margin_, | 640 return RowInfo((y - top_margin_) / row_height_ * row_height_ + top_margin_, |
| 627 row_height_); | 641 row_height_); |
| 628 } | 642 } |
| 629 | 643 |
| 630 } // namespace views | 644 } // namespace views |
| OLD | NEW |