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