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 // A button with custom rendering. The base of ImageButton and LabelButton.
20 // Note that this type of button is not focusable by default and will not be
21 // part of the focus chain. Call SetFocusable(true) to make it part of the
23 class VIEWS_EXPORT CustomButton
: public Button
,
24 public gfx::AnimationDelegate
{
26 // An enum describing the events on which a button should notify its listener.
32 // The menu button's class name.
33 static const char kViewClassName
[];
35 static const CustomButton
* AsCustomButton(const views::View
* view
);
36 static CustomButton
* AsCustomButton(views::View
* view
);
38 ~CustomButton() override
;
40 // Get/sets the current display state of the button.
41 ButtonState
state() const { return state_
; }
42 void SetState(ButtonState state
);
44 // Starts throbbing. See HoverAnimation for a description of cycles_til_stop.
45 void StartThrobbing(int cycles_til_stop
);
47 // Stops throbbing immediately.
50 // Set how long the hover animation will last for.
51 void SetAnimationDuration(int duration
);
53 void set_triggerable_event_flags(int triggerable_event_flags
) {
54 triggerable_event_flags_
= triggerable_event_flags
;
56 int triggerable_event_flags() const { return triggerable_event_flags_
; }
58 // Sets whether |RequestFocus| should be invoked on a mouse press. The default
60 void set_request_focus_on_press(bool value
) {
61 request_focus_on_press_
= value
;
63 bool request_focus_on_press() const { return request_focus_on_press_
; }
65 // See description above field.
66 void set_animate_on_state_change(bool value
) {
67 animate_on_state_change_
= value
;
70 // Sets the event on which the button should notify its listener.
71 void set_notify_action(NotifyAction notify_action
) {
72 notify_action_
= notify_action
;
75 void SetHotTracked(bool is_hot_tracked
);
76 bool IsHotTracked() const;
78 // Overridden from View:
79 void OnEnabledChanged() override
;
80 const char* GetClassName() const override
;
81 bool OnMousePressed(const ui::MouseEvent
& event
) override
;
82 bool OnMouseDragged(const ui::MouseEvent
& event
) override
;
83 void OnMouseReleased(const ui::MouseEvent
& event
) override
;
84 void OnMouseCaptureLost() override
;
85 void OnMouseEntered(const ui::MouseEvent
& event
) override
;
86 void OnMouseExited(const ui::MouseEvent
& event
) override
;
87 void OnMouseMoved(const ui::MouseEvent
& event
) override
;
88 bool OnKeyPressed(const ui::KeyEvent
& event
) override
;
89 bool OnKeyReleased(const ui::KeyEvent
& event
) override
;
90 void OnGestureEvent(ui::GestureEvent
* event
) override
;
91 bool AcceleratorPressed(const ui::Accelerator
& accelerator
) override
;
92 void ShowContextMenu(const gfx::Point
& p
,
93 ui::MenuSourceType source_type
) override
;
94 void OnDragDone() override
;
95 void GetAccessibleState(ui::AXViewState
* state
) override
;
96 void VisibilityChanged(View
* starting_from
, bool is_visible
) override
;
98 // Overridden from gfx::AnimationDelegate:
99 void AnimationProgressed(const gfx::Animation
* animation
) override
;
102 // Construct the Button with a Listener. See comment for Button's ctor.
103 explicit CustomButton(ButtonListener
* listener
);
105 // Invoked from SetState() when SetState() is passed a value that differs from
106 // the current state. CustomButton's implementation of StateChanged() does
107 // nothing; this method is provided for subclasses that wish to do something
109 virtual void StateChanged();
111 // Returns true if the event is one that can trigger notifying the listener.
112 // This implementation returns true if the left mouse button is down.
113 virtual bool IsTriggerableEvent(const ui::Event
& event
);
115 // Returns true if the button should become pressed when the user
116 // holds the mouse down over the button. For this implementation,
117 // we simply return IsTriggerableEvent(event).
118 virtual bool ShouldEnterPushedState(const ui::Event
& event
);
120 // Overridden from View:
121 void ViewHierarchyChanged(
122 const ViewHierarchyChangedDetails
& details
) override
;
123 void OnBlur() override
;
125 // The button state (defined in implementation)
129 scoped_ptr
<gfx::ThrobAnimation
> hover_animation_
;
132 // Should we animate when the state changes? Defaults to true.
133 bool animate_on_state_change_
;
135 // Is the hover animation running because StartThrob was invoked?
138 // Mouse event flags which can trigger button actions.
139 int triggerable_event_flags_
;
141 // See description above setter.
142 bool request_focus_on_press_
;
144 // The event on which the button should notify its listener.
145 NotifyAction notify_action_
;
147 DISALLOW_COPY_AND_ASSIGN(CustomButton
);
152 #endif // UI_VIEWS_CONTROLS_BUTTON_CUSTOM_BUTTON_H_