- add sources.
[platform/framework/web/crosswalk.git] / src / ui / views / controls / button / custom_button.h
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_BUTTON_CUSTOM_BUTTON_H_
6 #define UI_VIEWS_CONTROLS_BUTTON_CUSTOM_BUTTON_H_
7
8 #include "base/memory/scoped_ptr.h"
9 #include "ui/events/event_constants.h"
10 #include "ui/gfx/animation/animation_delegate.h"
11 #include "ui/views/controls/button/button.h"
12
13 namespace gfx {
14 class ThrobAnimation;
15 }
16
17 namespace views {
18
19 class CustomButtonStateChangedDelegate;
20
21 // A button with custom rendering. The common base class of ImageButton and
22 // TextButton.
23 // Note that this type of button is not focusable by default and will not be
24 // part of the focus chain.  Call set_focusable(true) to make it part of the
25 // focus chain.
26 class VIEWS_EXPORT CustomButton : public Button,
27                                   public gfx::AnimationDelegate {
28  public:
29   // The menu button's class name.
30   static const char kViewClassName[];
31
32   virtual ~CustomButton();
33
34   // Get/sets the current display state of the button.
35   ButtonState state() const { return state_; }
36   void SetState(ButtonState state);
37
38   // Starts throbbing. See HoverAnimation for a description of cycles_til_stop.
39   void StartThrobbing(int cycles_til_stop);
40
41   // Stops throbbing immediately.
42   void StopThrobbing();
43
44   // Set how long the hover animation will last for.
45   void SetAnimationDuration(int duration);
46
47   void set_triggerable_event_flags(int triggerable_event_flags) {
48     triggerable_event_flags_ = triggerable_event_flags;
49   }
50   int triggerable_event_flags() const { return triggerable_event_flags_; }
51
52   // Sets whether |RequestFocus| should be invoked on a mouse press. The default
53   // is true.
54   void set_request_focus_on_press(bool value) {
55     request_focus_on_press_ = value;
56   }
57   bool request_focus_on_press() const { return request_focus_on_press_; }
58
59   // See description above field.
60   void set_animate_on_state_change(bool value) {
61     animate_on_state_change_ = value;
62   }
63
64   void SetHotTracked(bool is_hot_tracked);
65   bool IsHotTracked() const;
66
67   // Overridden from View:
68   virtual void OnEnabledChanged() OVERRIDE;
69   virtual const char* GetClassName() const OVERRIDE;
70   virtual bool OnMousePressed(const ui::MouseEvent& event) OVERRIDE;
71   virtual bool OnMouseDragged(const ui::MouseEvent& event) OVERRIDE;
72   virtual void OnMouseReleased(const ui::MouseEvent& event) OVERRIDE;
73   virtual void OnMouseCaptureLost() OVERRIDE;
74   virtual void OnMouseEntered(const ui::MouseEvent& event) OVERRIDE;
75   virtual void OnMouseExited(const ui::MouseEvent& event) OVERRIDE;
76   virtual void OnMouseMoved(const ui::MouseEvent& event) OVERRIDE;
77   virtual bool OnKeyPressed(const ui::KeyEvent& event) OVERRIDE;
78   virtual bool OnKeyReleased(const ui::KeyEvent& event) OVERRIDE;
79   virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE;
80   virtual bool AcceleratorPressed(const ui::Accelerator& accelerator) OVERRIDE;
81   virtual void ShowContextMenu(const gfx::Point& p,
82                                ui::MenuSourceType source_type) OVERRIDE;
83   virtual void OnDragDone() OVERRIDE;
84   virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE;
85   virtual void VisibilityChanged(View* starting_from, bool is_visible) OVERRIDE;
86
87   // Overridden from gfx::AnimationDelegate:
88   virtual void AnimationProgressed(const gfx::Animation* animation) OVERRIDE;
89
90   // Takes ownership of the delegate.
91   void set_state_changed_delegate(CustomButtonStateChangedDelegate* delegate) {
92     state_changed_delegate_.reset(delegate);
93   }
94
95  protected:
96   // Construct the Button with a Listener. See comment for Button's ctor.
97   explicit CustomButton(ButtonListener* listener);
98
99   // Invoked from SetState() when SetState() is passed a value that differs from
100   // the current state. CustomButton's implementation of StateChanged() does
101   // nothing; this method is provided for subclasses that wish to do something
102   // on state changes.
103   virtual void StateChanged();
104
105   // Returns true if the event is one that can trigger notifying the listener.
106   // This implementation returns true if the left mouse button is down.
107   virtual bool IsTriggerableEvent(const ui::Event& event);
108
109   // Returns true if the button should become pressed when the user
110   // holds the mouse down over the button. For this implementation,
111   // we simply return IsTriggerableEvent(event).
112   virtual bool ShouldEnterPushedState(const ui::Event& event);
113
114   // Overridden from View:
115   virtual void ViewHierarchyChanged(
116       const ViewHierarchyChangedDetails& details) OVERRIDE;
117   virtual void OnBlur() OVERRIDE;
118
119   // The button state (defined in implementation)
120   ButtonState state_;
121
122   // Hover animation.
123   scoped_ptr<gfx::ThrobAnimation> hover_animation_;
124
125  private:
126   // Should we animate when the state changes? Defaults to true.
127   bool animate_on_state_change_;
128
129   // Is the hover animation running because StartThrob was invoked?
130   bool is_throbbing_;
131
132   // Mouse event flags which can trigger button actions.
133   int triggerable_event_flags_;
134
135   // See description above setter.
136   bool request_focus_on_press_;
137
138   scoped_ptr<CustomButtonStateChangedDelegate> state_changed_delegate_;
139
140   DISALLOW_COPY_AND_ASSIGN(CustomButton);
141 };
142
143 // Delegate for actions taken on state changes by CustomButton.
144 class VIEWS_EXPORT CustomButtonStateChangedDelegate {
145 public:
146   virtual ~CustomButtonStateChangedDelegate() {}
147   virtual void StateChanged(Button::ButtonState state) = 0;
148
149 protected:
150   CustomButtonStateChangedDelegate() {}
151
152 private:
153   DISALLOW_COPY_AND_ASSIGN(CustomButtonStateChangedDelegate);
154 };
155
156 }  // namespace views
157
158 #endif  // UI_VIEWS_CONTROLS_BUTTON_CUSTOM_BUTTON_H_