• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
3 // found in the LICENSE file.
4 
5 #ifndef UI_VIEWS_CONTROLS_SLIDER_H_
6 #define UI_VIEWS_CONTROLS_SLIDER_H_
7 
8 #include "ui/gfx/animation/animation_delegate.h"
9 #include "ui/views/view.h"
10 #include "ui/views/views_export.h"
11 
12 typedef unsigned int SkColor;
13 
14 namespace gfx {
15 class ImageSkia;
16 class SlideAnimation;
17 }
18 
19 namespace views {
20 
21 class Slider;
22 
23 enum SliderChangeReason {
24   VALUE_CHANGED_BY_USER,  // value was changed by the user (by clicking, e.g.)
25   VALUE_CHANGED_BY_API,   // value was changed by a call to SetValue.
26 };
27 
28 class VIEWS_EXPORT SliderListener {
29  public:
30   virtual void SliderValueChanged(Slider* sender,
31                                   float value,
32                                   float old_value,
33                                   SliderChangeReason reason) = 0;
34 
35   // Invoked when a drag starts or ends (more specifically, when the mouse
36   // button is pressed or released).
SliderDragStarted(Slider * sender)37   virtual void SliderDragStarted(Slider* sender) {}
SliderDragEnded(Slider * sender)38   virtual void SliderDragEnded(Slider* sender) {}
39 
40  protected:
~SliderListener()41   virtual ~SliderListener() {}
42 };
43 
44 class VIEWS_EXPORT Slider : public View, public gfx::AnimationDelegate {
45  public:
46   enum Orientation {
47     HORIZONTAL,
48     VERTICAL
49   };
50 
51   Slider(SliderListener* listener, Orientation orientation);
52   virtual ~Slider();
53 
value()54   float value() const { return value_; }
55   void SetValue(float value);
56 
57   // Set the delta used for changing the value via keyboard.
58   void SetKeyboardIncrement(float increment);
59 
60   void SetAccessibleName(const string16& name);
61 
set_enable_accessibility_events(bool enabled)62   void set_enable_accessibility_events(bool enabled) {
63     accessibility_events_enabled_ = enabled;
64   }
65 
set_focus_border_color(SkColor color)66   void set_focus_border_color(SkColor color) { focus_border_color_ = color; }
67 
68   // Update UI based on control on/off state.
69   void UpdateState(bool control_on);
70 
71  private:
72   void SetValueInternal(float value, SliderChangeReason reason);
73 
74   // Should be called on the Mouse Down event. Used to calculate relative
75   // position of the mouse cursor (or the touch point) on the button to
76   // accurately move the button using the MoveButtonTo() method.
77   void PrepareForMove(const gfx::Point& point);
78 
79   // Moves the button to the specified point and updates the value accordingly.
80   void MoveButtonTo(const gfx::Point& point);
81 
82   void OnPaintFocus(gfx::Canvas* canvas);
83 
84   // views::View overrides:
85   virtual gfx::Size GetPreferredSize() OVERRIDE;
86   virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
87   virtual bool OnMousePressed(const ui::MouseEvent& event) OVERRIDE;
88   virtual bool OnMouseDragged(const ui::MouseEvent& event) OVERRIDE;
89   virtual void OnMouseReleased(const ui::MouseEvent& event) OVERRIDE;
90   virtual bool OnKeyPressed(const ui::KeyEvent& event) OVERRIDE;
91   virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE;
92 
93   // ui::EventHandler overrides:
94   virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE;
95 
96   // gfx::AnimationDelegate overrides:
97   virtual void AnimationProgressed(const gfx::Animation* animation) OVERRIDE;
98 
99   SliderListener* listener_;
100   Orientation orientation_;
101 
102   scoped_ptr<gfx::SlideAnimation> move_animation_;
103 
104   float value_;
105   float keyboard_increment_;
106   float animating_value_;
107   bool value_is_valid_;
108   string16 accessible_name_;
109   bool accessibility_events_enabled_;
110   SkColor focus_border_color_;
111 
112   // Relative position of the mouse cursor (or the touch point) on the slider's
113   // button.
114   gfx::Point initial_button_offset_;
115 
116   const int* bar_active_images_;
117   const int* bar_disabled_images_;
118   const gfx::ImageSkia* thumb_;
119   const gfx::ImageSkia* images_[4];
120   int bar_height_;
121 
122   DISALLOW_COPY_AND_ASSIGN(Slider);
123 };
124 
125 }  // namespace views
126 
127 #endif  // UI_VIEWS_CONTROLS_SLIDER_H_
128