Chromium Code Reviews| Index: views/controls/scrollbar/native_scroll_bar_views.cc |
| diff --git a/views/controls/scrollbar/native_scroll_bar_views.cc b/views/controls/scrollbar/native_scroll_bar_views.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6100d58d56886b17bce0047c2775681fa6c0636a |
| --- /dev/null |
| +++ b/views/controls/scrollbar/native_scroll_bar_views.cc |
| @@ -0,0 +1,367 @@ |
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "views/controls/scrollbar/native_scroll_bar_views.h" |
| + |
| +#include "base/logging.h" |
| +#include "ui/base/keycodes/keyboard_codes.h" |
| +#include "ui/gfx/canvas.h" |
| +#include "ui/gfx/canvas_skia.h" |
| +#include "ui/gfx/path.h" |
| +#include "views/controls/button/custom_button.h" |
| +#include "views/controls/focusable_border.h" |
| +#include "views/controls/scrollbar/native_scroll_bar.h" |
| +#include "views/controls/scrollbar/scroll_bar.h" |
| +#include "views/controls/scrollbar/base_scroll_bar_button.h" |
| +#include "views/controls/scrollbar/base_scroll_bar_thumb.h" |
| + |
| +namespace views { |
| + |
| +namespace { |
| + |
| +// Wrapper for the scroll buttons. |
| +class ScrollBarButton : public BaseScrollBarButton { |
| + public: |
| + enum Type { |
| + UP, |
| + DOWN, |
| + LEFT, |
| + RIGHT, |
| + }; |
| + |
| + ScrollBarButton(ButtonListener* listener, Type type); |
| + virtual ~ScrollBarButton(); |
| + |
| + virtual gfx::Size GetPreferredSize() OVERRIDE; |
| + |
| + protected: |
| + virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE; |
| + |
| + private: |
| + gfx::NativeTheme::ExtraParams GetNativeThemeParams() const; |
| + gfx::NativeTheme::Part GetNativeThemePart() const; |
| + gfx::NativeTheme::State GetNativeThemeState() const; |
| + |
| + Type type_; |
| +}; |
| + |
| +// Wrapper for the scroll thumb |
| +class ScrollBarThumb : public BaseScrollBarThumb { |
| + public: |
| + explicit ScrollBarThumb(BaseScrollBar* scroll_bar); |
| + virtual ~ScrollBarThumb(); |
| + |
| + virtual gfx::Size GetPreferredSize() OVERRIDE; |
| + |
| + protected: |
| + virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE; |
| + |
| + private: |
| + gfx::NativeTheme::ExtraParams GetNativeThemeParams() const; |
| + gfx::NativeTheme::Part GetNativeThemePart() const; |
| + gfx::NativeTheme::State GetNativeThemeState() const; |
| + |
| + ScrollBar* scroll_bar_; |
| +}; |
| + |
| +///////////////////////////////////////////////////////////////////////////// |
| +// ScrollBarButton |
| + |
| +ScrollBarButton::ScrollBarButton( |
| + ButtonListener* listener, |
| + Type type) |
| + : BaseScrollBarButton(listener), |
| + type_(type) { |
| +} |
| + |
| +ScrollBarButton::~ScrollBarButton() { |
| +} |
| + |
| +gfx::Size ScrollBarButton::GetPreferredSize() { |
| + const gfx::NativeTheme* native_theme = gfx::NativeTheme::instance(); |
| + return native_theme->GetPartSize(GetNativeThemePart(), |
| + GetNativeThemeState(), |
| + GetNativeThemeParams()); |
| +} |
| + |
| +void ScrollBarButton::OnPaint(gfx::Canvas* canvas) { |
| + const gfx::NativeTheme* native_theme = gfx::NativeTheme::instance(); |
| + gfx::Rect bounds; |
| + bounds.set_size(GetPreferredSize()); |
| + |
| + native_theme->Paint(canvas->AsCanvasSkia(), |
| + GetNativeThemePart(), |
| + GetNativeThemeState(), |
| + bounds, |
| + GetNativeThemeParams()); |
| +} |
| + |
| +gfx::NativeTheme::ExtraParams |
| + ScrollBarButton::GetNativeThemeParams() const { |
| + gfx::NativeTheme::ExtraParams params; |
| + |
| + switch (state_) { |
| + case CustomButton::BS_HOT: |
| + params.scrollbar_arrow.is_hovering = true; |
| + break; |
| + default: |
| + params.scrollbar_arrow.is_hovering = false; |
| + break; |
| + } |
| + |
| + return params; |
| +} |
| + |
| +gfx::NativeTheme::Part |
| + ScrollBarButton::GetNativeThemePart() const { |
| + switch (type_) { |
| + case UP: |
| + return gfx::NativeTheme::kScrollbarUpArrow; |
| + case DOWN: |
| + return gfx::NativeTheme::kScrollbarDownArrow; |
| + case LEFT: |
| + return gfx::NativeTheme::kScrollbarLeftArrow; |
| + case RIGHT: |
| + return gfx::NativeTheme::kScrollbarRightArrow; |
| + } |
| +} |
| + |
| +gfx::NativeTheme::State |
| + ScrollBarButton::GetNativeThemeState() const { |
| + gfx::NativeTheme::State state; |
| + |
| + switch (state_) { |
| + case CustomButton::BS_HOT: |
| + state = gfx::NativeTheme::kHovered; |
| + break; |
| + case CustomButton::BS_PUSHED: |
| + state = gfx::NativeTheme::kPressed; |
| + break; |
| + case CustomButton::BS_DISABLED: |
| + state = gfx::NativeTheme::kDisabled; |
| + break; |
| + case CustomButton::BS_NORMAL: |
| + default: |
| + state = gfx::NativeTheme::kNormal; |
| + break; |
| + } |
| + |
| + return state; |
| +} |
| + |
| +///////////////////////////////////////////////////////////////////////////// |
| +// ScrollBarThumb |
| + |
| +ScrollBarThumb::ScrollBarThumb(BaseScrollBar* scroll_bar) |
| + : BaseScrollBarThumb(scroll_bar), |
| + scroll_bar_(scroll_bar) { |
| +} |
| + |
| +ScrollBarThumb::~ScrollBarThumb() { |
| +} |
| + |
| +gfx::Size ScrollBarThumb::GetPreferredSize() { |
| + const gfx::NativeTheme* native_theme = gfx::NativeTheme::instance(); |
| + return native_theme->GetPartSize(GetNativeThemePart(), |
| + GetNativeThemeState(), |
| + GetNativeThemeParams()); |
| +} |
| + |
| +void ScrollBarThumb::OnPaint(gfx::Canvas* canvas) { |
| + const gfx::NativeTheme* native_theme = gfx::NativeTheme::instance(); |
| + |
| + native_theme->Paint(canvas->AsCanvasSkia(), |
| + GetNativeThemePart(), |
| + GetNativeThemeState(), |
| + GetLocalBounds(), |
| + GetNativeThemeParams()); |
| +} |
| + |
| +gfx::NativeTheme::ExtraParams |
| + ScrollBarThumb::GetNativeThemeParams() const { |
| + gfx::NativeTheme::ExtraParams params; |
| + |
| + switch (GetState()) { |
| + case CustomButton::BS_HOT: |
| + params.scrollbar_thumb.is_hovering = true; |
| + break; |
| + default: |
| + params.scrollbar_thumb.is_hovering = false; |
| + break; |
| + } |
| + |
| + return params; |
| +} |
| + |
| +gfx::NativeTheme::Part ScrollBarThumb::GetNativeThemePart() const { |
| + if (scroll_bar_->IsHorizontal()) |
| + return gfx::NativeTheme::kScrollbarHorizontalThumb; |
| + else |
|
Ben Goodger (Google)
2011/08/23 16:05:15
no else after return
|
| + return gfx::NativeTheme::kScrollbarVerticalThumb; |
| +} |
| + |
| +gfx::NativeTheme::State ScrollBarThumb::GetNativeThemeState() const { |
| + gfx::NativeTheme::State state; |
| + |
| + switch (GetState()) { |
| + case CustomButton::BS_HOT: |
| + state = gfx::NativeTheme::kHovered; |
| + break; |
| + case CustomButton::BS_PUSHED: |
| + state = gfx::NativeTheme::kPressed; |
| + break; |
| + case CustomButton::BS_DISABLED: |
| + state = gfx::NativeTheme::kDisabled; |
| + break; |
| + case CustomButton::BS_NORMAL: |
| + default: |
| + state = gfx::NativeTheme::kNormal; |
| + break; |
| + } |
| + |
| + return state; |
| +} |
| + |
| +} // namespace |
| + |
| +//////////////////////////////////////////////////////////////////////////////// |
| +// NativeScrollBarViews, public: |
| + |
| +NativeScrollBarViews::NativeScrollBarViews(NativeScrollBar* scroll_bar) |
| + : BaseScrollBar(scroll_bar->IsHorizontal(), |
| + new ScrollBarThumb(this)), |
| + native_scroll_bar_(scroll_bar) { |
| + SetController(native_scroll_bar_->GetController()); |
| + |
| + if (native_scroll_bar_->IsHorizontal()) { |
| + prev_button_ = new ScrollBarButton(this, ScrollBarButton::LEFT); |
| + next_button_ = new ScrollBarButton(this, ScrollBarButton::RIGHT); |
| + |
| + part_ = gfx::NativeTheme::kScrollbarHorizontalTrack; |
| + } else { |
| + prev_button_ = new ScrollBarButton(this, ScrollBarButton::UP); |
| + next_button_ = new ScrollBarButton(this, ScrollBarButton::DOWN); |
| + |
| + part_ = gfx::NativeTheme::kScrollbarVerticalTrack; |
| + } |
| + |
| + state_ = gfx::NativeTheme::kNormal; |
| + |
| + AddChildView(prev_button_); |
| + AddChildView(next_button_); |
| +} |
| + |
| +NativeScrollBarViews::~NativeScrollBarViews() { |
| +} |
| + |
| +//////////////////////////////////////////////////////////////////////////////// |
| +// NativeScrollBarViews, View overrides: |
| + |
| +void NativeScrollBarViews::Layout() { |
| + SetBoundsRect(native_scroll_bar_->GetLocalBounds()); |
| + |
| + gfx::Size size = prev_button_->GetPreferredSize(); |
| + prev_button_->SetBounds(0, 0, size.width(), size.height()); |
| + |
| + if (native_scroll_bar_->IsHorizontal()) { |
| + next_button_->SetBounds(width() - size.width(), 0, |
| + size.width(), size.height()); |
| + } else { |
| + next_button_->SetBounds(0, height() - size.height(), |
| + size.width(), size.height()); |
| + } |
| + |
| + GetThumb()->SetBoundsRect(GetTrackBounds()); |
| +} |
| + |
| +gfx::Size NativeScrollBarViews::GetPreferredSize() { |
| + if (native_scroll_bar_->IsHorizontal()) |
| + return gfx::Size(0, GetHorizontalScrollBarHeight()); |
| + return gfx::Size(GetVerticalScrollBarWidth(), 0); |
| +} |
| + |
| +int NativeScrollBarViews::GetLayoutSize() const { |
| + gfx::Size size = prev_button_->GetPreferredSize(); |
| + return IsHorizontal() ? size.height() : size.width(); |
| +} |
| + |
| +void NativeScrollBarViews::ScrollToPosition(int position) { |
| + GetController()->ScrollToPosition(native_scroll_bar_, position); |
| +} |
| + |
| +int NativeScrollBarViews::GetScrollIncrement(bool is_page, |
| + bool is_positive) { |
| + return GetController()->GetScrollIncrement(native_scroll_bar_, |
| + is_page, |
| + is_positive); |
| +} |
| + |
| +void NativeScrollBarViews::OnPaint(gfx::Canvas* canvas) { |
| + const gfx::NativeTheme* native_theme = gfx::NativeTheme::instance(); |
| + gfx::Rect bounds = GetTrackBounds(); |
| + |
| + params_.scrollbar_track.track_x = bounds.x(); |
| + params_.scrollbar_track.track_y = bounds.y(); |
| + params_.scrollbar_track.track_width = bounds.width(); |
| + params_.scrollbar_track.track_height = bounds.height(); |
| + |
| + |
| + native_theme->Paint(canvas->AsCanvasSkia(), |
| + part_, |
| + state_, |
| + bounds, |
| + params_); |
| +} |
| + |
| +////////////////////////////////////////////////////////////////////////////// |
| +// BaseButton::ButtonListener overrides: |
| + |
| +void NativeScrollBarViews::ButtonPressed(Button* sender, |
| + const views::Event& event) { |
| + if (sender == prev_button_) { |
| + ScrollByAmount(SCROLL_PREV_LINE); |
| + } else if (sender == next_button_) { |
| + ScrollByAmount(SCROLL_NEXT_LINE); |
| + } |
| +} |
| + |
| +//////////////////////////////////////////////////////////////////////////////// |
| +// NativeScrollBarViews, NativeScrollBarWrapper overrides: |
| + |
| +int NativeScrollBarViews::GetPosition() const { |
| + return BaseScrollBar::GetPosition(); |
| +} |
| + |
| +View* NativeScrollBarViews::GetView() { |
| + return this; |
| +} |
| + |
| +void NativeScrollBarViews::Update(int viewport_size, |
| + int content_size, |
| + int current_pos) { |
| + BaseScrollBar::Update(viewport_size, content_size, current_pos); |
| +} |
| + |
| +//////////////////////////////////////////////////////////////////////////////// |
| +// NativeScrollBarViews, private: |
| + |
| +gfx::Rect NativeScrollBarViews::GetTrackBounds() const { |
| + gfx::Rect bounds = GetLocalBounds(); |
| + gfx::Size size = prev_button_->GetPreferredSize(); |
| + BaseScrollBarThumb* thumb = GetThumb(); |
| + |
| + if (native_scroll_bar_->IsHorizontal()) { |
| + bounds.set_x(bounds.x() + size.width()); |
| + bounds.set_width(bounds.width() - 2 * size.width()); |
| + bounds.set_height(thumb->GetPreferredSize().height()); |
| + } else { |
| + bounds.set_y(bounds.y() + size.height()); |
| + bounds.set_height(bounds.height() - 2 * size.height()); |
| + bounds.set_width(thumb->GetPreferredSize().width()); |
| + } |
| + |
| + return bounds; |
| +} |
| + |
| +} // namespace views |