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 // The menu button's class name.
27 static const char kViewClassName
[];
29 static const CustomButton
* AsCustomButton(const views::View
* view
);
30 static CustomButton
* AsCustomButton(views::View
* view
);
32 ~CustomButton() override
;
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 void OnEnabledChanged() override
;
69 const char* GetClassName() const override
;
70 bool OnMousePressed(const ui::MouseEvent
& event
) override
;
71 bool OnMouseDragged(const ui::MouseEvent
& event
) override
;
72 void OnMouseReleased(const ui::MouseEvent
& event
) override
;
73 void OnMouseCaptureLost() override
;
74 void OnMouseEntered(const ui::MouseEvent
& event
) override
;
75 void OnMouseExited(const ui::MouseEvent
& event
) override
;
76 void OnMouseMoved(const ui::MouseEvent
& event
) override
;
77 bool OnKeyPressed(const ui::KeyEvent
& event
) override
;
78 bool OnKeyReleased(const ui::KeyEvent
& event
) override
;
79 void OnGestureEvent(ui::GestureEvent
* event
) override
;
80 bool AcceleratorPressed(const ui::Accelerator
& accelerator
) override
;
81 void ShowContextMenu(const gfx::Point
& p
,
82 ui::MenuSourceType source_type
) override
;
83 void OnDragDone() override
;
84 void GetAccessibleState(ui::AXViewState
* state
) override
;
85 void VisibilityChanged(View
* starting_from
, bool is_visible
) override
;
87 // Overridden from gfx::AnimationDelegate:
88 void AnimationProgressed(const gfx::Animation
* animation
) override
;
91 // Construct the Button with a Listener. See comment for Button's ctor.
92 explicit CustomButton(ButtonListener
* listener
);
94 // Invoked from SetState() when SetState() is passed a value that differs from
95 // the current state. CustomButton's implementation of StateChanged() does
96 // nothing; this method is provided for subclasses that wish to do something
98 virtual void StateChanged();
100 // Returns true if the event is one that can trigger notifying the listener.
101 // This implementation returns true if the left mouse button is down.
102 virtual bool IsTriggerableEvent(const ui::Event
& event
);
104 // Returns true if the button should become pressed when the user
105 // holds the mouse down over the button. For this implementation,
106 // we simply return IsTriggerableEvent(event).
107 virtual bool ShouldEnterPushedState(const ui::Event
& event
);
109 // Overridden from View:
110 void ViewHierarchyChanged(
111 const ViewHierarchyChangedDetails
& details
) override
;
112 void OnBlur() override
;
114 // The button state (defined in implementation)
118 scoped_ptr
<gfx::ThrobAnimation
> hover_animation_
;
121 // Should we animate when the state changes? Defaults to true.
122 bool animate_on_state_change_
;
124 // Is the hover animation running because StartThrob was invoked?
127 // Mouse event flags which can trigger button actions.
128 int triggerable_event_flags_
;
130 // See description above setter.
131 bool request_focus_on_press_
;
133 DISALLOW_COPY_AND_ASSIGN(CustomButton
);
138 #endif // UI_VIEWS_CONTROLS_BUTTON_CUSTOM_BUTTON_H_