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 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 24 // focus chain. 25 class VIEWS_EXPORT CustomButton : public Button, 26 public gfx::AnimationDelegate { 27 public: 28 // The menu button's class name. 29 static const char kViewClassName[]; 30 31 static const CustomButton* AsCustomButton(const views::View* view); 32 static CustomButton* AsCustomButton(views::View* view); 33 34 virtual ~CustomButton(); 35 36 // Get/sets the current display state of the button. state()37 ButtonState state() const { return state_; } 38 void SetState(ButtonState state); 39 40 // Starts throbbing. See HoverAnimation for a description of cycles_til_stop. 41 void StartThrobbing(int cycles_til_stop); 42 43 // Stops throbbing immediately. 44 void StopThrobbing(); 45 46 // Set how long the hover animation will last for. 47 void SetAnimationDuration(int duration); 48 set_triggerable_event_flags(int triggerable_event_flags)49 void set_triggerable_event_flags(int triggerable_event_flags) { 50 triggerable_event_flags_ = triggerable_event_flags; 51 } triggerable_event_flags()52 int triggerable_event_flags() const { return triggerable_event_flags_; } 53 54 // Sets whether |RequestFocus| should be invoked on a mouse press. The default 55 // is true. set_request_focus_on_press(bool value)56 void set_request_focus_on_press(bool value) { 57 request_focus_on_press_ = value; 58 } request_focus_on_press()59 bool request_focus_on_press() const { return request_focus_on_press_; } 60 61 // See description above field. set_animate_on_state_change(bool value)62 void set_animate_on_state_change(bool value) { 63 animate_on_state_change_ = value; 64 } 65 66 void SetHotTracked(bool is_hot_tracked); 67 bool IsHotTracked() const; 68 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; 88 89 // Overridden from gfx::AnimationDelegate: 90 virtual void AnimationProgressed(const gfx::Animation* animation) OVERRIDE; 91 92 // Takes ownership of the delegate. set_state_changed_delegate(CustomButtonStateChangedDelegate * delegate)93 void set_state_changed_delegate(CustomButtonStateChangedDelegate* delegate) { 94 state_changed_delegate_.reset(delegate); 95 } 96 97 protected: 98 // Construct the Button with a Listener. See comment for Button's ctor. 99 explicit CustomButton(ButtonListener* listener); 100 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 104 // on state changes. 105 virtual void StateChanged(); 106 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); 110 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); 115 116 // Overridden from View: 117 virtual void ViewHierarchyChanged( 118 const ViewHierarchyChangedDetails& details) OVERRIDE; 119 virtual void OnBlur() OVERRIDE; 120 121 // The button state (defined in implementation) 122 ButtonState state_; 123 124 // Hover animation. 125 scoped_ptr<gfx::ThrobAnimation> hover_animation_; 126 127 private: 128 // Should we animate when the state changes? Defaults to true. 129 bool animate_on_state_change_; 130 131 // Is the hover animation running because StartThrob was invoked? 132 bool is_throbbing_; 133 134 // Mouse event flags which can trigger button actions. 135 int triggerable_event_flags_; 136 137 // See description above setter. 138 bool request_focus_on_press_; 139 140 scoped_ptr<CustomButtonStateChangedDelegate> state_changed_delegate_; 141 142 DISALLOW_COPY_AND_ASSIGN(CustomButton); 143 }; 144 145 // Delegate for actions taken on state changes by CustomButton. 146 class VIEWS_EXPORT CustomButtonStateChangedDelegate { 147 public: ~CustomButtonStateChangedDelegate()148 virtual ~CustomButtonStateChangedDelegate() {} 149 virtual void StateChanged(Button::ButtonState state) = 0; 150 151 protected: CustomButtonStateChangedDelegate()152 CustomButtonStateChangedDelegate() {} 153 154 private: 155 DISALLOW_COPY_AND_ASSIGN(CustomButtonStateChangedDelegate); 156 }; 157 158 } // namespace views 159 160 #endif // UI_VIEWS_CONTROLS_BUTTON_CUSTOM_BUTTON_H_ 161