| 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/slider.h" | 5 #include "ui/views/controls/slider.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "base/stringprintf.h" | 9 #include "base/stringprintf.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| 11 #include "grit/ui_resources.h" |
| 11 #include "third_party/skia/include/core/SkCanvas.h" | 12 #include "third_party/skia/include/core/SkCanvas.h" |
| 12 #include "third_party/skia/include/core/SkColor.h" | 13 #include "third_party/skia/include/core/SkColor.h" |
| 13 #include "third_party/skia/include/core/SkPaint.h" | 14 #include "third_party/skia/include/core/SkPaint.h" |
| 14 #include "ui/base/accessibility/accessible_view_state.h" | 15 #include "ui/base/accessibility/accessible_view_state.h" |
| 15 #include "ui/base/animation/slide_animation.h" | 16 #include "ui/base/animation/slide_animation.h" |
| 17 #include "ui/base/resource/resource_bundle.h" |
| 16 #include "ui/gfx/canvas.h" | 18 #include "ui/gfx/canvas.h" |
| 17 #include "ui/gfx/point.h" | 19 #include "ui/gfx/point.h" |
| 18 #include "ui/gfx/rect.h" | 20 #include "ui/gfx/rect.h" |
| 19 #include "ui/views/widget/widget.h" | 21 #include "ui/views/widget/widget.h" |
| 20 | 22 |
| 21 namespace { | 23 namespace { |
| 22 const int kSlideValueChangeDurationMS = 150; | 24 const int kSlideValueChangeDurationMS = 150; |
| 25 |
| 26 const int kBarImagesActive[] = { |
| 27 IDR_SLIDER_ACTIVE_LEFT, |
| 28 IDR_SLIDER_ACTIVE_CENTER, |
| 29 IDR_SLIDER_PRESSED_CENTER, |
| 30 IDR_SLIDER_PRESSED_RIGHT, |
| 31 }; |
| 32 |
| 33 const int kBarImagesDisabled[] = { |
| 34 IDR_SLIDER_DISABLED_LEFT, |
| 35 IDR_SLIDER_DISABLED_CENTER, |
| 36 IDR_SLIDER_DISABLED_CENTER, |
| 37 IDR_SLIDER_DISABLED_RIGHT, |
| 38 }; |
| 23 } | 39 } |
| 24 | 40 |
| 25 namespace views { | 41 namespace views { |
| 26 | 42 |
| 27 Slider::Slider(SliderListener* listener, Orientation orientation) | 43 Slider::Slider(SliderListener* listener, Orientation orientation) |
| 28 : listener_(listener), | 44 : listener_(listener), |
| 29 orientation_(orientation), | 45 orientation_(orientation), |
| 30 value_(0.f), | 46 value_(0.f), |
| 31 keyboard_increment_(0.1f), | 47 keyboard_increment_(0.1f), |
| 32 animating_value_(0.f), | 48 animating_value_(0.f), |
| 33 value_is_valid_(false), | 49 value_is_valid_(false), |
| 34 accessibility_events_enabled_(true), | 50 accessibility_events_enabled_(true), |
| 35 focus_border_color_(0) { | 51 focus_border_color_(0), |
| 52 bar_active_images_(kBarImagesActive), |
| 53 bar_disabled_images_(kBarImagesDisabled) { |
| 36 EnableCanvasFlippingForRTLUI(true); | 54 EnableCanvasFlippingForRTLUI(true); |
| 37 set_focusable(true); | 55 set_focusable(true); |
| 56 UpdateState(true); |
| 38 } | 57 } |
| 39 | 58 |
| 40 Slider::~Slider() { | 59 Slider::~Slider() { |
| 41 } | 60 } |
| 42 | 61 |
| 43 void Slider::SetValue(float value) { | 62 void Slider::SetValue(float value) { |
| 44 SetValueInternal(value, VALUE_CHANGED_BY_API); | 63 SetValueInternal(value, VALUE_CHANGED_BY_API); |
| 45 } | 64 } |
| 46 | 65 |
| 47 void Slider::SetKeyboardIncrement(float increment) { | 66 void Slider::SetKeyboardIncrement(float increment) { |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 int amount = base::i18n::IsRTL() ? width() - inset.left() - point.x() : | 105 int amount = base::i18n::IsRTL() ? width() - inset.left() - point.x() : |
| 87 point.x() - inset.left(); | 106 point.x() - inset.left(); |
| 88 SetValueInternal(static_cast<float>(amount) / (width() - inset.width()), | 107 SetValueInternal(static_cast<float>(amount) / (width() - inset.width()), |
| 89 VALUE_CHANGED_BY_USER); | 108 VALUE_CHANGED_BY_USER); |
| 90 } else { | 109 } else { |
| 91 SetValueInternal(1.0f - static_cast<float>(point.y()) / height(), | 110 SetValueInternal(1.0f - static_cast<float>(point.y()) / height(), |
| 92 VALUE_CHANGED_BY_USER); | 111 VALUE_CHANGED_BY_USER); |
| 93 } | 112 } |
| 94 } | 113 } |
| 95 | 114 |
| 115 void Slider::UpdateState(bool control_on) { |
| 116 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 117 if (control_on) { |
| 118 thumb_ = rb.GetImageNamed(IDR_SLIDER_ACTIVE_THUMB).ToImageSkia(); |
| 119 for (int i = 0; i < 4; ++i) |
| 120 images_[i] = rb.GetImageNamed(bar_active_images_[i]).ToImageSkia(); |
| 121 } else { |
| 122 thumb_ = rb.GetImageNamed(IDR_SLIDER_DISABLED_THUMB).ToImageSkia(); |
| 123 for (int i = 0; i < 4; ++i) |
| 124 images_[i] = rb.GetImageNamed(bar_disabled_images_[i]).ToImageSkia(); |
| 125 } |
| 126 bar_height_ = images_[LEFT]->height(); |
| 127 SchedulePaint(); |
| 128 } |
| 129 |
| 96 void Slider::SetAccessibleName(const string16& name) { | 130 void Slider::SetAccessibleName(const string16& name) { |
| 97 accessible_name_ = name; | 131 accessible_name_ = name; |
| 98 } | 132 } |
| 99 | 133 |
| 100 gfx::Size Slider::GetPreferredSize() { | 134 gfx::Size Slider::GetPreferredSize() { |
| 101 const int kSizeMajor = 200; | 135 const int kSizeMajor = 200; |
| 102 const int kSizeMinor = 40; | 136 const int kSizeMinor = 40; |
| 103 | 137 |
| 104 if (orientation_ == HORIZONTAL) | 138 if (orientation_ == HORIZONTAL) |
| 105 return gfx::Size(std::max(width(), kSizeMajor), kSizeMinor); | 139 return gfx::Size(std::max(width(), kSizeMajor), kSizeMinor); |
| 106 return gfx::Size(kSizeMinor, std::max(height(), kSizeMajor)); | 140 return gfx::Size(kSizeMinor, std::max(height(), kSizeMajor)); |
| 107 } | 141 } |
| 108 | 142 |
| 109 void Slider::OnPaint(gfx::Canvas* canvas) { | 143 void Slider::OnPaint(gfx::Canvas* canvas) { |
| 110 // TODO(sad): The painting code should use NativeTheme for various platforms. | |
| 111 const int kButtonRadius = 5; | |
| 112 const int kLineThickness = 5; | |
| 113 const SkColor kFullColor = SkColorSetARGB(125, 0, 0, 0); | |
| 114 const SkColor kEmptyColor = SkColorSetARGB(50, 0, 0, 0); | |
| 115 const SkColor kButtonColor = SK_ColorBLACK; | |
| 116 | |
| 117 gfx::Rect content = GetContentsBounds(); | 144 gfx::Rect content = GetContentsBounds(); |
| 118 float value = move_animation_.get() && move_animation_->is_animating() ? | 145 float value = move_animation_.get() && move_animation_->is_animating() ? |
| 119 animating_value_ : value_; | 146 animating_value_ : value_; |
| 147 if (orientation_ == HORIZONTAL) { |
| 148 // Paint slider bar with image resources. |
| 120 | 149 |
| 121 int button_cx = 0, button_cy = 0; | 150 // Inset the slider bar a little bit, so that the left or the right end of |
| 122 if (orientation_ == HORIZONTAL) { | 151 // the slider bar will not be exposed under the thumb button when the thumb |
| 123 int w = content.width() - kButtonRadius * 2; | 152 // button slides to the left most or right most position. |
| 153 const int kBarInsetX = 2; |
| 154 int bar_width = content.width() - kBarInsetX * 2; |
| 155 int bar_cy = content.height() / 2 - bar_height_ / 2; |
| 156 |
| 157 int w = content.width() - thumb_->width(); |
| 124 int full = value * w; | 158 int full = value * w; |
| 125 int empty = w - full; | 159 int middle = std::max(full, images_[LEFT]->width()); |
| 126 int y = content.height() / 2 - kLineThickness / 2; | |
| 127 canvas->FillRect(gfx::Rect(content.x() + kButtonRadius, y, | |
| 128 full, kLineThickness), | |
| 129 kFullColor); | |
| 130 canvas->FillRect(gfx::Rect(content.x() + full + 2 * kButtonRadius, y, | |
| 131 std::max(0, empty - kButtonRadius), kLineThickness), | |
| 132 kEmptyColor); | |
| 133 | 160 |
| 134 button_cx = content.x() + full + kButtonRadius; | 161 canvas->Save(); |
| 135 button_cy = y + kLineThickness / 2; | 162 canvas->Translate(gfx::Point(kBarInsetX, bar_cy)); |
| 163 canvas->DrawImageInt(*images_[LEFT], 0, 0); |
| 164 canvas->DrawImageInt(*images_[RIGHT], |
| 165 bar_width - images_[RIGHT]->width(), |
| 166 0); |
| 167 canvas->TileImageInt(*images_[CENTER_LEFT], |
| 168 images_[LEFT]->width(), |
| 169 0, |
| 170 middle - images_[LEFT]->width(), |
| 171 bar_height_); |
| 172 canvas->TileImageInt(*images_[CENTER_RIGHT], |
| 173 middle, |
| 174 0, |
| 175 bar_width - middle - images_[RIGHT]->width(), |
| 176 bar_height_); |
| 177 canvas->Restore(); |
| 178 |
| 179 // Paint slider thumb. |
| 180 int button_cx = content.x() + full; |
| 181 int thumb_y = content.height() / 2 - thumb_->height() / 2; |
| 182 canvas->DrawImageInt(*thumb_, button_cx, thumb_y); |
| 136 } else { | 183 } else { |
| 184 // TODO(jennyz): draw vertical slider bar with resources. |
| 185 // TODO(sad): The painting code should use NativeTheme for various |
| 186 // platforms. |
| 187 const int kButtonRadius = 5; |
| 188 const int kLineThickness = 5; |
| 189 const SkColor kFullColor = SkColorSetARGB(125, 0, 0, 0); |
| 190 const SkColor kEmptyColor = SkColorSetARGB(50, 0, 0, 0); |
| 191 const SkColor kButtonColor = SK_ColorBLACK; |
| 192 |
| 193 int button_cx = 0, button_cy = 0; |
| 137 int h = content.height() - kButtonRadius * 2; | 194 int h = content.height() - kButtonRadius * 2; |
| 138 int full = value * h; | 195 int full = value * h; |
| 139 int empty = h - full; | 196 int empty = h - full; |
| 140 int x = content.width() / 2 - kLineThickness / 2; | 197 int x = content.width() / 2 - kLineThickness / 2; |
| 141 canvas->FillRect(gfx::Rect(x, content.y() + kButtonRadius, | 198 canvas->FillRect(gfx::Rect(x, content.y() + kButtonRadius, |
| 142 kLineThickness, empty), | 199 kLineThickness, empty), |
| 143 kEmptyColor); | 200 kEmptyColor); |
| 144 canvas->FillRect(gfx::Rect(x, content.y() + empty + 2 * kButtonRadius, | 201 canvas->FillRect(gfx::Rect(x, content.y() + empty + 2 * kButtonRadius, |
| 145 kLineThickness, full), | 202 kLineThickness, full), |
| 146 kFullColor); | 203 kFullColor); |
| 147 | 204 |
| 148 button_cx = x + kLineThickness / 2; | 205 button_cx = x + kLineThickness / 2; |
| 149 button_cy = content.y() + empty + kButtonRadius; | 206 button_cy = content.y() + empty + kButtonRadius; |
| 207 |
| 208 SkPaint paint; |
| 209 paint.setStyle(SkPaint::kFill_Style); |
| 210 paint.setAntiAlias(true); |
| 211 paint.setColor(kButtonColor); |
| 212 canvas->sk_canvas()->drawCircle(button_cx, button_cy, kButtonRadius, paint); |
| 213 View::OnPaint(canvas); |
| 150 } | 214 } |
| 151 | |
| 152 SkPaint paint; | |
| 153 paint.setStyle(SkPaint::kFill_Style); | |
| 154 paint.setAntiAlias(true); | |
| 155 paint.setColor(kButtonColor); | |
| 156 canvas->sk_canvas()->drawCircle(button_cx, button_cy, kButtonRadius, paint); | |
| 157 View::OnPaint(canvas); | |
| 158 } | 215 } |
| 159 | 216 |
| 160 bool Slider::OnMousePressed(const views::MouseEvent& event) { | 217 bool Slider::OnMousePressed(const views::MouseEvent& event) { |
| 161 if (listener_) | 218 if (listener_) |
| 162 listener_->SliderDragStarted(this); | 219 listener_->SliderDragStarted(this); |
| 163 MoveButtonTo(event.location()); | 220 MoveButtonTo(event.location()); |
| 164 return true; | 221 return true; |
| 165 } | 222 } |
| 166 | 223 |
| 167 bool Slider::OnMouseDragged(const views::MouseEvent& event) { | 224 bool Slider::OnMouseDragged(const views::MouseEvent& event) { |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 221 void Slider::OnPaintFocusBorder(gfx::Canvas* canvas) { | 278 void Slider::OnPaintFocusBorder(gfx::Canvas* canvas) { |
| 222 if (!focus_border_color_) { | 279 if (!focus_border_color_) { |
| 223 View::OnPaintFocusBorder(canvas); | 280 View::OnPaintFocusBorder(canvas); |
| 224 } else if (HasFocus() && (focusable() || IsAccessibilityFocusable())) { | 281 } else if (HasFocus() && (focusable() || IsAccessibilityFocusable())) { |
| 225 canvas->DrawRect(gfx::Rect(1, 1, width() - 3, height() - 3), | 282 canvas->DrawRect(gfx::Rect(1, 1, width() - 3, height() - 3), |
| 226 focus_border_color_); | 283 focus_border_color_); |
| 227 } | 284 } |
| 228 } | 285 } |
| 229 | 286 |
| 230 } // namespace views | 287 } // namespace views |
| OLD | NEW |