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