Index: ui/views/controls/slider.cc |
diff --git a/ui/views/controls/slider.cc b/ui/views/controls/slider.cc |
index 6929fb0016b03556015d7476b3a13625bbc0f368..97b5eaf1756883b667133f3470a067a82c79c1b5 100644 |
--- a/ui/views/controls/slider.cc |
+++ b/ui/views/controls/slider.cc |
@@ -8,11 +8,13 @@ |
#include "base/message_loop.h" |
#include "base/stringprintf.h" |
#include "base/utf_string_conversions.h" |
+#include "grit/ui_resources.h" |
#include "third_party/skia/include/core/SkCanvas.h" |
#include "third_party/skia/include/core/SkColor.h" |
#include "third_party/skia/include/core/SkPaint.h" |
#include "ui/base/accessibility/accessible_view_state.h" |
#include "ui/base/animation/slide_animation.h" |
+#include "ui/base/resource/resource_bundle.h" |
#include "ui/gfx/canvas.h" |
#include "ui/gfx/point.h" |
#include "ui/gfx/rect.h" |
@@ -20,6 +22,28 @@ |
namespace { |
const int kSlideValueChangeDurationMS = 150; |
+ |
+const int kBarImagesActive[] = { |
+ IDR_SLIDER_ACTIVE_LEFT, |
+ IDR_SLIDER_ACTIVE_CENTER, |
+ IDR_SLIDER_PRESSED_CENTER, |
+ IDR_SLIDER_PRESSED_RIGHT, |
+}; |
+ |
+const int kBarImagesDisabled[] = { |
+ IDR_SLIDER_DISABLED_LEFT, |
+ IDR_SLIDER_DISABLED_CENTER, |
+ IDR_SLIDER_DISABLED_CENTER, |
+ IDR_SLIDER_DISABLED_RIGHT, |
+}; |
+ |
+// The image chunks. |
+enum BorderElements { |
+ LEFT, |
+ CENTER_LEFT, |
+ CENTER_RIGHT, |
+ RIGHT, |
+}; |
} |
namespace views { |
@@ -32,9 +56,12 @@ Slider::Slider(SliderListener* listener, Orientation orientation) |
animating_value_(0.f), |
value_is_valid_(false), |
accessibility_events_enabled_(true), |
- focus_border_color_(0) { |
+ focus_border_color_(0), |
+ bar_active_images_(kBarImagesActive), |
+ bar_disabled_images_(kBarImagesDisabled) { |
EnableCanvasFlippingForRTLUI(true); |
set_focusable(true); |
+ UpdateState(true); |
} |
Slider::~Slider() { |
@@ -93,6 +120,21 @@ void Slider::MoveButtonTo(const gfx::Point& point) { |
} |
} |
+void Slider::UpdateState(bool control_on) { |
+ ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
+ if (control_on) { |
+ thumb_ = rb.GetImageNamed(IDR_SLIDER_ACTIVE_THUMB).ToImageSkia(); |
+ for (int i = 0; i < 4; ++i) |
+ images_[i] = rb.GetImageNamed(bar_active_images_[i]).ToImageSkia(); |
+ } else { |
+ thumb_ = rb.GetImageNamed(IDR_SLIDER_DISABLED_THUMB).ToImageSkia(); |
+ for (int i = 0; i < 4; ++i) |
+ images_[i] = rb.GetImageNamed(bar_disabled_images_[i]).ToImageSkia(); |
+ } |
+ bar_height_ = images_[LEFT]->height(); |
+ SchedulePaint(); |
+} |
+ |
void Slider::SetAccessibleName(const string16& name) { |
accessible_name_ = name; |
} |
@@ -107,33 +149,56 @@ gfx::Size Slider::GetPreferredSize() { |
} |
void Slider::OnPaint(gfx::Canvas* canvas) { |
- // TODO(sad): The painting code should use NativeTheme for various platforms. |
- const int kButtonRadius = 5; |
- const int kLineThickness = 5; |
- const SkColor kFullColor = SkColorSetARGB(125, 0, 0, 0); |
- const SkColor kEmptyColor = SkColorSetARGB(50, 0, 0, 0); |
- const SkColor kButtonColor = SK_ColorBLACK; |
- |
gfx::Rect content = GetContentsBounds(); |
float value = move_animation_.get() && move_animation_->is_animating() ? |
animating_value_ : value_; |
- |
- int button_cx = 0, button_cy = 0; |
if (orientation_ == HORIZONTAL) { |
- int w = content.width() - kButtonRadius * 2; |
+ // Paint slider bar with image resources. |
+ |
+ // Inset the slider bar a little bit, so that the left or the right end of |
+ // the slider bar will not be exposed under the thumb button when the thumb |
+ // button slides to the left most or right most position. |
+ const int kBarInsetX = 2; |
+ int bar_width = content.width() - kBarInsetX * 2; |
+ int bar_cy = content.height() / 2 - bar_height_ / 2; |
+ |
+ int w = content.width() - thumb_->width(); |
int full = value * w; |
- int empty = w - full; |
- int y = content.height() / 2 - kLineThickness / 2; |
- canvas->FillRect(gfx::Rect(content.x() + kButtonRadius, y, |
- full, kLineThickness), |
- kFullColor); |
- canvas->FillRect(gfx::Rect(content.x() + full + 2 * kButtonRadius, y, |
- std::max(0, empty - kButtonRadius), kLineThickness), |
- kEmptyColor); |
+ int middle = std::max(full, images_[LEFT]->width()); |
- button_cx = content.x() + full + kButtonRadius; |
- button_cy = y + kLineThickness / 2; |
+ canvas->Save(); |
+ canvas->Translate(gfx::Point(kBarInsetX, bar_cy)); |
+ canvas->DrawImageInt(*images_[LEFT], 0, 0); |
+ canvas->DrawImageInt(*images_[RIGHT], |
+ bar_width - images_[RIGHT]->width(), |
+ 0); |
+ canvas->TileImageInt(*images_[CENTER_LEFT], |
+ images_[LEFT]->width(), |
+ 0, |
+ middle - images_[LEFT]->width(), |
+ bar_height_); |
+ canvas->TileImageInt(*images_[CENTER_RIGHT], |
+ middle, |
+ 0, |
+ bar_width - middle - images_[RIGHT]->width(), |
+ bar_height_); |
+ canvas->Restore(); |
+ |
+ // Paint slider thumb. |
+ int button_cx = content.x() + full; |
+ int thumb_y = content.height() / 2 - thumb_->height() / 2; |
+ canvas->DrawImageInt(*thumb_, button_cx, thumb_y); |
} else { |
+ // TODO(jennyz): draw vertical slider bar with resources. |
+ // TODO(sad): The painting code should use NativeTheme for various |
+ // platforms. |
+ const int kButtonRadius = 5; |
+ const int kLineThickness = 5; |
+ const SkColor kFullColor = SkColorSetARGB(125, 0, 0, 0); |
+ const SkColor kEmptyColor = SkColorSetARGB(50, 0, 0, 0); |
+ const SkColor kButtonColor = SK_ColorBLACK; |
+ |
+ int button_cx = 0, button_cy = 0; |
int h = content.height() - kButtonRadius * 2; |
int full = value * h; |
int empty = h - full; |
@@ -147,14 +212,14 @@ void Slider::OnPaint(gfx::Canvas* canvas) { |
button_cx = x + kLineThickness / 2; |
button_cy = content.y() + empty + kButtonRadius; |
- } |
- SkPaint paint; |
- paint.setStyle(SkPaint::kFill_Style); |
- paint.setAntiAlias(true); |
- paint.setColor(kButtonColor); |
- canvas->sk_canvas()->drawCircle(button_cx, button_cy, kButtonRadius, paint); |
- View::OnPaint(canvas); |
+ SkPaint paint; |
+ paint.setStyle(SkPaint::kFill_Style); |
+ paint.setAntiAlias(true); |
+ paint.setColor(kButtonColor); |
+ canvas->sk_canvas()->drawCircle(button_cx, button_cy, kButtonRadius, paint); |
+ View::OnPaint(canvas); |
+ } |
} |
bool Slider::OnMousePressed(const views::MouseEvent& event) { |