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_BUTTON_CUSTOM_BUTTON_H_
6 #define UI_VIEWS_CONTROLS_BUTTON_CUSTOM_BUTTON_H_
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"
19 class CustomButtonStateChangedDelegate;
21 // A button with custom rendering. The common base class of ImageButton and
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
26 class VIEWS_EXPORT CustomButton : public Button,
27 public gfx::AnimationDelegate {
29 // The menu button's class name.
30 static const char kViewClassName[];
32 virtual ~CustomButton();
34 // Get/sets the current display state of the button.
35 ButtonState state() const { return state_; }
36 void SetState(ButtonState state);
38 // Starts throbbing. See HoverAnimation for a description of cycles_til_stop.
39 void StartThrobbing(int cycles_til_stop);
41 // Stops throbbing immediately.
44 // Set how long the hover animation will last for.
45 void SetAnimationDuration(int duration);
47 void set_triggerable_event_flags(int triggerable_event_flags) {
48 triggerable_event_flags_ = triggerable_event_flags;
50 int triggerable_event_flags() const { return triggerable_event_flags_; }
52 // Sets whether |RequestFocus| should be invoked on a mouse press. The default
54 void set_request_focus_on_press(bool value) {
55 request_focus_on_press_ = value;
57 bool request_focus_on_press() const { return request_focus_on_press_; }
59 // See description above field.
60 void set_animate_on_state_change(bool value) {
61 animate_on_state_change_ = value;
64 void SetHotTracked(bool is_hot_tracked);
65 bool IsHotTracked() const;
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;
87 // Overridden from gfx::AnimationDelegate:
88 virtual void AnimationProgressed(const gfx::Animation* animation) OVERRIDE;
90 // Takes ownership of the delegate.
91 void set_state_changed_delegate(CustomButtonStateChangedDelegate* delegate) {
92 state_changed_delegate_.reset(delegate);
96 // Construct the Button with a Listener. See comment for Button's ctor.
97 explicit CustomButton(ButtonListener* listener);
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
103 virtual void StateChanged();
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);
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);
114 // Overridden from View:
115 virtual void ViewHierarchyChanged(
116 const ViewHierarchyChangedDetails& details) OVERRIDE;
117 virtual void OnBlur() OVERRIDE;
119 // The button state (defined in implementation)
123 scoped_ptr<gfx::ThrobAnimation> hover_animation_;
126 // Should we animate when the state changes? Defaults to true.
127 bool animate_on_state_change_;
129 // Is the hover animation running because StartThrob was invoked?
132 // Mouse event flags which can trigger button actions.
133 int triggerable_event_flags_;
135 // See description above setter.
136 bool request_focus_on_press_;
138 scoped_ptr<CustomButtonStateChangedDelegate> state_changed_delegate_;
140 DISALLOW_COPY_AND_ASSIGN(CustomButton);
143 // Delegate for actions taken on state changes by CustomButton.
144 class VIEWS_EXPORT CustomButtonStateChangedDelegate {
146 virtual ~CustomButtonStateChangedDelegate() {}
147 virtual void StateChanged(Button::ButtonState state) = 0;
150 CustomButtonStateChangedDelegate() {}
153 DISALLOW_COPY_AND_ASSIGN(CustomButtonStateChangedDelegate);
158 #endif // UI_VIEWS_CONTROLS_BUTTON_CUSTOM_BUTTON_H_