• 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 namespace test {
22 class SliderTestApi;
23 }
24 
25 class Slider;
26 
27 enum SliderChangeReason {
28   VALUE_CHANGED_BY_USER,  // value was changed by the user (by clicking, e.g.)
29   VALUE_CHANGED_BY_API,   // value was changed by a call to SetValue.
30 };
31 
32 class VIEWS_EXPORT SliderListener {
33  public:
34   virtual void SliderValueChanged(Slider* sender,
35                                   float value,
36                                   float old_value,
37                                   SliderChangeReason reason) = 0;
38 
39   // Invoked when a drag starts or ends (more specifically, when the mouse
40   // button is pressed or released).
SliderDragStarted(Slider * sender)41   virtual void SliderDragStarted(Slider* sender) {}
SliderDragEnded(Slider * sender)42   virtual void SliderDragEnded(Slider* sender) {}
43 
44  protected:
~SliderListener()45   virtual ~SliderListener() {}
46 };
47 
48 class VIEWS_EXPORT Slider : public View, public gfx::AnimationDelegate {
49  public:
50   enum Orientation {
51     HORIZONTAL,
52     VERTICAL
53   };
54 
55   Slider(SliderListener* listener, Orientation orientation);
56   virtual ~Slider();
57 
value()58   float value() const { return value_; }
59   void SetValue(float value);
60 
61   // Set the delta used for changing the value via keyboard.
62   void SetKeyboardIncrement(float increment);
63 
64   void SetAccessibleName(const base::string16& name);
65 
set_enable_accessibility_events(bool enabled)66   void set_enable_accessibility_events(bool enabled) {
67     accessibility_events_enabled_ = enabled;
68   }
69 
set_focus_border_color(SkColor color)70   void set_focus_border_color(SkColor color) { focus_border_color_ = color; }
71 
72   // Update UI based on control on/off state.
73   void UpdateState(bool control_on);
74 
75  private:
76   friend class test::SliderTestApi;
77 
78   void SetValueInternal(float value, SliderChangeReason reason);
79 
80   // Should be called on the Mouse Down event. Used to calculate relative
81   // position of the mouse cursor (or the touch point) on the button to
82   // accurately move the button using the MoveButtonTo() method.
83   void PrepareForMove(const gfx::Point& point);
84 
85   // Moves the button to the specified point and updates the value accordingly.
86   void MoveButtonTo(const gfx::Point& point);
87 
88   void OnPaintFocus(gfx::Canvas* canvas);
89 
90   // Notify the listener_, if not NULL, that dragging started.
91   void OnSliderDragStarted();
92 
93   // Notify the listener_, if not NULL, that dragging ended.
94   void OnSliderDragEnded();
95 
96   // views::View overrides:
97   virtual gfx::Size GetPreferredSize() const OVERRIDE;
98   virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
99   virtual bool OnMousePressed(const ui::MouseEvent& event) OVERRIDE;
100   virtual bool OnMouseDragged(const ui::MouseEvent& event) OVERRIDE;
101   virtual void OnMouseReleased(const ui::MouseEvent& event) OVERRIDE;
102   virtual bool OnKeyPressed(const ui::KeyEvent& event) OVERRIDE;
103   virtual void GetAccessibleState(ui::AXViewState* state) OVERRIDE;
104   virtual void OnFocus() OVERRIDE;
105   virtual void OnBlur() OVERRIDE;
106 
107   // ui::EventHandler overrides:
108   virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE;
109 
110   // gfx::AnimationDelegate overrides:
111   virtual void AnimationProgressed(const gfx::Animation* animation) OVERRIDE;
112 
set_listener(SliderListener * listener)113   void set_listener(SliderListener* listener) {
114     listener_ = listener;
115   }
116 
117   SliderListener* listener_;
118   Orientation orientation_;
119 
120   scoped_ptr<gfx::SlideAnimation> move_animation_;
121 
122   float value_;
123   float keyboard_increment_;
124   float animating_value_;
125   bool value_is_valid_;
126   base::string16 accessible_name_;
127   bool accessibility_events_enabled_;
128   SkColor focus_border_color_;
129 
130   // Relative position of the mouse cursor (or the touch point) on the slider's
131   // button.
132   gfx::Point initial_button_offset_;
133 
134   const int* bar_active_images_;
135   const int* bar_disabled_images_;
136   const gfx::ImageSkia* thumb_;
137   const gfx::ImageSkia* images_[4];
138   int bar_height_;
139 
140   DISALLOW_COPY_AND_ASSIGN(Slider);
141 };
142 
143 }  // namespace views
144 
145 #endif  // UI_VIEWS_CONTROLS_SLIDER_H_
146