| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 "chrome/views/bitmap_scroll_bar.h" | 5 #include "chrome/views/bitmap_scroll_bar.h" |
| 6 | 6 |
| 7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
| 8 #include "chrome/common/gfx/chrome_canvas.h" | 8 #include "chrome/common/gfx/chrome_canvas.h" |
| 9 #include "chrome/common/l10n_util.h" | 9 #include "chrome/common/l10n_util.h" |
| 10 #include "chrome/views/menu.h" | 10 #include "chrome/views/menu.h" |
| 11 #include "chrome/views/scroll_view.h" | 11 #include "chrome/views/scroll_view.h" |
| 12 #include "chrome/views/view_container.h" | 12 #include "chrome/views/view_container.h" |
| 13 #include "skia/include/SkBitmap.h" | 13 #include "skia/include/SkBitmap.h" |
| 14 | 14 |
| 15 #include "generated_resources.h" | 15 #include "generated_resources.h" |
| 16 | 16 |
| 17 #undef min |
| 18 #undef max |
| 19 |
| 17 namespace ChromeViews { | 20 namespace ChromeViews { |
| 18 | 21 |
| 19 namespace { | 22 namespace { |
| 20 | 23 |
| 21 // The distance the mouse can be dragged outside the bounds of the thumb during | 24 // The distance the mouse can be dragged outside the bounds of the thumb during |
| 22 // dragging before the scrollbar will snap back to its regular position. | 25 // dragging before the scrollbar will snap back to its regular position. |
| 23 static const int kScrollThumbDragOutSnap = 100; | 26 static const int kScrollThumbDragOutSnap = 100; |
| 24 | 27 |
| 25 /////////////////////////////////////////////////////////////////////////////// | 28 /////////////////////////////////////////////////////////////////////////////// |
| 26 // | 29 // |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 drag_start_position_(-1), | 81 drag_start_position_(-1), |
| 79 mouse_offset_(-1), | 82 mouse_offset_(-1), |
| 80 state_(BaseButton::BS_NORMAL) { | 83 state_(BaseButton::BS_NORMAL) { |
| 81 } | 84 } |
| 82 virtual ~BitmapScrollBarThumb() { } | 85 virtual ~BitmapScrollBarThumb() { } |
| 83 | 86 |
| 84 // Sets the size (width or height) of the thumb to the specified value. | 87 // Sets the size (width or height) of the thumb to the specified value. |
| 85 void SetSize(int size) { | 88 void SetSize(int size) { |
| 86 // Make sure the thumb is never sized smaller than its minimum possible | 89 // Make sure the thumb is never sized smaller than its minimum possible |
| 87 // display size. | 90 // display size. |
| 88 CSize prefsize; | 91 gfx::Size prefsize = GetPreferredSize(); |
| 89 GetPreferredSize(&prefsize); | |
| 90 size = std::max(size, | 92 size = std::max(size, |
| 91 static_cast<int>(scroll_bar_->IsHorizontal() ? | 93 static_cast<int>(scroll_bar_->IsHorizontal() ? |
| 92 prefsize.cx : prefsize.cy)); | 94 prefsize.width() : prefsize.height())); |
| 93 gfx::Rect thumb_bounds = bounds(); | 95 gfx::Rect thumb_bounds = bounds(); |
| 94 if (scroll_bar_->IsHorizontal()) { | 96 if (scroll_bar_->IsHorizontal()) { |
| 95 thumb_bounds.set_width(size); | 97 thumb_bounds.set_width(size); |
| 96 } else { | 98 } else { |
| 97 thumb_bounds.set_height(size); | 99 thumb_bounds.set_height(size); |
| 98 } | 100 } |
| 99 SetBounds(thumb_bounds.ToRECT()); | 101 SetBounds(thumb_bounds.ToRECT()); |
| 100 } | 102 } |
| 101 | 103 |
| 102 // Retrieves the size (width or height) of the thumb. | 104 // Retrieves the size (width or height) of the thumb. |
| (...skipping 17 matching lines...) Expand all Loading... |
| 120 | 122 |
| 121 // Gets the position of the thumb on the x or y axis. | 123 // Gets the position of the thumb on the x or y axis. |
| 122 int GetPosition() const { | 124 int GetPosition() const { |
| 123 gfx::Rect track_bounds = scroll_bar_->GetTrackBounds(); | 125 gfx::Rect track_bounds = scroll_bar_->GetTrackBounds(); |
| 124 if (scroll_bar_->IsHorizontal()) | 126 if (scroll_bar_->IsHorizontal()) |
| 125 return x() - track_bounds.x(); | 127 return x() - track_bounds.x(); |
| 126 return y() - track_bounds.y(); | 128 return y() - track_bounds.y(); |
| 127 } | 129 } |
| 128 | 130 |
| 129 // View overrides: | 131 // View overrides: |
| 130 virtual void GetPreferredSize(CSize* preferred_size) { | 132 virtual gfx::Size GetPreferredSize() { |
| 131 DCHECK(preferred_size); | 133 return gfx::Size(background_bitmap()->width(), |
| 132 preferred_size->cx = background_bitmap()->width(); | 134 start_cap_bitmap()->height() + |
| 133 preferred_size->cy = start_cap_bitmap()->height() + | 135 end_cap_bitmap()->height() + |
| 134 end_cap_bitmap()->height() + grippy_bitmap()->height(); | 136 grippy_bitmap()->height()); |
| 135 } | 137 } |
| 136 | 138 |
| 137 protected: | 139 protected: |
| 138 // View overrides: | 140 // View overrides: |
| 139 virtual void Paint(ChromeCanvas* canvas) { | 141 virtual void Paint(ChromeCanvas* canvas) { |
| 140 canvas->DrawBitmapInt(*start_cap_bitmap(), 0, 0); | 142 canvas->DrawBitmapInt(*start_cap_bitmap(), 0, 0); |
| 141 int top_cap_height = start_cap_bitmap()->height(); | 143 int top_cap_height = start_cap_bitmap()->height(); |
| 142 int bottom_cap_height = end_cap_bitmap()->height(); | 144 int bottom_cap_height = end_cap_bitmap()->height(); |
| 143 int thumb_body_height = height() - top_cap_height - bottom_cap_height; | 145 int thumb_body_height = height() - top_cap_height - bottom_cap_height; |
| 144 canvas->TileImageInt(*background_bitmap(), 0, top_cap_height, | 146 canvas->TileImageInt(*background_bitmap(), 0, top_cap_height, |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 273 AddChildView(next_button_); | 275 AddChildView(next_button_); |
| 274 AddChildView(thumb_); | 276 AddChildView(thumb_); |
| 275 | 277 |
| 276 SetContextMenuController(this); | 278 SetContextMenuController(this); |
| 277 prev_button_->SetContextMenuController(this); | 279 prev_button_->SetContextMenuController(this); |
| 278 next_button_->SetContextMenuController(this); | 280 next_button_->SetContextMenuController(this); |
| 279 thumb_->SetContextMenuController(this); | 281 thumb_->SetContextMenuController(this); |
| 280 } | 282 } |
| 281 | 283 |
| 282 gfx::Rect BitmapScrollBar::GetTrackBounds() const { | 284 gfx::Rect BitmapScrollBar::GetTrackBounds() const { |
| 283 CSize prefsize; | 285 gfx::Size prefsize = prev_button_->GetPreferredSize(); |
| 284 prev_button_->GetPreferredSize(&prefsize); | |
| 285 if (IsHorizontal()) { | 286 if (IsHorizontal()) { |
| 286 if (!show_scroll_buttons_) | 287 if (!show_scroll_buttons_) |
| 287 prefsize.cx = 0; | 288 prefsize.set_width(0); |
| 288 int new_width = std::max(0, | 289 int new_width = |
| 289 static_cast<int>(width() - (prefsize.cx * 2))); | 290 std::max(0, static_cast<int>(width() - (prefsize.width() * 2))); |
| 290 gfx::Rect track_bounds(prefsize.cx, 0, new_width, prefsize.cy); | 291 gfx::Rect track_bounds(prefsize.width(), 0, new_width, prefsize.height()); |
| 291 return track_bounds; | 292 return track_bounds; |
| 292 } | 293 } |
| 293 if (!show_scroll_buttons_) | 294 if (!show_scroll_buttons_) |
| 294 prefsize.cy = 0; | 295 prefsize.set_height(0); |
| 295 gfx::Rect track_bounds(0, prefsize.cy, prefsize.cx, | 296 gfx::Rect track_bounds(0, prefsize.height(), prefsize.width(), |
| 296 std::max(0l, height() - (prefsize.cy * 2))); | 297 std::max(0, height() - (prefsize.height() * 2))); |
| 297 return track_bounds; | 298 return track_bounds; |
| 298 } | 299 } |
| 299 | 300 |
| 300 void BitmapScrollBar::SetImage(ScrollBarPart part, | 301 void BitmapScrollBar::SetImage(ScrollBarPart part, |
| 301 BaseButton::ButtonState state, | 302 BaseButton::ButtonState state, |
| 302 SkBitmap* bitmap) { | 303 SkBitmap* bitmap) { |
| 303 DCHECK(part < PART_COUNT); | 304 DCHECK(part < PART_COUNT); |
| 304 DCHECK(state < BaseButton::kButtonStateCount); | 305 DCHECK(state < BaseButton::kButtonStateCount); |
| 305 switch (part) { | 306 switch (part) { |
| 306 case PREV_BUTTON: | 307 case PREV_BUTTON: |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 374 } | 375 } |
| 375 | 376 |
| 376 void BitmapScrollBar::TrackClicked() { | 377 void BitmapScrollBar::TrackClicked() { |
| 377 if (last_scroll_amount_ != SCROLL_NONE) | 378 if (last_scroll_amount_ != SCROLL_NONE) |
| 378 ScrollByAmount(last_scroll_amount_); | 379 ScrollByAmount(last_scroll_amount_); |
| 379 } | 380 } |
| 380 | 381 |
| 381 /////////////////////////////////////////////////////////////////////////////// | 382 /////////////////////////////////////////////////////////////////////////////// |
| 382 // BitmapScrollBar, View implementation: | 383 // BitmapScrollBar, View implementation: |
| 383 | 384 |
| 384 void BitmapScrollBar::GetPreferredSize(CSize* preferred_size) { | 385 gfx::Size BitmapScrollBar::GetPreferredSize() { |
| 385 DCHECK(preferred_size); | |
| 386 | |
| 387 // In this case, we're returning the desired width of the scrollbar and its | 386 // In this case, we're returning the desired width of the scrollbar and its |
| 388 // minimum allowable height. | 387 // minimum allowable height. |
| 389 CSize button_prefsize; | 388 gfx::Size button_prefsize = prev_button_->GetPreferredSize(); |
| 390 prev_button_->GetPreferredSize(&button_prefsize); | 389 return gfx::Size(button_prefsize.width(), button_prefsize.height() * 2); |
| 391 preferred_size->cx = button_prefsize.cx; | |
| 392 preferred_size->cy = button_prefsize.cy * 2; | |
| 393 } | 390 } |
| 394 | 391 |
| 395 void BitmapScrollBar::Paint(ChromeCanvas* canvas) { | 392 void BitmapScrollBar::Paint(ChromeCanvas* canvas) { |
| 396 // Paint the track. | 393 // Paint the track. |
| 397 gfx::Rect track_bounds = GetTrackBounds(); | 394 gfx::Rect track_bounds = GetTrackBounds(); |
| 398 canvas->TileImageInt(*images_[THUMB_TRACK][thumb_track_state_], | 395 canvas->TileImageInt(*images_[THUMB_TRACK][thumb_track_state_], |
| 399 track_bounds.x(), track_bounds.y(), | 396 track_bounds.x(), track_bounds.y(), |
| 400 track_bounds.width(), track_bounds.height()); | 397 track_bounds.width(), track_bounds.height()); |
| 401 } | 398 } |
| 402 | 399 |
| 403 void BitmapScrollBar::Layout() { | 400 void BitmapScrollBar::Layout() { |
| 404 // Size and place the two scroll buttons. | 401 // Size and place the two scroll buttons. |
| 405 if (show_scroll_buttons_) { | 402 if (show_scroll_buttons_) { |
| 406 CSize prefsize; | 403 gfx::Size prefsize = prev_button_->GetPreferredSize(); |
| 407 prev_button_->GetPreferredSize(&prefsize); | 404 prev_button_->SetBounds(gfx::Point(), prefsize); |
| 408 prev_button_->SetBounds(0, 0, prefsize.cx, prefsize.cy); | 405 prefsize = next_button_->GetPreferredSize(); |
| 409 next_button_->GetPreferredSize(&prefsize); | |
| 410 if (IsHorizontal()) { | 406 if (IsHorizontal()) { |
| 411 next_button_->SetBounds(width() - prefsize.cx, 0, prefsize.cx, | 407 next_button_->SetBounds(width() - prefsize.width(), 0, prefsize.width(), |
| 412 prefsize.cy); | 408 prefsize.height()); |
| 413 } else { | 409 } else { |
| 414 next_button_->SetBounds(0, height() - prefsize.cy, prefsize.cx, | 410 next_button_->SetBounds(0, height() - prefsize.height(), prefsize.width(), |
| 415 prefsize.cy); | 411 prefsize.height()); |
| 416 } | 412 } |
| 417 } else { | 413 } else { |
| 418 prev_button_->SetBounds(0, 0, 0, 0); | 414 prev_button_->SetBounds(0, 0, 0, 0); |
| 419 next_button_->SetBounds(0, 0, 0, 0); | 415 next_button_->SetBounds(0, 0, 0, 0); |
| 420 } | 416 } |
| 421 | 417 |
| 422 // Size and place the thumb | 418 // Size and place the thumb |
| 423 CSize thumb_prefsize; | 419 gfx::Size thumb_prefsize = thumb_->GetPreferredSize(); |
| 424 thumb_->GetPreferredSize(&thumb_prefsize); | |
| 425 gfx::Rect track_bounds = GetTrackBounds(); | 420 gfx::Rect track_bounds = GetTrackBounds(); |
| 426 | 421 |
| 427 // Preserve the height/width of the thumb (depending on orientation) as set | 422 // Preserve the height/width of the thumb (depending on orientation) as set |
| 428 // by the last call to |Update|, but coerce the width/height to be the | 423 // by the last call to |Update|, but coerce the width/height to be the |
| 429 // appropriate value for the bitmaps provided. | 424 // appropriate value for the bitmaps provided. |
| 430 if (IsHorizontal()) { | 425 if (IsHorizontal()) { |
| 431 thumb_->SetBounds(thumb_->x(), thumb_->y(), thumb_->width(), | 426 thumb_->SetBounds(thumb_->x(), thumb_->y(), thumb_->width(), |
| 432 thumb_prefsize.cy); | 427 thumb_prefsize.height()); |
| 433 } else { | 428 } else { |
| 434 thumb_->SetBounds(thumb_->x(), thumb_->y(), thumb_prefsize.cx, | 429 thumb_->SetBounds(thumb_->x(), thumb_->y(), thumb_prefsize.width(), |
| 435 thumb_->height()); | 430 thumb_->height()); |
| 436 } | 431 } |
| 437 | 432 |
| 438 // Hide the thumb if the track isn't tall enough to display even a tiny | 433 // Hide the thumb if the track isn't tall enough to display even a tiny |
| 439 // thumb. The user can only use the mousewheel, scroll buttons or keyboard | 434 // thumb. The user can only use the mousewheel, scroll buttons or keyboard |
| 440 // in this scenario. | 435 // in this scenario. |
| 441 if ((IsHorizontal() && (track_bounds.width() < thumb_prefsize.cx)) || | 436 if ((IsHorizontal() && (track_bounds.width() < thumb_prefsize.width()) || |
| 442 (!IsHorizontal() && (track_bounds.height() < thumb_prefsize.cy))) { | 437 (!IsHorizontal() && (track_bounds.height() < thumb_prefsize.height())))) { |
| 443 thumb_->SetVisible(false); | 438 thumb_->SetVisible(false); |
| 444 } else if (!thumb_->IsVisible()) { | 439 } else if (!thumb_->IsVisible()) { |
| 445 thumb_->SetVisible(true); | 440 thumb_->SetVisible(true); |
| 446 } | 441 } |
| 447 } | 442 } |
| 448 | 443 |
| 449 void BitmapScrollBar::DidChangeBounds(const CRect& previous, | 444 void BitmapScrollBar::DidChangeBounds(const CRect& previous, |
| 450 const CRect& current) { | 445 const CRect& current) { |
| 451 Layout(); | 446 Layout(); |
| 452 } | 447 } |
| (...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 665 // content size multiplied by the height of the thumb track. | 660 // content size multiplied by the height of the thumb track. |
| 666 double ratio = static_cast<double>(viewport_size) / contents_size_; | 661 double ratio = static_cast<double>(viewport_size) / contents_size_; |
| 667 int thumb_size = static_cast<int>(ratio * GetTrackSize()); | 662 int thumb_size = static_cast<int>(ratio * GetTrackSize()); |
| 668 thumb_->SetSize(thumb_size); | 663 thumb_->SetSize(thumb_size); |
| 669 | 664 |
| 670 int thumb_position = CalculateThumbPosition(contents_scroll_offset); | 665 int thumb_position = CalculateThumbPosition(contents_scroll_offset); |
| 671 thumb_->SetPosition(thumb_position); | 666 thumb_->SetPosition(thumb_position); |
| 672 } | 667 } |
| 673 | 668 |
| 674 int BitmapScrollBar::GetLayoutSize() const { | 669 int BitmapScrollBar::GetLayoutSize() const { |
| 675 CSize prefsize; | 670 gfx::Size prefsize = prev_button_->GetPreferredSize(); |
| 676 prev_button_->GetPreferredSize(&prefsize); | 671 return IsHorizontal() ? prefsize.height() : prefsize.width(); |
| 677 return IsHorizontal() ? prefsize.cy : prefsize.cx; | |
| 678 } | 672 } |
| 679 | 673 |
| 680 int BitmapScrollBar::GetPosition() const { | 674 int BitmapScrollBar::GetPosition() const { |
| 681 return thumb_->GetPosition(); | 675 return thumb_->GetPosition(); |
| 682 } | 676 } |
| 683 | 677 |
| 684 /////////////////////////////////////////////////////////////////////////////// | 678 /////////////////////////////////////////////////////////////////////////////// |
| 685 // BitmapScrollBar, private: | 679 // BitmapScrollBar, private: |
| 686 | 680 |
| 687 void BitmapScrollBar::ScrollContentsToOffset() { | 681 void BitmapScrollBar::ScrollContentsToOffset() { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 705 return (thumb_position * contents_size_) / GetTrackSize(); | 699 return (thumb_position * contents_size_) / GetTrackSize(); |
| 706 } | 700 } |
| 707 | 701 |
| 708 void BitmapScrollBar::SetThumbTrackState(BaseButton::ButtonState state) { | 702 void BitmapScrollBar::SetThumbTrackState(BaseButton::ButtonState state) { |
| 709 thumb_track_state_ = state; | 703 thumb_track_state_ = state; |
| 710 SchedulePaint(); | 704 SchedulePaint(); |
| 711 } | 705 } |
| 712 | 706 |
| 713 } | 707 } |
| 714 | 708 |
| OLD | NEW |