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_LABEL_BUTTON_H_
6#define UI_VIEWS_CONTROLS_BUTTON_LABEL_BUTTON_H_
7
8#include "base/compiler_specific.h"
9#include "base/memory/scoped_ptr.h"
10#include "third_party/skia/include/core/SkColor.h"
11#include "ui/gfx/font.h"
12#include "ui/gfx/image/image_skia.h"
13#include "ui/views/controls/button/custom_button.h"
14#include "ui/views/controls/image_view.h"
15#include "ui/views/controls/label.h"
16#include "ui/views/native_theme_delegate.h"
17
18namespace views {
19
20class Painter;
21
22// LabelButton is an alternative to TextButton, it's not focusable by default.
23class VIEWS_EXPORT LabelButton : public CustomButton,
24                                 public NativeThemeDelegate {
25 public:
26  // The length of the hover fade animation.
27  static const int kHoverAnimationDurationMs;
28
29  static const char kViewClassName[];
30
31  LabelButton(ButtonListener* listener, const string16& text);
32  virtual ~LabelButton();
33
34  // Get or set the image shown for the specified button state.
35  // GetImage returns the image for STATE_NORMAL if the state's image is empty.
36  virtual const gfx::ImageSkia& GetImage(ButtonState for_state);
37  void SetImage(ButtonState for_state, const gfx::ImageSkia& image);
38
39  // Get or set the text shown on the button.
40  const string16& GetText() const;
41  void SetText(const string16& text);
42
43  // Set the text color shown for the specified button state.
44  void SetTextColor(ButtonState for_state, SkColor color);
45
46  // Get or set the text's multi-line property to break on '\n', etc.
47  bool GetTextMultiLine() const;
48  void SetTextMultiLine(bool text_multi_line);
49
50  // Get or set the font used by this button.
51  const gfx::Font& GetFont() const;
52  void SetFont(const gfx::Font& font);
53
54  // Set the elide behavior of this button.
55  void SetElideBehavior(Label::ElideBehavior elide_behavior);
56
57  // Get or set the horizontal alignment used for the button; reversed in RTL.
58  // The optional image will lead the text, unless the button is right-aligned.
59  gfx::HorizontalAlignment GetHorizontalAlignment() const;
60  void SetHorizontalAlignment(gfx::HorizontalAlignment alignment);
61
62  // Call set_min_size(gfx::Size()) to clear the monotonically increasing size.
63  void set_min_size(const gfx::Size& min_size) { min_size_ = min_size; }
64  void set_max_size(const gfx::Size& max_size) { max_size_ = max_size; }
65
66  // Get or set the option to handle the return key; false by default.
67  bool is_default() const { return is_default_; }
68  void SetIsDefault(bool is_default);
69
70  // Get or set the button's overall style; the default is |STYLE_TEXTBUTTON|.
71  ButtonStyle style() const { return style_; }
72  void SetStyle(ButtonStyle style);
73
74  void SetFocusPainter(scoped_ptr<Painter> focus_painter);
75
76  // View:
77  virtual gfx::Size GetPreferredSize() OVERRIDE;
78  virtual void Layout() OVERRIDE;
79  virtual const char* GetClassName() const OVERRIDE;
80
81 protected:
82  ImageView* image() const { return image_; }
83  Label* label() const { return label_; }
84
85  // View:
86  virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
87  virtual void OnFocus() OVERRIDE;
88  virtual void OnBlur() OVERRIDE;
89
90  // Fill |params| with information about the button.
91  virtual void GetExtraParams(ui::NativeTheme::ExtraParams* params) const;
92
93  // Resets colors from the NativeTheme, explicitly set colors are unchanged.
94  virtual void ResetColorsFromNativeTheme();
95
96  // Updates the image view to contain the appropriate button state image.
97  void UpdateImage();
98
99  // NativeThemeDelegate:
100  virtual gfx::Rect GetThemePaintRect() const OVERRIDE;
101
102 private:
103  FRIEND_TEST_ALL_PREFIXES(LabelButtonTest, Init);
104  FRIEND_TEST_ALL_PREFIXES(LabelButtonTest, Label);
105  FRIEND_TEST_ALL_PREFIXES(LabelButtonTest, Image);
106  FRIEND_TEST_ALL_PREFIXES(LabelButtonTest, LabelAndImage);
107  FRIEND_TEST_ALL_PREFIXES(LabelButtonTest, Font);
108
109  // CustomButton:
110  virtual void StateChanged() OVERRIDE;
111
112  // View:
113  virtual void ChildPreferredSizeChanged(View* child) OVERRIDE;
114  virtual void OnNativeThemeChanged(const ui::NativeTheme* theme) OVERRIDE;
115
116  // NativeThemeDelegate:
117  virtual ui::NativeTheme::Part GetThemePart() const OVERRIDE;
118  virtual ui::NativeTheme::State GetThemeState(
119      ui::NativeTheme::ExtraParams* params) const OVERRIDE;
120  virtual const gfx::Animation* GetThemeAnimation() const OVERRIDE;
121  virtual ui::NativeTheme::State GetBackgroundThemeState(
122      ui::NativeTheme::ExtraParams* params) const OVERRIDE;
123  virtual ui::NativeTheme::State GetForegroundThemeState(
124      ui::NativeTheme::ExtraParams* params) const OVERRIDE;
125
126  // The image and label shown in the button.
127  ImageView* image_;
128  Label* label_;
129
130  // The images and colors for each button state.
131  gfx::ImageSkia button_state_images_[STATE_COUNT];
132  SkColor button_state_colors_[STATE_COUNT];
133
134  // Used to track whether SetTextColor() has been invoked.
135  bool explicitly_set_colors_[STATE_COUNT];
136
137  // |min_size_| increases monotonically with the preferred size.
138  gfx::Size min_size_;
139  // |max_size_| may be set to clamp the preferred size.
140  gfx::Size max_size_;
141
142  // Flag indicating default handling of the return key via an accelerator.
143  // Whether or not the button appears or behaves as the default button in its
144  // current context;
145  bool is_default_;
146
147  // The button's overall style.
148  ButtonStyle style_;
149
150  scoped_ptr<Painter> focus_painter_;
151
152  DISALLOW_COPY_AND_ASSIGN(LabelButton);
153};
154
155}  // namespace views
156
157#endif  // UI_VIEWS_CONTROLS_BUTTON_LABEL_BUTTON_H_
158