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.
5 #ifndef UI_VIEWS_CONTROLS_SLIDER_H_
6 #define UI_VIEWS_CONTROLS_SLIDER_H_
8 #include "ui/gfx/animation/animation_delegate.h"
9 #include "ui/views/view.h"
10 #include "ui/views/views_export.h"
12 typedef unsigned int SkColor
;
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.
32 class VIEWS_EXPORT SliderListener
{
34 virtual void SliderValueChanged(Slider
* sender
,
37 SliderChangeReason reason
) = 0;
39 // Invoked when a drag starts or ends (more specifically, when the mouse
40 // button is pressed or released).
41 virtual void SliderDragStarted(Slider
* sender
) {}
42 virtual void SliderDragEnded(Slider
* sender
) {}
45 virtual ~SliderListener() {}
48 class VIEWS_EXPORT Slider
: public View
, public gfx::AnimationDelegate
{
50 // Internal class name.
51 static const char kViewClassName
[];
58 Slider(SliderListener
* listener
, Orientation orientation
);
61 float value() const { return value_
; }
62 void SetValue(float value
);
64 // Set the delta used for changing the value via keyboard.
65 void SetKeyboardIncrement(float increment
);
67 void SetAccessibleName(const base::string16
& name
);
69 void set_enable_accessibility_events(bool enabled
) {
70 accessibility_events_enabled_
= enabled
;
73 void set_focus_border_color(SkColor color
) { focus_border_color_
= color
; }
75 // Update UI based on control on/off state.
76 void UpdateState(bool control_on
);
79 friend class test::SliderTestApi
;
81 void SetValueInternal(float value
, SliderChangeReason reason
);
83 // Should be called on the Mouse Down event. Used to calculate relative
84 // position of the mouse cursor (or the touch point) on the button to
85 // accurately move the button using the MoveButtonTo() method.
86 void PrepareForMove(const gfx::Point
& point
);
88 // Moves the button to the specified point and updates the value accordingly.
89 void MoveButtonTo(const gfx::Point
& point
);
91 void OnPaintFocus(gfx::Canvas
* canvas
);
93 // Notify the listener_, if not NULL, that dragging started.
94 void OnSliderDragStarted();
96 // Notify the listener_, if not NULL, that dragging ended.
97 void OnSliderDragEnded();
99 // views::View overrides:
100 const char* GetClassName() const override
;
101 gfx::Size
GetPreferredSize() const override
;
102 void OnPaint(gfx::Canvas
* canvas
) override
;
103 bool OnMousePressed(const ui::MouseEvent
& event
) override
;
104 bool OnMouseDragged(const ui::MouseEvent
& event
) override
;
105 void OnMouseReleased(const ui::MouseEvent
& event
) override
;
106 bool OnKeyPressed(const ui::KeyEvent
& event
) override
;
107 void GetAccessibleState(ui::AXViewState
* state
) override
;
108 void OnFocus() override
;
109 void OnBlur() override
;
111 // ui::EventHandler overrides:
112 void OnGestureEvent(ui::GestureEvent
* event
) override
;
114 // gfx::AnimationDelegate overrides:
115 void AnimationProgressed(const gfx::Animation
* animation
) override
;
117 void set_listener(SliderListener
* listener
) {
118 listener_
= listener
;
121 SliderListener
* listener_
;
122 Orientation orientation_
;
124 scoped_ptr
<gfx::SlideAnimation
> move_animation_
;
127 float keyboard_increment_
;
128 float animating_value_
;
129 bool value_is_valid_
;
130 base::string16 accessible_name_
;
131 bool accessibility_events_enabled_
;
132 SkColor focus_border_color_
;
134 // Relative position of the mouse cursor (or the touch point) on the slider's
136 gfx::Point initial_button_offset_
;
138 const int* bar_active_images_
;
139 const int* bar_disabled_images_
;
140 const gfx::ImageSkia
* thumb_
;
141 const gfx::ImageSkia
* images_
[4];
144 DISALLOW_COPY_AND_ASSIGN(Slider
);
149 #endif // UI_VIEWS_CONTROLS_SLIDER_H_