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 base of ImageButton and LabelButton.
22 // Note that this type of button is not focusable by default and will not be
23 // part of the focus chain. Call SetFocusable(true) to make it part of the
25 class VIEWS_EXPORT CustomButton
: public Button
,
26 public gfx::AnimationDelegate
{
28 // The menu button's class name.
29 static const char kViewClassName
[];
31 static const CustomButton
* AsCustomButton(const views::View
* view
);
32 static CustomButton
* AsCustomButton(views::View
* view
);
34 virtual ~CustomButton();
36 // Get/sets the current display state of the button.
37 ButtonState
state() const { return state_
; }
38 void SetState(ButtonState state
);
40 // Starts throbbing. See HoverAnimation for a description of cycles_til_stop.
41 void StartThrobbing(int cycles_til_stop
);
43 // Stops throbbing immediately.
46 // Set how long the hover animation will last for.
47 void SetAnimationDuration(int duration
);
49 void set_triggerable_event_flags(int triggerable_event_flags
) {
50 triggerable_event_flags_
= triggerable_event_flags
;
52 int triggerable_event_flags() const { return triggerable_event_flags_
; }
54 // Sets whether |RequestFocus| should be invoked on a mouse press. The default
56 void set_request_focus_on_press(bool value
) {
57 request_focus_on_press_
= value
;
59 bool request_focus_on_press() const { return request_focus_on_press_
; }
61 // See description above field.
62 void set_animate_on_state_change(bool value
) {
63 animate_on_state_change_
= value
;
66 void SetHotTracked(bool is_hot_tracked
);
67 bool IsHotTracked() const;
69 // Overridden from View:
70 virtual void OnEnabledChanged() OVERRIDE
;
71 virtual const char* GetClassName() const OVERRIDE
;
72 virtual bool OnMousePressed(const ui::MouseEvent
& event
) OVERRIDE
;
73 virtual bool OnMouseDragged(const ui::MouseEvent
& event
) OVERRIDE
;
74 virtual void OnMouseReleased(const ui::MouseEvent
& event
) OVERRIDE
;
75 virtual void OnMouseCaptureLost() OVERRIDE
;
76 virtual void OnMouseEntered(const ui::MouseEvent
& event
) OVERRIDE
;
77 virtual void OnMouseExited(const ui::MouseEvent
& event
) OVERRIDE
;
78 virtual void OnMouseMoved(const ui::MouseEvent
& event
) OVERRIDE
;
79 virtual bool OnKeyPressed(const ui::KeyEvent
& event
) OVERRIDE
;
80 virtual bool OnKeyReleased(const ui::KeyEvent
& event
) OVERRIDE
;
81 virtual void OnGestureEvent(ui::GestureEvent
* event
) OVERRIDE
;
82 virtual bool AcceleratorPressed(const ui::Accelerator
& accelerator
) OVERRIDE
;
83 virtual void ShowContextMenu(const gfx::Point
& p
,
84 ui::MenuSourceType source_type
) OVERRIDE
;
85 virtual void OnDragDone() OVERRIDE
;
86 virtual void GetAccessibleState(ui::AXViewState
* state
) OVERRIDE
;
87 virtual void VisibilityChanged(View
* starting_from
, bool is_visible
) OVERRIDE
;
89 // Overridden from gfx::AnimationDelegate:
90 virtual void AnimationProgressed(const gfx::Animation
* animation
) OVERRIDE
;
92 // Takes ownership of the delegate.
93 void set_state_changed_delegate(CustomButtonStateChangedDelegate
* delegate
) {
94 state_changed_delegate_
.reset(delegate
);
98 // Construct the Button with a Listener. See comment for Button's ctor.
99 explicit CustomButton(ButtonListener
* listener
);
101 // Invoked from SetState() when SetState() is passed a value that differs from
102 // the current state. CustomButton's implementation of StateChanged() does
103 // nothing; this method is provided for subclasses that wish to do something
105 virtual void StateChanged();
107 // Returns true if the event is one that can trigger notifying the listener.
108 // This implementation returns true if the left mouse button is down.
109 virtual bool IsTriggerableEvent(const ui::Event
& event
);
111 // Returns true if the button should become pressed when the user
112 // holds the mouse down over the button. For this implementation,
113 // we simply return IsTriggerableEvent(event).
114 virtual bool ShouldEnterPushedState(const ui::Event
& event
);
116 // Overridden from View:
117 virtual void ViewHierarchyChanged(
118 const ViewHierarchyChangedDetails
& details
) OVERRIDE
;
119 virtual void OnBlur() OVERRIDE
;
121 // The button state (defined in implementation)
125 scoped_ptr
<gfx::ThrobAnimation
> hover_animation_
;
128 // Should we animate when the state changes? Defaults to true.
129 bool animate_on_state_change_
;
131 // Is the hover animation running because StartThrob was invoked?
134 // Mouse event flags which can trigger button actions.
135 int triggerable_event_flags_
;
137 // See description above setter.
138 bool request_focus_on_press_
;
140 scoped_ptr
<CustomButtonStateChangedDelegate
> state_changed_delegate_
;
142 DISALLOW_COPY_AND_ASSIGN(CustomButton
);
145 // Delegate for actions taken on state changes by CustomButton.
146 class VIEWS_EXPORT CustomButtonStateChangedDelegate
{
148 virtual ~CustomButtonStateChangedDelegate() {}
149 virtual void StateChanged(Button::ButtonState state
) = 0;
152 CustomButtonStateChangedDelegate() {}
155 DISALLOW_COPY_AND_ASSIGN(CustomButtonStateChangedDelegate
);
160 #endif // UI_VIEWS_CONTROLS_BUTTON_CUSTOM_BUTTON_H_