summaryrefslogtreecommitdiffstats
path: root/views/controls/button/custom_button.h
blob: 5801401ab696a3c8c67dce7d9433ed8e7807c411 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef VIEWS_CONTROLS_BUTTON_CUSTOM_BUTTON_H_
#define VIEWS_CONTROLS_BUTTON_CUSTOM_BUTTON_H_

#include "app/animation.h"
#include "views/controls/button/button.h"

class ThrobAnimation;

namespace views {

// A button with custom rendering. The common base class of ImageButton and
// TextButton.
// Note that this type of button is not focusable by default and will not be
// part of the focus chain.  Call SetFocusable(true) to make it part of the
// focus chain.
class CustomButton : public Button,
                     public AnimationDelegate {
 public:
  virtual ~CustomButton();

  // Possible states
  enum ButtonState {
    BS_NORMAL = 0,
    BS_HOT,
    BS_PUSHED,
    BS_DISABLED,
    BS_COUNT
  };

  // Get/sets the current display state of the button.
  ButtonState state() const { return state_; }
  void SetState(ButtonState state);

  // Starts throbbing. See HoverAnimation for a description of cycles_til_stop.
  void StartThrobbing(int cycles_til_stop);

  // Set how long the hover animation will last for.
  void SetAnimationDuration(int duration);

  // Overridden from View:
  virtual void SetEnabled(bool enabled);
  virtual bool IsEnabled() const;
  virtual bool IsFocusable() const;

  void set_triggerable_event_flags(int triggerable_event_flags) {
    triggerable_event_flags_ = triggerable_event_flags;
  }

  int triggerable_event_flags() const {
    return triggerable_event_flags_;
  }

  // Sets whether |RequestFocus| should be invoked on a mouse press. The default
  // is true.
  void set_request_focus_on_press(bool value) {
    request_focus_on_press_ = value;
  }
  bool request_focus_on_press() const { return request_focus_on_press_; }

 protected:
  // Construct the Button with a Listener. See comment for Button's ctor.
  explicit CustomButton(ButtonListener* listener);

  // Returns true if the event is one that can trigger notifying the listener.
  // This implementation returns true if the left mouse button is down.
  virtual bool IsTriggerableEvent(const MouseEvent& e);

  // Overridden from View:
  virtual bool AcceleratorPressed(const Accelerator& accelerator);
  virtual bool OnMousePressed(const MouseEvent& e);
  virtual bool OnMouseDragged(const MouseEvent& e);
  virtual void OnMouseReleased(const MouseEvent& e, bool canceled);
  virtual void OnMouseEntered(const MouseEvent& e);
  virtual void OnMouseMoved(const MouseEvent& e);
  virtual void OnMouseExited(const MouseEvent& e);
  virtual bool OnKeyPressed(const KeyEvent& e);
  virtual bool OnKeyReleased(const KeyEvent& e);
  virtual void OnDragDone();
  virtual void ShowContextMenu(const gfx::Point& p, bool is_mouse_gesture);
  virtual void ViewHierarchyChanged(bool is_add, View *parent, View *child);
  virtual void SetHotTracked(bool flag);
  virtual bool IsHotTracked() const;
  virtual void WillLoseFocus();

  // Overridden from AnimationDelegate:
  virtual void AnimationProgressed(const Animation* animation);

  // Returns true if the button should become pressed when the user
  // holds the mouse down over the button. For this implementation,
  // we simply return IsTriggerableEvent(e).
  virtual bool ShouldEnterPushedState(const MouseEvent& e);

  // The button state (defined in implementation)
  ButtonState state_;

  // Hover animation.
  scoped_ptr<ThrobAnimation> hover_animation_;

 private:
  // Set / test whether the button is highlighted (in the hover state).
  void SetHighlighted(bool highlighted);
  bool IsHighlighted() const;

  // Returns whether the button is pushed.
  bool IsPushed() const;

  // Should we animate when the state changes? Defaults to true, but false while
  // throbbing.
  bool animate_on_state_change_;

  // Mouse event flags which can trigger button actions.
  int triggerable_event_flags_;

  // See description above setter.
  bool request_focus_on_press_;

  DISALLOW_COPY_AND_ASSIGN(CustomButton);
};

}  // namespace views

#endif  // VIEWS_CONTROLS_BUTTON_CUSTOM_BUTTON_H_