OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "views/controls/scrollbar/native_scroll_bar_views.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "ui/base/keycodes/keyboard_codes.h" |
| 9 #include "ui/gfx/canvas.h" |
| 10 #include "ui/gfx/canvas_skia.h" |
| 11 #include "ui/gfx/path.h" |
| 12 #include "views/controls/button/custom_button.h" |
| 13 #include "views/controls/focusable_border.h" |
| 14 #include "views/controls/scrollbar/native_scroll_bar.h" |
| 15 #include "views/controls/scrollbar/scroll_bar.h" |
| 16 #include "views/controls/scrollbar/base_scroll_bar_button.h" |
| 17 #include "views/controls/scrollbar/base_scroll_bar_thumb.h" |
| 18 |
| 19 namespace views { |
| 20 |
| 21 namespace { |
| 22 |
| 23 // Wrapper for the scroll buttons. |
| 24 class ScrollBarButton : public BaseScrollBarButton { |
| 25 public: |
| 26 enum Type { |
| 27 UP, |
| 28 DOWN, |
| 29 LEFT, |
| 30 RIGHT, |
| 31 }; |
| 32 |
| 33 ScrollBarButton(ButtonListener* listener, Type type); |
| 34 virtual ~ScrollBarButton(); |
| 35 |
| 36 virtual gfx::Size GetPreferredSize() OVERRIDE; |
| 37 |
| 38 protected: |
| 39 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE; |
| 40 |
| 41 private: |
| 42 gfx::NativeTheme::ExtraParams GetNativeThemeParams() const; |
| 43 gfx::NativeTheme::Part GetNativeThemePart() const; |
| 44 gfx::NativeTheme::State GetNativeThemeState() const; |
| 45 |
| 46 Type type_; |
| 47 }; |
| 48 |
| 49 // Wrapper for the scroll thumb |
| 50 class ScrollBarThumb : public BaseScrollBarThumb { |
| 51 public: |
| 52 explicit ScrollBarThumb(BaseScrollBar* scroll_bar); |
| 53 virtual ~ScrollBarThumb(); |
| 54 |
| 55 virtual gfx::Size GetPreferredSize() OVERRIDE; |
| 56 |
| 57 protected: |
| 58 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE; |
| 59 |
| 60 private: |
| 61 gfx::NativeTheme::ExtraParams GetNativeThemeParams() const; |
| 62 gfx::NativeTheme::Part GetNativeThemePart() const; |
| 63 gfx::NativeTheme::State GetNativeThemeState() const; |
| 64 |
| 65 ScrollBar* scroll_bar_; |
| 66 }; |
| 67 |
| 68 ///////////////////////////////////////////////////////////////////////////// |
| 69 // ScrollBarButton |
| 70 |
| 71 ScrollBarButton::ScrollBarButton( |
| 72 ButtonListener* listener, |
| 73 Type type) |
| 74 : BaseScrollBarButton(listener), |
| 75 type_(type) { |
| 76 } |
| 77 |
| 78 ScrollBarButton::~ScrollBarButton() { |
| 79 } |
| 80 |
| 81 gfx::Size ScrollBarButton::GetPreferredSize() { |
| 82 const gfx::NativeTheme* native_theme = gfx::NativeTheme::instance(); |
| 83 return native_theme->GetPartSize(GetNativeThemePart(), |
| 84 GetNativeThemeState(), |
| 85 GetNativeThemeParams()); |
| 86 } |
| 87 |
| 88 void ScrollBarButton::OnPaint(gfx::Canvas* canvas) { |
| 89 const gfx::NativeTheme* native_theme = gfx::NativeTheme::instance(); |
| 90 gfx::Rect bounds; |
| 91 bounds.set_size(GetPreferredSize()); |
| 92 |
| 93 native_theme->Paint(canvas->AsCanvasSkia(), |
| 94 GetNativeThemePart(), |
| 95 GetNativeThemeState(), |
| 96 bounds, |
| 97 GetNativeThemeParams()); |
| 98 } |
| 99 |
| 100 gfx::NativeTheme::ExtraParams |
| 101 ScrollBarButton::GetNativeThemeParams() const { |
| 102 gfx::NativeTheme::ExtraParams params; |
| 103 |
| 104 switch (state_) { |
| 105 case CustomButton::BS_HOT: |
| 106 params.scrollbar_arrow.is_hovering = true; |
| 107 break; |
| 108 default: |
| 109 params.scrollbar_arrow.is_hovering = false; |
| 110 break; |
| 111 } |
| 112 |
| 113 return params; |
| 114 } |
| 115 |
| 116 gfx::NativeTheme::Part |
| 117 ScrollBarButton::GetNativeThemePart() const { |
| 118 switch (type_) { |
| 119 case UP: |
| 120 return gfx::NativeTheme::kScrollbarUpArrow; |
| 121 case DOWN: |
| 122 return gfx::NativeTheme::kScrollbarDownArrow; |
| 123 case LEFT: |
| 124 return gfx::NativeTheme::kScrollbarLeftArrow; |
| 125 case RIGHT: |
| 126 return gfx::NativeTheme::kScrollbarRightArrow; |
| 127 default: |
| 128 return gfx::NativeTheme::kScrollbarUpArrow; |
| 129 } |
| 130 } |
| 131 |
| 132 gfx::NativeTheme::State |
| 133 ScrollBarButton::GetNativeThemeState() const { |
| 134 gfx::NativeTheme::State state; |
| 135 |
| 136 switch (state_) { |
| 137 case CustomButton::BS_HOT: |
| 138 state = gfx::NativeTheme::kHovered; |
| 139 break; |
| 140 case CustomButton::BS_PUSHED: |
| 141 state = gfx::NativeTheme::kPressed; |
| 142 break; |
| 143 case CustomButton::BS_DISABLED: |
| 144 state = gfx::NativeTheme::kDisabled; |
| 145 break; |
| 146 case CustomButton::BS_NORMAL: |
| 147 default: |
| 148 state = gfx::NativeTheme::kNormal; |
| 149 break; |
| 150 } |
| 151 |
| 152 return state; |
| 153 } |
| 154 |
| 155 ///////////////////////////////////////////////////////////////////////////// |
| 156 // ScrollBarThumb |
| 157 |
| 158 ScrollBarThumb::ScrollBarThumb(BaseScrollBar* scroll_bar) |
| 159 : BaseScrollBarThumb(scroll_bar), |
| 160 scroll_bar_(scroll_bar) { |
| 161 } |
| 162 |
| 163 ScrollBarThumb::~ScrollBarThumb() { |
| 164 } |
| 165 |
| 166 gfx::Size ScrollBarThumb::GetPreferredSize() { |
| 167 const gfx::NativeTheme* native_theme = gfx::NativeTheme::instance(); |
| 168 return native_theme->GetPartSize(GetNativeThemePart(), |
| 169 GetNativeThemeState(), |
| 170 GetNativeThemeParams()); |
| 171 } |
| 172 |
| 173 void ScrollBarThumb::OnPaint(gfx::Canvas* canvas) { |
| 174 const gfx::NativeTheme* native_theme = gfx::NativeTheme::instance(); |
| 175 |
| 176 native_theme->Paint(canvas->AsCanvasSkia(), |
| 177 GetNativeThemePart(), |
| 178 GetNativeThemeState(), |
| 179 GetLocalBounds(), |
| 180 GetNativeThemeParams()); |
| 181 } |
| 182 |
| 183 gfx::NativeTheme::ExtraParams |
| 184 ScrollBarThumb::GetNativeThemeParams() const { |
| 185 gfx::NativeTheme::ExtraParams params; |
| 186 |
| 187 switch (GetState()) { |
| 188 case CustomButton::BS_HOT: |
| 189 params.scrollbar_thumb.is_hovering = true; |
| 190 break; |
| 191 default: |
| 192 params.scrollbar_thumb.is_hovering = false; |
| 193 break; |
| 194 } |
| 195 |
| 196 return params; |
| 197 } |
| 198 |
| 199 gfx::NativeTheme::Part ScrollBarThumb::GetNativeThemePart() const { |
| 200 if (scroll_bar_->IsHorizontal()) |
| 201 return gfx::NativeTheme::kScrollbarHorizontalThumb; |
| 202 return gfx::NativeTheme::kScrollbarVerticalThumb; |
| 203 } |
| 204 |
| 205 gfx::NativeTheme::State ScrollBarThumb::GetNativeThemeState() const { |
| 206 gfx::NativeTheme::State state; |
| 207 |
| 208 switch (GetState()) { |
| 209 case CustomButton::BS_HOT: |
| 210 state = gfx::NativeTheme::kHovered; |
| 211 break; |
| 212 case CustomButton::BS_PUSHED: |
| 213 state = gfx::NativeTheme::kPressed; |
| 214 break; |
| 215 case CustomButton::BS_DISABLED: |
| 216 state = gfx::NativeTheme::kDisabled; |
| 217 break; |
| 218 case CustomButton::BS_NORMAL: |
| 219 default: |
| 220 state = gfx::NativeTheme::kNormal; |
| 221 break; |
| 222 } |
| 223 |
| 224 return state; |
| 225 } |
| 226 |
| 227 } // namespace |
| 228 |
| 229 //////////////////////////////////////////////////////////////////////////////// |
| 230 // NativeScrollBarViews, public: |
| 231 |
| 232 NativeScrollBarViews::NativeScrollBarViews(NativeScrollBar* scroll_bar) |
| 233 : BaseScrollBar(scroll_bar->IsHorizontal(), |
| 234 new ScrollBarThumb(this)), |
| 235 native_scroll_bar_(scroll_bar) { |
| 236 SetController(native_scroll_bar_->GetController()); |
| 237 |
| 238 if (native_scroll_bar_->IsHorizontal()) { |
| 239 prev_button_ = new ScrollBarButton(this, ScrollBarButton::LEFT); |
| 240 next_button_ = new ScrollBarButton(this, ScrollBarButton::RIGHT); |
| 241 |
| 242 part_ = gfx::NativeTheme::kScrollbarHorizontalTrack; |
| 243 } else { |
| 244 prev_button_ = new ScrollBarButton(this, ScrollBarButton::UP); |
| 245 next_button_ = new ScrollBarButton(this, ScrollBarButton::DOWN); |
| 246 |
| 247 part_ = gfx::NativeTheme::kScrollbarVerticalTrack; |
| 248 } |
| 249 |
| 250 state_ = gfx::NativeTheme::kNormal; |
| 251 |
| 252 AddChildView(prev_button_); |
| 253 AddChildView(next_button_); |
| 254 |
| 255 prev_button_->set_context_menu_controller(this); |
| 256 next_button_->set_context_menu_controller(this); |
| 257 } |
| 258 |
| 259 NativeScrollBarViews::~NativeScrollBarViews() { |
| 260 } |
| 261 |
| 262 //////////////////////////////////////////////////////////////////////////////// |
| 263 // NativeScrollBarViews, View overrides: |
| 264 |
| 265 void NativeScrollBarViews::Layout() { |
| 266 SetBoundsRect(native_scroll_bar_->GetLocalBounds()); |
| 267 |
| 268 gfx::Size size = prev_button_->GetPreferredSize(); |
| 269 prev_button_->SetBounds(0, 0, size.width(), size.height()); |
| 270 |
| 271 if (native_scroll_bar_->IsHorizontal()) { |
| 272 next_button_->SetBounds(width() - size.width(), 0, |
| 273 size.width(), size.height()); |
| 274 } else { |
| 275 next_button_->SetBounds(0, height() - size.height(), |
| 276 size.width(), size.height()); |
| 277 } |
| 278 |
| 279 GetThumb()->SetBoundsRect(GetTrackBounds()); |
| 280 } |
| 281 |
| 282 void NativeScrollBarViews::OnPaint(gfx::Canvas* canvas) { |
| 283 const gfx::NativeTheme* native_theme = gfx::NativeTheme::instance(); |
| 284 gfx::Rect bounds = GetTrackBounds(); |
| 285 |
| 286 params_.scrollbar_track.track_x = bounds.x(); |
| 287 params_.scrollbar_track.track_y = bounds.y(); |
| 288 params_.scrollbar_track.track_width = bounds.width(); |
| 289 params_.scrollbar_track.track_height = bounds.height(); |
| 290 |
| 291 |
| 292 native_theme->Paint(canvas->AsCanvasSkia(), |
| 293 part_, |
| 294 state_, |
| 295 bounds, |
| 296 params_); |
| 297 } |
| 298 |
| 299 gfx::Size NativeScrollBarViews::GetPreferredSize() { |
| 300 if (native_scroll_bar_->IsHorizontal()) |
| 301 return gfx::Size(0, GetHorizontalScrollBarHeight()); |
| 302 return gfx::Size(GetVerticalScrollBarWidth(), 0); |
| 303 } |
| 304 |
| 305 int NativeScrollBarViews::GetLayoutSize() const { |
| 306 gfx::Size size = prev_button_->GetPreferredSize(); |
| 307 return IsHorizontal() ? size.height() : size.width(); |
| 308 } |
| 309 |
| 310 void NativeScrollBarViews::ScrollToPosition(int position) { |
| 311 GetController()->ScrollToPosition(native_scroll_bar_, position); |
| 312 } |
| 313 |
| 314 int NativeScrollBarViews::GetScrollIncrement(bool is_page, |
| 315 bool is_positive) { |
| 316 return GetController()->GetScrollIncrement(native_scroll_bar_, |
| 317 is_page, |
| 318 is_positive); |
| 319 } |
| 320 |
| 321 ////////////////////////////////////////////////////////////////////////////// |
| 322 // BaseButton::ButtonListener overrides: |
| 323 |
| 324 void NativeScrollBarViews::ButtonPressed(Button* sender, |
| 325 const views::Event& event) { |
| 326 if (sender == prev_button_) { |
| 327 ScrollByAmount(SCROLL_PREV_LINE); |
| 328 } else if (sender == next_button_) { |
| 329 ScrollByAmount(SCROLL_NEXT_LINE); |
| 330 } |
| 331 } |
| 332 |
| 333 //////////////////////////////////////////////////////////////////////////////// |
| 334 // NativeScrollBarViews, NativeScrollBarWrapper overrides: |
| 335 |
| 336 int NativeScrollBarViews::GetPosition() const { |
| 337 return BaseScrollBar::GetPosition(); |
| 338 } |
| 339 |
| 340 View* NativeScrollBarViews::GetView() { |
| 341 return this; |
| 342 } |
| 343 |
| 344 void NativeScrollBarViews::Update(int viewport_size, |
| 345 int content_size, |
| 346 int current_pos) { |
| 347 BaseScrollBar::Update(viewport_size, content_size, current_pos); |
| 348 } |
| 349 |
| 350 //////////////////////////////////////////////////////////////////////////////// |
| 351 // NativeScrollBarViews, private: |
| 352 |
| 353 gfx::Rect NativeScrollBarViews::GetTrackBounds() const { |
| 354 gfx::Rect bounds = GetLocalBounds(); |
| 355 gfx::Size size = prev_button_->GetPreferredSize(); |
| 356 BaseScrollBarThumb* thumb = GetThumb(); |
| 357 |
| 358 if (native_scroll_bar_->IsHorizontal()) { |
| 359 bounds.set_x(bounds.x() + size.width()); |
| 360 bounds.set_width(bounds.width() - 2 * size.width()); |
| 361 bounds.set_height(thumb->GetPreferredSize().height()); |
| 362 } else { |
| 363 bounds.set_y(bounds.y() + size.height()); |
| 364 bounds.set_height(bounds.height() - 2 * size.height()); |
| 365 bounds.set_width(thumb->GetPreferredSize().width()); |
| 366 } |
| 367 |
| 368 return bounds; |
| 369 } |
| 370 |
| 371 } // namespace views |
OLD | NEW |